?login_element?

Subversion Repositories NedoOS

Rev

Rev 2023 | Rev 2026 | 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.  
  13. unsigned int RBR_THR = 0xF8EF;
  14. unsigned int IER = 0xF9EF;
  15. unsigned int IIR_FCR = 0xFAEF;
  16. unsigned int LCR = 0xFBEF;
  17. unsigned int MCR = 0xFCEF;
  18. unsigned int LSR = 0xFDEF;
  19. unsigned int MSR = 0xFEEF;
  20. unsigned int SR = 0xFFEF;
  21. unsigned int divider = 4;
  22.  
  23. struct fileStruct
  24. {
  25.   long picId;
  26.   unsigned int picYear;
  27.   unsigned long totalAmount;
  28.   unsigned char picRating[8];
  29.   unsigned char picName[256];
  30.   unsigned char picType[64];
  31.   unsigned char authorIds[64];
  32.   unsigned char authorTitle[64];
  33.   unsigned char authorRealName[64];
  34.   unsigned char afn[128];
  35.   unsigned char pfn[128];
  36.   unsigned char fileName[128];
  37. } curFileStruct;
  38.  
  39. unsigned char ver[] = "2.6";
  40. unsigned char netbuf[2048];
  41. unsigned char picture[16384];
  42. unsigned char crlf[2] = {13, 10};
  43. unsigned char keypress, verbose, randomPic, slideShow, netDriver;
  44. struct sockaddr_in targetadr;
  45. struct readstructure readStruct;
  46. unsigned long contLen;
  47. unsigned long count;
  48. unsigned int headlng;
  49. unsigned int slideShowTime = 0;
  50. unsigned int loaded;
  51.  
  52. struct packetStruct
  53. {
  54.   unsigned long contLen;
  55.   unsigned int headlng;
  56. } pack;
  57.  
  58. void emptyKeys(void)
  59. {
  60.   unsigned char loop, key;
  61.   do
  62.   {
  63.     key = _low_level_get();
  64.     if (loop > 64)
  65.     {
  66.       break;
  67.     }
  68.     loop++;
  69.   } while (key != 0);
  70. }
  71.  
  72. void printHelp(void)
  73. {
  74.   ATRIB(95);
  75.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  76.   ATRIB(33);
  77.   ATRIB(40);
  78.   printf("-------------------------------------------------------\n\r");
  79.   printf(" Управление:\n\r");
  80.   printf(" 'ESC' - выход из программы;\n\r");
  81.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  82.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  83.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  84.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  85.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  86.   printf(" 'V' не выводить информацию об авторах\n\r");
  87.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  88.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  89.   printf(" 'D' Переключение режима ZXNETUSB/ESP32\n\r");
  90.   printf(" 'H' Данная справочная информация\n\r");
  91.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  92.   ATRIB(93);
  93.   keypress = getchar();
  94. }
  95. void delay(unsigned long counter)
  96. {
  97.   unsigned long start, finish;
  98.   counter = counter / 20;
  99.   if (counter < 1)
  100.   {
  101.     counter = 1;
  102.   }
  103.   start = time();
  104.   finish = start + counter;
  105.  
  106.   while (start < finish)
  107.   {
  108.     start = time();
  109.   }
  110. }
  111.  
  112. unsigned int httpError(void)
  113. {
  114.   unsigned char *httpRes;
  115.   unsigned int httpErr;
  116.   httpRes = strstr(netbuf, "HTTP/1.1 ");
  117.  
  118.   if (httpRes != NULL)
  119.   {
  120.     httpErr = atol(httpRes + 9);
  121.   }
  122.   else
  123.   {
  124.     httpErr = 0;
  125.   }
  126.   return httpErr;
  127. }
  128.  
  129. void errorPrint(unsigned int error)
  130. {
  131.   switch (error)
  132.   {
  133.   case 2:
  134.     printf("02 SHUT_RDWR");
  135.     break;
  136.   case 4:
  137.     printf("04 ERR_INTR");
  138.     break;
  139.   case 23:
  140.     printf("23 ERR_NFILE");
  141.     break;
  142.   case 35:
  143.     printf("35 ERR_EAGAIN");
  144.     break;
  145.   case 37:
  146.     printf("37 ERR_ALREADY");
  147.     break;
  148.   case 38:
  149.     printf("38 ERR_NOTSOCK");
  150.     break;
  151.   case 40:
  152.     printf("40 ERR_EMSGSIZE");
  153.     break;
  154.   case 41:
  155.     printf("41 ERR_PROTOTYPE");
  156.     break;
  157.   case 47:
  158.     printf("47 ERR_AFNOSUPPORT");
  159.     break;
  160.   case 53:
  161.     printf("53 ERR_ECONNABORTED");
  162.     break;
  163.   case 54:
  164.     printf("54 ERR_CONNRESET");
  165.     break;
  166.   case 57:
  167.     printf("57 ERR_NOTCONN");
  168.     break;
  169.   case 65:
  170.     printf("65 ERR_HOSTUNREACH");
  171.     break;
  172.   default:
  173.     printf("%u UNKNOWN ERROR", error);
  174.     break;
  175.   }
  176. }
  177.  
  178. signed char OpenSock(unsigned char family, unsigned char protocol)
  179. {
  180.   signed char socket;
  181.   unsigned int todo;
  182.   todo = OS_NETSOCKET((family << 8) + protocol);
  183.   if (todo > 32767)
  184.   {
  185.     printf("OS_NETSOCKET: [ERROR:");
  186.     errorPrint(todo & 255);
  187.     printf("] Press any key.");
  188.     getchar();
  189.     exit(0);
  190.   }
  191.   else
  192.   {
  193.     socket = ((todo & 65280) >> 8);
  194.     // printf("OS_NETSOCKET: Socket #%d created\n\r", socket);
  195.   }
  196.   return socket;
  197. }
  198.  
  199. signed char netShutDown(signed char socket, unsigned char type)
  200. {
  201.   unsigned int todo;
  202.   todo = OS_NETSHUTDOWN(socket, type);
  203.   if (todo > 32767)
  204.   {
  205.     printf("OS_NETSHUTDOWN: [ERROR:");
  206.     errorPrint(todo & 255);
  207.     printf("] Press any key.");
  208.     return -1;
  209.   }
  210.   else
  211.   {
  212.     // printf("Socket #%d closed.\n\r", socket);
  213.   }
  214.   return 1;
  215. }
  216.  
  217. unsigned char netConnect(signed char socket)
  218. {
  219.   unsigned int todo, retry = 10;
  220.  
  221.   targetadr.family = AF_INET;
  222.   targetadr.porth = 00;
  223.   targetadr.portl = 80;
  224.   targetadr.b1 = 217;
  225.   targetadr.b2 = 146;
  226.   targetadr.b3 = 69;
  227.   targetadr.b4 = 13;
  228.   while (retry != 0)
  229.   {
  230.     todo = OS_NETCONNECT(socket, &targetadr);
  231.  
  232.     if (todo > 32767)
  233.     {
  234.       retry--;
  235.       printf("OS_NETCONNECT [ERROR:");
  236.       errorPrint(todo & 255);
  237.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  238.       YIELD();
  239.       netShutDown(socket, 0);
  240.       socket = OpenSock(AF_INET, SOCK_STREAM);
  241.     }
  242.     else
  243.     {
  244.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  245.       return 1;
  246.     }
  247.   }
  248.   getchar();
  249.   exit(0);
  250.   return 0;
  251. }
  252.  
  253. unsigned int tcpSend(signed char socket, unsigned int messageadr, unsigned int size)
  254. {
  255.   unsigned char retry = 10;
  256.   unsigned int todo;
  257.   readStruct.socket = socket;
  258.   readStruct.BufAdr = messageadr;
  259.   readStruct.bufsize = size;
  260.   readStruct.protocol = SOCK_STREAM;
  261.   while (retry > 0)
  262.   {
  263.     todo = OS_WIZNETWRITE(&readStruct);
  264.     if (todo > 32767)
  265.     {
  266.       printf("OS_WIZNETWRITE: [ERROR:");
  267.       errorPrint(todo & 255);
  268.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  269.       YIELD();
  270.       retry--;
  271.     }
  272.     else
  273.     {
  274.       // printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  275.       return todo;
  276.     }
  277.   }
  278.  
  279.   getchar();
  280.   exit(0);
  281.   return todo;
  282. }
  283.  
  284. unsigned int tcpRead(signed char socket)
  285. {
  286.   unsigned char retry = 10;
  287.   unsigned int todo;
  288.  
  289.   readStruct.socket = socket;
  290.   readStruct.BufAdr = (unsigned int)&netbuf;
  291.   readStruct.bufsize = sizeof(netbuf);
  292.   readStruct.protocol = SOCK_STREAM;
  293.  
  294.   while (retry > 0)
  295.   {
  296.     todo = OS_WIZNETREAD(&readStruct);
  297.  
  298.     if (todo > 32767)
  299.     {
  300.       if ((todo & 255) != ERR_EAGAIN)
  301.       {
  302.         printf("OS_WIZNETREAD: [ERROR:");
  303.         errorPrint(todo & 255);
  304.         printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  305.         YIELD();
  306.         retry--;
  307.       }
  308.     }
  309.     else
  310.     {
  311.       // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  312.       return todo;
  313.     }
  314.   }
  315.   getchar();
  316.   exit(0);
  317.   return todo;
  318. }
  319. unsigned int cutHeader(unsigned int todo)
  320. {
  321.   unsigned int recAmount, err;
  322.   unsigned char *count1;
  323.  
  324.   err = httpError();
  325.   if (err != 200)
  326.   {
  327.     printf("\r\nHTTP response:[%u]\r\n", err);
  328.     printf("^^^^^^^^^^^^^^^^^^^^^\r\n");
  329.     puts(netbuf);
  330.     getchar();
  331.   }
  332.   count1 = strstr(netbuf, "Content-Length:");
  333.   if (count1 == NULL)
  334.   {
  335.     printf("contLen  not found \r\n");
  336.     contLen = 0;
  337.   }
  338.   else
  339.   {
  340.     contLen = atol(count1 + 15);
  341.     // printf("Content-Length: %lu \n\r", contLen);
  342.   }
  343.  
  344.   count1 = strstr(netbuf, "\r\n\r\n");
  345.   if (count1 == NULL)
  346.   {
  347.     printf("header not found\r\n");
  348.   }
  349.   else
  350.   {
  351.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  352.     recAmount = todo - headlng;
  353.     // printf("header %u bytes\r\n", headlng);
  354.   }
  355.   return recAmount;
  356. }
  357.  
  358. ////////////////////////ESP32 PROCEDURES//////////////////////
  359. void uart_write(unsigned char data)
  360. {
  361.   while ((input(LSR) & 64) == 0)
  362.   {
  363.   }
  364.   output(RBR_THR, data);
  365. }
  366.  
  367. void uart_setrts(unsigned char mode)
  368. {
  369.   switch (mode)
  370.   {
  371.   case 1:
  372.     output(MCR, 2);
  373.     break;
  374.   case 0:
  375.     output(MCR, 0);
  376.     break;
  377.   default:
  378.     disable_interrupt();
  379.     output(MCR, 2);
  380.     output(MCR, 0);
  381.     enable_interrupt();
  382.   }
  383. }
  384.  
  385. void uart_init(unsigned char divisor)
  386. {
  387.   printf("\r\n Initing UART [divider:%u]\r\n", divisor);
  388.   output(MCR, 0x00);        // Disable input
  389.   output(IIR_FCR, 0x87);    // Enable fifo 8 level, and clear it
  390.   output(LCR, 0x83);        // 8n1, DLAB=1
  391.   output(RBR_THR, divisor); // 115200 (divider 1-115200, 3 - 38400)
  392.   output(IER, 0x00);        // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  393.   output(LCR, 0x03);        // 8n1, DLAB=0
  394.   output(IER, 0x00);        // Disable int
  395.   uart_setrts(0);
  396. }
  397.  
  398. unsigned char uart_hasByte(void)
  399. {
  400.   return (1 & input(LSR));
  401. }
  402.  
  403. unsigned char uart_read(void)
  404. {
  405.   uart_setrts(2);
  406.   return input(RBR_THR);
  407. }
  408.  
  409. unsigned char uart_readBlock(void)
  410. {
  411.   while (uart_hasByte() == 0)
  412.   {
  413.     uart_setrts(2);
  414.   }
  415.   return input(RBR_THR);
  416. }
  417.  
  418. void uart_flush(void)
  419. {
  420.   unsigned int count;
  421.   for (count = 0; count < 6000; count++)
  422.   {
  423.     uart_setrts(1);
  424.     uart_read();
  425.   }
  426.   printf("\r\nBuffer cleared.\r\n");
  427. }
  428. void getdata(unsigned int counted)
  429. {
  430.   unsigned int counter;
  431.   for (counter = 0; counter < counted; counter++)
  432.   {
  433.     netbuf[counter] = uart_readBlock();
  434.   }
  435.   netbuf[counter] = 0;
  436. }
  437.  
  438. void sendcommand(char *commandline)
  439. {
  440.   unsigned int count, cmdLen;
  441.   cmdLen = strlen(commandline);
  442.   for (count = 0; count < cmdLen; count++)
  443.   {
  444.     uart_write(commandline[count]);
  445.   }
  446.   uart_write('\r');
  447.   uart_write('\n');
  448.   // printf("Sended:[%s] \r\n", commandline);
  449. }
  450. unsigned char getAnswer(unsigned char skip)
  451. {
  452.   unsigned char readbyte;
  453.   unsigned int curPos = 0;
  454.   while (skip != 0)
  455.   {
  456.     uart_readBlock();
  457.     skip--;
  458.   }
  459.   while (42)
  460.   {
  461.     readbyte = uart_readBlock();
  462.     if (readbyte == 0x0a)
  463.     {
  464.       break;
  465.     }
  466.     netbuf[curPos] = readbyte;
  467.     curPos++;
  468.   }
  469.   netbuf[curPos - 1] = 0;
  470.   // printf("Answer:[%s]\r\n", netbuf);
  471.   return curPos;
  472. }
  473. void espReBoot(void)
  474. {
  475.   unsigned char byte;
  476.   uart_flush();
  477.   sendcommand("AT+RST");
  478.   printf("Resetting ESP...");
  479.   do
  480.   {
  481.     byte = uart_read();
  482.   } while (byte != 'P'); // WIFI GOT IP
  483.   uart_readBlock();      // CR
  484.   uart_readBlock();      // LN
  485.   puts("Reset complete.\r\n");
  486.  
  487.   sendcommand("ATE0");
  488.   do
  489.   {
  490.     byte = uart_read();
  491.   } while (byte != 'K'); // OK
  492.   // puts("Answer:[OK]");
  493.   uart_readBlock(); // CR
  494.   uart_readBlock(); // LN
  495.  
  496.   sendcommand("AT+CIPCLOSE");
  497.   getAnswer(2);
  498.   sendcommand("AT+CIPDINFO=0");
  499.   getAnswer(2);
  500.   sendcommand("AT+CIPMUX=0");
  501.   getAnswer(2);
  502.   sendcommand("AT+CIPSERVER=0");
  503.   getAnswer(2);
  504.   sendcommand("AT+CIPRECVMODE=1");
  505.   getAnswer(2);
  506. }
  507. unsigned int recvHead(void)
  508. {
  509.   unsigned char byte, dataRead = 0;
  510.   do
  511.   {
  512.     byte = uart_readBlock();
  513.     netbuf[dataRead] = byte;
  514.     dataRead++;
  515.   } while (byte != ',');
  516.   netbuf[dataRead] = 0;
  517.   loaded = atoi(netbuf + 13); // <actual_len>
  518.   return loaded;
  519. }
  520. // in netbuf data to send
  521. unsigned int fillPictureEsp(void)
  522. {
  523.   unsigned int packSize = 2000;
  524.   unsigned char cmd[256];
  525.   unsigned char link[512];
  526.   unsigned char sizeLink;
  527.   unsigned long toDownload, downloaded;
  528.   unsigned char byte;
  529.   unsigned int dataSize;
  530.   unsigned char skipHeader;
  531.   strcpy(link, netbuf);
  532.   strcat(link, "\r\n");
  533.   sizeLink = strlen(link);
  534.   sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  535.   getAnswer(2); // CONNECT
  536.   getAnswer(0); // OK
  537.   strcpy(cmd, "AT+CIPSEND=");
  538.   sprintf(netbuf, "%u", sizeLink + 2); // second CRLF in send command
  539.   strcat(cmd, netbuf);
  540.   sendcommand(cmd);
  541.   byte = 0;
  542.   while (byte != '>')
  543.   {
  544.     byte = uart_readBlock();
  545.     // putchar(byte);
  546.   }
  547.   sendcommand(link);
  548.   getAnswer(2); // Recv 132 bytes
  549.   getAnswer(2); // SEND OK
  550.   getAnswer(2); //+IPD,3872
  551.   skipHeader = 0;
  552.   downloaded = 0;
  553.   do
  554.   {
  555.     headlng = 0;
  556.     strcpy(netbuf, "AT+CIPRECVDATA=");
  557.     sprintf(link, "%u", packSize);
  558.     strcat(netbuf, link);
  559.     sendcommand(netbuf);
  560.     dataSize = recvHead();
  561.     getdata(dataSize); // Requested size
  562.     if (skipHeader == 0)
  563.     {
  564.       dataSize = cutHeader(dataSize);
  565.       toDownload = contLen;
  566.       skipHeader = 1;
  567.     }
  568.     downloaded = downloaded + dataSize;
  569.     memcpy(picture + downloaded - dataSize, netbuf + headlng, dataSize);
  570.     toDownload = toDownload - dataSize;
  571.     getAnswer(2); // OK
  572.     if (toDownload > 0)
  573.     {
  574.       getAnswer(2); // +IPD,1824
  575.     }
  576.   } while (toDownload > 0);
  577.   sendcommand("AT+CIPCLOSE");
  578.   getAnswer(0); // CLOSED
  579.   return 0;
  580. }
  581. unsigned char getPicEsp(unsigned long fileId)
  582. {
  583.   unsigned char buffer[] = "0000000000";
  584.   netbuf[0] = '\0';
  585.   sprintf(buffer, "%lu", fileId);
  586.   strcat(netbuf, "GET /file/id:");
  587.   strcat(netbuf, buffer);
  588.   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");
  589.   fillPictureEsp();
  590.   return 0;
  591. }
  592.  
  593. void loadEspConfig(void)
  594. {
  595.   unsigned char curParam[256];
  596.   unsigned char res;
  597.   FILE *espcom;
  598.   OS_SETSYSDRV();
  599.   OS_CHDIR("browser");
  600.   espcom = OS_OPENHANDLE("espcom.ini", 0x80);
  601.   if (((int)espcom) & 0xff)
  602.   {
  603.     printf("mrfesp.ini opening error\r\n");
  604.     return;
  605.   }
  606.  
  607.   OS_READHANDLE(curParam, espcom, 256);
  608.  
  609.   res = sscanf(curParam, "%x %x %x %x %x %x %x %x %u", &RBR_THR, &IER, &IIR_FCR, &LCR, &MCR, &LSR, &MSR, &SR, &divider);
  610.   puts("Config loaded:");
  611.   printf("     RBR_THR:0x%4x\r\n     IER    :0x%4x\r\n     IIR_FCR:0x%4x\r\n     LCR    :0x%4x\r\n", RBR_THR, IER, IIR_FCR, LCR);
  612.   printf("     MCR    :0x%4x\r\n     LSR    :0x%4x\r\n     MSR    :0x%4x\r\n     SR     :0x%4x\r\n", MCR, LSR, MSR, SR);
  613.   printf("     DIVIDER:%4u\r\n", divider);
  614. }
  615.  
  616. ////////////////////////ESP32 PROCEDURES//////////////////////
  617.  
  618. char *str_replace(char *dst, int num, const char *str,
  619.                   const char *orig, const char *rep)
  620. {
  621.   const char *ptr;
  622.   size_t len1 = strlen(orig);
  623.   size_t len2 = strlen(rep);
  624.   char *tmp = dst;
  625.  
  626.   num -= 1;
  627.   while ((ptr = strstr(str, orig)) != NULL)
  628.   {
  629.     num -= (ptr - str) + len2;
  630.     if (num < 1)
  631.       break;
  632.  
  633.     strncpy(dst, str, (size_t)(ptr - str));
  634.     dst += ptr - str;
  635.     strncpy(dst, rep, len2);
  636.     dst += len2;
  637.     str = ptr + len1;
  638.   }
  639.  
  640.   for (; (*dst = *str) && (num > 0); --num)
  641.   {
  642.     ++dst;
  643.     ++str;
  644.   }
  645.   return tmp;
  646. }
  647.  
  648. void fillPicture(signed char socket)
  649. {
  650.   unsigned int todo, w, pPos, headskip;
  651.   headskip = 0;
  652.   pPos = 0;
  653.   while (42)
  654.   {
  655.     headlng = 0;
  656.     todo = tcpRead(socket);
  657.     if (todo == 0)
  658.     {
  659.       break;
  660.     }
  661.     if (headskip == 0)
  662.     {
  663.       headskip = 1;
  664.       todo = cutHeader(todo);
  665.     }
  666.  
  667.     if (pPos + todo > sizeof(picture))
  668.     {
  669.       printf("dataBuffer overrun... %u reached \n\r", pPos + todo);
  670.       break;
  671.     }
  672.     for (w = 0; w < todo; w++)
  673.     {
  674.       picture[w + pPos] = netbuf[w + headlng];
  675.     }
  676.  
  677.     pPos = pPos + todo;
  678.     if (pPos == contLen)
  679.     {
  680.       break;
  681.     }
  682.   }
  683.   netShutDown(socket, 0);
  684. }
  685. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  686. {
  687.  
  688.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  689.   str_replace(pfn, tfnSize, pfn, "/", "_");
  690.   str_replace(pfn, tfnSize, pfn, ":", "_");
  691.   str_replace(pfn, tfnSize, pfn, "*", "_");
  692.   str_replace(pfn, tfnSize, pfn, "?", "_");
  693.   str_replace(pfn, tfnSize, pfn, "<", "_");
  694.   str_replace(pfn, tfnSize, pfn, ">", "_");
  695.   str_replace(pfn, tfnSize, pfn, "|", "_");
  696.   str_replace(pfn, tfnSize, pfn, " ", "_");
  697.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  698.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  699.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  700.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  701.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  702.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  703. }
  704.  
  705. void stringRepair(unsigned char *pfn, unsigned int tSize)
  706. {
  707.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  708.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  709.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  710.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  711.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  712.   str_replace(pfn, tSize, pfn, "\\/", "/");
  713. }
  714.  
  715. unsigned char getPic(unsigned long fileId)
  716. {
  717.   unsigned int todo;
  718.   unsigned char buffer[] = "0000000000";
  719.   signed char socket;
  720.   socket = OpenSock(AF_INET, SOCK_STREAM);
  721.   todo = netConnect(socket);
  722.   sprintf(buffer, "%lu", fileId);
  723.   strcpy(netbuf, "GET /file/id:");
  724.   strcat(netbuf, buffer);
  725.   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");
  726.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  727.   fillPicture(socket);
  728.   return 0;
  729. }
  730.  
  731. void ncReplace(void)
  732. {
  733.   unsigned char len;
  734.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  735.   {
  736.     if (curFileStruct.afn[len] < ' ')
  737.     {
  738.       curFileStruct.afn[len] = '_';
  739.     }
  740.   }
  741.  
  742.   for (len = 0; len < strlen(curFileStruct.pfn); len++)
  743.   {
  744.     if (curFileStruct.pfn[len] < ' ')
  745.     {
  746.       curFileStruct.pfn[len] = '_';
  747.     }
  748.   }
  749. }
  750.  
  751. unsigned char savePic(unsigned long fileId)
  752. {
  753.   FILE *fp2;
  754.   unsigned char afnSize, tfnSize;
  755.   unsigned char fileIdChar[10];
  756.  
  757.   afnSize = sizeof(curFileStruct.afn) - 1;
  758.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  759.  
  760.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  761.   nameRepair(curFileStruct.afn, afnSize);
  762.  
  763.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  764.   nameRepair(curFileStruct.pfn, tfnSize);
  765.   ncReplace();
  766.  
  767.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  768.   if (strlen(curFileStruct.fileName) > 62)
  769.   {
  770.     sprintf(fileIdChar, "-%ld", fileId);
  771.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  772.     curFileStruct.fileName[50] = '\0';
  773.     strcat(curFileStruct.fileName, fileIdChar);
  774.     strcat(curFileStruct.fileName, ".scr");
  775.   }
  776.   OS_SETSYSDRV();
  777.   OS_MKDIR("../downloads");        // Create if not exist
  778.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  779.   OS_CHDIR("../downloads/getpic");
  780.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  781.   if (((int)fp2) & 0xff)
  782.   {
  783.     printf("%s creating error\r\n", curFileStruct.fileName);
  784.     getchar();
  785.     exit(0);
  786.   }
  787.   OS_WRITEHANDLE(picture, fp2, 6912);
  788.   OS_CLOSEHANDLE(fp2);
  789.   return 0;
  790. }
  791.  
  792. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  793. {
  794.   unsigned int i, j;
  795.   unsigned int lenC, lenS;
  796.  
  797.   for (lenC = 0; c[lenC]; lenC++)
  798.     ;
  799.   for (lenS = 0; s[lenS]; lenS++)
  800.     ;
  801.  
  802.   for (i = startPos; i <= lenS - lenC; i++)
  803.   {
  804.     for (j = 0; s[i + j] == c[j]; j++)
  805.       ;
  806.  
  807.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  808.       return i;
  809.     if (j == lenC)
  810.       if (n - 1)
  811.         n--;
  812.       else
  813.         return i;
  814.   }
  815.   return -1;
  816. }
  817.  
  818. const char *parseJson(unsigned char *property)
  819. {
  820.   unsigned int w, lng, lngp1, findEnd, listPos;
  821.   unsigned char terminator;
  822.   int n;
  823.   n = -1;
  824.   // netbuf[0] = '\0';
  825.   n = pos(picture, property, 1, 0);
  826.   if (n == -1)
  827.   {
  828.     strcpy(netbuf, "-");
  829.     // printf("Property %s not found", property);
  830.     return netbuf;
  831.   }
  832.   lng = n - 1 + strlen(property);
  833.   if (picture[lng] == ':')
  834.   {
  835.     terminator = '\0';
  836.   }
  837.   if (picture[lng] == '\"')
  838.   {
  839.     terminator = '\"';
  840.   }
  841.   if (picture[lng] == '[')
  842.   {
  843.     terminator = ']';
  844.   }
  845.  
  846.   findEnd = 1;
  847.   lngp1 = lng + 1;
  848.  
  849.   while (42)
  850.   {
  851.  
  852.     if ((picture[lngp1 + findEnd] == ','))
  853.     {
  854.       if (terminator == '\0')
  855.       {
  856.         break;
  857.       }
  858.       if ((picture[lng + findEnd] == terminator))
  859.       {
  860.         findEnd--;
  861.         break;
  862.       }
  863.     }
  864.     findEnd++;
  865.   }
  866.   listPos = 0;
  867.   for (w = lngp1; w < findEnd + lngp1; w++)
  868.   {
  869.     netbuf[listPos] = picture[w];
  870.     listPos++;
  871.   }
  872.   netbuf[listPos] = '\0';
  873.   return netbuf;
  874. }
  875. void convert866(void)
  876. {
  877.   unsigned int lng, targetPos, w, q = 0;
  878.   unsigned char buffer[8], one, two;
  879.   unsigned int decVal;
  880.   lng = strlen(netbuf);
  881.   targetPos = lng + 1;
  882.  
  883.   while (q < lng)
  884.   {
  885.     one = netbuf[q];
  886.     two = netbuf[q + 1];
  887.     if (one == 92 && two == 117)
  888.     {
  889.       q = q + 2;
  890.       for (w = 0; w < 4; w++)
  891.       {
  892.         buffer[w] = netbuf[q + w];
  893.       }
  894.       q = q + 4;
  895.       buffer[4] = '\0';
  896.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  897.  
  898.       if (decVal < 1088)
  899.       {
  900.         decVal = decVal - 912;
  901.       }
  902.       if (decVal > 1087)
  903.       {
  904.         decVal = decVal - 864;
  905.       }
  906.       if (decVal == 1025)
  907.       {
  908.         decVal = 240;
  909.       }
  910.       if (decVal == 1105)
  911.       {
  912.         decVal = 241;
  913.       }
  914.  
  915.       netbuf[targetPos] = decVal;
  916.     }
  917.     else
  918.     {
  919.       netbuf[targetPos] = netbuf[q];
  920.       q++;
  921.     }
  922.     targetPos++;
  923.   }
  924.   netbuf[targetPos] = 0;
  925.  
  926.   for (w = lng + 1; w < targetPos + 1; w++)
  927.   {
  928.     netbuf[w - lng - 1] = netbuf[w];
  929.   }
  930. }
  931.  
  932. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  933. {
  934.   unsigned int retry, tSize;
  935.   unsigned int todo;
  936.   unsigned char buffer[] = "000000000";
  937.   unsigned char *count1, socket;
  938.   unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; GetPic)\r\n\r\n\0";
  939.  
  940.   switch (queryNum)
  941.   {
  942.   case 0:
  943.     strcpy(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:");
  944.     sprintf(buffer, "%u", limit);
  945.     strcat(netbuf, buffer);
  946.     strcat(netbuf, "/start:");
  947.     sprintf(buffer, "%lu", startPos);
  948.     strcat(netbuf, buffer);
  949.     strcat(netbuf, "/order:date,desc");
  950.     strcat(netbuf, userAgent);
  951.     break;
  952.  
  953.   case 1:
  954.     strcpy(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=4;zxPictureType=standard");
  955.     strcat(netbuf, userAgent);
  956.     break;
  957.  
  958.   case 3: // /api/export:author/filter:authorId=2202
  959.     strcpy(netbuf, "GET /api/export:author/filter:authorId=");
  960.     sprintf(buffer, "%lu", startPos);
  961.     strcat(netbuf, buffer);
  962.     strcat(netbuf, userAgent);
  963.     break;
  964.  
  965.   case 99: // GET /jsonElementData/elementId:182798
  966.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  967.     sprintf(buffer, "%lu", startPos);
  968.     strcat(netbuf, buffer);
  969.     strcat(netbuf, userAgent);
  970.     break;
  971.   }
  972.   retry = 10;
  973.   while (42)
  974.   {
  975.     if (netDriver == 0)
  976.     {
  977.       socket = OpenSock(AF_INET, SOCK_STREAM);
  978.       netConnect(socket);
  979.       todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  980.       fillPicture(socket);
  981.     }
  982.     else
  983.     {
  984.       fillPictureEsp();
  985.     }
  986.     count1 = strstr(picture, "responseStatus\":\"success");
  987.     if (count1 == NULL)
  988.     {
  989.       retry--;
  990.       ATRIB(91);
  991.       printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u][Retry:%u] [Pic:%lu]\r\n", queryNum, retry, startPos);
  992.       YIELD();
  993.       puts(netbuf);
  994.       getchar();
  995.       if (retry < 1)
  996.       {
  997.         return -1;
  998.       }
  999.     }
  1000.     else
  1001.     {
  1002.       break;
  1003.     }
  1004.   }
  1005.  
  1006.   count1 = strstr(picture, "\"id\":");
  1007.   if (count1 == NULL)
  1008.   {
  1009.     ATRIB(91);
  1010.     printf("PROCESS JSON: [ERROR: ID not found.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  1011.     YIELD();
  1012.     return -2;
  1013.   }
  1014.  
  1015.   netbuf[0] = '\0';
  1016.   if (queryNum < 3)
  1017.   {
  1018.     parseJson("\"id\":");
  1019.     curFileStruct.picId = atol(netbuf);
  1020.     parseJson(",\"title\":\"");
  1021.     convert866();
  1022.     strcpy(curFileStruct.picName, netbuf);
  1023.  
  1024.     tSize = sizeof(curFileStruct.picName);
  1025.     stringRepair(curFileStruct.picName, tSize);
  1026.  
  1027.     parseJson(",\"type\":\"");
  1028.     strcpy(curFileStruct.picType, netbuf);
  1029.     parseJson("\"rating\":\"");
  1030.     strcpy(curFileStruct.picRating, netbuf);
  1031.     parseJson("\"year\":\"");
  1032.     curFileStruct.picYear = atoi(netbuf);
  1033.     parseJson("\"totalAmount\":");
  1034.     curFileStruct.totalAmount = atol(netbuf);
  1035.     parseJson("\"authorIds\":[");
  1036.     strcpy(curFileStruct.authorIds, netbuf);
  1037.   }
  1038.   if (queryNum == 99)
  1039.   {
  1040.     parseJson(",\"title\":\"");
  1041.     convert866();
  1042.     strcpy(curFileStruct.authorTitle, netbuf);
  1043.     parseJson(",\"realName\":\"");
  1044.     convert866();
  1045.     strcpy(curFileStruct.authorRealName, netbuf);
  1046.   }
  1047.   return curFileStruct.picId;
  1048. }
  1049.  
  1050. void printData(void)
  1051. {
  1052.   ATRIB(93);
  1053.   printf(" #: ");
  1054.   ATRIB(97);
  1055.   printf("%lu", count);
  1056.   ATRIB(93);
  1057.   printf(" ID: ");
  1058.   ATRIB(97);
  1059.   printf("%lu ", curFileStruct.picId);
  1060.   ATRIB(93);
  1061.   printf(" Total Pics: ");
  1062.   ATRIB(97);
  1063.   printf("%lu\r\n", curFileStruct.totalAmount);
  1064.   ATRIB(93);
  1065.   printf(" Author: ");
  1066.   ATRIB(96);
  1067.   printf("%s\r\n", curFileStruct.authorTitle);
  1068.   ATRIB(93);
  1069.   printf(" TITLE: ");
  1070.   ATRIB(95);
  1071.   printf("%s\r\n", curFileStruct.picName);
  1072.   ATRIB(93);
  1073.   printf(" RATING: ");
  1074.   ATRIB(97);
  1075.   printf("%s", curFileStruct.picRating);
  1076.   ATRIB(93);
  1077.   printf(" YEAR: ");
  1078.   ATRIB(97);
  1079.   printf("%u\r\n", curFileStruct.picYear);
  1080.   ATRIB(93);
  1081.   printf(" AuthorsIDs ");
  1082.   ATRIB(97);
  1083.   printf("%s", curFileStruct.authorIds);
  1084.   ATRIB(93);
  1085.   printf(" Real name: ");
  1086.   ATRIB(97);
  1087.   printf("%s\r\n", curFileStruct.authorRealName);
  1088.   ATRIB(96);
  1089.   printf("\r\n");
  1090.   YIELD();
  1091. }
  1092. void safeKeys(unsigned char keypress)
  1093. {
  1094.   if (keypress == 27)
  1095.   {
  1096.     printf("Good bye...\r\n");
  1097.     ATRIB(37);
  1098.     ATRIB(40);
  1099.     exit(0);
  1100.   }
  1101.  
  1102.   if (keypress == 'j' || keypress == 'J')
  1103.   {
  1104.     printf("Jump to picture:");
  1105.     scanf("%lu", &count);
  1106.     if (count > curFileStruct.totalAmount - 1)
  1107.     {
  1108.       count = curFileStruct.totalAmount - 1;
  1109.     }
  1110.   }
  1111.  
  1112.   if (keypress == 'v' || keypress == 'V')
  1113.   {
  1114.     verbose = !verbose;
  1115.  
  1116.     if (verbose == 0)
  1117.     {
  1118.       BOX(1, 1, 80, 25, 40);
  1119.       AT(1, 1);
  1120.     }
  1121.   }
  1122.  
  1123.   if (keypress == 'h' || keypress == 'H')
  1124.   {
  1125.     printHelp();
  1126.   }
  1127.  
  1128.   if (keypress == 'r' || keypress == 'R')
  1129.   {
  1130.     randomPic = !randomPic;
  1131.  
  1132.     if (verbose == 1)
  1133.     {
  1134.       if (randomPic == 1)
  1135.       {
  1136.         printf("    Random mode enabled...\r\n");
  1137.       }
  1138.       else
  1139.       {
  1140.         printf("    Sequental mode enabled...\r\n");
  1141.       }
  1142.     }
  1143.   }
  1144.   if (keypress == 'a' || keypress == 'A')
  1145.   {
  1146.     slideShow = !slideShow;
  1147.     if (slideShow == 1)
  1148.     {
  1149.       if (verbose == 1)
  1150.         printf("    SlideShow mode enabled...\r\n\r\n");
  1151.       slideShowTime = 250;
  1152.     }
  1153.     else
  1154.     {
  1155.       if (verbose == 1)
  1156.         printf("    Manual mode enabled...\r\n\r\n");
  1157.       slideShowTime = 0;
  1158.     }
  1159.   }
  1160.  
  1161.   if (keypress == 'd' || keypress == 'D')
  1162.   {
  1163.     netDriver = !netDriver;
  1164.     if (netDriver == 1)
  1165.     {
  1166.       printf("    ESP32 mode enabled...\r\n\r\n");
  1167.       loadEspConfig();
  1168.       uart_init(divider);
  1169.       espReBoot();
  1170.     }
  1171.  
  1172.     else
  1173.     {
  1174.       if (verbose == 1)
  1175.         printf("    ZXNETUSB mode enabled...\r\n\r\n");
  1176.     }
  1177.   }
  1178. }
  1179.  
  1180. C_task main(void)
  1181. {
  1182.   unsigned char errno;
  1183.   unsigned long ipadress;
  1184.   long iddqd, idkfa;
  1185.  
  1186.   os_initstdio();
  1187.  
  1188.   count = 0;
  1189.   verbose = 1;
  1190.   randomPic = 0;
  1191.   slideShow = 0;
  1192.   netDriver = 0;
  1193.  
  1194.   BOX(1, 1, 80, 25, 40);
  1195.   AT(1, 1);
  1196.   printHelp();
  1197.   safeKeys(keypress);
  1198.  
  1199. start:
  1200.   keypress = 0;
  1201.   switch (randomPic)
  1202.   {
  1203.   case 0:
  1204.     iddqd = processJson(count, 1, 0);
  1205.     break;
  1206.   case 1:
  1207.     iddqd = processJson(0, 1, 1);
  1208.     break;
  1209.   }
  1210.  
  1211.   if (iddqd < 0)
  1212.   {
  1213.     count++;
  1214.     goto start;
  1215.   }
  1216.  
  1217.   if (verbose == 1)
  1218.   {
  1219.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  1220.  
  1221.     if (idkfa < 0)
  1222.     {
  1223.       printf(" Cant parse curFileStruct.authorIds = %s \r\n\r\n", curFileStruct.authorIds);
  1224.       count++;
  1225.       goto start;
  1226.     }
  1227.  
  1228.     printData();
  1229.   }
  1230.   else
  1231.   {
  1232.     // ATRIB(97);
  1233.     // printf(" Getting picture...\r\n");
  1234.   }
  1235.  
  1236.   if (strcmp(curFileStruct.picType, "standard") != 0)
  1237.   {
  1238.     printf("  >>Format '%s' not supported, skipped \n\r", curFileStruct.picType);
  1239.     count++;
  1240.     goto start;
  1241.   }
  1242.  
  1243.   if (netDriver == 0)
  1244.   {
  1245.     errno = getPic(iddqd);
  1246.   }
  1247.   else
  1248.   {
  1249.     errno = getPicEsp(iddqd);
  1250.   }
  1251.  
  1252. review:
  1253.   keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  1254.   emptyKeys();
  1255.  
  1256.   ///// Keys only for pictures
  1257.   if (keypress == 's' || keypress == 'S')
  1258.   {
  1259.     savePic(iddqd);
  1260.     if (verbose == 1)
  1261.       printf("        ID:%lu    TITLE:%s  SAVED\r\n\r\n", curFileStruct.picId, curFileStruct.picName);
  1262.     count++;
  1263.   }
  1264.  
  1265.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1266.   {
  1267.     if (count > 0)
  1268.     {
  1269.       count--;
  1270.     }
  1271.   }
  1272.   if (keypress == 251 || keypress == 32)
  1273.   {
  1274.     count++;
  1275.     goto start;
  1276.   }
  1277.   if (keypress == 'i' || keypress == 'I')
  1278.   {
  1279.     delay(100);
  1280.     getchar();
  1281.     goto review;
  1282.   }
  1283.   safeKeys(keypress);
  1284.   goto start;
  1285. }
  1286.