How the Protobuf Wire Format Actually Works (Tags, Varints, the Four Wire Types)

You have a .bin file, you open it in a hex editor, and the first line is something like 080712026869 and you are staring at it wondering what it is. That is the protobuf wire format, and it is not mysterious, it is just very deliberate about what it leaves out. The single most important thing to understand first is that field names are not on the wire at all. The wire does not know your message has a field called name, or user_name, or whatever you called it in the .proto. It only knows a number, and that number is what every byte you are about to read hangs off of.

Every field starts with a tag, and the tag is a varint that packs two things into one value: your field number in the upper bits and a wire type in the lower three bits. The math is field number times 8, plus the wire type. Field 1 with wire type 0 is 8, which is 0x08. Field 2 with wire type 2 is 18, which is 0x12. Field 6 with wire type 2 is 50, which is 0x32, and that is the 0x32 sitting right before the 6869 when a string is involved. For field numbers below 16 the whole tag fits in one byte, and most small messages are nothing but single-byte tags with a little data after each one. When the field number reaches 16 or higher the tag itself starts using more than one byte, because the tag is a varint and varints grow with the value.

Wire type 0 is the varint, and a lot of your types ride on it: int32, int64, uint32, uint64, bool, and every enum, all of them. A 7 in field 1 is 0807, the tag and then one byte for the value. Varints take one byte per seven bits, so small numbers stay small and the encoding gets bigger as the value does. Here is the one that catches people: a negative int32 is not two bytes, it is ten. A -1 in an int32 field signs out as a full sixty-four-bit varint, so 08 ffffffff ffffffff 01, ten bytes of value for a single minus one. If you care about negative numbers, sint32 fixes it with a zigzag encoding, and the same -1 is 6001, one byte. That is not a rounding detail, that is a real choice in the schema, and a message that is mostly small negative int32s is ten times bigger than it needs to be.

There are only four wire types, and that is the whole system. Wire type 1 is fixed64, eight bytes with no length, used by double and fixed64. A 1.5 double is 59 000000000000f83f, the tag and then eight little-endian bytes that are the IEEE double itself. Wire type 5 is fixed32, the same idea with four bytes, for float and fixed32. Wire type 2 is the length-delimited one, and this is where your strings, your bytes, and your nested messages all live. The shape is tag, then a length varint, then exactly that many bytes. The string hi in field 6 is 32026869: tag 0x32, length 0x02, then 6869, which is hi in ASCII. A nested message uses the same shape, and the bytes after the length are a complete little wire stream of their own, so a field 8 holding a sub-message whose field 1 is set to 7 is 42020807, and the 0807 inside it is a message you can decode again on its own. That recursion is the entire nested-message model, one stream containing streams.

The reason there are only four wire types matters more than it looks at first. The wire type does not tell you the declared type, it tells you the family. int32, int64, uint32, uint64, bool, and enum are all wire type 0, and the wire cannot tell them apart, because they are the same thing at the byte level, a varint. That is exactly why a decoder with no schema can look at field 7 and tell you with confidence that it is a varint, and then stop, because whether that varint is a 32-bit integer, a 64-bit one, a boolean, or an enum is information the bytes do not carry. It lives in the .proto. The four wire types give you the shape, the schema gives you the name, and a raw decode is the shape without the name.

One more thing before you go back to the hex editor: repeated scalar fields can be written two ways. Packed writes one tag, one length, and all the values in a run, so a repeated int32 of 1, 2, 3 is 4a03010203, one tag and three bytes. Unpacked writes a tag in front of every value, 4801 4802 4803, three times the tags for the same three numbers. Both decode to the same thing, and which one your encoder picks is set by the syntax line, proto3 defaulting to packed and proto2 to unpacked, which is why a port between the two can change the size of every message that carries a list. The raw decode tab at proto.datajutsu.com does this post for you on real bytes: drop in a .bin with no schema and it lays out every tag, every field number, every wire type, and a best-effort value, and then the decode tab with your .proto tells you what each of those fields actually is. Read it alongside what is protobuf for the big picture and protobuf field numbers for the forensics side, and the hex editor stops being a wall of numbers.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe