]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc1738.c
3 * AUTHOR: Harvest Derived
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
41 * RFC 1738 defines that these characters should be escaped, as well
42 * any non-US-ASCII character or anything between 0x00 - 0x1F.
44 static char rfc1738_unsafe_chars
[] = {
49 #if 0 /* done in code */
50 (char) 0x20, /* space */
65 static char rfc1738_reserved_chars
[] = {
76 * rfc1738_escape - Returns a static buffer contains the RFC 1738
77 * compliant, escaped version of the given url.
80 rfc1738_do_escape(const char *url
, int flags
)
83 static size_t bufsize
= 0;
86 unsigned int i
, do_escape
;
88 if (buf
== NULL
|| strlen(url
) * 3 > bufsize
) {
90 bufsize
= strlen(url
) * 3 + 1;
91 buf
= (char*)xcalloc(bufsize
, 1);
93 for (src
= url
, dst
= buf
; *src
!= '\0' && dst
< (buf
+ bufsize
- 1); src
++, dst
++) {
95 /* a-z, A-Z and 0-9 are SAFE. */
96 if ((*src
>= 'a' && *src
<= 'z') || (*src
>= 'A' && *src
<= 'Z') || (*src
>= '0' && *src
<= '9')) {
103 /* RFC 1738 defines these chars as unsafe */
104 if ((flags
& RFC1738_ESCAPE_UNSAFE
)) {
105 for (i
= 0; i
< sizeof(rfc1738_unsafe_chars
); i
++) {
106 if (*src
== rfc1738_unsafe_chars
[i
]) {
111 /* Handle % separately */
112 if (!(flags
& RFC1738_ESCAPE_NOPERCENT
) && *src
== '%')
114 /* Handle space separately */
115 else if (!(flags
& RFC1738_ESCAPE_NOSPACE
) && *src
<= ' ')
118 /* RFC 1738 defines these chars as reserved */
119 if ((flags
& RFC1738_ESCAPE_RESERVED
) && do_escape
== 0) {
120 for (i
= 0; i
< sizeof(rfc1738_reserved_chars
); i
++) {
121 if (*src
== rfc1738_reserved_chars
[i
]) {
127 if ((flags
& RFC1738_ESCAPE_CTRLS
) && do_escape
== 0) {
128 /* RFC 1738 says any control chars (0x00-0x1F) are encoded */
129 if ((unsigned char) *src
<= (unsigned char) 0x1F)
131 /* RFC 1738 says 0x7f is encoded */
132 else if (*src
== (char) 0x7F)
134 /* RFC 1738 says any non-US-ASCII are encoded */
135 else if (((unsigned char) *src
>= (unsigned char) 0x80))
138 /* Do the triplet encoding, or just copy the char */
139 if (do_escape
== 1) {
140 (void) snprintf(dst
, (bufsize
-(dst
-buf
)), "%%%02X", (unsigned char) *src
);
141 dst
+= sizeof(char) * 2;
151 * Converts a ascii hex code into a binary character.
156 if (ch
>= '0' && ch
<= '9')
158 if (ch
>= 'a' && ch
<= 'f')
159 return ch
- 'a' + 10;
160 if (ch
>= 'A' && ch
<= 'F')
161 return ch
- 'A' + 10;
166 * rfc1738_unescape() - Converts escaped characters (%xy numbers) in
167 * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab"
170 rfc1738_unescape(char *s
)
172 int i
, j
; /* i is write, j is read */
173 for (i
= j
= 0; s
[j
]; i
++, j
++) {
176 /* normal case, nothing more to do */
177 } else if (s
[j
+ 1] == '%') { /* %% case */
182 v1
= fromhex(s
[j
+ 1]);
184 continue; /* non-hex or \0 */
185 v2
= fromhex(s
[j
+ 2]);
187 continue; /* non-hex or \0 */
189 if (x
> 0 && x
<= 255) {