Compare commits
10 Commits
9ecb2aeba9
...
17144a7981
Author | SHA1 | Date | |
---|---|---|---|
17144a7981 | |||
b242ec16f3 | |||
1369fae820 | |||
d4613bb2ca | |||
![]() |
5d231649a9 | ||
![]() |
24137e1045 | ||
![]() |
c00e95736e | ||
![]() |
a3d6d97bd1 | ||
![]() |
be1bc42cb8 | ||
![]() |
6fb8ed90ba |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
zig-cache/
|
||||
.zig-cache/
|
||||
zig-out/
|
||||
.vscode
|
||||
|
53
build.zig
53
build.zig
@ -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
12
build.zig.zon
Normal file
@ -0,0 +1,12 @@
|
||||
.{
|
||||
.name = "zlib",
|
||||
.version = "0.1.0",
|
||||
.dependencies = .{},
|
||||
.paths = .{
|
||||
"readme.md",
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
"zlib",
|
||||
},
|
||||
}
|
0
example/exe/.zig-cache/h/timestamp
Normal file
0
example/exe/.zig-cache/h/timestamp
Normal 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" },
|
||||
};
|
BIN
example/exe/.zig-cache/z/bcf15b946bf5c3c42c96c97e20c86c29
Normal file
BIN
example/exe/.zig-cache/z/bcf15b946bf5c3c42c96c97e20c86c29
Normal file
Binary file not shown.
@ -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
|
||||
|
@ -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 = .{""},
|
||||
|
@ -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", .{});
|
||||
}
|
||||
|
32
src/main.zig
32
src/main.zig
@ -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);
|
||||
}
|
||||
|
58
zlib.zig
58
zlib.zig
@ -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",
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user