| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | // |
| 2 | | // Decompressor for GZIP data streams (RFC1952) |
| 2 | // Compressor/Decompressor for GZIP data streams (RFC1952) |
| 3 | 3 | |
| 4 | 4 | const std = @import("../std.zig"); |
| 5 | 5 | const io = std.io; |
| ... | ... | @@ -8,6 +8,8 @@ const testing = std.testing; |
| 8 | 8 | const mem = std.mem; |
| 9 | 9 | const deflate = std.compress.deflate; |
| 10 | 10 | |
| 11 | const magic = &[2]u8{ 0x1f, 0x8b }; |
| 12 | |
| 11 | 13 | // Flags for the FLG field in the header |
| 12 | 14 | const FTEXT = 1 << 0; |
| 13 | 15 | const FHCRC = 1 << 1; |
| ... | ... | @@ -17,6 +19,14 @@ const FCOMMENT = 1 << 4; |
| 17 | 19 | |
| 18 | 20 | const max_string_len = 1024; |
| 19 | 21 | |
| 22 | pub const Header = struct { |
| 23 | extra: ?[]const u8 = null, |
| 24 | filename: ?[]const u8 = null, |
| 25 | comment: ?[]const u8 = null, |
| 26 | modification_time: u32 = 0, |
| 27 | operating_system: u8 = 255, |
| 28 | }; |
| 29 | |
| 20 | 30 | pub fn Decompress(comptime ReaderType: type) type { |
| 21 | 31 | return struct { |
| 22 | 32 | const Self = @This(); |
| ... | ... | @@ -30,25 +40,19 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 30 | 40 | inflater: deflate.Decompressor(ReaderType), |
| 31 | 41 | in_reader: ReaderType, |
| 32 | 42 | hasher: std.hash.Crc32, |
| 33 | | read_amt: usize, |
| 34 | | |
| 35 | | info: struct { |
| 36 | | extra: ?[]const u8, |
| 37 | | filename: ?[]const u8, |
| 38 | | comment: ?[]const u8, |
| 39 | | modification_time: u32, |
| 40 | | operating_system: u8, |
| 41 | | }, |
| 43 | read_amt: u32, |
| 44 | |
| 45 | info: Header, |
| 42 | 46 | |
| 43 | | fn init(allocator: mem.Allocator, source: ReaderType) !Self { |
| 44 | | var hasher = std.compress.hashedReader(source, std.hash.Crc32.init()); |
| 47 | fn init(allocator: mem.Allocator, in_reader: ReaderType) !Self { |
| 48 | var hasher = std.compress.hashedReader(in_reader, std.hash.Crc32.init()); |
| 45 | 49 | const hashed_reader = hasher.reader(); |
| 46 | 50 | |
| 47 | 51 | // gzip header format is specified in RFC1952 |
| 48 | 52 | const header = try hashed_reader.readBytesNoEof(10); |
| 49 | 53 | |
| 50 | 54 | // Check the ID1/ID2 fields |
| 51 | | if (header[0] != 0x1f or header[1] != 0x8b) |
| 55 | if (!std.mem.eql(u8, header[0..2], magic)) |
| 52 | 56 | return error.BadHeader; |
| 53 | 57 | |
| 54 | 58 | const CM = header[2]; |
| ... | ... | @@ -88,15 +92,15 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 88 | 92 | errdefer if (comment) |p| allocator.free(p); |
| 89 | 93 | |
| 90 | 94 | if (FLG & FHCRC != 0) { |
| 91 | | const hash = try source.readInt(u16, .little); |
| 95 | const hash = try in_reader.readInt(u16, .little); |
| 92 | 96 | if (hash != @as(u16, @truncate(hasher.hasher.final()))) |
| 93 | 97 | return error.WrongChecksum; |
| 94 | 98 | } |
| 95 | 99 | |
| 96 | | return Self{ |
| 100 | return .{ |
| 97 | 101 | .allocator = allocator, |
| 98 | | .inflater = try deflate.decompressor(allocator, source, null), |
| 99 | | .in_reader = source, |
| 102 | .inflater = try deflate.decompressor(allocator, in_reader, null), |
| 103 | .in_reader = in_reader, |
| 100 | 104 | .hasher = std.hash.Crc32.init(), |
| 101 | 105 | .info = .{ |
| 102 | 106 | .filename = filename, |
| ... | ... | @@ -119,7 +123,7 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 119 | 123 | self.allocator.free(comment); |
| 120 | 124 | } |
| 121 | 125 | |
| 122 | | // Implements the io.Reader interface |
| 126 | /// Implements the io.Reader interface |
| 123 | 127 | pub fn read(self: *Self, buffer: []u8) Error!usize { |
| 124 | 128 | if (buffer.len == 0) |
| 125 | 129 | return 0; |
| ... | ... | @@ -128,10 +132,12 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 128 | 132 | const r = try self.inflater.read(buffer); |
| 129 | 133 | if (r != 0) { |
| 130 | 134 | self.hasher.update(buffer[0..r]); |
| 131 | | self.read_amt += r; |
| 135 | self.read_amt +%= @truncate(r); |
| 132 | 136 | return r; |
| 133 | 137 | } |
| 134 | 138 | |
| 139 | try self.inflater.close(); |
| 140 | |
| 135 | 141 | // We've reached the end of stream, check if the checksum matches |
| 136 | 142 | const hash = try self.in_reader.readInt(u32, .little); |
| 137 | 143 | if (hash != self.hasher.final()) |
| ... | ... | @@ -139,7 +145,7 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 139 | 145 | |
| 140 | 146 | // The ISIZE field is the size of the uncompressed input modulo 2^32 |
| 141 | 147 | const input_size = try self.in_reader.readInt(u32, .little); |
| 142 | | if (self.read_amt & 0xffffffff != input_size) |
| 148 | if (self.read_amt != input_size) |
| 143 | 149 | return error.CorruptedData; |
| 144 | 150 | |
| 145 | 151 | return 0; |
| ... | ... | @@ -155,7 +161,117 @@ pub fn decompress(allocator: mem.Allocator, reader: anytype) !Decompress(@TypeOf |
| 155 | 161 | return Decompress(@TypeOf(reader)).init(allocator, reader); |
| 156 | 162 | } |
| 157 | 163 | |
| 158 | | fn testReader(data: []const u8, comptime expected: []const u8) !void { |
| 164 | pub const CompressOptions = struct { |
| 165 | header: Header = .{}, |
| 166 | hash_header: bool = true, |
| 167 | level: deflate.Compression = .default_compression, |
| 168 | }; |
| 169 | |
| 170 | pub fn Compress(comptime WriterType: type) type { |
| 171 | return struct { |
| 172 | const Self = @This(); |
| 173 | |
| 174 | pub const Error = WriterType.Error || |
| 175 | deflate.Compressor(WriterType).Error; |
| 176 | pub const Writer = io.Writer(*Self, Error, write); |
| 177 | |
| 178 | allocator: mem.Allocator, |
| 179 | deflater: deflate.Compressor(WriterType), |
| 180 | out_writer: WriterType, |
| 181 | hasher: std.hash.Crc32, |
| 182 | write_amt: u32, |
| 183 | |
| 184 | fn init(allocator: mem.Allocator, out_writer: WriterType, options: CompressOptions) !Self { |
| 185 | var hasher = std.compress.hashedWriter(out_writer, std.hash.Crc32.init()); |
| 186 | const hashed_writer = hasher.writer(); |
| 187 | |
| 188 | // ID1/ID2 |
| 189 | try hashed_writer.writeAll(magic); |
| 190 | // CM |
| 191 | try hashed_writer.writeByte(8); |
| 192 | // Flags |
| 193 | try hashed_writer.writeByte( |
| 194 | @as(u8, if (options.hash_header) FHCRC else 0) | |
| 195 | @as(u8, if (options.header.extra) |_| FEXTRA else 0) | |
| 196 | @as(u8, if (options.header.filename) |_| FNAME else 0) | |
| 197 | @as(u8, if (options.header.comment) |_| FCOMMENT else 0), |
| 198 | ); |
| 199 | // Modification time |
| 200 | try hashed_writer.writeInt(u32, options.header.modification_time, .little); |
| 201 | // Extra flags |
| 202 | try hashed_writer.writeByte(0); |
| 203 | // Operating system |
| 204 | try hashed_writer.writeByte(options.header.operating_system); |
| 205 | |
| 206 | if (options.header.extra) |extra| { |
| 207 | try hashed_writer.writeInt(u16, @intCast(extra.len), .little); |
| 208 | try hashed_writer.writeAll(extra); |
| 209 | } |
| 210 | |
| 211 | if (options.header.filename) |filename| { |
| 212 | try hashed_writer.writeAll(filename); |
| 213 | try hashed_writer.writeByte(0); |
| 214 | } |
| 215 | |
| 216 | if (options.header.comment) |comment| { |
| 217 | try hashed_writer.writeAll(comment); |
| 218 | try hashed_writer.writeByte(0); |
| 219 | } |
| 220 | |
| 221 | if (options.hash_header) { |
| 222 | try out_writer.writeInt( |
| 223 | u16, |
| 224 | @truncate(hasher.hasher.final()), |
| 225 | .little, |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | return .{ |
| 230 | .allocator = allocator, |
| 231 | .deflater = try deflate.compressor(allocator, out_writer, .{ .level = options.level }), |
| 232 | .out_writer = out_writer, |
| 233 | .hasher = std.hash.Crc32.init(), |
| 234 | .write_amt = 0, |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | pub fn deinit(self: *Self) void { |
| 239 | self.deflater.deinit(); |
| 240 | } |
| 241 | |
| 242 | /// Implements the io.Writer interface |
| 243 | pub fn write(self: *Self, buffer: []const u8) Error!usize { |
| 244 | if (buffer.len == 0) |
| 245 | return 0; |
| 246 | |
| 247 | // Write to the compressed stream and update the computed checksum |
| 248 | const r = try self.deflater.write(buffer); |
| 249 | self.hasher.update(buffer[0..r]); |
| 250 | self.write_amt +%= @truncate(r); |
| 251 | return r; |
| 252 | } |
| 253 | |
| 254 | pub fn writer(self: *Self) Writer { |
| 255 | return .{ .context = self }; |
| 256 | } |
| 257 | |
| 258 | pub fn flush(self: *Self) Error!void { |
| 259 | try self.deflater.flush(); |
| 260 | } |
| 261 | |
| 262 | pub fn close(self: *Self) Error!void { |
| 263 | try self.deflater.close(); |
| 264 | try self.out_writer.writeInt(u32, self.hasher.final(), .little); |
| 265 | try self.out_writer.writeInt(u32, self.write_amt, .little); |
| 266 | } |
| 267 | }; |
| 268 | } |
| 269 | |
| 270 | pub fn compress(allocator: mem.Allocator, writer: anytype, options: CompressOptions) !Compress(@TypeOf(writer)) { |
| 271 | return Compress(@TypeOf(writer)).init(allocator, writer, options); |
| 272 | } |
| 273 | |
| 274 | fn testReader(expected: []const u8, data: []const u8) !void { |
| 159 | 275 | var in_stream = io.fixedBufferStream(data); |
| 160 | 276 | |
| 161 | 277 | var gzip_stream = try decompress(testing.allocator, in_stream.reader()); |
| ... | ... | @@ -169,70 +285,91 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void { |
| 169 | 285 | try testing.expectEqualSlices(u8, expected, buf); |
| 170 | 286 | } |
| 171 | 287 | |
| 288 | fn testWriter(expected: []const u8, data: []const u8, options: CompressOptions) !void { |
| 289 | var actual = std.ArrayList(u8).init(testing.allocator); |
| 290 | defer actual.deinit(); |
| 291 | |
| 292 | var gzip_stream = try compress(testing.allocator, actual.writer(), options); |
| 293 | defer gzip_stream.deinit(); |
| 294 | |
| 295 | // Write and compress the whole file |
| 296 | try gzip_stream.writer().writeAll(data); |
| 297 | try gzip_stream.close(); |
| 298 | |
| 299 | // Check against the reference |
| 300 | try testing.expectEqualSlices(u8, expected, actual.items); |
| 301 | } |
| 302 | |
| 172 | 303 | // All the test cases are obtained by compressing the RFC1952 text |
| 173 | 304 | // |
| 174 | 305 | // https://tools.ietf.org/rfc/rfc1952.txt length=25037 bytes |
| 175 | 306 | // SHA256=164ef0897b4cbec63abf1b57f069f3599bd0fb7c72c2a4dee21bd7e03ec9af67 |
| 176 | 307 | test "compressed data" { |
| 177 | | try testReader( |
| 178 | | @embedFile("testdata/rfc1952.txt.gz"), |
| 179 | | @embedFile("testdata/rfc1952.txt"), |
| 180 | | ); |
| 308 | const plain = @embedFile("testdata/rfc1952.txt"); |
| 309 | const compressed = @embedFile("testdata/rfc1952.txt.gz"); |
| 310 | try testReader(plain, compressed); |
| 311 | try testWriter(compressed, plain, .{ |
| 312 | .header = .{ |
| 313 | .filename = "rfc1952.txt", |
| 314 | .modification_time = 1706533053, |
| 315 | .operating_system = 3, |
| 316 | }, |
| 317 | }); |
| 181 | 318 | } |
| 182 | 319 | |
| 183 | 320 | test "sanity checks" { |
| 184 | 321 | // Truncated header |
| 185 | 322 | try testing.expectError( |
| 186 | 323 | error.EndOfStream, |
| 187 | | testReader(&[_]u8{ 0x1f, 0x8B }, ""), |
| 324 | testReader(undefined, &[_]u8{ 0x1f, 0x8B }), |
| 188 | 325 | ); |
| 189 | 326 | // Wrong CM |
| 190 | 327 | try testing.expectError( |
| 191 | 328 | error.InvalidCompression, |
| 192 | | testReader(&[_]u8{ |
| 329 | testReader(undefined, &[_]u8{ |
| 193 | 330 | 0x1f, 0x8b, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 194 | 331 | 0x00, 0x03, |
| 195 | | }, ""), |
| 332 | }), |
| 196 | 333 | ); |
| 197 | 334 | // Wrong checksum |
| 198 | 335 | try testing.expectError( |
| 199 | 336 | error.WrongChecksum, |
| 200 | | testReader(&[_]u8{ |
| 337 | testReader(undefined, &[_]u8{ |
| 201 | 338 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 202 | 339 | 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 203 | 340 | 0x00, 0x00, 0x00, 0x00, |
| 204 | | }, ""), |
| 341 | }), |
| 205 | 342 | ); |
| 206 | 343 | // Truncated checksum |
| 207 | 344 | try testing.expectError( |
| 208 | 345 | error.EndOfStream, |
| 209 | | testReader(&[_]u8{ |
| 346 | testReader(undefined, &[_]u8{ |
| 210 | 347 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 211 | 348 | 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, |
| 212 | | }, ""), |
| 349 | }), |
| 213 | 350 | ); |
| 214 | 351 | // Wrong initial size |
| 215 | 352 | try testing.expectError( |
| 216 | 353 | error.CorruptedData, |
| 217 | | testReader(&[_]u8{ |
| 354 | testReader(undefined, &[_]u8{ |
| 218 | 355 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 219 | 356 | 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 220 | 357 | 0x00, 0x00, 0x00, 0x01, |
| 221 | | }, ""), |
| 358 | }), |
| 222 | 359 | ); |
| 223 | 360 | // Truncated initial size field |
| 224 | 361 | try testing.expectError( |
| 225 | 362 | error.EndOfStream, |
| 226 | | testReader(&[_]u8{ |
| 363 | testReader(undefined, &[_]u8{ |
| 227 | 364 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 228 | 365 | 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 229 | 366 | 0x00, 0x00, 0x00, |
| 230 | | }, ""), |
| 367 | }), |
| 231 | 368 | ); |
| 232 | 369 | } |
| 233 | 370 | |
| 234 | 371 | test "header checksum" { |
| 235 | | try testReader(&[_]u8{ |
| 372 | try testReader("", &[_]u8{ |
| 236 | 373 | // GZIP header |
| 237 | 374 | 0x1f, 0x8b, 0x08, 0x12, 0x00, 0x09, 0x6e, 0x88, 0x00, 0xff, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x00, |
| 238 | 375 | |
| ... | ... | @@ -241,5 +378,5 @@ test "header checksum" { |
| 241 | 378 | |
| 242 | 379 | // GZIP data |
| 243 | 380 | 0x01, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 244 | | }, ""); |
| 381 | }); |
| 245 | 382 | } |