encode_decode.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include "protobuf.h"
  31. // This function is equivalent to rb_str_cat(), but unlike the real
  32. // rb_str_cat(), it doesn't leak memory in some versions of Ruby.
  33. // For more information, see:
  34. // https://bugs.ruby-lang.org/issues/11328
  35. VALUE noleak_rb_str_cat(VALUE rb_str, const char *str, long len) {
  36. char *p;
  37. size_t oldlen = RSTRING_LEN(rb_str);
  38. rb_str_modify_expand(rb_str, len);
  39. p = RSTRING_PTR(rb_str);
  40. memcpy(p + oldlen, str, len);
  41. rb_str_set_len(rb_str, oldlen + len);
  42. return rb_str;
  43. }
  44. // -----------------------------------------------------------------------------
  45. // Parsing.
  46. // -----------------------------------------------------------------------------
  47. #define DEREF(msg, ofs, type) *(type*)(((uint8_t *)msg) + ofs)
  48. // Creates a handlerdata that simply contains the offset for this field.
  49. static const void* newhandlerdata(upb_handlers* h, uint32_t ofs) {
  50. size_t* hd_ofs = ALLOC(size_t);
  51. *hd_ofs = ofs;
  52. upb_handlers_addcleanup(h, hd_ofs, xfree);
  53. return hd_ofs;
  54. }
  55. typedef struct {
  56. size_t ofs;
  57. const upb_msgdef *md;
  58. } submsg_handlerdata_t;
  59. // Creates a handlerdata that contains offset and submessage type information.
  60. static const void *newsubmsghandlerdata(upb_handlers* h, uint32_t ofs,
  61. const upb_fielddef* f) {
  62. submsg_handlerdata_t *hd = ALLOC(submsg_handlerdata_t);
  63. hd->ofs = ofs;
  64. hd->md = upb_fielddef_msgsubdef(f);
  65. upb_handlers_addcleanup(h, hd, xfree);
  66. return hd;
  67. }
  68. typedef struct {
  69. size_t ofs; // union data slot
  70. size_t case_ofs; // oneof_case field
  71. uint32_t oneof_case_num; // oneof-case number to place in oneof_case field
  72. const upb_msgdef *md; // msgdef, for oneof submessage handler
  73. } oneof_handlerdata_t;
  74. static const void *newoneofhandlerdata(upb_handlers *h,
  75. uint32_t ofs,
  76. uint32_t case_ofs,
  77. const upb_fielddef *f) {
  78. oneof_handlerdata_t *hd = ALLOC(oneof_handlerdata_t);
  79. hd->ofs = ofs;
  80. hd->case_ofs = case_ofs;
  81. // We reuse the field tag number as a oneof union discriminant tag. Note that
  82. // we don't expose these numbers to the user, so the only requirement is that
  83. // we have some unique ID for each union case/possibility. The field tag
  84. // numbers are already present and are easy to use so there's no reason to
  85. // create a separate ID space. In addition, using the field tag number here
  86. // lets us easily look up the field in the oneof accessor.
  87. hd->oneof_case_num = upb_fielddef_number(f);
  88. if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE) {
  89. hd->md = upb_fielddef_msgsubdef(f);
  90. } else {
  91. hd->md = NULL;
  92. }
  93. upb_handlers_addcleanup(h, hd, xfree);
  94. return hd;
  95. }
  96. // A handler that starts a repeated field. Gets the Repeated*Field instance for
  97. // this field (such an instance always exists even in an empty message).
  98. static void *startseq_handler(void* closure, const void* hd) {
  99. MessageHeader* msg = closure;
  100. const size_t *ofs = hd;
  101. return (void*)DEREF(msg, *ofs, VALUE);
  102. }
  103. // Handlers that append primitive values to a repeated field.
  104. #define DEFINE_APPEND_HANDLER(type, ctype) \
  105. static bool append##type##_handler(void *closure, const void *hd, \
  106. ctype val) { \
  107. VALUE ary = (VALUE)closure; \
  108. RepeatedField_push_native(ary, &val); \
  109. return true; \
  110. }
  111. DEFINE_APPEND_HANDLER(bool, bool)
  112. DEFINE_APPEND_HANDLER(int32, int32_t)
  113. DEFINE_APPEND_HANDLER(uint32, uint32_t)
  114. DEFINE_APPEND_HANDLER(float, float)
  115. DEFINE_APPEND_HANDLER(int64, int64_t)
  116. DEFINE_APPEND_HANDLER(uint64, uint64_t)
  117. DEFINE_APPEND_HANDLER(double, double)
  118. // Appends a string to a repeated field.
  119. static void* appendstr_handler(void *closure,
  120. const void *hd,
  121. size_t size_hint) {
  122. VALUE ary = (VALUE)closure;
  123. VALUE str = rb_str_new2("");
  124. rb_enc_associate(str, kRubyStringUtf8Encoding);
  125. RepeatedField_push_native(ary, &str);
  126. return (void*)str;
  127. }
  128. // Appends a 'bytes' string to a repeated field.
  129. static void* appendbytes_handler(void *closure,
  130. const void *hd,
  131. size_t size_hint) {
  132. VALUE ary = (VALUE)closure;
  133. VALUE str = rb_str_new2("");
  134. rb_enc_associate(str, kRubyString8bitEncoding);
  135. RepeatedField_push_native(ary, &str);
  136. return (void*)str;
  137. }
  138. // Sets a non-repeated string field in a message.
  139. static void* str_handler(void *closure,
  140. const void *hd,
  141. size_t size_hint) {
  142. MessageHeader* msg = closure;
  143. const size_t *ofs = hd;
  144. VALUE str = rb_str_new2("");
  145. rb_enc_associate(str, kRubyStringUtf8Encoding);
  146. DEREF(msg, *ofs, VALUE) = str;
  147. return (void*)str;
  148. }
  149. // Sets a non-repeated 'bytes' field in a message.
  150. static void* bytes_handler(void *closure,
  151. const void *hd,
  152. size_t size_hint) {
  153. MessageHeader* msg = closure;
  154. const size_t *ofs = hd;
  155. VALUE str = rb_str_new2("");
  156. rb_enc_associate(str, kRubyString8bitEncoding);
  157. DEREF(msg, *ofs, VALUE) = str;
  158. return (void*)str;
  159. }
  160. static size_t stringdata_handler(void* closure, const void* hd,
  161. const char* str, size_t len,
  162. const upb_bufhandle* handle) {
  163. VALUE rb_str = (VALUE)closure;
  164. noleak_rb_str_cat(rb_str, str, len);
  165. return len;
  166. }
  167. static bool stringdata_end_handler(void* closure, const void* hd) {
  168. MessageHeader* msg = closure;
  169. const size_t *ofs = hd;
  170. VALUE rb_str = DEREF(msg, *ofs, VALUE);
  171. rb_obj_freeze(rb_str);
  172. return true;
  173. }
  174. static bool appendstring_end_handler(void* closure, const void* hd) {
  175. VALUE ary = (VALUE)closure;
  176. int size = RepeatedField_size(ary);
  177. VALUE* last = RepeatedField_index_native(ary, size - 1);
  178. VALUE rb_str = *last;
  179. rb_obj_freeze(rb_str);
  180. return true;
  181. }
  182. // Appends a submessage to a repeated field (a regular Ruby array for now).
  183. static void *appendsubmsg_handler(void *closure, const void *hd) {
  184. VALUE ary = (VALUE)closure;
  185. const submsg_handlerdata_t *submsgdata = hd;
  186. VALUE subdesc =
  187. get_def_obj((void*)submsgdata->md);
  188. VALUE subklass = Descriptor_msgclass(subdesc);
  189. MessageHeader* submsg;
  190. VALUE submsg_rb = rb_class_new_instance(0, NULL, subklass);
  191. RepeatedField_push(ary, submsg_rb);
  192. TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg);
  193. return submsg;
  194. }
  195. // Sets a non-repeated submessage field in a message.
  196. static void *submsg_handler(void *closure, const void *hd) {
  197. MessageHeader* msg = closure;
  198. const submsg_handlerdata_t* submsgdata = hd;
  199. VALUE subdesc =
  200. get_def_obj((void*)submsgdata->md);
  201. VALUE subklass = Descriptor_msgclass(subdesc);
  202. VALUE submsg_rb;
  203. MessageHeader* submsg;
  204. if (DEREF(msg, submsgdata->ofs, VALUE) == Qnil) {
  205. DEREF(msg, submsgdata->ofs, VALUE) =
  206. rb_class_new_instance(0, NULL, subklass);
  207. }
  208. submsg_rb = DEREF(msg, submsgdata->ofs, VALUE);
  209. TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg);
  210. return submsg;
  211. }
  212. // Handler data for startmap/endmap handlers.
  213. typedef struct {
  214. size_t ofs;
  215. upb_fieldtype_t key_field_type;
  216. upb_fieldtype_t value_field_type;
  217. // We know that we can hold this reference because the handlerdata has the
  218. // same lifetime as the upb_handlers struct, and the upb_handlers struct holds
  219. // a reference to the upb_msgdef, which in turn has references to its subdefs.
  220. const upb_def* value_field_subdef;
  221. } map_handlerdata_t;
  222. // Temporary frame for map parsing: at the beginning of a map entry message, a
  223. // submsg handler allocates a frame to hold (i) a reference to the Map object
  224. // into which this message will be inserted and (ii) storage slots to
  225. // temporarily hold the key and value for this map entry until the end of the
  226. // submessage. When the submessage ends, another handler is called to insert the
  227. // value into the map.
  228. typedef struct {
  229. VALUE map;
  230. const map_handlerdata_t* handlerdata;
  231. char key_storage[NATIVE_SLOT_MAX_SIZE];
  232. char value_storage[NATIVE_SLOT_MAX_SIZE];
  233. } map_parse_frame_t;
  234. static void MapParseFrame_mark(void* _self) {
  235. map_parse_frame_t* frame = _self;
  236. // This shouldn't strictly be necessary since this should be rooted by the
  237. // message itself, but it can't hurt.
  238. rb_gc_mark(frame->map);
  239. native_slot_mark(frame->handlerdata->key_field_type, &frame->key_storage);
  240. native_slot_mark(frame->handlerdata->value_field_type, &frame->value_storage);
  241. }
  242. void MapParseFrame_free(void* self) {
  243. xfree(self);
  244. }
  245. rb_data_type_t MapParseFrame_type = {
  246. "MapParseFrame",
  247. { MapParseFrame_mark, MapParseFrame_free, NULL },
  248. };
  249. // Array of Ruby objects wrapping map_parse_frame_t.
  250. // We don't allow multiple concurrent decodes, so we assume that this global
  251. // variable is specific to the "current" decode.
  252. VALUE map_parse_frames;
  253. static map_parse_frame_t* map_push_frame(VALUE map,
  254. const map_handlerdata_t* handlerdata) {
  255. map_parse_frame_t* frame = ALLOC(map_parse_frame_t);
  256. frame->handlerdata = handlerdata;
  257. frame->map = map;
  258. native_slot_init(handlerdata->key_field_type, &frame->key_storage);
  259. native_slot_init(handlerdata->value_field_type, &frame->value_storage);
  260. rb_ary_push(map_parse_frames,
  261. TypedData_Wrap_Struct(rb_cObject, &MapParseFrame_type, frame));
  262. return frame;
  263. }
  264. static void map_pop_frame() {
  265. rb_ary_pop(map_parse_frames);
  266. }
  267. // Handler to begin a map entry: allocates a temporary frame. This is the
  268. // 'startsubmsg' handler on the msgdef that contains the map field.
  269. static void *startmapentry_handler(void *closure, const void *hd) {
  270. MessageHeader* msg = closure;
  271. const map_handlerdata_t* mapdata = hd;
  272. VALUE map_rb = DEREF(msg, mapdata->ofs, VALUE);
  273. return map_push_frame(map_rb, mapdata);
  274. }
  275. // Handler to end a map entry: inserts the value defined during the message into
  276. // the map. This is the 'endmsg' handler on the map entry msgdef.
  277. static bool endmap_handler(void *closure, const void *hd, upb_status* s) {
  278. map_parse_frame_t* frame = closure;
  279. const map_handlerdata_t* mapdata = hd;
  280. VALUE key = native_slot_get(
  281. mapdata->key_field_type, Qnil,
  282. &frame->key_storage);
  283. VALUE value_field_typeclass = Qnil;
  284. VALUE value;
  285. if (mapdata->value_field_type == UPB_TYPE_MESSAGE ||
  286. mapdata->value_field_type == UPB_TYPE_ENUM) {
  287. value_field_typeclass = get_def_obj(mapdata->value_field_subdef);
  288. }
  289. value = native_slot_get(
  290. mapdata->value_field_type, value_field_typeclass,
  291. &frame->value_storage);
  292. Map_index_set(frame->map, key, value);
  293. map_pop_frame();
  294. return true;
  295. }
  296. // Allocates a new map_handlerdata_t given the map entry message definition. If
  297. // the offset of the field within the parent message is also given, that is
  298. // added to the handler data as well. Note that this is called *twice* per map
  299. // field: once in the parent message handler setup when setting the startsubmsg
  300. // handler and once in the map entry message handler setup when setting the
  301. // key/value and endmsg handlers. The reason is that there is no easy way to
  302. // pass the handlerdata down to the sub-message handler setup.
  303. static map_handlerdata_t* new_map_handlerdata(
  304. size_t ofs,
  305. const upb_msgdef* mapentry_def,
  306. Descriptor* desc) {
  307. const upb_fielddef* key_field;
  308. const upb_fielddef* value_field;
  309. map_handlerdata_t* hd = ALLOC(map_handlerdata_t);
  310. hd->ofs = ofs;
  311. key_field = upb_msgdef_itof(mapentry_def, MAP_KEY_FIELD);
  312. assert(key_field != NULL);
  313. hd->key_field_type = upb_fielddef_type(key_field);
  314. value_field = upb_msgdef_itof(mapentry_def, MAP_VALUE_FIELD);
  315. assert(value_field != NULL);
  316. hd->value_field_type = upb_fielddef_type(value_field);
  317. hd->value_field_subdef = upb_fielddef_subdef(value_field);
  318. return hd;
  319. }
  320. // Handlers that set primitive values in oneofs.
  321. #define DEFINE_ONEOF_HANDLER(type, ctype) \
  322. static bool oneof##type##_handler(void *closure, const void *hd, \
  323. ctype val) { \
  324. const oneof_handlerdata_t *oneofdata = hd; \
  325. DEREF(closure, oneofdata->case_ofs, uint32_t) = \
  326. oneofdata->oneof_case_num; \
  327. DEREF(closure, oneofdata->ofs, ctype) = val; \
  328. return true; \
  329. }
  330. DEFINE_ONEOF_HANDLER(bool, bool)
  331. DEFINE_ONEOF_HANDLER(int32, int32_t)
  332. DEFINE_ONEOF_HANDLER(uint32, uint32_t)
  333. DEFINE_ONEOF_HANDLER(float, float)
  334. DEFINE_ONEOF_HANDLER(int64, int64_t)
  335. DEFINE_ONEOF_HANDLER(uint64, uint64_t)
  336. DEFINE_ONEOF_HANDLER(double, double)
  337. #undef DEFINE_ONEOF_HANDLER
  338. // Handlers for strings in a oneof.
  339. static void *oneofstr_handler(void *closure,
  340. const void *hd,
  341. size_t size_hint) {
  342. MessageHeader* msg = closure;
  343. const oneof_handlerdata_t *oneofdata = hd;
  344. VALUE str = rb_str_new2("");
  345. rb_enc_associate(str, kRubyStringUtf8Encoding);
  346. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  347. oneofdata->oneof_case_num;
  348. DEREF(msg, oneofdata->ofs, VALUE) = str;
  349. return (void*)str;
  350. }
  351. static void *oneofbytes_handler(void *closure,
  352. const void *hd,
  353. size_t size_hint) {
  354. MessageHeader* msg = closure;
  355. const oneof_handlerdata_t *oneofdata = hd;
  356. VALUE str = rb_str_new2("");
  357. rb_enc_associate(str, kRubyString8bitEncoding);
  358. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  359. oneofdata->oneof_case_num;
  360. DEREF(msg, oneofdata->ofs, VALUE) = str;
  361. return (void*)str;
  362. }
  363. static bool oneofstring_end_handler(void* closure, const void* hd) {
  364. MessageHeader* msg = closure;
  365. const oneof_handlerdata_t *oneofdata = hd;
  366. rb_obj_freeze(DEREF(msg, oneofdata->ofs, VALUE));
  367. return true;
  368. }
  369. // Handler for a submessage field in a oneof.
  370. static void *oneofsubmsg_handler(void *closure,
  371. const void *hd) {
  372. MessageHeader* msg = closure;
  373. const oneof_handlerdata_t *oneofdata = hd;
  374. uint32_t oldcase = DEREF(msg, oneofdata->case_ofs, uint32_t);
  375. VALUE subdesc =
  376. get_def_obj((void*)oneofdata->md);
  377. VALUE subklass = Descriptor_msgclass(subdesc);
  378. VALUE submsg_rb;
  379. MessageHeader* submsg;
  380. if (oldcase != oneofdata->oneof_case_num ||
  381. DEREF(msg, oneofdata->ofs, VALUE) == Qnil) {
  382. DEREF(msg, oneofdata->ofs, VALUE) =
  383. rb_class_new_instance(0, NULL, subklass);
  384. }
  385. // Set the oneof case *after* allocating the new class instance -- otherwise,
  386. // if the Ruby GC is invoked as part of a call into the VM, it might invoke
  387. // our mark routines, and our mark routines might see the case value
  388. // indicating a VALUE is present and expect a valid VALUE. See comment in
  389. // layout_set() for more detail: basically, the change to the value and the
  390. // case must be atomic w.r.t. the Ruby VM.
  391. DEREF(msg, oneofdata->case_ofs, uint32_t) =
  392. oneofdata->oneof_case_num;
  393. submsg_rb = DEREF(msg, oneofdata->ofs, VALUE);
  394. TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg);
  395. return submsg;
  396. }
  397. // Set up handlers for a repeated field.
  398. static void add_handlers_for_repeated_field(upb_handlers *h,
  399. const upb_fielddef *f,
  400. size_t offset) {
  401. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  402. upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset));
  403. upb_handlers_setstartseq(h, f, startseq_handler, &attr);
  404. upb_handlerattr_uninit(&attr);
  405. switch (upb_fielddef_type(f)) {
  406. #define SET_HANDLER(utype, ltype) \
  407. case utype: \
  408. upb_handlers_set##ltype(h, f, append##ltype##_handler, NULL); \
  409. break;
  410. SET_HANDLER(UPB_TYPE_BOOL, bool);
  411. SET_HANDLER(UPB_TYPE_INT32, int32);
  412. SET_HANDLER(UPB_TYPE_UINT32, uint32);
  413. SET_HANDLER(UPB_TYPE_ENUM, int32);
  414. SET_HANDLER(UPB_TYPE_FLOAT, float);
  415. SET_HANDLER(UPB_TYPE_INT64, int64);
  416. SET_HANDLER(UPB_TYPE_UINT64, uint64);
  417. SET_HANDLER(UPB_TYPE_DOUBLE, double);
  418. #undef SET_HANDLER
  419. case UPB_TYPE_STRING:
  420. case UPB_TYPE_BYTES: {
  421. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  422. upb_handlers_setstartstr(h, f, is_bytes ?
  423. appendbytes_handler : appendstr_handler,
  424. NULL);
  425. upb_handlers_setstring(h, f, stringdata_handler, NULL);
  426. upb_handlers_setendstr(h, f, appendstring_end_handler, NULL);
  427. break;
  428. }
  429. case UPB_TYPE_MESSAGE: {
  430. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  431. upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, f));
  432. upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr);
  433. upb_handlerattr_uninit(&attr);
  434. break;
  435. }
  436. }
  437. }
  438. // Set up handlers for a singular field.
  439. static void add_handlers_for_singular_field(upb_handlers *h,
  440. const upb_fielddef *f,
  441. size_t offset) {
  442. switch (upb_fielddef_type(f)) {
  443. case UPB_TYPE_BOOL:
  444. case UPB_TYPE_INT32:
  445. case UPB_TYPE_UINT32:
  446. case UPB_TYPE_ENUM:
  447. case UPB_TYPE_FLOAT:
  448. case UPB_TYPE_INT64:
  449. case UPB_TYPE_UINT64:
  450. case UPB_TYPE_DOUBLE:
  451. upb_msg_setscalarhandler(h, f, offset, -1);
  452. break;
  453. case UPB_TYPE_STRING:
  454. case UPB_TYPE_BYTES: {
  455. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  456. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  457. upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset));
  458. upb_handlers_setstartstr(h, f,
  459. is_bytes ? bytes_handler : str_handler,
  460. &attr);
  461. upb_handlers_setstring(h, f, stringdata_handler, &attr);
  462. upb_handlers_setendstr(h, f, stringdata_end_handler, &attr);
  463. upb_handlerattr_uninit(&attr);
  464. break;
  465. }
  466. case UPB_TYPE_MESSAGE: {
  467. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  468. upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, offset, f));
  469. upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr);
  470. upb_handlerattr_uninit(&attr);
  471. break;
  472. }
  473. }
  474. }
  475. // Adds handlers to a map field.
  476. static void add_handlers_for_mapfield(upb_handlers* h,
  477. const upb_fielddef* fielddef,
  478. size_t offset,
  479. Descriptor* desc) {
  480. const upb_msgdef* map_msgdef = upb_fielddef_msgsubdef(fielddef);
  481. map_handlerdata_t* hd = new_map_handlerdata(offset, map_msgdef, desc);
  482. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  483. upb_handlers_addcleanup(h, hd, xfree);
  484. upb_handlerattr_sethandlerdata(&attr, hd);
  485. upb_handlers_setstartsubmsg(h, fielddef, startmapentry_handler, &attr);
  486. upb_handlerattr_uninit(&attr);
  487. }
  488. // Adds handlers to a map-entry msgdef.
  489. static void add_handlers_for_mapentry(const upb_msgdef* msgdef,
  490. upb_handlers* h,
  491. Descriptor* desc) {
  492. const upb_fielddef* key_field = map_entry_key(msgdef);
  493. const upb_fielddef* value_field = map_entry_value(msgdef);
  494. map_handlerdata_t* hd = new_map_handlerdata(0, msgdef, desc);
  495. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  496. upb_handlers_addcleanup(h, hd, xfree);
  497. upb_handlerattr_sethandlerdata(&attr, hd);
  498. upb_handlers_setendmsg(h, endmap_handler, &attr);
  499. add_handlers_for_singular_field(
  500. h, key_field,
  501. offsetof(map_parse_frame_t, key_storage));
  502. add_handlers_for_singular_field(
  503. h, value_field,
  504. offsetof(map_parse_frame_t, value_storage));
  505. }
  506. // Set up handlers for a oneof field.
  507. static void add_handlers_for_oneof_field(upb_handlers *h,
  508. const upb_fielddef *f,
  509. size_t offset,
  510. size_t oneof_case_offset) {
  511. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  512. upb_handlerattr_sethandlerdata(
  513. &attr, newoneofhandlerdata(h, offset, oneof_case_offset, f));
  514. switch (upb_fielddef_type(f)) {
  515. #define SET_HANDLER(utype, ltype) \
  516. case utype: \
  517. upb_handlers_set##ltype(h, f, oneof##ltype##_handler, &attr); \
  518. break;
  519. SET_HANDLER(UPB_TYPE_BOOL, bool);
  520. SET_HANDLER(UPB_TYPE_INT32, int32);
  521. SET_HANDLER(UPB_TYPE_UINT32, uint32);
  522. SET_HANDLER(UPB_TYPE_ENUM, int32);
  523. SET_HANDLER(UPB_TYPE_FLOAT, float);
  524. SET_HANDLER(UPB_TYPE_INT64, int64);
  525. SET_HANDLER(UPB_TYPE_UINT64, uint64);
  526. SET_HANDLER(UPB_TYPE_DOUBLE, double);
  527. #undef SET_HANDLER
  528. case UPB_TYPE_STRING:
  529. case UPB_TYPE_BYTES: {
  530. bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
  531. upb_handlers_setstartstr(h, f, is_bytes ?
  532. oneofbytes_handler : oneofstr_handler,
  533. &attr);
  534. upb_handlers_setstring(h, f, stringdata_handler, NULL);
  535. upb_handlers_setendstr(h, f, oneofstring_end_handler, &attr);
  536. break;
  537. }
  538. case UPB_TYPE_MESSAGE: {
  539. upb_handlers_setstartsubmsg(h, f, oneofsubmsg_handler, &attr);
  540. break;
  541. }
  542. }
  543. upb_handlerattr_uninit(&attr);
  544. }
  545. static void add_handlers_for_message(const void *closure, upb_handlers *h) {
  546. const upb_msgdef* msgdef = upb_handlers_msgdef(h);
  547. Descriptor* desc = ruby_to_Descriptor(get_def_obj((void*)msgdef));
  548. upb_msg_field_iter i;
  549. // If this is a mapentry message type, set up a special set of handlers and
  550. // bail out of the normal (user-defined) message type handling.
  551. if (upb_msgdef_mapentry(msgdef)) {
  552. add_handlers_for_mapentry(msgdef, h, desc);
  553. return;
  554. }
  555. // Ensure layout exists. We may be invoked to create handlers for a given
  556. // message if we are included as a submsg of another message type before our
  557. // class is actually built, so to work around this, we just create the layout
  558. // (and handlers, in the class-building function) on-demand.
  559. if (desc->layout == NULL) {
  560. desc->layout = create_layout(desc->msgdef);
  561. }
  562. for (upb_msg_field_begin(&i, desc->msgdef);
  563. !upb_msg_field_done(&i);
  564. upb_msg_field_next(&i)) {
  565. const upb_fielddef *f = upb_msg_iter_field(&i);
  566. size_t offset = desc->layout->fields[upb_fielddef_index(f)].offset +
  567. sizeof(MessageHeader);
  568. if (upb_fielddef_containingoneof(f)) {
  569. size_t oneof_case_offset =
  570. desc->layout->fields[upb_fielddef_index(f)].case_offset +
  571. sizeof(MessageHeader);
  572. add_handlers_for_oneof_field(h, f, offset, oneof_case_offset);
  573. } else if (is_map_field(f)) {
  574. add_handlers_for_mapfield(h, f, offset, desc);
  575. } else if (upb_fielddef_isseq(f)) {
  576. add_handlers_for_repeated_field(h, f, offset);
  577. } else {
  578. add_handlers_for_singular_field(h, f, offset);
  579. }
  580. }
  581. }
  582. // Creates upb handlers for populating a message.
  583. static const upb_handlers *new_fill_handlers(Descriptor* desc,
  584. const void* owner) {
  585. // TODO(cfallin, haberman): once upb gets a caching/memoization layer for
  586. // handlers, reuse subdef handlers so that e.g. if we already parse
  587. // B-with-field-of-type-C, we don't have to rebuild the whole hierarchy to
  588. // parse A-with-field-of-type-B-with-field-of-type-C.
  589. return upb_handlers_newfrozen(desc->msgdef, owner,
  590. add_handlers_for_message, NULL);
  591. }
  592. // Constructs the handlers for filling a message's data into an in-memory
  593. // object.
  594. const upb_handlers* get_fill_handlers(Descriptor* desc) {
  595. if (!desc->fill_handlers) {
  596. desc->fill_handlers =
  597. new_fill_handlers(desc, &desc->fill_handlers);
  598. }
  599. return desc->fill_handlers;
  600. }
  601. // Constructs the upb decoder method for parsing messages of this type.
  602. // This is called from the message class creation code.
  603. const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor* desc,
  604. const void* owner) {
  605. const upb_handlers* handlers = get_fill_handlers(desc);
  606. upb_pbdecodermethodopts opts;
  607. upb_pbdecodermethodopts_init(&opts, handlers);
  608. return upb_pbdecodermethod_new(&opts, owner);
  609. }
  610. static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) {
  611. if (desc->fill_method == NULL) {
  612. desc->fill_method = new_fillmsg_decodermethod(
  613. desc, &desc->fill_method);
  614. }
  615. return desc->fill_method;
  616. }
  617. static const upb_json_parsermethod *msgdef_jsonparsermethod(Descriptor* desc) {
  618. if (desc->json_fill_method == NULL) {
  619. desc->json_fill_method =
  620. upb_json_parsermethod_new(desc->msgdef, &desc->json_fill_method);
  621. }
  622. return desc->json_fill_method;
  623. }
  624. // Stack-allocated context during an encode/decode operation. Contains the upb
  625. // environment and its stack-based allocator, an initial buffer for allocations
  626. // to avoid malloc() when possible, and a template for Ruby exception messages
  627. // if any error occurs.
  628. #define STACK_ENV_STACKBYTES 4096
  629. typedef struct {
  630. upb_env env;
  631. const char* ruby_error_template;
  632. char allocbuf[STACK_ENV_STACKBYTES];
  633. } stackenv;
  634. static void stackenv_init(stackenv* se, const char* errmsg);
  635. static void stackenv_uninit(stackenv* se);
  636. // Callback invoked by upb if any error occurs during parsing or serialization.
  637. static bool env_error_func(void* ud, const upb_status* status) {
  638. stackenv* se = ud;
  639. // Free the env -- rb_raise will longjmp up the stack past the encode/decode
  640. // function so it would not otherwise have been freed.
  641. stackenv_uninit(se);
  642. // TODO(haberman): have a way to verify that this is actually a parse error,
  643. // instead of just throwing "parse error" unconditionally.
  644. rb_raise(cParseError, se->ruby_error_template, upb_status_errmsg(status));
  645. // Never reached: rb_raise() always longjmp()s up the stack, past all of our
  646. // code, back to Ruby.
  647. return false;
  648. }
  649. static void stackenv_init(stackenv* se, const char* errmsg) {
  650. se->ruby_error_template = errmsg;
  651. upb_env_init2(&se->env, se->allocbuf, sizeof(se->allocbuf), NULL);
  652. upb_env_seterrorfunc(&se->env, env_error_func, se);
  653. }
  654. static void stackenv_uninit(stackenv* se) {
  655. upb_env_uninit(&se->env);
  656. }
  657. /*
  658. * call-seq:
  659. * MessageClass.decode(data) => message
  660. *
  661. * Decodes the given data (as a string containing bytes in protocol buffers wire
  662. * format) under the interpretration given by this message class's definition
  663. * and returns a message object with the corresponding field values.
  664. */
  665. VALUE Message_decode(VALUE klass, VALUE data) {
  666. VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
  667. Descriptor* desc = ruby_to_Descriptor(descriptor);
  668. VALUE msgklass = Descriptor_msgclass(descriptor);
  669. VALUE msg_rb;
  670. MessageHeader* msg;
  671. if (TYPE(data) != T_STRING) {
  672. rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
  673. }
  674. msg_rb = rb_class_new_instance(0, NULL, msgklass);
  675. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  676. // We generally expect this to be clear already, but clear it in case parsing
  677. // previously got interrupted somehow.
  678. rb_ary_clear(map_parse_frames);
  679. {
  680. const upb_pbdecodermethod* method = msgdef_decodermethod(desc);
  681. const upb_handlers* h = upb_pbdecodermethod_desthandlers(method);
  682. stackenv se;
  683. upb_sink sink;
  684. upb_pbdecoder* decoder;
  685. stackenv_init(&se, "Error occurred during parsing: %s");
  686. upb_sink_reset(&sink, h, msg);
  687. decoder = upb_pbdecoder_create(&se.env, method, &sink);
  688. upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
  689. upb_pbdecoder_input(decoder));
  690. stackenv_uninit(&se);
  691. }
  692. return msg_rb;
  693. }
  694. /*
  695. * call-seq:
  696. * MessageClass.decode_json(data) => message
  697. *
  698. * Decodes the given data (as a string containing bytes in protocol buffers wire
  699. * format) under the interpretration given by this message class's definition
  700. * and returns a message object with the corresponding field values.
  701. */
  702. VALUE Message_decode_json(VALUE klass, VALUE data) {
  703. VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
  704. Descriptor* desc = ruby_to_Descriptor(descriptor);
  705. VALUE msgklass = Descriptor_msgclass(descriptor);
  706. VALUE msg_rb;
  707. MessageHeader* msg;
  708. if (TYPE(data) != T_STRING) {
  709. rb_raise(rb_eArgError, "Expected string for JSON data.");
  710. }
  711. // TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to
  712. // convert, because string handlers pass data directly to message string
  713. // fields.
  714. msg_rb = rb_class_new_instance(0, NULL, msgklass);
  715. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  716. // We generally expect this to be clear already, but clear it in case parsing
  717. // previously got interrupted somehow.
  718. rb_ary_clear(map_parse_frames);
  719. {
  720. const upb_json_parsermethod* method = msgdef_jsonparsermethod(desc);
  721. stackenv se;
  722. upb_sink sink;
  723. upb_json_parser* parser;
  724. stackenv_init(&se, "Error occurred during parsing: %s");
  725. upb_sink_reset(&sink, get_fill_handlers(desc), msg);
  726. parser = upb_json_parser_create(&se.env, method, &sink);
  727. upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
  728. upb_json_parser_input(parser));
  729. stackenv_uninit(&se);
  730. }
  731. return msg_rb;
  732. }
  733. // -----------------------------------------------------------------------------
  734. // Serializing.
  735. // -----------------------------------------------------------------------------
  736. //
  737. // The code below also comes from upb's prototype Ruby binding, developed by
  738. // haberman@.
  739. /* stringsink *****************************************************************/
  740. // This should probably be factored into a common upb component.
  741. typedef struct {
  742. upb_byteshandler handler;
  743. upb_bytessink sink;
  744. char *ptr;
  745. size_t len, size;
  746. } stringsink;
  747. static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) {
  748. stringsink *sink = _sink;
  749. sink->len = 0;
  750. return sink;
  751. }
  752. static size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
  753. size_t len, const upb_bufhandle *handle) {
  754. stringsink *sink = _sink;
  755. size_t new_size = sink->size;
  756. UPB_UNUSED(hd);
  757. UPB_UNUSED(handle);
  758. while (sink->len + len > new_size) {
  759. new_size *= 2;
  760. }
  761. if (new_size != sink->size) {
  762. sink->ptr = realloc(sink->ptr, new_size);
  763. sink->size = new_size;
  764. }
  765. memcpy(sink->ptr + sink->len, ptr, len);
  766. sink->len += len;
  767. return len;
  768. }
  769. void stringsink_init(stringsink *sink) {
  770. upb_byteshandler_init(&sink->handler);
  771. upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL);
  772. upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL);
  773. upb_bytessink_reset(&sink->sink, &sink->handler, sink);
  774. sink->size = 32;
  775. sink->ptr = malloc(sink->size);
  776. sink->len = 0;
  777. }
  778. void stringsink_uninit(stringsink *sink) {
  779. free(sink->ptr);
  780. }
  781. /* msgvisitor *****************************************************************/
  782. // TODO: If/when we support proto2 semantics in addition to the current proto3
  783. // semantics, which means that we have true field presence, we will want to
  784. // modify msgvisitor so that it emits all present fields rather than all
  785. // non-default-value fields.
  786. static void putmsg(VALUE msg, const Descriptor* desc,
  787. upb_sink *sink, int depth, bool emit_defaults);
  788. static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) {
  789. upb_selector_t ret;
  790. bool ok = upb_handlers_getselector(f, type, &ret);
  791. UPB_ASSERT(ok);
  792. return ret;
  793. }
  794. static void putstr(VALUE str, const upb_fielddef *f, upb_sink *sink) {
  795. upb_sink subsink;
  796. if (str == Qnil) return;
  797. assert(BUILTIN_TYPE(str) == RUBY_T_STRING);
  798. // We should be guaranteed that the string has the correct encoding because
  799. // we ensured this at assignment time and then froze the string.
  800. if (upb_fielddef_type(f) == UPB_TYPE_STRING) {
  801. assert(rb_enc_from_index(ENCODING_GET(str)) == kRubyStringUtf8Encoding);
  802. } else {
  803. assert(rb_enc_from_index(ENCODING_GET(str)) == kRubyString8bitEncoding);
  804. }
  805. upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), RSTRING_LEN(str),
  806. &subsink);
  807. upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), RSTRING_PTR(str),
  808. RSTRING_LEN(str), NULL);
  809. upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
  810. }
  811. static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink *sink,
  812. int depth, bool emit_defaults) {
  813. upb_sink subsink;
  814. VALUE descriptor;
  815. Descriptor* subdesc;
  816. if (submsg == Qnil) return;
  817. descriptor = rb_ivar_get(submsg, descriptor_instancevar_interned);
  818. subdesc = ruby_to_Descriptor(descriptor);
  819. upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink);
  820. putmsg(submsg, subdesc, &subsink, depth + 1, emit_defaults);
  821. upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG));
  822. }
  823. static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink,
  824. int depth, bool emit_defaults) {
  825. upb_sink subsink;
  826. upb_fieldtype_t type = upb_fielddef_type(f);
  827. upb_selector_t sel = 0;
  828. int size;
  829. if (ary == Qnil) return;
  830. upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
  831. if (upb_fielddef_isprimitive(f)) {
  832. sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  833. }
  834. size = NUM2INT(RepeatedField_length(ary));
  835. for (int i = 0; i < size; i++) {
  836. void* memory = RepeatedField_index_native(ary, i);
  837. switch (type) {
  838. #define T(upbtypeconst, upbtype, ctype) \
  839. case upbtypeconst: \
  840. upb_sink_put##upbtype(&subsink, sel, *((ctype *)memory)); \
  841. break;
  842. T(UPB_TYPE_FLOAT, float, float)
  843. T(UPB_TYPE_DOUBLE, double, double)
  844. T(UPB_TYPE_BOOL, bool, int8_t)
  845. case UPB_TYPE_ENUM:
  846. T(UPB_TYPE_INT32, int32, int32_t)
  847. T(UPB_TYPE_UINT32, uint32, uint32_t)
  848. T(UPB_TYPE_INT64, int64, int64_t)
  849. T(UPB_TYPE_UINT64, uint64, uint64_t)
  850. case UPB_TYPE_STRING:
  851. case UPB_TYPE_BYTES:
  852. putstr(*((VALUE *)memory), f, &subsink);
  853. break;
  854. case UPB_TYPE_MESSAGE:
  855. putsubmsg(*((VALUE *)memory), f, &subsink, depth, emit_defaults);
  856. break;
  857. #undef T
  858. }
  859. }
  860. upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
  861. }
  862. static void put_ruby_value(VALUE value,
  863. const upb_fielddef *f,
  864. VALUE type_class,
  865. int depth,
  866. upb_sink *sink,
  867. bool emit_defaults) {
  868. upb_selector_t sel = 0;
  869. if (upb_fielddef_isprimitive(f)) {
  870. sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  871. }
  872. switch (upb_fielddef_type(f)) {
  873. case UPB_TYPE_INT32:
  874. upb_sink_putint32(sink, sel, NUM2INT(value));
  875. break;
  876. case UPB_TYPE_INT64:
  877. upb_sink_putint64(sink, sel, NUM2LL(value));
  878. break;
  879. case UPB_TYPE_UINT32:
  880. upb_sink_putuint32(sink, sel, NUM2UINT(value));
  881. break;
  882. case UPB_TYPE_UINT64:
  883. upb_sink_putuint64(sink, sel, NUM2ULL(value));
  884. break;
  885. case UPB_TYPE_FLOAT:
  886. upb_sink_putfloat(sink, sel, NUM2DBL(value));
  887. break;
  888. case UPB_TYPE_DOUBLE:
  889. upb_sink_putdouble(sink, sel, NUM2DBL(value));
  890. break;
  891. case UPB_TYPE_ENUM: {
  892. if (TYPE(value) == T_SYMBOL) {
  893. value = rb_funcall(type_class, rb_intern("resolve"), 1, value);
  894. }
  895. upb_sink_putint32(sink, sel, NUM2INT(value));
  896. break;
  897. }
  898. case UPB_TYPE_BOOL:
  899. upb_sink_putbool(sink, sel, value == Qtrue);
  900. break;
  901. case UPB_TYPE_STRING:
  902. case UPB_TYPE_BYTES:
  903. putstr(value, f, sink);
  904. break;
  905. case UPB_TYPE_MESSAGE:
  906. putsubmsg(value, f, sink, depth, emit_defaults);
  907. }
  908. }
  909. static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink,
  910. int depth, bool emit_defaults) {
  911. Map* self;
  912. upb_sink subsink;
  913. const upb_fielddef* key_field;
  914. const upb_fielddef* value_field;
  915. Map_iter it;
  916. if (map == Qnil) return;
  917. self = ruby_to_Map(map);
  918. upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
  919. assert(upb_fielddef_type(f) == UPB_TYPE_MESSAGE);
  920. key_field = map_field_key(f);
  921. value_field = map_field_value(f);
  922. for (Map_begin(map, &it); !Map_done(&it); Map_next(&it)) {
  923. VALUE key = Map_iter_key(&it);
  924. VALUE value = Map_iter_value(&it);
  925. upb_status status;
  926. upb_sink entry_sink;
  927. upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG),
  928. &entry_sink);
  929. upb_sink_startmsg(&entry_sink);
  930. put_ruby_value(key, key_field, Qnil, depth + 1, &entry_sink, emit_defaults);
  931. put_ruby_value(value, value_field, self->value_type_class, depth + 1,
  932. &entry_sink, emit_defaults);
  933. upb_sink_endmsg(&entry_sink, &status);
  934. upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG));
  935. }
  936. upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
  937. }
  938. static void putmsg(VALUE msg_rb, const Descriptor* desc,
  939. upb_sink *sink, int depth, bool emit_defaults) {
  940. MessageHeader* msg;
  941. upb_msg_field_iter i;
  942. upb_status status;
  943. upb_sink_startmsg(sink);
  944. // Protect against cycles (possible because users may freely reassign message
  945. // and repeated fields) by imposing a maximum recursion depth.
  946. if (depth > ENCODE_MAX_NESTING) {
  947. rb_raise(rb_eRuntimeError,
  948. "Maximum recursion depth exceeded during encoding.");
  949. }
  950. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  951. for (upb_msg_field_begin(&i, desc->msgdef);
  952. !upb_msg_field_done(&i);
  953. upb_msg_field_next(&i)) {
  954. upb_fielddef *f = upb_msg_iter_field(&i);
  955. bool is_matching_oneof = false;
  956. uint32_t offset =
  957. desc->layout->fields[upb_fielddef_index(f)].offset +
  958. sizeof(MessageHeader);
  959. if (upb_fielddef_containingoneof(f)) {
  960. uint32_t oneof_case_offset =
  961. desc->layout->fields[upb_fielddef_index(f)].case_offset +
  962. sizeof(MessageHeader);
  963. // For a oneof, check that this field is actually present -- skip all the
  964. // below if not.
  965. if (DEREF(msg, oneof_case_offset, uint32_t) !=
  966. upb_fielddef_number(f)) {
  967. continue;
  968. }
  969. // Otherwise, fall through to the appropriate singular-field handler
  970. // below.
  971. is_matching_oneof = true;
  972. }
  973. if (is_map_field(f)) {
  974. VALUE map = DEREF(msg, offset, VALUE);
  975. if (map != Qnil || emit_defaults) {
  976. putmap(map, f, sink, depth, emit_defaults);
  977. }
  978. } else if (upb_fielddef_isseq(f)) {
  979. VALUE ary = DEREF(msg, offset, VALUE);
  980. if (ary != Qnil) {
  981. putary(ary, f, sink, depth, emit_defaults);
  982. }
  983. } else if (upb_fielddef_isstring(f)) {
  984. VALUE str = DEREF(msg, offset, VALUE);
  985. if (is_matching_oneof || emit_defaults || RSTRING_LEN(str) > 0) {
  986. putstr(str, f, sink);
  987. }
  988. } else if (upb_fielddef_issubmsg(f)) {
  989. putsubmsg(DEREF(msg, offset, VALUE), f, sink, depth, emit_defaults);
  990. } else {
  991. upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  992. #define T(upbtypeconst, upbtype, ctype, default_value) \
  993. case upbtypeconst: { \
  994. ctype value = DEREF(msg, offset, ctype); \
  995. if (is_matching_oneof || emit_defaults || value != default_value) { \
  996. upb_sink_put##upbtype(sink, sel, value); \
  997. } \
  998. } \
  999. break;
  1000. switch (upb_fielddef_type(f)) {
  1001. T(UPB_TYPE_FLOAT, float, float, 0.0)
  1002. T(UPB_TYPE_DOUBLE, double, double, 0.0)
  1003. T(UPB_TYPE_BOOL, bool, uint8_t, 0)
  1004. case UPB_TYPE_ENUM:
  1005. T(UPB_TYPE_INT32, int32, int32_t, 0)
  1006. T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
  1007. T(UPB_TYPE_INT64, int64, int64_t, 0)
  1008. T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
  1009. case UPB_TYPE_STRING:
  1010. case UPB_TYPE_BYTES:
  1011. case UPB_TYPE_MESSAGE: rb_raise(rb_eRuntimeError, "Internal error.");
  1012. }
  1013. #undef T
  1014. }
  1015. }
  1016. upb_sink_endmsg(sink, &status);
  1017. }
  1018. static const upb_handlers* msgdef_pb_serialize_handlers(Descriptor* desc) {
  1019. if (desc->pb_serialize_handlers == NULL) {
  1020. desc->pb_serialize_handlers =
  1021. upb_pb_encoder_newhandlers(desc->msgdef, &desc->pb_serialize_handlers);
  1022. }
  1023. return desc->pb_serialize_handlers;
  1024. }
  1025. static const upb_handlers* msgdef_json_serialize_handlers(
  1026. Descriptor* desc, bool preserve_proto_fieldnames) {
  1027. if (preserve_proto_fieldnames) {
  1028. if (desc->json_serialize_handlers == NULL) {
  1029. desc->json_serialize_handlers =
  1030. upb_json_printer_newhandlers(
  1031. desc->msgdef, true, &desc->json_serialize_handlers);
  1032. }
  1033. return desc->json_serialize_handlers;
  1034. } else {
  1035. if (desc->json_serialize_handlers_preserve == NULL) {
  1036. desc->json_serialize_handlers_preserve =
  1037. upb_json_printer_newhandlers(
  1038. desc->msgdef, false, &desc->json_serialize_handlers_preserve);
  1039. }
  1040. return desc->json_serialize_handlers_preserve;
  1041. }
  1042. }
  1043. /*
  1044. * call-seq:
  1045. * MessageClass.encode(msg) => bytes
  1046. *
  1047. * Encodes the given message object to its serialized form in protocol buffers
  1048. * wire format.
  1049. */
  1050. VALUE Message_encode(VALUE klass, VALUE msg_rb) {
  1051. VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
  1052. Descriptor* desc = ruby_to_Descriptor(descriptor);
  1053. stringsink sink;
  1054. stringsink_init(&sink);
  1055. {
  1056. const upb_handlers* serialize_handlers =
  1057. msgdef_pb_serialize_handlers(desc);
  1058. stackenv se;
  1059. upb_pb_encoder* encoder;
  1060. VALUE ret;
  1061. stackenv_init(&se, "Error occurred during encoding: %s");
  1062. encoder = upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink);
  1063. putmsg(msg_rb, desc, upb_pb_encoder_input(encoder), 0, false);
  1064. ret = rb_str_new(sink.ptr, sink.len);
  1065. stackenv_uninit(&se);
  1066. stringsink_uninit(&sink);
  1067. return ret;
  1068. }
  1069. }
  1070. /*
  1071. * call-seq:
  1072. * MessageClass.encode_json(msg) => json_string
  1073. *
  1074. * Encodes the given message object into its serialized JSON representation.
  1075. */
  1076. VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
  1077. VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
  1078. Descriptor* desc = ruby_to_Descriptor(descriptor);
  1079. VALUE msg_rb;
  1080. VALUE preserve_proto_fieldnames = Qfalse;
  1081. VALUE emit_defaults = Qfalse;
  1082. stringsink sink;
  1083. if (argc < 1 || argc > 2) {
  1084. rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
  1085. }
  1086. msg_rb = argv[0];
  1087. if (argc == 2) {
  1088. VALUE hash_args = argv[1];
  1089. if (TYPE(hash_args) != T_HASH) {
  1090. rb_raise(rb_eArgError, "Expected hash arguments.");
  1091. }
  1092. preserve_proto_fieldnames = rb_hash_lookup2(
  1093. hash_args, ID2SYM(rb_intern("preserve_proto_fieldnames")), Qfalse);
  1094. emit_defaults = rb_hash_lookup2(
  1095. hash_args, ID2SYM(rb_intern("emit_defaults")), Qfalse);
  1096. }
  1097. stringsink_init(&sink);
  1098. {
  1099. const upb_handlers* serialize_handlers =
  1100. msgdef_json_serialize_handlers(desc, RTEST(preserve_proto_fieldnames));
  1101. upb_json_printer* printer;
  1102. stackenv se;
  1103. VALUE ret;
  1104. stackenv_init(&se, "Error occurred during encoding: %s");
  1105. printer = upb_json_printer_create(&se.env, serialize_handlers, &sink.sink);
  1106. putmsg(msg_rb, desc, upb_json_printer_input(printer), 0, RTEST(emit_defaults));
  1107. ret = rb_enc_str_new(sink.ptr, sink.len, rb_utf8_encoding());
  1108. stackenv_uninit(&se);
  1109. stringsink_uninit(&sink);
  1110. return ret;
  1111. }
  1112. }