Commit c0cd72a3eef996e597ba82b784de029ec6d88707

Authored by m-holger
1 parent 23d50a2f

Add private methods QPDF::isCached and QPDF::isUnresolved

include/qpdf/QPDF.hh
@@ -1175,6 +1175,8 @@ class QPDF @@ -1175,6 +1175,8 @@ class QPDF
1175 QPDFObjectHandle reserveStream(QPDFObjGen const& og); 1175 QPDFObjectHandle reserveStream(QPDFObjGen const& og);
1176 QPDFObjectHandle 1176 QPDFObjectHandle
1177 newIndirect(QPDFObjGen const&, std::shared_ptr<QPDFObject> const&); 1177 newIndirect(QPDFObjGen const&, std::shared_ptr<QPDFObject> const&);
  1178 + bool isCached(QPDFObjGen const& og);
  1179 + bool isUnresolved(QPDFObjGen const& og);
1178 1180
1179 // Calls finish() on the pipeline when done but does not delete it 1181 // Calls finish() on the pipeline when done but does not delete it
1180 bool pipeStreamData( 1182 bool pipeStreamData(
include/qpdf/QPDFObject.hh
@@ -134,7 +134,11 @@ class QPDFObject @@ -134,7 +134,11 @@ class QPDFObject
134 value = o->value; 134 value = o->value;
135 o->value = v; 135 o->value = v;
136 } 136 }
137 - 137 + bool
  138 + isUnresolved()
  139 + {
  140 + return value->type_code == ::ot_unresolved;
  141 + }
138 template <typename T> 142 template <typename T>
139 T* 143 T*
140 as() 144 as()
libqpdf/QPDF.cc
@@ -1425,8 +1425,7 @@ QPDF::fixDanglingReferences(bool force) @@ -1425,8 +1425,7 @@ QPDF::fixDanglingReferences(bool force)
1425 for (auto sub: to_check) { 1425 for (auto sub: to_check) {
1426 if (sub.isIndirect()) { 1426 if (sub.isIndirect()) {
1427 if (sub.getOwningQPDF() == this) { 1427 if (sub.getOwningQPDF() == this) {
1428 - QPDFObjGen og(sub.getObjGen());  
1429 - if (this->m->obj_cache.count(og) == 0) { 1428 + if (!isCached(sub.getObjGen())) {
1430 QTC::TC("qpdf", "QPDF detected dangling ref"); 1429 QTC::TC("qpdf", "QPDF detected dangling ref");
1431 queue.push_back(sub); 1430 queue.push_back(sub);
1432 } 1431 }
@@ -1886,7 +1885,7 @@ QPDF::readObjectAtOffset( @@ -1886,7 +1885,7 @@ QPDF::readObjectAtOffset(
1886 "expected endobj"); 1885 "expected endobj");
1887 } 1886 }
1888 1887
1889 - if (!this->m->obj_cache.count(og)) { 1888 + if (!isCached(og)) {
1890 // Store the object in the cache here so it gets cached 1889 // Store the object in the cache here so it gets cached
1891 // whether we first know the offset or whether we first know 1890 // whether we first know the offset or whether we first know
1892 // the object ID and generation (in which we case we would get 1891 // the object ID and generation (in which we case we would get
@@ -1947,7 +1946,7 @@ QPDF::resolve(QPDFObjGen const&amp; og) @@ -1947,7 +1946,7 @@ QPDF::resolve(QPDFObjGen const&amp; og)
1947 } 1946 }
1948 ResolveRecorder rr(this, og); 1947 ResolveRecorder rr(this, og);
1949 1948
1950 - if ((!this->m->obj_cache.count(og)) && this->m->xref_table.count(og)) { 1949 + if ((!isCached(og)) && this->m->xref_table.count(og)) {
1951 QPDFXRefEntry const& entry = this->m->xref_table[og]; 1950 QPDFXRefEntry const& entry = this->m->xref_table[og];
1952 try { 1951 try {
1953 switch (entry.getType()) { 1952 switch (entry.getType()) {
@@ -1985,7 +1984,7 @@ QPDF::resolve(QPDFObjGen const&amp; og) @@ -1985,7 +1984,7 @@ QPDF::resolve(QPDFObjGen const&amp; og)
1985 ": error reading object: " + e.what())); 1984 ": error reading object: " + e.what()));
1986 } 1985 }
1987 } 1986 }
1988 - if (this->m->obj_cache.count(og) == 0) { 1987 + if (!isCached(og)) {
1989 // PDF spec says unknown objects resolve to the null object. 1988 // PDF spec says unknown objects resolve to the null object.
1990 QTC::TC("qpdf", "QPDF resolve failure to null"); 1989 QTC::TC("qpdf", "QPDF resolve failure to null");
1991 QPDFObjectHandle oh = QPDFObjectHandle::newNull(); 1990 QPDFObjectHandle oh = QPDFObjectHandle::newNull();
@@ -2112,6 +2111,18 @@ QPDF::newIndirect(QPDFObjGen const&amp; og, std::shared_ptr&lt;QPDFObject&gt; const&amp; obj) @@ -2112,6 +2111,18 @@ QPDF::newIndirect(QPDFObjGen const&amp; og, std::shared_ptr&lt;QPDFObject&gt; const&amp; obj)
2112 return QPDFObjectHandle::Factory::newIndirect(this, og, obj); 2111 return QPDFObjectHandle::Factory::newIndirect(this, og, obj);
2113 } 2112 }
2114 2113
  2114 +bool
  2115 +QPDF::isCached(QPDFObjGen const& og)
  2116 +{
  2117 + return m->obj_cache.count(og) != 0;
  2118 +}
  2119 +
  2120 +bool
  2121 +QPDF::isUnresolved(QPDFObjGen const& og)
  2122 +{
  2123 + return !isCached(og) || m->obj_cache[og].object->isUnresolved();
  2124 +}
  2125 +
2115 QPDFObjectHandle 2126 QPDFObjectHandle
2116 QPDF::makeIndirectObject(QPDFObjectHandle oh) 2127 QPDF::makeIndirectObject(QPDFObjectHandle oh)
2117 { 2128 {
@@ -2129,7 +2140,7 @@ QPDF::makeIndirectObject(QPDFObjectHandle oh) @@ -2129,7 +2140,7 @@ QPDF::makeIndirectObject(QPDFObjectHandle oh)
2129 QPDFObjectHandle 2140 QPDFObjectHandle
2130 QPDF::reserveObjectIfNotExists(QPDFObjGen const& og) 2141 QPDF::reserveObjectIfNotExists(QPDFObjGen const& og)
2131 { 2142 {
2132 - if ((!m->obj_cache.count(og)) && (!m->xref_table.count(og))) { 2143 + if (!isCached(og) && !m->xref_table.count(og)) {
2133 resolve(og); 2144 resolve(og);
2134 m->obj_cache[og].object = QPDF_Reserved::create(); 2145 m->obj_cache[og].object = QPDF_Reserved::create();
2135 return newIndirect(og, m->obj_cache[og].object); 2146 return newIndirect(og, m->obj_cache[og].object);
libqpdf/QPDFObjectHandle.cc
@@ -2609,7 +2609,7 @@ QPDFObjectHandle::dereference() @@ -2609,7 +2609,7 @@ QPDFObjectHandle::dereference()
2609 if (!isInitialized()) { 2609 if (!isInitialized()) {
2610 return false; 2610 return false;
2611 } 2611 }
2612 - if (this->obj->getTypeCode() == QPDFObject::ot_unresolved) { 2612 + if (this->obj->isUnresolved()) {
2613 this->obj = QPDF::Resolver::resolve(this->qpdf, getObjGen()); 2613 this->obj = QPDF::Resolver::resolve(this->qpdf, getObjGen());
2614 } 2614 }
2615 return true; 2615 return true;