forked from datrs/sleep-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use header::*; | ||
use nom::{be_u16, be_u8, rest}; | ||
use std::str; | ||
|
||
const HEADER_LENGTH: usize = 32; | ||
const VERIFY_TRAILING_ZEROS: bool = true; | ||
|
||
named!( | ||
file_type<FileType>, | ||
switch!(be_u8, | ||
0 => value!(FileType::BitField) | | ||
1 => value!(FileType::Signatures) | | ||
2 => value!(FileType::Tree) | ||
) | ||
); | ||
|
||
named!( | ||
protocol_version<ProtocolVersion>, | ||
switch!(be_u8, | ||
0 => value!(ProtocolVersion::V0) | ||
) | ||
); | ||
|
||
named!( | ||
pub header<Header>, | ||
flat_map!( | ||
take!(HEADER_LENGTH), | ||
do_parse!( | ||
tag!(b"\x05\x02\x57") >> | ||
file_type: file_type >> | ||
protocol_version: protocol_version >> | ||
entry_size: be_u16 >> | ||
|
||
algorithm_len: be_u8 >> | ||
algorithm: switch!(map_res!(take!(algorithm_len), str::from_utf8), | ||
"BLAKE2b" => value!(HashType::BLAKE2b) | | ||
"Ed25519" => value!(HashType::Ed25519) | | ||
"" => value!(HashType::None) | ||
) >> | ||
|
||
verify!(rest, |bytes: &[u8]| { | ||
let header_consumed = bytes.len() + algorithm_len as usize + 8 == HEADER_LENGTH; | ||
let trailing_zeros = !VERIFY_TRAILING_ZEROS || bytes.iter().all(|&b| b == 0u8); | ||
header_consumed && trailing_zeros | ||
}) >> | ||
|
||
(Header { | ||
file_type, | ||
protocol_version, | ||
entry_size, | ||
hash_type: algorithm, | ||
}) | ||
) | ||
) | ||
); | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
use nom::IResult::Done; | ||
|
||
#[test] | ||
fn parse_file_type() { | ||
assert_eq!( | ||
file_type(b"\x00"), | ||
Done(&[][..], FileType::BitField) | ||
); | ||
assert_eq!( | ||
file_type(b"\x01"), | ||
Done(&[][..], FileType::Signatures) | ||
); | ||
assert_eq!( | ||
file_type(b"\x02"), | ||
Done(&[][..], FileType::Tree) | ||
); | ||
assert!(file_type(b"\xff").is_err()); | ||
} | ||
|
||
#[test] | ||
fn parse_header() { | ||
fn mk_header(prefix: &[u8]) -> [u8; 32] { | ||
let mut h = [0u8; 32]; | ||
h[0..prefix.len()].clone_from_slice(prefix); | ||
h | ||
} | ||
|
||
assert_eq!( | ||
header(&mk_header( | ||
b"\x05\x02W\x01\x00\x00\x28\x07BLAKE2b" | ||
)), | ||
Done( | ||
&[][..], | ||
Header { | ||
file_type: FileType::Signatures, | ||
protocol_version: ProtocolVersion::V0, | ||
entry_size: 40, | ||
hash_type: HashType::BLAKE2b | ||
} | ||
) | ||
); | ||
assert_eq!( | ||
header(&mk_header( | ||
b"\x05\x02W\x01\x00\x00\x28\x07BLAKE2b" | ||
)).unwrap() | ||
.1 | ||
.hash_type, | ||
HashType::BLAKE2b | ||
); | ||
assert_eq!( | ||
header(&mk_header( | ||
b"\x05\x02W\x01\x00\x00\x28\x07Ed25519" | ||
)).unwrap() | ||
.1 | ||
.hash_type, | ||
HashType::Ed25519 | ||
); | ||
assert_eq!( | ||
header(&mk_header(b"\x05\x02W\x01\x00\x00\x28\x00")) | ||
.unwrap() | ||
.1 | ||
.hash_type, | ||
HashType::None | ||
); | ||
assert!(header(&mk_header(b"\x05\x02W\x01\x00\x00\x28\x01B")).is_err()); | ||
assert!(header(&mk_header(b"\x05\x02W\x01\x00\x00\x28\x01B")).is_err()); | ||
|
||
let h = b"\x05\x02W\x01\x00\x00\x28\x19BLAKE2bXXXXXXXXXXXXXXXXXX"; | ||
assert!(header(h).is_incomplete()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
extern crate sleep_parser; | ||
|
||
use sleep_parser::*; | ||
|
||
#[test] | ||
fn issue_3() { | ||
// https://github.com/datrs/sleep-parser/issues/3 | ||
|
||
let data = b"\x05\x02W\x01\x00\xb0\xb0\xb0\xb0\xb0\xb0\xb0\xb0\xb0\xb0\xb0\xfb\x03p\xb0\xb0\xb0\xb0\xb0\xb0\xb0\xb0\xbb9\xb0\xf5\xf5"; | ||
assert!(Header::from_bytes(data).is_incomplete()); | ||
|
||
let data = b"\x05\x02W\x01\x00\x00\x00\x12\x12\x12\x00\x00S\xc3\xcf\x8a2\xcc\xd1\xce9\xc4K\x9343\x00602\xb5\x07"; | ||
assert!(Header::from_bytes(data).is_err()); | ||
} |