X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=pb_encode.c;h=d6ba7e34ef03a6cfee09506218c9984dd2d9b0a5;hb=696a01bf140e91661eae77663de99c78c95dcc73;hp=faaeac9db55e26f1b40c91f602124b4be151d82f;hpb=efef38cf7869f11750fba1dfb965e93b42d5d49e;p=apps%2Fagl-service-can-low-level.git diff --git a/pb_encode.c b/pb_encode.c index faaeac9d..d6ba7e34 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -6,16 +6,15 @@ #define NANOPB_INTERNALS #include "pb.h" #include "pb_encode.h" -#include -#ifdef __GNUC__ -/* Verify that we remember to check all return values for proper error propagation */ -#define checkreturn __attribute__((warn_unused_result)) +/* The warn_unused_result attribute appeared first in gcc-3.4.0 */ +#if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4) + #define checkreturn #else -#define checkreturn + /* Verify that we remember to check all return values for proper error propagation */ + #define checkreturn __attribute__((warn_unused_result)) #endif - typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn; /* --- Function pointers to field encoders --- @@ -29,7 +28,8 @@ static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = { &pb_enc_bytes, &pb_enc_string, - &pb_enc_submessage + &pb_enc_submessage, + NULL /* extensions */ }; /* pb_ostream_t implementation */ @@ -37,18 +37,28 @@ static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = { static bool checkreturn buf_write(pb_ostream_t *stream, const uint8_t *buf, size_t count) { uint8_t *dest = (uint8_t*)stream->state; - memcpy(dest, buf, count); stream->state = dest + count; + + while (count--) + *dest++ = *buf++; + return true; } pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize) { pb_ostream_t stream; +#ifdef PB_BUFFER_ONLY + stream.callback = (void*)1; /* Just some marker value */ +#else stream.callback = &buf_write; +#endif stream.state = buf; stream.max_size = bufsize; stream.bytes_written = 0; +#ifndef PB_NO_ERRMSG + stream.errmsg = NULL; +#endif return stream; } @@ -57,10 +67,15 @@ bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count if (stream->callback != NULL) { if (stream->bytes_written + count > stream->max_size) - return false; - + PB_RETURN_ERROR(stream, "stream full"); + +#ifdef PB_BUFFER_ONLY + if (!buf_write(stream, buf, count)) + PB_RETURN_ERROR(stream, "io error"); +#else if (!stream->callback(stream, buf, count)) - return false; + PB_RETURN_ERROR(stream, "io error"); +#endif } stream->bytes_written += count; @@ -69,10 +84,7 @@ bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count /* Main encoding stuff */ -/* Callbacks don't need this function because they usually know the data type - * without examining the field structure. - * Therefore it is static for now. - */ +/* 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) { @@ -83,6 +95,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie if (count == 0) return true; + /* 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)) @@ -98,8 +111,8 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie size = 8 * count; } else - { - pb_ostream_t sizestream = {0}; + { + pb_ostream_t sizestream = PB_OSTREAM_SIZING; p = pData; for (i = 0; i < count; i++) { @@ -110,7 +123,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie size = sizestream.bytes_written; } - if (!pb_encode_varint(stream, size)) + if (!pb_encode_varint(stream, (uint64_t)size)) return false; if (stream->callback == NULL) @@ -141,58 +154,155 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie return true; } +/* Encode a field with static allocation, i.e. one whose data is stored + * in the structure itself. */ +static bool checkreturn encode_static_field(pb_ostream_t *stream, + const pb_field_t *field, const void *pData) +{ + pb_encoder_t func; + const void *pSize; + bool dummy = true; + + func = PB_ENCODERS[PB_LTYPE(field->type)]; + + if (field->size_offset) + pSize = (const char*)pData + field->size_offset; + else + pSize = &dummy; + + switch (PB_HTYPE(field->type)) + { + case PB_HTYPE_REQUIRED: + 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; +} + +/* Encode a field with callback semantics. This means that a user function is + * called to provide and encode the actual data. */ +static bool checkreturn encode_callback_field(pb_ostream_t *stream, + const pb_field_t *field, const void *pData) +{ + const pb_callback_t *callback = (const pb_callback_t*)pData; + +#ifdef PB_OLD_CALLBACK_STYLE + const void *arg = callback->arg; +#else + void * const *arg = &(callback->arg); +#endif + + if (callback->funcs.encode != NULL) + { + if (!callback->funcs.encode(stream, field, arg)) + PB_RETURN_ERROR(stream, "callback error"); + } + return true; +} + +/* 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_handler(pb_ostream_t *stream, + const pb_extension_t *extension) +{ + const pb_field_t *field = (const pb_field_t*)extension->type->arg; + return encode_field(stream, field, extension->dest); +} + +/* Walk through all the registered extensions and give them a chance + * to encode themselves. */ +static bool checkreturn encode_extension_field(pb_ostream_t *stream, + const pb_field_t *field, const void *pData) +{ + const pb_extension_t *extension = *(const pb_extension_t* const *)pData; + UNUSED(field); + + while (extension) + { + bool status; + if (extension->type->encode) + status = extension->type->encode(stream, extension); + else + status = default_extension_handler(stream, extension); + + if (!status) + return false; + + extension = extension->next; + } + + return true; +} + 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; - const void *pSize; size_t prev_size = 0; while (field->tag != 0) { - pb_encoder_t func = PB_ENCODERS[PB_LTYPE(field->type)]; pData = (const char*)pData + prev_size + field->data_offset; - pSize = (const char*)pData + field->size_offset; - prev_size = field->data_size; - if (PB_HTYPE(field->type) == PB_HTYPE_ARRAY) + + /* 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; - - switch (PB_HTYPE(field->type)) + } + + if (PB_LTYPE(field->type) == PB_LTYPE_EXTENSION) { - case PB_HTYPE_REQUIRED: - if (!pb_encode_tag_for_field(stream, field)) - return false; - if (!func(stream, field, pData)) - return false; - break; - - case PB_HTYPE_OPTIONAL: - if (*(bool*)pSize) - { - if (!pb_encode_tag_for_field(stream, field)) - return false; - - if (!func(stream, field, pData)) - return false; - } - break; - - case PB_HTYPE_ARRAY: - if (!encode_array(stream, field, pData, *(size_t*)pSize, func)) - return false; - break; - - case PB_HTYPE_CALLBACK: - { - pb_callback_t *callback = (pb_callback_t*)pData; - if (callback->funcs.encode != NULL) - { - if (!callback->funcs.encode(stream, field, callback->arg)) - return false; - } - break; - } + /* 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++; @@ -201,11 +311,16 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons return true; } +bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) +{ + return pb_encode_submessage(stream, fields, src_struct); +} + /* Helper functions */ bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value) { uint8_t buffer[10]; - int i = 0; + size_t i = 0; if (value == 0) return pb_write(stream, (uint8_t*)&value, 1); @@ -225,9 +340,9 @@ bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value) { uint64_t zigzagged; if (value < 0) - zigzagged = ~(value << 1); + zigzagged = (uint64_t)(~(value << 1)); else - zigzagged = value << 1; + zigzagged = (uint64_t)(value << 1); return pb_encode_varint(stream, zigzagged); } @@ -243,7 +358,7 @@ bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value) lebytes[3] = bytes[0]; return pb_write(stream, lebytes, 4); #else - return pb_write(stream, (uint8_t*)value, 4); + return pb_write(stream, (const uint8_t*)value, 4); #endif } @@ -262,13 +377,13 @@ bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value) lebytes[7] = bytes[0]; return pb_write(stream, lebytes, 8); #else - return pb_write(stream, (uint8_t*)value, 8); + return pb_write(stream, (const uint8_t*)value, 8); #endif } -bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, int field_number) +bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number) { - int tag = wiretype | (field_number << 3); + uint64_t tag = wiretype | (field_number << 3); return pb_encode_varint(stream, tag); } @@ -297,7 +412,7 @@ bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t break; default: - return false; + PB_RETURN_ERROR(stream, "invalid field type"); } return pb_encode_tag(stream, wiretype, field->tag); @@ -305,7 +420,7 @@ bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size) { - if (!pb_encode_varint(stream, size)) + if (!pb_encode_varint(stream, (uint64_t)size)) return false; return pb_write(stream, buffer, size); @@ -314,7 +429,7 @@ bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, s bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) { /* First calculate the message size using a non-writing substream. */ - pb_ostream_t substream = {0}; + pb_ostream_t substream = PB_OSTREAM_SIZING; size_t size; bool status; @@ -323,14 +438,14 @@ bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fie size = substream.bytes_written; - if (!pb_encode_varint(stream, size)) + if (!pb_encode_varint(stream, (uint64_t)size)) return false; if (stream->callback == NULL) return pb_write(stream, NULL, size); /* Just sizing */ if (stream->bytes_written + size > stream->max_size) - return false; + PB_RETURN_ERROR(stream, "stream full"); /* Use a substream to verify that a callback doesn't write more than * what it did the first time. */ @@ -338,14 +453,20 @@ bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fie substream.state = stream->state; substream.max_size = size; substream.bytes_written = 0; +#ifndef PB_NO_ERRMSG + substream.errmsg = NULL; +#endif status = pb_encode(&substream, fields, src_struct); stream->bytes_written += substream.bytes_written; stream->state = substream.state; +#ifndef PB_NO_ERRMSG + stream->errmsg = substream.errmsg; +#endif if (substream.bytes_written != size) - return false; + PB_RETURN_ERROR(stream, "submsg size changed"); return status; } @@ -358,11 +479,11 @@ bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, co switch (field->data_size) { - case 1: value = *(uint8_t*)src; break; - case 2: value = *(uint16_t*)src; break; - case 4: value = *(uint32_t*)src; break; - case 8: value = *(uint64_t*)src; break; - default: return false; + case 1: value = *(const uint8_t*)src; break; + case 2: value = *(const uint16_t*)src; break; + case 4: value = *(const uint32_t*)src; break; + case 8: value = *(const uint64_t*)src; break; + default: PB_RETURN_ERROR(stream, "invalid data_size"); } return pb_encode_varint(stream, value); @@ -370,13 +491,13 @@ bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, co bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src) { - uint64_t value = 0; + int64_t value = 0; switch (field->data_size) { - case 4: value = *(int32_t*)src; break; - case 8: value = *(int64_t*)src; break; - default: return false; + case 4: value = *(const int32_t*)src; break; + case 8: value = *(const int64_t*)src; break; + default: PB_RETURN_ERROR(stream, "invalid data_size"); } return pb_encode_svarint(stream, value); @@ -396,22 +517,30 @@ bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, c bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src) { - pb_bytes_array_t *bytes = (pb_bytes_array_t*)src; + const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src; UNUSED(field); return pb_encode_string(stream, bytes->bytes, bytes->size); } bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src) { - UNUSED(field); - return pb_encode_string(stream, (uint8_t*)src, strlen((char*)src)); + /* strnlen() is not always available, so just use a for-loop */ + size_t size = 0; + const char *p = (const char*)src; + while (size < field->data_size && *p != '\0') + { + size++; + p++; + } + + return pb_encode_string(stream, (const uint8_t*)src, size); } bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src) { if (field->ptr == NULL) - return false; + PB_RETURN_ERROR(stream, "invalid field descriptor"); - return pb_encode_submessage(stream, (pb_field_t*)field->ptr, src); + return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src); }