1 //! Cyclic Redundancy Check
3 /// polynomial value for 8 bits Cyclic Redundancy Check
4 pub const POLY8: u8 = 0x31;
5 /// initialize value for 8 bits Cyclic Redundancy Check
6 pub const INIT8: u8 = 0x00;
7 /// Cyclic Redundancy Check for 8 bits
10 /// Cyclic Redundancy Check comparison table
15 fn a_crc8le_init(table: *mut u8, poly: u8);
16 fn a_crc8be_init(table: *mut u8, poly: u8);
17 fn a_crc8(table: *const u8, pdate: *const u8, nbyte: usize, value: u8) -> u8;
21 /// initialize for little-endian 8 bits Cyclic Redundancy Check
22 pub fn new_le(poly: u8) -> Self {
23 let mut ctx: Self = Self { table: [0; 0x100] };
24 unsafe { a_crc8le_init(ctx.table.as_mut_ptr(), poly) };
28 /// calculate for little-endian 8 bits Cyclic Redundancy Check
29 pub fn le(self, data: &[u8], value: u8) -> u8 {
30 unsafe { a_crc8(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
33 /// initialize for big-endian 8 bits Cyclic Redundancy Check
34 pub fn new_be(poly: u8) -> Self {
35 let mut ctx: Self = Self { table: [0; 0x100] };
36 unsafe { a_crc8be_init(ctx.table.as_mut_ptr(), poly) };
40 /// calculate for big-endian 8 bits Cyclic Redundancy Check
41 pub fn be(self, data: &[u8], value: u8) -> u8 {
42 unsafe { a_crc8(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
49 let ctx = crate::crc::crc8::new_le(POLY8);
53 let ctx = crate::crc::crc8::new_be(POLY8);
58 /// polynomial value for 16 bits Cyclic Redundancy Check
59 pub const POLY16: u16 = 0xA001;
60 /// initialize value for 16 bits Cyclic Redundancy Check
61 pub const INIT16: u16 = 0x0000;
62 /// Cyclic Redundancy Check for 16 bits
65 /// Cyclic Redundancy Check comparison table
70 fn a_crc16le_init(table: *mut u16, poly: u16);
71 fn a_crc16be_init(table: *mut u16, poly: u16);
72 fn a_crc16le(table: *const u16, pdate: *const u8, nbyte: usize, value: u16) -> u16;
73 fn a_crc16be(table: *const u16, pdate: *const u8, nbyte: usize, value: u16) -> u16;
77 /// initialize for little-endian 16 bits Cyclic Redundancy Check
78 pub fn new_le(poly: u16) -> Self {
79 let mut ctx: Self = Self { table: [0; 0x100] };
80 unsafe { a_crc16le_init(ctx.table.as_mut_ptr(), poly) };
84 /// calculate for little-endian 16 bits Cyclic Redundancy Check
85 pub fn le(self, data: &[u8], value: u16) -> u16 {
86 unsafe { a_crc16le(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
89 /// initialize for big-endian 16 bits Cyclic Redundancy Check
90 pub fn new_be(poly: u16) -> Self {
91 let mut ctx: Self = Self { table: [0; 0x100] };
92 unsafe { a_crc16be_init(ctx.table.as_mut_ptr(), poly) };
96 /// calculate for big-endian 16 bits Cyclic Redundancy Check
97 pub fn be(self, data: &[u8], value: u16) -> u16 {
98 unsafe { a_crc16be(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
105 let ctx = crate::crc::crc16::new_le(POLY16);
109 let ctx = crate::crc::crc16::new_be(POLY16);
114 /// polynomial value for 32 bits Cyclic Redundancy Check
115 pub const POLY32: u32 = 0xEDB88320;
116 /// initialize value for 32 bits Cyclic Redundancy Check
117 pub const INIT32: u32 = 0xFFFFFFFF;
118 /// Cyclic Redundancy Check for 32 bits
121 /// Cyclic Redundancy Check comparison table
126 fn a_crc32le_init(table: *mut u32, poly: u32);
127 fn a_crc32be_init(table: *mut u32, poly: u32);
128 fn a_crc32le(table: *const u32, pdate: *const u8, nbyte: usize, value: u32) -> u32;
129 fn a_crc32be(table: *const u32, pdate: *const u8, nbyte: usize, value: u32) -> u32;
133 /// initialize for little-endian 32 bits Cyclic Redundancy Check
134 pub fn new_le(poly: u32) -> Self {
135 let mut ctx: Self = Self { table: [0; 0x100] };
136 unsafe { a_crc32le_init(ctx.table.as_mut_ptr(), poly) };
140 /// calculate for little-endian 32 bits Cyclic Redundancy Check
141 pub fn le(self, data: &[u8], value: u32) -> u32 {
142 unsafe { a_crc32le(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
145 /// initialize for big-endian 32 bits Cyclic Redundancy Check
146 pub fn new_be(poly: u32) -> Self {
147 let mut ctx: Self = Self { table: [0; 0x100] };
148 unsafe { a_crc32be_init(ctx.table.as_mut_ptr(), poly) };
152 /// calculate for big-endian 32 bits Cyclic Redundancy Check
153 pub fn be(self, data: &[u8], value: u32) -> u32 {
154 unsafe { a_crc32be(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
161 let ctx = crate::crc::crc32::new_le(POLY32);
165 let ctx = crate::crc::crc32::new_be(POLY32);
170 /// polynomial value for 64 bits Cyclic Redundancy Check
171 pub const POLY64: u64 = 0x42F0E1EBA9EA3693;
172 /// initialize value for 64 bits Cyclic Redundancy Check
173 pub const INIT64: u64 = 0xFFFFFFFFFFFFFFFF;
174 /// Cyclic Redundancy Check for 64 bits
177 /// Cyclic Redundancy Check comparison table
182 fn a_crc64le_init(table: *mut u64, poly: u64);
183 fn a_crc64be_init(table: *mut u64, poly: u64);
184 fn a_crc64le(table: *const u64, pdate: *const u8, nbyte: usize, value: u64) -> u64;
185 fn a_crc64be(table: *const u64, pdate: *const u8, nbyte: usize, value: u64) -> u64;
189 /// initialize for little-endian 64 bits Cyclic Redundancy Check
190 pub fn new_le(poly: u64) -> Self {
191 let mut ctx: Self = Self { table: [0; 0x100] };
192 unsafe { a_crc64le_init(ctx.table.as_mut_ptr(), poly) };
196 /// calculate for little-endian 64 bits Cyclic Redundancy Check
197 pub fn le(self, data: &[u8], value: u64) -> u64 {
198 unsafe { a_crc64le(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
201 /// initialize for big-endian 64 bits Cyclic Redundancy Check
202 pub fn new_be(poly: u64) -> Self {
203 let mut ctx: Self = Self { table: [0; 0x100] };
204 unsafe { a_crc64be_init(ctx.table.as_mut_ptr(), poly) };
208 /// calculate for big-endian 64 bits Cyclic Redundancy Check
209 pub fn be(self, data: &[u8], value: u64) -> u64 {
210 unsafe { a_crc64be(self.table.as_ptr(), data.as_ptr(), data.len(), value) }
217 let ctx = crate::crc::crc64::new_le(POLY64);
221 let ctx = crate::crc::crc64::new_be(POLY64);