Skip to content

std.hash.crc: implement algorithms listed in CRC RevEng catalog #14396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2023
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
96 changes: 96 additions & 0 deletions lib/std/hash/crc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,102 @@ const builtin = @import("builtin");
const debug = std.debug;
const testing = std.testing;

pub usingnamespace @import("crc/catalog.zig");

pub fn Algorithm(comptime W: type) type {
return struct {
polynomial: W,
initial: W,
reflect_input: bool,
reflect_output: bool,
xor_output: W,
};
}

pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type {
return struct {
const Self = @This();
const I = if (@bitSizeOf(W) < 8) u8 else W;
const lookup_table = blk: {
@setEvalBranchQuota(2500);

const poly = if (algorithm.reflect_input)
@bitReverse(@as(I, algorithm.polynomial)) >> (@bitSizeOf(I) - @bitSizeOf(W))
else
@as(I, algorithm.polynomial) << (@bitSizeOf(I) - @bitSizeOf(W));

var table: [256]I = undefined;
for (table) |*e, i| {
var crc: I = i;
if (algorithm.reflect_input) {
var j: usize = 0;
while (j < 8) : (j += 1) {
crc = (crc >> 1) ^ ((crc & 1) * poly);
}
} else {
crc <<= @bitSizeOf(I) - 8;
var j: usize = 0;
while (j < 8) : (j += 1) {
crc = (crc << 1) ^ (((crc >> (@bitSizeOf(I) - 1)) & 1) * poly);
}
}
e.* = crc;
}
break :blk table;
};

crc: I,

pub fn init() Self {
const initial = if (algorithm.reflect_input)
@bitReverse(@as(I, algorithm.initial)) >> (@bitSizeOf(I) - @bitSizeOf(W))
else
@as(I, algorithm.initial) << (@bitSizeOf(I) - @bitSizeOf(W));
return Self{ .crc = initial };
}

inline fn tableEntry(index: I) I {
return lookup_table[@intCast(u8, index & 0xFF)];
}

pub fn update(self: *Self, bytes: []const u8) void {
var i: usize = 0;
if (@bitSizeOf(I) <= 8) {
while (i < bytes.len) : (i += 1) {
self.crc = tableEntry(self.crc ^ bytes[i]);
}
} else if (algorithm.reflect_input) {
while (i < bytes.len) : (i += 1) {
const table_index = self.crc ^ bytes[i];
self.crc = tableEntry(table_index) ^ (self.crc >> 8);
}
} else {
while (i < bytes.len) : (i += 1) {
const table_index = (self.crc >> (@bitSizeOf(I) - 8)) ^ bytes[i];
self.crc = tableEntry(table_index) ^ (self.crc << 8);
}
}
}

pub fn final(self: Self) W {
var c = self.crc;
if (algorithm.reflect_input != algorithm.reflect_output) {
c = @bitReverse(c);
}
if (!algorithm.reflect_output) {
c >>= @bitSizeOf(I) - @bitSizeOf(W);
}
return @intCast(W, c ^ algorithm.xor_output);
}

pub fn hash(bytes: []const u8) W {
var c = Self.init();
c.update(bytes);
return c.final();
}
};
}

pub const Polynomial = enum(u32) {
IEEE = 0xedb88320,
Castagnoli = 0x82f63b78,
Expand Down
Loading