-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_variant.rs
320 lines (290 loc) · 13.4 KB
/
common_variant.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
//
// ar file format (Common Variant)
//
//
//
// Archive File Signature (8 bytes) File Entry Data
//
// │ │
// │ │
// │ │
// │ │
// ▼ ▼
//
// ▽──────▽ ▽────────────────────────────▽
//
// ┌──────┬──────────────────────────────────────────────────────────┬────────────────────────────┐
// │▓▓▓▓▓▓│░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░│▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐│ ....
// │▓▓▓▓▓▓│░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░│▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐│
// └──────┴──────────────────────────────────────────────────────────┴────────────────────────────┘
//
// △──────────────────────────────────────────────────────────△
// ▲
// │
// │
// │
// │
// │
//
// File Entry Header (60 bytes)
//
//
use nom::{
bytes::complete::{tag, take},
combinator::{eof, map},
multi::{many0, many_till},
IResult,
};
use std::str;
//______________________________ Archive File Signature ______________________________
fn parse_file_signature(input: &[u8]) -> IResult<&[u8], &[u8]> {
tag(b"!<arch>\n")(input)
}
#[test]
fn test_parse_file_signature() {
let input = "\
!<arch>\n\
foo.txt 1487552916 501 20 100644 7 `\n\
foobar\n\n\
bar.awesome.txt 1487552919 501 20 100644 22 `\n\
This file is awesome!\n\
baz.txt 1487552349 42 12345 100664 4 `\n\
baz\n";
let (_, file_sig) =
parse_file_signature(input.as_bytes()).expect("failed in parsing file signature");
assert_eq!(file_sig, b"!<arch>\n");
}
//
// +--------------+----------+----+----+------+--------++
// | | | | | | ||
// | | | | | | ||
// +--------------+----------+----+----+------+--------++
// ^ ^ ^ ^ ^ ^ ^
// | | | | | | |
// | | | | | | |
// | | | | | | +-----------+
// +-----------+ +---------+ +-----+ | | | |
// | | | | +----+ +--------+ |
// | | | | | | |
// | | | | | |
// | | | | | | Terminating
// | | | | Characters
// File File | | (2 bytes)
// Identifier Modification Owner Group |
// (16 bytes) Timestamp id id File
// (12 bytes) (6 bytes) (6 bytes) Mode File
// (8 bytes) Size
// (10 bytes)
//
//
//______________________________ Entry Header ______________________________
#[derive(Debug, PartialEq)]
struct EntryHeader {
identifier: Vec<u8>,
mtime: u64,
uid: u32,
gid: u32,
mode: u32,
size: u64,
}
fn parse_identifier(input: &[u8]) -> IResult<&[u8], Vec<u8>> {
map(take(16usize), |identifier_padded: &[u8]| {
let mut identifier: Vec<u8> = identifier_padded.to_vec();
while identifier.last() == Some(&b' ') {
identifier.pop();
}
identifier
})(input)
}
#[test]
fn test_parse_identifier() {
let input = "\
!<arch>\n\
foo.txt 1487552916 501 20 100644 7 `\n\
foobar\n\n\
bar.awesome.txt 1487552919 501 20 100644 22 `\n\
This file is awesome!\n\
baz.txt 1487552349 42 12345 100664 4 `\n\
baz\n";
let (i, file_sig) =
parse_file_signature(input.as_bytes()).expect("failed in parsing file signature");
assert_eq!(file_sig, b"!<arch>\n");
let (_, identifier) = parse_identifier(i).expect("failed in parsing entry identifier");
assert_eq!(identifier, b"foo.txt");
}
fn byte_slice_to_number(byte_slice: &[u8], radix: u32) -> u64 {
let s = str::from_utf8(byte_slice).expect("failed in converting byte slice to string slice");
let num = u64::from_str_radix(s.trim_end(), radix)
.unwrap_or_else(|_| panic!("failed in converting: {} to number", s));
num
}
fn parse_number(input: &[u8], length: usize, radix: u32) -> IResult<&[u8], u64> {
map(take(length), |byte_slice| {
byte_slice_to_number(byte_slice, radix)
})(input)
}
fn parse_mtime(input: &[u8]) -> IResult<&[u8], u64> {
parse_number(input, 12, 10)
}
fn parse_uid(input: &[u8]) -> IResult<&[u8], u64> {
parse_number(input, 6, 10)
}
fn parse_gid(input: &[u8]) -> IResult<&[u8], u64> {
parse_number(input, 6, 10)
}
fn parse_mode(input: &[u8]) -> IResult<&[u8], u64> {
parse_number(input, 8, 8)
}
fn parse_size(input: &[u8]) -> IResult<&[u8], u64> {
parse_number(input, 10, 10)
}
#[test]
fn test_parse_header_numbers() {
let input = "\
!<arch>\n\
foo.txt 1487552916 501 20 100644 7 `\n\
foobar\n\n\
bar.awesome.txt 1487552919 501 20 100644 22 `\n\
This file is awesome!\n\
baz.txt 1487552349 42 12345 100664 4 `\n\
baz\n";
let (i, file_sig) =
parse_file_signature(input.as_bytes()).expect("failed in parsing file signature");
assert_eq!(file_sig, b"!<arch>\n");
let (i, identifier) = parse_identifier(i).expect("failed in parsing entry identifier");
assert_eq!(identifier, b"foo.txt");
let (i, mtime) = parse_mtime(i).expect("failed in parsing mtime");
assert_eq!(mtime, 1487552916);
let (i, uid) = parse_uid(i).expect("failed in parsing uid");
assert_eq!(uid, 501);
let (i, gid) = parse_gid(i).expect("failed in parsing gid");
assert_eq!(gid, 20);
let (i, mode) = parse_mode(i).expect("failed in parsing mode");
assert_eq!(mode, 0o100644);
let (_, size) = parse_size(i).expect("failed in parsing size");
assert_eq!(size, 7);
}
fn parse_entry_header_terminator(input: &[u8]) -> IResult<&[u8], &[u8]> {
tag(b"`\n")(input)
}
fn parse_entry_header(input: &[u8]) -> IResult<&[u8], EntryHeader> {
let (i, identifier) = parse_identifier(input).expect("failed in parsing entry identifier");
let (i, mtime) = parse_mtime(i).expect("failed in parsing mtime");
let (i, uid) = parse_uid(i).expect("failed in parsing uid");
let (i, gid) = parse_gid(i).expect("failed in parsing gid");
let (i, mode) = parse_mode(i).expect("failed in parsing mode");
let (i, size) = parse_size(i).expect("failed in parsing size");
let (i, _) =
parse_entry_header_terminator(i).expect("failed in parsing entry header terminator");
let entry_header = EntryHeader {
identifier,
mtime,
uid: uid as u32,
gid: gid as u32,
mode: mode as u32,
size,
};
Ok((i, entry_header))
}
#[test]
fn test_parse_entry_header() {
let input = "\
!<arch>\n\
foo.txt 1487552916 501 20 100644 7 `\n\
foobar\n\n\
bar.awesome.txt 1487552919 501 20 100644 22 `\n\
This file is awesome!\n\
baz.txt 1487552349 42 12345 100664 4 `\n\
baz\n";
let (i, file_sig) =
parse_file_signature(input.as_bytes()).expect("failed in parsing file signature");
assert_eq!(file_sig, b"!<arch>\n");
let (_, entry_header) = parse_entry_header(i).expect("failed in parsing entry header");
assert_eq!(entry_header.identifier, b"foo.txt");
assert_eq!(entry_header.mtime, 1487552916);
assert_eq!(entry_header.uid, 501);
assert_eq!(entry_header.gid, 20);
assert_eq!(entry_header.mode, 0o100644);
assert_eq!(entry_header.size, 7);
}
//______________________________ Entry Data ______________________________
fn parse_entry_data(input: &[u8], size: u64) -> IResult<&[u8], &[u8]> {
take(size)(input)
}
#[test]
fn test_parse_entry_data() {
let input = "\
!<arch>\n\
foo.txt 1487552916 501 20 100644 7 `\n\
foobar\n\n\
bar.awesome.txt 1487552919 501 20 100644 22 `\n\
This file is awesome!\n\
baz.txt 1487552349 42 12345 100664 4 `\n\
baz\n";
let (i, file_sig) =
parse_file_signature(input.as_bytes()).expect("failed in parsing file signature");
assert_eq!(file_sig, b"!<arch>\n");
let (i, entry_header) = parse_entry_header(i).expect("failed in parsing entry header");
assert_eq!(entry_header.identifier, b"foo.txt");
assert_eq!(entry_header.mtime, 1487552916);
assert_eq!(entry_header.uid, 501);
assert_eq!(entry_header.gid, 20);
assert_eq!(entry_header.mode, 0o100644);
assert_eq!(entry_header.size, 7);
let (_, data) = parse_entry_data(i, entry_header.size).expect("failed in parsing entry data");
assert_eq!(data, "foobar\n".as_bytes());
}
//______________________________ Entry ______________________________
#[derive(Debug, PartialEq)]
struct Entry {
header: EntryHeader,
data: Vec<u8>,
}
fn parse_newline_padding(input: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
many0(tag(b"\n"))(input)
}
fn parse_entry(input: &[u8]) -> IResult<&[u8], Entry> {
let (i, header) = parse_entry_header(input).expect("failed in parsing entry header");
let (i, data) = parse_entry_data(i, header.size).expect("failed in parsing entry data");
let (i, _) = parse_newline_padding(i).expect("failed in parsing newline padding");
let entry = Entry {
header,
data: data.to_vec(),
};
Ok((i, entry))
}
//______________________________ Complete Parser ______________________________
fn parser(input: &[u8]) -> IResult<&[u8], (Vec<Entry>, &[u8])> {
let (i, _) = parse_file_signature(input).expect("failed in parsing file signature");
many_till(parse_entry, eof)(i)
}
#[test]
fn test_parser() {
let input = "\
!<arch>\n\
foo.txt 1487552916 501 20 100644 7 `\n\
foobar\n\n\
bar.awesome.txt 1487552919 501 20 100644 22 `\n\
This file is awesome!\n\
baz.txt 1487552349 42 12345 100664 4 `\n\
baz\n";
let (i, (entries, empty)) =
parser(input.as_bytes()).expect("failed in parsing the archive file");
assert_eq!(i, b"");
assert_eq!(empty, b"");
assert_eq!(entries[0].header.identifier, b"foo.txt");
assert_eq!(entries[0].header.mtime, 1487552916);
assert_eq!(entries[0].header.uid, 501);
assert_eq!(entries[0].header.gid, 20);
assert_eq!(entries[0].header.mode, 0o100644);
assert_eq!(entries[0].header.size, 7);
assert_eq!(entries[0].data, "foobar\n".as_bytes());
assert_eq!(entries[1].header.identifier, b"bar.awesome.txt");
assert_eq!(entries[1].header.mtime, 1487552919);
assert_eq!(entries[1].header.uid, 501);
assert_eq!(entries[1].header.gid, 20);
assert_eq!(entries[1].header.mode, 0o100644);
assert_eq!(entries[1].header.size, 22);
assert_eq!(entries[1].data, "This file is awesome!\n".as_bytes());
}