diff --git a/build.zig b/build.zig index 2299ec0..3bd2c69 100644 --- a/build.zig +++ b/build.zig @@ -1,14 +1,14 @@ const std = @import("std"); 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 allow_lzo = b.option(bool, "allow_lzo", "Compile with lzo support"); - const debug = b.option(bool, "debug", "Enable options to make debugging easier."); + 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") orelse false; + 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 zig_squashfs_options = b.addOptions(); - zig_squashfs_options.addOption(bool, "use_c_libs", use_c_libs_option orelse false); - zig_squashfs_options.addOption(bool, "allow_lzo", allow_lzo orelse false); + zig_squashfs_options.addOption(bool, "use_zig_decomp", use_zig_decomp); + zig_squashfs_options.addOption(bool, "allow_lzo", allow_lzo); const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -16,19 +16,25 @@ pub fn build(b: *std.Build) !void { .root_source_file = b.path("src/root.zig"), .target = target, .optimize = if (debug == true) .Debug else optimize, - .link_libc = use_c_libs_option, + .link_libc = !use_zig_decomp, .valgrind = debug, .error_tracing = debug, .strip = if (debug == true) false else null, }); mod.addOptions("config", zig_squashfs_options); - if (use_c_libs_option == true) { - mod.linkSystemLibrary("zlib-ng", .{ .preferred_link_mode = .static }); + if (!use_zig_decomp) { + var zlib_ng = b.dependency("zlib_ng", .{}); + mod.linkLibrary(zlib_ng.artifact("zng")); + mod.linkSystemLibrary("lzma", .{ .preferred_link_mode = .static }); if (allow_lzo == true) 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"; @@ -44,7 +50,7 @@ pub fn build(b: *std.Build) !void { .root_source_file = b.path("src/bin/unsquashfs.zig"), .target = target, .optimize = if (debug == true) .Debug else optimize, - .link_libc = use_c_libs_option, + .link_libc = !use_zig_decomp, .imports = &.{ .{ .name = "zig_squashfs", .module = mod }, }, diff --git a/build.zig.zon b/build.zig.zon index fb60d72..2287975 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,7 +3,20 @@ .version = "0.0.6", .fingerprint = 0x37ba29474b87f145, // Changing this has security and trust implications. .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 = .{ "build.zig", "build.zig.zon", diff --git a/src/archive.zig b/src/archive.zig index f053e0b..1285435 100644 --- a/src/archive.zig +++ b/src/archive.zig @@ -18,7 +18,7 @@ const OffsetFile = @import("util/offset_file.zig"); const XattrTable = @import("xattr.zig"); const config = if (builtin.is_test) .{ - .use_c_libs = true, + .use_zig_decomp = !builtin.link_libc, .allow_lzo = false, } else @import("config"); @@ -45,8 +45,8 @@ pub fn init(alloc: std.mem.Allocator, fil: File, offset: u64) !Archive { .lzma => Decomp.lzmaDecompress, .xz => Decomp.xzDecompress, .zstd => Decomp.zstdDecompress, - .lz4 => if (config.use_c_libs) Decomp.cLz4 else return error.Lz4Unsupported, - .lzo => if (config.use_c_libs and config.allow_lzo) Decomp.lzoDecompress else return error.LzoUnsupported, + .lz4 => if (!config.use_zig_decomp) Decomp.cLz4 else return error.Lz4Unsupported, + .lzo => if (!config.use_zig_decomp and config.allow_lzo) Decomp.lzoDecompress else return error.LzoUnsupported, }; return .{ .alloc = alloc, diff --git a/src/decomp.zig b/src/decomp.zig index 0928e09..c079b95 100644 --- a/src/decomp.zig +++ b/src/decomp.zig @@ -6,7 +6,7 @@ const Reader = std.Io.Reader; const builtin = @import("builtin"); 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 } 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 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 { 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 { _ = alloc; 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) { c.Z_OK => out_len, 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 { 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 { // _ = 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 { 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 { // _ = alloc; @@ -176,7 +176,7 @@ pub fn cLz4(alloc: std.mem.Allocator, in: []u8, out: []u8) anyerror!usize { 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 { var rdr: Reader = .fixed(in);