32
32
#define BLOB_CLOSE 1
33
33
#define BLOB_CANCEL 2
34
34
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 ) /* {{{ */
36
59
{
37
60
/* shortcut for most common case */
38
61
if (sizeof (ISC_QUAD ) == sizeof (ISC_UINT64 )) {
@@ -49,13 +72,13 @@ int _php_ibase_string_to_quad(char const *id, ISC_QUAD *qd) /* {{{ */
49
72
}
50
73
/* }}} */
51
74
52
- char * _php_ibase_quad_to_string (ISC_QUAD const qd ) /* {{{ */
75
+ PHPAPI char * _php_ibase_quad_to_string (ISC_QUAD const qd ) /* {{{ */
53
76
{
54
77
char * result = (char * ) emalloc (BLOB_ID_LEN + 1 );
55
78
56
79
/* shortcut for most common case */
57
80
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 );
59
82
} else {
60
83
ISC_UINT64 res = ((ISC_UINT64 ) qd .gds_quad_high << 0x20 ) | qd .gds_quad_low ;
61
84
sprintf (result , BLOB_ID_MASK , res );
@@ -73,25 +96,25 @@ typedef struct { /* {{{ */
73
96
/* }}} */
74
97
} IBASE_BLOBINFO ;
75
98
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 ) /* {{{ */
77
100
{
78
101
if (ib_blob -> bl_qd .gds_quad_high || ib_blob -> bl_qd .gds_quad_low ) { /*not null ?*/
79
-
102
+
80
103
ISC_STATUS stat ;
81
104
char * bl_data ;
82
105
unsigned long cur_len ;
83
106
unsigned short seg_len ;
84
-
107
+
85
108
bl_data = emalloc (max_len + 1 );
86
-
109
+
87
110
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
90
113
: (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 ]);
93
116
}
94
-
117
+
95
118
bl_data [cur_len ] = '\0' ;
96
119
if (IB_STATUS [0 ] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment )) {
97
120
efree (bl_data );
@@ -106,15 +129,15 @@ int _php_ibase_blob_get(zval *return_value, ibase_blob *ib_blob, unsigned long m
106
129
}
107
130
/* }}} */
108
131
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 ) /* {{{ */
110
133
{
111
134
unsigned long put_cnt = 0 , rem_cnt ;
112
135
unsigned short chunk_size ;
113
136
114
137
convert_to_string_ex (string_arg );
115
138
116
139
for (rem_cnt = Z_STRLEN_PP (string_arg ); rem_cnt > 0 ; rem_cnt -= chunk_size ) {
117
-
140
+
118
141
chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short )rem_cnt ;
119
142
120
143
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) /* {{{
124
147
put_cnt += chunk_size ;
125
148
}
126
149
return SUCCESS ;
127
- }
150
+ }
128
151
/* }}} */
129
152
130
153
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
135
158
isc_info_blob_total_length ,
136
159
isc_info_blob_type
137
160
};
138
-
161
+
139
162
char bl_inf [sizeof (long )* 8 ], * p ;
140
163
141
164
bl_info -> max_segment = 0 ;
@@ -190,23 +213,23 @@ PHP_FUNCTION(ibase_blob_create)
190
213
ibase_blob * ib_blob ;
191
214
192
215
RESET_ERRMSG ;
193
-
216
+
194
217
if (FAILURE == zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "|r" , & link )) {
195
218
RETURN_FALSE ;
196
219
}
197
220
198
221
PHP_IBASE_LINK_TRANS (link , ib_link , trans );
199
-
222
+
200
223
ib_blob = (ibase_blob * ) emalloc (sizeof (ibase_blob ));
201
224
ib_blob -> bl_handle = NULL ;
202
225
ib_blob -> type = BLOB_INPUT ;
203
-
226
+
204
227
if (isc_create_blob (IB_STATUS , & ib_link -> handle , & trans -> handle , & ib_blob -> bl_handle , & ib_blob -> bl_qd )) {
205
228
_php_ibase_error (TSRMLS_C );
206
229
efree (ib_blob );
207
230
RETURN_FALSE ;
208
231
}
209
-
232
+
210
233
ZEND_REGISTER_RESOURCE (return_value , ib_blob , le_blob );
211
234
}
212
235
/* }}} */
@@ -226,7 +249,7 @@ PHP_FUNCTION(ibase_blob_open)
226
249
227
250
switch (ZEND_NUM_ARGS ()) {
228
251
default :
229
- WRONG_PARAM_COUNT ;
252
+ WRONG_PARAM_COUNT ;
230
253
case 1 :
231
254
if (FAILURE == zend_parse_parameters (1 TSRMLS_CC , "s" , & blob_id , & blob_id_len )) {
232
255
RETURN_FALSE ;
@@ -238,13 +261,13 @@ PHP_FUNCTION(ibase_blob_open)
238
261
}
239
262
break ;
240
263
}
241
-
264
+
242
265
PHP_IBASE_LINK_TRANS (link , ib_link , trans );
243
266
244
267
ib_blob = (ibase_blob * ) emalloc (sizeof (ibase_blob ));
245
268
ib_blob -> bl_handle = NULL ;
246
269
ib_blob -> type = BLOB_OUTPUT ;
247
-
270
+
248
271
do {
249
272
if (! _php_ibase_string_to_quad (blob_id , & ib_blob -> bl_qd )) {
250
273
_php_ibase_module_error ("String is not a BLOB ID" TSRMLS_CC );
@@ -256,7 +279,7 @@ PHP_FUNCTION(ibase_blob_open)
256
279
_php_ibase_error (TSRMLS_C );
257
280
break ;
258
281
}
259
-
282
+
260
283
ZEND_REGISTER_RESOURCE (return_value , ib_blob , le_blob );
261
284
return ;
262
285
@@ -327,13 +350,13 @@ static void _php_ibase_blob_end(INTERNAL_FUNCTION_PARAMETERS, int bl_end) /* {{{
327
350
ibase_blob * ib_blob ;
328
351
329
352
RESET_ERRMSG ;
330
-
353
+
331
354
if (ZEND_NUM_ARGS () != 1 || zend_get_parameters_ex (1 , & blob_arg ) == FAILURE ) {
332
355
WRONG_PARAM_COUNT ;
333
356
}
334
357
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
+
337
360
if (bl_end == BLOB_CLOSE ) { /* return id here */
338
361
339
362
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)
389
412
390
413
switch (ZEND_NUM_ARGS ()) {
391
414
default :
392
- WRONG_PARAM_COUNT ;
415
+ WRONG_PARAM_COUNT ;
393
416
case 1 :
394
417
if (FAILURE == zend_parse_parameters (1 TSRMLS_CC , "s" , & blob_id , & blob_id_len )) {
395
418
RETURN_FALSE ;
@@ -431,7 +454,7 @@ PHP_FUNCTION(ibase_blob_info)
431
454
}
432
455
433
456
array_init (return_value );
434
-
457
+
435
458
add_index_long (return_value , 0 , bl_info .total_length );
436
459
add_assoc_long (return_value , "length" , bl_info .total_length );
437
460
@@ -457,16 +480,16 @@ PHP_FUNCTION(ibase_blob_echo)
457
480
int blob_id_len ;
458
481
zval * link = NULL ;
459
482
ibase_db_link * ib_link ;
460
- ibase_trans * trans = NULL ;
483
+ ibase_trans * trans = NULL ;
461
484
ibase_blob ib_blob_id = { NULL , BLOB_OUTPUT };
462
- char bl_data [IBASE_BLOB_SEG ];
485
+ char bl_data [IBASE_BLOB_SEG ];
463
486
unsigned short seg_len ;
464
487
465
488
RESET_ERRMSG ;
466
489
467
490
switch (ZEND_NUM_ARGS ()) {
468
491
default :
469
- WRONG_PARAM_COUNT ;
492
+ WRONG_PARAM_COUNT ;
470
493
case 1 :
471
494
if (FAILURE == zend_parse_parameters (1 TSRMLS_CC , "s" , & blob_id , & blob_id_len )) {
472
495
RETURN_FALSE ;
@@ -491,16 +514,16 @@ PHP_FUNCTION(ibase_blob_echo)
491
514
& ib_blob_id .bl_qd )) {
492
515
break ;
493
516
}
494
-
517
+
495
518
while (!isc_get_segment (IB_STATUS , & ib_blob_id .bl_handle , & seg_len , sizeof (bl_data ), bl_data )
496
519
|| IB_STATUS [1 ] == isc_segment ) {
497
520
PHPWRITE (bl_data , seg_len );
498
521
}
499
-
522
+
500
523
if (IB_STATUS [0 ] && (IB_STATUS [1 ] != isc_segstr_eof )) {
501
524
break ;
502
525
}
503
-
526
+
504
527
if (isc_close_blob (IB_STATUS , & ib_blob_id .bl_handle )) {
505
528
break ;
506
529
}
@@ -527,27 +550,27 @@ PHP_FUNCTION(ibase_blob_import)
527
550
528
551
RESET_ERRMSG ;
529
552
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" ,
531
554
(ZEND_NUM_ARGS ()- 1 ) ? & link : & file , & file )) {
532
555
RETURN_FALSE ;
533
556
}
534
-
557
+
535
558
PHP_IBASE_LINK_TRANS (link , ib_link , trans );
536
559
537
560
php_stream_from_zval (stream , & file );
538
-
561
+
539
562
do {
540
563
if (isc_create_blob (IB_STATUS , & ib_link -> handle , & trans -> handle , & ib_blob .bl_handle ,
541
564
& ib_blob .bl_qd )) {
542
565
break ;
543
566
}
544
-
567
+
545
568
for (size = 0 ; (b = php_stream_read (stream , bl_data , sizeof (bl_data ))); size += b ) {
546
569
if (isc_put_segment (IB_STATUS , & ib_blob .bl_handle , b , bl_data )) {
547
570
break ;
548
571
}
549
572
}
550
-
573
+
551
574
if (isc_close_blob (IB_STATUS , & ib_blob .bl_handle )) {
552
575
break ;
553
576
}
0 commit comments