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
#![deny(missing_docs)]
use std::num::Wrapping;
use crate::binary::read::ReadScope;
use crate::binary::U32Be;
use crate::error::ParseError;
pub fn table_checksum(data: &[u8]) -> Result<Wrapping<u32>, ParseError> {
assert_eq!(data.len() % 4, 0, "data end is not 32-bit aligned");
let mut ctxt = ReadScope::new(data).ctxt();
let array = ctxt.read_array::<U32Be>(data.len() / 4)?;
Ok(array.iter().map(Wrapping).sum())
}
#[cfg(test)]
mod tests {
use super::Wrapping;
#[test]
fn test_table_checksum() {
let data = [0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4];
assert_eq!(super::table_checksum(&data).unwrap(), Wrapping(10));
}
#[test]
fn test_table_checksum_overflow() {
let data = [0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 2];
assert_eq!(super::table_checksum(&data).unwrap(), Wrapping(1));
}
}