Skip to content

Commit 18fa461

Browse files
author
Ard Biesheuvel
committed
More divide & conquer
1 parent f9c5fc6 commit 18fa461

File tree

8 files changed

+2733
-2640
lines changed

8 files changed

+2733
-2640
lines changed

ext/interbase/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ if test "$PHP_INTERBASE" != "no"; then
5353
PHP_ADD_LIBRARY_WITH_PATH($IBASE_LIBNAME, $IBASE_LIBDIR, INTERBASE_SHARED_LIBADD)
5454
PHP_ADD_INCLUDE($IBASE_INCDIR)
5555
AC_DEFINE(HAVE_IBASE,1,[ ])
56-
PHP_NEW_EXTENSION(interbase, interbase.c ibase_service.c ibase_events.c ibase_blobs.c, $ext_shared)
56+
PHP_NEW_EXTENSION(interbase, interbase.c ibase_query.c ibase_service.c ibase_events.c ibase_blobs.c, $ext_shared)
5757
PHP_SUBST(INTERBASE_SHARED_LIBADD)
5858
fi

ext/interbase/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (PHP_INTERBASE != "no" || PHP_INTERBASE5 != "no") {
99
if (CHECK_HEADER_ADD_INCLUDE("ibase.h", "CFLAGS_INTERBASE", PHP_PHP_BUILD + "\\interbase\\include;" + PHP_INTERBASE) &&
1010
(CHECK_LIB("fbclient_ms.lib", "interbase", PHP_PHP_BUILD + "\\interbase\\lib_ms;" + PHP_INTERBASE) ||
1111
CHECK_LIB("gds32_ms.lib", "interbase", PHP_PHP_BUILD + "\\interbase\\lib_ms;" + PHP_INTERBASE))) {
12-
EXTENSION("interbase", "interbase.c ibase_service.c ibase_events.c ibase_blobs.c");
12+
EXTENSION("interbase", "interbase.c ibase_query.c ibase_service.c ibase_events.c ibase_blobs.c");
1313
AC_DEFINE('HAVE_IBASE', 1, 'Have interbase library');
1414

1515
if (PHP_INTERBASE != "no") {

ext/interbase/ibase_blobs.c

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,30 @@
3232
#define BLOB_CLOSE 1
3333
#define BLOB_CANCEL 2
3434

35-
int _php_ibase_string_to_quad(char const *id, ISC_QUAD *qd) /* {{{ */
35+
static int le_blob;
36+
37+
static void _php_ibase_free_blob(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
38+
{
39+
ibase_blob *ib_blob = (ibase_blob *)rsrc->ptr;
40+
41+
if (ib_blob->bl_handle != NULL) { /* blob open*/
42+
if (isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)) {
43+
_php_ibase_module_error("You can lose data. Close any blob after reading from or "
44+
"writing to it. Use ibase_blob_close() before calling ibase_close()" TSRMLS_CC);
45+
}
46+
}
47+
efree(ib_blob);
48+
}
49+
/* }}} */
50+
51+
void php_ibase_blobs_minit(INIT_FUNC_ARGS) /* {{{ */
52+
{
53+
le_blob = zend_register_list_destructors_ex(_php_ibase_free_blob, NULL,
54+
"interbase blob", module_number);
55+
}
56+
/* }}} */
57+
58+
PHPAPI int _php_ibase_string_to_quad(char const *id, ISC_QUAD *qd) /* {{{ */
3659
{
3760
/* shortcut for most common case */
3861
if (sizeof(ISC_QUAD) == sizeof(ISC_UINT64)) {
@@ -49,13 +72,13 @@ int _php_ibase_string_to_quad(char const *id, ISC_QUAD *qd) /* {{{ */
4972
}
5073
/* }}} */
5174

52-
char *_php_ibase_quad_to_string(ISC_QUAD const qd) /* {{{ */
75+
PHPAPI char *_php_ibase_quad_to_string(ISC_QUAD const qd) /* {{{ */
5376
{
5477
char *result = (char *) emalloc(BLOB_ID_LEN+1);
5578

5679
/* shortcut for most common case */
5780
if (sizeof(ISC_QUAD) == sizeof(ISC_UINT64)) {
58-
sprintf(result, BLOB_ID_MASK, *(ISC_UINT64*)(void *) &qd);
81+
sprintf(result, BLOB_ID_MASK, *(ISC_UINT64*)(void *) &qd);
5982
} else {
6083
ISC_UINT64 res = ((ISC_UINT64) qd.gds_quad_high << 0x20) | qd.gds_quad_low;
6184
sprintf(result, BLOB_ID_MASK, res);
@@ -73,25 +96,25 @@ typedef struct { /* {{{ */
7396
/* }}} */
7497
} IBASE_BLOBINFO;
7598

76-
int _php_ibase_blob_get(zval *return_value, ibase_blob *ib_blob, unsigned long max_len TSRMLS_DC) /* {{{ */
99+
PHPAPI int _php_ibase_blob_get(zval *return_value, ibase_blob *ib_blob, unsigned long max_len TSRMLS_DC) /* {{{ */
77100
{
78101
if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/
79-
102+
80103
ISC_STATUS stat;
81104
char *bl_data;
82105
unsigned long cur_len;
83106
unsigned short seg_len;
84-
107+
85108
bl_data = emalloc(max_len + 1);
86-
109+
87110
for (cur_len = stat = 0; (stat == 0 || stat == isc_segment) && cur_len < max_len; cur_len += seg_len) {
88-
89-
unsigned short chunk_size = (max_len-cur_len) > USHRT_MAX ? USHRT_MAX
111+
112+
unsigned short chunk_size = (max_len-cur_len) > USHRT_MAX ? USHRT_MAX
90113
: (unsigned short)(max_len-cur_len);
91-
92-
stat = isc_get_segment(IB_STATUS, &ib_blob->bl_handle, &seg_len, chunk_size, &bl_data[cur_len]);
114+
115+
stat = isc_get_segment(IB_STATUS, &ib_blob->bl_handle, &seg_len, chunk_size, &bl_data[cur_len]);
93116
}
94-
117+
95118
bl_data[cur_len] = '\0';
96119
if (IB_STATUS[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) {
97120
efree(bl_data);
@@ -106,15 +129,15 @@ int _php_ibase_blob_get(zval *return_value, ibase_blob *ib_blob, unsigned long m
106129
}
107130
/* }}} */
108131

109-
int _php_ibase_blob_add(zval **string_arg, ibase_blob *ib_blob TSRMLS_DC) /* {{{ */
132+
PHPAPI int _php_ibase_blob_add(zval **string_arg, ibase_blob *ib_blob TSRMLS_DC) /* {{{ */
110133
{
111134
unsigned long put_cnt = 0, rem_cnt;
112135
unsigned short chunk_size;
113136

114137
convert_to_string_ex(string_arg);
115138

116139
for (rem_cnt = Z_STRLEN_PP(string_arg); rem_cnt > 0; rem_cnt -= chunk_size) {
117-
140+
118141
chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt;
119142

120143
if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, chunk_size, &Z_STRVAL_PP(string_arg)[put_cnt] )) {
@@ -124,7 +147,7 @@ int _php_ibase_blob_add(zval **string_arg, ibase_blob *ib_blob TSRMLS_DC) /* {{{
124147
put_cnt += chunk_size;
125148
}
126149
return SUCCESS;
127-
}
150+
}
128151
/* }}} */
129152

130153
static int _php_ibase_blob_info(isc_blob_handle bl_handle, IBASE_BLOBINFO *bl_info TSRMLS_DC) /* {{{ */
@@ -135,7 +158,7 @@ static int _php_ibase_blob_info(isc_blob_handle bl_handle, IBASE_BLOBINFO *bl_in
135158
isc_info_blob_total_length,
136159
isc_info_blob_type
137160
};
138-
161+
139162
char bl_inf[sizeof(long)*8], *p;
140163

141164
bl_info->max_segment = 0;
@@ -190,23 +213,23 @@ PHP_FUNCTION(ibase_blob_create)
190213
ibase_blob *ib_blob;
191214

192215
RESET_ERRMSG;
193-
216+
194217
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &link)) {
195218
RETURN_FALSE;
196219
}
197220

198221
PHP_IBASE_LINK_TRANS(link, ib_link, trans);
199-
222+
200223
ib_blob = (ibase_blob *) emalloc(sizeof(ibase_blob));
201224
ib_blob->bl_handle = NULL;
202225
ib_blob->type = BLOB_INPUT;
203-
226+
204227
if (isc_create_blob(IB_STATUS, &ib_link->handle, &trans->handle, &ib_blob->bl_handle, &ib_blob->bl_qd)) {
205228
_php_ibase_error(TSRMLS_C);
206229
efree(ib_blob);
207230
RETURN_FALSE;
208231
}
209-
232+
210233
ZEND_REGISTER_RESOURCE(return_value, ib_blob, le_blob);
211234
}
212235
/* }}} */
@@ -226,7 +249,7 @@ PHP_FUNCTION(ibase_blob_open)
226249

227250
switch (ZEND_NUM_ARGS()) {
228251
default:
229-
WRONG_PARAM_COUNT;
252+
WRONG_PARAM_COUNT;
230253
case 1:
231254
if (FAILURE == zend_parse_parameters(1 TSRMLS_CC, "s", &blob_id, &blob_id_len)) {
232255
RETURN_FALSE;
@@ -238,13 +261,13 @@ PHP_FUNCTION(ibase_blob_open)
238261
}
239262
break;
240263
}
241-
264+
242265
PHP_IBASE_LINK_TRANS(link, ib_link, trans);
243266

244267
ib_blob = (ibase_blob *) emalloc(sizeof(ibase_blob));
245268
ib_blob->bl_handle = NULL;
246269
ib_blob->type = BLOB_OUTPUT;
247-
270+
248271
do {
249272
if (! _php_ibase_string_to_quad(blob_id, &ib_blob->bl_qd)) {
250273
_php_ibase_module_error("String is not a BLOB ID" TSRMLS_CC);
@@ -256,7 +279,7 @@ PHP_FUNCTION(ibase_blob_open)
256279
_php_ibase_error(TSRMLS_C);
257280
break;
258281
}
259-
282+
260283
ZEND_REGISTER_RESOURCE(return_value, ib_blob, le_blob);
261284
return;
262285

@@ -327,13 +350,13 @@ static void _php_ibase_blob_end(INTERNAL_FUNCTION_PARAMETERS, int bl_end) /* {{{
327350
ibase_blob *ib_blob;
328351

329352
RESET_ERRMSG;
330-
353+
331354
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &blob_arg) == FAILURE) {
332355
WRONG_PARAM_COUNT;
333356
}
334357

335-
ZEND_FETCH_RESOURCE(ib_blob, ibase_blob *, blob_arg, -1, "Interbase blob", le_blob);
336-
358+
ZEND_FETCH_RESOURCE(ib_blob, ibase_blob *, blob_arg, -1, "Interbase blob", le_blob);
359+
337360
if (bl_end == BLOB_CLOSE) { /* return id here */
338361

339362
if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/
@@ -389,7 +412,7 @@ PHP_FUNCTION(ibase_blob_info)
389412

390413
switch (ZEND_NUM_ARGS()) {
391414
default:
392-
WRONG_PARAM_COUNT;
415+
WRONG_PARAM_COUNT;
393416
case 1:
394417
if (FAILURE == zend_parse_parameters(1 TSRMLS_CC, "s", &blob_id, &blob_id_len)) {
395418
RETURN_FALSE;
@@ -431,7 +454,7 @@ PHP_FUNCTION(ibase_blob_info)
431454
}
432455

433456
array_init(return_value);
434-
457+
435458
add_index_long(return_value, 0, bl_info.total_length);
436459
add_assoc_long(return_value, "length", bl_info.total_length);
437460

@@ -457,16 +480,16 @@ PHP_FUNCTION(ibase_blob_echo)
457480
int blob_id_len;
458481
zval *link = NULL;
459482
ibase_db_link *ib_link;
460-
ibase_trans *trans = NULL;
483+
ibase_trans *trans = NULL;
461484
ibase_blob ib_blob_id = { NULL, BLOB_OUTPUT };
462-
char bl_data[IBASE_BLOB_SEG];
485+
char bl_data[IBASE_BLOB_SEG];
463486
unsigned short seg_len;
464487

465488
RESET_ERRMSG;
466489

467490
switch (ZEND_NUM_ARGS()) {
468491
default:
469-
WRONG_PARAM_COUNT;
492+
WRONG_PARAM_COUNT;
470493
case 1:
471494
if (FAILURE == zend_parse_parameters(1 TSRMLS_CC, "s", &blob_id, &blob_id_len)) {
472495
RETURN_FALSE;
@@ -491,16 +514,16 @@ PHP_FUNCTION(ibase_blob_echo)
491514
&ib_blob_id.bl_qd)) {
492515
break;
493516
}
494-
517+
495518
while (!isc_get_segment(IB_STATUS, &ib_blob_id.bl_handle, &seg_len, sizeof(bl_data), bl_data)
496519
|| IB_STATUS[1] == isc_segment) {
497520
PHPWRITE(bl_data, seg_len);
498521
}
499-
522+
500523
if (IB_STATUS[0] && (IB_STATUS[1] != isc_segstr_eof)) {
501524
break;
502525
}
503-
526+
504527
if (isc_close_blob(IB_STATUS, &ib_blob_id.bl_handle)) {
505528
break;
506529
}
@@ -527,27 +550,27 @@ PHP_FUNCTION(ibase_blob_import)
527550

528551
RESET_ERRMSG;
529552

530-
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|r",
553+
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|r",
531554
(ZEND_NUM_ARGS()-1) ? &link : &file, &file)) {
532555
RETURN_FALSE;
533556
}
534-
557+
535558
PHP_IBASE_LINK_TRANS(link, ib_link, trans);
536559

537560
php_stream_from_zval(stream, &file);
538-
561+
539562
do {
540563
if (isc_create_blob(IB_STATUS, &ib_link->handle, &trans->handle, &ib_blob.bl_handle,
541564
&ib_blob.bl_qd)) {
542565
break;
543566
}
544-
567+
545568
for (size = 0; (b = php_stream_read(stream, bl_data, sizeof(bl_data))); size += b) {
546569
if (isc_put_segment(IB_STATUS, &ib_blob.bl_handle, b, bl_data)) {
547570
break;
548571
}
549572
}
550-
573+
551574
if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)) {
552575
break;
553576
}

0 commit comments

Comments
 (0)