storage.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 <stdint.h>
  31. #include <protobuf.h>
  32. #include <Zend/zend.h>
  33. #include "utf8.h"
  34. // -----------------------------------------------------------------------------
  35. // Native slot storage.
  36. // -----------------------------------------------------------------------------
  37. #define DEREF(memory, type) *(type*)(memory)
  38. size_t native_slot_size(upb_fieldtype_t type) {
  39. switch (type) {
  40. case UPB_TYPE_FLOAT: return 4;
  41. case UPB_TYPE_DOUBLE: return 8;
  42. case UPB_TYPE_BOOL: return 1;
  43. case UPB_TYPE_STRING: return sizeof(void*);
  44. case UPB_TYPE_BYTES: return sizeof(void*);
  45. case UPB_TYPE_MESSAGE: return sizeof(void*);
  46. case UPB_TYPE_ENUM: return 4;
  47. case UPB_TYPE_INT32: return 4;
  48. case UPB_TYPE_INT64: return 8;
  49. case UPB_TYPE_UINT32: return 4;
  50. case UPB_TYPE_UINT64: return 8;
  51. default: return 0;
  52. }
  53. }
  54. static bool native_slot_is_default(upb_fieldtype_t type, const void* memory) {
  55. switch (type) {
  56. #define CASE_TYPE(upb_type, c_type) \
  57. case UPB_TYPE_##upb_type: { \
  58. return DEREF(memory, c_type) == 0; \
  59. }
  60. CASE_TYPE(INT32, int32_t )
  61. CASE_TYPE(UINT32, uint32_t)
  62. CASE_TYPE(ENUM, int32_t )
  63. CASE_TYPE(INT64, int64_t )
  64. CASE_TYPE(UINT64, uint64_t)
  65. CASE_TYPE(FLOAT, float )
  66. CASE_TYPE(DOUBLE, double )
  67. CASE_TYPE(BOOL, int8_t )
  68. #undef CASE_TYPE
  69. case UPB_TYPE_STRING:
  70. case UPB_TYPE_BYTES:
  71. return Z_STRLEN_P(CACHED_PTR_TO_ZVAL_PTR(DEREF(memory, CACHED_VALUE*))) ==
  72. 0;
  73. case UPB_TYPE_MESSAGE:
  74. return Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(DEREF(memory, CACHED_VALUE*))) ==
  75. IS_NULL;
  76. default: return false;
  77. }
  78. }
  79. bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass,
  80. void* memory, zval* value PHP_PROTO_TSRMLS_DC) {
  81. switch (type) {
  82. case UPB_TYPE_STRING:
  83. case UPB_TYPE_BYTES: {
  84. if (!protobuf_convert_to_string(value)) {
  85. return false;
  86. }
  87. if (type == UPB_TYPE_STRING &&
  88. !is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) {
  89. zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
  90. return false;
  91. }
  92. zval* cached_zval = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  93. if (EXPECTED(cached_zval != NULL)) {
  94. #if PHP_MAJOR_VERSION < 7
  95. REPLACE_ZVAL_VALUE((zval**)memory, value, 1);
  96. #else
  97. zend_assign_to_variable(cached_zval, value, IS_CV);
  98. #endif
  99. }
  100. break;
  101. }
  102. case UPB_TYPE_MESSAGE: {
  103. if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_NULL) {
  104. zend_error(E_USER_ERROR, "Given value is not message.");
  105. return false;
  106. }
  107. if (Z_TYPE_P(value) == IS_OBJECT && klass != Z_OBJCE_P(value)) {
  108. zend_error(E_USER_ERROR, "Given message does not have correct class.");
  109. return false;
  110. }
  111. zval* property_ptr = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  112. if (EXPECTED(property_ptr != value)) {
  113. php_proto_zval_ptr_dtor(property_ptr);
  114. }
  115. #if PHP_MAJOR_VERSION < 7
  116. DEREF(memory, zval*) = value;
  117. Z_ADDREF_P(value);
  118. #else
  119. ZVAL_ZVAL(property_ptr, value, 1, 0);
  120. #endif
  121. break;
  122. }
  123. #define CASE_TYPE(upb_type, type, c_type, php_type) \
  124. case UPB_TYPE_##upb_type: { \
  125. c_type type##_value; \
  126. if (protobuf_convert_to_##type(value, &type##_value)) { \
  127. DEREF(memory, c_type) = type##_value; \
  128. } \
  129. break; \
  130. }
  131. CASE_TYPE(INT32, int32, int32_t, LONG)
  132. CASE_TYPE(UINT32, uint32, uint32_t, LONG)
  133. CASE_TYPE(ENUM, int32, int32_t, LONG)
  134. CASE_TYPE(INT64, int64, int64_t, LONG)
  135. CASE_TYPE(UINT64, uint64, uint64_t, LONG)
  136. CASE_TYPE(FLOAT, float, float, DOUBLE)
  137. CASE_TYPE(DOUBLE, double, double, DOUBLE)
  138. CASE_TYPE(BOOL, bool, int8_t, BOOL)
  139. #undef CASE_TYPE
  140. default:
  141. break;
  142. }
  143. return true;
  144. }
  145. bool native_slot_set_by_array(upb_fieldtype_t type,
  146. const zend_class_entry* klass, void* memory,
  147. zval* value TSRMLS_DC) {
  148. #if PHP_MAJOR_VERSION >= 7
  149. if (Z_ISREF_P(value)) {
  150. ZVAL_DEREF(value);
  151. }
  152. #endif
  153. switch (type) {
  154. case UPB_TYPE_STRING:
  155. case UPB_TYPE_BYTES: {
  156. if (!protobuf_convert_to_string(value)) {
  157. return false;
  158. }
  159. if (type == UPB_TYPE_STRING &&
  160. !is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) {
  161. zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
  162. return false;
  163. }
  164. // Handles repeated/map string field. Memory provided by
  165. // RepeatedField/Map is not initialized.
  166. #if PHP_MAJOR_VERSION < 7
  167. MAKE_STD_ZVAL(DEREF(memory, zval*));
  168. PHP_PROTO_ZVAL_STRINGL(DEREF(memory, zval*), Z_STRVAL_P(value),
  169. Z_STRLEN_P(value), 1);
  170. #else
  171. *(zend_string**)memory =
  172. zend_string_init(Z_STRVAL_P(value), Z_STRLEN_P(value), 0);
  173. #endif
  174. break;
  175. }
  176. case UPB_TYPE_MESSAGE: {
  177. if (Z_TYPE_P(value) != IS_OBJECT) {
  178. zend_error(E_USER_ERROR, "Given value is not message.");
  179. return false;
  180. }
  181. if (Z_TYPE_P(value) == IS_OBJECT && klass != Z_OBJCE_P(value)) {
  182. zend_error(E_USER_ERROR, "Given message does not have correct class.");
  183. return false;
  184. }
  185. #if PHP_MAJOR_VERSION < 7
  186. if (EXPECTED(DEREF(memory, zval*) != value)) {
  187. DEREF(memory, zval*) = value;
  188. Z_ADDREF_P(value);
  189. }
  190. #else
  191. DEREF(memory, zval*) = value;
  192. GC_ADDREF(Z_OBJ_P(value));
  193. #endif
  194. break;
  195. }
  196. default:
  197. return native_slot_set(type, klass, memory, value TSRMLS_CC);
  198. }
  199. return true;
  200. }
  201. bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass,
  202. void* memory, zval* value TSRMLS_DC) {
  203. #if PHP_MAJOR_VERSION >= 7
  204. if (Z_ISREF_P(value)) {
  205. ZVAL_DEREF(value);
  206. }
  207. #endif
  208. switch (type) {
  209. case UPB_TYPE_STRING:
  210. case UPB_TYPE_BYTES: {
  211. if (!protobuf_convert_to_string(value)) {
  212. return false;
  213. }
  214. if (type == UPB_TYPE_STRING &&
  215. !is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) {
  216. zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
  217. return false;
  218. }
  219. // Handles repeated/map string field. Memory provided by
  220. // RepeatedField/Map is not initialized.
  221. #if PHP_MAJOR_VERSION < 7
  222. MAKE_STD_ZVAL(DEREF(memory, zval*));
  223. PHP_PROTO_ZVAL_STRINGL(DEREF(memory, zval*), Z_STRVAL_P(value),
  224. Z_STRLEN_P(value), 1);
  225. #else
  226. *(zend_string**)memory =
  227. zend_string_init(Z_STRVAL_P(value), Z_STRLEN_P(value), 0);
  228. #endif
  229. break;
  230. }
  231. case UPB_TYPE_MESSAGE: {
  232. if (Z_TYPE_P(value) != IS_OBJECT) {
  233. zend_error(E_USER_ERROR, "Given value is not message.");
  234. return false;
  235. }
  236. if (Z_TYPE_P(value) == IS_OBJECT && klass != Z_OBJCE_P(value)) {
  237. zend_error(E_USER_ERROR, "Given message does not have correct class.");
  238. return false;
  239. }
  240. #if PHP_MAJOR_VERSION < 7
  241. if (EXPECTED(DEREF(memory, zval*) != value)) {
  242. DEREF(memory, zval*) = value;
  243. Z_ADDREF_P(value);
  244. }
  245. #else
  246. DEREF(memory, zend_object*) = Z_OBJ_P(value);
  247. GC_ADDREF(Z_OBJ_P(value));
  248. #endif
  249. break;
  250. }
  251. default:
  252. return native_slot_set(type, klass, memory, value TSRMLS_CC);
  253. }
  254. return true;
  255. }
  256. void native_slot_init(upb_fieldtype_t type, void* memory, CACHED_VALUE* cache) {
  257. zval* tmp = NULL;
  258. switch (type) {
  259. case UPB_TYPE_FLOAT:
  260. DEREF(memory, float) = 0.0;
  261. break;
  262. case UPB_TYPE_DOUBLE:
  263. DEREF(memory, double) = 0.0;
  264. break;
  265. case UPB_TYPE_BOOL:
  266. DEREF(memory, int8_t) = 0;
  267. break;
  268. case UPB_TYPE_STRING:
  269. case UPB_TYPE_BYTES:
  270. case UPB_TYPE_MESSAGE:
  271. DEREF(memory, CACHED_VALUE*) = cache;
  272. break;
  273. case UPB_TYPE_ENUM:
  274. case UPB_TYPE_INT32:
  275. DEREF(memory, int32_t) = 0;
  276. break;
  277. case UPB_TYPE_INT64:
  278. DEREF(memory, int64_t) = 0;
  279. break;
  280. case UPB_TYPE_UINT32:
  281. DEREF(memory, uint32_t) = 0;
  282. break;
  283. case UPB_TYPE_UINT64:
  284. DEREF(memory, uint64_t) = 0;
  285. break;
  286. default:
  287. break;
  288. }
  289. }
  290. void native_slot_get(upb_fieldtype_t type, const void* memory,
  291. CACHED_VALUE* cache TSRMLS_DC) {
  292. switch (type) {
  293. #define CASE(upb_type, php_type, c_type) \
  294. case UPB_TYPE_##upb_type: \
  295. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \
  296. ZVAL_##php_type(CACHED_PTR_TO_ZVAL_PTR(cache), DEREF(memory, c_type)); \
  297. return;
  298. CASE(FLOAT, DOUBLE, float)
  299. CASE(DOUBLE, DOUBLE, double)
  300. CASE(BOOL, BOOL, int8_t)
  301. CASE(INT32, LONG, int32_t)
  302. CASE(ENUM, LONG, uint32_t)
  303. #undef CASE
  304. #if SIZEOF_LONG == 4
  305. #define CASE(upb_type, c_type) \
  306. case UPB_TYPE_##upb_type: { \
  307. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \
  308. char buffer[MAX_LENGTH_OF_INT64]; \
  309. sprintf(buffer, "%lld", DEREF(memory, c_type)); \
  310. PHP_PROTO_ZVAL_STRING(CACHED_PTR_TO_ZVAL_PTR(cache), buffer, 1); \
  311. return; \
  312. }
  313. #else
  314. #define CASE(upb_type, c_type) \
  315. case UPB_TYPE_##upb_type: { \
  316. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \
  317. ZVAL_LONG(CACHED_PTR_TO_ZVAL_PTR(cache), DEREF(memory, c_type)); \
  318. return; \
  319. }
  320. #endif
  321. CASE(UINT64, uint64_t)
  322. CASE(INT64, int64_t)
  323. #undef CASE
  324. case UPB_TYPE_UINT32: {
  325. // Prepend bit-1 for negative numbers, so that uint32 value will be
  326. // consistent on both 32-bit and 64-bit architectures.
  327. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache);
  328. int value = DEREF(memory, int32_t);
  329. if (sizeof(int) == 8) {
  330. value |= (-((value >> 31) & 0x1) & 0xFFFFFFFF00000000);
  331. }
  332. ZVAL_LONG(CACHED_PTR_TO_ZVAL_PTR(cache), value);
  333. return;
  334. }
  335. case UPB_TYPE_STRING:
  336. case UPB_TYPE_BYTES: {
  337. // For optional string/bytes/message fields, the cache is owned by the
  338. // containing message and should have been updated during
  339. // setting/decoding. However, oneof accessor call this function by
  340. // providing the return value directly, which is not the same as the cache
  341. // value.
  342. zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  343. if (CACHED_PTR_TO_ZVAL_PTR(cache) != value) {
  344. PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache), Z_STRVAL_P(value),
  345. Z_STRLEN_P(value), 1);
  346. }
  347. break;
  348. }
  349. case UPB_TYPE_MESSAGE: {
  350. // Same as above for string/bytes fields.
  351. zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  352. if (CACHED_PTR_TO_ZVAL_PTR(cache) != value) {
  353. ZVAL_ZVAL(CACHED_PTR_TO_ZVAL_PTR(cache), value, 1, 0);
  354. }
  355. return;
  356. }
  357. default:
  358. return;
  359. }
  360. }
  361. void native_slot_get_by_array(upb_fieldtype_t type, const void* memory,
  362. CACHED_VALUE* cache TSRMLS_DC) {
  363. switch (type) {
  364. case UPB_TYPE_STRING:
  365. case UPB_TYPE_BYTES: {
  366. #if PHP_MAJOR_VERSION < 7
  367. zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  368. if (EXPECTED(CACHED_PTR_TO_ZVAL_PTR(cache) != value)) {
  369. PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache),
  370. Z_STRVAL_P(value), Z_STRLEN_P(value), 1);
  371. }
  372. #else
  373. ZVAL_NEW_STR(cache, zend_string_dup(*(zend_string**)memory, 0));
  374. #endif
  375. return;
  376. }
  377. case UPB_TYPE_MESSAGE: {
  378. #if PHP_MAJOR_VERSION < 7
  379. zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  380. if (EXPECTED(CACHED_PTR_TO_ZVAL_PTR(cache) != value)) {
  381. ZVAL_ZVAL(CACHED_PTR_TO_ZVAL_PTR(cache), value, 1, 0);
  382. }
  383. #else
  384. ZVAL_COPY(CACHED_PTR_TO_ZVAL_PTR(cache), memory);
  385. #endif
  386. return;
  387. }
  388. default:
  389. native_slot_get(type, memory, cache TSRMLS_CC);
  390. }
  391. }
  392. void native_slot_get_by_map_key(upb_fieldtype_t type, const void* memory,
  393. int length, CACHED_VALUE* cache TSRMLS_DC) {
  394. switch (type) {
  395. case UPB_TYPE_STRING:
  396. case UPB_TYPE_BYTES: {
  397. PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache), memory, length, 1);
  398. return;
  399. }
  400. default:
  401. native_slot_get(type, memory, cache TSRMLS_CC);
  402. }
  403. }
  404. void native_slot_get_by_map_value(upb_fieldtype_t type, const void* memory,
  405. CACHED_VALUE* cache TSRMLS_DC) {
  406. switch (type) {
  407. case UPB_TYPE_MESSAGE: {
  408. #if PHP_MAJOR_VERSION < 7
  409. zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  410. if (EXPECTED(CACHED_PTR_TO_ZVAL_PTR(cache) != value)) {
  411. ZVAL_ZVAL(CACHED_PTR_TO_ZVAL_PTR(cache), value, 1, 0);
  412. }
  413. #else
  414. GC_ADDREF(*(zend_object**)memory);
  415. ZVAL_OBJ(cache, *(zend_object**)memory);
  416. #endif
  417. return;
  418. }
  419. default:
  420. native_slot_get_by_array(type, memory, cache TSRMLS_CC);
  421. }
  422. }
  423. void native_slot_get_default(upb_fieldtype_t type,
  424. CACHED_VALUE* cache TSRMLS_DC) {
  425. switch (type) {
  426. #define CASE(upb_type, php_type) \
  427. case UPB_TYPE_##upb_type: \
  428. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \
  429. ZVAL_##php_type(CACHED_PTR_TO_ZVAL_PTR(cache), 0); \
  430. return;
  431. CASE(FLOAT, DOUBLE)
  432. CASE(DOUBLE, DOUBLE)
  433. CASE(BOOL, BOOL)
  434. CASE(INT32, LONG)
  435. CASE(UINT32, LONG)
  436. CASE(ENUM, LONG)
  437. #undef CASE
  438. #if SIZEOF_LONG == 4
  439. #define CASE(upb_type) \
  440. case UPB_TYPE_##upb_type: { \
  441. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \
  442. PHP_PROTO_ZVAL_STRING(CACHED_PTR_TO_ZVAL_PTR(cache), "0", 1); \
  443. return; \
  444. }
  445. #else
  446. #define CASE(upb_type) \
  447. case UPB_TYPE_##upb_type: { \
  448. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \
  449. ZVAL_LONG(CACHED_PTR_TO_ZVAL_PTR(cache), 0); \
  450. return; \
  451. }
  452. #endif
  453. CASE(UINT64)
  454. CASE(INT64)
  455. #undef CASE
  456. case UPB_TYPE_STRING:
  457. case UPB_TYPE_BYTES: {
  458. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache);
  459. PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache), "", 0, 1);
  460. break;
  461. }
  462. case UPB_TYPE_MESSAGE: {
  463. PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache);
  464. ZVAL_NULL(CACHED_PTR_TO_ZVAL_PTR(cache));
  465. return;
  466. }
  467. default:
  468. return;
  469. }
  470. }
  471. // -----------------------------------------------------------------------------
  472. // Map field utilities.
  473. // ----------------------------------------------------------------------------
  474. const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) {
  475. const upb_msgdef* subdef;
  476. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  477. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  478. return NULL;
  479. }
  480. subdef = upb_fielddef_msgsubdef(field);
  481. return upb_msgdef_mapentry(subdef) ? subdef : NULL;
  482. }
  483. const upb_msgdef* map_entry_msgdef(const upb_fielddef* field) {
  484. const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
  485. assert(subdef);
  486. return subdef;
  487. }
  488. bool is_map_field(const upb_fielddef* field) {
  489. return tryget_map_entry_msgdef(field) != NULL;
  490. }
  491. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  492. const upb_msgdef* subdef = map_entry_msgdef(field);
  493. return map_entry_key(subdef);
  494. }
  495. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  496. const upb_msgdef* subdef = map_entry_msgdef(field);
  497. return map_entry_value(subdef);
  498. }
  499. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  500. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  501. assert(key_field != NULL);
  502. return key_field;
  503. }
  504. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  505. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  506. assert(value_field != NULL);
  507. return value_field;
  508. }
  509. const zend_class_entry* field_type_class(
  510. const upb_fielddef* field PHP_PROTO_TSRMLS_DC) {
  511. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  512. Descriptor* desc = UNBOX_HASHTABLE_VALUE(
  513. Descriptor, get_def_obj(upb_fielddef_msgsubdef(field)));
  514. return desc->klass;
  515. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  516. EnumDescriptor* desc = UNBOX_HASHTABLE_VALUE(
  517. EnumDescriptor, get_def_obj(upb_fielddef_enumsubdef(field)));
  518. return desc->klass;
  519. }
  520. return NULL;
  521. }
  522. // -----------------------------------------------------------------------------
  523. // Memory layout management.
  524. // -----------------------------------------------------------------------------
  525. static size_t align_up_to(size_t offset, size_t granularity) {
  526. // Granularity must be a power of two.
  527. return (offset + granularity - 1) & ~(granularity - 1);
  528. }
  529. static uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage,
  530. const upb_fielddef* field) {
  531. return (uint32_t*)(((uint8_t*)storage) +
  532. layout->fields[upb_fielddef_index(field)].case_offset);
  533. }
  534. static int slot_property_cache(MessageLayout* layout, const void* storage,
  535. const upb_fielddef* field) {
  536. return layout->fields[upb_fielddef_index(field)].cache_index;
  537. }
  538. void* slot_memory(MessageLayout* layout, const void* storage,
  539. const upb_fielddef* field) {
  540. return ((uint8_t*)storage) + layout->fields[upb_fielddef_index(field)].offset;
  541. }
  542. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  543. MessageLayout* layout = ALLOC(MessageLayout);
  544. int nfields = upb_msgdef_numfields(msgdef);
  545. upb_msg_field_iter it;
  546. upb_msg_oneof_iter oit;
  547. size_t off = 0;
  548. int i = 0;
  549. // Reserve space for unknown fields.
  550. off += sizeof(void*);
  551. TSRMLS_FETCH();
  552. Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msgdef));
  553. layout->fields = ALLOC_N(MessageField, nfields);
  554. for (upb_msg_field_begin(&it, msgdef); !upb_msg_field_done(&it);
  555. upb_msg_field_next(&it)) {
  556. const upb_fielddef* field = upb_msg_iter_field(&it);
  557. size_t field_size;
  558. if (upb_fielddef_containingoneof(field)) {
  559. // Oneofs are handled separately below.
  560. continue;
  561. }
  562. // Allocate |field_size| bytes for this field in the layout.
  563. field_size = 0;
  564. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  565. field_size = sizeof(zval*);
  566. } else {
  567. field_size = native_slot_size(upb_fielddef_type(field));
  568. }
  569. // Align current offset up to | size | granularity.
  570. off = align_up_to(off, field_size);
  571. layout->fields[upb_fielddef_index(field)].offset = off;
  572. layout->fields[upb_fielddef_index(field)].case_offset =
  573. MESSAGE_FIELD_NO_CASE;
  574. const char* fieldname = upb_fielddef_name(field);
  575. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  576. zend_class_entry* old_scope = EG(scope);
  577. EG(scope) = desc->klass;
  578. #else
  579. zend_class_entry* old_scope = EG(fake_scope);
  580. EG(fake_scope) = desc->klass;
  581. #endif
  582. #if PHP_MAJOR_VERSION < 7
  583. zval member;
  584. ZVAL_STRINGL(&member, fieldname, strlen(fieldname), 0);
  585. zend_property_info* property_info =
  586. zend_get_property_info(desc->klass, &member, true TSRMLS_CC);
  587. #else
  588. zend_string* member = zend_string_init(fieldname, strlen(fieldname), 1);
  589. zend_property_info* property_info =
  590. zend_get_property_info(desc->klass, member, true);
  591. zend_string_release(member);
  592. #endif
  593. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  594. EG(scope) = old_scope;
  595. #else
  596. EG(fake_scope) = old_scope;
  597. #endif
  598. layout->fields[upb_fielddef_index(field)].cache_index =
  599. property_info->offset;
  600. off += field_size;
  601. }
  602. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  603. // one slot per oneof.
  604. //
  605. // We assign all value slots first, then pack the 'case' fields at the end,
  606. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  607. // bytes respectively and we want to avoid alignment overhead.
  608. //
  609. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  610. // space for oneof cases is conceptually as wide as field tag numbers. In
  611. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  612. // members (8 or 16 bits respectively), so conceivably we could assign
  613. // consecutive case numbers and then pick a smaller oneof case slot size, but
  614. // the complexity to implement this indirection is probably not worthwhile.
  615. for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit);
  616. upb_msg_oneof_next(&oit)) {
  617. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  618. upb_oneof_iter fit;
  619. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  620. // all fields.
  621. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  622. // Align the offset .
  623. off = align_up_to( off, field_size);
  624. // Assign all fields in the oneof this same offset.
  625. const char* oneofname = upb_oneofdef_name(oneof);
  626. for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit);
  627. upb_oneof_next(&fit)) {
  628. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  629. layout->fields[upb_fielddef_index(field)].offset = off;
  630. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  631. zend_class_entry* old_scope = EG(scope);
  632. EG(scope) = desc->klass;
  633. #else
  634. zend_class_entry* old_scope = EG(fake_scope);
  635. EG(fake_scope) = desc->klass;
  636. #endif
  637. #if PHP_MAJOR_VERSION < 7
  638. zval member;
  639. ZVAL_STRINGL(&member, oneofname, strlen(oneofname), 0);
  640. zend_property_info* property_info =
  641. zend_get_property_info(desc->klass, &member, true TSRMLS_CC);
  642. #else
  643. zend_string* member = zend_string_init(oneofname, strlen(oneofname), 1);
  644. zend_property_info* property_info =
  645. zend_get_property_info(desc->klass, member, true);
  646. zend_string_release(member);
  647. #endif
  648. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  649. EG(scope) = old_scope;
  650. #else
  651. EG(fake_scope) = old_scope;
  652. #endif
  653. layout->fields[upb_fielddef_index(field)].cache_index =
  654. property_info->offset;
  655. }
  656. i++;
  657. off += field_size;
  658. }
  659. // Now the case offset.
  660. for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit);
  661. upb_msg_oneof_next(&oit)) {
  662. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  663. upb_oneof_iter fit;
  664. size_t field_size = sizeof(uint32_t);
  665. // Align the offset .
  666. off = (off + field_size - 1) & ~(field_size - 1);
  667. // Assign all fields in the oneof this same offset.
  668. for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit);
  669. upb_oneof_next(&fit)) {
  670. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  671. layout->fields[upb_fielddef_index(field)].case_offset = off;
  672. }
  673. off += field_size;
  674. }
  675. layout->size = off;
  676. layout->msgdef = msgdef;
  677. return layout;
  678. }
  679. void free_layout(MessageLayout* layout) {
  680. FREE(layout->fields);
  681. FREE(layout);
  682. }
  683. void layout_init(MessageLayout* layout, void* storage,
  684. zend_object* object PHP_PROTO_TSRMLS_DC) {
  685. int i;
  686. upb_msg_field_iter it;
  687. // Init unknown fields
  688. memset(storage, 0, sizeof(void*));
  689. for (upb_msg_field_begin(&it, layout->msgdef), i = 0; !upb_msg_field_done(&it);
  690. upb_msg_field_next(&it), i++) {
  691. const upb_fielddef* field = upb_msg_iter_field(&it);
  692. void* memory = slot_memory(layout, storage, field);
  693. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  694. int cache_index = slot_property_cache(layout, storage, field);
  695. CACHED_VALUE* property_ptr = OBJ_PROP(object, cache_index);
  696. if (upb_fielddef_containingoneof(field)) {
  697. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  698. *oneof_case = ONEOF_CASE_NONE;
  699. } else if (is_map_field(field)) {
  700. zval_ptr_dtor(property_ptr);
  701. #if PHP_MAJOR_VERSION < 7
  702. MAKE_STD_ZVAL(*property_ptr);
  703. #endif
  704. map_field_create_with_field(map_field_type, field,
  705. property_ptr PHP_PROTO_TSRMLS_CC);
  706. DEREF(memory, CACHED_VALUE*) = property_ptr;
  707. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  708. zval_ptr_dtor(property_ptr);
  709. #if PHP_MAJOR_VERSION < 7
  710. MAKE_STD_ZVAL(*property_ptr);
  711. #endif
  712. repeated_field_create_with_field(repeated_field_type, field,
  713. property_ptr PHP_PROTO_TSRMLS_CC);
  714. DEREF(memory, CACHED_VALUE*) = property_ptr;
  715. } else {
  716. native_slot_init(upb_fielddef_type(field), memory, property_ptr);
  717. }
  718. }
  719. }
  720. // For non-singular fields, the related memory needs to point to the actual
  721. // zval in properties table first.
  722. static void* value_memory(const upb_fielddef* field, void* memory) {
  723. switch (upb_fielddef_type(field)) {
  724. case UPB_TYPE_STRING:
  725. case UPB_TYPE_BYTES:
  726. case UPB_TYPE_MESSAGE:
  727. memory = DEREF(memory, CACHED_VALUE*);
  728. break;
  729. default:
  730. // No operation
  731. break;
  732. }
  733. return memory;
  734. }
  735. zval* layout_get(MessageLayout* layout, const void* storage,
  736. const upb_fielddef* field, CACHED_VALUE* cache TSRMLS_DC) {
  737. void* memory = slot_memory(layout, storage, field);
  738. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  739. if (upb_fielddef_containingoneof(field)) {
  740. if (*oneof_case != upb_fielddef_number(field)) {
  741. native_slot_get_default(upb_fielddef_type(field), cache TSRMLS_CC);
  742. } else {
  743. native_slot_get(upb_fielddef_type(field), value_memory(field, memory),
  744. cache TSRMLS_CC);
  745. }
  746. return CACHED_PTR_TO_ZVAL_PTR(cache);
  747. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  748. return CACHED_PTR_TO_ZVAL_PTR(cache);
  749. } else {
  750. native_slot_get(upb_fielddef_type(field), value_memory(field, memory),
  751. cache TSRMLS_CC);
  752. return CACHED_PTR_TO_ZVAL_PTR(cache);
  753. }
  754. }
  755. void layout_set(MessageLayout* layout, MessageHeader* header,
  756. const upb_fielddef* field, zval* val TSRMLS_DC) {
  757. void* storage = message_data(header);
  758. void* memory = slot_memory(layout, storage, field);
  759. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  760. if (upb_fielddef_containingoneof(field)) {
  761. upb_fieldtype_t type = upb_fielddef_type(field);
  762. zend_class_entry *ce = NULL;
  763. // For non-singular fields, the related memory needs to point to the actual
  764. // zval in properties table first.
  765. switch (type) {
  766. case UPB_TYPE_MESSAGE: {
  767. const upb_msgdef* msg = upb_fielddef_msgsubdef(field);
  768. Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg));
  769. ce = desc->klass;
  770. // Intentionally fall through.
  771. }
  772. case UPB_TYPE_STRING:
  773. case UPB_TYPE_BYTES: {
  774. int property_cache_index =
  775. header->descriptor->layout->fields[upb_fielddef_index(field)]
  776. .cache_index;
  777. DEREF(memory, CACHED_VALUE*) =
  778. OBJ_PROP(&header->std, property_cache_index);
  779. memory = DEREF(memory, CACHED_VALUE*);
  780. break;
  781. }
  782. default:
  783. break;
  784. }
  785. native_slot_set(type, ce, memory, val TSRMLS_CC);
  786. *oneof_case = upb_fielddef_number(field);
  787. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  788. // Works for both repeated and map fields
  789. memory = DEREF(memory, void**);
  790. zval* property_ptr = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory);
  791. if (EXPECTED(property_ptr != val)) {
  792. zend_class_entry *subce = NULL;
  793. zval converted_value;
  794. if (upb_fielddef_ismap(field)) {
  795. const upb_msgdef* mapmsg = upb_fielddef_msgsubdef(field);
  796. const upb_fielddef* keyfield = upb_msgdef_ntof(mapmsg, "key", 3);
  797. const upb_fielddef* valuefield = upb_msgdef_ntof(mapmsg, "value", 5);
  798. if (upb_fielddef_descriptortype(valuefield) ==
  799. UPB_DESCRIPTOR_TYPE_MESSAGE) {
  800. const upb_msgdef* submsg = upb_fielddef_msgsubdef(valuefield);
  801. Descriptor* subdesc =
  802. UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(submsg));
  803. subce = subdesc->klass;
  804. }
  805. check_map_field(subce, upb_fielddef_descriptortype(keyfield),
  806. upb_fielddef_descriptortype(valuefield), val,
  807. &converted_value);
  808. } else {
  809. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  810. const upb_msgdef* submsg = upb_fielddef_msgsubdef(field);
  811. Descriptor* subdesc =
  812. UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(submsg));
  813. subce = subdesc->klass;
  814. }
  815. check_repeated_field(subce, upb_fielddef_descriptortype(field), val,
  816. &converted_value);
  817. }
  818. #if PHP_MAJOR_VERSION < 7
  819. REPLACE_ZVAL_VALUE((zval**)memory, &converted_value, 1);
  820. #else
  821. php_proto_zval_ptr_dtor(property_ptr);
  822. ZVAL_ZVAL(property_ptr, &converted_value, 1, 0);
  823. #endif
  824. zval_dtor(&converted_value);
  825. }
  826. } else {
  827. upb_fieldtype_t type = upb_fielddef_type(field);
  828. zend_class_entry *ce = NULL;
  829. if (type == UPB_TYPE_MESSAGE) {
  830. const upb_msgdef* msg = upb_fielddef_msgsubdef(field);
  831. Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg));
  832. ce = desc->klass;
  833. }
  834. native_slot_set(type, ce, value_memory(field, memory), val TSRMLS_CC);
  835. }
  836. }
  837. static void native_slot_merge(const upb_fielddef* field, const void* from_memory,
  838. void* to_memory PHP_PROTO_TSRMLS_DC) {
  839. upb_fieldtype_t type = upb_fielddef_type(field);
  840. zend_class_entry* ce = NULL;
  841. if (!native_slot_is_default(type, from_memory)) {
  842. switch (type) {
  843. #define CASE_TYPE(upb_type, c_type) \
  844. case UPB_TYPE_##upb_type: { \
  845. DEREF(to_memory, c_type) = DEREF(from_memory, c_type); \
  846. break; \
  847. }
  848. CASE_TYPE(INT32, int32_t)
  849. CASE_TYPE(UINT32, uint32_t)
  850. CASE_TYPE(ENUM, int32_t)
  851. CASE_TYPE(INT64, int64_t)
  852. CASE_TYPE(UINT64, uint64_t)
  853. CASE_TYPE(FLOAT, float)
  854. CASE_TYPE(DOUBLE, double)
  855. CASE_TYPE(BOOL, int8_t)
  856. #undef CASE_TYPE
  857. case UPB_TYPE_STRING:
  858. case UPB_TYPE_BYTES:
  859. native_slot_set(type, NULL, value_memory(field, to_memory),
  860. CACHED_PTR_TO_ZVAL_PTR(DEREF(
  861. from_memory, CACHED_VALUE*)) PHP_PROTO_TSRMLS_CC);
  862. break;
  863. case UPB_TYPE_MESSAGE: {
  864. const upb_msgdef* msg = upb_fielddef_msgsubdef(field);
  865. Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg));
  866. ce = desc->klass;
  867. if (native_slot_is_default(type, to_memory)) {
  868. #if PHP_MAJOR_VERSION < 7
  869. SEPARATE_ZVAL_IF_NOT_REF((zval**)value_memory(field, to_memory));
  870. #endif
  871. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(
  872. CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*)), ce);
  873. MessageHeader* submsg =
  874. UNBOX(MessageHeader,
  875. CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*)));
  876. custom_data_init(ce, submsg PHP_PROTO_TSRMLS_CC);
  877. }
  878. MessageHeader* sub_from =
  879. UNBOX(MessageHeader,
  880. CACHED_PTR_TO_ZVAL_PTR(DEREF(from_memory, CACHED_VALUE*)));
  881. MessageHeader* sub_to =
  882. UNBOX(MessageHeader,
  883. CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*)));
  884. layout_merge(desc->layout, sub_from, sub_to PHP_PROTO_TSRMLS_CC);
  885. break;
  886. }
  887. }
  888. }
  889. }
  890. static void native_slot_merge_by_array(const upb_fielddef* field, const void* from_memory,
  891. void* to_memory PHP_PROTO_TSRMLS_DC) {
  892. upb_fieldtype_t type = upb_fielddef_type(field);
  893. switch (type) {
  894. case UPB_TYPE_STRING:
  895. case UPB_TYPE_BYTES: {
  896. #if PHP_MAJOR_VERSION < 7
  897. MAKE_STD_ZVAL(DEREF(to_memory, zval*));
  898. PHP_PROTO_ZVAL_STRINGL(DEREF(to_memory, zval*),
  899. Z_STRVAL_P(*(zval**)from_memory),
  900. Z_STRLEN_P(*(zval**)from_memory), 1);
  901. #else
  902. DEREF(to_memory, zend_string*) =
  903. zend_string_dup(*(zend_string**)from_memory, 0);
  904. #endif
  905. break;
  906. }
  907. case UPB_TYPE_MESSAGE: {
  908. const upb_msgdef* msg = upb_fielddef_msgsubdef(field);
  909. Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg));
  910. zend_class_entry* ce = desc->klass;
  911. #if PHP_MAJOR_VERSION < 7
  912. MAKE_STD_ZVAL(DEREF(to_memory, zval*));
  913. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(DEREF(to_memory, zval*), ce);
  914. #else
  915. DEREF(to_memory, zend_object*) = ce->create_object(ce TSRMLS_CC);
  916. #endif
  917. MessageHeader* sub_from = UNBOX_HASHTABLE_VALUE(
  918. MessageHeader, DEREF(from_memory, PHP_PROTO_HASHTABLE_VALUE));
  919. MessageHeader* sub_to = UNBOX_HASHTABLE_VALUE(
  920. MessageHeader, DEREF(to_memory, PHP_PROTO_HASHTABLE_VALUE));
  921. custom_data_init(ce, sub_to PHP_PROTO_TSRMLS_CC);
  922. layout_merge(desc->layout, sub_from, sub_to PHP_PROTO_TSRMLS_CC);
  923. break;
  924. }
  925. default:
  926. native_slot_merge(field, from_memory, to_memory PHP_PROTO_TSRMLS_CC);
  927. break;
  928. }
  929. }
  930. void layout_merge(MessageLayout* layout, MessageHeader* from,
  931. MessageHeader* to PHP_PROTO_TSRMLS_DC) {
  932. int i, j;
  933. upb_msg_field_iter it;
  934. for (upb_msg_field_begin(&it, layout->msgdef), i = 0; !upb_msg_field_done(&it);
  935. upb_msg_field_next(&it), i++) {
  936. const upb_fielddef* field = upb_msg_iter_field(&it);
  937. void* to_memory = slot_memory(layout, message_data(to), field);
  938. void* from_memory = slot_memory(layout, message_data(from), field);
  939. if (upb_fielddef_containingoneof(field)) {
  940. uint32_t oneof_case_offset =
  941. layout->fields[upb_fielddef_index(field)].case_offset;
  942. // For a oneof, check that this field is actually present -- skip all the
  943. // below if not.
  944. if (DEREF((message_data(from) + oneof_case_offset), uint32_t) !=
  945. upb_fielddef_number(field)) {
  946. continue;
  947. }
  948. uint32_t* from_oneof_case = slot_oneof_case(layout, message_data(from), field);
  949. uint32_t* to_oneof_case = slot_oneof_case(layout, message_data(to), field);
  950. // For non-singular fields, the related memory needs to point to the
  951. // actual zval in properties table first.
  952. switch (upb_fielddef_type(field)) {
  953. case UPB_TYPE_MESSAGE:
  954. case UPB_TYPE_STRING:
  955. case UPB_TYPE_BYTES: {
  956. int property_cache_index =
  957. layout->fields[upb_fielddef_index(field)].cache_index;
  958. DEREF(to_memory, CACHED_VALUE*) =
  959. OBJ_PROP(&to->std, property_cache_index);
  960. break;
  961. }
  962. default:
  963. break;
  964. }
  965. *to_oneof_case = *from_oneof_case;
  966. // Otherwise, fall through to the appropriate singular-field handler
  967. // below.
  968. }
  969. if (is_map_field(field)) {
  970. int size, key_length, value_length;
  971. MapIter map_it;
  972. zval* to_map_php =
  973. CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*));
  974. zval* from_map_php =
  975. CACHED_PTR_TO_ZVAL_PTR(DEREF(from_memory, CACHED_VALUE*));
  976. Map* to_map = UNBOX(Map, to_map_php);
  977. Map* from_map = UNBOX(Map, from_map_php);
  978. size = upb_strtable_count(&from_map->table);
  979. if (size == 0) continue;
  980. const upb_msgdef *mapentry_def = upb_fielddef_msgsubdef(field);
  981. const upb_fielddef *value_field = upb_msgdef_itof(mapentry_def, 2);
  982. for (map_begin(from_map_php, &map_it TSRMLS_CC); !map_done(&map_it);
  983. map_next(&map_it)) {
  984. const char* key = map_iter_key(&map_it, &key_length);
  985. upb_value from_value = map_iter_value(&map_it, &value_length);
  986. upb_value to_value;
  987. void* from_mem = upb_value_memory(&from_value);
  988. void* to_mem = upb_value_memory(&to_value);
  989. memset(to_mem, 0, native_slot_size(to_map->value_type));
  990. native_slot_merge_by_array(value_field, from_mem,
  991. to_mem PHP_PROTO_TSRMLS_CC);
  992. map_index_set(to_map, key, key_length, to_value);
  993. }
  994. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  995. zval* to_array_php = CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*));
  996. zval* from_array_php = CACHED_PTR_TO_ZVAL_PTR(DEREF(from_memory, CACHED_VALUE*));
  997. RepeatedField* to_array = UNBOX(RepeatedField, to_array_php);
  998. RepeatedField* from_array = UNBOX(RepeatedField, from_array_php);
  999. int size = zend_hash_num_elements(PHP_PROTO_HASH_OF(from_array->array));
  1000. if (size > 0) {
  1001. for (j = 0; j < size; j++) {
  1002. void* from_memory = NULL;
  1003. void* to_memory =
  1004. ALLOC_N(char, native_slot_size(upb_fielddef_type(field)));
  1005. memset(to_memory, 0, native_slot_size(upb_fielddef_type(field)));
  1006. if (to_array->type == UPB_TYPE_MESSAGE) {
  1007. php_proto_zend_hash_index_find_zval(
  1008. PHP_PROTO_HASH_OF(from_array->array), j, (void**)&from_memory);
  1009. #if PHP_MAJOR_VERSION >= 7
  1010. from_memory = &Z_OBJ_P((zval*)from_memory);
  1011. #endif
  1012. } else {
  1013. php_proto_zend_hash_index_find_mem(
  1014. PHP_PROTO_HASH_OF(from_array->array), j, (void**)&from_memory);
  1015. }
  1016. native_slot_merge_by_array(field, from_memory,
  1017. to_memory PHP_PROTO_TSRMLS_CC);
  1018. repeated_field_push_native(to_array, to_memory);
  1019. FREE(to_memory);
  1020. }
  1021. }
  1022. } else {
  1023. native_slot_merge(field, from_memory, to_memory PHP_PROTO_TSRMLS_CC);
  1024. }
  1025. }
  1026. }
  1027. const char* layout_get_oneof_case(MessageLayout* layout, const void* storage,
  1028. const upb_oneofdef* oneof TSRMLS_DC) {
  1029. upb_oneof_iter i;
  1030. const upb_fielddef* first_field;
  1031. // Oneof is guaranteed to have at least one field. Get the first field.
  1032. for(upb_oneof_begin(&i, oneof); !upb_oneof_done(&i); upb_oneof_next(&i)) {
  1033. first_field = upb_oneof_iter_field(&i);
  1034. break;
  1035. }
  1036. uint32_t* oneof_case = slot_oneof_case(layout, storage, first_field);
  1037. if (*oneof_case == 0) {
  1038. return "";
  1039. }
  1040. const upb_fielddef* field = upb_oneofdef_itof(oneof, *oneof_case);
  1041. return upb_fielddef_name(field);
  1042. }