1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Ard Biesheuvel <abies@php.net>                               |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "pdo/php_pdo.h"
25 #include "pdo/php_pdo_driver.h"
26 #include "php_pdo_firebird.h"
27 #include "php_pdo_firebird_int.h"
28 
29 #include <time.h>
30 
31 #define RECORD_ERROR(stmt) _firebird_error(NULL, stmt,  __FILE__, __LINE__)
32 
33 #define READ_AND_RETURN_USING_MEMCPY(type, sqldata) do { \
34 		type ret; \
35 		memcpy(&ret, sqldata, sizeof(ret)); \
36 		return ret; \
37 	} while (0);
38 
get_isc_int64_from_sqldata(const ISC_SCHAR * sqldata)39 static zend_always_inline ISC_INT64 get_isc_int64_from_sqldata(const ISC_SCHAR *sqldata)
40 {
41 	READ_AND_RETURN_USING_MEMCPY(ISC_INT64, sqldata);
42 }
43 
get_isc_long_from_sqldata(const ISC_SCHAR * sqldata)44 static zend_always_inline ISC_LONG get_isc_long_from_sqldata(const ISC_SCHAR *sqldata)
45 {
46 	READ_AND_RETURN_USING_MEMCPY(ISC_LONG, sqldata);
47 }
48 
get_double_from_sqldata(const ISC_SCHAR * sqldata)49 static zend_always_inline double get_double_from_sqldata(const ISC_SCHAR *sqldata)
50 {
51 	READ_AND_RETURN_USING_MEMCPY(double, sqldata);
52 }
53 
get_float_from_sqldata(const ISC_SCHAR * sqldata)54 static zend_always_inline float get_float_from_sqldata(const ISC_SCHAR *sqldata)
55 {
56 	READ_AND_RETURN_USING_MEMCPY(float, sqldata);
57 }
58 
get_isc_timestamp_from_sqldata(const ISC_SCHAR * sqldata)59 static zend_always_inline ISC_TIMESTAMP get_isc_timestamp_from_sqldata(const ISC_SCHAR *sqldata)
60 {
61 	READ_AND_RETURN_USING_MEMCPY(ISC_TIMESTAMP, sqldata);
62 }
63 
get_isc_quad_from_sqldata(const ISC_SCHAR * sqldata)64 static zend_always_inline ISC_QUAD get_isc_quad_from_sqldata(const ISC_SCHAR *sqldata)
65 {
66 	READ_AND_RETURN_USING_MEMCPY(ISC_QUAD, sqldata);
67 }
68 
69 /* free the allocated space for passing field values to the db and back */
free_sqlda(XSQLDA const * sqlda)70 static void free_sqlda(XSQLDA const *sqlda) /* {{{ */
71 {
72 	int i;
73 
74 	for (i = 0; i < sqlda->sqld; ++i) {
75 		XSQLVAR const *var = &sqlda->sqlvar[i];
76 
77 		if (var->sqlind) {
78 			efree(var->sqlind);
79 		}
80 	}
81 }
82 /* }}} */
83 
84 /* called by PDO to clean up a statement handle */
firebird_stmt_dtor(pdo_stmt_t * stmt)85 static int firebird_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
86 {
87 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
88 	int result = 1;
89 
90 	/* release the statement */
91 	if (isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_drop)) {
92 		RECORD_ERROR(stmt);
93 		result = 0;
94 	}
95 
96 	zend_hash_destroy(S->named_params);
97 	FREE_HASHTABLE(S->named_params);
98 
99 	/* clean up the input descriptor */
100 	if (S->in_sqlda) {
101 		free_sqlda(S->in_sqlda);
102 		efree(S->in_sqlda);
103 	}
104 
105 	free_sqlda(&S->out_sqlda);
106 	efree(S);
107 
108 	return result;
109 }
110 /* }}} */
111 
112 /* called by PDO to execute a prepared query */
firebird_stmt_execute(pdo_stmt_t * stmt)113 static int firebird_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
114 {
115 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
116 	pdo_firebird_db_handle *H = S->H;
117 	zend_ulong affected_rows = 0;
118 	static char info_count[] = {isc_info_sql_records};
119 	char result[64];
120 
121 	do {
122 		/* named or open cursors should be closed first */
123 		if ((*S->name || S->cursor_open) && isc_dsql_free_statement(H->isc_status, &S->stmt, DSQL_close)) {
124 			break;
125 		}
126 		S->cursor_open = 0;
127 
128 		/* allocate storage for the output data */
129 		if (S->out_sqlda.sqld) {
130 			unsigned int i;
131 			for (i = 0; i < S->out_sqlda.sqld; i++) {
132 				XSQLVAR *var = &S->out_sqlda.sqlvar[i];
133 				if (var->sqlind) {
134 					efree(var->sqlind);
135 				}
136 				var->sqlind = (void*)ecalloc(1, var->sqllen + 2 * sizeof(short));
137 				var->sqldata = &((char*)var->sqlind)[sizeof(short)];
138 			}
139 		}
140 
141 		if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
142 			if (isc_dsql_execute2(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda, &S->out_sqlda)) {
143 				break;
144 			}
145 		} else if (isc_dsql_execute(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
146 			break;
147 		}
148 
149 		/* Determine how many rows have changed. In this case we are
150 		 * only interested in rows changed, not rows retrieved. That
151 		 * should be handled by the client when fetching. */
152 		stmt->row_count = affected_rows;
153 
154 		switch (S->statement_type) {
155 			case isc_info_sql_stmt_insert:
156 			case isc_info_sql_stmt_update:
157 			case isc_info_sql_stmt_delete:
158 			case isc_info_sql_stmt_exec_procedure:
159 				if (isc_dsql_sql_info(H->isc_status, &S->stmt, sizeof ( info_count),
160 					info_count, sizeof(result), result)) {
161 					break;
162 				}
163 				if (result[0] == isc_info_sql_records) {
164 					unsigned i = 3, result_size = isc_vax_integer(&result[1], 2);
165 					if (result_size > sizeof(result)) {
166 						goto error;
167 					}
168 					while (result[i] != isc_info_end && i < result_size) {
169 						short len = (short) isc_vax_integer(&result[i + 1], 2);
170 						if (len != 1 && len != 2 && len != 4) {
171 							goto error;
172 						}
173 						if (result[i] != isc_info_req_select_count) {
174 							affected_rows += isc_vax_integer(&result[i + 3], len);
175 						}
176 						i += len + 3;
177 					}
178 					stmt->row_count = affected_rows;
179 				}
180 			/* TODO Dead code or assert one of the previous cases are hit? */
181 			default:
182 				;
183 		}
184 
185 		/* commit? */
186 		if (stmt->dbh->auto_commit && isc_commit_retaining(H->isc_status, &H->tr)) {
187 			break;
188 		}
189 
190 		*S->name = 0;
191 		S->cursor_open = S->out_sqlda.sqln && (S->statement_type != isc_info_sql_stmt_exec_procedure);
192 		S->exhausted = !S->out_sqlda.sqln; /* There are data to fetch */
193 
194 		return 1;
195 	} while (0);
196 
197 error:
198 	RECORD_ERROR(stmt);
199 
200 	return 0;
201 }
202 /* }}} */
203 
204 /* called by PDO to fetch the next row from a statement */
firebird_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)205 static int firebird_stmt_fetch(pdo_stmt_t *stmt, /* {{{ */
206 	enum pdo_fetch_orientation ori, zend_long offset)
207 {
208 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
209 	pdo_firebird_db_handle *H = S->H;
210 
211 	if (!stmt->executed) {
212 		strcpy(stmt->error_code, "HY000");
213 		H->last_app_error = "Cannot fetch from a closed cursor";
214 	} else if (!S->exhausted) {
215 		if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
216 			stmt->row_count = 1;
217 			S->exhausted = 1;
218 			return 1;
219 		}
220 		if (isc_dsql_fetch(H->isc_status, &S->stmt, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
221 			if (H->isc_status[0] && H->isc_status[1]) {
222 				RECORD_ERROR(stmt);
223 			}
224 			S->exhausted = 1;
225 			return 0;
226 		}
227 		stmt->row_count++;
228 		return 1;
229 	}
230 	return 0;
231 }
232 /* }}} */
233 
234 /* called by PDO to retrieve information about the fields being returned */
firebird_stmt_describe(pdo_stmt_t * stmt,int colno)235 static int firebird_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
236 {
237 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
238 	struct pdo_column_data *col = &stmt->columns[colno];
239 	XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
240 	int colname_len;
241 	char *cp;
242 
243 	if ((var->sqltype & ~1) == SQL_TEXT) {
244 		var->sqltype = SQL_VARYING | (var->sqltype & 1);
245 	}
246 	colname_len = (S->H->fetch_table_names && var->relname_length)
247 					? (var->aliasname_length + var->relname_length + 1)
248 					: (var->aliasname_length);
249 	col->precision = -var->sqlscale;
250 	col->maxlen = var->sqllen;
251 	col->name = zend_string_alloc(colname_len, 0);
252 	cp = ZSTR_VAL(col->name);
253 	if (colname_len > var->aliasname_length) {
254 		memmove(cp, var->relname, var->relname_length);
255 		cp += var->relname_length;
256 		*cp++ = '.';
257 	}
258 	memmove(cp, var->aliasname, var->aliasname_length);
259 	*(cp+var->aliasname_length) = '\0';
260 
261 	return 1;
262 }
263 /* }}} */
264 
firebird_stmt_get_column_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)265 static int firebird_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
266 {
267 	pdo_firebird_stmt *S = (pdo_firebird_stmt *) stmt->driver_data;
268 	XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
269 
270 	enum pdo_param_type param_type;
271 	if (var->sqlscale < 0) {
272 		param_type = PDO_PARAM_STR;
273 	} else {
274 		switch (var->sqltype & ~1) {
275 			case SQL_SHORT:
276 			case SQL_LONG:
277 #if SIZEOF_ZEND_LONG >= 8
278 			case SQL_INT64:
279 #endif
280 				param_type = PDO_PARAM_INT;
281 				break;
282 #ifdef SQL_BOOLEAN
283 			case SQL_BOOLEAN:
284 				param_type = PDO_PARAM_BOOL;
285 				break;
286 #endif
287 			default:
288 				param_type = PDO_PARAM_STR;
289 				break;
290 		}
291 	}
292 
293 	array_init(return_value);
294 	add_assoc_long(return_value, "pdo_type", param_type);
295 	return 1;
296 }
297 
298 /* fetch a blob into a fetch buffer */
firebird_fetch_blob(pdo_stmt_t * stmt,int colno,zval * result,ISC_QUAD * blob_id)299 static int firebird_fetch_blob(pdo_stmt_t *stmt, int colno, zval *result, ISC_QUAD *blob_id)
300 {
301 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
302 	pdo_firebird_db_handle *H = S->H;
303 	isc_blob_handle blobh = PDO_FIREBIRD_HANDLE_INITIALIZER;
304 	char const bl_item = isc_info_blob_total_length;
305 	char bl_info[20];
306 	unsigned short i;
307 	int retval = 0;
308 	size_t len = 0;
309 
310 	if (isc_open_blob(H->isc_status, &H->db, &H->tr, &blobh, blob_id)) {
311 		RECORD_ERROR(stmt);
312 		return 0;
313 	}
314 
315 	if (isc_blob_info(H->isc_status, &blobh, 1, const_cast(&bl_item),
316 			sizeof(bl_info), bl_info)) {
317 		RECORD_ERROR(stmt);
318 		goto fetch_blob_end;
319 	}
320 
321 	/* find total length of blob's data */
322 	for (i = 0; i < sizeof(bl_info); ) {
323 		unsigned short item_len;
324 		char item = bl_info[i++];
325 
326 		if (item == isc_info_end || item == isc_info_truncated || item == isc_info_error
327 				|| i >= sizeof(bl_info)) {
328 			H->last_app_error = "Couldn't determine BLOB size";
329 			goto fetch_blob_end;
330 		}
331 
332 		item_len = (unsigned short) isc_vax_integer(&bl_info[i], 2);
333 
334 		if (item == isc_info_blob_total_length) {
335 			len = isc_vax_integer(&bl_info[i+2], item_len);
336 			break;
337 		}
338 		i += item_len+2;
339 	}
340 
341 	/* we've found the blob's length, now fetch! */
342 
343 	if (len) {
344 		zend_ulong cur_len;
345 		unsigned short seg_len;
346 		ISC_STATUS stat;
347 		zend_string *str;
348 
349 		/* prevent overflow */
350 		if (len > ZSTR_MAX_LEN) {
351 			result = 0;
352 			goto fetch_blob_end;
353 		}
354 
355 		str = zend_string_alloc(len, 0);
356 
357 		for (cur_len = stat = 0; (!stat || stat == isc_segment) && cur_len < len; cur_len += seg_len) {
358 
359 			unsigned short chunk_size = (len - cur_len) > USHRT_MAX ? USHRT_MAX
360 				: (unsigned short)(len - cur_len);
361 
362 			stat = isc_get_segment(H->isc_status, &blobh, &seg_len, chunk_size, ZSTR_VAL(str) + cur_len);
363 		}
364 
365 		ZSTR_VAL(str)[len] = '\0';
366 		ZVAL_STR(result, str);
367 
368 		if (H->isc_status[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) {
369 			H->last_app_error = "Error reading from BLOB";
370 			goto fetch_blob_end;
371 		}
372 	}
373 	retval = 1;
374 
375 fetch_blob_end:
376 	if (isc_close_blob(H->isc_status, &blobh)) {
377 		RECORD_ERROR(stmt);
378 		return 0;
379 	}
380 	return retval;
381 }
382 /* }}} */
383 
firebird_stmt_get_col(pdo_stmt_t * stmt,int colno,zval * result,enum pdo_param_type * type)384 static int firebird_stmt_get_col(
385 		pdo_stmt_t *stmt, int colno, zval *result, enum pdo_param_type *type)
386 {
387 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
388 	XSQLVAR const *var = &S->out_sqlda.sqlvar[colno];
389 
390 	if (*var->sqlind == -1) {
391 		ZVAL_NULL(result);
392 	} else {
393 		if (var->sqlscale < 0) {
394 			static ISC_INT64 const scales[] = { 1, 10, 100, 1000,
395 				10000,
396 				100000,
397 				1000000,
398 				10000000,
399 				100000000,
400 				1000000000,
401 				LL_LIT(10000000000),
402 				LL_LIT(100000000000),
403 				LL_LIT(1000000000000),
404 				LL_LIT(10000000000000),
405 				LL_LIT(100000000000000),
406 				LL_LIT(1000000000000000),
407 				LL_LIT(10000000000000000),
408 				LL_LIT(100000000000000000),
409 				LL_LIT(1000000000000000000)
410 			};
411 			ISC_INT64 n, f = scales[-var->sqlscale];
412 			zend_string *str;
413 
414 			switch (var->sqltype & ~1) {
415 				case SQL_SHORT:
416 					n = *(short*)var->sqldata;
417 					break;
418 				case SQL_LONG:
419 					n = get_isc_long_from_sqldata(var->sqldata);
420 					break;
421 				case SQL_INT64:
422 					n = get_isc_int64_from_sqldata(var->sqldata);
423 					break;
424 				case SQL_DOUBLE:
425 					break;
426 				EMPTY_SWITCH_DEFAULT_CASE()
427 			}
428 
429 			if ((var->sqltype & ~1) == SQL_DOUBLE) {
430 				str = zend_strpprintf(0, "%.*F", -var->sqlscale, get_double_from_sqldata(var->sqldata));
431 			} else if (n >= 0) {
432 				str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
433 					n / f, -var->sqlscale, n % f);
434 			} else if (n <= -f) {
435 				str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
436 					n / f, -var->sqlscale, -n % f);
437 			 } else {
438 				str = zend_strpprintf(0, "-0.%0*" LL_MASK "d", -var->sqlscale, -n % f);
439 			}
440 			ZVAL_STR(result, str);
441 		} else {
442 			switch (var->sqltype & ~1) {
443 				struct tm t;
444 				char *fmt;
445 
446 				case SQL_VARYING:
447 					ZVAL_STRINGL_FAST(result, &var->sqldata[2], *(short*)var->sqldata);
448 					break;
449 				case SQL_TEXT:
450 					ZVAL_STRINGL_FAST(result, var->sqldata, var->sqllen);
451 					break;
452 				case SQL_SHORT:
453 					ZVAL_LONG(result, *(short*)var->sqldata);
454 					break;
455 				case SQL_LONG:
456 					ZVAL_LONG(result, get_isc_long_from_sqldata(var->sqldata));
457 					break;
458 				case SQL_INT64:
459 #if SIZEOF_ZEND_LONG >= 8
460 					ZVAL_LONG(result, get_isc_int64_from_sqldata(var->sqldata));
461 #else
462 					ZVAL_STR(result, zend_strpprintf(0, "%" LL_MASK "d", get_isc_int64_from_sqldata(var->sqldata)));
463 #endif
464 					break;
465 				case SQL_FLOAT:
466 					/* TODO: Why is this not returned as the native type? */
467 					ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.8H", get_float_from_sqldata(var->sqldata)));
468 					break;
469 				case SQL_DOUBLE:
470 					/* TODO: Why is this not returned as the native type? */
471 					ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.16H", get_double_from_sqldata(var->sqldata)));
472 					break;
473 #ifdef SQL_BOOLEAN
474 				case SQL_BOOLEAN:
475 					ZVAL_BOOL(result, *(FB_BOOLEAN*)var->sqldata);
476 					break;
477 #endif
478 				case SQL_TYPE_DATE:
479 					isc_decode_sql_date((ISC_DATE*)var->sqldata, &t);
480 					fmt = S->H->date_format ? S->H->date_format : PDO_FB_DEF_DATE_FMT;
481 					if (0) {
482 				case SQL_TYPE_TIME:
483 						isc_decode_sql_time((ISC_TIME*)var->sqldata, &t);
484 						fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
485 					} else if (0) {
486 				case SQL_TIMESTAMP:
487 						{
488 							ISC_TIMESTAMP timestamp = get_isc_timestamp_from_sqldata(var->sqldata);
489 							isc_decode_timestamp(&timestamp, &t);
490 						}
491 						fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
492 					}
493 					/* convert the timestamp into a string */
494 					char buf[80];
495 					size_t len = strftime(buf, sizeof(buf), fmt, &t);
496 					ZVAL_STRINGL(result, buf, len);
497 					break;
498 				case SQL_BLOB: {
499 					ISC_QUAD quad = get_isc_quad_from_sqldata(var->sqldata);
500 					return firebird_fetch_blob(stmt, colno, result, &quad);
501 				}
502 			}
503 		}
504 	}
505 	return 1;
506 }
507 
firebird_bind_blob(pdo_stmt_t * stmt,ISC_QUAD * blob_id,zval * param)508 static int firebird_bind_blob(pdo_stmt_t *stmt, ISC_QUAD *blob_id, zval *param)
509 {
510 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
511 	pdo_firebird_db_handle *H = S->H;
512 	isc_blob_handle h = PDO_FIREBIRD_HANDLE_INITIALIZER;
513 	zval data;
514 	zend_ulong put_cnt = 0, rem_cnt;
515 	unsigned short chunk_size;
516 	int result = 1;
517 
518 	if (isc_create_blob(H->isc_status, &H->db, &H->tr, &h, blob_id)) {
519 		RECORD_ERROR(stmt);
520 		return 0;
521 	}
522 
523 	if (Z_TYPE_P(param) != IS_STRING) {
524 		ZVAL_STR(&data, zval_get_string_func(param));
525 	} else {
526 		ZVAL_COPY_VALUE(&data, param);
527 	}
528 
529 	for (rem_cnt = Z_STRLEN(data); rem_cnt > 0; rem_cnt -= chunk_size) {
530 		chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt;
531 		if (isc_put_segment(H->isc_status, &h, chunk_size, &Z_STRVAL(data)[put_cnt])) {
532 			RECORD_ERROR(stmt);
533 			result = 0;
534 			break;
535 		}
536 		put_cnt += chunk_size;
537 	}
538 
539 	if (Z_TYPE_P(param) != IS_STRING) {
540 		zval_ptr_dtor_str(&data);
541 	}
542 
543 	if (isc_close_blob(H->isc_status, &h)) {
544 		RECORD_ERROR(stmt);
545 		return 0;
546 	}
547 	return result;
548 }
549 
firebird_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)550 static int firebird_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, /* {{{ */
551 	enum pdo_param_event event_type)
552 {
553 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
554 	XSQLDA *sqlda = param->is_param ? S->in_sqlda : &S->out_sqlda;
555 	XSQLVAR *var;
556 
557 	if (event_type == PDO_PARAM_EVT_FREE) { /* not used */
558 		return 1;
559 	}
560 
561 	if (!sqlda || param->paramno >= sqlda->sqld) {
562 		strcpy(stmt->error_code, "HY093");
563 		S->H->last_app_error = "Invalid parameter index";
564 		return 0;
565 	}
566 	if (param->is_param && param->paramno == -1) {
567 		zval *index;
568 
569 		/* try to determine the index by looking in the named_params hash */
570 		if ((index = zend_hash_find(S->named_params, param->name)) != NULL) {
571 			param->paramno = Z_LVAL_P(index);
572 		} else {
573 			/* ... or by looking in the input descriptor */
574 			int i;
575 
576 			for (i = 0; i < sqlda->sqld; ++i) {
577 				XSQLVAR *var = &sqlda->sqlvar[i];
578 
579 				if ((var->aliasname_length && !strncasecmp(ZSTR_VAL(param->name), var->aliasname,
580 						min(ZSTR_LEN(param->name), var->aliasname_length)))
581 						|| (var->sqlname_length && !strncasecmp(ZSTR_VAL(param->name), var->sqlname,
582 						min(ZSTR_LEN(param->name), var->sqlname_length)))) {
583 					param->paramno = i;
584 					break;
585 				}
586 			}
587 			if (i >= sqlda->sqld) {
588 				strcpy(stmt->error_code, "HY093");
589 				S->H->last_app_error = "Invalid parameter name";
590 				return 0;
591 			}
592 		}
593 	}
594 
595 	var = &sqlda->sqlvar[param->paramno];
596 
597 	switch (event_type) {
598 		zval *parameter;
599 
600 		case PDO_PARAM_EVT_ALLOC:
601 			if (param->is_param) {
602 				/* allocate the parameter */
603 				if (var->sqlind) {
604 					efree(var->sqlind);
605 				}
606 				var->sqlind = (void*)emalloc(var->sqllen + 2*sizeof(short));
607 				var->sqldata = &((char*)var->sqlind)[sizeof(short)];
608 			}
609 			break;
610 
611 		case PDO_PARAM_EVT_EXEC_PRE:
612 			if (!param->is_param) {
613 				break;
614 			}
615 
616 			*var->sqlind = 0;
617 			if (Z_ISREF(param->parameter)) {
618 				parameter = Z_REFVAL(param->parameter);
619 			} else {
620 				parameter = &param->parameter;
621 			}
622 
623 			if (Z_TYPE_P(parameter) == IS_RESOURCE) {
624 				php_stream *stm = NULL;
625 
626 				php_stream_from_zval_no_verify(stm, parameter);
627 				if (stm) {
628 					zend_string *mem =  php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
629 					zval_ptr_dtor(parameter);
630 					ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
631 				} else {
632 					pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
633 					return 0;
634 				}
635 			}
636 
637 			switch (var->sqltype & ~1) {
638 				case SQL_ARRAY:
639 					strcpy(stmt->error_code, "HY000");
640 					S->H->last_app_error = "Cannot bind to array field";
641 					return 0;
642 
643 				case SQL_BLOB: {
644 					if (Z_TYPE_P(parameter) == IS_NULL) {
645 						/* Check if field allow NULL values */
646 						if (~var->sqltype & 1) {
647 							strcpy(stmt->error_code, "HY105");
648 							S->H->last_app_error = "Parameter requires non-null value";
649 							return 0;
650 						}
651 						*var->sqlind = -1;
652 						return 1;
653 					}
654 					ISC_QUAD quad = get_isc_quad_from_sqldata(var->sqldata);
655 					if (firebird_bind_blob(stmt, &quad, parameter) != 0) {
656 						memcpy(var->sqldata, &quad, sizeof(quad));
657 						return 1;
658 					}
659 					return 0;
660 				}
661 			}
662 
663 #ifdef SQL_BOOLEAN
664 			/* keep native BOOLEAN type */
665 			if ((var->sqltype & ~1) == SQL_BOOLEAN) {
666 				switch (Z_TYPE_P(parameter)) {
667 					case IS_LONG:
668 					case IS_DOUBLE:
669 					case IS_TRUE:
670 					case IS_FALSE:
671 						*(FB_BOOLEAN*)var->sqldata = zend_is_true(parameter) ? FB_TRUE : FB_FALSE;
672 						break;
673 					case IS_STRING:
674 						{
675 							zend_long lval;
676 							double dval;
677 
678 							if (Z_STRLEN_P(parameter) == 0) {
679 								*(FB_BOOLEAN*)var->sqldata = FB_FALSE;
680 								break;
681 							}
682 
683 							switch (is_numeric_string(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), &lval, &dval, 0)) {
684 								case IS_LONG:
685 									*(FB_BOOLEAN*)var->sqldata = (lval != 0) ? FB_TRUE : FB_FALSE;
686 									break;
687 								case IS_DOUBLE:
688 									*(FB_BOOLEAN*)var->sqldata = (dval != 0) ? FB_TRUE : FB_FALSE;
689 									break;
690 								default:
691 									if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "true", 4, 4)) {
692 										*(FB_BOOLEAN*)var->sqldata = FB_TRUE;
693 									} else if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "false", 5, 5)) {
694 										*(FB_BOOLEAN*)var->sqldata = FB_FALSE;
695 									} else {
696 										strcpy(stmt->error_code, "HY105");
697 										S->H->last_app_error = "Cannot convert string to boolean";
698 										return 0;
699 									}
700 
701 							}
702 						}
703 						break;
704 					case IS_NULL:
705 						*var->sqlind = -1;
706 						break;
707 					default:
708 						strcpy(stmt->error_code, "HY105");
709 						S->H->last_app_error = "Binding arrays/objects is not supported";
710 						return 0;
711 				}
712 				break;
713 			}
714 #endif
715 
716 
717 			/* check if a NULL should be inserted */
718 			switch (Z_TYPE_P(parameter)) {
719 				int force_null;
720 
721 				case IS_LONG:
722 					/* keep the allow-NULL flag */
723 					var->sqltype = (sizeof(zend_long) == 8 ? SQL_INT64 : SQL_LONG) | (var->sqltype & 1);
724 					var->sqldata = (void*)&Z_LVAL_P(parameter);
725 					var->sqllen = sizeof(zend_long);
726 					break;
727 				case IS_DOUBLE:
728 					/* keep the allow-NULL flag */
729 					var->sqltype = SQL_DOUBLE | (var->sqltype & 1);
730 					var->sqldata = (void*)&Z_DVAL_P(parameter);
731 					var->sqllen = sizeof(double);
732 					break;
733 				case IS_STRING:
734 					force_null = 0;
735 
736 					/* for these types, an empty string can be handled like a NULL value */
737 					switch (var->sqltype & ~1) {
738 						case SQL_SHORT:
739 						case SQL_LONG:
740 						case SQL_INT64:
741 						case SQL_FLOAT:
742 						case SQL_DOUBLE:
743 						case SQL_TIMESTAMP:
744 						case SQL_TYPE_DATE:
745 						case SQL_TYPE_TIME:
746 							force_null = (Z_STRLEN_P(parameter) == 0);
747 					}
748 					if (!force_null) {
749 						/* keep the allow-NULL flag */
750 						var->sqltype = SQL_TEXT | (var->sqltype & 1);
751 						var->sqldata = Z_STRVAL_P(parameter);
752 						var->sqllen = Z_STRLEN_P(parameter);
753 						break;
754 					}
755 					ZEND_FALLTHROUGH;
756 				case IS_NULL:
757 					/* complain if this field doesn't allow NULL values */
758 					if (~var->sqltype & 1) {
759 						strcpy(stmt->error_code, "HY105");
760 						S->H->last_app_error = "Parameter requires non-null value";
761 						return 0;
762 					}
763 					*var->sqlind = -1;
764 					break;
765 				default:
766 					strcpy(stmt->error_code, "HY105");
767 					S->H->last_app_error = "Binding arrays/objects is not supported";
768 					return 0;
769 			}
770 			break;
771 
772 		case PDO_PARAM_EVT_FETCH_POST:
773 			if (param->paramno == -1) {
774 				return 0;
775 			}
776 			if (param->is_param) {
777 				break;
778 			}
779 			if (Z_ISREF(param->parameter)) {
780 				parameter = Z_REFVAL(param->parameter);
781 			} else {
782 				parameter = &param->parameter;
783 			}
784 			zval_ptr_dtor(parameter);
785 			ZVAL_NULL(parameter);
786 			return firebird_stmt_get_col(stmt, param->paramno, parameter, NULL);
787 		default:
788 			;
789 	}
790 	return 1;
791 }
792 /* }}} */
793 
firebird_stmt_set_attribute(pdo_stmt_t * stmt,zend_long attr,zval * val)794 static int firebird_stmt_set_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
795 {
796 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
797 
798 	switch (attr) {
799 		default:
800 			return 0;
801 		case PDO_ATTR_CURSOR_NAME:
802 			if (!try_convert_to_string(val)) {
803 				return 0;
804 			}
805 
806 			if (isc_dsql_set_cursor_name(S->H->isc_status, &S->stmt, Z_STRVAL_P(val),0)) {
807 				RECORD_ERROR(stmt);
808 				return 0;
809 			}
810 			strlcpy(S->name, Z_STRVAL_P(val), sizeof(S->name));
811 			break;
812 	}
813 	return 1;
814 }
815 /* }}} */
816 
firebird_stmt_get_attribute(pdo_stmt_t * stmt,zend_long attr,zval * val)817 static int firebird_stmt_get_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
818 {
819 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
820 
821 	switch (attr) {
822 		default:
823 			return 0;
824 		case PDO_ATTR_CURSOR_NAME:
825 			if (*S->name) {
826 				ZVAL_STRING(val, S->name);
827 			} else {
828 				ZVAL_NULL(val);
829 			}
830 			break;
831 	}
832 	return 1;
833 }
834 /* }}} */
835 
firebird_stmt_cursor_closer(pdo_stmt_t * stmt)836 static int firebird_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
837 {
838 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
839 
840 	/* close the statement handle */
841 	if ((*S->name || S->cursor_open) && isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_close)) {
842 		RECORD_ERROR(stmt);
843 		return 0;
844 	}
845 	*S->name = 0;
846 	S->cursor_open = 0;
847 	return 1;
848 }
849 /* }}} */
850 
851 
852 const struct pdo_stmt_methods firebird_stmt_methods = { /* {{{ */
853 	firebird_stmt_dtor,
854 	firebird_stmt_execute,
855 	firebird_stmt_fetch,
856 	firebird_stmt_describe,
857 	firebird_stmt_get_col,
858 	firebird_stmt_param_hook,
859 	firebird_stmt_set_attribute,
860 	firebird_stmt_get_attribute,
861 	firebird_stmt_get_column_meta,
862 	NULL, /* next_rowset_func */
863 	firebird_stmt_cursor_closer
864 };
865 /* }}} */
866