[cmph] do our own definitions

so we don't import headers any more.
This commit is contained in:
Motiejus Jakštys 2022-03-20 08:42:22 +01:00 committed by Motiejus Jakštys
parent f4012c8694
commit 787fbcf375

View File

@ -5,32 +5,50 @@ const sort = std.sort;
const bdz = @import("bdz.zig"); const bdz = @import("bdz.zig");
const c = @cImport({ // must be kept in sync with the definition in cmph_types.h
@cInclude("cmph.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 // 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 // minus first 4 bytes") for further storage. The slice must be freed by the
// caller. // caller.
pub const Error = Allocator.Error || error{Overflow}; pub const Error = Allocator.Error || error{Overflow};
pub fn pack(allocator: Allocator, input: [][*:0]const u8) Error![]const u8 { pub fn pack(allocator: Allocator, input: [][*:0]const u8) Error![]const u8 {
var source = c.cmph_io_vector_adapter( const input_len = try math.cast(c_uint, input.len);
@ptrCast(*[*c]u8, input.ptr), var source = cmph_io_vector_adapter(input.ptr, input_len);
try math.cast(c_uint, input.len), defer cmph_io_vector_adapter_destroy(source);
); var config = cmph_config_new(source) orelse return error.OutOfMemory;
defer c.cmph_io_vector_adapter_destroy(source); cmph_config_set_algo(config, @enumToInt(CMPH_ALGO.CMPH_BDZ));
var config: *c.cmph_config_t = c.cmph_config_new(source) orelse cmph_config_set_b(config, 7);
return error.OutOfMemory; var mph = cmph_new(config) orelse return error.OutOfMemory;
c.cmph_config_set_algo(config, c.CMPH_BDZ); cmph_config_destroy(config);
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 size = c.cmph_packed_size(hash); const size = cmph_packed_size(mph);
var buf = try allocator.alloc(u8, size); var buf = try allocator.alloc(u8, size);
errdefer allocator.free(buf); errdefer allocator.free(buf);
c.cmph_pack(hash, &buf[0]); cmph_pack(mph, buf.ptr);
c.cmph_destroy(hash); cmph_destroy(mph);
return buf[4..]; return buf[4..];
} }
@ -142,3 +160,10 @@ test "pack str" {
for (hashes) |hash, i| for (hashes) |hash, i|
try testing.expectEqual(i, hash); 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));
}