zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 3a11c86837562dd6caf143e21ca8562a8374b08a (tree)
parent 32b44497e228c676de17d6c2a2ce78cf145a97c9
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Fri, 26 Jun 2026 15:25:42 -0700

std.hash.crc: fix namespace issues

in accordance with the style guide in the langref

Diffstat:
Mlib/std/debug/ElfFile.zig | 2+-
Mlib/std/hash.zig | 3+--
Mlib/std/hash/benchmark.zig | 2+-
Mlib/std/hash/crc.zig | 347+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Dlib/std/hash/crc/impl.zig | 103-------------------------------------------------------------------------------
5 files changed, 220 insertions(+), 237 deletions(-)

diff --git a/lib/std/debug/ElfFile.zig b/lib/std/debug/ElfFile.zig @@ -455,7 +455,7 @@ fn loadInner( }; if (opt_crc) |crc| { - if (std.hash.crc.Crc32.hash(mapped_mem) != crc) { + if (std.hash.Crc32.hash(mapped_mem) != crc) { return error.CrcMismatch; } } diff --git a/lib/std/hash.zig b/lib/std/hash.zig @@ -5,9 +5,8 @@ pub const autoHash = auto_hash.autoHash; pub const autoHashStrat = auto_hash.hash; pub const Strategy = auto_hash.HashStrategy; -// pub for polynomials + generic crc32 construction pub const crc = @import("hash/crc.zig"); -pub const Crc32 = crc.Crc32; +pub const Crc32 = crc.Crc32IsoHdlc; const fnv = @import("hash/fnv.zig"); pub const Fnv1a_32 = fnv.Fnv1a_32; diff --git a/lib/std/hash/benchmark.zig b/lib/std/hash/benchmark.zig @@ -61,7 +61,7 @@ const hashes = [_]Hash{ .init_default = true, }, Hash{ - .ty = hash.crc.Crc32, + .ty = hash.Crc32, .name = "crc32", }, Hash{ diff --git a/lib/std/hash/crc.zig b/lib/std/hash/crc.zig @@ -1,31 +1,19 @@ -//! This file is auto-generated by tools/update_crc_catalog.zig. +//! This file is maintained with the help of tools/update_crc_catalog.zig. const builtin = @import("builtin"); -const impl = @import("crc/impl.zig"); -pub const Crc = impl.Crc; -pub const Polynomial = impl.Polynomial; -pub const Crc32WithPoly = impl.Crc32WithPoly; -pub const Crc32SmallWithPoly = impl.Crc32SmallWithPoly; - -pub const Crc32 = Crc32IsoHdlc; - -test { - _ = @import("crc/test.zig"); -} - -pub const Crc32Iscsi = switch (builtin.cpu.hasAll(.x86, &.{ .@"64bit", .crc32 })) { - true => @import("crc/Crc32c.zig"), - else => Crc(u32, .{ +pub const Crc32Iscsi = if (builtin.cpu.hasAll(.x86, &.{ .@"64bit", .crc32 })) + @import("crc/Crc32c.zig") +else + Generic(u32, .{ .polynomial = 0x1edc6f41, .initial = 0xffffffff, .reflect_input = true, .reflect_output = true, .xor_output = 0xffffffff, - }), -}; + }); -pub const Crc3Gsm = Crc(u3, .{ +pub const Crc3Gsm = Generic(u3, .{ .polynomial = 0x3, .initial = 0x0, .reflect_input = false, @@ -33,7 +21,7 @@ pub const Crc3Gsm = Crc(u3, .{ .xor_output = 0x7, }); -pub const Crc3Rohc = Crc(u3, .{ +pub const Crc3Rohc = Generic(u3, .{ .polynomial = 0x3, .initial = 0x7, .reflect_input = true, @@ -41,7 +29,7 @@ pub const Crc3Rohc = Crc(u3, .{ .xor_output = 0x0, }); -pub const Crc4G704 = Crc(u4, .{ +pub const Crc4G704 = Generic(u4, .{ .polynomial = 0x3, .initial = 0x0, .reflect_input = true, @@ -49,7 +37,7 @@ pub const Crc4G704 = Crc(u4, .{ .xor_output = 0x0, }); -pub const Crc4Interlaken = Crc(u4, .{ +pub const Crc4Interlaken = Generic(u4, .{ .polynomial = 0x3, .initial = 0xf, .reflect_input = false, @@ -57,7 +45,7 @@ pub const Crc4Interlaken = Crc(u4, .{ .xor_output = 0xf, }); -pub const Crc5EpcC1g2 = Crc(u5, .{ +pub const Crc5EpcC1g2 = Generic(u5, .{ .polynomial = 0x09, .initial = 0x09, .reflect_input = false, @@ -65,7 +53,7 @@ pub const Crc5EpcC1g2 = Crc(u5, .{ .xor_output = 0x00, }); -pub const Crc5G704 = Crc(u5, .{ +pub const Crc5G704 = Generic(u5, .{ .polynomial = 0x15, .initial = 0x00, .reflect_input = true, @@ -73,7 +61,7 @@ pub const Crc5G704 = Crc(u5, .{ .xor_output = 0x00, }); -pub const Crc5Usb = Crc(u5, .{ +pub const Crc5Usb = Generic(u5, .{ .polynomial = 0x05, .initial = 0x1f, .reflect_input = true, @@ -81,7 +69,7 @@ pub const Crc5Usb = Crc(u5, .{ .xor_output = 0x1f, }); -pub const Crc6Cdma2000A = Crc(u6, .{ +pub const Crc6Cdma2000A = Generic(u6, .{ .polynomial = 0x27, .initial = 0x3f, .reflect_input = false, @@ -89,7 +77,7 @@ pub const Crc6Cdma2000A = Crc(u6, .{ .xor_output = 0x00, }); -pub const Crc6Cdma2000B = Crc(u6, .{ +pub const Crc6Cdma2000B = Generic(u6, .{ .polynomial = 0x07, .initial = 0x3f, .reflect_input = false, @@ -97,7 +85,7 @@ pub const Crc6Cdma2000B = Crc(u6, .{ .xor_output = 0x00, }); -pub const Crc6Darc = Crc(u6, .{ +pub const Crc6Darc = Generic(u6, .{ .polynomial = 0x19, .initial = 0x00, .reflect_input = true, @@ -105,7 +93,7 @@ pub const Crc6Darc = Crc(u6, .{ .xor_output = 0x00, }); -pub const Crc6G704 = Crc(u6, .{ +pub const Crc6G704 = Generic(u6, .{ .polynomial = 0x03, .initial = 0x00, .reflect_input = true, @@ -113,7 +101,7 @@ pub const Crc6G704 = Crc(u6, .{ .xor_output = 0x00, }); -pub const Crc6Gsm = Crc(u6, .{ +pub const Crc6Gsm = Generic(u6, .{ .polynomial = 0x2f, .initial = 0x00, .reflect_input = false, @@ -121,7 +109,7 @@ pub const Crc6Gsm = Crc(u6, .{ .xor_output = 0x3f, }); -pub const Crc7Mmc = Crc(u7, .{ +pub const Crc7Mmc = Generic(u7, .{ .polynomial = 0x09, .initial = 0x00, .reflect_input = false, @@ -129,7 +117,7 @@ pub const Crc7Mmc = Crc(u7, .{ .xor_output = 0x00, }); -pub const Crc7Rohc = Crc(u7, .{ +pub const Crc7Rohc = Generic(u7, .{ .polynomial = 0x4f, .initial = 0x7f, .reflect_input = true, @@ -137,7 +125,7 @@ pub const Crc7Rohc = Crc(u7, .{ .xor_output = 0x00, }); -pub const Crc7Umts = Crc(u7, .{ +pub const Crc7Umts = Generic(u7, .{ .polynomial = 0x45, .initial = 0x00, .reflect_input = false, @@ -145,7 +133,7 @@ pub const Crc7Umts = Crc(u7, .{ .xor_output = 0x00, }); -pub const Crc8Autosar = Crc(u8, .{ +pub const Crc8Autosar = Generic(u8, .{ .polynomial = 0x2f, .initial = 0xff, .reflect_input = false, @@ -153,7 +141,7 @@ pub const Crc8Autosar = Crc(u8, .{ .xor_output = 0xff, }); -pub const Crc8Bluetooth = Crc(u8, .{ +pub const Crc8Bluetooth = Generic(u8, .{ .polynomial = 0xa7, .initial = 0x00, .reflect_input = true, @@ -161,7 +149,7 @@ pub const Crc8Bluetooth = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Cdma2000 = Crc(u8, .{ +pub const Crc8Cdma2000 = Generic(u8, .{ .polynomial = 0x9b, .initial = 0xff, .reflect_input = false, @@ -169,7 +157,7 @@ pub const Crc8Cdma2000 = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Darc = Crc(u8, .{ +pub const Crc8Darc = Generic(u8, .{ .polynomial = 0x39, .initial = 0x00, .reflect_input = true, @@ -177,7 +165,7 @@ pub const Crc8Darc = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8DvbS2 = Crc(u8, .{ +pub const Crc8DvbS2 = Generic(u8, .{ .polynomial = 0xd5, .initial = 0x00, .reflect_input = false, @@ -185,7 +173,7 @@ pub const Crc8DvbS2 = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8GsmA = Crc(u8, .{ +pub const Crc8GsmA = Generic(u8, .{ .polynomial = 0x1d, .initial = 0x00, .reflect_input = false, @@ -193,7 +181,7 @@ pub const Crc8GsmA = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8GsmB = Crc(u8, .{ +pub const Crc8GsmB = Generic(u8, .{ .polynomial = 0x49, .initial = 0x00, .reflect_input = false, @@ -201,7 +189,7 @@ pub const Crc8GsmB = Crc(u8, .{ .xor_output = 0xff, }); -pub const Crc8Hitag = Crc(u8, .{ +pub const Crc8Hitag = Generic(u8, .{ .polynomial = 0x1d, .initial = 0xff, .reflect_input = false, @@ -209,7 +197,7 @@ pub const Crc8Hitag = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8I4321 = Crc(u8, .{ +pub const Crc8I4321 = Generic(u8, .{ .polynomial = 0x07, .initial = 0x00, .reflect_input = false, @@ -217,7 +205,7 @@ pub const Crc8I4321 = Crc(u8, .{ .xor_output = 0x55, }); -pub const Crc8ICode = Crc(u8, .{ +pub const Crc8ICode = Generic(u8, .{ .polynomial = 0x1d, .initial = 0xfd, .reflect_input = false, @@ -225,7 +213,7 @@ pub const Crc8ICode = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Lte = Crc(u8, .{ +pub const Crc8Lte = Generic(u8, .{ .polynomial = 0x9b, .initial = 0x00, .reflect_input = false, @@ -233,7 +221,7 @@ pub const Crc8Lte = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8MaximDow = Crc(u8, .{ +pub const Crc8MaximDow = Generic(u8, .{ .polynomial = 0x31, .initial = 0x00, .reflect_input = true, @@ -241,7 +229,7 @@ pub const Crc8MaximDow = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8MifareMad = Crc(u8, .{ +pub const Crc8MifareMad = Generic(u8, .{ .polynomial = 0x1d, .initial = 0xc7, .reflect_input = false, @@ -249,7 +237,7 @@ pub const Crc8MifareMad = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Nrsc5 = Crc(u8, .{ +pub const Crc8Nrsc5 = Generic(u8, .{ .polynomial = 0x31, .initial = 0xff, .reflect_input = false, @@ -257,7 +245,7 @@ pub const Crc8Nrsc5 = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Opensafety = Crc(u8, .{ +pub const Crc8Opensafety = Generic(u8, .{ .polynomial = 0x2f, .initial = 0x00, .reflect_input = false, @@ -265,7 +253,7 @@ pub const Crc8Opensafety = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Rohc = Crc(u8, .{ +pub const Crc8Rohc = Generic(u8, .{ .polynomial = 0x07, .initial = 0xff, .reflect_input = true, @@ -273,7 +261,7 @@ pub const Crc8Rohc = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8SaeJ1850 = Crc(u8, .{ +pub const Crc8SaeJ1850 = Generic(u8, .{ .polynomial = 0x1d, .initial = 0xff, .reflect_input = false, @@ -281,7 +269,7 @@ pub const Crc8SaeJ1850 = Crc(u8, .{ .xor_output = 0xff, }); -pub const Crc8Smbus = Crc(u8, .{ +pub const Crc8Smbus = Generic(u8, .{ .polynomial = 0x07, .initial = 0x00, .reflect_input = false, @@ -289,7 +277,7 @@ pub const Crc8Smbus = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Tech3250 = Crc(u8, .{ +pub const Crc8Tech3250 = Generic(u8, .{ .polynomial = 0x1d, .initial = 0xff, .reflect_input = true, @@ -297,7 +285,7 @@ pub const Crc8Tech3250 = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc8Wcdma = Crc(u8, .{ +pub const Crc8Wcdma = Generic(u8, .{ .polynomial = 0x9b, .initial = 0x00, .reflect_input = true, @@ -305,7 +293,7 @@ pub const Crc8Wcdma = Crc(u8, .{ .xor_output = 0x00, }); -pub const Crc10Atm = Crc(u10, .{ +pub const Crc10Atm = Generic(u10, .{ .polynomial = 0x233, .initial = 0x000, .reflect_input = false, @@ -313,7 +301,7 @@ pub const Crc10Atm = Crc(u10, .{ .xor_output = 0x000, }); -pub const Crc10Cdma2000 = Crc(u10, .{ +pub const Crc10Cdma2000 = Generic(u10, .{ .polynomial = 0x3d9, .initial = 0x3ff, .reflect_input = false, @@ -321,7 +309,7 @@ pub const Crc10Cdma2000 = Crc(u10, .{ .xor_output = 0x000, }); -pub const Crc10Gsm = Crc(u10, .{ +pub const Crc10Gsm = Generic(u10, .{ .polynomial = 0x175, .initial = 0x000, .reflect_input = false, @@ -329,7 +317,7 @@ pub const Crc10Gsm = Crc(u10, .{ .xor_output = 0x3ff, }); -pub const Crc11Flexray = Crc(u11, .{ +pub const Crc11Flexray = Generic(u11, .{ .polynomial = 0x385, .initial = 0x01a, .reflect_input = false, @@ -337,7 +325,7 @@ pub const Crc11Flexray = Crc(u11, .{ .xor_output = 0x000, }); -pub const Crc11Umts = Crc(u11, .{ +pub const Crc11Umts = Generic(u11, .{ .polynomial = 0x307, .initial = 0x000, .reflect_input = false, @@ -345,7 +333,7 @@ pub const Crc11Umts = Crc(u11, .{ .xor_output = 0x000, }); -pub const Crc12Cdma2000 = Crc(u12, .{ +pub const Crc12Cdma2000 = Generic(u12, .{ .polynomial = 0xf13, .initial = 0xfff, .reflect_input = false, @@ -353,7 +341,7 @@ pub const Crc12Cdma2000 = Crc(u12, .{ .xor_output = 0x000, }); -pub const Crc12Dect = Crc(u12, .{ +pub const Crc12Dect = Generic(u12, .{ .polynomial = 0x80f, .initial = 0x000, .reflect_input = false, @@ -361,7 +349,7 @@ pub const Crc12Dect = Crc(u12, .{ .xor_output = 0x000, }); -pub const Crc12Gsm = Crc(u12, .{ +pub const Crc12Gsm = Generic(u12, .{ .polynomial = 0xd31, .initial = 0x000, .reflect_input = false, @@ -369,7 +357,7 @@ pub const Crc12Gsm = Crc(u12, .{ .xor_output = 0xfff, }); -pub const Crc12Umts = Crc(u12, .{ +pub const Crc12Umts = Generic(u12, .{ .polynomial = 0x80f, .initial = 0x000, .reflect_input = false, @@ -377,7 +365,7 @@ pub const Crc12Umts = Crc(u12, .{ .xor_output = 0x000, }); -pub const Crc13Bbc = Crc(u13, .{ +pub const Crc13Bbc = Generic(u13, .{ .polynomial = 0x1cf5, .initial = 0x0000, .reflect_input = false, @@ -385,7 +373,7 @@ pub const Crc13Bbc = Crc(u13, .{ .xor_output = 0x0000, }); -pub const Crc14Darc = Crc(u14, .{ +pub const Crc14Darc = Generic(u14, .{ .polynomial = 0x0805, .initial = 0x0000, .reflect_input = true, @@ -393,7 +381,7 @@ pub const Crc14Darc = Crc(u14, .{ .xor_output = 0x0000, }); -pub const Crc14Gsm = Crc(u14, .{ +pub const Crc14Gsm = Generic(u14, .{ .polynomial = 0x202d, .initial = 0x0000, .reflect_input = false, @@ -401,7 +389,7 @@ pub const Crc14Gsm = Crc(u14, .{ .xor_output = 0x3fff, }); -pub const Crc15Can = Crc(u15, .{ +pub const Crc15Can = Generic(u15, .{ .polynomial = 0x4599, .initial = 0x0000, .reflect_input = false, @@ -409,7 +397,7 @@ pub const Crc15Can = Crc(u15, .{ .xor_output = 0x0000, }); -pub const Crc15Mpt1327 = Crc(u15, .{ +pub const Crc15Mpt1327 = Generic(u15, .{ .polynomial = 0x6815, .initial = 0x0000, .reflect_input = false, @@ -417,7 +405,7 @@ pub const Crc15Mpt1327 = Crc(u15, .{ .xor_output = 0x0001, }); -pub const Crc16Arc = Crc(u16, .{ +pub const Crc16Arc = Generic(u16, .{ .polynomial = 0x8005, .initial = 0x0000, .reflect_input = true, @@ -425,7 +413,7 @@ pub const Crc16Arc = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Cdma2000 = Crc(u16, .{ +pub const Crc16Cdma2000 = Generic(u16, .{ .polynomial = 0xc867, .initial = 0xffff, .reflect_input = false, @@ -433,7 +421,7 @@ pub const Crc16Cdma2000 = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Cms = Crc(u16, .{ +pub const Crc16Cms = Generic(u16, .{ .polynomial = 0x8005, .initial = 0xffff, .reflect_input = false, @@ -441,7 +429,7 @@ pub const Crc16Cms = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Dds110 = Crc(u16, .{ +pub const Crc16Dds110 = Generic(u16, .{ .polynomial = 0x8005, .initial = 0x800d, .reflect_input = false, @@ -449,7 +437,7 @@ pub const Crc16Dds110 = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16DectR = Crc(u16, .{ +pub const Crc16DectR = Generic(u16, .{ .polynomial = 0x0589, .initial = 0x0000, .reflect_input = false, @@ -457,7 +445,7 @@ pub const Crc16DectR = Crc(u16, .{ .xor_output = 0x0001, }); -pub const Crc16DectX = Crc(u16, .{ +pub const Crc16DectX = Generic(u16, .{ .polynomial = 0x0589, .initial = 0x0000, .reflect_input = false, @@ -465,7 +453,7 @@ pub const Crc16DectX = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Dnp = Crc(u16, .{ +pub const Crc16Dnp = Generic(u16, .{ .polynomial = 0x3d65, .initial = 0x0000, .reflect_input = true, @@ -473,7 +461,7 @@ pub const Crc16Dnp = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16En13757 = Crc(u16, .{ +pub const Crc16En13757 = Generic(u16, .{ .polynomial = 0x3d65, .initial = 0x0000, .reflect_input = false, @@ -481,7 +469,7 @@ pub const Crc16En13757 = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16Genibus = Crc(u16, .{ +pub const Crc16Genibus = Generic(u16, .{ .polynomial = 0x1021, .initial = 0xffff, .reflect_input = false, @@ -489,7 +477,7 @@ pub const Crc16Genibus = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16Gsm = Crc(u16, .{ +pub const Crc16Gsm = Generic(u16, .{ .polynomial = 0x1021, .initial = 0x0000, .reflect_input = false, @@ -497,7 +485,7 @@ pub const Crc16Gsm = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16Ibm3740 = Crc(u16, .{ +pub const Crc16Ibm3740 = Generic(u16, .{ .polynomial = 0x1021, .initial = 0xffff, .reflect_input = false, @@ -505,7 +493,7 @@ pub const Crc16Ibm3740 = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16IbmSdlc = Crc(u16, .{ +pub const Crc16IbmSdlc = Generic(u16, .{ .polynomial = 0x1021, .initial = 0xffff, .reflect_input = true, @@ -513,7 +501,7 @@ pub const Crc16IbmSdlc = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16IsoIec144433A = Crc(u16, .{ +pub const Crc16IsoIec144433A = Generic(u16, .{ .polynomial = 0x1021, .initial = 0xc6c6, .reflect_input = true, @@ -521,7 +509,7 @@ pub const Crc16IsoIec144433A = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Kermit = Crc(u16, .{ +pub const Crc16Kermit = Generic(u16, .{ .polynomial = 0x1021, .initial = 0x0000, .reflect_input = true, @@ -529,7 +517,7 @@ pub const Crc16Kermit = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Lj1200 = Crc(u16, .{ +pub const Crc16Lj1200 = Generic(u16, .{ .polynomial = 0x6f63, .initial = 0x0000, .reflect_input = false, @@ -537,7 +525,7 @@ pub const Crc16Lj1200 = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16M17 = Crc(u16, .{ +pub const Crc16M17 = Generic(u16, .{ .polynomial = 0x5935, .initial = 0xffff, .reflect_input = false, @@ -545,7 +533,7 @@ pub const Crc16M17 = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16MaximDow = Crc(u16, .{ +pub const Crc16MaximDow = Generic(u16, .{ .polynomial = 0x8005, .initial = 0x0000, .reflect_input = true, @@ -553,7 +541,7 @@ pub const Crc16MaximDow = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16Mcrf4xx = Crc(u16, .{ +pub const Crc16Mcrf4xx = Generic(u16, .{ .polynomial = 0x1021, .initial = 0xffff, .reflect_input = true, @@ -561,7 +549,7 @@ pub const Crc16Mcrf4xx = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Modbus = Crc(u16, .{ +pub const Crc16Modbus = Generic(u16, .{ .polynomial = 0x8005, .initial = 0xffff, .reflect_input = true, @@ -569,7 +557,7 @@ pub const Crc16Modbus = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Nrsc5 = Crc(u16, .{ +pub const Crc16Nrsc5 = Generic(u16, .{ .polynomial = 0x080b, .initial = 0xffff, .reflect_input = true, @@ -577,7 +565,7 @@ pub const Crc16Nrsc5 = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16OpensafetyA = Crc(u16, .{ +pub const Crc16OpensafetyA = Generic(u16, .{ .polynomial = 0x5935, .initial = 0x0000, .reflect_input = false, @@ -585,7 +573,7 @@ pub const Crc16OpensafetyA = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16OpensafetyB = Crc(u16, .{ +pub const Crc16OpensafetyB = Generic(u16, .{ .polynomial = 0x755b, .initial = 0x0000, .reflect_input = false, @@ -593,7 +581,7 @@ pub const Crc16OpensafetyB = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Profibus = Crc(u16, .{ +pub const Crc16Profibus = Generic(u16, .{ .polynomial = 0x1dcf, .initial = 0xffff, .reflect_input = false, @@ -601,7 +589,7 @@ pub const Crc16Profibus = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16Riello = Crc(u16, .{ +pub const Crc16Riello = Generic(u16, .{ .polynomial = 0x1021, .initial = 0xb2aa, .reflect_input = true, @@ -609,7 +597,7 @@ pub const Crc16Riello = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16SpiFujitsu = Crc(u16, .{ +pub const Crc16SpiFujitsu = Generic(u16, .{ .polynomial = 0x1021, .initial = 0x1d0f, .reflect_input = false, @@ -617,7 +605,7 @@ pub const Crc16SpiFujitsu = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16T10Dif = Crc(u16, .{ +pub const Crc16T10Dif = Generic(u16, .{ .polynomial = 0x8bb7, .initial = 0x0000, .reflect_input = false, @@ -625,7 +613,7 @@ pub const Crc16T10Dif = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Teledisk = Crc(u16, .{ +pub const Crc16Teledisk = Generic(u16, .{ .polynomial = 0xa097, .initial = 0x0000, .reflect_input = false, @@ -633,7 +621,7 @@ pub const Crc16Teledisk = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Tms37157 = Crc(u16, .{ +pub const Crc16Tms37157 = Generic(u16, .{ .polynomial = 0x1021, .initial = 0x89ec, .reflect_input = true, @@ -641,7 +629,7 @@ pub const Crc16Tms37157 = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Umts = Crc(u16, .{ +pub const Crc16Umts = Generic(u16, .{ .polynomial = 0x8005, .initial = 0x0000, .reflect_input = false, @@ -649,7 +637,7 @@ pub const Crc16Umts = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc16Usb = Crc(u16, .{ +pub const Crc16Usb = Generic(u16, .{ .polynomial = 0x8005, .initial = 0xffff, .reflect_input = true, @@ -657,7 +645,7 @@ pub const Crc16Usb = Crc(u16, .{ .xor_output = 0xffff, }); -pub const Crc16Xmodem = Crc(u16, .{ +pub const Crc16Xmodem = Generic(u16, .{ .polynomial = 0x1021, .initial = 0x0000, .reflect_input = false, @@ -665,7 +653,7 @@ pub const Crc16Xmodem = Crc(u16, .{ .xor_output = 0x0000, }); -pub const Crc17CanFd = Crc(u17, .{ +pub const Crc17CanFd = Generic(u17, .{ .polynomial = 0x1685b, .initial = 0x00000, .reflect_input = false, @@ -673,7 +661,7 @@ pub const Crc17CanFd = Crc(u17, .{ .xor_output = 0x00000, }); -pub const Crc21CanFd = Crc(u21, .{ +pub const Crc21CanFd = Generic(u21, .{ .polynomial = 0x102899, .initial = 0x000000, .reflect_input = false, @@ -681,7 +669,7 @@ pub const Crc21CanFd = Crc(u21, .{ .xor_output = 0x000000, }); -pub const Crc24Ble = Crc(u24, .{ +pub const Crc24Ble = Generic(u24, .{ .polynomial = 0x00065b, .initial = 0x555555, .reflect_input = true, @@ -689,7 +677,7 @@ pub const Crc24Ble = Crc(u24, .{ .xor_output = 0x000000, }); -pub const Crc24FlexrayA = Crc(u24, .{ +pub const Crc24FlexrayA = Generic(u24, .{ .polynomial = 0x5d6dcb, .initial = 0xfedcba, .reflect_input = false, @@ -697,7 +685,7 @@ pub const Crc24FlexrayA = Crc(u24, .{ .xor_output = 0x000000, }); -pub const Crc24FlexrayB = Crc(u24, .{ +pub const Crc24FlexrayB = Generic(u24, .{ .polynomial = 0x5d6dcb, .initial = 0xabcdef, .reflect_input = false, @@ -705,7 +693,7 @@ pub const Crc24FlexrayB = Crc(u24, .{ .xor_output = 0x000000, }); -pub const Crc24Interlaken = Crc(u24, .{ +pub const Crc24Interlaken = Generic(u24, .{ .polynomial = 0x328b63, .initial = 0xffffff, .reflect_input = false, @@ -713,7 +701,7 @@ pub const Crc24Interlaken = Crc(u24, .{ .xor_output = 0xffffff, }); -pub const Crc24LteA = Crc(u24, .{ +pub const Crc24LteA = Generic(u24, .{ .polynomial = 0x864cfb, .initial = 0x000000, .reflect_input = false, @@ -721,7 +709,7 @@ pub const Crc24LteA = Crc(u24, .{ .xor_output = 0x000000, }); -pub const Crc24LteB = Crc(u24, .{ +pub const Crc24LteB = Generic(u24, .{ .polynomial = 0x800063, .initial = 0x000000, .reflect_input = false, @@ -729,7 +717,7 @@ pub const Crc24LteB = Crc(u24, .{ .xor_output = 0x000000, }); -pub const Crc24Openpgp = Crc(u24, .{ +pub const Crc24Openpgp = Generic(u24, .{ .polynomial = 0x864cfb, .initial = 0xb704ce, .reflect_input = false, @@ -737,7 +725,7 @@ pub const Crc24Openpgp = Crc(u24, .{ .xor_output = 0x000000, }); -pub const Crc24Os9 = Crc(u24, .{ +pub const Crc24Os9 = Generic(u24, .{ .polynomial = 0x800063, .initial = 0xffffff, .reflect_input = false, @@ -745,7 +733,7 @@ pub const Crc24Os9 = Crc(u24, .{ .xor_output = 0xffffff, }); -pub const Crc30Cdma = Crc(u30, .{ +pub const Crc30Cdma = Generic(u30, .{ .polynomial = 0x2030b9c7, .initial = 0x3fffffff, .reflect_input = false, @@ -753,7 +741,7 @@ pub const Crc30Cdma = Crc(u30, .{ .xor_output = 0x3fffffff, }); -pub const Crc31Philips = Crc(u31, .{ +pub const Crc31Philips = Generic(u31, .{ .polynomial = 0x04c11db7, .initial = 0x7fffffff, .reflect_input = false, @@ -761,7 +749,7 @@ pub const Crc31Philips = Crc(u31, .{ .xor_output = 0x7fffffff, }); -pub const Crc32Aixm = Crc(u32, .{ +pub const Crc32Aixm = Generic(u32, .{ .polynomial = 0x814141ab, .initial = 0x00000000, .reflect_input = false, @@ -769,7 +757,7 @@ pub const Crc32Aixm = Crc(u32, .{ .xor_output = 0x00000000, }); -pub const Crc32Autosar = Crc(u32, .{ +pub const Crc32Autosar = Generic(u32, .{ .polynomial = 0xf4acfb13, .initial = 0xffffffff, .reflect_input = true, @@ -777,7 +765,7 @@ pub const Crc32Autosar = Crc(u32, .{ .xor_output = 0xffffffff, }); -pub const Crc32Base91D = Crc(u32, .{ +pub const Crc32Base91D = Generic(u32, .{ .polynomial = 0xa833982b, .initial = 0xffffffff, .reflect_input = true, @@ -785,7 +773,7 @@ pub const Crc32Base91D = Crc(u32, .{ .xor_output = 0xffffffff, }); -pub const Crc32Bzip2 = Crc(u32, .{ +pub const Crc32Bzip2 = Generic(u32, .{ .polynomial = 0x04c11db7, .initial = 0xffffffff, .reflect_input = false, @@ -793,7 +781,7 @@ pub const Crc32Bzip2 = Crc(u32, .{ .xor_output = 0xffffffff, }); -pub const Crc32CdRomEdc = Crc(u32, .{ +pub const Crc32CdRomEdc = Generic(u32, .{ .polynomial = 0x8001801b, .initial = 0x00000000, .reflect_input = true, @@ -801,7 +789,7 @@ pub const Crc32CdRomEdc = Crc(u32, .{ .xor_output = 0x00000000, }); -pub const Crc32Cksum = Crc(u32, .{ +pub const Crc32Cksum = Generic(u32, .{ .polynomial = 0x04c11db7, .initial = 0x00000000, .reflect_input = false, @@ -809,7 +797,7 @@ pub const Crc32Cksum = Crc(u32, .{ .xor_output = 0xffffffff, }); -pub const Crc32IsoHdlc = Crc(u32, .{ +pub const Crc32IsoHdlc = Generic(u32, .{ .polynomial = 0x04c11db7, .initial = 0xffffffff, .reflect_input = true, @@ -817,7 +805,7 @@ pub const Crc32IsoHdlc = Crc(u32, .{ .xor_output = 0xffffffff, }); -pub const Crc32Jamcrc = Crc(u32, .{ +pub const Crc32Jamcrc = Generic(u32, .{ .polynomial = 0x04c11db7, .initial = 0xffffffff, .reflect_input = true, @@ -825,7 +813,7 @@ pub const Crc32Jamcrc = Crc(u32, .{ .xor_output = 0x00000000, }); -pub const Crc32Koopman = Crc(u32, .{ +pub const Crc32Koopman = Generic(u32, .{ .polynomial = 0x741b8cd7, .initial = 0xffffffff, .reflect_input = true, @@ -833,7 +821,7 @@ pub const Crc32Koopman = Crc(u32, .{ .xor_output = 0xffffffff, }); -pub const Crc32Mef = Crc(u32, .{ +pub const Crc32Mef = Generic(u32, .{ .polynomial = 0x741b8cd7, .initial = 0xffffffff, .reflect_input = true, @@ -841,7 +829,7 @@ pub const Crc32Mef = Crc(u32, .{ .xor_output = 0x00000000, }); -pub const Crc32Mpeg2 = Crc(u32, .{ +pub const Crc32Mpeg2 = Generic(u32, .{ .polynomial = 0x04c11db7, .initial = 0xffffffff, .reflect_input = false, @@ -849,7 +837,7 @@ pub const Crc32Mpeg2 = Crc(u32, .{ .xor_output = 0x00000000, }); -pub const Crc32Xfer = Crc(u32, .{ +pub const Crc32Xfer = Generic(u32, .{ .polynomial = 0x000000af, .initial = 0x00000000, .reflect_input = false, @@ -857,7 +845,7 @@ pub const Crc32Xfer = Crc(u32, .{ .xor_output = 0x00000000, }); -pub const Crc40Gsm = Crc(u40, .{ +pub const Crc40Gsm = Generic(u40, .{ .polynomial = 0x0004820009, .initial = 0x0000000000, .reflect_input = false, @@ -865,7 +853,7 @@ pub const Crc40Gsm = Crc(u40, .{ .xor_output = 0xffffffffff, }); -pub const Crc64Ecma182 = Crc(u64, .{ +pub const Crc64Ecma182 = Generic(u64, .{ .polynomial = 0x42f0e1eba9ea3693, .initial = 0x0000000000000000, .reflect_input = false, @@ -873,7 +861,7 @@ pub const Crc64Ecma182 = Crc(u64, .{ .xor_output = 0x0000000000000000, }); -pub const Crc64GoIso = Crc(u64, .{ +pub const Crc64GoIso = Generic(u64, .{ .polynomial = 0x000000000000001b, .initial = 0xffffffffffffffff, .reflect_input = true, @@ -881,7 +869,7 @@ pub const Crc64GoIso = Crc(u64, .{ .xor_output = 0xffffffffffffffff, }); -pub const Crc64Ms = Crc(u64, .{ +pub const Crc64Ms = Generic(u64, .{ .polynomial = 0x259c84cba6426349, .initial = 0xffffffffffffffff, .reflect_input = true, @@ -889,7 +877,7 @@ pub const Crc64Ms = Crc(u64, .{ .xor_output = 0x0000000000000000, }); -pub const Crc64Redis = Crc(u64, .{ +pub const Crc64Redis = Generic(u64, .{ .polynomial = 0xad93d23594c935a9, .initial = 0x0000000000000000, .reflect_input = true, @@ -897,7 +885,7 @@ pub const Crc64Redis = Crc(u64, .{ .xor_output = 0x0000000000000000, }); -pub const Crc64We = Crc(u64, .{ +pub const Crc64We = Generic(u64, .{ .polynomial = 0x42f0e1eba9ea3693, .initial = 0xffffffffffffffff, .reflect_input = false, @@ -905,7 +893,7 @@ pub const Crc64We = Crc(u64, .{ .xor_output = 0xffffffffffffffff, }); -pub const Crc64Xz = Crc(u64, .{ +pub const Crc64Xz = Generic(u64, .{ .polynomial = 0x42f0e1eba9ea3693, .initial = 0xffffffffffffffff, .reflect_input = true, @@ -913,10 +901,109 @@ pub const Crc64Xz = Crc(u64, .{ .xor_output = 0xffffffffffffffff, }); -pub const Crc82Darc = Crc(u82, .{ +pub const Crc82Darc = Generic(u82, .{ .polynomial = 0x0308c0111011401440411, .initial = 0x000000000000000000000, .reflect_input = true, .reflect_output = true, .xor_output = 0x000000000000000000000, }); + +pub fn Algorithm(comptime W: type) type { + return struct { + polynomial: W, + initial: W, + reflect_input: bool, + reflect_output: bool, + xor_output: W, + }; +} + +pub fn Generic(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 = reflect(algorithm.polynomial); + var table: [256]I = undefined; + for (&table, 0..) |*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 = reflect(algorithm.initial); + return .{ .crc = initial }; + } + + inline fn tableEntry(index: I) I { + const short: u8 = @truncate(index); + return lookup_table[short]; + } + + 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(c ^ algorithm.xor_output); + } + + pub fn hash(bytes: []const u8) W { + var c = Self.init(); + c.update(bytes); + return c.final(); + } + + fn reflect(x: I) I { + const offset = @bitSizeOf(I) - @bitSizeOf(W); + if (algorithm.reflect_input) + return @bitReverse(x) >> offset + else + return x << offset; + } + }; +} + +test { + _ = @import("crc/test.zig"); +} diff --git a/lib/std/hash/crc/impl.zig b/lib/std/hash/crc/impl.zig @@ -1,103 +0,0 @@ -// There is a generic CRC implementation "Crc()" which can be parameterized via -// the Algorithm struct for a plethora of uses. -// -// The primary interface for all of the standard CRC algorithms is the -// generated file "crc.zig", which uses the implementation code here to define -// many standard CRCs. - -const std = @import("std"); - -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 = reflect(algorithm.polynomial); - var table: [256]I = undefined; - for (&table, 0..) |*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 = reflect(algorithm.initial); - return .{ .crc = initial }; - } - - inline fn tableEntry(index: I) I { - const short: u8 = @truncate(index); - return lookup_table[short]; - } - - 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(c ^ algorithm.xor_output); - } - - pub fn hash(bytes: []const u8) W { - var c = Self.init(); - c.update(bytes); - return c.final(); - } - - fn reflect(x: I) I { - const offset = @bitSizeOf(I) - @bitSizeOf(W); - if (algorithm.reflect_input) - return @bitReverse(x) >> offset - else - return x << offset; - } - }; -}