forked from brett19/php-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 40
/
transcoding.c
87 lines (70 loc) · 2.56 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Copyright 2016-2019 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "couchbase.h"
#define LOGARGS(lvl) LCB_LOG_##lvl, NULL, "pcbc/transcoding", __FILE__, __LINE__
int pcbc_decode_value(zval *decoder, zval *return_value, zval *bytes, uint32_t flags, uint8_t datatype)
{
int rv;
zval params[3];
ZVAL_UNDEF(¶ms[0]);
ZVAL_UNDEF(¶ms[1]);
ZVAL_UNDEF(¶ms[2]);
ZVAL_COPY(¶ms[0], bytes);
ZVAL_LONG(¶ms[1], flags);
ZVAL_LONG(¶ms[2], datatype);
rv = call_user_function(CG(function_table), NULL, decoder, return_value, 3, params);
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(¶ms[1]);
zval_ptr_dtor(¶ms[2]);
return rv;
}
int pcbc_encode_value(zval *encoder, zval *value, void **bytes, lcb_size_t *nbytes, lcb_uint32_t *flags,
uint8_t *datatype)
{
zval retval;
int rv;
ZVAL_UNDEF(&retval);
ZVAL_NULL(&retval);
rv = call_user_function(CG(function_table), NULL, encoder, &retval, 1, value);
if (rv != SUCCESS || Z_TYPE_P(&retval) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL(retval)) != 3) {
zval_ptr_dtor(&retval);
return FAILURE;
}
{
zval *zbytes, *zflags, *zdatatype;
zbytes = zend_hash_index_find(Z_ARRVAL(retval), 0);
zflags = zend_hash_index_find(Z_ARRVAL(retval), 1);
zdatatype = zend_hash_index_find(Z_ARRVAL(retval), 2);
if (!zbytes || Z_TYPE_P(zbytes) != IS_STRING) {
zval_ptr_dtor(&retval);
return FAILURE;
}
if (!zflags || Z_TYPE_P(zflags) != IS_LONG) {
zval_ptr_dtor(&retval);
return FAILURE;
}
if (!zdatatype || Z_TYPE_P(zdatatype) != IS_LONG) {
zval_ptr_dtor(&retval);
return FAILURE;
}
*nbytes = Z_STRLEN_P(zbytes);
*bytes = estrndup(Z_STRVAL_P(zbytes), *nbytes);
*flags = (uint32_t)Z_LVAL_P(zflags);
*datatype = (uint8_t)Z_LVAL_P(zdatatype);
}
zval_ptr_dtor(&retval);
return SUCCESS;
}