Class Protobuf
ProtoMessage m = Protobuf.decode(bytes); // fields by number, types guessed
String yaml = m.render(); // "1: 150 # !sint: 75\n2: hello\n"
Map<Object, Object> plain = m.toPlain(); // {1=150, 2=hello}
plain.put(2, "goodbye");
byte[] changed = Protobuf.encode(plain);
Without a schema, varints, fixed-width values and groups are what the wire says; whether a
length-delimited value is text, a nested message or bytes is guessed, as mitmproxy guesses it
(see ProtoValue). With a ProtoSchema, declared fields are read as declared, with
names, and the rest is guessed.
Decoding never throws anything but DecodeException for bad input, whatever the bytes:
nesting is bounded (64 levels; deeper length-delimited values stay
bytes), lengths are checked against the input, and nothing is allocated before it is known to
fit. Encoding a decoded message gives back the same bytes, except where the input used
non-minimal varints at the top level (nested messages are only recognized with minimal ones).
Plain values (toPlain(org.microproxy.contentviews.ProtoMessage)) re-encode to the same bytes when each field's occurrences were
together on the wire, as encoders write them.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intHow deep messages and groups may nest before length-delimited values are left as bytes.static final intThe largest field number protobuf allows, 229-1. -
Method Summary
Modifier and TypeMethodDescriptionstatic ProtoMessagedecode(byte[] data) Decodes a message without a schema.static ProtoMessagedecode(byte[] data, ProtoSchema schema, String messageType) Decodes a message asmessageTypeofschema; fields the type does not declare, or whose values do not match their declared type, are decoded without it.static byte[]Encodes plain values, astoPlain(org.microproxy.contentviews.ProtoMessage)makes them, without a schema.static byte[]encode(Map<?, ?> fields, ProtoSchema schema, String messageType) Encodes plain values asmessageType: keys may be field names, and values are encoded as their fields are declared (enum values may be names,sintfields are zigzag-encoded, packed fields packed, map fields from maps).static byte[]encode(ProtoMessage message) Encodes a message, field by field in its order.static byte[]packVarints(List<? extends Number> values) Packs integers as varints, as a packed repeated integer field holds them (negative numbers take ten bytes, as forint32andint64).static Stringrender(byte[] data) Decodes a message without a schema and renders it as YAML with field numbers; seeProtoMessage.render().toPlain(ProtoMessage message) A message as plain Java values, for scripts and quick edits.unpackVarints(byte[] data) Reads bytes as packed varints, as a packed repeated integer field holds them.static longzigzagDecode(long n) Decodes a zigzag-encoded 64-bit value: 0, 1, 2, 3 become 0, -1, 1, -2.static intzigzagDecode32(int n) Decodes a zigzag-encoded 32-bit value.static longzigzagEncode(long n) Zigzag-encodes a signed 64-bit value, assint64fields are: 0, -1, 1, -2 become 0, 1, 2, 3.static intzigzagEncode32(int n) Zigzag-encodes a signed 32-bit value, assint32fields are.
-
Field Details
-
MAX_FIELD_NUMBER
public static final int MAX_FIELD_NUMBERThe largest field number protobuf allows, 229-1.- See Also:
-
DEFAULT_MAX_DEPTH
public static final int DEFAULT_MAX_DEPTHHow deep messages and groups may nest before length-delimited values are left as bytes.- See Also:
-
-
Method Details
-
decode
Decodes a message without a schema.- Parameters:
data- the serialized message- Returns:
- the message, with every field keyed by number
- Throws:
DecodeException- ifdatais not a protobuf message
-
decode
public static ProtoMessage decode(byte[] data, ProtoSchema schema, String messageType) throws DecodeException Decodes a message asmessageTypeofschema; fields the type does not declare, or whose values do not match their declared type, are decoded without it.- Parameters:
data- the serialized messageschema- the schema, ornullto decode without onemessageType- the full name of the message's type, ornullto decode without one- Returns:
- the message
- Throws:
DecodeException- ifdatais not a protobuf messageIllegalArgumentException- if the schema has no typemessageType
-
render
Decodes a message without a schema and renders it as YAML with field numbers; seeProtoMessage.render().- Parameters:
data- the serialized message- Returns:
- the text, or
{} # empty protobuf messagefor no bytes - Throws:
DecodeException- ifdatais not a protobuf message
-
encode
Encodes a message, field by field in its order.- Parameters:
message- the message- Returns:
- the serialized message
-
encode
Encodes plain values, astoPlain(org.microproxy.contentviews.ProtoMessage)makes them, without a schema. Keys are field numbers (integers, or strings of digits). Values:- integers (
Integer,Long,BigIntegerup to 264-1) and booleans: varints; negative numbers take ten bytes, asint32andint64do; String: UTF-8;byte[]: bytes;Map: a nested message;List: a repeated field, one occurrence per element (not packed);Double: afixed64double;Float: afixed32float;- a
ProtoValue(such asProtoValue.Fixed32orProtoValue.Group): as it is;null: nothing.
- Parameters:
fields- the fields- Returns:
- the serialized message
- Throws:
IllegalArgumentException- if a key or value cannot be encoded
- integers (
-
encode
Encodes plain values asmessageType: keys may be field names, and values are encoded as their fields are declared (enum values may be names,sintfields are zigzag-encoded, packed fields packed, map fields from maps). Undeclared fields are encoded asencode(Map)does.- Parameters:
fields- the fieldsschema- the schemamessageType- the full name of the message's type- Returns:
- the serialized message
- Throws:
IllegalArgumentException- if the schema has no such type, or a key or value cannot be encoded
-
toPlain
A message as plain Java values, for scripts and quick edits. Keys are field numbers (Integer), or names for declared fields; a field that occurs more than once, or is declared repeated, is aList. Values:- without a declaration: varints as
Long(negative when the top bit is set), text asString, bytes asbyte[], nested messages asMap; fixed-width values and groups stayProtoValue.Fixed32,ProtoValue.Fixed64andProtoValue.Group, so that they encode back as they were; - with one, as declared:
Long(orBigIntegerforuint64andfixed64values aboveLong.MAX_VALUE),Boolean,Doubleforfloatanddouble, enum value names (or numbers, when unknown),String,byte[], andMaps for messages, groups and map fields.
- Parameters:
message- the message- Returns:
- a mutable, ordered map
- without a declaration: varints as
-
zigzagEncode
public static long zigzagEncode(long n) Zigzag-encodes a signed 64-bit value, assint64fields are: 0, -1, 1, -2 become 0, 1, 2, 3.- Parameters:
n- the signed value- Returns:
- the encoded value
-
zigzagDecode
public static long zigzagDecode(long n) Decodes a zigzag-encoded 64-bit value: 0, 1, 2, 3 become 0, -1, 1, -2.- Parameters:
n- the encoded value- Returns:
- the signed value
-
zigzagEncode32
public static int zigzagEncode32(int n) Zigzag-encodes a signed 32-bit value, assint32fields are.- Parameters:
n- the signed value- Returns:
- the encoded value, as 32 unsigned bits
-
zigzagDecode32
public static int zigzagDecode32(int n) Decodes a zigzag-encoded 32-bit value.- Parameters:
n- the encoded value- Returns:
- the signed value
-
unpackVarints
Reads bytes as packed varints, as a packed repeated integer field holds them.- Parameters:
data- the field's bytes- Returns:
- the values, as 64-bit varint values
- Throws:
DecodeException- if the bytes are not a sequence of varints
-
packVarints
Packs integers as varints, as a packed repeated integer field holds them (negative numbers take ten bytes, as forint32andint64).- Parameters:
values- the values- Returns:
- the field's bytes
-