Commit 0be4a8e7a8ee8b227611224a0e36ffe013360f9d

Authored by m-holger
1 parent 1111240a

Move `ParseGuard` to `QPDF::Doc` and update references

Relocate `ParseGuard` to `QPDF::Doc` for better encapsulation of parsing logic. Adjust references in `QPDFParser` accordingly to use the new placement.
include/qpdf/QPDF.hh
@@ -793,7 +793,6 @@ class QPDF @@ -793,7 +793,6 @@ class QPDF
793 class Writer; 793 class Writer;
794 class Resolver; 794 class Resolver;
795 class StreamCopier; 795 class StreamCopier;
796 - class ParseGuard;  
797 class Pipe; 796 class Pipe;
798 class JobSetter; 797 class JobSetter;
799 798
libqpdf/QPDFParser.cc
@@ -15,6 +15,36 @@ using namespace qpdf; @@ -15,6 +15,36 @@ using namespace qpdf;
15 15
16 using ObjectPtr = std::shared_ptr<QPDFObject>; 16 using ObjectPtr = std::shared_ptr<QPDFObject>;
17 17
  18 +// The ParseGuard class allows QPDFParser to detect re-entrant parsing. It also provides
  19 +// special access to allow the parser to create unresolved objects and dangling references.
  20 +class QPDF::Doc::ParseGuard
  21 +{
  22 + public:
  23 + ParseGuard(QPDF* qpdf) :
  24 + qpdf(qpdf)
  25 + {
  26 + if (qpdf) {
  27 + qpdf->inParse(true);
  28 + }
  29 + }
  30 +
  31 + static std::shared_ptr<QPDFObject>
  32 + getObject(QPDF* qpdf, int id, int gen, bool parse_pdf)
  33 + {
  34 + return qpdf->getObjectForParser(id, gen, parse_pdf);
  35 + }
  36 +
  37 + ~ParseGuard()
  38 + {
  39 + if (qpdf) {
  40 + qpdf->inParse(false);
  41 + }
  42 + }
  43 + QPDF* qpdf;
  44 +};
  45 +
  46 +using ParseGuard = QPDF::Doc::ParseGuard;
  47 +
18 QPDFObjectHandle 48 QPDFObjectHandle
19 QPDFParser::parse(InputSource& input, std::string const& object_description, QPDF* context) 49 QPDFParser::parse(InputSource& input, std::string const& object_description, QPDF* context)
20 { 50 {
@@ -126,7 +156,7 @@ QPDFParser::parse(bool&amp; empty, bool content_stream) @@ -126,7 +156,7 @@ QPDFParser::parse(bool&amp; empty, bool content_stream)
126 // effect of reading the object and changing the file pointer. If you do this, it will cause a 156 // effect of reading the object and changing the file pointer. If you do this, it will cause a
127 // logic error to be thrown from QPDF::inParse(). 157 // logic error to be thrown from QPDF::inParse().
128 158
129 - QPDF::ParseGuard pg(context); 159 + ParseGuard pg(context);
130 empty = false; 160 empty = false;
131 start = input.tell(); 161 start = input.tell();
132 162
@@ -262,7 +292,7 @@ QPDFParser::parseRemainder(bool content_stream) @@ -262,7 +292,7 @@ QPDFParser::parseRemainder(bool content_stream)
262 auto id = QIntC::to_int(int_buffer[(int_count - 1) % 2]); 292 auto id = QIntC::to_int(int_buffer[(int_count - 1) % 2]);
263 auto gen = QIntC::to_int(int_buffer[(int_count) % 2]); 293 auto gen = QIntC::to_int(int_buffer[(int_count) % 2]);
264 if (!(id < 1 || gen < 0 || gen >= 65535)) { 294 if (!(id < 1 || gen < 0 || gen >= 65535)) {
265 - add(QPDF::ParseGuard::getObject(context, id, gen, parse_pdf)); 295 + add(ParseGuard::getObject(context, id, gen, parse_pdf));
266 } else { 296 } else {
267 QTC::TC("qpdf", "QPDFParser invalid objgen"); 297 QTC::TC("qpdf", "QPDFParser invalid objgen");
268 addNull(); 298 addNull();
libqpdf/qpdf/QPDF_private.hh
@@ -41,36 +41,6 @@ class QPDF::StreamCopier @@ -41,36 +41,6 @@ class QPDF::StreamCopier
41 } 41 }
42 }; 42 };
43 43
44 -// The ParseGuard class allows QPDFParser to detect re-entrant parsing. It also provides  
45 -// special access to allow the parser to create unresolved objects and dangling references.  
46 -class QPDF::ParseGuard  
47 -{  
48 - friend class QPDFParser;  
49 -  
50 - private:  
51 - ParseGuard(QPDF* qpdf) :  
52 - qpdf(qpdf)  
53 - {  
54 - if (qpdf) {  
55 - qpdf->inParse(true);  
56 - }  
57 - }  
58 -  
59 - static std::shared_ptr<QPDFObject>  
60 - getObject(QPDF* qpdf, int id, int gen, bool parse_pdf)  
61 - {  
62 - return qpdf->getObjectForParser(id, gen, parse_pdf);  
63 - }  
64 -  
65 - ~ParseGuard()  
66 - {  
67 - if (qpdf) {  
68 - qpdf->inParse(false);  
69 - }  
70 - }  
71 - QPDF* qpdf;  
72 -};  
73 -  
74 // Pipe class is restricted to QPDF_Stream. 44 // Pipe class is restricted to QPDF_Stream.
75 class QPDF::Pipe 45 class QPDF::Pipe
76 { 46 {
@@ -412,6 +382,8 @@ class QPDF::PatternFinder final: public InputSource::Finder @@ -412,6 +382,8 @@ class QPDF::PatternFinder final: public InputSource::Finder
412 class QPDF::Doc 382 class QPDF::Doc
413 { 383 {
414 public: 384 public:
  385 + class ParseGuard;
  386 +
415 Doc() = delete; 387 Doc() = delete;
416 Doc(Doc const&) = delete; 388 Doc(Doc const&) = delete;
417 Doc(Doc&&) = delete; 389 Doc(Doc&&) = delete;