lib/util Use rfc1738.c from Squid for all our URL encode/decode needs.
authorAndrew Bartlett <abartlet@samba.org>
Thu, 29 Oct 2009 21:58:34 +0000 (08:58 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 2 Nov 2009 05:36:52 +0000 (16:36 +1100)
Andrew Bartlett

lib/util/config.mk
lib/util/rfc1738.c
lib/util/util.c
lib/util/util.h
source3/Makefile.in

index 9f33b0f5689d5a4afac16dda47dff3922810ddca..b6125563fb3bb117e6f4f6a1e490adb8835fe5a3 100644 (file)
@@ -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 \
index b202703f0926f02008570dc61842c34dd61f6c6c..1de319349f59af7ea53acb3b5a8e6bf506f38e18 100644 (file)
@@ -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$
  *
  *
  */
 
-#include "config.h"
-
-#if HAVE_STDIO_H
-#include <stdio.h>
-#endif
-#if HAVE_STRING_H
-#include <string.h>
-#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];
index fd0e6b8d799c717b0b704bdc95ac0f3b6dd4e84c..67b166b4212f01d85be7c61c9c9d7c78b8a91fbd 100644 (file)
@@ -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
 **/
index c766e3dce7332a3d13963e2f48009e4b8140912a..159f812d984d78ccf45848d2c0e288e558e06917 100644 (file)
@@ -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
index 74a6c0b576bb92a64566f5abfb04cf9c0f415454..cb10a7c6f5bfe52fa41e9485aface1ad03002a44 100644 (file)
@@ -371,7 +371,7 @@ UTIL_OBJ = ../lib/util/rbtree.o ../lib/util/signal.o ../lib/util/time.o \
                   ../lib/util/become_daemon.o ../lib/util/system.o \
                   ../lib/util/tevent_unix.o ../lib/util/tevent_ntstatus.o \
                   ../lib/util/smb_threads.o ../lib/util/util_id.o \
-                  ../lib/util/blocking.o
+                  ../lib/util/blocking.o ../lib/util/rfc1738.o 
 
 CRYPTO_OBJ = ../lib/crypto/crc32.o ../lib/crypto/md5.o \
                         ../lib/crypto/hmacmd5.o ../lib/crypto/arcfour.o \