Repository navigation
Expand file tree
/
Copy pathProtoBuf.zig
More file actions
164 lines (152 loc) · 6.03 KB
/
Copy pathProtoBuf.zig
File metadata and controls
164 lines (152 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! A minimal reader for the Protobuf wire format, without a schema:
//! https://protobuf.dev/programming-guides/encoding/
//!
//! Every read is bounds-checked, so truncated or corrupt data is an error, not a crash.
const ProtoBuf = @This();
const std = @import("std");
buf: []const u8,
cur: usize = 0,
pub const Error = error{
/// The data ends in the middle of a field.
EndOfData,
/// A varint longer than 10 bytes, the most a 64-bit value needs.
InvalidVarint,
/// Wire types 3 and 4 (deprecated groups), or 6 and 7, which don't exist.
UnsupportedWireType,
};
pub const Message = struct {
field_number: u61,
data: union(enum) {
VARINT: u64,
LEN: []const u8,
I64: u64,
I32: u32,
},
pub fn debugPrint(msg: Message) void {
switch (msg.data) {
.VARINT => |value| std.debug.print(
"{}:{s} {}\n",
.{ msg.field_number, @tagName(msg.data), value },
),
inline .I64, .I32 => |value| std.debug.print(
"{}:{s} 0x{x}\n",
.{ msg.field_number, @tagName(msg.data), value },
),
.LEN => |value| {
var hasNonAscii = false;
for (value) |c| if (c > 127) {
hasNonAscii = true;
};
if (hasNonAscii) {
std.debug.print(
"{}:{s} ({}) {x}\n",
.{ msg.field_number, @tagName(msg.data), value.len, value },
);
} else {
std.debug.print(
"{}:{s} ({}) \"{s}\"\n",
.{ msg.field_number, @tagName(msg.data), value.len, value },
);
}
},
}
}
};
/// The next field, or null at the end of the data.
pub fn next(self: *ProtoBuf) Error!?Message {
if (self.cur == self.buf.len) return null;
const tag = try self.parseVarInt();
// The low 3 bits are the wire type, the rest is the field number.
const field_number: u61 = @intCast(tag >> 3);
return .{
.field_number = field_number,
.data = switch (tag & 0b111) {
0 => .{ .VARINT = try self.parseVarInt() },
1 => .{ .I64 = std.mem.readInt(u64, (try self.take(8))[0..8], .little) },
2 => blk: {
const len = try self.parseVarInt();
// Checked before narrowing, so a huge length can't overflow `usize`.
if (len > self.buf.len - self.cur) return error.EndOfData;
break :blk .{ .LEN = try self.take(@intCast(len)) };
},
5 => .{ .I32 = std.mem.readInt(u32, (try self.take(4))[0..4], .little) },
else => return error.UnsupportedWireType,
},
};
}
pub fn parseVarInt(self: *ProtoBuf) Error!u64 {
const bytes = self.peek(10);
// Tags of fields 1 to 15 and values below 128 fit in one byte.
if (bytes.len > 0 and bytes[0] < 0x80) {
self.cur += 1;
return bytes[0];
}
var result: u64 = 0;
for (bytes, 0..) |byte, i| {
result |= @as(u64, byte & 0x7f) << @intCast(7 * i);
if (byte & 0x80 == 0) {
self.cur += i + 1;
return result;
}
}
return if (bytes.len < 10) error.EndOfData else error.InvalidVarint;
}
/// Up to `max` bytes at the current position, without moving past them.
/// With `take`, the only place that touches `buf` directly.
fn peek(self: *ProtoBuf, max: usize) []const u8 {
const rest = self.buf[self.cur..];
return rest[0..@min(rest.len, max)];
}
/// With `peek`, the only place that touches `buf` directly.
fn take(self: *ProtoBuf, len: usize) Error![]const u8 {
if (self.buf.len - self.cur < len) return error.EndOfData;
defer self.cur += len;
return self.buf[self.cur..][0..len];
}
test "117" {
var parser = ProtoBuf{ .buf = &.{117} };
try std.testing.expect(try parser.parseVarInt() == 117);
}
test "a 10-byte varint at the end of the data" {
var parser = ProtoBuf{ .buf = &.{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01 } };
try std.testing.expectEqual(std.math.maxInt(u64), try parser.parseVarInt());
try std.testing.expectEqual(10, parser.cur);
}
test "every wire type" {
// field 1 varint 300, field 2 bytes "hi", field 3 fixed64, field 4 fixed32.
var parser: ProtoBuf = .{ .buf = &.{
0x08, 0xac, 0x02, 0x12, 0x02, 'h', 'i', 0x19, 1, 0, 0, 0, 0, 0, 0, 0,
0x25, 2, 0, 0, 0,
} };
try std.testing.expectEqual(300, (try parser.next()).?.data.VARINT);
try std.testing.expectEqualStrings("hi", (try parser.next()).?.data.LEN);
try std.testing.expectEqual(1, (try parser.next()).?.data.I64);
const last = (try parser.next()).?;
try std.testing.expectEqual(4, last.field_number);
try std.testing.expectEqual(2, last.data.I32);
try std.testing.expectEqual(null, try parser.next());
}
test "truncated and corrupt data are errors" {
const cases = .{
// bytes field claiming 5 bytes with 2 left
.{ &[_]u8{ 0x12, 0x05, 'h', 'i' }, error.EndOfData },
// varint whose continuation bit runs off the end
.{ &[_]u8{ 0x08, 0x80 }, error.EndOfData },
// tag cut off mid-varint
.{ &[_]u8{0x80}, error.EndOfData },
// fixed64 and fixed32 with too few bytes
.{ &[_]u8{ 0x09, 1, 2, 3 }, error.EndOfData },
.{ &[_]u8{ 0x0d, 1, 2 }, error.EndOfData },
// a length so large it doesn't fit in memory
.{ &[_]u8{ 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01 }, error.EndOfData },
// 11-byte varint
.{ &[_]u8{ 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01 }, error.InvalidVarint },
// wire types 3 (group) and 7 (doesn't exist)
.{ &[_]u8{0x0b}, error.UnsupportedWireType },
.{ &[_]u8{0x0f}, error.UnsupportedWireType },
};
inline for (cases) |case| {
var parser: ProtoBuf = .{ .buf = case[0] };
try std.testing.expectError(case[1], parser.next());
}
}