1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2015 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Ard Biesheuvel <a.k.biesheuvel@its.tudelft.nl>              |
16   +----------------------------------------------------------------------+
17 */
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include "php.h"
24
25#if HAVE_IBASE
26
27#include "php_interbase.h"
28#include "php_ibase_includes.h"
29
30#define BLOB_CLOSE      1
31#define BLOB_CANCEL     2
32
33static int le_blob;
34
35static void _php_ibase_free_blob(zend_resource *rsrc) /* {{{ */
36{
37    ibase_blob *ib_blob = (ibase_blob *)rsrc->ptr;
38
39    if (ib_blob->bl_handle != NULL) { /* blob open*/
40        if (isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)) {
41            _php_ibase_module_error("You can lose data. Close any blob after reading from or "
42                "writing to it. Use ibase_blob_close() before calling ibase_close()");
43        }
44    }
45    efree(ib_blob);
46}
47/* }}} */
48
49void php_ibase_blobs_minit(INIT_FUNC_ARGS) /* {{{ */
50{
51    le_blob = zend_register_list_destructors_ex(_php_ibase_free_blob, NULL,
52        "interbase blob", module_number);
53}
54/* }}} */
55
56int _php_ibase_string_to_quad(char const *id, ISC_QUAD *qd) /* {{{ */
57{
58    /* shortcut for most common case */
59    if (sizeof(ISC_QUAD) == sizeof(ISC_UINT64)) {
60        return sscanf(id, BLOB_ID_MASK, (ISC_UINT64 *) qd);
61    } else {
62        ISC_UINT64 res;
63        if (sscanf(id, BLOB_ID_MASK, &res)) {
64            qd->gds_quad_high = (ISC_LONG) (res >> 0x20);
65            qd->gds_quad_low = (ISC_LONG) (res & 0xFFFFFFFF);
66            return 1;
67        }
68        return 0;
69    }
70}
71/* }}} */
72
73char *_php_ibase_quad_to_string(ISC_QUAD const qd) /* {{{ */
74{
75    char *result;
76
77    /* shortcut for most common case */
78    if (sizeof(ISC_QUAD) == sizeof(ISC_UINT64)) {
79        spprintf(&result, BLOB_ID_LEN+1, "0x%0*" LL_MASK "x", 16, *(ISC_UINT64*)(void *) &qd);
80    } else {
81        ISC_UINT64 res = ((ISC_UINT64) qd.gds_quad_high << 0x20) | qd.gds_quad_low;
82        spprintf(&result, BLOB_ID_LEN+1, "0x%0*" LL_MASK "x", 16, res);
83    }
84    return result;
85}
86/* }}} */
87
88typedef struct { /* {{{ */
89    ISC_LONG  max_segment;      /* Length of longest segment */
90    ISC_LONG  num_segments;     /* Total number of segments */
91    ISC_LONG  total_length;     /* Total length of blob */
92    int       bl_stream;        /* blob is stream ? */
93/* }}} */
94} IBASE_BLOBINFO;
95
96int _php_ibase_blob_get(zval *return_value, ibase_blob *ib_blob, unsigned long max_len) /* {{{ */
97{
98    if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/
99
100        ISC_STATUS stat;
101        char *bl_data;
102        unsigned long cur_len;
103        unsigned short seg_len;
104
105        bl_data = safe_emalloc(1, max_len, 1);
106
107        for (cur_len = stat = 0; (stat == 0 || stat == isc_segment) && cur_len < max_len; cur_len += seg_len) {
108
109            unsigned short chunk_size = (max_len-cur_len) > USHRT_MAX ? USHRT_MAX
110                : (unsigned short)(max_len-cur_len);
111
112            stat = isc_get_segment(IB_STATUS, &ib_blob->bl_handle, &seg_len, chunk_size, &bl_data[cur_len]);
113        }
114
115        bl_data[cur_len] = '\0';
116        if (IB_STATUS[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) {
117            efree(bl_data);
118            _php_ibase_error();
119            return FAILURE;
120        }
121        // TODO: avoid double reallocation???
122        RETVAL_STRINGL(bl_data, cur_len);
123        efree(bl_data);
124    } else { /* null blob */
125        RETVAL_EMPTY_STRING(); /* empty string */
126    }
127    return SUCCESS;
128}
129/* }}} */
130
131int _php_ibase_blob_add(zval *string_arg, ibase_blob *ib_blob) /* {{{ */
132{
133    unsigned long put_cnt = 0, rem_cnt;
134    unsigned short chunk_size;
135
136    convert_to_string_ex(string_arg);
137
138    for (rem_cnt = Z_STRLEN_P(string_arg); rem_cnt > 0; rem_cnt -= chunk_size)  {
139
140        chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt;
141
142        if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, chunk_size, &Z_STRVAL_P(string_arg)[put_cnt] )) {
143            _php_ibase_error();
144            return FAILURE;
145        }
146        put_cnt += chunk_size;
147    }
148    return SUCCESS;
149}
150/* }}} */
151
152static int _php_ibase_blob_info(isc_blob_handle bl_handle, IBASE_BLOBINFO *bl_info) /* {{{ */
153{
154    static char bl_items[] = {
155        isc_info_blob_num_segments,
156        isc_info_blob_max_segment,
157        isc_info_blob_total_length,
158        isc_info_blob_type
159    };
160
161    char bl_inf[sizeof(long)*8], *p;
162
163    bl_info->max_segment = 0;
164    bl_info->num_segments = 0;
165    bl_info->total_length = 0;
166    bl_info->bl_stream = 0;
167
168    if (isc_blob_info(IB_STATUS, &bl_handle, sizeof(bl_items), bl_items, sizeof(bl_inf), bl_inf)) {
169        _php_ibase_error();
170        return FAILURE;
171    }
172
173    for (p = bl_inf; *p != isc_info_end && p < bl_inf + sizeof(bl_inf);) {
174        unsigned short item_len;
175        int item = *p++;
176
177        item_len = (short) isc_vax_integer(p, 2);
178        p += 2;
179        switch (item) {
180            case isc_info_blob_num_segments:
181                bl_info->num_segments = isc_vax_integer(p, item_len);
182                break;
183            case isc_info_blob_max_segment:
184                bl_info->max_segment = isc_vax_integer(p, item_len);
185                break;
186            case isc_info_blob_total_length:
187                bl_info->total_length = isc_vax_integer(p, item_len);
188                break;
189            case isc_info_blob_type:
190                bl_info->bl_stream = isc_vax_integer(p, item_len);
191                break;
192            case isc_info_end:
193                break;
194            case isc_info_truncated:
195            case isc_info_error:  /* hmm. don't think so...*/
196                _php_ibase_module_error("PHP module internal error");
197                return FAILURE;
198        } /* switch */
199        p += item_len;
200    } /* for */
201    return SUCCESS;
202}
203/* }}} */
204
205/* {{{ proto resource ibase_blob_create([resource link_identifier])
206   Create blob for adding data */
207PHP_FUNCTION(ibase_blob_create)
208{
209    zval *link = NULL;
210    ibase_db_link *ib_link;
211    ibase_trans *trans = NULL;
212    ibase_blob *ib_blob;
213
214    RESET_ERRMSG;
215
216    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &link)) {
217        RETURN_FALSE;
218    }
219
220    PHP_IBASE_LINK_TRANS(link, ib_link, trans);
221
222    ib_blob = (ibase_blob *) emalloc(sizeof(ibase_blob));
223    ib_blob->bl_handle = NULL;
224    ib_blob->type = BLOB_INPUT;
225
226    if (isc_create_blob(IB_STATUS, &ib_link->handle, &trans->handle, &ib_blob->bl_handle, &ib_blob->bl_qd)) {
227        _php_ibase_error();
228        efree(ib_blob);
229        RETURN_FALSE;
230    }
231
232    RETVAL_RES(zend_register_resource(ib_blob, le_blob));
233    Z_TRY_ADDREF_P(return_value);
234}
235/* }}} */
236
237/* {{{ proto resource ibase_blob_open([ resource link_identifier, ] string blob_id)
238   Open blob for retrieving data parts */
239PHP_FUNCTION(ibase_blob_open)
240{
241    char *blob_id;
242    size_t blob_id_len;
243    zval *link = NULL;
244    ibase_db_link *ib_link;
245    ibase_trans *trans = NULL;
246    ibase_blob *ib_blob;
247
248    RESET_ERRMSG;
249
250    switch (ZEND_NUM_ARGS()) {
251        default:
252            WRONG_PARAM_COUNT;
253        case 1:
254            if (FAILURE == zend_parse_parameters(1, "s", &blob_id, &blob_id_len)) {
255                RETURN_FALSE;
256            }
257            break;
258        case 2:
259            if (FAILURE == zend_parse_parameters(2, "rs", &link, &blob_id, &blob_id_len)) {
260                RETURN_FALSE;
261            }
262            break;
263    }
264
265    PHP_IBASE_LINK_TRANS(link, ib_link, trans);
266
267    ib_blob = (ibase_blob *) emalloc(sizeof(ibase_blob));
268    ib_blob->bl_handle = NULL;
269    ib_blob->type = BLOB_OUTPUT;
270
271    do {
272        if (! _php_ibase_string_to_quad(blob_id, &ib_blob->bl_qd)) {
273            _php_ibase_module_error("String is not a BLOB ID");
274            break;
275        }
276
277        if (isc_open_blob(IB_STATUS, &ib_link->handle, &trans->handle, &ib_blob->bl_handle,
278                &ib_blob->bl_qd)) {
279            _php_ibase_error();
280            break;
281        }
282
283        RETVAL_RES(zend_register_resource(ib_blob, le_blob));
284        Z_TRY_ADDREF_P(return_value);
285        return;
286
287    } while (0);
288
289    efree(ib_blob);
290    RETURN_FALSE;
291}
292/* }}} */
293
294/* {{{ proto bool ibase_blob_add(resource blob_handle, string data)
295   Add data into created blob */
296PHP_FUNCTION(ibase_blob_add)
297{
298    zval *blob_arg, *string_arg;
299    ibase_blob *ib_blob;
300
301    RESET_ERRMSG;
302
303    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &blob_arg, &string_arg)) {
304        return;
305    }
306
307    ib_blob = (ibase_blob *)zend_fetch_resource_ex(blob_arg, "Interbase blob", le_blob);
308
309    if (ib_blob->type != BLOB_INPUT) {
310        _php_ibase_module_error("BLOB is not open for input");
311        RETURN_FALSE;
312    }
313
314    if (_php_ibase_blob_add(string_arg, ib_blob) != SUCCESS) {
315        RETURN_FALSE;
316    }
317}
318/* }}} */
319
320/* {{{ proto string ibase_blob_get(resource blob_handle, int len)
321   Get len bytes data from open blob */
322PHP_FUNCTION(ibase_blob_get)
323{
324    zval *blob_arg;
325    unsigned long len_arg;
326    ibase_blob *ib_blob;
327
328    RESET_ERRMSG;
329
330    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &blob_arg, &len_arg)) {
331        return;
332    }
333
334    ib_blob = (ibase_blob *)zend_fetch_resource_ex(blob_arg, "Interbase blob", le_blob);
335
336    if (ib_blob->type != BLOB_OUTPUT) {
337        _php_ibase_module_error("BLOB is not open for output");
338        RETURN_FALSE;
339    }
340
341    if (_php_ibase_blob_get(return_value, ib_blob, len_arg) != SUCCESS) {
342        RETURN_FALSE;
343    }
344}
345/* }}} */
346
347static void _php_ibase_blob_end(INTERNAL_FUNCTION_PARAMETERS, int bl_end) /* {{{ */
348{
349    zval *blob_arg;
350    ibase_blob *ib_blob;
351    char *s;
352
353    RESET_ERRMSG;
354
355    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &blob_arg)) {
356        return;
357    }
358
359    ib_blob = (ibase_blob *)zend_fetch_resource_ex(blob_arg, "Interbase blob", le_blob);
360
361    if (bl_end == BLOB_CLOSE) { /* return id here */
362
363        if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/
364            if (isc_close_blob(IB_STATUS, &ib_blob->bl_handle)) {
365                _php_ibase_error();
366                RETURN_FALSE;
367            }
368        }
369        ib_blob->bl_handle = NULL;
370
371        s = _php_ibase_quad_to_string(ib_blob->bl_qd);
372        // TODO: avoid double reallocation???
373        RETVAL_STRINGL(s, BLOB_ID_LEN);
374        efree(s);
375    } else { /* discard created blob */
376        if (isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)) {
377            _php_ibase_error();
378            RETURN_FALSE;
379        }
380        ib_blob->bl_handle = NULL;
381        RETVAL_TRUE;
382    }
383    zend_list_delete(Z_RES_P(blob_arg));
384}
385/* }}} */
386
387/* {{{ proto string ibase_blob_close(resource blob_handle)
388   Close blob */
389PHP_FUNCTION(ibase_blob_close)
390{
391    _php_ibase_blob_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, BLOB_CLOSE);
392}
393/* }}} */
394
395/* {{{ proto bool ibase_blob_cancel(resource blob_handle)
396   Cancel creating blob */
397PHP_FUNCTION(ibase_blob_cancel)
398{
399    _php_ibase_blob_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, BLOB_CANCEL);
400}
401/* }}} */
402
403/* {{{ proto array ibase_blob_info([ resource link_identifier, ] string blob_id)
404   Return blob length and other useful info */
405PHP_FUNCTION(ibase_blob_info)
406{
407    char *blob_id;
408    size_t blob_id_len;
409    zval *link = NULL;
410    ibase_db_link *ib_link;
411    ibase_trans *trans = NULL;
412    ibase_blob ib_blob = { NULL, BLOB_INPUT };
413    IBASE_BLOBINFO bl_info;
414
415    RESET_ERRMSG;
416
417    switch (ZEND_NUM_ARGS()) {
418        default:
419            WRONG_PARAM_COUNT;
420        case 1:
421            if (FAILURE == zend_parse_parameters(1, "s", &blob_id, &blob_id_len)) {
422                RETURN_FALSE;
423            }
424            break;
425        case 2:
426            if (FAILURE == zend_parse_parameters(2, "rs", &link, &blob_id, &blob_id_len)) {
427                RETURN_FALSE;
428            }
429            break;
430    }
431
432    PHP_IBASE_LINK_TRANS(link, ib_link, trans);
433
434    if (! _php_ibase_string_to_quad(blob_id, &ib_blob.bl_qd)) {
435        _php_ibase_module_error("Unrecognized BLOB ID");
436        RETURN_FALSE;
437    }
438
439    if (ib_blob.bl_qd.gds_quad_high || ib_blob.bl_qd.gds_quad_low) { /* not null ? */
440        if (isc_open_blob(IB_STATUS, &ib_link->handle, &trans->handle, &ib_blob.bl_handle,
441                &ib_blob.bl_qd)) {
442            _php_ibase_error();
443            RETURN_FALSE;
444        }
445
446        if (_php_ibase_blob_info(ib_blob.bl_handle, &bl_info)) {
447            RETURN_FALSE;
448        }
449        if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)) {
450            _php_ibase_error();
451            RETURN_FALSE;
452        }
453    } else { /* null blob, all values to zero */
454        bl_info.max_segment = 0;
455        bl_info.num_segments = 0;
456        bl_info.total_length = 0;
457        bl_info.bl_stream = 0;
458    }
459
460    array_init(return_value);
461
462    add_index_long(return_value, 0, bl_info.total_length);
463    add_assoc_long(return_value, "length", bl_info.total_length);
464
465    add_index_long(return_value, 1, bl_info.num_segments);
466    add_assoc_long(return_value, "numseg", bl_info.num_segments);
467
468    add_index_long(return_value, 2, bl_info.max_segment);
469    add_assoc_long(return_value, "maxseg", bl_info.max_segment);
470
471    add_index_bool(return_value, 3, bl_info.bl_stream);
472    add_assoc_bool(return_value, "stream", bl_info.bl_stream);
473
474    add_index_bool(return_value, 4, (!ib_blob.bl_qd.gds_quad_high && !ib_blob.bl_qd.gds_quad_low));
475    add_assoc_bool(return_value, "isnull", (!ib_blob.bl_qd.gds_quad_high && !ib_blob.bl_qd.gds_quad_low));
476}
477/* }}} */
478
479/* {{{ proto bool ibase_blob_echo([ resource link_identifier, ] string blob_id)
480   Output blob contents to browser */
481PHP_FUNCTION(ibase_blob_echo)
482{
483    char *blob_id;
484    size_t blob_id_len;
485    zval *link = NULL;
486    ibase_db_link *ib_link;
487    ibase_trans *trans = NULL;
488    ibase_blob ib_blob_id = { NULL, BLOB_OUTPUT  };
489    char bl_data[IBASE_BLOB_SEG];
490    unsigned short seg_len;
491
492    RESET_ERRMSG;
493
494    switch (ZEND_NUM_ARGS()) {
495        default:
496            WRONG_PARAM_COUNT;
497        case 1:
498            if (FAILURE == zend_parse_parameters(1, "s", &blob_id, &blob_id_len)) {
499                RETURN_FALSE;
500            }
501            break;
502        case 2:
503            if (FAILURE == zend_parse_parameters(2, "rs", &link, &blob_id, &blob_id_len)) {
504                RETURN_FALSE;
505            }
506            break;
507    }
508
509    PHP_IBASE_LINK_TRANS(link, ib_link, trans);
510
511    if (! _php_ibase_string_to_quad(blob_id, &ib_blob_id.bl_qd)) {
512        _php_ibase_module_error("Unrecognized BLOB ID");
513        RETURN_FALSE;
514    }
515
516    do {
517        if (isc_open_blob(IB_STATUS, &ib_link->handle, &trans->handle, &ib_blob_id.bl_handle,
518                &ib_blob_id.bl_qd)) {
519            break;
520        }
521
522        while (!isc_get_segment(IB_STATUS, &ib_blob_id.bl_handle, &seg_len, sizeof(bl_data), bl_data)
523                || IB_STATUS[1] == isc_segment) {
524            PHPWRITE(bl_data, seg_len);
525        }
526
527        if (IB_STATUS[0] && (IB_STATUS[1] != isc_segstr_eof)) {
528            break;
529        }
530
531        if (isc_close_blob(IB_STATUS, &ib_blob_id.bl_handle)) {
532            break;
533        }
534        RETURN_TRUE;
535    } while (0);
536
537    _php_ibase_error();
538    RETURN_FALSE;
539}
540/* }}} */
541
542/* {{{ proto string ibase_blob_import([ resource link_identifier, ] resource file)
543   Create blob, copy file in it, and close it */
544PHP_FUNCTION(ibase_blob_import)
545{
546    zval *link = NULL, *file;
547    int size;
548    unsigned short b;
549    ibase_blob ib_blob = { NULL, 0 };
550    ibase_db_link *ib_link;
551    ibase_trans *trans = NULL;
552    char bl_data[IBASE_BLOB_SEG];
553    php_stream *stream;
554    char *s;
555
556    RESET_ERRMSG;
557
558    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r|r",
559            (ZEND_NUM_ARGS()-1) ? &link : &file, &file)) {
560        RETURN_FALSE;
561    }
562
563    PHP_IBASE_LINK_TRANS(link, ib_link, trans);
564
565    php_stream_from_zval(stream, file);
566
567    do {
568        if (isc_create_blob(IB_STATUS, &ib_link->handle, &trans->handle, &ib_blob.bl_handle,
569                &ib_blob.bl_qd)) {
570            break;
571        }
572
573        for (size = 0; (b = php_stream_read(stream, bl_data, sizeof(bl_data))); size += b) {
574            if (isc_put_segment(IB_STATUS, &ib_blob.bl_handle, b, bl_data)) {
575                break;
576            }
577        }
578
579        if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)) {
580            break;
581        }
582        s = _php_ibase_quad_to_string(ib_blob.bl_qd);
583        // TODO: avoid double reallocation???
584        RETVAL_STRINGL(s, BLOB_ID_LEN);
585        efree(s);
586        return;
587    } while (0);
588
589    _php_ibase_error();
590    RETURN_FALSE;
591}
592/* }}} */
593
594#endif /* HAVE_IBASE */
595
596/*
597 * Local variables:
598 * tab-width: 4
599 * c-basic-offset: 4
600 * End:
601 * vim600: sw=4 ts=4 fdm=marker
602 * vim<600: sw=4 ts=4
603 */
604