Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
pb_common.c
00001 /* pb_common.c: Common support functions for pb_encode.c and pb_decode.c. 00002 * 00003 * 2014 Petteri Aimonen <jpa@kapsi.fi> 00004 */ 00005 00006 #include "pb_common.h" 00007 00008 bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct) 00009 { 00010 iter->start = fields; 00011 iter->pos = fields; 00012 iter->required_field_index = 0; 00013 iter->dest_struct = dest_struct; 00014 iter->pData = (char*)dest_struct + iter->pos->data_offset; 00015 iter->pSize = (char*)iter->pData + iter->pos->size_offset; 00016 00017 return (iter->pos->tag != 0); 00018 } 00019 00020 bool pb_field_iter_next(pb_field_iter_t *iter) 00021 { 00022 const pb_field_t *prev_field = iter->pos; 00023 00024 if (prev_field->tag == 0) 00025 { 00026 /* Handle empty message types, where the first field is already the terminator. 00027 * In other cases, the iter->pos never points to the terminator. */ 00028 return false; 00029 } 00030 00031 iter->pos++; 00032 00033 if (iter->pos->tag == 0) 00034 { 00035 /* Wrapped back to beginning, reinitialize */ 00036 (void)pb_field_iter_begin(iter, iter->start, iter->dest_struct); 00037 return false; 00038 } 00039 else 00040 { 00041 /* Increment the pointers based on previous field size */ 00042 size_t prev_size = prev_field->data_size; 00043 00044 if (PB_HTYPE(prev_field->type) == PB_HTYPE_ONEOF && 00045 PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF) 00046 { 00047 /* Don't advance pointers inside unions */ 00048 prev_size = 0; 00049 iter->pData = (char*)iter->pData - prev_field->data_offset; 00050 } 00051 else if (PB_ATYPE(prev_field->type) == PB_ATYPE_STATIC && 00052 PB_HTYPE(prev_field->type) == PB_HTYPE_REPEATED) 00053 { 00054 /* In static arrays, the data_size tells the size of a single entry and 00055 * array_size is the number of entries */ 00056 prev_size *= prev_field->array_size; 00057 } 00058 else if (PB_ATYPE(prev_field->type) == PB_ATYPE_POINTER) 00059 { 00060 /* Pointer fields always have a constant size in the main structure. 00061 * The data_size only applies to the dynamically allocated area. */ 00062 prev_size = sizeof(void*); 00063 } 00064 00065 if (PB_HTYPE(prev_field->type) == PB_HTYPE_REQUIRED) 00066 { 00067 /* Count the required fields, in order to check their presence in the 00068 * decoder. */ 00069 iter->required_field_index++; 00070 } 00071 00072 iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset; 00073 iter->pSize = (char*)iter->pData + iter->pos->size_offset; 00074 return true; 00075 } 00076 } 00077 00078 bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag) 00079 { 00080 const pb_field_t *start = iter->pos; 00081 00082 do { 00083 if (iter->pos->tag == tag && 00084 PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION) 00085 { 00086 /* Found the wanted field */ 00087 return true; 00088 } 00089 00090 (void)pb_field_iter_next(iter); 00091 } while (iter->pos != start); 00092 00093 /* Searched all the way back to start, and found nothing. */ 00094 return false; 00095 } 00096 00097 00098
Generated on Fri Sep 23 2022 19:29:17 by
 1.7.2
 1.7.2