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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ name: CI

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ "**" ]
branches: ["**"]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-ci
cancel-in-progress: true

env:
ZIG_VERSION: 0.14.1
ZIG_VERSION: 0.16.0
Comment thread
GrapeBaBa marked this conversation as resolved.

jobs:
build-test:
Expand All @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.zig-cache
zig-out
.zig-cache/
zig-out/
zig-pkg/
8 changes: 4 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
}),
});

lib.addCSourceFiles(.{
lib.root_module.addCSourceFiles(.{
.root = upstream.path("."),
.files = &[_][]const u8{
"snappy-sinksource.cc",
Expand All @@ -44,12 +44,12 @@ pub fn build(b: *std.Build) void {
.PROJECT_VERSION_PATCH = @as(i64, @intCast(snappy_version.patch)),
});

lib.addIncludePath(upstream.path("."));
lib.addConfigHeader(snappy_stubs_public_h);
lib.root_module.addIncludePath(upstream.path("."));
lib.root_module.addConfigHeader(snappy_stubs_public_h);

lib.installHeader(upstream.path("snappy.h"), "snappy.h");
lib.installHeader(upstream.path("snappy-c.h"), "snappy-c.h");
lib.installHeader(snappy_stubs_public_h.getOutput(), "snappy-stubs-public.h");
lib.installHeader(snappy_stubs_public_h.getOutputFile(), "snappy-stubs-public.h");
b.installArtifact(lib);

const module = b.addModule("snappy", .{
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.name = .snappy,
.version = "0.1.0",
.fingerprint = 0xcbfb5fb8aa1a809f,
.minimum_zig_version = "0.14.1",
.minimum_zig_version = "0.16.0",
Comment thread
GrapeBaBa marked this conversation as resolved.
.dependencies = .{
.snappy = .{
.url = "git+https://github.com/google/snappy#6f99459b5b837fa18abb1be317d3ac868530f384",
Expand Down
24 changes: 12 additions & 12 deletions src/frame.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ pub const CompressError = std.mem.Allocator.Error || snappy.Error;
///
/// Caller owns the returned memory.
pub fn compress(allocator: std.mem.Allocator, bytes: []const u8) CompressError![]u8 {
var out = std.ArrayList(u8).init(allocator);
errdefer out.deinit();
try out.appendSlice(&IDENTIFIER_FRAME);
var out = std.ArrayList(u8).empty;
errdefer out.deinit(allocator);
try out.appendSlice(allocator, &IDENTIFIER_FRAME);

const max_compressed_len = snappy.maxCompressedLength(UNCOMPRESSED_CHUNK_SIZE_LIMIT);
var compressed_buf = try allocator.alloc(u8, max_compressed_len);
Expand All @@ -59,15 +59,15 @@ pub fn compress(allocator: std.mem.Allocator, bytes: []const u8) CompressError![

var header: [4]u8 = .{ @intFromEnum(chunk_type), 0, 0, 0 };
std.mem.writeInt(u24, header[1..4], @intCast(frame_size), .little);
try out.appendSlice(&header);
try out.appendSlice(allocator, &header);

var checksum: [4]u8 = undefined;
std.mem.writeInt(u32, &checksum, crc(chunk), .little);
try out.appendSlice(&checksum);
try out.appendSlice(payload);
try out.appendSlice(allocator, &checksum);
try out.appendSlice(allocator, payload);
}

return out.toOwnedSlice();
return out.toOwnedSlice(allocator);
}

/// Parse framed Snappy data and return the uncompressed payload,
Expand All @@ -83,8 +83,8 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr

var slice = bytes[IDENTIFIER_FRAME.len..];

var out = std.ArrayList(u8).init(allocator);
errdefer out.deinit();
var out = std.ArrayList(u8).empty;
errdefer out.deinit(allocator);

while (slice.len > 0) {
if (slice.len < 4) break;
Expand All @@ -102,7 +102,7 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr
const uncompressed_len = try snappy.uncompress(compressed, uncompressed[0..]);

if (crc(uncompressed[0..uncompressed_len]) != std.mem.bytesToValue(u32, checksum)) return UncompressError.BadChecksum;
try out.appendSlice(uncompressed[0..uncompressed_len]);
try out.appendSlice(allocator, uncompressed[0..uncompressed_len]);
},
.uncompressed => {
const checksum = frame[0..4];
Expand All @@ -112,7 +112,7 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr
return UncompressError.IllegalChunkLength;
}
if (crc(uncompressed) != std.mem.bytesToValue(u32, checksum)) return UncompressError.BadChecksum;
try out.appendSlice(uncompressed);
try out.appendSlice(allocator, uncompressed);
},
.padding,
.skippable,
Expand All @@ -126,7 +126,7 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr

if (out.items.len == 0) return null;

return try out.toOwnedSlice();
return try out.toOwnedSlice(allocator);
}

/// Masked CRC32C hash used by the Snappy framing format.
Expand Down
36 changes: 15 additions & 21 deletions src/snappy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ test {

test "round trip - raw" {
const allocator = std.testing.allocator;
const io = std.testing.io;

var dir = try std.fs.cwd().openDir("testdata", .{ .iterate = true });
defer dir.close();
var dir = try std.Io.Dir.cwd().openDir(io, "testdata", .{ .iterate = true });
defer dir.close(io);

var it = dir.iterate();
while (try it.next()) |entry| {
while (try it.next(io)) |entry| {
if (entry.kind != .file) continue;

var file = try dir.openFile(entry.name, .{});
defer file.close();

const bytes = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
const bytes = try dir.readFileAlloc(io, entry.name, allocator, .unlimited);
defer allocator.free(bytes);

const compressed = try allocator.alloc(u8, raw.maxCompressedLength(bytes.len));
Expand All @@ -41,19 +39,17 @@ test "round trip - raw" {

test "bad data" {
const allocator = std.testing.allocator;
const io = std.testing.io;

var dir = try std.fs.cwd().openDir("testdata", .{ .iterate = true });
defer dir.close();
var dir = try std.Io.Dir.cwd().openDir(io, "testdata", .{ .iterate = true });
defer dir.close(io);

var it = dir.iterate();
while (try it.next()) |entry| {
while (try it.next(io)) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.startsWith(u8, entry.name, "baddata")) continue;

var file = try dir.openFile(entry.name, .{});
defer file.close();

const bytes = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
const bytes = try dir.readFileAlloc(io, entry.name, allocator, .unlimited);
defer allocator.free(bytes);
const got = try allocator.alloc(u8, try raw.uncompressedLength(bytes));
defer allocator.free(got);
Expand All @@ -63,18 +59,16 @@ test "bad data" {

test "round trip - framed" {
const allocator = std.testing.allocator;
const io = std.testing.io;

var dir = try std.fs.cwd().openDir("testdata", .{ .iterate = true });
defer dir.close();
var dir = try std.Io.Dir.cwd().openDir(io, "testdata", .{ .iterate = true });
defer dir.close(io);

var it = dir.iterate();
while (try it.next()) |entry| {
while (try it.next(io)) |entry| {
if (entry.kind != .file) continue;

var file = try dir.openFile(entry.name, .{});
defer file.close();

const bytes = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
const bytes = try dir.readFileAlloc(io, entry.name, allocator, .unlimited);
defer allocator.free(bytes);
const d = bytes[0..];
const compressed = try frame.compress(allocator, d[0..]);
Expand Down
Loading