.PB
Protocol Buffers binary data
Introduction
Protocol Buffers is Google’s format for structured data, used inside Google since the
early 2000s and open source since 2008. The fields of each message are declared in a
.proto schema, and the protoc compiler turns the schema into
code that reads and writes the compact binary form.
A file holding one encoded message is commonly given .pb; the Protocol
Buffers documentation recommends .binpb. Many larger formats are
Protocol Buffers messages underneath, including ONNX
models.
Software
- protoc and the official libraries for C++, Java, Python, Go, C# and more
- Protoscope, which prints any message as readable text, with or without the schema
- Buf, a command-line tool for schemas and encoded messages
- Netron, for machine learning models stored as Protocol Buffers
Structure
An encoded message has no header and no magic number. It is a run of fields, and each field starts with a tag: the field number from the schema shifted left three bits, plus a three-bit wire type saying how the value is stored.
| Wire type | Name | Used for |
|---|---|---|
0 | VARINT | integers, booleans and enums |
1 | I64 | fixed 8-byte values: fixed64, double |
2 | LEN | a varint length, then that many bytes: strings, bytes, nested messages, packed lists |
3, 4 | SGROUP, EGROUP | start and end of a group, an old feature no longer used |
5 | I32 | fixed 4-byte values: fixed32, float |
Tags, lengths and integers are varints: seven bits per byte, least significant group first, with the top bit set on every byte except the last. Fixed-size values are little-endian. The field names are not stored, so the schema is needed to give the numbers meaning.