| ... | ... | @@ -108,53 +108,37 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T { |
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | | const OneByteReadInStream = struct { |
| 112 | | const Error = error{NoError}; |
| 113 | | const Stream = std.io.InStream(Error); |
| 114 | | |
| 115 | | stream: Stream, |
| 116 | | str: []const u8, |
| 117 | | curr: usize, |
| 118 | | |
| 119 | | fn init(str: []const u8) @This() { |
| 120 | | return @This(){ |
| 121 | | .stream = Stream{ .readFn = readFn }, |
| 122 | | .str = str, |
| 123 | | .curr = 0, |
| 124 | | }; |
| 125 | | } |
| 126 | | |
| 127 | | fn readFn(in_stream: *Stream, dest: []u8) Error!usize { |
| 128 | | const self = @fieldParentPtr(@This(), "stream", in_stream); |
| 129 | | if (self.str.len <= self.curr or dest.len == 0) |
| 130 | | return 0; |
| 111 | fn test_read_stream_ileb128(comptime T: type, encoded: []const u8) !T { |
| 112 | var in_stream = std.io.SliceInStream.init(encoded); |
| 113 | return try readILEB128(T, &in_stream.stream); |
| 114 | } |
| 131 | 115 | |
| 132 | | dest[0] = self.str[self.curr]; |
| 133 | | self.curr += 1; |
| 134 | | return 1; |
| 135 | | } |
| 136 | | }; |
| 116 | fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T { |
| 117 | var in_stream = std.io.SliceInStream.init(encoded); |
| 118 | return try readULEB128(T, &in_stream.stream); |
| 119 | } |
| 137 | 120 | |
| 138 | 121 | fn test_read_ileb128(comptime T: type, encoded: []const u8) !T { |
| 139 | | var in_stream = OneByteReadInStream.init(encoded); |
| 140 | | const v1 = try readILEB128(T, &in_stream.stream); |
| 122 | var in_stream = std.io.SliceInStream.init(encoded); |
| 123 | const v1 = readILEB128(T, &in_stream.stream); |
| 141 | 124 | var in_ptr = encoded.ptr; |
| 142 | | const v2 = try readILEB128Mem(T, &in_ptr); |
| 125 | const v2 = readILEB128Mem(T, &in_ptr); |
| 143 | 126 | testing.expectEqual(v1, v2); |
| 144 | | return v2; |
| 127 | return v1; |
| 145 | 128 | } |
| 146 | 129 | |
| 147 | 130 | fn test_read_uleb128(comptime T: type, encoded: []const u8) !T { |
| 148 | | var in_stream = OneByteReadInStream.init(encoded); |
| 149 | | const v1 = try readULEB128(T, &in_stream.stream); |
| 131 | var in_stream = std.io.SliceInStream.init(encoded); |
| 132 | const v1 = readULEB128(T, &in_stream.stream); |
| 150 | 133 | var in_ptr = encoded.ptr; |
| 151 | | const v2 = try readULEB128Mem(T, &in_ptr); |
| 152 | | return v2; |
| 134 | const v2 = readULEB128Mem(T, &in_ptr); |
| 135 | testing.expectEqual(v1, v2); |
| 136 | return v1; |
| 153 | 137 | } |
| 154 | 138 | |
| 155 | 139 | test "deserialize signed LEB128" { |
| 156 | 140 | // Truncated |
| 157 | | testing.expectError(error.EndOfStream, test_read_ileb128(i64, "\x80")); |
| 141 | testing.expectError(error.EndOfStream, test_read_stream_ileb128(i64, "\x80")); |
| 158 | 142 | |
| 159 | 143 | // Overflow |
| 160 | 144 | testing.expectError(error.Overflow, test_read_ileb128(i8, "\x80\x80\x40")); |
| ... | ... | @@ -188,7 +172,7 @@ test "deserialize signed LEB128" { |
| 188 | 172 | |
| 189 | 173 | test "deserialize unsigned LEB128" { |
| 190 | 174 | // Truncated |
| 191 | | testing.expectError(error.EndOfStream, test_read_uleb128(u64, "\x80")); |
| 175 | testing.expectError(error.EndOfStream, test_read_stream_uleb128(u64, "\x80")); |
| 192 | 176 | |
| 193 | 177 | // Overflow |
| 194 | 178 | testing.expectError(error.Overflow, test_read_uleb128(u8, "\x80\x80\x40")); |