Developer

Reading Raw Protobuf Binary Without a .proto Schema: Wire Types, Field Numbers, and Limits

Walk through a nine-byte Person message by hand, identify ambiguous wire values, and compare the local inspection with a schema-backed JSON decode.

A service writes a nine-byte message to a queue. You have the bytes but the schema repository is on another team's machine. You can still check where each field starts, which field numbers occur, and how many bytes each value occupies. Those observations can answer a concrete debugging question such as “did the producer send field two?” They cannot tell you whether the team named that field age, status or retry_count. Keep the structural observations separate from any guesses you make about their meaning.

Walk one message from start to finish

Download the nine-byte Person binary. Open it in the on-device Protobuf inspector and compare the reported fields with the walkthrough below. Each field starts with a varint tag. The lowest three bits of the decoded tag give the wire type. Shifting the tag right by three bits gives the field number. Decode the entire tag as a varint before splitting those bits. Larger field numbers need more than one tag byte even when their value is very small.

Offset   Bytes              Read as
0        0A                 tag 10: field 1, wire type 2
1        03                 length 3 bytes
2        41 64 61           bytes spelling Ada in UTF-8
5        10                 tag 16: field 2, wire type 0
6        25                 varint 37
7        18                 tag 24: field 3, wire type 0
8        01                 varint 1
The first five bits of tag 0A give field number one and the low three bits select wire type two.
Splitting 00001010 into 00001 and 010 identifies field 1 and a length-delimited value. The schema supplies the field name and declared type.

The length of the first payload is a byte count. With this ASCII name the number of letters happens to equal the number of bytes. A name containing an accented letter or an emoji can occupy more bytes than visible characters. Starting at offset two, consume exactly three bytes and resume reading tags at offset five. A decoder that looks for a zero byte to terminate a string can run past the field, because the wire format uses the length and supplies no string terminator.

Use the wire type to advance, then stop interpreting

Wire typeHow to advancePossible declarations
0, varintRead seven payload bits at a time until the continuation bit clearsInteger, ZigZag integer, boolean or enum
1, fixed widthConsume eight bytes in little-endian orderfixed64, sfixed64 or double
2, length-delimitedRead a varint length, then consume that many bytesString, bytes, embedded message or packed values
5, fixed widthConsume four bytes in little-endian orderfixed32, sfixed32 or float

Try the separate three-byte example 08 96 01. The tag 08 selects field one with wire type zero. The low seven bits of 96 hexadecimal are 16 hexadecimal, or 22 decimal. The next byte contributes one group of 128, yielding 150. A uint32 declaration would report 150. A sint32 declaration would reverse ZigZag encoding and report 75. Reading the groups correctly establishes a raw value, while reporting a signed application value requires knowing which declaration the writer used.

Length-delimited fields are particularly ambiguous. The bytes for “Ada” form valid UTF-8, but a bytes field can hold those exact bytes without ever being declared a string. A run of small integers can also be a packed repeated field. Recursively attempting to parse every such payload as another message produces plausible-looking tags by accident. When comparing a pair of messages, record the length and a short hex excerpt. Label any proposed text or nested interpretation as a hypothesis until the schema confirms it.

Check that the input is one serialized payload

A Protobuf message has no universal magic number or built-in total length. A captured file can contain a transport header, a length prefix, compressed bytes or multiple messages. For example, gRPC frames carry a compression flag and a four-byte length before their message data. Confirm the framing with the producer or a documented export method before taking a slice of a capture. Randomly removing bytes until the parser accepts the remainder can yield a valid-looking but completely unrelated sequence of fields.

Concatenating two valid messages of the same type can also yield a parsable sequence of fields. Seeing field two twice is therefore insufficient evidence of corruption. It might be a repeated field, a repeated occurrence of a singular field or two records whose boundary was lost. Order in a serialized stream need not match the declaration order. For a regression comparison, use expected field numbers and known message boundaries. A difference in raw byte order alone does not establish a change in application meaning.

Confirm the interpretation with the writer's schema

Now download the matching Person schema. It declares package demo and a Person message containing a string name with field number one, a uint32 age with number two and a bool active with number three. That extra evidence turns 37 into an age and 1 into true. A different schema could decode exactly the same bytes successfully under different field names. Merely getting a JSON result without errors does not establish that you chose the intended message type.

The schema-aware Protobuf to JSON tool temporarily uploads the binary and schema when you run the decode. Choose demo.Person for this sample and compare the result with the expected JSON response. The response contains the message type and a data object with name Ada, age 37 and active true. Compare values and their JSON types. The text "37" and the number 37 are different JSON values even when a viewer renders them almost identically.

Limitations of schema-free inspection

The local inspector accepts ordinary varints, fixed-width values and bounded length-delimited fields. The historical start-group and end-group wire types are outside its supported set. It limits the number of fields and stops on invalid lengths or truncated values. A declared length extending past the available input can signal a partial queue dump, an incorrect starting offset or actual damage. Recover a complete payload from the producer before drawing conclusions about the missing field. Supplying a schema cannot recreate bytes absent from the capture.

Field names, comments, default handling, oneof membership, units and business rules are not transmitted as a schema alongside each message. An absent field might be optional, omitted at its default, or unknown to that writer. For a useful incident handoff, record the byte count, expected framing, observed field numbers and wire types, and the producer version. Ask for the message's fully qualified name and matching schema revision. Those details let the next engineer confirm your structural observations and resolve the remaining semantic questions.

References & further reading

Tools for this guide