union Hasher [src]

Fields

raw: void
gzip: struct { crc: std.hash.Crc32 = .init(), count: u32 = 0, }
zlib: std.hash.Adler32

Members

Source

pub const Hasher = union(Container) { raw: void, gzip: struct { crc: std.hash.Crc32 = .init(), count: u32 = 0, }, zlib: std.hash.Adler32, pub fn init(containter: Container) Hasher { return switch (containter) { .gzip => .{ .gzip = .{} }, .zlib => .{ .zlib = .{} }, .raw => .raw, }; } pub fn container(h: Hasher) Container { return h; } pub fn update(h: *Hasher, buf: []const u8) void { switch (h.*) { .raw => {}, .gzip => |*gzip| { gzip.update(buf); gzip.count +%= buf.len; }, .zlib => |*zlib| { zlib.update(buf); }, inline .gzip, .zlib => |*x| x.update(buf), } } pub fn writeFooter(hasher: *Hasher, writer: *std.Io.Writer) std.Io.Writer.Error!void { var bits: [4]u8 = undefined; switch (hasher.*) { .gzip => |*gzip| { // GZIP 8 bytes footer // - 4 bytes, CRC32 (CRC-32) // - 4 bytes, ISIZE (Input SIZE) - size of the original (uncompressed) input data modulo 2^32 std.mem.writeInt(u32, &bits, gzip.final(), .little); try writer.writeAll(&bits); std.mem.writeInt(u32, &bits, gzip.bytes_read, .little); try writer.writeAll(&bits); }, .zlib => |*zlib| { // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952). // 4 bytes of ADLER32 (Adler-32 checksum) // Checksum value of the uncompressed data (excluding any // dictionary data) computed according to Adler-32 // algorithm. std.mem.writeInt(u32, &bits, zlib.final, .big); try writer.writeAll(&bits); }, .raw => {}, } } }