So I'm trying to serialize a struct containing a byte array and its size:
struct Msg {
unsigned char* payload;
size_t payload_size;
};
the following code uses an instance of this struct called msg
.
DiagResp resp = DIAG_RESP__INIT;
ProtobufCBinaryData payload;
payload.data = msg->payload;
payload.len = msg->payload_size;
resp.payload = &payload;
resp.n_payload = msg->payload_size;
resp.has_payload_size = 1;
resp.payload_size = msg->payload_size;
size_t packed_size = diag_resp__get_packed_size(&resp);
printf("packed_size=%lu, msg->received_bytes=%lu\n",
packed_size, msg->received_bytes);
*ret_payload = malloc(packed_size);
diag_resp__pack(&resp, *ret_payload);
this is from the .proto
file:
message DiagResp {
...
optional int32 payload_size = 10;
repeated bytes payload = 11;
}
The printf
call prints out this:
packed_size=4256922, msg->received_bytes=4
...but if I don't attempt to serialize the payload
field, everything works as expected. So how do I serialize a repeated bytes
field? It is not covered in the protobuf-c
documentation...