forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathval.c
368 lines (323 loc) · 8.35 KB
/
val.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2008 1&1 Internet AG
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../dprint.h"
#include "../../db/db_ut.h"
#include "val.h"
#include "my_con.h"
#include <string.h>
#include <stdio.h>
/*
* Convert str to db value, does not copy strings
*/
int db_mysql_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
{
static str dummy_string = {"", 0};
if (!_v) {
LM_ERR("invalid parameter value\n");
return -1;
}
if (!_s) {
memset(_v, 0, sizeof(db_val_t));
/* Initialize the string pointers to a dummy empty
* string so that we do not crash when the NULL flag
* is set but the module does not check it properly
*/
VAL_STRING(_v) = dummy_string.s;
VAL_STR(_v) = dummy_string;
VAL_BLOB(_v) = dummy_string;
VAL_TYPE(_v) = _t;
VAL_NULL(_v) = 1;
return 0;
}
VAL_NULL(_v) = 0;
switch(_t) {
case DB_INT:
LM_DBG("converting INT [%s]\n", _s);
if (db_str2int(_s, &VAL_INT(_v)) < 0) {
LM_ERR("error while converting integer value from string\n");
return -2;
} else {
VAL_TYPE(_v) = DB_INT;
return 0;
}
break;
case DB_BIGINT:
LM_DBG("converting INT BIG[%s]\n", _s);
if (db_str2bigint(_s, &VAL_BIGINT(_v)) < 0) {
LM_ERR("error while converting big integer value from string\n");
return -2;
} else {
VAL_TYPE(_v) = DB_BIGINT;
return 0;
}
break;
case DB_BITMAP:
LM_DBG("converting BITMAP [%s]\n", _s);
if (db_str2int(_s, &VAL_INT(_v)) < 0) {
LM_ERR("error while converting bitmap value from string\n");
return -3;
} else {
VAL_TYPE(_v) = DB_BITMAP;
return 0;
}
break;
case DB_DOUBLE:
LM_DBG("converting DOUBLE [%s]\n", _s);
if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
LM_ERR("error while converting double value from string\n");
return -4;
} else {
VAL_TYPE(_v) = DB_DOUBLE;
return 0;
}
break;
case DB_STRING:
LM_DBG("converting STRING [%s]\n", _s);
VAL_STRING(_v) = _s;
VAL_TYPE(_v) = DB_STRING;
return 0;
case DB_STR:
LM_DBG("converting STR [%.*s]\n", _l, _s);
VAL_STR(_v).s = (char*)_s;
VAL_STR(_v).len = _l;
VAL_TYPE(_v) = DB_STR;
return 0;
case DB_DATETIME:
LM_DBG("converting DATETIME [%s]\n", _s);
if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
LM_ERR("error while converting datetime value from string\n");
return -5;
} else {
VAL_TYPE(_v) = DB_DATETIME;
return 0;
}
break;
case DB_BLOB:
LM_DBG("converting BLOB [%.*s]\n", _l, _s);
VAL_BLOB(_v).s = (char*)_s;
VAL_BLOB(_v).len = _l;
VAL_TYPE(_v) = DB_BLOB;
return 0;
}
return -6;
}
/*
* Used when converting values to be used in a DB query
*/
int db_mysql_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
{
int l;
char* old_s;
if (!_c || !_v || !_s || !_len || !*_len) {
LM_ERR("invalid parameter value\n");
return -1;
}
if (VAL_NULL(_v)) {
if (*_len < sizeof("NULL")) {
LM_ERR("buffer too small\n");
return -1;
}
*_len = snprintf(_s, *_len, "NULL");
return 0;
}
switch(VAL_TYPE(_v)) {
case DB_INT:
if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
LM_ERR("error while converting string to int\n");
return -2;
} else {
return 0;
}
break;
case DB_BIGINT:
if (db_bigint2str(VAL_BIGINT(_v), _s, _len) < 0) {
LM_ERR("error while converting bigint to string\n");
return -2;
} else {
return 0;
}
break;
case DB_BITMAP:
if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
LM_ERR("error while converting string to int\n");
return -3;
} else {
return 0;
}
break;
case DB_DOUBLE:
if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
LM_ERR("error while converting string to double\n");
return -4;
} else {
return 0;
}
break;
case DB_STRING:
l = strlen(VAL_STRING(_v));
if (*_len < (l * 2 + 3)) {
LM_ERR("destination STRING buffer too short (have %d, need %d)\n",
*_len, l * 2 + 3);
return -5;
} else {
old_s = _s;
*_s++ = '\'';
_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STRING(_v), l);
*_s++ = '\'';
*_s = '\0'; /* FIXME */
*_len = _s - old_s;
return 0;
}
break;
case DB_STR:
if (*_len < (VAL_STR(_v).len * 2 + 3)) {
LM_ERR("destination STR buffer too short (have %d, need %d)\n",
*_len, VAL_STR(_v).len * 2 + 3);
return -6;
} else {
old_s = _s;
*_s++ = '\'';
_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, VAL_STR(_v).len);
*_s++ = '\'';
*_s = '\0';
*_len = _s - old_s;
return 0;
}
break;
case DB_DATETIME:
if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
LM_ERR("error while converting string to time_t\n");
return -7;
} else {
return 0;
}
break;
case DB_BLOB:
l = VAL_BLOB(_v).len;
if (*_len < (l * 2 + 3)) {
LM_ERR("destination BLOB buffer too short (have %d, need %d)\n",
*_len, l * 2 + 3);
return -8;
} else {
old_s = _s;
*_s++ = '\'';
_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, l);
*_s++ = '\'';
*_s = '\0';
*_len = _s - old_s;
return 0;
}
break;
default:
LM_DBG("unknown data type\n");
return -9;
}
/*return -8; --not reached*/
}
int db_mysql_val2bind(const db_val_t* v, MYSQL_BIND *binds, unsigned int i)
{
struct tm t;
MYSQL_TIME *mt;
if (VAL_NULL(v)) {
*(binds[i].is_null) = 1;
*(binds[i].length) = 0;
binds[i].buffer= NULL;
switch(VAL_TYPE(v)) {
case DB_INT:
binds[i].buffer_type= MYSQL_TYPE_LONG; break;
case DB_BIGINT:
binds[i].buffer_type= MYSQL_TYPE_LONGLONG; break;
case DB_BITMAP:
binds[i].buffer_type= MYSQL_TYPE_LONG; break;
case DB_DOUBLE:
binds[i].buffer_type= MYSQL_TYPE_DOUBLE; break;
case DB_STRING:
binds[i].buffer_type= MYSQL_TYPE_STRING; break;
case DB_STR:
binds[i].buffer_type= MYSQL_TYPE_STRING; break;
case DB_DATETIME:
binds[i].buffer_type= MYSQL_TYPE_DATETIME; break;
case DB_BLOB:
binds[i].buffer_type= MYSQL_TYPE_BLOB; break;
default:
LM_ERR("unknown NULL data type (%d)\n",VAL_TYPE(v));
return -10;
}
return 0;
} else {
*(binds[i].is_null) = 0;
}
switch(VAL_TYPE(v)) {
case DB_INT:
binds[i].buffer_type= MYSQL_TYPE_LONG;
binds[i].buffer= (char*)&(VAL_INT(v));
*binds[i].length= sizeof(VAL_INT(v));
break;
case DB_BIGINT:
binds[i].buffer_type= MYSQL_TYPE_LONGLONG;
binds[i].buffer= (char*)&(VAL_BIGINT(v));
*binds[i].length= sizeof(VAL_BIGINT(v));
break;
case DB_BITMAP:
binds[i].buffer_type= MYSQL_TYPE_LONG;
binds[i].buffer= (char*)&(VAL_BITMAP(v));
*binds[i].length= sizeof(VAL_BITMAP(v));
break;
case DB_DOUBLE:
binds[i].buffer_type= MYSQL_TYPE_DOUBLE;
binds[i].buffer= (char*)&(VAL_DOUBLE(v));
*binds[i].length= sizeof(VAL_DOUBLE(v));
break;
case DB_STRING:
binds[i].buffer_type= MYSQL_TYPE_STRING;
binds[i].buffer= (char*)VAL_STRING(v);
*binds[i].length= strlen(VAL_STRING(v));
break;
case DB_STR:
binds[i].buffer_type= MYSQL_TYPE_STRING;
binds[i].buffer= VAL_STR(v).s;
*binds[i].length= VAL_STR(v).len;
break;
case DB_DATETIME:
binds[i].buffer_type= MYSQL_TYPE_DATETIME;
localtime_r( &VAL_TIME(v), &t );
mt = (MYSQL_TIME*)binds[i].buffer;
mt->year = 1900 + t.tm_year;
mt->month = (t.tm_mon)+1;
mt->day = t.tm_mday;
mt->hour = t.tm_hour;
mt->minute = t.tm_min;
mt->second = t.tm_sec;
*binds[i].length= sizeof(MYSQL_TIME);
break;
case DB_BLOB:
binds[i].buffer_type= MYSQL_TYPE_BLOB;
binds[i].buffer= VAL_BLOB(v).s;
*binds[i].length= VAL_BLOB(v).len;
break;
default:
LM_ERR("unknown data type (%d)\n",VAL_TYPE(v));
return -9;
}
LM_DBG("added val (%d): len=%ld; type=%d; is_null=%d\n", i,
*(binds[i].length), binds[i].buffer_type, *(binds[i].is_null));
return 0;
}