1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

lib/util Use rfc1738.c from Squid for all our URL encode/decode needs.

Andrew Bartlett
This commit is contained in:
Andrew Bartlett 2009-10-30 08:58:34 +11:00
parent 87195f55de
commit 7a290130bd
5 changed files with 72 additions and 70 deletions

View 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 \

View 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$
*
@ -32,14 +48,7 @@
*
*/
#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];

View 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
**/

View 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

View 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 \