Repository navigation
polar loop: file-transfer protocol + parsers #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,138 @@ | ||||||||||||
| // A tiny protobuf reader: just enough to walk the fields of a message whose | ||||||||||||
| // layout the caller already knows. No schema, no generated code, no | ||||||||||||
| // dependency. Every read that runs off the end or meets a wire type it does | ||||||||||||
| // not know returns null: a damaged file is refused, never half-decoded. | ||||||||||||
|
|
||||||||||||
| import 'dart:typed_data'; | ||||||||||||
|
|
||||||||||||
| /// One decoded field. | ||||||||||||
| class PbField { | ||||||||||||
| final int field; | ||||||||||||
|
|
||||||||||||
| /// 0 varint, 1 fixed64, 2 length-delimited, 5 fixed32. | ||||||||||||
| final int wire; | ||||||||||||
|
|
||||||||||||
| /// The value of a varint (wire 0), or the raw bits of a fixed field. | ||||||||||||
| final int value; | ||||||||||||
|
|
||||||||||||
| /// The payload of a length-delimited field (wire 2), else null. | ||||||||||||
| final List<int>? bytes; | ||||||||||||
| const PbField(this.field, this.wire, this.value, this.bytes); | ||||||||||||
|
|
||||||||||||
| /// The varint read as a signed 32-bit value (protobuf `int32`, which | ||||||||||||
| /// sign-extends a negative number to ten bytes). | ||||||||||||
| int get int32 => value.toSigned(32); | ||||||||||||
|
|
||||||||||||
| /// The varint read as `sint32` (zigzag). | ||||||||||||
| int get sint32 => (value >>> 1) ^ -(value & 1); | ||||||||||||
|
|
||||||||||||
| /// The varint read as a bool. | ||||||||||||
| bool get boolean => value != 0; | ||||||||||||
|
|
||||||||||||
| /// A fixed32 field read as a float. | ||||||||||||
| double get float32 => | ||||||||||||
| (ByteData(4)..setUint32(0, value, Endian.little)) | ||||||||||||
| .getFloat32(0, Endian.little); | ||||||||||||
|
|
||||||||||||
| /// The payload as a UTF-8 string. | ||||||||||||
| String get string => String.fromCharCodes(bytes ?? const <int>[]); | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decode the payload as UTF-8. For
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Every field of [b] in order, or null if [b] is not well-formed. | ||||||||||||
| List<PbField>? pbFields(List<int> b) { | ||||||||||||
| final out = <PbField>[]; | ||||||||||||
| var i = 0; | ||||||||||||
| while (i < b.length) { | ||||||||||||
| final tag = _varint(b, i); | ||||||||||||
| if (tag == null) return null; | ||||||||||||
| i = tag.$2; | ||||||||||||
| final field = tag.$1 >> 3; | ||||||||||||
| final wire = tag.$1 & 7; | ||||||||||||
| if (field == 0) return null; | ||||||||||||
| switch (wire) { | ||||||||||||
| case 0: | ||||||||||||
| final v = _varint(b, i); | ||||||||||||
| if (v == null) return null; | ||||||||||||
| out.add(PbField(field, 0, v.$1, null)); | ||||||||||||
| i = v.$2; | ||||||||||||
| case 1: | ||||||||||||
| if (i + 8 > b.length) return null; | ||||||||||||
| var v = 0; | ||||||||||||
| for (var k = 7; k >= 0; k--) { | ||||||||||||
| v = (v << 8) | b[i + k]; | ||||||||||||
| } | ||||||||||||
| out.add(PbField(field, 1, v, null)); | ||||||||||||
| i += 8; | ||||||||||||
| case 2: | ||||||||||||
| final len = _varint(b, i); | ||||||||||||
| if (len == null || len.$1 < 0 || len.$2 + len.$1 > b.length) { | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oversized length prefixes can make Polar parsers throw. In Suggested fix
Committable suggestion
|
||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
| out.add(PbField(field, 2, 0, b.sublist(len.$2, len.$2 + len.$1))); | ||||||||||||
| i = len.$2 + len.$1; | ||||||||||||
| case 5: | ||||||||||||
| if (i + 4 > b.length) return null; | ||||||||||||
| final v = b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24); | ||||||||||||
| out.add(PbField(field, 5, v, null)); | ||||||||||||
| i += 4; | ||||||||||||
| default: | ||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| return out; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Repeated varints, whether packed into one field or sent one per field. | ||||||||||||
| /// Null if a packed run is damaged. | ||||||||||||
| List<int>? pbVarints(Iterable<PbField> fields) { | ||||||||||||
| final out = <int>[]; | ||||||||||||
| for (final f in fields) { | ||||||||||||
| if (f.wire == 0) { | ||||||||||||
| out.add(f.value); | ||||||||||||
| } else if (f.wire == 2) { | ||||||||||||
| final b = f.bytes!; | ||||||||||||
| var i = 0; | ||||||||||||
| while (i < b.length) { | ||||||||||||
| final v = _varint(b, i); | ||||||||||||
| if (v == null) return null; | ||||||||||||
| out.add(v.$1); | ||||||||||||
| i = v.$2; | ||||||||||||
| } | ||||||||||||
| } else { | ||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| return out; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Repeated floats, packed or one per field. | ||||||||||||
| List<double>? pbFloats(Iterable<PbField> fields) { | ||||||||||||
| final out = <double>[]; | ||||||||||||
| for (final f in fields) { | ||||||||||||
| if (f.wire == 5) { | ||||||||||||
| out.add(f.float32); | ||||||||||||
| } else if (f.wire == 2) { | ||||||||||||
| final b = f.bytes!; | ||||||||||||
| if (b.length % 4 != 0) return null; | ||||||||||||
| final d = ByteData.sublistView(Uint8List.fromList(b)); | ||||||||||||
| for (var i = 0; i < b.length; i += 4) { | ||||||||||||
| out.add(d.getFloat32(i, Endian.little)); | ||||||||||||
| } | ||||||||||||
| } else { | ||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| return out; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// (value, next index). Reads up to 64 bits: a negative int32 is ten bytes. | ||||||||||||
| (int, int)? _varint(List<int> b, int i) { | ||||||||||||
| var v = 0; | ||||||||||||
| for (var shift = 0; shift < 64; shift += 7) { | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Overflowing varints are accepted When a ten-byte varint has payload bits beyond the low bit of its tenth byte, both varint readers accept the out-of-range value, so malformed scalar values, tags, or lengths can be returned as valid data and decoded incorrectly. Reject a tenth varint byte with payload bits beyond the one valid 64-bit bit. Also at |
||||||||||||
| if (i >= b.length) return null; | ||||||||||||
| final byte = b[i++]; | ||||||||||||
| v |= (byte & 0x7F) << shift; | ||||||||||||
| if (byte & 0x80 == 0) return (v, i); | ||||||||||||
|
Comment on lines
+134
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Malformed tenth varint bytes decode as a wrong value. The loop accepts a tenth varint byte above Reject a tenth byte above Suggested fix
Committable suggestion
|
||||||||||||
| } | ||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · UTF-8 strings become mojibake
When a caller reads a length-delimited string containing non-ASCII UTF-8,
PbField.stringmaps each byte directly to a character withString.fromCharCodes, so callers receive mojibake instead of the original text.Decode the field bytes as UTF-8 in
PbField.string.Also at
lib/src/pb_lite.dart:39.