From 1967ceeda40bd65a4f07cc73e8ca9c40b2144511 Mon Sep 17 00:00:00 2001 From: Ard Biesheuvel Date: Sun, 17 Aug 2003 12:31:55 +0000 Subject: [PATCH] Apply fix for ibase_blob_get() to ibase_fetch_*() Apply fix for ibase_blob_add() to ibase_query()/ibase_execute() --- ext/interbase/interbase.c | 199 +++++++++++++++++++------------------- 1 file changed, 102 insertions(+), 97 deletions(-) diff --git a/ext/interbase/interbase.c b/ext/interbase/interbase.c index 6eac189167..159846dde7 100644 --- a/ext/interbase/interbase.c +++ b/ext/interbase/interbase.c @@ -1285,6 +1285,28 @@ _php_ibase_alloc_query_error: } /* }}} */ +/* ((( _php_ibase_blob_add */ +static int _php_ibase_blob_add(zval **string_arg, ibase_blob_handle *ib_blob TSRMLS_DC) +{ + unsigned long put_cnt = 0, rem_cnt; + unsigned short chunk_size; + + convert_to_string_ex(string_arg); + + for (rem_cnt = Z_STRLEN_PP(string_arg); rem_cnt > 0; rem_cnt -= chunk_size) { + + chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt; + + if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, chunk_size, &Z_STRVAL_PP(string_arg)[put_cnt] )) { + _php_ibase_error(TSRMLS_C); + return FAILURE; + } + put_cnt += chunk_size; + } + return SUCCESS; +} +/* }}} */ + /* {{{ _php_ibase_bind() Bind parameter placeholders in a previously prepared query */ static int _php_ibase_bind(XSQLDA *sqlda, zval **b_vars, BIND_BUF *buf, ibase_query *ib_query TSRMLS_DC) @@ -1442,6 +1464,7 @@ static int _php_ibase_bind(XSQLDA *sqlda, zval **b_vars, BIND_BUF *buf, ibase_qu case SQL_BLOB: { ibase_blob_handle *ib_blob_id; + if (Z_TYPE_P(b_var) != IS_STRING || Z_STRLEN_P(b_var) != sizeof(ibase_blob_handle) || ((ibase_blob_handle *)(Z_STRVAL_P(b_var)))->bl_handle != 0) { ibase_blob_handle *ib_blob; @@ -1455,13 +1478,15 @@ static int _php_ibase_bind(XSQLDA *sqlda, zval **b_vars, BIND_BUF *buf, ibase_qu _php_ibase_error(TSRMLS_C); return FAILURE; } - convert_to_string(b_var); - if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, (unsigned short) Z_STRLEN_P(b_var), Z_STRVAL_P(b_var))) { - _php_ibase_error(TSRMLS_C); + + if (_php_ibase_blob_add(&b_var, ib_blob TSRMLS_CC) != SUCCESS) { + efree(ib_blob); return FAILURE; } + if (isc_close_blob(IB_STATUS, &ib_blob->bl_handle)) { _php_ibase_error(TSRMLS_C); + efree(ib_blob); return FAILURE; } ib_blob_id = ib_blob; @@ -1612,8 +1637,7 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul default: - Z_BVAL_P(return_value) = 1; - Z_TYPE_P(return_value) = IS_BOOL; + RETVAL_BOOL(1); } /* allocate sqlda and output buffers */ @@ -1668,6 +1692,8 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul switch (ib_query->statement_type) { + unsigned long affected_rows; + case isc_info_sql_stmt_insert: case isc_info_sql_stmt_update: case isc_info_sql_stmt_delete: @@ -1677,6 +1703,8 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul _php_ibase_error(TSRMLS_C); goto _php_ibase_exec_error; } + + affected_rows = 0; if (result[0] == isc_info_sql_records) { unsigned i = 3, result_size = isc_vax_integer(&result[1],2); @@ -1684,11 +1712,15 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul while (result[i] != isc_info_end && i < result_size) { short len = (short)isc_vax_integer(&result[i+1],2); if (result[i] != isc_info_req_select_count) { - ib_query->trans->affected_rows += isc_vax_integer(&result[i+3],len); + affected_rows += isc_vax_integer(&result[i+3],len); } i += len+3; } - } + } + if (affected_rows > 0) { + ib_query->trans->affected_rows = affected_rows; + RETVAL_LONG(affected_rows); + } } rv = SUCCESS; @@ -1908,7 +1940,7 @@ static void _php_ibase_trans_end(INTERNAL_FUNCTION_PARAMETERS, int commit) { ibase_trans *trans = NULL; int res_id = 0; - ISC_STATUS result = 0; + ISC_STATUS result; RESET_ERRMSG; @@ -1956,7 +1988,7 @@ static void _php_ibase_trans_end(INTERNAL_FUNCTION_PARAMETERS, int commit) switch (commit) { - case ROLLBACK: + default: /* == case ROLLBACK: */ result = isc_rollback_transaction(IB_STATUS, &trans->handle); break; case COMMIT: @@ -2480,6 +2512,41 @@ static int _php_ibase_arr_zval(zval *ar_zval, char **datap, ibase_array *ib_arra } /* }}} */ + +/* {{{ _php_ibase_blob_get */ +static int _php_ibase_blob_get(zval *return_value, ibase_blob_handle *ib_blob, unsigned long max_len TSRMLS_DC) +{ + if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/ + + ISC_STATUS stat; + char *bl_data; + unsigned long cur_len; + unsigned short seg_len; + + bl_data = emalloc(max_len + 1); + + for (cur_len = stat = 0; (stat == 0 || stat == isc_segment) && cur_len < max_len; cur_len += seg_len) { + + unsigned short chunk_size = (max_len-cur_len) > USHRT_MAX ? USHRT_MAX : (unsigned short)(max_len-cur_len); + + stat = isc_get_segment(IB_STATUS, &ib_blob->bl_handle, &seg_len, chunk_size, &bl_data[cur_len]); + } + + bl_data[cur_len] = '\0'; + if (IB_STATUS[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) { + efree(bl_data); + _php_ibase_error(TSRMLS_C); + return FAILURE; + } + RETVAL_STRINGL(bl_data, cur_len, 0); + } else { /* null blob */ + RETVAL_STRING("", 1); /* empty string */ + } + return SUCCESS; +} + +/* }}} */ + /* {{{ _php_ibase_fetch_hash() */ #define FETCH_ROW 2 @@ -2562,18 +2629,20 @@ static void _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type) break; case SQL_BLOB: if (flag & PHP_IBASE_FETCH_BLOBS) { /* fetch blob contents into hash */ - int stat; - isc_blob_handle bl_handle = NULL; - ISC_LONG max_len = 0, cur_len = 0; - char bl_items[1], *bl_data, bl_info[20], *p; - - if (isc_open_blob(IB_STATUS, &ib_result->link, &ib_result->trans, &bl_handle, (ISC_QUAD ISC_FAR *) var->sqldata)) { + + ibase_blob_handle blob_handle; + unsigned long max_len = 0; + char bl_items[] = {isc_info_blob_total_length}, bl_info[20], *p; + + blob_handle.bl_handle = NULL; + blob_handle.bl_qd = *(ISC_QUAD ISC_FAR *) var->sqldata; + + if (isc_open_blob(IB_STATUS, &ib_result->link, &ib_result->trans, &blob_handle.bl_handle, &blob_handle.bl_qd)) { _php_ibase_error(TSRMLS_C); RETURN_FALSE; } - bl_items[0] = isc_info_blob_total_length; - if (isc_blob_info(IB_STATUS, &bl_handle, sizeof(bl_items), bl_items, sizeof(bl_info), bl_info)) { + if (isc_blob_info(IB_STATUS, &blob_handle.bl_handle, sizeof(bl_items), bl_items, sizeof(bl_info), bl_info)) { _php_ibase_error(TSRMLS_C); RETURN_FALSE; } @@ -2582,61 +2651,39 @@ static void _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type) for (p = bl_info; *p != isc_info_end && p < bl_info + sizeof(bl_info);) { unsigned short item_len, item = *p++; - item_len = (short) isc_vax_integer(p, 2); + item_len = (unsigned short) isc_vax_integer(p, 2); p += 2; if (item == isc_info_blob_total_length) { max_len = isc_vax_integer(p, item_len); + break; } p += item_len; } - bl_data = emalloc(max_len+1); - - for (cur_len = stat = 0; stat == 0 && cur_len < max_len;) { - unsigned short seg_len; - unsigned short max_seg = (unsigned short) (max_len - cur_len > USHRT_MAX ? USHRT_MAX : max_len-cur_len); - - stat = isc_get_segment(IB_STATUS, &bl_handle, &seg_len, max_seg, &bl_data[cur_len]); - cur_len += seg_len; - if (cur_len > max_len) { /* never! */ - efree(bl_data); - _php_ibase_module_error("PHP module internal error"); - RETURN_FALSE; - } + if (max_len == 0) { + _php_ibase_module_error("Could not determine BLOB size (internal error)"); + RETURN_FALSE; } - if (IB_STATUS[0] && IB_STATUS[1] && (IB_STATUS[1] != isc_segstr_eof)) { - efree(bl_data); - _php_ibase_error(TSRMLS_C); + if (_php_ibase_blob_get(tmp, &blob_handle, max_len TSRMLS_CC) != SUCCESS) { RETURN_FALSE; } - bl_data[cur_len] = '\0'; - if (isc_close_blob(IB_STATUS, &bl_handle)) { - efree(bl_data); + + if (isc_close_blob(IB_STATUS, &blob_handle.bl_handle)) { _php_ibase_error(TSRMLS_C); - RETURN_FALSE; } - Z_TYPE_P(tmp) = IS_STRING; - Z_STRLEN_P(tmp) = cur_len; - Z_STRVAL_P(tmp) = estrndup(bl_data, cur_len); - efree(bl_data); } else { /* blob id only */ ISC_QUAD *bl_qd = (ISC_QUAD ISC_FAR *) var->sqldata; ibase_blob_handle *ib_blob_id; - ib_blob_id = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle)); + ib_blob_id = (ibase_blob_handle *) ecalloc(1, sizeof(ibase_blob_handle) + 1); ib_blob_id->link = ib_result->link; ib_blob_id->trans_handle = ib_result->trans; - ib_blob_id->bl_qd.gds_quad_high = bl_qd->gds_quad_high; - ib_blob_id->bl_qd.gds_quad_low = bl_qd->gds_quad_low; - ib_blob_id->bl_handle = NULL; + ib_blob_id->bl_qd = *bl_qd; - Z_TYPE_P(tmp) = IS_STRING; - Z_STRLEN_P(tmp) = sizeof(ibase_blob_handle); - Z_STRVAL_P(tmp) = estrndup((char *) ib_blob_id, sizeof(ibase_blob_handle)); - efree(ib_blob_id); + ZVAL_STRINGL(tmp, (char *) ib_blob_id, sizeof(ibase_blob_handle),0); } break; case SQL_ARRAY: { @@ -3339,7 +3386,6 @@ PHP_FUNCTION(ibase_blob_open) RETURN_LONG(zend_list_insert(ib_blob, le_blob)); } -/* }}} */ /* {{{ proto bool ibase_blob_add(int blob_id, string data) Add data into created blob */ @@ -3347,8 +3393,6 @@ PHP_FUNCTION(ibase_blob_add) { zval **blob_arg, **string_arg; ibase_blob_handle *ib_blob; - unsigned long put_cnt = 0, rem_cnt; - unsigned short chunk_size; RESET_ERRMSG; @@ -3359,20 +3403,9 @@ PHP_FUNCTION(ibase_blob_add) if (_php_ibase_get_blob_handle(blob_arg, &ib_blob TSRMLS_CC) != SUCCESS) { RETURN_FALSE; } - - convert_to_string_ex(string_arg); - - for (rem_cnt = Z_STRLEN_PP(string_arg); rem_cnt > 0; rem_cnt -= chunk_size) { - - chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt; - - if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, chunk_size, &Z_STRVAL_PP(string_arg)[put_cnt] )) { - _php_ibase_error(TSRMLS_C); - RETURN_FALSE; - } - put_cnt += chunk_size; + if (_php_ibase_blob_add(string_arg, ib_blob TSRMLS_CC) != SUCCESS) { + RETURN_FALSE; } - RETURN_TRUE; } /* }}} */ @@ -3381,9 +3414,6 @@ PHP_FUNCTION(ibase_blob_add) PHP_FUNCTION(ibase_blob_get) { zval **blob_arg, **len_arg; - ISC_STATUS stat; - char *bl_data; - unsigned long max_len, cur_len; ibase_blob_handle *ib_blob; RESET_ERRMSG; @@ -3392,38 +3422,13 @@ PHP_FUNCTION(ibase_blob_get) WRONG_PARAM_COUNT; } - convert_to_long_ex(len_arg); - max_len = Z_LVAL_PP(len_arg); - if (_php_ibase_get_blob_handle(blob_arg, &ib_blob TSRMLS_CC) != SUCCESS) { RETURN_FALSE; } - - if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/ - - bl_data = emalloc(max_len + 1); - - for (cur_len = stat = 0; stat == 0 || stat == isc_segment; ) { - unsigned short seg_len, chunk_size = (max_len-cur_len) > USHRT_MAX ? USHRT_MAX : (unsigned short)(max_len-cur_len); - stat = isc_get_segment(IB_STATUS, &ib_blob->bl_handle, &seg_len, chunk_size, &bl_data[cur_len]); - - cur_len += seg_len; - - if (cur_len == max_len) { - break; - } - } - - bl_data[cur_len] = '\0'; - if (IB_STATUS[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) { - efree(bl_data); - _php_ibase_error(TSRMLS_C); - RETURN_FALSE; - } - RETURN_STRINGL(bl_data, cur_len, 0); - } else { /* null blob */ - RETURN_STRING("", 1); /* empty string */ + convert_to_long_ex(len_arg); + if (_php_ibase_blob_get(return_value, ib_blob, Z_LVAL_PP(len_arg) TSRMLS_CC) != SUCCESS) { + RETURN_FALSE; } } /* }}} */ -- 2.40.0