| 1 | /// The polynomial used for the crc32 lookup table. |
| 2 | /// |
| 3 | /// See also https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations |
| 4 | const POLYNOMIALrust-analyzer cargo libbz2-rs-sys 0.2.5 crctable/POLYNOMIAL.: u32 = 0x04C11DB7; |
| 5 | |
| 6 | /// Most implementations (ethernet, zlib) use the reflected version of this polynomial. |
| 7 | const _: () = assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(POLYNOMIALrust-analyzer cargo libbz2-rs-sys 0.2.5 crctable/POLYNOMIAL..reverse_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]reverse_bits().() == 0xEDB88320); |
| 8 | |
| 9 | /// Lookup table to speed up crc32 checksum calculation. |
| 10 | /// |
| 11 | /// The original C implementation notes: |
| 12 | /// |
| 13 | /// > I think this is an implementation of the AUTODIN-II, |
| 14 | /// > Ethernet & FDDI 32-bit CRC standard. Vaguely derived |
| 15 | /// > from code by Rob Warnock, in Section 51 of the |
| 16 | /// > comp.compression FAQ. |
| 17 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) static BZ2_CRC32TABLErust-analyzer cargo libbz2-rs-sys 0.2.5 crctable/BZ2_CRC32TABLE.: [u32; 256] = generate_crc32_tablerust-analyzer cargo libbz2-rs-sys 0.2.5 crctable/generate_crc32_table().(POLYNOMIALrust-analyzer cargo libbz2-rs-sys 0.2.5 crctable/POLYNOMIAL.); |
| 18 | |
| 19 | /// Generate the crc32 lookup table. |
| 20 | /// |
| 21 | /// Note that contrary to most material you'll find on the internet, we're using the non-reflected |
| 22 | /// polynomial, which impacts some of the logic (e.g. we bitwise and with 0x80000000 instead of 0x1). |
| 23 | /// |
| 24 | /// This [article] has some excellent additional detail on how crc works, and how to make it fast. |
| 25 | /// |
| 26 | /// [article]: https://create.stephan-brumme.com/crc32/ |
| 27 | const fn generate_crc32_tablerust-analyzer cargo libbz2-rs-sys 0.2.5 crctable/generate_crc32_table().(polynomial: u32) -> [u32; 256] { |
| 28 | let mut table = [0u32; 256]; |
| 29 | |
| 30 | let mut i = 0; |
| 31 | while i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().256 { |
| 32 | let mut crc = (i as u32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().24; |
| 33 | |
| 34 | let mut j = 0; |
| 35 | while j rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().8 { |
| 36 | if (crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0x80000000) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0 { |
| 37 | crc = (crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXor<Self>`]bitxor().polynomial; |
| 38 | } else { |
| 39 | crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShlAssign<i32>`]shl_assign().<<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShlAssign<i32>`]shl_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShlAssign<i32>`]shl_assign().1; |
| 40 | } |
| 41 | |
| 42 | j rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1; |
| 43 | } |
| 44 | |
| 45 | table[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = crc; |
| 46 | |
| 47 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 48 | } |
| 49 | |
| 50 | table |
| 51 | } |