Annotation of libwww/Library/src/HTEscape.c, revision 2.15
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.15 ! eric 6: ** @(#) $Id: HTEscape.c,v 2.14 1996/04/12 17:46:37 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.13 frystyk 14: #include "sysdep.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] =
32: {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
33: 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
34: 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
35: 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
36: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
37: 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
38: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 /* 7X pqrstuvwxyz{\}~ DEL */
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:
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:
2.9 frystyk 94: PRIVATE char from_hex (char c)
2.1 frystyk 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:
2.9 frystyk 101: PUBLIC char * HTUnEscape (char * str)
2.1 frystyk 102: {
103: char * p = str;
104: char * q = str;
105:
106: if (!str) { /* Just for safety ;-) */
107: if (URI_TRACE)
2.12 eric 108: HTTrace("HTUnEscape.. Called with NULL argument.\n");
2.1 frystyk 109: return "";
110: }
111: while(*p) {
112: if (*p == HEX_ESCAPE) {
113: p++;
114: if (*p) *q = from_hex(*p++) * 16;
2.15 ! eric 115: #ifdef UNTESTED /* suggestion from Markku Savela - I just copied it in - EGP */
! 116: if (*p) *q = FROMASCII(*q + from_hex(*p)), ++p;
! 117: q++;
! 118: #else /* UNTESTED */
2.7 frystyk 119: if (*p) *q = FROMASCII(*q + from_hex(*p));
120: p++, q++;
2.15 ! eric 121: #endif /* !UNTESTED */
2.1 frystyk 122: } else {
123: *q++ = *p++;
124: }
125: }
126:
127: *q++ = 0;
128: return str;
129:
130: } /* HTUnEscape */
131:
Webmaster