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