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