authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2020-06-11 18:42:56+00:00
committergravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2020-06-16 16:20:58+00:00
loge94eba5df552abf63ee55846ac62b1e0f5a80098
tree3ed3a04950071666925397249077922af97ba3b3
parent7f24860737fba2b40c375b7c9fb37fe694bfa539

Overhauled leb128:

handles integers < 8 bits incorrect overflow bugs fixed simplified *mem implementations added wrte* functions added thurough write/read testing

1 files changed, 34 insertions(+), 45 deletions(-)

lib/std/debug/leb128.zig+34-45
......@@ -1,8 +1,11 @@
11const std = @import("std");
22const testing = std.testing;
33
4///Read a single unsigned LEB128 value from the given reader as type T,
5/// or error.Overflow if the value cannot fit.
4//@TODO: you can take *slice and alter slice.ptr
5// make sign bits check more efficient
6// add wrapper readLEB128 and write LEB128 that infer from type?
7// or use assertions?
8
69pub fn readULEB128(comptime T: type, reader: var) !T {
710 const U = if (T.bit_count < 8) u8 else T;
811 const ShiftT = std.math.Log2Int(U);
......@@ -25,14 +28,11 @@ pub fn readULEB128(comptime T: type, reader: var) !T {
2528 }
2629
2730 //only applies in the case that we extended to u8
28 if (U != T) {
29 if (value > std.math.maxInt(T)) return error.Overflow;
30 }
31 if (value > std.math.maxInt(T)) return error.Overflow;
3132
3233 return @truncate(T, value);
3334}
3435
35///Write a single unsigned integer as unsigned LEB128 to the given writer.
3636pub fn writeULEB128(writer: var, uint_value: var) !void {
3737 const T = @TypeOf(uint_value);
3838 const U = if (T.bit_count < 8) u8 else T;
......@@ -50,27 +50,22 @@ pub fn writeULEB128(writer: var, uint_value: var) !void {
5050 }
5151}
5252
53///Read a single unsinged integer from the given memory as type T.
54/// The provided slice reference will be updated to point to the byte after the last byte read.
55pub fn readULEB128Mem(comptime T: type, ptr: *[]const u8) !T {
56 var buf = std.io.fixedBufferStream(ptr.*);
53pub fn readULEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
54 const max_group = (T.bit_count + 6) / 7;
55 var buf = std.io.fixedBufferStream(ptr.*[0 .. max_group + 1]);
5756 const value = try readULEB128(T, buf.reader());
58 ptr.*.ptr += buf.pos;
57 ptr.* += @intCast(usize, try buf.getPos());
5958 return value;
6059}
6160
62///Write a single unsigned LEB128 integer to the given memory as unsigned LEB128,
63/// returning the number of bytes written.
6461pub fn writeULEB128Mem(ptr: []u8, uint_value: var) !usize {
6562 const T = @TypeOf(uint_value);
6663 const max_group = (T.bit_count + 6) / 7;
6764 var buf = std.io.fixedBufferStream(ptr);
6865 try writeULEB128(buf.writer(), uint_value);
69 return buf.pos;
66 return try buf.getPos();
7067}
7168
72///Read a single signed LEB128 value from the given reader as type T,
73/// or error.Overflow if the value cannot fit.
7469pub fn readILEB128(comptime T: type, reader: var) !T {
7570 const S = if (T.bit_count < 8) i8 else T;
7671 const U = std.meta.Int(false, S.bit_count);
......@@ -85,24 +80,23 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
8580 const byte = try reader.readByte();
8681 var temp = @as(U, byte & 0x7f);
8782
88 const shift = group * 7;
89 if (@shlWithOverflow(U, temp, shift, &temp)) {
83 if (@shlWithOverflow(U, temp, group * 7, &temp)) {
9084 //Overflow is ok so long as the sign bit is set and this is the last byte
9185 if (byte & 0x80 != 0) return error.Overflow;
9286 if (@bitCast(S, temp) >= 0) return error.Overflow;
9387
9488 //and all the overflowed bits are 1
95 const remaining_shift = @intCast(u3, U.bit_count - @as(u16, shift));
96 const remaining_bits = @bitCast(i8, byte | 0x80) >> remaining_shift;
97 if (remaining_bits != -1) return error.Overflow;
89 const check_bits_shift = @intCast(u3, U.bit_count - @as(u16, group * 7));
90 const check_bits_remaining = 7 - check_bits_shift;
91 const check_bits = byte >> check_bits_shift;
92 const num_consecutive_ones = @ctz(u8, ~check_bits);
93 if (num_consecutive_ones < check_bits_remaining) return error.Overflow;
9894 }
9995
10096 value |= temp;
10197 if (byte & 0x80 == 0) {
102 const needs_sign_ext = group + 1 < max_group;
103 if (byte & 0x40 != 0 and needs_sign_ext) {
104 const ones = @as(S, -1);
105 value |= @bitCast(U, ones) << (shift + 7);
98 if (byte & 0x40 != 0 and group + 1 < max_group) {
99 value |= @bitCast(U, @as(S, -1)) << ((group + 1) * 7);
106100 }
107101 break;
108102 }
......@@ -110,16 +104,12 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
110104 return error.Overflow;
111105 }
112106
113 const result = @bitCast(S, value);
114107 //Only applies if we extended to i8
115 if (S != T) {
116 if (result > std.math.maxInt(T) or result < std.math.minInt(T)) return error.Overflow;
117 }
108 if (@bitCast(S, value) > std.math.maxInt(T) or @bitCast(S, value) < std.math.minInt(T)) return error.Overflow;
118109
119 return @truncate(T, result);
110 return @truncate(T, @bitCast(S, value));
120111}
121112
122///Write a single signed integer as signed LEB128 to the given writer.
123113pub fn writeILEB128(writer: var, int_value: var) !void {
124114 const T = @TypeOf(int_value);
125115 const S = if (T.bit_count < 8) i8 else T;
......@@ -141,22 +131,19 @@ pub fn writeILEB128(writer: var, int_value: var) !void {
141131 }
142132}
143133
144///Read a single singed LEB128 integer from the given memory as type T.
145/// The provided slice reference will be updated to point to the byte after the last byte read.
146pub fn readILEB128Mem(comptime T: type, ptr: *[]const u8) !T {
147 var buf = std.io.fixedBufferStream(ptr.*);
134pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
135 const max_group = (T.bit_count + 6) / 7;
136 var buf = std.io.fixedBufferStream(ptr.*[0 .. max_group + 1]);
148137 const value = try readILEB128(T, buf.reader());
149 ptr.*.ptr += buf.pos;
138 ptr.* += @intCast(usize, try buf.getPos());
150139 return value;
151140}
152141
153///Write a single signed LEB128 integer to the given memory as unsigned LEB128,
154/// returning the number of bytes written.
155142pub fn writeILEB128Mem(ptr: []u8, int_value: var) !usize {
156143 const T = @TypeOf(int_value);
157144 var buf = std.io.fixedBufferStream(ptr);
158145 try writeILEB128(buf.writer(), int_value);
159 return buf.pos;
146 return try buf.getPos();
160147}
161148
162149//tests
......@@ -173,7 +160,7 @@ fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T {
173160fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
174161 var reader = std.io.fixedBufferStream(encoded);
175162 const v1 = try readILEB128(T, reader.reader());
176 var in_ptr = encoded;
163 var in_ptr = encoded.ptr;
177164 const v2 = try readILEB128Mem(T, &in_ptr);
178165 testing.expectEqual(v1, v2);
179166 return v1;
......@@ -182,7 +169,7 @@ fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
182169fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
183170 var reader = std.io.fixedBufferStream(encoded);
184171 const v1 = try readULEB128(T, reader.reader());
185 var in_ptr = encoded;
172 var in_ptr = encoded.ptr;
186173 const v2 = try readULEB128Mem(T, &in_ptr);
187174 testing.expectEqual(v1, v2);
188175 return v1;
......@@ -190,7 +177,7 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
190177
191178fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {
192179 var reader = std.io.fixedBufferStream(encoded);
193 var in_ptr = encoded;
180 var in_ptr = encoded.ptr;
194181 var i: usize = 0;
195182 while (i < N) : (i += 1) {
196183 const v1 = try readILEB128(T, reader.reader());
......@@ -201,7 +188,7 @@ fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u
201188
202189fn test_read_uleb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {
203190 var reader = std.io.fixedBufferStream(encoded);
204 var in_ptr = encoded;
191 var in_ptr = encoded.ptr;
205192 var i: usize = 0;
206193 while (i < N) : (i += 1) {
207194 const v1 = try readULEB128(T, reader.reader());
......@@ -298,6 +285,8 @@ test "deserialize unsigned LEB128" {
298285fn test_write_leb128(value: var) !void {
299286 const T = @TypeOf(value);
300287
288 if (T.bit_count == 0) std.debug.warn("{}\n", .{@typeName(T)});
289
301290 const writeStream = if (T.is_signed) writeILEB128 else writeULEB128;
302291 const writeMem = if (T.is_signed) writeILEB128Mem else writeULEB128Mem;
303292 const readStream = if (T.is_signed) readILEB128 else readULEB128;
......@@ -335,13 +324,13 @@ fn test_write_leb128(value: var) !void {
335324
336325 //mem read
337326 var buf_ref: []u8 = buf[0..];
338 const mr = try readMem(T, &buf_ref);
327 const mr = try readMem(T, &buf_ref.ptr);
339328 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
340329 testing.expect(mr == value);
341330
342331 //bigger type mem read
343332 buf_ref = buf[0..];
344 const bmr = try readMem(T, &buf_ref);
333 const bmr = try readMem(T, &buf_ref.ptr);
345334 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
346335 testing.expect(bmr == value);
347336}