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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
const std = @import("std");
const testing = std.testing;
pub fn readULEB128(comptime T: type, in_stream: var) !T {
const ShiftT = @IntType(false, std.math.log2(T.bit_count));
var result: T = 0;
var shift: ShiftT = 0;
while (true) {
const byte = try in_stream.readByte();
var operand: T = undefined;
if (@shlWithOverflow(T, byte & 0x7f, shift, &operand))
return error.Overflow;
result |= operand;
if (@addWithOverflow(ShiftT, shift, 7, &shift))
return error.Overflow;
if ((byte & 0x80) == 0)
return result;
}
}
pub fn readULEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
const ShiftT = @IntType(false, std.math.log2(T.bit_count));
var result: T = 0;
var shift: ShiftT = 0;
var i: usize = 0;
while (true) {
const byte = ptr.*[i];
i += 1;
var operand: T = undefined;
if (@shlWithOverflow(T, byte & 0x7f, shift, &operand))
return error.Overflow;
result |= operand;
if (@addWithOverflow(ShiftT, shift, 7, &shift))
return error.Overflow;
if ((byte & 0x80) == 0) {
ptr.* += i;
return result;
}
}
}
pub fn readILEB128(comptime T: type, in_stream: var) !T {
const ShiftT = @IntType(false, std.math.log2(T.bit_count));
var result: T = 0;
var shift: ShiftT = 0;
while (true) {
const byte = u8(try in_stream.readByte());
var operand: T = undefined;
if (@shlWithOverflow(T, @intCast(T, byte & 0x7f), shift, &operand))
return error.Overflow;
result |= operand;
if (@addWithOverflow(ShiftT, shift, 7, &shift))
return error.Overflow;
if ((byte & 0x80) == 0) {
if (shift <= ShiftT(T.bit_count - 1) and (byte & 0x40) != 0) {
result |= T(-1) << shift;
}
return result;
}
}
}
pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
const ShiftT = @IntType(false, std.math.log2(T.bit_count));
var result: T = 0;
var shift: ShiftT = 0;
var i: usize = 0;
while (true) {
const byte = ptr.*[i];
i += 1;
var operand: T = undefined;
if (@shlWithOverflow(T, @intCast(T, byte & 0x7f), shift, &operand))
return error.Overflow;
result |= operand;
if (@addWithOverflow(ShiftT, shift, 7, &shift))
return error.Overflow;
if ((byte & 0x80) == 0) {
if (shift <= ShiftT(T.bit_count - 1) and (byte & 0x40) != 0) {
result |= T(-1) << shift;
}
ptr.* += i;
return result;
}
}
}
const OneByteReadInStream = struct {
const Error = error{NoError};
const Stream = std.io.InStream(Error);
stream: Stream,
str: []const u8,
curr: usize,
fn init(str: []const u8) @This() {
return @This(){
.stream = Stream{ .readFn = readFn },
.str = str,
.curr = 0,
};
}
fn readFn(in_stream: *Stream, dest: []u8) Error!usize {
const self = @fieldParentPtr(@This(), "stream", in_stream);
if (self.str.len <= self.curr or dest.len == 0)
return 0;
dest[0] = self.str[self.curr];
self.curr += 1;
return 1;
}
};
fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
var in_stream = OneByteReadInStream.init(encoded);
const v1 = try readILEB128(T, &in_stream.stream);
var in_ptr = encoded.ptr;
const v2 = try readILEB128Mem(T, &in_ptr);
testing.expectEqual(v1, v2);
return v2;
}
fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
var in_stream = OneByteReadInStream.init(encoded);
const v1 = try readULEB128(T, &in_stream.stream);
var in_ptr = encoded.ptr;
const v2 = try readULEB128Mem(T, &in_ptr);
return v2;
}
test "deserialize signed LEB128" {
// Truncated
testing.expectError(error.EndOfStream, test_read_ileb128(i64, "\x80"));
// Overflow
testing.expectError(error.Overflow, test_read_ileb128(i8, "\x80\x80\x40"));
testing.expectError(error.Overflow, test_read_ileb128(i16, "\x80\x80\x80\x40"));
testing.expectError(error.Overflow, test_read_ileb128(i32, "\x80\x80\x80\x80\x40"));
testing.expectError(error.Overflow, test_read_ileb128(i64, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x40"));
// Decode SLEB128
testing.expect((try test_read_ileb128(i64, "\x00")) == 0);
testing.expect((try test_read_ileb128(i64, "\x01")) == 1);
testing.expect((try test_read_ileb128(i64, "\x3f")) == 63);
testing.expect((try test_read_ileb128(i64, "\x40")) == -64);
testing.expect((try test_read_ileb128(i64, "\x41")) == -63);
testing.expect((try test_read_ileb128(i64, "\x7f")) == -1);
testing.expect((try test_read_ileb128(i64, "\x80\x01")) == 128);
testing.expect((try test_read_ileb128(i64, "\x81\x01")) == 129);
testing.expect((try test_read_ileb128(i64, "\xff\x7e")) == -129);
testing.expect((try test_read_ileb128(i64, "\x80\x7f")) == -128);
testing.expect((try test_read_ileb128(i64, "\x81\x7f")) == -127);
testing.expect((try test_read_ileb128(i64, "\xc0\x00")) == 64);
testing.expect((try test_read_ileb128(i64, "\xc7\x9f\x7f")) == -12345);
// Decode unnormalized SLEB128 with extra padding bytes.
testing.expect((try test_read_ileb128(i64, "\x80\x00")) == 0);
testing.expect((try test_read_ileb128(i64, "\x80\x80\x00")) == 0);
testing.expect((try test_read_ileb128(i64, "\xff\x00")) == 0x7f);
testing.expect((try test_read_ileb128(i64, "\xff\x80\x00")) == 0x7f);
testing.expect((try test_read_ileb128(i64, "\x80\x81\x00")) == 0x80);
testing.expect((try test_read_ileb128(i64, "\x80\x81\x80\x00")) == 0x80);
}
test "deserialize unsigned LEB128" {
// Truncated
testing.expectError(error.EndOfStream, test_read_uleb128(u64, "\x80"));
// Overflow
testing.expectError(error.Overflow, test_read_uleb128(u8, "\x80\x80\x40"));
testing.expectError(error.Overflow, test_read_uleb128(u16, "\x80\x80\x80\x40"));
testing.expectError(error.Overflow, test_read_uleb128(u32, "\x80\x80\x80\x80\x40"));
testing.expectError(error.Overflow, test_read_uleb128(u64, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x40"));
// Decode ULEB128
testing.expect((try test_read_uleb128(u64, "\x00")) == 0);
testing.expect((try test_read_uleb128(u64, "\x01")) == 1);
testing.expect((try test_read_uleb128(u64, "\x3f")) == 63);
testing.expect((try test_read_uleb128(u64, "\x40")) == 64);
testing.expect((try test_read_uleb128(u64, "\x7f")) == 0x7f);
testing.expect((try test_read_uleb128(u64, "\x80\x01")) == 0x80);
testing.expect((try test_read_uleb128(u64, "\x81\x01")) == 0x81);
testing.expect((try test_read_uleb128(u64, "\x90\x01")) == 0x90);
testing.expect((try test_read_uleb128(u64, "\xff\x01")) == 0xff);
testing.expect((try test_read_uleb128(u64, "\x80\x02")) == 0x100);
testing.expect((try test_read_uleb128(u64, "\x81\x02")) == 0x101);
testing.expect((try test_read_uleb128(u64, "\x80\xc1\x80\x80\x10")) == 4294975616);
// Decode ULEB128 with extra padding bytes
testing.expect((try test_read_uleb128(u64, "\x80\x00")) == 0);
testing.expect((try test_read_uleb128(u64, "\x80\x80\x00")) == 0);
testing.expect((try test_read_uleb128(u64, "\xff\x00")) == 0x7f);
testing.expect((try test_read_uleb128(u64, "\xff\x80\x00")) == 0x7f);
testing.expect((try test_read_uleb128(u64, "\x80\x81\x00")) == 0x80);
testing.expect((try test_read_uleb128(u64, "\x80\x81\x80\x00")) == 0x80);
}
|