X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=pb_decode.c;h=8e01fd7aff36f27dc4ed2f62ebc5da832cb0a259;hb=0ee4bb96b1e53d16a29869864eff87c8934894ae;hp=9d65504c611c86fd62ea669a2fead500d965dc53;hpb=1463e687e36c8dd404d33c6ef1cba61b574adc1e;p=apps%2Fagl-service-can-low-level.git diff --git a/pb_decode.c b/pb_decode.c index 9d65504c..8e01fd7a 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -36,26 +36,41 @@ static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = { * pb_istream * **************/ -bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count) +static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count) { - if (stream->bytes_left < count) - PB_RETURN_ERROR(stream, "end-of-stream"); + uint8_t *source = (uint8_t*)stream->state; - if (!stream->callback(stream, buf, count)) - PB_RETURN_ERROR(stream, "io error"); + if (buf != NULL) + memcpy(buf, source, count); - stream->bytes_left -= count; + stream->state = source + count; return true; } -static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count) +bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count) { - uint8_t *source = (uint8_t*)stream->state; + if (buf == NULL && stream->callback != buf_read) + { + /* Skip input bytes */ + uint8_t tmp[16]; + while (count > 16) + { + if (!pb_read(stream, tmp, 16)) + return false; + + count -= 16; + } + + return pb_read(stream, tmp, count); + } + + if (stream->bytes_left < count) + PB_RETURN_ERROR(stream, "end-of-stream"); - if (buf != NULL) - memcpy(buf, source, count); + if (!stream->callback(stream, buf, count)) + PB_RETURN_ERROR(stream, "io error"); - stream->state = source + count; + stream->bytes_left -= count; return true; } @@ -83,7 +98,7 @@ static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest) bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest) { uint8_t byte; - uint8_t bitpos = 0; + int bitpos = 0; *dest = 0; while (bitpos < 64 && pb_read(stream, &byte, 1)) @@ -415,13 +430,11 @@ static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_str * Decode all fields * *********************/ -bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct) +bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct) { uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0}; /* Used to check for required fields */ pb_field_iterator_t iter; - pb_message_set_to_defaults(fields, dest_struct); - pb_field_init(&iter, fields, dest_struct); while (stream->bytes_left) @@ -449,7 +462,7 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void if (PB_HTYPE(iter.current->type) == PB_HTYPE_REQUIRED && iter.required_field_index < PB_MAX_REQUIRED_FIELDS) { - fields_seen[iter.required_field_index >> 3] |= 1 << (iter.required_field_index & 7); + fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7)); } if (!decode_field(stream, wire_type, &iter)) @@ -470,6 +483,12 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void return true; } +bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct) +{ + pb_message_set_to_defaults(fields, dest_struct); + return pb_decode_noinit(stream, fields, dest_struct); +} + /* Field decoders */ bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest) @@ -479,9 +498,9 @@ bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest) return false; if (value & 1) - *dest = ~(value >> 1); + *dest = (int64_t)(~(value >> 1)); else - *dest = value >> 1; + *dest = (int64_t)(value >> 1); return true; } @@ -535,9 +554,9 @@ bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, vo switch (field->data_size) { - case 1: *(uint8_t*)dest = value; break; - case 2: *(uint16_t*)dest = value; break; - case 4: *(uint32_t*)dest = value; break; + case 1: *(uint8_t*)dest = (uint8_t)value; break; + case 2: *(uint16_t*)dest = (uint16_t)value; break; + case 4: *(uint32_t*)dest = (uint32_t)value; break; case 8: *(uint64_t*)dest = value; break; default: PB_RETURN_ERROR(stream, "invalid data_size"); } @@ -552,7 +571,7 @@ bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, v switch (field->data_size) { - case 4: *(int32_t*)dest = value; break; + case 4: *(int32_t*)dest = (int32_t)value; break; case 8: *(int64_t*)dest = value; break; default: PB_RETURN_ERROR(stream, "invalid data_size"); } @@ -608,6 +627,7 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field { bool status; pb_istream_t substream; + const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr; if (!pb_make_string_substream(stream, &substream)) return false; @@ -615,7 +635,13 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field if (field->ptr == NULL) PB_RETURN_ERROR(stream, "invalid field descriptor"); - status = pb_decode(&substream, (pb_field_t*)field->ptr, dest); + /* New array entries need to be initialized, while required and optional + * submessages have already been initialized in the top-level pb_decode. */ + if (PB_HTYPE(field->type) == PB_HTYPE_ARRAY) + status = pb_decode(&substream, submsg_fields, dest); + else + status = pb_decode_noinit(&substream, submsg_fields, dest); + pb_close_string_substream(stream, &substream); return status; }