message.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "protobuf.h"
  33. static zend_class_entry* message_type;
  34. zend_object_handlers* message_handlers;
  35. static zend_function_entry message_methods[] = {
  36. PHP_ME(Message, encode, NULL, ZEND_ACC_PUBLIC)
  37. PHP_ME(Message, decode, NULL, ZEND_ACC_PUBLIC)
  38. PHP_ME(Message, readOneof, NULL, ZEND_ACC_PROTECTED)
  39. PHP_ME(Message, writeOneof, NULL, ZEND_ACC_PROTECTED)
  40. {NULL, NULL, NULL}
  41. };
  42. // Forward declare static functions.
  43. static void message_set_property(zval* object, zval* member, zval* value,
  44. const zend_literal* key TSRMLS_DC);
  45. static zval* message_get_property(zval* object, zval* member, int type,
  46. const zend_literal* key TSRMLS_DC);
  47. static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
  48. const zend_literal* key TSRMLS_DC);
  49. static zend_object_value message_create(zend_class_entry* ce TSRMLS_DC);
  50. static void message_free(void* object TSRMLS_DC);
  51. // -----------------------------------------------------------------------------
  52. // PHP Message Handlers
  53. // -----------------------------------------------------------------------------
  54. void message_init(TSRMLS_D) {
  55. zend_class_entry class_type;
  56. INIT_CLASS_ENTRY(class_type, "Google\\Protobuf\\Internal\\Message",
  57. message_methods);
  58. message_type = zend_register_internal_class(&class_type TSRMLS_CC);
  59. message_handlers = PEMALLOC(zend_object_handlers);
  60. memcpy(message_handlers, zend_get_std_object_handlers(),
  61. sizeof(zend_object_handlers));
  62. message_handlers->write_property = message_set_property;
  63. message_handlers->read_property = message_get_property;
  64. message_handlers->get_property_ptr_ptr = message_get_property_ptr_ptr;
  65. }
  66. static void message_set_property(zval* object, zval* member, zval* value,
  67. const zend_literal* key TSRMLS_DC) {
  68. if (Z_TYPE_P(member) != IS_STRING) {
  69. zend_error(E_USER_ERROR, "Unexpected type for field name");
  70. return;
  71. }
  72. if (Z_OBJCE_P(object) != EG(scope)) {
  73. // User cannot set property directly (e.g., $m->a = 1)
  74. zend_error(E_USER_ERROR, "Cannot access private property.");
  75. return;
  76. }
  77. const upb_fielddef* field;
  78. MessageHeader* self = zend_object_store_get_object(object TSRMLS_CC);
  79. field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
  80. if (field == NULL) {
  81. zend_error(E_USER_ERROR, "Unknown field: %s", Z_STRVAL_P(member));
  82. }
  83. layout_set(self->descriptor->layout, self, field, value TSRMLS_CC);
  84. }
  85. static zval* message_get_property(zval* object, zval* member, int type,
  86. const zend_literal* key TSRMLS_DC) {
  87. if (Z_TYPE_P(member) != IS_STRING) {
  88. zend_error(E_USER_ERROR, "Property name has to be a string.");
  89. return EG(uninitialized_zval_ptr);
  90. }
  91. if (Z_OBJCE_P(object) != EG(scope)) {
  92. // User cannot get property directly (e.g., $a = $m->a)
  93. zend_error(E_USER_ERROR, "Cannot access private property.");
  94. return EG(uninitialized_zval_ptr);
  95. }
  96. zend_property_info* property_info = NULL;
  97. // All properties should have been declared in the generated code and have
  98. // corresponding zvals in properties_table.
  99. ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
  100. if (zend_hash_quick_find(&Z_OBJCE_P(object)->properties_info,
  101. Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, h,
  102. (void**)&property_info) != SUCCESS) {
  103. zend_error(E_USER_ERROR, "Property does not exist.");
  104. return EG(uninitialized_zval_ptr);
  105. }
  106. MessageHeader* self =
  107. (MessageHeader*)zend_object_store_get_object(object TSRMLS_CC);
  108. const upb_fielddef* field;
  109. field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
  110. if (field == NULL) {
  111. return EG(uninitialized_zval_ptr);
  112. }
  113. return layout_get(
  114. self->descriptor->layout, message_data(self), field,
  115. &Z_OBJ_P(object)->properties_table[property_info->offset] TSRMLS_CC);
  116. }
  117. static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
  118. const zend_literal* key TSRMLS_DC) {
  119. return NULL;
  120. }
  121. // -----------------------------------------------------------------------------
  122. // C Message Utilities
  123. // -----------------------------------------------------------------------------
  124. void* message_data(void* msg) {
  125. return ((uint8_t*)msg) + sizeof(MessageHeader);
  126. }
  127. static void message_free(void* object TSRMLS_DC) {
  128. MessageHeader* msg = (MessageHeader*)object;
  129. int i;
  130. for (i = 0; i < msg->std.ce->default_properties_count; i++) {
  131. zval_ptr_dtor(&msg->std.properties_table[i]);
  132. }
  133. efree(msg->std.properties_table);
  134. efree(msg);
  135. }
  136. static zend_object_value message_create(zend_class_entry* ce TSRMLS_DC) {
  137. zend_object_value return_value;
  138. zval* php_descriptor = get_ce_obj(ce);
  139. Descriptor* desc = zend_object_store_get_object(php_descriptor TSRMLS_CC);
  140. MessageHeader* msg = (MessageHeader*)ALLOC_N(
  141. uint8_t, sizeof(MessageHeader) + desc->layout->size);
  142. memset(message_data(msg), 0, desc->layout->size);
  143. // We wrap first so that everything in the message object is GC-rooted in
  144. // case a collection happens during object creation in layout_init().
  145. msg->descriptor = desc;
  146. zend_object_std_init(&msg->std, ce TSRMLS_CC);
  147. object_properties_init(&msg->std, ce);
  148. layout_init(desc->layout, message_data(msg),
  149. msg->std.properties_table TSRMLS_CC);
  150. return_value.handle = zend_objects_store_put(
  151. msg, (zend_objects_store_dtor_t)zend_objects_destroy_object, message_free,
  152. NULL TSRMLS_CC);
  153. return_value.handlers = message_handlers;
  154. return return_value;
  155. }
  156. void build_class_from_descriptor(zval* php_descriptor TSRMLS_DC) {
  157. Descriptor* desc = UNBOX(Descriptor, php_descriptor);
  158. // Map entries don't have existing php class.
  159. if (upb_msgdef_mapentry(desc->msgdef)) {
  160. return;
  161. }
  162. zend_class_entry* registered_ce = desc->klass;
  163. if (desc->layout == NULL) {
  164. MessageLayout* layout = create_layout(desc->msgdef);
  165. desc->layout = layout;
  166. }
  167. registered_ce->create_object = message_create;
  168. }
  169. // -----------------------------------------------------------------------------
  170. // PHP Methods
  171. // -----------------------------------------------------------------------------
  172. PHP_METHOD(Message, readOneof) {
  173. long index;
  174. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  175. FAILURE) {
  176. return;
  177. }
  178. MessageHeader* msg =
  179. (MessageHeader*)zend_object_store_get_object(getThis() TSRMLS_CC);
  180. const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
  181. int property_cache_index =
  182. msg->descriptor->layout->fields[upb_fielddef_index(field)].cache_index;
  183. zval** cache_ptr = &(msg->std.properties_table)[property_cache_index];
  184. layout_get(msg->descriptor->layout, message_data(msg), field,
  185. &return_value TSRMLS_CC);
  186. }
  187. PHP_METHOD(Message, writeOneof) {
  188. long index;
  189. zval* value;
  190. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &index, &value) ==
  191. FAILURE) {
  192. return;
  193. }
  194. MessageHeader* msg =
  195. (MessageHeader*)zend_object_store_get_object(getThis() TSRMLS_CC);
  196. const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
  197. layout_set(msg->descriptor->layout, msg, field, value TSRMLS_CC);
  198. }