]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc1738.c
From: Markus Gyger <mgyger@itr.ch>
[thirdparty/squid.git] / lib / rfc1738.c
1 /*
2 * $Id: rfc1738.c,v 1.5 1996/09/17 16:32:26 wessels Exp $
3 *
4 * DEBUG:
5 * AUTHOR: Harvest Derived
6 *
7 * SQUID Internet Object Cache http://www.nlanr.net/Squid/
8 * --------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from the
11 * Internet community. Development is led by Duane Wessels of the
12 * National Laboratory for Applied Network Research and funded by
13 * the National Science Foundation.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
31 /*
32 * Copyright (c) 1994, 1995. All rights reserved.
33 *
34 * The Harvest software was developed by the Internet Research Task
35 * Force Research Group on Resource Discovery (IRTF-RD):
36 *
37 * Mic Bowman of Transarc Corporation.
38 * Peter Danzig of the University of Southern California.
39 * Darren R. Hardy of the University of Colorado at Boulder.
40 * Udi Manber of the University of Arizona.
41 * Michael F. Schwartz of the University of Colorado at Boulder.
42 * Duane Wessels of the University of Colorado at Boulder.
43 *
44 * This copyright notice applies to software in the Harvest
45 * ``src/'' directory only. Users should consult the individual
46 * copyright notices in the ``components/'' subdirectories for
47 * copyright information about other software bundled with the
48 * Harvest source code distribution.
49 *
50 * TERMS OF USE
51 *
52 * The Harvest software may be used and re-distributed without
53 * charge, provided that the software origin and research team are
54 * cited in any use of the system. Most commonly this is
55 * accomplished by including a link to the Harvest Home Page
56 * (http://harvest.cs.colorado.edu/) from the query page of any
57 * Broker you deploy, as well as in the query result pages. These
58 * links are generated automatically by the standard Broker
59 * software distribution.
60 *
61 * The Harvest software is provided ``as is'', without express or
62 * implied warranty, and with no support nor obligation to assist
63 * in its use, correction, modification or enhancement. We assume
64 * no liability with respect to the infringement of copyrights,
65 * trade secrets, or any patents, and are not responsible for
66 * consequential damages. Proper use of the Harvest software is
67 * entirely the responsibility of the user.
68 *
69 * DERIVATIVE WORKS
70 *
71 * Users may make derivative works from the Harvest software, subject
72 * to the following constraints:
73 *
74 * - You must include the above copyright notice and these
75 * accompanying paragraphs in all forms of derivative works,
76 * and any documentation and other materials related to such
77 * distribution and use acknowledge that the software was
78 * developed at the above institutions.
79 *
80 * - You must notify IRTF-RD regarding your distribution of
81 * the derivative work.
82 *
83 * - You must clearly notify users that your are distributing
84 * a modified version and not the original Harvest software.
85 *
86 * - Any derivative product is also subject to these copyright
87 * and use restrictions.
88 *
89 * Note that the Harvest software is NOT in the public domain. We
90 * retain copyright, as specified above.
91 *
92 * HISTORY OF FREE SOFTWARE STATUS
93 *
94 * Originally we required sites to license the software in cases
95 * where they were going to build commercial products/services
96 * around Harvest. In June 1995 we changed this policy. We now
97 * allow people to use the core Harvest software (the code found in
98 * the Harvest ``src/'' directory) for free. We made this change
99 * in the interest of encouraging the widest possible deployment of
100 * the technology. The Harvest software is really a reference
101 * implementation of a set of protocols and formats, some of which
102 * we intend to standardize. We encourage commercial
103 * re-implementations of code complying to this set of standards.
104 */
105
106 #include "config.h"
107
108 #if HAVE_STDIO_H
109 #include <stdio.h>
110 #endif
111 #if HAVE_STRING_H
112 #include <string.h>
113 #endif
114
115 #include "util.h"
116 #define BIG_BUFSIZ (BUFSIZ * 4)
117
118 /*
119 * RFC 1738 defines that these characters should be escaped, as well
120 * any non-US-ASCII character or anything between 0x00 - 0x1F.
121 */
122 static char rfc1738_unsafe_chars[] =
123 {
124 (char) 0x3C, /* < */
125 (char) 0x3E, /* > */
126 (char) 0x22, /* " */
127 (char) 0x23, /* # */
128 (char) 0x25, /* % */
129 (char) 0x7B, /* { */
130 (char) 0x7D, /* } */
131 (char) 0x7C, /* | */
132 (char) 0x5C, /* \ */
133 (char) 0x5E, /* ^ */
134 (char) 0x7E, /* ~ */
135 (char) 0x5B, /* [ */
136 (char) 0x5D, /* ] */
137 (char) 0x60, /* ` */
138 (char) 0x27, /* ' */
139 (char) 0x20 /* space */
140 };
141
142 /*
143 * rfc1738_escape - Returns a static buffer contains the RFC 1738
144 * compliant, escaped version of the given url.
145 */
146 char *
147 rfc1738_escape(char *url)
148 {
149 static char buf[BIG_BUFSIZ];
150 char *p, *q;
151 int i, do_escape;
152
153 for (p = url, q = &buf[0]; *p != '\0'; p++, q++) {
154 do_escape = 0;
155
156 /* RFC 1738 defines these chars as unsafe */
157 for (i = 0; i < sizeof(rfc1738_unsafe_chars); i++) {
158 if (*p == rfc1738_unsafe_chars[i]) {
159 do_escape = 1;
160 break;
161 }
162 }
163 /* RFC 1738 says any control chars (0x00-0x1F) are encoded */
164 if ((unsigned char) *p <= (unsigned char) 0x1F) {
165 do_escape = 1;
166 }
167 /* RFC 1738 says 0x7f is encoded */
168 if (*p == (char) 0x7F) {
169 do_escape = 1;
170 }
171 /* RFC 1738 says any non-US-ASCII are encoded */
172 if (((unsigned char) *p >= (unsigned char) 0x80) &&
173 ((unsigned char) *p <= (unsigned char) 0xFF)) {
174 do_escape = 1;
175 }
176 /* Do the triplet encoding, or just copy the char */
177 if (do_escape == 1) {
178 (void) sprintf(q, "%%%02x", (unsigned char) *p);
179 q += sizeof(char) * 2;
180 } else {
181 *q = *p;
182 }
183 }
184 *q = '\0';
185 return (buf);
186 }
187
188 /*
189 * rfc1738_unescape() - Converts escaped characters (%xy numbers) in
190 * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab"
191 */
192 void
193 rfc1738_unescape(char *s)
194 {
195 char hexnum[3];
196 int i, j; /* i is write, j is read */
197 unsigned int x;
198
199 for (i = j = 0; s[j]; i++, j++) {
200 s[i] = s[j];
201 if (s[i] == '%') {
202 hexnum[0] = s[++j];
203 if (hexnum[0] != '%') {
204 hexnum[1] = s[++j];
205 hexnum[2] = '\0';
206 sscanf(hexnum, "%x", &x);
207 s[i] = (char) (0x0ff & x);
208 } else {
209 s[i] = '%';
210 }
211 }
212 }
213 s[i] = '\0';
214 }