Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ concurrency:
cancel-in-progress: true

env:
ZIG_VERSION: 0.14.1
ZIG_VERSION: master

jobs:
build-test:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.zig-cache
zig-out
zig-out.zig-cache/
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",

.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
138 changes: 138 additions & 0 deletions zig-out/include/snappy-c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2011 Martin Gieseking <martin.gieseking@uos.de>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Plain C interface (a wrapper around the C++ implementation).
*/

#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_C_H_
#define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_C_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

/*
* Return values; see the documentation for each function to know
* what each can return.
*/
typedef enum {
SNAPPY_OK = 0,
SNAPPY_INVALID_INPUT = 1,
SNAPPY_BUFFER_TOO_SMALL = 2
} snappy_status;

/*
* Takes the data stored in "input[0..input_length-1]" and stores
* it in the array pointed to by "compressed".
*
* <compressed_length> signals the space available in "compressed".
* If it is not at least equal to "snappy_max_compressed_length(input_length)",
* SNAPPY_BUFFER_TOO_SMALL is returned. After successful compression,
* <compressed_length> contains the true length of the compressed output,
* and SNAPPY_OK is returned.
*
* Example:
* size_t output_length = snappy_max_compressed_length(input_length);
* char* output = (char*)malloc(output_length);
* if (snappy_compress(input, input_length, output, &output_length)
* == SNAPPY_OK) {
* ... Process(output, output_length) ...
* }
* free(output);
*/
snappy_status snappy_compress(const char* input,
size_t input_length,
char* compressed,
size_t* compressed_length);

/*
* Given data in "compressed[0..compressed_length-1]" generated by
* calling the snappy_compress routine, this routine stores
* the uncompressed data to
* uncompressed[0..uncompressed_length-1].
* Returns failure (a value not equal to SNAPPY_OK) if the message
* is corrupted and could not be decrypted.
*
* <uncompressed_length> signals the space available in "uncompressed".
* If it is not at least equal to the value returned by
* snappy_uncompressed_length for this stream, SNAPPY_BUFFER_TOO_SMALL
* is returned. After successful decompression, <uncompressed_length>
* contains the true length of the decompressed output.
*
* Example:
* size_t output_length;
* if (snappy_uncompressed_length(input, input_length, &output_length)
* != SNAPPY_OK) {
* ... fail ...
* }
* char* output = (char*)malloc(output_length);
* if (snappy_uncompress(input, input_length, output, &output_length)
* == SNAPPY_OK) {
* ... Process(output, output_length) ...
* }
* free(output);
*/
snappy_status snappy_uncompress(const char* compressed,
size_t compressed_length,
char* uncompressed,
size_t* uncompressed_length);

/*
* Returns the maximal size of the compressed representation of
* input data that is "source_length" bytes in length.
*/
size_t snappy_max_compressed_length(size_t source_length);

/*
* REQUIRES: "compressed[]" was produced by snappy_compress()
* Returns SNAPPY_OK and stores the length of the uncompressed data in
* *result normally. Returns SNAPPY_INVALID_INPUT on parsing error.
* This operation takes O(1) time.
*/
snappy_status snappy_uncompressed_length(const char* compressed,
size_t compressed_length,
size_t* result);

/*
* Check if the contents of "compressed[]" can be uncompressed successfully.
* Does not return the uncompressed data; if so, returns SNAPPY_OK,
* or if not, returns SNAPPY_INVALID_INPUT.
* Takes time proportional to compressed_length, but is usually at least a
* factor of four faster than actual decompression.
*/
snappy_status snappy_validate_compressed_buffer(const char* compressed,
size_t compressed_length);

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_C_H_ */
64 changes: 64 additions & 0 deletions zig-out/include/snappy-stubs-public.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* This file was generated by ConfigHeader using the Zig Build System. */
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Various type stubs for the open-source version of Snappy.
//
// This file cannot include config.h, as it is included from snappy.h,
// which is a public header. Instead, snappy-stubs-public.h is generated by
// from snappy-stubs-public.h.in at configure time.

#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
#define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_

#include <cstddef>

#if 1 // HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif // HAVE_SYS_UIO_H

#define SNAPPY_MAJOR 1
#define SNAPPY_MINOR 2
#define SNAPPY_PATCHLEVEL 2
#define SNAPPY_VERSION \
((SNAPPY_MAJOR << 16) | (SNAPPY_MINOR << 8) | SNAPPY_PATCHLEVEL)

namespace snappy {

#if !1 // !HAVE_SYS_UIO_H
// Windows does not have an iovec type, yet the concept is universally useful.
// It is simple to define it ourselves, so we put it inside our own namespace.
struct iovec {
void* iov_base;
size_t iov_len;
};
#endif // !HAVE_SYS_UIO_H

} // namespace snappy

#endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
Loading
Loading