authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2020-06-11 20:53:25+00:00
committergravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2020-06-16 16:20:59+00:00
loga0160d776f30fab70c786cd1159203a593af51b4
treed8bb3b1a7e7c7bfc6bb7e29c2cb1dbb9e25adaab
parent00ec81b0dc41e113624152cdb268d06f73e6f88c

Code cleanup, documentation added, read*Mem functions now take *[]const u8


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

lib/std/debug/leb128.zig+45-34
......@@ -1,11 +1,8 @@
11const std = @import("std");
22const testing = std.testing;
33
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
4///Read a single unsigned LEB128 value from the given reader as type T,
5/// or error.Overflow if the value cannot fit.
96pub fn readULEB128(comptime T: type, reader: var) !T {
107 const U = if (T.bit_count < 8) u8 else T;
118 const ShiftT = std.math.Log2Int(U);
......@@ -28,11 +25,14 @@ pub fn readULEB128(comptime T: type, reader: var) !T {
2825 }
2926
3027 //only applies in the case that we extended to u8
31 if (value > std.math.maxInt(T)) return error.Overflow;
28 if (U != T) {
29 if (value > std.math.maxInt(T)) return error.Overflow;
30 }
3231
3332 return @truncate(T, value);
3433}
3534
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,22 +50,27 @@ pub fn writeULEB128(writer: var, uint_value: var) !void {
5050 }
5151}
5252
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]);
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.*);
5657 const value = try readULEB128(T, buf.reader());
57 ptr.* += @intCast(usize, try buf.getPos());
58 ptr.*.ptr += buf.pos;
5859 return value;
5960}
6061
62///Write a single unsigned LEB128 integer to the given memory as unsigned LEB128,
63/// returning the number of bytes written.
6164pub fn writeULEB128Mem(ptr: []u8, uint_value: var) !usize {
6265 const T = @TypeOf(uint_value);
6366 const max_group = (T.bit_count + 6) / 7;
6467 var buf = std.io.fixedBufferStream(ptr);
6568 try writeULEB128(buf.writer(), uint_value);
66 return try buf.getPos();
69 return buf.pos;
6770}
6871
72///Read a single signed LEB128 value from the given reader as type T,
73/// or error.Overflow if the value cannot fit.
6974pub fn readILEB128(comptime T: type, reader: var) !T {
7075 const S = if (T.bit_count < 8) i8 else T;
7176 const U = std.meta.Int(false, S.bit_count);
......@@ -80,23 +85,24 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
8085 const byte = try reader.readByte();
8186 var temp = @as(U, byte & 0x7f);
8287
83 if (@shlWithOverflow(U, temp, group * 7, &temp)) {
88 const shift = group * 7;
89 if (@shlWithOverflow(U, temp, shift, &temp)) {
8490 //Overflow is ok so long as the sign bit is set and this is the last byte
8591 if (byte & 0x80 != 0) return error.Overflow;
8692 if (@bitCast(S, temp) >= 0) return error.Overflow;
8793
8894 //and all the overflowed bits are 1
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;
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;
9498 }
9599
96100 value |= temp;
97101 if (byte & 0x80 == 0) {
98 if (byte & 0x40 != 0 and group + 1 < max_group) {
99 value |= @bitCast(U, @as(S, -1)) << ((group + 1) * 7);
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);
100106 }
101107 break;
102108 }
......@@ -104,12 +110,16 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
104110 return error.Overflow;
105111 }
106112
113 const result = @bitCast(S, value);
107114 //Only applies if we extended to i8
108 if (@bitCast(S, value) > std.math.maxInt(T) or @bitCast(S, value) < std.math.minInt(T)) return error.Overflow;
115 if (S != T) {
116 if (result > std.math.maxInt(T) or result < std.math.minInt(T)) return error.Overflow;
117 }
109118
110 return @truncate(T, @bitCast(S, value));
119 return @truncate(T, result);
111120}
112121
122///Write a single signed integer as signed LEB128 to the given writer.
113123pub fn writeILEB128(writer: var, int_value: var) !void {
114124 const T = @TypeOf(int_value);
115125 const S = if (T.bit_count < 8) i8 else T;
......@@ -131,19 +141,22 @@ pub fn writeILEB128(writer: var, int_value: var) !void {
131141 }
132142}
133143
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]);
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.*);
137148 const value = try readILEB128(T, buf.reader());
138 ptr.* += @intCast(usize, try buf.getPos());
149 ptr.*.ptr += buf.pos;
139150 return value;
140151}
141152
153///Write a single signed LEB128 integer to the given memory as unsigned LEB128,
154/// returning the number of bytes written.
142155pub fn writeILEB128Mem(ptr: []u8, int_value: var) !usize {
143156 const T = @TypeOf(int_value);
144157 var buf = std.io.fixedBufferStream(ptr);
145158 try writeILEB128(buf.writer(), int_value);
146 return try buf.getPos();
159 return buf.pos;
147160}
148161
149162//tests
......@@ -160,7 +173,7 @@ fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T {
160173fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
161174 var reader = std.io.fixedBufferStream(encoded);
162175 const v1 = try readILEB128(T, reader.reader());
163 var in_ptr = encoded.ptr;
176 var in_ptr = encoded;
164177 const v2 = try readILEB128Mem(T, &in_ptr);
165178 testing.expectEqual(v1, v2);
166179 return v1;
......@@ -169,7 +182,7 @@ fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
169182fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
170183 var reader = std.io.fixedBufferStream(encoded);
171184 const v1 = try readULEB128(T, reader.reader());
172 var in_ptr = encoded.ptr;
185 var in_ptr = encoded;
173186 const v2 = try readULEB128Mem(T, &in_ptr);
174187 testing.expectEqual(v1, v2);
175188 return v1;
......@@ -177,7 +190,7 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
177190
178191fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {
179192 var reader = std.io.fixedBufferStream(encoded);
180 var in_ptr = encoded.ptr;
193 var in_ptr = encoded;
181194 var i: usize = 0;
182195 while (i < N) : (i += 1) {
183196 const v1 = try readILEB128(T, reader.reader());
......@@ -188,7 +201,7 @@ fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u
188201
189202fn test_read_uleb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {
190203 var reader = std.io.fixedBufferStream(encoded);
191 var in_ptr = encoded.ptr;
204 var in_ptr = encoded;
192205 var i: usize = 0;
193206 while (i < N) : (i += 1) {
194207 const v1 = try readULEB128(T, reader.reader());
......@@ -285,8 +298,6 @@ test "deserialize unsigned LEB128" {
285298fn test_write_leb128(value: var) !void {
286299 const T = @TypeOf(value);
287300
288 if (T.bit_count == 0) std.debug.warn("{}\n", .{@typeName(T)});
289
290301 const writeStream = if (T.is_signed) writeILEB128 else writeULEB128;
291302 const writeMem = if (T.is_signed) writeILEB128Mem else writeULEB128Mem;
292303 const readStream = if (T.is_signed) readILEB128 else readULEB128;
......@@ -324,13 +335,13 @@ fn test_write_leb128(value: var) !void {
324335
325336 //mem read
326337 var buf_ref: []u8 = buf[0..];
327 const mr = try readMem(T, &buf_ref.ptr);
338 const mr = try readMem(T, &buf_ref);
328339 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
329340 testing.expect(mr == value);
330341
331342 //bigger type mem read
332343 buf_ref = buf[0..];
333 const bmr = try readMem(T, &buf_ref.ptr);
344 const bmr = try readMem(T, &buf_ref);
334345 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
335346 testing.expect(bmr == value);
336347}