pb_common.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* pb_common.c: Common support functions for pb_encode.c and pb_decode.c.
  2. *
  3. * 2014 Petteri Aimonen <jpa@kapsi.fi>
  4. */
  5. #include "pb_common.h"
  6. bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct)
  7. {
  8. iter->start = fields;
  9. iter->pos = fields;
  10. iter->required_field_index = 0;
  11. iter->dest_struct = dest_struct;
  12. iter->pData = (char*)dest_struct + iter->pos->data_offset;
  13. iter->pSize = (char*)iter->pData + iter->pos->size_offset;
  14. return (iter->pos->tag != 0);
  15. }
  16. bool pb_field_iter_next(pb_field_iter_t *iter)
  17. {
  18. const pb_field_t *prev_field = iter->pos;
  19. if (prev_field->tag == 0)
  20. {
  21. /* Handle empty message types, where the first field is already the terminator.
  22. * In other cases, the iter->pos never points to the terminator. */
  23. return false;
  24. }
  25. iter->pos++;
  26. if (iter->pos->tag == 0)
  27. {
  28. /* Wrapped back to beginning, reinitialize */
  29. (void)pb_field_iter_begin(iter, iter->start, iter->dest_struct);
  30. return false;
  31. }
  32. else
  33. {
  34. /* Increment the pointers based on previous field size */
  35. size_t prev_size = prev_field->data_size;
  36. if (PB_HTYPE(prev_field->type) == PB_HTYPE_ONEOF &&
  37. PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
  38. {
  39. /* Don't advance pointers inside unions */
  40. prev_size = 0;
  41. iter->pData = (char*)iter->pData - prev_field->data_offset;
  42. }
  43. else if (PB_ATYPE(prev_field->type) == PB_ATYPE_STATIC &&
  44. PB_HTYPE(prev_field->type) == PB_HTYPE_REPEATED)
  45. {
  46. /* In static arrays, the data_size tells the size of a single entry and
  47. * array_size is the number of entries */
  48. prev_size *= prev_field->array_size;
  49. }
  50. else if (PB_ATYPE(prev_field->type) == PB_ATYPE_POINTER)
  51. {
  52. /* Pointer fields always have a constant size in the main structure.
  53. * The data_size only applies to the dynamically allocated area. */
  54. prev_size = sizeof(void*);
  55. }
  56. if (PB_HTYPE(prev_field->type) == PB_HTYPE_REQUIRED)
  57. {
  58. /* Count the required fields, in order to check their presence in the
  59. * decoder. */
  60. iter->required_field_index++;
  61. }
  62. iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
  63. iter->pSize = (char*)iter->pData + iter->pos->size_offset;
  64. return true;
  65. }
  66. }
  67. bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag)
  68. {
  69. const pb_field_t *start = iter->pos;
  70. do {
  71. if (iter->pos->tag == tag &&
  72. PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
  73. {
  74. /* Found the wanted field */
  75. return true;
  76. }
  77. (void)pb_field_iter_next(iter);
  78. } while (iter->pos != start);
  79. /* Searched all the way back to start, and found nothing. */
  80. return false;
  81. }