]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc1738.c
5 * AUTHOR: Harvest Derived
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
47 * RFC 1738 defines that these characters should be escaped, as well
48 * any non-US-ASCII character or anything between 0x00 - 0x1F.
50 static char rfc1738_unsafe_chars
[] = {
55 #if 0 /* done in code */
68 (char) 0x20 /* space */
71 static char rfc1738_reserved_chars
[] = {
82 * rfc1738_escape - Returns a static buffer contains the RFC 1738
83 * compliant, escaped version of the given url.
86 rfc1738_do_escape(const char *url
, int flags
)
89 static size_t bufsize
= 0;
92 unsigned int i
, do_escape
;
94 if (buf
== NULL
|| strlen(url
) * 3 > bufsize
) {
96 bufsize
= strlen(url
) * 3 + 1;
97 buf
= (char*)xcalloc(bufsize
, 1);
99 for (p
= url
, q
= buf
; *p
!= '\0' && q
< (buf
+ bufsize
- 1); p
++, q
++) {
102 /* RFC 1738 defines these chars as unsafe */
103 for (i
= 0; i
< sizeof(rfc1738_unsafe_chars
); i
++) {
104 if (*p
== rfc1738_unsafe_chars
[i
]) {
109 /* Handle % separately */
110 if (flags
!= RFC1738_ESCAPE_UNESCAPED
&& *p
== '%')
112 /* RFC 1738 defines these chars as reserved */
113 for (i
= 0; i
< sizeof(rfc1738_reserved_chars
) && flags
== RFC1738_ESCAPE_RESERVED
; i
++) {
114 if (*p
== rfc1738_reserved_chars
[i
]) {
119 /* RFC 1738 says any control chars (0x00-0x1F) are encoded */
120 if ((unsigned char) *p
<= (unsigned char) 0x1F) {
123 /* RFC 1738 says 0x7f is encoded */
124 if (*p
== (char) 0x7F) {
127 /* RFC 1738 says any non-US-ASCII are encoded */
128 if (((unsigned char) *p
>= (unsigned char) 0x80)) {
131 /* Do the triplet encoding, or just copy the char */
132 /* note: we do not need snprintf here as q is appropriately
135 if (do_escape
== 1) {
136 (void) sprintf(q
, "%%%02X", (unsigned char) *p
);
137 q
+= sizeof(char) * 2;
146 #if 0 /* legacy API */
148 * rfc1738_escape - Returns a static buffer that contains the RFC
149 * 1738 compliant, escaped version of the given url.
152 rfc1738_escape(const char *url
)
154 return rfc1738_do_escape(url
, 0);
158 * rfc1738_escape_unescaped - Returns a static buffer that contains
159 * the RFC 1738 compliant, escaped version of the given url.
162 rfc1738_escape_unescaped(const char *url
)
164 return rfc1738_do_escape(url
, -1);
168 * rfc1738_escape_part - Returns a static buffer that contains the
169 * RFC 1738 compliant, escaped version of the given url segment.
172 rfc1738_escape_part(const char *url
)
174 return rfc1738_do_escape(url
, 1);
179 * Converts a ascii hex code into a binary character.
181 inline int fromhex(char ch
); // prototype to keep GCC happy.
186 if (ch
>= '0' && ch
<= '9')
188 if (ch
>= 'a' && ch
<= 'f')
189 return ch
- 'a' + 10;
190 if (ch
>= 'A' && ch
<= 'F')
191 return ch
- 'A' + 10;
196 * rfc1738_unescape() - Converts escaped characters (%xy numbers) in
197 * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab"
200 rfc1738_unescape(char *s
)
202 int i
, j
; /* i is write, j is read */
203 for (i
= j
= 0; s
[j
]; i
++, j
++) {
206 /* normal case, nothing more to do */
207 } else if (s
[j
+ 1] == '%') { /* %% case */
213 v1
= fromhex(s
[j
+ 1]);
215 continue; /* non-hex or \0 */
216 v2
= fromhex(s
[j
+ 2]);
218 continue; /* non-hex or \0 */
220 if (x
> 0 && x
<= 255) {