map.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. #include <ext/spl/spl_iterators.h>
  31. #include <Zend/zend_API.h>
  32. #include <Zend/zend_interfaces.h>
  33. #include "protobuf.h"
  34. #include "utf8.h"
  35. ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetGet, 0, 0, 1)
  36. ZEND_ARG_INFO(0, index)
  37. ZEND_END_ARG_INFO()
  38. ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetSet, 0, 0, 2)
  39. ZEND_ARG_INFO(0, index)
  40. ZEND_ARG_INFO(0, newval)
  41. ZEND_END_ARG_INFO()
  42. ZEND_BEGIN_ARG_INFO(arginfo_void, 0)
  43. ZEND_END_ARG_INFO()
  44. // Utilities
  45. void* upb_value_memory(upb_value* v) {
  46. return (void*)(&v->val);
  47. }
  48. // -----------------------------------------------------------------------------
  49. // Basic map operations on top of upb's strtable.
  50. //
  51. // Note that we roll our own `Map` container here because, as for
  52. // `RepeatedField`, we want a strongly-typed container. This is so that any user
  53. // errors due to incorrect map key or value types are raised as close as
  54. // possible to the error site, rather than at some deferred point (e.g.,
  55. // serialization).
  56. //
  57. // We build our `Map` on top of upb_strtable so that we're able to take
  58. // advantage of the native_slot storage abstraction, as RepeatedField does.
  59. // (This is not quite a perfect mapping -- see the key conversions below -- but
  60. // gives us full support and error-checking for all value types for free.)
  61. // -----------------------------------------------------------------------------
  62. // Map values are stored using the native_slot abstraction (as with repeated
  63. // field values), but keys are a bit special. Since we use a strtable, we need
  64. // to store keys as sequences of bytes such that equality of those bytes maps
  65. // one-to-one to equality of keys. We store strings directly (i.e., they map to
  66. // their own bytes) and integers as native integers (using the native_slot
  67. // abstraction).
  68. // Note that there is another tradeoff here in keeping string keys as native
  69. // strings rather than PHP strings: traversing the Map requires conversion to
  70. // PHP string values on every traversal, potentially creating more garbage. We
  71. // should consider ways to cache a PHP version of the key if this becomes an
  72. // issue later.
  73. // Forms a key to use with the underlying strtable from a PHP key value. |buf|
  74. // must point to TABLE_KEY_BUF_LENGTH bytes of temporary space, used to
  75. // construct a key byte sequence if needed. |out_key| and |out_length| provide
  76. // the resulting key data/length.
  77. #define TABLE_KEY_BUF_LENGTH 8 // sizeof(uint64_t)
  78. static bool table_key(Map* self, zval* key,
  79. char* buf,
  80. const char** out_key,
  81. size_t* out_length TSRMLS_DC) {
  82. switch (self->key_type) {
  83. case UPB_TYPE_STRING:
  84. if (!protobuf_convert_to_string(key)) {
  85. return false;
  86. }
  87. if (!is_structurally_valid_utf8(Z_STRVAL_P(key), Z_STRLEN_P(key))) {
  88. zend_error(E_USER_ERROR, "Given key is not UTF8 encoded.");
  89. return false;
  90. }
  91. *out_key = Z_STRVAL_P(key);
  92. *out_length = Z_STRLEN_P(key);
  93. break;
  94. #define CASE_TYPE(upb_type, type, c_type, php_type) \
  95. case UPB_TYPE_##upb_type: { \
  96. c_type type##_value; \
  97. if (!protobuf_convert_to_##type(key, &type##_value)) { \
  98. return false; \
  99. } \
  100. native_slot_set(self->key_type, NULL, buf, key TSRMLS_CC); \
  101. *out_key = buf; \
  102. *out_length = native_slot_size(self->key_type); \
  103. break; \
  104. }
  105. CASE_TYPE(BOOL, bool, int8_t, BOOL)
  106. CASE_TYPE(INT32, int32, int32_t, LONG)
  107. CASE_TYPE(INT64, int64, int64_t, LONG)
  108. CASE_TYPE(UINT32, uint32, uint32_t, LONG)
  109. CASE_TYPE(UINT64, uint64, uint64_t, LONG)
  110. #undef CASE_TYPE
  111. default:
  112. // Map constructor should not allow a Map with another key type to be
  113. // constructed.
  114. assert(false);
  115. break;
  116. }
  117. return true;
  118. }
  119. // -----------------------------------------------------------------------------
  120. // MapField methods
  121. // -----------------------------------------------------------------------------
  122. static zend_function_entry map_field_methods[] = {
  123. PHP_ME(MapField, __construct, NULL, ZEND_ACC_PUBLIC)
  124. PHP_ME(MapField, offsetExists, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  125. PHP_ME(MapField, offsetGet, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  126. PHP_ME(MapField, offsetSet, arginfo_offsetSet, ZEND_ACC_PUBLIC)
  127. PHP_ME(MapField, offsetUnset, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  128. PHP_ME(MapField, count, arginfo_void, ZEND_ACC_PUBLIC)
  129. ZEND_FE_END
  130. };
  131. // -----------------------------------------------------------------------------
  132. // MapField creation/desctruction
  133. // -----------------------------------------------------------------------------
  134. zend_class_entry* map_field_type;
  135. zend_object_handlers* map_field_handlers;
  136. static void map_begin_internal(Map *map, MapIter *iter) {
  137. iter->self = map;
  138. upb_strtable_begin(&iter->it, &map->table);
  139. }
  140. static HashTable *map_field_get_gc(zval *object, zval ***table,
  141. int *n TSRMLS_DC) {
  142. // TODO(teboring): Unfortunately, zend engine does not support garbage
  143. // collection for custom array. We have to use zend engine's native array
  144. // instead.
  145. *table = NULL;
  146. *n = 0;
  147. return NULL;
  148. }
  149. void map_field_init(TSRMLS_D) {
  150. zend_class_entry class_type;
  151. const char* class_name = "Google\\Protobuf\\Internal\\MapField";
  152. INIT_CLASS_ENTRY_EX(class_type, class_name, strlen(class_name),
  153. map_field_methods);
  154. map_field_type = zend_register_internal_class(&class_type TSRMLS_CC);
  155. map_field_type->create_object = map_field_create;
  156. zend_class_implements(map_field_type TSRMLS_CC, 2, spl_ce_ArrayAccess,
  157. spl_ce_Countable);
  158. map_field_handlers = PEMALLOC(zend_object_handlers);
  159. memcpy(map_field_handlers, zend_get_std_object_handlers(),
  160. sizeof(zend_object_handlers));
  161. map_field_handlers->get_gc = map_field_get_gc;
  162. }
  163. zend_object_value map_field_create(zend_class_entry *ce TSRMLS_DC) {
  164. zend_object_value retval = {0};
  165. Map *intern;
  166. intern = emalloc(sizeof(Map));
  167. memset(intern, 0, sizeof(Map));
  168. zend_object_std_init(&intern->std, ce TSRMLS_CC);
  169. object_properties_init(&intern->std, ce);
  170. // Table value type is always UINT64: this ensures enough space to store the
  171. // native_slot value.
  172. if (!upb_strtable_init(&intern->table, UPB_CTYPE_UINT64)) {
  173. zend_error(E_USER_ERROR, "Could not allocate table.");
  174. }
  175. retval.handle = zend_objects_store_put(
  176. intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
  177. (zend_objects_free_object_storage_t)map_field_free, NULL TSRMLS_CC);
  178. retval.handlers = map_field_handlers;
  179. return retval;
  180. }
  181. void map_field_free(void *object TSRMLS_DC) {
  182. Map *map = (Map *)object;
  183. switch (map->value_type) {
  184. case UPB_TYPE_MESSAGE:
  185. case UPB_TYPE_STRING:
  186. case UPB_TYPE_BYTES: {
  187. MapIter it;
  188. int len;
  189. for (map_begin_internal(map, &it); !map_done(&it); map_next(&it)) {
  190. upb_value value = map_iter_value(&it, &len);
  191. void *mem = upb_value_memory(&value);
  192. zval_ptr_dtor(mem);
  193. }
  194. break;
  195. }
  196. default:
  197. break;
  198. }
  199. upb_strtable_uninit(&map->table);
  200. zend_object_std_dtor(&map->std TSRMLS_CC);
  201. efree(object);
  202. }
  203. void map_field_create_with_type(zend_class_entry *ce, const upb_fielddef *field,
  204. zval **map_field TSRMLS_DC) {
  205. MAKE_STD_ZVAL(*map_field);
  206. Z_TYPE_PP(map_field) = IS_OBJECT;
  207. Z_OBJVAL_PP(map_field) =
  208. map_field_type->create_object(map_field_type TSRMLS_CC);
  209. Map* intern =
  210. (Map*)zend_object_store_get_object(*map_field TSRMLS_CC);
  211. const upb_fielddef *key_field = map_field_key(field);
  212. const upb_fielddef *value_field = map_field_value(field);
  213. intern->key_type = upb_fielddef_type(key_field);
  214. intern->value_type = upb_fielddef_type(value_field);
  215. intern->msg_ce = field_type_class(value_field TSRMLS_CC);
  216. }
  217. static void map_field_free_element(void *object) {
  218. }
  219. // -----------------------------------------------------------------------------
  220. // MapField Handlers
  221. // -----------------------------------------------------------------------------
  222. static bool map_field_read_dimension(zval *object, zval *key, int type,
  223. zval **retval TSRMLS_DC) {
  224. Map *intern =
  225. (Map *)zend_object_store_get_object(object TSRMLS_CC);
  226. char keybuf[TABLE_KEY_BUF_LENGTH];
  227. const char* keyval = NULL;
  228. size_t length = 0;
  229. upb_value v;
  230. #ifndef NDEBUG
  231. v.ctype = UPB_CTYPE_UINT64;
  232. #endif
  233. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  234. return false;
  235. }
  236. if (upb_strtable_lookup2(&intern->table, keyval, length, &v)) {
  237. void* mem = upb_value_memory(&v);
  238. native_slot_get(intern->value_type, mem, retval TSRMLS_CC);
  239. return true;
  240. } else {
  241. zend_error(E_USER_ERROR, "Given key doesn't exist.");
  242. return false;
  243. }
  244. }
  245. bool map_index_set(Map *intern, const char* keyval, int length, upb_value v) {
  246. // Replace any existing value by issuing a 'remove' operation first.
  247. upb_strtable_remove2(&intern->table, keyval, length, NULL);
  248. if (!upb_strtable_insert2(&intern->table, keyval, length, v)) {
  249. zend_error(E_USER_ERROR, "Could not insert into table");
  250. return false;
  251. }
  252. return true;
  253. }
  254. static bool map_field_write_dimension(zval *object, zval *key,
  255. zval *value TSRMLS_DC) {
  256. Map *intern = (Map *)zend_object_store_get_object(object TSRMLS_CC);
  257. char keybuf[TABLE_KEY_BUF_LENGTH];
  258. const char* keyval = NULL;
  259. size_t length = 0;
  260. upb_value v;
  261. void* mem;
  262. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  263. return false;
  264. }
  265. mem = upb_value_memory(&v);
  266. memset(mem, 0, native_slot_size(intern->value_type));
  267. if (!native_slot_set(intern->value_type, intern->msg_ce, mem, value
  268. TSRMLS_CC)) {
  269. return false;
  270. }
  271. #ifndef NDEBUG
  272. v.ctype = UPB_CTYPE_UINT64;
  273. #endif
  274. // Replace any existing value by issuing a 'remove' operation first.
  275. upb_strtable_remove2(&intern->table, keyval, length, NULL);
  276. if (!upb_strtable_insert2(&intern->table, keyval, length, v)) {
  277. zend_error(E_USER_ERROR, "Could not insert into table");
  278. return false;
  279. }
  280. return true;
  281. }
  282. static bool map_field_unset_dimension(zval *object, zval *key TSRMLS_DC) {
  283. Map *intern = (Map *)zend_object_store_get_object(object TSRMLS_CC);
  284. char keybuf[TABLE_KEY_BUF_LENGTH];
  285. const char* keyval = NULL;
  286. size_t length = 0;
  287. upb_value v;
  288. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  289. return false;
  290. }
  291. #ifndef NDEBUG
  292. v.ctype = UPB_CTYPE_UINT64;
  293. #endif
  294. upb_strtable_remove2(&intern->table, keyval, length, &v);
  295. return true;
  296. }
  297. // -----------------------------------------------------------------------------
  298. // PHP MapField Methods
  299. // -----------------------------------------------------------------------------
  300. PHP_METHOD(MapField, __construct) {
  301. long key_type, value_type;
  302. zend_class_entry* klass = NULL;
  303. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|C", &key_type,
  304. &value_type, &klass) == FAILURE) {
  305. return;
  306. }
  307. Map* intern =
  308. (Map*)zend_object_store_get_object(getThis() TSRMLS_CC);
  309. intern->key_type = to_fieldtype(key_type);
  310. intern->value_type = to_fieldtype(value_type);
  311. intern->msg_ce = klass;
  312. // Check that the key type is an allowed type.
  313. switch (intern->key_type) {
  314. case UPB_TYPE_INT32:
  315. case UPB_TYPE_INT64:
  316. case UPB_TYPE_UINT32:
  317. case UPB_TYPE_UINT64:
  318. case UPB_TYPE_BOOL:
  319. case UPB_TYPE_STRING:
  320. case UPB_TYPE_BYTES:
  321. // These are OK.
  322. break;
  323. default:
  324. zend_error(E_USER_ERROR, "Invalid key type for map.");
  325. }
  326. }
  327. PHP_METHOD(MapField, offsetExists) {
  328. zval *key;
  329. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) ==
  330. FAILURE) {
  331. return;
  332. }
  333. Map *intern = (Map *)zend_object_store_get_object(getThis() TSRMLS_CC);
  334. char keybuf[TABLE_KEY_BUF_LENGTH];
  335. const char* keyval = NULL;
  336. size_t length = 0;
  337. upb_value v;
  338. #ifndef NDEBUG
  339. v.ctype = UPB_CTYPE_UINT64;
  340. #endif
  341. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  342. RETURN_BOOL(false);
  343. }
  344. RETURN_BOOL(upb_strtable_lookup2(&intern->table, keyval, length, &v));
  345. }
  346. PHP_METHOD(MapField, offsetGet) {
  347. zval *index, *value;
  348. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) ==
  349. FAILURE) {
  350. return;
  351. }
  352. map_field_read_dimension(getThis(), index, BP_VAR_R,
  353. return_value_ptr TSRMLS_CC);
  354. }
  355. PHP_METHOD(MapField, offsetSet) {
  356. zval *index, *value;
  357. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) ==
  358. FAILURE) {
  359. return;
  360. }
  361. map_field_write_dimension(getThis(), index, value TSRMLS_CC);
  362. }
  363. PHP_METHOD(MapField, offsetUnset) {
  364. zval *index;
  365. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) ==
  366. FAILURE) {
  367. return;
  368. }
  369. map_field_unset_dimension(getThis(), index TSRMLS_CC);
  370. }
  371. PHP_METHOD(MapField, count) {
  372. Map *intern =
  373. (Map *)zend_object_store_get_object(getThis() TSRMLS_CC);
  374. if (zend_parse_parameters_none() == FAILURE) {
  375. return;
  376. }
  377. RETURN_LONG(upb_strtable_count(&intern->table));
  378. }
  379. // -----------------------------------------------------------------------------
  380. // Map Iterator
  381. // -----------------------------------------------------------------------------
  382. void map_begin(zval *map_php, MapIter *iter TSRMLS_DC) {
  383. Map *self = UNBOX(Map, map_php);
  384. map_begin_internal(self, iter);
  385. }
  386. void map_next(MapIter *iter) {
  387. upb_strtable_next(&iter->it);
  388. }
  389. bool map_done(MapIter *iter) {
  390. return upb_strtable_done(&iter->it);
  391. }
  392. const char *map_iter_key(MapIter *iter, int *len) {
  393. *len = upb_strtable_iter_keylength(&iter->it);
  394. return upb_strtable_iter_key(&iter->it);
  395. }
  396. upb_value map_iter_value(MapIter *iter, int *len) {
  397. *len = native_slot_size(iter->self->value_type);
  398. return upb_strtable_iter_value(&iter->it);
  399. }