pb_encode.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /* pb_encode.c -- encode a protobuf using minimal resources
  2. *
  3. * 2011 Petteri Aimonen <jpa@kapsi.fi>
  4. */
  5. #include "pb.h"
  6. #include "pb_encode.h"
  7. #include "pb_common.h"
  8. /* Use the GCC warn_unused_result attribute to check that all return values
  9. * are propagated correctly. On other compilers and gcc before 3.4.0 just
  10. * ignore the annotation.
  11. */
  12. #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
  13. #define checkreturn
  14. #else
  15. #define checkreturn __attribute__((warn_unused_result))
  16. #endif
  17. /**************************************
  18. * Declarations internal to this file *
  19. **************************************/
  20. static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
  21. static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field);
  22. static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field);
  23. static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field);
  24. static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field);
  25. static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field);
  26. static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field);
  27. static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension);
  28. static void *pb_const_cast(const void *p);
  29. static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high);
  30. static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field);
  31. static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field);
  32. static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
  33. static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field);
  34. static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field);
  35. static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
  36. #ifdef PB_WITHOUT_64BIT
  37. #define pb_int64_t int32_t
  38. #define pb_uint64_t uint32_t
  39. #else
  40. #define pb_int64_t int64_t
  41. #define pb_uint64_t uint64_t
  42. #endif
  43. /*******************************
  44. * pb_ostream_t implementation *
  45. *******************************/
  46. static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
  47. {
  48. size_t i;
  49. pb_byte_t *dest = (pb_byte_t*)stream->state;
  50. stream->state = dest + count;
  51. for (i = 0; i < count; i++)
  52. dest[i] = buf[i];
  53. return true;
  54. }
  55. pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
  56. {
  57. pb_ostream_t stream;
  58. #ifdef PB_BUFFER_ONLY
  59. stream.callback = (void*)1; /* Just a marker value */
  60. #else
  61. stream.callback = &buf_write;
  62. #endif
  63. stream.state = buf;
  64. stream.max_size = bufsize;
  65. stream.bytes_written = 0;
  66. #ifndef PB_NO_ERRMSG
  67. stream.errmsg = NULL;
  68. #endif
  69. return stream;
  70. }
  71. bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
  72. {
  73. if (stream->callback != NULL)
  74. {
  75. if (stream->bytes_written + count > stream->max_size)
  76. PB_RETURN_ERROR(stream, "stream full");
  77. #ifdef PB_BUFFER_ONLY
  78. if (!buf_write(stream, buf, count))
  79. PB_RETURN_ERROR(stream, "io error");
  80. #else
  81. if (!stream->callback(stream, buf, count))
  82. PB_RETURN_ERROR(stream, "io error");
  83. #endif
  84. }
  85. stream->bytes_written += count;
  86. return true;
  87. }
  88. /*************************
  89. * Encode a single field *
  90. *************************/
  91. /* Encode a static array. Handles the size calculations and possible packing. */
  92. static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field)
  93. {
  94. pb_size_t i;
  95. pb_size_t count;
  96. size_t size;
  97. count = *(pb_size_t*)field->pSize;
  98. if (count == 0)
  99. return true;
  100. if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
  101. PB_RETURN_ERROR(stream, "array max size exceeded");
  102. /* We always pack arrays if the datatype allows it. */
  103. if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
  104. {
  105. if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
  106. return false;
  107. /* Determine the total size of packed array. */
  108. if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
  109. {
  110. size = 4 * (size_t)count;
  111. }
  112. else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  113. {
  114. size = 8 * (size_t)count;
  115. }
  116. else
  117. {
  118. pb_ostream_t sizestream = PB_OSTREAM_SIZING;
  119. void *pData_orig = field->pData;
  120. for (i = 0; i < count; i++)
  121. {
  122. if (!pb_enc_varint(&sizestream, field))
  123. PB_RETURN_ERROR(stream, PB_GET_ERROR(&sizestream));
  124. field->pData = (char*)field->pData + field->data_size;
  125. }
  126. field->pData = pData_orig;
  127. size = sizestream.bytes_written;
  128. }
  129. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  130. return false;
  131. if (stream->callback == NULL)
  132. return pb_write(stream, NULL, size); /* Just sizing.. */
  133. /* Write the data */
  134. for (i = 0; i < count; i++)
  135. {
  136. if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32 || PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  137. {
  138. if (!pb_enc_fixed(stream, field))
  139. return false;
  140. }
  141. else
  142. {
  143. if (!pb_enc_varint(stream, field))
  144. return false;
  145. }
  146. field->pData = (char*)field->pData + field->data_size;
  147. }
  148. }
  149. else /* Unpacked fields */
  150. {
  151. for (i = 0; i < count; i++)
  152. {
  153. /* Normally the data is stored directly in the array entries, but
  154. * for pointer-type string and bytes fields, the array entries are
  155. * actually pointers themselves also. So we have to dereference once
  156. * more to get to the actual data. */
  157. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
  158. (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
  159. PB_LTYPE(field->type) == PB_LTYPE_BYTES))
  160. {
  161. bool status;
  162. void *pData_orig = field->pData;
  163. field->pData = *(void* const*)field->pData;
  164. if (!field->pData)
  165. {
  166. /* Null pointer in array is treated as empty string / bytes */
  167. status = pb_encode_tag_for_field(stream, field) &&
  168. pb_encode_varint(stream, 0);
  169. }
  170. else
  171. {
  172. status = encode_basic_field(stream, field);
  173. }
  174. field->pData = pData_orig;
  175. if (!status)
  176. return false;
  177. }
  178. else
  179. {
  180. if (!encode_basic_field(stream, field))
  181. return false;
  182. }
  183. field->pData = (char*)field->pData + field->data_size;
  184. }
  185. }
  186. return true;
  187. }
  188. /* In proto3, all fields are optional and are only encoded if their value is "non-zero".
  189. * This function implements the check for the zero value. */
  190. static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field)
  191. {
  192. pb_type_t type = field->type;
  193. if (PB_ATYPE(type) == PB_ATYPE_STATIC)
  194. {
  195. if (PB_HTYPE(type) == PB_HTYPE_REQUIRED)
  196. {
  197. /* Required proto2 fields inside proto3 submessage, pretty rare case */
  198. return false;
  199. }
  200. else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  201. {
  202. /* Repeated fields inside proto3 submessage: present if count != 0 */
  203. return *(const pb_size_t*)field->pSize == 0;
  204. }
  205. else if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
  206. {
  207. /* Oneof fields */
  208. return *(const pb_size_t*)field->pSize == 0;
  209. }
  210. else if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
  211. {
  212. /* Proto2 optional fields inside proto3 submessage */
  213. return *(const bool*)field->pSize == false;
  214. }
  215. /* Rest is proto3 singular fields */
  216. if (PB_LTYPE(type) == PB_LTYPE_BYTES)
  217. {
  218. const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)field->pData;
  219. return bytes->size == 0;
  220. }
  221. else if (PB_LTYPE(type) == PB_LTYPE_STRING)
  222. {
  223. return *(const char*)field->pData == '\0';
  224. }
  225. else if (PB_LTYPE(type) == PB_LTYPE_FIXED_LENGTH_BYTES)
  226. {
  227. /* Fixed length bytes is only empty if its length is fixed
  228. * as 0. Which would be pretty strange, but we can check
  229. * it anyway. */
  230. return field->data_size == 0;
  231. }
  232. else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
  233. {
  234. /* Check all fields in the submessage to find if any of them
  235. * are non-zero. The comparison cannot be done byte-per-byte
  236. * because the C struct may contain padding bytes that must
  237. * be skipped.
  238. */
  239. pb_field_iter_t iter;
  240. if (pb_field_iter_begin(&iter, field->submsg_desc, field->pData))
  241. {
  242. do
  243. {
  244. if (!pb_check_proto3_default_value(&iter))
  245. {
  246. return false;
  247. }
  248. } while (pb_field_iter_next(&iter));
  249. }
  250. return true;
  251. }
  252. }
  253. {
  254. /* Catch-all branch that does byte-per-byte comparison for zero value.
  255. *
  256. * This is for all pointer fields, and for static PB_LTYPE_VARINT,
  257. * UVARINT, SVARINT, FIXED32, FIXED64, EXTENSION fields, and also
  258. * callback fields. These all have integer or pointer value which
  259. * can be compared with 0.
  260. */
  261. pb_size_t i;
  262. const char *p = (const char*)field->pData;
  263. for (i = 0; i < field->data_size; i++)
  264. {
  265. if (p[i] != 0)
  266. {
  267. return false;
  268. }
  269. }
  270. return true;
  271. }
  272. }
  273. /* Encode a field with static or pointer allocation, i.e. one whose data
  274. * is available to the encoder directly. */
  275. static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  276. {
  277. if (!field->pData)
  278. {
  279. /* Missing pointer field */
  280. return true;
  281. }
  282. if (!pb_encode_tag_for_field(stream, field))
  283. return false;
  284. switch (PB_LTYPE(field->type))
  285. {
  286. case PB_LTYPE_VARINT:
  287. case PB_LTYPE_UVARINT:
  288. case PB_LTYPE_SVARINT:
  289. return pb_enc_varint(stream, field);
  290. case PB_LTYPE_FIXED32:
  291. case PB_LTYPE_FIXED64:
  292. return pb_enc_fixed(stream, field);
  293. case PB_LTYPE_BYTES:
  294. return pb_enc_bytes(stream, field);
  295. case PB_LTYPE_STRING:
  296. return pb_enc_string(stream, field);
  297. case PB_LTYPE_SUBMESSAGE:
  298. return pb_enc_submessage(stream, field);
  299. case PB_LTYPE_FIXED_LENGTH_BYTES:
  300. return pb_enc_fixed_length_bytes(stream, field);
  301. default:
  302. PB_RETURN_ERROR(stream, "invalid field type");
  303. }
  304. }
  305. /* Encode a field with callback semantics. This means that a user function is
  306. * called to provide and encode the actual data. */
  307. static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  308. {
  309. if (field->descriptor->field_callback != NULL)
  310. {
  311. if (!field->descriptor->field_callback(NULL, stream, field))
  312. PB_RETURN_ERROR(stream, "callback error");
  313. }
  314. return true;
  315. }
  316. /* Encode a single field of any callback, pointer or static type. */
  317. static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field)
  318. {
  319. if (PB_ATYPE(field->type) == PB_ATYPE_CALLBACK)
  320. {
  321. return encode_callback_field(stream, field);
  322. }
  323. else if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
  324. {
  325. return encode_array(stream, field);
  326. }
  327. else if (PB_HTYPE(field->type) == PB_HTYPE_REQUIRED)
  328. {
  329. if (!field->pData)
  330. PB_RETURN_ERROR(stream, "missing required field");
  331. return encode_basic_field(stream, field);
  332. }
  333. else if (PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL)
  334. {
  335. if (PB_ATYPE(field->type) == PB_ATYPE_STATIC)
  336. {
  337. if (!field->pSize)
  338. {
  339. /* Proto3 singular field */
  340. if (pb_check_proto3_default_value(field))
  341. return true;
  342. }
  343. else if (*(const bool *)field->pSize == false)
  344. {
  345. /* Missing optional field */
  346. return true;
  347. }
  348. }
  349. return encode_basic_field(stream, field);
  350. }
  351. else if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
  352. {
  353. if (*(const pb_size_t*)field->pSize != field->tag)
  354. {
  355. /* Different type oneof field */
  356. return true;
  357. }
  358. return encode_basic_field(stream, field);
  359. }
  360. else
  361. {
  362. PB_RETURN_ERROR(stream, "invalid field type");
  363. }
  364. }
  365. /* Default handler for extension fields. Expects to have a pb_msgdesc_t
  366. * pointer in the extension->type->arg field, pointing to a message with
  367. * only one field in it. */
  368. static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension)
  369. {
  370. pb_field_iter_t iter;
  371. if (!pb_field_iter_begin_extension(&iter, (pb_extension_t*)pb_const_cast(extension)))
  372. PB_RETURN_ERROR(stream, "invalid extension");
  373. return encode_field(stream, &iter);
  374. }
  375. /* Walk through all the registered extensions and give them a chance
  376. * to encode themselves. */
  377. static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  378. {
  379. const pb_extension_t *extension = *(const pb_extension_t* const *)field->pData;
  380. while (extension)
  381. {
  382. bool status;
  383. if (extension->type->encode)
  384. status = extension->type->encode(stream, extension);
  385. else
  386. status = default_extension_encoder(stream, extension);
  387. if (!status)
  388. return false;
  389. extension = extension->next;
  390. }
  391. return true;
  392. }
  393. /*********************
  394. * Encode all fields *
  395. *********************/
  396. static void *pb_const_cast(const void *p)
  397. {
  398. /* Note: this casts away const, in order to use the common field iterator
  399. * logic for both encoding and decoding. */
  400. union {
  401. void *p1;
  402. const void *p2;
  403. } t;
  404. t.p2 = p;
  405. return t.p1;
  406. }
  407. bool checkreturn pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
  408. {
  409. pb_field_iter_t iter;
  410. if (!pb_field_iter_begin(&iter, fields, pb_const_cast(src_struct)))
  411. return true; /* Empty message type */
  412. do {
  413. if (PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
  414. {
  415. /* Special case for the extension field placeholder */
  416. if (!encode_extension_field(stream, &iter))
  417. return false;
  418. }
  419. else
  420. {
  421. /* Regular field */
  422. if (!encode_field(stream, &iter))
  423. return false;
  424. }
  425. } while (pb_field_iter_next(&iter));
  426. return true;
  427. }
  428. bool checkreturn pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags)
  429. {
  430. if ((flags & PB_ENCODE_DELIMITED) != 0)
  431. {
  432. return pb_encode_submessage(stream, fields, src_struct);
  433. }
  434. else if ((flags & PB_ENCODE_NULLTERMINATED) != 0)
  435. {
  436. const pb_byte_t zero = 0;
  437. if (!pb_encode(stream, fields, src_struct))
  438. return false;
  439. return pb_write(stream, &zero, 1);
  440. }
  441. else
  442. {
  443. return pb_encode(stream, fields, src_struct);
  444. }
  445. }
  446. bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct)
  447. {
  448. pb_ostream_t stream = PB_OSTREAM_SIZING;
  449. if (!pb_encode(&stream, fields, src_struct))
  450. return false;
  451. *size = stream.bytes_written;
  452. return true;
  453. }
  454. /********************
  455. * Helper functions *
  456. ********************/
  457. /* This function avoids 64-bit shifts as they are quite slow on many platforms. */
  458. static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high)
  459. {
  460. size_t i = 0;
  461. pb_byte_t buffer[10];
  462. pb_byte_t byte = (pb_byte_t)(low & 0x7F);
  463. low >>= 7;
  464. while (i < 4 && (low != 0 || high != 0))
  465. {
  466. byte |= 0x80;
  467. buffer[i++] = byte;
  468. byte = (pb_byte_t)(low & 0x7F);
  469. low >>= 7;
  470. }
  471. if (high)
  472. {
  473. byte = (pb_byte_t)(byte | ((high & 0x07) << 4));
  474. high >>= 3;
  475. while (high)
  476. {
  477. byte |= 0x80;
  478. buffer[i++] = byte;
  479. byte = (pb_byte_t)(high & 0x7F);
  480. high >>= 7;
  481. }
  482. }
  483. buffer[i++] = byte;
  484. return pb_write(stream, buffer, i);
  485. }
  486. bool checkreturn pb_encode_varint(pb_ostream_t *stream, pb_uint64_t value)
  487. {
  488. if (value <= 0x7F)
  489. {
  490. /* Fast path: single byte */
  491. pb_byte_t byte = (pb_byte_t)value;
  492. return pb_write(stream, &byte, 1);
  493. }
  494. else
  495. {
  496. #ifdef PB_WITHOUT_64BIT
  497. return pb_encode_varint_32(stream, value, 0);
  498. #else
  499. return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)(value >> 32));
  500. #endif
  501. }
  502. }
  503. bool checkreturn pb_encode_svarint(pb_ostream_t *stream, pb_int64_t value)
  504. {
  505. pb_uint64_t zigzagged;
  506. if (value < 0)
  507. zigzagged = ~((pb_uint64_t)value << 1);
  508. else
  509. zigzagged = (pb_uint64_t)value << 1;
  510. return pb_encode_varint(stream, zigzagged);
  511. }
  512. bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
  513. {
  514. uint32_t val = *(const uint32_t*)value;
  515. pb_byte_t bytes[4];
  516. bytes[0] = (pb_byte_t)(val & 0xFF);
  517. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  518. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  519. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  520. return pb_write(stream, bytes, 4);
  521. }
  522. #ifndef PB_WITHOUT_64BIT
  523. bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
  524. {
  525. uint64_t val = *(const uint64_t*)value;
  526. pb_byte_t bytes[8];
  527. bytes[0] = (pb_byte_t)(val & 0xFF);
  528. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  529. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  530. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  531. bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
  532. bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
  533. bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
  534. bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
  535. return pb_write(stream, bytes, 8);
  536. }
  537. #endif
  538. bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
  539. {
  540. pb_uint64_t tag = ((pb_uint64_t)field_number << 3) | wiretype;
  541. return pb_encode_varint(stream, tag);
  542. }
  543. bool pb_encode_tag_for_field ( pb_ostream_t* stream, const pb_field_iter_t* field )
  544. {
  545. pb_wire_type_t wiretype;
  546. switch (PB_LTYPE(field->type))
  547. {
  548. case PB_LTYPE_VARINT:
  549. case PB_LTYPE_UVARINT:
  550. case PB_LTYPE_SVARINT:
  551. wiretype = PB_WT_VARINT;
  552. break;
  553. case PB_LTYPE_FIXED32:
  554. wiretype = PB_WT_32BIT;
  555. break;
  556. case PB_LTYPE_FIXED64:
  557. wiretype = PB_WT_64BIT;
  558. break;
  559. case PB_LTYPE_BYTES:
  560. case PB_LTYPE_STRING:
  561. case PB_LTYPE_SUBMESSAGE:
  562. case PB_LTYPE_FIXED_LENGTH_BYTES:
  563. wiretype = PB_WT_STRING;
  564. break;
  565. default:
  566. PB_RETURN_ERROR(stream, "invalid field type");
  567. }
  568. return pb_encode_tag(stream, wiretype, field->tag);
  569. }
  570. bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
  571. {
  572. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  573. return false;
  574. return pb_write(stream, buffer, size);
  575. }
  576. bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
  577. {
  578. /* First calculate the message size using a non-writing substream. */
  579. pb_ostream_t substream = PB_OSTREAM_SIZING;
  580. size_t size;
  581. bool status;
  582. if (!pb_encode(&substream, fields, src_struct))
  583. {
  584. #ifndef PB_NO_ERRMSG
  585. stream->errmsg = substream.errmsg;
  586. #endif
  587. return false;
  588. }
  589. size = substream.bytes_written;
  590. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  591. return false;
  592. if (stream->callback == NULL)
  593. return pb_write(stream, NULL, size); /* Just sizing */
  594. if (stream->bytes_written + size > stream->max_size)
  595. PB_RETURN_ERROR(stream, "stream full");
  596. /* Use a substream to verify that a callback doesn't write more than
  597. * what it did the first time. */
  598. substream.callback = stream->callback;
  599. substream.state = stream->state;
  600. substream.max_size = size;
  601. substream.bytes_written = 0;
  602. #ifndef PB_NO_ERRMSG
  603. substream.errmsg = NULL;
  604. #endif
  605. status = pb_encode(&substream, fields, src_struct);
  606. stream->bytes_written += substream.bytes_written;
  607. stream->state = substream.state;
  608. #ifndef PB_NO_ERRMSG
  609. stream->errmsg = substream.errmsg;
  610. #endif
  611. if (substream.bytes_written != size)
  612. PB_RETURN_ERROR(stream, "submsg size changed");
  613. return status;
  614. }
  615. /* Field encoders */
  616. static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field)
  617. {
  618. if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
  619. {
  620. /* Perform unsigned integer extension */
  621. pb_uint64_t value = 0;
  622. if (field->data_size == sizeof(uint_least8_t))
  623. value = *(const uint_least8_t*)field->pData;
  624. else if (field->data_size == sizeof(uint_least16_t))
  625. value = *(const uint_least16_t*)field->pData;
  626. else if (field->data_size == sizeof(uint32_t))
  627. value = *(const uint32_t*)field->pData;
  628. else if (field->data_size == sizeof(pb_uint64_t))
  629. value = *(const pb_uint64_t*)field->pData;
  630. else
  631. PB_RETURN_ERROR(stream, "invalid data_size");
  632. return pb_encode_varint(stream, value);
  633. }
  634. else
  635. {
  636. /* Perform signed integer extension */
  637. pb_int64_t value = 0;
  638. if (field->data_size == sizeof(int_least8_t))
  639. value = *(const int_least8_t*)field->pData;
  640. else if (field->data_size == sizeof(int_least16_t))
  641. value = *(const int_least16_t*)field->pData;
  642. else if (field->data_size == sizeof(int32_t))
  643. value = *(const int32_t*)field->pData;
  644. else if (field->data_size == sizeof(pb_int64_t))
  645. value = *(const pb_int64_t*)field->pData;
  646. else
  647. PB_RETURN_ERROR(stream, "invalid data_size");
  648. if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
  649. return pb_encode_svarint(stream, value);
  650. #ifdef PB_WITHOUT_64BIT
  651. else if (value < 0)
  652. return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)-1);
  653. #endif
  654. else
  655. return pb_encode_varint(stream, (pb_uint64_t)value);
  656. }
  657. }
  658. static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field)
  659. {
  660. if (field->data_size == sizeof(uint32_t))
  661. {
  662. return pb_encode_fixed32(stream, field->pData);
  663. }
  664. #ifndef PB_WITHOUT_64BIT
  665. else if (field->data_size == sizeof(uint64_t))
  666. {
  667. return pb_encode_fixed64(stream, field->pData);
  668. }
  669. #endif
  670. else
  671. {
  672. PB_RETURN_ERROR(stream, "invalid data_size");
  673. }
  674. }
  675. static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
  676. {
  677. const pb_bytes_array_t *bytes = NULL;
  678. bytes = (const pb_bytes_array_t*)field->pData;
  679. if (bytes == NULL)
  680. {
  681. /* Treat null pointer as an empty bytes field */
  682. return pb_encode_string(stream, NULL, 0);
  683. }
  684. if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
  685. PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
  686. {
  687. PB_RETURN_ERROR(stream, "bytes size exceeded");
  688. }
  689. return pb_encode_string(stream, bytes->bytes, bytes->size);
  690. }
  691. static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field)
  692. {
  693. size_t size = 0;
  694. size_t max_size = field->data_size;
  695. const char *str = (const char*)field->pData;
  696. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  697. {
  698. max_size = (size_t)-1;
  699. }
  700. else
  701. {
  702. /* pb_dec_string() assumes string fields end with a null
  703. * terminator when the type isn't PB_ATYPE_POINTER, so we
  704. * shouldn't allow more than max-1 bytes to be written to
  705. * allow space for the null terminator.
  706. */
  707. if (max_size == 0)
  708. PB_RETURN_ERROR(stream, "zero-length string");
  709. max_size -= 1;
  710. }
  711. if (str == NULL)
  712. {
  713. size = 0; /* Treat null pointer as an empty string */
  714. }
  715. else
  716. {
  717. const char *p = str;
  718. /* strnlen() is not always available, so just use a loop */
  719. while (size < max_size && *p != '\0')
  720. {
  721. size++;
  722. p++;
  723. }
  724. if (*p != '\0')
  725. {
  726. PB_RETURN_ERROR(stream, "unterminated string");
  727. }
  728. }
  729. return pb_encode_string(stream, (const pb_byte_t*)str, size);
  730. }
  731. static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field)
  732. {
  733. if (field->submsg_desc == NULL)
  734. PB_RETURN_ERROR(stream, "invalid field descriptor");
  735. return pb_encode_submessage(stream, field->submsg_desc, field->pData);
  736. }
  737. static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
  738. {
  739. return pb_encode_string(stream, (const pb_byte_t*)field->pData, field->data_size);
  740. }