-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhead_block.rs
170 lines (152 loc) · 4.1 KB
/
head_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use nom;
use nom::be_u32;
use std::convert::From;
use util::get_bit_at;
use vint::vint;
/// general Header valid for all rar blocks
#[derive(PartialEq, Debug, Clone, Default)]
pub struct HeadBlock {
pub crc: u32,
pub size: u64,
pub typ: Typ,
pub flags: Flags,
pub extra_area_size: u64,
pub data_area_size: u64,
}
impl HeadBlock {
/// Create a new HeaderBlock
pub fn new(crc: u32, size: u64, typ: Typ, flags: Flags) -> Self {
HeadBlock {
crc,
size,
typ,
flags,
extra_area_size: 0,
data_area_size: 0,
}
}
/// Parse a HeaderBlock from a byte slice
pub fn parse(input: &[u8]) -> nom::IResult<&[u8], HeadBlock> {
// get the base header
let (mut input, mut bh) = base_header(input)?;
// check for a extra area
if bh.flags.extra_area {
let (i, s) = vint(input)?;
input = i;
bh.extra_area_size = s;
}
// check for a data area
if bh.flags.data_area {
let (i, s) = vint(input)?;
input = i;
bh.data_area_size = s;
}
Ok((input, bh))
}
}
#[test]
fn test_header() {
let data = [0xF3, 0xE1, 0x82, 0xEB, 0x0B, 0x01, 0x05, 0x07, 0x00];
let mut flags = Flags::new();
flags.extra_area = true;
flags.skip = true;
let mut h = HeadBlock::new(4091642603, 11, Typ::MainArchive, flags);
h.extra_area_size = 7;
assert_eq!(HeadBlock::parse(&data), Ok((&[0x00][..], h)));
}
/// Definition of the header block typ
#[derive(PartialEq, Debug, Clone)]
pub enum Typ {
MainArchive,
File,
Service,
Encryption,
EndArchive,
Unknown,
}
impl From<u64> for Typ {
fn from(i: u64) -> Self {
match i {
1 => Typ::MainArchive,
2 => Typ::File,
3 => Typ::Service,
4 => Typ::Encryption,
5 => Typ::EndArchive,
_ => Typ::Unknown,
}
}
}
impl Default for Typ {
fn default() -> Typ {
Typ::Unknown
}
}
/// Flags for a header block
#[derive(PartialEq, Debug, Clone, Default)]
pub struct Flags {
pub extra_area: bool, // Extra are is present in the end of header.
pub data_area: bool, // Data area is present in the end of header.
pub skip: bool, // Blocks with unknown type and this flag must be skipped when updating an archive.
pub data_prev: bool, // Data area is continuing from previous volume.
pub data_next: bool, // Data area is continuing in next volume.
pub preceding: bool, // Block depends on preceding file block.
pub preserve: bool, // Preserve a child block if host block is modified.
}
impl Flags {
pub fn new() -> Flags {
Flags {
extra_area: false,
data_area: false,
skip: false,
data_prev: false,
data_next: false,
preceding: false,
preserve: false,
}
}
}
impl From<u64> for Flags {
fn from(i: u64) -> Self {
let mut f = Flags::new();
if get_bit_at(i, 0) {
f.extra_area = true;
}
if get_bit_at(i, 1) {
f.data_area = true;
}
if get_bit_at(i, 2) {
f.skip = true;
}
if get_bit_at(i, 3) {
f.data_prev = true;
}
if get_bit_at(i, 4) {
f.data_next = true;
}
if get_bit_at(i, 5) {
f.preceding = true;
}
if get_bit_at(i, 6) {
f.preserve = true;
}
f
}
}
named_attr!(#[doc = "Get a base header"], base_header(&[u8]) -> HeadBlock,
do_parse!(
crc: be_u32 >>
size: vint >>
typ: vint >>
flags: vint >>
(HeadBlock::new(crc, size, typ.into(), flags.into()))
)
);
#[test]
fn test_base_header() {
let data = [0xF3, 0xE1, 0x82, 0xEB, 0x0B, 0x01, 0x05, 0x07];
let mut flags = Flags::new();
flags.extra_area = true;
flags.skip = true;
let header = HeadBlock::new(4091642603, 11, Typ::MainArchive, flags);
assert_eq!(base_header(&data), Ok((&[0x07][..], header)));
}