Use zig packed versions of zlib-ng, lz4, and zstd.

Changed use_c_libs to use_zig_decomp so c libraries are now default
This commit is contained in:
Caleb J. Gardner
2026-03-18 05:24:58 -05:00
parent 8b8c9a772f
commit 50cae8b63d
4 changed files with 42 additions and 23 deletions
+17 -11
View File
@@ -1,14 +1,14 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.Build) !void { pub fn build(b: *std.Build) !void {
const use_c_libs_option = b.option(bool, "use_c_libs", "Use C versions of decompression libraries instead of the Zig standard library ones"); const use_zig_decomp = b.option(bool, "use_zig_decomp", "Use zig standard library for decompression.") orelse false;
const allow_lzo = b.option(bool, "allow_lzo", "Compile with lzo support"); const allow_lzo = b.option(bool, "allow_lzo", "Compile with lzo support") orelse false;
const debug = b.option(bool, "debug", "Enable options to make debugging easier."); const debug = b.option(bool, "debug", "Enable options to make debugging easier.") orelse false;
const version_string_option = b.option([]const u8, "version", "Version of the library/binary"); const version_string_option = b.option([]const u8, "version", "Version of the library/binary");
const zig_squashfs_options = b.addOptions(); const zig_squashfs_options = b.addOptions();
zig_squashfs_options.addOption(bool, "use_c_libs", use_c_libs_option orelse false); zig_squashfs_options.addOption(bool, "use_zig_decomp", use_zig_decomp);
zig_squashfs_options.addOption(bool, "allow_lzo", allow_lzo orelse false); zig_squashfs_options.addOption(bool, "allow_lzo", allow_lzo);
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
@@ -16,19 +16,25 @@ pub fn build(b: *std.Build) !void {
.root_source_file = b.path("src/root.zig"), .root_source_file = b.path("src/root.zig"),
.target = target, .target = target,
.optimize = if (debug == true) .Debug else optimize, .optimize = if (debug == true) .Debug else optimize,
.link_libc = use_c_libs_option, .link_libc = !use_zig_decomp,
.valgrind = debug, .valgrind = debug,
.error_tracing = debug, .error_tracing = debug,
.strip = if (debug == true) false else null, .strip = if (debug == true) false else null,
}); });
mod.addOptions("config", zig_squashfs_options); mod.addOptions("config", zig_squashfs_options);
if (use_c_libs_option == true) { if (!use_zig_decomp) {
mod.linkSystemLibrary("zlib-ng", .{ .preferred_link_mode = .static }); var zlib_ng = b.dependency("zlib_ng", .{});
mod.linkLibrary(zlib_ng.artifact("zng"));
mod.linkSystemLibrary("lzma", .{ .preferred_link_mode = .static }); mod.linkSystemLibrary("lzma", .{ .preferred_link_mode = .static });
if (allow_lzo == true) if (allow_lzo == true)
mod.linkSystemLibrary("minilzo", .{ .preferred_link_mode = .static }); mod.linkSystemLibrary("minilzo", .{ .preferred_link_mode = .static });
mod.linkSystemLibrary("lz4", .{ .preferred_link_mode = .static });
mod.linkSystemLibrary("zstd", .{ .preferred_link_mode = .static }); var lz4 = b.dependency("lz4", .{});
mod.linkLibrary(lz4.artifact("lz4"));
var zstd = b.dependency("zstd", .{});
mod.linkLibrary(zstd.artifact("zstd"));
} }
var version = version_string_option orelse "0.0.0-testing"; var version = version_string_option orelse "0.0.0-testing";
@@ -44,7 +50,7 @@ pub fn build(b: *std.Build) !void {
.root_source_file = b.path("src/bin/unsquashfs.zig"), .root_source_file = b.path("src/bin/unsquashfs.zig"),
.target = target, .target = target,
.optimize = if (debug == true) .Debug else optimize, .optimize = if (debug == true) .Debug else optimize,
.link_libc = use_c_libs_option, .link_libc = !use_zig_decomp,
.imports = &.{ .imports = &.{
.{ .name = "zig_squashfs", .module = mod }, .{ .name = "zig_squashfs", .module = mod },
}, },
+14 -1
View File
@@ -3,7 +3,20 @@
.version = "0.0.6", .version = "0.0.6",
.fingerprint = 0x37ba29474b87f145, // Changing this has security and trust implications. .fingerprint = 0x37ba29474b87f145, // Changing this has security and trust implications.
.minimum_zig_version = "0.15.2", .minimum_zig_version = "0.15.2",
.dependencies = .{}, .dependencies = .{
.zlib_ng = .{
.url = "git+https://github.com/CalebQ42/zig-zlib-ng#8988c0850be6ad54ef53b8d85f323825b9fcb3ae",
.hash = "zlib_ng-2.3.3-pre1-2HYS4H5CAAD1PdTeOOZDvpLMXZzg0-4aBRUVdtq8LUXu",
},
.zstd = .{
.url = "git+https://github.com/allyourcodebase/zstd.git?ref=1.5.7-1#e1a501be57f42c541e8a5597e4b59a074dfd09a3",
.hash = "zstd-1.5.7-1-KEItkAMwAAD6OKY3m0OOmXG7aL-aLUfrDqbP5J5oYapU",
},
.lz4 = .{
.url = "git+https://github.com/allyourcodebase/lz4.git?ref=1.10.0-6#41f52ab227caf9d48cf88c89a4d2946caa12b102",
.hash = "lz4-1.10.0-6-ewyzw-4NAAAWDpY4xpiqr4LQhZQAC0x_rGnW2iPh6jk2",
},
},
.paths = .{ .paths = .{
"build.zig", "build.zig",
"build.zig.zon", "build.zig.zon",
+3 -3
View File
@@ -18,7 +18,7 @@ const OffsetFile = @import("util/offset_file.zig");
const XattrTable = @import("xattr.zig"); const XattrTable = @import("xattr.zig");
const config = if (builtin.is_test) .{ const config = if (builtin.is_test) .{
.use_c_libs = true, .use_zig_decomp = !builtin.link_libc,
.allow_lzo = false, .allow_lzo = false,
} else @import("config"); } else @import("config");
@@ -45,8 +45,8 @@ pub fn init(alloc: std.mem.Allocator, fil: File, offset: u64) !Archive {
.lzma => Decomp.lzmaDecompress, .lzma => Decomp.lzmaDecompress,
.xz => Decomp.xzDecompress, .xz => Decomp.xzDecompress,
.zstd => Decomp.zstdDecompress, .zstd => Decomp.zstdDecompress,
.lz4 => if (config.use_c_libs) Decomp.cLz4 else return error.Lz4Unsupported, .lz4 => if (!config.use_zig_decomp) Decomp.cLz4 else return error.Lz4Unsupported,
.lzo => if (config.use_c_libs and config.allow_lzo) Decomp.lzoDecompress else return error.LzoUnsupported, .lzo => if (!config.use_zig_decomp and config.allow_lzo) Decomp.lzoDecompress else return error.LzoUnsupported,
}; };
return .{ return .{
.alloc = alloc, .alloc = alloc,
+8 -8
View File
@@ -6,7 +6,7 @@ const Reader = std.Io.Reader;
const builtin = @import("builtin"); const builtin = @import("builtin");
const config = if (builtin.is_test) .{ const config = if (builtin.is_test) .{
.use_c_libs = builtin.link_libc == true, .use_zig_decomp = !builtin.link_libc,
.allow_lzo = false, // Change once LZO compilation is fixed .allow_lzo = false, // Change once LZO compilation is fixed
} else @import("config"); } else @import("config");
@@ -31,7 +31,7 @@ pub const CompressionType = enum(u16) {
pub const DecompFn = *const fn (alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize; // TODO: replace anyerror to definitive error types. pub const DecompFn = *const fn (alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize; // TODO: replace anyerror to definitive error types.
pub const gzipDecompress = if (config.use_c_libs) cGzip else zigGzip; pub const gzipDecompress = if (!config.use_zig_decomp) cGzip else zigGzip;
fn zigGzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { fn zigGzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); var rdr: Reader = .fixed(in);
@@ -43,7 +43,7 @@ fn zigGzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
fn cGzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { fn cGzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
_ = alloc; _ = alloc;
var out_len: usize = out.len; var out_len: usize = out.len;
const res = c.zng_uncompress2(out.ptr, &out_len, in.ptr, in.len); const res = c.zng_uncompress(out.ptr, &out_len, in.ptr, in.len);
return switch (res) { return switch (res) {
c.Z_OK => out_len, c.Z_OK => out_len,
c.Z_MEM_ERROR => error.NotEnoughMemory, c.Z_MEM_ERROR => error.NotEnoughMemory,
@@ -53,7 +53,7 @@ fn cGzip(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
}; };
} }
pub const lzmaDecompress = if (config.use_c_libs) cLzma else zigLzma; pub const lzmaDecompress = if (!config.use_zig_decomp) cLzma else zigLzma;
fn zigLzma(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { fn zigLzma(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); var rdr: Reader = .fixed(in);
@@ -90,7 +90,7 @@ fn cLzma(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
}; };
} }
// pub const lzoDecompress = if (config.use_c_libs) cLzo else zigLzo; // pub const lzoDecompress = if (!config.use_zig_decomp) cLzo else zigLzo;
// fn zigLzo(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { // fn zigLzo(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
// _ = alloc; // _ = alloc;
@@ -123,7 +123,7 @@ pub fn cLzo(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
}; };
} }
pub const xzDecompress = if (config.use_c_libs) cXz else zigXz; pub const xzDecompress = if (!config.use_zig_decomp) cXz else zigXz;
fn zigXz(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { fn zigXz(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); var rdr: Reader = .fixed(in);
@@ -161,7 +161,7 @@ fn cXz(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
}; };
} }
// pub const lz4Decompress = if (config.use_c_libs) cLz4 else zigLz4; // pub const lz4Decompress = if (!config.use_zig_decomp) cLz4 else zigLz4;
// fn zigLz4(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { // fn zigLz4(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
// _ = alloc; // _ = alloc;
@@ -176,7 +176,7 @@ pub fn cLz4(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
return error.Lz4DecompressFailed; return error.Lz4DecompressFailed;
} }
pub const zstdDecompress = if (config.use_c_libs) cZstd else zigZstd; pub const zstdDecompress = if (!config.use_zig_decomp) cZstd else zigZstd;
pub fn zigZstd(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { pub fn zigZstd(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize {
var rdr: Reader = .fixed(in); var rdr: Reader = .fixed(in);