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 793 class Writer;
794 794 class Resolver;
795 795 class StreamCopier;
796   - class ParseGuard;
797 796 class Pipe;
798 797 class JobSetter;
799 798  
... ...
libqpdf/QPDFParser.cc
... ... @@ -15,6 +15,36 @@ using namespace qpdf;
15 15  
16 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 48 QPDFObjectHandle
19 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 156 // effect of reading the object and changing the file pointer. If you do this, it will cause a
127 157 // logic error to be thrown from QPDF::inParse().
128 158  
129   - QPDF::ParseGuard pg(context);
  159 + ParseGuard pg(context);
130 160 empty = false;
131 161 start = input.tell();
132 162  
... ... @@ -262,7 +292,7 @@ QPDFParser::parseRemainder(bool content_stream)
262 292 auto id = QIntC::to_int(int_buffer[(int_count - 1) % 2]);
263 293 auto gen = QIntC::to_int(int_buffer[(int_count) % 2]);
264 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 296 } else {
267 297 QTC::TC("qpdf", "QPDFParser invalid objgen");
268 298 addNull();
... ...
libqpdf/qpdf/QPDF_private.hh
... ... @@ -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 44 // Pipe class is restricted to QPDF_Stream.
75 45 class QPDF::Pipe
76 46 {
... ... @@ -412,6 +382,8 @@ class QPDF::PatternFinder final: public InputSource::Finder
412 382 class QPDF::Doc
413 383 {
414 384 public:
  385 + class ParseGuard;
  386 +
415 387 Doc() = delete;
416 388 Doc(Doc const&) = delete;
417 389 Doc(Doc&&) = delete;
... ...