OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // https://developers.google.com/protocol-buffers/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
9 // * Redistributions of source code must retain the above copyright | |
10 // notice, this list of conditions and the following disclaimer. | |
11 // * Redistributions in binary form must reproduce the above | |
12 // copyright notice, this list of conditions and the following disclaimer | |
13 // in the documentation and/or other materials provided with the | |
14 // distribution. | |
15 // * Neither the name of Google Inc. nor the names of its | |
16 // contributors may be used to endorse or promote products derived from | |
17 // this software without specific prior written permission. | |
18 // | |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 #include <ext/spl/spl_iterators.h> | |
32 #include <Zend/zend_API.h> | |
33 #include <Zend/zend_interfaces.h> | |
34 | |
35 #include "protobuf.h" | |
36 #include "utf8.h" | |
37 | |
38 ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetGet, 0, 0, 1) | |
39 ZEND_ARG_INFO(0, index) | |
40 ZEND_END_ARG_INFO() | |
41 | |
42 ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetSet, 0, 0, 2) | |
43 ZEND_ARG_INFO(0, index) | |
44 ZEND_ARG_INFO(0, newval) | |
45 ZEND_END_ARG_INFO() | |
46 | |
47 ZEND_BEGIN_ARG_INFO(arginfo_void, 0) | |
48 ZEND_END_ARG_INFO() | |
49 | |
50 // Utilities | |
51 | |
52 void* upb_value_memory(upb_value* v) { | |
53 return (void*)(&v->val); | |
54 } | |
55 | |
56 // ----------------------------------------------------------------------------- | |
57 // Basic map operations on top of upb's strtable. | |
58 // | |
59 // Note that we roll our own `Map` container here because, as for | |
60 // `RepeatedField`, we want a strongly-typed container. This is so that any user | |
61 // errors due to incorrect map key or value types are raised as close as | |
62 // possible to the error site, rather than at some deferred point (e.g., | |
63 // serialization). | |
64 // | |
65 // We build our `Map` on top of upb_strtable so that we're able to take | |
66 // advantage of the native_slot storage abstraction, as RepeatedField does. | |
67 // (This is not quite a perfect mapping -- see the key conversions below -- but | |
68 // gives us full support and error-checking for all value types for free.) | |
69 // ----------------------------------------------------------------------------- | |
70 | |
71 // Map values are stored using the native_slot abstraction (as with repeated | |
72 // field values), but keys are a bit special. Since we use a strtable, we need | |
73 // to store keys as sequences of bytes such that equality of those bytes maps | |
74 // one-to-one to equality of keys. We store strings directly (i.e., they map to | |
75 // their own bytes) and integers as native integers (using the native_slot | |
76 // abstraction). | |
77 | |
78 // Note that there is another tradeoff here in keeping string keys as native | |
79 // strings rather than PHP strings: traversing the Map requires conversion to | |
80 // PHP string values on every traversal, potentially creating more garbage. We | |
81 // should consider ways to cache a PHP version of the key if this becomes an | |
82 // issue later. | |
83 | |
84 // Forms a key to use with the underlying strtable from a PHP key value. |buf| | |
85 // must point to TABLE_KEY_BUF_LENGTH bytes of temporary space, used to | |
86 // construct a key byte sequence if needed. |out_key| and |out_length| provide | |
87 // the resulting key data/length. | |
88 #define TABLE_KEY_BUF_LENGTH 8 // sizeof(uint64_t) | |
89 static bool table_key(Map* self, zval* key, | |
90 char* buf, | |
91 const char** out_key, | |
92 size_t* out_length TSRMLS_DC) { | |
93 switch (self->key_type) { | |
94 case UPB_TYPE_STRING: | |
95 if (!protobuf_convert_to_string(key)) { | |
96 return false; | |
97 } | |
98 if (!is_structurally_valid_utf8(Z_STRVAL_P(key), Z_STRLEN_P(key))) { | |
99 zend_error(E_USER_ERROR, "Given key is not UTF8 encoded."); | |
100 return false; | |
101 } | |
102 *out_key = Z_STRVAL_P(key); | |
103 *out_length = Z_STRLEN_P(key); | |
104 break; | |
105 | |
106 #define CASE_TYPE(upb_type, type, c_type, php_type) \ | |
107 case UPB_TYPE_##upb_type: { \ | |
108 c_type type##_value; \ | |
109 if (!protobuf_convert_to_##type(key, &type##_value)) { \ | |
110 return false; \ | |
111 } \ | |
112 native_slot_set(self->key_type, NULL, buf, key TSRMLS_CC); \ | |
113 *out_key = buf; \ | |
114 *out_length = native_slot_size(self->key_type); \ | |
115 break; \ | |
116 } | |
117 CASE_TYPE(BOOL, bool, int8_t, BOOL) | |
118 CASE_TYPE(INT32, int32, int32_t, LONG) | |
119 CASE_TYPE(INT64, int64, int64_t, LONG) | |
120 CASE_TYPE(UINT32, uint32, uint32_t, LONG) | |
121 CASE_TYPE(UINT64, uint64, uint64_t, LONG) | |
122 | |
123 #undef CASE_TYPE | |
124 | |
125 default: | |
126 // Map constructor should not allow a Map with another key type to be | |
127 // constructed. | |
128 assert(false); | |
129 break; | |
130 } | |
131 | |
132 return true; | |
133 } | |
134 | |
135 // ----------------------------------------------------------------------------- | |
136 // MapField methods | |
137 // ----------------------------------------------------------------------------- | |
138 | |
139 static zend_function_entry map_field_methods[] = { | |
140 PHP_ME(MapField, __construct, NULL, ZEND_ACC_PUBLIC) | |
141 PHP_ME(MapField, offsetExists, arginfo_offsetGet, ZEND_ACC_PUBLIC) | |
142 PHP_ME(MapField, offsetGet, arginfo_offsetGet, ZEND_ACC_PUBLIC) | |
143 PHP_ME(MapField, offsetSet, arginfo_offsetSet, ZEND_ACC_PUBLIC) | |
144 PHP_ME(MapField, offsetUnset, arginfo_offsetGet, ZEND_ACC_PUBLIC) | |
145 PHP_ME(MapField, count, arginfo_void, ZEND_ACC_PUBLIC) | |
146 ZEND_FE_END | |
147 }; | |
148 | |
149 // ----------------------------------------------------------------------------- | |
150 // MapField creation/desctruction | |
151 // ----------------------------------------------------------------------------- | |
152 | |
153 zend_class_entry* map_field_type; | |
154 zend_object_handlers* map_field_handlers; | |
155 | |
156 static void map_begin_internal(Map *map, MapIter *iter) { | |
157 iter->self = map; | |
158 upb_strtable_begin(&iter->it, &map->table); | |
159 } | |
160 | |
161 static HashTable *map_field_get_gc(zval *object, zval ***table, | |
162 int *n TSRMLS_DC) { | |
163 // TODO(teboring): Unfortunately, zend engine does not support garbage | |
164 // collection for custom array. We have to use zend engine's native array | |
165 // instead. | |
166 *table = NULL; | |
167 *n = 0; | |
168 return NULL; | |
169 } | |
170 | |
171 void map_field_init(TSRMLS_D) { | |
172 zend_class_entry class_type; | |
173 const char* class_name = "Google\\Protobuf\\Internal\\MapField"; | |
174 INIT_CLASS_ENTRY_EX(class_type, class_name, strlen(class_name), | |
175 map_field_methods); | |
176 | |
177 map_field_type = zend_register_internal_class(&class_type TSRMLS_CC); | |
178 map_field_type->create_object = map_field_create; | |
179 | |
180 zend_class_implements(map_field_type TSRMLS_CC, 2, spl_ce_ArrayAccess, | |
181 spl_ce_Countable); | |
182 | |
183 map_field_handlers = PEMALLOC(zend_object_handlers); | |
184 memcpy(map_field_handlers, zend_get_std_object_handlers(), | |
185 sizeof(zend_object_handlers)); | |
186 map_field_handlers->get_gc = map_field_get_gc; | |
187 } | |
188 | |
189 zend_object_value map_field_create(zend_class_entry *ce TSRMLS_DC) { | |
190 zend_object_value retval = {0}; | |
191 Map *intern; | |
192 | |
193 intern = emalloc(sizeof(Map)); | |
194 memset(intern, 0, sizeof(Map)); | |
195 | |
196 zend_object_std_init(&intern->std, ce TSRMLS_CC); | |
197 object_properties_init(&intern->std, ce); | |
198 | |
199 // Table value type is always UINT64: this ensures enough space to store the | |
200 // native_slot value. | |
201 if (!upb_strtable_init(&intern->table, UPB_CTYPE_UINT64)) { | |
202 zend_error(E_USER_ERROR, "Could not allocate table."); | |
203 } | |
204 | |
205 retval.handle = zend_objects_store_put( | |
206 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, | |
207 (zend_objects_free_object_storage_t)map_field_free, NULL TSRMLS_CC); | |
208 retval.handlers = map_field_handlers; | |
209 | |
210 return retval; | |
211 } | |
212 | |
213 void map_field_free(void *object TSRMLS_DC) { | |
214 Map *map = (Map *)object; | |
215 | |
216 switch (map->value_type) { | |
217 case UPB_TYPE_MESSAGE: | |
218 case UPB_TYPE_STRING: | |
219 case UPB_TYPE_BYTES: { | |
220 MapIter it; | |
221 int len; | |
222 for (map_begin_internal(map, &it); !map_done(&it); map_next(&it)) { | |
223 upb_value value = map_iter_value(&it, &len); | |
224 void *mem = upb_value_memory(&value); | |
225 zval_ptr_dtor(mem); | |
226 } | |
227 break; | |
228 } | |
229 default: | |
230 break; | |
231 } | |
232 | |
233 upb_strtable_uninit(&map->table); | |
234 zend_object_std_dtor(&map->std TSRMLS_CC); | |
235 efree(object); | |
236 } | |
237 | |
238 void map_field_create_with_type(zend_class_entry *ce, const upb_fielddef *field, | |
239 zval **map_field TSRMLS_DC) { | |
240 MAKE_STD_ZVAL(*map_field); | |
241 Z_TYPE_PP(map_field) = IS_OBJECT; | |
242 Z_OBJVAL_PP(map_field) = | |
243 map_field_type->create_object(map_field_type TSRMLS_CC); | |
244 | |
245 Map* intern = | |
246 (Map*)zend_object_store_get_object(*map_field TSRMLS_CC); | |
247 | |
248 const upb_fielddef *key_field = map_field_key(field); | |
249 const upb_fielddef *value_field = map_field_value(field); | |
250 intern->key_type = upb_fielddef_type(key_field); | |
251 intern->value_type = upb_fielddef_type(value_field); | |
252 intern->msg_ce = field_type_class(value_field TSRMLS_CC); | |
253 } | |
254 | |
255 static void map_field_free_element(void *object) { | |
256 } | |
257 | |
258 // ----------------------------------------------------------------------------- | |
259 // MapField Handlers | |
260 // ----------------------------------------------------------------------------- | |
261 | |
262 static bool map_field_read_dimension(zval *object, zval *key, int type, | |
263 zval **retval TSRMLS_DC) { | |
264 Map *intern = | |
265 (Map *)zend_object_store_get_object(object TSRMLS_CC); | |
266 | |
267 char keybuf[TABLE_KEY_BUF_LENGTH]; | |
268 const char* keyval = NULL; | |
269 size_t length = 0; | |
270 upb_value v; | |
271 #ifndef NDEBUG | |
272 v.ctype = UPB_CTYPE_UINT64; | |
273 #endif | |
274 if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) { | |
275 return false; | |
276 } | |
277 | |
278 if (upb_strtable_lookup2(&intern->table, keyval, length, &v)) { | |
279 void* mem = upb_value_memory(&v); | |
280 native_slot_get(intern->value_type, mem, retval TSRMLS_CC); | |
281 return true; | |
282 } else { | |
283 zend_error(E_USER_ERROR, "Given key doesn't exist."); | |
284 return false; | |
285 } | |
286 } | |
287 | |
288 bool map_index_set(Map *intern, const char* keyval, int length, upb_value v) { | |
289 // Replace any existing value by issuing a 'remove' operation first. | |
290 upb_strtable_remove2(&intern->table, keyval, length, NULL); | |
291 if (!upb_strtable_insert2(&intern->table, keyval, length, v)) { | |
292 zend_error(E_USER_ERROR, "Could not insert into table"); | |
293 return false; | |
294 } | |
295 return true; | |
296 } | |
297 | |
298 static bool map_field_write_dimension(zval *object, zval *key, | |
299 zval *value TSRMLS_DC) { | |
300 Map *intern = (Map *)zend_object_store_get_object(object TSRMLS_CC); | |
301 | |
302 char keybuf[TABLE_KEY_BUF_LENGTH]; | |
303 const char* keyval = NULL; | |
304 size_t length = 0; | |
305 upb_value v; | |
306 void* mem; | |
307 if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) { | |
308 return false; | |
309 } | |
310 | |
311 mem = upb_value_memory(&v); | |
312 memset(mem, 0, native_slot_size(intern->value_type)); | |
313 if (!native_slot_set(intern->value_type, intern->msg_ce, mem, value | |
314 TSRMLS_CC)) { | |
315 return false; | |
316 } | |
317 #ifndef NDEBUG | |
318 v.ctype = UPB_CTYPE_UINT64; | |
319 #endif | |
320 | |
321 // Replace any existing value by issuing a 'remove' operation first. | |
322 upb_strtable_remove2(&intern->table, keyval, length, NULL); | |
323 if (!upb_strtable_insert2(&intern->table, keyval, length, v)) { | |
324 zend_error(E_USER_ERROR, "Could not insert into table"); | |
325 return false; | |
326 } | |
327 | |
328 return true; | |
329 } | |
330 | |
331 static bool map_field_unset_dimension(zval *object, zval *key TSRMLS_DC) { | |
332 Map *intern = (Map *)zend_object_store_get_object(object TSRMLS_CC); | |
333 | |
334 char keybuf[TABLE_KEY_BUF_LENGTH]; | |
335 const char* keyval = NULL; | |
336 size_t length = 0; | |
337 upb_value v; | |
338 if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) { | |
339 return false; | |
340 } | |
341 #ifndef NDEBUG | |
342 v.ctype = UPB_CTYPE_UINT64; | |
343 #endif | |
344 | |
345 upb_strtable_remove2(&intern->table, keyval, length, &v); | |
346 | |
347 return true; | |
348 } | |
349 | |
350 // ----------------------------------------------------------------------------- | |
351 // PHP MapField Methods | |
352 // ----------------------------------------------------------------------------- | |
353 | |
354 PHP_METHOD(MapField, __construct) { | |
355 long key_type, value_type; | |
356 zend_class_entry* klass = NULL; | |
357 | |
358 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|C", &key_type, | |
359 &value_type, &klass) == FAILURE) { | |
360 return; | |
361 } | |
362 | |
363 Map* intern = | |
364 (Map*)zend_object_store_get_object(getThis() TSRMLS_CC); | |
365 intern->key_type = to_fieldtype(key_type); | |
366 intern->value_type = to_fieldtype(value_type); | |
367 intern->msg_ce = klass; | |
368 | |
369 // Check that the key type is an allowed type. | |
370 switch (intern->key_type) { | |
371 case UPB_TYPE_INT32: | |
372 case UPB_TYPE_INT64: | |
373 case UPB_TYPE_UINT32: | |
374 case UPB_TYPE_UINT64: | |
375 case UPB_TYPE_BOOL: | |
376 case UPB_TYPE_STRING: | |
377 case UPB_TYPE_BYTES: | |
378 // These are OK. | |
379 break; | |
380 default: | |
381 zend_error(E_USER_ERROR, "Invalid key type for map."); | |
382 } | |
383 } | |
384 | |
385 PHP_METHOD(MapField, offsetExists) { | |
386 zval *key; | |
387 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == | |
388 FAILURE) { | |
389 return; | |
390 } | |
391 | |
392 Map *intern = (Map *)zend_object_store_get_object(getThis() TSRMLS_CC); | |
393 | |
394 char keybuf[TABLE_KEY_BUF_LENGTH]; | |
395 const char* keyval = NULL; | |
396 size_t length = 0; | |
397 upb_value v; | |
398 #ifndef NDEBUG | |
399 v.ctype = UPB_CTYPE_UINT64; | |
400 #endif | |
401 if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) { | |
402 RETURN_BOOL(false); | |
403 } | |
404 | |
405 RETURN_BOOL(upb_strtable_lookup2(&intern->table, keyval, length, &v)); | |
406 } | |
407 | |
408 PHP_METHOD(MapField, offsetGet) { | |
409 zval *index, *value; | |
410 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == | |
411 FAILURE) { | |
412 return; | |
413 } | |
414 map_field_read_dimension(getThis(), index, BP_VAR_R, | |
415 return_value_ptr TSRMLS_CC); | |
416 } | |
417 | |
418 PHP_METHOD(MapField, offsetSet) { | |
419 zval *index, *value; | |
420 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) == | |
421 FAILURE) { | |
422 return; | |
423 } | |
424 map_field_write_dimension(getThis(), index, value TSRMLS_CC); | |
425 } | |
426 | |
427 PHP_METHOD(MapField, offsetUnset) { | |
428 zval *index; | |
429 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == | |
430 FAILURE) { | |
431 return; | |
432 } | |
433 map_field_unset_dimension(getThis(), index TSRMLS_CC); | |
434 } | |
435 | |
436 PHP_METHOD(MapField, count) { | |
437 Map *intern = | |
438 (Map *)zend_object_store_get_object(getThis() TSRMLS_CC); | |
439 | |
440 if (zend_parse_parameters_none() == FAILURE) { | |
441 return; | |
442 } | |
443 | |
444 RETURN_LONG(upb_strtable_count(&intern->table)); | |
445 } | |
446 | |
447 // ----------------------------------------------------------------------------- | |
448 // Map Iterator | |
449 // ----------------------------------------------------------------------------- | |
450 | |
451 void map_begin(zval *map_php, MapIter *iter TSRMLS_DC) { | |
452 Map *self = UNBOX(Map, map_php); | |
453 map_begin_internal(self, iter); | |
454 } | |
455 | |
456 void map_next(MapIter *iter) { | |
457 upb_strtable_next(&iter->it); | |
458 } | |
459 | |
460 bool map_done(MapIter *iter) { | |
461 return upb_strtable_done(&iter->it); | |
462 } | |
463 | |
464 const char *map_iter_key(MapIter *iter, int *len) { | |
465 *len = upb_strtable_iter_keylength(&iter->it); | |
466 return upb_strtable_iter_key(&iter->it); | |
467 } | |
468 | |
469 upb_value map_iter_value(MapIter *iter, int *len) { | |
470 *len = native_slot_size(iter->self->value_type); | |
471 return upb_strtable_iter_value(&iter->it); | |
472 } | |
OLD | NEW |