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:58+00:00
log928d3ee9ea7f1a7eb17b1ec3a8ec36559e1e9968
treed8bb3b1a7e7c7bfc6bb7e29c2cb1dbb9e25adaab
parente94eba5df552abf63ee55846ac62b1e0f5a80098

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 @@...@@ -1,11 +1,8 @@
1const std = @import("std");1const std = @import("std");
2const testing = std.testing;2const testing = std.testing;
33
4//@TODO: you can take *slice and alter slice.ptr4///Read a single unsigned LEB128 value from the given reader as type T,
5// make sign bits check more efficient5/// or error.Overflow if the value cannot fit.
6// add wrapper readLEB128 and write LEB128 that infer from type?
7// or use assertions?
8
9pub fn readULEB128(comptime T: type, reader: var) !T {6pub fn readULEB128(comptime T: type, reader: var) !T {
10 const U = if (T.bit_count < 8) u8 else T;7 const U = if (T.bit_count < 8) u8 else T;
11 const ShiftT = std.math.Log2Int(U);8 const ShiftT = std.math.Log2Int(U);
...@@ -28,11 +25,14 @@ pub fn readULEB128(comptime T: type, reader: var) !T {...@@ -28,11 +25,14 @@ pub fn readULEB128(comptime T: type, reader: var) !T {
28 }25 }
2926
30 //only applies in the case that we extended to u827 //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
33 return @truncate(T, value);32 return @truncate(T, value);
34}33}
3534
35///Write a single unsigned integer as unsigned LEB128 to the given writer.
36pub fn writeULEB128(writer: var, uint_value: var) !void {36pub fn writeULEB128(writer: var, uint_value: var) !void {
37 const T = @TypeOf(uint_value);37 const T = @TypeOf(uint_value);
38 const U = if (T.bit_count < 8) u8 else T;38 const U = if (T.bit_count < 8) u8 else T;
...@@ -50,22 +50,27 @@ pub fn writeULEB128(writer: var, uint_value: var) !void {...@@ -50,22 +50,27 @@ pub fn writeULEB128(writer: var, uint_value: var) !void {
50 }50 }
51}51}
5252
53pub fn readULEB128Mem(comptime T: type, ptr: *[*]const u8) !T {53///Read a single unsinged integer from the given memory as type T.
54 const max_group = (T.bit_count + 6) / 7;54/// The provided slice reference will be updated to point to the byte after the last byte read.
55 var buf = std.io.fixedBufferStream(ptr.*[0 .. max_group + 1]);55pub fn readULEB128Mem(comptime T: type, ptr: *[]const u8) !T {
56 var buf = std.io.fixedBufferStream(ptr.*);
56 const value = try readULEB128(T, buf.reader());57 const value = try readULEB128(T, buf.reader());
57 ptr.* += @intCast(usize, try buf.getPos());58 ptr.*.ptr += buf.pos;
58 return value;59 return value;
59}60}
6061
62///Write a single unsigned LEB128 integer to the given memory as unsigned LEB128,
63/// returning the number of bytes written.
61pub fn writeULEB128Mem(ptr: []u8, uint_value: var) !usize {64pub fn writeULEB128Mem(ptr: []u8, uint_value: var) !usize {
62 const T = @TypeOf(uint_value);65 const T = @TypeOf(uint_value);
63 const max_group = (T.bit_count + 6) / 7;66 const max_group = (T.bit_count + 6) / 7;
64 var buf = std.io.fixedBufferStream(ptr);67 var buf = std.io.fixedBufferStream(ptr);
65 try writeULEB128(buf.writer(), uint_value);68 try writeULEB128(buf.writer(), uint_value);
66 return try buf.getPos();69 return buf.pos;
67}70}
6871
72///Read a single signed LEB128 value from the given reader as type T,
73/// or error.Overflow if the value cannot fit.
69pub fn readILEB128(comptime T: type, reader: var) !T {74pub fn readILEB128(comptime T: type, reader: var) !T {
70 const S = if (T.bit_count < 8) i8 else T;75 const S = if (T.bit_count < 8) i8 else T;
71 const U = std.meta.Int(false, S.bit_count);76 const U = std.meta.Int(false, S.bit_count);
...@@ -80,23 +85,24 @@ pub fn readILEB128(comptime T: type, reader: var) !T {...@@ -80,23 +85,24 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
80 const byte = try reader.readByte();85 const byte = try reader.readByte();
81 var temp = @as(U, byte & 0x7f);86 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)) {
84 //Overflow is ok so long as the sign bit is set and this is the last byte90 //Overflow is ok so long as the sign bit is set and this is the last byte
85 if (byte & 0x80 != 0) return error.Overflow;91 if (byte & 0x80 != 0) return error.Overflow;
86 if (@bitCast(S, temp) >= 0) return error.Overflow;92 if (@bitCast(S, temp) >= 0) return error.Overflow;
8793
88 //and all the overflowed bits are 194 //and all the overflowed bits are 1
89 const check_bits_shift = @intCast(u3, U.bit_count - @as(u16, group * 7));95 const remaining_shift = @intCast(u3, U.bit_count - @as(u16, shift));
90 const check_bits_remaining = 7 - check_bits_shift;96 const remaining_bits = @bitCast(i8, byte | 0x80) >> remaining_shift;
91 const check_bits = byte >> check_bits_shift;97 if (remaining_bits != -1) return error.Overflow;
92 const num_consecutive_ones = @ctz(u8, ~check_bits);
93 if (num_consecutive_ones < check_bits_remaining) return error.Overflow;
94 }98 }
9599
96 value |= temp;100 value |= temp;
97 if (byte & 0x80 == 0) {101 if (byte & 0x80 == 0) {
98 if (byte & 0x40 != 0 and group + 1 < max_group) {102 const needs_sign_ext = group + 1 < max_group;
99 value |= @bitCast(U, @as(S, -1)) << ((group + 1) * 7);103 if (byte & 0x40 != 0 and needs_sign_ext) {
104 const ones = @as(S, -1);
105 value |= @bitCast(U, ones) << (shift + 7);
100 }106 }
101 break;107 break;
102 }108 }
...@@ -104,12 +110,16 @@ pub fn readILEB128(comptime T: type, reader: var) !T {...@@ -104,12 +110,16 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
104 return error.Overflow;110 return error.Overflow;
105 }111 }
106112
113 const result = @bitCast(S, value);
107 //Only applies if we extended to i8114 //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);
111}120}
112121
122///Write a single signed integer as signed LEB128 to the given writer.
113pub fn writeILEB128(writer: var, int_value: var) !void {123pub fn writeILEB128(writer: var, int_value: var) !void {
114 const T = @TypeOf(int_value);124 const T = @TypeOf(int_value);
115 const S = if (T.bit_count < 8) i8 else T;125 const S = if (T.bit_count < 8) i8 else T;
...@@ -131,19 +141,22 @@ pub fn writeILEB128(writer: var, int_value: var) !void {...@@ -131,19 +141,22 @@ pub fn writeILEB128(writer: var, int_value: var) !void {
131 }141 }
132}142}
133143
134pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T {144///Read a single singed LEB128 integer from the given memory as type T.
135 const max_group = (T.bit_count + 6) / 7;145/// The provided slice reference will be updated to point to the byte after the last byte read.
136 var buf = std.io.fixedBufferStream(ptr.*[0 .. max_group + 1]);146pub fn readILEB128Mem(comptime T: type, ptr: *[]const u8) !T {
147 var buf = std.io.fixedBufferStream(ptr.*);
137 const value = try readILEB128(T, buf.reader());148 const value = try readILEB128(T, buf.reader());
138 ptr.* += @intCast(usize, try buf.getPos());149 ptr.*.ptr += buf.pos;
139 return value;150 return value;
140}151}
141152
153///Write a single signed LEB128 integer to the given memory as unsigned LEB128,
154/// returning the number of bytes written.
142pub fn writeILEB128Mem(ptr: []u8, int_value: var) !usize {155pub fn writeILEB128Mem(ptr: []u8, int_value: var) !usize {
143 const T = @TypeOf(int_value);156 const T = @TypeOf(int_value);
144 var buf = std.io.fixedBufferStream(ptr);157 var buf = std.io.fixedBufferStream(ptr);
145 try writeILEB128(buf.writer(), int_value);158 try writeILEB128(buf.writer(), int_value);
146 return try buf.getPos();159 return buf.pos;
147}160}
148161
149//tests162//tests
...@@ -160,7 +173,7 @@ fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T {...@@ -160,7 +173,7 @@ fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T {
160fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {173fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
161 var reader = std.io.fixedBufferStream(encoded);174 var reader = std.io.fixedBufferStream(encoded);
162 const v1 = try readILEB128(T, reader.reader());175 const v1 = try readILEB128(T, reader.reader());
163 var in_ptr = encoded.ptr;176 var in_ptr = encoded;
164 const v2 = try readILEB128Mem(T, &in_ptr);177 const v2 = try readILEB128Mem(T, &in_ptr);
165 testing.expectEqual(v1, v2);178 testing.expectEqual(v1, v2);
166 return v1;179 return v1;
...@@ -169,7 +182,7 @@ fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {...@@ -169,7 +182,7 @@ fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
169fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {182fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
170 var reader = std.io.fixedBufferStream(encoded);183 var reader = std.io.fixedBufferStream(encoded);
171 const v1 = try readULEB128(T, reader.reader());184 const v1 = try readULEB128(T, reader.reader());
172 var in_ptr = encoded.ptr;185 var in_ptr = encoded;
173 const v2 = try readULEB128Mem(T, &in_ptr);186 const v2 = try readULEB128Mem(T, &in_ptr);
174 testing.expectEqual(v1, v2);187 testing.expectEqual(v1, v2);
175 return v1;188 return v1;
...@@ -177,7 +190,7 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {...@@ -177,7 +190,7 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
177190
178fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {191fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {
179 var reader = std.io.fixedBufferStream(encoded);192 var reader = std.io.fixedBufferStream(encoded);
180 var in_ptr = encoded.ptr;193 var in_ptr = encoded;
181 var i: usize = 0;194 var i: usize = 0;
182 while (i < N) : (i += 1) {195 while (i < N) : (i += 1) {
183 const v1 = try readILEB128(T, reader.reader());196 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...@@ -188,7 +201,7 @@ fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u
188201
189fn test_read_uleb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {202fn test_read_uleb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void {
190 var reader = std.io.fixedBufferStream(encoded);203 var reader = std.io.fixedBufferStream(encoded);
191 var in_ptr = encoded.ptr;204 var in_ptr = encoded;
192 var i: usize = 0;205 var i: usize = 0;
193 while (i < N) : (i += 1) {206 while (i < N) : (i += 1) {
194 const v1 = try readULEB128(T, reader.reader());207 const v1 = try readULEB128(T, reader.reader());
...@@ -285,8 +298,6 @@ test "deserialize unsigned LEB128" {...@@ -285,8 +298,6 @@ test "deserialize unsigned LEB128" {
285fn test_write_leb128(value: var) !void {298fn test_write_leb128(value: var) !void {
286 const T = @TypeOf(value);299 const T = @TypeOf(value);
287300
288 if (T.bit_count == 0) std.debug.warn("{}\n", .{@typeName(T)});
289
290 const writeStream = if (T.is_signed) writeILEB128 else writeULEB128;301 const writeStream = if (T.is_signed) writeILEB128 else writeULEB128;
291 const writeMem = if (T.is_signed) writeILEB128Mem else writeULEB128Mem;302 const writeMem = if (T.is_signed) writeILEB128Mem else writeULEB128Mem;
292 const readStream = if (T.is_signed) readILEB128 else readULEB128;303 const readStream = if (T.is_signed) readILEB128 else readULEB128;
...@@ -324,13 +335,13 @@ fn test_write_leb128(value: var) !void {...@@ -324,13 +335,13 @@ fn test_write_leb128(value: var) !void {
324335
325 //mem read336 //mem read
326 var buf_ref: []u8 = buf[0..];337 var buf_ref: []u8 = buf[0..];
327 const mr = try readMem(T, &buf_ref.ptr);338 const mr = try readMem(T, &buf_ref);
328 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);339 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
329 testing.expect(mr == value);340 testing.expect(mr == value);
330341
331 //bigger type mem read342 //bigger type mem read
332 buf_ref = buf[0..];343 buf_ref = buf[0..];
333 const bmr = try readMem(T, &buf_ref.ptr);344 const bmr = try readMem(T, &buf_ref);
334 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);345 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
335 testing.expect(bmr == value);346 testing.expect(bmr == value);
336}347}