bool pb_field_iter_next(pb_field_iter_t *iter) { const pb_field_t *prev_field = iter->pos; if (prev_field->tag == 0) { /* Handle empty message types, where the first field is already the terminator. * In other cases, the iter->pos never points to the terminator. */ return false; } iter->pos++; if (iter->pos->tag == 0) { /* Wrapped back to beginning, reinitialize */ (void)pb_field_iter_begin(iter, iter->start, iter->dest_struct); return false; } else { /* Increment the pointers based on previous field size */ size_t prev_size = prev_field->data_size; if (PB_HTYPE(prev_field->type) == PB_HTYPE_ONEOF && PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF) { /* Don't advance pointers inside unions */ prev_size = 0; iter->pData = (char*)iter->pData - prev_field->data_offset; } else if (PB_ATYPE(prev_field->type) == PB_ATYPE_STATIC && PB_HTYPE(prev_field->type) == PB_HTYPE_REPEATED) { /* In static arrays, the data_size tells the size of a single entry and * array_size is the number of entries */ prev_size *= prev_field->array_size; } else if (PB_ATYPE(prev_field->type) == PB_ATYPE_POINTER) { /* Pointer fields always have a constant size in the main structure. * The data_size only applies to the dynamically allocated area. */ prev_size = sizeof(void*); } if (PB_HTYPE(prev_field->type) == PB_HTYPE_REQUIRED) { /* Count the required fields, in order to check their presence in the * decoder. */ iter->required_field_index++; } iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset; iter->pSize = (char*)iter->pData + iter->pos->size_offset; return true; } }
static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src) { size_t size = 0; size_t max_size = field->data_size; const char *p = (const char*)src; if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) max_size = (size_t)-1; if (src == NULL) { size = 0; /* Threat null pointer as an empty string */ } else { /* strnlen() is not always available, so just use a loop */ while (size < max_size && *p != '\0') { size++; p++; } } return pb_encode_string(stream, (const uint8_t*)src, size); }
static bool pb_field_next(pb_field_iterator_t *iter) { bool notwrapped = true; size_t prev_size = iter->current->data_size; if (PB_ATYPE(iter->current->type) == PB_ATYPE_STATIC && PB_HTYPE(iter->current->type) == PB_HTYPE_REPEATED) { prev_size *= iter->current->array_size; } if (PB_HTYPE(iter->current->type) == PB_HTYPE_REQUIRED) iter->required_field_index++; iter->current++; iter->field_index++; if (iter->current->tag == 0) { iter->current = iter->start; iter->field_index = 0; iter->required_field_index = 0; iter->pData = iter->dest_struct; prev_size = 0; notwrapped = false; } iter->pData = (char*)iter->pData + prev_size + iter->current->data_offset; iter->pSize = (char*)iter->pData + iter->current->size_offset; return notwrapped; }
/* Initialize message fields to default values, recursively */ static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct) { pb_field_iterator_t iter; pb_field_init(&iter, fields, dest_struct); /* Initialize size/has fields and apply default values */ do { pb_type_t type; type = iter.current->type; if (iter.current->tag == 0) continue; if (PB_ATYPE(type) == PB_ATYPE_STATIC) { /* Initialize the size field for optional/repeated fields to 0. */ if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL) { *(bool*)iter.pSize = false; } else if (PB_HTYPE(type) == PB_HTYPE_REPEATED) { *(size_t*)iter.pSize = 0; continue; /* Array is empty, no need to initialize contents */ } /* Initialize field contents to default value */ if (PB_LTYPE(iter.current->type) == PB_LTYPE_SUBMESSAGE) { pb_message_set_to_defaults((const pb_field_t *) iter.current->ptr, iter.pData); } else if (iter.current->ptr != NULL) { memcpy(iter.pData, iter.current->ptr, iter.current->data_size); } else { memset(iter.pData, 0, iter.current->data_size); } } else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK) { continue; /* Don't overwrite callback */ } } while (pb_field_next(&iter)); }
/* Encode a field with static or pointer allocation, i.e. one whose data * is available to the encoder directly. */ static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData) { pb_encoder_t func; const void *pSize; bool implicit_has = true; func = PB_ENCODERS[PB_LTYPE(field->type)]; if (field->size_offset) pSize = (const char*)pData + field->size_offset; else pSize = &implicit_has; if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) { /* pData is a pointer to the field, which contains pointer to * the data. If the 2nd pointer is NULL, it is interpreted as if * the has_field was false. */ pData = *(const void* const*)pData; implicit_has = (pData != NULL); } switch (PB_HTYPE(field->type)) { case PB_HTYPE_REQUIRED: if (!pData) PB_RETURN_ERROR(stream, "missing required field"); if (!pb_encode_tag_for_field(stream, field)) return false; if (!func(stream, field, pData)) return false; break; case PB_HTYPE_OPTIONAL: if (*(const bool*)pSize) { if (!pb_encode_tag_for_field(stream, field)) return false; if (!func(stream, field, pData)) return false; } break; case PB_HTYPE_REPEATED: if (!encode_array(stream, field, pData, *(const size_t*)pSize, func)) return false; break; default: PB_RETURN_ERROR(stream, "invalid field type"); } return true; }
static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter) { switch (PB_ATYPE(iter->current->type)) { case PB_ATYPE_STATIC: return decode_static_field(stream, wire_type, iter); case PB_ATYPE_CALLBACK: return decode_callback_field(stream, wire_type, iter); default: PB_RETURN_ERROR(stream, "invalid field type"); } }
/* Encode a single field of any callback or static type. */ static bool checkreturn encode_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData) { switch (PB_ATYPE(field->type)) { case PB_ATYPE_STATIC: return encode_static_field(stream, field, pData); case PB_ATYPE_CALLBACK: return encode_callback_field(stream, field, pData); default: PB_RETURN_ERROR(stream, "invalid field type"); } }
/* Default handler for extension fields. Expects to have a pb_field_t * pointer in the extension->type->arg field. */ static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension) { const pb_field_t *field = (const pb_field_t*)extension->type->arg; if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) { /* For pointer extensions, the pointer is stored directly * in the extension structure. This avoids having an extra * indirection. */ return encode_field(stream, field, &extension->dest); } else { return encode_field(stream, field, extension->dest); } }
static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src) { const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src; if (src == NULL) { /* Threat null pointer as an empty bytes field */ return pb_encode_string(stream, NULL, 0); } if (PB_ATYPE(field->type) == PB_ATYPE_STATIC && PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size) { PB_RETURN_ERROR(stream, "bytes size exceeded"); } return pb_encode_string(stream, bytes->bytes, bytes->size); }
bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) { const pb_field_t *field = fields; const void *pData = src_struct; size_t prev_size = 0; while (field->tag != 0) { pData = (const char*)pData + prev_size + field->data_offset; prev_size = field->data_size; /* Special case for static arrays */ if (PB_ATYPE(field->type) == PB_ATYPE_STATIC && PB_HTYPE(field->type) == PB_HTYPE_REPEATED) { prev_size *= field->array_size; } if (PB_LTYPE(field->type) == PB_LTYPE_EXTENSION) { /* Special case for the extension field placeholder */ if (!encode_extension_field(stream, field, pData)) return false; } else { /* Regular field */ if (!encode_field(stream, field, pData)) return false; } field++; } return true; }
/* Encode a static array. Handles the size calculations and possible packing. */ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field, const void *pData, size_t count, pb_encoder_t func) { size_t i; const void *p; size_t size; if (count == 0) return true; if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size) PB_RETURN_ERROR(stream, "array max size exceeded"); /* We always pack arrays if the datatype allows it. */ if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE) { if (!pb_encode_tag(stream, PB_WT_STRING, field->tag)) return false; /* Determine the total size of packed array. */ if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32) { size = 4 * count; } else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64) { size = 8 * count; } else { pb_ostream_t sizestream = PB_OSTREAM_SIZING; p = pData; for (i = 0; i < count; i++) { if (!func(&sizestream, field, p)) return false; p = (const char*)p + field->data_size; } size = sizestream.bytes_written; } if (!pb_encode_varint(stream, (uint64_t)size)) return false; if (stream->callback == NULL) return pb_write(stream, NULL, size); /* Just sizing.. */ /* Write the data */ p = pData; for (i = 0; i < count; i++) { if (!func(stream, field, p)) return false; p = (const char*)p + field->data_size; } } else { p = pData; for (i = 0; i < count; i++) { if (!pb_encode_tag_for_field(stream, field)) return false; /* Normally the data is stored directly in the array entries, but * for pointer-type string and bytes fields, the array entries are * actually pointers themselves also. So we have to dereference once * more to get to the actual data. */ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER && (PB_LTYPE(field->type) == PB_LTYPE_STRING || PB_LTYPE(field->type) == PB_LTYPE_BYTES)) { if (!func(stream, field, *(const void* const*)p)) return false; } else { if (!func(stream, field, p)) return false; } p = (const char*)p + field->data_size; } } return true; }