From: Bernhard Krämer Date: Sun, 9 Oct 2016 18:27:28 +0000 (+0200) Subject: Prevent fields with default value from encoding when proto3 option is set X-Git-Tag: 5.0.2~186^2~57^2 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=ee44d0cee9fa87891fdc5371578f6ff3974a8d59;hp=ba97926cfd1e82bd294cf5e484633850d8e0b368;p=apps%2Fagl-service-can-low-level.git Prevent fields with default value from encoding when proto3 option is set --- diff --git a/pb_encode.c b/pb_encode.c index 4685614c..4f57fa5f 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -210,6 +210,23 @@ static bool checkreturn encode_basic_field(pb_ostream_t *stream, if (field->size_offset) pSize = (const char*)pData + field->size_offset; + else if (!field->size_offset && PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL) + { + /* In proto3 there are optional fields but no has_ flag, do not encode this fields + * when value is default or empty. */ + if(PB_LTYPE(field->type) == PB_LTYPE_BYTES){ + const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)pData; + if(bytes->size == 0) + implicit_has = false; + else if ((PB_LTYPE(field->type) == PB_LTYPE_STRING && *(const char*)pData == '\0') || + (field->data_size == sizeof(uint_least8_t) && *(const uint_least8_t*)pData == 0) || + (field->data_size == sizeof(uint_least16_t) && *(const uint_least16_t*)pData == 0) || + (field->data_size == sizeof(uint32_t) && *(const uint_least32_t*)pData == 0) || + (field->data_size == sizeof(uint64_t) && *(const uint_least64_t*)pData == 0)) + implicit_has = false; + } + pSize = &implicit_has; + } else pSize = &implicit_has;