2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-05 08:02:11 +00:00

QPPFObjGen : tidy QPDF::readObjectAtOffset

Change method signature to use QPDFObjGen.
This commit is contained in:
m-holger 2022-07-23 13:47:18 +01:00
parent 6ea17d1f0e
commit c0168cf88c
3 changed files with 26 additions and 50 deletions

View File

@ -1146,10 +1146,8 @@ class QPDF
bool attempt_recovery, bool attempt_recovery,
qpdf_offset_t offset, qpdf_offset_t offset,
std::string const& description, std::string const& description,
int exp_objid, QPDFObjGen const& exp_og,
int exp_generation, QPDFObjGen& og);
int& act_objid,
int& act_generation);
bool objectChanged(QPDFObjGen const& og, std::shared_ptr<QPDFObject>& oph); bool objectChanged(QPDFObjGen const& og, std::shared_ptr<QPDFObject>& oph);
std::shared_ptr<QPDFObject> resolve(int objid, int generation); std::shared_ptr<QPDFObject> resolve(int objid, int generation);
void resolveObjectsInStream(int obj_stream_number); void resolveObjectsInStream(int obj_stream_number);

View File

@ -1055,12 +1055,11 @@ QPDF::read_xrefStream(qpdf_offset_t xref_offset)
{ {
bool found = false; bool found = false;
if (!this->m->ignore_xref_streams) { if (!this->m->ignore_xref_streams) {
int xobj; QPDFObjGen x_og;
int xgen;
QPDFObjectHandle xref_obj; QPDFObjectHandle xref_obj;
try { try {
xref_obj = readObjectAtOffset( xref_obj = readObjectAtOffset(
false, xref_offset, "xref stream", 0, 0, xobj, xgen); false, xref_offset, "xref stream", QPDFObjGen(), x_og);
} catch (QPDFExc&) { } catch (QPDFExc&) {
// ignore -- report error below // ignore -- report error below
} }
@ -1776,13 +1775,11 @@ QPDF::readObjectAtOffset(
bool try_recovery, bool try_recovery,
qpdf_offset_t offset, qpdf_offset_t offset,
std::string const& description, std::string const& description,
int exp_objid, QPDFObjGen const& exp_og,
int exp_generation, QPDFObjGen& og)
int& objid,
int& generation)
{ {
bool check_og = true; bool check_og = true;
if (exp_objid == 0) { if (exp_og.getObj() == 0) {
// This method uses an expect object ID of 0 to indicate that // This method uses an expect object ID of 0 to indicate that
// we don't know or don't care what the actual object ID is at // we don't know or don't care what the actual object ID is at
// this offset. This is true when we read the xref stream and // this offset. This is true when we read the xref stream and
@ -1794,7 +1791,7 @@ QPDF::readObjectAtOffset(
check_og = false; check_og = false;
try_recovery = false; try_recovery = false;
} else { } else {
setLastObjectDescription(description, exp_objid, exp_generation); setLastObjectDescription(description, exp_og.getObj(), exp_og.getGen());
} }
if (!this->m->attempt_recovery) { if (!this->m->attempt_recovery) {
@ -1840,9 +1837,9 @@ QPDF::readObjectAtOffset(
offset, offset,
"expected n n obj"); "expected n n obj");
} }
objid = QUtil::string_to_int(tobjid.getValue().c_str()); int objid = QUtil::string_to_int(tobjid.getValue().c_str());
generation = QUtil::string_to_int(tgen.getValue().c_str()); int generation = QUtil::string_to_int(tgen.getValue().c_str());
og = QPDFObjGen(objid, generation);
if (objid == 0) { if (objid == 0) {
QTC::TC("qpdf", "QPDF object id 0"); QTC::TC("qpdf", "QPDF object id 0");
throw QPDFExc( throw QPDFExc(
@ -1852,17 +1849,14 @@ QPDF::readObjectAtOffset(
offset, offset,
"object with ID 0"); "object with ID 0");
} }
if (check_og && (exp_og != og)) {
if (check_og &&
(!((objid == exp_objid) && (generation == exp_generation)))) {
QTC::TC("qpdf", "QPDF err wrong objid/generation"); QTC::TC("qpdf", "QPDF err wrong objid/generation");
QPDFExc e( QPDFExc e(
qpdf_e_damaged_pdf, qpdf_e_damaged_pdf,
this->m->file->getName(), this->m->file->getName(),
this->m->last_object_description, this->m->last_object_description,
offset, offset,
(std::string("expected ") + (std::string("expected ") + exp_og.unparse(' ') + " obj"));
QPDFObjGen(exp_objid, exp_generation).unparse(' ') + " obj"));
if (try_recovery) { if (try_recovery) {
// Will be retried below // Will be retried below
throw e; throw e;
@ -1876,18 +1870,12 @@ QPDF::readObjectAtOffset(
if (try_recovery) { if (try_recovery) {
// Try again after reconstructing xref table // Try again after reconstructing xref table
reconstruct_xref(e); reconstruct_xref(e);
QPDFObjGen og(exp_objid, exp_generation); if (this->m->xref_table.count(exp_og) &&
if (this->m->xref_table.count(og) && (this->m->xref_table[exp_og].getType() == 1)) {
(this->m->xref_table[og].getType() == 1)) { qpdf_offset_t new_offset =
qpdf_offset_t new_offset = this->m->xref_table[og].getOffset(); this->m->xref_table[exp_og].getOffset();
QPDFObjectHandle result = readObjectAtOffset( QPDFObjectHandle result = readObjectAtOffset(
false, false, new_offset, description, exp_og, og);
new_offset,
description,
exp_objid,
exp_generation,
objid,
generation);
QTC::TC("qpdf", "QPDF recovered in readObjectAtOffset"); QTC::TC("qpdf", "QPDF recovered in readObjectAtOffset");
return result; return result;
} else { } else {
@ -1897,8 +1885,7 @@ QPDF::readObjectAtOffset(
"", "",
0, 0,
std::string( std::string(
"object " + "object " + exp_og.unparse(' ') +
QPDFObjGen(exp_objid, exp_generation).unparse(' ') +
" not found in file after regenerating" " not found in file after regenerating"
" cross reference table")); " cross reference table"));
return QPDFObjectHandle::newNull(); return QPDFObjectHandle::newNull();
@ -1909,7 +1896,7 @@ QPDF::readObjectAtOffset(
} }
QPDFObjectHandle oh = QPDFObjectHandle oh =
readObject(this->m->file, description, objid, generation, false); readObject(this->m->file, description, og.getObj(), og.getGen(), false);
if (!(readToken(this->m->file) == if (!(readToken(this->m->file) ==
QPDFTokenizer::Token(QPDFTokenizer::tt_word, "endobj"))) { QPDFTokenizer::Token(QPDFTokenizer::tt_word, "endobj"))) {
@ -1921,7 +1908,6 @@ QPDF::readObjectAtOffset(
"expected endobj"); "expected endobj");
} }
QPDFObjGen og(objid, generation);
if (!this->m->obj_cache.count(og)) { if (!this->m->obj_cache.count(og)) {
// Store the object in the cache here so it gets cached // Store the object in the cache here so it gets cached
// whether we first know the offset or whether we first know // whether we first know the offset or whether we first know
@ -2014,16 +2000,9 @@ QPDF::resolve(int objid, int generation)
{ {
qpdf_offset_t offset = entry.getOffset(); qpdf_offset_t offset = entry.getOffset();
// Object stored in cache by readObjectAtOffset // Object stored in cache by readObjectAtOffset
int aobjid; QPDFObjGen a_og;
int ageneration; QPDFObjectHandle oh =
QPDFObjectHandle oh = readObjectAtOffset( readObjectAtOffset(true, offset, "", og, a_og);
true,
offset,
"",
objid,
generation,
aobjid,
ageneration);
} }
break; break;

View File

@ -325,11 +325,10 @@ QPDF::readLinearizationData()
QPDFObjectHandle QPDFObjectHandle
QPDF::readHintStream(Pipeline& pl, qpdf_offset_t offset, size_t length) QPDF::readHintStream(Pipeline& pl, qpdf_offset_t offset, size_t length)
{ {
int obj; QPDFObjGen og;
int gen;
QPDFObjectHandle H = readObjectAtOffset( QPDFObjectHandle H = readObjectAtOffset(
false, offset, "linearization hint stream", 0, 0, obj, gen); false, offset, "linearization hint stream", QPDFObjGen(), og);
ObjCache& oc = this->m->obj_cache[QPDFObjGen(obj, gen)]; ObjCache& oc = this->m->obj_cache[og];
qpdf_offset_t min_end_offset = oc.end_before_space; qpdf_offset_t min_end_offset = oc.end_before_space;
qpdf_offset_t max_end_offset = oc.end_after_space; qpdf_offset_t max_end_offset = oc.end_after_space;
if (!H.isStream()) { if (!H.isStream()) {