encode_decode.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. /* stringsink *****************************************************************/
  32. typedef struct {
  33. upb_byteshandler handler;
  34. upb_bytessink sink;
  35. char *ptr;
  36. size_t len, size;
  37. } stringsink;
  38. static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) {
  39. stringsink *sink = _sink;
  40. sink->len = 0;
  41. return sink;
  42. }
  43. static size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
  44. size_t len, const upb_bufhandle *handle) {
  45. stringsink *sink = _sink;
  46. size_t new_size = sink->size;
  47. UPB_UNUSED(hd);
  48. UPB_UNUSED(handle);
  49. while (sink->len + len > new_size) {
  50. new_size *= 2;
  51. }
  52. if (new_size != sink->size) {
  53. sink->ptr = realloc(sink->ptr, new_size);
  54. sink->size = new_size;
  55. }
  56. memcpy(sink->ptr + sink->len, ptr, len);
  57. sink->len += len;
  58. return len;
  59. }
  60. void stringsink_init(stringsink *sink) {
  61. upb_byteshandler_init(&sink->handler);
  62. upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL);
  63. upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL);
  64. upb_bytessink_reset(&sink->sink, &sink->handler, sink);
  65. sink->size = 32;
  66. sink->ptr = malloc(sink->size);
  67. sink->len = 0;
  68. }
  69. void stringsink_uninit(stringsink *sink) { free(sink->ptr); }
  70. /* stackenv *****************************************************************/
  71. // Stack-allocated context during an encode/decode operation. Contains the upb
  72. // environment and its stack-based allocator, an initial buffer for allocations
  73. // to avoid malloc() when possible, and a template for PHP exception messages
  74. // if any error occurs.
  75. #define STACK_ENV_STACKBYTES 4096
  76. typedef struct {
  77. upb_env env;
  78. const char *php_error_template;
  79. char allocbuf[STACK_ENV_STACKBYTES];
  80. } stackenv;
  81. static void stackenv_init(stackenv* se, const char* errmsg);
  82. static void stackenv_uninit(stackenv* se);
  83. // Callback invoked by upb if any error occurs during parsing or serialization.
  84. static bool env_error_func(void* ud, const upb_status* status) {
  85. stackenv* se = ud;
  86. // Free the env -- zend_error will longjmp up the stack past the
  87. // encode/decode function so it would not otherwise have been freed.
  88. stackenv_uninit(se);
  89. // TODO(teboring): have a way to verify that this is actually a parse error,
  90. // instead of just throwing "parse error" unconditionally.
  91. zend_error(E_ERROR, se->php_error_template, upb_status_errmsg(status));
  92. // Never reached.
  93. return false;
  94. }
  95. static void stackenv_init(stackenv* se, const char* errmsg) {
  96. se->php_error_template = errmsg;
  97. upb_env_init2(&se->env, se->allocbuf, sizeof(se->allocbuf), NULL);
  98. upb_env_seterrorfunc(&se->env, env_error_func, se);
  99. }
  100. static void stackenv_uninit(stackenv* se) {
  101. upb_env_uninit(&se->env);
  102. }
  103. // -----------------------------------------------------------------------------
  104. // Parsing.
  105. // -----------------------------------------------------------------------------
  106. #define DEREF(msg, ofs, type) *(type*)(((uint8_t *)msg) + ofs)
  107. // Creates a handlerdata that simply contains the offset for this field.
  108. static const void* newhandlerdata(upb_handlers* h, uint32_t ofs) {
  109. size_t* hd_ofs = (size_t*)malloc(sizeof(size_t));
  110. *hd_ofs = ofs;
  111. upb_handlers_addcleanup(h, hd_ofs, free);
  112. return hd_ofs;
  113. }
  114. typedef struct {
  115. size_t ofs;
  116. const upb_msgdef *md;
  117. } submsg_handlerdata_t;
  118. // Creates a handlerdata that contains offset and submessage type information.
  119. static const void *newsubmsghandlerdata(upb_handlers* h, uint32_t ofs,
  120. const upb_fielddef* f) {
  121. submsg_handlerdata_t* hd =
  122. (submsg_handlerdata_t*)malloc(sizeof(submsg_handlerdata_t));
  123. hd->ofs = ofs;
  124. hd->md = upb_fielddef_msgsubdef(f);
  125. upb_handlers_addcleanup(h, hd, free);
  126. return hd;
  127. }
  128. typedef struct {
  129. size_t ofs; // union data slot
  130. size_t case_ofs; // oneof_case field
  131. int property_ofs; // properties table cache
  132. uint32_t oneof_case_num; // oneof-case number to place in oneof_case field
  133. const upb_msgdef *md; // msgdef, for oneof submessage handler
  134. } oneof_handlerdata_t;
  135. static const void *newoneofhandlerdata(upb_handlers *h,
  136. uint32_t ofs,
  137. uint32_t case_ofs,
  138. int property_ofs,
  139. const upb_fielddef *f) {
  140. oneof_handlerdata_t* hd =
  141. (oneof_handlerdata_t*)malloc(sizeof(oneof_handlerdata_t));
  142. hd->ofs = ofs;
  143. hd->case_ofs = case_ofs;
  144. hd->property_ofs = property_ofs;
  145. // We reuse the field tag number as a oneof union discriminant tag. Note that
  146. // we don't expose these numbers to the user, so the only requirement is that
  147. // we have some unique ID for each union case/possibility. The field tag
  148. // numbers are already present and are easy to use so there's no reason to
  149. // create a separate ID space. In addition, using the field tag number here
  150. // lets us easily look up the field in the oneof accessor.
  151. hd->oneof_case_num = upb_fielddef_number(f);
  152. if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE) {
  153. hd->md = upb_fielddef_msgsubdef(f);
  154. } else {
  155. hd->md = NULL;
  156. }
  157. upb_handlers_addcleanup(h, hd, free);
  158. return hd;
  159. }
  160. // A handler that starts a repeated field. Gets the Repeated*Field instance for
  161. // this field (such an instance always exists even in an empty message).
  162. static void *startseq_handler(void* closure, const void* hd) {
  163. MessageHeader* msg = closure;
  164. const size_t *ofs = hd;
  165. return (void*)(*DEREF(msg, *ofs, zval**));
  166. }
  167. // Handlers that append primitive values to a repeated field.
  168. #define DEFINE_APPEND_HANDLER(type, ctype) \
  169. static bool append##type##_handler(void* closure, const void* hd, \
  170. ctype val TSRMLS_DC) { \
  171. zval* array = (zval*)closure; \
  172. RepeatedField* intern = \
  173. (RepeatedField*)zend_object_store_get_object(array TSRMLS_CC); \
  174. repeated_field_push_native(intern, &val TSRMLS_CC); \
  175. return true; \
  176. }
  177. DEFINE_APPEND_HANDLER(bool, bool)
  178. DEFINE_APPEND_HANDLER(int32, int32_t)
  179. DEFINE_APPEND_HANDLER(uint32, uint32_t)
  180. DEFINE_APPEND_HANDLER(float, float)
  181. DEFINE_APPEND_HANDLER(int64, int64_t)
  182. DEFINE_APPEND_HANDLER(uint64, uint64_t)
  183. DEFINE_APPEND_HANDLER(double, double)
  184. // Appends a string to a repeated field.
  185. static void* appendstr_handler(void *closure,
  186. const void *hd,
  187. size_t size_hint TSRMLS_DC) {
  188. zval* array = (zval*)closure;
  189. RepeatedField* intern =
  190. (RepeatedField*)zend_object_store_get_object(array TSRMLS_CC);
  191. zval* str;
  192. MAKE_STD_ZVAL(str);
  193. ZVAL_STRING(str, "", 1);
  194. repeated_field_push_native(intern, &str TSRMLS_CC);
  195. return (void*)str;
  196. }
  197. // Appends a 'bytes' string to a repeated field.
  198. static void* appendbytes_handler(void *closure,
  199. const void *hd,
  200. size_t size_hint TSRMLS_DC) {
  201. zval* array = (zval*)closure;
  202. RepeatedField* intern =
  203. (RepeatedField*)zend_object_store_get_object(array TSRMLS_CC);
  204. zval* str;
  205. MAKE_STD_ZVAL(str);
  206. ZVAL_STRING(str, "", 1);
  207. repeated_field_push_native(intern, &str TSRMLS_CC);
  208. return (void*)str;
  209. }
  210. static void *empty_php_string(zval** value_ptr) {
  211. SEPARATE_ZVAL_IF_NOT_REF(value_ptr);
  212. zval* str = *value_ptr;
  213. zval_dtor(str);
  214. ZVAL_STRINGL(str, "", 0, 1);
  215. return (void*)str;
  216. }
  217. // Sets a non-repeated string field in a message.
  218. static void* str_handler(void *closure,
  219. const void *hd,
  220. size_t size_hint) {
  221. MessageHeader* msg = closure;
  222. const size_t *ofs = hd;
  223. return empty_php_string(DEREF(msg, *ofs, zval**));
  224. }
  225. // Sets a non-repeated 'bytes' field in a message.
  226. static void* bytes_handler(void *closure,
  227. const void *hd,
  228. size_t size_hint) {
  229. MessageHeader* msg = closure;
  230. const size_t *ofs = hd;
  231. return empty_php_string(DEREF(msg, *ofs, zval**));
  232. }
  233. static size_t stringdata_handler(void* closure, const void* hd,
  234. const char* str, size_t len,
  235. const upb_bufhandle* handle) {
  236. zval* php_str = (zval*)closure;
  237. char* old_str = Z_STRVAL_P(php_str);
  238. size_t old_len = Z_STRLEN_P(php_str);
  239. assert(old_str != NULL);
  240. char* new_str = emalloc(old_len + len + 1);
  241. memcpy(new_str, old_str, old_len);
  242. memcpy(new_str + old_len, str, len);
  243. new_str[old_len + len] = 0;
  244. FREE(old_str);
  245. Z_STRVAL_P(php_str) = new_str;
  246. Z_STRLEN_P(php_str) = old_len + len;
  247. return len;
  248. }
  249. // Appends a submessage to a repeated field.
  250. static void *appendsubmsg_handler(void *closure, const void *hd TSRMLS_DC) {
  251. zval* array = (zval*)closure;
  252. RepeatedField* intern =
  253. (RepeatedField*)zend_object_store_get_object(array TSRMLS_CC);
  254. const submsg_handlerdata_t *submsgdata = hd;
  255. zval* subdesc_php = get_def_obj((void*)submsgdata->md);
  256. Descriptor* subdesc = zend_object_store_get_object(subdesc_php TSRMLS_CC);
  257. zend_class_entry* subklass = subdesc->klass;
  258. MessageHeader* submsg;
  259. zval* val = NULL;
  260. MAKE_STD_ZVAL(val);
  261. Z_TYPE_P(val) = IS_OBJECT;
  262. Z_OBJVAL_P(val) = subklass->create_object(subklass TSRMLS_CC);
  263. repeated_field_push_native(intern, &val TSRMLS_CC);
  264. submsg = zend_object_store_get_object(val TSRMLS_CC);
  265. return submsg;
  266. }
  267. // Sets a non-repeated submessage field in a message.
  268. static void *submsg_handler(void *closure, const void *hd TSRMLS_DC) {
  269. MessageHeader* msg = closure;
  270. const submsg_handlerdata_t* submsgdata = hd;
  271. zval* subdesc_php = get_def_obj((void*)submsgdata->md);
  272. Descriptor* subdesc = zend_object_store_get_object(subdesc_php TSRMLS_CC);
  273. zend_class_entry* subklass = subdesc->klass;
  274. zval* submsg_php;
  275. MessageHeader* submsg;
  276. if (Z_TYPE_P(*DEREF(msg, submsgdata->ofs, zval**)) == IS_NULL) {
  277. zval* val = NULL;
  278. MAKE_STD_ZVAL(val);
  279. Z_TYPE_P(val) = IS_OBJECT;
  280. Z_OBJVAL_P(val) = subklass->create_object(subklass TSRMLS_CC);
  281. zval_ptr_dtor(DEREF(msg, submsgdata->ofs, zval**));
  282. *DEREF(msg, submsgdata->ofs, zval**) = val;
  283. }
  284. submsg_php = *DEREF(msg, submsgdata->ofs, zval**);
  285. submsg = zend_object_store_get_object(submsg_php TSRMLS_CC);
  286. return submsg;
  287. }
  288. // Handler data for startmap/endmap handlers.
  289. typedef struct {
  290. size_t ofs;
  291. upb_fieldtype_t key_field_type;
  292. upb_fieldtype_t value_field_type;
  293. // We know that we can hold this reference because the handlerdata has the
  294. // same lifetime as the upb_handlers struct, and the upb_handlers struct holds
  295. // a reference to the upb_msgdef, which in turn has references to its subdefs.
  296. const upb_def* value_field_subdef;
  297. } map_handlerdata_t;
  298. // Temporary frame for map parsing: at the beginning of a map entry message, a
  299. // submsg handler allocates a frame to hold (i) a reference to the Map object
  300. // into which this message will be inserted and (ii) storage slots to
  301. // temporarily hold the key and value for this map entry until the end of the
  302. // submessage. When the submessage ends, another handler is called to insert the
  303. // value into the map.
  304. typedef struct {
  305. zval* map;
  306. char key_storage[NATIVE_SLOT_MAX_SIZE];
  307. char value_storage[NATIVE_SLOT_MAX_SIZE];
  308. } map_parse_frame_t;
  309. static void map_slot_init(void* memory, upb_fieldtype_t type) {
  310. switch (type) {
  311. case UPB_TYPE_STRING:
  312. case UPB_TYPE_BYTES: {
  313. // Store zval** in memory in order to be consistent with the layout of
  314. // singular fields.
  315. zval** holder = ALLOC(zval*);
  316. zval* tmp;
  317. MAKE_STD_ZVAL(tmp);
  318. ZVAL_STRINGL(tmp, "", 0, 1);
  319. *holder = tmp;
  320. *(zval***)memory = holder;
  321. break;
  322. }
  323. case UPB_TYPE_MESSAGE: {
  324. zval** holder = ALLOC(zval*);
  325. zval* tmp;
  326. MAKE_STD_ZVAL(tmp);
  327. ZVAL_NULL(tmp);
  328. *holder = tmp;
  329. *(zval***)memory = holder;
  330. break;
  331. }
  332. default:
  333. native_slot_init(type, memory, NULL);
  334. }
  335. }
  336. static void map_slot_uninit(void* memory, upb_fieldtype_t type) {
  337. switch (type) {
  338. case UPB_TYPE_MESSAGE:
  339. case UPB_TYPE_STRING:
  340. case UPB_TYPE_BYTES: {
  341. zval** holder = *(zval***)memory;
  342. zval_ptr_dtor(holder);
  343. FREE(holder);
  344. break;
  345. }
  346. default:
  347. break;
  348. }
  349. }
  350. static void map_slot_key(upb_fieldtype_t type, const void* from, char** keyval,
  351. size_t* length) {
  352. if (type == UPB_TYPE_STRING) {
  353. zval* key_php = **(zval***)from;
  354. *keyval = Z_STRVAL_P(key_php);
  355. *length = Z_STRLEN_P(key_php);
  356. } else {
  357. *keyval = from;
  358. *length = native_slot_size(type);
  359. }
  360. }
  361. static void map_slot_value(upb_fieldtype_t type, const void* from,
  362. upb_value* v) {
  363. size_t len;
  364. void* to = upb_value_memory(v);
  365. #ifndef NDEBUG
  366. v->ctype = UPB_CTYPE_UINT64;
  367. #endif
  368. memset(to, 0, native_slot_size(type));
  369. switch (type) {
  370. case UPB_TYPE_STRING:
  371. case UPB_TYPE_BYTES:
  372. case UPB_TYPE_MESSAGE: {
  373. *(zval**)to = **(zval***)from;
  374. Z_ADDREF_PP((zval**)to);
  375. break;
  376. }
  377. default:
  378. len = native_slot_size(type);
  379. memcpy(to, from, len);
  380. }
  381. }
  382. // Handler to begin a map entry: allocates a temporary frame. This is the
  383. // 'startsubmsg' handler on the msgdef that contains the map field.
  384. static void *startmapentry_handler(void *closure, const void *hd) {
  385. MessageHeader* msg = closure;
  386. const map_handlerdata_t* mapdata = hd;
  387. zval* map = *DEREF(msg, mapdata->ofs, zval**);
  388. map_parse_frame_t* frame = ALLOC(map_parse_frame_t);
  389. frame->map = map;
  390. map_slot_init(&frame->key_storage, mapdata->key_field_type);
  391. map_slot_init(&frame->value_storage, mapdata->value_field_type);
  392. return frame;
  393. }
  394. // Handler to end a map entry: inserts the value defined during the message into
  395. // the map. This is the 'endmsg' handler on the map entry msgdef.
  396. static bool endmap_handler(void *closure, const void *hd, upb_status* s
  397. TSRMLS_DC) {
  398. map_parse_frame_t* frame = closure;
  399. const map_handlerdata_t* mapdata = hd;
  400. Map *map = (Map *)zend_object_store_get_object(frame->map TSRMLS_CC);
  401. const char* keyval = NULL;
  402. upb_value v;
  403. size_t length;
  404. map_slot_key(map->key_type, &frame->key_storage, &keyval, &length);
  405. map_slot_value(map->value_type, &frame->value_storage, &v);
  406. map_index_set(map, keyval, length, v);
  407. map_slot_uninit(&frame->key_storage, mapdata->key_field_type);
  408. map_slot_uninit(&frame->value_storage, mapdata->value_field_type);
  409. FREE(frame);
  410. return true;
  411. }
  412. // Allocates a new map_handlerdata_t given the map entry message definition. If
  413. // the offset of the field within the parent message is also given, that is
  414. // added to the handler data as well. Note that this is called *twice* per map
  415. // field: once in the parent message handler setup when setting the startsubmsg
  416. // handler and once in the map entry message handler setup when setting the
  417. // key/value and endmsg handlers. The reason is that there is no easy way to
  418. // pass the handlerdata down to the sub-message handler setup.
  419. static map_handlerdata_t* new_map_handlerdata(
  420. size_t ofs,
  421. const upb_msgdef* mapentry_def,
  422. Descriptor* desc) {
  423. const upb_fielddef* key_field;
  424. const upb_fielddef* value_field;
  425. // TODO(teboring): Use emalloc and efree.
  426. map_handlerdata_t* hd =
  427. (map_handlerdata_t*)malloc(sizeof(map_handlerdata_t));
  428. hd->ofs = ofs;
  429. key_field = upb_msgdef_itof(mapentry_def, MAP_KEY_FIELD);
  430. assert(key_field != NULL);
  431. hd->key_field_type = upb_fielddef_type(key_field);
  432. value_field = upb_msgdef_itof(mapentry_def, MAP_VALUE_FIELD);
  433. assert(value_field != NULL);
  434. hd->value_field_type = upb_fielddef_type(value_field);
  435. hd->value_field_subdef = upb_fielddef_subdef(value_field);
  436. return hd;
  437. }
  438. // Handlers that set primitive values in oneofs.
  439. #define DEFINE_ONEOF_HANDLER(type, ctype) \
  440. static bool oneof##type##_handler(void *closure, const void *hd, \
  441. ctype val) { \
  442. const oneof_handlerdata_t *oneofdata = hd; \
  443. DEREF(closure, oneofdata->case_ofs, uint32_t) = \
  444. oneofdata->oneof_case_num; \
  445. DEREF(closure, oneofdata->ofs, ctype) = val; \
  446. return true; \
  447. }
  448. DEFINE_ONEOF_HANDLER(bool, bool)
  449. DEFINE_ONEOF_HANDLER(int32, int32_t)
  450. DEFINE_ONEOF_HANDLER(uint32, uint32_t)
  451. DEFINE_ONEOF_HANDLER(float, float)
  452. DEFINE_ONEOF_HANDLER(int64, int64_t)
  453. DEFINE_ONEOF_HANDLER(uint64, uint64_t)
  454. DEFINE_ONEOF_HANDLER(double, double)
  455. #undef DEFINE_ONEOF_HANDLER
  456. // Handlers for strings in a oneof.
  457. static void *oneofstr_handler(void *closure,
  458. const void *hd,
  459. size_t size_hint) {
  460. MessageHeader* msg = closure;
  461. const oneof_handlerdata_t *oneofdata = hd;
  462. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  463. oneofdata->oneof_case_num;
  464. DEREF(msg, oneofdata->ofs, zval**) =
  465. &(msg->std.properties_table)[oneofdata->property_ofs];
  466. return empty_php_string(DEREF(msg, oneofdata->ofs, zval**));
  467. }
  468. static void *oneofbytes_handler(void *closure,
  469. const void *hd,
  470. size_t size_hint) {
  471. MessageHeader* msg = closure;
  472. const oneof_handlerdata_t *oneofdata = hd;
  473. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  474. oneofdata->oneof_case_num;
  475. DEREF(msg, oneofdata->ofs, zval**) =
  476. &(msg->std.properties_table)[oneofdata->property_ofs];
  477. // TODO(teboring): Add it back.
  478. // rb_enc_associate(str, kRubyString8bitEncoding);
  479. SEPARATE_ZVAL_IF_NOT_REF(DEREF(msg, oneofdata->ofs, zval**));
  480. zval* str = *DEREF(msg, oneofdata->ofs, zval**);
  481. zval_dtor(str);
  482. ZVAL_STRINGL(str, "", 0, 1);
  483. return (void*)str;
  484. }
  485. // Handler for a submessage field in a oneof.
  486. static void *oneofsubmsg_handler(void *closure,
  487. const void *hd TSRMLS_DC) {
  488. MessageHeader* msg = closure;
  489. const oneof_handlerdata_t *oneofdata = hd;
  490. uint32_t oldcase = DEREF(msg, oneofdata->case_ofs, uint32_t);
  491. zval* subdesc_php = get_def_obj((void*)oneofdata->md);
  492. Descriptor* subdesc = zend_object_store_get_object(subdesc_php TSRMLS_CC);
  493. zend_class_entry* subklass = subdesc->klass;
  494. zval* submsg_php;
  495. MessageHeader* submsg;
  496. if (oldcase != oneofdata->oneof_case_num) {
  497. DEREF(msg, oneofdata->ofs, zval**) =
  498. &(msg->std.properties_table)[oneofdata->property_ofs];
  499. }
  500. if (Z_TYPE_P(*DEREF(msg, oneofdata->ofs, zval**)) == IS_NULL) {
  501. zval* val = NULL;
  502. MAKE_STD_ZVAL(val);
  503. Z_TYPE_P(val) = IS_OBJECT;
  504. Z_OBJVAL_P(val) = subklass->create_object(subklass TSRMLS_CC);
  505. zval_ptr_dtor(DEREF(msg, oneofdata->ofs, zval**));
  506. *DEREF(msg, oneofdata->ofs, zval**) = val;
  507. }
  508. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  509. oneofdata->oneof_case_num;
  510. submsg_php = *DEREF(msg, oneofdata->ofs, zval**);
  511. submsg = zend_object_store_get_object(submsg_php TSRMLS_CC);
  512. return submsg;
  513. }
  514. // Set up handlers for a repeated field.
  515. static void add_handlers_for_repeated_field(upb_handlers *h,
  516. const upb_fielddef *f,
  517. size_t offset) {
  518. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  519. upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset));
  520. upb_handlers_setstartseq(h, f, startseq_handler, &attr);
  521. upb_handlerattr_uninit(&attr);
  522. switch (upb_fielddef_type(f)) {
  523. #define SET_HANDLER(utype, ltype) \
  524. case utype: \
  525. upb_handlers_set##ltype(h, f, append##ltype##_handler, NULL); \
  526. break;
  527. SET_HANDLER(UPB_TYPE_BOOL, bool);
  528. SET_HANDLER(UPB_TYPE_INT32, int32);
  529. SET_HANDLER(UPB_TYPE_UINT32, uint32);
  530. SET_HANDLER(UPB_TYPE_ENUM, int32);
  531. SET_HANDLER(UPB_TYPE_FLOAT, float);
  532. SET_HANDLER(UPB_TYPE_INT64, int64);
  533. SET_HANDLER(UPB_TYPE_UINT64, uint64);
  534. SET_HANDLER(UPB_TYPE_DOUBLE, double);
  535. #undef SET_HANDLER
  536. case UPB_TYPE_STRING:
  537. case UPB_TYPE_BYTES: {
  538. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  539. upb_handlers_setstartstr(h, f, is_bytes ?
  540. appendbytes_handler : appendstr_handler,
  541. NULL);
  542. upb_handlers_setstring(h, f, stringdata_handler, NULL);
  543. break;
  544. }
  545. case UPB_TYPE_MESSAGE: {
  546. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  547. upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, f));
  548. upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr);
  549. upb_handlerattr_uninit(&attr);
  550. break;
  551. }
  552. }
  553. }
  554. // Set up handlers for a singular field.
  555. static void add_handlers_for_singular_field(upb_handlers *h,
  556. const upb_fielddef *f,
  557. size_t offset) {
  558. switch (upb_fielddef_type(f)) {
  559. case UPB_TYPE_BOOL:
  560. case UPB_TYPE_INT32:
  561. case UPB_TYPE_UINT32:
  562. case UPB_TYPE_ENUM:
  563. case UPB_TYPE_FLOAT:
  564. case UPB_TYPE_INT64:
  565. case UPB_TYPE_UINT64:
  566. case UPB_TYPE_DOUBLE:
  567. upb_shim_set(h, f, offset, -1);
  568. break;
  569. case UPB_TYPE_STRING:
  570. case UPB_TYPE_BYTES: {
  571. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  572. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  573. upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset));
  574. upb_handlers_setstartstr(h, f,
  575. is_bytes ? bytes_handler : str_handler,
  576. &attr);
  577. upb_handlers_setstring(h, f, stringdata_handler, &attr);
  578. upb_handlerattr_uninit(&attr);
  579. break;
  580. }
  581. case UPB_TYPE_MESSAGE: {
  582. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  583. upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, offset, f));
  584. upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr);
  585. upb_handlerattr_uninit(&attr);
  586. break;
  587. }
  588. }
  589. }
  590. // Adds handlers to a map field.
  591. static void add_handlers_for_mapfield(upb_handlers* h,
  592. const upb_fielddef* fielddef,
  593. size_t offset,
  594. Descriptor* desc) {
  595. const upb_msgdef* map_msgdef = upb_fielddef_msgsubdef(fielddef);
  596. map_handlerdata_t* hd = new_map_handlerdata(offset, map_msgdef, desc);
  597. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  598. upb_handlers_addcleanup(h, hd, free);
  599. upb_handlerattr_sethandlerdata(&attr, hd);
  600. upb_handlers_setstartsubmsg(h, fielddef, startmapentry_handler, &attr);
  601. upb_handlerattr_uninit(&attr);
  602. }
  603. // Adds handlers to a map-entry msgdef.
  604. static void add_handlers_for_mapentry(const upb_msgdef* msgdef, upb_handlers* h,
  605. Descriptor* desc) {
  606. const upb_fielddef* key_field = map_entry_key(msgdef);
  607. const upb_fielddef* value_field = map_entry_value(msgdef);
  608. map_handlerdata_t* hd = new_map_handlerdata(0, msgdef, desc);
  609. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  610. upb_handlers_addcleanup(h, hd, free);
  611. upb_handlerattr_sethandlerdata(&attr, hd);
  612. upb_handlers_setendmsg(h, endmap_handler, &attr);
  613. add_handlers_for_singular_field(h, key_field,
  614. offsetof(map_parse_frame_t, key_storage));
  615. add_handlers_for_singular_field(h, value_field,
  616. offsetof(map_parse_frame_t, value_storage));
  617. }
  618. // Set up handlers for a oneof field.
  619. static void add_handlers_for_oneof_field(upb_handlers *h,
  620. const upb_fielddef *f,
  621. size_t offset,
  622. size_t oneof_case_offset,
  623. int property_cache_offset) {
  624. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  625. upb_handlerattr_sethandlerdata(
  626. &attr, newoneofhandlerdata(h, offset, oneof_case_offset,
  627. property_cache_offset, f));
  628. switch (upb_fielddef_type(f)) {
  629. #define SET_HANDLER(utype, ltype) \
  630. case utype: \
  631. upb_handlers_set##ltype(h, f, oneof##ltype##_handler, &attr); \
  632. break;
  633. SET_HANDLER(UPB_TYPE_BOOL, bool);
  634. SET_HANDLER(UPB_TYPE_INT32, int32);
  635. SET_HANDLER(UPB_TYPE_UINT32, uint32);
  636. SET_HANDLER(UPB_TYPE_ENUM, int32);
  637. SET_HANDLER(UPB_TYPE_FLOAT, float);
  638. SET_HANDLER(UPB_TYPE_INT64, int64);
  639. SET_HANDLER(UPB_TYPE_UINT64, uint64);
  640. SET_HANDLER(UPB_TYPE_DOUBLE, double);
  641. #undef SET_HANDLER
  642. case UPB_TYPE_STRING:
  643. case UPB_TYPE_BYTES: {
  644. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  645. upb_handlers_setstartstr(h, f, is_bytes ?
  646. oneofbytes_handler : oneofstr_handler,
  647. &attr);
  648. upb_handlers_setstring(h, f, stringdata_handler, NULL);
  649. break;
  650. }
  651. case UPB_TYPE_MESSAGE: {
  652. upb_handlers_setstartsubmsg(h, f, oneofsubmsg_handler, &attr);
  653. break;
  654. }
  655. }
  656. upb_handlerattr_uninit(&attr);
  657. }
  658. static void add_handlers_for_message(const void *closure, upb_handlers *h
  659. TSRMLS_DC) {
  660. const upb_msgdef* msgdef = upb_handlers_msgdef(h);
  661. Descriptor* desc = (Descriptor*)zend_object_store_get_object(
  662. get_def_obj((void*)msgdef) TSRMLS_CC);
  663. upb_msg_field_iter i;
  664. // If this is a mapentry message type, set up a special set of handlers and
  665. // bail out of the normal (user-defined) message type handling.
  666. if (upb_msgdef_mapentry(msgdef)) {
  667. add_handlers_for_mapentry(msgdef, h, desc);
  668. return;
  669. }
  670. // Ensure layout exists. We may be invoked to create handlers for a given
  671. // message if we are included as a submsg of another message type before our
  672. // class is actually built, so to work around this, we just create the layout
  673. // (and handlers, in the class-building function) on-demand.
  674. if (desc->layout == NULL) {
  675. desc->layout = create_layout(desc->msgdef);
  676. }
  677. for (upb_msg_field_begin(&i, desc->msgdef);
  678. !upb_msg_field_done(&i);
  679. upb_msg_field_next(&i)) {
  680. const upb_fielddef *f = upb_msg_iter_field(&i);
  681. size_t offset = desc->layout->fields[upb_fielddef_index(f)].offset +
  682. sizeof(MessageHeader);
  683. if (upb_fielddef_containingoneof(f)) {
  684. size_t oneof_case_offset =
  685. desc->layout->fields[upb_fielddef_index(f)].case_offset +
  686. sizeof(MessageHeader);
  687. int property_cache_index =
  688. desc->layout->fields[upb_fielddef_index(f)].cache_index;
  689. add_handlers_for_oneof_field(h, f, offset, oneof_case_offset,
  690. property_cache_index);
  691. } else if (is_map_field(f)) {
  692. add_handlers_for_mapfield(h, f, offset, desc);
  693. } else if (upb_fielddef_isseq(f)) {
  694. add_handlers_for_repeated_field(h, f, offset);
  695. } else {
  696. add_handlers_for_singular_field(h, f, offset);
  697. }
  698. }
  699. }
  700. // Creates upb handlers for populating a message.
  701. static const upb_handlers *new_fill_handlers(Descriptor* desc,
  702. const void* owner) {
  703. // TODO(cfallin, haberman): once upb gets a caching/memoization layer for
  704. // handlers, reuse subdef handlers so that e.g. if we already parse
  705. // B-with-field-of-type-C, we don't have to rebuild the whole hierarchy to
  706. // parse A-with-field-of-type-B-with-field-of-type-C.
  707. return upb_handlers_newfrozen(desc->msgdef, owner,
  708. add_handlers_for_message, NULL);
  709. }
  710. // Constructs the handlers for filling a message's data into an in-memory
  711. // object.
  712. const upb_handlers* get_fill_handlers(Descriptor* desc) {
  713. if (!desc->fill_handlers) {
  714. desc->fill_handlers =
  715. new_fill_handlers(desc, &desc->fill_handlers);
  716. }
  717. return desc->fill_handlers;
  718. }
  719. const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor* desc,
  720. const void* owner) {
  721. const upb_handlers* handlers = get_fill_handlers(desc);
  722. upb_pbdecodermethodopts opts;
  723. upb_pbdecodermethodopts_init(&opts, handlers);
  724. return upb_pbdecodermethod_new(&opts, owner);
  725. }
  726. static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) {
  727. if (desc->fill_method == NULL) {
  728. desc->fill_method = new_fillmsg_decodermethod(
  729. desc, &desc->fill_method);
  730. }
  731. return desc->fill_method;
  732. }
  733. // -----------------------------------------------------------------------------
  734. // Serializing.
  735. // -----------------------------------------------------------------------------
  736. static void putmsg(zval* msg, const Descriptor* desc, upb_sink* sink,
  737. int depth TSRMLS_DC);
  738. static void putstr(zval* str, const upb_fielddef* f, upb_sink* sink);
  739. static void putrawstr(const char* str, int len, const upb_fielddef* f,
  740. upb_sink* sink);
  741. static void putsubmsg(zval* submsg, const upb_fielddef* f, upb_sink* sink,
  742. int depth TSRMLS_DC);
  743. static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink,
  744. int depth TSRMLS_DC);
  745. static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink, int depth
  746. TSRMLS_DC);
  747. static upb_selector_t getsel(const upb_fielddef* f, upb_handlertype_t type) {
  748. upb_selector_t ret;
  749. bool ok = upb_handlers_getselector(f, type, &ret);
  750. UPB_ASSERT(ok);
  751. return ret;
  752. }
  753. static void put_optional_value(void* memory, int len, const upb_fielddef* f,
  754. int depth, upb_sink* sink TSRMLS_DC) {
  755. assert(upb_fielddef_label(f) == UPB_LABEL_OPTIONAL);
  756. switch (upb_fielddef_type(f)) {
  757. #define T(upbtypeconst, upbtype, ctype, default_value) \
  758. case upbtypeconst: { \
  759. ctype value = DEREF(memory, 0, ctype); \
  760. if (value != default_value) { \
  761. upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); \
  762. upb_sink_put##upbtype(sink, sel, value); \
  763. } \
  764. } break;
  765. T(UPB_TYPE_FLOAT, float, float, 0.0)
  766. T(UPB_TYPE_DOUBLE, double, double, 0.0)
  767. T(UPB_TYPE_BOOL, bool, uint8_t, 0)
  768. T(UPB_TYPE_ENUM, int32, int32_t, 0)
  769. T(UPB_TYPE_INT32, int32, int32_t, 0)
  770. T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
  771. T(UPB_TYPE_INT64, int64, int64_t, 0)
  772. T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
  773. #undef T
  774. case UPB_TYPE_STRING:
  775. case UPB_TYPE_BYTES:
  776. putrawstr(memory, len, f, sink);
  777. break;
  778. case UPB_TYPE_MESSAGE: {
  779. zval* submsg = *(zval**)memory;
  780. putsubmsg(submsg, f, sink, depth TSRMLS_CC);
  781. break;
  782. }
  783. default:
  784. assert(false);
  785. }
  786. }
  787. // Only string/bytes fields are stored as zval.
  788. static const char* raw_value(void* memory, const upb_fielddef* f) {
  789. switch (upb_fielddef_type(f)) {
  790. case UPB_TYPE_STRING:
  791. case UPB_TYPE_BYTES:
  792. return Z_STRVAL_PP((zval**)memory);
  793. break;
  794. default:
  795. return memory;
  796. }
  797. }
  798. static int raw_value_len(void* memory, int len, const upb_fielddef* f) {
  799. switch (upb_fielddef_type(f)) {
  800. case UPB_TYPE_STRING:
  801. case UPB_TYPE_BYTES:
  802. return Z_STRLEN_PP((zval**)memory);
  803. break;
  804. default:
  805. return len;
  806. }
  807. }
  808. static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink,
  809. int depth TSRMLS_DC) {
  810. Map* self;
  811. upb_sink subsink;
  812. const upb_fielddef* key_field;
  813. const upb_fielddef* value_field;
  814. MapIter it;
  815. int len;
  816. if (map == NULL) return;
  817. self = UNBOX(Map, map);
  818. upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
  819. assert(upb_fielddef_type(f) == UPB_TYPE_MESSAGE);
  820. key_field = map_field_key(f);
  821. value_field = map_field_value(f);
  822. for (map_begin(map, &it TSRMLS_CC); !map_done(&it); map_next(&it)) {
  823. upb_status status;
  824. upb_sink entry_sink;
  825. upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG),
  826. &entry_sink);
  827. upb_sink_startmsg(&entry_sink);
  828. // Serialize key.
  829. const char *key = map_iter_key(&it, &len);
  830. put_optional_value(key, len, key_field, depth + 1, &entry_sink TSRMLS_CC);
  831. // Serialize value.
  832. upb_value value = map_iter_value(&it, &len);
  833. put_optional_value(raw_value(upb_value_memory(&value), value_field),
  834. raw_value_len(upb_value_memory(&value), len, value_field),
  835. value_field, depth + 1, &entry_sink TSRMLS_CC);
  836. upb_sink_endmsg(&entry_sink, &status);
  837. upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG));
  838. }
  839. upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
  840. }
  841. static void putmsg(zval* msg_php, const Descriptor* desc, upb_sink* sink,
  842. int depth TSRMLS_DC) {
  843. upb_msg_field_iter i;
  844. upb_status status;
  845. upb_sink_startmsg(sink);
  846. // Protect against cycles (possible because users may freely reassign message
  847. // and repeated fields) by imposing a maximum recursion depth.
  848. if (depth > ENCODE_MAX_NESTING) {
  849. zend_error(E_ERROR,
  850. "Maximum recursion depth exceeded during encoding.");
  851. }
  852. MessageHeader* msg = zend_object_store_get_object(msg_php TSRMLS_CC);
  853. for (upb_msg_field_begin(&i, desc->msgdef); !upb_msg_field_done(&i);
  854. upb_msg_field_next(&i)) {
  855. upb_fielddef* f = upb_msg_iter_field(&i);
  856. uint32_t offset = desc->layout->fields[upb_fielddef_index(f)].offset +
  857. sizeof(MessageHeader);
  858. if (upb_fielddef_containingoneof(f)) {
  859. uint32_t oneof_case_offset =
  860. desc->layout->fields[upb_fielddef_index(f)].case_offset +
  861. sizeof(MessageHeader);
  862. // For a oneof, check that this field is actually present -- skip all the
  863. // below if not.
  864. if (DEREF(msg, oneof_case_offset, uint32_t) != upb_fielddef_number(f)) {
  865. continue;
  866. }
  867. // Otherwise, fall through to the appropriate singular-field handler
  868. // below.
  869. }
  870. if (is_map_field(f)) {
  871. zval* map = *DEREF(msg, offset, zval**);
  872. if (map != NULL) {
  873. putmap(map, f, sink, depth TSRMLS_CC);
  874. }
  875. } else if (upb_fielddef_isseq(f)) {
  876. zval* array = *DEREF(msg, offset, zval**);
  877. if (array != NULL) {
  878. putarray(array, f, sink, depth TSRMLS_CC);
  879. }
  880. } else if (upb_fielddef_isstring(f)) {
  881. zval* str = *DEREF(msg, offset, zval**);
  882. if (Z_STRLEN_P(str) > 0) {
  883. putstr(str, f, sink);
  884. }
  885. } else if (upb_fielddef_issubmsg(f)) {
  886. putsubmsg(*DEREF(msg, offset, zval**), f, sink, depth TSRMLS_CC);
  887. } else {
  888. upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  889. #define T(upbtypeconst, upbtype, ctype, default_value) \
  890. case upbtypeconst: { \
  891. ctype value = DEREF(msg, offset, ctype); \
  892. if (value != default_value) { \
  893. upb_sink_put##upbtype(sink, sel, value); \
  894. } \
  895. } break;
  896. switch (upb_fielddef_type(f)) {
  897. T(UPB_TYPE_FLOAT, float, float, 0.0)
  898. T(UPB_TYPE_DOUBLE, double, double, 0.0)
  899. T(UPB_TYPE_BOOL, bool, uint8_t, 0)
  900. case UPB_TYPE_ENUM:
  901. T(UPB_TYPE_INT32, int32, int32_t, 0)
  902. T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
  903. T(UPB_TYPE_INT64, int64, int64_t, 0)
  904. T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
  905. case UPB_TYPE_STRING:
  906. case UPB_TYPE_BYTES:
  907. case UPB_TYPE_MESSAGE:
  908. zend_error(E_ERROR, "Internal error.");
  909. }
  910. #undef T
  911. }
  912. }
  913. upb_sink_endmsg(sink, &status);
  914. }
  915. static void putstr(zval* str, const upb_fielddef *f, upb_sink *sink) {
  916. upb_sink subsink;
  917. if (ZVAL_IS_NULL(str)) return;
  918. assert(Z_TYPE_P(str) == IS_STRING);
  919. // Ensure that the string has the correct encoding. We also check at field-set
  920. // time, but the user may have mutated the string object since then.
  921. if (upb_fielddef_type(f) == UPB_TYPE_STRING &&
  922. !is_structurally_valid_utf8(Z_STRVAL_P(str), Z_STRLEN_P(str))) {
  923. zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
  924. return;
  925. }
  926. upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), Z_STRLEN_P(str),
  927. &subsink);
  928. upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), Z_STRVAL_P(str),
  929. Z_STRLEN_P(str), NULL);
  930. upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
  931. }
  932. static void putrawstr(const char* str, int len, const upb_fielddef* f,
  933. upb_sink* sink) {
  934. upb_sink subsink;
  935. if (len == 0) return;
  936. // Ensure that the string has the correct encoding. We also check at field-set
  937. // time, but the user may have mutated the string object since then.
  938. if (upb_fielddef_type(f) == UPB_TYPE_STRING &&
  939. !is_structurally_valid_utf8(str, len)) {
  940. zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
  941. return;
  942. }
  943. upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), len, &subsink);
  944. upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), str, len, NULL);
  945. upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
  946. }
  947. static void putsubmsg(zval* submsg, const upb_fielddef* f, upb_sink* sink,
  948. int depth TSRMLS_DC) {
  949. upb_sink subsink;
  950. if (Z_TYPE_P(submsg) == IS_NULL) return;
  951. zval* php_descriptor = get_def_obj(upb_fielddef_msgsubdef(f));
  952. Descriptor* subdesc =
  953. (Descriptor*)zend_object_store_get_object(php_descriptor TSRMLS_CC);
  954. upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink);
  955. putmsg(submsg, subdesc, &subsink, depth + 1 TSRMLS_CC);
  956. upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG));
  957. }
  958. static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink,
  959. int depth TSRMLS_DC) {
  960. upb_sink subsink;
  961. upb_fieldtype_t type = upb_fielddef_type(f);
  962. upb_selector_t sel = 0;
  963. int size, i;
  964. assert(array != NULL);
  965. RepeatedField* intern =
  966. (RepeatedField*)zend_object_store_get_object(array TSRMLS_CC);
  967. size = zend_hash_num_elements(HASH_OF(intern->array));
  968. if (size == 0) return;
  969. upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
  970. if (upb_fielddef_isprimitive(f)) {
  971. sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  972. }
  973. for (i = 0; i < size; i++) {
  974. void* memory = repeated_field_index_native(intern, i TSRMLS_CC);
  975. switch (type) {
  976. #define T(upbtypeconst, upbtype, ctype) \
  977. case upbtypeconst: \
  978. upb_sink_put##upbtype(&subsink, sel, *((ctype*)memory)); \
  979. break;
  980. T(UPB_TYPE_FLOAT, float, float)
  981. T(UPB_TYPE_DOUBLE, double, double)
  982. T(UPB_TYPE_BOOL, bool, int8_t)
  983. case UPB_TYPE_ENUM:
  984. T(UPB_TYPE_INT32, int32, int32_t)
  985. T(UPB_TYPE_UINT32, uint32, uint32_t)
  986. T(UPB_TYPE_INT64, int64, int64_t)
  987. T(UPB_TYPE_UINT64, uint64, uint64_t)
  988. case UPB_TYPE_STRING:
  989. case UPB_TYPE_BYTES:
  990. putstr(*((zval**)memory), f, &subsink);
  991. break;
  992. case UPB_TYPE_MESSAGE:
  993. putsubmsg(*((zval**)memory), f, &subsink, depth TSRMLS_CC);
  994. break;
  995. #undef T
  996. }
  997. }
  998. upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
  999. }
  1000. static const upb_handlers* msgdef_pb_serialize_handlers(Descriptor* desc) {
  1001. if (desc->pb_serialize_handlers == NULL) {
  1002. desc->pb_serialize_handlers =
  1003. upb_pb_encoder_newhandlers(desc->msgdef, &desc->pb_serialize_handlers);
  1004. }
  1005. return desc->pb_serialize_handlers;
  1006. }
  1007. // -----------------------------------------------------------------------------
  1008. // PHP encode/decode methods
  1009. // -----------------------------------------------------------------------------
  1010. PHP_METHOD(Message, encode) {
  1011. zval* php_descriptor = get_ce_obj(Z_OBJCE_P(getThis()));
  1012. Descriptor* desc =
  1013. (Descriptor*)zend_object_store_get_object(php_descriptor TSRMLS_CC);
  1014. stringsink sink;
  1015. stringsink_init(&sink);
  1016. {
  1017. const upb_handlers* serialize_handlers = msgdef_pb_serialize_handlers(desc);
  1018. stackenv se;
  1019. upb_pb_encoder* encoder;
  1020. stackenv_init(&se, "Error occurred during encoding: %s");
  1021. encoder = upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink);
  1022. putmsg(getThis(), desc, upb_pb_encoder_input(encoder), 0 TSRMLS_CC);
  1023. RETVAL_STRINGL(sink.ptr, sink.len, 1);
  1024. stackenv_uninit(&se);
  1025. stringsink_uninit(&sink);
  1026. }
  1027. }
  1028. PHP_METHOD(Message, decode) {
  1029. zval* php_descriptor = get_ce_obj(Z_OBJCE_P(getThis()));
  1030. Descriptor* desc =
  1031. (Descriptor*)zend_object_store_get_object(php_descriptor TSRMLS_CC);
  1032. MessageHeader* msg = zend_object_store_get_object(getThis() TSRMLS_CC);
  1033. char *data = NULL;
  1034. int data_len;
  1035. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) ==
  1036. FAILURE) {
  1037. return;
  1038. }
  1039. {
  1040. const upb_pbdecodermethod* method = msgdef_decodermethod(desc);
  1041. const upb_handlers* h = upb_pbdecodermethod_desthandlers(method);
  1042. stackenv se;
  1043. upb_sink sink;
  1044. upb_pbdecoder* decoder;
  1045. stackenv_init(&se, "Error occurred during parsing: %s");
  1046. upb_sink_reset(&sink, h, msg);
  1047. decoder = upb_pbdecoder_create(&se.env, method, &sink);
  1048. upb_bufsrc_putbuf(data, data_len, upb_pbdecoder_input(decoder));
  1049. stackenv_uninit(&se);
  1050. }
  1051. }