pb_encode.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high);
  29. static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field);
  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. /* In PB_BUFFER_ONLY configuration the callback pointer is just int*.
  60. * NULL pointer marks a sizing field, so put a non-NULL value to mark a buffer stream.
  61. */
  62. static const int marker = 0;
  63. stream.callback = &marker;
  64. #else
  65. stream.callback = &buf_write;
  66. #endif
  67. stream.state = buf;
  68. stream.max_size = bufsize;
  69. stream.bytes_written = 0;
  70. #ifndef PB_NO_ERRMSG
  71. stream.errmsg = NULL;
  72. #endif
  73. return stream;
  74. }
  75. bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
  76. {
  77. if (count > 0 && stream->callback != NULL)
  78. {
  79. if (stream->bytes_written + count < stream->bytes_written ||
  80. stream->bytes_written + count > stream->max_size)
  81. {
  82. PB_RETURN_ERROR(stream, "stream full");
  83. }
  84. #ifdef PB_BUFFER_ONLY
  85. if (!buf_write(stream, buf, count))
  86. PB_RETURN_ERROR(stream, "io error");
  87. #else
  88. if (!stream->callback(stream, buf, count))
  89. PB_RETURN_ERROR(stream, "io error");
  90. #endif
  91. }
  92. stream->bytes_written += count;
  93. return true;
  94. }
  95. /*************************
  96. * Encode a single field *
  97. *************************/
  98. /* Read a bool value without causing undefined behavior even if the value
  99. * is invalid. See issue #434 and
  100. * https://stackoverflow.com/questions/27661768/weird-results-for-conditional
  101. */
  102. static bool safe_read_bool(const void *pSize)
  103. {
  104. const char *p = (const char *)pSize;
  105. size_t i;
  106. for (i = 0; i < sizeof(bool); i++)
  107. {
  108. if (p[i] != 0)
  109. return true;
  110. }
  111. return false;
  112. }
  113. /* Encode a static array. Handles the size calculations and possible packing. */
  114. static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field)
  115. {
  116. pb_size_t i;
  117. pb_size_t count;
  118. #ifndef PB_ENCODE_ARRAYS_UNPACKED
  119. size_t size;
  120. #endif
  121. count = *(pb_size_t*)field->pSize;
  122. if (count == 0)
  123. return true;
  124. if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
  125. PB_RETURN_ERROR(stream, "array max size exceeded");
  126. #ifndef PB_ENCODE_ARRAYS_UNPACKED
  127. /* We always pack arrays if the datatype allows it. */
  128. if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
  129. {
  130. if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
  131. return false;
  132. /* Determine the total size of packed array. */
  133. if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
  134. {
  135. size = 4 * (size_t)count;
  136. }
  137. else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  138. {
  139. size = 8 * (size_t)count;
  140. }
  141. else
  142. {
  143. pb_ostream_t sizestream = PB_OSTREAM_SIZING;
  144. void *pData_orig = field->pData;
  145. for (i = 0; i < count; i++)
  146. {
  147. if (!pb_enc_varint(&sizestream, field))
  148. PB_RETURN_ERROR(stream, PB_GET_ERROR(&sizestream));
  149. field->pData = (char*)field->pData + field->data_size;
  150. }
  151. field->pData = pData_orig;
  152. size = sizestream.bytes_written;
  153. }
  154. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  155. return false;
  156. if (stream->callback == NULL)
  157. return pb_write(stream, NULL, size); /* Just sizing.. */
  158. /* Write the data */
  159. for (i = 0; i < count; i++)
  160. {
  161. if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32 || PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  162. {
  163. if (!pb_enc_fixed(stream, field))
  164. return false;
  165. }
  166. else
  167. {
  168. if (!pb_enc_varint(stream, field))
  169. return false;
  170. }
  171. field->pData = (char*)field->pData + field->data_size;
  172. }
  173. }
  174. else /* Unpacked fields */
  175. #endif
  176. {
  177. for (i = 0; i < count; i++)
  178. {
  179. /* Normally the data is stored directly in the array entries, but
  180. * for pointer-type string and bytes fields, the array entries are
  181. * actually pointers themselves also. So we have to dereference once
  182. * more to get to the actual data. */
  183. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
  184. (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
  185. PB_LTYPE(field->type) == PB_LTYPE_BYTES))
  186. {
  187. bool status;
  188. void *pData_orig = field->pData;
  189. field->pData = *(void* const*)field->pData;
  190. if (!field->pData)
  191. {
  192. /* Null pointer in array is treated as empty string / bytes */
  193. status = pb_encode_tag_for_field(stream, field) &&
  194. pb_encode_varint(stream, 0);
  195. }
  196. else
  197. {
  198. status = encode_basic_field(stream, field);
  199. }
  200. field->pData = pData_orig;
  201. if (!status)
  202. return false;
  203. }
  204. else
  205. {
  206. if (!encode_basic_field(stream, field))
  207. return false;
  208. }
  209. field->pData = (char*)field->pData + field->data_size;
  210. }
  211. }
  212. return true;
  213. }
  214. /* In proto3, all fields are optional and are only encoded if their value is "non-zero".
  215. * This function implements the check for the zero value. */
  216. static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field)
  217. {
  218. pb_type_t type = field->type;
  219. if (PB_ATYPE(type) == PB_ATYPE_STATIC)
  220. {
  221. if (PB_HTYPE(type) == PB_HTYPE_REQUIRED)
  222. {
  223. /* Required proto2 fields inside proto3 submessage, pretty rare case */
  224. return false;
  225. }
  226. else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  227. {
  228. /* Repeated fields inside proto3 submessage: present if count != 0 */
  229. return *(const pb_size_t*)field->pSize == 0;
  230. }
  231. else if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
  232. {
  233. /* Oneof fields */
  234. return *(const pb_size_t*)field->pSize == 0;
  235. }
  236. else if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
  237. {
  238. /* Proto2 optional fields inside proto3 message, or proto3
  239. * submessage fields. */
  240. return safe_read_bool(field->pSize) == false;
  241. }
  242. else if (field->descriptor->default_value)
  243. {
  244. /* Proto3 messages do not have default values, but proto2 messages
  245. * can contain optional fields without has_fields (generator option 'proto3').
  246. * In this case they must always be encoded, to make sure that the
  247. * non-zero default value is overwritten.
  248. */
  249. return false;
  250. }
  251. /* Rest is proto3 singular fields */
  252. if (PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
  253. {
  254. /* Simple integer / float fields */
  255. pb_size_t i;
  256. const char *p = (const char*)field->pData;
  257. for (i = 0; i < field->data_size; i++)
  258. {
  259. if (p[i] != 0)
  260. {
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. else if (PB_LTYPE(type) == PB_LTYPE_BYTES)
  267. {
  268. const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)field->pData;
  269. return bytes->size == 0;
  270. }
  271. else if (PB_LTYPE(type) == PB_LTYPE_STRING)
  272. {
  273. return *(const char*)field->pData == '\0';
  274. }
  275. else if (PB_LTYPE(type) == PB_LTYPE_FIXED_LENGTH_BYTES)
  276. {
  277. /* Fixed length bytes is only empty if its length is fixed
  278. * as 0. Which would be pretty strange, but we can check
  279. * it anyway. */
  280. return field->data_size == 0;
  281. }
  282. else if (PB_LTYPE_IS_SUBMSG(type))
  283. {
  284. /* Check all fields in the submessage to find if any of them
  285. * are non-zero. The comparison cannot be done byte-per-byte
  286. * because the C struct may contain padding bytes that must
  287. * be skipped. Note that usually proto3 submessages have
  288. * a separate has_field that is checked earlier in this if.
  289. */
  290. pb_field_iter_t iter;
  291. if (pb_field_iter_begin(&iter, field->submsg_desc, field->pData))
  292. {
  293. do
  294. {
  295. if (!pb_check_proto3_default_value(&iter))
  296. {
  297. return false;
  298. }
  299. } while (pb_field_iter_next(&iter));
  300. }
  301. return true;
  302. }
  303. }
  304. else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  305. {
  306. return field->pData == NULL;
  307. }
  308. else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
  309. {
  310. if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
  311. {
  312. const pb_extension_t *extension = *(const pb_extension_t* const *)field->pData;
  313. return extension == NULL;
  314. }
  315. else if (field->descriptor->field_callback == pb_default_field_callback)
  316. {
  317. pb_callback_t *pCallback = (pb_callback_t*)field->pData;
  318. return pCallback->funcs.encode == NULL;
  319. }
  320. else
  321. {
  322. return field->descriptor->field_callback == NULL;
  323. }
  324. }
  325. return false; /* Not typically reached, safe default for weird special cases. */
  326. }
  327. /* Encode a field with static or pointer allocation, i.e. one whose data
  328. * is available to the encoder directly. */
  329. static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  330. {
  331. if (!field->pData)
  332. {
  333. /* Missing pointer field */
  334. return true;
  335. }
  336. if (!pb_encode_tag_for_field(stream, field))
  337. return false;
  338. switch (PB_LTYPE(field->type))
  339. {
  340. case PB_LTYPE_BOOL:
  341. return pb_enc_bool(stream, field);
  342. case PB_LTYPE_VARINT:
  343. case PB_LTYPE_UVARINT:
  344. case PB_LTYPE_SVARINT:
  345. return pb_enc_varint(stream, field);
  346. case PB_LTYPE_FIXED32:
  347. case PB_LTYPE_FIXED64:
  348. return pb_enc_fixed(stream, field);
  349. case PB_LTYPE_BYTES:
  350. return pb_enc_bytes(stream, field);
  351. case PB_LTYPE_STRING:
  352. return pb_enc_string(stream, field);
  353. case PB_LTYPE_SUBMESSAGE:
  354. case PB_LTYPE_SUBMSG_W_CB:
  355. return pb_enc_submessage(stream, field);
  356. case PB_LTYPE_FIXED_LENGTH_BYTES:
  357. return pb_enc_fixed_length_bytes(stream, field);
  358. default:
  359. PB_RETURN_ERROR(stream, "invalid field type");
  360. }
  361. }
  362. /* Encode a field with callback semantics. This means that a user function is
  363. * called to provide and encode the actual data. */
  364. static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  365. {
  366. if (field->descriptor->field_callback != NULL)
  367. {
  368. if (!field->descriptor->field_callback(NULL, stream, field))
  369. PB_RETURN_ERROR(stream, "callback error");
  370. }
  371. return true;
  372. }
  373. /* Encode a single field of any callback, pointer or static type. */
  374. static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field)
  375. {
  376. /* Check field presence */
  377. if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
  378. {
  379. if (*(const pb_size_t*)field->pSize != field->tag)
  380. {
  381. /* Different type oneof field */
  382. return true;
  383. }
  384. }
  385. else if (PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL)
  386. {
  387. if (field->pSize)
  388. {
  389. if (safe_read_bool(field->pSize) == false)
  390. {
  391. /* Missing optional field */
  392. return true;
  393. }
  394. }
  395. else if (PB_ATYPE(field->type) == PB_ATYPE_STATIC)
  396. {
  397. /* Proto3 singular field */
  398. if (pb_check_proto3_default_value(field))
  399. return true;
  400. }
  401. }
  402. if (!field->pData)
  403. {
  404. if (PB_HTYPE(field->type) == PB_HTYPE_REQUIRED)
  405. PB_RETURN_ERROR(stream, "missing required field");
  406. /* Pointer field set to NULL */
  407. return true;
  408. }
  409. /* Then encode field contents */
  410. if (PB_ATYPE(field->type) == PB_ATYPE_CALLBACK)
  411. {
  412. return encode_callback_field(stream, field);
  413. }
  414. else if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
  415. {
  416. return encode_array(stream, field);
  417. }
  418. else
  419. {
  420. return encode_basic_field(stream, field);
  421. }
  422. }
  423. /* Default handler for extension fields. Expects to have a pb_msgdesc_t
  424. * pointer in the extension->type->arg field, pointing to a message with
  425. * only one field in it. */
  426. static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension)
  427. {
  428. pb_field_iter_t iter;
  429. if (!pb_field_iter_begin_extension_const(&iter, extension))
  430. PB_RETURN_ERROR(stream, "invalid extension");
  431. return encode_field(stream, &iter);
  432. }
  433. /* Walk through all the registered extensions and give them a chance
  434. * to encode themselves. */
  435. static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  436. {
  437. const pb_extension_t *extension = *(const pb_extension_t* const *)field->pData;
  438. while (extension)
  439. {
  440. bool status;
  441. if (extension->type->encode)
  442. status = extension->type->encode(stream, extension);
  443. else
  444. status = default_extension_encoder(stream, extension);
  445. if (!status)
  446. return false;
  447. extension = extension->next;
  448. }
  449. return true;
  450. }
  451. /*********************
  452. * Encode all fields *
  453. *********************/
  454. bool checkreturn pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
  455. {
  456. pb_field_iter_t iter;
  457. if (!pb_field_iter_begin_const(&iter, fields, src_struct))
  458. return true; /* Empty message type */
  459. do {
  460. if (PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
  461. {
  462. /* Special case for the extension field placeholder */
  463. if (!encode_extension_field(stream, &iter))
  464. return false;
  465. }
  466. else
  467. {
  468. /* Regular field */
  469. if (!encode_field(stream, &iter))
  470. return false;
  471. }
  472. } while (pb_field_iter_next(&iter));
  473. return true;
  474. }
  475. bool checkreturn pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags)
  476. {
  477. if ((flags & PB_ENCODE_DELIMITED) != 0)
  478. {
  479. return pb_encode_submessage(stream, fields, src_struct);
  480. }
  481. else if ((flags & PB_ENCODE_NULLTERMINATED) != 0)
  482. {
  483. const pb_byte_t zero = 0;
  484. if (!pb_encode(stream, fields, src_struct))
  485. return false;
  486. return pb_write(stream, &zero, 1);
  487. }
  488. else
  489. {
  490. return pb_encode(stream, fields, src_struct);
  491. }
  492. }
  493. bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct)
  494. {
  495. pb_ostream_t stream = PB_OSTREAM_SIZING;
  496. if (!pb_encode(&stream, fields, src_struct))
  497. return false;
  498. *size = stream.bytes_written;
  499. return true;
  500. }
  501. /********************
  502. * Helper functions *
  503. ********************/
  504. /* This function avoids 64-bit shifts as they are quite slow on many platforms. */
  505. static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high)
  506. {
  507. size_t i = 0;
  508. pb_byte_t buffer[10];
  509. pb_byte_t byte = (pb_byte_t)(low & 0x7F);
  510. low >>= 7;
  511. while (i < 4 && (low != 0 || high != 0))
  512. {
  513. byte |= 0x80;
  514. buffer[i++] = byte;
  515. byte = (pb_byte_t)(low & 0x7F);
  516. low >>= 7;
  517. }
  518. if (high)
  519. {
  520. byte = (pb_byte_t)(byte | ((high & 0x07) << 4));
  521. high >>= 3;
  522. while (high)
  523. {
  524. byte |= 0x80;
  525. buffer[i++] = byte;
  526. byte = (pb_byte_t)(high & 0x7F);
  527. high >>= 7;
  528. }
  529. }
  530. buffer[i++] = byte;
  531. return pb_write(stream, buffer, i);
  532. }
  533. bool checkreturn pb_encode_varint(pb_ostream_t *stream, pb_uint64_t value)
  534. {
  535. if (value <= 0x7F)
  536. {
  537. /* Fast path: single byte */
  538. pb_byte_t byte = (pb_byte_t)value;
  539. return pb_write(stream, &byte, 1);
  540. }
  541. else
  542. {
  543. #ifdef PB_WITHOUT_64BIT
  544. return pb_encode_varint_32(stream, value, 0);
  545. #else
  546. return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)(value >> 32));
  547. #endif
  548. }
  549. }
  550. bool checkreturn pb_encode_svarint(pb_ostream_t *stream, pb_int64_t value)
  551. {
  552. pb_uint64_t zigzagged;
  553. if (value < 0)
  554. zigzagged = ~((pb_uint64_t)value << 1);
  555. else
  556. zigzagged = (pb_uint64_t)value << 1;
  557. return pb_encode_varint(stream, zigzagged);
  558. }
  559. bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
  560. {
  561. #if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
  562. /* Fast path if we know that we're on little endian */
  563. return pb_write(stream, (const pb_byte_t*)value, 4);
  564. #else
  565. uint32_t val = *(const uint32_t*)value;
  566. pb_byte_t bytes[4];
  567. bytes[0] = (pb_byte_t)(val & 0xFF);
  568. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  569. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  570. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  571. return pb_write(stream, bytes, 4);
  572. #endif
  573. }
  574. #ifndef PB_WITHOUT_64BIT
  575. bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
  576. {
  577. #if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
  578. /* Fast path if we know that we're on little endian */
  579. return pb_write(stream, (const pb_byte_t*)value, 8);
  580. #else
  581. uint64_t val = *(const uint64_t*)value;
  582. pb_byte_t bytes[8];
  583. bytes[0] = (pb_byte_t)(val & 0xFF);
  584. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  585. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  586. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  587. bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
  588. bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
  589. bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
  590. bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
  591. return pb_write(stream, bytes, 8);
  592. #endif
  593. }
  594. #endif
  595. bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
  596. {
  597. pb_uint64_t tag = ((pb_uint64_t)field_number << 3) | wiretype;
  598. return pb_encode_varint(stream, tag);
  599. }
  600. bool pb_encode_tag_for_field ( pb_ostream_t* stream, const pb_field_iter_t* field )
  601. {
  602. pb_wire_type_t wiretype;
  603. switch (PB_LTYPE(field->type))
  604. {
  605. case PB_LTYPE_BOOL:
  606. case PB_LTYPE_VARINT:
  607. case PB_LTYPE_UVARINT:
  608. case PB_LTYPE_SVARINT:
  609. wiretype = PB_WT_VARINT;
  610. break;
  611. case PB_LTYPE_FIXED32:
  612. wiretype = PB_WT_32BIT;
  613. break;
  614. case PB_LTYPE_FIXED64:
  615. wiretype = PB_WT_64BIT;
  616. break;
  617. case PB_LTYPE_BYTES:
  618. case PB_LTYPE_STRING:
  619. case PB_LTYPE_SUBMESSAGE:
  620. case PB_LTYPE_SUBMSG_W_CB:
  621. case PB_LTYPE_FIXED_LENGTH_BYTES:
  622. wiretype = PB_WT_STRING;
  623. break;
  624. default:
  625. PB_RETURN_ERROR(stream, "invalid field type");
  626. }
  627. return pb_encode_tag(stream, wiretype, field->tag);
  628. }
  629. bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
  630. {
  631. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  632. return false;
  633. return pb_write(stream, buffer, size);
  634. }
  635. bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
  636. {
  637. /* First calculate the message size using a non-writing substream. */
  638. pb_ostream_t substream = PB_OSTREAM_SIZING;
  639. size_t size;
  640. bool status;
  641. if (!pb_encode(&substream, fields, src_struct))
  642. {
  643. #ifndef PB_NO_ERRMSG
  644. stream->errmsg = substream.errmsg;
  645. #endif
  646. return false;
  647. }
  648. size = substream.bytes_written;
  649. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  650. return false;
  651. if (stream->callback == NULL)
  652. return pb_write(stream, NULL, size); /* Just sizing */
  653. if (stream->bytes_written + size > stream->max_size)
  654. PB_RETURN_ERROR(stream, "stream full");
  655. /* Use a substream to verify that a callback doesn't write more than
  656. * what it did the first time. */
  657. substream.callback = stream->callback;
  658. substream.state = stream->state;
  659. substream.max_size = size;
  660. substream.bytes_written = 0;
  661. #ifndef PB_NO_ERRMSG
  662. substream.errmsg = NULL;
  663. #endif
  664. status = pb_encode(&substream, fields, src_struct);
  665. stream->bytes_written += substream.bytes_written;
  666. stream->state = substream.state;
  667. #ifndef PB_NO_ERRMSG
  668. stream->errmsg = substream.errmsg;
  669. #endif
  670. if (substream.bytes_written != size)
  671. PB_RETURN_ERROR(stream, "submsg size changed");
  672. return status;
  673. }
  674. /* Field encoders */
  675. static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field)
  676. {
  677. uint32_t value = safe_read_bool(field->pData) ? 1 : 0;
  678. PB_UNUSED(field);
  679. return pb_encode_varint(stream, value);
  680. }
  681. static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field)
  682. {
  683. if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
  684. {
  685. /* Perform unsigned integer extension */
  686. pb_uint64_t value = 0;
  687. if (field->data_size == sizeof(uint_least8_t))
  688. value = *(const uint_least8_t*)field->pData;
  689. else if (field->data_size == sizeof(uint_least16_t))
  690. value = *(const uint_least16_t*)field->pData;
  691. else if (field->data_size == sizeof(uint32_t))
  692. value = *(const uint32_t*)field->pData;
  693. else if (field->data_size == sizeof(pb_uint64_t))
  694. value = *(const pb_uint64_t*)field->pData;
  695. else
  696. PB_RETURN_ERROR(stream, "invalid data_size");
  697. return pb_encode_varint(stream, value);
  698. }
  699. else
  700. {
  701. /* Perform signed integer extension */
  702. pb_int64_t value = 0;
  703. if (field->data_size == sizeof(int_least8_t))
  704. value = *(const int_least8_t*)field->pData;
  705. else if (field->data_size == sizeof(int_least16_t))
  706. value = *(const int_least16_t*)field->pData;
  707. else if (field->data_size == sizeof(int32_t))
  708. value = *(const int32_t*)field->pData;
  709. else if (field->data_size == sizeof(pb_int64_t))
  710. value = *(const pb_int64_t*)field->pData;
  711. else
  712. PB_RETURN_ERROR(stream, "invalid data_size");
  713. if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
  714. return pb_encode_svarint(stream, value);
  715. #ifdef PB_WITHOUT_64BIT
  716. else if (value < 0)
  717. return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)-1);
  718. #endif
  719. else
  720. return pb_encode_varint(stream, (pb_uint64_t)value);
  721. }
  722. }
  723. static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field)
  724. {
  725. #ifdef PB_CONVERT_DOUBLE_FLOAT
  726. if (field->data_size == sizeof(float) && PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  727. {
  728. return pb_encode_float_as_double(stream, *(float*)field->pData);
  729. }
  730. #endif
  731. if (field->data_size == sizeof(uint32_t))
  732. {
  733. return pb_encode_fixed32(stream, field->pData);
  734. }
  735. #ifndef PB_WITHOUT_64BIT
  736. else if (field->data_size == sizeof(uint64_t))
  737. {
  738. return pb_encode_fixed64(stream, field->pData);
  739. }
  740. #endif
  741. else
  742. {
  743. PB_RETURN_ERROR(stream, "invalid data_size");
  744. }
  745. }
  746. static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
  747. {
  748. const pb_bytes_array_t *bytes = NULL;
  749. bytes = (const pb_bytes_array_t*)field->pData;
  750. if (bytes == NULL)
  751. {
  752. /* Treat null pointer as an empty bytes field */
  753. return pb_encode_string(stream, NULL, 0);
  754. }
  755. if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
  756. bytes->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
  757. {
  758. PB_RETURN_ERROR(stream, "bytes size exceeded");
  759. }
  760. return pb_encode_string(stream, bytes->bytes, (size_t)bytes->size);
  761. }
  762. static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field)
  763. {
  764. size_t size = 0;
  765. size_t max_size = (size_t)field->data_size;
  766. const char *str = (const char*)field->pData;
  767. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  768. {
  769. max_size = (size_t)-1;
  770. }
  771. else
  772. {
  773. /* pb_dec_string() assumes string fields end with a null
  774. * terminator when the type isn't PB_ATYPE_POINTER, so we
  775. * shouldn't allow more than max-1 bytes to be written to
  776. * allow space for the null terminator.
  777. */
  778. if (max_size == 0)
  779. PB_RETURN_ERROR(stream, "zero-length string");
  780. max_size -= 1;
  781. }
  782. if (str == NULL)
  783. {
  784. size = 0; /* Treat null pointer as an empty string */
  785. }
  786. else
  787. {
  788. const char *p = str;
  789. /* strnlen() is not always available, so just use a loop */
  790. while (size < max_size && *p != '\0')
  791. {
  792. size++;
  793. p++;
  794. }
  795. if (*p != '\0')
  796. {
  797. PB_RETURN_ERROR(stream, "unterminated string");
  798. }
  799. }
  800. #ifdef PB_VALIDATE_UTF8
  801. if (!pb_validate_utf8(str))
  802. PB_RETURN_ERROR(stream, "invalid utf8");
  803. #endif
  804. return pb_encode_string(stream, (const pb_byte_t*)str, size);
  805. }
  806. static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field)
  807. {
  808. if (field->submsg_desc == NULL)
  809. PB_RETURN_ERROR(stream, "invalid field descriptor");
  810. if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL)
  811. {
  812. /* Message callback is stored right before pSize. */
  813. pb_callback_t *callback = (pb_callback_t*)field->pSize - 1;
  814. if (callback->funcs.encode)
  815. {
  816. if (!callback->funcs.encode(stream, field, &callback->arg))
  817. return false;
  818. }
  819. }
  820. return pb_encode_submessage(stream, field->submsg_desc, field->pData);
  821. }
  822. static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
  823. {
  824. return pb_encode_string(stream, (const pb_byte_t*)field->pData, (size_t)field->data_size);
  825. }
  826. #ifdef PB_CONVERT_DOUBLE_FLOAT
  827. bool pb_encode_float_as_double(pb_ostream_t *stream, float value)
  828. {
  829. union { float f; uint32_t i; } in;
  830. uint_least8_t sign;
  831. int exponent;
  832. uint64_t mantissa;
  833. in.f = value;
  834. /* Decompose input value */
  835. sign = (uint_least8_t)((in.i >> 31) & 1);
  836. exponent = (int)((in.i >> 23) & 0xFF) - 127;
  837. mantissa = in.i & 0x7FFFFF;
  838. if (exponent == 128)
  839. {
  840. /* Special value (NaN etc.) */
  841. exponent = 1024;
  842. }
  843. else if (exponent == -127)
  844. {
  845. if (!mantissa)
  846. {
  847. /* Zero */
  848. exponent = -1023;
  849. }
  850. else
  851. {
  852. /* Denormalized */
  853. mantissa <<= 1;
  854. while (!(mantissa & 0x800000))
  855. {
  856. mantissa <<= 1;
  857. exponent--;
  858. }
  859. mantissa &= 0x7FFFFF;
  860. }
  861. }
  862. /* Combine fields */
  863. mantissa <<= 29;
  864. mantissa |= (uint64_t)(exponent + 1023) << 52;
  865. mantissa |= (uint64_t)sign << 63;
  866. return pb_encode_fixed64(stream, &mantissa);
  867. }
  868. #endif