forked from composefs/tar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_corpus.rs
More file actions
810 lines (761 loc) · 24.5 KB
/
generate_corpus.rs
File metadata and controls
810 lines (761 loc) · 24.5 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! Generate seed corpus files for the `parse` fuzz target.
//!
//! Each seed exercises a distinct parser code path: empty archives, various
//! entry types, GNU long name/link extensions, PAX extended headers, edge-case
//! sizes, and multi-entry archives.
//!
//! Run via: `cargo run --manifest-path fuzz/Cargo.toml --bin generate-corpus`
//! or: `just generate-corpus`
use std::fs;
use std::path::Path;
use tar_core::builder::EntryBuilder;
use tar_core::{EntryType, SparseEntry, HEADER_SIZE};
/// End-of-archive marker: two 512-byte zero blocks.
const EOA: [u8; 1024] = [0u8; 1024];
fn main() {
let corpus_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("corpus/parse");
fs::create_dir_all(&corpus_dir).expect("create corpus dir");
let mut seeds: Vec<(&str, Vec<u8>)> = Vec::new();
// 0. Empty archive (just end-of-archive markers)
seeds.push(("empty", EOA.to_vec()));
// 1. Single regular file, short path, no content
seeds.push((
"regular_empty",
build_archive(
|b| {
b.path(b"hello.txt")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
},
&[],
),
));
// 2. Single regular file with content
seeds.push((
"regular_content",
build_archive(
|b| {
b.path(b"data.bin")
.entry_type(EntryType::Regular)
.mode(0o755)
.unwrap()
.size(13)
.unwrap()
.uid(1000)
.unwrap()
.gid(1000)
.unwrap()
.mtime(1700000000)
.unwrap();
},
b"Hello, world!",
),
));
// 3. GNU long name (path > 100 chars)
{
let long_path = "deep/".repeat(25) + "file.txt"; // 130 chars
seeds.push((
"gnu_long_name",
build_archive(
|b| {
b.path(long_path.as_bytes())
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
},
&[],
),
));
}
// 4. GNU long link (symlink target > 100 chars)
{
let long_target = "/usr/share/".to_string() + &"x".repeat(100);
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"mylink")
.link_name(long_target.as_bytes())
.entry_type(EntryType::Symlink)
.mode(0o777)
.unwrap()
.size(0)
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("gnu_long_link", archive));
}
// 5. GNU long name AND long link combined
{
let long_path = "a/".repeat(60) + "linked";
let long_target = "b/".repeat(60) + "target";
let mut builder = EntryBuilder::new_gnu();
builder
.path(long_path.as_bytes())
.link_name(long_target.as_bytes())
.entry_type(EntryType::Symlink)
.mode(0o777)
.unwrap()
.size(0)
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("gnu_long_both", archive));
}
// 6. Symlink (short target)
{
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"link.txt")
.link_name(b"target.txt")
.entry_type(EntryType::Symlink)
.mode(0o777)
.unwrap()
.size(0)
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("symlink", archive));
}
// 7. Hardlink
{
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"hardlink.txt")
.link_name(b"original.txt")
.entry_type(EntryType::Link)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("hardlink", archive));
}
// 8. Directory
seeds.push((
"directory",
build_archive(
|b| {
b.path(b"mydir/")
.entry_type(EntryType::Directory)
.mode(0o755)
.unwrap()
.size(0)
.unwrap();
},
&[],
),
));
// 9. FIFO
seeds.push((
"fifo",
build_archive(
|b| {
b.path(b"mypipe")
.entry_type(EntryType::Fifo)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
},
&[],
),
));
// 10. Character device
{
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"dev/null")
.entry_type(EntryType::Char)
.mode(0o666)
.unwrap()
.size(0)
.unwrap()
.device(1, 3)
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("char_device", archive));
}
// 11. Block device
{
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"dev/sda")
.entry_type(EntryType::Block)
.mode(0o660)
.unwrap()
.size(0)
.unwrap()
.device(8, 0)
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("block_device", archive));
}
// 12. PAX extended header with long path
{
let long_path = "pax/".repeat(40) + "readme.md"; // 169 chars
seeds.push((
"pax_long_path",
build_archive_pax(
|b| {
b.path(long_path.as_bytes())
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(4)
.unwrap();
},
b"test",
),
));
}
// 13. PAX with large uid/gid (overflow octal)
{
seeds.push((
"pax_large_ids",
build_archive_pax(
|b| {
b.path(b"bigids.txt")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap()
.uid(u64::from(u32::MAX) + 1)
.unwrap()
.gid(u64::from(u32::MAX) + 1)
.unwrap();
},
&[],
),
));
}
// 14. PAX with custom xattr
{
let mut builder = EntryBuilder::new_ustar();
builder
.path(b"xattr.txt")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
builder
.add_pax("SCHILY.xattr.user.test", b"value123")
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("pax_xattr", archive));
}
// 15. PAX with multiple extensions (path + size + mtime + xattr)
{
let long_path = "multi_pax/".repeat(15) + "complex.dat";
let mut builder = EntryBuilder::new_ustar();
builder
.path(long_path.as_bytes())
.entry_type(EntryType::Regular)
.mode(0o600)
.unwrap()
.size(7)
.unwrap()
.mtime(9999999999)
.unwrap();
builder
.add_pax(
"SCHILY.xattr.security.selinux",
b"system_u:object_r:usr_t:s0",
)
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(b"payload");
let pad = (HEADER_SIZE - 7) % HEADER_SIZE;
archive.extend(std::iter::repeat_n(0u8, pad));
archive.extend_from_slice(&EOA);
seeds.push(("pax_multi_ext", archive));
}
// 16. Multiple files in one archive
{
let mut archive = Vec::new();
for i in 0..5u8 {
let name = format!("file_{i}.txt");
let content = format!("content {i}");
let mut builder = EntryBuilder::new_gnu();
builder
.path(name.as_bytes())
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(content.len() as u64)
.unwrap();
archive.extend_from_slice(&builder.finish_bytes());
archive.extend_from_slice(content.as_bytes());
let pad = (HEADER_SIZE - (content.len() % HEADER_SIZE)) % HEADER_SIZE;
archive.extend(std::iter::repeat_n(0u8, pad));
}
archive.extend_from_slice(&EOA);
seeds.push(("multi_file", archive));
}
// 17. Mixed types: dir + file + symlink
{
let mut archive = Vec::new();
let mut b = EntryBuilder::new_gnu();
b.path(b"project/")
.entry_type(EntryType::Directory)
.mode(0o755)
.unwrap()
.size(0)
.unwrap();
archive.extend_from_slice(&b.finish_bytes());
let content = b"fn main() {}";
let mut b = EntryBuilder::new_gnu();
b.path(b"project/main.rs")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(content.len() as u64)
.unwrap();
archive.extend_from_slice(&b.finish_bytes());
archive.extend_from_slice(content);
let pad = (HEADER_SIZE - (content.len() % HEADER_SIZE)) % HEADER_SIZE;
archive.extend(std::iter::repeat_n(0u8, pad));
let mut b = EntryBuilder::new_gnu();
b.path(b"project/latest")
.link_name(b"main.rs")
.entry_type(EntryType::Symlink)
.mode(0o777)
.unwrap()
.size(0)
.unwrap();
archive.extend_from_slice(&b.finish_bytes());
archive.extend_from_slice(&EOA);
seeds.push(("mixed_types", archive));
}
// 18. Content exactly 512 bytes (block boundary)
{
let content = vec![0xABu8; 512];
seeds.push((
"exact_block",
build_archive(
|b| {
b.path(b"block.bin")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(512)
.unwrap();
},
&content,
),
));
}
// 19. Content just over block boundary (513 bytes)
{
let content = vec![0xCDu8; 513];
seeds.push((
"over_block",
build_archive(
|b| {
b.path(b"overblock.bin")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(513)
.unwrap();
},
&content,
),
));
}
// 20. setuid mode
seeds.push((
"mode_setuid",
build_archive(
|b| {
b.path(b"setuid.bin")
.entry_type(EntryType::Regular)
.mode(0o4755)
.unwrap()
.size(0)
.unwrap();
},
&[],
),
));
// 21. Minimal valid header (no EOA — tests truncated input)
{
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"minimal")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
seeds.push(("minimal_header", builder.finish_bytes()));
}
// 22. Username and groupname in header
{
let mut builder = EntryBuilder::new_ustar();
builder
.path(b"owned.txt")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap()
.uid(1000)
.unwrap()
.gid(1000)
.unwrap()
.username(b"testuser")
.unwrap()
.groupname(b"testgroup")
.unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("uname_gname", archive));
}
// 23. PAX linkpath override
{
let mut builder = EntryBuilder::new_ustar();
builder
.path(b"pax_link")
.link_name(b"short")
.entry_type(EntryType::Symlink)
.mode(0o777)
.unwrap()
.size(0)
.unwrap();
// Force a PAX linkpath by using a long target
let long_target = "/target/".repeat(20) + "dest";
builder.link_name(long_target.as_bytes());
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("pax_linkpath", archive));
}
// 24. PAX uname/gname override (long names that overflow header fields)
{
let mut builder = EntryBuilder::new_ustar();
builder
.path(b"pax_owner.txt")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
// 33-byte names overflow the 32-byte GNU header field, requiring PAX
let long_uname = "u".repeat(33);
let long_gname = "g".repeat(33);
builder.username(long_uname.as_bytes()).unwrap();
builder.groupname(long_gname.as_bytes()).unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("pax_uname_gname", archive));
}
// 25. PAX fractional mtime
{
let mut builder = EntryBuilder::new_ustar();
builder
.path(b"fractional_mtime.txt")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap()
.mtime(1700000000)
.unwrap();
builder.add_pax("mtime", b"1700000000.123456789").unwrap();
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend_from_slice(&EOA);
seeds.push(("pax_fractional_mtime", archive));
}
// 26. Single zero block followed by valid header (mid-stream zero block recovery)
{
let mut archive = Vec::new();
// One zero block (not two, so not end-of-archive)
archive.extend(std::iter::repeat_n(0u8, HEADER_SIZE));
// A valid entry
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"after_zero.txt")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(0)
.unwrap();
archive.extend_from_slice(&builder.finish_bytes());
archive.extend_from_slice(&EOA);
seeds.push(("zero_block_recovery", archive));
}
// 27. V7 format (no magic/version — old-style header)
// We create a raw header without ustar magic
{
let mut raw = [0u8; HEADER_SIZE];
// path: "v7file.txt"
raw[0..10].copy_from_slice(b"v7file.txt");
// mode: "0000644\0"
raw[100..108].copy_from_slice(b"0000644\0");
// uid: "0001000\0"
raw[108..116].copy_from_slice(b"0001000\0");
// gid: "0001000\0"
raw[116..124].copy_from_slice(b"0001000\0");
// size: "00000000000\0"
raw[124..136].copy_from_slice(b"00000000000\0");
// mtime: "00000000000\0"
raw[136..148].copy_from_slice(b"00000000000\0");
// typeflag: '0' (regular)
raw[156] = b'0';
// No magic — this is a V7 header
// Compute checksum
// Checksum field (148..156) should be spaces during computation
raw[148..156].copy_from_slice(b" ");
let cksum: u32 = raw.iter().map(|&b| b as u32).sum();
let cksum_str = format!("{cksum:06o}\0 ");
raw[148..156].copy_from_slice(cksum_str.as_bytes());
let mut archive = raw.to_vec();
archive.extend_from_slice(&EOA);
seeds.push(("v7_format", archive));
}
// 28. GNU sparse, 2 inline entries
{
let sparse_map = [
SparseEntry {
offset: 0,
length: 100,
},
SparseEntry {
offset: 1000,
length: 200,
},
];
let on_disk: u64 = 300;
let real_size: u64 = 1200;
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"sparse_gnu_basic.bin")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(on_disk)
.unwrap()
.sparse(&sparse_map, real_size);
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend(vec![0u8; on_disk.next_multiple_of(512) as usize]);
archive.extend_from_slice(&EOA);
seeds.push(("sparse_gnu_basic", archive));
}
// 29. GNU sparse, 6 entries (needs extension block: >4 inline descriptors)
{
let sparse_map: Vec<SparseEntry> = (0..6)
.map(|i| SparseEntry {
offset: i * 1000,
length: 50,
})
.collect();
let on_disk: u64 = 300;
let real_size: u64 = 5050;
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"sparse_gnu_ext.bin")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(on_disk)
.unwrap()
.sparse(&sparse_map, real_size);
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend(vec![0u8; on_disk.next_multiple_of(512) as usize]);
archive.extend_from_slice(&EOA);
seeds.push(("sparse_gnu_ext", archive));
}
// 30. GNU sparse, 28 entries (multiple extension blocks: 4 inline + 21 + 3)
{
let sparse_map: Vec<SparseEntry> = (0..28)
.map(|i| SparseEntry {
offset: i * 500,
length: 30,
})
.collect();
let on_disk: u64 = 28 * 30;
let real_size: u64 = 27 * 500 + 30;
let mut builder = EntryBuilder::new_gnu();
builder
.path(b"sparse_gnu_multi_ext.bin")
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(on_disk)
.unwrap()
.sparse(&sparse_map, real_size);
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend(vec![0u8; on_disk.next_multiple_of(512) as usize]);
archive.extend_from_slice(&EOA);
seeds.push(("sparse_gnu_multi_ext", archive));
}
// 31. PAX sparse v1.0, 2 entries
{
let sparse_map = [
SparseEntry {
offset: 0,
length: 100,
},
SparseEntry {
offset: 3000,
length: 400,
},
];
let on_disk: u64 = 500;
let real_size: u64 = 3400;
let mut builder = EntryBuilder::new_ustar();
builder
.path(b"sparse_pax_basic.dat")
.mode(0o644)
.unwrap()
.size(on_disk)
.unwrap()
.sparse(&sparse_map, real_size);
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend(vec![0u8; on_disk.next_multiple_of(512) as usize]);
archive.extend_from_slice(&EOA);
seeds.push(("sparse_pax_basic", archive));
}
// 32. PAX sparse v1.0, 10 entries
{
let sparse_map: Vec<SparseEntry> = (0..10)
.map(|i| SparseEntry {
offset: i * 1000,
length: 50,
})
.collect();
let on_disk: u64 = 500;
let real_size: u64 = 9050;
let mut builder = EntryBuilder::new_ustar();
builder
.path(b"sparse_pax_many.dat")
.mode(0o644)
.unwrap()
.size(on_disk)
.unwrap()
.sparse(&sparse_map, real_size);
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend(vec![0u8; on_disk.next_multiple_of(512) as usize]);
archive.extend_from_slice(&EOA);
seeds.push(("sparse_pax_many", archive));
}
// 33. GNU sparse with long path (>100 bytes)
{
let long_path = "sparse/".repeat(20) + "data.bin"; // 148 chars
let sparse_map = [
SparseEntry {
offset: 0,
length: 200,
},
SparseEntry {
offset: 4096,
length: 100,
},
];
let on_disk: u64 = 300;
let real_size: u64 = 4196;
let mut builder = EntryBuilder::new_gnu();
builder
.path(long_path.as_bytes())
.entry_type(EntryType::Regular)
.mode(0o644)
.unwrap()
.size(on_disk)
.unwrap()
.sparse(&sparse_map, real_size);
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend(vec![0u8; on_disk.next_multiple_of(512) as usize]);
archive.extend_from_slice(&EOA);
seeds.push(("sparse_gnu_longpath", archive));
}
// 34. PAX sparse with long path (>100 bytes)
{
let long_path = "paxsparse/".repeat(15) + "file.dat"; // 158 chars
let sparse_map = [
SparseEntry {
offset: 0,
length: 512,
},
SparseEntry {
offset: 8192,
length: 256,
},
];
let on_disk: u64 = 768;
let real_size: u64 = 8448;
let mut builder = EntryBuilder::new_ustar();
builder
.path(long_path.as_bytes())
.mode(0o644)
.unwrap()
.size(on_disk)
.unwrap()
.sparse(&sparse_map, real_size);
let hdr = builder.finish_bytes();
let mut archive = hdr;
archive.extend(vec![0u8; on_disk.next_multiple_of(512) as usize]);
archive.extend_from_slice(&EOA);
seeds.push(("sparse_pax_longpath", archive));
}
// Write seeds
let mut count = 0;
for (name, data) in &seeds {
let path = corpus_dir.join(name);
fs::write(&path, data).unwrap_or_else(|e| panic!("write {}: {e}", path.display()));
count += 1;
println!("{:>4} {:>6} bytes {}", count, data.len(), name);
}
println!("\nGenerated {count} seed files in {}", corpus_dir.display());
}
/// Build a single-entry GNU archive with optional content.
fn build_archive(configure: impl FnOnce(&mut EntryBuilder), content: &[u8]) -> Vec<u8> {
let mut builder = EntryBuilder::new_gnu();
configure(&mut builder);
assemble(builder, content)
}
/// Build a single-entry PAX/UStar archive with optional content.
fn build_archive_pax(configure: impl FnOnce(&mut EntryBuilder), content: &[u8]) -> Vec<u8> {
let mut builder = EntryBuilder::new_ustar();
configure(&mut builder);
assemble(builder, content)
}
/// Assemble header + content + padding + EOA into a complete archive.
fn assemble(mut builder: EntryBuilder, content: &[u8]) -> Vec<u8> {
let hdr = builder.finish_bytes();
let mut archive = Vec::with_capacity(hdr.len() + content.len() + HEADER_SIZE + EOA.len());
archive.extend_from_slice(&hdr);
if !content.is_empty() {
archive.extend_from_slice(content);
let pad = (HEADER_SIZE - (content.len() % HEADER_SIZE)) % HEADER_SIZE;
archive.extend(std::iter::repeat_n(0u8, pad));
}
archive.extend_from_slice(&EOA);
archive
}