?login_element?

Subversion Repositories NedoOS

Rev

Rev 1462 | Rev 1491 | 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. unsigned char netbuf[1452];
  13. unsigned char picture[7000];
  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 > 6912) {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 q, 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)  { printf("Property not found...\n\r");}
  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.   const char *titleptr;
  408.   unsigned long idpic, bytecount;
  409.  
  410.   socket = OpenSock(AF_INET, SOCK_STREAM);
  411.   netConnect (socket);
  412.    
  413.           netbuf[0] = '\0';
  414.       strcat (netbuf, cmdlist1);
  415.           sprintf(buffer, "%u", limit);
  416.           strcat (netbuf, buffer);
  417.           strcat (netbuf, cmdlist2);
  418.           sprintf(buffer, "%lu", startPos);
  419.           strcat (netbuf, buffer);
  420.           strcat (netbuf, cmdlist3);
  421.              
  422.           todo = tcpSend (socket, (unsigned int)&netbuf, strlen(netbuf));
  423.          
  424.          
  425.   headskip = 0;
  426.   pPos = 0;
  427.   fillPicture(socket);
  428.   count = strstr (picture, "responseStatus\":\"success");
  429.   if ( count == NULL)
  430.   {
  431.     printf ("BAD JSON no(responseStatus"":""success)\r\n");
  432.  
  433.         exit(0);
  434.   }
  435.  
  436.   count = strstr (picture, "\"id\":");
  437.   if ( count == NULL)
  438.   {
  439.     printf ("BAD JSON: ID not found \r\n");
  440.         exit(0);
  441.   }
  442.  
  443.  
  444.        
  445.   netbuf        [0] = '\0';
  446.   picName       [0] = '\0';
  447.   picId         [0] = '\0';
  448.   picType       [0] = '\0';
  449.   picRating [0] = '\0';
  450.   picYear       [0] = '\0';
  451.   strcat (picId, parseJson("\"id\":"));
  452.   idpic = atol (netbuf);
  453.   parseJson(",\"title\":\"");
  454.   convert866();
  455.   strcat (picName, netbuf);
  456.   parseJson(",\"type\":\"");
  457.   strcat (picType, netbuf) ;
  458.   parseJson("\"rating\":\"");
  459.   strcat (picRating, netbuf) ;
  460.   parseJson("\"year\":\"");
  461.   strcat (picYear, netbuf) ;
  462.  
  463.   /*
  464.   "type":"standard",
  465.   strcat (piclist, netbuf) ;
  466.   strcat (piclist, "\n\r");
  467.   strcat (piclist, parseJson ("\"dateCreated\":"));
  468.   strcat (piclist, "\n\r");
  469.   strcat (piclist, parseJson("\"authorIds\":["));
  470.   strcat (piclist, "\n\r");
  471.   strcat (piclist, parseJson("\"rating\":\""));
  472.   strcat (piclist, "\n\r");
  473.   strcat (piclist, parseJson("\"views\":\""));
  474.   strcat (piclist, "\n\r");
  475.   strcat (piclist, parseJson("\"year\":\""));
  476.   strcat (piclist, "\n\r");
  477. */
  478.  
  479. return idpic;
  480. }
  481.  
  482. C_task main (void)
  483. {
  484.   unsigned char errno,keypress;
  485.   unsigned long iddqd, count;
  486.   os_initstdio();
  487.   piclist[0] = '\0';
  488.  
  489.         BOX(1, 1, 80, 8, 40);
  490.         BOX(1, 9, 80, 17, 47);
  491.        
  492.         AT(1,1);
  493.        
  494.         ATRIB(33);
  495.         ATRIB(40);
  496.         count = 0;
  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.         do {key = _low_level_get();} while (key == 0);
  504.         ATRIB(30);
  505.         ATRIB(47);
  506.         AT(1,8);
  507. start:
  508.  
  509.         piclist[0] = '\0';
  510.         iddqd = processJson(count, 1);
  511.         printf(" ID:%s  TITLE:%s\r\n",picId, picName);
  512.         printf(" RATING:%s      YEAR:%s \r\n", picRating, picYear);
  513.  
  514.         if(!strcmp(picType, "standard"))
  515.        
  516.         {
  517.                 errno = getPic(iddqd);
  518.                 keypress = viewScreen6912((unsigned int)&picture);
  519.         }
  520.         else {printf("  >>Format %s not supported, skipped \n\r", picType);count++; goto start;}
  521.  
  522. if (keypress == 's' || keypress == 'S')  
  523. {  
  524.         savePic(iddqd);
  525.         printf("        ID:%s    TITLE:%s  SAVED\r\n",picId, picName);
  526. }
  527.  
  528. if (keypress == 27)  
  529. {  
  530.        
  531.         printf("Good bye...\r\n");
  532.         ATRIB(37);
  533.         ATRIB(40);
  534.         exit(0);
  535. }
  536. if (keypress == 248 || keypress == 'b' || keypress == 'B')  
  537. {  
  538.         if (count > 0) {count--;}
  539. }
  540.  
  541. if (keypress == 251|| keypress == 32)  
  542. {  
  543.         count++;
  544. }
  545.  
  546. if (keypress == 'j' || keypress == 'J')  
  547. {  
  548. printf("Jump to picture:");
  549. scanf ("%lu", &count);
  550. }
  551.  
  552. if (keypress == 'i' || keypress == 'I')  
  553. {  
  554. do {key = _low_level_get();} while (key == 0);
  555. }
  556.  
  557.  
  558. goto start;
  559.  
  560. }
  561.  
  562.  
  563.