Finished (?) decompression restructuring

This commit is contained in:
Caleb J. Gardner
2026-03-26 06:40:17 -05:00
parent 8e4661c4c6
commit a1b9828578
4 changed files with 43 additions and 18 deletions
+3
View File
@@ -6,6 +6,9 @@ pub const Error = error{
OutOfMemory, OutOfMemory,
BadInput, BadInput,
OutputTooSmall, OutputTooSmall,
ReadFailed,
WriteFailed,
EndOfStream,
}; };
vtable: *const struct { vtable: *const struct {
+1
View File
@@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const Reader = std.Io.Reader;
const builtin = @import("builtin"); const builtin = @import("builtin");
const Decompressor = @import("../decomp.zig"); const Decompressor = @import("../decomp.zig");
+22 -17
View File
@@ -3,7 +3,9 @@ const std = @import("std");
const Decompressor = @import("../decomp.zig"); const Decompressor = @import("../decomp.zig");
const c = @import("c.zig").c; 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 { fn lzo(_: std.mem.Allocator, in: []u8, out: []u8) Decompressor.Error!usize {
var res = c.lzo_init(); var res = c.lzo_init();
@@ -13,26 +15,29 @@ fn lzo(_: std.mem.Allocator, in: []u8, out: []u8) Decompressor.Error!usize {
return switch (res) { return switch (res) {
c.LZO_E_OK => out_len, c.LZO_E_OK => out_len,
c.LZO_E_ERROR => error.LzoError,
c.LZO_E_OUT_OF_MEMORY => Decompressor.Error.OutOfMemory, c.LZO_E_OUT_OF_MEMORY => Decompressor.Error.OutOfMemory,
c.LZO_E_NOT_COMPRESSIBLE => error.LzoNotCompressible, c.LZO_E_ERROR,
c.LZO_E_INPUT_OVERRUN => error.LzoInputOverrun, c.LZO_E_INPUT_OVERRUN,
c.LZO_E_OUTPUT_OVERRUN => error.LzoOutputOverrun, c.LZO_E_LOOKBEHIND_OVERRUN,
c.LZO_E_LOOKBEHIND_OVERRUN => error.LzoLookbehindOverrun, c.LZO_E_EOF_NOT_FOUND,
c.LZO_E_EOF_NOT_FOUND => error.LzoEofNotFound, c.LZO_E_NOT_YET_IMPLEMENTED,
c.LZO_E_INPUT_NOT_CONSUMED => Decompressor.Error.OutputTooSmall, c.LZO_E_INVALID_ARGUMENT,
c.LZO_E_NOT_YET_IMPLEMENTED => error.LzoNotYetImplemented, c.LZO_E_INVALID_ALIGNMENT,
c.LZO_E_INVALID_ARGUMENT => error.LzoInvalidArgument, => Decompressor.Error.BadInput,
c.LZO_E_INVALID_ALIGNMENT => error.LzoInvalidAlignment, c.LZO_E_INPUT_NOT_CONSUMED,
c.LZO_E_OUTPUT_NOT_CONSUMED, c.LZO_E_OUTPUT_OVERRUN => Decompressor.Error.OutputTooSmall, c.LZO_E_OUTPUT_NOT_CONSUMED,
else => error.UnknownResult, 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)); 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. if (res > 0) return @abs(res);
return error.Lz4DecompressFailed; return Decompressor.Error.BadInput; // TODO: Find out what error values it can return.
} }
+17 -1
View File
@@ -2,7 +2,11 @@ const std = @import("std");
const Reader = std.Io.Reader; const Reader = std.Io.Reader;
const builtin = @import("builtin"); 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 { pub fn gzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); 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); 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 { pub fn lzma(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); var rdr: Reader = .fixed(in);
var decomp = try std.compress.lzma.decompress(alloc, rdr.adaptToOldInterface()); var decomp = try std.compress.lzma.decompress(alloc, rdr.adaptToOldInterface());
return decomp.read(out); 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 { pub fn xz(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); var rdr: Reader = .fixed(in);
var decomp = try std.compress.xz.decompress(alloc, rdr.adaptToOldInterface()); var decomp = try std.compress.xz.decompress(alloc, rdr.adaptToOldInterface());
return decomp.read(out); 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 { pub fn zstd(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); var rdr: Reader = .fixed(in);
const buf = try alloc.alloc(u8, 1024 * 1024); const buf = try alloc.alloc(u8, 1024 * 1024);