Files
zig-squashfs/src/decomp.zig
T
Caleb J. Gardner 3e97aabe53 Fleshing out more of the library.
Mainly adding functions to File
2026-04-07 15:41:30 -05:00

32 lines
996 B
Zig

const std = @import("std");
pub const StatelessDecomp = *const fn (std.mem.Allocator, in: []u8, out: []u8) Error!usize;
pub const Error = error{
OutOfMemory,
EndOfStream,
ReadFailed,
WriteFailed,
};
const Decompressor = @This();
alloc: std.mem.Allocator = std.heap.smp_allocator,
vtable: *const struct {
decompress: *const fn (*const Decompressor, in: []u8, out: []u8) Error!usize = defaultDecompress,
stateless: StatelessDecomp,
},
/// Create a copy of the decompressor using it's stateless function and the new allocator.
pub fn statelessCopy(self: Decompressor, alloc: std.mem.Allocator) Decompressor {
return &.{ .alloc = alloc, .vtable = &.{ .stateless = self.vtable.stateless } };
}
pub fn decompress(self: *const Decompressor, in: []u8, out: []u8) Error!usize {
return self.vtable.decompress(self, in, out);
}
fn defaultDecompress(self: *const Decompressor, in: []u8, out: []u8) Error!usize {
return self.vtable.stateless(self.alloc, in, out);
}