diff options
Diffstat (limited to 'pb_encode.c')
-rw-r--r-- | pb_encode.c | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/pb_encode.c b/pb_encode.c index 9f91c9d5..4f57fa5f 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -49,7 +49,8 @@ static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = { &pb_enc_bytes, &pb_enc_string, &pb_enc_submessage, - NULL /* extensions */ + NULL, /* extensions */ + &pb_enc_bytes /* PB_LTYPE_FIXED_LENGTH_BYTES */ }; /******************************* @@ -209,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; @@ -498,6 +516,7 @@ bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t case PB_LTYPE_BYTES: case PB_LTYPE_STRING: case PB_LTYPE_SUBMESSAGE: + case PB_LTYPE_FIXED_LENGTH_BYTES: wiretype = PB_WT_STRING; break; @@ -636,11 +655,16 @@ static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *f 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; + const pb_bytes_array_t *bytes = NULL; + + if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES) + return pb_encode_string(stream, (const pb_byte_t*)src, field->data_size); + + bytes = (const pb_bytes_array_t*)src; if (src == NULL) { - /* Threat null pointer as an empty bytes field */ + /* Treat null pointer as an empty bytes field */ return pb_encode_string(stream, NULL, 0); } @@ -664,7 +688,7 @@ static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *fi if (src == NULL) { - size = 0; /* Threat null pointer as an empty string */ + size = 0; /* Treat null pointer as an empty string */ } else { |