?login_element?

Subversion Repositories NedoOS

Rev

Rev 1618 | Rev 1651 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. unsigned char netbuf[1452];
  13. unsigned char picture[16384];
  14. unsigned char picId[16];
  15. unsigned char picType[32];
  16. unsigned char picName[255];
  17. unsigned char picYear[8];
  18. unsigned char picRating[8];
  19. unsigned char crlf[2] = {13, 10};
  20. unsigned long bytecount;
  21. unsigned char status, key;
  22. struct sockaddr_in targetadr;
  23. struct readstructure readStruct;
  24. unsigned long contLen;
  25.  
  26. extern void dns_resolve(void);
  27.  
  28. void errorPrint(unsigned int error)
  29. {
  30.   switch (error)
  31.   {
  32.   case 2:
  33.     printf("02 SHUT_RDWR\n\r");
  34.     break;
  35.   case 4:
  36.     printf("04 ERR_INTR\n\r");
  37.     break;
  38.   case 23:
  39.     printf("23 ERR_NFILE\n\r");
  40.     break;
  41.   case 35:
  42.     printf("35 ERR_EAGAIN or ERR_EWOULDBLOCK\n\r");
  43.     break;
  44.   case 37:
  45.     printf("37 ERR_ALREADY\n\r");
  46.     break;
  47.   case 38:
  48.     printf("38 ERR_NOTSOCK\n\r");
  49.     break;
  50.   case 40:
  51.     printf("40 ERR_EMSGSIZE\n\r");
  52.     break;
  53.   case 41:
  54.     printf("41 ERR_PROTOTYPE\n\r");
  55.     break;
  56.   case 47:
  57.     printf("47 ERR_AFNOSUPPORT\n\r");
  58.     break;
  59.   case 53:
  60.     printf("53 ERR_ECONNABORTED\n\r");
  61.     break;
  62.   case 54:
  63.     printf("54 ERR_CONNRESET\n\r");
  64.     break;
  65.   case 57:
  66.     printf("57 ERR_NOTCONN\n\r");
  67.     break;
  68.   case 65:
  69.     printf("65 ERR_HOSTUNREACH\n\r");
  70.     break;
  71.   default:
  72.     printf("%u UNKNOWN ERROR\n\r", error);
  73.     break;
  74.   }
  75.   YIELD();
  76.   //    do {key = _low_level_get();} while (key == 0);
  77. }
  78.  
  79. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  80. {
  81.   unsigned char socket, retry = 100;
  82.   unsigned int todo;
  83.   todo = OS_NETSOCKET((family << 8) + protocol);
  84.   if (todo > 32767)
  85.   {
  86.     printf("OS_NETSOCKET: ");
  87.     errorPrint(todo & 255);
  88.     exit(0);
  89.   }
  90.   else
  91.   {
  92.     socket = ((todo & 65280) >> 8);
  93.     // printf ("OS_NETSOCKET: Socket #%d created\n\r", socket);
  94.   }
  95.   return socket;
  96. }
  97.  
  98. unsigned char netConnect(unsigned char socket)
  99. {
  100.   unsigned int todo;
  101.  
  102.   targetadr.family = AF_INET;
  103.   targetadr.porth = 00;
  104.   targetadr.portl = 80;
  105.   targetadr.b1 = 217;
  106.   targetadr.b2 = 146;
  107.   targetadr.b3 = 69;
  108.   targetadr.b4 = 13;
  109.  
  110.   todo = OS_NETCONNECT(socket, &targetadr);
  111.   if (todo > 32767)
  112.   {
  113.     printf("OS_NETCONNECT: ");
  114.     errorPrint(todo & 255);
  115.     exit(0);
  116.   }
  117.   else
  118.   {
  119.     // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  120.   }
  121.   return 0;
  122. }
  123.  
  124. unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
  125. {
  126.   unsigned char retry = 100;
  127.   unsigned int todo;
  128.   readStruct.socket = socket;
  129.   readStruct.BufAdr = messageadr;
  130.   readStruct.bufsize = size;
  131.   readStruct.protocol = SOCK_STREAM;
  132.  
  133. wizwrite:
  134.   todo = OS_WIZNETWRITE(&readStruct);
  135.   if (todo > 32767)
  136.   {
  137.     printf("OS_WIZNETWRITE: ");
  138.     errorPrint(todo & 255);
  139.     if (retry == 0)
  140.     {
  141.       exit(0);
  142.     }
  143.     retry--;
  144.     goto wizwrite;
  145.   }
  146.   else
  147.   {
  148.     //  printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  149.   }
  150.   return todo;
  151. }
  152.  
  153. unsigned int tcpRead(unsigned char socket)
  154. {
  155.   unsigned char retry = 50;
  156.   unsigned int err, todo;
  157.  
  158.   readStruct.socket = socket;
  159.   readStruct.BufAdr = (unsigned int)&netbuf;
  160.   readStruct.bufsize = sizeof(netbuf);
  161.   readStruct.protocol = SOCK_STREAM;
  162. wizread:
  163.   todo = OS_WIZNETREAD(&readStruct);
  164.   err = todo & 255;
  165.   if (todo > 32767)
  166.   {
  167.     YIELD();
  168.   if (bytecount == 0) return 0;
  169.     retry--;
  170.     if (retry == 0)
  171.     {
  172.       if (err == ERR_EAGAIN)
  173.       {
  174.         todo = 0;
  175.         return todo;
  176.       }
  177.       printf("OS_WIZNETREAD: ");
  178.       errorPrint(err);
  179.       exit(0);
  180.     }
  181.     goto wizread;
  182.   }
  183.   // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  184.   return todo;
  185. }
  186.  
  187. unsigned int cutHeader(unsigned int todo)
  188. {
  189.   unsigned int q, headlng;
  190.   unsigned char *count;
  191.   count = strstr(netbuf, "Content-Length:");
  192.   if (count == NULL)
  193.   {
  194.     printf("contLen  not found \r\n");
  195.     contLen = 0;
  196.   }
  197.   else
  198.   {
  199.  
  200.     contLen = atol(count + 15);
  201.           bytecount = contLen;
  202.     //printf ("Dlinna  soderzhimogo = %lu \n\r", bytecount);
  203.   }
  204.  
  205.   count = strstr(netbuf, "\r\n\r\n");
  206.   if (count == NULL)
  207.   {
  208.     printf("header not found\r\n");
  209.   }
  210.   else
  211.   {
  212.     headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
  213.     q = todo - headlng;
  214.     memcpy(&netbuf, count + 4, q);
  215.     // printf ("header removed. %u bytes\r\n", headlng);
  216.   }
  217.  
  218.   return q;
  219. }
  220.  
  221. unsigned int netShutDown(unsigned char socket)
  222. {
  223.   unsigned int todo;
  224.   todo = OS_NETSHUTDOWN(socket);
  225.   if (todo > 32767)
  226.   {
  227.     printf("OS_NETSHUTDOWN: ");
  228.     errorPrint(todo & 255);
  229.     return 255;
  230.   }
  231.   else
  232.   {
  233.     // printf ("Socket #%u closed.\n\r", socket);
  234.   }
  235.   return 0;
  236. }
  237.  
  238. void fillPicture(unsigned char socket)
  239. {
  240.   unsigned int todo, w, pPos, q, headskip;
  241.  
  242.   headskip = 0;
  243.   pPos = 0;
  244.   bytecount = 255;
  245.   while (1)
  246.   {
  247.       todo = tcpRead(socket);
  248.         if (todo == 0)
  249.     {
  250.       break;
  251.     }
  252.     q = todo;
  253.     if (headskip == 0)
  254.     {
  255.       headskip = 1;
  256.       q = cutHeader(todo);
  257.     }
  258.     for (w = 0; w < q; w++)
  259.     {
  260.       picture[w + pPos] = netbuf[w];
  261.     }
  262.       bytecount = bytecount - q;
  263.       if (pPos > sizeof(picture))
  264.     {
  265.       printf("Picture overrun... \n\r");
  266.       break;
  267.     } // 1.1
  268.     pPos = pPos + q;
  269.   }
  270.   netShutDown(socket);
  271. }
  272.  
  273. unsigned char getPic(unsigned long fileId)
  274. {
  275.   unsigned int todo;
  276.   unsigned char cmdlist1[] = "GET \/file\/id:";
  277.   unsigned char cmdlist2[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0";
  278.   unsigned char buffer[] = "0000000000";
  279.   unsigned char socket;
  280.   socket = OpenSock(AF_INET, SOCK_STREAM);
  281.   todo = netConnect(socket);
  282.   netbuf[0] = '\0';
  283.   sprintf(buffer, "%lu", fileId);
  284.   strcat(netbuf, cmdlist1);
  285.   strcat(netbuf, buffer);
  286.   strcat(netbuf, cmdlist2);
  287.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  288.   fillPicture(socket);
  289.   return 0;
  290. }
  291. unsigned char savePic(unsigned long fileId)
  292. {
  293.   FILE *fp2;
  294.   unsigned char fileName[32];
  295.   unsigned char buffer[] = "0000000000";
  296.   strcpy(fileName, "zxart_");
  297.   sprintf(buffer, "%lu", fileId);
  298.   strcat(fileName, buffer);
  299.   strcat(fileName, ".scr");
  300.  
  301.   fp2 = OS_CREATEHANDLE(fileName, 0x80);
  302.   if (((int)fp2) & 0xff)
  303.   {
  304.     printf(fileName);
  305.     printf(" creating error\r\n");
  306.     exit(0);
  307.   }
  308.   OS_WRITEHANDLE(picture, fp2, 6912);
  309.   OS_CLOSEHANDLE(fp2);
  310.   return 0;
  311. }
  312.  
  313. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  314. {
  315.   unsigned int i, j;
  316.   unsigned int lenC, lenS;
  317.  
  318.   for (lenC = 0; c[lenC]; lenC++)
  319.     ;
  320.   for (lenS = 0; s[lenS]; lenS++)
  321.     ;
  322.  
  323.   for (i = startPos; i <= lenS - lenC; i++)
  324.   {
  325.     for (j = 0; s[i + j] == c[j]; j++)
  326.       ;
  327.  
  328.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  329.       return i;
  330.     if (j == lenC)
  331.       if (n - 1)
  332.         n--;
  333.       else
  334.         return i;
  335.   }
  336.   return -1;
  337. }
  338.  
  339. const char *parseJson(unsigned char *property)
  340. {
  341.   unsigned int w, lng, lngp1, findEnd, listPos;
  342.   unsigned char terminator;
  343.   int n;
  344.   n = -1;
  345.   netbuf[0] = '\0';
  346.   n = pos(picture, property, 1, 0);
  347.   if (n == -1)
  348.   {
  349.     strcpy(netbuf, "0\0");
  350.     return netbuf;
  351.   }
  352.   lng = n - 1 + strlen(property);
  353.   if (picture[lng] == ':')
  354.   {
  355.     terminator = '\0';
  356.   }
  357.   if (picture[lng] == '\"')
  358.   {
  359.     terminator = '\"';
  360.   }
  361.   if (picture[lng] == '[')
  362.   {
  363.     terminator = ']';
  364.   }
  365.  
  366.   findEnd = 1;
  367.   lngp1 = lng + 1;
  368.  
  369.   while (42)
  370.   {
  371.  
  372.     if ((picture[lngp1 + findEnd] == ','))
  373.     {
  374.       if (terminator == '\0')
  375.       {
  376.         break;
  377.       }
  378.       if ((picture[lng + findEnd] == terminator))
  379.       {
  380.         findEnd--;
  381.         break;
  382.       }
  383.     }
  384.     findEnd++;
  385.   }
  386.   listPos = 0;
  387.   for (w = lngp1; w < findEnd + lngp1; w++)
  388.   {
  389.     netbuf[listPos] = picture[w];
  390.     listPos++;
  391.   }
  392.   netbuf[listPos] = '\0';
  393.   return netbuf;
  394. }
  395. void convert866(void)
  396. {
  397.   unsigned int lng, targetPos, w, q = 0;
  398.   unsigned char buffer[8], one, two;
  399.   unsigned int decVal;
  400.   lng = strlen(netbuf);
  401.   targetPos = lng + 1;
  402.  
  403.   while (q < lng)
  404.   {
  405.     one = netbuf[q];
  406.     two = netbuf[q + 1];
  407.     if (one == 92 && two == 117)
  408.     {
  409.       q = q + 2;
  410.       for (w = 0; w < 4; w++)
  411.       {
  412.         buffer[w] = netbuf[q + w];
  413.       }
  414.       q = q + 4;
  415.       buffer[4] = '\0';
  416.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  417.  
  418.       if (decVal < 1088)
  419.       {
  420.         decVal = decVal - 912;
  421.       }
  422.       if (decVal > 1087)
  423.       {
  424.         decVal = decVal - 864;
  425.       }
  426.       if (decVal == 1025)
  427.       {
  428.         decVal = 240;
  429.       }
  430.       if (decVal == 1105)
  431.       {
  432.         decVal = 241;
  433.       }
  434.       netbuf[targetPos] = decVal;
  435.     }
  436.     else
  437.     {
  438.       netbuf[targetPos] = netbuf[q];
  439.       q++;
  440.     }
  441.     targetPos++;
  442.   }
  443.   netbuf[targetPos] = '\0';
  444.  
  445.   for (w = lng + 1; w < targetPos + 1; w++)
  446.   {
  447.     netbuf[w - lng - 1] = netbuf[w];
  448.   }
  449. }
  450.  
  451. unsigned long processJson(unsigned long startPos, unsigned char limit)
  452. {
  453.   unsigned int retry;
  454.   unsigned int todo, pPos, headskip;
  455.   unsigned char cmdlist1[] = "GET /api/export:zxPicture\/filter:zxPicture\/limit:";
  456.   unsigned char cmdlist2[] = "\/start:";
  457.   unsigned char cmdlist3[] = "\/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0";
  458.   unsigned char buffer[] = "000000000";
  459.   unsigned char *count, socket;
  460.   unsigned long idpic;
  461.   retry = 5;
  462. rejson:
  463.   socket = OpenSock(AF_INET, SOCK_STREAM);
  464.   netConnect(socket);
  465.  
  466.   netbuf[0] = '\0';
  467.   strcat(netbuf, cmdlist1);
  468.   sprintf(buffer, "%u", limit);
  469.   strcat(netbuf, buffer);
  470.   strcat(netbuf, cmdlist2);
  471.   sprintf(buffer, "%lu", startPos);
  472.   strcat(netbuf, buffer);
  473.   strcat(netbuf, cmdlist3);
  474.  
  475.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  476.  
  477.   headskip = 0;
  478.   pPos = 0;
  479.   fillPicture(socket);
  480.   count = strstr(picture, "responseStatus\":\"success");
  481.   if (count == NULL)
  482.   {
  483.     printf("BAD JSON, no responseStatus: success\r\n");
  484.     retry--;
  485.     YIELD();
  486.     if (retry > 0)
  487.       goto rejson;
  488.     exit(0);
  489.   }
  490.  
  491.   count = strstr(picture, "\"id\":");
  492.   if (count == NULL)
  493.   {
  494.     printf("BAD JSON: ID not found \r\n");
  495.     exit(0);
  496.   }
  497.   netbuf[0] = '\0';
  498.   picName[0] = '\0';
  499.   picId[0] = '\0';
  500.   picType[0] = '\0';
  501.   picRating[0] = '\0';
  502.   picYear[0] = '\0';
  503.   strcat(picId, parseJson("\"id\":"));
  504.   idpic = atol(netbuf);
  505.   parseJson(",\"title\":\"");
  506.   convert866();
  507.   strcat(picName, netbuf);
  508.   parseJson(",\"type\":\"");
  509.   strcat(picType, netbuf);
  510.   parseJson("\"rating\":\"");
  511.   strcat(picRating, netbuf);
  512.   parseJson("\"year\":\"");
  513.   strcat(picYear, netbuf);
  514.   return idpic;
  515. }
  516.  
  517. C_task main(void)
  518. {
  519.   unsigned char errno, keypress;
  520.   unsigned long iddqd, count, ipadress;
  521.   os_initstdio();
  522.  
  523.   count = 0;
  524.  
  525.   BOX(1, 1, 80, 25, 40);
  526.   AT(1, 1);
  527.   ATRIB(97);
  528.   ATRIB(40);
  529.   printf("              GETPIC 1.4 zxart.ee picture viewer for nedoNET\n\r");
  530.   ATRIB(33);
  531.   ATRIB(40);
  532.   printf(" Управление:\n\r");
  533.   printf("      'ESC' - выход из программы;\n\r");
  534.   printf("      '<-' или 'B' к последним картинкам;\n\r");
  535.   printf("      '->' или 'Пробел' к более старым картинкам\n\r");
  536.   printf("      'J' Прыжок на  указанную по счету картинку,<15000\n\r");
  537.   printf("      'I' Просмотр экрана информации о картинках\n\r");
  538.   printf("      'S' Сохранить картинку на диск в текущую папку\n\r");
  539.   printf("      ----------------Нажмите любую кнопку----------------");
  540.  
  541. //  ipadress = OS_DNSRESOLVE(44);
  542. //  printf("\n\r  OS_DNSRESOLVE =  %lu \n\r", ipadress);
  543.  
  544.   do
  545.   {
  546.     key = _low_level_get();
  547.   } while (key == 0);
  548.  
  549.   AT(1, 10);
  550.  
  551. start:
  552.   iddqd = processJson(count, 1);
  553.   ATRIB(97);
  554.   printf("#:%lu ID:%s   TITLE:%s\r\n", count, picId, picName);
  555.   ATRIB(93);
  556.   printf(" RATING:%s    YEAR:%s \r\n", picRating, picYear);
  557.  
  558.   if (!strcmp(picType, "standard"))
  559.  
  560.   {
  561.     errno = getPic(iddqd);
  562.   review:
  563.     keypress = viewScreen6912((unsigned int)&picture);
  564.   }
  565.   else
  566.   {
  567.     printf("  >>Format %s not supported, skipped \n\r", picType);
  568.     count++;
  569.     goto start;
  570.   }
  571.  
  572.   if (keypress == 's' || keypress == 'S')
  573.   {
  574.     savePic(iddqd);
  575.     printf("        ID:%s    TITLE:%s  SAVED\r\n", picId, picName);
  576.     count++;
  577.   }
  578.  
  579.   if (keypress == 27)
  580.   {
  581.     printf("Good bye...\r\n");
  582.     ATRIB(37);
  583.     ATRIB(40);
  584.     exit(0);
  585.   }
  586.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  587.   {
  588.     if (count > 0)
  589.     {
  590.       count--;
  591.     }
  592.   }
  593.  
  594.   if (keypress == 251 || keypress == 32)
  595.   {
  596.     count++;
  597.   }
  598.  
  599.   if (keypress == 'j' || keypress == 'J')
  600.   {
  601.     printf("Jump to picture:");
  602.     scanf("%lu", &count);
  603.   }
  604.  
  605.   if (keypress == 'i' || keypress == 'I')
  606.   {
  607.     do
  608.     {
  609.       key = _low_level_get();
  610.     } while (key == 0);
  611.     goto review;
  612.   }
  613.   goto start;
  614. }
  615.