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
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
use crate::cache::{CacheEntry, ObjectCache};
use crate::error::EncryptedError;
use crate::parser::{parse_indirect_object, parse_indirect_object_detailed, Parser};
use crate::security::password::{normalize_password, PasswordMode};
use crate::security::standard::{
check_owner_password, check_owner_password_r5, check_owner_password_r6,
check_owner_password_v4, check_user_password, check_user_password_r5, check_user_password_r6,
check_user_password_v4, decrypt_cipher_bytes, decrypt_strings_in_object, per_object_key,
ObjectKeyAlg, StandardHandlerInputs, StandardHandlerR5Inputs, StringCipher,
};
use crate::{
load_xref_and_trailer, load_xref_and_trailer_with_repair, Diagnostic, Diagnostics, Dictionary,
Error, Object, ObjectRef, Result, XrefForm, XrefOffset,
};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{Cursor, Read, Seek, SeekFrom};
/// Lazily parsed PDF document handle.
///
/// `Pdf` is the core type of the crate. Opening a document only reads the cross-reference
/// table and the trailer; individual objects are parsed on first access via
/// [`Pdf::resolve`]. The same handle is what every higher-level helper
/// ([`crate::pages`], [`crate::outline`], [`crate::fonts`], [`crate::write_pdf`])
/// consumes.
///
/// # Examples
///
/// ```no_run
/// use std::fs::File;
/// use std::io::BufReader;
/// use flpdf::{ObjectRef, Pdf};
///
/// let mut pdf = Pdf::open(BufReader::new(File::open("input.pdf")?))?;
/// println!("version {}", pdf.version());
/// let catalog = pdf.resolve(pdf.root_ref().expect("root"))?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Pdf<R: Read + Seek> {
reader: R,
version: String,
/// `true` when the file header carries the `%QDF-1.0` marker (qpdf/flpdf
/// QDF form). Used by [`resolve`](Self::resolve) to disambiguate the
/// exact-window indirect `/Length` case: QDF holders count the framing
/// EOL (keep the parser's endstream-scan to preserve round-trip), whereas
/// non-QDF holders are the spec content length (flpdf-9hc.28).
is_qdf: bool,
trailer: Dictionary,
startxref: u64,
last_xref_form: XrefForm,
repair_diagnostics: Diagnostics,
cache: ObjectCache,
compressed_member_parents: BTreeMap<ObjectRef, (ObjectRef, u32)>,
source_xref_offsets: Vec<(ObjectRef, u64)>,
source_xref_entries: BTreeMap<ObjectRef, XrefOffset>,
encryption: Option<EncryptionState>,
}
#[derive(Debug, Clone)]
struct EncryptionState {
file_key: Vec<u8>,
stream_mode: EncryptionMode,
string_mode: EncryptionMode,
crypt_filters: BTreeMap<Vec<u8>, EncryptionMode>,
encrypt_metadata: bool,
encrypt_ref: Option<ObjectRef>,
weak_crypto: bool,
permissions: Permissions,
/// Whether the supplied password authenticated as the user password.
user_password_matched: bool,
/// Whether the supplied password authenticated as the owner password.
/// Many real PDFs share an empty password for both, so both flags can
/// be true simultaneously.
owner_password_matched: bool,
}
/// Standard security handler permission bits from an encrypted document's `/P` entry.
///
/// These flags are advisory. They report the producer's requested restrictions but do
/// not enforce them while reading or rewriting the document.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Permissions {
raw: i32,
}
impl Permissions {
fn new(raw: i32) -> Self {
Self { raw }
}
/// Raw signed `/P` value.
pub fn raw(self) -> i32 {
self.raw
}
/// Print the document, possibly at degraded quality if high-quality printing is denied.
pub fn can_print(self) -> bool {
self.has_bit(0x0004)
}
/// Modify document contents by operations other than controlled form/annotation edits.
pub fn can_modify(self) -> bool {
self.has_bit(0x0008)
}
/// Copy or otherwise extract text and graphics.
pub fn can_copy(self) -> bool {
self.has_bit(0x0010)
}
/// Add or modify annotations and interactive form fields.
pub fn can_annotate(self) -> bool {
self.has_bit(0x0020)
}
/// Fill in existing interactive form fields.
pub fn can_fill_forms(self) -> bool {
self.has_bit(0x0100)
}
/// Extract text and graphics for accessibility purposes.
pub fn can_extract_for_accessibility(self) -> bool {
self.has_bit(0x0200)
}
/// Assemble the document by inserting, rotating, or deleting pages/bookmarks.
pub fn can_assemble(self) -> bool {
self.has_bit(0x0400)
}
/// Print the document at high quality.
pub fn can_print_high_quality(self) -> bool {
self.has_bit(0x0800)
}
fn has_bit(self, bit: u32) -> bool {
(self.raw as u32) & bit != 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EncryptionMode {
Rc4,
Aes128,
Identity,
Aes256,
}
impl EncryptionMode {
/// qpdf's `show_encryption_method()` spelling for this method.
///
/// Source: qpdf `libqpdf/QPDFJob.cc` `show_encryption_method()` —
/// `e_rc4`→"RC4", `e_aes`→"AESv2", `e_aesv3`→"AESv3", `e_none`→"none".
/// flpdf's `Identity` (no-op crypt filter) maps to qpdf's `e_none`/"none".
fn qpdf_name(self) -> &'static str {
match self {
EncryptionMode::Rc4 => "RC4",
EncryptionMode::Aes128 => "AESv2",
EncryptionMode::Aes256 => "AESv3",
EncryptionMode::Identity => "none",
}
}
}
/// Read-only snapshot of an encrypted document's `/Encrypt` parameters,
/// surfaced for the `show-encryption` inspection subcommand
/// (flpdf-9hc.3.17). Built by re-reading the `/Encrypt` dictionary plus the
/// already-authenticated [`EncryptionState`]; does not run or alter
/// authentication.
#[derive(Debug, Clone)]
pub struct EncryptionInfo {
/// `/V` encryption algorithm version.
pub v: i64,
/// `/R` standard security handler revision.
pub r: i64,
/// Key length in bits (`/Length`, defaulting to 40 when absent for V<5;
/// 256 for V=5).
pub length_bits: i64,
/// `/Filter` security handler name (e.g. `Standard`).
pub filter: String,
/// Raw signed `/P` permission bits.
pub permissions: Permissions,
/// `/EncryptMetadata` flag (defaults to true when absent).
pub encrypt_metadata: bool,
/// qpdf-style method name for the stream crypt filter (`StmF`).
pub stream_method: &'static str,
/// qpdf-style method name for the string crypt filter (`StrF`).
pub string_method: &'static str,
/// qpdf-style method name for the embedded-file crypt filter (`EFF`),
/// when the document declares one.
pub eff_method: Option<&'static str>,
/// Named crypt filters from `/CF` mapped to their qpdf-style method
/// names, e.g. `StdCF` → `AESv2`.
pub named_crypt_filters: Vec<(String, &'static str)>,
}
/// Options for opening a PDF document.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PdfOpenOptions {
/// Enable xref/trailer repair when strict parsing fails.
pub repair: bool,
/// Password bytes supplied to the Standard security handler.
pub password: Vec<u8>,
/// How `password` should be interpreted before key derivation. See
/// [`PasswordMode`] for the qpdf-compatible semantics.
pub password_mode: PasswordMode,
/// Permit deprecated RC4-backed handlers and revision 5 AES-256.
pub allow_weak_crypto: bool,
/// Interpret [`password`](Self::password) as the precomputed file
/// encryption key in hex, NOT a user/owner password (qpdf
/// `--password-is-hex-key`). When set, all password→key derivation
/// (Algorithm 2 / 2.A / 2.B / 6 / 7) is skipped and `hex_decode(password)`
/// is used directly as the file key for stream/string decryption.
pub password_is_hex_key: bool,
}
impl<R: Read + Seek> Pdf<R> {
/// Open a document strictly: parse the cross-reference and trailer, but do not run
/// the recovery heuristics. Returns an [`Error`] if the document is malformed.
pub fn open(reader: R) -> Result<Self> {
Self::open_with_options(reader, PdfOpenOptions::default())
}
/// Open a document, falling back to qpdf-style xref/trailer recovery when the
/// strict parse fails. Diagnostics from the recovery pass are stored on the handle
/// and exposed via [`Pdf::repair_diagnostics`].
pub fn open_with_repair(reader: R) -> Result<Self> {
Self::open_with_options(
reader,
PdfOpenOptions {
repair: true,
..PdfOpenOptions::default()
},
)
}
/// Alias for [`Pdf::open_with_repair`].
pub fn open_best_effort(reader: R) -> Result<Self> {
Self::open_with_repair(reader)
}
/// Open a document with explicit repair and password options.
pub fn open_with_options(reader: R, options: PdfOpenOptions) -> Result<Self> {
Self::open_with_repair_mode(reader, options)
}
/// Diagnostics emitted while opening the document — typically warnings from the
/// xref/trailer recovery path. Always non-empty when the parse hit a soft failure.
pub fn repair_diagnostics(&self) -> &Diagnostics {
&self.repair_diagnostics
}
/// Whether this document authenticated an `/Encrypt` dictionary while opening.
pub fn is_encrypted(&self) -> bool {
self.encryption.is_some()
}
pub(crate) fn encryption_ref(&self) -> Option<ObjectRef> {
self.encryption
.as_ref()
.and_then(|encryption| encryption.encrypt_ref)
}
/// Whether opening this document required the weak-crypto opt-in.
pub fn uses_weak_crypto(&self) -> bool {
self.encryption
.as_ref()
.is_some_and(|encryption| encryption.weak_crypto)
}
/// Advisory standard security handler permissions from `/P`, if the document is encrypted.
pub fn permissions(&self) -> Option<Permissions> {
self.encryption
.as_ref()
.map(|encryption| encryption.permissions)
}
/// Whether the password supplied at open time authenticated against the
/// document's user password (`/U`). Always `false` for plaintext PDFs.
pub fn user_password_matched(&self) -> bool {
self.encryption
.as_ref()
.is_some_and(|encryption| encryption.user_password_matched)
}
/// Whether the password supplied at open time authenticated against the
/// document's owner password (`/O`). Always `false` for plaintext PDFs.
/// Many PDFs use an empty password for both, so this can be true at the
/// same time as [`Pdf::user_password_matched`].
pub fn owner_password_matched(&self) -> bool {
self.encryption
.as_ref()
.is_some_and(|encryption| encryption.owner_password_matched)
}
/// The derived file encryption key, if the document was opened as an
/// encrypted file. `None` for plaintext PDFs.
///
/// Read-only accessor for the `show-encryption-key` inspection
/// subcommand (flpdf-9hc.3.17); does not run or alter authentication.
pub fn encryption_file_key(&self) -> Option<&[u8]> {
self.encryption
.as_ref()
.map(|encryption| encryption.file_key.as_slice())
}
/// Read-only snapshot of the `/Encrypt` parameters for the
/// `show-encryption` inspection subcommand (flpdf-9hc.3.17).
///
/// Returns `None` for plaintext PDFs. Re-reads the `/Encrypt` dictionary
/// (for V/R/Length/Filter/CF, which the authenticated [`EncryptionState`]
/// does not retain verbatim) and combines it with the already-resolved
/// crypt-filter methods. This does NOT re-run or alter authentication
/// (layer-2 owns that ordering); it only reflects state from a document
/// already opened successfully.
pub fn encryption_info(&mut self) -> Result<Option<EncryptionInfo>> {
if self.encryption.is_none() {
return Ok(None);
}
let Some(encrypt) = self.encrypt_dictionary()? else {
return Ok(None);
};
let v = required_integer(&encrypt, "V")?;
let r = required_integer(&encrypt, "R")?;
let filter = required_name(&encrypt, "Filter")?.to_string();
// /Length is in bits and absent for V<5 (defaulting to 40 per the
// Standard handler); V=5 always uses a 256-bit key.
let length_bits = match encrypt.get("Length") {
Some(Object::Integer(value)) => *value,
_ if v >= 5 => 256,
_ => 40,
};
// EFF: optional crypt-filter selector for embedded files. Resolve it
// through the same named-CF map used for StmF/StrF.
let eff_selector = crypt_filter_selector(&encrypt, "EFF")?;
let encryption = self
.encryption
.as_ref()
.expect("checked is_some above; authenticate_if_encrypted set it");
let permissions = encryption.permissions;
let encrypt_metadata = encryption.encrypt_metadata;
let stream_method = encryption.stream_mode.qpdf_name();
let string_method = encryption.string_mode.qpdf_name();
let eff_method = eff_selector.and_then(|selector| {
if selector == "Identity" {
Some(EncryptionMode::Identity.qpdf_name())
} else {
encryption
.crypt_filters
.get(selector.as_bytes())
.map(|mode| mode.qpdf_name())
}
});
let named_crypt_filters = encryption
.crypt_filters
.iter()
.map(|(name, mode)| (String::from_utf8_lossy(name).into_owned(), mode.qpdf_name()))
.collect();
Ok(Some(EncryptionInfo {
v,
r,
length_bits,
filter,
permissions,
encrypt_metadata,
stream_method,
string_method,
eff_method,
named_crypt_filters,
}))
}
fn open_with_repair_mode(mut reader: R, options: PdfOpenOptions) -> Result<Self> {
let loaded = if options.repair {
load_xref_and_trailer_with_repair(&mut reader, options.repair)?
} else {
load_xref_and_trailer(&mut reader)?
};
let source_xref_entries = loaded.entries.clone();
let source_xref_offsets = loaded
.entries
.iter()
.filter_map(|(object_ref, offset)| match offset {
crate::XrefOffset::Free { .. } => None,
crate::XrefOffset::Offset(offset) => Some((*object_ref, *offset)),
crate::XrefOffset::Compressed { .. } => None,
})
.collect();
let cache = ObjectCache::from_offsets(&loaded.entries);
// Sniff the file header for the `%QDF-1.0` marker (flpdf-9hc.28). It
// sits within the first few lines (`%PDF-x.y`, binary marker,
// `%QDF-1.0`); 64 bytes is ample. Best-effort: any read error → false.
let is_qdf = {
// `Read::read` may return a short read (BufReader, pipes, …), so
// a single call could split `%QDF-1.0` across reads and miss it.
// `take(64).read_to_end` reads until 64 bytes or EOF regardless of
// chunking. Best-effort: any error → false.
let mut header = Vec::with_capacity(64);
if reader.seek(SeekFrom::Start(0)).is_ok() {
let _ = reader.by_ref().take(64).read_to_end(&mut header);
}
header.windows(b"%QDF-1.0".len()).any(|w| w == b"%QDF-1.0")
};
let mut pdf = Self {
reader,
version: loaded.version,
is_qdf,
trailer: loaded.trailer,
startxref: loaded.startxref,
last_xref_form: loaded.last_xref_form,
repair_diagnostics: loaded.repair_diagnostics,
cache,
compressed_member_parents: BTreeMap::new(),
source_xref_offsets,
source_xref_entries,
encryption: None,
};
pdf.authenticate_if_encrypted(&options)?;
Ok(pdf)
}
fn authenticate_if_encrypted(&mut self, options: &PdfOpenOptions) -> Result<()> {
let encrypt_ref = self.trailer().get_ref("Encrypt");
let Some(encrypt) = self.encrypt_dictionary()? else {
return Ok(());
};
let revision = required_revision(&encrypt)?;
let permissions = Permissions::new(required_permissions(&encrypt)?);
let crypt_filters = crypt_filter_modes(&encrypt, revision)?;
// Under `--password-is-hex-key` the --password value is a raw hex key,
// not a password, so password-encoding normalization (which can reject
// e.g. `--password-mode=unicode` on V<5) must not run and is unused by
// the hex-key branch. Skip it; the hex-key branch decodes the raw
// value itself. The `else` (layer-2) branches still see the normalized
// password unchanged.
let password = if options.password_is_hex_key {
Vec::new()
} else {
normalize_password(&options.password, options.password_mode, revision)?
};
let (
file_key,
stream_mode,
string_mode,
encrypt_metadata,
weak_crypto,
user_password_matched,
owner_password_matched,
) = if options.password_is_hex_key {
// qpdf `--password-is-hex-key`: the value passed via --password is
// the precomputed file encryption key as hex, NOT a user/owner
// password. We skip ALL password→key derivation (Algorithm 2 /
// 2.A / 2.B / 6 / 7) and the layer-2 user/owner attempt +
// bad-password ordering block entirely. This is a SEPARATE
// sibling branch: the `else` below preserves layer-2's reordered
// password/weak-crypto logic verbatim (flpdf-9hc.3.21).
//
// revision / crypt_filters / encrypt_ref / permissions are already
// determined above and do NOT depend on the password. Modes and
// /EncryptMetadata are likewise password-independent; compute them
// with the SAME revision-aware split layer-2 uses (the V<5/V=4
// helper returns RC4/RC4 unconditionally for V≠4, so it must not
// be applied to a V=5 document).
let file_key = decode_hex_file_key(&options.password)?;
let (stream_mode, string_mode, encrypt_metadata, weak_crypto) =
if matches!(revision, 5 | 6) {
let (stream_mode, string_mode) = standard_r5_or_r6_modes(&encrypt)?;
let encrypt_metadata = encrypt_metadata_flag(&encrypt)?;
// Same weak-crypto classification as layer-2's R5/R6 branch.
(stream_mode, string_mode, encrypt_metadata, revision == 5)
} else {
let inputs = standard_handler_inputs(&encrypt, self.trailer())?;
let (stream_mode, string_mode) = standard_v4_or_legacy_modes(&encrypt)?;
let weak_crypto = matches!(stream_mode, EncryptionMode::Rc4)
|| matches!(string_mode, EncryptionMode::Rc4)
|| crypt_filters
.values()
.any(|mode| matches!(mode, EncryptionMode::Rc4));
(
stream_mode,
string_mode,
inputs.encrypt_metadata,
weak_crypto,
)
};
// Honor the weak-crypto gate consistently with the password path:
// qpdf still requires --allow-weak-crypto for RC4 / R=5 even when a
// raw key is supplied. Keep the existing post-key gate behavior;
// do NOT special-case the explicit-key path.
if weak_crypto && !options.allow_weak_crypto {
return Err(EncryptedError::WeakCryptoNotAllowed.into());
}
// A raw key bypasses authentication, so neither the user nor the
// owner password was matched. qpdf likewise reports no password
// match for `--password-is-hex-key`; report both as false.
(
file_key,
stream_mode,
string_mode,
encrypt_metadata,
weak_crypto,
false,
false,
)
} else if matches!(revision, 5 | 6) {
// Error-variant firing order (must match qpdf, see flpdf-9hc.3.21):
//
// 1. Password authentication runs FIRST. If neither the user nor
// the owner password authenticates, return `BadPassword`.
// 2. ONLY after a password authenticates do we apply the
// weak-crypto gate (`WeakCryptoNotAllowed`). A correct
// password against a weak (R=5) file with `--allow-weak-crypto`
// absent still returns `WeakCryptoNotAllowed` — only the
// ordering relative to `BadPassword` changes here.
// 3. A wrong-length `/U` or `/O` entry on this authentication
// path is reported as `BadPassword` (an unusable credential
// entry is indistinguishable from a wrong password to a
// caller), not `Malformed`. This is scoped to the auth path
// via `standard_handler_r5_inputs` (its only caller); all
// other `Malformed` reclassification is intentionally NOT done
// (e.g. `/UE`/`/OE` length errors stay `Malformed`).
//
// Keep this ordering identical in the `else` (V<5 / V=4) branch
// below; do not re-introduce the weak-crypto-before-auth bug in
// either branch.
let inputs =
standard_handler_r5_inputs(&encrypt).map_err(map_uo_length_to_bad_password)?;
let (stream_mode, string_mode) = standard_r5_or_r6_modes(&encrypt)?;
let encrypt_metadata = encrypt_metadata_flag(&encrypt)?;
let weak_crypto = revision == 5;
let user_attempt = if revision == 5 {
check_user_password_r5(&password, &inputs)
} else {
check_user_password_r6(&password, &inputs)
};
let owner_attempt = if revision == 5 {
check_owner_password_r5(&password, &inputs)
} else {
check_owner_password_r6(&password, &inputs)
};
let user_password_matched = user_attempt.is_ok();
let owner_password_matched = owner_attempt.is_ok();
let file_key = match (user_attempt, owner_attempt) {
(Ok(key), _) => key,
(Err(_), Ok(key)) => key,
(Err(user_err), Err(_owner_err)) => return Err(user_err),
};
// Authentication succeeded — now apply the weak-crypto gate.
if weak_crypto && !options.allow_weak_crypto {
return Err(EncryptedError::WeakCryptoNotAllowed.into());
}
(
file_key,
stream_mode,
string_mode,
encrypt_metadata,
weak_crypto,
user_password_matched,
owner_password_matched,
)
} else {
let inputs = standard_handler_inputs(&encrypt, self.trailer())?;
let (stream_mode, string_mode) = standard_v4_or_legacy_modes(&encrypt)?;
let encrypt_metadata = inputs.encrypt_metadata;
let weak_crypto = matches!(stream_mode, EncryptionMode::Rc4)
|| matches!(string_mode, EncryptionMode::Rc4)
|| crypt_filters
.values()
.any(|mode| matches!(mode, EncryptionMode::Rc4));
// Same error-variant firing order as the R=5/R=6 branch above:
// password authentication runs FIRST (both failing →
// `BadPassword`); the weak-crypto gate (`WeakCryptoNotAllowed`)
// is applied ONLY after a password authenticates. A correct
// password against an RC4 file without `--allow-weak-crypto`
// still returns `WeakCryptoNotAllowed`; only the ordering relative
// to `BadPassword` changes. Do not move the gate back above the
// auth attempts (flpdf-9hc.3.21).
let v4_path = inputs.v == 4 && inputs.r == 4;
let user_attempt = if v4_path {
check_user_password_v4(&password, &inputs)
} else {
check_user_password(&password, &inputs)
};
let owner_attempt = if v4_path {
check_owner_password_v4(&password, &inputs)
} else {
check_owner_password(&password, &inputs)
};
let user_password_matched = user_attempt.is_ok();
let owner_password_matched = owner_attempt.is_ok();
let file_key = match (user_attempt, owner_attempt) {
(Ok(key), _) => key,
(Err(_), Ok(key)) => key,
(Err(user_err), Err(_owner_err)) => return Err(user_err),
};
// Authentication succeeded — now apply the weak-crypto gate.
if weak_crypto && !options.allow_weak_crypto {
return Err(EncryptedError::WeakCryptoNotAllowed.into());
}
(
file_key,
stream_mode,
string_mode,
encrypt_metadata,
weak_crypto,
user_password_matched,
owner_password_matched,
)
};
let r6_perms_warning = if revision == 6 {
r6_perms_warning(&encrypt, &file_key, permissions, encrypt_metadata)?
} else {
None
};
self.encryption = Some(EncryptionState {
file_key,
stream_mode,
string_mode,
crypt_filters,
encrypt_metadata,
encrypt_ref,
weak_crypto,
permissions,
user_password_matched,
owner_password_matched,
});
if let Some(warning) = r6_perms_warning {
self.repair_diagnostics
.push(Diagnostic::warning(warning, None));
}
Ok(())
}
fn encrypt_dictionary(&mut self) -> Result<Option<Dictionary>> {
match self.trailer().get("Encrypt").cloned() {
None => Ok(None),
Some(Object::Dictionary(dict)) => Ok(Some(dict)),
Some(Object::Reference(object_ref)) => match self.resolve(object_ref)? {
Object::Dictionary(dict) => Ok(Some(dict)),
_ => Err(EncryptedError::Malformed {
reason: "/Encrypt object is not a dictionary".into(),
}
.into()),
},
Some(_) => Err(EncryptedError::Malformed {
reason: "/Encrypt entry is not a dictionary or reference".into(),
}
.into()),
}
}
/// PDF version header as written in the first line of the file (e.g. `"1.7"`).
pub fn version(&self) -> &str {
&self.version
}
/// The trailer dictionary (or the dictionary attached to the trailing xref stream
/// for cross-reference-stream documents). This is where you'd reach for `/Root`,
/// `/Info`, `/Size`, `/ID`, etc.
pub fn trailer(&self) -> &Dictionary {
&self.trailer
}
pub(crate) fn startxref(&self) -> u64 {
self.startxref
}
pub(crate) fn previous_xref_offset(&self) -> u64 {
self.startxref()
}
pub(crate) fn last_xref_form(&self) -> XrefForm {
self.last_xref_form
}
pub(crate) fn source_xref_offsets(&self) -> Vec<(ObjectRef, u64)> {
self.source_xref_offsets.clone()
}
pub(crate) fn source_xref_entries(&self) -> BTreeMap<ObjectRef, XrefOffset> {
self.source_xref_entries.clone()
}
pub(crate) fn compressed_parent(&self, object_ref: ObjectRef) -> Option<(ObjectRef, u32)> {
self.compressed_member_parents.get(&object_ref).copied()
}
/// Replace `object_ref` with `object` in the in-memory object cache.
///
/// The original on-disk bytes are not touched; an incremental rewrite via
/// [`crate::write_pdf`] will see the updated value when it walks the cache and emit
/// a new revision for the touched object.
pub fn set_object(&mut self, object_ref: ObjectRef, object: Object) {
if let Some(CacheEntry::Compressed { stream, index }) =
self.cache.entry(object_ref).cloned()
{
let stream_ref = ObjectRef::new(stream, 0);
let (parent_ref, parent_index) = self
.compressed_parent_for_entry(stream_ref, index)
.unwrap_or((stream_ref, index));
self.compressed_member_parents
.insert(object_ref, (parent_ref, parent_index));
}
self.cache.set_resolved(object_ref, object);
}
pub fn delete_object(&mut self, object_ref: ObjectRef) {
if object_ref.number == 0
|| matches!(
self.cache.entry(object_ref),
Some(CacheEntry::Deleted | CacheEntry::Missing)
)
{
return;
}
self.cache.set_deleted(object_ref);
}
pub(crate) fn source_bytes(&mut self) -> Result<Vec<u8>> {
self.reader.seek(SeekFrom::Start(0))?;
let mut bytes = Vec::new();
self.reader.read_to_end(&mut bytes)?;
Ok(bytes)
}
/// Number of objects currently resolved in the cache. Useful when you want to
/// confirm that lazy resolution actually deferred work.
pub fn resolved_count(&self) -> usize {
self.cache.resolved_count()
}
pub(crate) fn resolved_object_refs(&self) -> Vec<ObjectRef> {
self.cache
.entries()
.iter()
.filter_map(|(object_ref, entry)| {
if matches!(entry, crate::cache::CacheEntry::Resolved(_)) {
Some(*object_ref)
} else {
None
}
})
.collect()
}
pub(crate) fn deleted_object_refs(&self) -> Vec<ObjectRef> {
self.cache.deleted_refs()
}
/// Every object reference known from the cross-reference table, including objects
/// that have not yet been parsed.
pub fn object_refs(&self) -> Vec<ObjectRef> {
self.cache.object_refs()
}
/// Object refs that the cross-reference table marks as live.
///
/// Excludes:
/// - `Deleted` — free entries (from `XrefOffset::Free`) and explicit
/// `delete_object()` calls,
/// - `Missing` — referenced but never present in any xref,
/// - `Reserved` — forward-reference placeholders that
/// [`Pdf::resolve`] returns as `Object::Null` (no real indirect
/// object behind them).
///
/// A `live_object_refs()` entry may still resolve to `Object::Null`; that
/// is a real null indirect object (e.g. `1 0 obj null endobj`), not an
/// absent one.
pub fn live_object_refs(&self) -> Vec<ObjectRef> {
self.cache
.entries()
.iter()
.filter_map(|(object_ref, entry)| match entry {
crate::cache::CacheEntry::Deleted
| crate::cache::CacheEntry::Missing
| crate::cache::CacheEntry::Reserved => None,
_ => Some(*object_ref),
})
.collect()
}
/// `/Root` as listed in the trailer, when present.
pub fn root_ref(&self) -> Option<ObjectRef> {
self.trailer.get_ref("Root")
}
/// Locate the linearization hint dictionary if this document is linearized
/// ("fast web view"). Returns `Ok(None)` for non-linearized documents.
///
/// This resolves object `(1, 0)` and inspects its `/Linearized` entry.
pub fn linearized_hint_ref(&mut self) -> Result<Option<ObjectRef>> {
let candidate = ObjectRef::new(1, 0);
let object = self.resolve(candidate)?;
let Some(dict) = object.into_dict() else {
return Ok(None);
};
let Some(linearized) = dict.get("Linearized") else {
return Ok(None);
};
Ok(match linearized {
Object::Integer(value) if *value > 0 => Some(candidate),
Object::Real(value) if value.is_finite() && *value > 0.0 => Some(candidate),
Object::Boolean(value) if *value => Some(candidate),
_ => None,
})
}
/// Resolve `object_ref` to its concrete value, parsing on demand.
///
/// Resolution caches the result so subsequent calls are constant-time. Unknown,
/// freed, or compressed-but-broken entries return [`Object::Null`] rather than an
/// error, matching the behavior the PDF spec mandates for missing objects (§7.3.10).
pub fn resolve(&mut self, object_ref: ObjectRef) -> Result<Object> {
match self.cache.entry(object_ref).cloned() {
Some(CacheEntry::Resolved(object)) => Ok(object),
Some(CacheEntry::Unresolved { offset }) => {
self.reader.seek(SeekFrom::Start(offset))?;
let mut bytes = Vec::new();
self.reader.read_to_end(&mut bytes)?;
let (parsed_ref, mut object, indirect_len) =
parse_indirect_object_detailed(&bytes)?;
if parsed_ref != object_ref {
return Ok(Object::Null);
}
// flpdf-9hc.27: when the stream's /Length is an indirect
// reference, the parser fell back to endstream-scan (it has no
// xref). Resolve the holder via the xref and re-slice to the
// authoritative length. This MUST happen before decryption:
// `object`/`bytes` are still ciphertext here, and
// `decrypt_resolved_object` decrypts in place afterwards.
if let Some(isl) = indirect_len {
if isl.holder != object_ref {
// Mark this object resolution in-progress before
// recursing into the holder: a cyclic length-holder
// chain (A's /Length -> B -> ... -> A) would otherwise
// recurse forever (resolve() does not otherwise mark
// in-progress). The cyclic re-entry now hits the
// `Reserved => Null` arm and the holder simply reads
// as non-Integer -> endstream-scan fallback. A holder
// resolution error is likewise non-fatal: fall back
// rather than failing the whole stream.
self.cache.set_reserved(object_ref);
if let Ok(Object::Integer(n)) = self.resolve(isl.holder) {
if let (Ok(n), Object::Stream(stream)) =
(usize::try_from(n), &mut object)
{
let auth_end = isl.data_start.saturating_add(n);
// Only override with the authoritative length
// when it lands STRICTLY inside the payload
// window — i.e. the resolved /Length is the
// spec content length and `endstream` (plus
// its mandatory preceding EOL) still follows.
// Then the exact `n` content bytes are used
// verbatim (no EOL trim), fixing non-QDF
// streams whose data legitimately ends with a
// newline (flpdf-9hc.27).
//
// The comparison is STRICT `<` by design.
// `auth_end == endstream_pos` is ambiguous:
// it is produced BOTH by flpdf's QDF holder
// convention (n counts the whole window incl.
// the framing EOL) AND by a non-conformant
// PDF lacking the ISO 32000-1 §7.3.8.1
// mandatory pre-`endstream` EOL. These want
// opposite handling and are indistinguishable
// from the object bytes alone. Treating the
// exact-window case as QDF (keep the parser's
// endstream-scan, which strips one framing
// EOL) preserves the pinned QDF round-trip /
// idempotence invariant; relaxing to `<=`
// empirically regresses qdf_tests
// `qdf_mode_round_trip_content_preserved` and
// `qdf_output_is_idempotent` (verified). The
// residual non-conformant exact-window case is
// the spec-aligned edge documented in
// flpdf-9hc.27; a whole-file QDF-detection
// refinement is tracked as flpdf-9hc.28.
// A too-large/garbage holder also lands here
// → safe endstream-scan fallback.
// flpdf-9hc.28: the exact-window case
// (auth_end == endstream_pos) is ambiguous —
// QDF holders count the framing EOL (keep the
// parser's endstream-scan / one-EOL trim to
// preserve QDF round-trip & idempotence),
// whereas a non-QDF holder is the spec content
// length and must be honoured verbatim (fixes
// a non-conformant stream whose data ends with
// a newline). Whole-file QDF detection picks
// the correct branch: strict `<` for QDF,
// inclusive `<=` for non-QDF.
let within_window = if self.is_qdf {
auth_end < isl.endstream_pos
} else {
auth_end <= isl.endstream_pos
};
if within_window && auth_end <= bytes.len() {
stream.data = bytes[isl.data_start..auth_end].to_vec();
}
}
}
}
}
let object = self.decrypt_resolved_object(object_ref, object)?;
self.cache.set_resolved(object_ref, object.clone());
Ok(object)
}
Some(CacheEntry::Compressed { stream, index }) => {
self.resolve_compressed_entry(object_ref, stream, index)
}
Some(CacheEntry::Missing | CacheEntry::Deleted) | None => Ok(Object::Null),
Some(CacheEntry::Reserved) => Ok(Object::Null),
}
}
fn resolve_compressed_entry(
&mut self,
object_ref: ObjectRef,
stream: u32,
index: u32,
) -> Result<Object> {
let stream_ref = ObjectRef::new(stream, 0);
let stream_object = match self.cache.entry(stream_ref).cloned() {
Some(CacheEntry::Resolved(object)) => object,
Some(CacheEntry::Unresolved { offset }) => {
self.reader.seek(SeekFrom::Start(offset))?;
let mut bytes = Vec::new();
self.reader.read_to_end(&mut bytes)?;
let (parsed_ref, object) = parse_indirect_object(&bytes)?;
if parsed_ref != stream_ref {
return Ok(Object::Null);
}
let object = self.decrypt_resolved_object(stream_ref, object)?;
self.cache.set_resolved(stream_ref, object.clone());
object
}
Some(CacheEntry::Compressed { .. }) => return Ok(Object::Null),
Some(CacheEntry::Missing | CacheEntry::Deleted) | None => return Ok(Object::Null),
Some(CacheEntry::Reserved) => return Ok(Object::Null),
};
let Some(stream_object) = stream_object.into_stream() else {
return Ok(Object::Null);
};
let (parent_ref, parent_index, object) =
self.parse_object_stream_chain_entry(stream_ref, &stream_object, index)?;
let object = self.decrypt_resolved_object(object_ref, object)?;
self.compressed_member_parents
.insert(object_ref, (parent_ref, parent_index));
self.cache.set_resolved(object_ref, object.clone());
Ok(object)
}
fn decrypt_resolved_object(&self, object_ref: ObjectRef, mut object: Object) -> Result<Object> {
let Some(encryption) = &self.encryption else {
return Ok(object);
};
if Some(object_ref) == encryption.encrypt_ref {
return Ok(object);
}
decrypt_object_strings(
object_ref,
&mut object,
encryption.string_mode,
&encryption.file_key,
encryption.encrypt_ref,
)?;
if let Object::Stream(stream) = &mut object {
if !encryption.encrypt_metadata && is_metadata_stream(&stream.dict) {
return Ok(object);
}
if stream_has_explicit_crypt_filter(&stream.dict) {
apply_explicit_crypt_filters(object_ref, stream, encryption)?;
} else {
decrypt_stream_bytes(
object_ref,
&mut stream.data,
encryption.stream_mode,
&encryption.file_key,
)?;
}
}
Ok(object)
}
fn parse_object_stream_chain_entry(
&mut self,
stream_ref: ObjectRef,
stream_object: &crate::Stream,
target_index: u32,
) -> Result<(ObjectRef, u32, Object)> {
let (member_stream_ref, member_index, member_stream) =
self.object_stream_chain_member(stream_ref, stream_object, target_index)?;
let object = parse_object_stream_entry(&member_stream, member_index)?;
Ok((member_stream_ref, member_index, object))
}
fn compressed_parent_for_entry(
&mut self,
stream_ref: ObjectRef,
target_index: u32,
) -> Result<(ObjectRef, u32)> {
let stream_object = self.resolve(stream_ref)?;
let Some(stream_object) = stream_object.into_stream() else {
return Err(Error::parse(0, "compressed parent is not an object stream"));
};
let (parent_ref, parent_index, _) =
self.object_stream_chain_member(stream_ref, &stream_object, target_index)?;
Ok((parent_ref, parent_index))
}
fn object_stream_chain_member(
&mut self,
stream_ref: ObjectRef,
stream_object: &crate::Stream,
target_index: u32,
) -> Result<(ObjectRef, u32, crate::Stream)> {
let mut streams = Vec::new();
self.collect_object_stream_chain(
stream_ref,
stream_object,
&mut streams,
&mut BTreeSet::new(),
)?;
let target_index = usize::try_from(target_index)
.map_err(|_| Error::parse(0, "compressed object index does not fit usize"))?;
let mut remaining = target_index;
for (member_stream_ref, member_stream) in streams {
let member_count = object_stream_count(&member_stream)?;
if remaining < member_count {
let member_index = u32::try_from(remaining)
.map_err(|_| Error::parse(0, "compressed object index does not fit u32"))?;
return Ok((member_stream_ref, member_index, member_stream));
}
remaining -= member_count;
}
Err(Error::parse(
0,
"compressed object index out of range for object stream chain",
))
}
fn collect_object_stream_chain(
&mut self,
stream_ref: ObjectRef,
stream_object: &crate::Stream,
streams: &mut Vec<(ObjectRef, crate::Stream)>,
seen: &mut BTreeSet<ObjectRef>,
) -> Result<()> {
if !seen.insert(stream_ref) {
return Err(Error::parse(0, "object stream /Extends cycle"));
}
if let Some(parent_ref) = stream_object.dict.get_ref("Extends") {
let parent_object = self.resolve(parent_ref)?;
let Some(parent_stream) = parent_object.into_stream() else {
return Err(Error::parse(0, "object stream /Extends is not a stream"));
};
self.collect_object_stream_chain(parent_ref, &parent_stream, streams, seen)?;
}
streams.push((stream_ref, stream_object.clone()));
Ok(())
}
}
impl<'a> Pdf<Cursor<&'a [u8]>> {
/// Open a PDF document from a borrowed byte slice without wrapping it in a `Cursor` manually.
///
/// This is a zero-copy convenience wrapper around [`Pdf::open`]. The resulting handle
/// borrows `bytes` for its lifetime, so it is not `'static` and cannot be moved out of
/// the scope that owns the original slice.
///
/// For an owned, movable version see [`Pdf::open_mem_owned`].
///
/// # Examples
///
/// ```no_run
/// use flpdf::Pdf;
///
/// let bytes: Vec<u8> = std::fs::read("input.pdf")?;
/// let mut pdf = Pdf::open_mem(&bytes)?;
/// println!("version {}", pdf.version());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn open_mem(bytes: &'a [u8]) -> crate::Result<Self> {
Self::open(Cursor::new(bytes))
}
/// Open a PDF document from a borrowed byte slice with explicit open options.
///
/// Like [`Pdf::open_mem`] but accepts a [`PdfOpenOptions`] struct for repair and
/// password configuration, mirroring [`Pdf::open_with_options`].
///
/// # Examples
///
/// ```no_run
/// use flpdf::{Pdf, PdfOpenOptions};
///
/// let bytes: Vec<u8> = std::fs::read("input.pdf")?;
/// let opts = PdfOpenOptions { repair: true, ..PdfOpenOptions::default() };
/// let mut pdf = Pdf::open_mem_with_options(&bytes, opts)?;
/// println!("version {}", pdf.version());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn open_mem_with_options(bytes: &'a [u8], options: PdfOpenOptions) -> crate::Result<Self> {
Self::open_with_options(Cursor::new(bytes), options)
}
}
impl Pdf<Cursor<Vec<u8>>> {
/// Open a PDF document from an owned byte vector without wrapping it in a `Cursor` manually.
///
/// This is the owned counterpart to [`Pdf::open_mem`]. The handle takes ownership of
/// `bytes` and is therefore `'static`—it can be freely moved and stored in data structures.
///
/// This is the preferred form for in-memory PDF handling in most contexts (e.g. WASM,
/// test helpers, fulgur's document pipeline).
///
/// # Examples
///
/// ```no_run
/// use flpdf::Pdf;
///
/// let bytes: Vec<u8> = std::fs::read("input.pdf")?;
/// let mut pdf = Pdf::open_mem_owned(bytes)?;
/// println!("version {}", pdf.version());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn open_mem_owned(bytes: Vec<u8>) -> crate::Result<Self> {
Self::open(Cursor::new(bytes))
}
/// Open a PDF document from an owned byte vector with explicit open options.
///
/// Like [`Pdf::open_mem_owned`] but accepts a [`PdfOpenOptions`] struct for repair
/// and password configuration, mirroring [`Pdf::open_with_options`].
///
/// # Examples
///
/// ```no_run
/// use flpdf::{Pdf, PdfOpenOptions};
///
/// let bytes: Vec<u8> = std::fs::read("input.pdf")?;
/// let opts = PdfOpenOptions { repair: true, ..PdfOpenOptions::default() };
/// let mut pdf = Pdf::open_mem_owned_with_options(bytes, opts)?;
/// println!("version {}", pdf.version());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn open_mem_owned_with_options(
bytes: Vec<u8>,
options: PdfOpenOptions,
) -> crate::Result<Self> {
Self::open_with_options(Cursor::new(bytes), options)
}
}
fn decrypt_object_strings(
object_ref: ObjectRef,
object: &mut Object,
mode: EncryptionMode,
file_key: &[u8],
encrypt_ref: Option<ObjectRef>,
) -> Result<()> {
match mode {
EncryptionMode::Rc4 => {
let key = per_object_key(
file_key,
object_ref.number,
u32::from(object_ref.generation),
ObjectKeyAlg::Rc4,
);
decrypt_strings_in_object(
object_ref,
object,
StringCipher::Rc4 { key: &key },
encrypt_ref,
)
}
EncryptionMode::Aes128 => {
let key = per_object_key(
file_key,
object_ref.number,
u32::from(object_ref.generation),
ObjectKeyAlg::Aes,
);
let key = aes128_object_key(&key)?;
decrypt_strings_in_object(
object_ref,
object,
StringCipher::Aes128 { key: &key },
encrypt_ref,
)
}
EncryptionMode::Identity => Ok(()),
EncryptionMode::Aes256 => {
let key = aes256_file_key(file_key)?;
decrypt_strings_in_object(
object_ref,
object,
StringCipher::Aes256 { key: &key },
encrypt_ref,
)
}
}
}
fn decrypt_stream_bytes(
object_ref: ObjectRef,
bytes: &mut Vec<u8>,
mode: EncryptionMode,
file_key: &[u8],
) -> Result<()> {
match mode {
EncryptionMode::Rc4 => {
let key = per_object_key(
file_key,
object_ref.number,
u32::from(object_ref.generation),
ObjectKeyAlg::Rc4,
);
decrypt_cipher_bytes(bytes, StringCipher::Rc4 { key: &key })
}
EncryptionMode::Aes128 => {
let key = per_object_key(
file_key,
object_ref.number,
u32::from(object_ref.generation),
ObjectKeyAlg::Aes,
);
let key = aes128_object_key(&key)?;
decrypt_cipher_bytes(bytes, StringCipher::Aes128 { key: &key })
}
EncryptionMode::Identity => Ok(()),
EncryptionMode::Aes256 => {
let key = aes256_file_key(file_key)?;
decrypt_cipher_bytes(bytes, StringCipher::Aes256 { key: &key })
}
}
}
fn apply_explicit_crypt_filters(
object_ref: ObjectRef,
stream: &mut crate::Stream,
encryption: &EncryptionState,
) -> Result<()> {
let decoded = crate::filters::decode_stream_data_with_crypt_filter(
&stream.dict,
&stream.data,
|decode_params, bytes| {
let mode = explicit_crypt_mode(encryption, decode_params)?;
let mut decrypted = bytes.to_vec();
decrypt_stream_bytes(object_ref, &mut decrypted, mode, &encryption.file_key)?;
Ok(decrypted)
},
)?;
stream.data = decoded;
stream.dict.remove("Filter");
stream.dict.remove("DecodeParms");
Ok(())
}
fn explicit_crypt_mode(
encryption: &EncryptionState,
decode_params: Option<&Object>,
) -> Result<EncryptionMode> {
let Some(params) = decode_params.and_then(Object::as_dict) else {
return Ok(EncryptionMode::Identity);
};
let name: &[u8] = match params.get("Name") {
None => return Ok(EncryptionMode::Identity),
Some(object) => object.as_name().ok_or_else(|| EncryptedError::Malformed {
reason: "/Crypt /DecodeParms /Name is not a name".into(),
})?,
};
if name == b"Identity" {
return Ok(EncryptionMode::Identity);
}
encryption.crypt_filters.get(name).copied().ok_or_else(|| {
EncryptedError::Malformed {
reason: format!("/CF entry '{}' not found", String::from_utf8_lossy(name)),
}
.into()
})
}
fn stream_has_explicit_crypt_filter(dict: &Dictionary) -> bool {
dict.get("Filter").is_some_and(|filter| {
filter.as_name() == Some(b"Crypt".as_slice())
|| filter.as_array().is_some_and(|filters| {
filters
.iter()
.any(|filter| filter.as_name() == Some(b"Crypt".as_slice()))
})
})
}
fn is_metadata_stream(dict: &Dictionary) -> bool {
dict.get("Type")
.and_then(Object::as_name)
.is_some_and(|name| name == b"Metadata")
}
fn aes256_file_key(file_key: &[u8]) -> Result<[u8; 32]> {
file_key.try_into().map_err(|_| {
EncryptedError::Malformed {
reason: "AES-256 file key is not 32 bytes".into(),
}
.into()
})
}
fn aes128_object_key(key: &[u8]) -> Result<[u8; 16]> {
key.try_into().map_err(|_| {
EncryptedError::Malformed {
reason: "AES-128 object key is not 16 bytes".into(),
}
.into()
})
}
pub(crate) fn parse_object_stream_entry(
stream_object: &crate::Stream,
target_index: u32,
) -> Result<Object> {
let stream_data = crate::filters::decode_stream_data(&stream_object.dict, &stream_object.data)?;
let stream_object_count = object_stream_count(stream_object)?;
let stream_data_first = parse_non_negative_i64(
stream_object
.dict
.get("First")
.ok_or(Error::Missing("Object stream /First"))?,
"Object stream /First",
)?;
let object_count = stream_object_count;
let first = usize::try_from(stream_data_first)
.map_err(|_| Error::parse(0, "Object stream /First does not fit usize"))?;
let mut header_parser = Parser::new(&stream_data);
let mut object_offsets = Vec::with_capacity(object_count);
for _ in 0..object_count {
let _object_number = parse_non_negative_u64(
header_parser.integer_for_indirect()?,
"object stream object number",
)?;
let object_offset = parse_non_negative_u64(
header_parser.integer_for_indirect()?,
"object stream object offset",
)?;
object_offsets.push(object_offset);
}
let target_index = usize::try_from(target_index)
.map_err(|_| Error::parse(0, "compressed object index does not fit usize"))?;
if target_index >= object_offsets.len() {
return Err(Error::parse(
0,
"compressed object index out of range for this stream",
));
}
let start = first
.checked_add(
usize::try_from(object_offsets[target_index])
.map_err(|_| Error::parse(0, "object stream offset does not fit usize"))?,
)
.ok_or_else(|| Error::parse(0, "compressed object offset overflow"))?;
if start > stream_data.len() {
return Err(Error::parse(0, "compressed object offset out of range"));
}
let mut object_parser = Parser::new(&stream_data[start..]);
object_parser.object()
}
fn standard_handler_inputs<'a>(
encrypt: &'a Dictionary,
trailer: &'a Dictionary,
) -> Result<StandardHandlerInputs<'a>> {
let filter = required_name(encrypt, "Filter")?;
let v = required_integer(encrypt, "V")?;
let r = required_integer(encrypt, "R")?;
if filter != "Standard" || !matches!((v, r), (1 | 2, 2 | 3) | (4, 4)) {
return Err(EncryptedError::UnsupportedHandler {
filter: filter.to_string(),
v,
r,
cfm: crypt_filter_method(encrypt),
}
.into());
}
let length_bits = match encrypt.get("Length") {
Some(Object::Integer(value)) => *value,
Some(_) => {
return Err(EncryptedError::Malformed {
reason: "/Length entry is not an integer".into(),
}
.into())
}
None => 40,
};
let p = required_permissions(encrypt)?;
let u = required_32_byte_string(encrypt, "U")?;
let o = required_32_byte_string(encrypt, "O")?;
let id0 = first_file_id(trailer)?;
let encrypt_metadata = encrypt_metadata_flag(encrypt)?;
Ok(StandardHandlerInputs {
v,
r,
length_bits,
p,
id0,
u,
o,
encrypt_metadata,
})
}
/// Reclassify a wrong-length `/U` or `/O` `Malformed` error from
/// [`standard_handler_r5_inputs`] as [`EncryptedError::BadPassword`].
///
/// Scoped to the V=5 R=5/R=6 authentication path (the sole caller of
/// `standard_handler_r5_inputs`): a `/U` or `/O` entry that is not exactly
/// 48 bytes is an unusable credential entry that is indistinguishable, from a
/// caller's perspective, from supplying the wrong password — qpdf reports
/// "invalid password" here, so we map to `BadPassword` for parity.
///
/// Only the `/U` / `/O` *length* error is remapped. `/UE` / `/OE` length
/// errors, missing entries, and non-string entries stay `Malformed`: those are
/// genuine structural defects, not credential mismatches. No broader
/// `Malformed` reclassification is performed (flpdf-9hc.3.21).
fn map_uo_length_to_bad_password(err: Error) -> Error {
match &err {
Error::Encrypted(EncryptedError::Malformed { reason })
if reason == "/U entry is not 48 bytes" || reason == "/O entry is not 48 bytes" =>
{
EncryptedError::BadPassword.into()
}
_ => err,
}
}
/// Decode the `--password` value as a raw hex file encryption key for
/// `--password-is-hex-key` (qpdf parity).
///
/// qpdf accepts upper- or lower-case hex and tolerates embedded whitespace;
/// the decoded key must be at most 32 bytes (the longest Standard-handler key,
/// AES-256). Invalid hex or an over-length key is reported as a clear
/// [`EncryptedError::Malformed`] — never a panic. An empty input decodes to an
/// empty key and is passed through unchanged (decryption then fails naturally
/// downstream; no special-casing here).
fn decode_hex_file_key(raw: &[u8]) -> Result<Vec<u8>> {
let trimmed: Vec<u8> = raw
.iter()
.copied()
.filter(|b| !b.is_ascii_whitespace())
.collect();
let key = hex::decode(&trimmed).map_err(|err| EncryptedError::Malformed {
reason: format!("--password-is-hex-key: --password is not valid hex ({err})"),
})?;
if key.len() > 32 {
return Err(EncryptedError::Malformed {
reason: format!(
"--password-is-hex-key: decoded key is {} bytes; \
the Standard security handler key is at most 32 bytes",
key.len()
),
}
.into());
}
Ok(key)
}
fn standard_handler_r5_inputs(encrypt: &Dictionary) -> Result<StandardHandlerR5Inputs<'_>> {
let filter = required_name(encrypt, "Filter")?;
let v = required_integer(encrypt, "V")?;
let r = required_integer(encrypt, "R")?;
if filter != "Standard" || v != 5 || !matches!(r, 5 | 6) {
return Err(EncryptedError::UnsupportedHandler {
filter: filter.to_string(),
v,
r,
cfm: crypt_filter_method(encrypt),
}
.into());
}
Ok(StandardHandlerR5Inputs {
u: required_48_byte_string(encrypt, "U")?,
o: required_48_byte_string(encrypt, "O")?,
ue: required_32_byte_string(encrypt, "UE")?,
oe: required_32_byte_string(encrypt, "OE")?,
})
}
fn encrypt_metadata_flag(encrypt: &Dictionary) -> Result<bool> {
match encrypt.get("EncryptMetadata") {
Some(Object::Boolean(value)) => Ok(*value),
Some(_) => Err(EncryptedError::Malformed {
reason: "/EncryptMetadata entry is not a boolean".into(),
}
.into()),
None => Ok(true),
}
}
fn required_permissions(encrypt: &Dictionary) -> Result<i32> {
i32::try_from(required_integer(encrypt, "P")?).map_err(|_| {
EncryptedError::Malformed {
reason: "/P entry is out of i32 range".into(),
}
.into()
})
}
fn r6_perms_warning(
encrypt: &Dictionary,
file_key: &[u8],
permissions: Permissions,
encrypt_metadata: bool,
) -> Result<Option<String>> {
let Some(perms) = encrypt.get("Perms") else {
return Ok(None);
};
let Object::String(bytes) = perms else {
return Ok(Some("R=6 /Perms entry is not a string".into()));
};
let Ok(mut block) = <[u8; 16]>::try_from(bytes.as_slice()) else {
return Ok(Some("R=6 /Perms entry is not 16 bytes".into()));
};
let Ok(file_key) = <&[u8; 32]>::try_from(file_key) else {
return Ok(Some(
"R=6 /Perms cannot be verified with non-256-bit file key".into(),
));
};
crate::security::primitives::aes256_ecb_decrypt_block(file_key, &mut block);
let perms_p = i32::from_le_bytes(block[..4].try_into().expect("slice length checked"));
let perms_metadata = match block[8] {
b'T' => true,
b'F' => false,
_ => {
return Ok(Some(
"R=6 /Perms encrypted-metadata flag is not T or F".into(),
))
}
};
if perms_p != permissions.raw() {
return Ok(Some(format!(
"R=6 /Perms permissions value {perms_p} does not match /P {}",
permissions.raw()
)));
}
if block[4..8] != [0xff; 4] {
return Ok(Some("R=6 /Perms reserved bytes are invalid".into()));
}
if perms_metadata != encrypt_metadata {
return Ok(Some(
"R=6 /Perms encrypted-metadata flag does not match /EncryptMetadata".into(),
));
}
if &block[9..12] != b"adb" {
return Ok(Some("R=6 /Perms magic bytes are not 'adb'".into()));
}
Ok(None)
}
fn required_revision(encrypt: &Dictionary) -> Result<i64> {
required_integer(encrypt, "R")
}
fn standard_v4_or_legacy_modes(encrypt: &Dictionary) -> Result<(EncryptionMode, EncryptionMode)> {
if required_integer(encrypt, "V").ok() != Some(4) {
return Ok((EncryptionMode::Rc4, EncryptionMode::Rc4));
}
Ok((
v4_mode_for_selector(encrypt, crypt_filter_selector(encrypt, "StmF")?)?,
v4_mode_for_selector(encrypt, crypt_filter_selector(encrypt, "StrF")?)?,
))
}
fn standard_r5_or_r6_modes(encrypt: &Dictionary) -> Result<(EncryptionMode, EncryptionMode)> {
Ok((
r5_or_r6_mode_for_selector(encrypt, crypt_filter_selector(encrypt, "StmF")?)?,
r5_or_r6_mode_for_selector(encrypt, crypt_filter_selector(encrypt, "StrF")?)?,
))
}
fn v4_mode_for_selector(encrypt: &Dictionary, selector: Option<String>) -> Result<EncryptionMode> {
let Some(selector) = selector else {
return Ok(EncryptionMode::Identity);
};
if selector == "Identity" {
return Ok(EncryptionMode::Identity);
}
let cfm = crypt_filter_method_for_name(encrypt, &selector)?.unwrap_or_else(|| "V2".into());
match cfm.as_str() {
"V2" => Ok(EncryptionMode::Rc4),
"AESV2" => Ok(EncryptionMode::Aes128),
"Identity" => Ok(EncryptionMode::Identity),
_ => unsupported_crypt_filter(encrypt, Some(cfm)),
}
}
fn r5_or_r6_mode_for_selector(
encrypt: &Dictionary,
selector: Option<String>,
) -> Result<EncryptionMode> {
let Some(selector) = selector else {
return Ok(EncryptionMode::Aes256);
};
if selector == "Identity" {
return Ok(EncryptionMode::Identity);
}
let cfm = crypt_filter_method_for_name(encrypt, &selector)?.unwrap_or_else(|| "AESV3".into());
match cfm.as_str() {
"AESV3" => Ok(EncryptionMode::Aes256),
"Identity" => Ok(EncryptionMode::Identity),
_ => unsupported_crypt_filter(encrypt, Some(cfm)),
}
}
fn crypt_filter_selector(encrypt: &Dictionary, key: &str) -> Result<Option<String>> {
let Some(value) = encrypt.get(key) else {
return Ok(None);
};
let name = value.as_name().ok_or_else(|| EncryptedError::Malformed {
reason: format!("/{key} entry is not a name"),
})?;
Ok(Some(String::from_utf8_lossy(name).into_owned()))
}
fn crypt_filter_method_for_name(encrypt: &Dictionary, name: &str) -> Result<Option<String>> {
let Some(cf) = encrypt.get("CF").and_then(Object::as_dict) else {
return Err(EncryptedError::Malformed {
reason: format!("/CF entry '{name}' not found"),
}
.into());
};
let Some(filter) = cf.get(name).and_then(Object::as_dict) else {
return Err(EncryptedError::Malformed {
reason: format!("/CF entry '{name}' not found"),
}
.into());
};
let Some(value) = filter.get("CFM") else {
return Ok(None);
};
let cfm = value.as_name().ok_or_else(|| EncryptedError::Malformed {
reason: format!("/CF/{name}/CFM entry is not a name"),
})?;
Ok(Some(String::from_utf8_lossy(cfm).into_owned()))
}
fn crypt_filter_modes(
encrypt: &Dictionary,
revision: i64,
) -> Result<BTreeMap<Vec<u8>, EncryptionMode>> {
let mut modes = BTreeMap::new();
let Some(cf) = encrypt.get("CF").and_then(Object::as_dict) else {
return Ok(modes);
};
for (name, value) in cf.iter() {
let Some(filter) = value.as_dict() else {
return Err(EncryptedError::Malformed {
reason: format!(
"/CF entry '{}' is not a dictionary",
String::from_utf8_lossy(name)
),
}
.into());
};
let cfm = match filter.get("CFM") {
None if revision >= 5 => "AESV3".to_string(),
None => "V2".to_string(),
Some(Object::Name(cfm)) => String::from_utf8_lossy(cfm).to_string(),
Some(_) => {
return Err(EncryptedError::Malformed {
reason: format!(
"/CF/{}/CFM entry is not a name",
String::from_utf8_lossy(name)
),
}
.into())
}
};
let mode = match (revision, cfm.as_str()) {
(_, "Identity") => EncryptionMode::Identity,
(5 | 6, "AESV3") => EncryptionMode::Aes256,
(5 | 6, _) => unsupported_crypt_filter(encrypt, Some(cfm))?,
(_, "V2") => EncryptionMode::Rc4,
(_, "AESV2") => EncryptionMode::Aes128,
(_, _) => unsupported_crypt_filter(encrypt, Some(cfm))?,
};
modes.insert(name.to_vec(), mode);
}
Ok(modes)
}
fn unsupported_crypt_filter<T>(encrypt: &Dictionary, cfm: Option<String>) -> Result<T> {
Err(EncryptedError::UnsupportedHandler {
filter: required_name(encrypt, "Filter")?.to_string(),
v: required_integer(encrypt, "V")?,
r: required_integer(encrypt, "R")?,
cfm,
}
.into())
}
fn required_integer(dict: &Dictionary, key: &'static str) -> Result<i64> {
match dict.get(key) {
Some(Object::Integer(value)) => Ok(*value),
Some(_) => Err(EncryptedError::Malformed {
reason: format!("/{key} entry is not an integer"),
}
.into()),
None => Err(EncryptedError::Malformed {
reason: format!("missing /{key} entry"),
}
.into()),
}
}
fn required_name<'a>(dict: &'a Dictionary, key: &'static str) -> Result<&'a str> {
match dict.get(key) {
Some(Object::Name(name)) => std::str::from_utf8(name).map_err(|_| {
EncryptedError::Malformed {
reason: format!("/{key} entry is not valid UTF-8"),
}
.into()
}),
Some(_) => Err(EncryptedError::Malformed {
reason: format!("/{key} entry is not a name"),
}
.into()),
None => Err(EncryptedError::Malformed {
reason: format!("missing /{key} entry"),
}
.into()),
}
}
fn required_32_byte_string<'a>(dict: &'a Dictionary, key: &'static str) -> Result<&'a [u8; 32]> {
match dict.get(key) {
Some(Object::String(bytes)) => bytes.as_slice().try_into().map_err(|_| {
EncryptedError::Malformed {
reason: format!("/{key} entry is not 32 bytes"),
}
.into()
}),
Some(_) => Err(EncryptedError::Malformed {
reason: format!("/{key} entry is not a string"),
}
.into()),
None => Err(EncryptedError::Malformed {
reason: format!("missing /{key} entry"),
}
.into()),
}
}
fn required_48_byte_string<'a>(dict: &'a Dictionary, key: &'static str) -> Result<&'a [u8; 48]> {
match dict.get(key) {
Some(Object::String(bytes)) => bytes.as_slice().try_into().map_err(|_| {
EncryptedError::Malformed {
reason: format!("/{key} entry is not 48 bytes"),
}
.into()
}),
Some(_) => Err(EncryptedError::Malformed {
reason: format!("/{key} entry is not a string"),
}
.into()),
None => Err(EncryptedError::Malformed {
reason: format!("missing /{key} entry"),
}
.into()),
}
}
fn first_file_id(trailer: &Dictionary) -> Result<&[u8]> {
match trailer.get("ID") {
Some(Object::Array(ids)) => match ids.first() {
Some(Object::String(id0)) => Ok(id0),
Some(_) => Err(EncryptedError::Malformed {
reason: "/ID first entry is not a string".into(),
}
.into()),
None => Err(EncryptedError::Malformed {
reason: "/ID array is empty".into(),
}
.into()),
},
Some(_) => Err(EncryptedError::Malformed {
reason: "/ID entry is not an array".into(),
}
.into()),
None => Err(EncryptedError::Malformed {
reason: "missing /ID entry".into(),
}
.into()),
}
}
fn crypt_filter_method(encrypt: &Dictionary) -> Option<String> {
let Some(Object::Dictionary(cf)) = encrypt.get("CF") else {
return None;
};
let Object::Dictionary(std_cf) = cf.get("StdCF")? else {
return None;
};
let Object::Name(cfm) = std_cf.get("CFM")? else {
return None;
};
Some(String::from_utf8_lossy(cfm).to_string())
}
pub(crate) fn object_stream_count(stream_object: &crate::Stream) -> Result<usize> {
usize::try_from(parse_non_negative_i64(
stream_object
.dict
.get("N")
.ok_or(Error::Missing("Object stream /N"))?,
"Object stream /N",
)?)
.map_err(|_| Error::parse(0, "Object stream /N does not fit usize"))
}
fn parse_non_negative_i64(value: &crate::Object, context: &str) -> Result<i64> {
let crate::Object::Integer(integer) = value else {
return Err(Error::parse(0, format!("{context} is not integer")));
};
if *integer < 0 {
return Err(Error::parse(0, format!("{context} is negative")));
}
Ok(*integer)
}
fn parse_non_negative_u64(value: i64, context: &str) -> Result<u64> {
if value < 0 {
return Err(Error::parse(0, format!("{context} is negative")));
}
Ok(value as u64)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pages::page_refs;
/// Minimal valid single-page PDF used across `open_mem` tests.
///
/// Structure:
/// 1 0 obj Catalog /Root
/// 2 0 obj Pages /Kids [3 0 R] /Count 1
/// 3 0 obj Page
fn minimal_pdf_bytes() -> Vec<u8> {
let mut pdf = Vec::new();
pdf.extend_from_slice(b"%PDF-1.4\n");
let off1 = pdf.len() as u64;
pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
let off2 = pdf.len() as u64;
pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
let off3 = pdf.len() as u64;
pdf.extend_from_slice(
b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
);
let xref_start = pdf.len() as u64;
let xref = format!(
"xref\n0 4\n0000000000 65535 f \n{:010} 00000 n \n{:010} 00000 n \n{:010} 00000 n \n",
off1, off2, off3,
);
pdf.extend_from_slice(xref.as_bytes());
let trailer =
format!("trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n");
pdf.extend_from_slice(trailer.as_bytes());
pdf
}
// ------------------------------------------------------------------
// Acceptance (1): open_mem_owned(Vec<u8>) opens an in-memory PDF
// ------------------------------------------------------------------
#[test]
fn open_mem_owned_opens_minimal_pdf() {
let bytes = minimal_pdf_bytes();
let mut pdf = Pdf::open_mem_owned(bytes).expect("open_mem_owned should succeed");
let refs = page_refs(&mut pdf).expect("page_refs should succeed");
assert_eq!(refs.len(), 1, "expected 1 page");
assert_eq!(
pdf.root_ref(),
Some(ObjectRef::new(1, 0)),
"expected root at 1 0 R"
);
}
// ------------------------------------------------------------------
// Acceptance (2): open_mem(&[u8]) opens an in-memory PDF
// ------------------------------------------------------------------
#[test]
fn open_mem_opens_minimal_pdf() {
let bytes = minimal_pdf_bytes();
let mut pdf = Pdf::open_mem(&bytes).expect("open_mem should succeed");
let refs = page_refs(&mut pdf).expect("page_refs should succeed");
assert_eq!(refs.len(), 1, "expected 1 page");
assert_eq!(
pdf.root_ref(),
Some(ObjectRef::new(1, 0)),
"expected root at 1 0 R"
);
}
// ------------------------------------------------------------------
// Acceptance (3): sugar wrappers match Cursor::new(bytes) directly
// ------------------------------------------------------------------
#[test]
fn open_mem_owned_matches_cursor_open() {
let bytes = minimal_pdf_bytes();
let mut pdf_cursor =
Pdf::open(Cursor::new(bytes.clone())).expect("Cursor::new open should succeed");
let refs_cursor = page_refs(&mut pdf_cursor).expect("page_refs from cursor");
let root_cursor = pdf_cursor.root_ref();
let mut pdf_owned = Pdf::open_mem_owned(bytes).expect("open_mem_owned should succeed");
let refs_owned = page_refs(&mut pdf_owned).expect("page_refs from open_mem_owned");
let root_owned = pdf_owned.root_ref();
assert_eq!(
refs_cursor, refs_owned,
"page refs from Cursor::new vs open_mem_owned must match"
);
assert_eq!(
root_cursor, root_owned,
"root ref from Cursor::new vs open_mem_owned must match"
);
}
#[test]
fn open_mem_matches_cursor_open() {
let bytes = minimal_pdf_bytes();
let mut pdf_cursor =
Pdf::open(Cursor::new(bytes.clone())).expect("Cursor::new open should succeed");
let refs_cursor = page_refs(&mut pdf_cursor).expect("page_refs from cursor");
let root_cursor = pdf_cursor.root_ref();
let mut pdf_mem = Pdf::open_mem(&bytes).expect("open_mem should succeed");
let refs_mem = page_refs(&mut pdf_mem).expect("page_refs from open_mem");
let root_mem = pdf_mem.root_ref();
assert_eq!(
refs_cursor, refs_mem,
"page refs from Cursor::new vs open_mem must match"
);
assert_eq!(
root_cursor, root_mem,
"root ref from Cursor::new vs open_mem must match"
);
}
// ------------------------------------------------------------------
// _with_options variants pass options through correctly (repair path)
// ------------------------------------------------------------------
#[test]
fn open_mem_owned_with_options_accepts_repair_flag() {
let bytes = minimal_pdf_bytes();
let opts = PdfOpenOptions {
repair: true,
..PdfOpenOptions::default()
};
let mut pdf =
Pdf::open_mem_owned_with_options(bytes, opts).expect("open_mem_owned_with_options");
let refs = page_refs(&mut pdf).expect("page_refs");
assert_eq!(refs.len(), 1);
}
#[test]
fn open_mem_with_options_accepts_repair_flag() {
let bytes = minimal_pdf_bytes();
let opts = PdfOpenOptions {
repair: true,
..PdfOpenOptions::default()
};
let mut pdf = Pdf::open_mem_with_options(&bytes, opts).expect("open_mem_with_options");
let refs = page_refs(&mut pdf).expect("page_refs");
assert_eq!(refs.len(), 1);
}
}