From 7a290130bdeb411625f16451af3f2cfd25eeaf00 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Fri, 30 Oct 2009 08:58:34 +1100 Subject: lib/util Use rfc1738.c from Squid for all our URL encode/decode needs. Andrew Bartlett --- lib/util/config.mk | 1 + lib/util/rfc1738.c | 74 +++++++++++++++++++++++++++++++++--------------------- lib/util/util.c | 40 ----------------------------- lib/util/util.h | 25 ++++++++++++++++++ 4 files changed, 71 insertions(+), 69 deletions(-) (limited to 'lib') diff --git a/lib/util/config.mk b/lib/util/config.mk index 9f33b0f568..b6125563fb 100644 --- a/lib/util/config.mk +++ b/lib/util/config.mk @@ -17,6 +17,7 @@ LIBSAMBA-UTIL_OBJ_FILES = $(addprefix $(libutilsrcdir)/, \ genrand.o \ dprintf.o \ util_str.o \ + rfc1738.o \ substitute.o \ util_strlist.o \ util_file.o \ diff --git a/lib/util/rfc1738.c b/lib/util/rfc1738.c index b202703f09..1de319349f 100644 --- a/lib/util/rfc1738.c +++ b/lib/util/rfc1738.c @@ -1,3 +1,19 @@ +/* + * NOTE: + * + * This file imported from the Squid project. The licence below is + * reproduced intact, but refers to files in Squid's repository, not + * in Samba. See COPYING for the GPLv3 notice (being the later + * version mentioned below). + * + * This file has also been modified, in particular to use talloc to + * allocate in rfc1738_escape() + * + * - Andrew Bartlett Oct-2009 + * + */ + + /* * $Id$ * @@ -32,14 +48,7 @@ * */ -#include "config.h" - -#if HAVE_STDIO_H -#include -#endif -#if HAVE_STRING_H -#include -#endif +#include "includes.h" #include "util.h" @@ -81,21 +90,26 @@ static char rfc1738_reserved_chars[] = { /* * rfc1738_escape - Returns a static buffer contains the RFC 1738 * compliant, escaped version of the given url. + * */ static char * -rfc1738_do_escape(const char *url, int encode_reserved) +rfc1738_do_escape(TALLOC_CTX *mem_ctx, const char *url, int encode_reserved) { - static char *buf; - static size_t bufsize = 0; + size_t bufsize = 0; const char *p; + char *buf; char *q; unsigned int i, do_escape; - if (buf == NULL || strlen(url) * 3 > bufsize) { - xfree(buf); - bufsize = strlen(url) * 3 + 1; - buf = xcalloc(bufsize, 1); + bufsize = strlen(url) * 3 + 1; + buf = talloc_array(mem_ctx, char, bufsize); + if (!buf) { + return NULL; } + + talloc_set_name_const(buf, buf); + buf[0] = '\0'; + for (p = url, q = buf; *p != '\0' && q < (buf + bufsize - 1); p++, q++) { do_escape = 0; @@ -129,11 +143,11 @@ rfc1738_do_escape(const char *url, int encode_reserved) do_escape = 1; } /* Do the triplet encoding, or just copy the char */ - /* note: we do not need snprintf here as q is appropriately - * allocated - KA */ + /* note: while we do not need snprintf here as q is appropriately + * allocated, Samba does to avoid our macro banning it -- abartlet */ if (do_escape == 1) { - (void) sprintf(q, "%%%02X", (unsigned char) *p); + (void) snprintf(q, 4, "%%%02X", (unsigned char) *p); q += sizeof(char) * 2; } else { *q = *p; @@ -145,39 +159,41 @@ rfc1738_do_escape(const char *url, int encode_reserved) /* * rfc1738_escape - Returns a static buffer that contains the RFC - * 1738 compliant, escaped version of the given url. + * 1738 compliant, escaped version of the given url. (escapes unsafe and % characters) */ char * -rfc1738_escape(const char *url) +rfc1738_escape(TALLOC_CTX *mem_ctx, const char *url) { - return rfc1738_do_escape(url, 0); + return rfc1738_do_escape(mem_ctx, url, 0); } /* * rfc1738_escape_unescaped - Returns a static buffer that contains - * the RFC 1738 compliant, escaped version of the given url. + * the RFC 1738 compliant, escaped version of the given url (escapes unsafe chars only) */ char * -rfc1738_escape_unescaped(const char *url) +rfc1738_escape_unescaped(TALLOC_CTX *mem_ctx, const char *url) { - return rfc1738_do_escape(url, -1); + return rfc1738_do_escape(mem_ctx, url, -1); } /* - * rfc1738_escape_part - Returns a static buffer that contains the - * RFC 1738 compliant, escaped version of the given url segment. + * rfc1738_escape_part - Returns a static buffer that contains the RFC + * 1738 compliant, escaped version of the given url segment. (escapes + * unsafe, reserved and % chars) It would mangle the :// in http://, + * and mangle paths (because of /). */ char * -rfc1738_escape_part(const char *url) +rfc1738_escape_part(TALLOC_CTX *mem_ctx, const char *url) { - return rfc1738_do_escape(url, 1); + return rfc1738_do_escape(mem_ctx, url, 1); } /* * rfc1738_unescape() - Converts escaped characters (%xy numbers) in * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab" */ -void +_PUBLIC_ void rfc1738_unescape(char *s) { char hexnum[3]; diff --git a/lib/util/util.c b/lib/util/util.c index fd0e6b8d79..67b166b421 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -666,46 +666,6 @@ _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_ return hex_buffer; } -/** - Unescape a URL encoded string, in place. -**/ - -_PUBLIC_ void rfc1738_unescape(char *buf) -{ - char *p=buf; - - while ((p=strchr(p,'+'))) - *p = ' '; - - p = buf; - - while (p && *p && (p=strchr(p,'%'))) { - int c1 = p[1]; - int c2 = p[2]; - - if (c1 >= '0' && c1 <= '9') - c1 = c1 - '0'; - else if (c1 >= 'A' && c1 <= 'F') - c1 = 10 + c1 - 'A'; - else if (c1 >= 'a' && c1 <= 'f') - c1 = 10 + c1 - 'a'; - else {p++; continue;} - - if (c2 >= '0' && c2 <= '9') - c2 = c2 - '0'; - else if (c2 >= 'A' && c2 <= 'F') - c2 = 10 + c2 - 'A'; - else if (c2 >= 'a' && c2 <= 'f') - c2 = 10 + c2 - 'a'; - else {p++; continue;} - - *p = (c1<<4) | c2; - - memmove(p+1, p+3, strlen(p+3)+1); - p++; - } -} - /** varient of strcmp() that handles NULL ptrs **/ diff --git a/lib/util/util.h b/lib/util/util.h index c766e3dce7..159f812d98 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -307,6 +307,31 @@ _PUBLIC_ void all_string_sub(char *s,const char *pattern,const char *insert, siz **/ _PUBLIC_ void rfc1738_unescape(char *buf); + +/** + * rfc1738_escape + * Returns a static buffer that contains the RFC + * 1738 compliant, escaped version of the given url. (escapes unsafe and % characters) + **/ +_PUBLIC_ char *rfc1738_escape(TALLOC_CTX *mem_ctx, const char *url); + +/** + * rfc1738_escape_unescaped + * + * Returns a static buffer that contains + * the RFC 1738 compliant, escaped version of the given url (escapes unsafe chars only) + **/ +_PUBLIC_ char *rfc1738_escape_unescaped(TALLOC_CTX *mem_ctx, const char *url); + +/** + * rfc1738_escape_part + * Returns a static buffer that contains the RFC + * 1738 compliant, escaped version of the given url segment. (escapes + * unsafe, reserved and % chars) It would mangle the :// in http://, + * and mangle paths (because of /). + **/ +_PUBLIC_ char *rfc1738_escape_part(TALLOC_CTX *mem_ctx, const char *url); + /** format a string into length-prefixed dotted domain format, as used in NBT and in some ADS structures -- cgit v1.2.3