mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-07 22:34:28 +00:00
Zig deflate compression/decompression implementation. It supports compression and decompression of gzip, zlib and raw deflate format. Fixes #18062. This PR replaces current compress/gzip and compress/zlib packages. Deflate package is renamed to flate. Flate is common name for deflate/inflate where deflate is compression and inflate decompression. There are breaking change. Methods signatures are changed because of removal of the allocator, and I also unified API for all three namespaces (flate, gzip, zlib). Currently I put old packages under v1 namespace they are still available as compress/v1/gzip, compress/v1/zlib, compress/v1/deflate. Idea is to give users of the current API little time to postpone analyzing what they had to change. Although that rises question when it is safe to remove that v1 namespace. Here is current API in the compress package: ```Zig // deflate fn compressor(allocator, writer, options) !Compressor(@TypeOf(writer)) fn Compressor(comptime WriterType) type fn decompressor(allocator, reader, null) !Decompressor(@TypeOf(reader)) fn Decompressor(comptime ReaderType: type) type // gzip fn compress(allocator, writer, options) !Compress(@TypeOf(writer)) fn Compress(comptime WriterType: type) type fn decompress(allocator, reader) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // zlib fn compressStream(allocator, writer, options) !CompressStream(@TypeOf(writer)) fn CompressStream(comptime WriterType: type) type fn decompressStream(allocator, reader) !DecompressStream(@TypeOf(reader)) fn DecompressStream(comptime ReaderType: type) type // xz fn decompress(allocator: Allocator, reader: anytype) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // lzma fn decompress(allocator, reader) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // lzma2 fn decompress(allocator, reader, writer !void // zstandard: fn DecompressStream(ReaderType, options) type fn decompressStream(allocator, reader) DecompressStream(@TypeOf(reader), .{}) struct decompress ``` The proposed naming convention: - Compressor/Decompressor for functions which return type, like Reader/Writer/GeneralPurposeAllocator - compressor/compressor for functions which are initializers for that type, like reader/writer/allocator - compress/decompress for one shot operations, accepts reader/writer pair, like read/write/alloc ```Zig /// Compress from reader and write compressed data to the writer. fn compress(reader: anytype, writer: anytype, options: Options) !void /// Create Compressor which outputs the writer. fn compressor(writer: anytype, options: Options) !Compressor(@TypeOf(writer)) /// Compressor type fn Compressor(comptime WriterType: type) type /// Decompress from reader and write plain data to the writer. fn decompress(reader: anytype, writer: anytype) !void /// Create Decompressor which reads from reader. fn decompressor(reader: anytype) Decompressor(@TypeOf(reader) /// Decompressor type fn Decompressor(comptime ReaderType: type) type ``` Comparing this implementation with the one we currently have in Zig's standard library (std). Std is roughly 1.2-1.4 times slower in decompression, and 1.1-1.2 times slower in compression. Compressed sizes are pretty much same in both cases. More resutls in [this](https://github.com/ianic/flate) repo. This library uses static allocations for all structures, doesn't require allocator. That makes sense especially for deflate where all structures, internal buffers are allocated to the full size. Little less for inflate where we std version uses less memory by not preallocating to theoretical max size array which are usually not fully used. For deflate this library allocates 395K while std 779K. For inflate this library allocates 74.5K while std around 36K. Inflate difference is because we here use 64K history instead of 32K in std. If merged existing usage of compress gzip/zlib/deflate need some changes. Here is example with necessary changes in comments: ```Zig const std = @import("std"); // To get this file: // wget -nc -O war_and_peace.txt https://www.gutenberg.org/ebooks/2600.txt.utf-8 const data = @embedFile("war_and_peace.txt"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer std.debug.assert(gpa.deinit() == .ok); const allocator = gpa.allocator(); try oldDeflate(allocator); try new(std.compress.flate, allocator); try oldZlib(allocator); try new(std.compress.zlib, allocator); try oldGzip(allocator); try new(std.compress.gzip, allocator); } pub fn new(comptime pkg: type, allocator: std.mem.Allocator) !void { var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor var cmp = try pkg.compressor(buf.writer(), .{}); _ = try cmp.write(data); try cmp.finish(); var fbs = std.io.fixedBufferStream(buf.items); // Decompressor var dcp = pkg.decompressor(fbs.reader()); const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldDeflate(allocator: std.mem.Allocator) !void { const deflate = std.compress.v1.deflate; // Compressor var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Remove allocator // Rename deflate -> flate var cmp = try deflate.compressor(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.close(); // Rename to finish cmp.deinit(); // Remove // Decompressor var fbs = std.io.fixedBufferStream(buf.items); // Remove allocator and last param // Rename deflate -> flate // Remove try var dcp = try deflate.decompressor(allocator, fbs.reader(), null); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldZlib(allocator: std.mem.Allocator) !void { const zlib = std.compress.v1.zlib; var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor // Rename compressStream => compressor // Remove allocator var cmp = try zlib.compressStream(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.finish(); cmp.deinit(); // Remove var fbs = std.io.fixedBufferStream(buf.items); // Decompressor // decompressStream => decompressor // Remove allocator // Remove try var dcp = try zlib.decompressStream(allocator, fbs.reader()); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldGzip(allocator: std.mem.Allocator) !void { const gzip = std.compress.v1.gzip; var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor // Rename compress => compressor // Remove allocator var cmp = try gzip.compress(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.close(); // Rename to finisho cmp.deinit(); // Remove var fbs = std.io.fixedBufferStream(buf.items); // Decompressor // Rename decompress => decompressor // Remove allocator // Remove try var dcp = try gzip.decompress(allocator, fbs.reader()); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } ```
282 lines
8.8 KiB
Zig
282 lines
8.8 KiB
Zig
//
|
|
// Compressor/Decompressor for ZLIB data streams (RFC1950)
|
|
|
|
const std = @import("std");
|
|
const io = std.io;
|
|
const fs = std.fs;
|
|
const testing = std.testing;
|
|
const mem = std.mem;
|
|
const deflate = @import("deflate.zig");
|
|
|
|
// Zlib header format as specified in RFC1950
|
|
const ZLibHeader = packed struct {
|
|
checksum: u5,
|
|
preset_dict: u1,
|
|
compression_level: u2,
|
|
compression_method: u4,
|
|
compression_info: u4,
|
|
|
|
const DEFLATE = 8;
|
|
const WINDOW_32K = 7;
|
|
};
|
|
|
|
pub fn DecompressStream(comptime ReaderType: type) type {
|
|
return struct {
|
|
const Self = @This();
|
|
|
|
pub const Error = ReaderType.Error ||
|
|
deflate.Decompressor(ReaderType).Error ||
|
|
error{ WrongChecksum, Unsupported };
|
|
pub const Reader = io.Reader(*Self, Error, read);
|
|
|
|
allocator: mem.Allocator,
|
|
inflater: deflate.Decompressor(ReaderType),
|
|
in_reader: ReaderType,
|
|
hasher: std.hash.Adler32,
|
|
|
|
fn init(allocator: mem.Allocator, source: ReaderType) !Self {
|
|
// Zlib header format is specified in RFC1950
|
|
const header_u16 = try source.readInt(u16, .big);
|
|
|
|
// verify the header checksum
|
|
if (header_u16 % 31 != 0)
|
|
return error.BadHeader;
|
|
const header = @as(ZLibHeader, @bitCast(header_u16));
|
|
|
|
// The CM field must be 8 to indicate the use of DEFLATE
|
|
if (header.compression_method != ZLibHeader.DEFLATE)
|
|
return error.InvalidCompression;
|
|
// CINFO is the base-2 logarithm of the LZ77 window size, minus 8.
|
|
// Values above 7 are unspecified and therefore rejected.
|
|
if (header.compression_info > ZLibHeader.WINDOW_32K)
|
|
return error.InvalidWindowSize;
|
|
|
|
const dictionary = null;
|
|
// TODO: Support this case
|
|
if (header.preset_dict != 0)
|
|
return error.Unsupported;
|
|
|
|
return Self{
|
|
.allocator = allocator,
|
|
.inflater = try deflate.decompressor(allocator, source, dictionary),
|
|
.in_reader = source,
|
|
.hasher = std.hash.Adler32.init(),
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
self.inflater.deinit();
|
|
}
|
|
|
|
// Implements the io.Reader interface
|
|
pub fn read(self: *Self, buffer: []u8) Error!usize {
|
|
if (buffer.len == 0)
|
|
return 0;
|
|
|
|
// Read from the compressed stream and update the computed checksum
|
|
const r = try self.inflater.read(buffer);
|
|
if (r != 0) {
|
|
self.hasher.update(buffer[0..r]);
|
|
return r;
|
|
}
|
|
|
|
// We've reached the end of stream, check if the checksum matches
|
|
const hash = try self.in_reader.readInt(u32, .big);
|
|
if (hash != self.hasher.final())
|
|
return error.WrongChecksum;
|
|
|
|
return 0;
|
|
}
|
|
|
|
pub fn reader(self: *Self) Reader {
|
|
return .{ .context = self };
|
|
}
|
|
};
|
|
}
|
|
|
|
pub fn decompressStream(allocator: mem.Allocator, reader: anytype) !DecompressStream(@TypeOf(reader)) {
|
|
return DecompressStream(@TypeOf(reader)).init(allocator, reader);
|
|
}
|
|
|
|
pub const CompressionLevel = enum(u2) {
|
|
no_compression = 0,
|
|
fastest = 1,
|
|
default = 2,
|
|
maximum = 3,
|
|
};
|
|
|
|
pub const CompressStreamOptions = struct {
|
|
level: CompressionLevel = .default,
|
|
};
|
|
|
|
pub fn CompressStream(comptime WriterType: type) type {
|
|
return struct {
|
|
const Self = @This();
|
|
|
|
const Error = WriterType.Error ||
|
|
deflate.Compressor(WriterType).Error;
|
|
pub const Writer = io.Writer(*Self, Error, write);
|
|
|
|
allocator: mem.Allocator,
|
|
deflator: deflate.Compressor(WriterType),
|
|
in_writer: WriterType,
|
|
hasher: std.hash.Adler32,
|
|
|
|
fn init(allocator: mem.Allocator, dest: WriterType, options: CompressStreamOptions) !Self {
|
|
var header = ZLibHeader{
|
|
.compression_info = ZLibHeader.WINDOW_32K,
|
|
.compression_method = ZLibHeader.DEFLATE,
|
|
.compression_level = @intFromEnum(options.level),
|
|
.preset_dict = 0,
|
|
.checksum = 0,
|
|
};
|
|
header.checksum = @as(u5, @truncate(31 - @as(u16, @bitCast(header)) % 31));
|
|
|
|
try dest.writeInt(u16, @as(u16, @bitCast(header)), .big);
|
|
|
|
const compression_level: deflate.Compression = switch (options.level) {
|
|
.no_compression => .no_compression,
|
|
.fastest => .best_speed,
|
|
.default => .default_compression,
|
|
.maximum => .best_compression,
|
|
};
|
|
|
|
return Self{
|
|
.allocator = allocator,
|
|
.deflator = try deflate.compressor(allocator, dest, .{ .level = compression_level }),
|
|
.in_writer = dest,
|
|
.hasher = std.hash.Adler32.init(),
|
|
};
|
|
}
|
|
|
|
pub fn write(self: *Self, bytes: []const u8) Error!usize {
|
|
if (bytes.len == 0) {
|
|
return 0;
|
|
}
|
|
|
|
const w = try self.deflator.write(bytes);
|
|
|
|
self.hasher.update(bytes[0..w]);
|
|
return w;
|
|
}
|
|
|
|
pub fn writer(self: *Self) Writer {
|
|
return .{ .context = self };
|
|
}
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
self.deflator.deinit();
|
|
}
|
|
|
|
pub fn finish(self: *Self) !void {
|
|
const hash = self.hasher.final();
|
|
try self.deflator.close();
|
|
try self.in_writer.writeInt(u32, hash, .big);
|
|
}
|
|
};
|
|
}
|
|
|
|
pub fn compressStream(allocator: mem.Allocator, writer: anytype, options: CompressStreamOptions) !CompressStream(@TypeOf(writer)) {
|
|
return CompressStream(@TypeOf(writer)).init(allocator, writer, options);
|
|
}
|
|
|
|
fn testDecompress(data: []const u8, expected: []const u8) !void {
|
|
var in_stream = io.fixedBufferStream(data);
|
|
|
|
var zlib_stream = try decompressStream(testing.allocator, in_stream.reader());
|
|
defer zlib_stream.deinit();
|
|
|
|
// Read and decompress the whole file
|
|
const buf = try zlib_stream.reader().readAllAlloc(testing.allocator, std.math.maxInt(usize));
|
|
defer testing.allocator.free(buf);
|
|
|
|
// Check against the reference
|
|
try testing.expectEqualSlices(u8, expected, buf);
|
|
}
|
|
|
|
// All the test cases are obtained by compressing the RFC1951 text
|
|
//
|
|
// https://tools.ietf.org/rfc/rfc1951.txt length=36944 bytes
|
|
// SHA256=5ebf4b5b7fe1c3a0c0ab9aa3ac8c0f3853a7dc484905e76e03b0b0f301350009
|
|
test "compressed data" {
|
|
const rfc1951_txt = @embedFile("testdata/rfc1951.txt");
|
|
|
|
// Compressed with compression level = 0
|
|
try testDecompress(
|
|
@embedFile("testdata/rfc1951.txt.z.0"),
|
|
rfc1951_txt,
|
|
);
|
|
// Compressed with compression level = 9
|
|
try testDecompress(
|
|
@embedFile("testdata/rfc1951.txt.z.9"),
|
|
rfc1951_txt,
|
|
);
|
|
// Compressed with compression level = 9 and fixed Huffman codes
|
|
try testDecompress(
|
|
@embedFile("testdata/rfc1951.txt.fixed.z.9"),
|
|
rfc1951_txt,
|
|
);
|
|
}
|
|
|
|
test "don't read past deflate stream's end" {
|
|
try testDecompress(&[_]u8{
|
|
0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0xc0, 0x00, 0xc1, 0xff,
|
|
0xff, 0x43, 0x30, 0x03, 0x03, 0xc3, 0xff, 0xff, 0xff, 0x01,
|
|
0x83, 0x95, 0x0b, 0xf5,
|
|
}, &[_]u8{
|
|
0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
|
|
0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
|
|
0x00, 0x00, 0xff, 0xff, 0xff,
|
|
});
|
|
}
|
|
|
|
test "sanity checks" {
|
|
// Truncated header
|
|
try testing.expectError(
|
|
error.EndOfStream,
|
|
testDecompress(&[_]u8{0x78}, ""),
|
|
);
|
|
// Failed FCHECK check
|
|
try testing.expectError(
|
|
error.BadHeader,
|
|
testDecompress(&[_]u8{ 0x78, 0x9D }, ""),
|
|
);
|
|
// Wrong CM
|
|
try testing.expectError(
|
|
error.InvalidCompression,
|
|
testDecompress(&[_]u8{ 0x79, 0x94 }, ""),
|
|
);
|
|
// Wrong CINFO
|
|
try testing.expectError(
|
|
error.InvalidWindowSize,
|
|
testDecompress(&[_]u8{ 0x88, 0x98 }, ""),
|
|
);
|
|
// Wrong checksum
|
|
try testing.expectError(
|
|
error.WrongChecksum,
|
|
testDecompress(&[_]u8{ 0x78, 0xda, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 }, ""),
|
|
);
|
|
// Truncated checksum
|
|
try testing.expectError(
|
|
error.EndOfStream,
|
|
testDecompress(&[_]u8{ 0x78, 0xda, 0x03, 0x00, 0x00 }, ""),
|
|
);
|
|
}
|
|
|
|
test "compress data" {
|
|
const allocator = testing.allocator;
|
|
const rfc1951_txt = @embedFile("testdata/rfc1951.txt");
|
|
|
|
for (std.meta.tags(CompressionLevel)) |level| {
|
|
var compressed_data = std.ArrayList(u8).init(allocator);
|
|
defer compressed_data.deinit();
|
|
|
|
var compressor = try compressStream(allocator, compressed_data.writer(), .{ .level = level });
|
|
defer compressor.deinit();
|
|
|
|
try compressor.writer().writeAll(rfc1951_txt);
|
|
try compressor.finish();
|
|
|
|
try testDecompress(compressed_data.items, rfc1951_txt);
|
|
}
|
|
}
|