Compare commits

...

10 Commits

Author SHA1 Message Date
17144a7981 fix for 0.15 master and tested 2025-05-15 15:10:56 -05:00
b242ec16f3 maybe this'll do? 2024-11-04 20:53:13 -05:00
1369fae820 replace git ignore 2024-11-04 20:41:15 -05:00
d4613bb2ca fixes 2024-11-04 20:39:37 -05:00
Igor Anić
5d231649a9 fix std.mem.copy rename 2024-01-04 22:06:05 +01:00
Igor Anić
24137e1045 add one more test in example 2023-12-31 15:21:57 +01:00
Igor Anić
c00e95736e fix example to use latest library 2023-12-31 15:21:33 +01:00
Igor Anić
a3d6d97bd1 fix build with newer Zig var => const 2023-12-31 15:16:48 +01:00
Igor Anić
be1bc42cb8 update example 2023-11-12 10:28:39 +01:00
Igor Anić
6fb8ed90ba fix build error on Linux 2023-11-12 10:26:34 +01:00
12 changed files with 102 additions and 108 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
zig-cache/
.zig-cache/
zig-out/
.vscode

View File

@ -1,37 +1,30 @@
const std = @import("std");
const zlib = @import("zlib.zig");
pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
pub fn build(b: *std.Build) void {
// Modules available to downstream dependencies
_ = b.addModule("zlib", .{
.source_file = .{ .path = (comptime thisDir()) ++ "/src/main.zig" },
const zlib = b.addModule("zlib", .{
.root_source_file = b.path("src/main.zig"),
.link_libc = true,
});
const lib = zlib.create(b, target, optimize);
b.installArtifact(lib.step);
const srcs = &.{
"zlib/adler32.c",
"zlib/compress.c",
"zlib/crc32.c",
"zlib/deflate.c",
"zlib/gzclose.c",
"zlib/gzlib.c",
"zlib/gzread.c",
"zlib/gzwrite.c",
"zlib/inflate.c",
"zlib/infback.c",
"zlib/inftrees.c",
"zlib/inffast.c",
"zlib/trees.c",
"zlib/uncompr.c",
"zlib/zutil.c",
};
const tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
});
lib.link(tests, .{});
const test_step = b.step("test", "Run tests");
test_step.dependOn(&tests.step);
const bin = b.addExecutable(.{
.name = "example1",
.root_source_file = .{ .path = "example/example1.zig" },
.target = target,
.optimize = optimize,
});
lib.link(bin, .{ .import_name = "zlib" });
b.installArtifact(bin);
}
/// Path to the directory with the build.zig.
fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse unreachable;
zlib.addCSourceFiles(.{ .files = srcs, .flags = &.{"-std=c89"} });
zlib.addIncludePath(b.path("zlib/"));
}

12
build.zig.zon Normal file
View File

@ -0,0 +1,12 @@
.{
.name = "zlib",
.version = "0.1.0",
.dependencies = .{},
.paths = .{
"readme.md",
"build.zig",
"build.zig.zon",
"src",
"zlib",
},
}

View File

View File

@ -0,0 +1,11 @@
pub const packages = struct {
pub const @"1220d624669f633ebe6c7afaba1a702c05c4c8e55a57b77a4d0d6ab1e3da07db11e2" = struct {
pub const build_root = "/home/rain/.cache/zig/p/1220d624669f633ebe6c7afaba1a702c05c4c8e55a57b77a4d0d6ab1e3da07db11e2";
pub const build_zig = @import("1220d624669f633ebe6c7afaba1a702c05c4c8e55a57b77a4d0d6ab1e3da07db11e2");
pub const deps: []const struct { []const u8, []const u8 } = &.{};
};
};
pub const root_deps: []const struct { []const u8, []const u8 } = &.{
.{ "zlib", "1220d624669f633ebe6c7afaba1a702c05c4c8e55a57b77a4d0d6ab1e3da07db11e2" },
};

View File

@ -67,8 +67,13 @@ pub fn build(b: *std.Build) void {
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
.link_libc = true,
});
unit_tests.linkLibrary(b.dependency("zlib", .{
.target = target,
.optimize = optimize,
}).artifact("z"));
unit_tests.addModule("zlib", zlib.module("zlib"));
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to

View File

@ -3,8 +3,8 @@
.version = "0.1.0",
.dependencies = .{
.zlib = .{
.url = "https://github.com/ianic/zig-zlib/archive/8be486b673aaadf7c52fafe7b28d5e21bf1fb57d.tar.gz",
.hash = "1220ddcca2206a6257c671bedb3ff50d3263de12d044db667d2a2ed0ccecf0910d5c",
.url = "https://github.com/ianic/zig-zlib/archive/a3d6d97bd1bfd027e758d0d6eb36535da8f1d5a7.tar.gz",
.hash = "1220d624669f633ebe6c7afaba1a702c05c4c8e55a57b77a4d0d6ab1e3da07db11e2",
},
},
.paths = .{""},

View File

@ -1,5 +1,6 @@
const std = @import("std");
const zlib = @import("zlib");
const testing = std.testing;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -27,3 +28,33 @@ test "simple test" {
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
test "zlib compress/decompress" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
//const allocator = testing.allocator;
const input = "Hello";
var cmp = try zlib.Compressor.init(allocator, .{ .header = .none });
defer cmp.deinit();
const compressed = try cmp.compressAllAlloc(input);
defer allocator.free(compressed);
showBuf(compressed);
var dcmp = try zlib.Decompressor.init(allocator, .{ .header = .none });
defer dcmp.deinit();
const decompressed = try dcmp.decompressAllAlloc(compressed);
defer allocator.free(decompressed);
try testing.expectEqualSlices(u8, input, decompressed);
}
// debug helper
fn showBuf(buf: []const u8) void {
std.debug.print("\n", .{});
for (buf) |b|
std.debug.print("0x{x:0>2}, ", .{b});
std.debug.print("\n", .{});
}

View File

@ -65,7 +65,7 @@ fn zalloc(private: ?*anyopaque, items: c_uint, size: c_uint) callconv(.C) ?*anyo
return null;
const allocator: *Allocator = @ptrCast(@alignCast(private.?));
var buf = allocator.allocWithOptions(u8, ZallocHeader.size_of_aligned + (items * size), @alignOf(*ZallocHeader), null) catch return null;
var buf = allocator.allocWithOptions(u8, ZallocHeader.size_of_aligned + (items * size), std.mem.Alignment.of(*ZallocHeader), null) catch return null;
const header: *ZallocHeader = @ptrCast(@alignCast(buf.ptr));
header.* = .{
.magic = magic_value,
@ -88,7 +88,7 @@ fn zfree(private: ?*anyopaque, addr: ?*anyopaque) callconv(.C) void {
}
var buf: []align(alignment) u8 = undefined;
buf.ptr = @as([*]align(alignment) u8, @ptrCast(header));
buf.ptr = @as([*]align(alignment) u8, @ptrCast(@alignCast(header)));
buf.len = ZallocHeader.size_of_aligned + header.size;
allocator.free(buf);
}
@ -145,7 +145,7 @@ pub const CompressorOptions = struct {
const Self = @This();
pub fn windowSize(self: Self) i6 {
var ws = @as(i6, if (self.window_size < 9) 9 else self.window_size);
const ws = @as(i6, if (self.window_size < 9) 9 else self.window_size);
return switch (self.header) {
.zlib => ws,
.none, .ws => -@as(i6, ws),
@ -165,7 +165,7 @@ pub fn CompressorWriter(comptime WriterType: type) type {
const Writer = std.io.Writer(*Self, WriterError, write);
pub fn init(allocator: Allocator, inner_writer: WriterType, opt: CompressorOptions) !Self {
var stream = try zStreamInit(allocator);
const stream = try zStreamInit(allocator);
errdefer zStreamDeinit(allocator, stream);
try checkRC(c.deflateInit2(
@ -190,7 +190,7 @@ pub fn CompressorWriter(comptime WriterType: type) type {
while (true) {
self.stream.next_out = &tmp;
self.stream.avail_out = tmp.len;
var rc = c.deflate(self.stream, c.Z_FINISH);
const rc = c.deflate(self.stream, c.Z_FINISH);
if (rc != c.Z_STREAM_END)
return errorFromInt(rc);
@ -213,7 +213,7 @@ pub fn CompressorWriter(comptime WriterType: type) type {
// compressed
self.stream.next_out = &tmp;
self.stream.avail_out = tmp.len;
var rc = c.deflate(self.stream, c.Z_PARTIAL_FLUSH);
const rc = c.deflate(self.stream, c.Z_PARTIAL_FLUSH);
if (rc != c.Z_OK)
return errorFromInt(rc);
@ -246,7 +246,7 @@ pub const DecompressorOptions = struct {
const Self = @This();
pub fn windowSize(self: Self) i5 {
var window_size = if (self.window_size < 8) 15 else self.window_size;
const window_size = if (self.window_size < 8) 15 else self.window_size;
return if (self.header == .none or self.header == .ws) -@as(i5, window_size) else window_size;
}
};
@ -264,7 +264,7 @@ pub fn DecompressorReader(comptime ReaderType: type) type {
const Reader = std.io.Reader(*Self, ReaderError, read);
pub fn init(allocator: Allocator, inner_reader: ReaderType, options: DecompressorOptions) !Self {
var stream = try zStreamInit(allocator);
const stream = try zStreamInit(allocator);
errdefer zStreamDeinit(allocator, stream);
const rc = c.inflateInit2(stream, options.windowSize());
@ -297,13 +297,13 @@ pub fn DecompressorReader(comptime ReaderType: type) type {
self.stream.next_out = @as([*]u8, @ptrFromInt(@intFromPtr(buf.ptr)));
self.stream.avail_out = @as(c_uint, @intCast(buf.len));
var rc = c.inflate(self.stream, c.Z_SYNC_FLUSH);
const rc = c.inflate(self.stream, c.Z_SYNC_FLUSH);
if (rc != c.Z_OK and rc != c.Z_STREAM_END)
return errorFromInt(rc);
if (self.stream.avail_in != 0) {
const done_pos = self.pos - self.stream.avail_in;
std.mem.copy(u8, self.tmp[0..], self.tmp[done_pos..]);
std.mem.copyForwards(u8, self.tmp[0..], self.tmp[done_pos..]);
self.pos = self.tmp[done_pos..].len;
}
@ -324,7 +324,7 @@ pub const Compressor = struct {
const Self = @This();
pub fn init(allocator: Allocator, opt: CompressorOptions) !Self {
var stream = try zStreamInit(allocator);
const stream = try zStreamInit(allocator);
errdefer zStreamDeinit(allocator, stream);
try checkRC(c.deflateInit2(
stream,
@ -361,11 +361,11 @@ pub const Compressor = struct {
var flag = c.Z_PARTIAL_FLUSH;
while (true) {
var out = tmp[len..];
const out = tmp[len..];
self.stream.next_out = @as([*]u8, @ptrFromInt(@intFromPtr(out.ptr)));
self.stream.avail_out = @as(c_uint, @intCast(out.len));
var rc = c.deflate(self.stream, flag);
const rc = c.deflate(self.stream, flag);
if (rc != c.Z_OK and rc != c.Z_STREAM_END)
return errorFromInt(rc);
@ -396,7 +396,7 @@ pub const Decompressor = struct {
const Self = @This();
pub fn init(allocator: Allocator, options: DecompressorOptions) !Self {
var stream = try zStreamInit(allocator);
const stream = try zStreamInit(allocator);
errdefer zStreamDeinit(allocator, stream);
try checkRC(c.inflateInit2(stream, options.windowSize()));
return .{
@ -425,11 +425,11 @@ pub const Decompressor = struct {
var tmp = try self.allocator.alloc(u8, chunk_size);
var len: usize = 0; // inflated part of the tmp buffer
while (true) {
var out = tmp[len..];
const out = tmp[len..];
self.stream.next_out = @as([*]u8, @ptrFromInt(@intFromPtr(out.ptr)));
self.stream.avail_out = @as(c_uint, @intCast(out.len));
var rc = c.inflate(self.stream, c.Z_SYNC_FLUSH);
const rc = c.inflate(self.stream, c.Z_SYNC_FLUSH);
if (rc != c.Z_OK and rc != c.Z_STREAM_END) {
return errorFromInt(rc);
}

View File

@ -1,58 +0,0 @@
const std = @import("std");
const Self = @This();
fn root() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
const root_path = root() ++ "/";
const package_path = root_path ++ "src/main.zig";
pub const include_dir = root_path ++ "zlib";
pub const Options = struct {
import_name: ?[]const u8 = null,
};
pub const Library = struct {
step: *std.build.LibExeObjStep,
pub fn link(self: Library, other: *std.build.LibExeObjStep, opts: Options) void {
other.addIncludePath(.{ .path = include_dir });
other.linkLibrary(self.step);
if (opts.import_name) |import_name|
other.addAnonymousModule(
import_name,
.{ .source_file = .{ .path = package_path } },
);
}
};
pub fn create(b: *std.build.Builder, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) Library {
const ret = b.addStaticLibrary(.{
.name = "z",
.target = target,
.optimize = optimize,
});
ret.linkLibC();
ret.addCSourceFiles(.{ .files = srcs, .flags = &.{"-std=c89"} });
return Library{ .step = ret };
}
const srcs = &.{
root_path ++ "zlib/adler32.c",
root_path ++ "zlib/compress.c",
root_path ++ "zlib/crc32.c",
root_path ++ "zlib/deflate.c",
root_path ++ "zlib/gzclose.c",
root_path ++ "zlib/gzlib.c",
root_path ++ "zlib/gzread.c",
root_path ++ "zlib/gzwrite.c",
root_path ++ "zlib/inflate.c",
root_path ++ "zlib/infback.c",
root_path ++ "zlib/inftrees.c",
root_path ++ "zlib/inffast.c",
root_path ++ "zlib/trees.c",
root_path ++ "zlib/uncompr.c",
root_path ++ "zlib/zutil.c",
};