1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
pub fn calculate_crc8(data: &[u8], poly: u8, init: u8, ref_in: bool, ref_out: bool, xor_out: u8) -> u8 { let mut crc = init; for d in data.iter() { let c = if ref_in { (*d).reverse_bits() } else { *d }; let mut i = 0x80; while i > 0 { let mut bit = (crc & 0x80) != 0; if (c & i) != 0 { bit = !bit; } crc <<= 1; if bit { crc ^= poly; } i >>= 1; } } if ref_out { crc = crc.reverse_bits(); } crc ^ xor_out } pub fn crc8(data: &[u8]) -> u8 { calculate_crc8(data, 0x07, 0x00, false, false, 0x00) } pub fn crc8_cdma2000(data: &[u8]) -> u8 { calculate_crc8(data, 0x9B, 0xFF, false, false, 0x00) } pub fn crc8_darc(data: &[u8]) -> u8 { calculate_crc8(data, 0x39, 0x00, true, true, 0x00) } pub fn crc8_dvb_s2(data: &[u8]) -> u8 { calculate_crc8(data, 0xD5, 0x00, false, false, 0x00) } pub fn crc8_ebu(data: &[u8]) -> u8 { calculate_crc8(data, 0x1D, 0xFF, true, true, 0x00) } pub fn crc8_i_code(data: &[u8]) -> u8 { calculate_crc8(data, 0x1D, 0xFD, false, false, 0x00) } pub fn crc8_itu(data: &[u8]) -> u8 { calculate_crc8(data, 0x07, 0x00, false, false, 0x55) } pub fn crc8_maxim(data: &[u8]) -> u8 { calculate_crc8(data, 0x31, 0x00, true, true, 0x00) } pub fn crc8_rohc(data: &[u8]) -> u8 { calculate_crc8(data, 0x07, 0xFF, true, true, 0x00) } pub fn crc8_wcdma(data: &[u8]) -> u8 { calculate_crc8(data, 0x9B, 0x00, true, true, 0x00) } #[cfg(test)] mod tests { use super::*; fn check_sequence() -> Vec<u8> { "123456789".to_owned().into_bytes() } fn check_sequence_long() -> Vec<u8> { "helloworldchris!".to_owned().into_bytes() } #[test] fn test_crc8() { assert_eq!(crc8(&check_sequence()), 0xF4); } #[test] fn test_crc8_long() { assert_eq!(crc8(&check_sequence_long()), 0x4E); } #[test] fn test_crc8_long_long() { let mut data = check_sequence_long(); data.append(&mut check_sequence_long()); assert_eq!(crc8(&data), 0xD2); } #[test] fn test_crc8_long_long_long() { let mut data = check_sequence_long(); data.append(&mut check_sequence_long()); data.append(&mut check_sequence_long()); assert_eq!(crc8(&data), 0xED); } #[test] fn test_crc8_demo() { let mut data = "123456789".to_owned().into_bytes(); let checksum = crc8(&data); data.push(checksum); assert_eq!(crc8(&data), 0); let mut data = "023456789".to_owned().into_bytes(); data.push(checksum); assert!(crc8(&data) != 0); let mut data = "023456799".to_owned().into_bytes(); data.push(checksum); assert!(crc8(&data) != 0); } #[test] fn test_crc8_cdma2000() { assert_eq!(crc8_cdma2000(&check_sequence()), 0xDA); } #[test] fn test_crc8_darc() { assert_eq!(crc8_darc(&check_sequence()), 0x15); } #[test] fn test_crc8_dvb_s2() { assert_eq!(crc8_dvb_s2(&check_sequence()), 0xBC); } #[test] fn test_crc8_ebu() { assert_eq!(crc8_ebu(&check_sequence()), 0x97); } #[test] fn test_crc8_i_code() { assert_eq!(crc8_i_code(&check_sequence()), 0x7E); } #[test] fn test_crc8_itu() { assert_eq!(crc8_itu(&check_sequence()), 0xA1); } #[test] fn test_crc8_maxim() { assert_eq!(crc8_maxim(&check_sequence()), 0xA1); } #[test] fn test_crc8_rohc() { assert_eq!(crc8_rohc(&check_sequence()), 0xD0); } #[test] fn test_crc8_wcdma() { assert_eq!(crc8_wcdma(&check_sequence()), 0x25); } }