Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
3 : * Copyright (C) 2005-2015 Frediano Ziglio
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Library General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Library General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Library General Public
16 : * License along with this library; if not, write to the
17 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 : * Boston, MA 02111-1307, USA.
19 : */
20 :
21 : #include <config.h>
22 :
23 : #include <stdlib.h>
24 :
25 : #if HAVE_UNISTD_H
26 : #include <unistd.h>
27 : #endif /* HAVE_UNISTD_H */
28 :
29 : #include <assert.h>
30 :
31 : #include <freetds/tds.h>
32 : #include <freetds/iconv.h>
33 : #include <freetds/tls.h>
34 : #include <freetds/checks.h>
35 : #include <freetds/utils/string.h>
36 : #include <freetds/replacements.h>
37 : #include <freetds/enum_cap.h>
38 : #include <freetds/utils.h>
39 :
40 : #if HAVE_SYS_SOCKET_H
41 : #include <sys/socket.h>
42 : #endif
43 :
44 : #ifdef HAVE_STRING_H
45 : #include <string.h>
46 : #endif
47 :
48 : #ifdef HAVE_LOCALE_H
49 : #include <locale.h>
50 : #endif /* HAVE_LOCALE_H */
51 :
52 : #ifdef HAVE_LANGINFO_H
53 : #include <langinfo.h>
54 : #endif /* HAVE_LANGINFO_H */
55 :
56 : static void tds_free_env(TDSCONNECTION * conn);
57 : static void tds_free_compute_results(TDSSOCKET * tds);
58 : static void tds_free_compute_result(TDSCOMPUTEINFO * comp_info);
59 :
60 : #undef TEST_MALLOC
61 : #define TEST_MALLOC(dest,type) \
62 : {if (!(dest = (type*)calloc(1, sizeof(type)))) goto Cleanup;}
63 :
64 : #undef TEST_CALLOC
65 : #define TEST_CALLOC(dest,type,n) \
66 : {if (!(dest = (type*)calloc((n), sizeof(type)))) goto Cleanup;}
67 :
68 : /**
69 : * \ingroup libtds
70 : * \defgroup mem Memory allocation
71 : * Allocate or free resources. Allocation can fail only on out of memory.
72 : * In such case they return NULL and leave the state as before call.
73 : * Mainly function names are in the form tds_alloc_XX or tds_free_XXX.
74 : * tds_alloc_XXX functions allocate structures and return pointer to allocated
75 : * data while tds_free_XXX take structure pointers and free them. Some functions
76 : * require additional parameters to initialize structure correctly.
77 : * The main exception are structures that use reference counting. These structures
78 : * have tds_alloc_XXX functions but instead of tds_free_XXX use tds_release_XXX.
79 : */
80 :
81 : /**
82 : * \addtogroup mem
83 : * @{
84 : */
85 :
86 : static volatile unsigned int inc_num = 1;
87 :
88 : /**
89 : * Get an id for dynamic query based on TDS information
90 : * \param conn state information for the connection and the TDS protocol
91 : * \return TDS_FAIL or TDS_SUCCESS
92 : */
93 : static char *
94 657816 : tds_get_dynid(TDSCONNECTION * conn, char *id)
95 : {
96 : unsigned long n;
97 : int i;
98 : char *p;
99 : char c;
100 :
101 : CHECK_CONN_EXTRA(conn);
102 :
103 657816 : inc_num = (inc_num + 1) & 0xffff;
104 : /* some version of Sybase require length <= 10, so we code id */
105 657816 : n = (unsigned long) (TDS_INTPTR) conn;
106 657816 : p = id;
107 657816 : *p++ = (char) ('a' + (n % 26u));
108 657816 : n /= 26u;
109 6578160 : for (i = 0; i < 9; ++i) {
110 5920344 : c = (char) ('0' + (n % 36u));
111 5920344 : *p++ = (c < ('0' + 10)) ? c : c + ('a' - '0' - 10);
112 : /* tdsdump_log(TDS_DBG_INFO1, "%d -> %d(%c)\n", (int) (n%36u),p[-1],p[-1]); */
113 5920344 : n /= 36u;
114 5920344 : if (i == 4)
115 657816 : n += 3u * inc_num;
116 : }
117 657816 : *p = 0;
118 657816 : return id;
119 : }
120 :
121 : #include <freetds/pushvis.h>
122 : extern const TDSCOLUMNFUNCS tds_invalid_funcs;
123 : #include <freetds/popvis.h>
124 :
125 : static TDSCOLUMN *
126 104913 : tds_alloc_column(void)
127 : {
128 : TDSCOLUMN *col;
129 :
130 104913 : TEST_MALLOC(col, TDSCOLUMN);
131 209826 : tds_dstr_init(&col->table_name);
132 209826 : tds_dstr_init(&col->column_name);
133 209826 : tds_dstr_init(&col->table_column_name);
134 104913 : col->funcs = &tds_invalid_funcs;
135 :
136 104913 : Cleanup:
137 104913 : return col;
138 : }
139 :
140 : static void
141 104839 : tds_free_column(TDSCOLUMN *col)
142 : {
143 104839 : tds_dstr_free(&col->table_name);
144 104839 : tds_dstr_free(&col->column_name);
145 104839 : tds_dstr_free(&col->table_column_name);
146 104839 : free(col);
147 104839 : }
148 :
149 :
150 : /**
151 : * \fn TDSDYNAMIC *tds_alloc_dynamic(TDSCONNECTION *conn, const char *id)
152 : * \brief Allocate a dynamic statement.
153 : * \param conn the connection within which to allocate the statement.
154 : * \param id a character label identifying the statement.
155 : * \return a pointer to the allocated structure (NULL on failure).
156 : *
157 : * tds_alloc_dynamic is used to implement placeholder code under TDS 5.0
158 : */
159 : TDSDYNAMIC *
160 657826 : tds_alloc_dynamic(TDSCONNECTION * conn, const char *id)
161 : {
162 : TDSDYNAMIC *dyn;
163 : char tmp_id[30];
164 :
165 657826 : if (id) {
166 : /* check to see if id already exists (shouldn't) */
167 20 : if (tds_lookup_dynamic(conn, id))
168 : return NULL;
169 : } else {
170 : unsigned int n;
171 : id = tmp_id;
172 :
173 : for (n = 0;;) {
174 657816 : if (!tds_lookup_dynamic(conn, tds_get_dynid(conn, tmp_id)))
175 : break;
176 10 : if (++n == 256)
177 : return NULL;
178 : }
179 : }
180 :
181 657826 : TEST_MALLOC(dyn, TDSDYNAMIC);
182 :
183 : /* take into account pointer in list */
184 657826 : dyn->ref_count = 2;
185 :
186 : /* insert into list */
187 657826 : dyn->next = conn->dyns;
188 657826 : conn->dyns = dyn;
189 :
190 657826 : strlcpy(dyn->id, id, TDS_MAX_DYNID_LEN);
191 :
192 657826 : return dyn;
193 :
194 657826 : Cleanup:
195 : return NULL;
196 : }
197 :
198 : /**
199 : * \fn void tds_free_input_params(TDSDYNAMIC *dyn)
200 : * \brief Frees all allocated input parameters of a dynamic statement.
201 : * \param dyn the dynamic statement whose input parameter are to be freed
202 : *
203 : * tds_free_input_params frees all parameters for the give dynamic statement
204 : */
205 : void
206 1502 : tds_free_input_params(TDSDYNAMIC * dyn)
207 : {
208 : TDSPARAMINFO *info;
209 :
210 659328 : info = dyn->params;
211 659328 : if (info) {
212 1482 : tds_free_param_results(info);
213 1084 : dyn->params = NULL;
214 : }
215 1502 : }
216 :
217 : /*
218 : * Called when dynamic got deallocated from server
219 : */
220 : void
221 658026 : tds_dynamic_deallocated(TDSCONNECTION *conn, TDSDYNAMIC *dyn)
222 : {
223 : TDSDYNAMIC **victim;
224 :
225 658026 : tdsdump_log(TDS_DBG_FUNC, "tds_dynamic_deallocated() : freeing dynamic_id %s\n", dyn->id);
226 :
227 658026 : victim = &conn->dyns;
228 1316252 : while (*victim != dyn) {
229 400 : if (*victim == NULL) {
230 200 : tdsdump_log(TDS_DBG_FUNC, "tds_dynamic_deallocated() : cannot find id %s\n", dyn->id);
231 : return;
232 : }
233 200 : victim = &(*victim)->next;
234 : }
235 :
236 : /* remove from list */
237 657826 : *victim = dyn->next;
238 657826 : dyn->next = NULL;
239 :
240 : /* assure there is no id left */
241 657826 : dyn->num_id = 0;
242 :
243 657826 : tds_release_dynamic(&dyn);
244 : }
245 :
246 :
247 : /**
248 : * \fn void tds_release_dynamic(TDSDYNAMIC **pdyn)
249 : * \brief Frees dynamic statement
250 : * \param pdyn pointer to dynamic statement to be freed.
251 : */
252 : void
253 1411717 : tds_release_dynamic(TDSDYNAMIC ** pdyn)
254 : {
255 : TDSDYNAMIC *dyn;
256 :
257 1411717 : dyn = *pdyn;
258 1411717 : *pdyn = NULL;
259 1411717 : if (!dyn || --dyn->ref_count > 0)
260 : return;
261 :
262 1315652 : tds_detach_results(dyn->res_info);
263 :
264 657826 : tds_free_results(dyn->res_info);
265 657826 : tds_free_input_params(dyn);
266 657826 : free(dyn->query);
267 657826 : free(dyn);
268 : }
269 :
270 : /**
271 : * \fn TDSPARAMINFO *tds_alloc_param_result(TDSPARAMINFO *old_param)
272 : * \brief Adds a output parameter to TDSPARAMINFO.
273 : * \param old_param a pointer to the TDSPARAMINFO structure containing the
274 : * current set of output parameter, or NULL if none exists.
275 : * \return a pointer to the new TDSPARAMINFO structure.
276 : *
277 : * tds_alloc_param_result() works a bit differently than the other alloc result
278 : * functions. Output parameters come in individually with no total number
279 : * given in advance, so we simply call this func every time with get a
280 : * TDS_PARAM_TOKEN and let it realloc the columns struct one bigger.
281 : * tds_free_all_results() usually cleans up after us.
282 : */
283 : TDSPARAMINFO *
284 42234 : tds_alloc_param_result(TDSPARAMINFO * old_param)
285 : {
286 : TDSPARAMINFO *param_info;
287 : TDSCOLUMN *colinfo;
288 :
289 : /* parameters cannot have row associated */
290 42234 : if (old_param && (old_param->current_row || old_param->row_free))
291 : return NULL;
292 :
293 42234 : colinfo = tds_alloc_column();
294 42234 : if (!colinfo)
295 : return NULL;
296 :
297 42234 : param_info = old_param;
298 42234 : if (!param_info) {
299 29986 : TEST_MALLOC(param_info, TDSPARAMINFO);
300 29986 : param_info->ref_count = 1;
301 : }
302 :
303 42234 : if (!TDS_RESIZE(param_info->columns, param_info->num_cols + 1u))
304 : goto Cleanup;
305 :
306 42234 : param_info->columns[param_info->num_cols++] = colinfo;
307 42234 : return param_info;
308 :
309 0 : Cleanup:
310 0 : if (!old_param)
311 0 : free(param_info);
312 0 : free(colinfo);
313 0 : return NULL;
314 : }
315 :
316 : /**
317 : * Delete latest parameter
318 : */
319 : void
320 14 : tds_free_param_result(TDSPARAMINFO * param_info)
321 : {
322 : TDSCOLUMN *col;
323 :
324 14 : if (!param_info || param_info->num_cols <= 0)
325 : return;
326 :
327 14 : col = param_info->columns[--param_info->num_cols];
328 14 : if (col->column_data && col->column_data_free)
329 14 : col->column_data_free(col);
330 :
331 14 : if (param_info->num_cols == 0)
332 14 : TDS_ZERO_FREE(param_info->columns);
333 :
334 : /*
335 : * NOTE some information should be freed too but when this function
336 : * is called are not used. I hope to remove the need for this
337 : * function ASAP
338 : * A better way is to support different way to allocate and get
339 : * parameters
340 : * -- freddy77
341 : */
342 14 : tds_free_column(col);
343 : }
344 :
345 : static void
346 : tds_free_tvp_row(TDS_TVP_ROW *row)
347 : {
348 72 : tds_free_param_results(row->params);
349 : }
350 :
351 : static void
352 26140 : tds_param_free(TDSCOLUMN *col)
353 : {
354 26140 : if (!col->column_data)
355 : return;
356 :
357 26140 : if (col->column_type == SYBMSTABLE) {
358 8 : tds_deinit_tvp((TDS_TVP *) col->column_data);
359 26132 : } else if (is_blob_col(col)) {
360 2166 : TDSBLOB *blob = (TDSBLOB *) col->column_data;
361 2166 : free(blob->textvalue);
362 : }
363 :
364 26140 : TDS_ZERO_FREE(col->column_data);
365 : }
366 :
367 : /**
368 : * Allocate data for a parameter.
369 : * @param curparam parameter to retrieve size information
370 : * @return NULL on failure or new data
371 : */
372 : void *
373 26140 : tds_alloc_param_data(TDSCOLUMN * curparam)
374 : {
375 : TDS_INT data_size;
376 : void *data;
377 :
378 26140 : CHECK_COLUMN_EXTRA(curparam);
379 :
380 26140 : data_size = curparam->funcs->row_len(curparam);
381 :
382 : /* allocate data */
383 26140 : if (curparam->column_data && curparam->column_data_free)
384 0 : curparam->column_data_free(curparam);
385 26140 : curparam->column_data_free = tds_param_free;
386 :
387 26140 : data = malloc(data_size);
388 26140 : curparam->column_data = (unsigned char*) data;
389 26140 : if (!data)
390 : return NULL;
391 : /* if is a blob reset buffer */
392 26140 : if (is_blob_col(curparam) || curparam->column_type == SYBMSTABLE)
393 2174 : memset(data, 0, data_size);
394 :
395 : return data;
396 : }
397 :
398 : /**
399 : * Allocate memory for storing compute info
400 : * return NULL on out of memory
401 : */
402 :
403 : static TDSCOMPUTEINFO *
404 72 : tds_alloc_compute_result(TDS_USMALLINT num_cols, TDS_USMALLINT by_cols)
405 : {
406 : TDS_USMALLINT col;
407 : TDSCOMPUTEINFO *info;
408 :
409 72 : TEST_MALLOC(info, TDSCOMPUTEINFO);
410 72 : info->ref_count = 1;
411 :
412 72 : TEST_CALLOC(info->columns, TDSCOLUMN *, num_cols);
413 :
414 72 : info->num_cols = num_cols;
415 144 : for (col = 0; col < num_cols; col++)
416 72 : if (!(info->columns[col] = tds_alloc_column()))
417 : goto Cleanup;
418 :
419 72 : if (by_cols) {
420 24 : TEST_CALLOC(info->bycolumns, TDS_SMALLINT, by_cols);
421 24 : info->by_cols = by_cols;
422 : }
423 :
424 : return info;
425 0 : Cleanup:
426 0 : tds_free_compute_result(info);
427 0 : return NULL;
428 : }
429 :
430 : TDSCOMPUTEINFO **
431 72 : tds_alloc_compute_results(TDSSOCKET * tds, TDS_USMALLINT num_cols, TDS_USMALLINT by_cols)
432 : {
433 : TDS_UINT n;
434 : TDSCOMPUTEINFO **comp_info;
435 : TDSCOMPUTEINFO *cur_comp_info;
436 :
437 72 : tdsdump_log(TDS_DBG_FUNC, "alloc_compute_result. num_cols = %d bycols = %d\n", num_cols, by_cols);
438 72 : tdsdump_log(TDS_DBG_FUNC, "alloc_compute_result. num_comp_info = %d\n", tds->num_comp_info);
439 :
440 72 : cur_comp_info = tds_alloc_compute_result(num_cols, by_cols);
441 72 : if (!cur_comp_info)
442 : return NULL;
443 :
444 72 : n = tds->num_comp_info;
445 72 : comp_info = (TDSCOMPUTEINFO **) TDS_RESIZE(tds->comp_info, n + 1u);
446 72 : if (!comp_info) {
447 0 : tds_free_compute_result(cur_comp_info);
448 0 : return NULL;
449 : }
450 :
451 72 : tds->comp_info = comp_info;
452 72 : comp_info[n] = cur_comp_info;
453 72 : tds->num_comp_info = n + 1u;
454 :
455 72 : tdsdump_log(TDS_DBG_FUNC, "alloc_compute_result. num_comp_info = %d\n", tds->num_comp_info);
456 :
457 : return comp_info;
458 : }
459 :
460 : TDSRESULTINFO *
461 35131 : tds_alloc_results(TDS_USMALLINT num_cols)
462 : {
463 : TDSRESULTINFO *res_info;
464 : TDS_USMALLINT col;
465 :
466 35131 : TEST_MALLOC(res_info, TDSRESULTINFO);
467 35131 : res_info->ref_count = 1;
468 35131 : if (num_cols)
469 34943 : TEST_CALLOC(res_info->columns, TDSCOLUMN *, num_cols);
470 62607 : for (col = 0; col < num_cols; col++)
471 62607 : if (!(res_info->columns[col] = tds_alloc_column()))
472 : goto Cleanup;
473 35131 : res_info->num_cols = num_cols;
474 35131 : res_info->row_size = 0;
475 35131 : return res_info;
476 0 : Cleanup:
477 0 : tds_free_results(res_info);
478 0 : return NULL;
479 : }
480 :
481 : void
482 465575 : tds_set_current_results(TDSSOCKET *tds, TDSRESULTINFO *info)
483 : {
484 465575 : tds_detach_results(info);
485 465575 : if (tds->current_results)
486 530 : tds->current_results->attached_to = NULL;
487 465575 : if (info)
488 465567 : info->attached_to = tds;
489 465575 : tds->in_row = (info != NULL);
490 465575 : tds->current_results = info;
491 465575 : }
492 :
493 : /**
494 : * Detach result info from it current socket
495 : */
496 : void
497 0 : tds_detach_results(TDSRESULTINFO *info)
498 : {
499 1478099 : if (info && info->attached_to) {
500 465031 : info->attached_to->current_results = NULL;
501 465031 : info->attached_to->in_row = false;
502 465031 : info->attached_to = NULL;
503 : }
504 0 : }
505 :
506 : static void
507 34591 : tds_row_free(TDSRESULTINFO *res_info, unsigned char *row)
508 : {
509 : int i;
510 : const TDSCOLUMN *col;
511 :
512 34591 : if (!res_info || !row)
513 : return;
514 :
515 62227 : for (i = 0; i < res_info->num_cols; ++i) {
516 62227 : col = res_info->columns[i];
517 :
518 62227 : if (is_blob_col(col)) {
519 3612 : TDSBLOB *blob = (TDSBLOB *) &row[col->column_data - res_info->current_row];
520 3612 : if (blob->textvalue)
521 2870 : TDS_ZERO_FREE(blob->textvalue);
522 : }
523 : }
524 :
525 34591 : free(row);
526 : }
527 :
528 : /**
529 : * Allocate space for row store
530 : * return NULL on out of memory
531 : */
532 : TDSRET
533 34621 : tds_alloc_row(TDSRESULTINFO * res_info)
534 : {
535 34621 : int i, num_cols = res_info->num_cols;
536 : unsigned char *ptr;
537 : TDSCOLUMN *col;
538 : TDS_UINT row_size;
539 :
540 : /* compute row size */
541 34621 : row_size = 0;
542 96922 : for (i = 0; i < num_cols; ++i) {
543 62301 : col = res_info->columns[i];
544 :
545 62301 : col->column_data_free = NULL;
546 :
547 62301 : row_size += col->funcs->row_len(col);
548 62301 : row_size += (TDS_ALIGN_SIZE - 1);
549 62301 : row_size -= row_size % TDS_ALIGN_SIZE;
550 : }
551 34621 : res_info->row_size = row_size;
552 :
553 34621 : ptr = tds_new0(unsigned char, row_size ? row_size : 1);
554 34621 : res_info->current_row = ptr;
555 34621 : if (!ptr)
556 : return TDS_FAIL;
557 34621 : res_info->row_free = tds_row_free;
558 :
559 : /* fill column_data */
560 34621 : row_size = 0;
561 96922 : for (i = 0; i < num_cols; ++i) {
562 62301 : col = res_info->columns[i];
563 :
564 62301 : col->column_data = ptr + row_size;
565 :
566 62301 : row_size += col->funcs->row_len(col);
567 62301 : row_size += (TDS_ALIGN_SIZE - 1);
568 62301 : row_size -= row_size % TDS_ALIGN_SIZE;
569 : }
570 :
571 : return TDS_SUCCESS;
572 : }
573 :
574 : TDSRET
575 72 : tds_alloc_compute_row(TDSCOMPUTEINFO * res_info)
576 : {
577 72 : return tds_alloc_row(res_info);
578 : }
579 :
580 : void
581 106271 : tds_free_param_results(TDSPARAMINFO * param_info)
582 : {
583 245973 : tds_free_results(param_info);
584 106271 : }
585 :
586 : static void
587 : tds_free_compute_result(TDSCOMPUTEINFO * comp_info)
588 : {
589 72 : tds_free_results(comp_info);
590 : }
591 :
592 : static void
593 138168 : tds_free_compute_results(TDSSOCKET * tds)
594 : {
595 138168 : TDSCOMPUTEINFO ** comp_info = tds->comp_info;
596 138168 : TDS_UINT i, num_comp = tds->num_comp_info;
597 :
598 138168 : tds->comp_info = NULL;
599 138168 : tds->num_comp_info = 0;
600 :
601 138240 : for (i = 0; i < num_comp; i++) {
602 72 : if (comp_info && comp_info[i]) {
603 144 : tds_detach_results(comp_info[i]);
604 72 : tds_free_compute_result(comp_info[i]);
605 : }
606 : }
607 138168 : if (num_comp)
608 48 : free(comp_info);
609 138168 : }
610 :
611 : void
612 195717 : tds_free_row(TDSRESULTINFO * res_info, unsigned char *row)
613 : {
614 195717 : assert(res_info);
615 195717 : if (!row || !res_info->row_free)
616 : return;
617 :
618 2720 : res_info->row_free(res_info, row);
619 : }
620 :
621 : void
622 1360132 : tds_free_results(TDSRESULTINFO * res_info)
623 : {
624 : int i;
625 : TDSCOLUMN *curcol;
626 :
627 1360132 : if (!res_info)
628 : return;
629 :
630 272842 : if (--res_info->ref_count != 0)
631 : return;
632 :
633 65159 : tds_detach_results(res_info);
634 :
635 65159 : if (res_info->num_cols && res_info->columns) {
636 104825 : for (i = 0; i < res_info->num_cols; i++)
637 104825 : if ((curcol = res_info->columns[i]) != NULL) {
638 104825 : if (curcol->bcp_terminator)
639 54 : TDS_ZERO_FREE(curcol->bcp_terminator);
640 104825 : tds_free_bcp_column_data(curcol->bcp_column_data);
641 104825 : curcol->bcp_column_data = NULL;
642 104825 : if (curcol->column_data && curcol->column_data_free)
643 26688 : curcol->column_data_free(curcol);
644 : }
645 : }
646 :
647 65159 : if (res_info->current_row && res_info->row_free)
648 31949 : res_info->row_free(res_info, res_info->current_row);
649 :
650 65159 : if (res_info->num_cols && res_info->columns) {
651 104825 : for (i = 0; i < res_info->num_cols; i++)
652 104825 : if ((curcol = res_info->columns[i]) != NULL)
653 104825 : tds_free_column(curcol);
654 65145 : free(res_info->columns);
655 : }
656 :
657 65159 : free(res_info->bycolumns);
658 :
659 65159 : free(res_info);
660 : }
661 :
662 : void
663 138168 : tds_free_all_results(TDSSOCKET * tds)
664 : {
665 138168 : tdsdump_log(TDS_DBG_FUNC, "tds_free_all_results()\n");
666 276336 : tds_detach_results(tds->res_info);
667 138168 : tds_free_results(tds->res_info);
668 138168 : tds->res_info = NULL;
669 276336 : tds_detach_results(tds->param_info);
670 276336 : tds_free_param_results(tds->param_info);
671 138168 : tds->param_info = NULL;
672 138168 : tds_free_compute_results(tds);
673 138168 : tds->has_status = false;
674 138168 : tds->in_row = false;
675 138168 : tds->ret_status = 0;
676 138168 : if (tds->cur_dyn)
677 6168 : tds_detach_results(tds->cur_dyn->res_info);
678 138168 : }
679 :
680 : /*
681 : * Return true if winsock is initialized, else false.
682 : */
683 : static bool
684 : winsock_initialized(void)
685 : {
686 : #ifdef _WIN32
687 : static bool initialized = false;
688 : static tds_mutex mtx = TDS_MUTEX_INITIALIZER;
689 :
690 : int erc;
691 :
692 : if (initialized)
693 : return true;
694 :
695 : tds_mutex_lock(&mtx);
696 : /* same check inside the mutex */
697 : if (initialized) {
698 : tds_mutex_unlock(&mtx);
699 : return true;
700 : }
701 :
702 : /* initialize the socket layer */
703 : erc = tds_socket_init();
704 : initialized = (erc == 0);
705 : tds_mutex_unlock(&mtx);
706 :
707 : if (erc != 0) {
708 : char *errstr = sock_strerror(erc);
709 : tdsdump_log(TDS_DBG_ERROR, "tds_init_winsock: WSAStartup failed with %d (%s)\n", erc, errstr);
710 : sock_strerror_free(errstr);
711 : return false;
712 : }
713 : #endif
714 : return true;
715 : }
716 :
717 : TDSCONTEXT *
718 3625 : tds_alloc_context(void * parent)
719 : {
720 : TDSCONTEXT *context;
721 : TDSLOCALE *locale;
722 :
723 : if (!winsock_initialized())
724 : return NULL;
725 :
726 3625 : if ((locale = tds_get_locale()) == NULL)
727 : return NULL;
728 :
729 3625 : if ((context = tds_new0(TDSCONTEXT, 1)) == NULL) {
730 0 : tds_free_locale(locale);
731 0 : return NULL;
732 : }
733 3625 : context->locale = locale;
734 3625 : context->parent = parent;
735 3625 : context->money_use_2_digits = false;
736 :
737 3625 : return context;
738 : }
739 :
740 : void
741 3595 : tds_free_context(TDSCONTEXT * context)
742 : {
743 3595 : if (!context)
744 : return;
745 :
746 3595 : tds_free_locale(context->locale);
747 3595 : free(context);
748 : }
749 :
750 : TDSLOCALE *
751 3705 : tds_alloc_locale(void)
752 : {
753 : TDSLOCALE *locale;
754 :
755 3705 : TEST_MALLOC(locale, TDSLOCALE);
756 :
757 : return locale;
758 :
759 0 : Cleanup:
760 0 : tds_free_locale(locale);
761 0 : return NULL;
762 : }
763 :
764 : /*
765 : * Default capabilities.
766 : */
767 : #undef REQ
768 : #define SUPPORTED_REQ_CAP(i) \
769 : REQ(i,LANG) REQ(i,RPC) REQ(i,EVT) REQ(i,MSTMT) REQ(i,BCP) REQ(i,CURSOR) REQ(i,DYNF) \
770 : REQ(i,MSG) REQ(i,PARAM) REQ(i,DATA_INT1) REQ(i,DATA_INT2) REQ(i,DATA_INT4) REQ(i,DATA_BIT) \
771 : REQ(i,DATA_CHAR) REQ(i,DATA_VCHAR) REQ(i,DATA_BIN) REQ(i,DATA_VBIN) REQ(i,DATA_MNY8) \
772 : REQ(i,DATA_MNY4) REQ(i,DATA_DATE8) REQ(i,DATA_DATE4) REQ(i,DATA_FLT4) REQ(i,DATA_FLT8) \
773 : REQ(i,DATA_NUM) REQ(i,DATA_TEXT) REQ(i,DATA_IMAGE) REQ(i,DATA_DEC) REQ(i,DATA_LCHAR) \
774 : REQ(i,DATA_LBIN) REQ(i,DATA_INTN) REQ(i,DATA_DATETIMEN) REQ(i,DATA_MONEYN) \
775 : REQ(i,CSR_PREV) REQ(i,CSR_FIRST) REQ(i,CSR_LAST) REQ(i,CSR_ABS) REQ(i,CSR_REL) \
776 : REQ(i,CSR_MULTI) REQ(i,CON_INBAND) REQ(i,PROTO_TEXT) REQ(i,PROTO_BULK) \
777 : REQ(i,DATA_SENSITIVITY) REQ(i,DATA_BOUNDARY) REQ(i,PROTO_DYNPROC) REQ(i,DATA_FLTN) \
778 : REQ(i,DATA_BITN) REQ(i,DATA_INT8) REQ(i,DOL_BULK) REQ(i,WIDETABLE) \
779 : REQ(i,DATA_UINT2) REQ(i,DATA_UINT4) REQ(i,DATA_UINT8) REQ(i,DATA_UINTN) REQ(i,LARGEIDENT) \
780 : REQ(i,SRVPKTSIZE) REQ(i,DATA_DATE) REQ(i,DATA_TIME) REQ(i,DATA_BIGTIME) REQ(i,DATA_BIGDATETIME)
781 : #define REQ(i,n) |(((TDS_REQ_ ## n / 8) == i)?(1<<(TDS_REQ_ ## n & 7)):0)
782 : #define REQB(i) 0 SUPPORTED_REQ_CAP(i)
783 :
784 : #undef RES
785 : #define SUPPORTED_RES_CAP(i) \
786 : RES(i,CON_NOOOB) RES(i,PROTO_NOTEXT) RES(i,PROTO_NOBULK) RES(i,NOTDSDEBUG)
787 : #define RES(i,n) |(((TDS_RES_ ## n / 8) == i)?(1<<(TDS_RES_ ## n & 7)):0)
788 : #define RESB(i) 0 SUPPORTED_RES_CAP(i)
789 :
790 : static const TDS_CAPABILITIES defaultcaps = { {
791 : /* type, len, data, data, data, data, data, data, data, data, data, data, data, data, data, data (14 bytes) */
792 : { 1, 14, { REQB(13), REQB(12), REQB(11), REQB(10), REQB(9), REQB(8), REQB(7),
793 : REQB(6), REQB(5), REQB(4), REQB(3), REQB(2), REQB(1), REQB(0) } },
794 : { 2, 14, { RESB(13), RESB(12), RESB(11), RESB(10), RESB(9), RESB(8), RESB(7),
795 : RESB(6), RESB(5), RESB(4), RESB(3), RESB(2), RESB(1), RESB(0) } }
796 : } };
797 : /* check we match the values size */
798 : TDS_COMPILE_CHECK(tds_values_len, sizeof(defaultcaps.types[0].values) == 14);
799 : /* check we match the default size */
800 : TDS_COMPILE_CHECK(tds_cap_len, sizeof(defaultcaps) == TDS_MAX_CAPABILITY);
801 :
802 : /**
803 : * Initialize login structure with locale information and other stuff for connection
804 : * @param locale locale information (copied to configuration information)
805 : * @result login structure or NULL if initialization error
806 : */
807 : TDSLOGIN*
808 3810 : tds_init_login(TDSLOGIN *login, TDSLOCALE * locale)
809 : {
810 : char hostname[128];
811 : #if HAVE_NL_LANGINFO && defined(CODESET)
812 : const char *charset;
813 : #elif !defined(_WIN32)
814 : char *lc_all, *tok = NULL;
815 : #endif
816 : #ifdef _WIN32
817 : char cp[128];
818 : #endif
819 :
820 : /*
821 : * TDS 7.0:
822 : * ODBC driver settings.
823 : * Change to initial language must succeed.
824 : */
825 3810 : login->option_flag2 = TDS_INIT_LANG_REQUIRED|TDS_ODBC_ON;
826 3810 : login->tds_version = TDS_DEFAULT_VERSION;
827 3810 : login->block_size = 0;
828 :
829 : #if HAVE_NL_LANGINFO && defined(CODESET)
830 3810 : charset = nl_langinfo(CODESET);
831 3810 : if (strcmp(tds_canonical_charset_name(charset), "US-ASCII") == 0)
832 3798 : charset = "ISO-8859-1";
833 3810 : if (!tds_dstr_copy(&login->client_charset, charset))
834 : return NULL;
835 : #else
836 : if (!tds_dstr_copy(&login->client_charset, "ISO-8859-1"))
837 : return NULL;
838 :
839 : #ifdef _WIN32
840 : /* for Windows uses GetLocaleInfoA */
841 : strcpy(cp, "CP");
842 : if (GetLocaleInfoA(GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE, cp + 2, sizeof(cp) - 2) > 0)
843 : if (!tds_dstr_copy(&login->client_charset, cp))
844 : return NULL;
845 : #else
846 : if ((lc_all = strdup(setlocale(LC_ALL, NULL))) == NULL)
847 : return NULL;
848 :
849 : if (strtok_r(lc_all, ".", &tok)) {
850 : char *encoding = strtok_r(NULL, "@", &tok);
851 : if (encoding) {
852 : if (!tds_dstr_copy(&login->client_charset, encoding)) {
853 : free(lc_all);
854 : return NULL;
855 : }
856 : }
857 : }
858 : free(lc_all);
859 : #endif
860 : #endif
861 :
862 3810 : if (locale) {
863 3810 : if (locale->language)
864 0 : if (!tds_dstr_copy(&login->language, locale->language))
865 : return NULL;
866 3810 : if (locale->server_charset)
867 0 : if (!tds_dstr_copy(&login->server_charset, locale->server_charset))
868 : return NULL;
869 : }
870 7620 : if (tds_dstr_isempty(&login->language)) {
871 3740 : if (!tds_dstr_copy(&login->language, TDS_DEF_LANG))
872 : return NULL;
873 : }
874 3810 : memset(hostname, '\0', sizeof(hostname));
875 3810 : gethostname(hostname, sizeof(hostname));
876 3810 : hostname[sizeof(hostname) - 1] = '\0'; /* make sure it's terminated */
877 3810 : if (!tds_dstr_copy(&login->client_host_name, hostname))
878 : return NULL;
879 :
880 3810 : login->valid_configuration = 1;
881 3810 : login->check_ssl_hostname = 1;
882 :
883 3810 : return login;
884 : }
885 :
886 : TDSCURSOR *
887 2364 : tds_alloc_cursor(TDSSOCKET *tds, const char *name, size_t namelen, const char *query, size_t querylen)
888 : {
889 : TDSCURSOR *cursor;
890 : TDSCURSOR *pcursor;
891 :
892 2364 : TEST_MALLOC(cursor, TDSCURSOR);
893 2364 : cursor->ref_count = 1;
894 :
895 2364 : cursor->type = TDS_CUR_TYPE_KEYSET;
896 2364 : cursor->concurrency = TDS_CUR_CONCUR_OPTIMISTIC;
897 :
898 2364 : TEST_CALLOC(cursor->cursor_name, char, namelen + 1);
899 2364 : memcpy(cursor->cursor_name, name, namelen);
900 :
901 2364 : TEST_CALLOC(cursor->query, char, querylen + 1);
902 2364 : memcpy(cursor->query, query, querylen);
903 :
904 2364 : if (tds->conn->cursors == NULL) {
905 2330 : tds->conn->cursors = cursor;
906 : } else {
907 0 : for (pcursor = tds->conn->cursors; pcursor->next; pcursor = pcursor->next)
908 0 : continue;
909 34 : pcursor->next = cursor;
910 : }
911 : /* take into account reference in connection list */
912 2364 : ++cursor->ref_count;
913 :
914 2364 : return cursor;
915 :
916 0 : Cleanup:
917 0 : tds_release_cursor(&cursor);
918 0 : return NULL;
919 : }
920 :
921 : /*
922 : * Called when cursor got deallocated from server
923 : */
924 : void
925 2364 : tds_cursor_deallocated(TDSCONNECTION *conn, TDSCURSOR *cursor)
926 : {
927 : TDSCURSOR **victim;
928 :
929 2364 : tdsdump_log(TDS_DBG_FUNC, "tds_cursor_deallocated() : freeing cursor_id %d\n", cursor->cursor_id);
930 :
931 2364 : victim = &conn->cursors;
932 4744 : while (*victim != cursor) {
933 16 : if (*victim == NULL) {
934 0 : tdsdump_log(TDS_DBG_FUNC, "tds_cursor_deallocated() : cannot find cursor_id %d\n", cursor->cursor_id);
935 : return;
936 : }
937 16 : victim = &(*victim)->next;
938 : }
939 :
940 : /* remove from list */
941 2364 : *victim = cursor->next;
942 2364 : cursor->next = NULL;
943 :
944 2364 : tds_release_cursor(&cursor);
945 : }
946 :
947 : /*
948 : * Decrement reference counter and free if necessary.
949 : * Called internally by libTDS and by upper library when you don't need
950 : * cursor reference anymore
951 : */
952 : void
953 89132 : tds_release_cursor(TDSCURSOR **pcursor)
954 : {
955 89132 : TDSCURSOR *cursor = *pcursor;
956 89132 : *pcursor = NULL;
957 89132 : if (!cursor || --cursor->ref_count > 0)
958 : return;
959 :
960 2364 : tdsdump_log(TDS_DBG_FUNC, "tds_release_cursor() : freeing cursor_id %d\n", cursor->cursor_id);
961 :
962 2364 : tdsdump_log(TDS_DBG_FUNC, "tds_release_cursor() : freeing cursor results\n");
963 4728 : tds_detach_results(cursor->res_info);
964 2364 : tds_free_results(cursor->res_info);
965 :
966 2364 : if (cursor->cursor_name) {
967 2364 : tdsdump_log(TDS_DBG_FUNC, "tds_release_cursor() : freeing cursor name\n");
968 2364 : free(cursor->cursor_name);
969 : }
970 :
971 2364 : if (cursor->query) {
972 2364 : tdsdump_log(TDS_DBG_FUNC, "tds_release_cursor() : freeing cursor query\n");
973 2364 : free(cursor->query);
974 : }
975 :
976 2364 : tdsdump_log(TDS_DBG_FUNC, "tds_release_cursor() : cursor_id %d freed\n", cursor->cursor_id);
977 2364 : free(cursor);
978 : }
979 :
980 : TDSLOGIN *
981 7142 : tds_alloc_login(bool use_environment)
982 : {
983 7142 : TDSLOGIN *login = NULL;
984 7142 : const char *server_name = TDS_DEF_SERVER;
985 :
986 7142 : TEST_MALLOC(login, TDSLOGIN);
987 7142 : login->check_ssl_hostname = 1;
988 7142 : login->use_utf16 = 1;
989 7142 : login->bulk_copy = 1;
990 14284 : tds_dstr_init(&login->server_name);
991 14284 : tds_dstr_init(&login->language);
992 14284 : tds_dstr_init(&login->server_charset);
993 14284 : tds_dstr_init(&login->client_host_name);
994 14284 : tds_dstr_init(&login->server_host_name);
995 14284 : tds_dstr_init(&login->app_name);
996 14284 : tds_dstr_init(&login->user_name);
997 14284 : tds_dstr_init(&login->password);
998 14284 : tds_dstr_init(&login->library);
999 14284 : tds_dstr_init(&login->new_password);
1000 :
1001 7142 : login->ip_addrs = NULL;
1002 :
1003 14284 : tds_dstr_init(&login->database);
1004 7142 : login->dump_file = NULL;
1005 14284 : tds_dstr_init(&login->client_charset);
1006 14284 : tds_dstr_init(&login->instance_name);
1007 14284 : tds_dstr_init(&login->server_realm_name);
1008 14284 : tds_dstr_init(&login->server_spn);
1009 14284 : tds_dstr_init(&login->cafile);
1010 14284 : tds_dstr_init(&login->crlfile);
1011 14284 : tds_dstr_init(&login->certificate_host_name);
1012 14284 : tds_dstr_init(&login->db_filename);
1013 14284 : tds_dstr_init(&login->openssl_ciphers);
1014 14284 : tds_dstr_init(&login->gnutls_ciphers);
1015 14284 : tds_dstr_init(&login->routing_address);
1016 :
1017 7142 : if (use_environment) {
1018 : const char *s;
1019 3332 : if ((s=getenv("DSQUERY")) != NULL)
1020 0 : server_name = s;
1021 :
1022 3332 : if ((s=getenv("TDSQUERY")) != NULL)
1023 0 : server_name = s;
1024 : }
1025 :
1026 7142 : if (!tds_dstr_copy(&login->server_name, server_name)) {
1027 0 : free(login);
1028 0 : return NULL;
1029 : }
1030 :
1031 7142 : login->capabilities = defaultcaps;
1032 7142 : login->use_ntlmv2_specified = 0;
1033 7142 : login->use_ntlmv2 = 1;
1034 :
1035 0 : Cleanup:
1036 : return login;
1037 : }
1038 :
1039 : void
1040 7832 : tds_free_login(TDSLOGIN * login)
1041 : {
1042 7832 : if (!login)
1043 : return;
1044 :
1045 : /* for security reason clear memory */
1046 7110 : tds_dstr_zero(&login->password);
1047 7110 : tds_dstr_free(&login->password);
1048 7110 : tds_dstr_zero(&login->new_password);
1049 7110 : tds_dstr_free(&login->new_password);
1050 7110 : tds_dstr_free(&login->server_name);
1051 7110 : tds_dstr_free(&login->language);
1052 7110 : tds_dstr_free(&login->server_charset);
1053 7110 : tds_dstr_free(&login->client_host_name);
1054 7110 : tds_dstr_free(&login->app_name);
1055 7110 : tds_dstr_free(&login->user_name);
1056 7110 : tds_dstr_free(&login->library);
1057 7110 : tds_dstr_free(&login->client_charset);
1058 7110 : tds_dstr_free(&login->server_host_name);
1059 :
1060 7110 : if (login->ip_addrs != NULL)
1061 3808 : freeaddrinfo(login->ip_addrs);
1062 :
1063 7110 : tds_dstr_free(&login->database);
1064 7110 : free(login->dump_file);
1065 7110 : tds_dstr_free(&login->instance_name);
1066 7110 : tds_dstr_free(&login->server_realm_name);
1067 7110 : tds_dstr_free(&login->server_spn);
1068 7110 : tds_dstr_free(&login->cafile);
1069 7110 : tds_dstr_free(&login->crlfile);
1070 7110 : tds_dstr_free(&login->certificate_host_name);
1071 7110 : tds_dstr_free(&login->db_filename);
1072 7110 : tds_dstr_free(&login->openssl_ciphers);
1073 7110 : tds_dstr_free(&login->gnutls_ciphers);
1074 7110 : tds_dstr_free(&login->routing_address);
1075 7110 : free(login);
1076 : }
1077 :
1078 : TDSPACKET *
1079 15413 : tds_alloc_packet(void *buf, unsigned len)
1080 : {
1081 15413 : TDSPACKET *packet = (TDSPACKET *) malloc(len + TDS_OFFSET(TDSPACKET, buf));
1082 15413 : if (TDS_LIKELY(packet)) {
1083 10732 : tds_packet_zero_data_start(packet);
1084 15413 : packet->data_len = 0;
1085 15413 : packet->capacity = len;
1086 15413 : packet->sid = 0;
1087 15413 : packet->next = NULL;
1088 15413 : if (buf) {
1089 231 : memcpy(packet->buf, buf, len);
1090 231 : packet->data_len = len;
1091 : }
1092 : }
1093 15413 : return packet;
1094 : }
1095 :
1096 : TDSPACKET *
1097 50452 : tds_realloc_packet(TDSPACKET *packet, unsigned len)
1098 : {
1099 58032 : if (packet->capacity < len) {
1100 6248 : packet = (TDSPACKET *) realloc(packet, len + TDS_OFFSET(TDSPACKET, buf));
1101 6248 : if (TDS_LIKELY(packet))
1102 6248 : packet->capacity = len;
1103 : }
1104 50452 : return packet;
1105 : }
1106 :
1107 : void
1108 52407 : tds_free_packets(TDSPACKET *packet)
1109 : {
1110 : TDSPACKET *next;
1111 67745 : for (; packet; packet = next) {
1112 15338 : next = packet->next;
1113 15338 : free(packet);
1114 : }
1115 52407 : }
1116 :
1117 : static void
1118 4589 : tds_deinit_connection(TDSCONNECTION *conn)
1119 : {
1120 4589 : if (conn->authentication)
1121 0 : conn->authentication->free(conn, conn->authentication);
1122 4589 : conn->authentication = NULL;
1123 9242 : while (conn->dyns)
1124 64 : tds_dynamic_deallocated(conn, conn->dyns);
1125 4615 : while (conn->cursors)
1126 26 : tds_cursor_deallocated(conn, conn->cursors);
1127 4589 : tds_ssl_deinit(conn);
1128 : /* close connection and free inactive sockets */
1129 4589 : tds_connection_close(conn);
1130 4589 : tds_wakeup_close(&conn->wakeup);
1131 4589 : tds_iconv_free(conn);
1132 4589 : free(conn->product_name);
1133 4589 : free(conn->server);
1134 4589 : tds_free_env(conn);
1135 9178 : tds_free_packets(conn->packet_cache);
1136 4589 : tds_mutex_free(&conn->list_mtx);
1137 : #if ENABLE_ODBC_MARS
1138 4634 : tds_free_packets(conn->packets);
1139 4634 : tds_free_packets(conn->recv_packet);
1140 4634 : tds_free_packets(conn->send_packets);
1141 2317 : free(conn->sessions);
1142 : #endif
1143 4589 : }
1144 :
1145 : static TDSCONNECTION *
1146 4619 : tds_init_connection(TDSCONNECTION *conn, TDSCONTEXT *context, unsigned int bufsize)
1147 : {
1148 4619 : conn->env.block_size = bufsize;
1149 4619 : conn->s = INVALID_SOCKET;
1150 4619 : conn->use_iconv = 1;
1151 4619 : conn->tds_ctx = context;
1152 4619 : conn->ncharsize = 1;
1153 4619 : conn->unicharsize = 1;
1154 :
1155 4619 : if (tds_wakeup_init(&conn->wakeup))
1156 : goto Cleanup;
1157 :
1158 4619 : if (tds_iconv_alloc(conn))
1159 : goto Cleanup;
1160 :
1161 9238 : if (tds_mutex_init(&conn->list_mtx))
1162 : goto Cleanup;
1163 :
1164 : #if ENABLE_ODBC_MARS
1165 2332 : TEST_CALLOC(conn->sessions, TDSSOCKET*, 64);
1166 2332 : conn->num_sessions = 64;
1167 : #endif
1168 2332 : return conn;
1169 :
1170 0 : Cleanup:
1171 0 : tds_wakeup_close(&conn->wakeup);
1172 0 : tds_iconv_free(conn);
1173 0 : return NULL;
1174 : }
1175 :
1176 : static TDSSOCKET *
1177 4629 : tds_init_socket(TDSSOCKET * tds_socket, unsigned int bufsize)
1178 : {
1179 : TDSPACKET *pkt;
1180 :
1181 4629 : tds_socket->parent = NULL;
1182 :
1183 4629 : tds_socket->recv_packet = tds_alloc_packet(NULL, bufsize);
1184 4629 : if (!tds_socket->recv_packet)
1185 : goto Cleanup;
1186 4629 : tds_socket->in_buf = tds_socket->recv_packet->buf;
1187 :
1188 4629 : pkt = tds_alloc_packet(NULL, bufsize + TDS_ADDITIONAL_SPACE);
1189 4629 : if (!pkt)
1190 : goto Cleanup;
1191 4629 : tds_set_current_send_packet(tds_socket, pkt);
1192 :
1193 4629 : tds_socket->out_buf_max = bufsize;
1194 :
1195 : /* Jeff's hack, init to no timeout */
1196 4629 : tds_socket->query_timeout = 0;
1197 4629 : tds_init_write_buf(tds_socket);
1198 4629 : tds_socket->state = TDS_DEAD;
1199 4629 : tds_socket->env_chg_func = NULL;
1200 9258 : if (tds_mutex_init(&tds_socket->wire_mtx))
1201 : goto Cleanup;
1202 :
1203 : #ifdef ENABLE_ODBC_MARS
1204 2342 : tds_socket->sid = 0;
1205 2342 : if (tds_cond_init(&tds_socket->packet_cond))
1206 : goto Cleanup;
1207 :
1208 2342 : tds_socket->recv_seq = 0;
1209 2342 : tds_socket->send_seq = 0;
1210 2342 : tds_socket->recv_wnd = 4;
1211 2342 : tds_socket->send_wnd = 4;
1212 : #endif
1213 2342 : return tds_socket;
1214 :
1215 0 : Cleanup:
1216 : return NULL;
1217 : }
1218 :
1219 :
1220 : #if ENABLE_ODBC_MARS
1221 : static void
1222 2317 : tds_free_connection(TDSCONNECTION *conn)
1223 : {
1224 2317 : if (!conn) return;
1225 2317 : assert(conn->in_net_tds == NULL);
1226 2317 : tds_deinit_connection(conn);
1227 2317 : free(conn);
1228 : }
1229 :
1230 : static TDSCONNECTION *
1231 2332 : tds_alloc_connection(TDSCONTEXT *context, unsigned int bufsize)
1232 : {
1233 : TDSCONNECTION *conn;
1234 :
1235 2332 : TEST_MALLOC(conn, TDSCONNECTION);
1236 2332 : if (!tds_init_connection(conn, context, bufsize))
1237 : goto Cleanup;
1238 : return conn;
1239 :
1240 0 : Cleanup:
1241 0 : tds_free_connection(conn);
1242 0 : return NULL;
1243 : }
1244 :
1245 : static TDSSOCKET *
1246 2342 : tds_alloc_socket_base(unsigned int bufsize)
1247 : {
1248 : TDSSOCKET *tds_socket;
1249 :
1250 2342 : TEST_MALLOC(tds_socket, TDSSOCKET);
1251 2342 : if (!tds_init_socket(tds_socket, bufsize))
1252 : goto Cleanup;
1253 : return tds_socket;
1254 :
1255 0 : Cleanup:
1256 0 : tds_free_socket(tds_socket);
1257 0 : return NULL;
1258 : }
1259 :
1260 : TDSSOCKET *
1261 2332 : tds_alloc_socket(TDSCONTEXT * context, unsigned int bufsize)
1262 : {
1263 2332 : TDSCONNECTION *conn = tds_alloc_connection(context, bufsize);
1264 : TDSSOCKET *tds;
1265 :
1266 2332 : if (!conn)
1267 : return NULL;
1268 :
1269 2332 : tds = tds_alloc_socket_base(bufsize);
1270 2332 : if (tds) {
1271 2332 : conn->sessions[0] = tds;
1272 2332 : tds->conn = conn;
1273 2332 : return tds;
1274 : }
1275 0 : tds_free_connection(conn);
1276 0 : return NULL;
1277 : }
1278 :
1279 : static bool
1280 10 : tds_alloc_new_sid(TDSSOCKET *tds)
1281 : {
1282 : uint16_t sid;
1283 10 : TDSCONNECTION *conn = tds->conn;
1284 :
1285 10 : tds_mutex_lock(&conn->list_mtx);
1286 12 : for (sid = 1; sid < conn->num_sessions; ++sid)
1287 12 : if (!conn->sessions[sid])
1288 : break;
1289 10 : if (sid == conn->num_sessions) {
1290 : /* extend array */
1291 0 : TDSSOCKET **s = (TDSSOCKET **) TDS_RESIZE(conn->sessions, sid+64);
1292 0 : if (!s)
1293 : goto error;
1294 0 : memset(s + conn->num_sessions, 0, sizeof(*s) * 64);
1295 0 : conn->num_sessions += 64;
1296 : }
1297 10 : conn->sessions[sid] = tds;
1298 10 : tds->sid = sid;
1299 10 : error:
1300 10 : tds_mutex_unlock(&conn->list_mtx);
1301 10 : return tds->sid != 0;
1302 : }
1303 :
1304 : TDSSOCKET *
1305 16 : tds_alloc_additional_socket(TDSCONNECTION *conn)
1306 : {
1307 : TDSSOCKET *tds;
1308 16 : if (!IS_TDS72_PLUS(conn) || !conn->mars)
1309 : return NULL;
1310 :
1311 10 : tds = tds_alloc_socket_base(sizeof(TDS72_SMP_HEADER) + conn->env.block_size);
1312 10 : if (!tds)
1313 : return NULL;
1314 10 : tds->send_packet->data_start = sizeof(TDS72_SMP_HEADER);
1315 10 : tds->out_buf = tds->send_packet->buf + sizeof(TDS72_SMP_HEADER);
1316 10 : tds->out_buf_max -= sizeof(TDS72_SMP_HEADER);
1317 :
1318 10 : tds->conn = conn;
1319 10 : if (!tds_alloc_new_sid(tds))
1320 : goto Cleanup;
1321 :
1322 10 : tds->state = TDS_IDLE;
1323 10 : if (TDS_FAILED(tds_append_syn(tds)))
1324 : goto Cleanup;
1325 :
1326 : return tds;
1327 :
1328 0 : Cleanup:
1329 0 : tds_free_socket(tds);
1330 0 : return NULL;
1331 : }
1332 : #else /* !ENABLE_ODBC_MARS */
1333 : TDSSOCKET *
1334 2287 : tds_alloc_socket(TDSCONTEXT * context, unsigned int bufsize)
1335 : {
1336 : TDSSOCKET *tds_socket;
1337 :
1338 2287 : TEST_MALLOC(tds_socket, TDSSOCKET);
1339 2287 : if (!tds_init_connection(tds_socket->conn, context, bufsize))
1340 : goto Cleanup;
1341 2287 : if (!tds_init_socket(tds_socket, bufsize))
1342 : goto Cleanup;
1343 : return tds_socket;
1344 :
1345 0 : Cleanup:
1346 0 : tds_free_socket(tds_socket);
1347 0 : return NULL;
1348 : }
1349 : #endif /* !ENABLE_ODBC_MARS */
1350 :
1351 : TDSSOCKET *
1352 7580 : tds_realloc_socket(TDSSOCKET * tds, unsigned int bufsize)
1353 : {
1354 : TDSPACKET *packet;
1355 : #if ENABLE_ODBC_MARS
1356 3918 : size_t smp_hdr_len = tds->conn->mars ? sizeof(TDS72_SMP_HEADER) : 0;
1357 : #else
1358 : enum { smp_hdr_len = 0 };
1359 : #endif
1360 :
1361 7580 : assert(tds && tds->out_buf && tds->send_packet);
1362 :
1363 7580 : if (bufsize < 512)
1364 0 : bufsize = 512;
1365 :
1366 : /* prevent nasty memory conditions, server should send the request at
1367 : * the beginning only */
1368 7580 : if (tds->out_pos > bufsize || tds->frozen)
1369 : return NULL;
1370 :
1371 7580 : tds->conn->env.block_size = bufsize;
1372 :
1373 15160 : packet = tds_realloc_packet(tds->send_packet, smp_hdr_len + bufsize + TDS_ADDITIONAL_SPACE);
1374 7580 : if (packet == NULL)
1375 : return NULL;
1376 :
1377 : #if ENABLE_ODBC_MARS
1378 3918 : packet->data_start = smp_hdr_len;
1379 : #endif
1380 7580 : tds->out_buf_max = bufsize;
1381 7580 : tds_set_current_send_packet(tds, packet);
1382 7580 : return tds;
1383 : }
1384 :
1385 : #if ENABLE_ODBC_MARS
1386 : static void
1387 2327 : tds_connection_remove_socket(TDSCONNECTION *conn, TDSSOCKET *tds)
1388 : {
1389 : unsigned n;
1390 2327 : bool must_free_connection = true;
1391 2327 : tds_mutex_lock(&conn->list_mtx);
1392 2327 : if (tds->sid < conn->num_sessions)
1393 2327 : conn->sessions[tds->sid] = NULL;
1394 150615 : for (n = 0; n < conn->num_sessions; ++n)
1395 148298 : if (TDSSOCKET_VALID(conn->sessions[n])) {
1396 : must_free_connection = false;
1397 : break;
1398 : }
1399 2327 : if (!must_free_connection) {
1400 : /* tds use connection member so must be valid */
1401 10 : tds_append_fin(tds);
1402 : }
1403 2327 : tds_mutex_unlock(&conn->list_mtx);
1404 :
1405 : /* detach entirely */
1406 2327 : tds->conn = NULL;
1407 :
1408 2327 : if (must_free_connection)
1409 2317 : tds_free_connection(conn);
1410 2327 : }
1411 : #else
1412 : static void inline
1413 : tds_connection_remove_socket(TDSCONNECTION *conn, TDSSOCKET *tds TDS_UNUSED)
1414 : {
1415 2272 : tds_deinit_connection(conn);
1416 : }
1417 : #endif
1418 :
1419 : void
1420 7159 : tds_free_socket(TDSSOCKET * tds)
1421 : {
1422 : #if ENABLE_EXTRA_CHECKS
1423 : TDSDYNAMIC *dyn;
1424 : TDSCURSOR *cur;
1425 : #endif
1426 :
1427 7159 : if (!tds)
1428 : return;
1429 :
1430 : /* detach this socket */
1431 4599 : tds_release_cur_dyn(tds);
1432 4599 : tds_release_cursor(&tds->cur_cursor);
1433 9198 : tds_detach_results(tds->current_results);
1434 : #if ENABLE_EXTRA_CHECKS
1435 4667 : for (dyn = tds->conn->dyns; dyn; dyn = dyn->next) {
1436 68 : if (dyn->res_info && dyn->res_info->attached_to == tds) {
1437 0 : assert(0);
1438 : }
1439 : }
1440 4625 : for (cur = tds->conn->cursors; cur; cur = cur->next) {
1441 26 : if (cur->res_info && cur->res_info->attached_to == tds) {
1442 0 : assert(0);
1443 : }
1444 : }
1445 : #endif
1446 4599 : tds_free_all_results(tds);
1447 : #if ENABLE_ODBC_MARS
1448 4654 : tds_cond_destroy(&tds->packet_cond);
1449 : #endif
1450 :
1451 6871 : tds_connection_remove_socket(tds->conn, tds);
1452 9198 : tds_free_packets(tds->recv_packet);
1453 4599 : if (tds->frozen_packets)
1454 : tds_free_packets(tds->frozen_packets);
1455 : else
1456 4599 : tds_free_packets(tds->send_packet);
1457 4599 : free(tds);
1458 : }
1459 :
1460 : void
1461 3675 : tds_free_locale(TDSLOCALE * locale)
1462 : {
1463 3675 : if (!locale)
1464 : return;
1465 :
1466 3675 : free(locale->language);
1467 3675 : free(locale->server_charset);
1468 3675 : free(locale->datetime_fmt);
1469 3675 : free(locale->date_fmt);
1470 3675 : free(locale->time_fmt);
1471 3675 : free(locale);
1472 : }
1473 :
1474 : static void
1475 4589 : tds_free_env(TDSCONNECTION* conn)
1476 : {
1477 4589 : if (conn->env.language)
1478 2906 : TDS_ZERO_FREE(conn->env.language);
1479 4589 : if (conn->env.charset)
1480 544 : TDS_ZERO_FREE(conn->env.charset);
1481 4589 : if (conn->env.database)
1482 3618 : TDS_ZERO_FREE(conn->env.database);
1483 4589 : }
1484 :
1485 : void
1486 15751 : tds_free_msg(TDSMESSAGE * message)
1487 : {
1488 15751 : if (message) {
1489 15751 : message->priv_msg_type = 0;
1490 15751 : message->msgno = 0;
1491 15751 : message->state = 0;
1492 15751 : message->severity = 0;
1493 15751 : message->line_number = 0;
1494 15751 : TDS_ZERO_FREE(message->message);
1495 15751 : TDS_ZERO_FREE(message->server);
1496 15751 : TDS_ZERO_FREE(message->proc_name);
1497 15751 : TDS_ZERO_FREE(message->sql_state);
1498 : }
1499 15751 : }
1500 :
1501 : #define SQLS_ENTRY(number,state) case number: p = state; break
1502 :
1503 : char *
1504 41314 : tds_alloc_client_sqlstate(int msgno)
1505 : {
1506 41314 : const char *p = NULL;
1507 :
1508 41314 : switch (msgno) {
1509 : SQLS_ENTRY(17000, "S1T00"); /* timeouts ??? */
1510 1 : SQLS_ENTRY(20004, "08S01"); /* Communication link failure */
1511 72 : SQLS_ENTRY(20006, "08S01");
1512 0 : SQLS_ENTRY(20009, "08S01");
1513 0 : SQLS_ENTRY(20020, "08S01");
1514 0 : SQLS_ENTRY(20019, "24000"); /* Invalid cursor state */
1515 0 : SQLS_ENTRY(20014, "28000"); /* Invalid authorization specification */
1516 36030 : SQLS_ENTRY(2400, "42000"); /* Syntax error or access violation */
1517 740 : SQLS_ENTRY(2401, "42000");
1518 1930 : SQLS_ENTRY(2403, "42000");
1519 0 : SQLS_ENTRY(2404, "42000");
1520 2211 : SQLS_ENTRY(2402, "S1000"); /* General error */
1521 : }
1522 :
1523 : if (p != NULL)
1524 40984 : return strdup(p);
1525 : else
1526 : return NULL;
1527 : }
1528 :
1529 : char *
1530 15642 : tds_alloc_lookup_sqlstate(TDSSOCKET * tds, int msgno)
1531 : {
1532 15642 : const char *p = NULL;
1533 15642 : char *q = NULL;
1534 :
1535 15642 : if (TDS_IS_MSSQL(tds)) {
1536 7402 : switch (msgno) { /* MSSQL Server */
1537 :
1538 752 : SQLS_ENTRY(3621,"01000");
1539 26 : SQLS_ENTRY(8153,"01003"); /* Null in aggregate */
1540 8 : SQLS_ENTRY(911, "08004"); /* Server rejected connection */
1541 0 : SQLS_ENTRY(512, "21000"); /* Subquery returns more than one value */
1542 0 : SQLS_ENTRY(213, "21S01"); /* Insert column list mismatch */
1543 0 : SQLS_ENTRY(109, "21S01");
1544 0 : SQLS_ENTRY(110, "21S01");
1545 0 : SQLS_ENTRY(1774,"21S02"); /* Ref column mismatch */
1546 0 : SQLS_ENTRY(8152,"22001"); /* String data would be truncated */
1547 0 : SQLS_ENTRY(5146,"22003"); /* Numeric value out of range */
1548 0 : SQLS_ENTRY(168, "22003"); /* Arithmetic overflow */
1549 656 : SQLS_ENTRY(220, "22003");
1550 0 : SQLS_ENTRY(232, "22003");
1551 0 : SQLS_ENTRY(234, "22003");
1552 0 : SQLS_ENTRY(236, "22003");
1553 0 : SQLS_ENTRY(238, "22003");
1554 0 : SQLS_ENTRY(244, "22003");
1555 0 : SQLS_ENTRY(246, "22003");
1556 0 : SQLS_ENTRY(248, "22003");
1557 0 : SQLS_ENTRY(519, "22003");
1558 0 : SQLS_ENTRY(520, "22003");
1559 0 : SQLS_ENTRY(521, "22003");
1560 0 : SQLS_ENTRY(522, "22003");
1561 0 : SQLS_ENTRY(523, "22003");
1562 0 : SQLS_ENTRY(524, "22003");
1563 0 : SQLS_ENTRY(1007,"22003");
1564 0 : SQLS_ENTRY(3606,"22003");
1565 0 : SQLS_ENTRY(8115,"22003");
1566 0 : SQLS_ENTRY(206, "22005"); /* Error in assignment */
1567 0 : SQLS_ENTRY(235, "22005");
1568 0 : SQLS_ENTRY(247, "22005");
1569 0 : SQLS_ENTRY(249, "22005");
1570 0 : SQLS_ENTRY(256, "22005");
1571 0 : SQLS_ENTRY(257, "22005");
1572 0 : SQLS_ENTRY(305, "22005");
1573 0 : SQLS_ENTRY(409, "22005");
1574 0 : SQLS_ENTRY(518, "22005");
1575 0 : SQLS_ENTRY(529, "22005");
1576 0 : SQLS_ENTRY(210, "22007"); /* Invalid datetime format */
1577 0 : SQLS_ENTRY(241, "22007");
1578 0 : SQLS_ENTRY(295, "22007");
1579 16 : SQLS_ENTRY(242, "22008"); /* Datetime out of range */
1580 0 : SQLS_ENTRY(296, "22008");
1581 0 : SQLS_ENTRY(298, "22008");
1582 0 : SQLS_ENTRY(535, "22008");
1583 0 : SQLS_ENTRY(542, "22008");
1584 8 : SQLS_ENTRY(517, "22008");
1585 0 : SQLS_ENTRY(3607, "22012"); /* Div by zero */
1586 164 : SQLS_ENTRY(8134, "22012");
1587 0 : SQLS_ENTRY(245, "22018"); /* Syntax error? */
1588 8 : SQLS_ENTRY(2627, "23000"); /* Constraint violation */
1589 8 : SQLS_ENTRY(515, "23000");
1590 0 : SQLS_ENTRY(233, "23000");
1591 0 : SQLS_ENTRY(273, "23000");
1592 0 : SQLS_ENTRY(530, "23000");
1593 0 : SQLS_ENTRY(2601,"23000");
1594 0 : SQLS_ENTRY(2615,"23000");
1595 0 : SQLS_ENTRY(2626,"23000");
1596 0 : SQLS_ENTRY(3604,"23000");
1597 0 : SQLS_ENTRY(3605,"23000");
1598 0 : SQLS_ENTRY(544, "23000");
1599 0 : SQLS_ENTRY(547, "23000");
1600 0 : SQLS_ENTRY(550, "23000");
1601 0 : SQLS_ENTRY(4415, "23000");
1602 0 : SQLS_ENTRY(1505, "23000");
1603 0 : SQLS_ENTRY(1508, "23000");
1604 0 : SQLS_ENTRY(3725, "23000");
1605 0 : SQLS_ENTRY(3726, "23000");
1606 0 : SQLS_ENTRY(4712, "23000");
1607 0 : SQLS_ENTRY(10055, "23000");
1608 0 : SQLS_ENTRY(10065, "23000");
1609 0 : SQLS_ENTRY(11011, "23000");
1610 0 : SQLS_ENTRY(11040, "23000");
1611 0 : SQLS_ENTRY(16999, "24000"); /* Invalid cursor state */
1612 0 : SQLS_ENTRY(16905, "24000");
1613 0 : SQLS_ENTRY(16917, "24000");
1614 0 : SQLS_ENTRY(16946, "24000");
1615 0 : SQLS_ENTRY(16950, "24000");
1616 0 : SQLS_ENTRY(266, "25000"); /* Invalid transaction state */
1617 0 : SQLS_ENTRY(277,"25000");
1618 0 : SQLS_ENTRY(611,"25000");
1619 0 : SQLS_ENTRY(3906,"25000");
1620 0 : SQLS_ENTRY(3908,"25000");
1621 0 : SQLS_ENTRY(6401,"25000");
1622 0 : SQLS_ENTRY(626, "25000");
1623 0 : SQLS_ENTRY(627, "25000");
1624 0 : SQLS_ENTRY(628, "25000");
1625 0 : SQLS_ENTRY(3902, "25000");
1626 0 : SQLS_ENTRY(3903, "25000");
1627 0 : SQLS_ENTRY(3916, "25000");
1628 0 : SQLS_ENTRY(3918, "25000");
1629 0 : SQLS_ENTRY(3919, "25000");
1630 0 : SQLS_ENTRY(3921, "25000");
1631 0 : SQLS_ENTRY(3922, "25000");
1632 16 : SQLS_ENTRY(3926, "25000");
1633 0 : SQLS_ENTRY(7969, "25000");
1634 0 : SQLS_ENTRY(8506, "25000");
1635 0 : SQLS_ENTRY(15626, "25000");
1636 0 : SQLS_ENTRY(18456, "28000"); /* Login failed? */
1637 0 : SQLS_ENTRY(6104, "37000"); /* Syntax error or access violation */
1638 0 : SQLS_ENTRY(8114, "37000");
1639 0 : SQLS_ENTRY(131, "37000");
1640 2 : SQLS_ENTRY(137, "37000");
1641 24 : SQLS_ENTRY(170, "37000");
1642 0 : SQLS_ENTRY(174, "37000");
1643 0 : SQLS_ENTRY(201, "37000");
1644 8 : SQLS_ENTRY(2812, "37000");
1645 0 : SQLS_ENTRY(2526, "37000");
1646 0 : SQLS_ENTRY(8144, "37000");
1647 0 : SQLS_ENTRY(17308, "42000"); /* Syntax/Access violation */
1648 0 : SQLS_ENTRY(17571, "42000");
1649 0 : SQLS_ENTRY(18002, "42000");
1650 0 : SQLS_ENTRY(229, "42000");
1651 0 : SQLS_ENTRY(230, "42000");
1652 0 : SQLS_ENTRY(262, "42000");
1653 0 : SQLS_ENTRY(2557, "42000");
1654 0 : SQLS_ENTRY(2571, "42000");
1655 0 : SQLS_ENTRY(2760, "42000");
1656 0 : SQLS_ENTRY(3110, "42000");
1657 0 : SQLS_ENTRY(3704, "42000");
1658 0 : SQLS_ENTRY(4613, "42000");
1659 0 : SQLS_ENTRY(4618, "42000");
1660 0 : SQLS_ENTRY(4834, "42000");
1661 0 : SQLS_ENTRY(5011, "42000");
1662 0 : SQLS_ENTRY(5116, "42000");
1663 0 : SQLS_ENTRY(5812, "42000");
1664 0 : SQLS_ENTRY(6004, "42000");
1665 0 : SQLS_ENTRY(6102, "42000");
1666 0 : SQLS_ENTRY(7956, "42000");
1667 0 : SQLS_ENTRY(11010, "42000");
1668 0 : SQLS_ENTRY(11045, "42000");
1669 0 : SQLS_ENTRY(14126, "42000");
1670 0 : SQLS_ENTRY(15247, "42000");
1671 0 : SQLS_ENTRY(15622, "42000");
1672 0 : SQLS_ENTRY(20604, "42000");
1673 0 : SQLS_ENTRY(21049, "42000");
1674 0 : SQLS_ENTRY(113, "42000");
1675 0 : SQLS_ENTRY(2714, "42S01"); /* Table or view already exists */
1676 24 : SQLS_ENTRY(208, "42S02"); /* Table or view not found */
1677 196 : SQLS_ENTRY(3701, "42S02");
1678 0 : SQLS_ENTRY(1913, "42S11"); /* Index already exists */
1679 0 : SQLS_ENTRY(15605, "42S11");
1680 0 : SQLS_ENTRY(307, "42S12"); /* Index not found */
1681 0 : SQLS_ENTRY(308, "42S12");
1682 0 : SQLS_ENTRY(10033, "42S12");
1683 0 : SQLS_ENTRY(15323, "42S12");
1684 0 : SQLS_ENTRY(18833, "42S12");
1685 0 : SQLS_ENTRY(4925, "42S21"); /* Column already exists */
1686 0 : SQLS_ENTRY(21255, "42S21");
1687 0 : SQLS_ENTRY(1911, "42S22"); /* Column not found */
1688 48 : SQLS_ENTRY(207, "42S22");
1689 0 : SQLS_ENTRY(4924, "42S22");
1690 0 : SQLS_ENTRY(4926, "42S22");
1691 0 : SQLS_ENTRY(15645, "42S22");
1692 0 : SQLS_ENTRY(21166, "42S22");
1693 : }
1694 : } else {
1695 8240 : switch (msgno) { /* Sybase */
1696 4 : SQLS_ENTRY(3621, "01000");
1697 0 : SQLS_ENTRY(9501, "01003"); /* Null in aggregate */
1698 1 : SQLS_ENTRY(911, "08004"); /* Server rejected connection */
1699 0 : SQLS_ENTRY(512, "21000"); /* Subquery returns more than one value */
1700 0 : SQLS_ENTRY(213, "21S01"); /* Insert column list mismatch */
1701 0 : SQLS_ENTRY(109, "21S01");
1702 0 : SQLS_ENTRY(110, "21S01");
1703 0 : SQLS_ENTRY(1715, "21S02"); /* Ref column mismatch */
1704 0 : SQLS_ENTRY(9502, "22001"); /* String data would be truncated */
1705 0 : SQLS_ENTRY(220, "22003"); /* Arithmetic overflow */
1706 0 : SQLS_ENTRY(168, "22003");
1707 0 : SQLS_ENTRY(227, "22003");
1708 0 : SQLS_ENTRY(232, "22003");
1709 0 : SQLS_ENTRY(234, "22003");
1710 0 : SQLS_ENTRY(236, "22003");
1711 0 : SQLS_ENTRY(238, "22003");
1712 0 : SQLS_ENTRY(244, "22003");
1713 0 : SQLS_ENTRY(246, "22003");
1714 0 : SQLS_ENTRY(247, "22003");
1715 0 : SQLS_ENTRY(248, "22003");
1716 0 : SQLS_ENTRY(519, "22003");
1717 0 : SQLS_ENTRY(520, "22003");
1718 0 : SQLS_ENTRY(521, "22003");
1719 0 : SQLS_ENTRY(522, "22003");
1720 0 : SQLS_ENTRY(523, "22003");
1721 0 : SQLS_ENTRY(524, "22003");
1722 0 : SQLS_ENTRY(3606, "22003");
1723 0 : SQLS_ENTRY(206, "22005"); /* Error in assignment */
1724 0 : SQLS_ENTRY(235, "22005");
1725 4 : SQLS_ENTRY(249, "22005");
1726 0 : SQLS_ENTRY(256, "22005");
1727 0 : SQLS_ENTRY(305, "22005");
1728 0 : SQLS_ENTRY(409, "22005");
1729 0 : SQLS_ENTRY(518, "22005");
1730 0 : SQLS_ENTRY(529, "22005");
1731 0 : SQLS_ENTRY(535, "22008"); /* Datetime out of range */
1732 0 : SQLS_ENTRY(542, "22008");
1733 2 : SQLS_ENTRY(517, "22008");
1734 0 : SQLS_ENTRY(3607, "22012"); /* Div by zero */
1735 0 : SQLS_ENTRY(245, "22018"); /* Syntax error? */
1736 0 : SQLS_ENTRY(544, "23000"); /* Constraint violation */
1737 0 : SQLS_ENTRY(233, "23000");
1738 0 : SQLS_ENTRY(273, "23000");
1739 0 : SQLS_ENTRY(530, "23000");
1740 0 : SQLS_ENTRY(2601,"23000");
1741 0 : SQLS_ENTRY(2615,"23000");
1742 0 : SQLS_ENTRY(2626,"23000");
1743 0 : SQLS_ENTRY(3604,"23000");
1744 0 : SQLS_ENTRY(3605,"23000");
1745 0 : SQLS_ENTRY(545, "23000");
1746 0 : SQLS_ENTRY(546, "23000");
1747 0 : SQLS_ENTRY(547, "23000");
1748 0 : SQLS_ENTRY(548, "23000");
1749 0 : SQLS_ENTRY(549, "23000");
1750 0 : SQLS_ENTRY(550, "23000");
1751 0 : SQLS_ENTRY(1505, "23000");
1752 0 : SQLS_ENTRY(1508, "23000");
1753 0 : SQLS_ENTRY(565, "24000"); /* Invalid cursor state */
1754 0 : SQLS_ENTRY(558, "24000");
1755 0 : SQLS_ENTRY(559, "24000");
1756 0 : SQLS_ENTRY(6235, "24000");
1757 0 : SQLS_ENTRY(583, "24000");
1758 0 : SQLS_ENTRY(6259, "24000");
1759 0 : SQLS_ENTRY(6260, "24000");
1760 0 : SQLS_ENTRY(562, "24000");
1761 0 : SQLS_ENTRY(277, "25000"); /* Invalid transaction state */
1762 0 : SQLS_ENTRY(611,"25000");
1763 0 : SQLS_ENTRY(3906,"25000");
1764 0 : SQLS_ENTRY(3908,"25000");
1765 0 : SQLS_ENTRY(6401,"25000");
1766 0 : SQLS_ENTRY(627, "25000");
1767 0 : SQLS_ENTRY(628, "25000");
1768 0 : SQLS_ENTRY(641, "25000");
1769 0 : SQLS_ENTRY(642, "25000");
1770 0 : SQLS_ENTRY(1276, "25000");
1771 0 : SQLS_ENTRY(3902, "25000");
1772 0 : SQLS_ENTRY(3903, "25000");
1773 0 : SQLS_ENTRY(6104, "37000"); /* Syntax error or access violation */
1774 0 : SQLS_ENTRY(102, "37000");
1775 24 : SQLS_ENTRY(137, "37000");
1776 0 : SQLS_ENTRY(7327, "37000");
1777 0 : SQLS_ENTRY(201, "37000");
1778 0 : SQLS_ENTRY(257, "37000");
1779 2 : SQLS_ENTRY(2812, "37000");
1780 0 : SQLS_ENTRY(2526, "37000");
1781 0 : SQLS_ENTRY(11021, "37000");
1782 0 : SQLS_ENTRY(229, "42000"); /* Syntax/Access violation */
1783 0 : SQLS_ENTRY(230, "42000");
1784 0 : SQLS_ENTRY(262, "42000");
1785 0 : SQLS_ENTRY(4602, "42000");
1786 0 : SQLS_ENTRY(4603, "42000");
1787 0 : SQLS_ENTRY(4608, "42000");
1788 0 : SQLS_ENTRY(10306, "42000");
1789 0 : SQLS_ENTRY(10323, "42000");
1790 0 : SQLS_ENTRY(10330, "42000");
1791 0 : SQLS_ENTRY(10331, "42000");
1792 0 : SQLS_ENTRY(10332, "42000");
1793 0 : SQLS_ENTRY(11110, "42000");
1794 0 : SQLS_ENTRY(11113, "42000");
1795 0 : SQLS_ENTRY(11118, "42000");
1796 0 : SQLS_ENTRY(11121, "42000");
1797 0 : SQLS_ENTRY(17222, "42000");
1798 0 : SQLS_ENTRY(17223, "42000");
1799 0 : SQLS_ENTRY(18350, "42000");
1800 0 : SQLS_ENTRY(18351, "42000");
1801 0 : SQLS_ENTRY(113, "42000");
1802 0 : SQLS_ENTRY(2714, "42S01"); /* Table or view already exists */
1803 0 : SQLS_ENTRY(208, "42S02"); /* Table or view not found */
1804 44 : SQLS_ENTRY(3701, "42S02");
1805 0 : SQLS_ENTRY(1913, "42S11"); /* Index already exists */
1806 0 : SQLS_ENTRY(307, "42S12"); /* Index not found */
1807 0 : SQLS_ENTRY(7010, "42S12");
1808 0 : SQLS_ENTRY(18091, "42S12");
1809 0 : SQLS_ENTRY(1921, "42S21"); /* Column already exists */
1810 0 : SQLS_ENTRY(1720, "42S22"); /* Column not found */
1811 6 : SQLS_ENTRY(207, "42S22");
1812 0 : SQLS_ENTRY(4934, "42S22");
1813 0 : SQLS_ENTRY(18117, "42S22");
1814 : }
1815 : }
1816 :
1817 15642 : if (p != NULL && (q = strdup(p)) != NULL) {
1818 : /* FIXME correct here ?? */
1819 : /* Convert known ODBC 3.x states listed above to 2.x */
1820 2051 : if (memcmp(q, "42S", 3) == 0)
1821 318 : memcpy(q, "S00", 3);
1822 :
1823 : return q;
1824 : }
1825 : return NULL;
1826 : }
1827 :
1828 : BCPCOLDATA *
1829 3178 : tds_alloc_bcp_column_data(unsigned int column_size)
1830 : {
1831 : BCPCOLDATA *coldata;
1832 :
1833 3178 : TEST_MALLOC(coldata, BCPCOLDATA);
1834 :
1835 3178 : if (column_size > 4 * 1024)
1836 116 : column_size = 4 * 1024;
1837 3178 : TEST_CALLOC(coldata->data, unsigned char, column_size);
1838 :
1839 : return coldata;
1840 0 : Cleanup:
1841 0 : tds_free_bcp_column_data(coldata);
1842 0 : return NULL;
1843 : }
1844 :
1845 : void
1846 104825 : tds_free_bcp_column_data(BCPCOLDATA * coldata)
1847 : {
1848 104825 : if (!coldata)
1849 : return;
1850 :
1851 3178 : free(coldata->data);
1852 3178 : free(coldata);
1853 : }
1854 :
1855 : TDSBCPINFO *
1856 394 : tds_alloc_bcpinfo(void)
1857 : {
1858 : TDSBCPINFO *bcpinfo;
1859 :
1860 394 : TEST_MALLOC(bcpinfo, TDSBCPINFO);
1861 :
1862 788 : tds_dstr_init(&bcpinfo->hint);
1863 788 : tds_dstr_init(&bcpinfo->tablename);
1864 :
1865 394 : return bcpinfo;
1866 394 : Cleanup:
1867 : return NULL;
1868 : }
1869 :
1870 : void
1871 648 : tds_deinit_bcpinfo(TDSBCPINFO *bcpinfo)
1872 : {
1873 648 : tds_dstr_free(&bcpinfo->hint);
1874 648 : tds_dstr_free(&bcpinfo->tablename);
1875 648 : TDS_ZERO_FREE(bcpinfo->insert_stmt);
1876 648 : tds_free_results(bcpinfo->bindinfo);
1877 648 : bcpinfo->bindinfo = NULL;
1878 648 : TDS_ZERO_FREE(bcpinfo->sybase_colinfo);
1879 648 : bcpinfo->sybase_count = 0;
1880 648 : }
1881 :
1882 : void
1883 2722 : tds_free_bcpinfo(TDSBCPINFO *bcpinfo)
1884 : {
1885 2722 : if (bcpinfo) {
1886 394 : tds_deinit_bcpinfo(bcpinfo);
1887 394 : free(bcpinfo);
1888 : }
1889 2722 : }
1890 :
1891 : /**
1892 : * Reallocate a pointer and update it if success
1893 : * \param pp pointer to pointer to be reallocated
1894 : * \param new_size new size to be allocated
1895 : * \return new pointer allocated, NULL on failure
1896 : */
1897 : void *
1898 140212 : tds_realloc(void **pp, size_t new_size)
1899 : {
1900 : void *p;
1901 :
1902 : /* some implementation of malloc/realloc does not like size==0 */
1903 140212 : if (!new_size)
1904 0 : new_size = 1;
1905 :
1906 : /* use malloc if not allocated before, some implementations require it */
1907 140212 : if (*pp)
1908 55313 : p = realloc(*pp, new_size);
1909 : else
1910 84899 : p = malloc(new_size);
1911 :
1912 : /* update pointer only on success */
1913 140212 : if (p)
1914 140212 : *pp = p;
1915 :
1916 140212 : return p;
1917 : }
1918 :
1919 : void
1920 16 : tds_deinit_tvp(TDS_TVP *table)
1921 : {
1922 : TDS_TVP_ROW *tvp_row, *next_row;
1923 :
1924 16 : free(table->schema);
1925 16 : table->schema = NULL;
1926 16 : free(table->name);
1927 16 : table->name = NULL;
1928 32 : tds_free_param_results(table->metadata);
1929 16 : table->metadata = NULL;
1930 52 : for (tvp_row = table->row; tvp_row != NULL; tvp_row = next_row) {
1931 36 : next_row = tvp_row->next;
1932 36 : tds_free_tvp_row(tvp_row);
1933 36 : free(tvp_row);
1934 : }
1935 16 : table->row = NULL;
1936 16 : }
1937 :
1938 : /** @} */
|