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