Skip to content

Commit

Permalink
Update to Zig 0.13, add support for build system
Browse files Browse the repository at this point in the history
  • Loading branch information
mgord9518 committed Jan 23, 2025
1 parent 23bdba8 commit 6d30ddf
Show file tree
Hide file tree
Showing 14 changed files with 804 additions and 736 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Specify filepatterns you want git to ignore.

zig-cache/
.zig-cache/
zig-out/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# LZig4
Implementing lz4 in zig.

Currently supports Zig 0.13
50 changes: 50 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});

const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable(.{
.name = "lz4",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const clap_dep = b.dependency("clap", .{
.target = target,
.optimize = optimize,
});

const lzig4_module = b.addModule("lzig4", .{
.root_source_file = b.path("lib/root.zig"),
});

exe.root_module.addImport("clap", clap_dep.module("clap"));
exe.root_module.addImport("lzig4", lzig4_module);

b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);

run_cmd.step.dependOn(b.getInstallStep());

if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("lib/root.zig"),
.target = target,
.optimize = optimize,
});

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
}
16 changes: 16 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.{
.name = "LZig4",
.version = "0.0.8",

.paths = [][]const u8{
"build.zig",
"build.zig.zon",
},

.dependencies = .{
.clap = .{
.url = "https://github.com/Hejsil/zig-clap/archive/c0193e9247335a6c1688b946325060289405de2a.tar.gz",
.hash = "12207ee987ce045596cb992cfb15b0d6d9456e50d4721c3061c69dabc2962053644d",
},
},
}
44 changes: 20 additions & 24 deletions decompress.zig → lib/decompress.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ const Allocator = mem.Allocator;
const frame = @import("read_frame_header.zig");
const block = @import("read_block.zig");


pub const Decompressor = struct {
//allocator : *Allocator,
frame_header: frame.FrameHeader,
const Self = @This();

pub fn init() void {

}
pub fn init() void {}

pub fn decompressAlloc(self: *Self, allocator: *Allocator, compressed: []const u8) ![]u8 {
pub fn decompressAlloc(self: *Self, allocator: Allocator, compressed: []const u8) ![]u8 {
var read: usize = 0;
var written: usize = 0;
var decompressed: []u8 = try allocator.alloc(u8, 0);
Expand All @@ -27,7 +24,7 @@ pub const Decompressor = struct {
while (read < compressed.len) {
read = try frame.readFrameHeader(compressed, &self.frame_header);
//std.debug.warn("FrameHeader: {}\n", self.frame_header);
switch(self.frame_header) {
switch (self.frame_header) {
frame.FrameHeader.General => |frame_header| {
var frame_read: usize = 0;
var frame_written: usize = 0;
Expand Down Expand Up @@ -60,7 +57,7 @@ pub const Decompressor = struct {
read_out.* = read;
written_out.* = written;
}
switch(self.frame_header) {
switch (self.frame_header) {
frame.FrameHeader.General => {
var frame_read: usize = 0;
var frame_written: usize = 0;
Expand All @@ -79,8 +76,8 @@ pub const Decompressor = struct {

fn decompressGeneralFrame(self: Self, compressed: []const u8, read_out: *usize, decompressed: []u8, written_out: *usize) !void {
const frame_descriptor = &self.frame_header.General;
var read = usize(0);
var written = usize(0);
var read: usize = 0;
var written: usize = 0;
defer {
read_out.* = read;
written_out.* = written;
Expand All @@ -97,9 +94,9 @@ pub const Decompressor = struct {
read += decode_read;
written += decode_written;
}
try block.decodeBlock(compressed[read..read+header.size], &decode_read, decompressed[written..], &decode_written);
try block.decodeBlock(compressed[read .. read + header.size], &decode_read, decompressed[written..], &decode_written);
} else {
std.mem.copy(u8, decompressed[written..], compressed[read..read+header.size]);
std.mem.copyForwards(u8, decompressed[written..], compressed[read .. read + header.size]);
read += header.size;
written += header.size;
}
Expand All @@ -111,30 +108,29 @@ pub const Decompressor = struct {
}
};


test "decompress simple" {
const block1 = []u8{0x8F, 1, 2, 3, 4, 5, 6, 7, 8, 0x02, 0x00, 0xFF, 0x04};
const compressed = [4]u8{0x04, 0x22, 0x4D, 0x18} ++ [3]u8{0x40, 0x40, 0xFE} ++ []u8{@intCast(u8, block1.len), 0x00, 0x00, 0x00} ++ block1 ++ []u8{0x00, 0x00, 0x00, 0x00};
var data = []u8{0} ** 512;
const expected_data = []u8{1,2,3,4,5,6,7,8} ++ []u8{7,8} ** 139;
const block1 = [_]u8{ 0x8F, 1, 2, 3, 4, 5, 6, 7, 8, 0x02, 0x00, 0xFF, 0x04 };
const compressed = [4]u8{ 0x04, 0x22, 0x4D, 0x18 } ++ [3]u8{ 0x40, 0x40, 0xFE } ++ [_]u8{ @as(u8, @intCast(block1.len)), 0x00, 0x00, 0x00 } ++ block1 ++ [_]u8{ 0x00, 0x00, 0x00, 0x00 };
var data = [_]u8{0} ** 512;
const expected_data = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 } ++ [_]u8{ 7, 8 } ** 139;
var decompressor: Decompressor = undefined;
var read: usize = undefined;
var written: usize = undefined;
try decompressor.decompress(compressed, &read, data[0..], &written);
testing.expectEqual(compressed.len, read);
testing.expectEqual(expected_data.len, written);
testing.expectEqualSlices(u8, expected_data, data[0..expected_data.len]);
try decompressor.decompress(&compressed, &read, data[0..], &written);
try testing.expectEqual(compressed.len, read);
try testing.expectEqual(expected_data.len, written);
try testing.expectEqualSlices(u8, &expected_data, data[0..expected_data.len]);
}

test "decompress small lorem ipsum" {
const compressed = @embedFile("lorem.txt.lz4");
const expected = @embedFile("lorem.txt");
var data = []u8{0} ** 1024;
var data = [_]u8{0} ** 1024;
var decompressor: Decompressor = undefined;
var read: usize = undefined;
var written: usize = undefined;
try decompressor.decompress(compressed, &read, data[0..], &written);
testing.expectEqual(compressed.len, read);
testing.expectEqual(expected.len, written);
testing.expectEqualSlices(u8, expected, data[0..expected.len]);
try testing.expectEqual(compressed.len, read);
try testing.expectEqual(expected.len, written);
try testing.expectEqualSlices(u8, expected, data[0..expected.len]);
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 6d30ddf

Please sign in to comment.