| ... | ... | @@ -5,22 +5,24 @@ pub fn readULEB128(comptime T: type, in_stream: var) !T { |
| 5 | 5 | const ShiftT = @IntType(false, std.math.log2(T.bit_count)); |
| 6 | 6 | |
| 7 | 7 | var result: T = 0; |
| 8 | | var shift: ShiftT = 0; |
| 8 | var shift: usize = 0; |
| 9 | 9 | |
| 10 | 10 | while (true) { |
| 11 | 11 | const byte = try in_stream.readByte(); |
| 12 | 12 | |
| 13 | if (shift > T.bit_count) |
| 14 | return error.Overflow; |
| 15 | |
| 13 | 16 | var operand: T = undefined; |
| 14 | | if (@shlWithOverflow(T, byte & 0x7f, shift, &operand)) |
| 17 | if (@shlWithOverflow(T, byte & 0x7f, @intCast(ShiftT, shift), &operand)) |
| 15 | 18 | return error.Overflow; |
| 16 | 19 | |
| 17 | 20 | result |= operand; |
| 18 | 21 | |
| 19 | | if (@addWithOverflow(ShiftT, shift, 7, &shift)) |
| 20 | | return error.Overflow; |
| 21 | | |
| 22 | 22 | if ((byte & 0x80) == 0) |
| 23 | 23 | return result; |
| 24 | |
| 25 | shift += 7; |
| 24 | 26 | } |
| 25 | 27 | } |
| 26 | 28 | |
| ... | ... | @@ -28,82 +30,92 @@ pub fn readULEB128Mem(comptime T: type, ptr: *[*]const u8) !T { |
| 28 | 30 | const ShiftT = @IntType(false, std.math.log2(T.bit_count)); |
| 29 | 31 | |
| 30 | 32 | var result: T = 0; |
| 31 | | var shift: ShiftT = 0; |
| 33 | var shift: usize = 0; |
| 32 | 34 | var i: usize = 0; |
| 33 | 35 | |
| 34 | | while (true) { |
| 36 | while (true) : (i += 1) { |
| 35 | 37 | const byte = ptr.*[i]; |
| 36 | | i += 1; |
| 38 | |
| 39 | if (shift > T.bit_count) |
| 40 | return error.Overflow; |
| 37 | 41 | |
| 38 | 42 | var operand: T = undefined; |
| 39 | | if (@shlWithOverflow(T, byte & 0x7f, shift, &operand)) |
| 43 | if (@shlWithOverflow(T, byte & 0x7f, @intCast(ShiftT, shift), &operand)) |
| 40 | 44 | return error.Overflow; |
| 41 | 45 | |
| 42 | 46 | result |= operand; |
| 43 | 47 | |
| 44 | | if (@addWithOverflow(ShiftT, shift, 7, &shift)) |
| 45 | | return error.Overflow; |
| 46 | | |
| 47 | 48 | if ((byte & 0x80) == 0) { |
| 48 | 49 | ptr.* += i; |
| 49 | 50 | return result; |
| 50 | 51 | } |
| 52 | |
| 53 | shift += 7; |
| 51 | 54 | } |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | pub fn readILEB128(comptime T: type, in_stream: var) !T { |
| 58 | const UT = @IntType(false, T.bit_count); |
| 55 | 59 | const ShiftT = @IntType(false, std.math.log2(T.bit_count)); |
| 56 | 60 | |
| 57 | | var result: T = 0; |
| 58 | | var shift: ShiftT = 0; |
| 61 | var result: UT = 0; |
| 62 | var shift: usize = 0; |
| 59 | 63 | |
| 60 | 64 | while (true) { |
| 61 | 65 | const byte = u8(try in_stream.readByte()); |
| 62 | 66 | |
| 63 | | var operand: T = undefined; |
| 64 | | if (@shlWithOverflow(T, @intCast(T, byte & 0x7f), shift, &operand)) |
| 67 | if (shift > T.bit_count) |
| 65 | 68 | return error.Overflow; |
| 66 | 69 | |
| 70 | var operand: UT = undefined; |
| 71 | if (@shlWithOverflow(UT, UT(byte & 0x7f), @intCast(ShiftT, shift), &operand)) { |
| 72 | if (byte != 0x7f) |
| 73 | return error.Overflow; |
| 74 | } |
| 75 | |
| 67 | 76 | result |= operand; |
| 68 | 77 | |
| 69 | | if (@addWithOverflow(ShiftT, shift, 7, &shift)) |
| 70 | | return error.Overflow; |
| 78 | shift += 7; |
| 71 | 79 | |
| 72 | 80 | if ((byte & 0x80) == 0) { |
| 73 | | if (shift <= ShiftT(T.bit_count - 1) and (byte & 0x40) != 0) { |
| 74 | | result |= T(-1) << shift; |
| 81 | if (shift < T.bit_count and (byte & 0x40) != 0) { |
| 82 | result |= @bitCast(UT, @intCast(T, -1)) << @intCast(ShiftT, shift); |
| 75 | 83 | } |
| 76 | | return result; |
| 84 | return @bitCast(T, result); |
| 77 | 85 | } |
| 78 | 86 | } |
| 79 | 87 | } |
| 80 | 88 | |
| 81 | 89 | pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T { |
| 90 | const UT = @IntType(false, T.bit_count); |
| 82 | 91 | const ShiftT = @IntType(false, std.math.log2(T.bit_count)); |
| 83 | 92 | |
| 84 | | var result: T = 0; |
| 85 | | var shift: ShiftT = 0; |
| 93 | var result: UT = 0; |
| 94 | var shift: usize = 0; |
| 86 | 95 | var i: usize = 0; |
| 87 | 96 | |
| 88 | | while (true) { |
| 97 | while (true) : (i += 1) { |
| 89 | 98 | const byte = ptr.*[i]; |
| 90 | | i += 1; |
| 91 | 99 | |
| 92 | | var operand: T = undefined; |
| 93 | | if (@shlWithOverflow(T, @intCast(T, byte & 0x7f), shift, &operand)) |
| 100 | if (shift > T.bit_count) |
| 94 | 101 | return error.Overflow; |
| 95 | 102 | |
| 103 | var operand: UT = undefined; |
| 104 | if (@shlWithOverflow(UT, UT(byte & 0x7f), @intCast(ShiftT, shift), &operand)) { |
| 105 | if (byte != 0x7f) |
| 106 | return error.Overflow; |
| 107 | } |
| 108 | |
| 96 | 109 | result |= operand; |
| 97 | 110 | |
| 98 | | if (@addWithOverflow(ShiftT, shift, 7, &shift)) |
| 99 | | return error.Overflow; |
| 111 | shift += 7; |
| 100 | 112 | |
| 101 | 113 | if ((byte & 0x80) == 0) { |
| 102 | | if (shift <= ShiftT(T.bit_count - 1) and (byte & 0x40) != 0) { |
| 103 | | result |= T(-1) << shift; |
| 114 | if (shift < T.bit_count and (byte & 0x40) != 0) { |
| 115 | result |= @bitCast(UT, @intCast(T, -1)) << @intCast(ShiftT, shift); |
| 104 | 116 | } |
| 105 | 117 | ptr.* += i; |
| 106 | | return result; |
| 118 | return @bitCast(T, result); |
| 107 | 119 | } |
| 108 | 120 | } |
| 109 | 121 | } |
| ... | ... | @@ -145,6 +157,7 @@ test "deserialize signed LEB128" { |
| 145 | 157 | testing.expectError(error.Overflow, test_read_ileb128(i16, "\x80\x80\x80\x40")); |
| 146 | 158 | testing.expectError(error.Overflow, test_read_ileb128(i32, "\x80\x80\x80\x80\x40")); |
| 147 | 159 | testing.expectError(error.Overflow, test_read_ileb128(i64, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x40")); |
| 160 | testing.expectError(error.Overflow, test_read_ileb128(i8, "\xff\x7e")); |
| 148 | 161 | |
| 149 | 162 | // Decode SLEB128 |
| 150 | 163 | testing.expect((try test_read_ileb128(i64, "\x00")) == 0); |
| ... | ... | @@ -160,6 +173,13 @@ test "deserialize signed LEB128" { |
| 160 | 173 | testing.expect((try test_read_ileb128(i64, "\x81\x7f")) == -127); |
| 161 | 174 | testing.expect((try test_read_ileb128(i64, "\xc0\x00")) == 64); |
| 162 | 175 | testing.expect((try test_read_ileb128(i64, "\xc7\x9f\x7f")) == -12345); |
| 176 | testing.expect((try test_read_ileb128(i8, "\xff\x7f")) == -1); |
| 177 | testing.expect((try test_read_ileb128(i16, "\xff\xff\x7f")) == -1); |
| 178 | testing.expect((try test_read_ileb128(i32, "\xff\xff\xff\xff\x7f")) == -1); |
| 179 | testing.expect((try test_read_ileb128(i32, "\x80\x80\x80\x80\x08")) == -0x80000000); |
| 180 | testing.expect((try test_read_ileb128(i64, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01")) == @bitCast(i64, @intCast(u64, 0x8000000000000000))); |
| 181 | testing.expect((try test_read_ileb128(i64, "\x80\x80\x80\x80\x80\x80\x80\x80\x40")) == -0x4000000000000000); |
| 182 | testing.expect((try test_read_ileb128(i64, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x7f")) == -0x8000000000000000); |
| 163 | 183 | |
| 164 | 184 | // Decode unnormalized SLEB128 with extra padding bytes. |
| 165 | 185 | testing.expect((try test_read_ileb128(i64, "\x80\x00")) == 0); |
| ... | ... | @@ -175,8 +195,11 @@ test "deserialize unsigned LEB128" { |
| 175 | 195 | testing.expectError(error.EndOfStream, test_read_stream_uleb128(u64, "\x80")); |
| 176 | 196 | |
| 177 | 197 | // Overflow |
| 198 | testing.expectError(error.Overflow, test_read_uleb128(u8, "\x80\x02")); |
| 178 | 199 | testing.expectError(error.Overflow, test_read_uleb128(u8, "\x80\x80\x40")); |
| 200 | testing.expectError(error.Overflow, test_read_uleb128(u16, "\x80\x80\x84")); |
| 179 | 201 | testing.expectError(error.Overflow, test_read_uleb128(u16, "\x80\x80\x80\x40")); |
| 202 | testing.expectError(error.Overflow, test_read_uleb128(u32, "\x80\x80\x80\x80\x90")); |
| 180 | 203 | testing.expectError(error.Overflow, test_read_uleb128(u32, "\x80\x80\x80\x80\x40")); |
| 181 | 204 | testing.expectError(error.Overflow, test_read_uleb128(u64, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x40")); |
| 182 | 205 | |
| ... | ... | @@ -193,6 +216,7 @@ test "deserialize unsigned LEB128" { |
| 193 | 216 | testing.expect((try test_read_uleb128(u64, "\x80\x02")) == 0x100); |
| 194 | 217 | testing.expect((try test_read_uleb128(u64, "\x81\x02")) == 0x101); |
| 195 | 218 | testing.expect((try test_read_uleb128(u64, "\x80\xc1\x80\x80\x10")) == 4294975616); |
| 219 | testing.expect((try test_read_uleb128(u64, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01")) == 0x8000000000000000); |
| 196 | 220 | |
| 197 | 221 | // Decode ULEB128 with extra padding bytes |
| 198 | 222 | testing.expect((try test_read_uleb128(u64, "\x80\x00")) == 0); |