4 * This file imported from the Squid project. The licence below is
5 * reproduced intact, but refers to files in Squid's repository, not
6 * in Samba. See COPYING for the GPLv3 notice (being the later
7 * version mentioned below).
9 * This file has also been modified, in particular to use talloc to
10 * allocate in rfc1738_escape()
12 * - Andrew Bartlett Oct-2009
21 * AUTHOR: Harvest Derived
23 * SQUID Web Proxy Cache http://www.squid-cache.org/
24 * ----------------------------------------------------------
26 * Squid is the result of efforts by numerous individuals from
27 * the Internet community; see the CONTRIBUTORS file for full
28 * details. Many organizations have provided support for Squid's
29 * development; see the SPONSORS file for full details. Squid is
30 * Copyrighted (C) 2001 by the Regents of the University of
31 * California; see the COPYRIGHT file for full details. Squid
32 * incorporates software developed and/or copyrighted by other
33 * sources; see the CREDITS file for full details.
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
53 #include "lib/util/samba_util.h"
56 * RFC 1738 defines that these characters should be escaped, as well
57 * any non-US-ASCII character or anything between 0x00 - 0x1F.
59 static char rfc1738_unsafe_chars[] = {
64 #if 0 /* done in code */
77 (char) 0x20 /* space */
80 static char rfc1738_reserved_chars[] = {
91 * rfc1738_escape - Returns a static buffer contains the RFC 1738
92 * compliant, escaped version of the given url.
96 rfc1738_do_escape(TALLOC_CTX *mem_ctx, const char *url, int encode_reserved)
102 unsigned int i, do_escape;
104 bufsize = strlen(url) * 3 + 1;
105 buf = talloc_array(mem_ctx, char, bufsize);
110 talloc_set_name_const(buf, buf);
113 for (p = url, q = buf; *p != '\0' && q < (buf + bufsize - 1); p++, q++) {
116 /* RFC 1738 defines these chars as unsafe */
117 for (i = 0; i < sizeof(rfc1738_unsafe_chars); i++) {
118 if (*p == rfc1738_unsafe_chars[i]) {
123 /* Handle % separately */
124 if (encode_reserved >= 0 && *p == '%')
126 /* RFC 1738 defines these chars as reserved */
127 for (i = 0; i < sizeof(rfc1738_reserved_chars) && encode_reserved > 0; i++) {
128 if (*p == rfc1738_reserved_chars[i]) {
133 /* RFC 1738 says any control chars (0x00-0x1F) are encoded */
134 if ((unsigned char) *p <= (unsigned char) 0x1F) {
137 /* RFC 1738 says 0x7f is encoded */
138 if (*p == (char) 0x7F) {
141 /* RFC 1738 says any non-US-ASCII are encoded */
142 if (((unsigned char) *p >= (unsigned char) 0x80)) {
145 /* Do the triplet encoding, or just copy the char */
146 /* note: while we do not need snprintf here as q is appropriately
147 * allocated, Samba does to avoid our macro banning it -- abartlet */
149 if (do_escape == 1) {
150 (void) snprintf(q, 4, "%%%02X", (unsigned char) *p);
151 q += sizeof(char) * 2;
161 * rfc1738_escape - Returns a buffer that contains the RFC
162 * 1738 compliant, escaped version of the given url. (escapes unsafe and % characters)
165 rfc1738_escape(TALLOC_CTX *mem_ctx, const char *url)
167 return rfc1738_do_escape(mem_ctx, url, 0);
171 * rfc1738_escape_unescaped - Returns a buffer that contains
172 * the RFC 1738 compliant, escaped version of the given url (escapes unsafe chars only)
175 rfc1738_escape_unescaped(TALLOC_CTX *mem_ctx, const char *url)
177 return rfc1738_do_escape(mem_ctx, url, -1);
181 * rfc1738_escape_part - Returns a buffer that contains the RFC
182 * 1738 compliant, escaped version of the given url segment. (escapes
183 * unsafe, reserved and % chars) It would mangle the :// in http://,
184 * and mangle paths (because of /).
187 rfc1738_escape_part(TALLOC_CTX *mem_ctx, const char *url)
189 return rfc1738_do_escape(mem_ctx, url, 1);
193 * rfc1738_unescape() - Converts escaped characters (%xy numbers) in
194 * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab"
197 rfc1738_unescape(char *s)
200 int i, j; /* i is write, j is read */
202 for (i = j = 0; s[j]; i++, j++) {
206 if (s[j + 1] == '%') { /* %% case */
210 if (s[j + 1] && s[j + 2]) {
211 if (s[j + 1] == '0' && s[j + 2] == '0') { /* %00 case */
215 hexnum[0] = s[j + 1];
216 hexnum[1] = s[j + 2];
218 if (1 == sscanf(hexnum, "%x", &x)) {
219 s[i] = (char) (0x0ff & x);