| ... | ... | @@ -1,11 +1,8 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const testing = std.testing; |
| 3 | 3 | |
| 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. |
| 9 | 6 | pub fn readULEB128(comptime T: type, reader: var) !T { |
| 10 | 7 | const U = if (T.bit_count < 8) u8 else T; |
| 11 | 8 | const ShiftT = std.math.Log2Int(U); |
| ... | ... | @@ -28,11 +25,14 @@ pub fn readULEB128(comptime T: type, reader: var) !T { |
| 28 | 25 | } |
| 29 | 26 | |
| 30 | 27 | //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 | } |
| 32 | 31 | |
| 33 | 32 | return @truncate(T, value); |
| 34 | 33 | } |
| 35 | 34 | |
| 35 | ///Write a single unsigned integer as unsigned LEB128 to the given writer. |
| 36 | 36 | pub fn writeULEB128(writer: var, uint_value: var) !void { |
| 37 | 37 | const T = @TypeOf(uint_value); |
| 38 | 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 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | | pub 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. |
| 55 | pub fn readULEB128Mem(comptime T: type, ptr: *[]const u8) !T { |
| 56 | var buf = std.io.fixedBufferStream(ptr.*); |
| 56 | 57 | const value = try readULEB128(T, buf.reader()); |
| 57 | | ptr.* += @intCast(usize, try buf.getPos()); |
| 58 | ptr.*.ptr += buf.pos; |
| 58 | 59 | return value; |
| 59 | 60 | } |
| 60 | 61 | |
| 62 | ///Write a single unsigned LEB128 integer to the given memory as unsigned LEB128, |
| 63 | /// returning the number of bytes written. |
| 61 | 64 | pub fn writeULEB128Mem(ptr: []u8, uint_value: var) !usize { |
| 62 | 65 | const T = @TypeOf(uint_value); |
| 63 | 66 | const max_group = (T.bit_count + 6) / 7; |
| 64 | 67 | var buf = std.io.fixedBufferStream(ptr); |
| 65 | 68 | try writeULEB128(buf.writer(), uint_value); |
| 66 | | return try buf.getPos(); |
| 69 | return buf.pos; |
| 67 | 70 | } |
| 68 | 71 | |
| 72 | ///Read a single signed LEB128 value from the given reader as type T, |
| 73 | /// or error.Overflow if the value cannot fit. |
| 69 | 74 | pub fn readILEB128(comptime T: type, reader: var) !T { |
| 70 | 75 | const S = if (T.bit_count < 8) i8 else T; |
| 71 | 76 | const U = std.meta.Int(false, S.bit_count); |
| ... | ... | @@ -80,23 +85,24 @@ pub fn readILEB128(comptime T: type, reader: var) !T { |
| 80 | 85 | const byte = try reader.readByte(); |
| 81 | 86 | var temp = @as(U, byte & 0x7f); |
| 82 | 87 | |
| 83 | | if (@shlWithOverflow(U, temp, group * 7, &temp)) { |
| 88 | const shift = group * 7; |
| 89 | if (@shlWithOverflow(U, temp, shift, &temp)) { |
| 84 | 90 | //Overflow is ok so long as the sign bit is set and this is the last byte |
| 85 | 91 | if (byte & 0x80 != 0) return error.Overflow; |
| 86 | 92 | if (@bitCast(S, temp) >= 0) return error.Overflow; |
| 87 | 93 | |
| 88 | 94 | //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; |
| 94 | 98 | } |
| 95 | 99 | |
| 96 | 100 | value |= temp; |
| 97 | 101 | 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); |
| 100 | 106 | } |
| 101 | 107 | break; |
| 102 | 108 | } |
| ... | ... | @@ -104,12 +110,16 @@ pub fn readILEB128(comptime T: type, reader: var) !T { |
| 104 | 110 | return error.Overflow; |
| 105 | 111 | } |
| 106 | 112 | |
| 113 | const result = @bitCast(S, value); |
| 107 | 114 | //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 | } |
| 109 | 118 | |
| 110 | | return @truncate(T, @bitCast(S, value)); |
| 119 | return @truncate(T, result); |
| 111 | 120 | } |
| 112 | 121 | |
| 122 | ///Write a single signed integer as signed LEB128 to the given writer. |
| 113 | 123 | pub fn writeILEB128(writer: var, int_value: var) !void { |
| 114 | 124 | const T = @TypeOf(int_value); |
| 115 | 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 | 141 | } |
| 132 | 142 | } |
| 133 | 143 | |
| 134 | | pub 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. |
| 146 | pub fn readILEB128Mem(comptime T: type, ptr: *[]const u8) !T { |
| 147 | var buf = std.io.fixedBufferStream(ptr.*); |
| 137 | 148 | const value = try readILEB128(T, buf.reader()); |
| 138 | | ptr.* += @intCast(usize, try buf.getPos()); |
| 149 | ptr.*.ptr += buf.pos; |
| 139 | 150 | return value; |
| 140 | 151 | } |
| 141 | 152 | |
| 153 | ///Write a single signed LEB128 integer to the given memory as unsigned LEB128, |
| 154 | /// returning the number of bytes written. |
| 142 | 155 | pub fn writeILEB128Mem(ptr: []u8, int_value: var) !usize { |
| 143 | 156 | const T = @TypeOf(int_value); |
| 144 | 157 | var buf = std.io.fixedBufferStream(ptr); |
| 145 | 158 | try writeILEB128(buf.writer(), int_value); |
| 146 | | return try buf.getPos(); |
| 159 | return buf.pos; |
| 147 | 160 | } |
| 148 | 161 | |
| 149 | 162 | //tests |
| ... | ... | @@ -160,7 +173,7 @@ fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T { |
| 160 | 173 | fn test_read_ileb128(comptime T: type, encoded: []const u8) !T { |
| 161 | 174 | var reader = std.io.fixedBufferStream(encoded); |
| 162 | 175 | const v1 = try readILEB128(T, reader.reader()); |
| 163 | | var in_ptr = encoded.ptr; |
| 176 | var in_ptr = encoded; |
| 164 | 177 | const v2 = try readILEB128Mem(T, &in_ptr); |
| 165 | 178 | testing.expectEqual(v1, v2); |
| 166 | 179 | return v1; |
| ... | ... | @@ -169,7 +182,7 @@ fn test_read_ileb128(comptime T: type, encoded: []const u8) !T { |
| 169 | 182 | fn test_read_uleb128(comptime T: type, encoded: []const u8) !T { |
| 170 | 183 | var reader = std.io.fixedBufferStream(encoded); |
| 171 | 184 | const v1 = try readULEB128(T, reader.reader()); |
| 172 | | var in_ptr = encoded.ptr; |
| 185 | var in_ptr = encoded; |
| 173 | 186 | const v2 = try readULEB128Mem(T, &in_ptr); |
| 174 | 187 | testing.expectEqual(v1, v2); |
| 175 | 188 | return v1; |
| ... | ... | @@ -177,7 +190,7 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T { |
| 177 | 190 | |
| 178 | 191 | fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void { |
| 179 | 192 | var reader = std.io.fixedBufferStream(encoded); |
| 180 | | var in_ptr = encoded.ptr; |
| 193 | var in_ptr = encoded; |
| 181 | 194 | var i: usize = 0; |
| 182 | 195 | while (i < N) : (i += 1) { |
| 183 | 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 | 201 | |
| 189 | 202 | fn test_read_uleb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) !void { |
| 190 | 203 | var reader = std.io.fixedBufferStream(encoded); |
| 191 | | var in_ptr = encoded.ptr; |
| 204 | var in_ptr = encoded; |
| 192 | 205 | var i: usize = 0; |
| 193 | 206 | while (i < N) : (i += 1) { |
| 194 | 207 | const v1 = try readULEB128(T, reader.reader()); |
| ... | ... | @@ -285,8 +298,6 @@ test "deserialize unsigned LEB128" { |
| 285 | 298 | fn test_write_leb128(value: var) !void { |
| 286 | 299 | const T = @TypeOf(value); |
| 287 | 300 | |
| 288 | | if (T.bit_count == 0) std.debug.warn("{}\n", .{@typeName(T)}); |
| 289 | | |
| 290 | 301 | const writeStream = if (T.is_signed) writeILEB128 else writeULEB128; |
| 291 | 302 | const writeMem = if (T.is_signed) writeILEB128Mem else writeULEB128Mem; |
| 292 | 303 | const readStream = if (T.is_signed) readILEB128 else readULEB128; |
| ... | ... | @@ -324,13 +335,13 @@ fn test_write_leb128(value: var) !void { |
| 324 | 335 | |
| 325 | 336 | //mem read |
| 326 | 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 | 339 | testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos); |
| 329 | 340 | testing.expect(mr == value); |
| 330 | 341 | |
| 331 | 342 | //bigger type mem read |
| 332 | 343 | buf_ref = buf[0..]; |
| 333 | | const bmr = try readMem(T, &buf_ref.ptr); |
| 344 | const bmr = try readMem(T, &buf_ref); |
| 334 | 345 | testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos); |
| 335 | 346 | testing.expect(bmr == value); |
| 336 | 347 | } |