?login_element?

Subversion Repositories NedoOS

Rev

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