forked from brett19/php-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcoding.c
70 lines (55 loc) · 1.66 KB
/
transcoding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "transcoding.h"
int pcbc_bytes_to_zval(bucket_object *obj, zval **zvalue, const void *bytes,
lcb_size_t nbytes, lcb_uint32_t flags, lcb_uint8_t datatype TSRMLS_DC) {
zval zbytes, zflags, zdatatype;
zval *zparams[] = { &zbytes, &zflags, &zdatatype };
if (nbytes == 0) {
MAKE_STD_ZVAL(*zvalue);
ZVAL_NULL(*zvalue);
return SUCCESS;
}
INIT_ZVAL(zbytes);
ZVAL_STRINGL(&zbytes, bytes, nbytes, 0);
INIT_ZVAL(zflags);
ZVAL_LONG(&zflags, flags);
INIT_ZVAL(zdatatype);
ZVAL_LONG(&zdatatype, datatype);
MAKE_STD_ZVAL(*zvalue);
if (call_user_function(CG(function_table), NULL, obj->decoder, *zvalue,
3, zparams TSRMLS_CC) != SUCCESS) {
return FAILURE;
}
return SUCCESS;
}
int pcbc_zval_to_bytes(bucket_object *obj, zval *value,
const void **bytes, lcb_size_t *nbytes, lcb_uint32_t *flags,
lcb_uint8_t *datatype TSRMLS_DC) {
zval zretval, **zpbytes, **zpflags, **zpdatatype;
zval *zparams[] = { value };
HashTable *retval;
if (call_user_function(CG(function_table), NULL, obj->encoder, &zretval,
1, zparams TSRMLS_CC) != SUCCESS) {
return FAILURE;
}
retval = Z_ARRVAL(zretval);
if (zend_hash_num_elements(retval) != 3) {
return FAILURE;
}
zend_hash_index_find(retval, 0, (void**)&zpbytes);
zend_hash_index_find(retval, 1, (void**)&zpflags);
zend_hash_index_find(retval, 2, (void**)&zpdatatype);
if (Z_TYPE_PP(zpbytes) != IS_STRING) {
return FAILURE;
}
if (Z_TYPE_PP(zpflags) != IS_LONG) {
return FAILURE;
}
if (Z_TYPE_PP(zpdatatype) != IS_LONG) {
return FAILURE;
}
*bytes = Z_STRVAL_PP(zpbytes);
*nbytes = Z_STRLEN_PP(zpbytes);
*flags = Z_LVAL_PP(zpflags);
*datatype = (lcb_uint8_t)Z_LVAL_PP(zpdatatype);
return SUCCESS;
}