Home > database >  google protobuf - how to access size of buffer as specified in .proto file?
google protobuf - how to access size of buffer as specified in .proto file?

Time:07-15

My protobuf file is

message Msg{
    // User Authentication data as bytes. 
    bytes MsgData = 1 [(nanopb).max_size = 2048];
}

When I generate the C API, the relevant parts are:

#define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; }

/* Struct definitions */
typedef PB_BYTES_ARRAY_T(2048) Msg_AuthenticationData_t;

/* Maximum encoded size of messages (where known) */
#define Msg_size  2051

QUESTION - 1

The size of the buffer, is 2048; how do I access that value using the API?

I know I can do sizeof(Msg_AuthenticationData_t.bytes) because it's set at 2048 at compile time but seems like there should be an API.

QUESTION - 2
And what is with the 2051? How is that usable?

CodePudding user response:

sizeof(Msg_AuthenticationData_t.bytes) is the API to get the size of the array. That's the way it works for any C type.

The encoded message size is the maximum size of the output of pb_encode(). Because of the field type and length headers in protobuf encoding, it can be a few bytes larger than the payload.

  • Related