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 : /**
22 : * \file
23 : * \brief Contains all routines to get replies from server
24 : */
25 : #include <config.h>
26 :
27 : #if HAVE_STRING_H
28 : #include <string.h>
29 : #endif /* HAVE_STRING_H */
30 :
31 : #if HAVE_STDLIB_H
32 : #include <stdlib.h>
33 : #endif /* HAVE_STDLIB_H */
34 :
35 : #include <assert.h>
36 :
37 : #if HAVE_MALLOC_H
38 : #include <malloc.h>
39 : #endif /* HAVE_MALLOC_H */
40 :
41 : #include <freetds/tds.h>
42 : #include <freetds/utils/string.h>
43 : #include <freetds/convert.h>
44 : #include <freetds/iconv.h>
45 : #include <freetds/checks.h>
46 : #include <freetds/bytes.h>
47 : #include <freetds/alloca.h>
48 : #include <freetds/encodings.h>
49 : #include <freetds/enum_cap.h>
50 : #include <freetds/replacements.h>
51 :
52 : /** \cond HIDDEN_SYMBOLS */
53 : #define USE_ICONV (tds->conn->use_iconv)
54 :
55 : #define TDS_GET_COLUMN_TYPE(col) do { \
56 : TDS_TINYINT _tds_type = tds_get_byte(tds); \
57 : if (!is_tds_type_valid(_tds_type)) \
58 : return TDS_FAIL; \
59 : tds_set_column_type(tds->conn, col, (TDS_SERVER_TYPE) _tds_type); \
60 : } while(0)
61 :
62 : #define TDS_GET_COLUMN_INFO(tds, col) \
63 : TDS_PROPAGATE(col->funcs->get_info(tds, col))
64 :
65 : /** \endcond */
66 :
67 : static TDSRET tds_process_info(TDSSOCKET * tds, int marker);
68 : static TDSRET tds_process_compute_result(TDSSOCKET * tds);
69 : static TDSRET tds_process_compute_names(TDSSOCKET * tds);
70 : static TDSRET tds7_process_compute_result(TDSSOCKET * tds);
71 : static TDSRET tds5_process_result(TDSSOCKET * tds);
72 : static TDSRET tds_process_col_name(TDSSOCKET * tds);
73 : static TDSRET tds_process_col_fmt(TDSSOCKET * tds);
74 : static TDSRET tds_process_tabname(TDSSOCKET *tds);
75 : static TDSRET tds_process_colinfo(TDSSOCKET * tds, char **names, int num_names);
76 : static TDSRET tds_process_compute(TDSSOCKET * tds);
77 : static TDSRET tds_process_cursor_tokens(TDSSOCKET * tds);
78 : static TDSRET tds_process_row(TDSSOCKET * tds);
79 : static TDSRET tds_process_nbcrow(TDSSOCKET * tds);
80 : static TDSRET tds_process_featureextack(TDSSOCKET * tds);
81 : static TDSRET tds_process_param_result(TDSSOCKET * tds, TDSPARAMINFO ** info);
82 : static TDSRET tds7_process_result(TDSSOCKET * tds);
83 : static TDSDYNAMIC *tds_process_dynamic(TDSSOCKET * tds);
84 : static TDSRET tds_process_auth(TDSSOCKET * tds);
85 : static TDSRET tds_process_env_chg(TDSSOCKET * tds);
86 : static TDSRET tds_process_param_result_tokens(TDSSOCKET * tds);
87 : static TDSRET tds_process_params_result_token(TDSSOCKET * tds);
88 : static TDSRET tds_process_dyn_result(TDSSOCKET * tds);
89 : static TDSRET tds5_process_result2(TDSSOCKET * tds);
90 : static TDSRET tds5_process_dyn_result2(TDSSOCKET * tds);
91 : static TDSRET tds_process_default_tokens(TDSSOCKET * tds, int marker);
92 : static TDSRET tds5_process_optioncmd(TDSSOCKET * tds);
93 : static TDSRET tds_process_end(TDSSOCKET * tds, int marker, /*@out@*/ int *flags_parm);
94 :
95 : static TDSRET tds_get_data_info(TDSSOCKET * tds, TDSCOLUMN * curcol, int is_param);
96 : static /*@observer@*/ const char *tds_token_name(unsigned char marker);
97 : static void adjust_character_column_size(TDSSOCKET * tds, TDSCOLUMN * curcol);
98 : static int determine_adjusted_size(const TDSICONV * char_conv, int size);
99 : static /*@observer@*/ const char *tds_pr_op(int op);
100 : static int tds_alloc_get_string(TDSSOCKET * tds, /*@special@*/ char **string, size_t len) /*allocates *string*/;
101 :
102 : /**
103 : * \ingroup libtds
104 : * \defgroup token Results processing
105 : * Handle tokens in packets. Many PDU (packets data unit) contain tokens.
106 : * (like result description, rows, data, errors and many other).
107 : */
108 :
109 :
110 : /**
111 : * \addtogroup token
112 : * @{
113 : */
114 :
115 : /**
116 : * tds_process_default_tokens() is a catch all function that is called to
117 : * process tokens not known to other tds_process_* routines
118 : * @tds
119 : * @param marker Token type
120 : */
121 : static TDSRET
122 59659 : tds_process_default_tokens(TDSSOCKET * tds, int marker)
123 : {
124 : int tok_size;
125 : int done_flags;
126 : TDS_INT ret_status;
127 : TDS_CAPABILITY_TYPE *cap;
128 :
129 59659 : CHECK_TDS_EXTRA(tds);
130 :
131 59659 : tdsdump_log(TDS_DBG_FUNC, "tds_process_default_tokens() marker is %x(%s)\n", marker, tds_token_name(marker));
132 :
133 59659 : if (IS_TDSDEAD(tds)) {
134 56 : tdsdump_log(TDS_DBG_FUNC, "leaving tds_process_default_tokens() connection dead\n");
135 56 : tds_close_socket(tds);
136 56 : return TDS_FAIL;
137 : }
138 :
139 59603 : switch (marker) {
140 718 : case TDS_AUTH_TOKEN:
141 718 : return tds_process_auth(tds);
142 : break;
143 20467 : case TDS_ENVCHANGE_TOKEN:
144 20467 : return tds_process_env_chg(tds);
145 : break;
146 3658 : case TDS_DONE_TOKEN:
147 : case TDS_DONEPROC_TOKEN:
148 : case TDS_DONEINPROC_TOKEN:
149 3658 : return tds_process_end(tds, marker, &done_flags);
150 : break;
151 0 : case TDS_PROCID_TOKEN:
152 0 : tds_get_n(tds, NULL, 8);
153 0 : break;
154 0 : case TDS_RETURNSTATUS_TOKEN:
155 0 : ret_status = tds_get_int(tds);
156 0 : marker = tds_peek(tds);
157 0 : if (marker != TDS_PARAM_TOKEN && marker != TDS_DONEPROC_TOKEN && marker != TDS_DONE_TOKEN
158 0 : && marker != TDS5_PARAMFMT_TOKEN)
159 : break;
160 0 : tds->has_status = true;
161 0 : tds->ret_status = ret_status;
162 0 : tdsdump_log(TDS_DBG_INFO1, "tds_process_default_tokens: return status is %d\n", tds->ret_status);
163 : break;
164 15664 : case TDS_ERROR_TOKEN:
165 : case TDS_INFO_TOKEN:
166 : case TDS_EED_TOKEN:
167 15664 : return tds_process_info(tds, marker);
168 : break;
169 718 : case TDS_CAPABILITY_TOKEN:
170 718 : tok_size = tds_get_usmallint(tds);
171 718 : cap = tds->conn->capabilities.types;
172 718 : memset(cap, 0, 2*sizeof(*cap));
173 718 : cap[0].type = 1;
174 718 : cap[0].len = sizeof(cap[0].values);
175 718 : cap[1].type = 2;
176 718 : cap[1].len = sizeof(cap[1].values);
177 2872 : while (tok_size > 1) {
178 : unsigned char type, size, *p;
179 :
180 1436 : type = tds_get_byte(tds);
181 1436 : size = tds_get_byte(tds);
182 1436 : tok_size -= 2 + size;
183 1436 : if (type != 1 && type != 2) {
184 0 : tds_get_n(tds, NULL, size);
185 0 : continue;
186 : }
187 1436 : if (size > sizeof(cap->values)) {
188 0 : tds_get_n(tds, NULL, size - sizeof(cap->values));
189 0 : size = sizeof(cap->values);
190 : }
191 1436 : p = (unsigned char *) &cap[type];
192 1436 : if (!tds_get_n(tds, p-size, size))
193 : return TDS_FAIL;
194 : /*
195 : * Sybase 11.0 servers return the wrong length in the capability packet,
196 : * causing us to read past the done packet.
197 : */
198 1436 : if (tds->conn->product_version < TDS_SYB_VER(12, 0, 0) && type == 2)
199 : break;
200 : }
201 : break;
202 : /* PARAM_TOKEN can be returned inserting text in db, to return new timestamp */
203 0 : case TDS_PARAM_TOKEN:
204 0 : tds_unget_byte(tds);
205 0 : return tds_process_param_result_tokens(tds);
206 : break;
207 0 : case TDS7_RESULT_TOKEN:
208 0 : return tds7_process_result(tds);
209 : break;
210 8 : case TDS_OPTIONCMD_TOKEN:
211 8 : return tds5_process_optioncmd(tds);
212 : break;
213 0 : case TDS_RESULT_TOKEN:
214 0 : return tds5_process_result(tds);
215 : break;
216 0 : case TDS_ROWFMT2_TOKEN:
217 0 : return tds5_process_result2(tds);
218 : break;
219 0 : case TDS_COLNAME_TOKEN:
220 0 : return tds_process_col_name(tds);
221 : break;
222 0 : case TDS_COLFMT_TOKEN:
223 0 : return tds_process_col_fmt(tds);
224 : break;
225 0 : case TDS_ROW_TOKEN:
226 0 : return tds_process_row(tds);
227 : break;
228 2 : case TDS5_PARAMFMT_TOKEN:
229 : /* store discarded parameters in param_info, not in old dynamic */
230 2 : tds_release_cur_dyn(tds);
231 2 : return tds_process_dyn_result(tds);
232 : break;
233 0 : case TDS5_PARAMFMT2_TOKEN:
234 0 : tds_release_cur_dyn(tds);
235 0 : return tds5_process_dyn_result2(tds);
236 : break;
237 2 : case TDS5_PARAMS_TOKEN:
238 : /* save params */
239 2 : return tds_process_params_result_token(tds);
240 : break;
241 0 : case TDS_CURINFO_TOKEN:
242 0 : return tds_process_cursor_tokens(tds);
243 : break;
244 7482 : case TDS_CONTROL_FEATUREEXTACK_TOKEN:
245 7482 : if (IS_TDS74_PLUS(tds->conn))
246 716 : return tds_process_featureextack(tds);
247 : /* fall through */
248 : case TDS5_DYNAMIC_TOKEN:
249 : case TDS_LOGINACK_TOKEN:
250 : case TDS_ORDERBY_TOKEN:
251 17642 : tdsdump_log(TDS_DBG_WARN, "Eating %s token\n", tds_token_name(marker));
252 17642 : tds_get_n(tds, NULL, tds_get_usmallint(tds));
253 17642 : break;
254 0 : case TDS_MSG_TOKEN:
255 0 : tok_size = tds_get_byte(tds);
256 0 : if (tok_size >= 3) {
257 0 : tds_get_byte(tds);
258 0 : tds5_negotiate_set_msg_type(tds->conn->authentication, tds_get_usmallint(tds));
259 0 : tok_size -= 3;
260 : }
261 0 : tds_get_n(tds, NULL, tok_size);
262 0 : break;
263 2 : case TDS_TABNAME_TOKEN: /* used for FOR BROWSE query */
264 2 : return tds_process_tabname(tds);
265 : break;
266 6 : case TDS_COLINFO_TOKEN:
267 6 : return tds_process_colinfo(tds, NULL, 0);
268 : break;
269 0 : case TDS_SESSIONSTATE_TOKEN:
270 : case TDS_ORDERBY2_TOKEN:
271 0 : tdsdump_log(TDS_DBG_WARN, "Eating %s token\n", tds_token_name(marker));
272 0 : tds_get_n(tds, NULL, tds_get_uint(tds));
273 0 : break;
274 0 : case TDS_NBC_ROW_TOKEN:
275 0 : return tds_process_nbcrow(tds);
276 : break;
277 0 : default:
278 0 : tds_close_socket(tds);
279 0 : tdserror(tds_get_ctx(tds), tds, TDSEBTOK, 0);
280 0 : tdsdump_log(TDS_DBG_ERROR, "Unknown marker: %d(%x)!!\n", marker, (unsigned char) marker);
281 : return TDS_FAIL;
282 : }
283 0 : return TDS_SUCCESS;
284 : }
285 :
286 : static TDSRET
287 3650 : tds_process_loginack(TDSSOCKET *tds, TDSRET *login_succeeded)
288 : {
289 : unsigned int len;
290 : unsigned char ack;
291 : TDS_UINT product_version;
292 3650 : int memrc = 0;
293 3650 : TDS_USMALLINT orig_tds_version = tds->conn->tds_version;
294 :
295 : struct { unsigned char major, minor, tiny[2];
296 : unsigned int reported;
297 : const char *name;
298 : } ver;
299 :
300 3650 : tds->conn->tds71rev1 = 0;
301 3650 : len = tds_get_usmallint(tds);
302 3650 : if (len < 10)
303 : return TDS_FAIL;
304 3650 : ack = tds_get_byte(tds);
305 :
306 3650 : ver.major = tds_get_byte(tds);
307 3650 : ver.minor = tds_get_byte(tds);
308 3650 : ver.tiny[0] = tds_get_byte(tds);
309 3650 : ver.tiny[1] = tds_get_byte(tds);
310 3650 : ver.reported = (ver.major << 24) | (ver.minor << 16) | (ver.tiny[0] << 8) | ver.tiny[1];
311 :
312 3650 : if (ver.reported == 0x07010000)
313 0 : tds->conn->tds71rev1 = 1;
314 :
315 : /* Log reported server product name, cf. MS-TDS LOGINACK documentation. */
316 3650 : switch (ver.reported) {
317 0 : case 0x07000000:
318 0 : ver.name = "7.0";
319 0 : tds->conn->tds_version = 0x700;
320 0 : break;
321 0 : case 0x07010000:
322 0 : ver.name = "2000";
323 0 : tds->conn->tds_version = 0x701;
324 0 : break;
325 1476 : case 0x71000001:
326 1476 : ver.name = "2000 SP1";
327 1476 : tds->conn->tds_version = 0x701;
328 1476 : break;
329 0 : case 0x72090002:
330 0 : ver.name = "2005";
331 0 : tds->conn->tds_version = 0x702;
332 0 : break;
333 0 : case 0x730A0003:
334 0 : ver.name = "2008 (no NBCROW or fSparseColumnSet)";
335 0 : tds->conn->tds_version = 0x703;
336 0 : break;
337 738 : case 0x730B0003:
338 738 : ver.name = "2008";
339 738 : tds->conn->tds_version = 0x703;
340 738 : break;
341 716 : case 0x74000004:
342 716 : ver.name = "2012-2019";
343 716 : tds->conn->tds_version = 0x704;
344 716 : break;
345 : default:
346 : ver.name = "unknown";
347 : break;
348 : }
349 :
350 3650 : tdsdump_log(TDS_DBG_FUNC, "server reports TDS version %x.%x.%x.%x\n",
351 : ver.major, ver.minor, ver.tiny[0], ver.tiny[1]);
352 3650 : tdsdump_log(TDS_DBG_FUNC, "Product name for 0x%x is %s\n", ver.reported, ver.name);
353 :
354 : /* Get server product name. */
355 : /* Ignore product name length; some servers seem to set it incorrectly. */
356 3650 : tds_get_byte(tds);
357 3650 : product_version = 0;
358 : /* Compute product name length from packet length. */
359 3650 : len -= 10;
360 3650 : free(tds->conn->product_name);
361 3650 : if (ver.major >= 7u) {
362 2930 : product_version = 0x80u;
363 2930 : memrc += tds_alloc_get_string(tds, &tds->conn->product_name, len / 2);
364 720 : } else if (ver.major >= 5) {
365 720 : memrc += tds_alloc_get_string(tds, &tds->conn->product_name, len);
366 : } else {
367 0 : memrc += tds_alloc_get_string(tds, &tds->conn->product_name, len);
368 0 : if (tds->conn->product_name != NULL && strstr(tds->conn->product_name, "Microsoft") != NULL)
369 0 : product_version = 0x80u;
370 : }
371 3650 : if (memrc != 0)
372 : return TDS_FAIL;
373 :
374 3650 : product_version |= tds_get_byte(tds); product_version <<= 8;
375 3650 : product_version |= tds_get_byte(tds); product_version <<= 8;
376 3650 : product_version |= tds_get_byte(tds); product_version <<= 8;
377 3650 : product_version |= tds_get_byte(tds);
378 :
379 : /*
380 : * MSSQL 6.5 and 7.0 seem to return strange values for this
381 : * using TDS 4.2, something like 5F 06 32 FF for 6.50
382 : */
383 3650 : if (ver.major == 4 && ver.minor == 2 && (product_version & 0xff0000ffu) == 0x5f0000ffu)
384 0 : product_version = ((product_version & 0xffff00u) | 0x800000u) << 8;
385 3650 : tds->conn->product_version = product_version;
386 3650 : tdsdump_log(TDS_DBG_FUNC, "Product version %lX\n", (unsigned long) product_version);
387 :
388 : /* internal version is ignored for TDS 8.0+ */
389 3650 : if (orig_tds_version >= 0x800)
390 0 : tds->conn->tds_version = orig_tds_version;
391 :
392 : /*
393 : * TDS 5.0 reports 5 on success 6 on failure
394 : * TDS 4.2 reports 1 on success and is not
395 : * present on failure
396 : */
397 3650 : if (ack == 5 || ack == 1 || (IS_TDS50(tds->conn) && ack == 0x85)) {
398 3648 : *login_succeeded = TDS_SUCCESS;
399 : /* authentication is now useless */
400 3648 : if (tds->conn->authentication) {
401 718 : tds->conn->authentication->free(tds->conn, tds->conn->authentication);
402 718 : tds->conn->authentication = NULL;
403 : }
404 : }
405 :
406 : return TDS_SUCCESS;
407 : }
408 :
409 : /**
410 : * tds_process_login_tokens() is called after sending the login packet
411 : * to the server. It returns the success or failure of the login
412 : * dependent on the protocol version. 4.2 sends an ACK token only when
413 : * successful, TDS 5.0 sends it always with a success byte within
414 : * @tds
415 : */
416 : TDSRET
417 3670 : tds_process_login_tokens(TDSSOCKET * tds)
418 : {
419 3670 : TDSRET succeed = TDS_FAIL;
420 : int marker;
421 :
422 3670 : CHECK_TDS_EXTRA(tds);
423 :
424 3670 : tdsdump_log(TDS_DBG_FUNC, "tds_process_login_tokens()\n");
425 : do {
426 30304 : marker = tds_get_byte(tds);
427 :
428 30304 : tdsdump_log(TDS_DBG_FUNC, "looking for login token, got %x(%s)\n", marker, tds_token_name(marker));
429 :
430 30304 : switch (marker) {
431 3650 : case TDS_LOGINACK_TOKEN:
432 3650 : TDS_PROPAGATE(tds_process_loginack(tds, &succeed));
433 : break;
434 26654 : default:
435 26654 : TDS_PROPAGATE(tds_process_default_tokens(tds, marker));
436 : break;
437 : }
438 30292 : if (marker == TDS_DONE_TOKEN && IS_TDS50(tds->conn) && tds->conn->authentication) {
439 0 : TDSAUTHENTICATION *auth = tds->conn->authentication;
440 0 : if (TDS_SUCCEED(auth->handle_next(tds, auth, 0))) {
441 0 : marker = 0;
442 0 : continue;
443 : }
444 : }
445 30292 : } while (marker != TDS_DONE_TOKEN);
446 :
447 : /* set the spid */
448 3658 : if (TDS_IS_MSSQL(tds))
449 2930 : tds->conn->spid = TDS_GET_A2BE(tds->in_buf+4);
450 :
451 3662 : tdsdump_log(TDS_DBG_FUNC, "tds_process_login_tokens() returning %s\n",
452 4 : (succeed == TDS_SUCCESS)? "TDS_SUCCESS" : "TDS_FAIL");
453 :
454 3658 : return succeed;
455 : }
456 :
457 : /**
458 : * Process authentication token.
459 : * This token is only TDS 7.0+.
460 : * \tds
461 : */
462 : static TDSRET
463 718 : tds_process_auth(TDSSOCKET * tds)
464 : {
465 : unsigned int pdu_size;
466 :
467 718 : CHECK_TDS_EXTRA(tds);
468 :
469 : #if ENABLE_EXTRA_CHECKS
470 718 : if (!IS_TDS7_PLUS(tds->conn))
471 0 : tdsdump_log(TDS_DBG_ERROR, "Called auth on TDS version < 7\n");
472 : #endif
473 :
474 718 : pdu_size = tds_get_usmallint(tds);
475 718 : tdsdump_log(TDS_DBG_INFO1, "TDS_AUTH_TOKEN PDU size %u\n", pdu_size);
476 :
477 718 : if (!tds->conn->authentication)
478 : return TDS_FAIL;
479 :
480 718 : return tds->conn->authentication->handle_next(tds, tds->conn->authentication, pdu_size);
481 : }
482 :
483 : /**
484 : * process all streams.
485 : * tds_process_tokens() is called after submitting a query with
486 : * tds_submit_query() and is responsible for calling the routines to
487 : * populate tds->res_info if appropriate (some query have no result sets)
488 : * @tds
489 : * @param result_type A pointer to an integer variable which
490 : * tds_process_tokens sets to indicate the current type of result.
491 : * @par
492 : * <b>Values that indicate command status</b>
493 : * <table>
494 : * <tr><td>TDS_DONE_RESULT</td><td>The results of a command have been completely processed.
495 : * This command returned no rows.</td></tr>
496 : * <tr><td>TDS_DONEPROC_RESULT</td><td>The results of a command have been completely processed.
497 : * This command returned rows.</td></tr>
498 : * <tr><td>TDS_DONEINPROC_RESULT</td><td>The results of a command have been completely processed.
499 : * This command returned rows.</td></tr>
500 : * </table>
501 : * <b>Values that indicate results information is available</b>
502 : * <table><tr>
503 : * <td>TDS_ROWFMT_RESULT</td><td>Regular Data format information</td>
504 : * <td>tds->res_info now contains the result details ; tds->current_results now points to that data</td>
505 : * </tr><tr>
506 : * <td>TDS_COMPUTEFMT_ RESULT</td><td>Compute data format information</td>
507 : * <td>tds->comp_info now contains the result data; tds->current_results now points to that data</td>
508 : * </tr><tr>
509 : * <td>TDS_DESCRIBE_RESULT</td><td></td>
510 : * <td></td>
511 : * </tr></table>
512 : * <b>Values that indicate data is available</b>
513 : * <table><tr>
514 : * <td><b>Value</b></td><td><b>Meaning</b></td><td><b>Information returned</b></td>
515 : * </tr><tr>
516 : * <td>TDS_ROW_RESULT</td><td>Regular row results</td>
517 : * <td>1 or more rows of regular data can now be retrieved</td>
518 : * </tr><tr>
519 : * <td>TDS_COMPUTE_RESULT</td><td>Compute row results</td>
520 : * <td>A single row of compute data can now be retrieved</td>
521 : * </tr><tr>
522 : * <td>TDS_PARAM_RESULT</td><td>Return parameter results</td>
523 : * <td>param_info or cur_dyn->params contain returned parameters</td>
524 : * </tr><tr>
525 : * <td>TDS_STATUS_RESULT</td><td>Stored procedure status results</td>
526 : * <td>tds->ret_status contain the returned code</td>
527 : * </tr></table>
528 : * @param done_flags Flags contained in the TDS_DONE*_TOKEN readed
529 : * @param flag Flags to select token type to stop/return
530 : * @todo Complete TDS_DESCRIBE_RESULT description
531 : * @retval TDS_SUCCESS if a result set is available for processing.
532 : * @retval TDS_FAIL on error.
533 : * @retval TDS_NO_MORE_RESULTS if all results have been completely processed.
534 : * @retval anything returned by one of the many functions it calls. :-(
535 : */
536 : TDSRET
537 543898 : tds_process_tokens(TDSSOCKET *tds, TDS_INT *result_type, int *done_flags, unsigned flag)
538 : {
539 : int marker;
540 543898 : TDSPARAMINFO *pinfo = NULL;
541 : TDSCOLUMN *curcol;
542 : TDSRET rc;
543 543898 : TDS_INT8 saved_rows_affected = tds->rows_affected;
544 : TDS_INT ret_status;
545 543898 : int cancel_seen = 0;
546 543898 : unsigned return_flag = 0;
547 :
548 : /** \cond HIDDEN_SYMBOLS */
549 : #define SET_RETURN(ret, f) do { \
550 : *result_type = ret; \
551 : return_flag = TDS_RETURN_##f | TDS_STOPAT_##f; \
552 : if (flag & TDS_STOPAT_##f) {\
553 : tds_unget_byte(tds); \
554 : tdsdump_log(TDS_DBG_FUNC, "tds_process_tokens::SET_RETURN stopping on current token\n"); \
555 : goto set_return_exit; \
556 : } } while(0)
557 : /** \endcond */
558 :
559 543898 : CHECK_TDS_EXTRA(tds);
560 :
561 543898 : tdsdump_log(TDS_DBG_FUNC, "tds_process_tokens(%p, %p, %p, 0x%x)\n", tds, result_type, done_flags, flag);
562 :
563 543898 : if (tds->state == TDS_IDLE || tds->state == TDS_SENDING) {
564 57969 : tdsdump_log(TDS_DBG_FUNC, "tds_process_tokens() state is COMPLETED\n");
565 57969 : *result_type = TDS_DONE_RESULT;
566 57969 : return TDS_NO_MORE_RESULTS;
567 : }
568 :
569 485929 : if (tds_set_state(tds, TDS_READING) != TDS_READING)
570 : return TDS_FAIL;
571 :
572 : rc = TDS_SUCCESS;
573 : for (;;) {
574 :
575 657336 : marker = tds_get_byte(tds);
576 657336 : tdsdump_log(TDS_DBG_INFO1, "processing result tokens. marker is %x(%s)\n", marker, tds_token_name(marker));
577 :
578 657336 : switch (marker) {
579 25585 : case TDS7_RESULT_TOKEN:
580 :
581 : /*
582 : * If we're processing the results of a cursor fetch
583 : * from sql server we don't want to pass back the
584 : * TDS_ROWFMT_RESULT to the calling API
585 : */
586 25585 : if (tds->current_op != TDS_OP_CURSORFETCH)
587 24761 : SET_RETURN(TDS_ROWFMT_RESULT, ROWFMT);
588 :
589 25345 : rc = tds7_process_result(tds);
590 25345 : if (TDS_FAILED(rc))
591 : break;
592 : /* handle browse information (if present) */
593 25345 : marker = tds_get_byte(tds);
594 25345 : if (marker != TDS_TABNAME_TOKEN)
595 25055 : tds_unget_byte(tds);
596 : else
597 290 : rc = tds_process_tabname(tds);
598 : break;
599 58 : case TDS_RESULT_TOKEN:
600 58 : SET_RETURN(TDS_ROWFMT_RESULT, ROWFMT);
601 58 : rc = tds5_process_result(tds);
602 58 : break;
603 6840 : case TDS_ROWFMT2_TOKEN:
604 6840 : SET_RETURN(TDS_ROWFMT_RESULT, ROWFMT);
605 6816 : rc = tds5_process_result2(tds);
606 6816 : break;
607 0 : case TDS_COLNAME_TOKEN:
608 0 : rc = tds_process_col_name(tds);
609 0 : break;
610 0 : case TDS_COLFMT_TOKEN:
611 0 : SET_RETURN(TDS_ROWFMT_RESULT, ROWFMT);
612 0 : rc = tds_process_col_fmt(tds);
613 0 : if (TDS_FAILED(rc))
614 : break;
615 : /* handle browse information (if present) */
616 0 : marker = tds_get_byte(tds);
617 0 : if (marker != TDS_TABNAME_TOKEN)
618 0 : tds_unget_byte(tds);
619 : else
620 0 : rc = tds_process_tabname(tds);
621 : break;
622 5282 : case TDS_PARAM_TOKEN:
623 5282 : tds_unget_byte(tds);
624 5282 : if (tds->current_op) {
625 4448 : tdsdump_log(TDS_DBG_FUNC, "processing parameters for op %d\n", tds->current_op);
626 15934 : while ((marker = tds_get_byte(tds)) == TDS_PARAM_TOKEN) {
627 11486 : tdsdump_log(TDS_DBG_INFO1, "calling tds_process_param_result\n");
628 11486 : rc = tds_process_param_result(tds, &pinfo);
629 11486 : if (TDS_FAILED(rc))
630 : goto set_return_exit;
631 : }
632 4448 : tds_unget_byte(tds);
633 4448 : tdsdump_log(TDS_DBG_FUNC, "%d hidden return parameters\n", pinfo ? pinfo->num_cols : -1);
634 4448 : if (pinfo && pinfo->num_cols > 0) {
635 4448 : curcol = pinfo->columns[0];
636 4448 : if (tds->current_op == TDS_OP_CURSOROPEN && tds->cur_cursor) {
637 2346 : TDSCURSOR *cursor = tds->cur_cursor;
638 :
639 2346 : cursor->cursor_id = *(TDS_INT *) curcol->column_data;
640 2346 : tdsdump_log(TDS_DBG_FUNC, "stored internal cursor id %d\n", cursor->cursor_id);
641 2346 : cursor->srv_status &= ~(TDS_CUR_ISTAT_CLOSED|TDS_CUR_ISTAT_OPEN|TDS_CUR_ISTAT_DEALLOC);
642 2346 : cursor->srv_status |= cursor->cursor_id ? TDS_CUR_ISTAT_OPEN : TDS_CUR_ISTAT_CLOSED|TDS_CUR_ISTAT_DEALLOC;
643 : }
644 4448 : if ((tds->current_op == TDS_OP_PREPARE || tds->current_op == TDS_OP_PREPEXEC)
645 2102 : && tds->cur_dyn && tds->cur_dyn->num_id == 0 && curcol->column_cur_size > 0) {
646 2092 : tds->cur_dyn->num_id = *(TDS_INT *) curcol->column_data;
647 : }
648 4448 : if (tds->current_op == TDS_OP_UNPREPARE)
649 0 : tds_dynamic_deallocated(tds->conn, tds->cur_dyn);
650 : }
651 4448 : tds_free_param_results(pinfo);
652 : } else {
653 834 : SET_RETURN(TDS_PARAM_RESULT, PROC);
654 834 : rc = tds_process_param_result_tokens(tds);
655 : }
656 : break;
657 18 : case TDS_COMPUTE_NAMES_TOKEN:
658 18 : rc = tds_process_compute_names(tds);
659 18 : break;
660 18 : case TDS_COMPUTE_RESULT_TOKEN:
661 18 : SET_RETURN(TDS_COMPUTEFMT_RESULT, COMPUTEFMT);
662 18 : rc = tds_process_compute_result(tds);
663 18 : break;
664 54 : case TDS7_COMPUTE_RESULT_TOKEN:
665 54 : SET_RETURN(TDS_COMPUTEFMT_RESULT, COMPUTEFMT);
666 54 : rc = tds7_process_compute_result(tds);
667 54 : break;
668 394171 : case TDS_ROW_TOKEN:
669 : case TDS_NBC_ROW_TOKEN:
670 : /* overstepped the mark... */
671 394171 : if (tds->cur_cursor) {
672 2288 : tds_set_current_results(tds, tds->cur_cursor->res_info);
673 2288 : tdsdump_log(TDS_DBG_INFO1, "tds_process_tokens(). set current_results to cursor->res_info\n");
674 : } else {
675 : /* assure that we point to row, not to compute */
676 391883 : if (tds->res_info)
677 391883 : tds_set_current_results(tds, tds->res_info);
678 : }
679 : /* I don't know when this it's false but it happened, also server can send garbage... */
680 394171 : if (tds->current_results)
681 394171 : tds->current_results->rows_exist = true;
682 394171 : SET_RETURN(TDS_ROW_RESULT, ROW);
683 :
684 365739 : switch (marker) {
685 360787 : case TDS_ROW_TOKEN:
686 360787 : rc = tds_process_row(tds);
687 360787 : break;
688 4952 : case TDS_NBC_ROW_TOKEN:
689 4952 : rc = tds_process_nbcrow(tds);
690 4952 : break;
691 : }
692 : break;
693 184 : case TDS_CMP_ROW_TOKEN:
694 : /* I don't know when this it's false but it happened, also server can send garbage... */
695 184 : if (tds->res_info)
696 184 : tds->res_info->rows_exist = true;
697 184 : SET_RETURN(TDS_COMPUTE_RESULT, COMPUTE);
698 104 : rc = tds_process_compute(tds);
699 104 : break;
700 11946 : case TDS_RETURNSTATUS_TOKEN:
701 11946 : ret_status = tds_get_int(tds);
702 11946 : marker = tds_peek(tds);
703 11946 : if (marker != TDS_PARAM_TOKEN && marker != TDS_DONEPROC_TOKEN && marker != TDS_DONE_TOKEN && marker != TDS5_PARAMFMT_TOKEN && marker != TDS5_PARAMFMT2_TOKEN)
704 : break;
705 11785 : if (tds->current_op) {
706 : /* TODO perhaps we should use ret_status ?? */
707 : } else {
708 : /* TODO optimize */
709 1799 : flag &= ~TDS_STOPAT_PROC;
710 1799 : SET_RETURN(TDS_STATUS_RESULT, PROC);
711 1799 : tds->has_status = true;
712 1799 : tds->ret_status = ret_status;
713 1799 : tdsdump_log(TDS_DBG_FUNC, "tds_process_tokens: return status is %d\n", tds->ret_status);
714 : rc = TDS_SUCCESS;
715 : }
716 : break;
717 984 : case TDS5_DYNAMIC_TOKEN:
718 : /* process acknowledge dynamic */
719 984 : tds_set_cur_dyn(tds, tds_process_dynamic(tds));
720 : /* special case, prepared statement cannot be prepared */
721 1968 : if (!tds->cur_dyn || tds->cur_dyn->emulated)
722 : break;
723 984 : marker = tds_get_byte(tds);
724 984 : if (marker != TDS_EED_TOKEN) {
725 918 : tds_unget_byte(tds);
726 918 : break;
727 : }
728 66 : tds_process_info(tds, marker);
729 130 : if (!tds->cur_dyn || !tds->cur_dyn->emulated)
730 : break;
731 60 : marker = tds_get_byte(tds);
732 60 : if (marker != TDS_DONE_TOKEN) {
733 0 : tds_unget_byte(tds);
734 0 : break;
735 : }
736 60 : rc = tds_process_end(tds, marker, done_flags);
737 60 : if (done_flags)
738 60 : *done_flags &= ~TDS_DONE_ERROR;
739 : /* FIXME warning to macro expansion */
740 60 : SET_RETURN(TDS_DONE_RESULT, DONE);
741 : break;
742 18 : case TDS5_PARAMFMT_TOKEN:
743 18 : SET_RETURN(TDS_DESCRIBE_RESULT, PARAMFMT);
744 18 : rc = tds_process_dyn_result(tds);
745 18 : break;
746 336 : case TDS5_PARAMFMT2_TOKEN:
747 336 : SET_RETURN(TDS_DESCRIBE_RESULT, PARAMFMT);
748 336 : rc = tds5_process_dyn_result2(tds);
749 336 : break;
750 98 : case TDS5_PARAMS_TOKEN:
751 98 : SET_RETURN(TDS_PARAM_RESULT, PROC);
752 98 : rc = tds_process_params_result_token(tds);
753 98 : break;
754 40 : case TDS_CURINFO_TOKEN:
755 40 : rc = tds_process_cursor_tokens(tds);
756 40 : break;
757 139943 : case TDS_DONE_TOKEN:
758 139943 : SET_RETURN(TDS_DONE_RESULT, DONE);
759 139547 : rc = tds_process_end(tds, marker, done_flags);
760 139547 : switch (tds->current_op) {
761 280 : case TDS_OP_DYN_DEALLOC:
762 280 : if (done_flags && (*done_flags & TDS_DONE_ERROR) == 0)
763 278 : tds_dynamic_deallocated(tds->conn, tds->cur_dyn);
764 : break;
765 : default:
766 : break;
767 : }
768 : break;
769 12102 : case TDS_DONEPROC_TOKEN:
770 12102 : SET_RETURN(TDS_DONEPROC_RESULT, DONE);
771 12094 : rc = tds_process_end(tds, marker, done_flags);
772 12094 : tds->rows_affected = saved_rows_affected;
773 12094 : switch (tds->current_op) {
774 : default:
775 : break;
776 2346 : case TDS_OP_CURSOROPEN:
777 2346 : *result_type = TDS_DONE_RESULT;
778 2346 : break;
779 280 : case TDS_OP_CURSORCLOSE:
780 280 : tdsdump_log(TDS_DBG_FUNC, "TDS_OP_CURSORCLOSE\n");
781 280 : if (tds->cur_cursor) {
782 :
783 280 : TDSCURSOR *cursor = tds->cur_cursor;
784 :
785 280 : cursor->srv_status &= ~TDS_CUR_ISTAT_OPEN;
786 280 : cursor->srv_status |= TDS_CUR_ISTAT_CLOSED|TDS_CUR_ISTAT_DECLARED;
787 280 : if (cursor->status.dealloc == TDS_CURSOR_STATE_SENT) {
788 0 : tds_cursor_deallocated(tds->conn, cursor);
789 : }
790 : }
791 280 : *result_type = TDS_NO_MORE_RESULTS;
792 280 : rc = TDS_NO_MORE_RESULTS;
793 280 : break;
794 2068 : case TDS_OP_UNPREPARE:
795 2068 : if (done_flags && (*done_flags & TDS_DONE_ERROR) == 0)
796 2068 : tds_dynamic_deallocated(tds->conn, tds->cur_dyn);
797 2068 : *result_type = TDS_NO_MORE_RESULTS;
798 2068 : rc = TDS_NO_MORE_RESULTS;
799 2068 : break;
800 1392 : case TDS_OP_CURSOR:
801 : case TDS_OP_CURSORPREPARE:
802 : case TDS_OP_CURSOREXECUTE:
803 : case TDS_OP_CURSORPREPEXEC:
804 : case TDS_OP_CURSORUNPREPARE:
805 : case TDS_OP_CURSORFETCH:
806 : case TDS_OP_CURSOROPTION:
807 : case TDS_OP_PREPEXECRPC:
808 1392 : *result_type = TDS_NO_MORE_RESULTS;
809 1392 : rc = TDS_NO_MORE_RESULTS;
810 1392 : break;
811 : }
812 : break;
813 26554 : case TDS_DONEINPROC_TOKEN:
814 26554 : switch(tds->current_op) {
815 3502 : case TDS_OP_CURSOROPEN:
816 : case TDS_OP_CURSORFETCH:
817 : case TDS_OP_PREPARE:
818 : case TDS_OP_CURSORCLOSE:
819 3502 : rc = tds_process_end(tds, marker, done_flags);
820 3502 : if (tds->rows_affected != TDS_NO_COUNT) {
821 3090 : saved_rows_affected = tds->rows_affected;
822 : }
823 : break;
824 23052 : default:
825 23052 : SET_RETURN(TDS_DONEINPROC_RESULT, DONE);
826 23012 : rc = tds_process_end(tds, marker, done_flags);
827 23012 : break;
828 : }
829 : break;
830 8600 : case TDS_ERROR_TOKEN:
831 : case TDS_INFO_TOKEN:
832 : case TDS_EED_TOKEN:
833 8600 : SET_RETURN(TDS_MSG_RESULT, MSG);
834 8532 : rc = tds_process_default_tokens(tds, marker);
835 8532 : break;
836 6767 : case TDS_ENVCHANGE_TOKEN:
837 6767 : SET_RETURN(TDS_MSG_RESULT, ENV);
838 6767 : rc = tds_process_default_tokens(tds, marker);
839 6767 : break;
840 17738 : default:
841 17738 : SET_RETURN(TDS_OTHERS_RESULT, OTHERS);
842 17702 : rc = tds_process_default_tokens(tds, marker);
843 17702 : break;
844 : }
845 :
846 657503 : set_return_exit:
847 657336 : if (TDS_FAILED(rc)) {
848 1822 : if (rc == TDS_CANCELLED)
849 1778 : tds_set_state(tds, TDS_PENDING);
850 : else
851 44 : tds_close_socket(tds);
852 : return rc;
853 : }
854 :
855 655514 : cancel_seen |= tds->in_cancel;
856 655514 : if (cancel_seen) {
857 : /* during cancel handle all tokens */
858 75203 : flag = TDS_HANDLE_ALL;
859 : }
860 :
861 655514 : if ((return_flag & flag) != 0) {
862 483405 : tds_set_state(tds, TDS_PENDING);
863 483405 : return rc;
864 : }
865 :
866 172109 : if (tds->state == TDS_IDLE || tds->state == TDS_SENDING)
867 692 : return cancel_seen ? TDS_CANCELLED : TDS_NO_MORE_RESULTS;
868 :
869 171417 : if (tds->state == TDS_DEAD) {
870 : /* TODO free all results ?? */
871 : return TDS_FAIL;
872 : }
873 : }
874 : }
875 :
876 : /**
877 : * Process results for simple query as "SET TEXTSIZE" or "USE dbname"
878 : * If the statement returns results, beware they are discarded.
879 : *
880 : * This function was written to avoid direct calls to tds_process_default_tokens
881 : * (which caused problems such as ignoring query errors).
882 : * Results are read until idle state or severe failure (do not stop for
883 : * statement failure).
884 : * @return see tds_process_tokens for results (TDS_NO_MORE_RESULTS is never returned)
885 : */
886 : TDSRET
887 7526 : tds_process_simple_query(TDSSOCKET * tds)
888 : {
889 : TDS_INT res_type;
890 : TDS_INT done_flags;
891 : TDSRET rc;
892 7526 : TDSRET ret = TDS_SUCCESS;
893 :
894 7526 : CHECK_TDS_EXTRA(tds);
895 :
896 22052 : while ((rc = tds_process_tokens(tds, &res_type, &done_flags, TDS_RETURN_DONE)) == TDS_SUCCESS) {
897 7000 : switch (res_type) {
898 :
899 7000 : case TDS_DONE_RESULT:
900 : case TDS_DONEPROC_RESULT:
901 : case TDS_DONEINPROC_RESULT:
902 7000 : if ((done_flags & TDS_DONE_ERROR) != 0)
903 18 : ret = TDS_FAIL;
904 : break;
905 :
906 : default:
907 : break;
908 : }
909 : }
910 7526 : if (TDS_FAILED(rc))
911 12 : ret = rc;
912 :
913 7526 : return ret;
914 : }
915 :
916 : /**
917 : * Holds list of names
918 : */
919 : struct namelist
920 : {
921 : /** string name */
922 : char *name;
923 : /** next element in the list */
924 : struct namelist *next;
925 : };
926 :
927 : /**
928 : * Frees list of names
929 : * \param head list head to free
930 : */
931 : static void
932 310 : tds_free_namelist(struct namelist *head)
933 : {
934 310 : struct namelist *cur = head, *prev;
935 :
936 958 : while (cur != NULL) {
937 338 : prev = cur;
938 338 : cur = cur->next;
939 338 : free(prev->name);
940 338 : free(prev);
941 : }
942 310 : }
943 :
944 : /**
945 : * Reads list of names (usually table names)
946 : * \tds
947 : * \param remainder bytes left to read
948 : * \param p_head list head to return
949 : * \param large true if name length from network are 2 byte (usually 1)
950 : */
951 : static int
952 20 : tds_read_namelist(TDSSOCKET * tds, int remainder, struct namelist **p_head, int large)
953 : {
954 20 : struct namelist *head = NULL, *cur = NULL, *prev;
955 20 : int num_names = 0;
956 :
957 : /*
958 : * this is a little messy...TDS 5.0 gives the number of columns
959 : * upfront, while in TDS 4.2, you're expected to figure it out
960 : * by the size of the message. So, I use a link list to get the
961 : * colum names and then allocate the result structure, copy
962 : * and delete the linked list
963 : */
964 64 : while (remainder > 0) {
965 : TDS_USMALLINT namelen;
966 :
967 24 : prev = cur;
968 24 : if (!(cur = tds_new(struct namelist, 1))) {
969 0 : tds_free_namelist(head);
970 0 : return -1;
971 : }
972 :
973 24 : cur->next = NULL;
974 24 : if (prev)
975 4 : prev->next = cur;
976 : else
977 : head = cur;
978 :
979 24 : if (large) {
980 0 : namelen = tds_get_usmallint(tds);
981 0 : remainder -= 2;
982 : } else {
983 24 : namelen = tds_get_byte(tds);
984 24 : --remainder;
985 : }
986 :
987 24 : if (tds_alloc_get_string(tds, &cur->name, namelen) < 0) {
988 0 : tds_free_namelist(head);
989 0 : return -1;
990 : }
991 :
992 24 : remainder -= namelen;
993 24 : if (IS_TDS7_PLUS(tds->conn))
994 0 : remainder -= namelen;
995 24 : num_names++;
996 : }
997 :
998 20 : *p_head = head;
999 20 : return num_names;
1000 : }
1001 :
1002 : /**
1003 : * tds_process_col_name() is one half of the result set under TDS 4.2
1004 : * it contains all the column names, a TDS_COLFMT_TOKEN should
1005 : * immediately follow this token with the datatype/size information
1006 : * This is a 4.2 only function
1007 : * \tds
1008 : */
1009 : static TDSRET
1010 0 : tds_process_col_name(TDSSOCKET * tds)
1011 : {
1012 : int hdrsize;
1013 0 : int col, num_names = 0;
1014 0 : struct namelist *head = NULL, *cur = NULL;
1015 : TDSCOLUMN *curcol;
1016 : TDSRESULTINFO *info;
1017 :
1018 0 : CHECK_TDS_EXTRA(tds);
1019 :
1020 0 : hdrsize = tds_get_usmallint(tds);
1021 :
1022 0 : if ((num_names = tds_read_namelist(tds, hdrsize, &head, 0)) < 0)
1023 : return TDS_FAIL;
1024 :
1025 : /* free results/computes/params etc... */
1026 0 : tds_free_all_results(tds);
1027 0 : tds->rows_affected = TDS_NO_COUNT;
1028 :
1029 0 : if ((info = tds_alloc_results(num_names)) == NULL)
1030 : goto memory_error;
1031 :
1032 0 : tds->res_info = info;
1033 0 : tds_set_current_results(tds, info);
1034 :
1035 0 : cur = head;
1036 0 : for (col = 0; col < num_names; ++col) {
1037 0 : curcol = info->columns[col];
1038 0 : if (!tds_dstr_copy(&curcol->column_name, cur->name))
1039 : goto memory_error;
1040 0 : cur = cur->next;
1041 : }
1042 0 : tds_free_namelist(head);
1043 0 : return TDS_SUCCESS;
1044 :
1045 0 : memory_error:
1046 0 : tds_free_namelist(head);
1047 0 : return TDS_FAIL;
1048 : }
1049 :
1050 : /**
1051 : * tds_process_col_fmt() is the other half of result set processing
1052 : * under TDS 4.2. It follows tds_process_col_name(). It contains all the
1053 : * column type and size information.
1054 : * This is a 4.2 only function
1055 : * \tds
1056 : */
1057 : static TDSRET
1058 0 : tds_process_col_fmt(TDSSOCKET * tds)
1059 : {
1060 : unsigned int col;
1061 : TDSCOLUMN *curcol;
1062 : TDSRESULTINFO *info;
1063 : TDS_USMALLINT flags;
1064 :
1065 0 : CHECK_TDS_EXTRA(tds);
1066 :
1067 0 : tds_get_usmallint(tds); /* hdrsize */
1068 :
1069 : /* TODO use current_results instead of res_info ?? */
1070 0 : info = tds->res_info;
1071 0 : if (!info || info->num_cols < 0)
1072 : return TDS_FAIL;
1073 0 : for (col = 0; col < info->num_cols; col++) {
1074 0 : curcol = info->columns[col];
1075 : /* In Sybase all 4 byte are used for usertype, while mssql place 2 byte as usertype and 2 byte as flags */
1076 0 : if (TDS_IS_MSSQL(tds)) {
1077 0 : curcol->column_usertype = tds_get_smallint(tds);
1078 0 : flags = tds_get_usmallint(tds);
1079 0 : curcol->column_nullable = flags & 0x01;
1080 0 : curcol->column_writeable = (flags & 0x08) > 0;
1081 0 : curcol->column_identity = (flags & 0x10) > 0;
1082 : } else {
1083 0 : curcol->column_usertype = tds_get_int(tds);
1084 : }
1085 : /* on with our regularly scheduled code (mlilback, 11/7/01) */
1086 0 : TDS_GET_COLUMN_TYPE(curcol);
1087 :
1088 0 : tdsdump_log(TDS_DBG_INFO1, "processing result. type = %d(%s), varint_size %d\n",
1089 0 : curcol->column_type, tds_prtype(curcol->column_type), curcol->column_varint_size);
1090 :
1091 0 : TDS_GET_COLUMN_INFO(tds, curcol);
1092 :
1093 : /* Adjust column size according to client's encoding */
1094 0 : curcol->on_server.column_size = curcol->column_size;
1095 0 : adjust_character_column_size(tds, curcol);
1096 : }
1097 :
1098 0 : return tds_alloc_row(info);
1099 : }
1100 :
1101 : /**
1102 : * Reads table names for TDS 7.1+.
1103 : * TDS 7.1+ return table names as an array of names
1104 : * (so database.schema.owner.name as separate names)
1105 : * \tds
1106 : * \param remainder bytes left to read
1107 : * \param p_head pointer to list head to return
1108 : * \return number of element returned or -1 on error
1109 : */
1110 : static int
1111 290 : tds71_read_table_names(TDSSOCKET *tds, int remainder, struct namelist **p_head)
1112 : {
1113 290 : struct namelist *head = NULL, *cur = NULL, *prev;
1114 290 : int num_names = 0;
1115 :
1116 : /*
1117 : * this is a little messy...TDS 5.0 gives the number of columns
1118 : * upfront, while in TDS 4.2, you're expected to figure it out
1119 : * by the size of the message. So, I use a link list to get the
1120 : * colum names and then allocate the result structure, copy
1121 : * and delete the linked list
1122 : */
1123 894 : while (remainder > 0) {
1124 : int elements, i;
1125 : size_t len;
1126 : char *partials[4], *p;
1127 :
1128 314 : prev = cur;
1129 314 : if (!(cur = tds_new(struct namelist, 1))) {
1130 0 : tds_free_namelist(head);
1131 0 : return -1;
1132 : }
1133 :
1134 314 : cur->name = NULL;
1135 314 : cur->next = NULL;
1136 314 : if (prev)
1137 24 : prev->next = cur;
1138 : else
1139 : head = cur;
1140 :
1141 314 : elements = tds_get_byte(tds);
1142 314 : --remainder;
1143 314 : if (elements <= 0 || elements > 4) {
1144 0 : tds_free_namelist(head);
1145 0 : return -1;
1146 : }
1147 :
1148 : /* read partials IDs and compute full length */
1149 : len = 0;
1150 314 : for (i = 0; i < elements; ++i) {
1151 314 : TDS_USMALLINT namelen = tds_get_usmallint(tds);
1152 314 : remainder -= 2 + 2 * namelen;
1153 314 : if (tds_alloc_get_string(tds, &partials[i], namelen) < 0) {
1154 0 : while (i > 0)
1155 0 : free(partials[--i]);
1156 0 : tds_free_namelist(head);
1157 0 : return -1;
1158 : }
1159 314 : len += tds_quote_id(tds, NULL, partials[i], -1) + 1;
1160 : }
1161 :
1162 : /* allocate full name */
1163 314 : p = tds_new(char, len);
1164 314 : if (!p) {
1165 : i = elements;
1166 0 : while (i > 0)
1167 0 : free(partials[--i]);
1168 0 : tds_free_namelist(head);
1169 0 : return -1;
1170 : }
1171 :
1172 : /* compose names */
1173 314 : cur->name = p;
1174 628 : for (i = 0; i < elements; ++i) {
1175 314 : p += tds_quote_id(tds, p, partials[i], -1);
1176 314 : *p++ = '.';
1177 314 : free(partials[i]);
1178 : }
1179 314 : *--p = 0;
1180 :
1181 314 : num_names++;
1182 : }
1183 :
1184 290 : *p_head = head;
1185 290 : return num_names;
1186 : }
1187 :
1188 : /**
1189 : * Process list of table from network.
1190 : * This token is only TDS 4.2
1191 : * \tds
1192 : */
1193 : static TDSRET
1194 292 : tds_process_tabname(TDSSOCKET *tds)
1195 : {
1196 : struct namelist *head, *cur;
1197 : int num_names, hdrsize, i;
1198 : char **names;
1199 : unsigned char marker;
1200 : TDSRET rc;
1201 :
1202 292 : hdrsize = tds_get_usmallint(tds);
1203 :
1204 : /* different structure for tds7.1 */
1205 : /* hdrsize check is required for tds7.1 revision 1 (mssql without SPs) */
1206 : /* TODO change tds_version ?? */
1207 292 : if (IS_TDS71_PLUS(tds->conn) && (!IS_TDS71(tds->conn) || !tds->conn->tds71rev1))
1208 290 : num_names = tds71_read_table_names(tds, hdrsize, &head);
1209 : else
1210 2 : num_names = tds_read_namelist(tds, hdrsize, &head, IS_TDS7_PLUS(tds->conn));
1211 292 : if (num_names <= 0)
1212 : return TDS_FAIL;
1213 :
1214 : /* put in an array */
1215 292 : names = tds_new(char*, num_names);
1216 292 : if (!names) {
1217 0 : tds_free_namelist(head);
1218 0 : return TDS_FAIL;
1219 : }
1220 612 : for (cur = head, i = 0; i < num_names; ++i, cur = cur->next)
1221 320 : names[i] = cur->name;
1222 :
1223 292 : rc = TDS_SUCCESS;
1224 292 : marker = tds_get_byte(tds);
1225 292 : if (marker != TDS_COLINFO_TOKEN)
1226 0 : tds_unget_byte(tds);
1227 : else
1228 292 : rc = tds_process_colinfo(tds, names, num_names);
1229 :
1230 292 : free(names);
1231 292 : tds_free_namelist(head);
1232 292 : return rc;
1233 : }
1234 :
1235 : /**
1236 : * Reads column information.
1237 : * This token is only TDS 4.2
1238 : * \tds
1239 : * \param[in] names table names
1240 : * \param[in] num_names number of table names
1241 : */
1242 : static TDSRET
1243 298 : tds_process_colinfo(TDSSOCKET * tds, char **names, int num_names)
1244 : {
1245 : unsigned int hdrsize, l;
1246 : TDSCOLUMN *curcol;
1247 : TDSRESULTINFO *info;
1248 298 : unsigned int bytes_read = 0;
1249 : struct {
1250 : unsigned char num_col;
1251 : unsigned char num_table;
1252 : unsigned char flags;
1253 : } col_info;
1254 :
1255 298 : CHECK_TDS_EXTRA(tds);
1256 :
1257 298 : hdrsize = tds_get_usmallint(tds);
1258 :
1259 298 : info = tds->current_results;
1260 :
1261 1722 : while (bytes_read < hdrsize) {
1262 :
1263 1126 : tds_get_n(tds, &col_info, 3);
1264 1126 : bytes_read += 3;
1265 :
1266 1126 : curcol = NULL;
1267 1126 : if (info && col_info.num_col > 0 && col_info.num_col <= info->num_cols)
1268 1126 : curcol = info->columns[col_info.num_col - 1];
1269 :
1270 1126 : if (curcol) {
1271 1126 : curcol->column_writeable = (col_info.flags & 0x4) == 0;
1272 1126 : curcol->column_key = (col_info.flags & 0x8) > 0;
1273 1126 : curcol->column_hidden = (col_info.flags & 0x10) > 0;
1274 :
1275 1126 : if (names && col_info.num_table > 0 && col_info.num_table <= num_names)
1276 814 : if (!tds_dstr_copy(&curcol->table_name, names[col_info.num_table - 1]))
1277 : return TDS_FAIL;
1278 : }
1279 : /* read real column name */
1280 1126 : if (col_info.flags & 0x20) {
1281 36 : l = tds_get_byte(tds);
1282 36 : if (curcol) {
1283 36 : tds_dstr_get(tds, &curcol->table_column_name, l);
1284 36 : if (IS_TDS7_PLUS(tds->conn))
1285 32 : l *= 2;
1286 : } else {
1287 0 : if (IS_TDS7_PLUS(tds->conn))
1288 0 : l *= 2;
1289 : /* discard silently */
1290 0 : tds_get_n(tds, NULL, l);
1291 : }
1292 36 : bytes_read += l + 1;
1293 : }
1294 : }
1295 :
1296 : return TDS_SUCCESS;
1297 : }
1298 :
1299 : /**
1300 : * process output parameters of a stored
1301 : * procedure. This differs from regular row/compute results in that there
1302 : * is no total number of parameters given, they just show up singly.
1303 : * \tds
1304 : * \param[out] pinfo output parameter.
1305 : * Should point to a not allocated structure
1306 : */
1307 : static TDSRET
1308 12576 : tds_process_param_result(TDSSOCKET * tds, TDSPARAMINFO ** pinfo)
1309 : {
1310 : TDSCOLUMN *curparam;
1311 : TDSPARAMINFO *info;
1312 : TDSRET token;
1313 :
1314 12576 : tdsdump_log(TDS_DBG_FUNC, "tds_process_param_result(%p, %p)\n", tds, pinfo);
1315 :
1316 12576 : CHECK_TDS_EXTRA(tds);
1317 12576 : if (*pinfo)
1318 7294 : CHECK_PARAMINFO_EXTRA(*pinfo);
1319 :
1320 : /* TODO check if current_results is a param result */
1321 :
1322 : /* limited to 64K but possible types are always smaller (not TEXT/IMAGE) */
1323 12576 : tds_get_smallint(tds); /* header size */
1324 12576 : if ((info = tds_alloc_param_result(*pinfo)) == NULL)
1325 : return TDS_FAIL;
1326 :
1327 12576 : *pinfo = info;
1328 12576 : curparam = info->columns[info->num_cols - 1];
1329 :
1330 : /*
1331 : * FIXME check support for tds7+ (seem to use same format of tds5 for data...)
1332 : * perhaps varint_size can be 2 or collation can be specified ??
1333 : */
1334 12576 : TDS_PROPAGATE(tds_get_data_info(tds, curparam, 1));
1335 :
1336 12576 : curparam->column_cur_size = curparam->column_size; /* needed ?? */
1337 :
1338 12576 : if (tds_alloc_param_data(curparam) == NULL)
1339 : return TDS_FAIL;
1340 :
1341 12576 : token = curparam->funcs->get_data(tds, curparam);
1342 12576 : if (TDS_UNLIKELY(tds_write_dump))
1343 0 : tdsdump_col(curparam);
1344 :
1345 : /*
1346 : * Real output parameters will either be unnamed or will have a valid
1347 : * parameter name beginning with '@'. Ignore any other Spurious parameters
1348 : * such as those returned from calls to writetext in the proc.
1349 : */
1350 25152 : if (!tds_dstr_isempty(&curparam->column_name) && tds_dstr_cstr(&curparam->column_name)[0] != '@')
1351 18 : tds_free_param_result(*pinfo);
1352 :
1353 : return token;
1354 : }
1355 :
1356 : /**
1357 : * Process parameters from networks.
1358 : * Read all consecutives paramaters, not a single one.
1359 : * Parameters are then stored in tds->param_info or tds->cur_dyn->res_info
1360 : * depending if we are reading cursor results or normal parameters.
1361 : * \tds
1362 : */
1363 : static TDSRET
1364 834 : tds_process_param_result_tokens(TDSSOCKET * tds)
1365 : {
1366 : int marker;
1367 : TDSPARAMINFO **pinfo;
1368 :
1369 834 : CHECK_TDS_EXTRA(tds);
1370 :
1371 834 : if (tds->cur_dyn)
1372 0 : pinfo = &(tds->cur_dyn->res_info);
1373 : else
1374 834 : pinfo = &(tds->param_info);
1375 :
1376 1924 : while ((marker = tds_get_byte(tds)) == TDS_PARAM_TOKEN) {
1377 1090 : TDS_PROPAGATE(tds_process_param_result(tds, pinfo));
1378 : }
1379 834 : if (!marker) {
1380 0 : tdsdump_log(TDS_DBG_FUNC, "error: tds_process_param_result() returned TDS_FAIL\n");
1381 : return TDS_FAIL;
1382 : }
1383 :
1384 834 : tds_set_current_results(tds, *pinfo);
1385 834 : tds_unget_byte(tds);
1386 834 : return TDS_SUCCESS;
1387 : }
1388 :
1389 : /**
1390 : * tds_process_params_result_token() processes params on TDS5.
1391 : * \tds
1392 : */
1393 : static TDSRET
1394 100 : tds_process_params_result_token(TDSSOCKET * tds)
1395 : {
1396 : unsigned int i;
1397 : TDSPARAMINFO *info;
1398 :
1399 100 : CHECK_TDS_EXTRA(tds);
1400 :
1401 : /* TODO check if current_results is a param result */
1402 100 : info = tds->current_results;
1403 100 : if (!info)
1404 : return TDS_FAIL;
1405 :
1406 158 : for (i = 0; i < info->num_cols; i++) {
1407 158 : TDSCOLUMN *curcol = info->columns[i];
1408 158 : TDS_PROPAGATE(curcol->funcs->get_data(tds, curcol));
1409 : }
1410 : return TDS_SUCCESS;
1411 : }
1412 :
1413 : /**
1414 : * tds_process_compute_result() processes compute result sets. These functions
1415 : * need work but since they get little use, nobody has complained!
1416 : * It is very similar to normal result sets.
1417 : * \tds
1418 : */
1419 : static TDSRET
1420 18 : tds_process_compute_result(TDSSOCKET * tds)
1421 : {
1422 : unsigned int col, num_cols;
1423 18 : TDS_TINYINT by_cols = 0;
1424 : TDS_SMALLINT *cur_by_col;
1425 18 : TDS_SMALLINT compute_id = 0;
1426 : TDSCOLUMN *curcol;
1427 18 : TDSCOMPUTEINFO *info = NULL;
1428 : unsigned int i;
1429 :
1430 18 : CHECK_TDS_EXTRA(tds);
1431 :
1432 18 : tds_get_smallint(tds); /* header size*/
1433 :
1434 : /*
1435 : * Compute statement id which this relates to.
1436 : * You can have more than one compute clause in a SQL statement
1437 : */
1438 :
1439 18 : compute_id = tds_get_smallint(tds);
1440 18 : num_cols = tds_get_byte(tds);
1441 :
1442 18 : tdsdump_log(TDS_DBG_INFO1, "tds_process_compute_result(): compute_id %d for %d columns\n", compute_id, num_cols);
1443 :
1444 24 : for (i=0; i < tds->num_comp_info; ++i) {
1445 24 : if (tds->comp_info[i]->computeid == compute_id) {
1446 : info = tds->comp_info[i];
1447 : break;
1448 : }
1449 : }
1450 18 : if (NULL == info) {
1451 0 : tdsdump_log(TDS_DBG_FUNC, "logic error: compute_id (%d) from server not found in tds->comp_info\n", compute_id);
1452 : return TDS_FAIL;
1453 : }
1454 :
1455 18 : tdsdump_log(TDS_DBG_FUNC, "found computeid %d in tds->comp_info\n", info->computeid);
1456 18 : tds_set_current_results(tds, info);
1457 :
1458 18 : tdsdump_log(TDS_DBG_INFO1, "processing compute result. num_cols = %d\n", num_cols);
1459 :
1460 18 : if (info->num_cols < num_cols)
1461 : return TDS_FAIL;
1462 :
1463 : /*
1464 : * Iterate over compute columns returned,
1465 : * e.g. COMPUTE SUM(x), AVG(x) would return num_cols = 2.
1466 : */
1467 18 : for (col = 0; col < num_cols; col++) {
1468 18 : tdsdump_log(TDS_DBG_INFO1, "processing compute column %d\n", col);
1469 18 : curcol = info->columns[col];
1470 :
1471 18 : curcol->column_operator = tds_get_byte(tds);
1472 18 : curcol->column_operand = tds_get_byte(tds);
1473 :
1474 : /* If no name has been defined for the compute column, use "max", "avg" etc. */
1475 36 : if (tds_dstr_isempty(&curcol->column_name))
1476 18 : if (!tds_dstr_copy(&curcol->column_name, tds_pr_op(curcol->column_operator)))
1477 : return TDS_FAIL;
1478 :
1479 : /* User defined data type of the column */
1480 18 : curcol->column_usertype = tds_get_int(tds);
1481 :
1482 36 : TDS_GET_COLUMN_TYPE(curcol);
1483 :
1484 18 : TDS_GET_COLUMN_INFO(tds, curcol);
1485 :
1486 18 : tdsdump_log(TDS_DBG_INFO1, "compute column_size is %d\n", curcol->column_size);
1487 :
1488 : /* Adjust column size according to client's encoding */
1489 18 : curcol->on_server.column_size = curcol->column_size;
1490 : /* TODO check if this column can have collation information associated */
1491 18 : adjust_character_column_size(tds, curcol);
1492 :
1493 : /* skip locale */
1494 18 : if (!IS_TDS42(tds->conn))
1495 18 : tds_get_n(tds, NULL, tds_get_byte(tds));
1496 : }
1497 :
1498 18 : by_cols = tds_get_byte(tds);
1499 :
1500 18 : tdsdump_log(TDS_DBG_INFO1, "processing tds compute result, by_cols = %d\n", by_cols);
1501 :
1502 18 : if (by_cols) {
1503 8 : if ((info->bycolumns = tds_new0(TDS_SMALLINT, by_cols)) == NULL)
1504 : return TDS_FAIL;
1505 : }
1506 18 : info->by_cols = by_cols;
1507 :
1508 18 : cur_by_col = info->bycolumns;
1509 26 : for (col = 0; col < by_cols; col++) {
1510 8 : *cur_by_col = tds_get_byte(tds);
1511 8 : cur_by_col++;
1512 : }
1513 :
1514 18 : return tds_alloc_compute_row(info);
1515 : }
1516 :
1517 : /**
1518 : * Reads data information from wire
1519 : * \tds
1520 : * \param curcol column where to store information
1521 : */
1522 : static TDSRET
1523 43955 : tds7_get_data_info(TDSSOCKET * tds, TDSCOLUMN * curcol)
1524 : {
1525 43955 : CHECK_TDS_EXTRA(tds);
1526 43955 : CHECK_COLUMN_EXTRA(curcol);
1527 :
1528 : /* User defined data type of the column */
1529 43955 : curcol->column_usertype = IS_TDS72_PLUS(tds->conn) ? tds_get_int(tds) : tds_get_smallint(tds);
1530 :
1531 43955 : curcol->column_flags = tds_get_smallint(tds); /* Flags */
1532 :
1533 43955 : curcol->column_nullable = curcol->column_flags & 0x01;
1534 43955 : curcol->column_writeable = (curcol->column_flags & 0x08) > 0;
1535 43955 : curcol->column_identity = (curcol->column_flags & 0x10) > 0;
1536 43955 : curcol->column_computed = (curcol->column_flags & 0x20) > 0;
1537 :
1538 87910 : TDS_GET_COLUMN_TYPE(curcol); /* sets "cardinal" type */
1539 :
1540 43955 : curcol->column_timestamp = (curcol->column_type == SYBBINARY && curcol->column_usertype == TDS_UT_TIMESTAMP);
1541 :
1542 43955 : TDS_GET_COLUMN_INFO(tds, curcol);
1543 :
1544 : /* Adjust column size according to client's encoding */
1545 43955 : curcol->on_server.column_size = curcol->column_size;
1546 :
1547 : /* NOTE adjustements must be done after curcol->char_conv initialization */
1548 43955 : adjust_character_column_size(tds, curcol);
1549 :
1550 : /*
1551 : * under 7.0 lengths are number of characters not
1552 : * number of bytes...tds_get_string handles this
1553 : */
1554 43955 : tds_dstr_get(tds, &curcol->column_name, tds_get_byte(tds));
1555 :
1556 43975 : tdsdump_log(TDS_DBG_INFO1, "tds7_get_data_info: \n"
1557 : "\tcolname = %s\n"
1558 : "\ttype = %d (%s)\n"
1559 : "\tserver's type = %d (%s)\n"
1560 : "\tcolumn_varint_size = %d\n"
1561 : "\tcolumn_size = %d (%d on server)\n",
1562 4 : tds_dstr_cstr(&curcol->column_name),
1563 4 : curcol->column_type, tds_prtype(curcol->column_type),
1564 4 : curcol->on_server.column_type, tds_prtype(curcol->on_server.column_type),
1565 4 : curcol->column_varint_size,
1566 : curcol->column_size, curcol->on_server.column_size);
1567 :
1568 43955 : CHECK_COLUMN_EXTRA(curcol);
1569 :
1570 43955 : return TDS_SUCCESS;
1571 : }
1572 :
1573 : /**
1574 : * tds7_process_result() is the TDS 7.0 result set processing routine. It
1575 : * is responsible for populating the tds->res_info structure.
1576 : * This is a TDS 7.0 only function
1577 : * \tds
1578 : */
1579 : static TDSRET
1580 25345 : tds7_process_result(TDSSOCKET * tds)
1581 : {
1582 : int col, num_cols;
1583 : TDSRET result;
1584 : TDSRESULTINFO *info;
1585 :
1586 25345 : CHECK_TDS_EXTRA(tds);
1587 25345 : tdsdump_log(TDS_DBG_INFO1, "processing TDS7 result metadata.\n");
1588 :
1589 : /* read number of columns and allocate the columns structure */
1590 :
1591 25345 : num_cols = tds_get_smallint(tds);
1592 :
1593 : /* This can be a DUMMY results token from a cursor fetch */
1594 :
1595 25345 : if (num_cols < 0) {
1596 824 : tdsdump_log(TDS_DBG_INFO1, "no meta data\n");
1597 : return TDS_SUCCESS;
1598 : }
1599 :
1600 24521 : tds_free_all_results(tds);
1601 24521 : tds->rows_affected = TDS_NO_COUNT;
1602 :
1603 24521 : if ((info = tds_alloc_results(num_cols)) == NULL)
1604 : return TDS_FAIL;
1605 24521 : tds_set_current_results(tds, info);
1606 24521 : if (tds->cur_cursor) {
1607 304 : tds_free_results(tds->cur_cursor->res_info);
1608 304 : tds->cur_cursor->res_info = info;
1609 304 : tdsdump_log(TDS_DBG_INFO1, "set current_results to cursor->res_info\n");
1610 : } else {
1611 24217 : tds->res_info = info;
1612 24217 : tdsdump_log(TDS_DBG_INFO1, "set current_results (%d column%s) to tds->res_info\n", num_cols, (num_cols==1? "":"s"));
1613 : }
1614 :
1615 : /*
1616 : * loop through the columns populating COLINFO struct from
1617 : * server response
1618 : */
1619 24521 : tdsdump_log(TDS_DBG_INFO1, "setting up %d columns\n", num_cols);
1620 43901 : for (col = 0; col < num_cols; col++) {
1621 43901 : TDSCOLUMN *curcol = info->columns[col];
1622 :
1623 43901 : TDS_PROPAGATE(tds7_get_data_info(tds, curcol));
1624 : }
1625 :
1626 24521 : if (num_cols > 0) {
1627 : static const char dashes[31] = "------------------------------";
1628 24521 : tdsdump_log(TDS_DBG_INFO1, " %-20s %-15s %-15s %-7s\n", "name", "size/wsize", "type/wtype", "utype");
1629 24521 : tdsdump_log(TDS_DBG_INFO1, " %-20s %15s %15s %7s\n", dashes+10, dashes+30-15, dashes+30-15, dashes+30-7);
1630 : }
1631 43901 : for (col = 0; col < num_cols; col++) {
1632 43901 : TDSCOLUMN *curcol = info->columns[col];
1633 :
1634 43913 : tdsdump_log(TDS_DBG_INFO1, " %-20s %7d/%-7d %7d/%-7d %7d\n",
1635 4 : tds_dstr_cstr(&curcol->column_name),
1636 : curcol->column_size, curcol->on_server.column_size,
1637 4 : curcol->column_type, curcol->on_server.column_type,
1638 : curcol->column_usertype);
1639 : }
1640 :
1641 : /* all done now allocate a row for tds_process_row to use */
1642 24521 : result = tds_alloc_row(info);
1643 24521 : CHECK_TDS_EXTRA(tds);
1644 24521 : return result;
1645 : }
1646 :
1647 : /**
1648 : * Reads data metadata from wire
1649 : * \param tds state information for the socket and the TDS protocol
1650 : * \param curcol column where to store information
1651 : * \param is_param true if metadata are for a parameter (false for normal
1652 : * column)
1653 : */
1654 : static TDSRET
1655 13300 : tds_get_data_info(TDSSOCKET * tds, TDSCOLUMN * curcol, int is_param)
1656 : {
1657 13300 : CHECK_TDS_EXTRA(tds);
1658 13300 : CHECK_COLUMN_EXTRA(curcol);
1659 :
1660 13300 : tdsdump_log(TDS_DBG_INFO1, "tds_get_data_info(%p, %p, %d) %s\n", tds, curcol, is_param, is_param? "[for parameter]" : "");
1661 :
1662 13300 : tds_dstr_get(tds, &curcol->column_name, tds_get_byte(tds));
1663 :
1664 13300 : curcol->column_flags = tds_get_byte(tds); /* Flags */
1665 13300 : if (!is_param) {
1666 : /* TODO check if all flags are the same for all TDS versions */
1667 696 : if (IS_TDS50(tds->conn))
1668 696 : curcol->column_hidden = curcol->column_flags & 0x1;
1669 696 : curcol->column_key = (curcol->column_flags & 0x2) > 1;
1670 696 : curcol->column_writeable = (curcol->column_flags & 0x10) > 1;
1671 696 : curcol->column_nullable = (curcol->column_flags & 0x20) > 1;
1672 696 : curcol->column_identity = (curcol->column_flags & 0x40) > 1;
1673 : #if 0
1674 : /****************************************
1675 : * NumParts=BYTE; (introduced in TDS 7.2)
1676 : * PartName=US_VARCHAR;(introduced in TDS 7.2)
1677 : * TableName=NumParts, {PartName}-;
1678 : * ColName= HYPERLINK \l "B_VARCHAR_Def" B_VARCHAR;
1679 : * ColumnData=UserType, Flags, [TableName], // <Only specified if text, //ntext or image columns are included //in the rowset being described> ColName;
1680 : * NoMetaData='0xFF', '0xFF';
1681 : */
1682 : enum column_flag_bits_according_to_microsoft {
1683 : case_sensitive = 0x0001
1684 : , nullable = 0x0002
1685 : , updateable = 0x0004
1686 : , might_be_updateable = 0x0008
1687 : , identity = 0x0010
1688 : , computed = 0x0020
1689 : , us_reserved_odbc = 0x0040 | 0x0080
1690 : , is_fixed_len_clr_type = 0x0100
1691 : , is_hidden_browse_pk = 0x0200
1692 : , is_browse_pk = 0x0400
1693 : , might_be_nullable = 0x0800
1694 : };
1695 : /* TODO: implement members in TDSCOLUMN */
1696 : if (IS_TDS72_PLUS(tds->conn)) {
1697 : curcol->is_computed = (curcol->column_flags & (1 << 4)) > 1;
1698 : curcol->us_reserved_odbc1 = (curcol->column_flags & (1 << 5)) > 1;
1699 : curcol->us_reserved_odbc2 = (curcol->column_flags & (1 << 6)) > 1;
1700 : curcol->is_fixed_len_clr_type = (curcol->column_flags & (1 << 7)) > 1;
1701 : }
1702 : #endif
1703 : }
1704 :
1705 13300 : if (IS_TDS72_PLUS(tds->conn)) {
1706 6344 : tds_get_n(tds, NULL, 2);
1707 : #if 0
1708 : /* TODO: implement members in TDSCOLUMN, values untested */
1709 : curcol->us_reserved1 = (curcol->column_flags & 0x01);
1710 : curcol->us_reserved2 = (curcol->column_flags & 0x02);
1711 : curcol->us_reserved3 = (curcol->column_flags & 0x04);
1712 : curcol->us_reserved4 = (curcol->column_flags & 0x08);
1713 : curcol->is_hidden = (curcol->column_flags & 0x10);
1714 : curcol->is_key = (curcol->column_flags & 0x20);
1715 : curcol->is_nullable_unknown = (curcol->column_flags & 0x40);
1716 : #endif
1717 : }
1718 :
1719 13300 : curcol->column_usertype = tds_get_int(tds);
1720 26600 : TDS_GET_COLUMN_TYPE(curcol);
1721 :
1722 13300 : tdsdump_log(TDS_DBG_INFO1, "processing result. type = %d(%s), varint_size %d\n",
1723 0 : curcol->column_type, tds_prtype(curcol->column_type), curcol->column_varint_size);
1724 :
1725 13300 : TDS_GET_COLUMN_INFO(tds, curcol);
1726 :
1727 13300 : tdsdump_log(TDS_DBG_INFO1, "processing result. column_size %d\n", curcol->column_size);
1728 :
1729 : /* Adjust column size according to client's encoding */
1730 13300 : curcol->on_server.column_size = curcol->column_size;
1731 13300 : adjust_character_column_size(tds, curcol);
1732 :
1733 13300 : return TDS_SUCCESS;
1734 : }
1735 :
1736 : /**
1737 : * tds5_process_result() is the TDS 5.0 result set processing routine.
1738 : * It is responsible for populating the tds->res_info structure.
1739 : * This is a TDS 5.0 only function
1740 : * \tds
1741 : */
1742 : static TDSRET
1743 58 : tds5_process_result(TDSSOCKET * tds)
1744 : {
1745 : unsigned int col, num_cols;
1746 : TDSCOLUMN *curcol;
1747 : TDSRESULTINFO *info;
1748 :
1749 58 : CHECK_TDS_EXTRA(tds);
1750 :
1751 58 : tds_free_all_results(tds);
1752 58 : tds->rows_affected = TDS_NO_COUNT;
1753 :
1754 58 : tds_get_usmallint(tds); /* header size */
1755 :
1756 : /* read number of columns and allocate the columns structure */
1757 58 : num_cols = tds_get_usmallint(tds);
1758 :
1759 58 : if ((info = tds_alloc_results(num_cols)) == NULL)
1760 : return TDS_FAIL;
1761 58 : tds_set_current_results(tds, info);
1762 58 : if (tds->cur_cursor)
1763 0 : tds->cur_cursor->res_info = info;
1764 : else
1765 58 : tds->res_info = info;
1766 :
1767 : /*
1768 : * loop through the columns populating COLINFO struct from
1769 : * server response
1770 : */
1771 696 : for (col = 0; col < info->num_cols; col++) {
1772 696 : curcol = info->columns[col];
1773 :
1774 696 : TDS_PROPAGATE(tds_get_data_info(tds, curcol, 0));
1775 :
1776 : /* skip locale information */
1777 : /* NOTE do not put into tds_get_data_info, param do not have locale information */
1778 696 : tds_get_n(tds, NULL, tds_get_byte(tds));
1779 : }
1780 58 : return tds_alloc_row(info);
1781 : }
1782 :
1783 : /**
1784 : * tds5_process_result2() is the new TDS 5.0 result set processing routine.
1785 : * It is responsible for populating the tds->res_info structure.
1786 : * This is a TDS 5.0 only function
1787 : * \tds
1788 : */
1789 : static TDSRET
1790 6816 : tds5_process_result2(TDSSOCKET * tds)
1791 : {
1792 : unsigned int colnamelen;
1793 : TDS_USMALLINT col, num_cols;
1794 : TDSCOLUMN *curcol;
1795 : TDSRESULTINFO *info;
1796 :
1797 6816 : CHECK_TDS_EXTRA(tds);
1798 :
1799 6816 : tdsdump_log(TDS_DBG_INFO1, "tds5_process_result2\n");
1800 :
1801 : /*
1802 : * free previous resultset
1803 : */
1804 6816 : tds_free_all_results(tds);
1805 6816 : tds->rows_affected = TDS_NO_COUNT;
1806 :
1807 : /*
1808 : * read length of packet (4 bytes)
1809 : */
1810 6816 : tds_get_uint(tds);
1811 :
1812 : /* read number of columns and allocate the columns structure */
1813 6816 : num_cols = tds_get_usmallint(tds);
1814 :
1815 6816 : if ((info = tds_alloc_results(num_cols)) == NULL)
1816 : return TDS_FAIL;
1817 6816 : tds_set_current_results(tds, info);
1818 6816 : if (tds->cur_cursor)
1819 8 : tds->cur_cursor->res_info = info;
1820 : else
1821 6808 : tds->res_info = info;
1822 :
1823 6816 : tdsdump_log(TDS_DBG_INFO1, "num_cols=%d\n", num_cols);
1824 :
1825 : /* TODO reuse some code... */
1826 : /*
1827 : * loop through the columns populating COLINFO struct from
1828 : * server response
1829 : */
1830 11518 : for (col = 0; col < info->num_cols; col++) {
1831 11518 : curcol = info->columns[col];
1832 :
1833 : /* label */
1834 11518 : tds_dstr_get(tds, &curcol->column_name, tds_get_byte(tds));
1835 :
1836 : /* TODO save information somewhere */
1837 : /* database */
1838 11518 : colnamelen = tds_get_byte(tds);
1839 11518 : tds_get_n(tds, NULL, colnamelen);
1840 : /*
1841 : * tds_get_n(tds, curcol->catalog_name, colnamelen);
1842 : * curcol->catalog_name[colnamelen] = '\0';
1843 : */
1844 :
1845 : /* owner */
1846 11518 : colnamelen = tds_get_byte(tds);
1847 11518 : tds_get_n(tds, NULL, colnamelen);
1848 : /*
1849 : * tds_get_n(tds, curcol->schema_name, colnamelen);
1850 : * curcol->schema_name[colnamelen] = '\0';
1851 : */
1852 :
1853 : /* table */
1854 : /* TODO use with owner and database */
1855 11518 : tds_dstr_get(tds, &curcol->table_name, tds_get_byte(tds));
1856 :
1857 : /* table column name */
1858 11518 : tds_dstr_get(tds, &curcol->table_column_name, tds_get_byte(tds));
1859 :
1860 : /* if label is empty, use the table column name */
1861 23036 : if (tds_dstr_isempty(&curcol->column_name))
1862 8496 : if (!tds_dstr_dup(&curcol->column_name, &curcol->table_column_name))
1863 : return TDS_FAIL;
1864 :
1865 : /* flags (4 bytes) */
1866 11518 : curcol->column_flags = tds_get_int(tds);
1867 11518 : curcol->column_hidden = curcol->column_flags & 0x1;
1868 11518 : curcol->column_key = (curcol->column_flags & 0x2) > 1;
1869 11518 : curcol->column_writeable = (curcol->column_flags & 0x10) > 1;
1870 11518 : curcol->column_nullable = (curcol->column_flags & 0x20) > 1;
1871 11518 : curcol->column_identity = (curcol->column_flags & 0x40) > 1;
1872 :
1873 11518 : curcol->column_usertype = tds_get_int(tds);
1874 :
1875 23036 : TDS_GET_COLUMN_TYPE(curcol);
1876 :
1877 11518 : TDS_GET_COLUMN_INFO(tds, curcol);
1878 :
1879 : /* Adjust column size according to client's encoding */
1880 11518 : curcol->on_server.column_size = curcol->column_size;
1881 11518 : adjust_character_column_size(tds, curcol);
1882 :
1883 : /* discard Locale */
1884 11518 : tds_get_n(tds, NULL, tds_get_byte(tds));
1885 :
1886 : /*
1887 : * Dump all information on this column
1888 : */
1889 11518 : tdsdump_log(TDS_DBG_INFO1, "col %d:\n", col);
1890 11518 : tdsdump_log(TDS_DBG_INFO1, "\tcolumn_name=[%s]\n", tds_dstr_cstr(&curcol->column_name));
1891 : /*
1892 : tdsdump_log(TDS_DBG_INFO1, "\tcolumn_name=[%s]\n", curcol->column_colname);
1893 : tdsdump_log(TDS_DBG_INFO1, "\tcatalog=[%s] schema=[%s] table=[%s]\n",
1894 : curcol->catalog_name, curcol->schema_name, curcol->table_name, curcol->column_colname);
1895 : */
1896 11518 : tdsdump_log(TDS_DBG_INFO1, "\tflags=%x utype=%d type=%d server type %d varint=%d\n",
1897 0 : curcol->column_flags, curcol->column_usertype, curcol->column_type, curcol->on_server.column_type,
1898 0 : curcol->column_varint_size);
1899 :
1900 11518 : tdsdump_log(TDS_DBG_INFO1, "\tcolsize=%d prec=%d scale=%d\n",
1901 0 : curcol->column_size, curcol->column_prec, curcol->column_scale);
1902 : }
1903 6816 : return tds_alloc_row(info);
1904 : }
1905 :
1906 : /**
1907 : * tds_process_compute() processes compute rows and places them in the row
1908 : * buffer.
1909 : * \tds
1910 : */
1911 : static TDSRET
1912 104 : tds_process_compute(TDSSOCKET * tds)
1913 : {
1914 : unsigned int i;
1915 : TDSCOLUMN *curcol;
1916 : TDSCOMPUTEINFO *info;
1917 : TDS_INT id;
1918 :
1919 104 : CHECK_TDS_EXTRA(tds);
1920 :
1921 104 : id = tds_get_smallint(tds);
1922 :
1923 104 : tdsdump_log(TDS_DBG_INFO1, "tds_process_compute() found compute id %d\n", id);
1924 :
1925 128 : for (i = 0;; ++i) {
1926 152 : if (i >= tds->num_comp_info) {
1927 0 : tdsdump_log(TDS_DBG_INFO1, "tds_process_compute() FAIL: id exceeds bound (%d)\n", tds->num_comp_info);
1928 : return TDS_FAIL;
1929 : }
1930 128 : info = tds->comp_info[i];
1931 128 : if (info->computeid == id)
1932 : break;
1933 : }
1934 104 : tds_set_current_results(tds, info);
1935 :
1936 208 : for (i = 0; i < info->num_cols; i++) {
1937 104 : curcol = info->columns[i];
1938 104 : if (TDS_FAILED(curcol->funcs->get_data(tds, curcol))) {
1939 0 : tdsdump_log(TDS_DBG_INFO1, "tds_process_compute() FAIL: get_data() failed\n");
1940 : return TDS_FAIL;
1941 : }
1942 : }
1943 : return TDS_SUCCESS;
1944 : }
1945 :
1946 : /**
1947 : * tds_process_row() processes rows and places them in the row buffer.
1948 : * \tds
1949 : */
1950 : static TDSRET
1951 360787 : tds_process_row(TDSSOCKET * tds)
1952 : {
1953 : unsigned int i;
1954 : TDSCOLUMN *curcol;
1955 : TDSRESULTINFO *info;
1956 :
1957 360787 : CHECK_TDS_EXTRA(tds);
1958 :
1959 360787 : info = tds->current_results;
1960 360787 : if (!info || info->num_cols <= 0)
1961 : return TDS_FAIL;
1962 :
1963 1162653 : for (i = 0; i < info->num_cols; i++) {
1964 1162653 : tdsdump_log(TDS_DBG_INFO1, "tds_process_row(): reading column %d \n", i);
1965 1162653 : curcol = info->columns[i];
1966 1162653 : TDS_PROPAGATE(curcol->funcs->get_data(tds, curcol));
1967 : }
1968 : return TDS_SUCCESS;
1969 : }
1970 :
1971 : /**
1972 : * tds_process_nbcrow() processes rows and places them in the row buffer.
1973 : */
1974 : static TDSRET
1975 4952 : tds_process_nbcrow(TDSSOCKET * tds)
1976 : {
1977 : unsigned int i;
1978 : TDSCOLUMN *curcol;
1979 : TDSRESULTINFO *info;
1980 : char *nbcbuf;
1981 :
1982 4952 : CHECK_TDS_EXTRA(tds);
1983 :
1984 4952 : info = tds->current_results;
1985 4952 : if (!info || info->num_cols <= 0)
1986 : return TDS_FAIL;
1987 :
1988 4952 : nbcbuf = (char *) alloca((info->num_cols + 7) / 8);
1989 4952 : tds_get_n(tds, nbcbuf, (info->num_cols + 7) / 8);
1990 41784 : for (i = 0; i < info->num_cols; i++) {
1991 36832 : curcol = info->columns[i];
1992 36832 : tdsdump_log(TDS_DBG_INFO1, "tds_process_nbcrow(): reading column %d \n", i);
1993 36832 : if (nbcbuf[i / 8] & (1 << (i % 8))) {
1994 9840 : curcol->column_cur_size = -1;
1995 : } else {
1996 26992 : TDS_PROPAGATE(curcol->funcs->get_data(tds, curcol));
1997 : }
1998 : }
1999 : return TDS_SUCCESS;
2000 : }
2001 :
2002 : static TDSRET
2003 716 : tds_process_featureextack(TDSSOCKET * tds)
2004 : {
2005 716 : CHECK_TDS_EXTRA(tds);
2006 :
2007 : /* TODO do something with it */
2008 716 : for (;;) {
2009 : TDS_UINT data_len;
2010 : TDS_TINYINT feature_id;
2011 :
2012 1432 : feature_id = tds_get_byte(tds);
2013 1432 : if (feature_id == 0xff)
2014 : break;
2015 :
2016 716 : data_len = tds_get_uint(tds);
2017 716 : tds_get_n(tds, NULL, data_len);
2018 : }
2019 716 : return TDS_SUCCESS;
2020 : }
2021 :
2022 : /**
2023 : * Attempt to close all deferred closes (dynamics and cursors).
2024 : * \tds
2025 : */
2026 : static void
2027 8 : tds_process_pending_closes(TDSSOCKET *tds)
2028 : {
2029 : TDSDYNAMIC *dyn, *next_dyn;
2030 : TDSCURSOR *cursor, *next_cursor;
2031 8 : int all_closed = 1;
2032 :
2033 : /* avoid recursions */
2034 8 : tds->conn->pending_close = 0;
2035 :
2036 : /* scan all cursors to close */
2037 8 : cursor = tds->conn->cursors;
2038 8 : if (cursor)
2039 0 : ++cursor->ref_count;
2040 0 : for (; cursor; cursor = next_cursor) {
2041 0 : next_cursor = cursor->next;
2042 0 : if (next_cursor)
2043 0 : ++next_cursor->ref_count;
2044 :
2045 0 : if (cursor->defer_close) {
2046 0 : cursor->status.dealloc = TDS_CURSOR_STATE_REQUESTED;
2047 0 : if (TDS_FAILED(tds_cursor_close(tds, cursor))
2048 0 : || TDS_FAILED(tds_process_simple_query(tds))) {
2049 : all_closed = 0;
2050 : } else {
2051 0 : cursor->defer_close = false;
2052 0 : tds_cursor_dealloc(tds, cursor);
2053 : }
2054 : }
2055 0 : tds_release_cursor(&cursor);
2056 : }
2057 :
2058 : /* scan all dynamic to close */
2059 8 : dyn = tds->conn->dyns;
2060 8 : if (dyn)
2061 8 : ++dyn->ref_count;
2062 8 : for (; dyn; dyn = next_dyn) {
2063 8 : next_dyn = dyn->next;
2064 8 : if (next_dyn)
2065 0 : ++next_dyn->ref_count;
2066 :
2067 8 : if (dyn->defer_close) {
2068 8 : if (TDS_FAILED(tds_submit_unprepare(tds, dyn))
2069 8 : || TDS_FAILED(tds_process_simple_query(tds))) {
2070 : all_closed = 0;
2071 : } else {
2072 8 : dyn->defer_close = false;
2073 : }
2074 : }
2075 8 : tds_release_dynamic(&dyn);
2076 : }
2077 :
2078 8 : if (!all_closed)
2079 0 : tds->conn->pending_close = 1;
2080 8 : }
2081 :
2082 : /**
2083 : * tds_process_end() processes any of the DONE, DONEPROC, or DONEINPROC
2084 : * tokens.
2085 : * \param tds state information for the socket and the TDS protocol
2086 : * \param marker TDS token number
2087 : * \param flags_parm filled with bit flags (see TDS_DONE_ constants).
2088 : * Is NULL nothing is returned
2089 : */
2090 : static TDSRET
2091 181873 : tds_process_end(TDSSOCKET * tds, int marker TDS_UNUSED, int *flags_parm)
2092 : {
2093 : bool more_results, was_cancelled, error, done_count_valid;
2094 : int status;
2095 : TDS_INT8 rows_affected;
2096 :
2097 181873 : CHECK_TDS_EXTRA(tds);
2098 :
2099 181873 : status = tds_get_usmallint(tds);
2100 :
2101 181873 : tds_get_smallint(tds); /* state/command */
2102 :
2103 181873 : more_results = (status & TDS_DONE_MORE_RESULTS) != 0;
2104 181873 : was_cancelled = (status & TDS_DONE_CANCELLED) != 0;
2105 181873 : error = (status & TDS_DONE_ERROR) != 0;
2106 181873 : done_count_valid = (status & TDS_DONE_COUNT) != 0;
2107 :
2108 :
2109 181873 : tdsdump_log(TDS_DBG_FUNC, "tds_process_end: more_results = %d\n"
2110 : "\t\twas_cancelled = %d\n"
2111 : "\t\terror = %d\n"
2112 : "\t\tdone_count_valid = %d\n", more_results, was_cancelled, error, done_count_valid);
2113 :
2114 181873 : tds->in_row = false;
2115 :
2116 181873 : if (tds->res_info) {
2117 38315 : tds->res_info->more_results = more_results;
2118 : /* FIXME this should not happen !!! */
2119 38315 : if (tds->current_results == NULL)
2120 0 : tds_set_current_results(tds, tds->res_info);
2121 :
2122 : }
2123 :
2124 181873 : if (flags_parm)
2125 157200 : *flags_parm = status;
2126 :
2127 181873 : rows_affected = IS_TDS72_PLUS(tds->conn) ? tds_get_int8(tds) : tds_get_int(tds);
2128 181873 : tdsdump_log(TDS_DBG_FUNC, " rows_affected = %" PRId64 "\n", rows_affected);
2129 :
2130 181873 : if (was_cancelled || (!more_results && !tds->in_cancel)) {
2131 79591 : tdsdump_log(TDS_DBG_FUNC, "tds_process_end() state set to TDS_IDLE\n");
2132 : /* reset of in_cancel should must done before setting IDLE */
2133 79591 : tds->in_cancel = 0;
2134 79591 : if (tds->bulk_query) {
2135 400 : tds->out_flag = TDS_BULK;
2136 400 : tds_set_state(tds, TDS_SENDING);
2137 400 : tds->bulk_query = false;
2138 : } else {
2139 79191 : tds_set_state(tds, TDS_IDLE);
2140 79191 : if (tds->conn->pending_close)
2141 8 : tds_process_pending_closes(tds);
2142 : }
2143 : }
2144 :
2145 181873 : if (IS_TDSDEAD(tds))
2146 : return TDS_FAIL;
2147 :
2148 : /*
2149 : * rows affected is in the tds struct because a query may affect rows but
2150 : * have no result set.
2151 : */
2152 :
2153 181873 : if (done_count_valid)
2154 51969 : tds->rows_affected = rows_affected;
2155 : else
2156 129904 : tds->rows_affected = TDS_NO_COUNT;
2157 :
2158 : if (IS_TDSDEAD(tds))
2159 : return TDS_FAIL;
2160 :
2161 181873 : return was_cancelled ? TDS_CANCELLED : TDS_SUCCESS;
2162 : }
2163 :
2164 : static TDSRET
2165 0 : tds_process_env_routing(TDSSOCKET * tds)
2166 : {
2167 0 : unsigned len = tds_get_usmallint(tds);
2168 0 : if (len) {
2169 : /* protocol (byte, 0 for ip)
2170 : * port (short, not 0)
2171 : * us_varchar
2172 : */
2173 : uint8_t protocol;
2174 : uint16_t port, address_len;
2175 0 : if (len < 5)
2176 : return TDS_FAIL;
2177 0 : protocol = tds_get_byte(tds);
2178 0 : port = tds_get_usmallint(tds);
2179 0 : address_len = tds_get_usmallint(tds);
2180 0 : len -= 5;
2181 0 : if (address_len * 2u < len)
2182 : return TDS_FAIL;
2183 0 : if (protocol == 0 && port != 0 && tds->login) {
2184 0 : tds->login->routing_port = port;
2185 0 : tds_dstr_get(tds, &tds->login->routing_address, address_len);
2186 0 : tds_get_n(tds, NULL, len - 2 * address_len);
2187 : } else {
2188 0 : tds_get_n(tds, NULL, len);
2189 : }
2190 : }
2191 0 : tds_get_n(tds, NULL, tds_get_usmallint(tds));
2192 0 : return TDS_SUCCESS;
2193 : }
2194 :
2195 : /**
2196 : * tds_process_env_chg()
2197 : * when ever certain things change on the server, such as database, character
2198 : * set, language, or block size. A environment change message is generated
2199 : * There is no action taken currently, but certain functions at the CLI level
2200 : * that return the name of the current database will need to use this.
2201 : * \tds
2202 : */
2203 : static TDSRET
2204 20467 : tds_process_env_chg(TDSSOCKET * tds)
2205 : {
2206 : unsigned int size;
2207 : TDS_TINYINT type;
2208 20467 : char *oldval = NULL;
2209 20467 : char *newval = NULL;
2210 : char **dest;
2211 : int new_block_size;
2212 20467 : int memrc = 0;
2213 :
2214 20467 : CHECK_TDS_EXTRA(tds);
2215 :
2216 20467 : size = tds_get_usmallint(tds);
2217 20467 : if (TDS_UNLIKELY(size < 1)) {
2218 0 : tdsdump_log(TDS_DBG_ERROR, "Got invalid size %u\n", size);
2219 0 : tds_close_socket(tds);
2220 0 : return TDS_FAIL;
2221 : }
2222 :
2223 : /*
2224 : * this came in a patch, apparently someone saw an env message
2225 : * that was different from what we are handling? -- brian
2226 : * changed back because it won't handle multibyte chars -- 7.0
2227 : */
2228 : /* tds_get_n(tds,NULL,size); */
2229 :
2230 20467 : type = tds_get_byte(tds);
2231 :
2232 : /*
2233 : * handle collate default change (if you change db or during login)
2234 : * this environment is not a string so need different handles
2235 : */
2236 20467 : if (type == TDS_ENV_SQLCOLLATION) {
2237 : /* save new collation */
2238 5538 : size = tds_get_byte(tds);
2239 5538 : tdsdump_log(TDS_DBG_ERROR, "tds_process_env_chg(): %d bytes of collation data received\n", size);
2240 5538 : tdsdump_dump_buf(TDS_DBG_NETWORK, "tds->conn->collation was", tds->conn->collation, 5);
2241 5538 : memset(tds->conn->collation, 0, 5);
2242 5538 : if (size < 5) {
2243 0 : tds_get_n(tds, tds->conn->collation, size);
2244 : } else {
2245 5538 : tds_get_n(tds, tds->conn->collation, 5);
2246 5538 : tds_get_n(tds, NULL, size - 5);
2247 5538 : tds7_srv_charset_changed(tds->conn, tds->conn->collation);
2248 : }
2249 5538 : tdsdump_dump_buf(TDS_DBG_NETWORK, "tds->conn->collation now", tds->conn->collation, 5);
2250 : /* discard old one */
2251 5538 : tds_get_n(tds, NULL, tds_get_byte(tds));
2252 5538 : return TDS_SUCCESS;
2253 : }
2254 :
2255 14929 : if (type == TDS_ENV_BEGINTRANS) {
2256 : /* TODO check size */
2257 344 : size = tds_get_byte(tds);
2258 344 : tds_get_n(tds, tds->conn->tds72_transaction, 8);
2259 344 : tds_get_n(tds, NULL, tds_get_byte(tds));
2260 344 : return TDS_SUCCESS;
2261 : }
2262 :
2263 14585 : if (type == TDS_ENV_COMMITTRANS || type == TDS_ENV_ROLLBACKTRANS) {
2264 310 : memset(tds->conn->tds72_transaction, 0, 8);
2265 310 : tds_get_n(tds, NULL, tds_get_byte(tds));
2266 310 : tds_get_n(tds, NULL, tds_get_byte(tds));
2267 310 : return TDS_SUCCESS;
2268 : }
2269 :
2270 14275 : if (IS_TDS71_PLUS(tds->conn) && type == TDS_ENV_ROUTING)
2271 0 : return tds_process_env_routing(tds);
2272 :
2273 : /* discard byte values, not still supported */
2274 : /* TODO support them */
2275 14275 : if (IS_TDS71_PLUS(tds->conn) && type > TDS_ENV_PACKSIZE) {
2276 : /* discard rest of the packet */
2277 0 : tds_get_n(tds, NULL, size - 1);
2278 0 : return TDS_SUCCESS;
2279 : }
2280 :
2281 : /* fetch the new value */
2282 14275 : memrc += tds_alloc_get_string(tds, &newval, tds_get_byte(tds));
2283 :
2284 : /* fetch the old value */
2285 14275 : memrc += tds_alloc_get_string(tds, &oldval, tds_get_byte(tds));
2286 :
2287 14275 : if (memrc != 0) {
2288 0 : free(newval);
2289 0 : free(oldval);
2290 0 : return TDS_FAIL;
2291 : }
2292 :
2293 14275 : dest = NULL;
2294 14275 : switch (type) {
2295 3648 : case TDS_ENV_PACKSIZE:
2296 7296 : new_block_size = atoi(newval);
2297 3648 : if (new_block_size >= 512) {
2298 3648 : tdsdump_log(TDS_DBG_INFO1, "changing block size from %s to %d\n", oldval, new_block_size);
2299 : /*
2300 : * Is possible to have a shrink if server limits packet
2301 : * size more than what we specified
2302 : */
2303 : /* Reallocate buffer if possible (strange values from server or out of memory) use older buffer */
2304 3648 : tds_realloc_socket(tds, new_block_size);
2305 : }
2306 : break;
2307 7153 : case TDS_ENV_DATABASE:
2308 7153 : dest = &tds->conn->env.database;
2309 7153 : break;
2310 2930 : case TDS_ENV_LANG:
2311 2930 : dest = &tds->conn->env.language;
2312 2930 : break;
2313 544 : case TDS_ENV_CHARSET:
2314 544 : tdsdump_log(TDS_DBG_FUNC, "server indicated charset change to \"%s\"\n", newval);
2315 544 : dest = &tds->conn->env.charset;
2316 544 : tds_srv_charset_changed(tds->conn, newval);
2317 544 : break;
2318 : }
2319 14275 : if (tds->env_chg_func) {
2320 7947 : (*(tds->env_chg_func)) (tds, type, oldval, newval);
2321 : }
2322 :
2323 14275 : free(oldval);
2324 14275 : if (newval) {
2325 14275 : if (dest) {
2326 10627 : free(*dest);
2327 10627 : *dest = newval;
2328 : } else
2329 3648 : free(newval);
2330 : }
2331 :
2332 : return TDS_SUCCESS;
2333 : }
2334 :
2335 : /**
2336 : * tds_process_info() is called for INFO, ERR, or EED tokens and is responsible
2337 : * for calling the CLI's message handling routine
2338 : * \returns TDS_SUCCESS if informational, TDS_FAIL if error.
2339 : */
2340 : static TDSRET
2341 15730 : tds_process_info(TDSSOCKET * tds, int marker)
2342 : {
2343 : int rc;
2344 : unsigned int len_sqlstate;
2345 15730 : int has_eed = 0;
2346 : TDSMESSAGE msg;
2347 15730 : unsigned int packet_len, char_len, readed_len = 10;
2348 :
2349 15730 : CHECK_TDS_EXTRA(tds);
2350 :
2351 15730 : if (!tds->in_row)
2352 15448 : tds_free_all_results(tds);
2353 :
2354 : /* make sure message has been freed */
2355 15730 : memset(&msg, 0, sizeof(TDSMESSAGE));
2356 :
2357 : /* packet length */
2358 15730 : packet_len = tds_get_usmallint(tds);
2359 :
2360 : /* message number */
2361 15730 : msg.msgno = tds_get_int(tds);
2362 :
2363 : /* msg state */
2364 15730 : msg.state = tds_get_byte(tds);
2365 :
2366 : /* msg level */
2367 15730 : msg.severity = tds_get_byte(tds);
2368 :
2369 : /* determine if msg or error */
2370 15730 : switch (marker) {
2371 2470 : case TDS_EED_TOKEN:
2372 2470 : if (msg.severity <= 10)
2373 2251 : msg.priv_msg_type = 0;
2374 : else
2375 219 : msg.priv_msg_type = 1;
2376 :
2377 : /* read SQL state */
2378 2470 : len_sqlstate = tds_get_byte(tds);
2379 2470 : msg.sql_state = tds_new(char, len_sqlstate + 1);
2380 2470 : if (!msg.sql_state) {
2381 0 : tds_free_msg(&msg);
2382 0 : return TDS_FAIL;
2383 : }
2384 :
2385 2470 : tds_get_n(tds, msg.sql_state, len_sqlstate);
2386 2470 : msg.sql_state[len_sqlstate] = '\0';
2387 :
2388 : /* do a better mapping using native errors */
2389 2470 : if (strcmp(msg.sql_state, "ZZZZZ") == 0)
2390 2369 : TDS_ZERO_FREE(msg.sql_state);
2391 :
2392 : /* if has_eed = 1, extended error data follows */
2393 2470 : has_eed = tds_get_byte(tds);
2394 :
2395 : /* junk status and transaction state */
2396 2470 : tds_get_smallint(tds);
2397 2470 : readed_len += 4 + len_sqlstate;
2398 2470 : break;
2399 11862 : case TDS_INFO_TOKEN:
2400 11862 : msg.priv_msg_type = 0;
2401 11862 : break;
2402 1398 : case TDS_ERROR_TOKEN:
2403 1398 : msg.priv_msg_type = 1;
2404 1398 : break;
2405 0 : default:
2406 0 : tdsdump_log(TDS_DBG_ERROR, "tds_process_info() called with unknown marker '%d'!\n", (int) marker);
2407 0 : tds_free_msg(&msg);
2408 0 : return TDS_FAIL;
2409 : }
2410 :
2411 15730 : tdsdump_log(TDS_DBG_ERROR, "tds_process_info() reading message %d from server\n", msg.msgno);
2412 :
2413 : #define GET_STRING(dest, len_type) do { \
2414 : unsigned int to_read_size = tds_get_ ## len_type(tds); \
2415 : char_len += to_read_size; \
2416 : rc += tds_alloc_get_string(tds, dest, to_read_size); \
2417 : } while(0)
2418 :
2419 15730 : char_len = 0;
2420 15730 : rc = 0;
2421 :
2422 : /* the message */
2423 15730 : GET_STRING(&msg.message, usmallint);
2424 :
2425 : /* server name */
2426 15730 : GET_STRING(&msg.server, byte);
2427 :
2428 15730 : if ((!msg.server || !msg.server[0]) && tds->login) {
2429 2 : TDS_ZERO_FREE(msg.server);
2430 4 : if (-1 == asprintf(&msg.server, "[%s]", tds_dstr_cstr(&tds->login->server_name))) {
2431 0 : tdsdump_log(TDS_DBG_ERROR, "out of memory (%d), %s\n", errno, strerror(errno));
2432 : return TDS_FAIL;
2433 : }
2434 : }
2435 :
2436 : /* stored proc name if available */
2437 15730 : GET_STRING(&msg.proc_name, byte);
2438 :
2439 15730 : readed_len += char_len * (IS_TDS7_PLUS(tds->conn) ? 2 : 1);
2440 :
2441 : /* line number in the sql statement where the problem occured */
2442 : /* login still not done, we don't know exactly the version */
2443 22860 : if (tds->conn->product_version == 0 ?
2444 7130 : IS_TDS7_PLUS(tds->conn) && readed_len + 4 <= packet_len :
2445 : IS_TDS72_PLUS(tds->conn)) {
2446 6726 : msg.line_number = tds_get_int(tds);
2447 6726 : readed_len += 4;
2448 : } else {
2449 9004 : msg.line_number = tds_get_smallint(tds);
2450 9004 : readed_len += 2;
2451 : }
2452 : /* discard additional bytes */
2453 15730 : if (packet_len > readed_len)
2454 0 : tds_get_n(tds, NULL, packet_len - readed_len);
2455 : #undef GET_STRING
2456 :
2457 : /*
2458 : * If the server doesn't provide an sqlstate, map one via server native errors
2459 : * I'm assuming there is not a protocol I'm missing to fetch these from the server?
2460 : * I know sybase has an sqlstate column in it's sysmessages table, mssql doesn't and
2461 : * TDS_EED_TOKEN is not being called for me.
2462 : */
2463 15730 : if (msg.sql_state == NULL)
2464 15629 : msg.sql_state = tds_alloc_lookup_sqlstate(tds, msg.msgno);
2465 :
2466 :
2467 : /* In case extended error data is sent, we just try to discard it */
2468 15730 : if (has_eed == 1) {
2469 : int next_marker;
2470 : for (;;) {
2471 10 : switch (next_marker = tds_get_byte(tds)) {
2472 4 : case TDS5_PARAMFMT_TOKEN:
2473 : case TDS5_PARAMFMT2_TOKEN:
2474 : case TDS5_PARAMS_TOKEN:
2475 4 : if (TDS_FAILED(tds_process_default_tokens(tds, next_marker)))
2476 0 : --rc;
2477 4 : continue;
2478 : }
2479 : break;
2480 : }
2481 2 : tds_unget_byte(tds);
2482 : }
2483 :
2484 : /*
2485 : * call the msg_handler that was set by an upper layer
2486 : * (dblib, ctlib or some other one). Call it with the pointer to
2487 : * the "parent" structure.
2488 : */
2489 :
2490 15730 : if (rc != 0) {
2491 0 : tds_free_msg(&msg);
2492 0 : return TDS_FAIL;
2493 : }
2494 :
2495 : /* special case, */
2496 15730 : if (marker == TDS_EED_TOKEN && tds->cur_dyn && !TDS_IS_MSSQL(tds) && msg.msgno == 2782) {
2497 : /* we must emulate prepare */
2498 60 : tds->cur_dyn->emulated = true;
2499 60 : tds_dynamic_deallocated(tds->conn, tds->cur_dyn);
2500 15670 : } else if (marker == TDS_INFO_TOKEN && msg.msgno == 16954 && TDS_IS_MSSQL(tds)
2501 2058 : && tds->current_op == TDS_OP_CURSOROPEN && tds->cur_cursor) {
2502 : /* here mssql say "Executing SQL directly; no cursor." opening cursor */
2503 : } else {
2504 :
2505 13612 : if (tds_get_ctx(tds)->msg_handler) {
2506 13206 : tdsdump_log(TDS_DBG_ERROR, "tds_process_info() calling client msg handler\n");
2507 13206 : tds_get_ctx(tds)->msg_handler(tds_get_ctx(tds), tds, &msg);
2508 406 : } else if (msg.msgno) {
2509 406 : tdsdump_log(TDS_DBG_WARN,
2510 : "Msg %d, Severity %d, State %d, Server %s, Line %d\n%s\n",
2511 : msg.msgno,
2512 0 : msg.severity ,
2513 0 : msg.state, msg.server, msg.line_number, msg.message);
2514 : }
2515 : }
2516 :
2517 15730 : if (!tds->conn->server) {
2518 3658 : tds->conn->server = msg.server;
2519 3658 : msg.server = NULL;
2520 : }
2521 15730 : tds_free_msg(&msg);
2522 :
2523 15730 : tdsdump_log(TDS_DBG_ERROR, "tds_process_info() returning TDS_SUCCESS\n");
2524 :
2525 : return TDS_SUCCESS;
2526 : }
2527 :
2528 : /**
2529 : * Reads a string from wire in a new allocated buffer
2530 : * \tds
2531 : * \param string output string
2532 : * \param len length of string to read
2533 : * \returns 0 for success, -1 on error.
2534 : */
2535 : static int
2536 79728 : tds_alloc_get_string(TDSSOCKET * tds, char **string, size_t len)
2537 : {
2538 : char *s;
2539 : size_t out_len;
2540 :
2541 79728 : CHECK_TDS_EXTRA(tds);
2542 :
2543 : /* assure sufficient space for every conversion */
2544 79728 : s = tds_new(char, len * 4 + 1);
2545 79728 : out_len = tds_get_string(tds, len, s, len * 4);
2546 79728 : if (!s) {
2547 0 : *string = NULL;
2548 0 : return -1;
2549 : }
2550 79728 : s = (char*) realloc(s, out_len + 1);
2551 79728 : s[out_len] = '\0';
2552 79728 : *string = s;
2553 79728 : return 0;
2554 : }
2555 :
2556 : /**
2557 : * \remarks Process the incoming token stream until it finds
2558 : * an end token (DONE, DONEPROC, DONEINPROC) with the cancel flag set.
2559 : * At that point the connection should be ready to handle a new query.
2560 : * \tds
2561 : */
2562 : TDSRET
2563 11645 : tds_process_cancel(TDSSOCKET * tds)
2564 : {
2565 11645 : CHECK_TDS_EXTRA(tds);
2566 :
2567 : /* silly cases, nothing to do */
2568 11645 : if (!tds->in_cancel)
2569 : return TDS_SUCCESS;
2570 : /* TODO handle cancellation sending data */
2571 1559 : if (tds->state != TDS_PENDING)
2572 : return TDS_SUCCESS;
2573 :
2574 : /* TODO support TDS5 cancel, wait for cancel packet first, then wait for done */
2575 0 : for (;;) {
2576 : TDS_INT result_type;
2577 :
2578 1543 : switch (tds_process_tokens(tds, &result_type, NULL, 0)) {
2579 : case TDS_FAIL:
2580 1543 : return TDS_FAIL;
2581 1543 : case TDS_CANCELLED:
2582 : case TDS_SUCCESS:
2583 : case TDS_NO_MORE_RESULTS:
2584 1543 : return TDS_SUCCESS;
2585 : }
2586 : }
2587 : }
2588 :
2589 : /**
2590 : * Finds a dynamic given string id
2591 : * \return dynamic or NULL is not found
2592 : * \param conn state information for the socket and the TDS protocol
2593 : * \param id dynamic id to search
2594 : */
2595 : TDSDYNAMIC *
2596 657860 : tds_lookup_dynamic(TDSCONNECTION * conn, const char *id)
2597 : {
2598 : TDSDYNAMIC *curr;
2599 :
2600 : CHECK_CONN_EXTRA(conn);
2601 :
2602 1314296 : for (curr = conn->dyns; curr != NULL; curr = curr->next) {
2603 656446 : if (!strcmp(curr->id, id))
2604 : return curr;
2605 : }
2606 : return NULL;
2607 : }
2608 :
2609 : /**
2610 : * tds_process_dynamic()
2611 : * finds the element of the dyns array for the id
2612 : * \tds
2613 : * \return allocated dynamic or NULL on failure.
2614 : */
2615 : static TDSDYNAMIC *
2616 984 : tds_process_dynamic(TDSSOCKET * tds)
2617 : {
2618 : unsigned int token_sz;
2619 : unsigned char type;
2620 984 : TDS_TINYINT id_len, drain = 0;
2621 : char id[TDS_MAX_DYNID_LEN + 1];
2622 :
2623 984 : CHECK_TDS_EXTRA(tds);
2624 :
2625 984 : token_sz = tds_get_usmallint(tds);
2626 984 : type = tds_get_byte(tds);
2627 984 : tds_get_byte(tds); /* status */
2628 : /* handle only acknowledge */
2629 984 : if (type != TDS_DYN_ACK) {
2630 0 : tdsdump_log(TDS_DBG_ERROR, "Unrecognized TDS5_DYN type %x\n", type);
2631 0 : tds_get_n(tds, NULL, token_sz - 2);
2632 0 : return NULL;
2633 : }
2634 984 : id_len = tds_get_byte(tds);
2635 984 : if (id_len > TDS_MAX_DYNID_LEN) {
2636 0 : drain = id_len - TDS_MAX_DYNID_LEN;
2637 0 : id_len = TDS_MAX_DYNID_LEN;
2638 : }
2639 984 : id_len = tds_get_string(tds, id_len, id, TDS_MAX_DYNID_LEN);
2640 984 : id[id_len] = '\0';
2641 984 : if (drain) {
2642 0 : tds_get_n(tds, NULL, drain);
2643 : }
2644 984 : return tds_lookup_dynamic(tds->conn, id);
2645 : }
2646 :
2647 : /**
2648 : * Process results from dynamic.
2649 : * \tds
2650 : */
2651 : static TDSRET
2652 20 : tds_process_dyn_result(TDSSOCKET * tds)
2653 : {
2654 : unsigned int col, num_cols;
2655 : TDSCOLUMN *curcol;
2656 : TDSPARAMINFO *info;
2657 : TDSDYNAMIC *dyn;
2658 :
2659 20 : CHECK_TDS_EXTRA(tds);
2660 :
2661 20 : tds_get_usmallint(tds); /* header size */
2662 20 : num_cols = tds_get_usmallint(tds);
2663 :
2664 : /* read number of columns and allocate the columns structure */
2665 20 : if ((info = tds_alloc_results(num_cols)) == NULL)
2666 : return TDS_FAIL;
2667 20 : if (tds->cur_dyn) {
2668 0 : dyn = tds->cur_dyn;
2669 0 : tds_free_param_results(dyn->res_info);
2670 0 : dyn->res_info = info;
2671 : } else {
2672 20 : tds_free_param_results(tds->param_info);
2673 20 : tds->param_info = info;
2674 : }
2675 20 : tds_set_current_results(tds, info);
2676 :
2677 48 : for (col = 0; col < info->num_cols; col++) {
2678 28 : curcol = info->columns[col];
2679 :
2680 28 : TDS_PROPAGATE(tds_get_data_info(tds, curcol, 1));
2681 :
2682 : /* skip locale information */
2683 28 : tds_get_n(tds, NULL, tds_get_byte(tds));
2684 : }
2685 :
2686 20 : return tds_alloc_row(info);
2687 : }
2688 :
2689 : /**
2690 : * Process new TDS 5.0 token for describing output parameters
2691 : * \tds
2692 : */
2693 : static TDSRET
2694 336 : tds5_process_dyn_result2(TDSSOCKET * tds)
2695 : {
2696 : unsigned int col, num_cols;
2697 : TDSCOLUMN *curcol;
2698 : TDSPARAMINFO *info;
2699 :
2700 336 : CHECK_TDS_EXTRA(tds);
2701 :
2702 336 : tds_get_uint(tds); /* header size */
2703 336 : num_cols = tds_get_usmallint(tds);
2704 :
2705 : /* read number of columns and allocate the columns structure */
2706 336 : if ((info = tds_alloc_results(num_cols)) == NULL)
2707 : return TDS_FAIL;
2708 336 : if (tds->cur_dyn) {
2709 256 : TDSDYNAMIC *dyn = tds->cur_dyn;
2710 256 : tds_free_param_results(dyn->res_info);
2711 256 : dyn->res_info = info;
2712 : } else {
2713 80 : tds_free_param_results(tds->param_info);
2714 80 : tds->param_info = info;
2715 : }
2716 336 : tds_set_current_results(tds, info);
2717 :
2718 774 : for (col = 0; col < info->num_cols; col++) {
2719 438 : curcol = info->columns[col];
2720 :
2721 : /* TODO reuse tds_get_data_info code, sligthly different */
2722 :
2723 : /* column name */
2724 438 : tds_dstr_get(tds, &curcol->column_name, tds_get_byte(tds));
2725 :
2726 : /* column status */
2727 438 : curcol->column_flags = tds_get_int(tds);
2728 438 : curcol->column_nullable = (curcol->column_flags & 0x20) > 0;
2729 :
2730 : /* user type */
2731 438 : curcol->column_usertype = tds_get_int(tds);
2732 :
2733 : /* column type */
2734 876 : TDS_GET_COLUMN_TYPE(curcol);
2735 :
2736 438 : TDS_GET_COLUMN_INFO(tds, curcol);
2737 :
2738 : /* Adjust column size according to client's encoding */
2739 438 : curcol->on_server.column_size = curcol->column_size;
2740 438 : adjust_character_column_size(tds, curcol);
2741 :
2742 : /* discard Locale */
2743 438 : tds_get_n(tds, NULL, tds_get_byte(tds));
2744 :
2745 438 : tdsdump_log(TDS_DBG_INFO1, "elem %d:\n", col);
2746 438 : tdsdump_log(TDS_DBG_INFO1, "\tcolumn_name=[%s]\n", tds_dstr_cstr(&curcol->column_name));
2747 438 : tdsdump_log(TDS_DBG_INFO1, "\tflags=%x utype=%d type=%d server type %d varint=%d\n",
2748 0 : curcol->column_flags, curcol->column_usertype, curcol->column_type, curcol->on_server.column_type,
2749 0 : curcol->column_varint_size);
2750 438 : tdsdump_log(TDS_DBG_INFO1, "\tcolsize=%d prec=%d scale=%d\n",
2751 0 : curcol->column_size, curcol->column_prec, curcol->column_scale);
2752 : }
2753 :
2754 336 : return tds_alloc_row(info);
2755 : }
2756 :
2757 : /**
2758 : * tds_process_compute_names() processes compute result sets.
2759 : * \tds
2760 : */
2761 : static TDSRET
2762 18 : tds_process_compute_names(TDSSOCKET * tds)
2763 : {
2764 : int hdrsize;
2765 18 : int num_cols = 0;
2766 18 : TDS_USMALLINT compute_id = 0;
2767 : TDSCOMPUTEINFO *info;
2768 : int col;
2769 :
2770 18 : struct namelist *head = NULL, *cur;
2771 :
2772 18 : CHECK_TDS_EXTRA(tds);
2773 :
2774 18 : hdrsize = tds_get_usmallint(tds);
2775 18 : tdsdump_log(TDS_DBG_INFO1, "processing tds5 compute names. hdrsize = %d\n", hdrsize);
2776 :
2777 : /*
2778 : * compute statement id which this relates
2779 : * to. You can have more than one compute
2780 : * statement in a SQL statement
2781 : */
2782 18 : compute_id = tds_get_usmallint(tds);
2783 :
2784 18 : if ((num_cols = tds_read_namelist(tds, hdrsize - 2, &head, 0)) <= 0)
2785 : return TDS_FAIL;
2786 :
2787 18 : tdsdump_log(TDS_DBG_INFO1, "processing tds5 compute names. num_cols = %d\n", num_cols);
2788 :
2789 18 : if ((tds->comp_info = tds_alloc_compute_results(tds, num_cols, 0)) == NULL)
2790 : goto memory_error;
2791 :
2792 18 : tdsdump_log(TDS_DBG_INFO1, "processing tds5 compute names. num_comp_info = %d\n", tds->num_comp_info);
2793 :
2794 18 : info = tds->comp_info[tds->num_comp_info - 1];
2795 18 : tds_set_current_results(tds, info);
2796 :
2797 18 : info->computeid = compute_id;
2798 :
2799 18 : cur = head;
2800 36 : for (col = 0; col < num_cols; col++) {
2801 18 : TDSCOLUMN *curcol = info->columns[col];
2802 :
2803 18 : if (!tds_dstr_copy(&curcol->column_name, cur->name))
2804 : goto memory_error;
2805 :
2806 18 : cur = cur->next;
2807 : }
2808 18 : tds_free_namelist(head);
2809 18 : return TDS_SUCCESS;
2810 :
2811 0 : memory_error:
2812 0 : tds_free_namelist(head);
2813 0 : return TDS_FAIL;
2814 : }
2815 :
2816 : /**
2817 : * tds7_process_compute_result() processes compute result sets for TDS 7/8.
2818 : * They is are very similar to normal result sets.
2819 : * \tds
2820 : */
2821 : static TDSRET
2822 54 : tds7_process_compute_result(TDSSOCKET * tds)
2823 : {
2824 : unsigned int col, num_cols;
2825 : TDS_TINYINT by_cols;
2826 : TDS_SMALLINT *cur_by_col;
2827 : TDS_USMALLINT compute_id;
2828 : TDSCOLUMN *curcol;
2829 : TDSCOMPUTEINFO *info;
2830 :
2831 54 : CHECK_TDS_EXTRA(tds);
2832 :
2833 : /* compute without result should never happens */
2834 54 : if (!tds->res_info)
2835 : return TDS_FAIL;
2836 :
2837 : /*
2838 : * number of compute columns returned - so
2839 : * COMPUTE SUM(x), AVG(x)... would return
2840 : * num_cols = 2
2841 : */
2842 :
2843 54 : num_cols = tds_get_usmallint(tds);
2844 :
2845 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. num_cols = %u\n", num_cols);
2846 :
2847 : /*
2848 : * compute statement id which this relates
2849 : * to. You can have more than one compute
2850 : * statement in a SQL statement
2851 : */
2852 :
2853 54 : compute_id = tds_get_usmallint(tds);
2854 :
2855 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. compute_id = %u\n", compute_id);
2856 : /*
2857 : * number of "by" columns in compute - so
2858 : * COMPUTE SUM(x) BY a, b, c would return
2859 : * by_cols = 3
2860 : */
2861 :
2862 54 : by_cols = tds_get_byte(tds);
2863 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. by_cols = %d\n", by_cols);
2864 :
2865 54 : if ((tds->comp_info = tds_alloc_compute_results(tds, num_cols, by_cols)) == NULL)
2866 : return TDS_FAIL;
2867 :
2868 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. num_comp_info = %d\n", tds->num_comp_info);
2869 :
2870 54 : info = tds->comp_info[tds->num_comp_info - 1];
2871 54 : tds_set_current_results(tds, info);
2872 :
2873 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. point 0\n");
2874 :
2875 54 : info->computeid = compute_id;
2876 :
2877 : /*
2878 : * the by columns are a list of the column
2879 : * numbers in the select statement
2880 : */
2881 :
2882 54 : cur_by_col = info->bycolumns;
2883 78 : for (col = 0; col < by_cols; col++) {
2884 24 : *cur_by_col = tds_get_smallint(tds);
2885 24 : cur_by_col++;
2886 : }
2887 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. point 1\n");
2888 :
2889 54 : for (col = 0; col < num_cols; col++) {
2890 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. point 2\n");
2891 54 : curcol = info->columns[col];
2892 :
2893 54 : curcol->column_operator = tds_get_byte(tds);
2894 54 : curcol->column_operand = tds_get_smallint(tds);
2895 :
2896 54 : TDS_PROPAGATE(tds7_get_data_info(tds, curcol));
2897 :
2898 108 : if (tds_dstr_isempty(&curcol->column_name))
2899 54 : if (!tds_dstr_copy(&curcol->column_name, tds_pr_op(curcol->column_operator)))
2900 : return TDS_FAIL;
2901 : }
2902 :
2903 : /* all done now allocate a row for tds_process_row to use */
2904 54 : tdsdump_log(TDS_DBG_INFO1, "processing tds7 compute result. point 5 \n");
2905 54 : return tds_alloc_compute_row(info);
2906 : }
2907 :
2908 : /**
2909 : * Reads cursor command results.
2910 : * This contains status of cursors.
2911 : * \tds
2912 : */
2913 : static TDSRET
2914 40 : tds_process_cursor_tokens(TDSSOCKET * tds)
2915 : {
2916 : TDS_USMALLINT hdrsize;
2917 : TDS_INT cursor_id;
2918 : TDS_TINYINT namelen;
2919 : TDS_USMALLINT cursor_status;
2920 : TDSCURSOR *cursor;
2921 :
2922 40 : CHECK_TDS_EXTRA(tds);
2923 :
2924 40 : hdrsize = tds_get_usmallint(tds);
2925 40 : cursor_id = tds_get_int(tds);
2926 40 : hdrsize -= sizeof(TDS_INT);
2927 40 : if (cursor_id == 0){
2928 0 : namelen = tds_get_byte(tds);
2929 0 : hdrsize -= 1;
2930 : /* discard name */
2931 0 : tds_get_n(tds, NULL, namelen);
2932 0 : hdrsize -= namelen;
2933 : }
2934 40 : tds_get_byte(tds); /* cursor command */
2935 40 : cursor_status = tds_get_usmallint(tds);
2936 40 : hdrsize -= 3;
2937 :
2938 40 : if (hdrsize == sizeof(TDS_INT))
2939 8 : tds_get_int(tds); /* row count TODO useless ?? */
2940 :
2941 40 : if (tds->cur_cursor) {
2942 38 : cursor = tds->cur_cursor;
2943 38 : cursor->cursor_id = cursor_id;
2944 38 : cursor->srv_status = cursor_status;
2945 38 : if ((cursor_status & TDS_CUR_ISTAT_DEALLOC) != 0)
2946 8 : tds_cursor_deallocated(tds->conn, cursor);
2947 : }
2948 40 : return TDS_SUCCESS;
2949 : }
2950 :
2951 : /**
2952 : * Process option cmd results.
2953 : * This token is available only on TDS 5.0 (Sybase).
2954 : * \tds
2955 : */
2956 : static TDSRET
2957 8 : tds5_process_optioncmd(TDSSOCKET * tds)
2958 : {
2959 : TDS_INT command;
2960 : TDS_TINYINT option;
2961 : TDS_TINYINT argsize;
2962 : TDS_INT arg;
2963 :
2964 8 : CHECK_TDS_EXTRA(tds);
2965 :
2966 8 : tdsdump_log(TDS_DBG_INFO1, "tds5_process_optioncmd()\n");
2967 :
2968 8 : if (!IS_TDS50(tds->conn))
2969 : return TDS_FAIL;
2970 :
2971 8 : tds_get_usmallint(tds); /* length */
2972 8 : command = tds_get_byte(tds);
2973 8 : option = tds_get_byte(tds);
2974 8 : argsize = tds_get_byte(tds);
2975 :
2976 8 : switch (argsize) {
2977 : case 0:
2978 : arg = 0;
2979 : break;
2980 8 : case 1:
2981 8 : arg = tds_get_byte(tds);
2982 8 : break;
2983 0 : case 4:
2984 0 : arg = tds_get_int(tds);
2985 0 : break;
2986 0 : default:
2987 0 : tdsdump_log(TDS_DBG_INFO1, "oops: cannot process option %d of size %d\n", option, argsize);
2988 : /* ignore argument */
2989 0 : tds_get_n(tds, NULL, argsize);
2990 0 : return TDS_FAIL;
2991 : }
2992 8 : tdsdump_log(TDS_DBG_INFO1, "received option %d value %d\n", option, arg);
2993 :
2994 8 : if (command != TDS_OPT_INFO)
2995 : return TDS_FAIL;
2996 :
2997 8 : tds->option_value = arg;
2998 :
2999 8 : return TDS_SUCCESS;
3000 : }
3001 :
3002 : /**
3003 : * Returns string representation for a given operation
3004 : * \param op operation code
3005 : * \return string representation. Empty if not found.
3006 : */
3007 : static const char *
3008 72 : tds_pr_op(int op)
3009 : {
3010 : /** \cond HIDDEN_SYMBOLS */
3011 : #define TYPE(con, s) case con: return s; break
3012 : /** \endcond */
3013 72 : switch (op) {
3014 : TYPE(SYBAOPAVG, "avg");
3015 : TYPE(SYBAOPAVGU, "avg");
3016 0 : TYPE(SYBAOPCNT, "count");
3017 0 : TYPE(SYBAOPCNTU, "count");
3018 32 : TYPE(SYBAOPMAX, "max");
3019 8 : TYPE(SYBAOPMIN, "min");
3020 32 : TYPE(SYBAOPSUM, "sum");
3021 0 : TYPE(SYBAOPSUMU, "sum");
3022 0 : TYPE(SYBAOPCHECKSUM_AGG, "checksum_agg");
3023 0 : TYPE(SYBAOPCNT_BIG, "count");
3024 0 : TYPE(SYBAOPSTDEV, "stdevp");
3025 0 : TYPE(SYBAOPSTDEVP, "stdevp");
3026 0 : TYPE(SYBAOPVAR, "var");
3027 0 : TYPE(SYBAOPVARP, "varp");
3028 : default:
3029 : break;
3030 : }
3031 0 : return "";
3032 : #undef TYPE
3033 : }
3034 :
3035 : /**
3036 : * Returns string representation of the given type.
3037 : * \param type data type
3038 : * \return type as string. Empty if not found.
3039 : */
3040 : const char *
3041 47262 : tds_prtype(int type)
3042 : {
3043 : /** \cond HIDDEN_SYMBOLS */
3044 : #define TYPE(con, s) case con: return s; break
3045 : /** \endcond */
3046 47262 : switch (type) {
3047 : TYPE(SYBAOPAVG, "avg");
3048 0 : TYPE(SYBAOPCNT, "count");
3049 0 : TYPE(SYBAOPMAX, "max");
3050 0 : TYPE(SYBAOPMIN, "min");
3051 0 : TYPE(SYBAOPSUM, "sum");
3052 :
3053 1250 : TYPE(SYBBINARY, "binary");
3054 1250 : TYPE(SYBLONGBINARY, "longbinary");
3055 990 : TYPE(SYBBIT, "bit");
3056 990 : TYPE(SYBBITN, "bit-null");
3057 3710 : TYPE(SYBCHAR, "char");
3058 770 : TYPE(SYBDATETIME4, "smalldatetime");
3059 770 : TYPE(SYBDATETIME, "datetime");
3060 0 : TYPE(SYBDATETIMN, "datetime-null");
3061 990 : TYPE(SYBDECIMAL, "decimal");
3062 1380 : TYPE(SYBFLT8, "float");
3063 0 : TYPE(SYBFLTN, "float-null");
3064 1262 : TYPE(SYBIMAGE, "image");
3065 1310 : TYPE(SYBSINT1, "signed tinyint");
3066 1400 : TYPE(SYBINT1, "tinyint");
3067 1380 : TYPE(SYBINT2, "smallint");
3068 1392 : TYPE(SYBINT4, "int");
3069 1440 : TYPE(SYBINT8, "bigint");
3070 1380 : TYPE(SYBUINT1, "unsigned tinyint");
3071 1380 : TYPE(SYBUINT2, "unsigned smallint");
3072 1380 : TYPE(SYBUINT4, "unsigned int");
3073 1380 : TYPE(SYBUINT8, "unsigned bigint");
3074 0 : TYPE(SYBINTN, "integer-null");
3075 1380 : TYPE(SYBMONEY4, "smallmoney");
3076 1380 : TYPE(SYBMONEY, "money");
3077 0 : TYPE(SYBMONEYN, "money-null");
3078 0 : TYPE(SYBNTEXT, "UCS-2 text");
3079 0 : TYPE(SYBNVARCHAR, "UCS-2 varchar");
3080 1210 : TYPE(SYBNUMERIC, "numeric");
3081 1380 : TYPE(SYBREAL, "real");
3082 1550 : TYPE(SYBTEXT, "text");
3083 350 : TYPE(SYBUNIQUE, "uniqueidentifier");
3084 1250 : TYPE(SYBVARBINARY, "varbinary");
3085 1554 : TYPE(SYBVARCHAR, "varchar");
3086 0 : TYPE(SYBVARIANT, "variant");
3087 0 : TYPE(SYBVOID, "void");
3088 1250 : TYPE(XSYBBINARY, "xbinary");
3089 1550 : TYPE(XSYBCHAR, "xchar");
3090 0 : TYPE(XSYBNCHAR, "x UCS-2 char");
3091 4 : TYPE(XSYBNVARCHAR, "x UCS-2 varchar");
3092 1250 : TYPE(XSYBVARBINARY, "xvarbinary");
3093 1550 : TYPE(XSYBVARCHAR, "xvarchar");
3094 0 : TYPE(SYBMSXML, "xml");
3095 710 : TYPE(SYBMSDATE, "date");
3096 710 : TYPE(SYBMSTIME, "time");
3097 710 : TYPE(SYBMSDATETIME2, "datetime2");
3098 710 : TYPE(SYBMSDATETIMEOFFSET, "datetimeoffset");
3099 770 : TYPE(SYBDATE, "date");
3100 770 : TYPE(SYBTIME, "time");
3101 710 : TYPE(SYB5BIGTIME, "bigtime");
3102 710 : TYPE(SYB5BIGDATETIME, "bigdatetime");
3103 0 : TYPE(SYBMSTABLE, "user-defined table type");
3104 : default:
3105 : break;
3106 : }
3107 0 : return "";
3108 : #undef TYPE
3109 : }
3110 :
3111 : /**
3112 : * Returns string representation for a given token type
3113 : * \param marker token type
3114 : * \return string representation. Empty if not token not valid.
3115 : */
3116 : static const char *
3117 84 : tds_token_name(unsigned char marker)
3118 : {
3119 84 : switch (marker) {
3120 :
3121 : case TDS5_PARAMFMT2_TOKEN:
3122 : return "TDS5_PARAMFMT2";
3123 0 : case TDS_ORDERBY2_TOKEN:
3124 0 : return "ORDERBY2";
3125 0 : case TDS_ROWFMT2_TOKEN:
3126 0 : return "ROWFMT2";
3127 0 : case TDS_LOGOUT_TOKEN:
3128 0 : return "LOGOUT";
3129 0 : case TDS_RETURNSTATUS_TOKEN:
3130 0 : return "RETURNSTATUS";
3131 0 : case TDS_PROCID_TOKEN:
3132 0 : return "PROCID";
3133 4 : case TDS7_RESULT_TOKEN:
3134 4 : return "TDS7_RESULT";
3135 0 : case TDS_CURINFO_TOKEN:
3136 0 : return "TDS_CURINFO";
3137 0 : case TDS7_COMPUTE_RESULT_TOKEN:
3138 0 : return "TDS7_COMPUTE_RESULT";
3139 0 : case TDS_COLNAME_TOKEN:
3140 0 : return "COLNAME";
3141 0 : case TDS_COLFMT_TOKEN:
3142 0 : return "COLFMT";
3143 0 : case TDS_DYNAMIC2_TOKEN:
3144 0 : return "DYNAMIC2";
3145 0 : case TDS_TABNAME_TOKEN:
3146 0 : return "TABNAME";
3147 0 : case TDS_COLINFO_TOKEN:
3148 0 : return "COLINFO";
3149 0 : case TDS_COMPUTE_NAMES_TOKEN:
3150 0 : return "COMPUTE_NAMES";
3151 0 : case TDS_COMPUTE_RESULT_TOKEN:
3152 0 : return "COMPUTE_RESULT";
3153 0 : case TDS_ORDERBY_TOKEN:
3154 0 : return "ORDERBY";
3155 0 : case TDS_ERROR_TOKEN:
3156 0 : return "ERROR";
3157 16 : case TDS_INFO_TOKEN:
3158 16 : return "INFO";
3159 0 : case TDS_PARAM_TOKEN:
3160 0 : return "PARAM";
3161 4 : case TDS_LOGINACK_TOKEN:
3162 4 : return "LOGINACK";
3163 0 : case TDS_CONTROL_FEATUREEXTACK_TOKEN:
3164 0 : return "CONTROL/FEATUREEXTACK";
3165 8 : case TDS_ROW_TOKEN:
3166 8 : return "ROW";
3167 0 : case TDS_NBC_ROW_TOKEN:
3168 0 : return "NBC_ROW";
3169 0 : case TDS_CMP_ROW_TOKEN:
3170 0 : return "CMP_ROW";
3171 0 : case TDS5_PARAMS_TOKEN:
3172 0 : return "TDS5_PARAMS";
3173 0 : case TDS_CAPABILITY_TOKEN:
3174 0 : return "CAPABILITY";
3175 32 : case TDS_ENVCHANGE_TOKEN:
3176 32 : return "ENVCHANGE";
3177 0 : case TDS_SESSIONSTATE_TOKEN:
3178 0 : return "SESSIONSTATE";
3179 0 : case TDS_EED_TOKEN:
3180 0 : return "EED";
3181 0 : case TDS_DBRPC_TOKEN:
3182 0 : return "DBRPC";
3183 0 : case TDS5_DYNAMIC_TOKEN:
3184 0 : return "TDS5_DYNAMIC";
3185 0 : case TDS5_PARAMFMT_TOKEN:
3186 0 : return "TDS5_PARAMFMT";
3187 4 : case TDS_AUTH_TOKEN:
3188 4 : return "AUTH";
3189 0 : case TDS_RESULT_TOKEN:
3190 0 : return "RESULT";
3191 16 : case TDS_DONE_TOKEN:
3192 16 : return "DONE";
3193 0 : case TDS_DONEPROC_TOKEN:
3194 0 : return "DONEPROC";
3195 0 : case TDS_DONEINPROC_TOKEN:
3196 0 : return "DONEINPROC";
3197 0 : case TDS_MSG_TOKEN:
3198 0 : return "MSG";
3199 :
3200 : default:
3201 : break;
3202 : }
3203 :
3204 0 : return "";
3205 : }
3206 :
3207 : /**
3208 : * Adjust column size according to client's encoding
3209 : * \tds
3210 : * \param curcol column to adjust
3211 : */
3212 : static void
3213 69229 : adjust_character_column_size(TDSSOCKET * tds, TDSCOLUMN * curcol)
3214 : {
3215 69229 : CHECK_TDS_EXTRA(tds);
3216 69229 : CHECK_COLUMN_EXTRA(curcol);
3217 :
3218 69229 : if (is_ascii_type(curcol->on_server.column_type)) {
3219 : /* don't override setting from column collation */
3220 23344 : if (!curcol->char_conv)
3221 5542 : curcol->char_conv = tds->conn->char_convs[client2server_chardata];
3222 : goto compute;
3223 : }
3224 :
3225 45885 : if (IS_TDS7_PLUS(tds->conn)) {
3226 38509 : if (is_unicode_type(curcol->on_server.column_type))
3227 4081 : curcol->char_conv = tds->conn->char_convs[client2ucs2];
3228 : goto compute;
3229 : }
3230 :
3231 : /* Sybase UNI(VAR)CHAR fields are transmitted via SYBLONGBINARY and in UTF-16 */
3232 7376 : if (is_unicode_type(curcol->on_server.column_type) ||
3233 808 : (curcol->on_server.column_type == SYBLONGBINARY && (
3234 808 : curcol->column_usertype == USER_UNICHAR_TYPE ||
3235 : curcol->column_usertype == USER_UNIVARCHAR_TYPE))) {
3236 800 : const int canonic_client = tds->conn->char_convs[client2ucs2]->from.charset.canonic;
3237 800 : const int sybase_utf16 = TDS_CHARSET_UTF_16LE;
3238 :
3239 1600 : if (tds_capability_has_res(tds->conn, TDS_RES_IMAGE_NONCHAR)) {
3240 0 : curcol->char_conv = tds_iconv_get_info(tds->conn, canonic_client, TDS_CHARSET_UTF_8);
3241 0 : goto compute;
3242 : }
3243 :
3244 800 : curcol->char_conv = tds_iconv_get_info(tds->conn, canonic_client, sybase_utf16);
3245 :
3246 : /* fallback to UCS-2LE */
3247 : /* FIXME should be useless. Does not works always */
3248 800 : if (!curcol->char_conv)
3249 0 : curcol->char_conv = tds->conn->char_convs[client2ucs2];
3250 : }
3251 :
3252 76605 : compute:
3253 69229 : if (!USE_ICONV || !curcol->char_conv)
3254 : return;
3255 :
3256 19520 : curcol->on_server.column_size = curcol->column_size;
3257 39040 : curcol->column_size = determine_adjusted_size(curcol->char_conv, curcol->column_size);
3258 :
3259 19520 : tdsdump_log(TDS_DBG_INFO1, "adjust_character_column_size:\n"
3260 : "\tServer charset: %s\n"
3261 : "\tServer column_size: %d\n"
3262 : "\tClient charset: %s\n"
3263 : "\tClient column_size: %d\n",
3264 : curcol->char_conv->to.charset.name,
3265 : curcol->on_server.column_size,
3266 : curcol->char_conv->from.charset.name,
3267 : curcol->column_size);
3268 : }
3269 :
3270 : /**
3271 : * Allow for maximum possible size of converted data,
3272 : * while being careful about integer division truncation.
3273 : * All character data pass through iconv. It doesn't matter if the server side
3274 : * is Unicode or not; even Latin1 text need conversion if,
3275 : * for example, the client is UTF-8.
3276 : * \param char_conv conversion structure
3277 : * \param size unconverted byte size
3278 : * \return maximum size for converted string
3279 : */
3280 : static int
3281 : determine_adjusted_size(const TDSICONV * char_conv, int size)
3282 : {
3283 : if (!char_conv)
3284 : return size;
3285 :
3286 : /* same charset */
3287 19520 : if ((char_conv->flags & TDS_ENCODING_MEMCPY) != 0
3288 16130 : || char_conv->to.charset.canonic == char_conv->from.charset.canonic)
3289 : return size;
3290 :
3291 : /* avoid possible overflow */
3292 16130 : if (size >= 0x10000000)
3293 : return 0x7fffffff;
3294 :
3295 15986 : size *= char_conv->from.charset.max_bytes_per_char;
3296 15986 : if (size % char_conv->to.charset.min_bytes_per_char)
3297 0 : size += char_conv->to.charset.min_bytes_per_char;
3298 15986 : size /= char_conv->to.charset.min_bytes_per_char;
3299 :
3300 : return size;
3301 : }
3302 :
3303 : /** @} */
|