[cmph] do our own definitions
so we don't import headers any more.
This commit is contained in:
parent
f4012c8694
commit
787fbcf375
59
src/cmph.zig
59
src/cmph.zig
@ -5,32 +5,50 @@ const sort = std.sort;
|
||||
|
||||
const bdz = @import("bdz.zig");
|
||||
|
||||
const c = @cImport({
|
||||
@cInclude("cmph.h");
|
||||
});
|
||||
// must be kept in sync with the definition in cmph_types.h
|
||||
const CMPH_ALGO = enum(c_int) {
|
||||
CMPH_BMZ,
|
||||
CMPH_BMZ8,
|
||||
CMPH_CHM,
|
||||
CMPH_BRZ,
|
||||
CMPH_FCH,
|
||||
CMPH_BDZ,
|
||||
CMPH_BDZ_PH,
|
||||
CMPH_CHD_PH,
|
||||
CMPH_CHD,
|
||||
CMPH_COUNT,
|
||||
};
|
||||
|
||||
extern fn cmph_io_vector_adapter(vector: [*]const [*:0]const u8, len: c_uint) [*]u8;
|
||||
extern fn cmph_io_vector_adapter_destroy(key_source: [*]u8) void;
|
||||
extern fn cmph_config_new(key_source: [*]const u8) ?[*]u8;
|
||||
extern fn cmph_config_set_algo(mph: [*]u8, algo: c_int) void;
|
||||
extern fn cmph_config_set_b(mph: [*]u8, b: c_int) void;
|
||||
extern fn cmph_new(config: [*]const u8) ?[*]const u8;
|
||||
extern fn cmph_config_destroy(mph: [*]u8) void;
|
||||
extern fn cmph_packed_size(mphf: [*]const u8) u32;
|
||||
extern fn cmph_pack(mphf: [*]const u8, packed_mphf: [*]u8) void;
|
||||
extern fn cmph_destroy(mphf: [*]const u8) void;
|
||||
|
||||
// pack packs cmph hashes for the given input and returns a slice ("cmph pack
|
||||
// minus first 4 bytes") for further storage. The slice must be freed by the
|
||||
// caller.
|
||||
pub const Error = Allocator.Error || error{Overflow};
|
||||
pub fn pack(allocator: Allocator, input: [][*:0]const u8) Error![]const u8 {
|
||||
var source = c.cmph_io_vector_adapter(
|
||||
@ptrCast(*[*c]u8, input.ptr),
|
||||
try math.cast(c_uint, input.len),
|
||||
);
|
||||
defer c.cmph_io_vector_adapter_destroy(source);
|
||||
var config: *c.cmph_config_t = c.cmph_config_new(source) orelse
|
||||
return error.OutOfMemory;
|
||||
c.cmph_config_set_algo(config, c.CMPH_BDZ);
|
||||
c.cmph_config_set_b(config, 7);
|
||||
var hash: *c.cmph_t = c.cmph_new(config) orelse return error.OutOfMemory;
|
||||
c.cmph_config_destroy(config);
|
||||
const input_len = try math.cast(c_uint, input.len);
|
||||
var source = cmph_io_vector_adapter(input.ptr, input_len);
|
||||
defer cmph_io_vector_adapter_destroy(source);
|
||||
var config = cmph_config_new(source) orelse return error.OutOfMemory;
|
||||
cmph_config_set_algo(config, @enumToInt(CMPH_ALGO.CMPH_BDZ));
|
||||
cmph_config_set_b(config, 7);
|
||||
var mph = cmph_new(config) orelse return error.OutOfMemory;
|
||||
cmph_config_destroy(config);
|
||||
|
||||
const size = c.cmph_packed_size(hash);
|
||||
const size = cmph_packed_size(mph);
|
||||
var buf = try allocator.alloc(u8, size);
|
||||
errdefer allocator.free(buf);
|
||||
c.cmph_pack(hash, &buf[0]);
|
||||
c.cmph_destroy(hash);
|
||||
cmph_pack(mph, buf.ptr);
|
||||
cmph_destroy(mph);
|
||||
return buf[4..];
|
||||
}
|
||||
|
||||
@ -142,3 +160,10 @@ test "pack str" {
|
||||
for (hashes) |hash, i|
|
||||
try testing.expectEqual(i, hash);
|
||||
}
|
||||
|
||||
test "CMPH_ALGO is in sync" {
|
||||
const c = @cImport({
|
||||
@cInclude("cmph_types.h");
|
||||
});
|
||||
try testing.expectEqual(c.CMPH_BDZ, @enumToInt(CMPH_ALGO.CMPH_BDZ));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user