QPDF_private.hh
33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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
#ifndef QPDF_PRIVATE_HH
#define QPDF_PRIVATE_HH
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
#include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QPDFOutlineDocumentHelper.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageLabelDocumentHelper.hh>
#include <qpdf/QPDFTokenizer_private.hh>
using namespace qpdf;
namespace qpdf
{
class Stream;
namespace is
{
class OffsetBuffer;
} // namespace is
} // namespace qpdf
class BitStream;
class BitWriter;
class QPDF::ObjCache
{
public:
ObjCache() = default;
ObjCache(
std::shared_ptr<QPDFObject> object,
qpdf_offset_t end_before_space = 0,
qpdf_offset_t end_after_space = 0) :
object(std::move(object)),
end_before_space(end_before_space),
end_after_space(end_after_space)
{
}
std::shared_ptr<QPDFObject> object;
qpdf_offset_t end_before_space{0};
qpdf_offset_t end_after_space{0};
};
class QPDF::EncryptionParameters
{
friend class QPDF;
public:
EncryptionParameters() = default;
int
P() const
{
return static_cast<int>(P_.to_ullong());
}
// Bits in P are numbered from 1 as in the PDF spec.
bool P(size_t bit) const;
int
R()
{
return R_;
}
void initialize(QPDF& qpdf);
encryption_method_e interpretCF(Name const& cf) const;
private:
bool encrypted{false};
bool encryption_initialized{false};
std::bitset<32> P_{0xfffffffc}; // Specification always requires bits 1 and 2 to be cleared.
int encryption_V{0};
int R_{0};
bool encrypt_metadata{true};
std::map<std::string, encryption_method_e> crypt_filters;
encryption_method_e cf_stream{e_none};
encryption_method_e cf_string{e_none};
encryption_method_e cf_file{e_none};
std::string provided_password;
std::string user_password;
std::string encryption_key;
std::string cached_object_encryption_key;
QPDFObjGen cached_key_og{};
bool user_password_matched{false};
bool owner_password_matched{false};
};
class QPDF::StringDecrypter final: public QPDFObjectHandle::StringDecrypter
{
friend class QPDF;
public:
StringDecrypter(QPDF* qpdf, QPDFObjGen og);
~StringDecrypter() final = default;
void
decryptString(std::string& val) final
{
qpdf->decryptString(val, og);
}
private:
QPDF* qpdf;
QPDFObjGen og;
};
// PDF 1.4: Table F.4
struct QPDF::HPageOffsetEntry
{
int delta_nobjects{0}; // 1
qpdf_offset_t delta_page_length{0}; // 2
// vectors' sizes = nshared_objects
int nshared_objects{0}; // 3
std::vector<int> shared_identifiers; // 4
std::vector<int> shared_numerators; // 5
qpdf_offset_t delta_content_offset{0}; // 6
qpdf_offset_t delta_content_length{0}; // 7
};
// PDF 1.4: Table F.3
struct QPDF::HPageOffset
{
int min_nobjects{0}; // 1
qpdf_offset_t first_page_offset{0}; // 2
int nbits_delta_nobjects{0}; // 3
int min_page_length{0}; // 4
int nbits_delta_page_length{0}; // 5
int min_content_offset{0}; // 6
int nbits_delta_content_offset{0}; // 7
int min_content_length{0}; // 8
int nbits_delta_content_length{0}; // 9
int nbits_nshared_objects{0}; // 10
int nbits_shared_identifier{0}; // 11
int nbits_shared_numerator{0}; // 12
int shared_denominator{0}; // 13
// vector size is npages
std::vector<HPageOffsetEntry> entries;
};
// PDF 1.4: Table F.6
struct QPDF::HSharedObjectEntry
{
// Item 3 is a 128-bit signature (unsupported by Acrobat)
int delta_group_length{0}; // 1
int signature_present{0}; // 2 -- always 0
int nobjects_minus_one{0}; // 4 -- always 0
};
// PDF 1.4: Table F.5
struct QPDF::HSharedObject
{
int first_shared_obj{0}; // 1
qpdf_offset_t first_shared_offset{0}; // 2
int nshared_first_page{0}; // 3
int nshared_total{0}; // 4
int nbits_nobjects{0}; // 5
int min_group_length{0}; // 6
int nbits_delta_group_length{0}; // 7
// vector size is nshared_total
std::vector<HSharedObjectEntry> entries;
};
// PDF 1.4: Table F.9
struct QPDF::HGeneric
{
int first_object{0}; // 1
qpdf_offset_t first_object_offset{0}; // 2
int nobjects{0}; // 3
int group_length{0}; // 4
};
// Other linearization data structures
// Initialized from Linearization Parameter dictionary
struct QPDF::LinParameters
{
qpdf_offset_t file_size{0}; // /L
int first_page_object{0}; // /O
qpdf_offset_t first_page_end{0}; // /E
size_t npages{0}; // /N
qpdf_offset_t xref_zero_offset{0}; // /T
int first_page{0}; // /P
qpdf_offset_t H_offset{0}; // offset of primary hint stream
qpdf_offset_t H_length{0}; // length of primary hint stream
};
// Computed hint table value data structures. These tables contain the computed values on which
// the hint table values are based. They exclude things like number of bits and store actual
// values instead of mins and deltas. File offsets are also absolute rather than being offset
// by the size of the primary hint table. We populate the hint table structures from these
// during writing and compare the hint table values with these during validation. We ignore
// some values for various reasons described in the code. Those values are omitted from these
// structures. Note also that object numbers are object numbers from the input file, not the
// output file.
// Naming convention: CHSomething is analogous to HSomething above. "CH" is computed hint.
struct QPDF::CHPageOffsetEntry
{
int nobjects{0};
int nshared_objects{0};
// vectors' sizes = nshared_objects
std::vector<int> shared_identifiers;
};
struct QPDF::CHPageOffset
{
// vector size is npages
std::vector<CHPageOffsetEntry> entries;
};
struct QPDF::CHSharedObjectEntry
{
CHSharedObjectEntry(int object) :
object(object)
{
}
int object;
};
// PDF 1.4: Table F.5
struct QPDF::CHSharedObject
{
int first_shared_obj{0};
int nshared_first_page{0};
int nshared_total{0};
// vector size is nshared_total
std::vector<CHSharedObjectEntry> entries;
};
// No need for CHGeneric -- HGeneric is fine as is.
// Data structures to support optimization -- implemented in QPDF_optimization.cc
class QPDF::ObjUser
{
public:
enum user_e { ou_page = 1, ou_thumb, ou_trailer_key, ou_root_key, ou_root };
ObjUser() = delete;
// type must be ou_root
ObjUser(user_e type);
// type must be one of ou_page or ou_thumb
ObjUser(user_e type, size_t pageno);
// type must be one of ou_trailer_key or ou_root_key
ObjUser(user_e type, std::string const& key);
bool operator<(ObjUser const&) const;
user_e ou_type;
size_t pageno{0}; // if ou_page;
std::string key; // if ou_trailer_key or ou_root_key
};
struct QPDF::UpdateObjectMapsFrame
{
UpdateObjectMapsFrame(ObjUser const& ou, QPDFObjectHandle oh, bool top);
ObjUser const& ou;
QPDFObjectHandle oh;
bool top;
};
class QPDF::PatternFinder final: public InputSource::Finder
{
public:
PatternFinder(QPDF& qpdf, bool (QPDF::*checker)()) :
qpdf(qpdf),
checker(checker)
{
}
~PatternFinder() final = default;
bool
check() final
{
return (this->qpdf.*checker)();
}
private:
QPDF& qpdf;
bool (QPDF::*checker)();
};
// This class is used to represent a PDF document.
//
// The main function of the QPDF class is to represent a PDF document. Doc is the implementation
// class for this aspect of QPDF.
class QPDF::Doc
{
public:
class JobSetter;
class ParseGuard;
class Resolver;
class Writer;
class Encryption
{
public:
// This class holds data read from the encryption dictionary.
Encryption(
int V,
int R,
int Length_bytes,
int P,
std::string const& O,
std::string const& U,
std::string const& OE,
std::string const& UE,
std::string const& Perms,
std::string const& id1,
bool encrypt_metadata) :
V(V),
R(R),
Length_bytes(Length_bytes),
P(static_cast<unsigned long long>(P)),
O(O),
U(U),
OE(OE),
UE(UE),
Perms(Perms),
id1(id1),
encrypt_metadata(encrypt_metadata)
{
}
Encryption(int V, int R, int Length_bytes, bool encrypt_metadata) :
V(V),
R(R),
Length_bytes(Length_bytes),
encrypt_metadata(encrypt_metadata)
{
}
int getV() const;
int getR() const;
int getLengthBytes() const;
int getP() const;
// Bits in P are numbered from 1 as in the PDF spec.
bool getP(size_t bit) const;
std::string const& getO() const;
std::string const& getU() const;
std::string const& getOE() const;
std::string const& getUE() const;
std::string const& getPerms() const;
std::string const& getId1() const;
bool getEncryptMetadata() const;
// Bits in P are numbered from 1 as in the PDF spec.
void setP(size_t bit, bool val);
void setP(unsigned long val);
void setO(std::string const&);
void setU(std::string const&);
void setId1(std::string const& val);
void setV5EncryptionParameters(
std::string const& O,
std::string const& OE,
std::string const& U,
std::string const& UE,
std::string const& Perms);
std::string compute_encryption_key(std::string const& password) const;
bool
check_owner_password(std::string& user_password, std::string const& owner_password) const;
bool check_user_password(std::string const& user_password) const;
std::string
recover_encryption_key_with_password(std::string const& password, bool& perms_valid) const;
void compute_encryption_O_U(char const* user_password, char const* owner_password);
std::string
compute_encryption_parameters_V5(char const* user_password, char const* owner_password);
std::string compute_parameters(char const* user_password, char const* owner_password);
private:
static constexpr unsigned int OU_key_bytes_V4 = 16; // ( == sizeof(MD5::Digest)
Encryption(Encryption const&) = delete;
Encryption& operator=(Encryption const&) = delete;
std::string hash_V5(
std::string const& password, std::string const& salt, std::string const& udata) const;
std::string
compute_O_value(std::string const& user_password, std::string const& owner_password) const;
std::string compute_U_value(std::string const& user_password) const;
std::string compute_encryption_key_from_password(std::string const& password) const;
std::string recover_encryption_key_with_password(std::string const& password) const;
bool check_owner_password_V4(
std::string& user_password, std::string const& owner_password) const;
bool check_owner_password_V5(std::string const& owner_passworda) const;
std::string compute_Perms_value_V5_clear() const;
std::string compute_O_rc4_key(
std::string const& user_password, std::string const& owner_password) const;
std::string compute_U_value_R2(std::string const& user_password) const;
std::string compute_U_value_R3(std::string const& user_password) const;
bool check_user_password_V4(std::string const& user_password) const;
bool check_user_password_V5(std::string const& user_password) const;
int V;
int R;
int Length_bytes;
std::bitset<32> P{0xfffffffc}; // Specification always requires bits 1 and 2 to be cleared.
std::string O;
std::string U;
std::string OE;
std::string UE;
std::string Perms;
std::string id1;
bool encrypt_metadata;
}; // class QPDF::Doc::Encryption
class Linearization
{
public:
Linearization() = delete;
Linearization(Linearization const&) = delete;
Linearization(Linearization&&) = delete;
Linearization& operator=(Linearization const&) = delete;
Linearization& operator=(Linearization&&) = delete;
~Linearization() = default;
Linearization(QPDF& qpdf, QPDF::Members* m) :
qpdf(qpdf),
m(m)
{
}
// For QPDFWriter:
template <typename T>
void optimize_internal(
T const& object_stream_data,
bool allow_changes = true,
std::function<int(QPDFObjectHandle&)> skip_stream_parameters = nullptr);
void optimize(
QPDFWriter::ObjTable const& obj,
std::function<int(QPDFObjectHandle&)> skip_stream_parameters);
// Get lists of all objects in order according to the part of a linearized file that they
// belong to.
void getLinearizedParts(
QPDFWriter::ObjTable const& obj,
std::vector<QPDFObjectHandle>& part4,
std::vector<QPDFObjectHandle>& part6,
std::vector<QPDFObjectHandle>& part7,
std::vector<QPDFObjectHandle>& part8,
std::vector<QPDFObjectHandle>& part9);
void generateHintStream(
QPDFWriter::NewObjTable const& new_obj,
QPDFWriter::ObjTable const& obj,
std::string& hint_stream,
int& S,
int& O,
bool compressed);
// methods to support linearization checking -- implemented in QPDF_linearization.cc
void readLinearizationData();
void checkLinearizationInternal();
void dumpLinearizationDataInternal();
void linearizationWarning(std::string_view);
qpdf::Dictionary readHintStream(Pipeline&, qpdf_offset_t offset, size_t length);
void readHPageOffset(BitStream);
void readHSharedObject(BitStream);
void readHGeneric(BitStream, HGeneric&);
qpdf_offset_t maxEnd(ObjUser const& ou);
qpdf_offset_t getLinearizationOffset(QPDFObjGen);
QPDFObjectHandle
getUncompressedObject(QPDFObjectHandle&, std::map<int, int> const& object_stream_data);
QPDFObjectHandle getUncompressedObject(QPDFObjectHandle&, QPDFWriter::ObjTable const& obj);
int lengthNextN(int first_object, int n);
void checkHPageOffset(
std::vector<QPDFObjectHandle> const& pages, std::map<int, int>& idx_to_obj);
void checkHSharedObject(
std::vector<QPDFObjectHandle> const& pages, std::map<int, int>& idx_to_obj);
void checkHOutlines();
void dumpHPageOffset();
void dumpHSharedObject();
void dumpHGeneric(HGeneric&);
qpdf_offset_t adjusted_offset(qpdf_offset_t offset);
template <typename T>
void calculateLinearizationData(T const& object_stream_data);
template <typename T>
void pushOutlinesToPart(
std::vector<QPDFObjectHandle>& part,
std::set<QPDFObjGen>& lc_outlines,
T const& object_stream_data);
int outputLengthNextN(
int in_object,
int n,
QPDFWriter::NewObjTable const& new_obj,
QPDFWriter::ObjTable const& obj);
void calculateHPageOffset(
QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj);
void calculateHSharedObject(
QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj);
void
calculateHOutline(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj);
void writeHPageOffset(BitWriter&);
void writeHSharedObject(BitWriter&);
void writeHGeneric(BitWriter&, HGeneric&);
// Methods to support optimization
void updateObjectMaps(
ObjUser const& ou,
QPDFObjectHandle oh,
std::function<int(QPDFObjectHandle&)> skip_stream_parameters);
void filterCompressedObjects(std::map<int, int> const& object_stream_data);
void filterCompressedObjects(QPDFWriter::ObjTable const& object_stream_data);
private:
QPDF& qpdf;
QPDF::Members* m;
};
class Objects
{
public:
class Foreign
{
class Copier
{
public:
Copier(QPDF& qpdf) :
qpdf(qpdf)
{
}
QPDFObjectHandle copied(QPDFObjectHandle const& foreign);
private:
QPDFObjectHandle
replace_indirect_object(QPDFObjectHandle const& foreign, bool top = false);
void reserve_objects(QPDFObjectHandle const& foreign, bool top = false);
QPDF& qpdf;
std::map<QPDFObjGen, QPDFObjectHandle> object_map;
std::vector<QPDFObjectHandle> to_copy;
QPDFObjGen::set visiting;
};
public:
Foreign(QPDF& qpdf) :
qpdf(qpdf)
{
}
Foreign() = delete;
Foreign(Foreign const&) = delete;
Foreign(Foreign&&) = delete;
Foreign& operator=(Foreign const&) = delete;
Foreign& operator=(Foreign&&) = delete;
~Foreign() = default;
// Return a local handle to the foreign object. Copy the foreign object if necessary.
QPDFObjectHandle
copied(QPDFObjectHandle const& foreign)
{
return copier(foreign).copied(foreign);
}
private:
Copier& copier(QPDFObjectHandle const& foreign);
QPDF& qpdf;
std::map<unsigned long long, Copier> copiers;
}; // class QPDF::Doc::Objects::Foreign
class Streams
{
// Copier manages the copying of streams into this PDF. It is used both for copying
// local and foreign streams.
class Copier final: public QPDFObjectHandle::StreamDataProvider
{
class Data
{
friend class Streams;
public:
Data(Stream& source, QPDFObjectHandle const& dest_dict);
private:
std::shared_ptr<EncryptionParameters> encp;
std::shared_ptr<InputSource> file;
QPDFObjGen source_og;
qpdf_offset_t offset;
size_t length;
QPDFObjectHandle dest_dict;
bool is_root_metadata{false};
};
public:
Copier() = delete;
Copier(StreamDataProvider const&) = delete;
Copier(StreamDataProvider&&) = delete;
Copier& operator=(StreamDataProvider const&) = delete;
Copier& operator=(StreamDataProvider&&) = delete;
Copier(Streams& streams);
~Copier() final = default;
bool provideStreamData(
QPDFObjGen const& og,
Pipeline* pipeline,
bool suppress_warnings,
bool will_retry) final;
void
register_copy(QPDFObjGen local_og, QPDFObjectHandle const& foreign_stream)
{
copied_streams.insert_or_assign(local_og, foreign_stream);
}
void
register_copy(
QPDFObjGen local_og,
Stream& foreign,
qpdf_offset_t offset,
QPDFObjectHandle const& local_dict)
{
copied_data.insert_or_assign(local_og, Data(foreign, local_dict));
}
private:
Streams& streams;
std::map<QPDFObjGen, QPDFObjectHandle> copied_streams;
std::map<QPDFObjGen, Data> copied_data;
};
public:
Streams(QPDF& qpdf) :
qpdf_(qpdf),
copier_(std::make_shared<Copier>(*this))
{
}
Streams() = delete;
Streams(Streams const&) = delete;
Streams(Streams&&) = delete;
Streams& operator=(Streams const&) = delete;
Streams& operator=(Streams&&) = delete;
~Streams() = default;
public:
static bool
pipeStreamData(
QPDF* qpdf,
QPDFObjGen og,
qpdf_offset_t offset,
size_t length,
QPDFObjectHandle dict,
bool is_root_metadata,
Pipeline* pipeline,
bool suppress_warnings,
bool will_retry)
{
return qpdf->pipeStreamData(
og,
offset,
length,
dict,
is_root_metadata,
pipeline,
suppress_warnings,
will_retry);
}
QPDF&
qpdf() const
{
return qpdf_;
}
std::shared_ptr<Copier>&
copier()
{
return copier_;
}
bool immediate_copy_from() const;
private:
QPDF& qpdf_;
std::shared_ptr<Copier> copier_;
}; // class QPDF::Doc::Objects::Streams
public:
Objects() = delete;
Objects(Objects const&) = delete;
Objects(Objects&&) = delete;
Objects& operator=(Objects const&) = delete;
Objects& operator=(Objects&&) = delete;
~Objects() = default;
Objects(QPDF& qpdf, QPDF::Members* m) :
qpdf(qpdf),
m(m),
foreign_(qpdf),
streams_(qpdf)
{
}
Foreign&
foreign()
{
return foreign_;
}
Streams&
streams()
{
return streams_;
}
void parse(char const* password);
std::shared_ptr<QPDFObject> const& resolve(QPDFObjGen og);
void inParse(bool);
QPDFObjGen nextObjGen();
QPDFObjectHandle newIndirect(QPDFObjGen, std::shared_ptr<QPDFObject> const&);
void updateCache(
QPDFObjGen og,
std::shared_ptr<QPDFObject> const& object,
qpdf_offset_t end_before_space,
qpdf_offset_t end_after_space,
bool destroy = true);
bool resolveXRefTable();
QPDFObjectHandle readObjectAtOffset(
qpdf_offset_t offset, std::string const& description, bool skip_cache_if_in_xref);
QPDFTokenizer::Token readToken(InputSource& input, size_t max_len = 0);
QPDFObjectHandle makeIndirectFromQPDFObject(std::shared_ptr<QPDFObject> const& obj);
std::shared_ptr<QPDFObject> getObjectForParser(int id, int gen, bool parse_pdf);
std::shared_ptr<QPDFObject> getObjectForJSON(int id, int gen);
size_t tableSize();
// For QPDFWriter:
std::map<QPDFObjGen, QPDFXRefEntry> const& getXRefTableInternal();
// Get a list of objects that would be permitted in an object stream.
template <typename T>
std::vector<T> getCompressibleObjGens();
std::vector<QPDFObjGen> getCompressibleObjVector();
std::vector<bool> getCompressibleObjSet();
private:
void setTrailer(QPDFObjectHandle obj);
void reconstruct_xref(QPDFExc& e, bool found_startxref = true);
void read_xref(qpdf_offset_t offset, bool in_stream_recovery = false);
bool parse_xrefFirst(std::string const& line, int& obj, int& num, int& bytes);
bool read_xrefEntry(qpdf_offset_t& f1, int& f2, char& type);
bool read_bad_xrefEntry(qpdf_offset_t& f1, int& f2, char& type);
qpdf_offset_t read_xrefTable(qpdf_offset_t offset);
qpdf_offset_t read_xrefStream(qpdf_offset_t offset, bool in_stream_recovery = false);
qpdf_offset_t processXRefStream(
qpdf_offset_t offset, QPDFObjectHandle& xref_stream, bool in_stream_recovery = false);
std::pair<int, std::array<int, 3>>
processXRefW(QPDFObjectHandle& dict, std::function<QPDFExc(std::string_view)> damaged);
int processXRefSize(
QPDFObjectHandle& dict,
int entry_size,
std::function<QPDFExc(std::string_view)> damaged);
std::pair<int, std::vector<std::pair<int, int>>> processXRefIndex(
QPDFObjectHandle& dict,
int max_num_entries,
std::function<QPDFExc(std::string_view)> damaged);
void insertXrefEntry(int obj, int f0, qpdf_offset_t f1, int f2);
void insertFreeXrefEntry(QPDFObjGen);
QPDFObjectHandle readTrailer();
QPDFObjectHandle readObject(std::string const& description, QPDFObjGen og);
void readStream(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset);
void validateStreamLineEnd(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset);
QPDFObjectHandle
readObjectInStream(qpdf::is::OffsetBuffer& input, int stream_id, int obj_id);
size_t recoverStreamLength(
std::shared_ptr<InputSource> input, QPDFObjGen og, qpdf_offset_t stream_offset);
QPDFObjGen read_object_start(qpdf_offset_t offset);
void readObjectAtOffset(
bool attempt_recovery,
qpdf_offset_t offset,
std::string const& description,
QPDFObjGen exp_og);
void resolveObjectsInStream(int obj_stream_number);
bool isCached(QPDFObjGen og);
bool isUnresolved(QPDFObjGen og);
void setLastObjectDescription(std::string const& description, QPDFObjGen og);
QPDF& qpdf;
QPDF::Members* m;
Foreign foreign_;
Streams streams_;
}; // class QPDF::Doc::Objects
// This class is used to represent a PDF Pages tree.
class Pages
{
public:
Pages() = delete;
Pages(Pages const&) = delete;
Pages(Pages&&) = delete;
Pages& operator=(Pages const&) = delete;
Pages& operator=(Pages&&) = delete;
~Pages() = default;
Pages(QPDF& qpdf, QPDF::Members* m) :
qpdf(qpdf),
m(m)
{
}
void getAllPagesInternal(
QPDFObjectHandle cur_pages,
QPDFObjGen::set& visited,
QPDFObjGen::set& seen,
bool media_box,
bool resources);
void insertPage(QPDFObjectHandle newpage, int pos);
void flattenPagesTree();
void insertPageobjToPage(QPDFObjectHandle const& obj, int pos, bool check_duplicate);
void pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys);
void pushInheritedAttributesToPageInternal(
QPDFObjectHandle,
std::map<std::string, std::vector<QPDFObjectHandle>>&,
bool allow_changes,
bool warn_skipped_keys);
private:
QPDF& qpdf;
QPDF::Members* m;
}; // class QPDF::Doc::Pages
Doc() = delete;
Doc(Doc const&) = delete;
Doc(Doc&&) = delete;
Doc& operator=(Doc const&) = delete;
Doc& operator=(Doc&&) = delete;
~Doc() = default;
Doc(QPDF& qpdf, QPDF::Members& m) :
qpdf(qpdf),
m(m),
lin_(qpdf, &m),
objects_(qpdf, &m),
pages_(qpdf, &m)
{
}
Linearization&
linearization()
{
return lin_;
};
Objects&
objects()
{
return objects_;
};
Pages&
pages()
{
return pages_;
}
bool reconstructed_xref() const;
QPDFAcroFormDocumentHelper&
acroform()
{
if (!acroform_) {
acroform_ = std::make_unique<QPDFAcroFormDocumentHelper>(qpdf);
}
return *acroform_;
}
QPDFEmbeddedFileDocumentHelper&
embedded_files()
{
if (!embedded_files_) {
embedded_files_ = std::make_unique<QPDFEmbeddedFileDocumentHelper>(qpdf);
}
return *embedded_files_;
}
QPDFOutlineDocumentHelper&
outlines()
{
if (!outlines_) {
outlines_ = std::make_unique<QPDFOutlineDocumentHelper>(qpdf);
}
return *outlines_;
}
QPDFPageDocumentHelper&
page_dh()
{
if (!page_dh_) {
page_dh_ = std::make_unique<QPDFPageDocumentHelper>(qpdf);
}
return *page_dh_;
}
QPDFPageLabelDocumentHelper&
page_labels()
{
if (!page_labels_) {
page_labels_ = std::make_unique<QPDFPageLabelDocumentHelper>(qpdf);
}
return *page_labels_;
}
private:
QPDF& qpdf;
QPDF::Members& m;
Linearization lin_;
Objects objects_;
Pages pages_;
// Document Helpers;
std::unique_ptr<QPDFAcroFormDocumentHelper> acroform_;
std::unique_ptr<QPDFEmbeddedFileDocumentHelper> embedded_files_;
std::unique_ptr<QPDFOutlineDocumentHelper> outlines_;
std::unique_ptr<QPDFPageDocumentHelper> page_dh_;
std::unique_ptr<QPDFPageLabelDocumentHelper> page_labels_;
};
class QPDF::Members
{
friend class QPDF;
friend class ResolveRecorder;
public:
Members(QPDF& qpdf);
Members(Members const&) = delete;
~Members() = default;
private:
Doc doc;
Doc::Linearization& lin;
Doc::Objects& objects;
Doc::Pages& pages;
std::shared_ptr<QPDFLogger> log;
unsigned long long unique_id{0};
qpdf::Tokenizer tokenizer;
std::shared_ptr<InputSource> file;
std::string last_object_description;
std::shared_ptr<QPDFObject::Description> last_ostream_description;
bool provided_password_is_hex_key{false};
bool ignore_xref_streams{false};
bool suppress_warnings{false};
size_t max_warnings{0};
bool attempt_recovery{true};
bool check_mode{false};
std::shared_ptr<EncryptionParameters> encp;
std::string pdf_version;
std::map<QPDFObjGen, QPDFXRefEntry> xref_table;
// Various tables are indexed by object id, with potential size id + 1
int xref_table_max_id{std::numeric_limits<int>::max() - 1};
qpdf_offset_t xref_table_max_offset{0};
std::set<int> deleted_objects;
std::map<QPDFObjGen, ObjCache> obj_cache;
std::set<QPDFObjGen> resolving;
QPDFObjectHandle trailer;
std::vector<QPDFObjectHandle> all_pages;
bool invalid_page_found{false};
std::map<QPDFObjGen, int> pageobj_to_pages_pos;
bool pushed_inherited_attributes_to_pages{false};
bool ever_pushed_inherited_attributes_to_pages{false};
bool ever_called_get_all_pages{false};
std::vector<QPDFExc> warnings;
bool reconstructed_xref{false};
bool in_read_xref_stream{false};
bool fixed_dangling_refs{false};
bool immediate_copy_from{false};
bool in_parse{false};
bool parsed{false};
std::set<int> resolved_object_streams;
// Linearization data
qpdf_offset_t first_xref_item_offset{0}; // actual value from file
bool uncompressed_after_compressed{false};
bool linearization_warnings{false}; // set by linearizationWarning, used by checkLinearization
// Linearization parameter dictionary and hint table data: may be read from file or computed
// prior to writing a linearized file
QPDFObjectHandle lindict;
LinParameters linp;
HPageOffset page_offset_hints;
HSharedObject shared_object_hints;
HGeneric outline_hints;
// Computed linearization data: used to populate above tables during writing and to compare
// with them during validation. c_ means computed.
LinParameters c_linp;
CHPageOffset c_page_offset_data;
CHSharedObject c_shared_object_data;
HGeneric c_outline_data;
// Object ordering data for linearized files: initialized by calculateLinearizationData().
// Part numbers refer to the PDF 1.4 specification.
std::vector<QPDFObjectHandle> part4;
std::vector<QPDFObjectHandle> part6;
std::vector<QPDFObjectHandle> part7;
std::vector<QPDFObjectHandle> part8;
std::vector<QPDFObjectHandle> part9;
// Optimization data
std::map<ObjUser, std::set<QPDFObjGen>> obj_user_to_objects;
std::map<QPDFObjGen, std::set<ObjUser>> object_to_obj_users;
};
// The Resolver class is restricted to QPDFObject and BaseHandle so that only it can resolve
// indirect references.
class QPDF::Doc::Resolver
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
private:
static std::shared_ptr<QPDFObject> const&
resolved(QPDF* qpdf, QPDFObjGen og)
{
return qpdf->m->objects.resolve(og);
}
};
inline bool
QPDF::Doc::reconstructed_xref() const
{
return m.reconstructed_xref;
}
inline QPDF::Doc&
QPDF::doc()
{
return m->doc;
}
// Throw a generic exception for unusual error conditions that do not be covered during CI testing.
inline void
QPDF::no_ci_stop_if(bool condition, std::string const& message, std::string const& context)
{
if (condition) {
throw damagedPDF(context, message);
}
}
#endif // QPDF_PRIVATE_HH