Annotation of libwww/Library/src/HTEscape.c, revision 2.1

2.1     ! frystyk     1: /*             Escape and Unesacpe Illegal Characters in a URI      HTEscape.c
        !             2: **             ===============================================
        !             3: **
        !             4: ** history:
        !             5: **     Nov 13 94       Spawned from HTParse, as it then can be used in utility
        !             6: **                     programs without loading the whole library
        !             7: */
        !             8: 
        !             9: /* Platform dependent stuff */
        !            10: #include "HTUtils.h"
        !            11: #include "tcp.h"
        !            12: 
        !            13: /* Library Includes */
        !            14: #include "HTEscape.h"                                   /* Implemented here */
        !            15: 
        !            16: #define HEX_ESCAPE '%'
        !            17: 
        !            18: /* ------------------------------------------------------------------------- */
        !            19: 
        !            20: /*             Escape undesirable characters using %           HTEscape()
        !            21: **             -------------------------------------
        !            22: **
        !            23: **     This function takes a pointer to a string in which
        !            24: **     some characters may be unacceptable unescaped.
        !            25: **     It returns a string which has these characters
        !            26: **     represented by a '%' character followed by two hex digits.
        !            27: **
        !            28: **     In the tradition of being conservative in what you do and liberal
        !            29: **     in what you accept, we encode some characters which in fact are
        !            30: **     allowed in URLs unencoded -- so DON'T use the table below for
        !            31: **     parsing! 
        !            32: **
        !            33: **     Unlike HTUnEscape(), this routine returns a malloced string.
        !            34: **
        !            35: */
        !            36: 
        !            37: /* Not BOTH static AND const at the same time in gcc :-(, Henrik 18/03-94 
        !            38: **  code gen error in gcc when making random access to
        !            39: **  static const table(!!)  */
        !            40: /* PRIVATE CONST unsigned char isAcceptable[96] = */
        !            41: PRIVATE unsigned char isAcceptable[96] =
        !            42: 
        !            43: /* Overencodes */
        !            44: /*     Bit 0           xalpha          -- see HTFile.h
        !            45: **     Bit 1           xpalpha         -- as xalpha but with plus.
        !            46: **     Bit 2 ...       path            -- as xpalpha but with /
        !            47: */
        !            48:     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
        !            49:     {    0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4,      /* 2x   !"#$%&'()*+,-./  */
        !            50:          7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,      /* 3x  0123456789:;<=>?  */
        !            51:         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,       /* 4x  @ABCDEFGHIJKLMNO  */
        !            52:         7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7,       /* 5X  PQRSTUVWXYZ[\]^_  */
        !            53:         0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,       /* 6x  `abcdefghijklmno  */
        !            54:         7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 };     /* 7X  pqrstuvwxyz{\}~  DEL */
        !            55: 
        !            56: PRIVATE char *hex = "0123456789ABCDEF";
        !            57: 
        !            58: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
        !            59:        unsigned char, mask)
        !            60: {
        !            61: #define ACCEPTABLE(a)  ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
        !            62:     CONST char * p;
        !            63:     char * q;
        !            64:     char * result;
        !            65:     int unacceptable = 0;
        !            66:     for(p=str; *p; p++)
        !            67:         if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
        !            68:                unacceptable++;
        !            69:     result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
        !            70:     if (result == NULL) outofmem(__FILE__, "HTEscape");
        !            71:     for(q=result, p=str; *p; p++) {
        !            72:        unsigned char a = TOASCII(*p);
        !            73:        if (!ACCEPTABLE(a)) {
        !            74:            *q++ = HEX_ESCAPE;  /* Means hex commming */
        !            75:            *q++ = hex[a >> 4];
        !            76:            *q++ = hex[a & 15];
        !            77:        }
        !            78:        else *q++ = *p;
        !            79:     }
        !            80:     *q++ = 0;                  /* Terminate */
        !            81:     return result;
        !            82: }
        !            83: 
        !            84: 
        !            85: /*             Decode %xx escaped characters                   HTUnEscape()
        !            86: **             -----------------------------
        !            87: **
        !            88: **     This function takes a pointer to a string in which some
        !            89: **     characters may have been encoded in %xy form, where xy is
        !            90: **     the acsii hex code for character 16x+y.
        !            91: **     The string is converted in place, as it will never grow.
        !            92: */
        !            93: 
        !            94: PRIVATE char from_hex ARGS1(char, c)
        !            95: {
        !            96:     return  c >= '0' && c <= '9' ?  c - '0' 
        !            97:            : c >= 'A' && c <= 'F'? c - 'A' + 10
        !            98:            : c - 'a' + 10;     /* accept small letters just in case */
        !            99: }
        !           100: 
        !           101: PUBLIC char * HTUnEscape ARGS1( char *, str)
        !           102: {
        !           103:     char * p = str;
        !           104:     char * q = str;
        !           105: 
        !           106:     if (!str) {                                              /* Just for safety ;-) */
        !           107:        if (URI_TRACE)
        !           108:            fprintf(stderr, "HTUnEscape.. Called with NULL argument.\n");
        !           109:        return "";
        !           110:     }
        !           111:     while(*p) {
        !           112:         if (*p == HEX_ESCAPE) {
        !           113:            p++;
        !           114:            if (*p) *q = from_hex(*p++) * 16;
        !           115:            if (*p) *q = FROMASCII(*q + from_hex(*p++));
        !           116:            q++;
        !           117:        } else {
        !           118:            *q++ = *p++; 
        !           119:        }
        !           120:     }
        !           121:     
        !           122:     *q++ = 0;
        !           123:     return str;
        !           124:     
        !           125: } /* HTUnEscape */
        !           126: 

Webmaster