encode_decode.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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) { \
  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); \
  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) {
  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) {
  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) {
  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) {
  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, upb_value* v) {
  362. size_t len;
  363. void* to = upb_value_memory(v);
  364. #ifndef NDEBUG
  365. v->ctype = UPB_CTYPE_UINT64;
  366. #endif
  367. memset(to, 0, native_slot_size(type));
  368. switch (type) {
  369. case UPB_TYPE_STRING:
  370. case UPB_TYPE_BYTES:
  371. case UPB_TYPE_MESSAGE: {
  372. *(zval**)to = **(zval***)from;
  373. Z_ADDREF_PP((zval**)to);
  374. break;
  375. }
  376. default:
  377. len = native_slot_size(type);
  378. memcpy(to, from, len);
  379. }
  380. }
  381. // Handler to begin a map entry: allocates a temporary frame. This is the
  382. // 'startsubmsg' handler on the msgdef that contains the map field.
  383. static void *startmapentry_handler(void *closure, const void *hd) {
  384. MessageHeader* msg = closure;
  385. const map_handlerdata_t* mapdata = hd;
  386. zval* map = *DEREF(msg, mapdata->ofs, zval**);
  387. map_parse_frame_t* frame = ALLOC(map_parse_frame_t);
  388. frame->map = map;
  389. map_slot_init(&frame->key_storage, mapdata->key_field_type);
  390. map_slot_init(&frame->value_storage, mapdata->value_field_type);
  391. return frame;
  392. }
  393. // Handler to end a map entry: inserts the value defined during the message into
  394. // the map. This is the 'endmsg' handler on the map entry msgdef.
  395. static bool endmap_handler(void *closure, const void *hd, upb_status* s) {
  396. map_parse_frame_t* frame = closure;
  397. const map_handlerdata_t* mapdata = hd;
  398. Map *map = (Map *)zend_object_store_get_object(frame->map TSRMLS_CC);
  399. const char* keyval = NULL;
  400. upb_value v;
  401. size_t length;
  402. map_slot_key(map->key_type, &frame->key_storage, &keyval, &length);
  403. map_slot_value(map->value_type, &frame->value_storage, &v);
  404. map_index_set(map, keyval, length, v);
  405. map_slot_uninit(&frame->key_storage, mapdata->key_field_type);
  406. map_slot_uninit(&frame->value_storage, mapdata->value_field_type);
  407. FREE(frame);
  408. return true;
  409. }
  410. // Allocates a new map_handlerdata_t given the map entry message definition. If
  411. // the offset of the field within the parent message is also given, that is
  412. // added to the handler data as well. Note that this is called *twice* per map
  413. // field: once in the parent message handler setup when setting the startsubmsg
  414. // handler and once in the map entry message handler setup when setting the
  415. // key/value and endmsg handlers. The reason is that there is no easy way to
  416. // pass the handlerdata down to the sub-message handler setup.
  417. static map_handlerdata_t* new_map_handlerdata(
  418. size_t ofs,
  419. const upb_msgdef* mapentry_def,
  420. Descriptor* desc) {
  421. const upb_fielddef* key_field;
  422. const upb_fielddef* value_field;
  423. // TODO(teboring): Use emalloc and efree.
  424. map_handlerdata_t* hd =
  425. (map_handlerdata_t*)malloc(sizeof(map_handlerdata_t));
  426. hd->ofs = ofs;
  427. key_field = upb_msgdef_itof(mapentry_def, MAP_KEY_FIELD);
  428. assert(key_field != NULL);
  429. hd->key_field_type = upb_fielddef_type(key_field);
  430. value_field = upb_msgdef_itof(mapentry_def, MAP_VALUE_FIELD);
  431. assert(value_field != NULL);
  432. hd->value_field_type = upb_fielddef_type(value_field);
  433. hd->value_field_subdef = upb_fielddef_subdef(value_field);
  434. return hd;
  435. }
  436. // Handlers that set primitive values in oneofs.
  437. #define DEFINE_ONEOF_HANDLER(type, ctype) \
  438. static bool oneof##type##_handler(void *closure, const void *hd, \
  439. ctype val) { \
  440. const oneof_handlerdata_t *oneofdata = hd; \
  441. DEREF(closure, oneofdata->case_ofs, uint32_t) = \
  442. oneofdata->oneof_case_num; \
  443. DEREF(closure, oneofdata->ofs, ctype) = val; \
  444. return true; \
  445. }
  446. DEFINE_ONEOF_HANDLER(bool, bool)
  447. DEFINE_ONEOF_HANDLER(int32, int32_t)
  448. DEFINE_ONEOF_HANDLER(uint32, uint32_t)
  449. DEFINE_ONEOF_HANDLER(float, float)
  450. DEFINE_ONEOF_HANDLER(int64, int64_t)
  451. DEFINE_ONEOF_HANDLER(uint64, uint64_t)
  452. DEFINE_ONEOF_HANDLER(double, double)
  453. #undef DEFINE_ONEOF_HANDLER
  454. // Handlers for strings in a oneof.
  455. static void *oneofstr_handler(void *closure,
  456. const void *hd,
  457. size_t size_hint) {
  458. MessageHeader* msg = closure;
  459. const oneof_handlerdata_t *oneofdata = hd;
  460. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  461. oneofdata->oneof_case_num;
  462. DEREF(msg, oneofdata->ofs, zval**) =
  463. &(msg->std.properties_table)[oneofdata->property_ofs];
  464. return empty_php_string(DEREF(msg, oneofdata->ofs, zval**));
  465. }
  466. static void *oneofbytes_handler(void *closure,
  467. const void *hd,
  468. size_t size_hint) {
  469. MessageHeader* msg = closure;
  470. const oneof_handlerdata_t *oneofdata = hd;
  471. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  472. oneofdata->oneof_case_num;
  473. DEREF(msg, oneofdata->ofs, zval**) =
  474. &(msg->std.properties_table)[oneofdata->property_ofs];
  475. // TODO(teboring): Add it back.
  476. // rb_enc_associate(str, kRubyString8bitEncoding);
  477. SEPARATE_ZVAL_IF_NOT_REF(DEREF(msg, oneofdata->ofs, zval**));
  478. zval* str = *DEREF(msg, oneofdata->ofs, zval**);
  479. zval_dtor(str);
  480. ZVAL_STRINGL(str, "", 0, 1);
  481. return (void*)str;
  482. }
  483. // Handler for a submessage field in a oneof.
  484. static void *oneofsubmsg_handler(void *closure,
  485. const void *hd) {
  486. MessageHeader* msg = closure;
  487. const oneof_handlerdata_t *oneofdata = hd;
  488. uint32_t oldcase = DEREF(msg, oneofdata->case_ofs, uint32_t);
  489. zval* subdesc_php = get_def_obj((void*)oneofdata->md);
  490. Descriptor* subdesc = zend_object_store_get_object(subdesc_php TSRMLS_CC);
  491. zend_class_entry* subklass = subdesc->klass;
  492. zval* submsg_php;
  493. MessageHeader* submsg;
  494. if (oldcase != oneofdata->oneof_case_num) {
  495. DEREF(msg, oneofdata->ofs, zval**) =
  496. &(msg->std.properties_table)[oneofdata->property_ofs];
  497. }
  498. if (Z_TYPE_P(*DEREF(msg, oneofdata->ofs, zval**)) == IS_NULL) {
  499. zval* val = NULL;
  500. MAKE_STD_ZVAL(val);
  501. Z_TYPE_P(val) = IS_OBJECT;
  502. Z_OBJVAL_P(val) = subklass->create_object(subklass TSRMLS_CC);
  503. zval_ptr_dtor(DEREF(msg, oneofdata->ofs, zval**));
  504. *DEREF(msg, oneofdata->ofs, zval**) = val;
  505. }
  506. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  507. oneofdata->oneof_case_num;
  508. submsg_php = *DEREF(msg, oneofdata->ofs, zval**);
  509. submsg = zend_object_store_get_object(submsg_php TSRMLS_CC);
  510. return submsg;
  511. }
  512. // Set up handlers for a repeated field.
  513. static void add_handlers_for_repeated_field(upb_handlers *h,
  514. const upb_fielddef *f,
  515. size_t offset) {
  516. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  517. upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset));
  518. upb_handlers_setstartseq(h, f, startseq_handler, &attr);
  519. upb_handlerattr_uninit(&attr);
  520. switch (upb_fielddef_type(f)) {
  521. #define SET_HANDLER(utype, ltype) \
  522. case utype: \
  523. upb_handlers_set##ltype(h, f, append##ltype##_handler, NULL); \
  524. break;
  525. SET_HANDLER(UPB_TYPE_BOOL, bool);
  526. SET_HANDLER(UPB_TYPE_INT32, int32);
  527. SET_HANDLER(UPB_TYPE_UINT32, uint32);
  528. SET_HANDLER(UPB_TYPE_ENUM, int32);
  529. SET_HANDLER(UPB_TYPE_FLOAT, float);
  530. SET_HANDLER(UPB_TYPE_INT64, int64);
  531. SET_HANDLER(UPB_TYPE_UINT64, uint64);
  532. SET_HANDLER(UPB_TYPE_DOUBLE, double);
  533. #undef SET_HANDLER
  534. case UPB_TYPE_STRING:
  535. case UPB_TYPE_BYTES: {
  536. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  537. upb_handlers_setstartstr(h, f, is_bytes ?
  538. appendbytes_handler : appendstr_handler,
  539. NULL);
  540. upb_handlers_setstring(h, f, stringdata_handler, NULL);
  541. break;
  542. }
  543. case UPB_TYPE_MESSAGE: {
  544. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  545. upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, f));
  546. upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr);
  547. upb_handlerattr_uninit(&attr);
  548. break;
  549. }
  550. }
  551. }
  552. // Set up handlers for a singular field.
  553. static void add_handlers_for_singular_field(upb_handlers *h,
  554. const upb_fielddef *f,
  555. size_t offset) {
  556. switch (upb_fielddef_type(f)) {
  557. case UPB_TYPE_BOOL:
  558. case UPB_TYPE_INT32:
  559. case UPB_TYPE_UINT32:
  560. case UPB_TYPE_ENUM:
  561. case UPB_TYPE_FLOAT:
  562. case UPB_TYPE_INT64:
  563. case UPB_TYPE_UINT64:
  564. case UPB_TYPE_DOUBLE:
  565. upb_shim_set(h, f, offset, -1);
  566. break;
  567. case UPB_TYPE_STRING:
  568. case UPB_TYPE_BYTES: {
  569. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  570. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  571. upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset));
  572. upb_handlers_setstartstr(h, f,
  573. is_bytes ? bytes_handler : str_handler,
  574. &attr);
  575. upb_handlers_setstring(h, f, stringdata_handler, &attr);
  576. upb_handlerattr_uninit(&attr);
  577. break;
  578. }
  579. case UPB_TYPE_MESSAGE: {
  580. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  581. upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, offset, f));
  582. upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr);
  583. upb_handlerattr_uninit(&attr);
  584. break;
  585. }
  586. }
  587. }
  588. // Adds handlers to a map field.
  589. static void add_handlers_for_mapfield(upb_handlers* h,
  590. const upb_fielddef* fielddef,
  591. size_t offset,
  592. Descriptor* desc) {
  593. const upb_msgdef* map_msgdef = upb_fielddef_msgsubdef(fielddef);
  594. map_handlerdata_t* hd = new_map_handlerdata(offset, map_msgdef, desc);
  595. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  596. upb_handlers_addcleanup(h, hd, free);
  597. upb_handlerattr_sethandlerdata(&attr, hd);
  598. upb_handlers_setstartsubmsg(h, fielddef, startmapentry_handler, &attr);
  599. upb_handlerattr_uninit(&attr);
  600. }
  601. // Adds handlers to a map-entry msgdef.
  602. static void add_handlers_for_mapentry(const upb_msgdef* msgdef, upb_handlers* h,
  603. Descriptor* desc) {
  604. const upb_fielddef* key_field = map_entry_key(msgdef);
  605. const upb_fielddef* value_field = map_entry_value(msgdef);
  606. map_handlerdata_t* hd = new_map_handlerdata(0, msgdef, desc);
  607. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  608. upb_handlers_addcleanup(h, hd, free);
  609. upb_handlerattr_sethandlerdata(&attr, hd);
  610. upb_handlers_setendmsg(h, endmap_handler, &attr);
  611. add_handlers_for_singular_field(h, key_field,
  612. offsetof(map_parse_frame_t, key_storage));
  613. add_handlers_for_singular_field(h, value_field,
  614. offsetof(map_parse_frame_t, value_storage));
  615. }
  616. // Set up handlers for a oneof field.
  617. static void add_handlers_for_oneof_field(upb_handlers *h,
  618. const upb_fielddef *f,
  619. size_t offset,
  620. size_t oneof_case_offset,
  621. int property_cache_offset) {
  622. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  623. upb_handlerattr_sethandlerdata(
  624. &attr, newoneofhandlerdata(h, offset, oneof_case_offset,
  625. property_cache_offset, f));
  626. switch (upb_fielddef_type(f)) {
  627. #define SET_HANDLER(utype, ltype) \
  628. case utype: \
  629. upb_handlers_set##ltype(h, f, oneof##ltype##_handler, &attr); \
  630. break;
  631. SET_HANDLER(UPB_TYPE_BOOL, bool);
  632. SET_HANDLER(UPB_TYPE_INT32, int32);
  633. SET_HANDLER(UPB_TYPE_UINT32, uint32);
  634. SET_HANDLER(UPB_TYPE_ENUM, int32);
  635. SET_HANDLER(UPB_TYPE_FLOAT, float);
  636. SET_HANDLER(UPB_TYPE_INT64, int64);
  637. SET_HANDLER(UPB_TYPE_UINT64, uint64);
  638. SET_HANDLER(UPB_TYPE_DOUBLE, double);
  639. #undef SET_HANDLER
  640. case UPB_TYPE_STRING:
  641. case UPB_TYPE_BYTES: {
  642. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  643. upb_handlers_setstartstr(h, f, is_bytes ?
  644. oneofbytes_handler : oneofstr_handler,
  645. &attr);
  646. upb_handlers_setstring(h, f, stringdata_handler, NULL);
  647. break;
  648. }
  649. case UPB_TYPE_MESSAGE: {
  650. upb_handlers_setstartsubmsg(h, f, oneofsubmsg_handler, &attr);
  651. break;
  652. }
  653. }
  654. upb_handlerattr_uninit(&attr);
  655. }
  656. static void add_handlers_for_message(const void *closure, upb_handlers *h) {
  657. const upb_msgdef* msgdef = upb_handlers_msgdef(h);
  658. Descriptor* desc = (Descriptor*)zend_object_store_get_object(
  659. get_def_obj((void*)msgdef) TSRMLS_CC);
  660. upb_msg_field_iter i;
  661. // If this is a mapentry message type, set up a special set of handlers and
  662. // bail out of the normal (user-defined) message type handling.
  663. if (upb_msgdef_mapentry(msgdef)) {
  664. add_handlers_for_mapentry(msgdef, h, desc);
  665. return;
  666. }
  667. // Ensure layout exists. We may be invoked to create handlers for a given
  668. // message if we are included as a submsg of another message type before our
  669. // class is actually built, so to work around this, we just create the layout
  670. // (and handlers, in the class-building function) on-demand.
  671. if (desc->layout == NULL) {
  672. desc->layout = create_layout(desc->msgdef);
  673. }
  674. for (upb_msg_field_begin(&i, desc->msgdef);
  675. !upb_msg_field_done(&i);
  676. upb_msg_field_next(&i)) {
  677. const upb_fielddef *f = upb_msg_iter_field(&i);
  678. size_t offset = desc->layout->fields[upb_fielddef_index(f)].offset +
  679. sizeof(MessageHeader);
  680. if (upb_fielddef_containingoneof(f)) {
  681. size_t oneof_case_offset =
  682. desc->layout->fields[upb_fielddef_index(f)].case_offset +
  683. sizeof(MessageHeader);
  684. int property_cache_index =
  685. desc->layout->fields[upb_fielddef_index(f)].cache_index;
  686. add_handlers_for_oneof_field(h, f, offset, oneof_case_offset,
  687. property_cache_index);
  688. } else if (is_map_field(f)) {
  689. add_handlers_for_mapfield(h, f, offset, desc);
  690. } else if (upb_fielddef_isseq(f)) {
  691. add_handlers_for_repeated_field(h, f, offset);
  692. } else {
  693. add_handlers_for_singular_field(h, f, offset);
  694. }
  695. }
  696. }
  697. // Creates upb handlers for populating a message.
  698. static const upb_handlers *new_fill_handlers(Descriptor* desc,
  699. const void* owner) {
  700. // TODO(cfallin, haberman): once upb gets a caching/memoization layer for
  701. // handlers, reuse subdef handlers so that e.g. if we already parse
  702. // B-with-field-of-type-C, we don't have to rebuild the whole hierarchy to
  703. // parse A-with-field-of-type-B-with-field-of-type-C.
  704. return upb_handlers_newfrozen(desc->msgdef, owner,
  705. add_handlers_for_message, NULL);
  706. }
  707. // Constructs the handlers for filling a message's data into an in-memory
  708. // object.
  709. const upb_handlers* get_fill_handlers(Descriptor* desc) {
  710. if (!desc->fill_handlers) {
  711. desc->fill_handlers =
  712. new_fill_handlers(desc, &desc->fill_handlers);
  713. }
  714. return desc->fill_handlers;
  715. }
  716. const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor* desc,
  717. const void* owner) {
  718. const upb_handlers* handlers = get_fill_handlers(desc);
  719. upb_pbdecodermethodopts opts;
  720. upb_pbdecodermethodopts_init(&opts, handlers);
  721. return upb_pbdecodermethod_new(&opts, owner);
  722. }
  723. static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) {
  724. if (desc->fill_method == NULL) {
  725. desc->fill_method = new_fillmsg_decodermethod(
  726. desc, &desc->fill_method);
  727. }
  728. return desc->fill_method;
  729. }
  730. // -----------------------------------------------------------------------------
  731. // Serializing.
  732. // -----------------------------------------------------------------------------
  733. static void putmsg(zval* msg, const Descriptor* desc, upb_sink* sink,
  734. int depth);
  735. static void putstr(zval* str, const upb_fielddef* f, upb_sink* sink);
  736. static void putrawstr(const char* str, int len, const upb_fielddef* f,
  737. upb_sink* sink);
  738. static void putsubmsg(zval* submsg, const upb_fielddef* f, upb_sink* sink,
  739. int depth);
  740. static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink,
  741. int depth);
  742. static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink, int depth);
  743. static upb_selector_t getsel(const upb_fielddef* f, upb_handlertype_t type) {
  744. upb_selector_t ret;
  745. bool ok = upb_handlers_getselector(f, type, &ret);
  746. UPB_ASSERT(ok);
  747. return ret;
  748. }
  749. static void put_optional_value(void* memory, int len, const upb_fielddef* f,
  750. int depth, upb_sink* sink) {
  751. assert(upb_fielddef_label(f) == UPB_LABEL_OPTIONAL);
  752. switch (upb_fielddef_type(f)) {
  753. #define T(upbtypeconst, upbtype, ctype, default_value) \
  754. case upbtypeconst: { \
  755. ctype value = DEREF(memory, 0, ctype); \
  756. if (value != default_value) { \
  757. upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); \
  758. upb_sink_put##upbtype(sink, sel, value); \
  759. } \
  760. } break;
  761. T(UPB_TYPE_FLOAT, float, float, 0.0)
  762. T(UPB_TYPE_DOUBLE, double, double, 0.0)
  763. T(UPB_TYPE_BOOL, bool, uint8_t, 0)
  764. T(UPB_TYPE_ENUM, int32, int32_t, 0)
  765. T(UPB_TYPE_INT32, int32, int32_t, 0)
  766. T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
  767. T(UPB_TYPE_INT64, int64, int64_t, 0)
  768. T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
  769. #undef T
  770. case UPB_TYPE_STRING:
  771. case UPB_TYPE_BYTES:
  772. putrawstr(memory, len, f, sink);
  773. break;
  774. case UPB_TYPE_MESSAGE: {
  775. zval* submsg = *(zval**)memory;
  776. putsubmsg(submsg, f, sink, depth);
  777. break;
  778. }
  779. default:
  780. assert(false);
  781. }
  782. }
  783. // Only string/bytes fields are stored as zval.
  784. static const char* raw_value(void* memory, const upb_fielddef* f) {
  785. switch (upb_fielddef_type(f)) {
  786. case UPB_TYPE_STRING:
  787. case UPB_TYPE_BYTES:
  788. return Z_STRVAL_PP((zval**)memory);
  789. break;
  790. default:
  791. return memory;
  792. }
  793. }
  794. static int raw_value_len(void* memory, int len, const upb_fielddef* f) {
  795. switch (upb_fielddef_type(f)) {
  796. case UPB_TYPE_STRING:
  797. case UPB_TYPE_BYTES:
  798. return Z_STRLEN_PP((zval**)memory);
  799. break;
  800. default:
  801. return len;
  802. }
  803. }
  804. static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink,
  805. int depth) {
  806. Map* self;
  807. upb_sink subsink;
  808. const upb_fielddef* key_field;
  809. const upb_fielddef* value_field;
  810. MapIter it;
  811. int len;
  812. if (map == NULL) return;
  813. self = UNBOX(Map, map);
  814. upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
  815. assert(upb_fielddef_type(f) == UPB_TYPE_MESSAGE);
  816. key_field = map_field_key(f);
  817. value_field = map_field_value(f);
  818. for (map_begin(map, &it); !map_done(&it); map_next(&it)) {
  819. upb_status status;
  820. upb_sink entry_sink;
  821. upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG),
  822. &entry_sink);
  823. upb_sink_startmsg(&entry_sink);
  824. // Serialize key.
  825. const char *key = map_iter_key(&it, &len);
  826. put_optional_value(key, len, key_field, depth + 1, &entry_sink);
  827. // Serialize value.
  828. upb_value value = map_iter_value(&it, &len);
  829. put_optional_value(raw_value(upb_value_memory(&value), value_field),
  830. raw_value_len(upb_value_memory(&value), len, value_field),
  831. value_field, depth + 1, &entry_sink);
  832. upb_sink_endmsg(&entry_sink, &status);
  833. upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG));
  834. }
  835. upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
  836. }
  837. static void putmsg(zval* msg_php, const Descriptor* desc, upb_sink* sink,
  838. int depth) {
  839. upb_msg_field_iter i;
  840. upb_status status;
  841. upb_sink_startmsg(sink);
  842. // Protect against cycles (possible because users may freely reassign message
  843. // and repeated fields) by imposing a maximum recursion depth.
  844. if (depth > ENCODE_MAX_NESTING) {
  845. zend_error(E_ERROR,
  846. "Maximum recursion depth exceeded during encoding.");
  847. }
  848. MessageHeader* msg = zend_object_store_get_object(msg_php TSRMLS_CC);
  849. for (upb_msg_field_begin(&i, desc->msgdef); !upb_msg_field_done(&i);
  850. upb_msg_field_next(&i)) {
  851. upb_fielddef* f = upb_msg_iter_field(&i);
  852. uint32_t offset = desc->layout->fields[upb_fielddef_index(f)].offset +
  853. sizeof(MessageHeader);
  854. if (upb_fielddef_containingoneof(f)) {
  855. uint32_t oneof_case_offset =
  856. desc->layout->fields[upb_fielddef_index(f)].case_offset +
  857. sizeof(MessageHeader);
  858. // For a oneof, check that this field is actually present -- skip all the
  859. // below if not.
  860. if (DEREF(msg, oneof_case_offset, uint32_t) != upb_fielddef_number(f)) {
  861. continue;
  862. }
  863. // Otherwise, fall through to the appropriate singular-field handler
  864. // below.
  865. }
  866. if (is_map_field(f)) {
  867. zval* map = *DEREF(msg, offset, zval**);
  868. if (map != NULL) {
  869. putmap(map, f, sink, depth);
  870. }
  871. } else if (upb_fielddef_isseq(f)) {
  872. zval* array = *DEREF(msg, offset, zval**);
  873. if (array != NULL) {
  874. putarray(array, f, sink, depth);
  875. }
  876. } else if (upb_fielddef_isstring(f)) {
  877. zval* str = *DEREF(msg, offset, zval**);
  878. if (Z_STRLEN_P(str) > 0) {
  879. putstr(str, f, sink);
  880. }
  881. } else if (upb_fielddef_issubmsg(f)) {
  882. putsubmsg(*DEREF(msg, offset, zval**), f, sink, depth);
  883. } else {
  884. upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  885. #define T(upbtypeconst, upbtype, ctype, default_value) \
  886. case upbtypeconst: { \
  887. ctype value = DEREF(msg, offset, ctype); \
  888. if (value != default_value) { \
  889. upb_sink_put##upbtype(sink, sel, value); \
  890. } \
  891. } break;
  892. switch (upb_fielddef_type(f)) {
  893. T(UPB_TYPE_FLOAT, float, float, 0.0)
  894. T(UPB_TYPE_DOUBLE, double, double, 0.0)
  895. T(UPB_TYPE_BOOL, bool, uint8_t, 0)
  896. case UPB_TYPE_ENUM:
  897. T(UPB_TYPE_INT32, int32, int32_t, 0)
  898. T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
  899. T(UPB_TYPE_INT64, int64, int64_t, 0)
  900. T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
  901. case UPB_TYPE_STRING:
  902. case UPB_TYPE_BYTES:
  903. case UPB_TYPE_MESSAGE:
  904. zend_error(E_ERROR, "Internal error.");
  905. }
  906. #undef T
  907. }
  908. }
  909. upb_sink_endmsg(sink, &status);
  910. }
  911. static void putstr(zval* str, const upb_fielddef *f, upb_sink *sink) {
  912. upb_sink subsink;
  913. if (ZVAL_IS_NULL(str)) return;
  914. assert(Z_TYPE_P(str) == IS_STRING);
  915. // Ensure that the string has the correct encoding. We also check at field-set
  916. // time, but the user may have mutated the string object since then.
  917. if (upb_fielddef_type(f) == UPB_TYPE_STRING &&
  918. !is_structurally_valid_utf8(Z_STRVAL_P(str), Z_STRLEN_P(str))) {
  919. zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
  920. return;
  921. }
  922. upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), Z_STRLEN_P(str),
  923. &subsink);
  924. upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), Z_STRVAL_P(str),
  925. Z_STRLEN_P(str), NULL);
  926. upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
  927. }
  928. static void putrawstr(const char* str, int len, const upb_fielddef* f,
  929. upb_sink* sink) {
  930. upb_sink subsink;
  931. if (len == 0) return;
  932. // Ensure that the string has the correct encoding. We also check at field-set
  933. // time, but the user may have mutated the string object since then.
  934. if (upb_fielddef_type(f) == UPB_TYPE_STRING &&
  935. !is_structurally_valid_utf8(str, len)) {
  936. zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
  937. return;
  938. }
  939. upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), len, &subsink);
  940. upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), str, len, NULL);
  941. upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
  942. }
  943. static void putsubmsg(zval* submsg, const upb_fielddef* f, upb_sink* sink,
  944. int depth) {
  945. upb_sink subsink;
  946. if (Z_TYPE_P(submsg) == IS_NULL) return;
  947. zval* php_descriptor = get_def_obj(upb_fielddef_msgsubdef(f));
  948. Descriptor* subdesc =
  949. (Descriptor*)zend_object_store_get_object(php_descriptor TSRMLS_CC);
  950. upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink);
  951. putmsg(submsg, subdesc, &subsink, depth + 1);
  952. upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG));
  953. }
  954. static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink,
  955. int depth) {
  956. upb_sink subsink;
  957. upb_fieldtype_t type = upb_fielddef_type(f);
  958. upb_selector_t sel = 0;
  959. int size, i;
  960. assert(array != NULL);
  961. RepeatedField* intern =
  962. (RepeatedField*)zend_object_store_get_object(array TSRMLS_CC);
  963. size = zend_hash_num_elements(HASH_OF(intern->array));
  964. if (size == 0) return;
  965. upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
  966. if (upb_fielddef_isprimitive(f)) {
  967. sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  968. }
  969. for (i = 0; i < size; i++) {
  970. void* memory = repeated_field_index_native(intern, i);
  971. switch (type) {
  972. #define T(upbtypeconst, upbtype, ctype) \
  973. case upbtypeconst: \
  974. upb_sink_put##upbtype(&subsink, sel, *((ctype*)memory)); \
  975. break;
  976. T(UPB_TYPE_FLOAT, float, float)
  977. T(UPB_TYPE_DOUBLE, double, double)
  978. T(UPB_TYPE_BOOL, bool, int8_t)
  979. case UPB_TYPE_ENUM:
  980. T(UPB_TYPE_INT32, int32, int32_t)
  981. T(UPB_TYPE_UINT32, uint32, uint32_t)
  982. T(UPB_TYPE_INT64, int64, int64_t)
  983. T(UPB_TYPE_UINT64, uint64, uint64_t)
  984. case UPB_TYPE_STRING:
  985. case UPB_TYPE_BYTES:
  986. putstr(*((zval**)memory), f, &subsink);
  987. break;
  988. case UPB_TYPE_MESSAGE:
  989. putsubmsg(*((zval**)memory), f, &subsink, depth);
  990. break;
  991. #undef T
  992. }
  993. }
  994. upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
  995. }
  996. static const upb_handlers* msgdef_pb_serialize_handlers(Descriptor* desc) {
  997. if (desc->pb_serialize_handlers == NULL) {
  998. desc->pb_serialize_handlers =
  999. upb_pb_encoder_newhandlers(desc->msgdef, &desc->pb_serialize_handlers);
  1000. }
  1001. return desc->pb_serialize_handlers;
  1002. }
  1003. // -----------------------------------------------------------------------------
  1004. // PHP encode/decode methods
  1005. // -----------------------------------------------------------------------------
  1006. PHP_METHOD(Message, encode) {
  1007. zval* php_descriptor = get_ce_obj(Z_OBJCE_P(getThis()));
  1008. Descriptor* desc =
  1009. (Descriptor*)zend_object_store_get_object(php_descriptor TSRMLS_CC);
  1010. stringsink sink;
  1011. stringsink_init(&sink);
  1012. {
  1013. const upb_handlers* serialize_handlers = msgdef_pb_serialize_handlers(desc);
  1014. stackenv se;
  1015. upb_pb_encoder* encoder;
  1016. stackenv_init(&se, "Error occurred during encoding: %s");
  1017. encoder = upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink);
  1018. putmsg(getThis(), desc, upb_pb_encoder_input(encoder), 0);
  1019. RETVAL_STRINGL(sink.ptr, sink.len, 1);
  1020. stackenv_uninit(&se);
  1021. stringsink_uninit(&sink);
  1022. }
  1023. }
  1024. PHP_METHOD(Message, decode) {
  1025. zval* php_descriptor = get_ce_obj(Z_OBJCE_P(getThis()));
  1026. Descriptor* desc =
  1027. (Descriptor*)zend_object_store_get_object(php_descriptor TSRMLS_CC);
  1028. MessageHeader* msg = zend_object_store_get_object(getThis() TSRMLS_CC);
  1029. char *data = NULL;
  1030. int data_len;
  1031. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) ==
  1032. FAILURE) {
  1033. return;
  1034. }
  1035. {
  1036. const upb_pbdecodermethod* method = msgdef_decodermethod(desc);
  1037. const upb_handlers* h = upb_pbdecodermethod_desthandlers(method);
  1038. stackenv se;
  1039. upb_sink sink;
  1040. upb_pbdecoder* decoder;
  1041. stackenv_init(&se, "Error occurred during parsing: %s");
  1042. upb_sink_reset(&sink, h, msg);
  1043. decoder = upb_pbdecoder_create(&se.env, method, &sink);
  1044. upb_bufsrc_putbuf(data, data_len, upb_pbdecoder_input(decoder));
  1045. stackenv_uninit(&se);
  1046. }
  1047. }