http_dec.c
changeset 19 4da6dbf01423
equal deleted inserted replaced
18:64ed4238657f 19:4da6dbf01423
       
     1 #include <stdio.h>
       
     2 #include <unicode/utf8.h>
       
     3 #include "dictre.h"
       
     4 
       
     5 static int url_get(FILE *f)
       
     6 {
       
     7     int val;
       
     8 
       
     9     val = fgetc(f);
       
    10     if (val == '%')
       
    11     {
       
    12         unsigned char num[3];
       
    13         num[2] = '\0';
       
    14         num[0] = fgetc(f);
       
    15         num[1] = fgetc(f);
       
    16         val = strtol(num, 0, 16);
       
    17     } else if (val == '&')
       
    18         return END_OF_URL;
       
    19     else if (val == '+')
       
    20         return ' ';
       
    21     /*printf("[%i]", val);*/
       
    22     return val;
       
    23 }
       
    24 
       
    25 static int char2num(unsigned char c)
       
    26 {
       
    27     if (c >= '0' && c <= '9')
       
    28         return c - '0';
       
    29     if (c >= 'A' && c <= 'Z')
       
    30         return c + 10 - 'A';
       
    31     if (c >= 'a' && c <= 'z')
       
    32         return c + 10 - 'a';
       
    33     return 0;
       
    34 }
       
    35 
       
    36 int http_getc(FILE *f)
       
    37 {
       
    38     int c;
       
    39     static unsigned char tmp[6]; /* for a UTF-8 string */
       
    40     static int itmp = -1;
       
    41     static int tmplen;
       
    42 
       
    43     if (itmp == -1)
       
    44     {
       
    45         c = url_get(f);
       
    46         if (c == '&')
       
    47         {
       
    48             c = url_get(f);
       
    49             if (c == '#')
       
    50             {
       
    51                 char iserror = 0;
       
    52                 int entval;
       
    53                 /*Get number*/
       
    54                 entval = 0;
       
    55                 while((c = url_get(f)) != ';')
       
    56                 {
       
    57                     /* Digits in base 10 */
       
    58                     entval = char2num(c) + entval * 10;
       
    59                 }
       
    60                 /*printf("{%i}", entval);*/
       
    61                 /*Get utf-8 version*/
       
    62                 tmplen = 0;
       
    63                 U8_APPEND(tmp, tmplen, 6, entval, iserror);
       
    64                 if (iserror)
       
    65                     return -3;
       
    66                 /* We need not to program itmp for the next run
       
    67                  * if we have only one character to send */
       
    68                 if (tmplen != 1)
       
    69                     itmp = 1;
       
    70                 return tmp[0];
       
    71             }
       
    72             else
       
    73                 return HTTP_DECODE_ERROR; /* ERROR! */
       
    74         }
       
    75     } else /* We already have a character to keep on sending */
       
    76     {
       
    77         int tosend;
       
    78         tosend = itmp;
       
    79         ++itmp;
       
    80         if (itmp == tmplen)
       
    81             itmp = -1;
       
    82         return tmp[tosend];
       
    83     }
       
    84     return c;
       
    85 }