ferrocrypt 0.3.0-beta.1

Recipient-oriented file and directory encryption: passphrase (Argon2id) and X25519 public-key recipients, XChaCha20-Poly1305 STREAM payloads, HKDF-SHA3-256 / HMAC-SHA3-256 key derivation and authentication.
Documentation
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
//! FCA wire-format constants and primitives: header parse/build,
//! manifest serialize/parse, and big-endian integer helpers.
//!
//! See `ferrocrypt-lib/FORMAT.md` §9 — §9.2 (fixed header), §9.3
//! (manifest layout), §9.4 (per-entry prefix), §9.10 (writer
//! obligations), §9.11 (reader pipeline).

use std::io::{self, Cursor, Read, Write};

use crate::CryptoError;
use crate::crypto::tlv::validate_no_known_critical;
use crate::error::FormatDefect;

use super::limits::{
    ARCHIVE_ENTRY_MODE_UNSUPPORTED, ArchiveLimits, enforce_archive_ext_cap,
    enforce_entry_count_cap, enforce_entry_ext_cap, enforce_manifest_len_cap,
    enforce_path_bytes_cap, enforce_total_entry_ext_cap, enforce_total_plaintext_bytes_cap,
};
use super::model::{ArchiveEntry, ArchiveEntryKind, FcaHeader, Manifest};
use super::path::validate_fca_path;
use super::tree::validate_manifest_tree;

pub(crate) const FCA_MAGIC: &[u8; 4] = b"FCA\0";
pub(crate) const FCA_VERSION: u8 = 0x01;
/// Size of the fixed FCA header in bytes: `magic(4) || version(1) ||
/// flags(2) || entry_count(4) || archive_ext_len(4) || manifest_len(4)
/// || total_file_bytes(8)` (FORMAT.md §9.2). Documented as a spec
/// constant for callers that need it for byte-level inspection or
/// fixture construction; the parser reads each field individually and
/// does not use this constant directly.
#[allow(dead_code)]
pub(crate) const FCA_HEADER_SIZE: usize = 27;
pub(crate) const FCA_ENTRY_FIXED_SIZE: usize = 18;

pub(crate) const KIND_FILE: u8 = 0x01;
pub(crate) const KIND_DIR: u8 = 0x02;

pub(crate) const FCA_FLAGS_V1: u16 = 0;
pub(crate) const FCA_ENTRY_FLAGS_V1: u8 = 0;
pub(crate) const PERMISSION_BITS_MASK: u32 = 0o777;

/// Streaming buffer size for [`copy_exact_n`] (64 KiB). Aligned with
/// [`crate::crypto::stream::BUFFER_SIZE`] — matching the AEAD chunk
/// size avoids stalling the encrypt/decrypt pipeline on internal
/// re-buffering, but the constants are separate because the archive
/// layer does not depend on crypto/stream.
const COPY_BUFFER_SIZE: usize = 64 * 1024;

/// Single source of truth for the "Malformed archive manifest"
/// rejection. Used by every `parse_manifest_bytes` arm whose only
/// useful diagnostic is "the bytes don't match what `header` declared"
/// (truncated entry header, slice overflow on a path region,
/// trailing bytes after the last declared entry).
fn malformed_manifest() -> CryptoError {
    CryptoError::InvalidInput("Malformed archive manifest".to_string())
}

/// Single source of truth for the "Empty archive" rejection. Used by
/// the writer's pre-emit `entry_count == 0` check, the reader's
/// header `entry_count == 0` arm, and the tree validator's
/// `entries.is_empty()` check.
pub(super) fn empty_archive_error() -> CryptoError {
    CryptoError::InvalidInput("Empty archive".to_string())
}

/// Initial capacity hint for the parsed-entries `Vec` in
/// [`parse_manifest_bytes`]. Caps the allocation so an
/// adversary-controlled `header.entry_count` cannot drive a
/// multi-megabyte pre-allocation; small archives still fit in one
/// allocation, larger ones grow the `Vec` organically.
const MANIFEST_PARSE_INITIAL_CAPACITY: usize = 1024;

/// Converts a `u32` wire-format length field to `usize` for allocation
/// or slicing. Rejects with [`CryptoError::InvalidInput`] if the value
/// won't fit in the current platform's `usize`. On every supported
/// target `usize` is at least as wide as `u32`, so this only fires
/// under a hypothetical narrower-`usize` platform — but the check is
/// defense-in-depth so a regression in a structural cap is caught at
/// the conversion boundary, not inside an `as usize` cast.
pub(super) fn require_fits_usize(value: u32, label: &str) -> Result<usize, CryptoError> {
    usize::try_from(value)
        .map_err(|_| CryptoError::InvalidInput(format!("{label} cannot fit in memory")))
}

/// FCA archive-level TLV validator: scans `archive_ext` under the v1.0
/// no-known-critical policy, capped by [`ArchiveLimits::max_archive_ext_bytes`]
/// (region) and [`ArchiveLimits::max_tlv_value_bytes`] (per-value). Used
/// by the reader (post-`parse_fca_header`) and any future archive-level
/// TLV emitter on the writer side. Single source of truth for the cap
/// pair so callers can't drift the policy.
pub(crate) fn validate_archive_ext_tlv(
    bytes: &[u8],
    limits: &ArchiveLimits,
) -> Result<(), CryptoError> {
    validate_no_known_critical(
        bytes,
        limits.max_archive_ext_bytes,
        limits.max_tlv_value_bytes,
    )
}

/// FCA per-entry TLV validator: scans `entry_ext` under the v1.0
/// no-known-critical policy, capped by [`ArchiveLimits::max_entry_ext_bytes`]
/// (region) and [`ArchiveLimits::max_tlv_value_bytes`] (per-value). Used
/// symmetrically by writer-side `checked_manifest_len` (FORMAT.md §9.10
/// writer obligation) and reader-side `parse_manifest_bytes`. Single
/// source of truth for the cap pair.
pub(crate) fn validate_entry_ext_tlv(
    bytes: &[u8],
    limits: &ArchiveLimits,
) -> Result<(), CryptoError> {
    validate_no_known_critical(
        bytes,
        limits.max_entry_ext_bytes,
        limits.max_tlv_value_bytes,
    )
}

/// Single source of truth for the per-manifest-entry cap-and-shape
/// sequence shared by writer and reader.
///
/// Writer ([`checked_manifest_len`]) and reader
/// ([`parse_manifest_bytes`]) MUST run these checks in the same order
/// so a future change to per-entry validation (new cap, reordered
/// rejection priority) lands in one place. The entry-flags check is
/// reader-only (writer always emits zero) so it stays inline at the
/// reader. Per-entry TLV scanning ([`validate_entry_ext_tlv`]) is also
/// separate because the bytes become available at different points
/// (writer: from `entry.entry_ext`; reader: from the resolved slice
/// after the path has been parsed).
///
/// `path` is `Some(&entry.path_utf8)` on the writer side and `None` on
/// the reader's pre-allocation guard (the path bytes haven't been
/// resolved yet).
pub(crate) fn enforce_manifest_entry_caps(
    mode: u32,
    path_len: u32,
    path: Option<&str>,
    entry_ext_len: u64,
    total_entry_ext_bytes: &mut u64,
    limits: &ArchiveLimits,
) -> Result<(), CryptoError> {
    if mode > PERMISSION_BITS_MASK {
        return Err(CryptoError::InvalidInput(
            ARCHIVE_ENTRY_MODE_UNSUPPORTED.to_string(),
        ));
    }
    if path_len == 0 {
        return Err(CryptoError::InvalidInput(
            "Empty archive entry path".to_string(),
        ));
    }
    enforce_path_bytes_cap(path_len, path, limits)?;
    enforce_entry_ext_cap(entry_ext_len, path, limits)?;
    enforce_total_entry_ext_cap(entry_ext_len, total_entry_ext_bytes, limits)?;
    Ok(())
}

pub(super) fn read_u8<R: Read>(r: &mut R) -> io::Result<u8> {
    let mut b = [0u8; 1];
    r.read_exact(&mut b)?;
    Ok(b[0])
}

pub(super) fn read_u16_be<R: Read>(r: &mut R) -> io::Result<u16> {
    let mut b = [0u8; 2];
    r.read_exact(&mut b)?;
    Ok(u16::from_be_bytes(b))
}

pub(super) fn read_u32_be<R: Read>(r: &mut R) -> io::Result<u32> {
    let mut b = [0u8; 4];
    r.read_exact(&mut b)?;
    Ok(u32::from_be_bytes(b))
}

pub(super) fn read_u64_be<R: Read>(r: &mut R) -> io::Result<u64> {
    let mut b = [0u8; 8];
    r.read_exact(&mut b)?;
    Ok(u64::from_be_bytes(b))
}

pub(super) fn write_u8<W: Write>(w: &mut W, n: u8) -> io::Result<()> {
    w.write_all(&[n])
}

/// Reads exactly `size` bytes from `reader` and writes them to `writer`.
/// Used by both the encrypt-side content pass (source file → encrypted
/// stream) and the decrypt-side content extraction (encrypted stream →
/// output file). FORMAT.md §9.9: archive content bytes MUST NOT use
/// unbounded `io::copy`, which would happily keep reading past `size`
/// on a misbehaving reader.
///
/// On a short read (reader returns `Ok(0)` while bytes are still
/// expected), returns [`CryptoError::InvalidInput`] with the
/// "shorter than declared size" diagnostic. The `?` on `read` threads
/// `StreamError` markers from the underlying decrypt stream through
/// `From<io::Error> for CryptoError` so authentication / truncation /
/// extra-data signals surface as the typed `CryptoError::Payload*`
/// variant rather than as a generic archive error.
pub(super) fn copy_exact_n<R: Read, W: Write>(
    reader: &mut R,
    writer: &mut W,
    size: u64,
) -> Result<(), CryptoError> {
    let mut buf = [0u8; COPY_BUFFER_SIZE];
    let mut remaining = size;
    while remaining > 0 {
        // Buffer is 64 KiB which fits any usize on supported targets;
        // the `min` ensures the cast is bounded by the smaller side.
        let want = std::cmp::min(buf.len() as u64, remaining) as usize;
        let n = reader.read(&mut buf[..want])?;
        if n == 0 {
            return Err(CryptoError::InvalidInput(
                "Archive file content is shorter than declared size".to_string(),
            ));
        }
        writer.write_all(&buf[..n]).map_err(CryptoError::Io)?;
        remaining -= n as u64;
    }
    Ok(())
}

pub(super) fn write_u16_be<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
    w.write_all(&n.to_be_bytes())
}

pub(super) fn write_u32_be<W: Write>(w: &mut W, n: u32) -> io::Result<()> {
    w.write_all(&n.to_be_bytes())
}

pub(super) fn write_u64_be<W: Write>(w: &mut W, n: u64) -> io::Result<()> {
    w.write_all(&n.to_be_bytes())
}

/// Writes the 27-byte FCA fixed header. Returns the writer on success
/// so the caller can chain `archive_ext` + manifest + content writes.
/// Refuses `entry_count == 0` and `manifest_len == 0` before emitting
/// any bytes.
///
/// **Caller contract** — this is a raw byte writer. It does NOT enforce
/// `ArchiveLimits` against `archive_ext_len`, `manifest_len`,
/// `entry_count`, or `total_file_bytes`; it only rejects two structural
/// "must be non-zero" cases. Production callers preflight all caps in
/// the metadata pass ([`checked_manifest_len`], the `archive::encode`
/// resource-cap loop, and `parse_fca_header`'s own caps on the read
/// side) and then call this helper. Any future emitter that supplies a
/// non-zero `archive_ext_len` MUST cap it against
/// [`crate::ArchiveLimits::max_archive_ext_bytes`] and TLV-validate the
/// region (per `FORMAT.md` §9.10) before calling this function.
pub(crate) fn write_fca_header<W: Write>(
    mut w: W,
    entry_count: u32,
    archive_ext_len: u32,
    manifest_len: u32,
    total_file_bytes: u64,
) -> Result<W, CryptoError> {
    if entry_count == 0 {
        return Err(empty_archive_error());
    }
    if manifest_len == 0 {
        return Err(malformed_manifest());
    }

    w.write_all(FCA_MAGIC).map_err(CryptoError::Io)?;
    write_u8(&mut w, FCA_VERSION).map_err(CryptoError::Io)?;
    write_u16_be(&mut w, FCA_FLAGS_V1).map_err(CryptoError::Io)?;
    write_u32_be(&mut w, entry_count).map_err(CryptoError::Io)?;
    write_u32_be(&mut w, archive_ext_len).map_err(CryptoError::Io)?;
    write_u32_be(&mut w, manifest_len).map_err(CryptoError::Io)?;
    write_u64_be(&mut w, total_file_bytes).map_err(CryptoError::Io)?;
    Ok(w)
}

/// Parses and structurally validates the 27-byte FCA fixed header.
/// All resource caps are applied here so downstream allocations
/// (`archive_ext` buffer, manifest buffer, entry vector) are bounded
/// by the time they fire.
pub fn parse_fca_header<R: Read>(
    reader: &mut R,
    limits: ArchiveLimits,
) -> Result<FcaHeader, CryptoError> {
    limits.validate()?;

    let mut magic = [0u8; 4];
    reader.read_exact(&mut magic)?;
    if &magic != FCA_MAGIC {
        return Err(CryptoError::InvalidInput(
            "Bad FerroCrypt archive magic".to_string(),
        ));
    }

    let version = read_u8(reader)?;
    if version != FCA_VERSION {
        return Err(CryptoError::InvalidFormat(
            FormatDefect::UnsupportedArchiveVersion { version },
        ));
    }

    let flags = read_u16_be(reader)?;
    if flags != 0 {
        return Err(CryptoError::InvalidInput(
            "FerroCrypt archive header has non-zero flags".to_string(),
        ));
    }

    let entry_count = read_u32_be(reader)?;
    let archive_ext_len = read_u32_be(reader)?;
    let manifest_len = read_u32_be(reader)?;
    let total_file_bytes = read_u64_be(reader)?;

    if entry_count == 0 {
        return Err(empty_archive_error());
    }
    enforce_entry_count_cap(entry_count, &limits)?;
    enforce_archive_ext_cap(u64::from(archive_ext_len), &limits)?;
    let _ = require_fits_usize(archive_ext_len, "Archive extension length")?;
    if manifest_len == 0 {
        return Err(malformed_manifest());
    }
    enforce_manifest_len_cap(u64::from(manifest_len), &limits)?;
    let _ = require_fits_usize(manifest_len, "Archive manifest length")?;
    enforce_total_plaintext_bytes_cap(total_file_bytes, &limits)?;

    Ok(FcaHeader {
        entry_count,
        archive_ext_len,
        manifest_len,
        total_file_bytes,
    })
}

/// Pre-computes the serialized manifest length with checked
/// arithmetic before any allocation, validating per-entry mode and
/// path-byte caps along the way. Caller invokes this BEFORE
/// allocating the manifest buffer; if it returns
/// [`CryptoError::InvalidInput`], no allocation has happened.
pub(crate) fn checked_manifest_len(
    entries: &[ArchiveEntry],
    limits: ArchiveLimits,
) -> Result<usize, CryptoError> {
    let limits = limits.validate()?;

    let mut len: usize = 0;
    let mut total_entry_ext_bytes: u64 = 0;
    for entry in entries {
        let path_len = entry.path_utf8.len();
        let entry_ext_len = entry.entry_ext.len();
        enforce_manifest_entry_caps(
            entry.mode,
            u32::try_from(path_len).unwrap_or(u32::MAX),
            Some(&entry.path_utf8),
            entry_ext_len as u64,
            &mut total_entry_ext_bytes,
            &limits,
        )?;

        // FORMAT.md §9.10: writers MUST apply the same TLV canonicality
        // rules as readers before emitting. v1 writers normally pass
        // `entry_ext = Vec::new()`, but a future v1.x caller that
        // constructs an `ArchiveEntry` directly with malformed
        // `entry_ext` bytes is rejected here, preserving the
        // "FerroCrypt MUST NOT write archives its own default reader
        // will reject" invariant.
        validate_entry_ext_tlv(&entry.entry_ext, &limits)?;

        len = len
            .checked_add(FCA_ENTRY_FIXED_SIZE)
            .and_then(|n| n.checked_add(path_len))
            .and_then(|n| n.checked_add(entry_ext_len))
            .ok_or_else(|| {
                CryptoError::InvalidInput("Archive manifest length overflow".to_string())
            })?;

        enforce_manifest_len_cap(len as u64, &limits)?;
    }

    Ok(len)
}

/// Serializes a [`Manifest`] into the FCA manifest byte sequence.
/// The total byte length matches [`checked_manifest_len`] exactly,
/// asserted via `debug_assert_eq!` post-condition.
pub(crate) fn serialize_manifest(
    manifest: &Manifest,
    limits: ArchiveLimits,
) -> Result<Vec<u8>, CryptoError> {
    let expected_len = checked_manifest_len(&manifest.entries, limits)?;
    let mut out = Vec::with_capacity(expected_len);

    for entry in &manifest.entries {
        let path_bytes = entry.path_utf8.as_bytes();
        // path_len fits in u16 because checked_manifest_len rejected
        // path_len > u16::MAX.
        let path_len = u16::try_from(path_bytes.len()).map_err(|_| {
            CryptoError::InvalidInput("Archive path exceeds FCA u16 length".to_string())
        })?;
        // entry_ext_len fits in u32 because checked_manifest_len
        // rejected larger values.
        let entry_ext_len = u32::try_from(entry.entry_ext.len()).map_err(|_| {
            CryptoError::InvalidInput(
                "Archive entry extension length cannot fit in u32".to_string(),
            )
        })?;

        let kind = match entry.kind {
            ArchiveEntryKind::File => KIND_FILE,
            ArchiveEntryKind::Directory => KIND_DIR,
        };

        write_u8(&mut out, kind).map_err(CryptoError::Io)?;
        write_u8(&mut out, FCA_ENTRY_FLAGS_V1).map_err(CryptoError::Io)?;
        // mode fits in u16 because checked_manifest_len rejected
        // mode > 0o777.
        let mode_u16 = u16::try_from(entry.mode).map_err(|_| {
            CryptoError::InvalidInput("Archive entry mode does not fit in u16".to_string())
        })?;
        write_u16_be(&mut out, mode_u16).map_err(CryptoError::Io)?;
        write_u16_be(&mut out, path_len).map_err(CryptoError::Io)?;
        write_u32_be(&mut out, entry_ext_len).map_err(CryptoError::Io)?;
        write_u64_be(&mut out, entry.size).map_err(CryptoError::Io)?;
        out.extend_from_slice(path_bytes);
        out.extend_from_slice(&entry.entry_ext);
    }

    debug_assert_eq!(out.len(), expected_len);
    Ok(out)
}

/// Parses the manifest byte sequence into a [`Manifest`]. Validates
/// per-entry shape, UTF-8 path grammar (via [`validate_fca_path`]),
/// total-bytes equality against `header.total_file_bytes`, no-trailing-
/// bytes, and the manifest tree shape (via the crate-internal
/// `validate_manifest_tree`).
///
/// The caller MUST have already trimmed `bytes` to exactly
/// `header.manifest_len` bytes.
pub fn parse_manifest_bytes(
    bytes: &[u8],
    header: FcaHeader,
    limits: ArchiveLimits,
) -> Result<Manifest, CryptoError> {
    let limits = limits.validate()?;

    if bytes.len() != require_fits_usize(header.manifest_len, "Archive manifest length")? {
        return Err(malformed_manifest());
    }

    let mut cursor = Cursor::new(bytes);
    let entry_count_usize = require_fits_usize(header.entry_count, "Archive entry count")?;
    let mut entries = Vec::with_capacity(entry_count_usize.min(MANIFEST_PARSE_INITIAL_CAPACITY));
    let mut total_file_bytes: u64 = 0;
    let mut total_entry_ext_bytes: u64 = 0;

    for _ in 0..header.entry_count {
        let pos = cursor.position() as usize;
        let remaining = bytes.len().saturating_sub(pos);
        if remaining < FCA_ENTRY_FIXED_SIZE {
            return Err(malformed_manifest());
        }

        let kind_byte = read_u8(&mut cursor)?;
        let entry_flags = read_u8(&mut cursor)?;
        let mode = read_u16_be(&mut cursor)?;
        let path_len = read_u16_be(&mut cursor)?;
        let entry_ext_len = read_u32_be(&mut cursor)?;
        let size = read_u64_be(&mut cursor)?;

        if entry_flags != 0 {
            return Err(CryptoError::InvalidInput(
                "Archive entry has non-zero reserved flags".to_string(),
            ));
        }
        enforce_manifest_entry_caps(
            u32::from(mode),
            u32::from(path_len),
            None,
            u64::from(entry_ext_len),
            &mut total_entry_ext_bytes,
            &limits,
        )?;

        let path_start = cursor.position() as usize;
        let path_end = path_start
            .checked_add(path_len as usize)
            .ok_or_else(malformed_manifest)?;
        if path_end > bytes.len() {
            return Err(malformed_manifest());
        }

        let path_bytes = &bytes[path_start..path_end];
        cursor.set_position(path_end as u64);

        let path_utf8 = std::str::from_utf8(path_bytes)
            .map_err(|_| CryptoError::InvalidInput("Archive path is not valid UTF-8".to_string()))?
            .to_owned();

        validate_fca_path(&path_utf8, limits)?;

        let ext_start = cursor.position() as usize;
        let ext_end = ext_start
            .checked_add(entry_ext_len as usize)
            .ok_or_else(malformed_manifest)?;
        if ext_end > bytes.len() {
            return Err(malformed_manifest());
        }

        // Per FORMAT.md §9.5: every per-entry TLV region is parsed +
        // canonicality-validated under the no-known-critical policy.
        // v1 defines no per-entry TLV tags, so any tag present is
        // either ignorable (skip after canonicality) or critical
        // (reject as `UnknownCriticalTag`). Scan on the borrowed slice
        // first so a malformed or unknown-critical region is rejected
        // before any allocation.
        let entry_ext_slice = &bytes[ext_start..ext_end];
        validate_entry_ext_tlv(entry_ext_slice, &limits)?;
        let entry_ext_bytes = entry_ext_slice.to_vec();
        cursor.set_position(ext_end as u64);

        let kind = match kind_byte {
            KIND_FILE => {
                total_file_bytes = total_file_bytes.checked_add(size).ok_or_else(|| {
                    CryptoError::InvalidInput("Archive total file bytes overflow".to_string())
                })?;
                ArchiveEntryKind::File
            }
            KIND_DIR => {
                if size != 0 {
                    return Err(CryptoError::InvalidInput(
                        "Directory archive entry has non-zero size".to_string(),
                    ));
                }
                ArchiveEntryKind::Directory
            }
            _ => {
                return Err(CryptoError::InvalidInput(
                    "Unsupported archive entry kind".to_string(),
                ));
            }
        };

        entries.push(ArchiveEntry {
            kind,
            path_utf8,
            mode: u32::from(mode),
            size,
            source_path: None,
            entry_ext: entry_ext_bytes,
        });
    }

    if cursor.position() as usize != bytes.len() {
        return Err(malformed_manifest());
    }

    if total_file_bytes != header.total_file_bytes {
        return Err(CryptoError::InvalidInput(
            "Archive total-bytes mismatch".to_string(),
        ));
    }
    enforce_total_plaintext_bytes_cap(total_file_bytes, &limits)?;

    let (root_name, root_is_file, root_mode) =
        validate_manifest_tree(&entries, total_file_bytes, limits)?;

    Ok(Manifest {
        entries,
        total_file_bytes,
        root_name,
        root_is_file,
        root_mode,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::OsString;
    use std::io::Cursor;

    /// Constructs raw header bytes from explicit field values for
    /// testing rejections that the writer would otherwise refuse to
    /// emit (e.g. zero entry_count, non-zero flags, bad version).
    fn raw_header_bytes(
        version: u8,
        flags: u16,
        entry_count: u32,
        archive_ext_len: u32,
        manifest_len: u32,
        total_file_bytes: u64,
    ) -> Vec<u8> {
        let mut buf = Vec::with_capacity(FCA_HEADER_SIZE);
        buf.extend_from_slice(FCA_MAGIC);
        buf.push(version);
        buf.extend_from_slice(&flags.to_be_bytes());
        buf.extend_from_slice(&entry_count.to_be_bytes());
        buf.extend_from_slice(&archive_ext_len.to_be_bytes());
        buf.extend_from_slice(&manifest_len.to_be_bytes());
        buf.extend_from_slice(&total_file_bytes.to_be_bytes());
        debug_assert_eq!(buf.len(), FCA_HEADER_SIZE);
        buf
    }

    #[test]
    fn header_round_trip() {
        let mut buf = Vec::new();
        let _ = write_fca_header(&mut buf, 7, 0, 200, 4096).expect("valid params write");
        assert_eq!(buf.len(), FCA_HEADER_SIZE);

        let mut cur = Cursor::new(&buf);
        let parsed = parse_fca_header(&mut cur, ArchiveLimits::default()).expect("valid parse");
        assert_eq!(parsed.entry_count, 7);
        assert_eq!(parsed.archive_ext_len, 0);
        assert_eq!(parsed.manifest_len, 200);
        assert_eq!(parsed.total_file_bytes, 4096);
    }

    /// Non-zero `archive_ext_len` round-trips through the writer and
    /// parser. v1 writers emit zero, but the parser MUST accept any
    /// caller-provided value within the cap.
    #[test]
    fn header_round_trip_with_archive_ext_len() {
        let mut buf = Vec::new();
        let _ = write_fca_header(&mut buf, 1, 32, 200, 0).expect("valid params write");

        let mut cur = Cursor::new(&buf);
        let parsed = parse_fca_header(&mut cur, ArchiveLimits::default()).expect("valid parse");
        assert_eq!(parsed.archive_ext_len, 32);
    }

    #[test]
    fn rejects_oversize_archive_ext_len() {
        let limits = ArchiveLimits::default();
        let bytes = raw_header_bytes(
            FCA_VERSION,
            0,
            5,
            limits.max_archive_ext_bytes + 1,
            100,
            1024,
        );
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, limits).unwrap_err();
        assert!(format!("{err}").contains("Archive extension length cap exceeded"));
    }

    /// `entry_ext_len` above the per-entry cap rejects via
    /// `parse_manifest_bytes`. Pin so a future refactor that loosens
    /// the per-entry path doesn't silently widen the resource budget.
    #[test]
    fn parse_rejects_oversize_entry_ext_len() {
        let limits = ArchiveLimits::default().with_max_entry_ext_bytes(8);
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 9, 10, b"file", &[0u8; 9]);
        let header = FcaHeader {
            entry_count: 1,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: 10,
        };
        let err = parse_manifest_bytes(&bytes, header, limits).unwrap_err();
        assert!(format!("{err}").contains("Archive entry extension length cap exceeded"));
    }

    /// Sum of per-entry TLV bytes above
    /// `max_total_entry_ext_bytes` rejects, even when each individual
    /// entry's `entry_ext_len` is under `max_entry_ext_bytes`. The
    /// per-entry TLV regions are valid ignorable TLVs so the cap
    /// fires before the scanner does.
    #[test]
    fn parse_rejects_total_entry_ext_above_cap() {
        let limits = ArchiveLimits::default()
            .with_max_entry_ext_bytes(8)
            .with_max_total_entry_ext_bytes(10);
        let one_tlv = tlv_bytes(0x0001, &[0xAA, 0xBB]);
        let mut bytes = Vec::new();
        bytes.extend(raw_entry_bytes(
            KIND_FILE, 0, 0o644, 1, 8, 5, b"a", &one_tlv,
        ));
        bytes.extend(raw_entry_bytes(
            KIND_FILE, 0, 0o644, 1, 8, 5, b"b", &one_tlv,
        ));
        let header = FcaHeader {
            entry_count: 2,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: 10,
        };
        let err = parse_manifest_bytes(&bytes, header, limits).unwrap_err();
        assert!(format!("{err}").contains("total entry-extension bytes cap exceeded"));
    }

    /// A malformed TLV inside `entry_ext` rejects via the shared
    /// scanner. v1 defines no per-entry tags, so any structurally
    /// invalid TLV trips `MalformedTlv` from `scan_tlv_region`. Five
    /// bytes — one short of the six-byte `tag(u16) || len(u32)`
    /// minimum, so the scanner exits its bounds check before parsing
    /// the tag.
    #[test]
    fn parse_rejects_malformed_entry_ext_tlv() {
        let mut truncated = tlv_bytes(0x0001, &[]);
        truncated.pop();
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 5, 10, b"file", &truncated);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err:?}").contains("MalformedTlv"));
    }

    /// An unknown critical tag (`0x8001..=0xFFFF`) inside `entry_ext`
    /// rejects with `UnknownCriticalTag`. v1 defines no critical tags,
    /// so any critical-range tag is an upgrade-required signal.
    #[test]
    fn parse_rejects_unknown_critical_entry_ext_tag() {
        let critical = tlv_bytes(0x8001, &[]);
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 6, 10, b"file", &critical);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err:?}").contains("UnknownCriticalTag"));
    }

    /// Reserved tag `0x0000` inside `entry_ext` rejects via
    /// `MalformedTlv`. Symmetric coverage with the FCR header TLV
    /// reserved-tag rejection in `crypto::tlv::tests`.
    #[test]
    fn parse_rejects_reserved_entry_ext_tag() {
        let reserved = tlv_bytes(0x0000, &[]);
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 6, 10, b"file", &reserved);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err:?}").contains("MalformedTlv"));
    }

    /// A non-empty ignorable TLV in `entry_ext` round-trips through
    /// the parser unchanged: the scanner accepts it, the entry's
    /// `entry_ext` field is the same bytes after parsing.
    #[test]
    fn parse_accepts_ignorable_entry_ext_tlv() {
        let ignorable = tlv_bytes(0x0001, b"meta");
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 10, 0, b"file", &ignorable);
        let parsed = parse_with_header(&bytes, 1, 0).unwrap();
        assert_eq!(parsed.entries[0].entry_ext, ignorable);
    }

    #[test]
    fn header_size_is_exactly_27_bytes() {
        let mut buf = Vec::new();
        let _ = write_fca_header(&mut buf, 1, 0, 1, 0).expect("valid");
        assert_eq!(buf.len(), 27);
        assert_eq!(buf.len(), FCA_HEADER_SIZE);
    }

    #[test]
    fn rejects_bad_magic() {
        let mut bytes = raw_header_bytes(FCA_VERSION, 0, 5, 0, 100, 1024);
        bytes[0] = b'X';
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("Bad FerroCrypt archive magic"));
    }

    #[test]
    fn rejects_unsupported_version() {
        let bytes = raw_header_bytes(0xFF, 0, 5, 0, 100, 1024);
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        match err {
            CryptoError::InvalidFormat(FormatDefect::UnsupportedArchiveVersion {
                version: 0xFF,
            }) => {}
            other => panic!("expected UnsupportedArchiveVersion(0xFF), got {other:?}"),
        }
    }

    /// `FORMAT.md` §11 reserves `0x00` across every version domain.
    /// Pin the FCA path's rejection symmetric with the existing
    /// `0xFF` test so a future writer bug that emits `0x00` cannot
    /// regress to "OlderArchive 0" framing.
    #[test]
    fn rejects_reserved_zero_version() {
        let bytes = raw_header_bytes(0x00, 0, 5, 0, 100, 1024);
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        match err {
            CryptoError::InvalidFormat(FormatDefect::UnsupportedArchiveVersion { version: 0 }) => {}
            other => panic!("expected UnsupportedArchiveVersion(0), got {other:?}"),
        }
    }

    #[test]
    fn rejects_nonzero_flags() {
        let bytes = raw_header_bytes(FCA_VERSION, 1, 5, 0, 100, 1024);
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("non-zero flags"));
    }

    #[test]
    fn rejects_zero_entry_count() {
        let bytes = raw_header_bytes(FCA_VERSION, 0, 0, 0, 100, 1024);
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("Empty archive"));
    }

    #[test]
    fn rejects_oversize_entry_count() {
        let limits = ArchiveLimits::default();
        let bytes = raw_header_bytes(FCA_VERSION, 0, limits.max_entry_count + 1, 0, 100, 1024);
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, limits).unwrap_err();
        assert!(format!("{err}").contains("entry-count cap exceeded"));
    }

    #[test]
    fn rejects_zero_manifest_len() {
        let bytes = raw_header_bytes(FCA_VERSION, 0, 5, 0, 0, 1024);
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("Malformed archive manifest"));
    }

    #[test]
    fn rejects_oversize_manifest_len() {
        let limits = ArchiveLimits::default();
        let bytes = raw_header_bytes(FCA_VERSION, 0, 5, 0, limits.max_manifest_bytes + 1, 1024);
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, limits).unwrap_err();
        assert!(format!("{err}").contains("manifest length cap exceeded"));
    }

    #[test]
    fn rejects_oversize_total_file_bytes() {
        let limits = ArchiveLimits::default();
        let bytes = raw_header_bytes(
            FCA_VERSION,
            0,
            5,
            0,
            100,
            limits.max_total_plaintext_bytes + 1,
        );
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, limits).unwrap_err();
        assert!(format!("{err}").contains("total-bytes cap exceeded"));
    }

    /// Boundary check: `max_entry_count` exactly is admissible,
    /// `max_entry_count + 1` rejected. Same `>` vs `>=` regression
    /// guard as the limits-side enforce-helper test.
    #[test]
    fn entry_count_at_cap_admissible() {
        let limits = ArchiveLimits::default().with_max_entry_count(10);
        let bytes = raw_header_bytes(FCA_VERSION, 0, 10, 0, 100, 1024);
        let mut cur = Cursor::new(&bytes);
        let parsed = parse_fca_header(&mut cur, limits).expect("at-cap is admissible");
        assert_eq!(parsed.entry_count, 10);
    }

    /// Truncated mid-header (valid magic + version + flags, then the
    /// reader runs out of bytes before entry_count) surfaces as
    /// `CryptoError::Io` (typically `UnexpectedEof`). Confirms the
    /// parser fails closed on short input rather than reading past
    /// end-of-stream.
    #[test]
    fn rejects_short_header() {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(FCA_MAGIC);
        bytes.push(FCA_VERSION);
        bytes.extend_from_slice(&FCA_FLAGS_V1.to_be_bytes());
        // 7 bytes: header is cut off right before entry_count.
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        assert!(
            matches!(err, CryptoError::Io(_)),
            "short header should surface as CryptoError::Io"
        );
    }

    /// Loop pin: a truncated header at every byte boundary 0..27
    /// rejects. Pre-magic truncation (0..4 bytes) surfaces as
    /// `CryptoError::Io` from the magic read; magic-then-truncated
    /// (4 bytes — version absent) hits the next read; etc. The full
    /// 27-byte valid header is the only admissible case at this size.
    /// Pinned because the existing single-slice test (`rejects_short_header`)
    /// only exercises one byte boundary.
    #[test]
    fn rejects_truncated_header_at_every_byte() {
        let full = raw_header_bytes(FCA_VERSION, 0, 5, 0, 100, 1024);
        assert_eq!(full.len(), FCA_HEADER_SIZE);

        for cut in 0..FCA_HEADER_SIZE {
            let truncated = &full[..cut];
            let mut cur = Cursor::new(truncated);
            let result = parse_fca_header(&mut cur, ArchiveLimits::default());
            assert!(
                result.is_err(),
                "truncated to {cut} bytes must reject (got {:?})",
                result.map(|h| h.entry_count),
            );
        }

        // Sanity: the full 27-byte header parses successfully.
        let mut cur = Cursor::new(&full);
        let parsed = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap();
        assert_eq!(parsed.entry_count, 5);
    }

    /// Manifest-len declared SHORTER than the real manifest bytes:
    /// the parser reads exactly `manifest_len` bytes, then either
    /// the truncated bytes parse (with leftover bytes flowing into
    /// the content region) or fail structurally. Either way the
    /// rejection is well-defined and the typed `manifest_len` mismatch
    /// path is exercised via `parse_manifest_bytes`.
    #[test]
    fn parse_rejects_manifest_len_shorter_than_real_bytes() {
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 0, 10, b"file", &[]);
        // Declare manifest_len = real - 5 (slice off the last 5 bytes).
        let header = FcaHeader {
            entry_count: 1,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32 - 5,
            total_file_bytes: 10,
        };
        let err = parse_manifest_bytes(&bytes, header, ArchiveLimits::default()).unwrap_err();
        // The byte slice is longer than declared `manifest_len` →
        // `parse_manifest_bytes` rejects via the length-mismatch arm.
        assert!(format!("{err}").contains("Malformed archive manifest"));
    }

    /// Completely empty input fails at the first `read_exact` for
    /// magic, surfaces as `CryptoError::Io`. Pinned because an empty
    /// stream is a common adversarial probe.
    #[test]
    fn rejects_empty_input() {
        let bytes = Vec::new();
        let mut cur = Cursor::new(&bytes);
        let err = parse_fca_header(&mut cur, ArchiveLimits::default()).unwrap_err();
        assert!(matches!(err, CryptoError::Io(_)));
    }

    /// `write_fca_header` MUST refuse `entry_count == 0` before any
    /// byte is emitted. Pin both the rejection and the "no bytes
    /// written" property — a partial header on disk would be worse
    /// than a clean error.
    #[test]
    fn write_rejects_zero_entry_count_before_emitting_bytes() {
        let mut buf = Vec::new();
        let err = write_fca_header(&mut buf, 0, 0, 100, 1024).unwrap_err();
        assert!(format!("{err}").contains("Empty archive"));
        assert!(
            buf.is_empty(),
            "writer must not emit bytes for invalid params"
        );
    }

    /// `manifest_len == 0` is invalid for every non-empty archive and
    /// the writer must refuse it before emitting a partial header.
    #[test]
    fn write_rejects_zero_manifest_len_before_emitting_bytes() {
        let mut buf = Vec::new();
        let err = write_fca_header(&mut buf, 1, 0, 0, 0).unwrap_err();
        assert!(format!("{err}").contains("Malformed archive manifest"));
        assert!(
            buf.is_empty(),
            "writer must not emit bytes for invalid params"
        );
    }

    /// Magic byte order is `F C A \0`. Pinned because a future
    /// refactor that flipped endianness or got the constant from a
    /// macro would need an explicit byte-by-byte check to fail.
    #[test]
    fn magic_byte_order_is_fca_nul() {
        assert_eq!(FCA_MAGIC, b"FCA\0");
        assert_eq!(FCA_MAGIC[0], b'F');
        assert_eq!(FCA_MAGIC[1], b'C');
        assert_eq!(FCA_MAGIC[2], b'A');
        assert_eq!(FCA_MAGIC[3], 0);
    }

    // -- Manifest serialize / parse ----------------------------------------

    use super::super::model::make_entry;
    use crate::crypto::tlv::tlv_bytes;

    fn make_single_file_manifest() -> Manifest {
        Manifest {
            entries: vec![make_entry("hello.txt", ArchiveEntryKind::File, 13, 0o644)],
            total_file_bytes: 13,
            root_name: OsString::from("hello.txt"),
            root_is_file: true,
            root_mode: 0o644,
        }
    }

    fn make_directory_manifest() -> Manifest {
        Manifest {
            entries: vec![
                make_entry("photos", ArchiveEntryKind::Directory, 0, 0o755),
                make_entry("photos/cover.jpg", ArchiveEntryKind::File, 1024, 0o644),
                make_entry("photos/index.txt", ArchiveEntryKind::File, 50, 0o644),
            ],
            total_file_bytes: 1074,
            root_name: OsString::from("photos"),
            root_is_file: false,
            root_mode: 0o755,
        }
    }

    /// Round-trip through serialize → parse for a single-file
    /// manifest. Pin every Manifest field across the round-trip.
    #[test]
    fn manifest_round_trip_single_file() {
        let manifest = make_single_file_manifest();
        let bytes = serialize_manifest(&manifest, ArchiveLimits::default()).unwrap();

        let header = FcaHeader {
            entry_count: 1,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: manifest.total_file_bytes,
        };
        let parsed = parse_manifest_bytes(&bytes, header, ArchiveLimits::default()).unwrap();

        assert_eq!(parsed.entries.len(), 1);
        assert_eq!(parsed.entries[0].path_utf8, "hello.txt");
        assert_eq!(parsed.entries[0].mode, 0o644);
        assert_eq!(parsed.entries[0].size, 13);
        assert_eq!(parsed.entries[0].kind, ArchiveEntryKind::File);
        assert_eq!(parsed.total_file_bytes, 13);
        assert_eq!(parsed.root_name, OsString::from("hello.txt"));
        assert!(parsed.root_is_file);
    }

    #[test]
    fn manifest_round_trip_directory_tree() {
        let manifest = make_directory_manifest();
        let bytes = serialize_manifest(&manifest, ArchiveLimits::default()).unwrap();

        let header = FcaHeader {
            entry_count: 3,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: manifest.total_file_bytes,
        };
        let parsed = parse_manifest_bytes(&bytes, header, ArchiveLimits::default()).unwrap();

        assert_eq!(parsed.entries.len(), 3);
        assert_eq!(parsed.total_file_bytes, 1074);
        assert_eq!(parsed.root_name, OsString::from("photos"));
        assert!(!parsed.root_is_file);
    }

    #[test]
    fn manifest_round_trip_empty_file() {
        let manifest = Manifest {
            entries: vec![make_entry("empty.txt", ArchiveEntryKind::File, 0, 0o644)],
            total_file_bytes: 0,
            root_name: OsString::from("empty.txt"),
            root_is_file: true,
            root_mode: 0o644,
        };
        let bytes = serialize_manifest(&manifest, ArchiveLimits::default()).unwrap();

        let header = FcaHeader {
            entry_count: 1,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: 0,
        };
        let parsed = parse_manifest_bytes(&bytes, header, ArchiveLimits::default()).unwrap();
        assert_eq!(parsed.entries[0].size, 0);
    }

    /// Determinism: same Manifest must serialize to byte-identical
    /// output. Pinned so a future refactor that introduced HashMap
    /// ordering or thread-local state would fail.
    #[test]
    fn serialize_is_deterministic() {
        let m = make_directory_manifest();
        let a = serialize_manifest(&m, ArchiveLimits::default()).unwrap();
        let b = serialize_manifest(&m, ArchiveLimits::default()).unwrap();
        assert_eq!(a, b);
    }

    /// FORMAT.md §9.4: manifest entry `size` is `u64`. Confirm there is
    /// no remnant ustar 8 GiB cap by encoding `u64::MAX` and inspecting
    /// the raw bytes.
    #[test]
    fn serialize_supports_size_u64_max() {
        let m = Manifest {
            entries: vec![make_entry(
                "huge.bin",
                ArchiveEntryKind::File,
                u64::MAX,
                0o644,
            )],
            total_file_bytes: u64::MAX,
            root_name: OsString::from("huge.bin"),
            root_is_file: true,
            root_mode: 0o644,
        };
        // Defaults' max_total_plaintext_bytes is 64 GiB; bump it for
        // this synthetic test only.
        let limits = ArchiveLimits::default().with_max_total_plaintext_bytes(u64::MAX);
        let bytes = serialize_manifest(&m, limits).unwrap();

        // Entry layout: kind(1) flags(1) mode(2) path_len(2)
        // entry_ext_len(4) size(8) → size starts at offset 10.
        let size_field = &bytes[10..18];
        assert_eq!(size_field, &u64::MAX.to_be_bytes());
    }

    // -- checked_manifest_len ----------------------------------------------

    #[test]
    fn checked_manifest_len_empty_is_zero() {
        let len = checked_manifest_len(&[], ArchiveLimits::default()).unwrap();
        assert_eq!(len, 0);
    }

    #[test]
    fn checked_manifest_len_one_entry() {
        let entries = [make_entry("hi", ArchiveEntryKind::File, 5, 0o644)];
        let len = checked_manifest_len(&entries, ArchiveLimits::default()).unwrap();
        // 18 fixed + 2 path bytes
        assert_eq!(len, FCA_ENTRY_FIXED_SIZE + 2);
    }

    #[test]
    fn checked_manifest_len_rejects_oversize_mode() {
        let entries = [make_entry("file", ArchiveEntryKind::File, 0, 0o7777)];
        let err = checked_manifest_len(&entries, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("mode contains unsupported bits"));
    }

    #[test]
    fn checked_manifest_len_rejects_path_above_cap() {
        let l = ArchiveLimits::default().with_max_path_bytes(5);
        let entries = [make_entry("toolong.txt", ArchiveEntryKind::File, 0, 0o644)];
        let err = checked_manifest_len(&entries, l).unwrap_err();
        assert!(format!("{err}").contains("byte-length cap exceeded"));
    }

    #[test]
    fn checked_manifest_len_rejects_empty_path() {
        let entries = [make_entry("", ArchiveEntryKind::File, 0, 0o644)];
        let err = checked_manifest_len(&entries, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("Empty archive entry path"));
    }

    /// FORMAT.md §9.10: writer-side TLV canonicality is enforced
    /// before emission. A caller that hand-rolls an `ArchiveEntry`
    /// with an unknown critical tag in `entry_ext` is rejected by
    /// `checked_manifest_len`, mirroring the reader's
    /// `parse_manifest_bytes` rejection path. v1 native writers never
    /// hit this path (they pass `entry_ext: Vec::new()`); the test
    /// pins the symmetry guarantee for v1.x writers that emit known
    /// tags.
    #[test]
    fn checked_manifest_len_rejects_unknown_critical_entry_ext() {
        let mut entry = make_entry("file", ArchiveEntryKind::File, 0, 0o644);
        entry.entry_ext = tlv_bytes(0x8001, &[]);
        let err = checked_manifest_len(&[entry], ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err:?}").contains("UnknownCriticalTag"));
    }

    /// Symmetric coverage: malformed `entry_ext` bytes (truncated TLV
    /// header) are rejected by the writer-side validation before any
    /// allocation, not after. Five bytes — one short of the six-byte
    /// `tag(u16) || len(u32)` minimum.
    #[test]
    fn checked_manifest_len_rejects_malformed_entry_ext() {
        let mut entry = make_entry("file", ArchiveEntryKind::File, 0, 0o644);
        entry.entry_ext = tlv_bytes(0x0001, &[]);
        entry.entry_ext.pop();
        let err = checked_manifest_len(&[entry], ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err:?}").contains("MalformedTlv"));
    }

    /// Writer-side and reader-side serialize / parse round-trip with
    /// a non-empty ignorable per-entry TLV. Pin so a future regression
    /// in either path fails this test rather than mysteriously
    /// dropping bytes.
    #[test]
    fn manifest_round_trip_with_ignorable_entry_ext() {
        let mut entry = make_entry("hello.txt", ArchiveEntryKind::File, 0, 0o644);
        entry.entry_ext = tlv_bytes(0x0001, b"meta");

        let manifest = Manifest {
            entries: vec![entry.clone()],
            total_file_bytes: 0,
            root_name: OsString::from("hello.txt"),
            root_is_file: true,
            root_mode: 0o644,
        };
        let bytes = serialize_manifest(&manifest, ArchiveLimits::default()).unwrap();

        let parsed = parse_with_header(&bytes, 1, 0).unwrap();
        assert_eq!(parsed.entries[0].entry_ext, entry.entry_ext);
    }

    #[test]
    fn checked_manifest_len_rejects_above_manifest_cap() {
        let l = ArchiveLimits::default().with_max_manifest_bytes(20);
        // Two entries, each 18 + 5 = 23 bytes → total 46 > cap 20.
        let entries = [
            make_entry("file1", ArchiveEntryKind::File, 0, 0o644),
            make_entry("file2", ArchiveEntryKind::File, 0, 0o644),
        ];
        let err = checked_manifest_len(&entries, l).unwrap_err();
        assert!(format!("{err}").contains("manifest length cap exceeded"));
    }

    // -- Manifest parser rejection cases -----------------------------------

    /// Helper that constructs raw manifest bytes for one synthetic
    /// entry, allowing each field to be set independently to test
    /// rejection cases. v1 callers pass `entry_ext = &[]`; tests that
    /// drive the per-entry TLV path supply non-empty values.
    #[allow(clippy::too_many_arguments)]
    fn raw_entry_bytes(
        kind: u8,
        flags: u8,
        mode: u16,
        path_len: u16,
        entry_ext_len: u32,
        size: u64,
        path: &[u8],
        entry_ext: &[u8],
    ) -> Vec<u8> {
        let mut buf = Vec::with_capacity(FCA_ENTRY_FIXED_SIZE + path.len() + entry_ext.len());
        buf.push(kind);
        buf.push(flags);
        buf.extend_from_slice(&mode.to_be_bytes());
        buf.extend_from_slice(&path_len.to_be_bytes());
        buf.extend_from_slice(&entry_ext_len.to_be_bytes());
        buf.extend_from_slice(&size.to_be_bytes());
        buf.extend_from_slice(path);
        buf.extend_from_slice(entry_ext);
        buf
    }

    fn parse_with_header(
        bytes: &[u8],
        entry_count: u32,
        total_file_bytes: u64,
    ) -> Result<Manifest, CryptoError> {
        let header = FcaHeader {
            entry_count,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes,
        };
        parse_manifest_bytes(bytes, header, ArchiveLimits::default())
    }

    #[test]
    fn parse_rejects_unknown_entry_kind() {
        let bytes = raw_entry_bytes(0xFF, 0, 0o644, 4, 0, 10, b"fake", &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("Unsupported archive entry kind"));
    }

    /// `0x00` is reserved across every kind/version byte in the spec
    /// (`FORMAT.md` §11). Symmetric with `parse_rejects_unknown_entry_kind`'s
    /// `0xFF` case so neither sentinel can regress to a silent
    /// accept-as-some-future-kind path.
    #[test]
    fn parse_rejects_reserved_zero_entry_kind() {
        let bytes = raw_entry_bytes(0x00, 0, 0o644, 4, 0, 10, b"fake", &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("Unsupported archive entry kind"));
    }

    #[test]
    fn parse_rejects_nonzero_entry_flags() {
        let bytes = raw_entry_bytes(KIND_FILE, 0x01, 0o644, 4, 0, 10, b"file", &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("non-zero reserved flags"));
    }

    #[test]
    fn parse_rejects_invalid_mode() {
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o7777, 4, 0, 10, b"file", &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("mode contains unsupported bits"));
    }

    #[test]
    fn parse_rejects_directory_with_nonzero_size() {
        let bytes = raw_entry_bytes(KIND_DIR, 0, 0o755, 3, 0, 100, b"dir", &[]);
        let err = parse_with_header(&bytes, 1, 0).unwrap_err();
        assert!(format!("{err}").contains("Directory archive entry has non-zero size"));
    }

    #[test]
    fn parse_rejects_zero_path_len() {
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 0, 0, 10, b"", &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("Empty archive entry path"));
    }

    #[test]
    fn parse_rejects_invalid_utf8() {
        // 0xFF is not a valid UTF-8 start byte.
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 1, 0, 10, &[0xFF], &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("not valid UTF-8"));
    }

    #[test]
    fn parse_rejects_truncated_manifest() {
        // Header claims 1 entry but bytes are short.
        let bytes = vec![KIND_FILE, 0, 0, 0]; // only 4 bytes — fixed header is 18
        let err = parse_with_header(&bytes, 1, 0).unwrap_err();
        assert!(format!("{err}").contains("Malformed archive manifest"));
    }

    #[test]
    fn parse_rejects_path_running_past_manifest() {
        // Fixed entry header says path_len = 100 but only 4 path bytes
        // are present.
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 100, 0, 10, b"path", &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("Malformed archive manifest"));
    }

    #[test]
    fn parse_rejects_trailing_bytes_after_last_entry() {
        // One full entry + one extra byte after it.
        let mut bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 0, 10, b"file", &[]);
        bytes.push(0xAA);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        assert!(format!("{err}").contains("Malformed archive manifest"));
    }

    #[test]
    fn parse_rejects_manifest_len_mismatch() {
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 0, 10, b"file", &[]);
        let header = FcaHeader {
            entry_count: 1,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32 + 1,
            total_file_bytes: 10,
        };
        let err = parse_manifest_bytes(&bytes, header, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("Malformed archive manifest"));
    }

    #[test]
    fn parse_rejects_total_bytes_mismatch() {
        // One file entry of size 10; declared total is 99.
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 0, 10, b"file", &[]);
        let err = parse_with_header(&bytes, 1, 99).unwrap_err();
        assert!(format!("{err}").contains("total-bytes mismatch"));
    }

    /// `total_file_bytes` overflow during summation: two files each
    /// claiming `u64::MAX` — second `checked_add` fails.
    #[test]
    fn parse_rejects_total_bytes_overflow_during_sum() {
        let mut bytes = Vec::new();
        bytes.extend(raw_entry_bytes(
            KIND_FILE,
            0,
            0o644,
            1,
            0,
            u64::MAX,
            b"a",
            &[],
        ));
        bytes.extend(raw_entry_bytes(KIND_FILE, 0, 0o644, 1, 0, 1, b"b", &[]));
        // Header `total_file_bytes` doesn't matter — the overflow
        // fires first.
        let header = FcaHeader {
            entry_count: 2,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: 0,
        };
        let err = parse_manifest_bytes(&bytes, header, ArchiveLimits::default()).unwrap_err();
        assert!(format!("{err}").contains("total file bytes overflow"));
    }

    /// Single file entry with `size = u64::MAX` round-trips. Pin
    /// "no remnant ustar 8 GiB cap" on the parse side (FORMAT.md §9.4).
    #[test]
    fn parse_accepts_size_u64_max() {
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 4, 0, u64::MAX, b"huge", &[]);
        let limits = ArchiveLimits::default().with_max_total_plaintext_bytes(u64::MAX);
        let header = FcaHeader {
            entry_count: 1,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: u64::MAX,
        };
        let parsed = parse_manifest_bytes(&bytes, header, limits).unwrap();
        assert_eq!(parsed.entries[0].size, u64::MAX);
    }

    /// `validate_fca_path` is invoked from inside `parse_manifest_bytes`
    /// — confirm path-grammar rejection surfaces through the manifest
    /// parser. Use `..` as the grammar trip-wire.
    #[test]
    fn parse_runs_path_grammar() {
        let bytes = raw_entry_bytes(KIND_FILE, 0, 0o644, 2, 0, 10, b"..", &[]);
        let err = parse_with_header(&bytes, 1, 10).unwrap_err();
        // Either a path-grammar error or a tree-shape error. The
        // grammar should fire first.
        let s = format!("{err}");
        assert!(
            s.contains("forbidden component") || s.contains("Unsafe path"),
            "got: {s}",
        );
    }

    /// Tree validation runs as the final step — confirm a structurally
    /// valid manifest with bad tree shape rejects through the parser.
    #[test]
    fn parse_runs_tree_validation() {
        // Two files at top level (different roots).
        let mut bytes = Vec::new();
        bytes.extend(raw_entry_bytes(KIND_FILE, 0, 0o644, 1, 0, 10, b"a", &[]));
        bytes.extend(raw_entry_bytes(KIND_FILE, 0, 0o644, 1, 0, 10, b"b", &[]));
        let err = parse_with_header(&bytes, 2, 20).unwrap_err();
        assert!(format!("{err}").contains("multiple top-level roots"));
    }

    /// Near-cap stress (Batch 2.6): a manifest with `max_entry_count`
    /// directory entries serializes, parses, and round-trips in
    /// reasonable time and memory. Covers §3 "very large manifest"
    /// and §15 "many tiny entries near 250,000 cap" without paying the
    /// filesystem-op cost of actually creating 250k files.
    ///
    /// Constructs entries directly in memory and drives the serialize
    /// → parse pipeline. Uses zero-byte directory entries so the
    /// total bytes stays at zero (no `total_file_bytes` interaction).
    /// One root + 249,999 descendant dirs at depth 2.
    #[test]
    fn manifest_round_trip_near_entry_count_cap() {
        let limits = ArchiveLimits::default();
        let cap = limits.max_entry_count;

        let mut entries = Vec::with_capacity(cap as usize);
        entries.push(make_entry("root", ArchiveEntryKind::Directory, 0, 0o755));
        for i in 1..cap {
            entries.push(make_entry(
                &format!("root/d{i:07}"),
                ArchiveEntryKind::Directory,
                0,
                0o755,
            ));
        }
        assert_eq!(entries.len(), cap as usize);

        let manifest = Manifest {
            entries,
            total_file_bytes: 0,
            root_name: OsString::from("root"),
            root_is_file: false,
            root_mode: 0o755,
        };

        let bytes = serialize_manifest(&manifest, limits).unwrap();
        // Sanity: the serialized manifest fits in the manifest cap.
        assert!(bytes.len() as u64 <= u64::from(limits.max_manifest_bytes));

        let header = FcaHeader {
            entry_count: cap,
            archive_ext_len: 0,
            manifest_len: bytes.len() as u32,
            total_file_bytes: 0,
        };
        let parsed = parse_manifest_bytes(&bytes, header, limits).unwrap();
        assert_eq!(parsed.entries.len(), cap as usize);
        assert_eq!(parsed.root_name, OsString::from("root"));
    }
}