-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathend_block.rs
60 lines (51 loc) · 1.64 KB
/
end_block.rs
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
use head_block::HeadBlock;
use nom;
use util::to_bool;
use vint::vint;
/// EndBlock which determines the end of an .rar file
#[derive(PartialEq, Debug, Default)]
pub struct EndBlock {
pub head: HeadBlock,
pub last_volume: bool,
}
impl EndBlock {
/// Parse the end block information from a byte slice
pub fn parse(inp: &[u8]) -> nom::IResult<&[u8], EndBlock> {
// get the base header
let (input, head) = HeadBlock::parse(inp)?;
// check if the defined type is end archive header
if head.typ != ::head_block::Typ::EndArchive {
return Err(nom::Err::Error(error_position!(inp, nom::ErrorKind::IsNot)));
}
// check for the last volume flag
let (input, lv) = vint(input)?;
let last_volume = !to_bool(lv as u8);
// create the end block
let end = EndBlock { head, last_volume };
Ok((input, end))
}
}
#[test]
fn test_archive() {
// test a success case
let data = [0x1D, 0x77, 0x56, 0x51, 0x03, 0x05, 0x04, 0x00];
let mut flags = ::head_block::Flags::new();
flags.skip = true;
let arc = EndBlock {
head: HeadBlock::new(494360145, 3, ::head_block::Typ::EndArchive, flags),
last_volume: true,
};
assert_eq!(EndBlock::parse(&data), Ok((&[][..], arc)));
// test a wrong header type
let data = [
0xF3, 0xE1, 0x82, 0xEB, 0x0B, 0x02, 0x05, 0x07, 0x00, 0x06, 0x01, 0x01, 0x80, 0x80, 0x80,
0x00, 0x8C, 0x0D, 0x88, 0xE2,
];
assert_eq!(
EndBlock::parse(&data),
Err(nom::Err::Error(error_position!(
&data[..],
nom::ErrorKind::IsNot
)))
);
}