| 1 |
//! Helper module to compute a CRC32 checksum |
| 2 |
|
| 3 |
use std::io::{self, Read}; |
| 4 |
|
| 5 |
use crc32fast::Hasher; |
| 6 |
|
| 7 |
/// Reader that validates the CRC32 when it reaches the EOF. |
| 8 |
#[derive(Debug)] |
| 9 |
pub struct Crc32Reader<R> { |
| 10 |
inner: R, |
| 11 |
hasher: Hasher, |
| 12 |
check: u32, |
| 13 |
/// Signals if `inner` stores aes encrypted data. |
| 14 |
/// AE-2 encrypted data doesn't use crc and sets the value to 0. |
| 15 |
enabled: bool, |
| 16 |
} |
| 17 |
|
| 18 |
impl<R> Crc32Reader<R> { |
| 19 |
/// Get a new `Crc32Reader` which checks the inner reader against checksum. |
| 20 |
/// The check can be disabled with the arg `should_disable` (used in aes for example) |
| 21 |
pub(crate) fn new(inner: R, checksum: u32, should_disable: bool) -> Crc32Reader<R> { |
| 22 |
Crc32Reader { |
| 23 |
inner, |
| 24 |
hasher: Hasher::new(), |
| 25 |
check: checksum, |
| 26 |
enabled: !should_disable, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
fn check_matches(&self) -> bool { |
| 31 |
self.check == self.hasher.clone().finalize() |
| 32 |
} |
| 33 |
|
| 34 |
pub fn into_inner(self) -> R { |
| 35 |
self.inner |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
macro_rules! invalid_checksum { |
| 40 |
( $( $x:expr ),* ) => { |
| 41 |
io::Error::new(io::ErrorKind::InvalidData, "Invalid checksum") |
| 42 |
}; |
| 43 |
} |
| 44 |
|
| 45 |
impl<R: Read> Read for Crc32Reader<R> { |
| 46 |
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 47 |
let count = self.inner.read(buf)?; |
| 48 |
|
| 49 |
if self.enabled { |
| 50 |
if count == 0 && !buf.is_empty() && !self.check_matches() { |
| 51 |
return Err(invalid_checksum!()); |
| 52 |
} |
| 53 |
self.hasher.update(&buf[..count]); |
| 54 |
} |
| 55 |
Ok(count) |
| 56 |
} |
| 57 |
|
| 58 |
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
| 59 |
let start = buf.len(); |
| 60 |
let n = self.inner.read_to_end(buf)?; |
| 61 |
|
| 62 |
if self.enabled { |
| 63 |
self.hasher.update(&buf[start..]); |
| 64 |
if !self.check_matches() { |
| 65 |
return Err(invalid_checksum!()); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
Ok(n) |
| 70 |
} |
| 71 |
|
| 72 |
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { |
| 73 |
let start = buf.len(); |
| 74 |
let n = self.inner.read_to_string(buf)?; |
| 75 |
|
| 76 |
if self.enabled { |
| 77 |
self.hasher.update(&buf.as_bytes()[start..]); |
| 78 |
if !self.check_matches() { |
| 79 |
return Err(invalid_checksum!()); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
Ok(n) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#[cfg(test)] |
| 88 |
mod tests { |
| 89 |
use std::io::Read; |
| 90 |
|
| 91 |
use super::Crc32Reader; |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_empty_reader() { |
| 95 |
let data: &[u8] = b""; |
| 96 |
let mut buf = [0; 1]; |
| 97 |
|
| 98 |
let mut reader = Crc32Reader::new(data, 0, false); |
| 99 |
assert_eq!(reader.read(&mut buf).unwrap(), 0); |
| 100 |
|
| 101 |
let mut reader = Crc32Reader::new(data, 1, false); |
| 102 |
assert!( |
| 103 |
reader |
| 104 |
.read(&mut buf) |
| 105 |
.unwrap_err() |
| 106 |
.to_string() |
| 107 |
.contains("Invalid checksum") |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_byte_by_byte() { |
| 113 |
let data: &[u8] = b"1234"; |
| 114 |
let mut buf = [0; 1]; |
| 115 |
|
| 116 |
let mut reader = Crc32Reader::new(data, 0x9be_3e0a3, false); |
| 117 |
assert_eq!(reader.read(&mut buf).unwrap(), 1); |
| 118 |
assert_eq!(reader.read(&mut buf).unwrap(), 1); |
| 119 |
assert_eq!(reader.read(&mut buf).unwrap(), 1); |
| 120 |
assert_eq!(reader.read(&mut buf).unwrap(), 1); |
| 121 |
assert_eq!(reader.read(&mut buf).unwrap(), 0); |
| 122 |
// Can keep reading 0 bytes after the end |
| 123 |
assert_eq!(reader.read(&mut buf).unwrap(), 0); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_zero_read() { |
| 128 |
let data: &[u8] = b"1234"; |
| 129 |
let mut buf = [0; 5]; |
| 130 |
|
| 131 |
let mut reader = Crc32Reader::new(data, 0x9be3_e0a3, false); |
| 132 |
assert_eq!(reader.read(&mut buf[..0]).unwrap(), 0); |
| 133 |
assert_eq!(reader.read(&mut buf).unwrap(), 4); |
| 134 |
} |
| 135 |
} |