[svn-commits] tilghman: branch 1.6.2 r277774 - in /branches/1.6.2: ./ res/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sat Jul 17 12:41:05 CDT 2010


Author: tilghman
Date: Sat Jul 17 12:41:01 2010
New Revision: 277774

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=277774
Log:
Merged revisions 277773 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
  ........
................

Modified:
    branches/1.6.2/   (props changed)
    branches/1.6.2/res/res_config_odbc.c
    branches/1.6.2/res/res_config_pgsql.c

Propchange: branches/1.6.2/
------------------------------------------------------------------------------
Binary property 'trunk-merged' - no diff available.

Modified: branches/1.6.2/res/res_config_odbc.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.2/res/res_config_odbc.c?view=diff&rev=277774&r1=277773&r2=277774
==============================================================================
--- branches/1.6.2/res/res_config_odbc.c (original)
+++ branches/1.6.2/res/res_config_odbc.c Sat Jul 17 12:41:01 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);
@@ -132,22 +168,29 @@
 	va_list aq;
 	struct custom_prepare_struct cps = { .sql = sql };
 
+	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_obj(database, 0);
 
 	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 *);
@@ -166,6 +209,7 @@
 
 	if (!stmt) {
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
@@ -174,6 +218,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;
 	}
 
@@ -181,12 +226,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++) {
@@ -199,6 +246,7 @@
 			if (var)
 				ast_variables_destroy(var);
 			ast_odbc_release_obj(obj);
+			ast_string_field_free_memory(&cps);
 			return NULL;
 		}
 
@@ -220,15 +268,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, "");
+				}
 			}
 		}
 	}
@@ -236,6 +289,7 @@
 
 	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 	ast_odbc_release_obj(obj);
+	ast_string_field_free_memory(&cps);
 	return var;
 }
 
@@ -280,19 +334,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_obj(database, 0);
-	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);
@@ -316,6 +374,7 @@
 
 	if (!stmt) {
 		ast_odbc_release_obj(obj);
+		ast_string_field_free_memory(&cps);
 		return NULL;
 	}
 
@@ -324,6 +383,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;
 	}
 
@@ -332,6 +392,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;
 	}
 
@@ -368,11 +429,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);
 				}
@@ -383,6 +448,7 @@
 
 	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 	ast_odbc_release_obj(obj);
+	ast_string_field_free_memory(&cps);
 	return cfg;
 }
 
@@ -411,20 +477,24 @@
 	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;
+
+	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_obj(database, 0))) {
 		ast_odbc_release_table(tableptr);
-		return -1;
-	}
-
-	obj = ast_odbc_request_obj(database, 0);
-	if (!obj) {
-		ast_odbc_release_table(tableptr);
+		ast_string_field_free_memory(&cps);
 		return -1;
 	}
 
@@ -432,6 +502,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 *);
@@ -458,20 +529,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: branches/1.6.2/res/res_config_pgsql.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.2/res/res_config_pgsql.c?view=diff&rev=277774&r1=277773&r2=277774
==============================================================================
--- branches/1.6.2/res/res_config_pgsql.c (original)
+++ branches/1.6.2/res/res_config_pgsql.c Sat Jul 17 12:41:01 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 svn-commits mailing list