Created
September 16, 2017 11:27
-
-
Save archshift/080adf97d852d12167531ac1a21716ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn make_crc(buf: &[u8]) -> u8 { | |
let crc_for_byte = |mut byte: u8| { | |
const polynomial: u8 = 0x89; | |
if byte & 0x80 != 0 { | |
byte ^= polynomial | |
} | |
for _ in 1..8 { | |
byte <<= 1; | |
if byte & 0x80 != 0 { | |
byte ^= polynomial; | |
} | |
} | |
byte | |
}; | |
buf.iter().fold(0u8, |out_byte, b| crc_for_byte((out_byte << 1) ^ b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment