From a1b9828578a073e13fbcc27f12e1392b11a6d90c Mon Sep 17 00:00:00 2001 From: "Caleb J. Gardner" Date: Thu, 26 Mar 2026 06:40:17 -0500 Subject: [PATCH] Finished (?) decompression restructuring --- src/decomp.zig | 3 +++ src/decomp/gzip.zig | 1 + src/decomp/misc_c.zig | 39 ++++++++++++++++++++++----------------- src/decomp/zig_decomp.zig | 18 +++++++++++++++++- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/decomp.zig b/src/decomp.zig index 79cb77d..e8cfcd3 100644 --- a/src/decomp.zig +++ b/src/decomp.zig @@ -6,6 +6,9 @@ pub const Error = error{ OutOfMemory, BadInput, OutputTooSmall, + ReadFailed, + WriteFailed, + EndOfStream, }; vtable: *const struct { diff --git a/src/decomp/gzip.zig b/src/decomp/gzip.zig index 3249f47..c87301b 100644 --- a/src/decomp/gzip.zig +++ b/src/decomp/gzip.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const Reader = std.Io.Reader; const builtin = @import("builtin"); const Decompressor = @import("../decomp.zig"); diff --git a/src/decomp/misc_c.zig b/src/decomp/misc_c.zig index 76f7997..06d3d37 100644 --- a/src/decomp/misc_c.zig +++ b/src/decomp/misc_c.zig @@ -3,7 +3,9 @@ const std = @import("std"); const Decompressor = @import("../decomp.zig"); const c = @import("c.zig").c; -pub const LzoDecompressor = struct { interface: Decompressor = .{ .vtable = .{ .stateless = lzo } } }; +pub const Lzo = struct { + interface: Decompressor = .{ .vtable = &.{ .stateless = lzo } }, +}; fn lzo(_: std.mem.Allocator, in: []u8, out: []u8) Decompressor.Error!usize { var res = c.lzo_init(); @@ -13,26 +15,29 @@ fn lzo(_: std.mem.Allocator, in: []u8, out: []u8) Decompressor.Error!usize { return switch (res) { c.LZO_E_OK => out_len, - c.LZO_E_ERROR => error.LzoError, c.LZO_E_OUT_OF_MEMORY => Decompressor.Error.OutOfMemory, - c.LZO_E_NOT_COMPRESSIBLE => error.LzoNotCompressible, - c.LZO_E_INPUT_OVERRUN => error.LzoInputOverrun, - c.LZO_E_OUTPUT_OVERRUN => error.LzoOutputOverrun, - c.LZO_E_LOOKBEHIND_OVERRUN => error.LzoLookbehindOverrun, - c.LZO_E_EOF_NOT_FOUND => error.LzoEofNotFound, - c.LZO_E_INPUT_NOT_CONSUMED => Decompressor.Error.OutputTooSmall, - c.LZO_E_NOT_YET_IMPLEMENTED => error.LzoNotYetImplemented, - c.LZO_E_INVALID_ARGUMENT => error.LzoInvalidArgument, - c.LZO_E_INVALID_ALIGNMENT => error.LzoInvalidAlignment, - c.LZO_E_OUTPUT_NOT_CONSUMED, c.LZO_E_OUTPUT_OVERRUN => Decompressor.Error.OutputTooSmall, - else => error.UnknownResult, + c.LZO_E_ERROR, + c.LZO_E_INPUT_OVERRUN, + c.LZO_E_LOOKBEHIND_OVERRUN, + c.LZO_E_EOF_NOT_FOUND, + c.LZO_E_NOT_YET_IMPLEMENTED, + c.LZO_E_INVALID_ARGUMENT, + c.LZO_E_INVALID_ALIGNMENT, + => Decompressor.Error.BadInput, + c.LZO_E_INPUT_NOT_CONSUMED, + c.LZO_E_OUTPUT_NOT_CONSUMED, + c.LZO_E_OUTPUT_OVERRUN, + => Decompressor.Error.OutputTooSmall, + else => Decompressor.Error.BadInput, }; } -pub const Lz4Decompressor = struct { interface: Decompressor = .{ .vtable = .{ .stateless = lz4 } } }; +pub const Lz4 = struct { + interface: Decompressor = .{ .vtable = &.{ .stateless = lz4 } }, +}; -fn lz4(_: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { +fn lz4(_: std.mem.Allocator, in: []u8, out: []u8) Decompressor.Error!usize { const res = c.LZ4_decompress_safe(in.ptr, out.ptr, @intCast(in.len), @intCast(out.len)); - if (res > 0) return @abs(res); // TODO: Find out what error values it can return. - return error.Lz4DecompressFailed; + if (res > 0) return @abs(res); + return Decompressor.Error.BadInput; // TODO: Find out what error values it can return. } diff --git a/src/decomp/zig_decomp.zig b/src/decomp/zig_decomp.zig index aba437d..8a20883 100644 --- a/src/decomp/zig_decomp.zig +++ b/src/decomp/zig_decomp.zig @@ -2,7 +2,11 @@ const std = @import("std"); const Reader = std.Io.Reader; const builtin = @import("builtin"); -pub const DecompFn = *const fn (alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize; // TODO: replace anyerror to definitive error types. +const Decompressor = @import("../decomp.zig"); + +pub const Gzip = struct { + interface: Decompressor = .{ .vtable = &.{ .stateless = gzip } }, +}; pub fn gzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { var rdr: Reader = .fixed(in); @@ -12,18 +16,30 @@ pub fn gzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { return decomp.reader.readSliceShort(out); } +pub const Lzma = struct { + interface: Decompressor = .{ .vtable = &.{ .stateless = lzma } }, +}; + pub fn lzma(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { var rdr: Reader = .fixed(in); var decomp = try std.compress.lzma.decompress(alloc, rdr.adaptToOldInterface()); return decomp.read(out); } +pub const Xz = struct { + interface: Decompressor = .{ .vtable = &.{ .stateless = xz } }, +}; + pub fn xz(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { var rdr: Reader = .fixed(in); var decomp = try std.compress.xz.decompress(alloc, rdr.adaptToOldInterface()); return decomp.read(out); } +pub const Zstd = struct { + interface: Decompressor = .{ .vtable = &.{ .stateless = zstd } }, +}; + pub fn zstd(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { var rdr: Reader = .fixed(in); const buf = try alloc.alloc(u8, 1024 * 1024);