| ... | @@ -46,7 +46,7 @@ pub fn readULEB128Mem(comptime T: type, ptr: *[*]const u8) !T { | ... | @@ -46,7 +46,7 @@ pub fn readULEB128Mem(comptime T: type, ptr: *[*]const u8) !T { |
| 46 | result |= operand; | 46 | result |= operand; |
| 47 | | 47 | |
| 48 | if ((byte & 0x80) == 0) { | 48 | if ((byte & 0x80) == 0) { |
| 49 | ptr.* += i; | 49 | ptr.* += i + 1; |
| 50 | return result; | 50 | return result; |
| 51 | } | 51 | } |
| 52 | | 52 | |
| ... | @@ -114,7 +114,7 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T { | ... | @@ -114,7 +114,7 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T { |
| 114 | if (shift < T.bit_count and (byte & 0x40) != 0) { | 114 | if (shift < T.bit_count and (byte & 0x40) != 0) { |
| 115 | result |= @bitCast(UT, @intCast(T, -1)) << @intCast(ShiftT, shift); | 115 | result |= @bitCast(UT, @intCast(T, -1)) << @intCast(ShiftT, shift); |
| 116 | } | 116 | } |
| 117 | ptr.* += i; | 117 | ptr.* += i + 1; |
| 118 | return @bitCast(T, result); | 118 | return @bitCast(T, result); |
| 119 | } | 119 | } |
| 120 | } | 120 | } |
| ... | @@ -148,6 +148,28 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T { | ... | @@ -148,6 +148,28 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T { |
| 148 | return v1; | 148 | return v1; |
| 149 | } | 149 | } |
| 150 | | 150 | |
| | 151 | fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) void { |
| | 152 | var in_stream = std.io.SliceInStream.init(encoded); |
| | 153 | var in_ptr = encoded.ptr; |
| | 154 | var i: usize = 0; |
| | 155 | while (i < N) : (i += 1) { |
| | 156 | const v1 = readILEB128(T, &in_stream.stream); |
| | 157 | const v2 = readILEB128Mem(T, &in_ptr); |
| | 158 | testing.expectEqual(v1, v2); |
| | 159 | } |
| | 160 | } |
| | 161 | |
| | 162 | fn test_read_uleb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) void { |
| | 163 | var in_stream = std.io.SliceInStream.init(encoded); |
| | 164 | var in_ptr = encoded.ptr; |
| | 165 | var i: usize = 0; |
| | 166 | while (i < N) : (i += 1) { |
| | 167 | const v1 = readULEB128(T, &in_stream.stream); |
| | 168 | const v2 = readULEB128Mem(T, &in_ptr); |
| | 169 | testing.expectEqual(v1, v2); |
| | 170 | } |
| | 171 | } |
| | 172 | |
| 151 | test "deserialize signed LEB128" { | 173 | test "deserialize signed LEB128" { |
| 152 | // Truncated | 174 | // Truncated |
| 153 | testing.expectError(error.EndOfStream, test_read_stream_ileb128(i64, "\x80")); | 175 | testing.expectError(error.EndOfStream, test_read_stream_ileb128(i64, "\x80")); |
| ... | @@ -188,6 +210,9 @@ test "deserialize signed LEB128" { | ... | @@ -188,6 +210,9 @@ test "deserialize signed LEB128" { |
| 188 | testing.expect((try test_read_ileb128(i64, "\xff\x80\x00")) == 0x7f); | 210 | testing.expect((try test_read_ileb128(i64, "\xff\x80\x00")) == 0x7f); |
| 189 | testing.expect((try test_read_ileb128(i64, "\x80\x81\x00")) == 0x80); | 211 | testing.expect((try test_read_ileb128(i64, "\x80\x81\x00")) == 0x80); |
| 190 | testing.expect((try test_read_ileb128(i64, "\x80\x81\x80\x00")) == 0x80); | 212 | testing.expect((try test_read_ileb128(i64, "\x80\x81\x80\x00")) == 0x80); |
| | 213 | |
| | 214 | // Decode sequence of SLEB128 values |
| | 215 | test_read_ileb128_seq(i64, 4, "\x81\x01\x3f\x80\x7f\x80\x80\x80\x00"); |
| 191 | } | 216 | } |
| 192 | | 217 | |
| 193 | test "deserialize unsigned LEB128" { | 218 | test "deserialize unsigned LEB128" { |
| ... | @@ -225,4 +250,7 @@ test "deserialize unsigned LEB128" { | ... | @@ -225,4 +250,7 @@ test "deserialize unsigned LEB128" { |
| 225 | testing.expect((try test_read_uleb128(u64, "\xff\x80\x00")) == 0x7f); | 250 | testing.expect((try test_read_uleb128(u64, "\xff\x80\x00")) == 0x7f); |
| 226 | testing.expect((try test_read_uleb128(u64, "\x80\x81\x00")) == 0x80); | 251 | testing.expect((try test_read_uleb128(u64, "\x80\x81\x00")) == 0x80); |
| 227 | testing.expect((try test_read_uleb128(u64, "\x80\x81\x80\x00")) == 0x80); | 252 | testing.expect((try test_read_uleb128(u64, "\x80\x81\x80\x00")) == 0x80); |
| | 253 | |
| | 254 | // Decode sequence of ULEB128 values |
| | 255 | test_read_uleb128_seq(u64, 4, "\x81\x01\x3f\x80\x7f\x80\x80\x80\x00"); |
| 228 | } | 256 | } |