protobuf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 "protobuf.h"
  31. #include <zend_hash.h>
  32. ZEND_DECLARE_MODULE_GLOBALS(protobuf)
  33. static PHP_GINIT_FUNCTION(protobuf);
  34. static PHP_GSHUTDOWN_FUNCTION(protobuf);
  35. static PHP_RINIT_FUNCTION(protobuf);
  36. static PHP_RSHUTDOWN_FUNCTION(protobuf);
  37. static PHP_MINIT_FUNCTION(protobuf);
  38. static PHP_MSHUTDOWN_FUNCTION(protobuf);
  39. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  40. // instances.
  41. static HashTable* upb_def_to_php_obj_map;
  42. // Global map from message/enum's php class entry to corresponding wrapper
  43. // Descriptor/EnumDescriptor instances.
  44. static HashTable* ce_to_php_obj_map;
  45. // Global map from message/enum's proto fully-qualified name to corresponding
  46. // wrapper Descriptor/EnumDescriptor instances.
  47. static HashTable* proto_to_php_obj_map;
  48. static HashTable* reserved_names;
  49. // -----------------------------------------------------------------------------
  50. // Global maps.
  51. // -----------------------------------------------------------------------------
  52. static void add_to_table(HashTable* t, const void* def, void* value) {
  53. uint nIndex = (ulong)def & t->nTableMask;
  54. zval* pDest = NULL;
  55. php_proto_zend_hash_index_update_mem(t, (zend_ulong)def, &value,
  56. sizeof(zval*), (void**)&pDest);
  57. }
  58. static void* get_from_table(const HashTable* t, const void* def) {
  59. void** value;
  60. if (php_proto_zend_hash_index_find_mem(t, (zend_ulong)def, (void**)&value) ==
  61. FAILURE) {
  62. return NULL;
  63. }
  64. return *value;
  65. }
  66. static bool exist_in_table(const HashTable* t, const void* def) {
  67. void** value;
  68. return (php_proto_zend_hash_index_find_mem(t, (zend_ulong)def,
  69. (void**)&value) == SUCCESS);
  70. }
  71. static void add_to_list(HashTable* t, void* value) {
  72. zval* pDest = NULL;
  73. php_proto_zend_hash_next_index_insert_mem(t, &value, sizeof(void*),
  74. (void**)&pDest);
  75. }
  76. static void add_to_strtable(HashTable* t, const char* key, int key_size,
  77. void* value) {
  78. zval* pDest = NULL;
  79. php_proto_zend_hash_update_mem(t, key, key_size, &value, sizeof(void*),
  80. (void**)&pDest);
  81. }
  82. static void* get_from_strtable(const HashTable* t, const char* key, int key_size) {
  83. void** value;
  84. if (php_proto_zend_hash_find_mem(t, key, key_size, (void**)&value) ==
  85. FAILURE) {
  86. return NULL;
  87. }
  88. return *value;
  89. }
  90. void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value) {
  91. #if PHP_MAJOR_VERSION < 7
  92. Z_ADDREF_P(value);
  93. #else
  94. ++GC_REFCOUNT(value);
  95. #endif
  96. add_to_table(upb_def_to_php_obj_map, def, value);
  97. }
  98. PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void* def) {
  99. return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(upb_def_to_php_obj_map, def);
  100. }
  101. void add_ce_obj(const void* ce, PHP_PROTO_HASHTABLE_VALUE value) {
  102. #if PHP_MAJOR_VERSION < 7
  103. Z_ADDREF_P(value);
  104. #else
  105. ++GC_REFCOUNT(value);
  106. #endif
  107. add_to_table(ce_to_php_obj_map, ce, value);
  108. }
  109. PHP_PROTO_HASHTABLE_VALUE get_ce_obj(const void* ce) {
  110. return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(ce_to_php_obj_map, ce);
  111. }
  112. bool class_added(const void* ce) {
  113. return exist_in_table(ce_to_php_obj_map, ce);
  114. }
  115. void add_proto_obj(const char* proto, PHP_PROTO_HASHTABLE_VALUE value) {
  116. #if PHP_MAJOR_VERSION < 7
  117. Z_ADDREF_P(value);
  118. #else
  119. ++GC_REFCOUNT(value);
  120. #endif
  121. add_to_strtable(proto_to_php_obj_map, proto, strlen(proto), value);
  122. }
  123. PHP_PROTO_HASHTABLE_VALUE get_proto_obj(const char* proto) {
  124. return (PHP_PROTO_HASHTABLE_VALUE)get_from_strtable(proto_to_php_obj_map,
  125. proto, strlen(proto));
  126. }
  127. // -----------------------------------------------------------------------------
  128. // Well Known Types.
  129. // -----------------------------------------------------------------------------
  130. bool is_inited_file_any;
  131. bool is_inited_file_api;
  132. bool is_inited_file_duration;
  133. bool is_inited_file_field_mask;
  134. bool is_inited_file_empty;
  135. bool is_inited_file_source_context;
  136. bool is_inited_file_struct;
  137. bool is_inited_file_timestamp;
  138. bool is_inited_file_type;
  139. bool is_inited_file_wrappers;
  140. // -----------------------------------------------------------------------------
  141. // Reserved Name.
  142. // -----------------------------------------------------------------------------
  143. // Although we already have kReservedNames, we still add them to hash table to
  144. // speed up look up.
  145. const char *const kReservedNames[] = {
  146. "abstract", "and", "array", "as", "break",
  147. "callable", "case", "catch", "class", "clone",
  148. "const", "continue", "declare", "default", "die",
  149. "do", "echo", "else", "elseif", "empty",
  150. "enddeclare", "endfor", "endforeach", "endif", "endswitch",
  151. "endwhile", "eval", "exit", "extends", "final",
  152. "for", "foreach", "function", "global", "goto",
  153. "if", "implements", "include", "include_once", "instanceof",
  154. "insteadof", "interface", "isset", "list", "namespace",
  155. "new", "or", "print", "private", "protected",
  156. "public", "require", "require_once", "return", "static",
  157. "switch", "throw", "trait", "try", "unset",
  158. "use", "var", "while", "xor", "int",
  159. "float", "bool", "string", "true", "false",
  160. "null", "void", "iterable"};
  161. const int kReservedNamesSize = 73;
  162. bool is_reserved_name(const char* name) {
  163. void** value;
  164. return (php_proto_zend_hash_find(reserved_names, name, strlen(name),
  165. (void**)&value) == SUCCESS);
  166. }
  167. // -----------------------------------------------------------------------------
  168. // Utilities.
  169. // -----------------------------------------------------------------------------
  170. zend_function_entry protobuf_functions[] = {
  171. ZEND_FE_END
  172. };
  173. static const zend_module_dep protobuf_deps[] = {
  174. ZEND_MOD_OPTIONAL("date")
  175. ZEND_MOD_END
  176. };
  177. zend_module_entry protobuf_module_entry = {
  178. STANDARD_MODULE_HEADER_EX,
  179. NULL,
  180. protobuf_deps,
  181. PHP_PROTOBUF_EXTNAME, // extension name
  182. protobuf_functions, // function list
  183. PHP_MINIT(protobuf), // process startup
  184. PHP_MSHUTDOWN(protobuf), // process shutdown
  185. PHP_RINIT(protobuf), // request shutdown
  186. PHP_RSHUTDOWN(protobuf), // request shutdown
  187. NULL, // extension info
  188. PHP_PROTOBUF_VERSION, // extension version
  189. PHP_MODULE_GLOBALS(protobuf), // globals descriptor
  190. PHP_GINIT(protobuf), // globals ctor
  191. PHP_GSHUTDOWN(protobuf), // globals dtor
  192. NULL, // post deactivate
  193. STANDARD_MODULE_PROPERTIES_EX
  194. };
  195. // install module
  196. ZEND_GET_MODULE(protobuf)
  197. // global variables
  198. static PHP_GINIT_FUNCTION(protobuf) {
  199. }
  200. static PHP_GSHUTDOWN_FUNCTION(protobuf) {
  201. }
  202. #if PHP_MAJOR_VERSION >= 7
  203. static void php_proto_hashtable_descriptor_release(zval* value) {
  204. void* ptr = Z_PTR_P(value);
  205. zend_object* object = *(zend_object**)ptr;
  206. if(--GC_REFCOUNT(object) == 0) {
  207. zend_objects_store_del(object);
  208. }
  209. efree(ptr);
  210. }
  211. #endif
  212. static PHP_RINIT_FUNCTION(protobuf) {
  213. int i = 0;
  214. ALLOC_HASHTABLE(upb_def_to_php_obj_map);
  215. zend_hash_init(upb_def_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
  216. ALLOC_HASHTABLE(ce_to_php_obj_map);
  217. zend_hash_init(ce_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
  218. ALLOC_HASHTABLE(proto_to_php_obj_map);
  219. zend_hash_init(proto_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
  220. ALLOC_HASHTABLE(reserved_names);
  221. zend_hash_init(reserved_names, 16, NULL, NULL, 0);
  222. for (i = 0; i < kReservedNamesSize; i++) {
  223. php_proto_zend_hash_update(reserved_names, kReservedNames[i],
  224. strlen(kReservedNames[i]));
  225. }
  226. generated_pool = NULL;
  227. generated_pool_php = NULL;
  228. internal_generated_pool_php = NULL;
  229. is_inited_file_any = false;
  230. is_inited_file_api = false;
  231. is_inited_file_duration = false;
  232. is_inited_file_field_mask = false;
  233. is_inited_file_empty = false;
  234. is_inited_file_source_context = false;
  235. is_inited_file_struct = false;
  236. is_inited_file_timestamp = false;
  237. is_inited_file_type = false;
  238. is_inited_file_wrappers = false;
  239. return 0;
  240. }
  241. static PHP_RSHUTDOWN_FUNCTION(protobuf) {
  242. zend_hash_destroy(upb_def_to_php_obj_map);
  243. FREE_HASHTABLE(upb_def_to_php_obj_map);
  244. zend_hash_destroy(ce_to_php_obj_map);
  245. FREE_HASHTABLE(ce_to_php_obj_map);
  246. zend_hash_destroy(proto_to_php_obj_map);
  247. FREE_HASHTABLE(proto_to_php_obj_map);
  248. zend_hash_destroy(reserved_names);
  249. FREE_HASHTABLE(reserved_names);
  250. #if PHP_MAJOR_VERSION < 7
  251. if (generated_pool_php != NULL) {
  252. zval_dtor(generated_pool_php);
  253. FREE_ZVAL(generated_pool_php);
  254. }
  255. if (internal_generated_pool_php != NULL) {
  256. zval_dtor(internal_generated_pool_php);
  257. FREE_ZVAL(internal_generated_pool_php);
  258. }
  259. #else
  260. if (generated_pool_php != NULL) {
  261. zval tmp;
  262. ZVAL_OBJ(&tmp, generated_pool_php);
  263. zval_dtor(&tmp);
  264. }
  265. if (internal_generated_pool_php != NULL) {
  266. zval tmp;
  267. ZVAL_OBJ(&tmp, internal_generated_pool_php);
  268. zval_dtor(&tmp);
  269. }
  270. #endif
  271. is_inited_file_any = true;
  272. is_inited_file_api = true;
  273. is_inited_file_duration = true;
  274. is_inited_file_field_mask = true;
  275. is_inited_file_empty = true;
  276. is_inited_file_source_context = true;
  277. is_inited_file_struct = true;
  278. is_inited_file_timestamp = true;
  279. is_inited_file_type = true;
  280. is_inited_file_wrappers = true;
  281. return 0;
  282. }
  283. static PHP_MINIT_FUNCTION(protobuf) {
  284. descriptor_pool_init(TSRMLS_C);
  285. descriptor_init(TSRMLS_C);
  286. enum_descriptor_init(TSRMLS_C);
  287. enum_value_descriptor_init(TSRMLS_C);
  288. field_descriptor_init(TSRMLS_C);
  289. gpb_type_init(TSRMLS_C);
  290. internal_descriptor_pool_init(TSRMLS_C);
  291. map_field_init(TSRMLS_C);
  292. map_field_iter_init(TSRMLS_C);
  293. message_init(TSRMLS_C);
  294. oneof_descriptor_init(TSRMLS_C);
  295. repeated_field_init(TSRMLS_C);
  296. repeated_field_iter_init(TSRMLS_C);
  297. util_init(TSRMLS_C);
  298. gpb_metadata_any_init(TSRMLS_C);
  299. gpb_metadata_api_init(TSRMLS_C);
  300. gpb_metadata_duration_init(TSRMLS_C);
  301. gpb_metadata_field_mask_init(TSRMLS_C);
  302. gpb_metadata_empty_init(TSRMLS_C);
  303. gpb_metadata_source_context_init(TSRMLS_C);
  304. gpb_metadata_struct_init(TSRMLS_C);
  305. gpb_metadata_timestamp_init(TSRMLS_C);
  306. gpb_metadata_type_init(TSRMLS_C);
  307. gpb_metadata_wrappers_init(TSRMLS_C);
  308. any_init(TSRMLS_C);
  309. api_init(TSRMLS_C);
  310. bool_value_init(TSRMLS_C);
  311. bytes_value_init(TSRMLS_C);
  312. double_value_init(TSRMLS_C);
  313. duration_init(TSRMLS_C);
  314. enum_init(TSRMLS_C);
  315. enum_value_init(TSRMLS_C);
  316. field_cardinality_init(TSRMLS_C);
  317. field_init(TSRMLS_C);
  318. field_kind_init(TSRMLS_C);
  319. field_mask_init(TSRMLS_C);
  320. float_value_init(TSRMLS_C);
  321. empty_init(TSRMLS_C);
  322. int32_value_init(TSRMLS_C);
  323. int64_value_init(TSRMLS_C);
  324. list_value_init(TSRMLS_C);
  325. method_init(TSRMLS_C);
  326. mixin_init(TSRMLS_C);
  327. null_value_init(TSRMLS_C);
  328. option_init(TSRMLS_C);
  329. source_context_init(TSRMLS_C);
  330. string_value_init(TSRMLS_C);
  331. struct_init(TSRMLS_C);
  332. syntax_init(TSRMLS_C);
  333. timestamp_init(TSRMLS_C);
  334. type_init(TSRMLS_C);
  335. u_int32_value_init(TSRMLS_C);
  336. u_int64_value_init(TSRMLS_C);
  337. value_init(TSRMLS_C);
  338. return 0;
  339. }
  340. static PHP_MSHUTDOWN_FUNCTION(protobuf) {
  341. PEFREE(message_handlers);
  342. PEFREE(repeated_field_handlers);
  343. PEFREE(repeated_field_iter_handlers);
  344. PEFREE(map_field_handlers);
  345. PEFREE(map_field_iter_handlers);
  346. return 0;
  347. }