message.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 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 <php.h>
  31. #include <stdlib.h>
  32. #include <ext/json/php_json.h>
  33. #include "protobuf.h"
  34. static zend_class_entry* message_type;
  35. zend_object_handlers* message_handlers;
  36. static zend_function_entry message_methods[] = {
  37. PHP_ME(Message, encode, NULL, ZEND_ACC_PUBLIC)
  38. PHP_ME(Message, decode, NULL, ZEND_ACC_PUBLIC)
  39. PHP_ME(Message, jsonEncode, NULL, ZEND_ACC_PUBLIC)
  40. PHP_ME(Message, jsonDecode, NULL, ZEND_ACC_PUBLIC)
  41. PHP_ME(Message, readOneof, NULL, ZEND_ACC_PROTECTED)
  42. PHP_ME(Message, writeOneof, NULL, ZEND_ACC_PROTECTED)
  43. PHP_ME(Message, whichOneof, NULL, ZEND_ACC_PROTECTED)
  44. PHP_ME(Message, __construct, NULL, ZEND_ACC_PROTECTED)
  45. {NULL, NULL, NULL}
  46. };
  47. // Forward declare static functions.
  48. static void message_set_property(zval* object, zval* member, zval* value,
  49. const zend_literal* key TSRMLS_DC);
  50. static zval* message_get_property(zval* object, zval* member, int type,
  51. const zend_literal* key TSRMLS_DC);
  52. static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
  53. const zend_literal* key TSRMLS_DC);
  54. static HashTable* message_get_properties(zval* object TSRMLS_DC);
  55. static HashTable* message_get_gc(zval* object, zval*** table, int* n TSRMLS_DC);
  56. static zend_object_value message_create(zend_class_entry* ce TSRMLS_DC);
  57. static void message_free(void* object TSRMLS_DC);
  58. // -----------------------------------------------------------------------------
  59. // PHP Message Handlers
  60. // -----------------------------------------------------------------------------
  61. void message_init(TSRMLS_D) {
  62. zend_class_entry class_type;
  63. INIT_CLASS_ENTRY(class_type, "Google\\Protobuf\\Internal\\Message",
  64. message_methods);
  65. message_type = zend_register_internal_class(&class_type TSRMLS_CC);
  66. message_handlers = PEMALLOC(zend_object_handlers);
  67. memcpy(message_handlers, zend_get_std_object_handlers(),
  68. sizeof(zend_object_handlers));
  69. message_handlers->write_property = message_set_property;
  70. message_handlers->read_property = message_get_property;
  71. message_handlers->get_property_ptr_ptr = message_get_property_ptr_ptr;
  72. message_handlers->get_properties = message_get_properties;
  73. message_handlers->get_gc = message_get_gc;
  74. }
  75. static void message_set_property(zval* object, zval* member, zval* value,
  76. const zend_literal* key TSRMLS_DC) {
  77. if (Z_TYPE_P(member) != IS_STRING) {
  78. zend_error(E_USER_ERROR, "Unexpected type for field name");
  79. return;
  80. }
  81. if (Z_OBJCE_P(object) != EG(scope)) {
  82. // User cannot set property directly (e.g., $m->a = 1)
  83. zend_error(E_USER_ERROR, "Cannot access private property.");
  84. return;
  85. }
  86. const upb_fielddef* field;
  87. MessageHeader* self = zend_object_store_get_object(object TSRMLS_CC);
  88. field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
  89. if (field == NULL) {
  90. zend_error(E_USER_ERROR, "Unknown field: %s", Z_STRVAL_P(member));
  91. }
  92. layout_set(self->descriptor->layout, self, field, value TSRMLS_CC);
  93. }
  94. static zval* message_get_property(zval* object, zval* member, int type,
  95. const zend_literal* key TSRMLS_DC) {
  96. if (Z_TYPE_P(member) != IS_STRING) {
  97. zend_error(E_USER_ERROR, "Property name has to be a string.");
  98. return EG(uninitialized_zval_ptr);
  99. }
  100. if (Z_OBJCE_P(object) != EG(scope)) {
  101. // User cannot get property directly (e.g., $a = $m->a)
  102. zend_error(E_USER_ERROR, "Cannot access private property.");
  103. return EG(uninitialized_zval_ptr);
  104. }
  105. zend_property_info* property_info = NULL;
  106. // All properties should have been declared in the generated code and have
  107. // corresponding zvals in properties_table.
  108. ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
  109. if (zend_hash_quick_find(&Z_OBJCE_P(object)->properties_info,
  110. Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, h,
  111. (void**)&property_info) != SUCCESS) {
  112. zend_error(E_USER_ERROR, "Property does not exist.");
  113. return EG(uninitialized_zval_ptr);
  114. }
  115. MessageHeader* self =
  116. (MessageHeader*)zend_object_store_get_object(object TSRMLS_CC);
  117. const upb_fielddef* field;
  118. field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
  119. if (field == NULL) {
  120. return EG(uninitialized_zval_ptr);
  121. }
  122. return layout_get(
  123. self->descriptor->layout, message_data(self), field,
  124. &Z_OBJ_P(object)->properties_table[property_info->offset] TSRMLS_CC);
  125. }
  126. static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
  127. const zend_literal* key TSRMLS_DC) {
  128. return NULL;
  129. }
  130. static HashTable* message_get_properties(zval* object TSRMLS_DC) {
  131. return NULL;
  132. }
  133. static HashTable* message_get_gc(zval* object, zval*** table, int* n TSRMLS_DC) {
  134. zend_object* zobj = Z_OBJ_P(object);
  135. *table = zobj->properties_table;
  136. *n = zobj->ce->default_properties_count;
  137. return NULL;
  138. }
  139. // -----------------------------------------------------------------------------
  140. // C Message Utilities
  141. // -----------------------------------------------------------------------------
  142. void* message_data(void* msg) {
  143. return ((uint8_t*)msg) + sizeof(MessageHeader);
  144. }
  145. static void message_free(void* object TSRMLS_DC) {
  146. MessageHeader* msg = (MessageHeader*)object;
  147. int i;
  148. for (i = 0; i < msg->std.ce->default_properties_count; i++) {
  149. zval_ptr_dtor(&msg->std.properties_table[i]);
  150. }
  151. efree(msg->std.properties_table);
  152. efree(msg);
  153. }
  154. static zend_object_value message_create(zend_class_entry* ce TSRMLS_DC) {
  155. zend_object_value return_value;
  156. zval* php_descriptor = get_ce_obj(ce);
  157. Descriptor* desc = zend_object_store_get_object(php_descriptor TSRMLS_CC);
  158. MessageHeader* msg = (MessageHeader*)ALLOC_N(
  159. uint8_t, sizeof(MessageHeader) + desc->layout->size);
  160. memset(message_data(msg), 0, desc->layout->size);
  161. // We wrap first so that everything in the message object is GC-rooted in
  162. // case a collection happens during object creation in layout_init().
  163. msg->descriptor = desc;
  164. zend_object_std_init(&msg->std, ce TSRMLS_CC);
  165. object_properties_init(&msg->std, ce);
  166. layout_init(desc->layout, message_data(msg),
  167. msg->std.properties_table TSRMLS_CC);
  168. return_value.handle = zend_objects_store_put(
  169. msg, (zend_objects_store_dtor_t)zend_objects_destroy_object, message_free,
  170. NULL TSRMLS_CC);
  171. return_value.handlers = message_handlers;
  172. return return_value;
  173. }
  174. void build_class_from_descriptor(zval* php_descriptor TSRMLS_DC) {
  175. Descriptor* desc = UNBOX(Descriptor, php_descriptor);
  176. // Map entries don't have existing php class.
  177. if (upb_msgdef_mapentry(desc->msgdef)) {
  178. return;
  179. }
  180. zend_class_entry* registered_ce = desc->klass;
  181. if (desc->layout == NULL) {
  182. MessageLayout* layout = create_layout(desc->msgdef);
  183. desc->layout = layout;
  184. }
  185. registered_ce->create_object = message_create;
  186. }
  187. // -----------------------------------------------------------------------------
  188. // PHP Methods
  189. // -----------------------------------------------------------------------------
  190. // At the first time the message is created, the class entry hasn't been
  191. // modified. As a result, the first created instance will be a normal zend
  192. // object. Here, we manually modify it to our message in such a case.
  193. PHP_METHOD(Message, __construct) {
  194. if (Z_OBJVAL_P(getThis()).handlers != message_handlers) {
  195. zend_class_entry* ce = Z_OBJCE_P(getThis());
  196. zval_dtor(getThis());
  197. Z_OBJVAL_P(getThis()) = message_create(ce TSRMLS_CC);
  198. }
  199. }
  200. PHP_METHOD(Message, readOneof) {
  201. long index;
  202. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  203. FAILURE) {
  204. return;
  205. }
  206. MessageHeader* msg =
  207. (MessageHeader*)zend_object_store_get_object(getThis() TSRMLS_CC);
  208. const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
  209. int property_cache_index =
  210. msg->descriptor->layout->fields[upb_fielddef_index(field)].cache_index;
  211. zval** cache_ptr = &(msg->std.properties_table)[property_cache_index];
  212. layout_get(msg->descriptor->layout, message_data(msg), field,
  213. &return_value TSRMLS_CC);
  214. }
  215. PHP_METHOD(Message, writeOneof) {
  216. long index;
  217. zval* value;
  218. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &index, &value) ==
  219. FAILURE) {
  220. return;
  221. }
  222. MessageHeader* msg =
  223. (MessageHeader*)zend_object_store_get_object(getThis() TSRMLS_CC);
  224. const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
  225. layout_set(msg->descriptor->layout, msg, field, value TSRMLS_CC);
  226. }
  227. PHP_METHOD(Message, whichOneof) {
  228. char* oneof_name;
  229. int length;
  230. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &oneof_name,
  231. &length) == FAILURE) {
  232. return;
  233. }
  234. MessageHeader* msg =
  235. (MessageHeader*)zend_object_store_get_object(getThis() TSRMLS_CC);
  236. const upb_oneofdef* oneof =
  237. upb_msgdef_ntoo(msg->descriptor->msgdef, oneof_name, length);
  238. const char* oneof_case_name = layout_get_oneof_case(
  239. msg->descriptor->layout, message_data(msg), oneof TSRMLS_CC);
  240. RETURN_STRING(oneof_case_name, 1);
  241. }