Pavel
Pavel

Reputation: 277

I can describe constant size message in protobuf?

For example, I have next message:

Message Header {
    fixed32 messageType;
    fixed32 messageSize;
}

I can be sure that on any platform, on any language, Header will be seralized in constant size byte array?

Upvotes: 2

Views: 542

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064004

Yes...ish, that should be predictable and fixed sized. The payload is fixed sized, and the header is varint encoded. There's a slight gotcha here is that the varint specification doesn't actually prohibit suboptimal encoding - it can in theory contain up to 9 extra bytes of zero padding with just the continuation bit set. But in practice this isn't a problem.

So: these are the same data interpreted as varint (value: 1):

Expected:

00000001

Suboptimal:

10000001 10000000 10000000 10000000 00000000

Very suboptimal:

10000001 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 00000000

(the MSB of each byte is the continuation flag; the other 7 bits are payload, with the least significant group first; the maximum size of a varint here should be 10 bytes since only 64 bits are expected, but I wonder if any libraries don't explicitly check this!)

In reality: libraries use the first form.

Upvotes: 2

Related Questions