encode_decode.c 46 KB

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