| ... | ... | @@ -6,17 +6,15 @@ const Writer = std.Io.Writer; |
| 6 | 6 | const Reader = std.Io.Reader; |
| 7 | 7 | |
| 8 | 8 | /// An accumulating buffer for LZ sequences |
| 9 | | pub const LzAccumBuffer = struct { |
| 9 | pub const AccumBuffer = struct { |
| 10 | 10 | /// Buffer |
| 11 | 11 | buf: ArrayList(u8), |
| 12 | | |
| 13 | 12 | /// Buffer memory limit |
| 14 | 13 | memlimit: usize, |
| 15 | | |
| 16 | 14 | /// Total number of bytes sent through the buffer |
| 17 | 15 | len: usize, |
| 18 | 16 | |
| 19 | | pub fn init(memlimit: usize) LzAccumBuffer { |
| 17 | pub fn init(memlimit: usize) AccumBuffer { |
| 20 | 18 | return .{ |
| 21 | 19 | .buf = .{}, |
| 22 | 20 | .memlimit = memlimit, |
| ... | ... | @@ -24,20 +22,20 @@ pub const LzAccumBuffer = struct { |
| 24 | 22 | }; |
| 25 | 23 | } |
| 26 | 24 | |
| 27 | | pub fn appendByte(self: *LzAccumBuffer, allocator: Allocator, byte: u8) !void { |
| 25 | pub fn appendByte(self: *AccumBuffer, allocator: Allocator, byte: u8) !void { |
| 28 | 26 | try self.buf.append(allocator, byte); |
| 29 | 27 | self.len += 1; |
| 30 | 28 | } |
| 31 | 29 | |
| 32 | 30 | /// Reset the internal dictionary |
| 33 | | pub fn reset(self: *LzAccumBuffer, writer: *Writer) !void { |
| 31 | pub fn reset(self: *AccumBuffer, writer: *Writer) !void { |
| 34 | 32 | try writer.writeAll(self.buf.items); |
| 35 | 33 | self.buf.clearRetainingCapacity(); |
| 36 | 34 | self.len = 0; |
| 37 | 35 | } |
| 38 | 36 | |
| 39 | 37 | /// Retrieve the last byte or return a default |
| 40 | | pub fn lastOr(self: LzAccumBuffer, lit: u8) u8 { |
| 38 | pub fn lastOr(self: AccumBuffer, lit: u8) u8 { |
| 41 | 39 | const buf_len = self.buf.items.len; |
| 42 | 40 | return if (buf_len == 0) |
| 43 | 41 | lit |
| ... | ... | @@ -46,7 +44,7 @@ pub const LzAccumBuffer = struct { |
| 46 | 44 | } |
| 47 | 45 | |
| 48 | 46 | /// Retrieve the n-th last byte |
| 49 | | pub fn lastN(self: LzAccumBuffer, dist: usize) !u8 { |
| 47 | pub fn lastN(self: AccumBuffer, dist: usize) !u8 { |
| 50 | 48 | const buf_len = self.buf.items.len; |
| 51 | 49 | if (dist > buf_len) { |
| 52 | 50 | return error.CorruptInput; |
| ... | ... | @@ -57,7 +55,7 @@ pub const LzAccumBuffer = struct { |
| 57 | 55 | |
| 58 | 56 | /// Append a literal |
| 59 | 57 | pub fn appendLiteral( |
| 60 | | self: *LzAccumBuffer, |
| 58 | self: *AccumBuffer, |
| 61 | 59 | allocator: Allocator, |
| 62 | 60 | lit: u8, |
| 63 | 61 | writer: *Writer, |
| ... | ... | @@ -72,7 +70,7 @@ pub const LzAccumBuffer = struct { |
| 72 | 70 | |
| 73 | 71 | /// Fetch an LZ sequence (length, distance) from inside the buffer |
| 74 | 72 | pub fn appendLz( |
| 75 | | self: *LzAccumBuffer, |
| 73 | self: *AccumBuffer, |
| 76 | 74 | allocator: Allocator, |
| 77 | 75 | len: usize, |
| 78 | 76 | dist: usize, |
| ... | ... | @@ -95,12 +93,12 @@ pub const LzAccumBuffer = struct { |
| 95 | 93 | self.len += len; |
| 96 | 94 | } |
| 97 | 95 | |
| 98 | | pub fn finish(self: *LzAccumBuffer, writer: *Writer) !void { |
| 96 | pub fn finish(self: *AccumBuffer, writer: *Writer) !void { |
| 99 | 97 | try writer.writeAll(self.buf.items); |
| 100 | 98 | self.buf.clearRetainingCapacity(); |
| 101 | 99 | } |
| 102 | 100 | |
| 103 | | pub fn deinit(self: *LzAccumBuffer, allocator: Allocator) void { |
| 101 | pub fn deinit(self: *AccumBuffer, allocator: Allocator) void { |
| 104 | 102 | self.buf.deinit(allocator); |
| 105 | 103 | self.* = undefined; |
| 106 | 104 | } |
| ... | ... | @@ -109,59 +107,43 @@ pub const LzAccumBuffer = struct { |
| 109 | 107 | pub const Decode = struct { |
| 110 | 108 | lzma_decode: lzma.Decode, |
| 111 | 109 | |
| 112 | | pub fn init(allocator: Allocator) !Decode { |
| 113 | | return Decode{ |
| 114 | | .lzma_decode = try lzma.Decode.init( |
| 115 | | allocator, |
| 116 | | .{ |
| 117 | | .lc = 0, |
| 118 | | .lp = 0, |
| 119 | | .pb = 0, |
| 120 | | }, |
| 121 | | null, |
| 122 | | ), |
| 123 | | }; |
| 110 | pub fn init(gpa: Allocator) !Decode { |
| 111 | return .{ .lzma_decode = try lzma.Decode.init(gpa, .{ .lc = 0, .lp = 0, .pb = 0 }) }; |
| 124 | 112 | } |
| 125 | 113 | |
| 126 | | pub fn deinit(self: *Decode, allocator: Allocator) void { |
| 127 | | self.lzma_decode.deinit(allocator); |
| 114 | pub fn deinit(self: *Decode, gpa: Allocator) void { |
| 115 | self.lzma_decode.deinit(gpa); |
| 128 | 116 | self.* = undefined; |
| 129 | 117 | } |
| 130 | 118 | |
| 131 | | pub fn decompress( |
| 132 | | self: *Decode, |
| 133 | | allocator: Allocator, |
| 134 | | reader: *Reader, |
| 135 | | writer: *Writer, |
| 136 | | ) !void { |
| 137 | | var accum = LzAccumBuffer.init(std.math.maxInt(usize)); |
| 138 | | defer accum.deinit(allocator); |
| 119 | pub fn decompress(d: *Decode, reader: *Reader, allocating: *Writer.Allocating) !void { |
| 120 | const gpa = allocating.allocator; |
| 121 | |
| 122 | var accum = AccumBuffer.init(std.math.maxInt(usize)); |
| 123 | defer accum.deinit(gpa); |
| 139 | 124 | |
| 140 | 125 | while (true) { |
| 141 | | const status = try reader.readByte(); |
| 126 | const status = try reader.takeByte(); |
| 142 | 127 | |
| 143 | 128 | switch (status) { |
| 144 | 129 | 0 => break, |
| 145 | | 1 => try parseUncompressed(allocator, reader, writer, &accum, true), |
| 146 | | 2 => try parseUncompressed(allocator, reader, writer, &accum, false), |
| 147 | | else => try self.parseLzma(allocator, reader, writer, &accum, status), |
| 130 | 1 => try parseUncompressed(reader, allocating, &accum, true), |
| 131 | 2 => try parseUncompressed(reader, allocating, &accum, false), |
| 132 | else => try d.parseLzma(reader, allocating, &accum, status), |
| 148 | 133 | } |
| 149 | 134 | } |
| 150 | 135 | |
| 151 | | try accum.finish(writer); |
| 136 | try accum.finish(&allocating.writer); |
| 152 | 137 | } |
| 153 | 138 | |
| 154 | 139 | fn parseLzma( |
| 155 | | self: *Decode, |
| 156 | | allocator: Allocator, |
| 140 | d: *Decode, |
| 157 | 141 | reader: *Reader, |
| 158 | | writer: *Writer, |
| 159 | | accum: *LzAccumBuffer, |
| 142 | allocating: *Writer.Allocating, |
| 143 | accum: *AccumBuffer, |
| 160 | 144 | status: u8, |
| 161 | 145 | ) !void { |
| 162 | | if (status & 0x80 == 0) { |
| 163 | | return error.CorruptInput; |
| 164 | | } |
| 146 | if (status & 0x80 == 0) return error.CorruptInput; |
| 165 | 147 | |
| 166 | 148 | const Reset = struct { |
| 167 | 149 | dict: bool, |
| ... | ... | @@ -169,23 +151,23 @@ pub const Decode = struct { |
| 169 | 151 | props: bool, |
| 170 | 152 | }; |
| 171 | 153 | |
| 172 | | const reset = switch ((status >> 5) & 0x3) { |
| 173 | | 0 => Reset{ |
| 154 | const reset: Reset = switch ((status >> 5) & 0x3) { |
| 155 | 0 => .{ |
| 174 | 156 | .dict = false, |
| 175 | 157 | .state = false, |
| 176 | 158 | .props = false, |
| 177 | 159 | }, |
| 178 | | 1 => Reset{ |
| 160 | 1 => .{ |
| 179 | 161 | .dict = false, |
| 180 | 162 | .state = true, |
| 181 | 163 | .props = false, |
| 182 | 164 | }, |
| 183 | | 2 => Reset{ |
| 165 | 2 => .{ |
| 184 | 166 | .dict = false, |
| 185 | 167 | .state = true, |
| 186 | 168 | .props = true, |
| 187 | 169 | }, |
| 188 | | 3 => Reset{ |
| 170 | 3 => .{ |
| 189 | 171 | .dict = true, |
| 190 | 172 | .state = true, |
| 191 | 173 | .props = true, |
| ... | ... | @@ -196,24 +178,24 @@ pub const Decode = struct { |
| 196 | 178 | const unpacked_size = blk: { |
| 197 | 179 | var tmp: u64 = status & 0x1F; |
| 198 | 180 | tmp <<= 16; |
| 199 | | tmp |= try reader.readInt(u16, .big); |
| 181 | tmp |= try reader.takeInt(u16, .big); |
| 200 | 182 | break :blk tmp + 1; |
| 201 | 183 | }; |
| 202 | 184 | |
| 203 | 185 | const packed_size = blk: { |
| 204 | | const tmp: u17 = try reader.readInt(u16, .big); |
| 186 | const tmp: u17 = try reader.takeInt(u16, .big); |
| 205 | 187 | break :blk tmp + 1; |
| 206 | 188 | }; |
| 207 | 189 | |
| 208 | | if (reset.dict) { |
| 209 | | try accum.reset(writer); |
| 210 | | } |
| 190 | if (reset.dict) try accum.reset(&allocating.writer); |
| 191 | |
| 192 | const ld = &d.lzma_decode; |
| 211 | 193 | |
| 212 | 194 | if (reset.state) { |
| 213 | | var new_props = self.lzma_decode.properties; |
| 195 | var new_props = ld.properties; |
| 214 | 196 | |
| 215 | 197 | if (reset.props) { |
| 216 | | var props = try reader.readByte(); |
| 198 | var props = try reader.takeByte(); |
| 217 | 199 | if (props >= 225) { |
| 218 | 200 | return error.CorruptInput; |
| 219 | 201 | } |
| ... | ... | @@ -231,38 +213,44 @@ pub const Decode = struct { |
| 231 | 213 | new_props = .{ .lc = lc, .lp = lp, .pb = pb }; |
| 232 | 214 | } |
| 233 | 215 | |
| 234 | | try self.lzma_decode.resetState(allocator, new_props); |
| 216 | try ld.resetState(allocating.allocator, new_props); |
| 235 | 217 | } |
| 236 | 218 | |
| 237 | | self.lzma_decode.unpacked_size = unpacked_size + accum.len; |
| 219 | var range_decoder = try lzma.RangeDecoder.init(reader); |
| 238 | 220 | |
| 239 | | var counter = std.io.countingReader(reader); |
| 240 | | const counter_reader = counter.reader(); |
| 241 | | |
| 242 | | var rangecoder = try lzma.RangeDecoder.init(counter_reader); |
| 243 | | while (try self.lzma_decode.process(allocator, counter_reader, writer, accum, &rangecoder) == .continue_) {} |
| 244 | | |
| 245 | | if (counter.bytes_read != packed_size) { |
| 246 | | return error.CorruptInput; |
| 221 | while (true) { |
| 222 | if (accum.len >= unpacked_size) break; |
| 223 | if (range_decoder.isFinished()) break; |
| 224 | switch (try ld.process(reader, allocating, accum, &range_decoder)) { |
| 225 | .more => continue, |
| 226 | .finished => break, |
| 227 | } |
| 247 | 228 | } |
| 229 | if (accum.len != unpacked_size) return error.DecompressedSizeMismatch; |
| 230 | |
| 231 | // TODO restore this error |
| 232 | //if (counter.bytes_read != packed_size) { |
| 233 | // return error.CorruptInput; |
| 234 | //} |
| 235 | _ = packed_size; |
| 248 | 236 | } |
| 249 | 237 | |
| 250 | 238 | fn parseUncompressed( |
| 251 | | allocator: Allocator, |
| 252 | 239 | reader: *Reader, |
| 253 | | writer: *Writer, |
| 254 | | accum: *LzAccumBuffer, |
| 240 | allocating: *Writer.Allocating, |
| 241 | accum: *AccumBuffer, |
| 255 | 242 | reset_dict: bool, |
| 256 | 243 | ) !void { |
| 257 | | const unpacked_size = @as(u17, try reader.readInt(u16, .big)) + 1; |
| 244 | const unpacked_size = @as(u17, try reader.takeInt(u16, .big)) + 1; |
| 258 | 245 | |
| 259 | | if (reset_dict) { |
| 260 | | try accum.reset(writer); |
| 261 | | } |
| 246 | if (reset_dict) try accum.reset(&allocating.writer); |
| 247 | |
| 248 | const gpa = allocating.allocator; |
| 262 | 249 | |
| 263 | | var i: @TypeOf(unpacked_size) = 0; |
| 264 | | while (i < unpacked_size) : (i += 1) { |
| 265 | | try accum.appendByte(allocator, try reader.readByte()); |
| 250 | var i = unpacked_size; |
| 251 | while (i != 0) { |
| 252 | try accum.appendByte(gpa, try reader.takeByte()); |
| 253 | i -= 1; |
| 266 | 254 | } |
| 267 | 255 | } |
| 268 | 256 | }; |
| ... | ... | @@ -273,13 +261,13 @@ test "decompress hello world stream" { |
| 273 | 261 | |
| 274 | 262 | const gpa = std.testing.allocator; |
| 275 | 263 | |
| 276 | | var stream: std.Io.Reader = .fixed(compressed); |
| 277 | | |
| 278 | | var decode = try Decode.init(gpa, &stream); |
| 264 | var decode = try Decode.init(gpa); |
| 279 | 265 | defer decode.deinit(gpa); |
| 280 | 266 | |
| 281 | | const result = try decode.reader.allocRemaining(gpa, .unlimited); |
| 282 | | defer gpa.free(result); |
| 267 | var stream: std.Io.Reader = .fixed(compressed); |
| 268 | var result: std.Io.Writer.Allocating = .init(gpa); |
| 269 | defer result.deinit(); |
| 283 | 270 | |
| 284 | | try std.testing.expectEqualStrings(expected, result); |
| 271 | try decode.decompress(&stream, &result); |
| 272 | try std.testing.expectEqualStrings(expected, result.written()); |
| 285 | 273 | } |