pb_encode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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 bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  27. static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  28. static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  29. static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  30. static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  31. static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  32. static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  33. static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src);
  34. /* --- Function pointers to field encoders ---
  35. * Order in the array must match pb_action_t LTYPE numbering.
  36. */
  37. static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
  38. &pb_enc_varint,
  39. &pb_enc_uvarint,
  40. &pb_enc_svarint,
  41. &pb_enc_fixed32,
  42. &pb_enc_fixed64,
  43. &pb_enc_bytes,
  44. &pb_enc_string,
  45. &pb_enc_submessage,
  46. NULL /* extensions */
  47. };
  48. /*******************************
  49. * pb_ostream_t implementation *
  50. *******************************/
  51. static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
  52. {
  53. pb_byte_t *dest = (pb_byte_t*)stream->state;
  54. stream->state = dest + count;
  55. while (count--)
  56. *dest++ = *buf++;
  57. return true;
  58. }
  59. pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
  60. {
  61. pb_ostream_t stream;
  62. #ifdef PB_BUFFER_ONLY
  63. stream.callback = (void*)1; /* Just a marker value */
  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 (stream->callback != NULL)
  78. {
  79. if (stream->bytes_written + count > stream->max_size)
  80. PB_RETURN_ERROR(stream, "stream full");
  81. #ifdef PB_BUFFER_ONLY
  82. if (!buf_write(stream, buf, count))
  83. PB_RETURN_ERROR(stream, "io error");
  84. #else
  85. if (!stream->callback(stream, buf, count))
  86. PB_RETURN_ERROR(stream, "io error");
  87. #endif
  88. }
  89. stream->bytes_written += count;
  90. return true;
  91. }
  92. /*************************
  93. * Encode a single field *
  94. *************************/
  95. /* Encode a static array. Handles the size calculations and possible packing. */
  96. static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
  97. const void *pData, size_t count, pb_encoder_t func)
  98. {
  99. size_t i;
  100. const void *p;
  101. size_t size;
  102. if (count == 0)
  103. return true;
  104. if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
  105. PB_RETURN_ERROR(stream, "array max size exceeded");
  106. /* We always pack arrays if the datatype allows it. */
  107. if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
  108. {
  109. if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
  110. return false;
  111. /* Determine the total size of packed array. */
  112. if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
  113. {
  114. size = 4 * count;
  115. }
  116. else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  117. {
  118. size = 8 * count;
  119. }
  120. else
  121. {
  122. pb_ostream_t sizestream = PB_OSTREAM_SIZING;
  123. p = pData;
  124. for (i = 0; i < count; i++)
  125. {
  126. if (!func(&sizestream, field, p))
  127. return false;
  128. p = (const char*)p + field->data_size;
  129. }
  130. size = sizestream.bytes_written;
  131. }
  132. if (!pb_encode_varint(stream, (uint64_t)size))
  133. return false;
  134. if (stream->callback == NULL)
  135. return pb_write(stream, NULL, size); /* Just sizing.. */
  136. /* Write the data */
  137. p = pData;
  138. for (i = 0; i < count; i++)
  139. {
  140. if (!func(stream, field, p))
  141. return false;
  142. p = (const char*)p + field->data_size;
  143. }
  144. }
  145. else
  146. {
  147. p = pData;
  148. for (i = 0; i < count; i++)
  149. {
  150. if (!pb_encode_tag_for_field(stream, field))
  151. return false;
  152. /* Normally the data is stored directly in the array entries, but
  153. * for pointer-type string and bytes fields, the array entries are
  154. * actually pointers themselves also. So we have to dereference once
  155. * more to get to the actual data. */
  156. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
  157. (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
  158. PB_LTYPE(field->type) == PB_LTYPE_BYTES))
  159. {
  160. if (!func(stream, field, *(const void* const*)p))
  161. return false;
  162. }
  163. else
  164. {
  165. if (!func(stream, field, p))
  166. return false;
  167. }
  168. p = (const char*)p + field->data_size;
  169. }
  170. }
  171. return true;
  172. }
  173. /* Encode a field with static or pointer allocation, i.e. one whose data
  174. * is available to the encoder directly. */
  175. static bool checkreturn encode_basic_field(pb_ostream_t *stream,
  176. const pb_field_t *field, const void *pData)
  177. {
  178. pb_encoder_t func;
  179. const void *pSize;
  180. bool implicit_has = true;
  181. func = PB_ENCODERS[PB_LTYPE(field->type)];
  182. if (field->size_offset)
  183. pSize = (const char*)pData + field->size_offset;
  184. else
  185. pSize = &implicit_has;
  186. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  187. {
  188. /* pData is a pointer to the field, which contains pointer to
  189. * the data. If the 2nd pointer is NULL, it is interpreted as if
  190. * the has_field was false.
  191. */
  192. pData = *(const void* const*)pData;
  193. implicit_has = (pData != NULL);
  194. }
  195. switch (PB_HTYPE(field->type))
  196. {
  197. case PB_HTYPE_REQUIRED:
  198. if (!pData)
  199. PB_RETURN_ERROR(stream, "missing required field");
  200. if (!pb_encode_tag_for_field(stream, field))
  201. return false;
  202. if (!func(stream, field, pData))
  203. return false;
  204. break;
  205. case PB_HTYPE_OPTIONAL:
  206. if (*(const bool*)pSize)
  207. {
  208. if (!pb_encode_tag_for_field(stream, field))
  209. return false;
  210. if (!func(stream, field, pData))
  211. return false;
  212. }
  213. break;
  214. case PB_HTYPE_REPEATED:
  215. if (!encode_array(stream, field, pData, *(const pb_size_t*)pSize, func))
  216. return false;
  217. break;
  218. case PB_HTYPE_ONEOF:
  219. if (*(const pb_size_t*)pSize == field->tag)
  220. {
  221. if (!pb_encode_tag_for_field(stream, field))
  222. return false;
  223. if (!func(stream, field, pData))
  224. return false;
  225. }
  226. break;
  227. default:
  228. PB_RETURN_ERROR(stream, "invalid field type");
  229. }
  230. return true;
  231. }
  232. /* Encode a field with callback semantics. This means that a user function is
  233. * called to provide and encode the actual data. */
  234. static bool checkreturn encode_callback_field(pb_ostream_t *stream,
  235. const pb_field_t *field, const void *pData)
  236. {
  237. const pb_callback_t *callback = (const pb_callback_t*)pData;
  238. #ifdef PB_OLD_CALLBACK_STYLE
  239. const void *arg = callback->arg;
  240. #else
  241. void * const *arg = &(callback->arg);
  242. #endif
  243. if (callback->funcs.encode != NULL)
  244. {
  245. if (!callback->funcs.encode(stream, field, arg))
  246. PB_RETURN_ERROR(stream, "callback error");
  247. }
  248. return true;
  249. }
  250. /* Encode a single field of any callback or static type. */
  251. static bool checkreturn encode_field(pb_ostream_t *stream,
  252. const pb_field_t *field, const void *pData)
  253. {
  254. switch (PB_ATYPE(field->type))
  255. {
  256. case PB_ATYPE_STATIC:
  257. case PB_ATYPE_POINTER:
  258. return encode_basic_field(stream, field, pData);
  259. case PB_ATYPE_CALLBACK:
  260. return encode_callback_field(stream, field, pData);
  261. default:
  262. PB_RETURN_ERROR(stream, "invalid field type");
  263. }
  264. }
  265. /* Default handler for extension fields. Expects to have a pb_field_t
  266. * pointer in the extension->type->arg field. */
  267. static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
  268. const pb_extension_t *extension)
  269. {
  270. const pb_field_t *field = (const pb_field_t*)extension->type->arg;
  271. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  272. {
  273. /* For pointer extensions, the pointer is stored directly
  274. * in the extension structure. This avoids having an extra
  275. * indirection. */
  276. return encode_field(stream, field, &extension->dest);
  277. }
  278. else
  279. {
  280. return encode_field(stream, field, extension->dest);
  281. }
  282. }
  283. /* Walk through all the registered extensions and give them a chance
  284. * to encode themselves. */
  285. static bool checkreturn encode_extension_field(pb_ostream_t *stream,
  286. const pb_field_t *field, const void *pData)
  287. {
  288. const pb_extension_t *extension = *(const pb_extension_t* const *)pData;
  289. PB_UNUSED(field);
  290. while (extension)
  291. {
  292. bool status;
  293. if (extension->type->encode)
  294. status = extension->type->encode(stream, extension);
  295. else
  296. status = default_extension_encoder(stream, extension);
  297. if (!status)
  298. return false;
  299. extension = extension->next;
  300. }
  301. return true;
  302. }
  303. /*********************
  304. * Encode all fields *
  305. *********************/
  306. static void *remove_const(const void *p)
  307. {
  308. /* Note: this casts away const, in order to use the common field iterator
  309. * logic for both encoding and decoding. */
  310. union {
  311. void *p1;
  312. const void *p2;
  313. } t;
  314. t.p2 = p;
  315. return t.p1;
  316. }
  317. bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
  318. {
  319. pb_field_iter_t iter;
  320. if (!pb_field_iter_begin(&iter, fields, remove_const(src_struct)))
  321. return true; /* Empty message type */
  322. do {
  323. if (PB_LTYPE(iter.pos->type) == PB_LTYPE_EXTENSION)
  324. {
  325. /* Special case for the extension field placeholder */
  326. if (!encode_extension_field(stream, iter.pos, iter.pData))
  327. return false;
  328. }
  329. else
  330. {
  331. /* Regular field */
  332. if (!encode_field(stream, iter.pos, iter.pData))
  333. return false;
  334. }
  335. } while (pb_field_iter_next(&iter));
  336. return true;
  337. }
  338. bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
  339. {
  340. return pb_encode_submessage(stream, fields, src_struct);
  341. }
  342. bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct)
  343. {
  344. pb_ostream_t stream = PB_OSTREAM_SIZING;
  345. if (!pb_encode(&stream, fields, src_struct))
  346. return false;
  347. *size = stream.bytes_written;
  348. return true;
  349. }
  350. /********************
  351. * Helper functions *
  352. ********************/
  353. bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
  354. {
  355. pb_byte_t buffer[10];
  356. size_t i = 0;
  357. if (value <= 0x7F)
  358. {
  359. pb_byte_t v = (pb_byte_t)value;
  360. return pb_write(stream, &v, 1);
  361. }
  362. while (value)
  363. {
  364. buffer[i] = (pb_byte_t)((value & 0x7F) | 0x80);
  365. value >>= 7;
  366. i++;
  367. }
  368. buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
  369. return pb_write(stream, buffer, i);
  370. }
  371. bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
  372. {
  373. uint64_t zigzagged;
  374. if (value < 0)
  375. zigzagged = ~((uint64_t)value << 1);
  376. else
  377. zigzagged = (uint64_t)value << 1;
  378. return pb_encode_varint(stream, zigzagged);
  379. }
  380. bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
  381. {
  382. uint32_t val = *(const uint32_t*)value;
  383. pb_byte_t bytes[4];
  384. bytes[0] = (pb_byte_t)(val & 0xFF);
  385. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  386. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  387. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  388. return pb_write(stream, bytes, 4);
  389. }
  390. bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
  391. {
  392. uint64_t val = *(const uint64_t*)value;
  393. pb_byte_t bytes[8];
  394. bytes[0] = (pb_byte_t)(val & 0xFF);
  395. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  396. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  397. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  398. bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
  399. bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
  400. bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
  401. bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
  402. return pb_write(stream, bytes, 8);
  403. }
  404. bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
  405. {
  406. uint64_t tag = ((uint64_t)field_number << 3) | wiretype;
  407. return pb_encode_varint(stream, tag);
  408. }
  409. bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
  410. {
  411. pb_wire_type_t wiretype;
  412. switch (PB_LTYPE(field->type))
  413. {
  414. case PB_LTYPE_VARINT:
  415. case PB_LTYPE_UVARINT:
  416. case PB_LTYPE_SVARINT:
  417. wiretype = PB_WT_VARINT;
  418. break;
  419. case PB_LTYPE_FIXED32:
  420. wiretype = PB_WT_32BIT;
  421. break;
  422. case PB_LTYPE_FIXED64:
  423. wiretype = PB_WT_64BIT;
  424. break;
  425. case PB_LTYPE_BYTES:
  426. case PB_LTYPE_STRING:
  427. case PB_LTYPE_SUBMESSAGE:
  428. wiretype = PB_WT_STRING;
  429. break;
  430. default:
  431. PB_RETURN_ERROR(stream, "invalid field type");
  432. }
  433. return pb_encode_tag(stream, wiretype, field->tag);
  434. }
  435. bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
  436. {
  437. if (!pb_encode_varint(stream, (uint64_t)size))
  438. return false;
  439. return pb_write(stream, buffer, size);
  440. }
  441. bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
  442. {
  443. /* First calculate the message size using a non-writing substream. */
  444. pb_ostream_t substream = PB_OSTREAM_SIZING;
  445. size_t size;
  446. bool status;
  447. if (!pb_encode(&substream, fields, src_struct))
  448. {
  449. #ifndef PB_NO_ERRMSG
  450. stream->errmsg = substream.errmsg;
  451. #endif
  452. return false;
  453. }
  454. size = substream.bytes_written;
  455. if (!pb_encode_varint(stream, (uint64_t)size))
  456. return false;
  457. if (stream->callback == NULL)
  458. return pb_write(stream, NULL, size); /* Just sizing */
  459. if (stream->bytes_written + size > stream->max_size)
  460. PB_RETURN_ERROR(stream, "stream full");
  461. /* Use a substream to verify that a callback doesn't write more than
  462. * what it did the first time. */
  463. substream.callback = stream->callback;
  464. substream.state = stream->state;
  465. substream.max_size = size;
  466. substream.bytes_written = 0;
  467. #ifndef PB_NO_ERRMSG
  468. substream.errmsg = NULL;
  469. #endif
  470. status = pb_encode(&substream, fields, src_struct);
  471. stream->bytes_written += substream.bytes_written;
  472. stream->state = substream.state;
  473. #ifndef PB_NO_ERRMSG
  474. stream->errmsg = substream.errmsg;
  475. #endif
  476. if (substream.bytes_written != size)
  477. PB_RETURN_ERROR(stream, "submsg size changed");
  478. return status;
  479. }
  480. /* Field encoders */
  481. static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  482. {
  483. int64_t value = 0;
  484. if (field->data_size == sizeof(int_least8_t))
  485. value = *(const int_least8_t*)src;
  486. else if (field->data_size == sizeof(int_least16_t))
  487. value = *(const int_least16_t*)src;
  488. else if (field->data_size == sizeof(int32_t))
  489. value = *(const int32_t*)src;
  490. else if (field->data_size == sizeof(int64_t))
  491. value = *(const int64_t*)src;
  492. else
  493. PB_RETURN_ERROR(stream, "invalid data_size");
  494. return pb_encode_varint(stream, (uint64_t)value);
  495. }
  496. static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  497. {
  498. uint64_t value = 0;
  499. if (field->data_size == sizeof(uint_least8_t))
  500. value = *(const uint_least8_t*)src;
  501. else if (field->data_size == sizeof(uint_least16_t))
  502. value = *(const uint_least16_t*)src;
  503. else if (field->data_size == sizeof(uint32_t))
  504. value = *(const uint32_t*)src;
  505. else if (field->data_size == sizeof(uint64_t))
  506. value = *(const uint64_t*)src;
  507. else
  508. PB_RETURN_ERROR(stream, "invalid data_size");
  509. return pb_encode_varint(stream, value);
  510. }
  511. static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  512. {
  513. int64_t value = 0;
  514. if (field->data_size == sizeof(int_least8_t))
  515. value = *(const int_least8_t*)src;
  516. else if (field->data_size == sizeof(int_least16_t))
  517. value = *(const int_least16_t*)src;
  518. else if (field->data_size == sizeof(int32_t))
  519. value = *(const int32_t*)src;
  520. else if (field->data_size == sizeof(int64_t))
  521. value = *(const int64_t*)src;
  522. else
  523. PB_RETURN_ERROR(stream, "invalid data_size");
  524. return pb_encode_svarint(stream, value);
  525. }
  526. static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  527. {
  528. PB_UNUSED(field);
  529. return pb_encode_fixed64(stream, src);
  530. }
  531. static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  532. {
  533. PB_UNUSED(field);
  534. return pb_encode_fixed32(stream, src);
  535. }
  536. static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  537. {
  538. const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
  539. if (src == NULL)
  540. {
  541. /* Threat null pointer as an empty bytes field */
  542. return pb_encode_string(stream, NULL, 0);
  543. }
  544. if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
  545. PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
  546. {
  547. PB_RETURN_ERROR(stream, "bytes size exceeded");
  548. }
  549. return pb_encode_string(stream, bytes->bytes, bytes->size);
  550. }
  551. static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  552. {
  553. size_t size = 0;
  554. size_t max_size = field->data_size;
  555. const char *p = (const char*)src;
  556. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  557. max_size = (size_t)-1;
  558. if (src == NULL)
  559. {
  560. size = 0; /* Threat null pointer as an empty string */
  561. }
  562. else
  563. {
  564. /* strnlen() is not always available, so just use a loop */
  565. while (size < max_size && *p != '\0')
  566. {
  567. size++;
  568. p++;
  569. }
  570. }
  571. return pb_encode_string(stream, (const pb_byte_t*)src, size);
  572. }
  573. static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
  574. {
  575. if (field->ptr == NULL)
  576. PB_RETURN_ERROR(stream, "invalid field descriptor");
  577. return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
  578. }