[asterisk-commits] dvossel: branch dvossel/sip_uri_encode_decode r238131 - in /team/dvossel/sip_...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 6 12:22:34 CST 2010


Author: dvossel
Date: Wed Jan  6 12:22:30 2010
New Revision: 238131

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=238131
Log:
merging of the uri encode fixes with the decode fixes


Added:
    team/dvossel/sip_uri_encode_decode/main/test_utils.c   (with props)
Modified:
    team/dvossel/sip_uri_encode_decode/include/asterisk/utils.h
    team/dvossel/sip_uri_encode_decode/main/test.c
    team/dvossel/sip_uri_encode_decode/main/utils.c

Modified: team/dvossel/sip_uri_encode_decode/include/asterisk/utils.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_uri_encode_decode/include/asterisk/utils.h?view=diff&rev=238131&r1=238130&r2=238131
==============================================================================
--- team/dvossel/sip_uri_encode_decode/include/asterisk/utils.h (original)
+++ team/dvossel/sip_uri_encode_decode/include/asterisk/utils.h Wed Jan  6 12:22:30 2010
@@ -248,22 +248,25 @@
  */
 int ast_base64decode(unsigned char *dst, const char *src, int max);
 
-/*!  \brief Turn text string to URI-encoded %XX version 
-
-\note 	At this point, we're converting from ISO-8859-x (8-bit), not UTF8
-	as in the SIP protocol spec 
-	If doreserved == 1 we will convert reserved characters also.
-	RFC 2396, section 2.4
-	outbuf needs to have more memory allocated than the instring
-	to have room for the expansion. Every char that is converted
-	is replaced by three ASCII characters.
-	\param string	String to be converted
-	\param outbuf	Resulting encoded string
-	\param buflen	Size of output buffer
-	\param doreserved	Convert reserved characters
-*/
-
-char *ast_uri_encode(const char *string, char *outbuf, int buflen, int doreserved);
+/*! \brief Turn text string to URI-encoded %XX version 
+ *
+ * \note 
+ *  At this point, this function is encoding agnostic; it does not
+ *  check whether it is fed legal UTF-8. We escape control
+ *  characters (\x00-\x1F\x7F), '%', and all characters above 0x7F.
+ *  If do_special_char == 1 we will convert all characters except alnum
+ *  and the mark set.
+ *  Outbuf needs to have more memory allocated than the instring
+ *  to have room for the expansion. Every char that is converted
+ *  is replaced by three ASCII characters.
+ *
+ *  \param string	String to be converted
+ *  \param outbuf	Resulting encoded string
+ *  \param buflen	Size of output buffer
+ *  \param do_special_char	Convert all non alphanum characters execept
+ *         those in the mark set as defined by rfc 3261 section 25.1
+ */
+char *ast_uri_encode(const char *string, char *outbuf, int buflen, int do_special_char);
 
 /*!	\brief Decode URI, URN, URL (overwrite string)
 	\param s	String to be decoded 
@@ -755,4 +758,8 @@
  */
 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
 
+/*!
+ * \brief Registers util api unit tests
+ */
+void ast_utils_register_tests(void);
 #endif /* _ASTERISK_UTILS_H */

Modified: team/dvossel/sip_uri_encode_decode/main/test.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_uri_encode_decode/main/test.c?view=diff&rev=238131&r1=238130&r2=238131
==============================================================================
--- team/dvossel/sip_uri_encode_decode/main/test.c (original)
+++ team/dvossel/sip_uri_encode_decode/main/test.c Wed Jan  6 12:22:30 2010
@@ -837,8 +837,8 @@
 	/* Register cli commands */
 	ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
 
-	/* in the future this function could be used to register functions not
-	 * defined within a module */
+	/* register functions not defined within a module here */
+	ast_utils_register_tests();
 #endif
 
 	return 0;

Added: team/dvossel/sip_uri_encode_decode/main/test_utils.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_uri_encode_decode/main/test_utils.c?view=auto&rev=238131
==============================================================================
--- team/dvossel/sip_uri_encode_decode/main/test_utils.c (added)
+++ team/dvossel/sip_uri_encode_decode/main/test_utils.c Wed Jan  6 12:22:30 2010
@@ -1,0 +1,99 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2010, Digium, Inc.
+ *
+ * David Vossel <dvossel at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Unit Tests for utils API
+ *
+ * \author David Vossel <dvossel at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include "asterisk/_private.h"
+#include "asterisk/utils.h"
+#include "asterisk/test.h"
+
+AST_TEST_DEFINE(uri_encode_decode_test)
+{
+	int res = AST_TEST_PASS;
+	const char *in = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
+	const char *expected1 = "abcdefghijklmnopurstuvwxyz%20ABCDEFGHIJKLMNOPQRSTUVWXYZ%201234567890%20~%60!%40%23%24%25%5E%26*()_-%2B%3D%7B%5B%7D%5D%7C%5C%3A%3B%22'%3C%2C%3E.%3F%2F";
+	const char *expected2 = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%25^&*()_-+={[}]|\\:;\"'<,>.?/";
+	char out[256] = { 0 };
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "uri_encode_decode_test";
+		info->category = "main/utils/";
+		info->summary = "encode and decode a hex escaped string";
+		info->description = "encode a string, verify encoded string matches what we expect.  Decode the encoded string, verify decoded string matches the original string.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_status_update(&args->status_update, "Input before executing ast_uri_encode:\n%s\n", in);
+	ast_test_status_update(&args->status_update, "Output expected for ast_uri_encode with enabling do_special_char:\n%s\n", expected1);
+	ast_test_status_update(&args->status_update, "Output expected for ast_uri_encode with out enabling do_special_char:\n%s\n\n", expected2);
+
+	/* Test with do_special_char enabled */
+	ast_uri_encode(in, out, sizeof(out), 1);
+	ast_test_status_update(&args->status_update, "Output after enabling do_special_char:\n%s\n", out);
+	if (strcmp(expected1, out)) {
+		ast_test_status_update(&args->status_update, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n");
+		res = AST_TEST_FAIL;
+	}
+
+	/* Verify uri decode matches original */
+	ast_uri_decode(out);
+	if (strcmp(in, out)) {
+		ast_test_status_update(&args->status_update, "Decoded string did not match original input\n\n");
+		res = AST_TEST_FAIL;
+	} else {
+		ast_test_status_update(&args->status_update, "Decoded string matched original input\n\n");
+	}
+
+	/* Test with do_special_char disabled */
+	out[0] = '\0';
+	ast_uri_encode(in, out, sizeof(out), 0);
+	ast_test_status_update(&args->status_update, "Output after disabling do_special_char:\n%s\n", out);
+	if (strcmp(expected2, out)) {
+		ast_test_status_update(&args->status_update, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n");
+		res = AST_TEST_FAIL;
+	}
+
+	/* Verify uri decode matches original */
+	ast_uri_decode(out);
+	if (strcmp(in, out)) {
+		ast_test_status_update(&args->status_update, "Decoded string did not match original input\n\n");
+		res = AST_TEST_FAIL;
+	} else {
+		ast_test_status_update(&args->status_update, "Decoded string matched original input\n\n");
+	}
+	return res;
+}
+
+void ast_utils_register_tests(void)
+{
+	AST_TEST_REGISTER(uri_encode_decode_test);
+}
+
+

Propchange: team/dvossel/sip_uri_encode_decode/main/test_utils.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dvossel/sip_uri_encode_decode/main/test_utils.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/dvossel/sip_uri_encode_decode/main/test_utils.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/dvossel/sip_uri_encode_decode/main/utils.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_uri_encode_decode/main/utils.c?view=diff&rev=238131&r1=238130&r2=238131
==============================================================================
--- team/dvossel/sip_uri_encode_decode/main/utils.c (original)
+++ team/dvossel/sip_uri_encode_decode/main/utils.c Wed Jan  6 12:22:30 2010
@@ -368,41 +368,44 @@
 	b2a[(int)'/'] = 63;
 }
 
-/*! \brief  ast_uri_encode: Turn text string to URI-encoded %XX version
-\note 	At this point, we're converting from ISO-8859-x (8-bit), not UTF8
-	as in the SIP protocol spec 
-	If doreserved == 1 we will convert reserved characters also.
-	RFC 2396, section 2.4
-	outbuf needs to have more memory allocated than the instring
-	to have room for the expansion. Every char that is converted
-	is replaced by three ASCII characters.
-
-	Note: The doreserved option is needed for replaces header in
-	SIP transfers.
-*/
-char *ast_uri_encode(const char *string, char *outbuf, int buflen, int doreserved) 
-{
-	char *reserved = ";/?:@&=+$,# ";	/* Reserved chars */
-
- 	const char *ptr  = string;	/* Start with the string */
+/*! \brief Turn text string to URI-encoded %XX version 
+ *
+ * \note 
+ *  At this point, this function is encoding agnostic; it does not
+ *  check whether it is fed legal UTF-8. We escape control
+ *  characters (\x00-\x1F\x7F), '%', and all characters above 0x7F.
+ *  If do_special_char == 1 we will convert all characters except alnum
+ *  and mark.
+ *  Outbuf needs to have more memory allocated than the instring
+ *  to have room for the expansion. Every char that is converted
+ *  is replaced by three ASCII characters.
+ */
+char *ast_uri_encode(const char *string, char *outbuf, int buflen, int do_special_char)
+{
+	const char *ptr  = string;	/* Start with the string */
 	char *out = NULL;
 	char *buf = NULL;
-
+	const char *mark = "-_.!~*'()"; /* no encode set, RFC 2396 section 2.3, RFC 3261 sec 25 */
 	ast_copy_string(outbuf, string, buflen);
 
-	/* If there's no characters to convert, just go through and don't do anything */
 	while (*ptr) {
-		if ((*ptr < 32) || (doreserved && strchr(reserved, *ptr))) {
+		if ((const signed char) *ptr < 32 || *ptr == 0x7f || *ptr == '%' ||
+				(do_special_char &&
+				!(*ptr >= '0' && *ptr <= '9') &&      /* num */
+				!(*ptr >= 'A' && *ptr <= 'Z') &&      /* ALPHA */
+				!(*ptr >= 'a' && *ptr <= 'z') &&      /* alpha */
+				!strchr(mark, *ptr))) {               /* mark set */
+
 			/* Oops, we need to start working here */
 			if (!buf) {
 				buf = outbuf;
 				out = buf + (ptr - string) ;	/* Set output ptr */
 			}
-			out += sprintf(out, "%%%02x", (unsigned char) *ptr);
+			out += sprintf(out, "%%%02X", (unsigned char) *ptr);
 		} else if (buf) {
 			*out = *ptr;	/* Continue copying the string */
 			out++;
-		} 
+		}
 		ptr++;
 	}
 	if (buf)




More information about the asterisk-commits mailing list