[asterisk-commits] tilghman: branch tilghman/realtime_failover r277813 - in /team/tilghman/realt...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jul 18 11:03:48 CDT 2010


Author: tilghman
Date: Sun Jul 18 11:03:42 2010
New Revision: 277813

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=277813
Log:
Merged revisions 277773,277775 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/trunk

................
  r277773 | tilghman | 2010-07-17 12:39:28 -0500 (Sat, 17 Jul 2010) | 15 lines
  
  Merged revisions 277568 via svnmerge from 
  https://origsvn.digium.com/svn/asterisk/branches/1.4
  
  ........
    r277568 | tilghman | 2010-07-16 16:54:29 -0500 (Fri, 16 Jul 2010) | 8 lines
    
    Since we split values at the semicolon, we should store values with a semicolon as an encoded value.
    
    (closes issue #17369)
     Reported by: gkservice
     Patches: 
           20100625__issue17369.diff.txt uploaded by tilghman (license 14)
     Tested by: tilghman
  ........
................
  r277775 | tilghman | 2010-07-17 12:42:32 -0500 (Sat, 17 Jul 2010) | 12 lines
  
  Merged revisions 277738 via svnmerge from 
  https://origsvn.digium.com/svn/asterisk/branches/1.4
  
  ........
    r277738 | tilghman | 2010-07-17 11:59:11 -0500 (Sat, 17 Jul 2010) | 5 lines
    
    Remove uclibc cross-compile triplet, as uclibc has a working fork()... it's only uclinux that does not.
    
    (closes issue #17616)
     Reported by: pprindeville
  ........
................

Modified:
    team/tilghman/realtime_failover/   (props changed)
    team/tilghman/realtime_failover/addons/res_config_mysql.c
    team/tilghman/realtime_failover/autoconf/ast_func_fork.m4
    team/tilghman/realtime_failover/configure
    team/tilghman/realtime_failover/include/asterisk/autoconfig.h.in
    team/tilghman/realtime_failover/include/asterisk/config.h
    team/tilghman/realtime_failover/main/config.c
    team/tilghman/realtime_failover/res/res_config_odbc.c
    team/tilghman/realtime_failover/res/res_config_pgsql.c

Propchange: team/tilghman/realtime_failover/
------------------------------------------------------------------------------
    automerge = *

Propchange: team/tilghman/realtime_failover/
------------------------------------------------------------------------------
Binary property 'branch-1.4-merged' - no diff available.

Propchange: team/tilghman/realtime_failover/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Sun Jul 18 11:03:42 2010
@@ -1,1 +1,1 @@
-/trunk:1-277734
+/trunk:1-277812

Modified: team/tilghman/realtime_failover/addons/res_config_mysql.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/realtime_failover/addons/res_config_mysql.c?view=diff&rev=277813&r1=277812&r2=277813
==============================================================================
--- team/tilghman/realtime_failover/addons/res_config_mysql.c (original)
+++ team/tilghman/realtime_failover/addons/res_config_mysql.c Sun Jul 18 11:03:42 2010
@@ -46,6 +46,7 @@
 #include "asterisk/cli.h"
 #include "asterisk/utils.h"
 #include "asterisk/threadstorage.h"
+#include "asterisk/strings.h"
 
 #define RES_CONFIG_MYSQL_CONF "res_config_mysql.conf"
 #define RES_CONFIG_MYSQL_CONF_OLD "res_mysql.conf"
@@ -54,16 +55,27 @@
 
 #define ESCAPE_STRING(buf, var) \
 	do { \
-		if ((valsz = strlen(var)) * 2 + 1 > ast_str_size(buf)) { \
-			ast_str_make_space(&(buf), valsz * 2 + 1); \
+		struct ast_str *semi = ast_str_thread_get(&scratch2_buf, strlen(var) * 3 + 1); \
+		const char *chunk = var; \
+		ast_str_reset(semi); \
+		for (; *chunk; chunk++) { \
+			if (strchr(";^", *chunk)) { \
+				ast_str_append(&semi, 0, "^%02hhX", *chunk); \
+			} else { \
+				ast_str_append(&semi, 0, "%c", *chunk); \
+			} \
 		} \
-		mysql_real_escape_string(&dbh->handle, ast_str_buffer(buf), var, valsz); \
+		if (ast_str_strlen(semi) * 2 + 1 > ast_str_size(buf)) { \
+			ast_str_make_space(&(buf), ast_str_strlen(semi) * 2 + 1); \
+		} \
+		mysql_real_escape_string(&dbh->handle, ast_str_buffer(buf), ast_str_buffer(semi), ast_str_strlen(semi)); \
 	} while (0)
 
 AST_THREADSTORAGE(sql_buf);
 AST_THREADSTORAGE(sql2_buf);
 AST_THREADSTORAGE(find_buf);
 AST_THREADSTORAGE(scratch_buf);
+AST_THREADSTORAGE(scratch2_buf);
 AST_THREADSTORAGE(modify_buf);
 AST_THREADSTORAGE(modify2_buf);
 AST_THREADSTORAGE(modify3_buf);
@@ -290,13 +302,25 @@
 	return column;
 }
 
+static char *decode_chunk(char *chunk)
+{
+	char *orig = chunk;
+	for (; *chunk; chunk++) {
+		if (*chunk == '^' && strchr("0123456789ABCDEFabcdef", chunk[1]) && strchr("0123456789ABCDEFabcdef", chunk[2])) {
+			sscanf(chunk + 1, "%02hhd", chunk);
+			memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1);
+		}
+	}
+	return orig;
+}
+
 static struct ast_variable *realtime_mysql(const char *database, const char *table, va_list ap)
 {
 	struct mysql_conn *dbh;
 	MYSQL_RES *result;
 	MYSQL_ROW row;
 	MYSQL_FIELD *fields;
-	int numFields, i, valsz;
+	int numFields, i;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 16);
 	struct ast_str *buf = ast_str_thread_get(&scratch_buf, 16);
 	char *stringp;
@@ -375,11 +399,11 @@
 				}
 				for (stringp = ast_strdupa(row[i]), chunk = strsep(&stringp, ";"); chunk; chunk = strsep(&stringp, ";")) {
 					if (prev) {
-						if ((prev->next = ast_variable_new(fields[i].name, chunk, ""))) {
+						if ((prev->next = ast_variable_new(fields[i].name, decode_chunk(chunk), ""))) {
 							prev = prev->next;
 						}
 					} else {
-						prev = var = ast_variable_new(fields[i].name, chunk, "");
+						prev = var = ast_variable_new(fields[i].name, decode_chunk(chunk), "");
 					}
 				}
 			}
@@ -400,7 +424,7 @@
 	MYSQL_RES *result;
 	MYSQL_ROW row;
 	MYSQL_FIELD *fields;
-	int numFields, i, valsz;
+	int numFields, i;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 16);
 	struct ast_str *buf = ast_str_thread_get(&scratch_buf, 16);
 	const char *initfield = NULL;
@@ -500,7 +524,7 @@
 				if (ast_strlen_zero(row[i]))
 					continue;
 				for (stringp = ast_strdupa(row[i]), chunk = strsep(&stringp, ";"); chunk; chunk = strsep(&stringp, ";")) {
-					if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
+					if (chunk && !ast_strlen_zero(decode_chunk(ast_strip(chunk)))) {
 						if (initfield && !strcmp(initfield, fields[i].name)) {
 							ast_category_rename(cat, chunk);
 						}
@@ -525,7 +549,6 @@
 {
 	struct mysql_conn *dbh;
 	my_ulonglong numrows;
-	int valsz;
 	const char *newparam, *newval;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 100), *buf = ast_str_thread_get(&scratch_buf, 100);
 	struct tables *table;
@@ -535,7 +558,7 @@
 		ast_log(LOG_WARNING, "MySQL RealTime: Invalid database specified: '%s' (check res_mysql.conf)\n", database);
 		return -1;
 	}
-		
+
 	if (!tablename) {
 		ast_log(LOG_WARNING, "MySQL RealTime: No table specified.\n");
 		release_database(dbh);
@@ -588,7 +611,7 @@
 
 	/* If the column length isn't long enough, give a chance to lengthen it. */
 	if (strncmp(column->type, "char", 4) == 0 || strncmp(column->type, "varchar", 7) == 0) {
-		internal_require(database, tablename, newparam, RQ_CHAR, valsz, SENTINEL);
+		internal_require(database, tablename, newparam, RQ_CHAR, ast_str_strlen(buf), SENTINEL);
 	}
 
 	while ((newparam = va_arg(ap, const char *))) {
@@ -605,7 +628,7 @@
 
 		/* If the column length isn't long enough, give a chance to lengthen it. */
 		if (strncmp(column->type, "char", 4) == 0 || strncmp(column->type, "varchar", 7) == 0) {
-			internal_require(database, tablename, newparam, RQ_CHAR, valsz, SENTINEL);
+			internal_require(database, tablename, newparam, RQ_CHAR, ast_str_strlen(buf), SENTINEL);
 		}
 	}
 	va_end(ap);
@@ -644,7 +667,6 @@
 	my_ulonglong numrows;
 	int first = 1;
 	const char *newparam, *newval;
-	size_t valsz;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 100), *buf = ast_str_thread_get(&scratch_buf, 100);
 	struct ast_str *where = ast_str_thread_get(&sql2_buf, 100);
 	struct tables *table;
@@ -701,7 +723,7 @@
 
 		/* If the column length isn't long enough, give a chance to lengthen it. */
 		if (strncmp(column->type, "char", 4) == 0 || strncmp(column->type, "varchar", 7) == 0) {
-			internal_require(database, tablename, newparam, RQ_CHAR, valsz, SENTINEL);
+			internal_require(database, tablename, newparam, RQ_CHAR, ast_str_strlen(buf), SENTINEL);
 		}
 	}
 
@@ -725,7 +747,7 @@
 
 		/* If the column length isn't long enough, give a chance to lengthen it. */
 		if (strncmp(column->type, "char", 4) == 0 || strncmp(column->type, "varchar", 7) == 0) {
-			internal_require(database, tablename, newparam, RQ_CHAR, valsz, SENTINEL);
+			internal_require(database, tablename, newparam, RQ_CHAR, ast_str_strlen(buf), SENTINEL);
 		}
 	}
 	va_end(ap);
@@ -764,7 +786,6 @@
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 16);
 	struct ast_str *sql2 = ast_str_thread_get(&sql2_buf, 16);
 	struct ast_str *buf = ast_str_thread_get(&scratch_buf, 16);
-	int valsz;
 	const char *newparam, *newval;
 
 	if (!(dbh = find_database(database, 1))) {
@@ -796,16 +817,15 @@
 	ast_str_set(&sql, 0, "INSERT INTO %s (%s", table, newparam);
 	ast_str_set(&sql2, 0, ") VALUES ('%s'", ast_str_buffer(buf));
 
-	internal_require(database, table, newparam, RQ_CHAR, valsz, SENTINEL);
+	internal_require(database, table, newparam, RQ_CHAR, ast_str_strlen(buf), SENTINEL);
 
 	while ((newparam = va_arg(ap, const char *))) {
 		if ((newval = va_arg(ap, const char *))) {
 			ESCAPE_STRING(buf, newval);
 		} else {
-			valsz = 0;
 			ast_str_reset(buf);
 		}
-		if (internal_require(database, table, newparam, RQ_CHAR, valsz, SENTINEL) == 0) {
+		if (internal_require(database, table, newparam, RQ_CHAR, ast_str_strlen(buf), SENTINEL) == 0) {
 			ast_str_append(&sql, 0, ", %s", newparam);
 			ast_str_append(&sql2, 0, ", '%s'", ast_str_buffer(buf));
 		}
@@ -841,7 +861,6 @@
 	my_ulonglong numrows;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 16);
 	struct ast_str *buf = ast_str_thread_get(&scratch_buf, 16);
-	int valsz;
 	const char *newparam, *newval;
 
 	if (!(dbh = find_database(database, 1))) {
@@ -1059,7 +1078,6 @@
 			ast_str_append(&sql, 0, " NOT NULL");
 		}
 		if (!ast_strlen_zero(column->dflt)) {
-			size_t valsz;
 			ESCAPE_STRING(escbuf, column->dflt);
 			ast_str_append(&sql, 0, " DEFAULT '%s'", ast_str_buffer(escbuf));
 		}

Modified: team/tilghman/realtime_failover/autoconf/ast_func_fork.m4
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/realtime_failover/autoconf/ast_func_fork.m4?view=diff&rev=277813&r1=277812&r2=277813
==============================================================================
--- team/tilghman/realtime_failover/autoconf/ast_func_fork.m4 (original)
+++ team/tilghman/realtime_failover/autoconf/ast_func_fork.m4 Sun Jul 18 11:03:42 2010
@@ -13,7 +13,7 @@
 fi
 if test "x$ac_cv_func_fork_works" = xcross; then
   case $host in
-    *-*-amigaos* | *-*-msdosdjgpp* | *-*-uclinux* | *-*-linux-uclibc* )
+    *-*-amigaos* | *-*-msdosdjgpp* | *-*-uclinux* )
       # Override, as these systems have only a dummy fork() stub
       ac_cv_func_fork_works=no
       ;;

Modified: team/tilghman/realtime_failover/include/asterisk/autoconfig.h.in
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/realtime_failover/include/asterisk/autoconfig.h.in?view=diff&rev=277813&r1=277812&r2=277813
==============================================================================
--- team/tilghman/realtime_failover/include/asterisk/autoconfig.h.in (original)
+++ team/tilghman/realtime_failover/include/asterisk/autoconfig.h.in Sun Jul 18 11:03:42 2010
@@ -803,7 +803,7 @@
 /* Define to 1 if you have the `strtoq' function. */
 #undef HAVE_STRTOQ
 
-/* Define to 1 if `st_blksize' is a member of `struct stat'. */
+/* Define to 1 if `st_blksize' is member of `struct stat'. */
 #undef HAVE_STRUCT_STAT_ST_BLKSIZE
 
 /* Define to 1 if you have the mISDN Supplemental Services library. */
@@ -1071,11 +1071,11 @@
 /* Define to the one symbol short name of this package. */
 #undef PACKAGE_TARNAME
 
-/* Define to the home page for this package. */
-#undef PACKAGE_URL
-
 /* Define to the version of this package. */
 #undef PACKAGE_VERSION
+
+/* Define to 1 if the C compiler supports function prototypes. */
+#undef PROTOTYPES
 
 /* Define to necessary symbol if this constant uses a non-standard name on
    your system. */
@@ -1095,6 +1095,11 @@
 
 /* Define to the type of arg 5 for `select'. */
 #undef SELECT_TYPE_ARG5
+
+/* Define to 1 if the `setvbuf' function takes the buffering type as its
+   second argument and the buffer pointer as the third, as on System V before
+   release 3. */
+#undef SETVBUF_REVERSED
 
 /* The size of `char *', as computed by sizeof. */
 #undef SIZEOF_CHAR_P
@@ -1125,46 +1130,50 @@
 /* Define to 1 if your <sys/time.h> declares `struct tm'. */
 #undef TM_IN_SYS_TIME
 
-/* Enable extensions on AIX 3, Interix.  */
+/* Define to 1 if on AIX 3.
+   System headers sometimes define this.
+   We just want to avoid a redefinition error message.  */
 #ifndef _ALL_SOURCE
 # undef _ALL_SOURCE
 #endif
+
+/* Number of bits in a file offset, on hosts where this is settable. */
+#undef _FILE_OFFSET_BITS
+
 /* Enable GNU extensions on systems that have them.  */
 #ifndef _GNU_SOURCE
 # undef _GNU_SOURCE
 #endif
-/* Enable threading extensions on Solaris.  */
+
+/* Define to 1 to make fseeko visible on some hosts (e.g. glibc 2.2). */
+#undef _LARGEFILE_SOURCE
+
+/* Define for large files, on AIX-style hosts. */
+#undef _LARGE_FILES
+
+/* Define to 1 if on MINIX. */
+#undef _MINIX
+
+/* Define to 2 if the system does not provide POSIX.1 features except with
+   this defined. */
+#undef _POSIX_1_SOURCE
+
+/* Define to 1 if you need to in order for `stat' and other things to work. */
+#undef _POSIX_SOURCE
+
+/* Enable extensions on Solaris.  */
+#ifndef __EXTENSIONS__
+# undef __EXTENSIONS__
+#endif
 #ifndef _POSIX_PTHREAD_SEMANTICS
 # undef _POSIX_PTHREAD_SEMANTICS
 #endif
-/* Enable extensions on HP NonStop.  */
 #ifndef _TANDEM_SOURCE
 # undef _TANDEM_SOURCE
 #endif
-/* Enable general extensions on Solaris.  */
-#ifndef __EXTENSIONS__
-# undef __EXTENSIONS__
-#endif
-
-
-/* Number of bits in a file offset, on hosts where this is settable. */
-#undef _FILE_OFFSET_BITS
-
-/* Define to 1 to make fseeko visible on some hosts (e.g. glibc 2.2). */
-#undef _LARGEFILE_SOURCE
-
-/* Define for large files, on AIX-style hosts. */
-#undef _LARGE_FILES
-
-/* Define to 1 if on MINIX. */
-#undef _MINIX
-
-/* Define to 2 if the system does not provide POSIX.1 features except with
-   this defined. */
-#undef _POSIX_1_SOURCE
-
-/* Define to 1 if you need to in order for `stat' and other things to work. */
-#undef _POSIX_SOURCE
+
+/* Define like PROTOTYPES; this can be used by system headers. */
+#undef __PROTOTYPES
 
 /* Define to empty if `const' does not conform to ANSI C. */
 #undef const

Modified: team/tilghman/realtime_failover/include/asterisk/config.h
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/realtime_failover/include/asterisk/config.h?view=diff&rev=277813&r1=277812&r2=277813
==============================================================================
--- team/tilghman/realtime_failover/include/asterisk/config.h (original)
+++ team/tilghman/realtime_failover/include/asterisk/config.h Sun Jul 18 11:03:42 2010
@@ -729,6 +729,27 @@
 }
 )
 
+/*!
+ * \brief Remove standard encoding from realtime values, which ensures
+ * that a semicolon embedded within a single value is not treated upon
+ * retrieval as multiple values.
+ * \param chunk Data to be decoded
+ * \return The decoded data, in the original buffer
+ * \since 1.8
+ * \warn This function modifies the original buffer
+ */
+char *ast_realtime_decode_chunk(char *chunk);
+
+/*!
+ * \brief Encodes a chunk of data for realtime
+ * \param dest Destination buffer
+ * \param maxlen Length passed through to ast_str_* functions
+ * \param chunk Source data to be encoded
+ * \return Buffer within dest
+ * \since 1.8
+ */
+char *ast_realtime_encode_chunk(struct ast_str **dest, ssize_t maxlen, const char *chunk);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/tilghman/realtime_failover/main/config.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/realtime_failover/main/config.c?view=diff&rev=277813&r1=277812&r2=277813
==============================================================================
--- team/tilghman/realtime_failover/main/config.c (original)
+++ team/tilghman/realtime_failover/main/config.c Sun Jul 18 11:03:42 2010
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2010, Digium, Inc.
  *
  * Mark Spencer <markster at digium.com>
  *
@@ -2356,6 +2356,35 @@
 	return res;
 }
 
+char *ast_realtime_decode_chunk(char *chunk)
+{
+	char *orig = chunk;
+	for (; *chunk; chunk++) {
+		if (*chunk == '^' && strchr("0123456789ABCDEFabcdef", chunk[1]) && strchr("0123456789ABCDEFabcdef", chunk[2])) {
+			sscanf(chunk + 1, "%02hhd", chunk);
+			memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1);
+		}
+	}
+	return orig;
+}
+
+char *ast_realtime_encode_chunk(struct ast_str **dest, ssize_t maxlen, const char *chunk)
+{
+	if (!strchr(chunk, ';') && !strchr(chunk, '^')) {
+		ast_str_set(dest, maxlen, "%s", chunk);
+	} else {
+		ast_str_reset(*dest);
+		for (; *chunk; chunk++) {
+			if (strchr(";^", *chunk)) {
+				ast_str_append(dest, maxlen, "^%02hhX", *chunk);
+			} else {
+				ast_str_append(dest, maxlen, "%c", *chunk);
+			}
+		}
+	}
+	return ast_str_buffer(*dest);
+}
+
 /*! \brief Helper function to parse arguments
  * See documentation in config.h
  */

Modified: team/tilghman/realtime_failover/res/res_config_odbc.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/realtime_failover/res/res_config_odbc.c?view=diff&rev=277813&r1=277812&r2=277813
==============================================================================
--- team/tilghman/realtime_failover/res/res_config_odbc.c (original)
+++ team/tilghman/realtime_failover/res/res_config_odbc.c Sun Jul 18 11:03:42 2010
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2010, Digium, Inc.
  *
  * Mark Spencer <markster at digium.com>
  *
@@ -44,21 +44,36 @@
 #include "asterisk/lock.h"
 #include "asterisk/res_odbc.h"
 #include "asterisk/utils.h"
+#include "asterisk/stringfields.h"
 
 AST_THREADSTORAGE(sql_buf);
 
 struct custom_prepare_struct {
 	const char *sql;
 	const char *extra;
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(encoding)[256];
+	);
 	va_list ap;
 	unsigned long long skip;
 };
 
+static void decode_chunk(char *chunk)
+{
+	for (; *chunk; chunk++) {
+		if (*chunk == '^' && strchr("0123456789ABCDEFabcdef", chunk[1]) && strchr("0123456789ABCDEFabcdef", chunk[2])) {
+			sscanf(chunk + 1, "%02hhd", chunk);
+			memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1);
+		}
+	}
+}
+
 static SQLHSTMT custom_prepare(struct odbc_obj *obj, void *data)
 {
 	int res, x = 1, count = 0;
 	struct custom_prepare_struct *cps = data;
 	const char *newparam, *newval;
+	char encodebuf[1024];
 	SQLHSTMT stmt;
 	va_list ap;
 
@@ -86,6 +101,27 @@
 			continue;
 		}
 		ast_debug(1, "Parameter %d ('%s') = '%s'\n", x, newparam, newval);
+		if (strchr(newval, ';') || strchr(newval, '^')) {
+			char *eptr = encodebuf;
+			const char *vptr = newval;
+			for (; *vptr && eptr < encodebuf + sizeof(encodebuf); vptr++) {
+				if (strchr("^;", *vptr)) {
+					/* We use ^XX, instead of %XX because '%' is a special character in SQL */
+					snprintf(eptr, encodebuf + sizeof(encodebuf) - eptr, "^%02hhX", *vptr);
+					eptr += 3;
+					vptr++;
+				} else {
+					*eptr++ = *vptr++;
+				}
+			}
+			if (eptr < encodebuf + sizeof(encodebuf)) {
+				*eptr = '\0';
+			} else {
+				encodebuf[sizeof(encodebuf) - 1] = '\0';
+			}
+			ast_string_field_set(cps, encoding[x], encodebuf);
+			newval = cps->encoding[x];
+		}
 		SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
 	}
 	va_end(ap);
@@ -133,22 +169,29 @@
 	struct custom_prepare_struct cps = { .sql = sql };
 	struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
 
+	if (ast_string_field_init(&cps, 256)) {
+		return NULL;
+	}
 	va_copy(cps.ap, ap);
 	va_copy(aq, ap);
 
-	if (!table)
-		return NULL;
+	if (!table) {
+		ast_string_field_free_memory(&cps);
+		return NULL;
+	}
 
 	obj = ast_odbc_request_obj2(database, connected_flag);
 
 	if (!obj) {
 		ast_log(LOG_ERROR, "No database handle available with the name of '%s' (check res_odbc.conf)\n", database);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
 	newparam = va_arg(aq, const char *);
 	if (!newparam) {
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 	newval = va_arg(aq, const char *);
@@ -167,6 +210,7 @@
 
 	if (!stmt) {
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
@@ -175,6 +219,7 @@
 		ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
@@ -182,12 +227,14 @@
 	if (res == SQL_NO_DATA) {
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 	for (x = 0; x < colcount; x++) {
@@ -200,6 +247,7 @@
 			if (var)
 				ast_variables_destroy(var);
 			ast_odbc_release_obj(obj);
+			ast_string_field_free_memory(&cps);
 			return NULL;
 		}
 
@@ -221,15 +269,20 @@
 			return NULL;
 		}
 		stringp = rowdata;
-		while(stringp) {
+		while (stringp) {
 			chunk = strsep(&stringp, ";");
 			if (!ast_strlen_zero(ast_strip(chunk))) {
+				if (strchr(chunk, '^')) {
+					decode_chunk(chunk);
+				}
 				if (prev) {
 					prev->next = ast_variable_new(coltitle, chunk, "");
-					if (prev->next)
+					if (prev->next) {
 						prev = prev->next;
-				} else 
+					}
+				} else {
 					prev = var = ast_variable_new(coltitle, chunk, "");
+				}
 			}
 		}
 	}
@@ -237,6 +290,7 @@
 
 	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 	ast_odbc_release_obj(obj);
+	ast_string_field_free_memory(&cps);
 	return var;
 }
 
@@ -282,19 +336,23 @@
 	struct custom_prepare_struct cps = { .sql = sql };
 	va_list aq;
 
+	if (!table || ast_string_field_init(&cps, 256)) {
+		return NULL;
+	}
 	va_copy(cps.ap, ap);
 	va_copy(aq, ap);
 
-	if (!table)
-		return NULL;
 
 	obj = ast_odbc_request_obj2(database, connected_flag);
-	if (!obj)
-		return NULL;
+	if (!obj) {
+		ast_string_field_free_memory(&cps);
+		return NULL;
+	}
 
 	newparam = va_arg(aq, const char *);
 	if (!newparam)  {
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 	initfield = ast_strdupa(newparam);
@@ -318,6 +376,7 @@
 
 	if (!stmt) {
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
@@ -326,6 +385,7 @@
 		ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
 		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
@@ -334,6 +394,7 @@
 		ast_log(LOG_WARNING, "Out of memory!\n");
 		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
@@ -370,11 +431,15 @@
 				continue;
 			}
 			stringp = rowdata;
-			while(stringp) {
+			while (stringp) {
 				chunk = strsep(&stringp, ";");
 				if (!ast_strlen_zero(ast_strip(chunk))) {
-					if (initfield && !strcmp(initfield, coltitle))
+					if (strchr(chunk, '^')) {
+						decode_chunk(chunk);
+					}
+					if (initfield && !strcmp(initfield, coltitle)) {
 						ast_category_rename(cat, chunk);
+					}
 					var = ast_variable_new(coltitle, chunk, "");
 					ast_variable_append(cat, var);
 				}
@@ -385,6 +450,7 @@
 
 	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 	ast_odbc_release_obj(obj);
+	ast_string_field_free_memory(&cps);
 	return cfg;
 }
 
@@ -413,21 +479,25 @@
 	int res, count = 1;
 	va_list aq;
 	struct custom_prepare_struct cps = { .sql = sql, .extra = lookup };
-	struct odbc_cache_tables *tableptr = ast_odbc_find_table(database, table);
+	struct odbc_cache_tables *tableptr;
 	struct odbc_cache_columns *column;
 	struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
 
+	if (!table) {
+		return -1;
+	}
+
 	va_copy(cps.ap, ap);
 	va_copy(aq, ap);
-	
-	if (!table) {
+
+	if (ast_string_field_init(&cps, 256)) {
+		return -1;
+	}
+
+	tableptr = ast_odbc_find_table(database, table);
+	if (!(obj = ast_odbc_request_obj2(database, connected_flag))) {
 		ast_odbc_release_table(tableptr);
-		return -1;
-	}
-
-	obj = ast_odbc_request_obj2(database, connected_flag);
-	if (!obj) {
-		ast_odbc_release_table(tableptr);
+		ast_string_field_free_memory(&cps);
 		return -1;
 	}
 
@@ -435,6 +505,7 @@
 	if (!newparam)  {
 		ast_odbc_release_obj(obj);
 		ast_odbc_release_table(tableptr);
+		ast_string_field_free_memory(&cps);
 		return -1;
 	}
 	newval = va_arg(aq, const char *);
@@ -461,20 +532,23 @@
 
 	if (!stmt) {
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return -1;
 	}
 
 	res = SQLRowCount(stmt, &rowcount);
 	SQLFreeHandle (SQL_HANDLE_STMT, stmt);
 	ast_odbc_release_obj(obj);
+	ast_string_field_free_memory(&cps);
 
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
 		return -1;
 	}
 
-	if (rowcount >= 0)
-		return (int)rowcount;
+	if (rowcount >= 0) {
+		return (int) rowcount;
+	}
 
 	return -1;
 }

Modified: team/tilghman/realtime_failover/res/res_config_pgsql.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/realtime_failover/res/res_config_pgsql.c?view=diff&rev=277813&r1=277812&r2=277813
==============================================================================
--- team/tilghman/realtime_failover/res/res_config_pgsql.c (original)
+++ team/tilghman/realtime_failover/res/res_config_pgsql.c Sun Jul 18 11:03:42 2010
@@ -1,8 +1,8 @@
 /*
  * Asterisk -- A telephony toolkit for Linux.
  *
- * Copyright (C) 1999-2005, Digium, Inc.
- * 
+ * Copyright (C) 1999-2010, Digium, Inc.
+ *
  * Manuel Guesdon <mguesdon at oxymium.net> - PostgreSQL RealTime Driver Author/Adaptor
  * Mark Spencer <markster at digium.com>  - Asterisk Author
  * Matthew Boehm <mboehm at cytelcom.com> - MySQL RealTime Driver Author
@@ -46,6 +46,7 @@
 AST_THREADSTORAGE(findtable_buf);
 AST_THREADSTORAGE(where_buf);
 AST_THREADSTORAGE(escapebuf_buf);
+AST_THREADSTORAGE(semibuf_buf);
 
 #define RES_CONFIG_PGSQL_CONF "res_pgsql.conf"
 
@@ -95,11 +96,21 @@
 
 #define ESCAPE_STRING(buffer, stringname) \
 	do { \
-		int len; \
-		if ((len = strlen(stringname)) > (ast_str_size(buffer) - 1) / 2) { \
-			ast_str_make_space(&buffer, len * 2 + 1); \
+		int len = strlen(stringname); \
+		struct ast_str *semi = ast_str_thread_get(&semibuf_buf, len * 3 + 1); \
+		const char *chunk = stringname; \
+		ast_str_reset(semi); \
+		for (; *chunk; chunk++) { \
+			if (strchr(";^", *chunk)) { \
+				ast_str_append(&semi, 0, "^%02hhX", *chunk); \
+			} else { \
+				ast_str_append(&semi, 0, "%c", *chunk); \
+			} \
 		} \
-		PQescapeStringConn(pgsqlConn, ast_str_buffer(buffer), stringname, len, &pgresult); \
+		if (ast_str_strlen(semi) > (ast_str_size(buffer) - 1) / 2) { \
+			ast_str_make_space(&buffer, ast_str_strlen(semi) * 2 + 1); \
+		} \
+		PQescapeStringConn(pgsqlConn, ast_str_buffer(buffer), ast_str_buffer(semi), ast_str_size(buffer), &pgresult); \
 	} while (0)
 
 static void destroy_table(struct tables *table)
@@ -391,7 +402,7 @@
 				stringp = PQgetvalue(result, rowIndex, i);
 				while (stringp) {
 					chunk = strsep(&stringp, ";");
-					if (!ast_strlen_zero(ast_strip(chunk))) {
+					if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
 						if (prev) {
 							prev->next = ast_variable_new(fieldnames[i], chunk, "");
 							if (prev->next) {
@@ -550,7 +561,7 @@
 				stringp = PQgetvalue(result, rowIndex, i);
 				while (stringp) {
 					chunk = strsep(&stringp, ";");
-					if (!ast_strlen_zero(ast_strip(chunk))) {
+					if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
 						if (initfield && !strcmp(initfield, fieldnames[i])) {
 							ast_category_rename(cat, chunk);
 						}
@@ -744,7 +755,7 @@
 			release_table(table);
 			return -1;
 		}
-			
+
 		newval = va_arg(ap, const char *);
 		ESCAPE_STRING(escapebuf, newval);
 		if (pgresult) {




More information about the asterisk-commits mailing list