Annotation of libwww/Library/src/HTEscape.c, revision 2.18
2.2 frystyk 1: /* HTEscape.c
2: ** ESCAPE AND UNESACPE ILLEGAL CHARACTERS IN A URI
3: **
2.5 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.2 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.18 ! frystyk 6: ** @(#) $Id: HTEscape.c,v 2.17 1998/05/04 19:36:27 frystyk Exp $
2.1 frystyk 7: **
8: ** history:
9: ** Nov 13 94 Spawned from HTParse, as it then can be used in utility
10: ** programs without loading the whole library
11: */
12:
2.4 frystyk 13: /* Library include files */
2.17 frystyk 14: #include "wwwsys.h"
2.1 frystyk 15: #include "HTUtils.h"
16: #include "HTEscape.h" /* Implemented here */
17:
18: #define HEX_ESCAPE '%'
2.6 frystyk 19: #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
20:
21: /*
2.13 frystyk 22: ** Not BOTH static AND const at the same time in gcc :-(, Henrik 18/03-94
23: ** code gen error in gcc when making random access to static const table(!!)
2.6 frystyk 24: */
25:
26: /*
27: ** Bit 0 xalpha -- see HTFile.h
28: ** Bit 1 xpalpha -- as xalpha but with plus.
29: ** Bit 2 ... path -- as xpalpha but with /
30: */
31: PRIVATE unsigned char isAcceptable[96] =
2.18 ! frystyk 32: {/* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF */
! 33: 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xF,0xC,0x0,0xF,0xF,0x8, /* 2x !"#$%&'()*+,-./ */
! 34: 0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0x1,0x0,0x0,0x0,0x0,0x0, /* 3x 0123456789:;<=>? */
! 35: 0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF, /* 4x @ABCDEFGHIJKLMNO */
! 36: 0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0x0,0x0,0x0,0x0,0xF, /* 5X PQRSTUVWXYZ[\]^_ */
! 37: 0x0,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF, /* 6x `abcdefghijklmno */
! 38: 0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0x0,0x0,0x0,0x0,0x0 /* 7X pqrstuvwxyz{\}~DEL */
2.6 frystyk 39: };
40: PRIVATE char *hex = "0123456789ABCDEF";
2.1 frystyk 41:
42: /* ------------------------------------------------------------------------- */
43:
44: /* Escape undesirable characters using % HTEscape()
45: ** -------------------------------------
46: **
47: ** This function takes a pointer to a string in which
48: ** some characters may be unacceptable unescaped.
49: ** It returns a string which has these characters
50: ** represented by a '%' character followed by two hex digits.
51: **
52: ** In the tradition of being conservative in what you do and liberal
53: ** in what you accept, we encode some characters which in fact are
54: ** allowed in URLs unencoded -- so DON'T use the table below for
55: ** parsing!
56: **
2.13 frystyk 57: ** Unlike HTUnEscape(), this routine returns a HT_MALLOCed string.
2.1 frystyk 58: **
59: */
2.13 frystyk 60: PUBLIC char * HTEscape (const char * str, HTURIEncoding mask)
2.1 frystyk 61: {
2.13 frystyk 62: const char * p;
2.1 frystyk 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++;
2.11 frystyk 69: if ((result = (char *) HT_MALLOC(p-str + unacceptable+ unacceptable + 1)) == NULL)
70: HT_OUTOFMEM("HTEscape");
2.1 frystyk 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:
2.16 frystyk 85: PUBLIC char HTAsciiHexToChar (char c)
86: {
87: return c >= '0' && c <= '9' ? c - '0'
88: : c >= 'A' && c <= 'F'? c - 'A' + 10
89: : c - 'a' + 10; /* accept small letters just in case */
90: }
91:
2.1 frystyk 92: /* Decode %xx escaped characters HTUnEscape()
93: ** -----------------------------
94: **
95: ** This function takes a pointer to a string in which some
96: ** characters may have been encoded in %xy form, where xy is
97: ** the acsii hex code for character 16x+y.
98: ** The string is converted in place, as it will never grow.
99: */
2.9 frystyk 100: PUBLIC char * HTUnEscape (char * str)
2.1 frystyk 101: {
102: char * p = str;
103: char * q = str;
104:
105: if (!str) { /* Just for safety ;-) */
106: if (URI_TRACE)
2.12 eric 107: HTTrace("HTUnEscape.. Called with NULL argument.\n");
2.1 frystyk 108: return "";
109: }
110: while(*p) {
111: if (*p == HEX_ESCAPE) {
112: p++;
2.16 frystyk 113: if (*p) *q = HTAsciiHexToChar(*p++) * 16;
114: #if 1
115: /* Suggestion from Markku Savela */
116: if (*p) *q = FROMASCII(*q + HTAsciiHexToChar(*p)), ++p;
2.15 eric 117: q++;
2.16 frystyk 118: #else
119: if (*p) *q = FROMASCII(*q + HTAsciiHexToChar(*p));
2.7 frystyk 120: p++, q++;
2.16 frystyk 121: #endif
2.1 frystyk 122: } else {
123: *q++ = *p++;
124: }
125: }
126:
127: *q++ = 0;
128: return str;
129:
130: } /* HTUnEscape */
131:
Webmaster