This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
forked from couchbase/php-ext-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ht.c
367 lines (298 loc) · 8.06 KB
/
ht.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright 2012 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. |
+----------------------------------------------------------------------+
*/
/* Various utility functions, e.g. wrappers around zend_hash_* functions that
* are annoying to use in their native forms.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "internal.h"
/**
* Checks that we're passed an array. If not, it's a fatal error as there's
* likely something wrong in our code.
*/
#ifdef WIN32
#define __func__ __FUNCTION__
#endif
#define ISARRAY_SANITY(zv) \
if (IS_ARRAY != Z_TYPE_P(zv)) { \
php_error(E_ERROR, "%s given non-array zval in php "\
"couchbase extension", __func__); \
}
#define HK_STRING(hk, s, i) \
s = (hk)->key; i = (hk)->key_len + 1;
PHP_COUCHBASE_LOCAL
void pcbc_ht_key_create(const char *key, int len, pcbc_ht_key *ki)
{
memset(ki, 0, sizeof(*ki));
assert(len);
if (len == -1) {
len = strlen(key);
assert(len);
ki->_allocated = 0;
ki->key_len = len;
ki->key = key;
return;
}
ki->key = emalloc(len + 1);
memcpy((char *)ki->key, key, len);
((char *)ki->key)[len] = '\0';
ki->key_len = len;
ki->_allocated = 1;
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_key_cleanup(pcbc_ht_key *info)
{
if (info->key != NULL && info->_allocated) {
efree((void *)info->key);
}
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_key_free(pcbc_ht_key *info)
{
if (!info) {
return;
}
pcbc_ht_key_cleanup(info);
efree(info);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_entry_free(pcbc_ht_entry *pair)
{
pcbc_ht_key_free(pair->key_info);
efree(pair);
}
PHP_COUCHBASE_LOCAL
pcbc_ht_key *pcbc_ht_iter_key(zval *assoc)
{
char *curr_key = NULL;
unsigned int curr_key_len;
int curr_key_type;
unsigned long curr_idx;
pcbc_ht_key *curr_key_info = NULL;
ISARRAY_SANITY(assoc);
curr_key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(assoc),
(char **)&curr_key, &curr_key_len, &curr_idx, 1, NULL);
switch (curr_key_type) {
case HASH_KEY_IS_LONG:
curr_key_len = curr_idx;
break;
case HASH_KEY_IS_STRING:
/**
* zend gives us nul-terminated key strings,
* and for consistency's sake we don't want that
*/
curr_key_len--;
break;
default: /* something went badly wrong */
return NULL;
}
curr_key_info = (pcbc_ht_key *)emalloc(sizeof(pcbc_ht_key));
if (curr_key_info == NULL) {
php_error(E_ERROR, "Unable to allocate memory for a key info struct!");
}
curr_key_info->key = curr_key;
curr_key_info->key_type = curr_key_type;
curr_key_info->key_len = curr_key_len;
curr_key_info->_allocated = 1;
return curr_key_info;
}
PHP_COUCHBASE_LOCAL
zval *pcbc_ht_iter_value(zval *assoc)
{
zval **curr_data = NULL;
ISARRAY_SANITY(assoc);
zend_hash_get_current_data(Z_ARRVAL_P(assoc), (void **)&curr_data);
return *curr_data;
}
PHP_COUCHBASE_LOCAL
pcbc_ht_entry *pcbc_ht_iter_entry(zval *assoc)
{
pcbc_ht_entry *curr_entry;
ISARRAY_SANITY(assoc);
curr_entry = (pcbc_ht_entry *)emalloc(sizeof(pcbc_ht_entry));
if (curr_entry == NULL) {
php_error(E_ERROR,
"Unable to allocate memory for a key-value pair struct!");
}
curr_entry->key_info = pcbc_ht_iter_key(assoc);
curr_entry->data = pcbc_ht_iter_value(assoc);
return curr_entry;
}
PHP_COUCHBASE_LOCAL
unsigned long pcbc_ht_len(zval *assoc)
{
ISARRAY_SANITY(assoc);
return zend_hash_num_elements(Z_ARRVAL_P(assoc));
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_iter_init(zval *assoc)
{
ISARRAY_SANITY(assoc);
zend_hash_internal_pointer_reset(Z_ARRVAL_P(assoc));
}
PHP_COUCHBASE_LOCAL
int pcbc_ht_iter_remaining(zval *assoc)
{
ISARRAY_SANITY(assoc);
return ((zend_hash_has_more_elements(Z_ARRVAL_P(assoc)) == SUCCESS)
? 1 : 0);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_iter_next(zval *assoc)
{
ISARRAY_SANITY(assoc);
zend_hash_move_forward(Z_ARRVAL_P(assoc));
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_del(zval *assoc, const char *key, unsigned int key_len)
{
pcbc_ht_key hk = { NULL };
ISARRAY_SANITY(assoc);
pcbc_ht_key_create(key, key_len, &hk);
zend_hash_del(Z_ARRVAL_P(assoc), hk.key, hk.key_len + 1);
pcbc_ht_key_cleanup(&hk);
}
PHP_COUCHBASE_LOCAL
zval *pcbc_ht_hkfind(zval *assoc, pcbc_ht_key *hk)
{
zval **ppdata = NULL;
const char *key;
int nkey;
ISARRAY_SANITY(assoc);
HK_STRING(hk, key, nkey);
assert(nkey);
if (zend_hash_find(Z_ARRVAL_P(assoc),
key, nkey, (void **)&ppdata) != SUCCESS) {
return NULL;
}
return *ppdata;
}
PHP_COUCHBASE_LOCAL
zval *pcbc_ht_find(zval *assoc, const char *key, int key_len)
{
zval *ret;
pcbc_ht_key hk = { NULL };
pcbc_ht_key_create(key, key_len, &hk);
ret = pcbc_ht_hkfind(assoc, &hk);
pcbc_ht_key_cleanup(&hk);
return ret;
}
PHP_COUCHBASE_LOCAL
zval *pcbc_ht_ifind(zval *assoc, unsigned long idx)
{
zval *data;
ISARRAY_SANITY(assoc);
if (!zend_hash_index_exists(Z_ARRVAL_P(assoc), idx)) {
return NULL;
}
if (zend_hash_index_find(Z_ARRVAL_P(assoc),
idx, (void **)&data) != SUCCESS) {
return NULL;
}
return data;
}
PHP_COUCHBASE_LOCAL
int pcbc_ht_exists(zval *assoc, const char *key, int key_len)
{
pcbc_ht_key hk = { NULL };
const char *zh_key;
int zh_nkey;
int ret;
ISARRAY_SANITY(assoc);
pcbc_ht_key_create(key, key_len, &hk);
HK_STRING(&hk, zh_key, zh_nkey);
ret = zend_hash_exists(Z_ARRVAL_P(assoc), zh_key, zh_nkey);
pcbc_ht_key_cleanup(&hk);
return ret;
}
PHP_COUCHBASE_LOCAL
int pcbc_ht_iexists(zval *assoc, unsigned long idx)
{
if (IS_ARRAY != Z_TYPE_P(assoc)) {
php_error(E_RECOVERABLE_ERROR,
"assoc_idx_exists given non-array zval, in couchbase php-ext");
}
return ((zend_hash_index_exists(Z_ARRVAL_P(assoc), idx)) ? 1 : 0);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_hkstores(zval *assoc, pcbc_ht_key *hk,
const char *value, int nvalue)
{
const char *zh_key;
int zh_nkey;
ISARRAY_SANITY(assoc);
if (nvalue == -1) {
nvalue = strlen(value);
}
HK_STRING(hk, zh_key, zh_nkey);
/* value is duplicated, so it's sane to cast to char */
add_assoc_stringl_ex(assoc, zh_key, zh_nkey, (char *)value, nvalue, 1);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_stores(zval *assoc,
const char *key, int nkey, const char *value, int nvalue)
{
pcbc_ht_key hk;
pcbc_ht_key_create(key, nkey, &hk);
pcbc_ht_hkstores(assoc, &hk, value, nvalue);
pcbc_ht_key_cleanup(&hk);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_hkstoreb(zval *assoc, pcbc_ht_key *hk, zend_bool value)
{
const char *zh_key;
int zh_nkey;
ISARRAY_SANITY(assoc);
HK_STRING(hk, zh_key, zh_nkey);
add_assoc_bool_ex(assoc, zh_key, zh_nkey, value);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_storeb(zval *assoc, const char *key, int nkey, zend_bool value)
{
pcbc_ht_key hk;
pcbc_ht_key_create(key, nkey, &hk);
pcbc_ht_hkstoreb(assoc, &hk, value);
pcbc_ht_key_cleanup(&hk);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_hkstorez(zval *assoc, pcbc_ht_key *hk, zval *value)
{
const char *zh_key;
int zh_nkey;
ISARRAY_SANITY(assoc);
HK_STRING(hk, zh_key, zh_nkey);
add_assoc_zval_ex(assoc, zh_key, zh_nkey, value);
}
PHP_COUCHBASE_LOCAL
void pcbc_ht_dispose(zval *assoc)
{
if (IS_ARRAY != Z_TYPE_P(assoc)) {
php_error(E_RECOVERABLE_ERROR,
"assoc_destroy given non-array zval, in couchbase php-ext");
}
zval_ptr_dtor(&assoc);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/