?login_element?

Subversion Repositories NedoOS

Rev

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