]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/rfc1738.c
Link diskd to compat libraries properly
[thirdparty/squid.git] / lib / rfc1738.c
CommitLineData
30a4f2a8 1/*
262a0e14 2 * $Id$
30a4f2a8 3 *
26ac0430 4 * DEBUG:
30a4f2a8 5 * AUTHOR: Harvest Derived
6 *
2b6662ba 7 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 8 * ----------------------------------------------------------
30a4f2a8 9 *
2b6662ba 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.
30a4f2a8 18 *
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.
26ac0430 23 *
30a4f2a8 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.
26ac0430 28 *
30a4f2a8 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
cbdec147 31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 32 *
30a4f2a8 33 */
34
35#include "config.h"
1fa9b1a7
AJ
36#include "rfc1738.h"
37#include "util.h"
0d94e9fe 38
30a4f2a8 39#if HAVE_STDIO_H
090089c4 40#include <stdio.h>
30a4f2a8 41#endif
42#if HAVE_STRING_H
090089c4 43#include <string.h>
30a4f2a8 44#endif
45
26ac0430 46/*
090089c4 47 * RFC 1738 defines that these characters should be escaped, as well
48 * any non-US-ASCII character or anything between 0x00 - 0x1F.
49 */
26ac0430 50static char rfc1738_unsafe_chars[] = {
090089c4 51 (char) 0x3C, /* < */
52 (char) 0x3E, /* > */
53 (char) 0x22, /* " */
54 (char) 0x23, /* # */
9bc73deb 55#if 0 /* done in code */
090089c4 56 (char) 0x25, /* % */
9bc73deb 57#endif
090089c4 58 (char) 0x7B, /* { */
59 (char) 0x7D, /* } */
60 (char) 0x7C, /* | */
61 (char) 0x5C, /* \ */
62 (char) 0x5E, /* ^ */
63 (char) 0x7E, /* ~ */
64 (char) 0x5B, /* [ */
65 (char) 0x5D, /* ] */
66 (char) 0x60, /* ` */
67 (char) 0x27, /* ' */
68 (char) 0x20 /* space */
69};
70
26ac0430 71static char rfc1738_reserved_chars[] = {
9bc73deb 72 (char) 0x3b, /* ; */
73 (char) 0x2f, /* / */
74 (char) 0x3f, /* ? */
75 (char) 0x3a, /* : */
76 (char) 0x40, /* @ */
77 (char) 0x3d, /* = */
78 (char) 0x26 /* & */
79};
80
090089c4 81/*
26ac0430 82 * rfc1738_escape - Returns a static buffer contains the RFC 1738
090089c4 83 * compliant, escaped version of the given url.
84 */
1fa9b1a7
AJ
85char *
86rfc1738_do_escape(const char *url, int flags)
090089c4 87{
3fdadc70 88 static char *buf;
89 static size_t bufsize = 0;
0ee4272b 90 const char *p;
91 char *q;
2d72d4fd 92 unsigned int i, do_escape;
090089c4 93
3fdadc70 94 if (buf == NULL || strlen(url) * 3 > bufsize) {
26ac0430
AJ
95 xfree(buf);
96 bufsize = strlen(url) * 3 + 1;
1fa9b1a7 97 buf = (char*)xcalloc(bufsize, 1);
3fdadc70 98 }
6512a760 99 for (p = url, q = buf; *p != '\0' && q < (buf + bufsize - 1); p++, q++) {
26ac0430 100 do_escape = 0;
090089c4 101
26ac0430
AJ
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]) {
105 do_escape = 1;
106 break;
107 }
108 }
109 /* Handle % separately */
1fa9b1a7 110 if (flags != RFC1738_ESCAPE_UNESCAPED && *p == '%')
26ac0430
AJ
111 do_escape = 1;
112 /* RFC 1738 defines these chars as reserved */
1fa9b1a7 113 for (i = 0; i < sizeof(rfc1738_reserved_chars) && flags == RFC1738_ESCAPE_RESERVED; i++) {
26ac0430
AJ
114 if (*p == rfc1738_reserved_chars[i]) {
115 do_escape = 1;
116 break;
117 }
118 }
119 /* RFC 1738 says any control chars (0x00-0x1F) are encoded */
120 if ((unsigned char) *p <= (unsigned char) 0x1F) {
121 do_escape = 1;
122 }
123 /* RFC 1738 says 0x7f is encoded */
124 if (*p == (char) 0x7F) {
125 do_escape = 1;
126 }
127 /* RFC 1738 says any non-US-ASCII are encoded */
128 if (((unsigned char) *p >= (unsigned char) 0x80)) {
129 do_escape = 1;
130 }
131 /* Do the triplet encoding, or just copy the char */
132 /* note: we do not need snprintf here as q is appropriately
133 * allocated - KA */
93fc29fe 134
26ac0430
AJ
135 if (do_escape == 1) {
136 (void) sprintf(q, "%%%02X", (unsigned char) *p);
137 q += sizeof(char) * 2;
138 } else {
139 *q = *p;
140 }
090089c4 141 }
142 *q = '\0';
143 return (buf);
144}
145
1fa9b1a7 146#if 0 /* legacy API */
9bc73deb 147/*
148 * rfc1738_escape - Returns a static buffer that contains the RFC
149 * 1738 compliant, escaped version of the given url.
150 */
151char *
152rfc1738_escape(const char *url)
153{
154 return rfc1738_do_escape(url, 0);
155}
156
157/*
158 * rfc1738_escape_unescaped - Returns a static buffer that contains
159 * the RFC 1738 compliant, escaped version of the given url.
160 */
161char *
162rfc1738_escape_unescaped(const char *url)
163{
164 return rfc1738_do_escape(url, -1);
165}
166
167/*
168 * rfc1738_escape_part - Returns a static buffer that contains the
169 * RFC 1738 compliant, escaped version of the given url segment.
170 */
171char *
172rfc1738_escape_part(const char *url)
173{
174 return rfc1738_do_escape(url, 1);
175}
1fa9b1a7 176#endif /* 0 */
9bc73deb 177
090089c4 178/*
26ac0430 179 * rfc1738_unescape() - Converts escaped characters (%xy numbers) in
090089c4 180 * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab"
181 */
79c8a298 182static inline int
e0d6837c
HN
183fromhex(char ch)
184{
185 if (ch >= '0' && ch <= '9')
186 return ch - '0';
187 if (ch >= 'a' && ch <= 'f')
188 return ch - 'a' + 10;
189 if (ch >= 'A' && ch <= 'F')
190 return ch - 'A' + 10;
191 return -1;
192}
193
b8d8561b 194void
8d917594 195rfc1738_unescape(char *s)
090089c4 196{
090089c4 197 int i, j; /* i is write, j is read */
090089c4 198 for (i = j = 0; s[j]; i++, j++) {
26ac0430 199 s[i] = s[j];
e0d6837c
HN
200 if (s[j] != '%') {
201 /* normal case, nothing more to do */
202 } else if (s[j + 1] == '%') { /* %% case */
203 j++; /* Skip % */
204 } else {
205 /* decode */
206 char v1, v2;
55b5914b 207 int x;
e0d6837c 208 v1 = fromhex(s[j + 1]);
79c8a298
A
209 if (v1 < 0)
210 continue; /* non-hex or \0 */
e0d6837c 211 v2 = fromhex(s[j + 2]);
79c8a298
A
212 if (v2 < 0)
213 continue; /* non-hex or \0 */
55b5914b
HN
214 x = v1 << 4 | v2;
215 if (x > 0 && x <= 255) {
216 s[i] = x;
217 j += 2;
218 }
26ac0430 219 }
090089c4 220 }
221 s[i] = '\0';
222}