Reading bytes without a schema
A Protobuf byte string carries no field names and no type names. The only clues are tags (field number plus wire type) and the length of each value. Schemaless decoding rebuilds boundaries from those clues and offers every plausible reading of each value.
Recovering boundaries
- Read the tag as a varint to get the field number and wire type.
- Wire type 0 reads a varint, 1 reads eight bytes and 5 reads four.
- Wire type 2 reads a length varint and slices that many bytes.
- Repeat the same steps inside the body to build nested candidates.
Why there are several candidates
A length-delimited body may be text, a message or a packed array. For example 0a 03 08 96 01 is field 1 with a length-delimited value whose body 08 96 01 reads as field 1 varint 150, and also as the text \x08\x96\x01. The tool shows both candidates; the schema decides which is right.
Decoding with a schema
With a root message selected, the field number finds the field in the schema and the value is read by its type. int32, int64, sint, bool and enum are varints, fixed32 and float are four bytes, fixed64 and double are eight, and string, bytes and message are length-delimited. Enum numbers gain their labels, and a map becomes an object of keys and values.
| Schema type | Wire type | Shown as |
|---|---|---|
| int32, int64, uint, sint, bool | 0 | Integer; sint is un-ZigZagged |
| enum | 0 | Label if known, otherwise the number |
| fixed32, sfixed32, float | 5 | Integer or floating point |
| fixed64, sfixed64, double | 1 | Integer or floating point |
| string | 2 | UTF-8 text |
| bytes | 2 | Base64 |
| message | 2 | A nested tree |
| repeated integer (proto3) | 2 | A packed array |
When the schema and the wire type disagree, decoding skips that field and leaves a warning. The schema may be older than the bytes, or the bytes may belong to another message, so read the warnings first.
Questions
Can a schemaless decode be wrong?
Numeric ranges are usually right, but signed versus unsigned, double versus fixed64 and text versus message cannot be told from bytes. The schema settles which candidate applies.
How are non-UTF-8 bytes shown?
They are dropped from the text candidate and shown as hex in the bytes candidate. A bytes field is shown as Base64.
What happens to damaged bytes?
If a length runs past the remaining bytes or a varint never ends, decoding stops there with a warning and later fields are not read.
With the root message empty in zigzag-nested you see candidates; with a root selected in order-wire you see schema-based decoding.
Written against the Protobuf language guide (proto2 and proto3) and the Protobuf encoding documentation. Every schema, byte string and name here is synthetic.