diff --git a/lib/std/hash/crc.zig b/lib/std/hash/crc.zig index ad2e0018c9f3..1c69978f80cd 100644 --- a/lib/std/hash/crc.zig +++ b/lib/std/hash/crc.zig @@ -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, diff --git a/lib/std/hash/crc/catalog.zig b/lib/std/hash/crc/catalog.zig new file mode 100644 index 000000000000..ed08accce643 --- /dev/null +++ b/lib/std/hash/crc/catalog.zig @@ -0,0 +1,903 @@ +//! This file is auto-generated by tools/update_crc_catalog.zig. + +const Crc = @import("../crc.zig").Crc; + +test { + _ = @import("catalog_test.zig"); +} + +pub const Crc3Gsm = Crc(u3, .{ + .polynomial = 0x3, + .initial = 0x0, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x7, +}); + +pub const Crc3Rohc = Crc(u3, .{ + .polynomial = 0x3, + .initial = 0x7, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0, +}); + +pub const Crc4G704 = Crc(u4, .{ + .polynomial = 0x3, + .initial = 0x0, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0, +}); + +pub const Crc4Interlaken = Crc(u4, .{ + .polynomial = 0x3, + .initial = 0xf, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xf, +}); + +pub const Crc5EpcC1g2 = Crc(u5, .{ + .polynomial = 0x09, + .initial = 0x09, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc5G704 = Crc(u5, .{ + .polynomial = 0x15, + .initial = 0x00, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc5Usb = Crc(u5, .{ + .polynomial = 0x05, + .initial = 0x1f, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x1f, +}); + +pub const Crc6Cdma2000A = Crc(u6, .{ + .polynomial = 0x27, + .initial = 0x3f, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc6Cdma2000B = Crc(u6, .{ + .polynomial = 0x07, + .initial = 0x3f, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc6Darc = Crc(u6, .{ + .polynomial = 0x19, + .initial = 0x00, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc6G704 = Crc(u6, .{ + .polynomial = 0x03, + .initial = 0x00, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc6Gsm = Crc(u6, .{ + .polynomial = 0x2f, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x3f, +}); + +pub const Crc7Mmc = Crc(u7, .{ + .polynomial = 0x09, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc7Rohc = Crc(u7, .{ + .polynomial = 0x4f, + .initial = 0x7f, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc7Umts = Crc(u7, .{ + .polynomial = 0x45, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8Autosar = Crc(u8, .{ + .polynomial = 0x2f, + .initial = 0xff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xff, +}); + +pub const Crc8Bluetooth = Crc(u8, .{ + .polynomial = 0xa7, + .initial = 0x00, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc8Cdma2000 = Crc(u8, .{ + .polynomial = 0x9b, + .initial = 0xff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8Darc = Crc(u8, .{ + .polynomial = 0x39, + .initial = 0x00, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc8DvbS2 = Crc(u8, .{ + .polynomial = 0xd5, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8GsmA = Crc(u8, .{ + .polynomial = 0x1d, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8GsmB = Crc(u8, .{ + .polynomial = 0x49, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xff, +}); + +pub const Crc8Hitag = Crc(u8, .{ + .polynomial = 0x1d, + .initial = 0xff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8I4321 = Crc(u8, .{ + .polynomial = 0x07, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x55, +}); + +pub const Crc8ICode = Crc(u8, .{ + .polynomial = 0x1d, + .initial = 0xfd, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8Lte = Crc(u8, .{ + .polynomial = 0x9b, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8MaximDow = Crc(u8, .{ + .polynomial = 0x31, + .initial = 0x00, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc8MifareMad = Crc(u8, .{ + .polynomial = 0x1d, + .initial = 0xc7, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8Nrsc5 = Crc(u8, .{ + .polynomial = 0x31, + .initial = 0xff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8Opensafety = Crc(u8, .{ + .polynomial = 0x2f, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8Rohc = Crc(u8, .{ + .polynomial = 0x07, + .initial = 0xff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc8SaeJ1850 = Crc(u8, .{ + .polynomial = 0x1d, + .initial = 0xff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xff, +}); + +pub const Crc8Smbus = Crc(u8, .{ + .polynomial = 0x07, + .initial = 0x00, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00, +}); + +pub const Crc8Tech3250 = Crc(u8, .{ + .polynomial = 0x1d, + .initial = 0xff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc8Wcdma = Crc(u8, .{ + .polynomial = 0x9b, + .initial = 0x00, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00, +}); + +pub const Crc10Atm = Crc(u10, .{ + .polynomial = 0x233, + .initial = 0x000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000, +}); + +pub const Crc10Cdma2000 = Crc(u10, .{ + .polynomial = 0x3d9, + .initial = 0x3ff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000, +}); + +pub const Crc10Gsm = Crc(u10, .{ + .polynomial = 0x175, + .initial = 0x000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x3ff, +}); + +pub const Crc11Flexray = Crc(u11, .{ + .polynomial = 0x385, + .initial = 0x01a, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000, +}); + +pub const Crc11Umts = Crc(u11, .{ + .polynomial = 0x307, + .initial = 0x000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000, +}); + +pub const Crc12Cdma2000 = Crc(u12, .{ + .polynomial = 0xf13, + .initial = 0xfff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000, +}); + +pub const Crc12Dect = Crc(u12, .{ + .polynomial = 0x80f, + .initial = 0x000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000, +}); + +pub const Crc12Gsm = Crc(u12, .{ + .polynomial = 0xd31, + .initial = 0x000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xfff, +}); + +pub const Crc12Umts = Crc(u12, .{ + .polynomial = 0x80f, + .initial = 0x000, + .reflect_input = false, + .reflect_output = true, + .xor_output = 0x000, +}); + +pub const Crc13Bbc = Crc(u13, .{ + .polynomial = 0x1cf5, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc14Darc = Crc(u14, .{ + .polynomial = 0x0805, + .initial = 0x0000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc14Gsm = Crc(u14, .{ + .polynomial = 0x202d, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x3fff, +}); + +pub const Crc15Can = Crc(u15, .{ + .polynomial = 0x4599, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc15Mpt1327 = Crc(u15, .{ + .polynomial = 0x6815, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0001, +}); + +pub const Crc16Arc = Crc(u16, .{ + .polynomial = 0x8005, + .initial = 0x0000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16Cdma2000 = Crc(u16, .{ + .polynomial = 0xc867, + .initial = 0xffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16Cms = Crc(u16, .{ + .polynomial = 0x8005, + .initial = 0xffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16Dds110 = Crc(u16, .{ + .polynomial = 0x8005, + .initial = 0x800d, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16DectR = Crc(u16, .{ + .polynomial = 0x0589, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0001, +}); + +pub const Crc16DectX = Crc(u16, .{ + .polynomial = 0x0589, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16Dnp = Crc(u16, .{ + .polynomial = 0x3d65, + .initial = 0x0000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffff, +}); + +pub const Crc16En13757 = Crc(u16, .{ + .polynomial = 0x3d65, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffff, +}); + +pub const Crc16Genibus = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0xffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffff, +}); + +pub const Crc16Gsm = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffff, +}); + +pub const Crc16Ibm3740 = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0xffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16IbmSdlc = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0xffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffff, +}); + +pub const Crc16IsoIec144433A = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0xc6c6, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16Kermit = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0x0000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16Lj1200 = Crc(u16, .{ + .polynomial = 0x6f63, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16M17 = Crc(u16, .{ + .polynomial = 0x5935, + .initial = 0xffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16MaximDow = Crc(u16, .{ + .polynomial = 0x8005, + .initial = 0x0000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffff, +}); + +pub const Crc16Mcrf4xx = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0xffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16Modbus = Crc(u16, .{ + .polynomial = 0x8005, + .initial = 0xffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16Nrsc5 = Crc(u16, .{ + .polynomial = 0x080b, + .initial = 0xffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16OpensafetyA = Crc(u16, .{ + .polynomial = 0x5935, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16OpensafetyB = Crc(u16, .{ + .polynomial = 0x755b, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16Profibus = Crc(u16, .{ + .polynomial = 0x1dcf, + .initial = 0xffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffff, +}); + +pub const Crc16Riello = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0xb2aa, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16SpiFujitsu = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0x1d0f, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16T10Dif = Crc(u16, .{ + .polynomial = 0x8bb7, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16Teledisk = Crc(u16, .{ + .polynomial = 0xa097, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16Tms37157 = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0x89ec, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000, +}); + +pub const Crc16Umts = Crc(u16, .{ + .polynomial = 0x8005, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc16Usb = Crc(u16, .{ + .polynomial = 0x8005, + .initial = 0xffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffff, +}); + +pub const Crc16Xmodem = Crc(u16, .{ + .polynomial = 0x1021, + .initial = 0x0000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000, +}); + +pub const Crc17CanFd = Crc(u17, .{ + .polynomial = 0x1685b, + .initial = 0x00000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00000, +}); + +pub const Crc21CanFd = Crc(u21, .{ + .polynomial = 0x102899, + .initial = 0x000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000000, +}); + +pub const Crc24Ble = Crc(u24, .{ + .polynomial = 0x00065b, + .initial = 0x555555, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x000000, +}); + +pub const Crc24FlexrayA = Crc(u24, .{ + .polynomial = 0x5d6dcb, + .initial = 0xfedcba, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000000, +}); + +pub const Crc24FlexrayB = Crc(u24, .{ + .polynomial = 0x5d6dcb, + .initial = 0xabcdef, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000000, +}); + +pub const Crc24Interlaken = Crc(u24, .{ + .polynomial = 0x328b63, + .initial = 0xffffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffffff, +}); + +pub const Crc24LteA = Crc(u24, .{ + .polynomial = 0x864cfb, + .initial = 0x000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000000, +}); + +pub const Crc24LteB = Crc(u24, .{ + .polynomial = 0x800063, + .initial = 0x000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000000, +}); + +pub const Crc24Openpgp = Crc(u24, .{ + .polynomial = 0x864cfb, + .initial = 0xb704ce, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x000000, +}); + +pub const Crc24Os9 = Crc(u24, .{ + .polynomial = 0x800063, + .initial = 0xffffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffffff, +}); + +pub const Crc30Cdma = Crc(u30, .{ + .polynomial = 0x2030b9c7, + .initial = 0x3fffffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x3fffffff, +}); + +pub const Crc31Philips = Crc(u31, .{ + .polynomial = 0x04c11db7, + .initial = 0x7fffffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x7fffffff, +}); + +pub const Crc32Aixm = Crc(u32, .{ + .polynomial = 0x814141ab, + .initial = 0x00000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00000000, +}); + +pub const Crc32Autosar = Crc(u32, .{ + .polynomial = 0xf4acfb13, + .initial = 0xffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffffffff, +}); + +pub const Crc32Base91D = Crc(u32, .{ + .polynomial = 0xa833982b, + .initial = 0xffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffffffff, +}); + +pub const Crc32Bzip2 = Crc(u32, .{ + .polynomial = 0x04c11db7, + .initial = 0xffffffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffffffff, +}); + +pub const Crc32CdRomEdc = Crc(u32, .{ + .polynomial = 0x8001801b, + .initial = 0x00000000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00000000, +}); + +pub const Crc32Cksum = Crc(u32, .{ + .polynomial = 0x04c11db7, + .initial = 0x00000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffffffff, +}); + +pub const Crc32Iscsi = Crc(u32, .{ + .polynomial = 0x1edc6f41, + .initial = 0xffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffffffff, +}); + +pub const Crc32IsoHdlc = Crc(u32, .{ + .polynomial = 0x04c11db7, + .initial = 0xffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffffffff, +}); + +pub const Crc32Jamcrc = Crc(u32, .{ + .polynomial = 0x04c11db7, + .initial = 0xffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00000000, +}); + +pub const Crc32Mef = Crc(u32, .{ + .polynomial = 0x741b8cd7, + .initial = 0xffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x00000000, +}); + +pub const Crc32Mpeg2 = Crc(u32, .{ + .polynomial = 0x04c11db7, + .initial = 0xffffffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00000000, +}); + +pub const Crc32Xfer = Crc(u32, .{ + .polynomial = 0x000000af, + .initial = 0x00000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x00000000, +}); + +pub const Crc40Gsm = Crc(u40, .{ + .polynomial = 0x0004820009, + .initial = 0x0000000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffffffffff, +}); + +pub const Crc64Ecma182 = Crc(u64, .{ + .polynomial = 0x42f0e1eba9ea3693, + .initial = 0x0000000000000000, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0x0000000000000000, +}); + +pub const Crc64GoIso = Crc(u64, .{ + .polynomial = 0x000000000000001b, + .initial = 0xffffffffffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffffffffffffffff, +}); + +pub const Crc64Ms = Crc(u64, .{ + .polynomial = 0x259c84cba6426349, + .initial = 0xffffffffffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000000000000000, +}); + +pub const Crc64Redis = Crc(u64, .{ + .polynomial = 0xad93d23594c935a9, + .initial = 0x0000000000000000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x0000000000000000, +}); + +pub const Crc64We = Crc(u64, .{ + .polynomial = 0x42f0e1eba9ea3693, + .initial = 0xffffffffffffffff, + .reflect_input = false, + .reflect_output = false, + .xor_output = 0xffffffffffffffff, +}); + +pub const Crc64Xz = Crc(u64, .{ + .polynomial = 0x42f0e1eba9ea3693, + .initial = 0xffffffffffffffff, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0xffffffffffffffff, +}); + +pub const Crc82Darc = Crc(u82, .{ + .polynomial = 0x0308c0111011401440411, + .initial = 0x000000000000000000000, + .reflect_input = true, + .reflect_output = true, + .xor_output = 0x000000000000000000000, +}); diff --git a/lib/std/hash/crc/catalog_test.zig b/lib/std/hash/crc/catalog_test.zig new file mode 100644 index 000000000000..b9bf21fc998f --- /dev/null +++ b/lib/std/hash/crc/catalog_test.zig @@ -0,0 +1,1237 @@ +//! This file is auto-generated by tools/update_crc_catalog.zig. + +const std = @import("../../std.zig"); +const testing = std.testing; +const catalog = @import("catalog.zig"); + +test "CRC-3/GSM" { + const Crc3Gsm = catalog.Crc3Gsm; + + try testing.expectEqual(@as(u3, 0x4), Crc3Gsm.hash("123456789")); + + var c = Crc3Gsm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u3, 0x4), c.final()); +} + +test "CRC-3/ROHC" { + const Crc3Rohc = catalog.Crc3Rohc; + + try testing.expectEqual(@as(u3, 0x6), Crc3Rohc.hash("123456789")); + + var c = Crc3Rohc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u3, 0x6), c.final()); +} + +test "CRC-4/G-704" { + const Crc4G704 = catalog.Crc4G704; + + try testing.expectEqual(@as(u4, 0x7), Crc4G704.hash("123456789")); + + var c = Crc4G704.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u4, 0x7), c.final()); +} + +test "CRC-4/INTERLAKEN" { + const Crc4Interlaken = catalog.Crc4Interlaken; + + try testing.expectEqual(@as(u4, 0xb), Crc4Interlaken.hash("123456789")); + + var c = Crc4Interlaken.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u4, 0xb), c.final()); +} + +test "CRC-5/EPC-C1G2" { + const Crc5EpcC1g2 = catalog.Crc5EpcC1g2; + + try testing.expectEqual(@as(u5, 0x00), Crc5EpcC1g2.hash("123456789")); + + var c = Crc5EpcC1g2.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u5, 0x00), c.final()); +} + +test "CRC-5/G-704" { + const Crc5G704 = catalog.Crc5G704; + + try testing.expectEqual(@as(u5, 0x07), Crc5G704.hash("123456789")); + + var c = Crc5G704.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u5, 0x07), c.final()); +} + +test "CRC-5/USB" { + const Crc5Usb = catalog.Crc5Usb; + + try testing.expectEqual(@as(u5, 0x19), Crc5Usb.hash("123456789")); + + var c = Crc5Usb.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u5, 0x19), c.final()); +} + +test "CRC-6/CDMA2000-A" { + const Crc6Cdma2000A = catalog.Crc6Cdma2000A; + + try testing.expectEqual(@as(u6, 0x0d), Crc6Cdma2000A.hash("123456789")); + + var c = Crc6Cdma2000A.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u6, 0x0d), c.final()); +} + +test "CRC-6/CDMA2000-B" { + const Crc6Cdma2000B = catalog.Crc6Cdma2000B; + + try testing.expectEqual(@as(u6, 0x3b), Crc6Cdma2000B.hash("123456789")); + + var c = Crc6Cdma2000B.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u6, 0x3b), c.final()); +} + +test "CRC-6/DARC" { + const Crc6Darc = catalog.Crc6Darc; + + try testing.expectEqual(@as(u6, 0x26), Crc6Darc.hash("123456789")); + + var c = Crc6Darc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u6, 0x26), c.final()); +} + +test "CRC-6/G-704" { + const Crc6G704 = catalog.Crc6G704; + + try testing.expectEqual(@as(u6, 0x06), Crc6G704.hash("123456789")); + + var c = Crc6G704.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u6, 0x06), c.final()); +} + +test "CRC-6/GSM" { + const Crc6Gsm = catalog.Crc6Gsm; + + try testing.expectEqual(@as(u6, 0x13), Crc6Gsm.hash("123456789")); + + var c = Crc6Gsm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u6, 0x13), c.final()); +} + +test "CRC-7/MMC" { + const Crc7Mmc = catalog.Crc7Mmc; + + try testing.expectEqual(@as(u7, 0x75), Crc7Mmc.hash("123456789")); + + var c = Crc7Mmc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u7, 0x75), c.final()); +} + +test "CRC-7/ROHC" { + const Crc7Rohc = catalog.Crc7Rohc; + + try testing.expectEqual(@as(u7, 0x53), Crc7Rohc.hash("123456789")); + + var c = Crc7Rohc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u7, 0x53), c.final()); +} + +test "CRC-7/UMTS" { + const Crc7Umts = catalog.Crc7Umts; + + try testing.expectEqual(@as(u7, 0x61), Crc7Umts.hash("123456789")); + + var c = Crc7Umts.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u7, 0x61), c.final()); +} + +test "CRC-8/AUTOSAR" { + const Crc8Autosar = catalog.Crc8Autosar; + + try testing.expectEqual(@as(u8, 0xdf), Crc8Autosar.hash("123456789")); + + var c = Crc8Autosar.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xdf), c.final()); +} + +test "CRC-8/BLUETOOTH" { + const Crc8Bluetooth = catalog.Crc8Bluetooth; + + try testing.expectEqual(@as(u8, 0x26), Crc8Bluetooth.hash("123456789")); + + var c = Crc8Bluetooth.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x26), c.final()); +} + +test "CRC-8/CDMA2000" { + const Crc8Cdma2000 = catalog.Crc8Cdma2000; + + try testing.expectEqual(@as(u8, 0xda), Crc8Cdma2000.hash("123456789")); + + var c = Crc8Cdma2000.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xda), c.final()); +} + +test "CRC-8/DARC" { + const Crc8Darc = catalog.Crc8Darc; + + try testing.expectEqual(@as(u8, 0x15), Crc8Darc.hash("123456789")); + + var c = Crc8Darc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x15), c.final()); +} + +test "CRC-8/DVB-S2" { + const Crc8DvbS2 = catalog.Crc8DvbS2; + + try testing.expectEqual(@as(u8, 0xbc), Crc8DvbS2.hash("123456789")); + + var c = Crc8DvbS2.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xbc), c.final()); +} + +test "CRC-8/GSM-A" { + const Crc8GsmA = catalog.Crc8GsmA; + + try testing.expectEqual(@as(u8, 0x37), Crc8GsmA.hash("123456789")); + + var c = Crc8GsmA.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x37), c.final()); +} + +test "CRC-8/GSM-B" { + const Crc8GsmB = catalog.Crc8GsmB; + + try testing.expectEqual(@as(u8, 0x94), Crc8GsmB.hash("123456789")); + + var c = Crc8GsmB.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x94), c.final()); +} + +test "CRC-8/HITAG" { + const Crc8Hitag = catalog.Crc8Hitag; + + try testing.expectEqual(@as(u8, 0xb4), Crc8Hitag.hash("123456789")); + + var c = Crc8Hitag.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xb4), c.final()); +} + +test "CRC-8/I-432-1" { + const Crc8I4321 = catalog.Crc8I4321; + + try testing.expectEqual(@as(u8, 0xa1), Crc8I4321.hash("123456789")); + + var c = Crc8I4321.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xa1), c.final()); +} + +test "CRC-8/I-CODE" { + const Crc8ICode = catalog.Crc8ICode; + + try testing.expectEqual(@as(u8, 0x7e), Crc8ICode.hash("123456789")); + + var c = Crc8ICode.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x7e), c.final()); +} + +test "CRC-8/LTE" { + const Crc8Lte = catalog.Crc8Lte; + + try testing.expectEqual(@as(u8, 0xea), Crc8Lte.hash("123456789")); + + var c = Crc8Lte.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xea), c.final()); +} + +test "CRC-8/MAXIM-DOW" { + const Crc8MaximDow = catalog.Crc8MaximDow; + + try testing.expectEqual(@as(u8, 0xa1), Crc8MaximDow.hash("123456789")); + + var c = Crc8MaximDow.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xa1), c.final()); +} + +test "CRC-8/MIFARE-MAD" { + const Crc8MifareMad = catalog.Crc8MifareMad; + + try testing.expectEqual(@as(u8, 0x99), Crc8MifareMad.hash("123456789")); + + var c = Crc8MifareMad.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x99), c.final()); +} + +test "CRC-8/NRSC-5" { + const Crc8Nrsc5 = catalog.Crc8Nrsc5; + + try testing.expectEqual(@as(u8, 0xf7), Crc8Nrsc5.hash("123456789")); + + var c = Crc8Nrsc5.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xf7), c.final()); +} + +test "CRC-8/OPENSAFETY" { + const Crc8Opensafety = catalog.Crc8Opensafety; + + try testing.expectEqual(@as(u8, 0x3e), Crc8Opensafety.hash("123456789")); + + var c = Crc8Opensafety.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x3e), c.final()); +} + +test "CRC-8/ROHC" { + const Crc8Rohc = catalog.Crc8Rohc; + + try testing.expectEqual(@as(u8, 0xd0), Crc8Rohc.hash("123456789")); + + var c = Crc8Rohc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xd0), c.final()); +} + +test "CRC-8/SAE-J1850" { + const Crc8SaeJ1850 = catalog.Crc8SaeJ1850; + + try testing.expectEqual(@as(u8, 0x4b), Crc8SaeJ1850.hash("123456789")); + + var c = Crc8SaeJ1850.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x4b), c.final()); +} + +test "CRC-8/SMBUS" { + const Crc8Smbus = catalog.Crc8Smbus; + + try testing.expectEqual(@as(u8, 0xf4), Crc8Smbus.hash("123456789")); + + var c = Crc8Smbus.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0xf4), c.final()); +} + +test "CRC-8/TECH-3250" { + const Crc8Tech3250 = catalog.Crc8Tech3250; + + try testing.expectEqual(@as(u8, 0x97), Crc8Tech3250.hash("123456789")); + + var c = Crc8Tech3250.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x97), c.final()); +} + +test "CRC-8/WCDMA" { + const Crc8Wcdma = catalog.Crc8Wcdma; + + try testing.expectEqual(@as(u8, 0x25), Crc8Wcdma.hash("123456789")); + + var c = Crc8Wcdma.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u8, 0x25), c.final()); +} + +test "CRC-10/ATM" { + const Crc10Atm = catalog.Crc10Atm; + + try testing.expectEqual(@as(u10, 0x199), Crc10Atm.hash("123456789")); + + var c = Crc10Atm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u10, 0x199), c.final()); +} + +test "CRC-10/CDMA2000" { + const Crc10Cdma2000 = catalog.Crc10Cdma2000; + + try testing.expectEqual(@as(u10, 0x233), Crc10Cdma2000.hash("123456789")); + + var c = Crc10Cdma2000.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u10, 0x233), c.final()); +} + +test "CRC-10/GSM" { + const Crc10Gsm = catalog.Crc10Gsm; + + try testing.expectEqual(@as(u10, 0x12a), Crc10Gsm.hash("123456789")); + + var c = Crc10Gsm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u10, 0x12a), c.final()); +} + +test "CRC-11/FLEXRAY" { + const Crc11Flexray = catalog.Crc11Flexray; + + try testing.expectEqual(@as(u11, 0x5a3), Crc11Flexray.hash("123456789")); + + var c = Crc11Flexray.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u11, 0x5a3), c.final()); +} + +test "CRC-11/UMTS" { + const Crc11Umts = catalog.Crc11Umts; + + try testing.expectEqual(@as(u11, 0x061), Crc11Umts.hash("123456789")); + + var c = Crc11Umts.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u11, 0x061), c.final()); +} + +test "CRC-12/CDMA2000" { + const Crc12Cdma2000 = catalog.Crc12Cdma2000; + + try testing.expectEqual(@as(u12, 0xd4d), Crc12Cdma2000.hash("123456789")); + + var c = Crc12Cdma2000.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u12, 0xd4d), c.final()); +} + +test "CRC-12/DECT" { + const Crc12Dect = catalog.Crc12Dect; + + try testing.expectEqual(@as(u12, 0xf5b), Crc12Dect.hash("123456789")); + + var c = Crc12Dect.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u12, 0xf5b), c.final()); +} + +test "CRC-12/GSM" { + const Crc12Gsm = catalog.Crc12Gsm; + + try testing.expectEqual(@as(u12, 0xb34), Crc12Gsm.hash("123456789")); + + var c = Crc12Gsm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u12, 0xb34), c.final()); +} + +test "CRC-12/UMTS" { + const Crc12Umts = catalog.Crc12Umts; + + try testing.expectEqual(@as(u12, 0xdaf), Crc12Umts.hash("123456789")); + + var c = Crc12Umts.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u12, 0xdaf), c.final()); +} + +test "CRC-13/BBC" { + const Crc13Bbc = catalog.Crc13Bbc; + + try testing.expectEqual(@as(u13, 0x04fa), Crc13Bbc.hash("123456789")); + + var c = Crc13Bbc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u13, 0x04fa), c.final()); +} + +test "CRC-14/DARC" { + const Crc14Darc = catalog.Crc14Darc; + + try testing.expectEqual(@as(u14, 0x082d), Crc14Darc.hash("123456789")); + + var c = Crc14Darc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u14, 0x082d), c.final()); +} + +test "CRC-14/GSM" { + const Crc14Gsm = catalog.Crc14Gsm; + + try testing.expectEqual(@as(u14, 0x30ae), Crc14Gsm.hash("123456789")); + + var c = Crc14Gsm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u14, 0x30ae), c.final()); +} + +test "CRC-15/CAN" { + const Crc15Can = catalog.Crc15Can; + + try testing.expectEqual(@as(u15, 0x059e), Crc15Can.hash("123456789")); + + var c = Crc15Can.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u15, 0x059e), c.final()); +} + +test "CRC-15/MPT1327" { + const Crc15Mpt1327 = catalog.Crc15Mpt1327; + + try testing.expectEqual(@as(u15, 0x2566), Crc15Mpt1327.hash("123456789")); + + var c = Crc15Mpt1327.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u15, 0x2566), c.final()); +} + +test "CRC-16/ARC" { + const Crc16Arc = catalog.Crc16Arc; + + try testing.expectEqual(@as(u16, 0xbb3d), Crc16Arc.hash("123456789")); + + var c = Crc16Arc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xbb3d), c.final()); +} + +test "CRC-16/CDMA2000" { + const Crc16Cdma2000 = catalog.Crc16Cdma2000; + + try testing.expectEqual(@as(u16, 0x4c06), Crc16Cdma2000.hash("123456789")); + + var c = Crc16Cdma2000.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x4c06), c.final()); +} + +test "CRC-16/CMS" { + const Crc16Cms = catalog.Crc16Cms; + + try testing.expectEqual(@as(u16, 0xaee7), Crc16Cms.hash("123456789")); + + var c = Crc16Cms.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xaee7), c.final()); +} + +test "CRC-16/DDS-110" { + const Crc16Dds110 = catalog.Crc16Dds110; + + try testing.expectEqual(@as(u16, 0x9ecf), Crc16Dds110.hash("123456789")); + + var c = Crc16Dds110.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x9ecf), c.final()); +} + +test "CRC-16/DECT-R" { + const Crc16DectR = catalog.Crc16DectR; + + try testing.expectEqual(@as(u16, 0x007e), Crc16DectR.hash("123456789")); + + var c = Crc16DectR.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x007e), c.final()); +} + +test "CRC-16/DECT-X" { + const Crc16DectX = catalog.Crc16DectX; + + try testing.expectEqual(@as(u16, 0x007f), Crc16DectX.hash("123456789")); + + var c = Crc16DectX.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x007f), c.final()); +} + +test "CRC-16/DNP" { + const Crc16Dnp = catalog.Crc16Dnp; + + try testing.expectEqual(@as(u16, 0xea82), Crc16Dnp.hash("123456789")); + + var c = Crc16Dnp.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xea82), c.final()); +} + +test "CRC-16/EN-13757" { + const Crc16En13757 = catalog.Crc16En13757; + + try testing.expectEqual(@as(u16, 0xc2b7), Crc16En13757.hash("123456789")); + + var c = Crc16En13757.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xc2b7), c.final()); +} + +test "CRC-16/GENIBUS" { + const Crc16Genibus = catalog.Crc16Genibus; + + try testing.expectEqual(@as(u16, 0xd64e), Crc16Genibus.hash("123456789")); + + var c = Crc16Genibus.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xd64e), c.final()); +} + +test "CRC-16/GSM" { + const Crc16Gsm = catalog.Crc16Gsm; + + try testing.expectEqual(@as(u16, 0xce3c), Crc16Gsm.hash("123456789")); + + var c = Crc16Gsm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xce3c), c.final()); +} + +test "CRC-16/IBM-3740" { + const Crc16Ibm3740 = catalog.Crc16Ibm3740; + + try testing.expectEqual(@as(u16, 0x29b1), Crc16Ibm3740.hash("123456789")); + + var c = Crc16Ibm3740.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x29b1), c.final()); +} + +test "CRC-16/IBM-SDLC" { + const Crc16IbmSdlc = catalog.Crc16IbmSdlc; + + try testing.expectEqual(@as(u16, 0x906e), Crc16IbmSdlc.hash("123456789")); + + var c = Crc16IbmSdlc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x906e), c.final()); +} + +test "CRC-16/ISO-IEC-14443-3-A" { + const Crc16IsoIec144433A = catalog.Crc16IsoIec144433A; + + try testing.expectEqual(@as(u16, 0xbf05), Crc16IsoIec144433A.hash("123456789")); + + var c = Crc16IsoIec144433A.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xbf05), c.final()); +} + +test "CRC-16/KERMIT" { + const Crc16Kermit = catalog.Crc16Kermit; + + try testing.expectEqual(@as(u16, 0x2189), Crc16Kermit.hash("123456789")); + + var c = Crc16Kermit.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x2189), c.final()); +} + +test "CRC-16/LJ1200" { + const Crc16Lj1200 = catalog.Crc16Lj1200; + + try testing.expectEqual(@as(u16, 0xbdf4), Crc16Lj1200.hash("123456789")); + + var c = Crc16Lj1200.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xbdf4), c.final()); +} + +test "CRC-16/M17" { + const Crc16M17 = catalog.Crc16M17; + + try testing.expectEqual(@as(u16, 0x772b), Crc16M17.hash("123456789")); + + var c = Crc16M17.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x772b), c.final()); +} + +test "CRC-16/MAXIM-DOW" { + const Crc16MaximDow = catalog.Crc16MaximDow; + + try testing.expectEqual(@as(u16, 0x44c2), Crc16MaximDow.hash("123456789")); + + var c = Crc16MaximDow.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x44c2), c.final()); +} + +test "CRC-16/MCRF4XX" { + const Crc16Mcrf4xx = catalog.Crc16Mcrf4xx; + + try testing.expectEqual(@as(u16, 0x6f91), Crc16Mcrf4xx.hash("123456789")); + + var c = Crc16Mcrf4xx.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x6f91), c.final()); +} + +test "CRC-16/MODBUS" { + const Crc16Modbus = catalog.Crc16Modbus; + + try testing.expectEqual(@as(u16, 0x4b37), Crc16Modbus.hash("123456789")); + + var c = Crc16Modbus.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x4b37), c.final()); +} + +test "CRC-16/NRSC-5" { + const Crc16Nrsc5 = catalog.Crc16Nrsc5; + + try testing.expectEqual(@as(u16, 0xa066), Crc16Nrsc5.hash("123456789")); + + var c = Crc16Nrsc5.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xa066), c.final()); +} + +test "CRC-16/OPENSAFETY-A" { + const Crc16OpensafetyA = catalog.Crc16OpensafetyA; + + try testing.expectEqual(@as(u16, 0x5d38), Crc16OpensafetyA.hash("123456789")); + + var c = Crc16OpensafetyA.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x5d38), c.final()); +} + +test "CRC-16/OPENSAFETY-B" { + const Crc16OpensafetyB = catalog.Crc16OpensafetyB; + + try testing.expectEqual(@as(u16, 0x20fe), Crc16OpensafetyB.hash("123456789")); + + var c = Crc16OpensafetyB.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x20fe), c.final()); +} + +test "CRC-16/PROFIBUS" { + const Crc16Profibus = catalog.Crc16Profibus; + + try testing.expectEqual(@as(u16, 0xa819), Crc16Profibus.hash("123456789")); + + var c = Crc16Profibus.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xa819), c.final()); +} + +test "CRC-16/RIELLO" { + const Crc16Riello = catalog.Crc16Riello; + + try testing.expectEqual(@as(u16, 0x63d0), Crc16Riello.hash("123456789")); + + var c = Crc16Riello.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x63d0), c.final()); +} + +test "CRC-16/SPI-FUJITSU" { + const Crc16SpiFujitsu = catalog.Crc16SpiFujitsu; + + try testing.expectEqual(@as(u16, 0xe5cc), Crc16SpiFujitsu.hash("123456789")); + + var c = Crc16SpiFujitsu.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xe5cc), c.final()); +} + +test "CRC-16/T10-DIF" { + const Crc16T10Dif = catalog.Crc16T10Dif; + + try testing.expectEqual(@as(u16, 0xd0db), Crc16T10Dif.hash("123456789")); + + var c = Crc16T10Dif.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xd0db), c.final()); +} + +test "CRC-16/TELEDISK" { + const Crc16Teledisk = catalog.Crc16Teledisk; + + try testing.expectEqual(@as(u16, 0x0fb3), Crc16Teledisk.hash("123456789")); + + var c = Crc16Teledisk.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x0fb3), c.final()); +} + +test "CRC-16/TMS37157" { + const Crc16Tms37157 = catalog.Crc16Tms37157; + + try testing.expectEqual(@as(u16, 0x26b1), Crc16Tms37157.hash("123456789")); + + var c = Crc16Tms37157.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x26b1), c.final()); +} + +test "CRC-16/UMTS" { + const Crc16Umts = catalog.Crc16Umts; + + try testing.expectEqual(@as(u16, 0xfee8), Crc16Umts.hash("123456789")); + + var c = Crc16Umts.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xfee8), c.final()); +} + +test "CRC-16/USB" { + const Crc16Usb = catalog.Crc16Usb; + + try testing.expectEqual(@as(u16, 0xb4c8), Crc16Usb.hash("123456789")); + + var c = Crc16Usb.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0xb4c8), c.final()); +} + +test "CRC-16/XMODEM" { + const Crc16Xmodem = catalog.Crc16Xmodem; + + try testing.expectEqual(@as(u16, 0x31c3), Crc16Xmodem.hash("123456789")); + + var c = Crc16Xmodem.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u16, 0x31c3), c.final()); +} + +test "CRC-17/CAN-FD" { + const Crc17CanFd = catalog.Crc17CanFd; + + try testing.expectEqual(@as(u17, 0x04f03), Crc17CanFd.hash("123456789")); + + var c = Crc17CanFd.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u17, 0x04f03), c.final()); +} + +test "CRC-21/CAN-FD" { + const Crc21CanFd = catalog.Crc21CanFd; + + try testing.expectEqual(@as(u21, 0x0ed841), Crc21CanFd.hash("123456789")); + + var c = Crc21CanFd.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u21, 0x0ed841), c.final()); +} + +test "CRC-24/BLE" { + const Crc24Ble = catalog.Crc24Ble; + + try testing.expectEqual(@as(u24, 0xc25a56), Crc24Ble.hash("123456789")); + + var c = Crc24Ble.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0xc25a56), c.final()); +} + +test "CRC-24/FLEXRAY-A" { + const Crc24FlexrayA = catalog.Crc24FlexrayA; + + try testing.expectEqual(@as(u24, 0x7979bd), Crc24FlexrayA.hash("123456789")); + + var c = Crc24FlexrayA.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0x7979bd), c.final()); +} + +test "CRC-24/FLEXRAY-B" { + const Crc24FlexrayB = catalog.Crc24FlexrayB; + + try testing.expectEqual(@as(u24, 0x1f23b8), Crc24FlexrayB.hash("123456789")); + + var c = Crc24FlexrayB.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0x1f23b8), c.final()); +} + +test "CRC-24/INTERLAKEN" { + const Crc24Interlaken = catalog.Crc24Interlaken; + + try testing.expectEqual(@as(u24, 0xb4f3e6), Crc24Interlaken.hash("123456789")); + + var c = Crc24Interlaken.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0xb4f3e6), c.final()); +} + +test "CRC-24/LTE-A" { + const Crc24LteA = catalog.Crc24LteA; + + try testing.expectEqual(@as(u24, 0xcde703), Crc24LteA.hash("123456789")); + + var c = Crc24LteA.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0xcde703), c.final()); +} + +test "CRC-24/LTE-B" { + const Crc24LteB = catalog.Crc24LteB; + + try testing.expectEqual(@as(u24, 0x23ef52), Crc24LteB.hash("123456789")); + + var c = Crc24LteB.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0x23ef52), c.final()); +} + +test "CRC-24/OPENPGP" { + const Crc24Openpgp = catalog.Crc24Openpgp; + + try testing.expectEqual(@as(u24, 0x21cf02), Crc24Openpgp.hash("123456789")); + + var c = Crc24Openpgp.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0x21cf02), c.final()); +} + +test "CRC-24/OS-9" { + const Crc24Os9 = catalog.Crc24Os9; + + try testing.expectEqual(@as(u24, 0x200fa5), Crc24Os9.hash("123456789")); + + var c = Crc24Os9.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u24, 0x200fa5), c.final()); +} + +test "CRC-30/CDMA" { + const Crc30Cdma = catalog.Crc30Cdma; + + try testing.expectEqual(@as(u30, 0x04c34abf), Crc30Cdma.hash("123456789")); + + var c = Crc30Cdma.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u30, 0x04c34abf), c.final()); +} + +test "CRC-31/PHILIPS" { + const Crc31Philips = catalog.Crc31Philips; + + try testing.expectEqual(@as(u31, 0x0ce9e46c), Crc31Philips.hash("123456789")); + + var c = Crc31Philips.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u31, 0x0ce9e46c), c.final()); +} + +test "CRC-32/AIXM" { + const Crc32Aixm = catalog.Crc32Aixm; + + try testing.expectEqual(@as(u32, 0x3010bf7f), Crc32Aixm.hash("123456789")); + + var c = Crc32Aixm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0x3010bf7f), c.final()); +} + +test "CRC-32/AUTOSAR" { + const Crc32Autosar = catalog.Crc32Autosar; + + try testing.expectEqual(@as(u32, 0x1697d06a), Crc32Autosar.hash("123456789")); + + var c = Crc32Autosar.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0x1697d06a), c.final()); +} + +test "CRC-32/BASE91-D" { + const Crc32Base91D = catalog.Crc32Base91D; + + try testing.expectEqual(@as(u32, 0x87315576), Crc32Base91D.hash("123456789")); + + var c = Crc32Base91D.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0x87315576), c.final()); +} + +test "CRC-32/BZIP2" { + const Crc32Bzip2 = catalog.Crc32Bzip2; + + try testing.expectEqual(@as(u32, 0xfc891918), Crc32Bzip2.hash("123456789")); + + var c = Crc32Bzip2.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0xfc891918), c.final()); +} + +test "CRC-32/CD-ROM-EDC" { + const Crc32CdRomEdc = catalog.Crc32CdRomEdc; + + try testing.expectEqual(@as(u32, 0x6ec2edc4), Crc32CdRomEdc.hash("123456789")); + + var c = Crc32CdRomEdc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0x6ec2edc4), c.final()); +} + +test "CRC-32/CKSUM" { + const Crc32Cksum = catalog.Crc32Cksum; + + try testing.expectEqual(@as(u32, 0x765e7680), Crc32Cksum.hash("123456789")); + + var c = Crc32Cksum.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0x765e7680), c.final()); +} + +test "CRC-32/ISCSI" { + const Crc32Iscsi = catalog.Crc32Iscsi; + + try testing.expectEqual(@as(u32, 0xe3069283), Crc32Iscsi.hash("123456789")); + + var c = Crc32Iscsi.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0xe3069283), c.final()); +} + +test "CRC-32/ISO-HDLC" { + const Crc32IsoHdlc = catalog.Crc32IsoHdlc; + + try testing.expectEqual(@as(u32, 0xcbf43926), Crc32IsoHdlc.hash("123456789")); + + var c = Crc32IsoHdlc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0xcbf43926), c.final()); +} + +test "CRC-32/JAMCRC" { + const Crc32Jamcrc = catalog.Crc32Jamcrc; + + try testing.expectEqual(@as(u32, 0x340bc6d9), Crc32Jamcrc.hash("123456789")); + + var c = Crc32Jamcrc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0x340bc6d9), c.final()); +} + +test "CRC-32/MEF" { + const Crc32Mef = catalog.Crc32Mef; + + try testing.expectEqual(@as(u32, 0xd2c22f51), Crc32Mef.hash("123456789")); + + var c = Crc32Mef.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0xd2c22f51), c.final()); +} + +test "CRC-32/MPEG-2" { + const Crc32Mpeg2 = catalog.Crc32Mpeg2; + + try testing.expectEqual(@as(u32, 0x0376e6e7), Crc32Mpeg2.hash("123456789")); + + var c = Crc32Mpeg2.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0x0376e6e7), c.final()); +} + +test "CRC-32/XFER" { + const Crc32Xfer = catalog.Crc32Xfer; + + try testing.expectEqual(@as(u32, 0xbd0be338), Crc32Xfer.hash("123456789")); + + var c = Crc32Xfer.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u32, 0xbd0be338), c.final()); +} + +test "CRC-40/GSM" { + const Crc40Gsm = catalog.Crc40Gsm; + + try testing.expectEqual(@as(u40, 0xd4164fc646), Crc40Gsm.hash("123456789")); + + var c = Crc40Gsm.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u40, 0xd4164fc646), c.final()); +} + +test "CRC-64/ECMA-182" { + const Crc64Ecma182 = catalog.Crc64Ecma182; + + try testing.expectEqual(@as(u64, 0x6c40df5f0b497347), Crc64Ecma182.hash("123456789")); + + var c = Crc64Ecma182.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u64, 0x6c40df5f0b497347), c.final()); +} + +test "CRC-64/GO-ISO" { + const Crc64GoIso = catalog.Crc64GoIso; + + try testing.expectEqual(@as(u64, 0xb90956c775a41001), Crc64GoIso.hash("123456789")); + + var c = Crc64GoIso.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u64, 0xb90956c775a41001), c.final()); +} + +test "CRC-64/MS" { + const Crc64Ms = catalog.Crc64Ms; + + try testing.expectEqual(@as(u64, 0x75d4b74f024eceea), Crc64Ms.hash("123456789")); + + var c = Crc64Ms.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u64, 0x75d4b74f024eceea), c.final()); +} + +test "CRC-64/REDIS" { + const Crc64Redis = catalog.Crc64Redis; + + try testing.expectEqual(@as(u64, 0xe9c6d914c4b8d9ca), Crc64Redis.hash("123456789")); + + var c = Crc64Redis.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u64, 0xe9c6d914c4b8d9ca), c.final()); +} + +test "CRC-64/WE" { + const Crc64We = catalog.Crc64We; + + try testing.expectEqual(@as(u64, 0x62ec59e3f1a4f00a), Crc64We.hash("123456789")); + + var c = Crc64We.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u64, 0x62ec59e3f1a4f00a), c.final()); +} + +test "CRC-64/XZ" { + const Crc64Xz = catalog.Crc64Xz; + + try testing.expectEqual(@as(u64, 0x995dc9bbdf1939fa), Crc64Xz.hash("123456789")); + + var c = Crc64Xz.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u64, 0x995dc9bbdf1939fa), c.final()); +} + +test "CRC-82/DARC" { + const Crc82Darc = catalog.Crc82Darc; + + try testing.expectEqual(@as(u82, 0x09ea83f625023801fd612), Crc82Darc.hash("123456789")); + + var c = Crc82Darc.init(); + c.update("1234"); + c.update("56789"); + try testing.expectEqual(@as(u82, 0x09ea83f625023801fd612), c.final()); +} diff --git a/tools/crc/catalog.txt b/tools/crc/catalog.txt new file mode 100644 index 000000000000..a4371414477b --- /dev/null +++ b/tools/crc/catalog.txt @@ -0,0 +1,113 @@ +# https://reveng.sourceforge.io/crc-catalogue/all.htm +width=3 poly=0x3 init=0x0 refin=false refout=false xorout=0x7 check=0x4 residue=0x2 name="CRC-3/GSM" +width=3 poly=0x3 init=0x7 refin=true refout=true xorout=0x0 check=0x6 residue=0x0 name="CRC-3/ROHC" +width=4 poly=0x3 init=0x0 refin=true refout=true xorout=0x0 check=0x7 residue=0x0 name="CRC-4/G-704" +width=4 poly=0x3 init=0xf refin=false refout=false xorout=0xf check=0xb residue=0x2 name="CRC-4/INTERLAKEN" +width=5 poly=0x09 init=0x09 refin=false refout=false xorout=0x00 check=0x00 residue=0x00 name="CRC-5/EPC-C1G2" +width=5 poly=0x15 init=0x00 refin=true refout=true xorout=0x00 check=0x07 residue=0x00 name="CRC-5/G-704" +width=5 poly=0x05 init=0x1f refin=true refout=true xorout=0x1f check=0x19 residue=0x06 name="CRC-5/USB" +width=6 poly=0x27 init=0x3f refin=false refout=false xorout=0x00 check=0x0d residue=0x00 name="CRC-6/CDMA2000-A" +width=6 poly=0x07 init=0x3f refin=false refout=false xorout=0x00 check=0x3b residue=0x00 name="CRC-6/CDMA2000-B" +width=6 poly=0x19 init=0x00 refin=true refout=true xorout=0x00 check=0x26 residue=0x00 name="CRC-6/DARC" +width=6 poly=0x03 init=0x00 refin=true refout=true xorout=0x00 check=0x06 residue=0x00 name="CRC-6/G-704" +width=6 poly=0x2f init=0x00 refin=false refout=false xorout=0x3f check=0x13 residue=0x3a name="CRC-6/GSM" +width=7 poly=0x09 init=0x00 refin=false refout=false xorout=0x00 check=0x75 residue=0x00 name="CRC-7/MMC" +width=7 poly=0x4f init=0x7f refin=true refout=true xorout=0x00 check=0x53 residue=0x00 name="CRC-7/ROHC" +width=7 poly=0x45 init=0x00 refin=false refout=false xorout=0x00 check=0x61 residue=0x00 name="CRC-7/UMTS" +width=8 poly=0x2f init=0xff refin=false refout=false xorout=0xff check=0xdf residue=0x42 name="CRC-8/AUTOSAR" +width=8 poly=0xa7 init=0x00 refin=true refout=true xorout=0x00 check=0x26 residue=0x00 name="CRC-8/BLUETOOTH" +width=8 poly=0x9b init=0xff refin=false refout=false xorout=0x00 check=0xda residue=0x00 name="CRC-8/CDMA2000" +width=8 poly=0x39 init=0x00 refin=true refout=true xorout=0x00 check=0x15 residue=0x00 name="CRC-8/DARC" +width=8 poly=0xd5 init=0x00 refin=false refout=false xorout=0x00 check=0xbc residue=0x00 name="CRC-8/DVB-S2" +width=8 poly=0x1d init=0x00 refin=false refout=false xorout=0x00 check=0x37 residue=0x00 name="CRC-8/GSM-A" +width=8 poly=0x49 init=0x00 refin=false refout=false xorout=0xff check=0x94 residue=0x53 name="CRC-8/GSM-B" +width=8 poly=0x1d init=0xff refin=false refout=false xorout=0x00 check=0xb4 residue=0x00 name="CRC-8/HITAG" +width=8 poly=0x07 init=0x00 refin=false refout=false xorout=0x55 check=0xa1 residue=0xac name="CRC-8/I-432-1" +width=8 poly=0x1d init=0xfd refin=false refout=false xorout=0x00 check=0x7e residue=0x00 name="CRC-8/I-CODE" +width=8 poly=0x9b init=0x00 refin=false refout=false xorout=0x00 check=0xea residue=0x00 name="CRC-8/LTE" +width=8 poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xa1 residue=0x00 name="CRC-8/MAXIM-DOW" +width=8 poly=0x1d init=0xc7 refin=false refout=false xorout=0x00 check=0x99 residue=0x00 name="CRC-8/MIFARE-MAD" +width=8 poly=0x31 init=0xff refin=false refout=false xorout=0x00 check=0xf7 residue=0x00 name="CRC-8/NRSC-5" +width=8 poly=0x2f init=0x00 refin=false refout=false xorout=0x00 check=0x3e residue=0x00 name="CRC-8/OPENSAFETY" +width=8 poly=0x07 init=0xff refin=true refout=true xorout=0x00 check=0xd0 residue=0x00 name="CRC-8/ROHC" +width=8 poly=0x1d init=0xff refin=false refout=false xorout=0xff check=0x4b residue=0xc4 name="CRC-8/SAE-J1850" +width=8 poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4 residue=0x00 name="CRC-8/SMBUS" +width=8 poly=0x1d init=0xff refin=true refout=true xorout=0x00 check=0x97 residue=0x00 name="CRC-8/TECH-3250" +width=8 poly=0x9b init=0x00 refin=true refout=true xorout=0x00 check=0x25 residue=0x00 name="CRC-8/WCDMA" +width=10 poly=0x233 init=0x000 refin=false refout=false xorout=0x000 check=0x199 residue=0x000 name="CRC-10/ATM" +width=10 poly=0x3d9 init=0x3ff refin=false refout=false xorout=0x000 check=0x233 residue=0x000 name="CRC-10/CDMA2000" +width=10 poly=0x175 init=0x000 refin=false refout=false xorout=0x3ff check=0x12a residue=0x0c6 name="CRC-10/GSM" +width=11 poly=0x385 init=0x01a refin=false refout=false xorout=0x000 check=0x5a3 residue=0x000 name="CRC-11/FLEXRAY" +width=11 poly=0x307 init=0x000 refin=false refout=false xorout=0x000 check=0x061 residue=0x000 name="CRC-11/UMTS" +width=12 poly=0xf13 init=0xfff refin=false refout=false xorout=0x000 check=0xd4d residue=0x000 name="CRC-12/CDMA2000" +width=12 poly=0x80f init=0x000 refin=false refout=false xorout=0x000 check=0xf5b residue=0x000 name="CRC-12/DECT" +width=12 poly=0xd31 init=0x000 refin=false refout=false xorout=0xfff check=0xb34 residue=0x178 name="CRC-12/GSM" +width=12 poly=0x80f init=0x000 refin=false refout=true xorout=0x000 check=0xdaf residue=0x000 name="CRC-12/UMTS" +width=13 poly=0x1cf5 init=0x0000 refin=false refout=false xorout=0x0000 check=0x04fa residue=0x0000 name="CRC-13/BBC" +width=14 poly=0x0805 init=0x0000 refin=true refout=true xorout=0x0000 check=0x082d residue=0x0000 name="CRC-14/DARC" +width=14 poly=0x202d init=0x0000 refin=false refout=false xorout=0x3fff check=0x30ae residue=0x031e name="CRC-14/GSM" +width=15 poly=0x4599 init=0x0000 refin=false refout=false xorout=0x0000 check=0x059e residue=0x0000 name="CRC-15/CAN" +width=15 poly=0x6815 init=0x0000 refin=false refout=false xorout=0x0001 check=0x2566 residue=0x6815 name="CRC-15/MPT1327" +width=16 poly=0x8005 init=0x0000 refin=true refout=true xorout=0x0000 check=0xbb3d residue=0x0000 name="CRC-16/ARC" +width=16 poly=0xc867 init=0xffff refin=false refout=false xorout=0x0000 check=0x4c06 residue=0x0000 name="CRC-16/CDMA2000" +width=16 poly=0x8005 init=0xffff refin=false refout=false xorout=0x0000 check=0xaee7 residue=0x0000 name="CRC-16/CMS" +width=16 poly=0x8005 init=0x800d refin=false refout=false xorout=0x0000 check=0x9ecf residue=0x0000 name="CRC-16/DDS-110" +width=16 poly=0x0589 init=0x0000 refin=false refout=false xorout=0x0001 check=0x007e residue=0x0589 name="CRC-16/DECT-R" +width=16 poly=0x0589 init=0x0000 refin=false refout=false xorout=0x0000 check=0x007f residue=0x0000 name="CRC-16/DECT-X" +width=16 poly=0x3d65 init=0x0000 refin=true refout=true xorout=0xffff check=0xea82 residue=0x66c5 name="CRC-16/DNP" +width=16 poly=0x3d65 init=0x0000 refin=false refout=false xorout=0xffff check=0xc2b7 residue=0xa366 name="CRC-16/EN-13757" +width=16 poly=0x1021 init=0xffff refin=false refout=false xorout=0xffff check=0xd64e residue=0x1d0f name="CRC-16/GENIBUS" +width=16 poly=0x1021 init=0x0000 refin=false refout=false xorout=0xffff check=0xce3c residue=0x1d0f name="CRC-16/GSM" +width=16 poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1 residue=0x0000 name="CRC-16/IBM-3740" +width=16 poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff check=0x906e residue=0xf0b8 name="CRC-16/IBM-SDLC" +width=16 poly=0x1021 init=0xc6c6 refin=true refout=true xorout=0x0000 check=0xbf05 residue=0x0000 name="CRC-16/ISO-IEC-14443-3-A" +width=16 poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 check=0x2189 residue=0x0000 name="CRC-16/KERMIT" +width=16 poly=0x6f63 init=0x0000 refin=false refout=false xorout=0x0000 check=0xbdf4 residue=0x0000 name="CRC-16/LJ1200" +width=16 poly=0x5935 init=0xffff refin=false refout=false xorout=0x0000 check=0x772b residue=0x0000 name="CRC-16/M17" +width=16 poly=0x8005 init=0x0000 refin=true refout=true xorout=0xffff check=0x44c2 residue=0xb001 name="CRC-16/MAXIM-DOW" +width=16 poly=0x1021 init=0xffff refin=true refout=true xorout=0x0000 check=0x6f91 residue=0x0000 name="CRC-16/MCRF4XX" +width=16 poly=0x8005 init=0xffff refin=true refout=true xorout=0x0000 check=0x4b37 residue=0x0000 name="CRC-16/MODBUS" +width=16 poly=0x080b init=0xffff refin=true refout=true xorout=0x0000 check=0xa066 residue=0x0000 name="CRC-16/NRSC-5" +width=16 poly=0x5935 init=0x0000 refin=false refout=false xorout=0x0000 check=0x5d38 residue=0x0000 name="CRC-16/OPENSAFETY-A" +width=16 poly=0x755b init=0x0000 refin=false refout=false xorout=0x0000 check=0x20fe residue=0x0000 name="CRC-16/OPENSAFETY-B" +width=16 poly=0x1dcf init=0xffff refin=false refout=false xorout=0xffff check=0xa819 residue=0xe394 name="CRC-16/PROFIBUS" +width=16 poly=0x1021 init=0xb2aa refin=true refout=true xorout=0x0000 check=0x63d0 residue=0x0000 name="CRC-16/RIELLO" +width=16 poly=0x1021 init=0x1d0f refin=false refout=false xorout=0x0000 check=0xe5cc residue=0x0000 name="CRC-16/SPI-FUJITSU" +width=16 poly=0x8bb7 init=0x0000 refin=false refout=false xorout=0x0000 check=0xd0db residue=0x0000 name="CRC-16/T10-DIF" +width=16 poly=0xa097 init=0x0000 refin=false refout=false xorout=0x0000 check=0x0fb3 residue=0x0000 name="CRC-16/TELEDISK" +width=16 poly=0x1021 init=0x89ec refin=true refout=true xorout=0x0000 check=0x26b1 residue=0x0000 name="CRC-16/TMS37157" +width=16 poly=0x8005 init=0x0000 refin=false refout=false xorout=0x0000 check=0xfee8 residue=0x0000 name="CRC-16/UMTS" +width=16 poly=0x8005 init=0xffff refin=true refout=true xorout=0xffff check=0xb4c8 residue=0xb001 name="CRC-16/USB" +width=16 poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 check=0x31c3 residue=0x0000 name="CRC-16/XMODEM" +width=17 poly=0x1685b init=0x00000 refin=false refout=false xorout=0x00000 check=0x04f03 residue=0x00000 name="CRC-17/CAN-FD" +width=21 poly=0x102899 init=0x000000 refin=false refout=false xorout=0x000000 check=0x0ed841 residue=0x000000 name="CRC-21/CAN-FD" +width=24 poly=0x00065b init=0x555555 refin=true refout=true xorout=0x000000 check=0xc25a56 residue=0x000000 name="CRC-24/BLE" +width=24 poly=0x5d6dcb init=0xfedcba refin=false refout=false xorout=0x000000 check=0x7979bd residue=0x000000 name="CRC-24/FLEXRAY-A" +width=24 poly=0x5d6dcb init=0xabcdef refin=false refout=false xorout=0x000000 check=0x1f23b8 residue=0x000000 name="CRC-24/FLEXRAY-B" +width=24 poly=0x328b63 init=0xffffff refin=false refout=false xorout=0xffffff check=0xb4f3e6 residue=0x144e63 name="CRC-24/INTERLAKEN" +width=24 poly=0x864cfb init=0x000000 refin=false refout=false xorout=0x000000 check=0xcde703 residue=0x000000 name="CRC-24/LTE-A" +width=24 poly=0x800063 init=0x000000 refin=false refout=false xorout=0x000000 check=0x23ef52 residue=0x000000 name="CRC-24/LTE-B" +width=24 poly=0x864cfb init=0xb704ce refin=false refout=false xorout=0x000000 check=0x21cf02 residue=0x000000 name="CRC-24/OPENPGP" +width=24 poly=0x800063 init=0xffffff refin=false refout=false xorout=0xffffff check=0x200fa5 residue=0x800fe3 name="CRC-24/OS-9" +width=30 poly=0x2030b9c7 init=0x3fffffff refin=false refout=false xorout=0x3fffffff check=0x04c34abf residue=0x34efa55a name="CRC-30/CDMA" +width=31 poly=0x04c11db7 init=0x7fffffff refin=false refout=false xorout=0x7fffffff check=0x0ce9e46c residue=0x4eaf26f1 name="CRC-31/PHILIPS" +width=32 poly=0x814141ab init=0x00000000 refin=false refout=false xorout=0x00000000 check=0x3010bf7f residue=0x00000000 name="CRC-32/AIXM" +width=32 poly=0xf4acfb13 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0x1697d06a residue=0x904cddbf name="CRC-32/AUTOSAR" +width=32 poly=0xa833982b init=0xffffffff refin=true refout=true xorout=0xffffffff check=0x87315576 residue=0x45270551 name="CRC-32/BASE91-D" +width=32 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0xffffffff check=0xfc891918 residue=0xc704dd7b name="CRC-32/BZIP2" +width=32 poly=0x8001801b init=0x00000000 refin=true refout=true xorout=0x00000000 check=0x6ec2edc4 residue=0x00000000 name="CRC-32/CD-ROM-EDC" +width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680 residue=0xc704dd7b name="CRC-32/CKSUM" +width=32 poly=0x1edc6f41 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xe3069283 residue=0xb798b438 name="CRC-32/ISCSI" +width=32 poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xcbf43926 residue=0xdebb20e3 name="CRC-32/ISO-HDLC" +width=32 poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0x00000000 check=0x340bc6d9 residue=0x00000000 name="CRC-32/JAMCRC" +width=32 poly=0x741b8cd7 init=0xffffffff refin=true refout=true xorout=0x00000000 check=0xd2c22f51 residue=0x00000000 name="CRC-32/MEF" +width=32 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0x00000000 check=0x0376e6e7 residue=0x00000000 name="CRC-32/MPEG-2" +width=32 poly=0x000000af init=0x00000000 refin=false refout=false xorout=0x00000000 check=0xbd0be338 residue=0x00000000 name="CRC-32/XFER" +width=40 poly=0x0004820009 init=0x0000000000 refin=false refout=false xorout=0xffffffffff check=0xd4164fc646 residue=0xc4ff8071ff name="CRC-40/GSM" +width=64 poly=0x42f0e1eba9ea3693 init=0x0000000000000000 refin=false refout=false xorout=0x0000000000000000 check=0x6c40df5f0b497347 residue=0x0000000000000000 name="CRC-64/ECMA-182" +width=64 poly=0x000000000000001b init=0xffffffffffffffff refin=true refout=true xorout=0xffffffffffffffff check=0xb90956c775a41001 residue=0x5300000000000000 name="CRC-64/GO-ISO" +width=64 poly=0x259c84cba6426349 init=0xffffffffffffffff refin=true refout=true xorout=0x0000000000000000 check=0x75d4b74f024eceea residue=0x0000000000000000 name="CRC-64/MS" +width=64 poly=0xad93d23594c935a9 init=0x0000000000000000 refin=true refout=true xorout=0x0000000000000000 check=0xe9c6d914c4b8d9ca residue=0x0000000000000000 name="CRC-64/REDIS" +width=64 poly=0x42f0e1eba9ea3693 init=0xffffffffffffffff refin=false refout=false xorout=0xffffffffffffffff check=0x62ec59e3f1a4f00a residue=0xfcacbebd5931a992 name="CRC-64/WE" +width=64 poly=0x42f0e1eba9ea3693 init=0xffffffffffffffff refin=true refout=true xorout=0xffffffffffffffff check=0x995dc9bbdf1939fa residue=0x49958c9abd7d353f name="CRC-64/XZ" +width=82 poly=0x0308c0111011401440411 init=0x000000000000000000000 refin=true refout=true xorout=0x000000000000000000000 check=0x09ea83f625023801fd612 residue=0x000000000000000000000 name="CRC-82/DARC" diff --git a/tools/update_crc_catalog.zig b/tools/update_crc_catalog.zig new file mode 100644 index 000000000000..8182e8d810bf --- /dev/null +++ b/tools/update_crc_catalog.zig @@ -0,0 +1,169 @@ +const std = @import("std"); +const fs = std.fs; +const mem = std.mem; +const ascii = std.ascii; + +const catalog_txt = @embedFile("crc/catalog.txt"); + +pub fn main() anyerror!void { + var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + + const args = try std.process.argsAlloc(arena); + if (args.len <= 1) { + usageAndExit(std.io.getStdErr(), args[0], 1); + } + + const zig_src_root = args[1]; + if (mem.startsWith(u8, zig_src_root, "-")) { + usageAndExit(std.io.getStdErr(), args[0], 1); + } + + var zig_src_dir = try fs.cwd().openDir(zig_src_root, .{}); + defer zig_src_dir.close(); + + const target_sub_path = try fs.path.join(arena, &.{ "lib", "std", "hash", "crc" }); + var target_dir = try zig_src_dir.makeOpenPath(target_sub_path, .{}); + defer target_dir.close(); + + var zig_code_file = try target_dir.createFile("catalog.zig", .{}); + defer zig_code_file.close(); + + var cbw = std.io.bufferedWriter(zig_code_file.writer()); + defer cbw.flush() catch unreachable; + const code_writer = cbw.writer(); + + try code_writer.writeAll( + \\//! This file is auto-generated by tools/update_crc_catalog.zig. + \\ + \\const Crc = @import("../crc.zig").Crc; + \\ + \\test { + \\ _ = @import("catalog_test.zig"); + \\} + \\ + ); + + var zig_test_file = try target_dir.createFile("catalog_test.zig", .{}); + defer zig_test_file.close(); + + var tbw = std.io.bufferedWriter(zig_test_file.writer()); + defer tbw.flush() catch unreachable; + const test_writer = tbw.writer(); + + try test_writer.writeAll( + \\//! This file is auto-generated by tools/update_crc_catalog.zig. + \\ + \\const std = @import("../../std.zig"); + \\const testing = std.testing; + \\const catalog = @import("catalog.zig"); + \\ + ); + + var stream = std.io.fixedBufferStream(catalog_txt); + const reader = stream.reader(); + + while (try reader.readUntilDelimiterOrEofAlloc(arena, '\n', std.math.maxInt(usize))) |line| { + if (line.len == 0 or line[0] == '#') + continue; + + var width: []const u8 = undefined; + var poly: []const u8 = undefined; + var init: []const u8 = undefined; + var refin: []const u8 = undefined; + var refout: []const u8 = undefined; + var xorout: []const u8 = undefined; + var check: []const u8 = undefined; + var residue: []const u8 = undefined; + var name: []const u8 = undefined; + + var it = mem.split(u8, line, " "); + while (it.next()) |property| { + const i = mem.indexOf(u8, property, "=").?; + const key = property[0..i]; + const value = property[i + 1 ..]; + if (mem.eql(u8, key, "width")) { + width = value; + } else if (mem.eql(u8, key, "poly")) { + poly = value; + } else if (mem.eql(u8, key, "init")) { + init = value; + } else if (mem.eql(u8, key, "refin")) { + refin = value; + } else if (mem.eql(u8, key, "refout")) { + refout = value; + } else if (mem.eql(u8, key, "xorout")) { + xorout = value; + } else if (mem.eql(u8, key, "check")) { + check = value; + } else if (mem.eql(u8, key, "residue")) { + residue = value; + } else if (mem.eql(u8, key, "name")) { + name = mem.trim(u8, value, "\""); + } else { + unreachable; + } + } + + const snakecase = try ascii.allocLowerString(arena, name); + defer arena.free(snakecase); + + _ = mem.replace(u8, snakecase, "-", "_", snakecase); + _ = mem.replace(u8, snakecase, "/", "_", snakecase); + + var buf = try std.ArrayList(u8).initCapacity(arena, snakecase.len); + defer buf.deinit(); + + var prev: u8 = 0; + for (snakecase) |c, i| { + if (c == '_') { + // do nothing + } else if (i == 0) { + buf.appendAssumeCapacity(ascii.toUpper(c)); + } else if (prev == '_') { + buf.appendAssumeCapacity(ascii.toUpper(c)); + } else { + buf.appendAssumeCapacity(c); + } + prev = c; + } + + const camelcase = buf.items; + + try code_writer.writeAll(try std.fmt.allocPrint(arena, + \\ + \\pub const {s} = Crc(u{s}, .{{ + \\ .polynomial = {s}, + \\ .initial = {s}, + \\ .reflect_input = {s}, + \\ .reflect_output = {s}, + \\ .xor_output = {s}, + \\}}); + \\ + , .{ camelcase, width, poly, init, refin, refout, xorout })); + + try test_writer.writeAll(try std.fmt.allocPrint(arena, + \\ + \\test "{0s}" {{ + \\ const {1s} = catalog.{1s}; + \\ + \\ try testing.expectEqual(@as(u{2s}, {3s}), {1s}.hash("123456789")); + \\ + \\ var c = {1s}.init(); + \\ c.update("1234"); + \\ c.update("56789"); + \\ try testing.expectEqual(@as(u{2s}, {3s}), c.final()); + \\}} + \\ + , .{ name, camelcase, width, check })); + } +} + +fn usageAndExit(file: fs.File, arg0: []const u8, code: u8) noreturn { + file.writer().print( + \\Usage: {s} /path/git/zig + \\ + , .{arg0}) catch std.process.exit(1); + std.process.exit(code); +}