opyeb protobuflab

protobuflab · Decoding

Reading bytes without a schema

Finding boundaries from tags, placing candidates side by side, and confirming them with 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

  1. Read the tag as a varint to get the field number and wire type.
  2. Wire type 0 reads a varint, 1 reads eight bytes and 5 reads four.
  3. Wire type 2 reads a length varint and slices that many bytes.
  4. 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 typeWire typeShown as
int32, int64, uint, sint, bool0Integer; sint is un-ZigZagged
enum0Label if known, otherwise the number
fixed32, sfixed32, float5Integer or floating point
fixed64, sfixed64, double1Integer or floating point
string2UTF-8 text
bytes2Base64
message2A nested tree
repeated integer (proto3)2A 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.

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.