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