2012-06-25 14:51:44 +00:00
|
|
|
#include <qpdf/qpdf-config.h> // include first for large file support
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2013-11-29 15:20:50 +00:00
|
|
|
#include <algorithm>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <memory.h>
|
|
|
|
|
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/Pipeline.hh>
|
2009-03-08 19:00:19 +00:00
|
|
|
#include <qpdf/Pl_Discard.hh>
|
2012-07-21 09:37:14 +00:00
|
|
|
#include <qpdf/FileInputSource.hh>
|
|
|
|
#include <qpdf/BufferInputSource.hh>
|
2012-12-25 19:38:18 +00:00
|
|
|
#include <qpdf/OffsetInputSource.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
#include <qpdf/QPDFExc.hh>
|
|
|
|
#include <qpdf/QPDF_Null.hh>
|
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
|
2018-02-04 23:31:42 +00:00
|
|
|
std::string QPDF::qpdf_version = "7.1.1";
|
2009-10-24 04:47:17 +00:00
|
|
|
|
2012-06-22 03:06:48 +00:00
|
|
|
static char const* EMPTY_PDF =
|
|
|
|
"%PDF-1.3\n"
|
|
|
|
"1 0 obj\n"
|
|
|
|
"<< /Type /Catalog /Pages 2 0 R >>\n"
|
|
|
|
"endobj\n"
|
|
|
|
"2 0 obj\n"
|
|
|
|
"<< /Type /Pages /Kids [] /Count 0 >>\n"
|
|
|
|
"endobj\n"
|
|
|
|
"xref\n"
|
|
|
|
"0 3\n"
|
|
|
|
"0000000000 65535 f \n"
|
|
|
|
"0000000009 00000 n \n"
|
|
|
|
"0000000058 00000 n \n"
|
|
|
|
"trailer << /Size 3 /Root 1 0 R >>\n"
|
|
|
|
"startxref\n"
|
|
|
|
"110\n"
|
|
|
|
"%%EOF\n";
|
|
|
|
|
2012-07-11 19:29:41 +00:00
|
|
|
void
|
|
|
|
QPDF::CopiedStreamDataProvider::provideStreamData(
|
|
|
|
int objid, int generation, Pipeline* pipeline)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle foreign_stream =
|
2013-06-14 15:22:04 +00:00
|
|
|
this->foreign_streams[QPDFObjGen(objid, generation)];
|
2017-08-19 13:18:14 +00:00
|
|
|
foreign_stream.pipeStreamData(pipeline, 0, qpdf_dl_none);
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::CopiedStreamDataProvider::registerForeignStream(
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen const& local_og, QPDFObjectHandle foreign_stream)
|
2012-07-11 19:29:41 +00:00
|
|
|
{
|
|
|
|
this->foreign_streams[local_og] = foreign_stream;
|
|
|
|
}
|
|
|
|
|
2012-07-21 13:00:06 +00:00
|
|
|
QPDF::StringDecrypter::StringDecrypter(QPDF* qpdf, int objid, int gen) :
|
|
|
|
qpdf(qpdf),
|
|
|
|
objid(objid),
|
|
|
|
gen(gen)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::StringDecrypter::decryptString(std::string& val)
|
|
|
|
{
|
|
|
|
qpdf->decryptString(val, objid, gen);
|
|
|
|
}
|
2012-07-11 19:29:41 +00:00
|
|
|
|
2009-10-24 04:47:17 +00:00
|
|
|
std::string const&
|
|
|
|
QPDF::QPDFVersion()
|
|
|
|
{
|
|
|
|
return QPDF::qpdf_version;
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDF::Members::Members() :
|
2018-01-14 15:17:17 +00:00
|
|
|
provided_password_is_hex_key(false),
|
2008-04-29 12:55:25 +00:00
|
|
|
encrypted(false),
|
|
|
|
encryption_initialized(false),
|
|
|
|
ignore_xref_streams(false),
|
|
|
|
suppress_warnings(false),
|
2010-10-01 11:02:35 +00:00
|
|
|
out_stream(&std::cout),
|
|
|
|
err_stream(&std::cerr),
|
2008-04-29 12:55:25 +00:00
|
|
|
attempt_recovery(true),
|
2009-10-17 18:54:51 +00:00
|
|
|
encryption_V(0),
|
2012-12-30 02:31:21 +00:00
|
|
|
encryption_R(0),
|
2009-10-17 18:54:51 +00:00
|
|
|
encrypt_metadata(true),
|
2009-10-17 23:37:55 +00:00
|
|
|
cf_stream(e_none),
|
|
|
|
cf_string(e_none),
|
|
|
|
cf_file(e_none),
|
2008-04-29 12:55:25 +00:00
|
|
|
cached_key_objid(0),
|
|
|
|
cached_key_generation(0),
|
2012-07-11 19:29:41 +00:00
|
|
|
pushed_inherited_attributes_to_pages(false),
|
|
|
|
copied_stream_data_provider(0),
|
2017-07-25 14:21:27 +00:00
|
|
|
reconstructed_xref(false),
|
2008-04-29 12:55:25 +00:00
|
|
|
first_xref_item_offset(0),
|
|
|
|
uncompressed_after_compressed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDF::Members::~Members()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDF::QPDF() :
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF::~QPDF()
|
|
|
|
{
|
2010-06-06 18:03:21 +00:00
|
|
|
// If two objects are mutually referential (through each object
|
|
|
|
// having an array or dictionary that contains an indirect
|
|
|
|
// reference to the other), the circular references in the
|
|
|
|
// PointerHolder objects will prevent the objects from being
|
|
|
|
// deleted. Walk through all objects in the object cache, which
|
|
|
|
// is those objects that we read from the file, and break all
|
|
|
|
// resolved references. At this point, obviously no one is still
|
|
|
|
// using the QPDF object, but we'll explicitly clear the xref
|
|
|
|
// table anyway just to prevent any possibility of resolve()
|
|
|
|
// succeeding. Note that we can't break references like this at
|
|
|
|
// any time when the QPDF object is active. If we do, the next
|
|
|
|
// reference will reread the object from the file, which would
|
|
|
|
// have the effect of undoing any modifications that may have been
|
|
|
|
// made to any of the objects.
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.clear();
|
2013-06-14 15:22:04 +00:00
|
|
|
for (std::map<QPDFObjGen, ObjCache>::iterator iter =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache.begin();
|
|
|
|
iter != this->m->obj_cache.end(); ++iter)
|
2010-06-06 13:32:08 +00:00
|
|
|
{
|
|
|
|
QPDFObject::ObjAccessor::releaseResolved(
|
|
|
|
(*iter).second.object.getPointer());
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::processFile(char const* filename, char const* password)
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
FileInputSource* fi = new FileInputSource();
|
|
|
|
fi->setFilename(filename);
|
2012-09-23 21:42:26 +00:00
|
|
|
processInputSource(fi, password);
|
2010-10-01 10:20:38 +00:00
|
|
|
}
|
|
|
|
|
2012-06-21 11:59:56 +00:00
|
|
|
void
|
2012-06-23 22:23:06 +00:00
|
|
|
QPDF::processFile(char const* description, FILE* filep,
|
|
|
|
bool close_file, char const* password)
|
2012-06-21 11:59:56 +00:00
|
|
|
{
|
|
|
|
FileInputSource* fi = new FileInputSource();
|
2012-06-23 22:23:06 +00:00
|
|
|
fi->setFile(description, filep, close_file);
|
2012-09-23 21:42:26 +00:00
|
|
|
processInputSource(fi, password);
|
2012-06-21 11:59:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 10:20:38 +00:00
|
|
|
void
|
|
|
|
QPDF::processMemoryFile(char const* description,
|
|
|
|
char const* buf, size_t length,
|
|
|
|
char const* password)
|
|
|
|
{
|
2012-09-23 21:42:26 +00:00
|
|
|
processInputSource(
|
2013-02-24 02:46:21 +00:00
|
|
|
new BufferInputSource(
|
|
|
|
description,
|
|
|
|
new Buffer(QUtil::unsigned_char_pointer(buf), length),
|
|
|
|
true),
|
2012-09-23 21:42:26 +00:00
|
|
|
password);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::processInputSource(PointerHolder<InputSource> source,
|
|
|
|
char const* password)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file = source;
|
2010-10-01 10:20:38 +00:00
|
|
|
parse(password);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 15:17:17 +00:00
|
|
|
void
|
|
|
|
QPDF::setPasswordIsHexKey(bool val)
|
|
|
|
{
|
|
|
|
this->m->provided_password_is_hex_key = val;
|
|
|
|
}
|
|
|
|
|
2012-06-22 03:06:48 +00:00
|
|
|
void
|
|
|
|
QPDF::emptyPDF()
|
|
|
|
{
|
2012-06-22 13:46:33 +00:00
|
|
|
processMemoryFile("empty PDF", EMPTY_PDF, strlen(EMPTY_PDF));
|
2012-06-22 03:06:48 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::setIgnoreXRefStreams(bool val)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->ignore_xref_streams = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 11:02:35 +00:00
|
|
|
void
|
|
|
|
QPDF::setOutputStreams(std::ostream* out, std::ostream* err)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->out_stream = out ? out : &std::cout;
|
|
|
|
this->m->err_stream = err ? err : &std::cerr;
|
2010-10-01 11:02:35 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::setSuppressWarnings(bool val)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->suppress_warnings = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::setAttemptRecovery(bool val)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->attempt_recovery = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 00:24:44 +00:00
|
|
|
std::vector<QPDFExc>
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF::getWarnings()
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
std::vector<QPDFExc> result = this->m->warnings;
|
|
|
|
this->m->warnings.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-06 02:34:25 +00:00
|
|
|
bool
|
|
|
|
QPDF::findHeader()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_offset_t global_offset = this->m->file->tell();
|
|
|
|
std::string line = this->m->file->readLine(1024);
|
2017-08-06 02:34:25 +00:00
|
|
|
char const* p = line.c_str();
|
|
|
|
if (strncmp(p, "%PDF-", 5) != 0)
|
2010-10-01 10:20:38 +00:00
|
|
|
{
|
2017-08-06 02:34:25 +00:00
|
|
|
throw std::logic_error("findHeader is not looking at %PDF-");
|
2010-10-01 10:20:38 +00:00
|
|
|
}
|
2017-08-06 02:34:25 +00:00
|
|
|
p += 5;
|
|
|
|
std::string version;
|
|
|
|
// Note: The string returned by line.c_str() is always
|
|
|
|
// null-terminated. The code below never overruns the buffer
|
|
|
|
// because a null character always short-circuits further
|
|
|
|
// advancement.
|
|
|
|
bool valid = QUtil::is_digit(*p);
|
|
|
|
if (valid)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-06 02:34:25 +00:00
|
|
|
while (QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
version.append(1, *p++);
|
|
|
|
}
|
|
|
|
if ((*p == '.') && QUtil::is_digit(*(p+1)))
|
|
|
|
{
|
|
|
|
version.append(1, *p++);
|
|
|
|
while (QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
version.append(1, *p++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->pdf_version = version;
|
2012-12-25 19:38:18 +00:00
|
|
|
if (global_offset != 0)
|
|
|
|
{
|
2012-12-31 15:31:38 +00:00
|
|
|
// Empirical evidence strongly suggests that when there is
|
2012-12-25 19:38:18 +00:00
|
|
|
// leading material prior to the PDF header, all explicit
|
|
|
|
// offsets in the file are such that 0 points to the
|
|
|
|
// beginning of the header.
|
|
|
|
QTC::TC("qpdf", "QPDF global offset");
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file = new OffsetInputSource(this->m->file, global_offset);
|
2012-12-25 19:38:18 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-06 02:34:25 +00:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:54:07 +00:00
|
|
|
bool
|
|
|
|
QPDF::findStartxref()
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFTokenizer::Token t = readToken(this->m->file, true);
|
2017-08-05 18:54:07 +00:00
|
|
|
if (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "startxref"))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
t = readToken(this->m->file, true);
|
2017-08-05 18:54:07 +00:00
|
|
|
if (t.getType() == QPDFTokenizer::tt_integer)
|
|
|
|
{
|
|
|
|
// Position in front of offset token
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(this->m->file->getLastOffset(), SEEK_SET);
|
2017-08-05 18:54:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-06 02:34:25 +00:00
|
|
|
void
|
|
|
|
QPDF::parse(char const* password)
|
|
|
|
{
|
|
|
|
if (password)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->provided_password = password;
|
2017-08-06 02:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the header anywhere in the first 1024 bytes of the file.
|
|
|
|
PatternFinder hf(*this, &QPDF::findHeader);
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->file->findFirst("%PDF-", 0, 1024, hf))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF not a pdf file");
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2017-07-28 02:27:00 +00:00
|
|
|
"", 0, "can't find PDF header"));
|
2017-07-29 13:24:43 +00:00
|
|
|
// QPDFWriter writes files that usually require at least
|
|
|
|
// version 1.2 for /FlateDecode
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->pdf_version = "1.2";
|
2017-07-28 02:27:00 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
if (atof(this->m->pdf_version.c_str()) < 1.2)
|
2017-07-28 02:27:00 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->tokenizer.allowPoundAnywhereInName();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PDF spec says %%EOF must be found within the last 1024 bytes of
|
|
|
|
// the file. We add an extra 30 characters to leave room for the
|
|
|
|
// startxref stuff.
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(0, SEEK_END);
|
|
|
|
qpdf_offset_t end_offset = this->m->file->tell();
|
2017-08-05 18:54:07 +00:00
|
|
|
qpdf_offset_t start_offset = (end_offset > 1054 ? end_offset - 1054 : 0);
|
|
|
|
PatternFinder sf(*this, &QPDF::findStartxref);
|
|
|
|
qpdf_offset_t xref_offset = 0;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->file->findLast("startxref", start_offset, 0, sf))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-05 18:54:07 +00:00
|
|
|
xref_offset = QUtil::string_to_ll(
|
2017-08-22 01:33:44 +00:00
|
|
|
readToken(this->m->file).getValue().c_str());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-08-05 18:54:07 +00:00
|
|
|
if (xref_offset == 0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF can't find startxref");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
|
2009-10-19 23:09:19 +00:00
|
|
|
"can't find startxref");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
read_xref(xref_offset);
|
|
|
|
}
|
|
|
|
catch (QPDFExc& e)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->attempt_recovery)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
reconstruct_xref(e);
|
|
|
|
QTC::TC("qpdf", "QPDF reconstructed xref table");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initializeEncryption();
|
2012-12-30 00:00:05 +00:00
|
|
|
findAttachmentStreams();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::warn(QPDFExc const& e)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->warnings.push_back(e);
|
|
|
|
if (! this->m->suppress_warnings)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
*this->m->err_stream
|
|
|
|
<< "WARNING: "
|
|
|
|
<< this->m->warnings.back().what() << std::endl;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::setTrailer(QPDFObjectHandle obj)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->trailer.isInitialized())
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->trailer = obj;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::reconstruct_xref(QPDFExc& e)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->reconstructed_xref)
|
2017-07-25 14:21:27 +00:00
|
|
|
{
|
2017-07-26 18:38:49 +00:00
|
|
|
// Avoid xref reconstruction infinite loops. This is getting
|
|
|
|
// very hard to reproduce because qpdf is throwing many fewer
|
|
|
|
// exceptions while parsing. Most situations are warnings now.
|
2017-07-25 14:21:27 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->reconstructed_xref = true;
|
2017-07-25 14:21:27 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
|
2009-10-19 23:09:19 +00:00
|
|
|
"file is damaged"));
|
2008-04-29 12:55:25 +00:00
|
|
|
warn(e);
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
|
2009-10-19 23:09:19 +00:00
|
|
|
"Attempting to reconstruct cross-reference table"));
|
|
|
|
|
|
|
|
// Delete all references to type 1 (uncompressed) objects
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen> to_delete;
|
|
|
|
for (std::map<QPDFObjGen, QPDFXRefEntry>::iterator iter =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.begin();
|
|
|
|
iter != this->m->xref_table.end(); ++iter)
|
2009-10-19 23:09:19 +00:00
|
|
|
{
|
|
|
|
if (((*iter).second).getType() == 1)
|
|
|
|
{
|
|
|
|
to_delete.insert((*iter).first);
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 15:22:04 +00:00
|
|
|
for (std::set<QPDFObjGen>::iterator iter = to_delete.begin();
|
2009-10-19 23:09:19 +00:00
|
|
|
iter != to_delete.end(); ++iter)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.erase(*iter);
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(0, SEEK_END);
|
|
|
|
qpdf_offset_t eof = this->m->file->tell();
|
|
|
|
this->m->file->seek(0, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
bool in_obj = false;
|
2017-08-10 23:00:06 +00:00
|
|
|
qpdf_offset_t line_start = 0;
|
2017-08-22 14:24:19 +00:00
|
|
|
// Don't allow very long tokens here during recovery.
|
|
|
|
static size_t const MAX_LEN = 100;
|
2017-08-22 01:33:44 +00:00
|
|
|
while (this->m->file->tell() < eof)
|
|
|
|
{
|
|
|
|
this->m->file->findAndSkipNextEOL();
|
|
|
|
qpdf_offset_t next_line_start = this->m->file->tell();
|
|
|
|
this->m->file->seek(line_start, SEEK_SET);
|
2017-08-22 14:24:19 +00:00
|
|
|
QPDFTokenizer::Token t1 = readToken(this->m->file, true, MAX_LEN);
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_offset_t token_start =
|
|
|
|
this->m->file->tell() - t1.getValue().length();
|
2017-08-10 23:00:06 +00:00
|
|
|
if (token_start >= next_line_start)
|
|
|
|
{
|
|
|
|
// don't process yet
|
|
|
|
}
|
|
|
|
else if (in_obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-10 23:00:06 +00:00
|
|
|
if (t1 == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "endobj"))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
in_obj = false;
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 23:00:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (t1.getType() == QPDFTokenizer::tt_integer)
|
|
|
|
{
|
2017-08-22 14:24:19 +00:00
|
|
|
QPDFTokenizer::Token t2 =
|
|
|
|
readToken(this->m->file, true, MAX_LEN);
|
|
|
|
QPDFTokenizer::Token t3 =
|
|
|
|
readToken(this->m->file, true, MAX_LEN);
|
2017-08-10 23:00:06 +00:00
|
|
|
if ((t2.getType() == QPDFTokenizer::tt_integer) &&
|
|
|
|
(t3 == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "obj")))
|
|
|
|
{
|
|
|
|
in_obj = true;
|
2017-08-29 16:27:59 +00:00
|
|
|
int obj = QUtil::string_to_int(t1.getValue().c_str());
|
|
|
|
int gen = QUtil::string_to_int(t2.getValue().c_str());
|
2017-08-10 23:00:06 +00:00
|
|
|
insertXrefEntry(obj, 1, token_start, gen, true);
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
else if ((! this->m->trailer.isInitialized()) &&
|
|
|
|
(t1 == QPDFTokenizer::Token(
|
|
|
|
QPDFTokenizer::tt_word, "trailer")))
|
2017-08-10 23:00:06 +00:00
|
|
|
{
|
|
|
|
QPDFObjectHandle t =
|
2017-08-22 01:33:44 +00:00
|
|
|
readObject(this->m->file, "trailer", 0, 0, false);
|
2017-08-10 23:00:06 +00:00
|
|
|
if (! t.isDictionary())
|
|
|
|
{
|
|
|
|
// Oh well. It was worth a try.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setTrailer(t);
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(next_line_start, SEEK_SET);
|
2017-08-10 23:00:06 +00:00
|
|
|
line_start = next_line_start;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->trailer.isInitialized())
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// We could check the last encountered object to see if it was
|
|
|
|
// an xref stream. If so, we could try to get the trailer
|
|
|
|
// from there. This may make it possible to recover files
|
|
|
|
// with bad startxref pointers even when they have object
|
|
|
|
// streams.
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
|
2009-10-19 23:09:19 +00:00
|
|
|
"unable to find trailer "
|
2009-02-21 02:54:31 +00:00
|
|
|
"dictionary while recovering damaged file");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We could iterate through the objects looking for streams and
|
|
|
|
// try to find objects inside of them, but it's probably not worth
|
|
|
|
// the trouble. Acrobat can't recover files with any errors in an
|
|
|
|
// xref stream, and this would be a real long shot anyway. If we
|
|
|
|
// wanted to do anything that involved looking at stream contents,
|
|
|
|
// we'd also have to call initializeEncryption() here. It's safe
|
|
|
|
// to call it more than once.
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDF::read_xref(qpdf_offset_t xref_offset)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
std::map<int, int> free_table;
|
2017-08-25 23:58:31 +00:00
|
|
|
std::set<qpdf_offset_t> visited;
|
2008-04-29 12:55:25 +00:00
|
|
|
while (xref_offset)
|
|
|
|
{
|
2017-08-25 23:58:31 +00:00
|
|
|
visited.insert(xref_offset);
|
2013-06-15 16:38:25 +00:00
|
|
|
char buf[7];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(xref_offset, SEEK_SET);
|
|
|
|
this->m->file->read(buf, sizeof(buf) - 1);
|
2013-06-15 16:38:25 +00:00
|
|
|
// The PDF spec says xref must be followed by a line
|
|
|
|
// terminator, but files exist in the wild where it is
|
|
|
|
// terminated by arbitrary whitespace.
|
2017-08-10 00:46:02 +00:00
|
|
|
if ((strncmp(buf, "xref", 4) == 0) &&
|
|
|
|
QUtil::is_space(buf[4]))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-15 16:38:25 +00:00
|
|
|
QTC::TC("qpdf", "QPDF xref space",
|
|
|
|
((buf[4] == '\n') ? 0 :
|
|
|
|
(buf[4] == '\r') ? 1 :
|
|
|
|
(buf[4] == ' ') ? 2 : 9999));
|
2017-08-10 00:46:02 +00:00
|
|
|
int skip = 4;
|
|
|
|
// buf is null-terminated, and QUtil::is_space('\0') is
|
|
|
|
// false, so this won't overrun.
|
|
|
|
while (QUtil::is_space(buf[skip]))
|
|
|
|
{
|
|
|
|
++skip;
|
|
|
|
}
|
|
|
|
xref_offset = read_xrefTable(xref_offset + skip);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xref_offset = read_xrefStream(xref_offset);
|
|
|
|
}
|
2017-08-25 23:58:31 +00:00
|
|
|
if (visited.count(xref_offset) != 0)
|
|
|
|
{
|
|
|
|
xref_offset = 0;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->trailer.isInitialized())
|
2012-06-24 19:26:28 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
|
2012-06-24 19:26:28 +00:00
|
|
|
"unable to find trailer while reading xref");
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
int size = this->m->trailer.getKey("/Size").getIntValue();
|
2010-06-05 19:48:32 +00:00
|
|
|
int max_obj = 0;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->xref_table.empty())
|
2010-06-05 19:48:32 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
max_obj = (*(this->m->xref_table.rbegin())).first.getObj();
|
2010-06-05 19:48:32 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->deleted_objects.empty())
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
max_obj = std::max(max_obj, *(this->m->deleted_objects.rbegin()));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
if (size != max_obj + 1)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF xref size mismatch");
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
|
2009-10-19 23:09:19 +00:00
|
|
|
std::string("reported number of objects (") +
|
2008-04-29 12:55:25 +00:00
|
|
|
QUtil::int_to_string(size) +
|
|
|
|
") inconsistent with actual number of objects (" +
|
|
|
|
QUtil::int_to_string(max_obj + 1) + ")"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// We no longer need the deleted_objects table, so go ahead and
|
|
|
|
// clear it out to make sure we never depend on its being set.
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->deleted_objects.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:37:05 +00:00
|
|
|
bool
|
|
|
|
QPDF::parse_xrefFirst(std::string const& line,
|
|
|
|
int& obj, int& num, int& bytes)
|
|
|
|
{
|
|
|
|
// is_space and is_digit both return false on '\0', so this will
|
|
|
|
// not overrun the null-terminated buffer.
|
|
|
|
char const* p = line.c_str();
|
|
|
|
char const* start = line.c_str();
|
|
|
|
|
|
|
|
// Skip zero or more spaces
|
|
|
|
while (QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
// Require digit
|
|
|
|
if (! QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string obj_str;
|
|
|
|
while (QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
obj_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Require space
|
|
|
|
if (! QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Skip spaces
|
|
|
|
while (QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
// Require digit
|
|
|
|
if (! QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string num_str;
|
|
|
|
while (QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
num_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Skip any space including line terminators
|
|
|
|
while (QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
bytes = p - start;
|
2017-08-29 16:27:59 +00:00
|
|
|
obj = QUtil::string_to_int(obj_str.c_str());
|
|
|
|
num = QUtil::string_to_int(num_str.c_str());
|
2017-08-10 23:37:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::parse_xrefEntry(std::string const& line,
|
|
|
|
qpdf_offset_t& f1, int& f2, char& type)
|
|
|
|
{
|
|
|
|
// is_space and is_digit both return false on '\0', so this will
|
|
|
|
// not overrun the null-terminated buffer.
|
|
|
|
char const* p = line.c_str();
|
|
|
|
|
|
|
|
// Skip zero or more spaces. There aren't supposed to be any.
|
|
|
|
bool invalid = false;
|
|
|
|
while (QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
QTC::TC("qpdf", "QPDF ignore first space in xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
// Require digit
|
|
|
|
if (! QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string f1_str;
|
|
|
|
while (QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
f1_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Require space
|
|
|
|
if (! QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (QUtil::is_space(*(p+1)))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF ignore first extra space in xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
// Skip spaces
|
|
|
|
while (QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
// Require digit
|
|
|
|
if (! QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string f2_str;
|
|
|
|
while (QUtil::is_digit(*p))
|
|
|
|
{
|
|
|
|
f2_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Require space
|
|
|
|
if (! QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (QUtil::is_space(*(p+1)))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF ignore second extra space in xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
// Skip spaces
|
|
|
|
while (QUtil::is_space(*p))
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
if ((*p == 'f') || (*p == 'n'))
|
|
|
|
{
|
|
|
|
type = *p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((f1_str.length() != 10) || (f2_str.length() != 5))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF ignore length error xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalid)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2017-08-10 23:37:05 +00:00
|
|
|
"xref table",
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->getLastOffset(),
|
2017-08-10 23:37:05 +00:00
|
|
|
"accepting invalid xref table entry"));
|
|
|
|
}
|
|
|
|
|
|
|
|
f1 = QUtil::string_to_ll(f1_str.c_str());
|
2017-08-29 16:27:59 +00:00
|
|
|
f2 = QUtil::string_to_int(f2_str.c_str());
|
2017-08-10 23:37:05 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-23 19:08:21 +00:00
|
|
|
qpdf_offset_t
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDF::read_xrefTable(qpdf_offset_t xref_offset)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
std::vector<QPDFObjGen> deleted_items;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(xref_offset, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
bool done = false;
|
|
|
|
while (! done)
|
|
|
|
{
|
2013-12-14 20:08:54 +00:00
|
|
|
char linebuf[51];
|
|
|
|
memset(linebuf, 0, sizeof(linebuf));
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->read(linebuf, sizeof(linebuf) - 1);
|
2013-12-14 20:08:54 +00:00
|
|
|
std::string line = linebuf;
|
2017-08-10 23:37:05 +00:00
|
|
|
int obj = 0;
|
|
|
|
int num = 0;
|
|
|
|
int bytes = 0;
|
|
|
|
if (! parse_xrefFirst(line, obj, num, bytes))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF invalid xref");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"xref table", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"xref syntax invalid");
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(this->m->file->getLastOffset() + bytes, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
for (int i = obj; i < obj + num; ++i)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
// This is needed by checkLinearization()
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->first_xref_item_offset = this->m->file->tell();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
std::string xref_entry = this->m->file->readLine(30);
|
2017-08-10 23:37:05 +00:00
|
|
|
// For xref_table, these will always be small enough to be ints
|
|
|
|
qpdf_offset_t f1 = 0;
|
|
|
|
int f2 = 0;
|
|
|
|
char type = '\0';
|
|
|
|
if (! parse_xrefEntry(xref_entry, f1, f2, type))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF invalid xref entry");
|
|
|
|
throw QPDFExc(
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"xref table", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"invalid xref entry (obj=" +
|
|
|
|
QUtil::int_to_string(i) + ")");
|
|
|
|
}
|
|
|
|
if (type == 'f')
|
|
|
|
{
|
|
|
|
// Save deleted items until after we've checked the
|
|
|
|
// XRefStm, if any.
|
2013-06-14 15:22:04 +00:00
|
|
|
deleted_items.push_back(QPDFObjGen(i, f2));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
insertXrefEntry(i, 1, f1, f2);
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_offset_t pos = this->m->file->tell();
|
|
|
|
QPDFTokenizer::Token t = readToken(this->m->file);
|
2008-04-29 12:55:25 +00:00
|
|
|
if (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "trailer"))
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(pos, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set offset to previous xref table if any
|
2009-10-19 23:09:19 +00:00
|
|
|
QPDFObjectHandle cur_trailer =
|
2017-08-22 01:33:44 +00:00
|
|
|
readObject(this->m->file, "trailer", 0, 0, false);
|
2008-04-29 12:55:25 +00:00
|
|
|
if (! cur_trailer.isDictionary())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF missing trailer");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"expected trailer dictionary");
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->trailer.isInitialized())
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
setTrailer(cur_trailer);
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->trailer.hasKey("/Size"))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF trailer lacks size");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"trailer", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"trailer dictionary lacks /Size key");
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->trailer.getKey("/Size").isInteger())
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF trailer size not integer");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"trailer", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"/Size key in trailer dictionary is not "
|
|
|
|
"an integer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur_trailer.hasKey("/XRefStm"))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->ignore_xref_streams)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF ignoring XRefStm in trailer");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cur_trailer.getKey("/XRefStm").isInteger())
|
|
|
|
{
|
|
|
|
// Read the xref stream but disregard any return value
|
|
|
|
// -- we'll use our trailer's /Prev key instead of the
|
|
|
|
// xref stream's.
|
|
|
|
(void) read_xrefStream(
|
|
|
|
cur_trailer.getKey("/XRefStm").getIntValue());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"xref stream", xref_offset,
|
2008-04-29 12:55:25 +00:00
|
|
|
"invalid /XRefStm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle any deleted items now that we've read the /XRefStm.
|
2013-06-14 15:22:04 +00:00
|
|
|
for (std::vector<QPDFObjGen>::iterator iter = deleted_items.begin();
|
2008-04-29 12:55:25 +00:00
|
|
|
iter != deleted_items.end(); ++iter)
|
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen& og = *iter;
|
|
|
|
insertXrefEntry(og.getObj(), 0, 0, og.getGen());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cur_trailer.hasKey("/Prev"))
|
|
|
|
{
|
|
|
|
if (! cur_trailer.getKey("/Prev").isInteger())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF trailer prev not integer");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"trailer", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"/Prev key in trailer dictionary is not "
|
|
|
|
"an integer");
|
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDF prev key in trailer dictionary");
|
|
|
|
xref_offset = cur_trailer.getKey("/Prev").getIntValue();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xref_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xref_offset;
|
|
|
|
}
|
|
|
|
|
2012-06-23 19:08:21 +00:00
|
|
|
qpdf_offset_t
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDF::read_xrefStream(qpdf_offset_t xref_offset)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
bool found = false;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->ignore_xref_streams)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
int xobj;
|
|
|
|
int xgen;
|
|
|
|
QPDFObjectHandle xref_obj;
|
|
|
|
try
|
|
|
|
{
|
2009-10-19 23:09:19 +00:00
|
|
|
xref_obj = readObjectAtOffset(
|
2011-01-31 14:59:42 +00:00
|
|
|
false, xref_offset, "xref stream", -1, 0, xobj, xgen);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-02-25 15:28:34 +00:00
|
|
|
catch (QPDFExc&)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// ignore -- report error below
|
|
|
|
}
|
|
|
|
if (xref_obj.isInitialized() &&
|
|
|
|
xref_obj.isStream() &&
|
|
|
|
xref_obj.getDict().getKey("/Type").isName() &&
|
|
|
|
xref_obj.getDict().getKey("/Type").getName() == "/XRef")
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF found xref stream");
|
|
|
|
found = true;
|
|
|
|
xref_offset = processXRefStream(xref_offset, xref_obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! found)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF can't find xref");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"", xref_offset, "xref not found");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return xref_offset;
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle& xref_obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QPDFObjectHandle dict = xref_obj.getDict();
|
|
|
|
QPDFObjectHandle W_obj = dict.getKey("/W");
|
|
|
|
QPDFObjectHandle Index_obj = dict.getKey("/Index");
|
|
|
|
if (! (W_obj.isArray() &&
|
|
|
|
(W_obj.getArrayNItems() >= 3) &&
|
|
|
|
W_obj.getArrayItem(0).isInteger() &&
|
|
|
|
W_obj.getArrayItem(1).isInteger() &&
|
|
|
|
W_obj.getArrayItem(2).isInteger() &&
|
|
|
|
dict.getKey("/Size").isInteger() &&
|
|
|
|
(Index_obj.isArray() || Index_obj.isNull())))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"xref stream", xref_offset,
|
2008-04-29 12:55:25 +00:00
|
|
|
"Cross-reference stream does not have"
|
|
|
|
" proper /W and /Index keys");
|
|
|
|
}
|
2013-10-05 16:28:52 +00:00
|
|
|
|
|
|
|
int W[3];
|
|
|
|
size_t entry_size = 0;
|
|
|
|
int max_bytes = sizeof(qpdf_offset_t);
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
W[i] = W_obj.getArrayItem(i).getIntValue();
|
|
|
|
if (W[i] > max_bytes)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2013-10-05 16:28:52 +00:00
|
|
|
"xref stream", xref_offset,
|
|
|
|
"Cross-reference stream's /W contains"
|
|
|
|
" impossibly large values");
|
|
|
|
}
|
|
|
|
entry_size += W[i];
|
|
|
|
}
|
2017-08-12 00:10:28 +00:00
|
|
|
if (entry_size == 0)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2017-08-12 00:10:28 +00:00
|
|
|
"xref stream", xref_offset,
|
|
|
|
"Cross-reference stream's /W indicates"
|
|
|
|
" entry size of 0");
|
|
|
|
}
|
2013-10-05 16:28:52 +00:00
|
|
|
long long max_num_entries =
|
|
|
|
static_cast<unsigned long long>(-1) / entry_size;
|
|
|
|
|
|
|
|
std::vector<long long> indx;
|
2008-04-29 12:55:25 +00:00
|
|
|
if (Index_obj.isArray())
|
|
|
|
{
|
|
|
|
int n_index = Index_obj.getArrayNItems();
|
|
|
|
if ((n_index % 2) || (n_index < 2))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"xref stream", xref_offset,
|
2008-04-29 12:55:25 +00:00
|
|
|
"Cross-reference stream's /Index has an"
|
|
|
|
" invalid number of values");
|
|
|
|
}
|
|
|
|
for (int i = 0; i < n_index; ++i)
|
|
|
|
{
|
|
|
|
if (Index_obj.getArrayItem(i).isInteger())
|
|
|
|
{
|
|
|
|
indx.push_back(Index_obj.getArrayItem(i).getIntValue());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"xref stream", xref_offset,
|
2008-04-29 12:55:25 +00:00
|
|
|
"Cross-reference stream's /Index's item " +
|
|
|
|
QUtil::int_to_string(i) +
|
|
|
|
" is not an integer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDF xref /Index is array",
|
|
|
|
n_index == 2 ? 0 : 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF xref /Index is null");
|
2013-10-05 16:28:52 +00:00
|
|
|
long long size = dict.getKey("/Size").getIntValue();
|
2008-04-29 12:55:25 +00:00
|
|
|
indx.push_back(0);
|
|
|
|
indx.push_back(size);
|
|
|
|
}
|
|
|
|
|
2013-10-05 16:28:52 +00:00
|
|
|
long long num_entries = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
for (unsigned int i = 1; i < indx.size(); i += 2)
|
|
|
|
{
|
2013-10-05 23:42:39 +00:00
|
|
|
if (indx.at(i) > max_num_entries - num_entries)
|
2013-10-05 16:28:52 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2013-10-05 16:28:52 +00:00
|
|
|
"xref stream", xref_offset,
|
|
|
|
"Cross-reference stream claims to contain"
|
|
|
|
" too many entries: " +
|
2013-10-05 23:42:39 +00:00
|
|
|
QUtil::int_to_string(indx.at(i)) + " " +
|
2013-10-05 16:28:52 +00:00
|
|
|
QUtil::int_to_string(max_num_entries) + " " +
|
|
|
|
QUtil::int_to_string(num_entries));
|
|
|
|
}
|
2013-10-05 23:42:39 +00:00
|
|
|
num_entries += indx.at(i);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 16:28:52 +00:00
|
|
|
// entry_size and num_entries have both been validated to ensure
|
|
|
|
// that this multiplication does not cause an overflow.
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t expected_size = entry_size * num_entries;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-12-25 00:18:52 +00:00
|
|
|
PointerHolder<Buffer> bp = xref_obj.getStreamData(qpdf_dl_specialized);
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t actual_size = bp->getSize();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
if (expected_size != actual_size)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFExc x(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"xref stream", xref_offset,
|
2009-10-04 23:56:56 +00:00
|
|
|
"Cross-reference stream data has the wrong size;"
|
|
|
|
" expected = " + QUtil::int_to_string(expected_size) +
|
|
|
|
"; actual = " + QUtil::int_to_string(actual_size));
|
|
|
|
if (expected_size > actual_size)
|
|
|
|
{
|
|
|
|
throw x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
warn(x);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cur_chunk = 0;
|
|
|
|
int chunk_count = 0;
|
|
|
|
|
|
|
|
bool saw_first_compressed_object = false;
|
|
|
|
|
2013-10-05 16:28:52 +00:00
|
|
|
// Actual size vs. expected size check above ensures that we will
|
|
|
|
// not overflow any buffers here. We know that entry_size *
|
|
|
|
// num_entries is equal to the size of the buffer.
|
2010-09-24 20:45:18 +00:00
|
|
|
unsigned char const* data = bp->getBuffer();
|
2008-04-29 12:55:25 +00:00
|
|
|
for (int i = 0; i < num_entries; ++i)
|
|
|
|
{
|
|
|
|
// Read this entry
|
|
|
|
unsigned char const* entry = data + (entry_size * i);
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t fields[3];
|
2008-04-29 12:55:25 +00:00
|
|
|
unsigned char const* p = entry;
|
|
|
|
for (int j = 0; j < 3; ++j)
|
|
|
|
{
|
|
|
|
fields[j] = 0;
|
|
|
|
if ((j == 0) && (W[0] == 0))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF default for xref stream field 0");
|
|
|
|
fields[0] = 1;
|
|
|
|
}
|
|
|
|
for (int k = 0; k < W[j]; ++k)
|
|
|
|
{
|
|
|
|
fields[j] <<= 8;
|
2013-02-24 02:46:21 +00:00
|
|
|
fields[j] += static_cast<int>(*p++);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the object and generation number. The object number is
|
|
|
|
// based on /Index. The generation number is 0 unless this is
|
|
|
|
// an uncompressed object record, in which case the generation
|
|
|
|
// number appears as the third field.
|
2013-10-05 23:42:39 +00:00
|
|
|
int obj = indx.at(cur_chunk) + chunk_count;
|
2008-04-29 12:55:25 +00:00
|
|
|
++chunk_count;
|
2013-10-05 23:42:39 +00:00
|
|
|
if (chunk_count >= indx.at(cur_chunk + 1))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
cur_chunk += 2;
|
|
|
|
chunk_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saw_first_compressed_object)
|
|
|
|
{
|
|
|
|
if (fields[0] != 2)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->uncompressed_after_compressed = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fields[0] == 2)
|
|
|
|
{
|
|
|
|
saw_first_compressed_object = true;
|
|
|
|
}
|
|
|
|
if (obj == 0)
|
|
|
|
{
|
|
|
|
// This is needed by checkLinearization()
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->first_xref_item_offset = xref_offset;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-02-24 02:46:21 +00:00
|
|
|
insertXrefEntry(obj, static_cast<int>(fields[0]),
|
|
|
|
fields[1], static_cast<int>(fields[2]));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->trailer.isInitialized())
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
setTrailer(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dict.hasKey("/Prev"))
|
|
|
|
{
|
|
|
|
if (! dict.getKey("/Prev").isInteger())
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"xref stream", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"/Prev key in xref stream dictionary is not "
|
|
|
|
"an integer");
|
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDF prev key in xref stream dictionary");
|
|
|
|
xref_offset = dict.getKey("/Prev").getIntValue();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xref_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xref_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-24 19:26:28 +00:00
|
|
|
QPDF::insertXrefEntry(int obj, int f0, qpdf_offset_t f1, int f2, bool overwrite)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Populate the xref table in such a way that the first reference
|
|
|
|
// to an object that we see, which is the one in the latest xref
|
2009-02-21 02:31:32 +00:00
|
|
|
// table in which it appears, is the one that gets stored. This
|
|
|
|
// works because we are reading more recent appends before older
|
|
|
|
// ones. Exception: if overwrite is true, then replace any
|
|
|
|
// existing object. This is used in xref recovery mode, which
|
|
|
|
// reads the file from beginning to end.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// If there is already an entry for this object and generation in
|
|
|
|
// the table, it means that a later xref table has registered this
|
|
|
|
// object. Disregard this one.
|
|
|
|
{ // private scope
|
|
|
|
int gen = (f0 == 2 ? 0 : f2);
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen og(obj, gen);
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->xref_table.count(og))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-02-21 02:31:32 +00:00
|
|
|
if (overwrite)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF xref overwrite object");
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.erase(og);
|
2009-02-21 02:31:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF xref reused object");
|
|
|
|
return;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->deleted_objects.count(obj))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF xref deleted object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (f0)
|
|
|
|
{
|
|
|
|
case 0:
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->deleted_objects.insert(obj);
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// f2 is generation
|
|
|
|
QTC::TC("qpdf", "QPDF xref gen > 0", ((f2 > 0) ? 1 : 0));
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table[QPDFObjGen(obj, f2)] = QPDFXRefEntry(f0, f1, f2);
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table[QPDFObjGen(obj, 0)] = QPDFXRefEntry(f0, f1, f2);
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"xref stream", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"unknown xref stream entry type " +
|
|
|
|
QUtil::int_to_string(f0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::showXRefTable()
|
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
for (std::map<QPDFObjGen, QPDFXRefEntry>::iterator iter =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.begin();
|
|
|
|
iter != this->m->xref_table.end(); ++iter)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen const& og = (*iter).first;
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFXRefEntry const& entry = (*iter).second;
|
2017-08-22 01:33:44 +00:00
|
|
|
*this->m->out_stream << og.getObj() << "/" << og.getGen() << ": ";
|
2008-04-29 12:55:25 +00:00
|
|
|
switch (entry.getType())
|
|
|
|
{
|
|
|
|
case 1:
|
2017-08-22 01:33:44 +00:00
|
|
|
*this->m->out_stream
|
|
|
|
<< "uncompressed; offset = " << entry.getOffset();
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2017-08-22 01:33:44 +00:00
|
|
|
*this->m->out_stream
|
|
|
|
<< "compressed; stream = "
|
|
|
|
<< entry.getObjStreamNumber()
|
|
|
|
<< ", index = " << entry.getObjStreamIndex();
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error("unknown cross-reference table type while"
|
|
|
|
" showing xref_table");
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
*this->m->out_stream << std::endl;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 23:18:57 +00:00
|
|
|
std::vector<QPDFObjectHandle>
|
|
|
|
QPDF::getAllObjects()
|
|
|
|
{
|
|
|
|
std::vector<QPDFObjectHandle> result;
|
|
|
|
for (std::map<QPDFObjGen, QPDFXRefEntry>::iterator iter =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.begin();
|
|
|
|
iter != this->m->xref_table.end(); ++iter)
|
2017-07-28 23:18:57 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
QPDFObjGen const& og = (*iter).first;
|
|
|
|
result.push_back(QPDFObjectHandle::Factory::newIndirect(
|
|
|
|
this, og.getObj(), og.getGen()));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:09:19 +00:00
|
|
|
void
|
|
|
|
QPDF::setLastObjectDescription(std::string const& description,
|
|
|
|
int objid, int generation)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description.clear();
|
2009-10-19 23:09:19 +00:00
|
|
|
if (! description.empty())
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description += description;
|
2009-10-19 23:09:19 +00:00
|
|
|
if (objid > 0)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description += ": ";
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (objid > 0)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description += "object " +
|
2009-10-19 23:09:19 +00:00
|
|
|
QUtil::int_to_string(objid) + " " +
|
|
|
|
QUtil::int_to_string(generation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
2010-09-24 19:10:08 +00:00
|
|
|
QPDF::readObject(PointerHolder<InputSource> input,
|
|
|
|
std::string const& description,
|
2009-10-19 23:09:19 +00:00
|
|
|
int objid, int generation, bool in_object_stream)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-10-19 23:09:19 +00:00
|
|
|
setLastObjectDescription(description, objid, generation);
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t offset = input->tell();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-07-21 13:00:06 +00:00
|
|
|
bool empty = false;
|
|
|
|
PointerHolder<StringDecrypter> decrypter_ph;
|
|
|
|
StringDecrypter* decrypter = 0;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->encrypted && (! in_object_stream))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-07-21 13:00:06 +00:00
|
|
|
decrypter_ph = new StringDecrypter(this, objid, generation);
|
|
|
|
decrypter = decrypter_ph.getPointer();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-07-21 13:00:06 +00:00
|
|
|
QPDFObjectHandle object = QPDFObjectHandle::parse(
|
2017-08-22 01:33:44 +00:00
|
|
|
input, description, this->m->tokenizer, empty, decrypter, this);
|
2012-07-21 13:00:06 +00:00
|
|
|
if (empty)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-07-21 13:00:06 +00:00
|
|
|
// Nothing in the PDF spec appears to allow empty objects, but
|
|
|
|
// they have been encountered in actual PDF files and Adobe
|
|
|
|
// Reader appears to ignore them.
|
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description,
|
2012-07-21 13:00:06 +00:00
|
|
|
input->getLastOffset(),
|
|
|
|
"empty object treated as null"));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-07-21 13:00:06 +00:00
|
|
|
else if (object.isDictionary() && (! in_object_stream))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-07-21 13:00:06 +00:00
|
|
|
// check for stream
|
|
|
|
qpdf_offset_t cur_offset = input->tell();
|
|
|
|
if (readToken(input) ==
|
|
|
|
QPDFTokenizer::Token(QPDFTokenizer::tt_word, "stream"))
|
|
|
|
{
|
|
|
|
// The PDF specification states that the word "stream"
|
|
|
|
// should be followed by either a carriage return and
|
|
|
|
// a newline or by a newline alone. It specifically
|
|
|
|
// disallowed following it by a carriage return alone
|
|
|
|
// since, in that case, there would be no way to tell
|
|
|
|
// whether the NL in a CR NL sequence was part of the
|
|
|
|
// stream data. However, some readers, including
|
|
|
|
// Adobe reader, accept a carriage return by itself
|
|
|
|
// when followed by a non-newline character, so that's
|
|
|
|
// what we do here.
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
if (input->read(&ch, 1) == 0)
|
|
|
|
{
|
|
|
|
// A premature EOF here will result in some
|
|
|
|
// other problem that will get reported at
|
|
|
|
// another time.
|
|
|
|
}
|
|
|
|
else if (ch == '\n')
|
|
|
|
{
|
|
|
|
// ready to read stream data
|
|
|
|
QTC::TC("qpdf", "QPDF stream with NL only");
|
|
|
|
}
|
|
|
|
else if (ch == '\r')
|
|
|
|
{
|
|
|
|
// Read another character
|
|
|
|
if (input->read(&ch, 1) != 0)
|
|
|
|
{
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
// Ready to read stream data
|
|
|
|
QTC::TC("qpdf", "QPDF stream with CRNL");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Treat the \r by itself as the
|
|
|
|
// whitespace after endstream and
|
|
|
|
// start reading stream data in spite
|
|
|
|
// of not having seen a newline.
|
|
|
|
QTC::TC("qpdf", "QPDF stream with CR only");
|
|
|
|
input->unreadCh(ch);
|
|
|
|
warn(QPDFExc(
|
|
|
|
qpdf_e_damaged_pdf,
|
|
|
|
input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description,
|
2012-07-21 13:00:06 +00:00
|
|
|
input->tell(),
|
|
|
|
"stream keyword followed"
|
|
|
|
" by carriage return only"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF stream without newline");
|
2017-07-29 15:59:15 +00:00
|
|
|
if (! QUtil::is_space(ch))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF stream with non-space");
|
|
|
|
input->unreadCh(ch);
|
|
|
|
}
|
2012-07-21 13:00:06 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description,
|
2012-07-21 13:00:06 +00:00
|
|
|
input->tell(),
|
|
|
|
"stream keyword not followed"
|
|
|
|
" by proper line terminator"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must get offset before accessing any additional
|
|
|
|
// objects since resolving a previously unresolved
|
|
|
|
// indirect object will change file position.
|
|
|
|
qpdf_offset_t stream_offset = input->tell();
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::map<std::string, QPDFObjectHandle> dict =
|
|
|
|
object.getDictAsMap();
|
|
|
|
|
|
|
|
if (dict.count("/Length") == 0)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF stream without length");
|
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description, offset,
|
2012-07-21 13:00:06 +00:00
|
|
|
"stream dictionary lacks /Length key");
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle length_obj = dict["/Length"];
|
|
|
|
if (! length_obj.isInteger())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF stream length not integer");
|
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description, offset,
|
2012-07-21 13:00:06 +00:00
|
|
|
"/Length key in stream dictionary is not "
|
|
|
|
"an integer");
|
|
|
|
}
|
|
|
|
|
|
|
|
length = length_obj.getIntValue();
|
2013-02-24 02:46:21 +00:00
|
|
|
input->seek(stream_offset + length, SEEK_SET);
|
2012-07-21 13:00:06 +00:00
|
|
|
if (! (readToken(input) ==
|
|
|
|
QPDFTokenizer::Token(
|
|
|
|
QPDFTokenizer::tt_word, "endstream")))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF missing endstream");
|
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description,
|
2012-07-21 13:00:06 +00:00
|
|
|
input->getLastOffset(),
|
|
|
|
"expected endstream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (QPDFExc& e)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->attempt_recovery)
|
2012-07-21 13:00:06 +00:00
|
|
|
{
|
2017-07-28 02:27:00 +00:00
|
|
|
warn(e);
|
2012-07-21 13:00:06 +00:00
|
|
|
length = recoverStreamLength(
|
|
|
|
input, objid, generation, stream_offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
object = QPDFObjectHandle::Factory::newStream(
|
|
|
|
this, objid, generation, object, stream_offset, length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
input->seek(cur_offset, SEEK_SET);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 13:00:06 +00:00
|
|
|
// Override last_offset so that it points to the beginning of the
|
|
|
|
// object we just read
|
|
|
|
input->setLastOffset(offset);
|
2008-04-29 12:55:25 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2017-08-10 01:14:48 +00:00
|
|
|
bool
|
|
|
|
QPDF::findEndstream()
|
|
|
|
{
|
|
|
|
// Find endstream or endobj. Position the input at that token.
|
2017-08-22 14:24:19 +00:00
|
|
|
QPDFTokenizer::Token t = readToken(this->m->file, true, 20);
|
2017-08-10 01:14:48 +00:00
|
|
|
if ((t.getType() == QPDFTokenizer::tt_word) &&
|
|
|
|
((t.getValue() == "endobj") ||
|
2017-08-22 11:19:41 +00:00
|
|
|
(t.getValue() == "endstream")))
|
2017-08-10 01:14:48 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(this->m->file->getLastOffset(), SEEK_SET);
|
2017-08-10 01:14:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t
|
2010-09-24 19:10:08 +00:00
|
|
|
QPDF::recoverStreamLength(PointerHolder<InputSource> input,
|
2012-06-21 23:32:21 +00:00
|
|
|
int objid, int generation,
|
|
|
|
qpdf_offset_t stream_offset)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Try to reconstruct stream length by looking for
|
2017-08-10 01:14:48 +00:00
|
|
|
// endstream or endobj
|
2009-10-19 23:09:19 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description, stream_offset,
|
2008-04-29 12:55:25 +00:00
|
|
|
"attempting to recover stream length"));
|
|
|
|
|
2017-08-10 01:14:48 +00:00
|
|
|
PatternFinder ef(*this, &QPDF::findEndstream);
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t length = 0;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->file->findFirst("end", stream_offset, 0, ef))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
length = this->m->file->tell() - stream_offset;
|
2017-08-10 01:14:48 +00:00
|
|
|
// Reread endstream but, if it was endobj, don't skip that.
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFTokenizer::Token t = readToken(this->m->file);
|
2017-08-10 01:14:48 +00:00
|
|
|
if (t.getValue() == "endobj")
|
2012-06-27 03:09:32 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(this->m->file->getLastOffset(), SEEK_SET);
|
2017-08-10 01:14:48 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
{
|
|
|
|
int this_obj_offset = 0;
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen this_obj(0, 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Make sure this is inside this object
|
2013-06-14 15:22:04 +00:00
|
|
|
for (std::map<QPDFObjGen, QPDFXRefEntry>::iterator iter =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.begin();
|
|
|
|
iter != this->m->xref_table.end(); ++iter)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen const& og = (*iter).first;
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFXRefEntry const& entry = (*iter).second;
|
|
|
|
if (entry.getType() == 1)
|
|
|
|
{
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t obj_offset = entry.getOffset();
|
2008-04-29 12:55:25 +00:00
|
|
|
if ((obj_offset > stream_offset) &&
|
|
|
|
((this_obj_offset == 0) ||
|
|
|
|
(this_obj_offset > obj_offset)))
|
|
|
|
{
|
|
|
|
this_obj_offset = obj_offset;
|
|
|
|
this_obj = og;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this_obj_offset &&
|
2013-06-14 15:22:04 +00:00
|
|
|
(this_obj.getObj() == objid) &&
|
|
|
|
(this_obj.getGen() == generation))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Well, we found endstream\nendobj within the space
|
|
|
|
// allowed for this object, so we're probably in good
|
|
|
|
// shape.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF found wrong endstream in recovery");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
{
|
2017-07-28 02:27:00 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description, stream_offset,
|
|
|
|
"unable to recover stream data;"
|
|
|
|
" treating stream as empty"));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-07-29 23:15:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description, stream_offset,
|
2017-07-29 23:15:06 +00:00
|
|
|
"recovered stream length: " +
|
|
|
|
QUtil::int_to_string(length)));
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QTC::TC("qpdf", "QPDF recovered stream length");
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFTokenizer::Token
|
2017-08-22 14:24:19 +00:00
|
|
|
QPDF::readToken(PointerHolder<InputSource> input,
|
|
|
|
bool allow_bad, size_t max_len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->tokenizer.readToken(
|
2017-08-22 14:24:19 +00:00
|
|
|
input, this->m->last_object_description, allow_bad, max_len);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
2009-10-19 23:09:19 +00:00
|
|
|
QPDF::readObjectAtOffset(bool try_recovery,
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t offset, std::string const& description,
|
2009-10-19 23:09:19 +00:00
|
|
|
int exp_objid, int exp_generation,
|
2008-04-29 12:55:25 +00:00
|
|
|
int& objid, int& generation)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->attempt_recovery)
|
2017-07-28 02:27:00 +00:00
|
|
|
{
|
|
|
|
try_recovery = false;
|
|
|
|
}
|
2009-10-19 23:09:19 +00:00
|
|
|
setLastObjectDescription(description, exp_objid, exp_generation);
|
2012-11-20 18:15:14 +00:00
|
|
|
|
|
|
|
// Special case: if offset is 0, just return null. Some PDF
|
2012-12-31 15:31:38 +00:00
|
|
|
// writers, in particular "Mac OS X 10.7.5 Quartz PDFContext", may
|
2012-11-20 18:15:14 +00:00
|
|
|
// store deleted objects in the xref table as "0000000000 00000
|
|
|
|
// n", which is not correct, but it won't hurt anything for to
|
|
|
|
// ignore these.
|
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF bogus 0 offset", 0);
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description, 0,
|
2012-11-20 18:15:14 +00:00
|
|
|
"object has offset 0"));
|
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(offset, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFTokenizer::Token tobjid = readToken(this->m->file);
|
|
|
|
QPDFTokenizer::Token tgen = readToken(this->m->file);
|
|
|
|
QPDFTokenizer::Token tobj = readToken(this->m->file);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
bool objidok = (tobjid.getType() == QPDFTokenizer::tt_integer);
|
|
|
|
int genok = (tgen.getType() == QPDFTokenizer::tt_integer);
|
|
|
|
int objok = (tobj == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "obj"));
|
|
|
|
|
|
|
|
QTC::TC("qpdf", "QPDF check objid", objidok ? 1 : 0);
|
|
|
|
QTC::TC("qpdf", "QPDF check generation", genok ? 1 : 0);
|
|
|
|
QTC::TC("qpdf", "QPDF check obj", objok ? 1 : 0);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (! (objidok && genok && objok))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF expected n n obj");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description, offset,
|
2009-10-19 23:09:19 +00:00
|
|
|
"expected n n obj");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-29 16:27:59 +00:00
|
|
|
objid = QUtil::string_to_int(tobjid.getValue().c_str());
|
|
|
|
generation = QUtil::string_to_int(tgen.getValue().c_str());
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-07-26 08:30:32 +00:00
|
|
|
if (objid == 0)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF object id 0");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description, offset,
|
2017-07-26 08:30:32 +00:00
|
|
|
"object with ID 0");
|
|
|
|
}
|
|
|
|
|
2011-01-31 14:59:42 +00:00
|
|
|
if ((exp_objid >= 0) &&
|
2008-04-29 12:55:25 +00:00
|
|
|
(! ((objid == exp_objid) && (generation == exp_generation))))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF err wrong objid/generation");
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFExc e(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description, offset,
|
2017-07-28 02:27:00 +00:00
|
|
|
std::string("expected ") +
|
|
|
|
QUtil::int_to_string(exp_objid) + " " +
|
|
|
|
QUtil::int_to_string(exp_generation) + " obj");
|
|
|
|
if (try_recovery)
|
|
|
|
{
|
|
|
|
// Will be retried below
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We can try reading the object anyway even if the ID
|
|
|
|
// doesn't match.
|
|
|
|
warn(e);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (QPDFExc& e)
|
|
|
|
{
|
2017-07-28 02:27:00 +00:00
|
|
|
if ((exp_objid >= 0) && try_recovery)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Try again after reconstructing xref table
|
|
|
|
reconstruct_xref(e);
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen og(exp_objid, exp_generation);
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->xref_table.count(og) &&
|
|
|
|
(this->m->xref_table[og].getType() == 1))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_offset_t new_offset = this->m->xref_table[og].getOffset();
|
2009-10-19 23:09:19 +00:00
|
|
|
QPDFObjectHandle result = readObjectAtOffset(
|
|
|
|
false, new_offset, description,
|
|
|
|
exp_objid, exp_generation, objid, generation);
|
2008-04-29 12:55:25 +00:00
|
|
|
QTC::TC("qpdf", "QPDF recovered in readObjectAtOffset");
|
|
|
|
return result;
|
|
|
|
}
|
2009-10-19 23:09:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF object gone after xref reconstruction");
|
|
|
|
warn(QPDFExc(
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_e_damaged_pdf, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"", 0,
|
|
|
|
std::string(
|
|
|
|
"object " +
|
|
|
|
QUtil::int_to_string(exp_objid) +
|
|
|
|
" " +
|
|
|
|
QUtil::int_to_string(exp_generation) +
|
|
|
|
" not found in file after regenerating"
|
|
|
|
" cross reference table")));
|
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle oh = readObject(
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file, description, objid, generation, false);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! (readToken(this->m->file) ==
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFTokenizer::Token(QPDFTokenizer::tt_word, "endobj")))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF err expected endobj");
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"expected endobj"));
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen og(objid, generation);
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->obj_cache.count(og))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Store the object in the cache here so it gets cached
|
|
|
|
// whether we first know the offset or whether we first know
|
|
|
|
// the object ID and generation (in which we case we would get
|
|
|
|
// here through resolve).
|
|
|
|
|
|
|
|
// Determine the end offset of this object before and after
|
|
|
|
// white space. We use these numbers to validate
|
|
|
|
// linearization hint tables. Offsets and lengths of objects
|
|
|
|
// may imply the end of an object to be anywhere between these
|
|
|
|
// values.
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_offset_t end_before_space = this->m->file->tell();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// skip over spaces
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
char ch;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->file->read(&ch, 1))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
if (! isspace(static_cast<unsigned char>(ch)))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(-1, SEEK_CUR);
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
2018-01-28 23:20:46 +00:00
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->tell(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"EOF after endobj");
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_offset_t end_after_space = this->m->file->tell();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache[og] =
|
2008-04-29 12:55:25 +00:00
|
|
|
ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh),
|
|
|
|
end_before_space, end_after_space);
|
|
|
|
}
|
|
|
|
|
|
|
|
return oh;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerHolder<QPDFObject>
|
|
|
|
QPDF::resolve(int objid, int generation)
|
|
|
|
{
|
|
|
|
// Check object cache before checking xref table. This allows us
|
|
|
|
// to insert things into the object cache that don't actually
|
|
|
|
// exist in the file.
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen og(objid, generation);
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->resolving.count(og))
|
2017-07-26 09:03:38 +00:00
|
|
|
{
|
|
|
|
// This can happen if an object references itself directly or
|
|
|
|
// indirectly in some key that has to be resolved during
|
|
|
|
// object parsing, such as stream length.
|
|
|
|
QTC::TC("qpdf", "QPDF recursion loop in resolve");
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"", this->m->file->getLastOffset(),
|
2017-07-26 09:03:38 +00:00
|
|
|
"loop detected resolving object " +
|
|
|
|
QUtil::int_to_string(objid) + " " +
|
|
|
|
QUtil::int_to_string(generation)));
|
|
|
|
return new QPDF_Null;
|
|
|
|
}
|
|
|
|
ResolveRecorder rr(this, og);
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->obj_cache.count(og))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->xref_table.count(og))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// PDF spec says unknown objects resolve to the null object.
|
|
|
|
return new QPDF_Null;
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFXRefEntry const& entry = this->m->xref_table[og];
|
2017-07-29 23:31:12 +00:00
|
|
|
bool success = false;
|
2017-07-28 02:27:00 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (entry.getType())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
qpdf_offset_t offset = entry.getOffset();
|
|
|
|
// Object stored in cache by readObjectAtOffset
|
|
|
|
int aobjid;
|
|
|
|
int ageneration;
|
|
|
|
QPDFObjectHandle oh =
|
|
|
|
readObjectAtOffset(true, offset, "", objid, generation,
|
|
|
|
aobjid, ageneration);
|
|
|
|
}
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-07-28 02:27:00 +00:00
|
|
|
case 2:
|
|
|
|
resolveObjectsInStream(entry.getObjStreamNumber());
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-07-28 02:27:00 +00:00
|
|
|
default:
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf,
|
|
|
|
this->m->file->getName(), "", 0,
|
2017-07-28 02:27:00 +00:00
|
|
|
"object " +
|
|
|
|
QUtil::int_to_string(objid) + "/" +
|
|
|
|
QUtil::int_to_string(generation) +
|
|
|
|
" has unexpected xref entry type");
|
|
|
|
}
|
2017-07-29 23:31:12 +00:00
|
|
|
success = true;
|
2017-07-28 02:27:00 +00:00
|
|
|
}
|
|
|
|
catch (QPDFExc& e)
|
|
|
|
{
|
|
|
|
warn(e);
|
2017-07-29 23:31:12 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
|
2017-07-29 23:31:12 +00:00
|
|
|
"object " +
|
|
|
|
QUtil::int_to_string(objid) + "/" +
|
|
|
|
QUtil::int_to_string(generation) +
|
|
|
|
": error reading object: " + e.what()));
|
|
|
|
}
|
|
|
|
if (! success)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF resolve failure to null");
|
2017-07-28 02:27:00 +00:00
|
|
|
QPDFObjectHandle oh = QPDFObjectHandle::newNull();
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache[og] =
|
2017-07-28 02:27:00 +00:00
|
|
|
ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh), -1, -1);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->obj_cache[og].object;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::resolveObjectsInStream(int obj_stream_number)
|
|
|
|
{
|
|
|
|
// Force resolution of object stream
|
|
|
|
QPDFObjectHandle obj_stream = getObjectByID(obj_stream_number, 0);
|
|
|
|
if (! obj_stream.isStream())
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"supposed object stream " +
|
|
|
|
QUtil::int_to_string(obj_stream_number) +
|
|
|
|
" is not a stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
// For linearization data in the object, use the data from the
|
|
|
|
// object stream for the objects in the stream.
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen stream_og(obj_stream_number, 0);
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t end_before_space =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache[stream_og].end_before_space;
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t end_after_space =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache[stream_og].end_after_space;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QPDFObjectHandle dict = obj_stream.getDict();
|
|
|
|
if (! (dict.getKey("/Type").isName() &&
|
|
|
|
dict.getKey("/Type").getName() == "/ObjStm"))
|
|
|
|
{
|
2009-10-19 23:09:19 +00:00
|
|
|
QTC::TC("qpdf", "QPDF ERR object stream with wrong type");
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"supposed object stream " +
|
|
|
|
QUtil::int_to_string(obj_stream_number) +
|
|
|
|
" has wrong type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! (dict.getKey("/N").isInteger() &&
|
|
|
|
dict.getKey("/First").isInteger()))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"object stream " +
|
|
|
|
QUtil::int_to_string(obj_stream_number) +
|
|
|
|
" has incorrect keys");
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = dict.getKey("/N").getIntValue();
|
|
|
|
int first = dict.getKey("/First").getIntValue();
|
|
|
|
|
|
|
|
std::map<int, int> offsets;
|
|
|
|
|
2017-12-25 00:18:52 +00:00
|
|
|
PointerHolder<Buffer> bp = obj_stream.getStreamData(qpdf_dl_specialized);
|
2010-09-24 19:10:08 +00:00
|
|
|
PointerHolder<InputSource> input = new BufferInputSource(
|
2008-04-29 12:55:25 +00:00
|
|
|
"object stream " + QUtil::int_to_string(obj_stream_number),
|
|
|
|
bp.getPointer());
|
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
QPDFTokenizer::Token tnum = readToken(input);
|
|
|
|
QPDFTokenizer::Token toffset = readToken(input);
|
2008-04-29 12:55:25 +00:00
|
|
|
if (! ((tnum.getType() == QPDFTokenizer::tt_integer) &&
|
|
|
|
(toffset.getType() == QPDFTokenizer::tt_integer)))
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->last_object_description,
|
|
|
|
input->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"expected integer in object stream header");
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:27:59 +00:00
|
|
|
int num = QUtil::string_to_int(tnum.getValue().c_str());
|
2012-06-23 19:08:21 +00:00
|
|
|
int offset = QUtil::string_to_ll(toffset.getValue().c_str());
|
2008-04-29 12:55:25 +00:00
|
|
|
offsets[num] = offset + first;
|
|
|
|
}
|
|
|
|
|
2013-02-23 22:41:27 +00:00
|
|
|
// To avoid having to read the object stream multiple times, store
|
|
|
|
// all objects that would be found here in the cache. Remember
|
|
|
|
// that some objects stored here might have been overridden by new
|
|
|
|
// objects appended to the file, so it is necessary to recheck the
|
|
|
|
// xref table and only cache what would actually be resolved here.
|
2008-04-29 12:55:25 +00:00
|
|
|
for (std::map<int, int>::iterator iter = offsets.begin();
|
|
|
|
iter != offsets.end(); ++iter)
|
|
|
|
{
|
|
|
|
int obj = (*iter).first;
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen og(obj, 0);
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFXRefEntry const& entry = this->m->xref_table[og];
|
2013-02-23 22:41:27 +00:00
|
|
|
if ((entry.getType() == 2) &&
|
|
|
|
(entry.getObjStreamNumber() == obj_stream_number))
|
|
|
|
{
|
|
|
|
int offset = (*iter).second;
|
|
|
|
input->seek(offset, SEEK_SET);
|
|
|
|
QPDFObjectHandle oh = readObject(input, "", obj, 0, true);
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache[og] =
|
2013-02-23 22:41:27 +00:00
|
|
|
ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh),
|
|
|
|
end_before_space, end_after_space);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF not caching overridden objstm object");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::makeIndirectObject(QPDFObjectHandle oh)
|
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen o1(0, 0);
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->obj_cache.empty())
|
2010-08-05 20:20:52 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
o1 = (*(this->m->obj_cache.rbegin())).first;
|
2010-08-05 20:20:52 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFObjGen o2 = (*(this->m->xref_table.rbegin())).first;
|
2008-04-29 12:55:25 +00:00
|
|
|
QTC::TC("qpdf", "QPDF indirect last obj from xref",
|
2013-06-14 15:22:04 +00:00
|
|
|
(o2.getObj() > o1.getObj()) ? 1 : 0);
|
|
|
|
int max_objid = std::max(o1.getObj(), o2.getObj());
|
|
|
|
QPDFObjGen next(max_objid + 1, 0);
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache[next] =
|
2008-04-29 12:55:25 +00:00
|
|
|
ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh), -1, -1);
|
2013-06-14 15:22:04 +00:00
|
|
|
return QPDFObjectHandle::Factory::newIndirect(
|
|
|
|
this, next.getObj(), next.getGen());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getObjectByObjGen(QPDFObjGen const& og)
|
|
|
|
{
|
|
|
|
return getObjectByID(og.getObj(), og.getGen());
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getObjectByID(int objid, int generation)
|
|
|
|
{
|
|
|
|
return QPDFObjectHandle::Factory::newIndirect(this, objid, generation);
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
void
|
|
|
|
QPDF::replaceObject(QPDFObjGen const& og, QPDFObjectHandle oh)
|
|
|
|
{
|
|
|
|
replaceObject(og.getObj(), og.getGen(), oh);
|
|
|
|
}
|
|
|
|
|
2011-08-10 16:42:48 +00:00
|
|
|
void
|
|
|
|
QPDF::replaceObject(int objid, int generation, QPDFObjectHandle oh)
|
|
|
|
{
|
|
|
|
if (oh.isIndirect())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF replaceObject called with indirect object");
|
|
|
|
throw std::logic_error(
|
|
|
|
"QPDF::replaceObject called with indirect object handle");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force new object to appear in the cache
|
|
|
|
resolve(objid, generation);
|
|
|
|
|
|
|
|
// Replace the object in the object cache
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen og(objid, generation);
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_cache[og] =
|
2011-08-10 16:42:48 +00:00
|
|
|
ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh), -1, -1);
|
|
|
|
}
|
|
|
|
|
2012-07-08 18:19:19 +00:00
|
|
|
void
|
|
|
|
QPDF::replaceReserved(QPDFObjectHandle reserved,
|
|
|
|
QPDFObjectHandle replacement)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF replaceReserved");
|
|
|
|
reserved.assertReserved();
|
2013-06-14 15:58:37 +00:00
|
|
|
replaceObject(reserved.getObjGen(), replacement);
|
2012-07-08 18:19:19 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 19:29:41 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::copyForeignObject(QPDFObjectHandle foreign)
|
|
|
|
{
|
|
|
|
return copyForeignObject(foreign, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::copyForeignObject(QPDFObjectHandle foreign, bool allow_page)
|
|
|
|
{
|
|
|
|
if (! foreign.isIndirect())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF copyForeign direct");
|
|
|
|
throw std::logic_error(
|
|
|
|
"QPDF::copyForeign called with direct object handle");
|
|
|
|
}
|
|
|
|
QPDF* other = foreign.getOwningQPDF();
|
|
|
|
if (other == this)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF copyForeign not foreign");
|
|
|
|
throw std::logic_error(
|
|
|
|
"QPDF::copyForeign called with object from this QPDF");
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
ObjCopier& obj_copier = this->m->object_copiers[other];
|
2012-07-11 19:29:41 +00:00
|
|
|
if (! obj_copier.visiting.empty())
|
|
|
|
{
|
|
|
|
throw std::logic_error("obj_copier.visiting is not empty"
|
|
|
|
" at the beginning of copyForeignObject");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have an object in this file for every referenced
|
|
|
|
// object in the old file. obj_copier.object_map maps foreign
|
2013-06-14 15:22:04 +00:00
|
|
|
// QPDFObjGen to local objects. For everything new that we have
|
|
|
|
// to copy, the local object will be a reservation, unless it is a
|
2012-07-11 19:29:41 +00:00
|
|
|
// stream, in which case the local object will already be a
|
|
|
|
// stream.
|
|
|
|
reserveObjects(foreign, obj_copier, true);
|
|
|
|
|
|
|
|
if (! obj_copier.visiting.empty())
|
|
|
|
{
|
|
|
|
throw std::logic_error("obj_copier.visiting is not empty"
|
|
|
|
" after reserving objects");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy any new objects and replace the reservations.
|
|
|
|
for (std::vector<QPDFObjectHandle>::iterator iter =
|
|
|
|
obj_copier.to_copy.begin();
|
|
|
|
iter != obj_copier.to_copy.end(); ++iter)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle& to_copy = *iter;
|
|
|
|
QPDFObjectHandle copy =
|
|
|
|
replaceForeignIndirectObjects(to_copy, obj_copier, true);
|
|
|
|
if (! to_copy.isStream())
|
|
|
|
{
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen og(to_copy.getObjGen());
|
2012-07-11 19:29:41 +00:00
|
|
|
replaceReserved(obj_copier.object_map[og], copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj_copier.to_copy.clear();
|
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
return obj_copier.object_map[foreign.getObjGen()];
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::reserveObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier,
|
|
|
|
bool top)
|
|
|
|
{
|
|
|
|
if (foreign.isReserved())
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"QPDF: attempting to copy a foreign reserved object");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foreign.isPagesObject())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF not copying pages object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((! top) && foreign.isPageObject())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF not crossing page boundary");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foreign.isIndirect())
|
|
|
|
{
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen foreign_og(foreign.getObjGen());
|
2012-07-11 19:29:41 +00:00
|
|
|
if (obj_copier.visiting.find(foreign_og) != obj_copier.visiting.end())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF loop reserving objects");
|
|
|
|
return;
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
if (obj_copier.object_map.find(foreign_og) !=
|
|
|
|
obj_copier.object_map.end())
|
2013-12-26 16:51:50 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF already reserved object");
|
|
|
|
return;
|
|
|
|
}
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF copy indirect");
|
|
|
|
obj_copier.visiting.insert(foreign_og);
|
2013-06-14 15:22:04 +00:00
|
|
|
std::map<QPDFObjGen, QPDFObjectHandle>::iterator mapping =
|
2012-07-11 19:29:41 +00:00
|
|
|
obj_copier.object_map.find(foreign_og);
|
|
|
|
if (mapping == obj_copier.object_map.end())
|
|
|
|
{
|
|
|
|
obj_copier.to_copy.push_back(foreign);
|
|
|
|
QPDFObjectHandle reservation;
|
|
|
|
if (foreign.isStream())
|
|
|
|
{
|
|
|
|
reservation = QPDFObjectHandle::newStream(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reservation = QPDFObjectHandle::newReserved(this);
|
|
|
|
}
|
|
|
|
obj_copier.object_map[foreign_og] = reservation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foreign.isArray())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF reserve array");
|
|
|
|
int n = foreign.getArrayNItems();
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
reserveObjects(foreign.getArrayItem(i), obj_copier, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (foreign.isDictionary())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF reserve dictionary");
|
|
|
|
std::set<std::string> keys = foreign.getKeys();
|
|
|
|
for (std::set<std::string>::iterator iter = keys.begin();
|
|
|
|
iter != keys.end(); ++iter)
|
|
|
|
{
|
|
|
|
reserveObjects(foreign.getKey(*iter), obj_copier, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (foreign.isStream())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF reserve stream");
|
|
|
|
reserveObjects(foreign.getDict(), obj_copier, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foreign.isIndirect())
|
|
|
|
{
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen foreign_og(foreign.getObjGen());
|
2012-07-11 19:29:41 +00:00
|
|
|
obj_copier.visiting.erase(foreign_og);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::replaceForeignIndirectObjects(
|
|
|
|
QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle result;
|
|
|
|
if ((! top) && foreign.isIndirect())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF replace indirect");
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen foreign_og(foreign.getObjGen());
|
2013-06-14 15:22:04 +00:00
|
|
|
std::map<QPDFObjGen, QPDFObjectHandle>::iterator mapping =
|
2012-07-11 19:29:41 +00:00
|
|
|
obj_copier.object_map.find(foreign_og);
|
|
|
|
if (mapping == obj_copier.object_map.end())
|
|
|
|
{
|
|
|
|
// This case would occur if this is a reference to a Page
|
|
|
|
// or Pages object that we didn't traverse into.
|
|
|
|
QTC::TC("qpdf", "QPDF replace foreign indirect with null");
|
|
|
|
result = QPDFObjectHandle::newNull();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = obj_copier.object_map[foreign_og];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (foreign.isArray())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF replace array");
|
|
|
|
result = QPDFObjectHandle::newArray();
|
|
|
|
int n = foreign.getArrayNItems();
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
result.appendItem(
|
|
|
|
replaceForeignIndirectObjects(
|
|
|
|
foreign.getArrayItem(i), obj_copier, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (foreign.isDictionary())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF replace dictionary");
|
|
|
|
result = QPDFObjectHandle::newDictionary();
|
|
|
|
std::set<std::string> keys = foreign.getKeys();
|
|
|
|
for (std::set<std::string>::iterator iter = keys.begin();
|
|
|
|
iter != keys.end(); ++iter)
|
|
|
|
{
|
|
|
|
result.replaceKey(
|
|
|
|
*iter,
|
|
|
|
replaceForeignIndirectObjects(
|
|
|
|
foreign.getKey(*iter), obj_copier, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (foreign.isStream())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF replace stream");
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen foreign_og(foreign.getObjGen());
|
2012-07-11 19:29:41 +00:00
|
|
|
result = obj_copier.object_map[foreign_og];
|
|
|
|
result.assertStream();
|
|
|
|
QPDFObjectHandle dict = result.getDict();
|
|
|
|
QPDFObjectHandle old_dict = foreign.getDict();
|
|
|
|
std::set<std::string> keys = old_dict.getKeys();
|
|
|
|
for (std::set<std::string>::iterator iter = keys.begin();
|
|
|
|
iter != keys.end(); ++iter)
|
|
|
|
{
|
|
|
|
dict.replaceKey(
|
|
|
|
*iter,
|
|
|
|
replaceForeignIndirectObjects(
|
|
|
|
old_dict.getKey(*iter), obj_copier, false));
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->copied_stream_data_provider == 0)
|
2012-07-11 19:29:41 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->copied_stream_data_provider =
|
|
|
|
new CopiedStreamDataProvider();
|
|
|
|
this->m->copied_streams = this->m->copied_stream_data_provider;
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen local_og(result.getObjGen());
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->copied_stream_data_provider->registerForeignStream(
|
2012-07-11 19:29:41 +00:00
|
|
|
local_og, foreign);
|
2017-08-22 01:33:44 +00:00
|
|
|
result.replaceStreamData(this->m->copied_streams,
|
2012-07-11 19:29:41 +00:00
|
|
|
dict.getKey("/Filter"),
|
|
|
|
dict.getKey("/DecodeParms"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreign.assertScalar();
|
|
|
|
result = foreign;
|
|
|
|
result.makeDirect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top && (! result.isStream()) && result.isIndirect())
|
|
|
|
{
|
|
|
|
throw std::logic_error("replacement for foreign object is indirect");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-07-08 18:19:19 +00:00
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
void
|
|
|
|
QPDF::swapObjects(QPDFObjGen const& og1, QPDFObjGen const& og2)
|
|
|
|
{
|
|
|
|
swapObjects(og1.getObj(), og1.getGen(), og2.getObj(), og2.getGen());
|
|
|
|
}
|
|
|
|
|
2011-08-10 16:42:48 +00:00
|
|
|
void
|
|
|
|
QPDF::swapObjects(int objid1, int generation1, int objid2, int generation2)
|
|
|
|
{
|
|
|
|
// Force objects to be loaded into cache; then swap them in the
|
|
|
|
// cache.
|
|
|
|
resolve(objid1, generation1);
|
|
|
|
resolve(objid2, generation2);
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen og1(objid1, generation1);
|
|
|
|
QPDFObjGen og2(objid2, generation2);
|
2017-08-22 01:33:44 +00:00
|
|
|
ObjCache t = this->m->obj_cache[og1];
|
|
|
|
this->m->obj_cache[og1] = this->m->obj_cache[og2];
|
|
|
|
this->m->obj_cache[og2] = t;
|
2011-08-10 16:42:48 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF::getFilename() const
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->file->getName();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::getPDFVersion() const
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->pdf_version;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 20:17:39 +00:00
|
|
|
int
|
|
|
|
QPDF::getExtensionLevel()
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
QPDFObjectHandle obj = getRoot();
|
|
|
|
if (obj.hasKey("/Extensions"))
|
|
|
|
{
|
|
|
|
obj = obj.getKey("/Extensions");
|
|
|
|
if (obj.isDictionary() && obj.hasKey("/ADBE"))
|
|
|
|
{
|
|
|
|
obj = obj.getKey("/ADBE");
|
|
|
|
if (obj.isDictionary() && obj.hasKey("/ExtensionLevel"))
|
|
|
|
{
|
|
|
|
obj = obj.getKey("/ExtensionLevel");
|
|
|
|
if (obj.isInteger())
|
|
|
|
{
|
|
|
|
result = obj.getIntValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getTrailer()
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->trailer;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getRoot()
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFObjectHandle root = this->m->trailer.getKey("/Root");
|
2017-07-28 22:02:39 +00:00
|
|
|
if (! root.isDictionary())
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"", this->m->file->getLastOffset(),
|
2017-07-28 22:02:39 +00:00
|
|
|
"unable to find /Root dictionary");
|
|
|
|
}
|
|
|
|
return root;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::getObjectStreamData(std::map<int, int>& omap)
|
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
for (std::map<QPDFObjGen, QPDFXRefEntry>::iterator iter =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->xref_table.begin();
|
|
|
|
iter != this->m->xref_table.end(); ++iter)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen const& og = (*iter).first;
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFXRefEntry const& entry = (*iter).second;
|
|
|
|
if (entry.getType() == 2)
|
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
omap[og.getObj()] = entry.getObjStreamNumber();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 18:53:47 +00:00
|
|
|
std::vector<QPDFObjGen>
|
|
|
|
QPDF::getCompressibleObjGens()
|
|
|
|
{
|
|
|
|
// Return a list of objects that are allowed to be in object
|
|
|
|
// streams. Walk through the objects by traversing the document
|
|
|
|
// from the root, including a traversal of the pages tree. This
|
|
|
|
// makes that objects that are on the same page are more likely to
|
|
|
|
// be in the same object stream, which is slightly more efficient,
|
2008-04-29 12:55:25 +00:00
|
|
|
// particularly with linearized files. This is better than
|
|
|
|
// iterating through the xref table since it avoids preserving
|
|
|
|
// orphaned items.
|
|
|
|
|
|
|
|
// Exclude encryption dictionary, if any
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFObjectHandle encryption_dict =
|
|
|
|
this->m->trailer.getKey("/Encrypt");
|
2013-06-14 18:53:47 +00:00
|
|
|
QPDFObjGen encryption_dict_og = encryption_dict.getObjGen();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2013-06-14 18:53:47 +00:00
|
|
|
std::set<QPDFObjGen> visited;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::list<QPDFObjectHandle> queue;
|
2017-08-22 01:33:44 +00:00
|
|
|
queue.push_front(this->m->trailer);
|
2013-06-14 18:53:47 +00:00
|
|
|
std::vector<QPDFObjGen> result;
|
2008-04-29 12:55:25 +00:00
|
|
|
while (! queue.empty())
|
|
|
|
{
|
|
|
|
QPDFObjectHandle obj = queue.front();
|
|
|
|
queue.pop_front();
|
|
|
|
if (obj.isIndirect())
|
|
|
|
{
|
2013-06-14 18:53:47 +00:00
|
|
|
QPDFObjGen og = obj.getObjGen();
|
|
|
|
if (visited.count(og))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF loop detected traversing objects");
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-14 18:53:47 +00:00
|
|
|
if (og == encryption_dict_og)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF exclude encryption dictionary");
|
|
|
|
}
|
|
|
|
else if (! obj.isStream())
|
|
|
|
{
|
2013-06-14 18:53:47 +00:00
|
|
|
result.push_back(og);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-06-14 18:53:47 +00:00
|
|
|
visited.insert(og);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
if (obj.isStream())
|
|
|
|
{
|
|
|
|
QPDFObjectHandle dict = obj.getDict();
|
|
|
|
std::set<std::string> keys = dict.getKeys();
|
|
|
|
for (std::set<std::string>::reverse_iterator iter = keys.rbegin();
|
|
|
|
iter != keys.rend(); ++iter)
|
|
|
|
{
|
|
|
|
std::string const& key = *iter;
|
|
|
|
QPDFObjectHandle value = dict.getKey(key);
|
|
|
|
if (key == "/Length")
|
|
|
|
{
|
|
|
|
// omit stream lengths
|
|
|
|
if (value.isIndirect())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF exclude indirect length");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
queue.push_front(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (obj.isDictionary())
|
|
|
|
{
|
|
|
|
std::set<std::string> keys = obj.getKeys();
|
|
|
|
for (std::set<std::string>::reverse_iterator iter = keys.rbegin();
|
|
|
|
iter != keys.rend(); ++iter)
|
|
|
|
{
|
|
|
|
queue.push_front(obj.getKey(*iter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (obj.isArray())
|
|
|
|
{
|
|
|
|
int n = obj.getArrayNItems();
|
|
|
|
for (int i = 1; i <= n; ++i)
|
|
|
|
{
|
|
|
|
queue.push_front(obj.getArrayItem(n - i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:42:27 +00:00
|
|
|
bool
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF::pipeStreamData(int objid, int generation,
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t offset, size_t length,
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle stream_dict,
|
2017-07-28 03:42:27 +00:00
|
|
|
Pipeline* pipeline,
|
2017-09-12 19:48:08 +00:00
|
|
|
bool suppress_warnings,
|
|
|
|
bool will_retry)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-07-28 03:42:27 +00:00
|
|
|
bool success = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<PointerHolder<Pipeline> > to_delete;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->encrypted)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-10-17 18:54:51 +00:00
|
|
|
decryptStream(pipeline, objid, generation, stream_dict, to_delete);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 19:00:19 +00:00
|
|
|
try
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->seek(offset, SEEK_SET);
|
2009-03-08 19:00:19 +00:00
|
|
|
char buf[10240];
|
|
|
|
while (length > 0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-03-08 19:00:19 +00:00
|
|
|
size_t to_read = (sizeof(buf) < length ? sizeof(buf) : length);
|
2017-08-22 01:33:44 +00:00
|
|
|
size_t len = this->m->file->read(buf, to_read);
|
2009-03-08 19:00:19 +00:00
|
|
|
if (len == 0)
|
|
|
|
{
|
2009-10-19 23:09:19 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf,
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2009-03-08 19:00:19 +00:00
|
|
|
"unexpected EOF reading stream data");
|
|
|
|
}
|
|
|
|
length -= len;
|
2013-02-24 02:46:21 +00:00
|
|
|
pipeline->write(QUtil::unsigned_char_pointer(buf), len);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-19 13:18:14 +00:00
|
|
|
pipeline->finish();
|
2017-07-28 03:42:27 +00:00
|
|
|
success = true;
|
2009-03-08 19:00:19 +00:00
|
|
|
}
|
2009-10-19 23:09:19 +00:00
|
|
|
catch (QPDFExc& e)
|
|
|
|
{
|
2017-07-28 03:42:27 +00:00
|
|
|
if (! suppress_warnings)
|
|
|
|
{
|
|
|
|
warn(e);
|
|
|
|
}
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
2017-07-29 16:07:19 +00:00
|
|
|
catch (std::exception& e)
|
2009-03-08 19:00:19 +00:00
|
|
|
{
|
2017-07-28 03:42:27 +00:00
|
|
|
if (! suppress_warnings)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF decoding error warning");
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"", this->m->file->getLastOffset(),
|
2017-07-28 03:42:27 +00:00
|
|
|
"error decoding stream data for object " +
|
|
|
|
QUtil::int_to_string(objid) + " " +
|
|
|
|
QUtil::int_to_string(generation) + ": " + e.what()));
|
2017-09-12 19:48:08 +00:00
|
|
|
if (will_retry)
|
|
|
|
{
|
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"", this->m->file->getLastOffset(),
|
|
|
|
"stream will be re-processed without"
|
|
|
|
" filtering to avoid data loss"));
|
|
|
|
}
|
2017-07-28 03:42:27 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-19 13:18:14 +00:00
|
|
|
if (! success)
|
2017-07-29 16:07:19 +00:00
|
|
|
{
|
2017-08-19 13:18:14 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
pipeline->finish();
|
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
|
|
|
// ignore
|
|
|
|
}
|
2017-07-29 16:07:19 +00:00
|
|
|
}
|
2017-07-28 03:42:27 +00:00
|
|
|
return success;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-12-30 00:00:05 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::findAttachmentStreams()
|
|
|
|
{
|
|
|
|
QPDFObjectHandle root = getRoot();
|
|
|
|
QPDFObjectHandle names = root.getKey("/Names");
|
|
|
|
if (! names.isDictionary())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle embeddedFiles = names.getKey("/EmbeddedFiles");
|
|
|
|
if (! embeddedFiles.isDictionary())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
names = embeddedFiles.getKey("/Names");
|
|
|
|
if (! names.isArray())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < names.getArrayNItems(); ++i)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle item = names.getArrayItem(i);
|
|
|
|
if (item.isDictionary() &&
|
|
|
|
item.getKey("/Type").isName() &&
|
|
|
|
(item.getKey("/Type").getName() == "/Filespec") &&
|
|
|
|
item.getKey("/EF").isDictionary() &&
|
|
|
|
item.getKey("/EF").getKey("/F").isStream())
|
|
|
|
{
|
|
|
|
QPDFObjectHandle stream = item.getKey("/EF").getKey("/F");
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->attachment_streams.insert(stream.getObjGen());
|
2012-12-30 00:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|