aboutsummaryrefslogtreecommitdiffstats
path: root/res/res_config_odbc.c
diff options
context:
space:
mode:
Diffstat (limited to 'res/res_config_odbc.c')
-rw-r--r--res/res_config_odbc.c105
1 files changed, 88 insertions, 17 deletions
diff --git a/res/res_config_odbc.c b/res/res_config_odbc.c
index 5c3c1f669..b735c0cd9 100644
--- a/res/res_config_odbc.c
+++ b/res/res_config_odbc.c
@@ -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@digium.com>
*
@@ -53,18 +53,33 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/options.h"
#include "asterisk/res_odbc.h"
#include "asterisk/utils.h"
+#include "asterisk/stringfields.h"
struct custom_prepare_struct {
const char *sql;
const char *extra;
+ AST_DECLARE_STRING_FIELDS(
+ AST_STRING_FIELD(encoding)[256];
+ );
va_list ap;
};
+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;
struct custom_prepare_struct *cps = data;
const char *newparam, *newval;
+ char encodebuf[1024];
SQLHSTMT stmt;
va_list ap;
@@ -85,6 +100,27 @@ static SQLHSTMT custom_prepare(struct odbc_obj *obj, void *data)
while ((newparam = va_arg(ap, const char *))) {
newval = va_arg(ap, const char *);
+ 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);
@@ -118,22 +154,29 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl
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)
+ 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 *);
@@ -152,6 +195,7 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl
if (!stmt) {
ast_odbc_release_obj(obj);
+ ast_string_field_free_memory(&cps);
return NULL;
}
@@ -160,6 +204,7 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl
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;
}
@@ -167,12 +212,14 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl
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++) {
@@ -185,6 +232,7 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl
if (var)
ast_variables_destroy(var);
ast_odbc_release_obj(obj);
+ ast_string_field_free_memory(&cps);
return NULL;
}
@@ -201,15 +249,20 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl
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);
+ }
}
}
}
@@ -217,6 +270,7 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj);
+ ast_string_field_free_memory(&cps);
return var;
}
@@ -248,20 +302,24 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
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;
memset(&ra, 0, sizeof(ra));
obj = ast_odbc_request_obj(database, 0);
- if (!obj)
+ 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);
@@ -285,6 +343,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
if (!stmt) {
ast_odbc_release_obj(obj);
+ ast_string_field_free_memory(&cps);
return NULL;
}
@@ -293,6 +352,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
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;
}
@@ -301,6 +361,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
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;
}
@@ -337,11 +398,15 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
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);
}
@@ -352,6 +417,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj);
+ ast_string_field_free_memory(&cps);
return cfg;
}
@@ -366,19 +432,21 @@ static int update_odbc(const char *database, const char *table, const char *keyf
va_list aq;
struct custom_prepare_struct cps = { .sql = sql, .extra = lookup };
+ if (!table || ast_string_field_init(&cps, 256)) {
+ return -1;
+ }
va_copy(cps.ap, ap);
va_copy(aq, ap);
-
- if (!table)
- return -1;
- obj = ast_odbc_request_obj(database, 0);
- if (!obj)
+ if (!(obj = ast_odbc_request_obj(database, 0))) {
+ ast_string_field_free_memory(&cps);
return -1;
+ }
newparam = va_arg(aq, const char *);
if (!newparam) {
ast_odbc_release_obj(obj);
+ ast_string_field_free_memory(&cps);
return -1;
}
newval = va_arg(aq, const char *);
@@ -394,20 +462,23 @@ static int update_odbc(const char *database, const char *table, const char *keyf
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;
}