?login_element?

Subversion Repositories NedoOS

Rev

Rev 1701 | Rev 1811 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

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