Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
.version = "0.0.1",
.dependencies = .{
.snappyz = .{
.url = "https://github.com/blockblaz/zig-snappy/archive/13fe068.tar.gz",
.hash = "1220613ed9cbd8ec648d59db9b798644b3578f8ca36c8ec313426ac19dc9f95da825",
.url = "https://github.com/blockblaz/zig-snappy/archive/beb2a56.tar.gz",
.hash = "1220c856c5d5b87a07060fd0f74568a36ac9d619cf711d949d342c2340df936d20e7",
},
},
.paths = .{""},
Expand Down
23 changes: 9 additions & 14 deletions src/frames.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn decode(allocator: Allocator, writer: anytype, data: []const u8) !void {
}
}

pub fn encode(allocator: Allocator, writer: anytype, data: []u8) !void {
pub fn encode(allocator: Allocator, writer: anytype, data: []const u8) !void {
// push identifier frame
try writer.writeAll(&IDENTIFIER_FRAME);
var i: usize = 0;
Expand All @@ -58,7 +58,8 @@ pub fn encode(allocator: Allocator, writer: anytype, data: []u8) !void {
defer allocator.free(crc_bytes);
std.mem.writePackedInt(u32, crc_bytes, 0, crc_hash, .little);

try writer.writeAll(frame_header ++ crc_bytes[0..4]);
try writer.writeAll(&frame_header);
try writer.writeAll(crc_bytes[0..4]);
try writer.writeAll(compressed);
} else {
const size = chunk.len + 4;
Expand All @@ -69,7 +70,8 @@ pub fn encode(allocator: Allocator, writer: anytype, data: []u8) !void {
defer allocator.free(crc_bytes);
std.mem.writePackedInt(u32, crc_bytes, 0, crc_hash, .little);

try writer.writeAll(frame_header ++ crc_bytes[0..4]);
try writer.writeAll(&frame_header);
try writer.writeAll(crc_bytes[0..4]);
try writer.writeAll(chunk);
}
i = i + UNCOMPRESSED_CHUNK_SIZE;
Expand Down Expand Up @@ -147,34 +149,27 @@ test "decode" {
test "encode" {
// this should lead to an uncompressed chunk
const data = "thissNaPpY";
const data_slice = try std.testing.allocator.alloc(u8, data.len);
defer std.testing.allocator.free(data_slice);

std.mem.copyForwards(u8, data_slice, data);

var arraylistdata = std.ArrayList(u8).init(std.testing.allocator);
defer arraylistdata.deinit();
const fbswriter = arraylistdata.writer();

try encode(std.testing.allocator, fbswriter, data_slice);
try encode(std.testing.allocator, fbswriter, data);
const dumped = arraylistdata.items;
const expected = IDENTIFIER_FRAME ++ [_]u8{ 0x01, 0x0e, 0x00, 0x00, 0x58, 0x09, 0xd7, 0x88 } ++ "thissNaPpY";

try std.testing.expectEqualSlices(std.meta.Child([]const u8), dumped, expected);
try std.testing.expectEqualSlices(u8, dumped, expected);
}

test "encode<>decode" {
// this should lead to a compressed chunk
const data = "thissNaPpYYYYYYYYYYYYYYYYYYYY";
const data_slice = try std.testing.allocator.alloc(u8, data.len);
defer std.testing.allocator.free(data_slice);

std.mem.copyForwards(u8, data_slice, data);
var arraylistdata = std.ArrayList(u8).init(std.testing.allocator);
defer arraylistdata.deinit();
const fbswriter = arraylistdata.writer();

try encode(std.testing.allocator, fbswriter, data_slice);
try encode(std.testing.allocator, fbswriter, data);
const encoded = arraylistdata.items;

var arraylistdata1 = std.ArrayList(u8).init(std.testing.allocator);
Expand All @@ -183,5 +178,5 @@ test "encode<>decode" {
try decode(std.testing.allocator, fbswriter1, encoded);
const decoded = arraylistdata1.items;

try std.testing.expectEqualSlices(std.meta.Child([]const u8), data, decoded);
try std.testing.expectEqualSlices(u8, data, decoded);
}