Nenhuma descrição

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