opyeb protobuflab

protobuflab · Wire

Reading the Protobuf wire format

Tags, varints, ZigZag, wire types 0, 1, 2 and 5, and how packed repeated fields look in bytes.

Protobuf does not send field names. It sends a tag that combines the field number and the wire type, followed by the value. That is why a schema-free reader can recover much of the structure, and why a mismatched schema silently misreads values.

tag = (field number << 3) | wire type

The tag is itself a varint. Field 1 with wire type 2 is (1 << 3) | 2 = 10, or 0x0a. Field 6 as a varint is (6 << 3) | 0 = 48, or 0x30.

Wire typeMeaningBytes for the value
0varint (int32, int64, uint, bool, enum, sint)variable
164-bit (fixed64, sfixed64, double)8
2length-delimited (string, bytes, message, packed)length varint + body
532-bit (fixed32, sfixed32, float)4

Varint and ZigZag

A varint splits the value into seven-bit groups and sets the high bit while more groups follow. 300 is 0xac 0x02. A negative number written as a plain varint takes ten bytes, so sint32 and sint64 fold the sign with ZigZag: -3 becomes 5 and 150 becomes 300.

Candidates inside a length-delimited value

Wire type 2 carries a length and then arbitrary bytes. Those bytes may be UTF-8 text, another message or a packed integer array, and the bytes alone cannot decide. This tool computes all three and shows them side by side.

Packed repeated fields

In proto3 a repeated integer field is packed by default and arrives as wire type 2. repeated sint32 samples = 3; holding [-2, 0, 5, 300] becomes 1a 05 03 00 0a d8 04. With a schema the values are unpacked into an array; without one the field is shown as a packed candidate.

Values can exceed 64 bits of safe JavaScript integers, so the tool prints large integers as strings, in the JSON preview as well, to keep the exact digits.

Questions

Can I read values without field numbers?

No. A value alone does not say which field it belongs to, although the tag in front gives the number and wire type so boundaries can be recovered.

Can double and fixed64 be told apart?

Not from bytes alone. The same eight bytes are read as both a double and an unsigned 64-bit integer and offered as candidates.

Why are groups not supported?

Groups are deprecated and use wire types 3 and 4. The tool reports a warning and stops interpreting after one.

The 46 bytes of the zigzag-nested preset hold two ZigZag values, a nested message, a double, a float and a string.

Open the tool

Written against the Protobuf language guide (proto2 and proto3) and the Protobuf encoding documentation. Every schema, byte string and name here is synthetic.