Protobuf Wire Format
Created on 2021-12-02T07:27:02-06:00
Encoded file is a concatenation of keys and value pairs.
Key indicates which field is written to and what type to read off the wire.
Value is the data on the wire.
Integers
Read bytes as long as the highest bit is set.
Clear flag bits from each byte read.
Reverse the bytes.
Bit shuffling to concatenate the 7-bit chunks together.
Wire types
0 Varint int32, int64, uint32, uint64, sint32, sint64, bool, enum 1 64-bit fixed64, sfixed64, double 2 Length-delimited string, bytes, embedded messages, packed repeated fields 3 Start group groups (deprecated) 4 End group groups (deprecated) 5 32-bit fixed32, sfixed32, float
Keys
An integer naming the field mixed with the wire type.
Encoded as varint((typecode << 3) | wire_type).
ZigZag encoding
Fixed size
4 bytes or 8 bytes of the type blitted in to the stream
Length-delimited
Integer holding size of payload followed by raw payload in bytes.
Optional
If a field is optional then it may not appear in binary at all.
Repeating
If you encounter a key in a repeating field, push the value to the array for that field.
If you encounter a key in a non-repeating field, overwrite the value for that field.
Packed
In 2.1.0 and beyond allow a packed format for varints, 32 and 64-bit fixed length.
Packed encoding is length of payload followed by each number in its wire format.