2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/qpdf-config.h> // include first for large file support
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <atomic>
|
2023-05-20 18:56:01 +00:00
|
|
|
#include <cstring>
|
2019-08-27 21:57:38 +00:00
|
|
|
#include <limits>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <map>
|
|
|
|
#include <regex>
|
2019-09-17 23:48:27 +00:00
|
|
|
#include <sstream>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <vector>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-07-21 09:37:14 +00:00
|
|
|
#include <qpdf/BufferInputSource.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/FileInputSource.hh>
|
2012-12-25 19:38:18 +00:00
|
|
|
#include <qpdf/OffsetInputSource.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Pipeline.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDFExc.hh>
|
2022-06-05 17:30:42 +00:00
|
|
|
#include <qpdf/QPDFLogger.hh>
|
2022-09-08 15:12:10 +00:00
|
|
|
#include <qpdf/QPDFObject_private.hh>
|
2022-09-28 15:58:42 +00:00
|
|
|
#include <qpdf/QPDFParser.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDF_Array.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDF_Null.hh>
|
2022-08-02 16:18:00 +00:00
|
|
|
#include <qpdf/QPDF_Reserved.hh>
|
2019-01-07 02:18:36 +00:00
|
|
|
#include <qpdf/QPDF_Stream.hh>
|
2022-08-10 12:16:06 +00:00
|
|
|
#include <qpdf/QPDF_Unresolved.hh>
|
2022-06-05 17:30:42 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// This must be a fixed value. This API returns a const reference to it, and the C API relies on its
|
|
|
|
// being static as well.
|
2022-02-05 13:26:32 +00:00
|
|
|
std::string const QPDF::qpdf_version(QPDF_VERSION);
|
2009-10-24 04:47:17 +00:00
|
|
|
|
2022-04-03 20:10:27 +00:00
|
|
|
static char const* EMPTY_PDF = (
|
|
|
|
// force line break
|
|
|
|
"%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-06-22 03:06:48 +00:00
|
|
|
|
2022-04-16 17:21:57 +00:00
|
|
|
namespace
|
2021-01-06 14:49:10 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
class InvalidInputSource: public InputSource
|
2021-01-06 14:49:10 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
public:
|
2023-05-20 13:25:46 +00:00
|
|
|
~InvalidInputSource() override = default;
|
2023-05-20 12:56:33 +00:00
|
|
|
qpdf_offset_t
|
2022-04-16 17:21:57 +00:00
|
|
|
findAndSkipNextEOL() override
|
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
std::string const&
|
2022-04-16 17:21:57 +00:00
|
|
|
getName() const override
|
|
|
|
{
|
|
|
|
static std::string name("closed input source");
|
|
|
|
return name;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
qpdf_offset_t
|
2022-04-16 17:21:57 +00:00
|
|
|
tell() override
|
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
void
|
2022-04-16 17:21:57 +00:00
|
|
|
seek(qpdf_offset_t offset, int whence) override
|
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
void
|
2022-04-16 17:21:57 +00:00
|
|
|
rewind() override
|
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
size_t
|
2022-04-16 17:21:57 +00:00
|
|
|
read(char* buffer, size_t length) override
|
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
void
|
2022-04-16 17:21:57 +00:00
|
|
|
unreadCh(char ch) override
|
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
}
|
2021-01-06 14:49:10 +00:00
|
|
|
|
2022-04-16 17:21:57 +00:00
|
|
|
private:
|
|
|
|
void
|
|
|
|
throwException()
|
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("QPDF operation attempted on a QPDF object with no input "
|
2023-05-27 17:19:52 +00:00
|
|
|
"source. QPDF operations are invalid before processFile (or "
|
|
|
|
"another process method) or after closeInputSource");
|
2022-04-16 17:21:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2021-01-06 14:49:10 +00:00
|
|
|
|
2019-01-07 02:18:36 +00:00
|
|
|
QPDF::ForeignStreamData::ForeignStreamData(
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<EncryptionParameters> encp,
|
|
|
|
std::shared_ptr<InputSource> file,
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDFObjGen const& foreign_og,
|
2019-01-07 02:18:36 +00:00
|
|
|
qpdf_offset_t offset,
|
|
|
|
size_t length,
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFObjectHandle local_dict) :
|
2019-01-07 02:18:36 +00:00
|
|
|
encp(encp),
|
|
|
|
file(file),
|
2022-07-23 15:48:28 +00:00
|
|
|
foreign_og(foreign_og),
|
2019-01-07 02:18:36 +00:00
|
|
|
offset(offset),
|
|
|
|
length(length),
|
|
|
|
local_dict(local_dict)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::CopiedStreamDataProvider::CopiedStreamDataProvider(QPDF& destination_qpdf) :
|
2020-04-05 03:35:35 +00:00
|
|
|
QPDFObjectHandle::StreamDataProvider(true),
|
2019-01-07 02:18:36 +00:00
|
|
|
destination_qpdf(destination_qpdf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-05 03:35:35 +00:00
|
|
|
bool
|
2012-07-11 19:29:41 +00:00
|
|
|
QPDF::CopiedStreamDataProvider::provideStreamData(
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFObjGen const& og, Pipeline* pipeline, bool suppress_warnings, bool will_retry)
|
2012-07-11 19:29:41 +00:00
|
|
|
{
|
2022-07-24 13:16:37 +00:00
|
|
|
std::shared_ptr<ForeignStreamData> foreign_data = foreign_stream_data[og];
|
2020-04-05 03:35:35 +00:00
|
|
|
bool result = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (foreign_data.get()) {
|
2020-04-05 03:35:35 +00:00
|
|
|
result = destination_qpdf.pipeForeignStreamData(
|
|
|
|
foreign_data, pipeline, suppress_warnings, will_retry);
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC("qpdf", "QPDF copy foreign with data", result ? 0 : 1);
|
|
|
|
} else {
|
2022-07-24 13:16:37 +00:00
|
|
|
auto foreign_stream = foreign_streams[og];
|
2020-04-05 03:35:35 +00:00
|
|
|
result = foreign_stream.pipeStreamData(
|
2022-04-02 21:14:10 +00:00
|
|
|
pipeline, nullptr, 0, qpdf_dl_none, suppress_warnings, will_retry);
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDF copy foreign with foreign_stream", result ? 0 : 1);
|
2019-01-07 02:18:36 +00:00
|
|
|
}
|
2020-04-05 03:35:35 +00:00
|
|
|
return result;
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-07 02:18:36 +00:00
|
|
|
void
|
|
|
|
QPDF::CopiedStreamDataProvider::registerForeignStream(
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFObjGen const& local_og, std::shared_ptr<ForeignStreamData> foreign_stream)
|
2019-01-07 02:18:36 +00:00
|
|
|
{
|
|
|
|
this->foreign_stream_data[local_og] = foreign_stream;
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDF::StringDecrypter::StringDecrypter(QPDF* qpdf, QPDFObjGen const& og) :
|
2012-07-21 13:00:06 +00:00
|
|
|
qpdf(qpdf),
|
2022-07-23 15:48:28 +00:00
|
|
|
og(og)
|
2012-07-21 13:00:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::StringDecrypter::decryptString(std::string& val)
|
|
|
|
{
|
2022-07-23 15:48:28 +00:00
|
|
|
qpdf->decryptString(val, og);
|
2012-07-21 13:00:06 +00:00
|
|
|
}
|
2012-07-11 19:29:41 +00:00
|
|
|
|
2009-10-24 04:47:17 +00:00
|
|
|
std::string const&
|
|
|
|
QPDF::QPDFVersion()
|
|
|
|
{
|
2022-02-05 13:26:32 +00:00
|
|
|
// The C API relies on this being a static value.
|
2009-10-24 04:47:17 +00:00
|
|
|
return QPDF::qpdf_version;
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:58:16 +00:00
|
|
|
QPDF::EncryptionParameters::EncryptionParameters() :
|
2008-04-29 12:55:25 +00:00
|
|
|
encrypted(false),
|
|
|
|
encryption_initialized(false),
|
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),
|
2019-08-24 13:34:48 +00:00
|
|
|
user_password_matched(false),
|
|
|
|
owner_password_matched(false)
|
2019-01-06 14:58:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDF::Members::Members() :
|
2022-06-05 17:30:42 +00:00
|
|
|
log(QPDFLogger::defaultLogger()),
|
2021-11-29 12:16:34 +00:00
|
|
|
file(new InvalidInputSource()),
|
2023-02-19 00:36:24 +00:00
|
|
|
encp(new EncryptionParameters)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDF::QPDF() :
|
|
|
|
m(new Members())
|
|
|
|
{
|
2018-02-16 22:25:27 +00:00
|
|
|
m->tokenizer.allowEOF();
|
2023-05-24 15:28:17 +00:00
|
|
|
// Generate a unique ID. It just has to be unique among all QPDF objects allocated throughout
|
|
|
|
// the lifetime of this running application.
|
2020-04-04 01:48:25 +00:00
|
|
|
static std::atomic<unsigned long long> unique_id{0};
|
|
|
|
m->unique_id = unique_id.fetch_add(1ULL);
|
2017-08-22 01:33:44 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF::~QPDF()
|
|
|
|
{
|
2023-05-24 15:28:17 +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
|
|
|
|
// std::shared_ptr 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 indirect references by replacing them with an internal object type representing that
|
|
|
|
// they have been destroyed. Note that we can't break references like this at any time when the
|
|
|
|
// QPDF object is active. The call to reset also causes all direct QPDFObjectHandle objects that
|
|
|
|
// are reachable from this object to release their association with this QPDF. Direct objects
|
|
|
|
// are not destroyed since they can be moved to other QPDF objects safely.
|
|
|
|
|
|
|
|
// 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.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->xref_table.clear();
|
|
|
|
for (auto const& iter: m->obj_cache) {
|
2022-09-08 15:06:15 +00:00
|
|
|
iter.second.object->disconnect();
|
2023-04-11 16:16:40 +00:00
|
|
|
if (iter.second.object->getTypeCode() != ::ot_null) {
|
|
|
|
iter.second.object->destroy();
|
|
|
|
}
|
2010-06-06 13:32:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 12:53:27 +00:00
|
|
|
std::shared_ptr<QPDF>
|
|
|
|
QPDF::create()
|
|
|
|
{
|
|
|
|
return std::make_shared<QPDF>();
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::processFile(char const* filename, char const* password)
|
|
|
|
{
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* fi = new FileInputSource(filename);
|
2022-04-09 18:35:56 +00:00
|
|
|
processInputSource(std::shared_ptr<InputSource>(fi), password);
|
2010-10-01 10:20:38 +00:00
|
|
|
}
|
|
|
|
|
2012-06-21 11:59:56 +00:00
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::processFile(char const* description, FILE* filep, bool close_file, char const* password)
|
2012-06-21 11:59:56 +00:00
|
|
|
{
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* fi = new FileInputSource(description, filep, close_file);
|
2022-04-09 18:35:56 +00:00
|
|
|
processInputSource(std::shared_ptr<InputSource>(fi), password);
|
2012-06-21 11:59:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 10:20:38 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::processMemoryFile(
|
2023-05-21 17:35:09 +00:00
|
|
|
char const* description, char const* buf, size_t length, char const* password)
|
2010-10-01 10:20:38 +00:00
|
|
|
{
|
2012-09-23 21:42:26 +00:00
|
|
|
processInputSource(
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<InputSource>(
|
2022-04-03 20:10:27 +00:00
|
|
|
// line-break
|
|
|
|
new BufferInputSource(
|
2023-05-21 17:35:09 +00:00
|
|
|
description, new Buffer(QUtil::unsigned_char_pointer(buf), length), true)),
|
2012-09-23 21:42:26 +00:00
|
|
|
password);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::processInputSource(std::shared_ptr<InputSource> source, char const* password)
|
2012-09-23 21:42:26 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file = source;
|
2010-10-01 10:20:38 +00:00
|
|
|
parse(password);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 19:10:29 +00:00
|
|
|
void
|
|
|
|
QPDF::closeInputSource()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file = std::shared_ptr<InputSource>(new InvalidInputSource());
|
2019-08-31 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 15:17:17 +00:00
|
|
|
void
|
|
|
|
QPDF::setPasswordIsHexKey(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->provided_password_is_hex_key = val;
|
2018-01-14 15:17:17 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-23 11:12:49 +00:00
|
|
|
void
|
|
|
|
QPDF::registerStreamFilter(
|
2023-05-21 17:35:09 +00:00
|
|
|
std::string const& filter_name, std::function<std::shared_ptr<QPDFStreamFilter>()> factory)
|
2020-12-23 11:12:49 +00:00
|
|
|
{
|
|
|
|
QPDF_Stream::registerStreamFilter(filter_name, factory);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::setIgnoreXRefStreams(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->ignore_xref_streams = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 17:30:42 +00:00
|
|
|
std::shared_ptr<QPDFLogger>
|
|
|
|
QPDF::getLogger()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->log;
|
2022-06-05 17:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::setLogger(std::shared_ptr<QPDFLogger> l)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->log = l;
|
2022-06-05 17:30:42 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 11:02:35 +00:00
|
|
|
void
|
|
|
|
QPDF::setOutputStreams(std::ostream* out, std::ostream* err)
|
|
|
|
{
|
2022-09-09 11:03:29 +00:00
|
|
|
setLogger(QPDFLogger::create());
|
2023-05-21 13:42:34 +00:00
|
|
|
m->log->setOutputStreams(out, err);
|
2010-10-01 11:02:35 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::setSuppressWarnings(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->suppress_warnings = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::setAttemptRecovery(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->attempt_recovery = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 03:11:38 +00:00
|
|
|
void
|
|
|
|
QPDF::setImmediateCopyFrom(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->immediate_copy_from = val;
|
2019-01-11 03:11:38 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 00:24:44 +00:00
|
|
|
std::vector<QPDFExc>
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF::getWarnings()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
std::vector<QPDFExc> result = m->warnings;
|
|
|
|
m->warnings.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-31 19:10:29 +00:00
|
|
|
bool
|
|
|
|
QPDF::anyWarnings() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return !m->warnings.empty();
|
2019-08-31 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 22:33:34 +00:00
|
|
|
size_t
|
|
|
|
QPDF::numWarnings() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->warnings.size();
|
2021-03-01 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 02:34:25 +00:00
|
|
|
bool
|
2022-05-21 11:41:09 +00:00
|
|
|
QPDF::validatePDFVersion(char const*& p, std::string& version)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-06 02:34:25 +00:00
|
|
|
bool valid = QUtil::is_digit(*p);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (valid) {
|
|
|
|
while (QUtil::is_digit(*p)) {
|
2017-08-06 02:34:25 +00:00
|
|
|
version.append(1, *p++);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((*p == '.') && QUtil::is_digit(*(p + 1))) {
|
2017-08-06 02:34:25 +00:00
|
|
|
version.append(1, *p++);
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_digit(*p)) {
|
2017-08-06 02:34:25 +00:00
|
|
|
version.append(1, *p++);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2017-08-06 02:34:25 +00:00
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
}
|
2022-05-21 11:41:09 +00:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::findHeader()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t global_offset = m->file->tell();
|
|
|
|
std::string line = m->file->readLine(1024);
|
2022-05-21 11:41:09 +00:00
|
|
|
char const* p = line.c_str();
|
|
|
|
if (strncmp(p, "%PDF-", 5) != 0) {
|
|
|
|
throw std::logic_error("findHeader is not looking at %PDF-");
|
|
|
|
}
|
|
|
|
p += 5;
|
|
|
|
std::string version;
|
2023-05-24 15:28:17 +00:00
|
|
|
// 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.
|
2022-05-21 11:41:09 +00:00
|
|
|
bool valid = validatePDFVersion(p, version);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (valid) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pdf_version = version;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (global_offset != 0) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Empirical evidence strongly suggests that when there is leading material prior to the
|
|
|
|
// PDF header, all explicit offsets in the file are such that 0 points to the beginning
|
|
|
|
// of the header.
|
2012-12-25 19:38:18 +00:00
|
|
|
QTC::TC("qpdf", "QPDF global offset");
|
2023-05-21 17:35:09 +00:00
|
|
|
m->file = std::shared_ptr<InputSource>(new OffsetInputSource(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()
|
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
if (readToken(m->file).isWord("startxref") && readToken(m->file).isInteger()) {
|
2022-09-29 13:33:11 +00:00
|
|
|
// Position in front of offset token
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(m->file->getLastOffset(), SEEK_SET);
|
2022-09-29 13:33:11 +00:00
|
|
|
return true;
|
2017-08-05 18:54:07 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-06 02:34:25 +00:00
|
|
|
void
|
|
|
|
QPDF::parse(char const* password)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (password) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encp->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);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->file->findFirst("%PDF-", 0, 1024, hf)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF not a pdf file");
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF("", 0, "can't find PDF header"));
|
2023-05-24 15:28:17 +00:00
|
|
|
// QPDFWriter writes files that usually require at least version 1.2 for /FlateDecode
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pdf_version = "1.2";
|
2017-07-28 02:27:00 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +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.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(0, SEEK_END);
|
|
|
|
qpdf_offset_t end_offset = 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;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->file->findLast("startxref", start_offset, 0, sf)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
xref_offset = QUtil::string_to_ll(readToken(m->file).getValue().c_str());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
|
|
|
if (xref_offset == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF can't find startxref");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("", 0, "can't find startxref");
|
2022-04-02 21:14:10 +00:00
|
|
|
}
|
|
|
|
try {
|
2020-11-04 12:46:46 +00:00
|
|
|
read_xref(xref_offset);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc&) {
|
2020-11-04 12:46:46 +00:00
|
|
|
throw;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("", 0, std::string("error reading xref: ") + e.what());
|
2022-04-02 21:14:10 +00:00
|
|
|
}
|
|
|
|
} catch (QPDFExc& e) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->attempt_recovery) {
|
2022-02-08 14:18:08 +00:00
|
|
|
reconstruct_xref(e);
|
|
|
|
QTC::TC("qpdf", "QPDF reconstructed xref table");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-03-19 16:57:27 +00:00
|
|
|
throw;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initializeEncryption();
|
2023-05-21 13:42:34 +00:00
|
|
|
m->parsed = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 16:50:36 +00:00
|
|
|
void
|
|
|
|
QPDF::inParse(bool v)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->in_parse == v) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// This happens if QPDFParser::parse tries to resolve an indirect object while it is
|
|
|
|
// parsing.
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("QPDF: re-entrant parsing detected. This is a qpdf bug."
|
|
|
|
" Please report at https://github.com/qpdf/qpdf/issues.");
|
2019-08-21 16:50:36 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->in_parse = v;
|
2019-08-21 16:50:36 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::warn(QPDFExc const& e)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->warnings.push_back(e);
|
|
|
|
if (!m->suppress_warnings) {
|
|
|
|
*m->log->getWarn() << "WARNING: " << m->warnings.back().what() << "\n";
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 22:03:44 +00:00
|
|
|
void
|
|
|
|
QPDF::warn(
|
|
|
|
qpdf_error_code_e error_code,
|
|
|
|
std::string const& object,
|
|
|
|
qpdf_offset_t offset,
|
|
|
|
std::string const& message)
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
warn(QPDFExc(error_code, getFilename(), object, offset, message));
|
2022-04-23 22:03:44 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::setTrailer(QPDFObjectHandle obj)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->trailer.isInitialized()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->trailer = obj;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::reconstruct_xref(QPDFExc& e)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->reconstructed_xref) {
|
2023-05-24 15:28:17 +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;
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->reconstructed_xref = true;
|
2022-11-24 16:50:46 +00:00
|
|
|
// We may find more objects, which may contain dangling references.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->fixed_dangling_refs = false;
|
2017-07-25 14:21:27 +00:00
|
|
|
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF("", 0, "file is damaged"));
|
2008-04-29 12:55:25 +00:00
|
|
|
warn(e);
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF("", 0, "Attempting to reconstruct cross-reference table"));
|
2009-10-19 23:09:19 +00:00
|
|
|
|
|
|
|
// Delete all references to type 1 (uncompressed) objects
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen> to_delete;
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->xref_table) {
|
2022-04-30 17:23:18 +00:00
|
|
|
if (iter.second.getType() == 1) {
|
|
|
|
to_delete.insert(iter.first);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& iter: to_delete) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->xref_table.erase(iter);
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(0, SEEK_END);
|
|
|
|
qpdf_offset_t eof = m->file->tell();
|
|
|
|
m->file->seek(0, SEEK_SET);
|
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;
|
2023-05-21 13:42:34 +00:00
|
|
|
while (m->file->tell() < eof) {
|
|
|
|
m->file->findAndSkipNextEOL();
|
|
|
|
qpdf_offset_t next_line_start = m->file->tell();
|
|
|
|
m->file->seek(line_start, SEEK_SET);
|
|
|
|
QPDFTokenizer::Token t1 = readToken(m->file, MAX_LEN);
|
2023-05-21 17:35:09 +00:00
|
|
|
qpdf_offset_t token_start = m->file->tell() - toO(t1.getValue().length());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (token_start >= next_line_start) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// don't process yet -- wait until we get to the line containing this token
|
2022-09-29 11:52:19 +00:00
|
|
|
} else if (t1.isInteger()) {
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFTokenizer::Token t2 = readToken(m->file, MAX_LEN);
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((t2.isInteger()) && (readToken(m->file, MAX_LEN).isWord("obj"))) {
|
2021-11-07 20:23:29 +00:00
|
|
|
int obj = QUtil::string_to_int(t1.getValue().c_str());
|
|
|
|
int gen = QUtil::string_to_int(t2.getValue().c_str());
|
2023-07-04 13:01:03 +00:00
|
|
|
insertReconstructedXrefEntry(obj, token_start, gen);
|
2017-08-10 23:00:06 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
} else if (!m->trailer.isInitialized() && t1.isWord("trailer")) {
|
2023-07-07 23:38:50 +00:00
|
|
|
QPDFObjectHandle t = readTrailer();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!t.isDictionary()) {
|
2021-11-07 20:23:29 +00:00
|
|
|
// Oh well. It was worth a try.
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2021-11-07 20:23:29 +00:00
|
|
|
setTrailer(t);
|
2017-08-10 23:00:06 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
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
|
|
|
}
|
2023-07-04 13:01:03 +00:00
|
|
|
m->deleted_objects.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2024-01-06 21:51:03 +00:00
|
|
|
if (!m->trailer.isInitialized()) {
|
|
|
|
qpdf_offset_t max_offset{0};
|
|
|
|
// If there are any xref streams, take the last one to appear.
|
|
|
|
for (auto const& iter: m->xref_table) {
|
|
|
|
auto entry = iter.second;
|
|
|
|
if (entry.getType() != 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto oh = getObjectByObjGen(iter.first);
|
|
|
|
try {
|
|
|
|
if (!oh.isStreamOfType("/XRef")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (std::exception&) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto offset = entry.getOffset();
|
|
|
|
if (offset > max_offset) {
|
|
|
|
max_offset = offset;
|
|
|
|
setTrailer(oh.getDict());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (max_offset > 0) {
|
|
|
|
try {
|
|
|
|
read_xref(max_offset);
|
|
|
|
} catch (std::exception&) {
|
2024-01-06 22:23:45 +00:00
|
|
|
throw damagedPDF(
|
|
|
|
"", 0, "error decoding candidate xref stream while recovering damaged file");
|
2024-01-06 21:51:03 +00:00
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDF recover xref stream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->trailer.isInitialized()) {
|
2023-05-24 15:28:17 +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.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("", 0, "unable to find trailer dictionary while recovering damaged file");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +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.
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-04-02 21:14:10 +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));
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(xref_offset, SEEK_SET);
|
2023-05-24 15:28:17 +00:00
|
|
|
// Some files miss the mark a little with startxref. We could do a better job of searching
|
|
|
|
// in the neighborhood for something that looks like either an xref table or stream, but the
|
|
|
|
// simple heuristic of skipping whitespace can help with the xref table case and is harmless
|
|
|
|
// with the stream case.
|
2019-08-20 01:43:28 +00:00
|
|
|
bool done = false;
|
|
|
|
bool skipped_space = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!done) {
|
2019-08-20 01:43:28 +00:00
|
|
|
char ch;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (1 == m->file->read(&ch, 1)) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QUtil::is_space(ch)) {
|
2019-08-20 01:43:28 +00:00
|
|
|
skipped_space = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->unreadCh(ch);
|
2019-08-20 01:43:28 +00:00
|
|
|
done = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDF eof skipping spaces before xref", skipped_space ? 0 : 1);
|
2019-08-20 01:43:28 +00:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->read(buf, sizeof(buf) - 1);
|
2023-05-24 15:28:17 +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.
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((strncmp(buf, "xref", 4) == 0) && QUtil::is_space(buf[4])) {
|
|
|
|
if (skipped_space) {
|
2019-08-20 01:43:28 +00:00
|
|
|
QTC::TC("qpdf", "QPDF xref skipped space");
|
2023-05-21 17:35:09 +00:00
|
|
|
warn(damagedPDF("", 0, "extraneous whitespace seen before xref"));
|
2019-08-20 01:43:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +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;
|
2023-05-24 15:28:17 +00:00
|
|
|
// buf is null-terminated, and QUtil::is_space('\0') is false, so this won't overrun.
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_space(buf[skip])) {
|
2017-08-10 00:46:02 +00:00
|
|
|
++skip;
|
|
|
|
}
|
|
|
|
xref_offset = read_xrefTable(xref_offset + skip);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
xref_offset = read_xrefStream(xref_offset);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (visited.count(xref_offset) != 0) {
|
2018-03-05 19:25:17 +00:00
|
|
|
QTC::TC("qpdf", "QPDF xref loop");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("", 0, "loop detected following xref tables");
|
2017-08-25 23:58:31 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->trailer.isInitialized()) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("", 0, "unable to find trailer while reading xref");
|
2012-06-24 19:26:28 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
int size = m->trailer.getKey("/Size").getIntValueAsInt();
|
2010-06-05 19:48:32 +00:00
|
|
|
int max_obj = 0;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->xref_table.empty()) {
|
|
|
|
max_obj = (*(m->xref_table.rbegin())).first.getObj();
|
2010-06-05 19:48:32 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->deleted_objects.empty()) {
|
|
|
|
max_obj = std::max(max_obj, *(m->deleted_objects.rbegin()));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((size < 1) || (size - 1 != max_obj)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF xref size mismatch");
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"",
|
|
|
|
0,
|
2022-09-28 11:25:52 +00:00
|
|
|
("reported number of objects (" + std::to_string(size) +
|
2023-05-21 17:35:09 +00:00
|
|
|
") is not one plus the highest object number (" + std::to_string(max_obj) + ")")));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// 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.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->deleted_objects.clear();
|
2024-01-07 22:05:35 +00:00
|
|
|
|
|
|
|
// Make sure we keep only the highest generation for any object.
|
2024-01-08 11:43:57 +00:00
|
|
|
QPDFObjGen last_og{-1, 0};
|
2024-01-17 10:39:06 +00:00
|
|
|
for (auto const& item: m->xref_table) {
|
|
|
|
auto id = item.first.getObj();
|
2024-02-04 21:11:53 +00:00
|
|
|
if (id == last_og.getObj() && id > 0) {
|
2024-01-08 11:43:57 +00:00
|
|
|
removeObject(last_og);
|
2024-02-04 21:11:53 +00:00
|
|
|
}
|
2024-01-17 10:39:06 +00:00
|
|
|
last_og = item.first;
|
2024-01-07 22:05:35 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:37:05 +00:00
|
|
|
bool
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::parse_xrefFirst(std::string const& line, int& obj, int& num, int& bytes)
|
2017-08-10 23:37:05 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// is_space and is_digit both return false on '\0', so this will not overrun the null-terminated
|
|
|
|
// buffer.
|
2017-08-10 23:37:05 +00:00
|
|
|
char const* p = line.c_str();
|
|
|
|
char const* start = line.c_str();
|
|
|
|
|
|
|
|
// Skip zero or more spaces
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
// Require digit
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string obj_str;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
obj_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Require space
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Skip spaces
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
// Require digit
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string num_str;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
num_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Skip any space including line terminators
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
++p;
|
|
|
|
}
|
2019-06-21 03:35:23 +00:00
|
|
|
bytes = toI(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
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::parse_xrefEntry(std::string const& line, qpdf_offset_t& f1, int& f2, char& type)
|
2017-08-10 23:37:05 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// is_space and is_digit both return false on '\0', so this will not overrun the null-terminated
|
|
|
|
// buffer.
|
2017-08-10 23:37:05 +00:00
|
|
|
char const* p = line.c_str();
|
|
|
|
|
|
|
|
// Skip zero or more spaces. There aren't supposed to be any.
|
|
|
|
bool invalid = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
++p;
|
|
|
|
QTC::TC("qpdf", "QPDF ignore first space in xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
// Require digit
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string f1_str;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
f1_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Require space
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QUtil::is_space(*(p + 1))) {
|
2017-08-10 23:37:05 +00:00
|
|
|
QTC::TC("qpdf", "QPDF ignore first extra space in xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
// Skip spaces
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
// Require digit
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Gather digits
|
|
|
|
std::string f2_str;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_digit(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
f2_str.append(1, *p++);
|
|
|
|
}
|
|
|
|
// Require space
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QUtil::is_space(*(p + 1))) {
|
2017-08-10 23:37:05 +00:00
|
|
|
QTC::TC("qpdf", "QPDF ignore second extra space in xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
// Skip spaces
|
2022-04-02 21:14:10 +00:00
|
|
|
while (QUtil::is_space(*p)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
++p;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((*p == 'f') || (*p == 'n')) {
|
2017-08-10 23:37:05 +00:00
|
|
|
type = *p;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2017-08-10 23:37:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((f1_str.length() != 10) || (f2_str.length() != 5)) {
|
2017-08-10 23:37:05 +00:00
|
|
|
QTC::TC("qpdf", "QPDF ignore length error xref entry");
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (invalid) {
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF("xref table", "accepting invalid xref table entry"));
|
2017-08-10 23:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(xref_offset, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
bool done = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!done) {
|
2013-12-14 20:08:54 +00:00
|
|
|
char linebuf[51];
|
|
|
|
memset(linebuf, 0, sizeof(linebuf));
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->read(linebuf, sizeof(linebuf) - 1);
|
2022-02-08 14:18:08 +00:00
|
|
|
std::string line = linebuf;
|
2017-08-10 23:37:05 +00:00
|
|
|
int obj = 0;
|
|
|
|
int num = 0;
|
|
|
|
int bytes = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!parse_xrefFirst(line, obj, num, bytes)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF invalid xref");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("xref table", "xref syntax invalid");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(m->file->getLastOffset() + bytes, SEEK_SET);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (qpdf_offset_t i = obj; i - num < obj; ++i) {
|
|
|
|
if (i == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// This is needed by checkLinearization()
|
2023-05-21 13:42:34 +00:00
|
|
|
m->first_xref_item_offset = m->file->tell();
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
std::string xref_entry = m->file->readLine(30);
|
2017-08-10 23:37:05 +00:00
|
|
|
// For xref_table, these will always be small enough to be ints
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t f1 = 0;
|
|
|
|
int f2 = 0;
|
|
|
|
char type = '\0';
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!parse_xrefEntry(xref_entry, f1, f2, type)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF invalid xref entry");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
"xref table", "invalid xref entry (obj=" + std::to_string(i) + ")");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (type == 'f') {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Save deleted items until after we've checked the XRefStm, if any.
|
2023-05-27 22:49:18 +00:00
|
|
|
deleted_items.emplace_back(toI(i), f2);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
insertXrefEntry(toI(i), 1, f1, f2);
|
|
|
|
}
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t pos = m->file->tell();
|
2022-09-29 13:33:11 +00:00
|
|
|
if (readToken(m->file).isWord("trailer")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
done = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(pos, SEEK_SET);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set offset to previous xref table if any
|
2023-07-07 23:38:50 +00:00
|
|
|
QPDFObjectHandle cur_trailer = readTrailer();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!cur_trailer.isDictionary()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF missing trailer");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("", "expected trailer dictionary");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->trailer.isInitialized()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
setTrailer(cur_trailer);
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->trailer.hasKey("/Size")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF trailer lacks size");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("trailer", "trailer dictionary lacks /Size key");
|
2022-04-02 21:14:10 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->trailer.getKey("/Size").isInteger()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF trailer size not integer");
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("trailer", "/Size key in trailer dictionary is not an integer");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cur_trailer.hasKey("/XRefStm")) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->ignore_xref_streams) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF ignoring XRefStm in trailer");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
if (cur_trailer.getKey("/XRefStm").isInteger()) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Read the xref stream but disregard any return value -- we'll use our trailer's
|
|
|
|
// /Prev key instead of the xref stream's.
|
2023-05-21 17:35:09 +00:00
|
|
|
(void)read_xrefStream(cur_trailer.getKey("/XRefStm").getIntValue());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("xref stream", xref_offset, "invalid /XRefStm");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle any deleted items now that we've read the /XRefStm.
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: deleted_items) {
|
2023-07-04 15:34:22 +00:00
|
|
|
insertFreeXrefEntry(og);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cur_trailer.hasKey("/Prev")) {
|
|
|
|
if (!cur_trailer.getKey("/Prev").isInteger()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF trailer prev not integer");
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("trailer", "/Prev key in trailer dictionary is not an integer");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDF prev key in trailer dictionary");
|
|
|
|
xref_offset = cur_trailer.getKey("/Prev").getIntValue();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
xref_offset = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->ignore_xref_streams) {
|
2022-07-23 12:47:18 +00:00
|
|
|
QPDFObjGen x_og;
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle xref_obj;
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2023-05-21 17:35:09 +00:00
|
|
|
xref_obj =
|
|
|
|
readObjectAtOffset(false, xref_offset, "xref stream", QPDFObjGen(0, 0), x_og, true);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc&) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// ignore -- report error below
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (xref_obj.isStreamOfType("/XRef")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF found xref stream");
|
2023-07-04 16:07:44 +00:00
|
|
|
return processXRefStream(xref_offset, xref_obj);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 16:07:44 +00:00
|
|
|
QTC::TC("qpdf", "QPDF can't find xref");
|
|
|
|
throw damagedPDF("", xref_offset, "xref not found");
|
|
|
|
return 0; // unreachable
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
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");
|
2023-05-21 17:35:09 +00:00
|
|
|
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()))) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"xref stream",
|
|
|
|
xref_offset,
|
2022-09-28 11:25:52 +00:00
|
|
|
"Cross-reference stream does not have proper /W and /Index keys");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-10-05 16:28:52 +00:00
|
|
|
|
|
|
|
int W[3];
|
|
|
|
size_t entry_size = 0;
|
|
|
|
int max_bytes = sizeof(qpdf_offset_t);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
W[i] = W_obj.getArrayItem(i).getIntValueAsInt();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (W[i] > max_bytes) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"xref stream",
|
|
|
|
xref_offset,
|
2022-09-28 11:25:52 +00:00
|
|
|
"Cross-reference stream's /W contains impossibly large values");
|
2013-10-05 16:28:52 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
entry_size += toS(W[i]);
|
2013-10-05 16:28:52 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (entry_size == 0) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
"xref stream", xref_offset, "Cross-reference stream's /W indicates entry size of 0");
|
2017-08-12 00:10:28 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
unsigned long long max_num_entries = static_cast<unsigned long long>(-1) / entry_size;
|
2013-10-05 16:28:52 +00:00
|
|
|
|
|
|
|
std::vector<long long> indx;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (Index_obj.isArray()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int n_index = Index_obj.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((n_index % 2) || (n_index < 2)) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"xref stream",
|
|
|
|
xref_offset,
|
2022-09-28 11:25:52 +00:00
|
|
|
"Cross-reference stream's /Index has an invalid number of "
|
|
|
|
"values");
|
2022-04-02 21:14:10 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < n_index; ++i) {
|
|
|
|
if (Index_obj.getArrayItem(i).isInteger()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
indx.push_back(Index_obj.getArrayItem(i).getIntValue());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"xref stream",
|
|
|
|
xref_offset,
|
2023-05-21 17:35:09 +00:00
|
|
|
("Cross-reference stream's /Index's item " + std::to_string(i) +
|
|
|
|
" is not an integer"));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC("qpdf", "QPDF xref /Index is array", n_index == 2 ? 0 : 1);
|
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF xref /Index is null");
|
|
|
|
long long size = dict.getKey("/Size").getIntValue();
|
|
|
|
indx.push_back(0);
|
|
|
|
indx.push_back(size);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t num_entries = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 1; i < indx.size(); i += 2) {
|
|
|
|
if (indx.at(i) > QIntC::to_longlong(max_num_entries - num_entries)) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"xref stream",
|
|
|
|
xref_offset,
|
2022-09-28 11:25:52 +00:00
|
|
|
("Cross-reference stream claims to contain too many entries: " +
|
2023-05-21 17:35:09 +00:00
|
|
|
std::to_string(indx.at(i)) + " " + std::to_string(max_num_entries) + " " +
|
2022-09-21 16:49:21 +00:00
|
|
|
std::to_string(num_entries)));
|
2013-10-05 16:28:52 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
num_entries += toS(indx.at(i));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +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
|
|
|
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<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
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (expected_size != actual_size) {
|
2022-09-28 11:25:52 +00:00
|
|
|
QPDFExc x = damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"xref stream",
|
|
|
|
xref_offset,
|
2022-09-28 11:25:52 +00:00
|
|
|
("Cross-reference stream data has the wrong size; expected = " +
|
2023-05-21 17:35:09 +00:00
|
|
|
std::to_string(expected_size) + "; actual = " + std::to_string(actual_size)));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (expected_size > actual_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw x;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
warn(x);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t cur_chunk = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
int chunk_count = 0;
|
|
|
|
|
|
|
|
bool saw_first_compressed_object = false;
|
|
|
|
|
2023-05-24 15:28:17 +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();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < num_entries; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Read this entry
|
|
|
|
unsigned char const* entry = data + (entry_size * i);
|
|
|
|
qpdf_offset_t fields[3];
|
|
|
|
unsigned char const* p = entry;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int j = 0; j < 3; ++j) {
|
2022-02-08 14:18:08 +00:00
|
|
|
fields[j] = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((j == 0) && (W[0] == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF default for xref stream field 0");
|
|
|
|
fields[0] = 1;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int k = 0; k < W[j]; ++k) {
|
2022-02-08 14:18:08 +00:00
|
|
|
fields[j] <<= 8;
|
|
|
|
fields[j] += toI(*p++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +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.
|
2022-02-08 14:18:08 +00:00
|
|
|
int obj = toI(indx.at(cur_chunk));
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((obj < 0) || ((std::numeric_limits<int>::max() - obj) < chunk_count)) {
|
2019-09-17 23:48:27 +00:00
|
|
|
std::ostringstream msg;
|
2020-10-21 19:29:28 +00:00
|
|
|
msg.imbue(std::locale::classic());
|
2019-09-17 23:48:27 +00:00
|
|
|
msg << "adding " << chunk_count << " to " << obj
|
2023-05-24 15:28:17 +00:00
|
|
|
<< " while computing index in xref stream would cause an integer overflow";
|
2019-09-17 23:48:27 +00:00
|
|
|
throw std::range_error(msg.str());
|
|
|
|
}
|
|
|
|
obj += chunk_count;
|
2022-02-08 14:18:08 +00:00
|
|
|
++chunk_count;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (chunk_count >= indx.at(cur_chunk + 1)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
cur_chunk += 2;
|
|
|
|
chunk_count = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (saw_first_compressed_object) {
|
|
|
|
if (fields[0] != 2) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->uncompressed_after_compressed = true;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (fields[0] == 2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
saw_first_compressed_object = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// This is needed by checkLinearization()
|
2023-05-21 13:42:34 +00:00
|
|
|
m->first_xref_item_offset = xref_offset;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (fields[0] == 0) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Ignore fields[2], which we don't care about in this case. This works around the issue
|
|
|
|
// of some PDF files that put invalid values, like -1, here for deleted objects.
|
2023-07-04 15:34:22 +00:00
|
|
|
insertFreeXrefEntry(QPDFObjGen(obj, 0));
|
|
|
|
} else {
|
|
|
|
insertXrefEntry(obj, toI(fields[0]), fields[1], toI(fields[2]));
|
2020-11-04 12:46:46 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->trailer.isInitialized()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
setTrailer(dict);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (dict.hasKey("/Prev")) {
|
|
|
|
if (!dict.getKey("/Prev").isInteger()) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
"xref stream", "/Prev key in xref stream dictionary is not an integer");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDF prev key in xref stream dictionary");
|
|
|
|
xref_offset = dict.getKey("/Prev").getIntValue();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
xref_offset = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return xref_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-07-04 13:24:30 +00:00
|
|
|
QPDF::insertXrefEntry(int obj, int f0, qpdf_offset_t f1, int f2)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +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 table in which it appears, is the one that gets stored.
|
2023-07-04 13:24:30 +00:00
|
|
|
// This works because we are reading more recent appends before older ones.
|
2023-05-24 15:28:17 +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.
|
2023-07-04 13:24:30 +00:00
|
|
|
|
|
|
|
if (m->deleted_objects.count(obj)) {
|
|
|
|
QTC::TC("qpdf", "QPDF xref deleted object");
|
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 15:34:22 +00:00
|
|
|
auto [iter, created] = m->xref_table.try_emplace(QPDFObjGen(obj, (f0 == 2 ? 0 : f2)));
|
|
|
|
if (!created) {
|
|
|
|
QTC::TC("qpdf", "QPDF xref reused object");
|
|
|
|
return;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-07-04 15:34:22 +00:00
|
|
|
switch (f0) {
|
2022-04-02 21:14:10 +00:00
|
|
|
case 1:
|
2022-02-08 14:18:08 +00:00
|
|
|
// f2 is generation
|
|
|
|
QTC::TC("qpdf", "QPDF xref gen > 0", ((f2 > 0) ? 1 : 0));
|
2023-07-04 15:34:22 +00:00
|
|
|
iter->second = QPDFXRefEntry(f1);
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case 2:
|
2023-07-04 15:34:22 +00:00
|
|
|
iter->second = QPDFXRefEntry(toI(f1), f2);
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("xref stream", "unknown xref stream entry type " + std::to_string(f0));
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-04 13:01:03 +00:00
|
|
|
|
2023-07-04 15:34:22 +00:00
|
|
|
void
|
|
|
|
QPDF::insertFreeXrefEntry(QPDFObjGen og)
|
|
|
|
{
|
|
|
|
if (!m->xref_table.count(og)) {
|
|
|
|
m->deleted_objects.insert(og.getObj());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:01:03 +00:00
|
|
|
// Replace uncompressed object. This is used in xref recovery mode, which reads the file from
|
|
|
|
// beginning to end.
|
|
|
|
void
|
|
|
|
QPDF::insertReconstructedXrefEntry(int obj, qpdf_offset_t f1, int f2)
|
|
|
|
{
|
2024-01-17 14:07:37 +00:00
|
|
|
if (!(obj > 0 && 0 <= f2 && f2 < 65535)) {
|
|
|
|
QTC::TC("qpdf", "QPDF xref overwrite invalid objgen");
|
|
|
|
return;
|
|
|
|
}
|
2023-07-04 13:01:03 +00:00
|
|
|
QPDFObjGen og(obj, f2);
|
|
|
|
if (!m->deleted_objects.count(obj)) {
|
|
|
|
// deleted_objects stores the uncompressed objects removed from the xref table at the start
|
|
|
|
// of recovery.
|
|
|
|
QTC::TC("qpdf", "QPDF xref overwrite object");
|
|
|
|
m->xref_table[QPDFObjGen(obj, f2)] = QPDFXRefEntry(f1);
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::showXRefTable()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
auto& cout = *m->log->getInfo();
|
|
|
|
for (auto const& iter: m->xref_table) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjGen const& og = iter.first;
|
|
|
|
QPDFXRefEntry const& entry = iter.second;
|
2022-07-23 15:48:28 +00:00
|
|
|
cout << og.unparse('/') << ": ";
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (entry.getType()) {
|
|
|
|
case 1:
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << "uncompressed; offset = " << entry.getOffset();
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case 2:
|
2023-05-21 17:35:09 +00:00
|
|
|
*m->log->getInfo() << "compressed; stream = " << entry.getObjStreamNumber()
|
|
|
|
<< ", index = " << entry.getObjStreamIndex();
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error("unknown cross-reference table type while"
|
|
|
|
" showing xref_table");
|
|
|
|
break;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->log->info("\n");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Resolve all objects in the xref table. If this triggers a xref table reconstruction abort and
|
|
|
|
// return false. Otherwise return true.
|
2022-11-24 16:50:46 +00:00
|
|
|
bool
|
|
|
|
QPDF::resolveXRefTable()
|
2019-01-04 15:17:33 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
bool may_change = !m->reconstructed_xref;
|
|
|
|
for (auto& iter: m->xref_table) {
|
2022-11-24 16:50:46 +00:00
|
|
|
if (isUnresolved(iter.first)) {
|
|
|
|
resolve(iter.first);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (may_change && m->reconstructed_xref) {
|
2022-11-24 16:50:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-04 15:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-24 16:50:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-26 21:43:41 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Ensure all objects in the pdf file, including those in indirect references, appear in the object
|
|
|
|
// cache.
|
2022-11-24 16:50:46 +00:00
|
|
|
void
|
|
|
|
QPDF::fixDanglingReferences(bool force)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->fixed_dangling_refs) {
|
2022-11-24 16:50:46 +00:00
|
|
|
return;
|
2022-11-26 21:43:41 +00:00
|
|
|
}
|
2022-11-24 16:50:46 +00:00
|
|
|
if (!resolveXRefTable()) {
|
2022-11-26 21:43:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF fix dangling triggered xref reconstruction");
|
2022-11-24 16:50:46 +00:00
|
|
|
resolveXRefTable();
|
2022-11-24 16:50:46 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->fixed_dangling_refs = true;
|
2019-01-04 15:17:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 18:52:03 +00:00
|
|
|
size_t
|
|
|
|
QPDF::getObjectCount()
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// This method returns the next available indirect object number. makeIndirectObject uses it for
|
|
|
|
// this purpose. After fixDanglingReferences is called, all objects in the xref table will also
|
|
|
|
// be in obj_cache.
|
2019-01-04 15:17:33 +00:00
|
|
|
fixDanglingReferences();
|
2022-09-14 11:35:32 +00:00
|
|
|
QPDFObjGen og;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->obj_cache.empty()) {
|
|
|
|
og = (*(m->obj_cache.rbegin())).first;
|
2018-06-22 18:52:03 +00:00
|
|
|
}
|
2019-06-21 03:35:23 +00:00
|
|
|
return toS(og.getObj());
|
2018-06-22 18:52:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 23:18:57 +00:00
|
|
|
std::vector<QPDFObjectHandle>
|
|
|
|
QPDF::getAllObjects()
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// After fixDanglingReferences is called, all objects are in the object cache.
|
2022-11-24 16:50:46 +00:00
|
|
|
fixDanglingReferences();
|
2017-07-28 23:18:57 +00:00
|
|
|
std::vector<QPDFObjectHandle> result;
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->obj_cache) {
|
2022-08-02 15:20:24 +00:00
|
|
|
result.push_back(newIndirect(iter.first, iter.second.object));
|
2017-07-28 23:18:57 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:09:19 +00:00
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::setLastObjectDescription(std::string const& description, QPDFObjGen const& og)
|
2009-10-19 23:09:19 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->last_object_description.clear();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!description.empty()) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->last_object_description += description;
|
2022-07-23 15:48:28 +00:00
|
|
|
if (og.isIndirect()) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->last_object_description += ": ";
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
2022-07-23 15:48:28 +00:00
|
|
|
if (og.isIndirect()) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->last_object_description += "object " + og.unparse(' ');
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 23:38:50 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::readTrailer()
|
|
|
|
{
|
|
|
|
qpdf_offset_t offset = m->file->tell();
|
|
|
|
bool empty = false;
|
|
|
|
auto object = QPDFParser(m->file, "trailer", m->tokenizer, nullptr, this).parse(empty, false);
|
|
|
|
if (empty) {
|
|
|
|
// 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(damagedPDF("trailer", "empty object treated as null"));
|
|
|
|
} else if (object.isDictionary() && readToken(m->file).isWord("stream")) {
|
|
|
|
warn(damagedPDF("trailer", m->file->tell(), "stream keyword found in trailer"));
|
|
|
|
}
|
|
|
|
// Override last_offset so that it points to the beginning of the object we just read
|
|
|
|
m->file->setLastOffset(offset);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
2023-07-07 19:37:13 +00:00
|
|
|
QPDF::readObject(std::string const& description, QPDFObjGen og)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-07-23 15:48:28 +00:00
|
|
|
setLastObjectDescription(description, og);
|
2023-07-07 19:37:13 +00:00
|
|
|
qpdf_offset_t offset = m->file->tell();
|
2012-07-21 13:00:06 +00:00
|
|
|
bool empty = false;
|
2023-07-08 15:20:01 +00:00
|
|
|
|
|
|
|
StringDecrypter decrypter{this, og};
|
|
|
|
StringDecrypter* decrypter_ptr = m->encp->encrypted ? &decrypter : nullptr;
|
|
|
|
auto object = QPDFParser(m->file, m->last_object_description, m->tokenizer, decrypter_ptr, this)
|
2023-05-21 17:35:09 +00:00
|
|
|
.parse(empty, false);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (empty) {
|
2023-05-24 15:28:17 +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.
|
2023-07-07 19:37:13 +00:00
|
|
|
warn(damagedPDF(m->file, m->file->getLastOffset(), "empty object treated as null"));
|
2023-07-08 15:20:01 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
auto token = readToken(m->file);
|
|
|
|
if (object.isDictionary() && token.isWord("stream")) {
|
|
|
|
readStream(object, og, offset);
|
|
|
|
token = readToken(m->file);
|
|
|
|
}
|
|
|
|
if (!token.isWord("endobj")) {
|
|
|
|
QTC::TC("qpdf", "QPDF err expected endobj");
|
|
|
|
warn(damagedPDF("expected endobj"));
|
2023-07-08 10:38:23 +00:00
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After reading stream dictionary and stream keyword, read rest of stream.
|
|
|
|
void
|
|
|
|
QPDF::readStream(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset)
|
2023-07-08 11:17:48 +00:00
|
|
|
{
|
|
|
|
validateStreamLineEnd(object, og, offset);
|
|
|
|
|
|
|
|
// Must get offset before accessing any additional objects since resolving a previously
|
|
|
|
// unresolved indirect object will change file position.
|
|
|
|
qpdf_offset_t stream_offset = m->file->tell();
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto length_obj = object.getKey("/Length");
|
|
|
|
|
|
|
|
if (!length_obj.isInteger()) {
|
|
|
|
if (length_obj.isNull()) {
|
|
|
|
QTC::TC("qpdf", "QPDF stream without length");
|
|
|
|
throw damagedPDF(offset, "stream dictionary lacks /Length key");
|
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDF stream length not integer");
|
|
|
|
throw damagedPDF(offset, "/Length key in stream dictionary is not an integer");
|
|
|
|
}
|
|
|
|
|
|
|
|
length = toS(length_obj.getUIntValue());
|
|
|
|
// Seek in two steps to avoid potential integer overflow
|
|
|
|
m->file->seek(stream_offset, SEEK_SET);
|
|
|
|
m->file->seek(toO(length), SEEK_CUR);
|
|
|
|
if (!readToken(m->file).isWord("endstream")) {
|
|
|
|
QTC::TC("qpdf", "QPDF missing endstream");
|
|
|
|
throw damagedPDF("expected endstream");
|
|
|
|
}
|
|
|
|
} catch (QPDFExc& e) {
|
|
|
|
if (m->attempt_recovery) {
|
|
|
|
warn(e);
|
|
|
|
length = recoverStreamLength(m->file, og, stream_offset);
|
|
|
|
} else {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
object = newIndirect(og, QPDF_Stream::create(this, og, object, stream_offset, length));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::validateStreamLineEnd(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset)
|
2023-07-08 10:38:23 +00:00
|
|
|
{
|
|
|
|
// 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. We have also seen files that have extraneous whitespace between the stream
|
|
|
|
// keyword and the newline.
|
2023-07-08 10:53:48 +00:00
|
|
|
while (true) {
|
2023-07-08 10:38:23 +00:00
|
|
|
char ch;
|
|
|
|
if (m->file->read(&ch, 1) == 0) {
|
|
|
|
// A premature EOF here will result in some other problem that will get reported at
|
|
|
|
// another time.
|
2023-07-08 11:17:48 +00:00
|
|
|
return;
|
2023-07-08 10:53:48 +00:00
|
|
|
}
|
|
|
|
if (ch == '\n') {
|
2023-07-08 10:38:23 +00:00
|
|
|
// ready to read stream data
|
|
|
|
QTC::TC("qpdf", "QPDF stream with NL only");
|
2023-07-08 11:17:48 +00:00
|
|
|
return;
|
2023-07-08 10:53:48 +00:00
|
|
|
}
|
|
|
|
if (ch == '\r') {
|
2023-07-08 10:38:23 +00:00
|
|
|
// Read another character
|
|
|
|
if (m->file->read(&ch, 1) != 0) {
|
|
|
|
if (ch == '\n') {
|
|
|
|
// Ready to read stream data
|
|
|
|
QTC::TC("qpdf", "QPDF stream with CRNL");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-07-08 10:38:23 +00:00
|
|
|
// 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");
|
2023-07-07 19:37:13 +00:00
|
|
|
m->file->unreadCh(ch);
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF(
|
2023-07-08 10:38:23 +00:00
|
|
|
m->file->tell(), "stream keyword followed by carriage return only"));
|
2012-07-21 13:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-08 11:17:48 +00:00
|
|
|
return;
|
2023-07-08 10:53:48 +00:00
|
|
|
}
|
|
|
|
if (!QUtil::is_space(ch)) {
|
2023-07-08 10:38:23 +00:00
|
|
|
QTC::TC("qpdf", "QPDF stream without newline");
|
|
|
|
m->file->unreadCh(ch);
|
|
|
|
warn(damagedPDF(
|
|
|
|
m->file->tell(), "stream keyword not followed by proper line terminator"));
|
2023-07-08 11:17:48 +00:00
|
|
|
return;
|
2023-07-08 10:38:23 +00:00
|
|
|
}
|
2023-07-08 10:53:48 +00:00
|
|
|
warn(damagedPDF(m->file->tell(), "stream keyword followed by extraneous whitespace"));
|
2023-07-08 10:38:23 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:14:59 +00:00
|
|
|
QPDFObjectHandle
|
2023-07-08 13:04:10 +00:00
|
|
|
QPDF::readObjectInStream(std::shared_ptr<InputSource>& input, int obj)
|
2023-07-07 19:14:59 +00:00
|
|
|
{
|
2023-07-08 13:04:10 +00:00
|
|
|
m->last_object_description.erase(7); // last_object_description starts with "object "
|
|
|
|
m->last_object_description += std::to_string(obj);
|
|
|
|
m->last_object_description += " 0";
|
2023-07-07 19:14:59 +00:00
|
|
|
|
|
|
|
bool empty = false;
|
|
|
|
auto object = QPDFParser(input, m->last_object_description, m->tokenizer, nullptr, this)
|
|
|
|
.parse(empty, false);
|
|
|
|
if (empty) {
|
|
|
|
// 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(damagedPDF(input, input->getLastOffset(), "empty object treated as null"));
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2017-08-10 01:14:48 +00:00
|
|
|
bool
|
|
|
|
QPDF::findEndstream()
|
|
|
|
{
|
|
|
|
// Find endstream or endobj. Position the input at that token.
|
2022-09-29 13:33:11 +00:00
|
|
|
auto t = readToken(m->file, 20);
|
|
|
|
if (t.isWord("endobj") || t.isWord("endstream")) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(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
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::recoverStreamLength(
|
2023-05-21 17:35:09 +00:00
|
|
|
std::shared_ptr<InputSource> input, QPDFObjGen const& og, qpdf_offset_t stream_offset)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// Try to reconstruct stream length by looking for endstream or endobj
|
2023-05-21 17:35:09 +00:00
|
|
|
warn(damagedPDF(input, stream_offset, "attempting to recover stream length"));
|
2008-04-29 12:55:25 +00:00
|
|
|
|
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;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->file->findFirst("end", stream_offset, 0, ef)) {
|
|
|
|
length = toS(m->file->tell() - stream_offset);
|
2017-08-10 01:14:48 +00:00
|
|
|
// Reread endstream but, if it was endobj, don't skip that.
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFTokenizer::Token t = readToken(m->file);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (t.getValue() == "endobj") {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(m->file->getLastOffset(), SEEK_SET);
|
2017-08-10 01:14:48 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (length) {
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t this_obj_offset = 0;
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDFObjGen this_og;
|
2022-02-08 14:18:08 +00:00
|
|
|
|
|
|
|
// Make sure this is inside this object
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->xref_table) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFXRefEntry const& entry = iter.second;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (entry.getType() == 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t obj_offset = entry.getOffset();
|
|
|
|
if ((obj_offset > stream_offset) &&
|
2023-05-21 17:35:09 +00:00
|
|
|
((this_obj_offset == 0) || (this_obj_offset > obj_offset))) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this_obj_offset = obj_offset;
|
2022-07-23 15:48:28 +00:00
|
|
|
this_og = iter.first;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 15:48:28 +00:00
|
|
|
if (this_obj_offset && (this_og == og)) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Well, we found endstream\nendobj within the space allowed for this object, so we're
|
|
|
|
// probably in good shape.
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF found wrong endstream in recovery");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (length == 0) {
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
input, stream_offset, "unable to recover stream data; treating stream as empty"));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 17:35:09 +00:00
|
|
|
warn(
|
|
|
|
damagedPDF(input, stream_offset, "recovered stream length: " + std::to_string(length)));
|
2017-07-29 23:15:06 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QTC::TC("qpdf", "QPDF recovered stream length");
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFTokenizer::Token
|
2022-04-09 18:35:56 +00:00
|
|
|
QPDF::readToken(std::shared_ptr<InputSource> input, size_t max_len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
return m->tokenizer.readToken(input, m->last_object_description, true, max_len);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::readObjectAtOffset(
|
|
|
|
bool try_recovery,
|
|
|
|
qpdf_offset_t offset,
|
|
|
|
std::string const& description,
|
2022-11-24 16:50:46 +00:00
|
|
|
QPDFObjGen exp_og,
|
2022-11-19 21:01:54 +00:00
|
|
|
QPDFObjGen& og,
|
|
|
|
bool skip_cache_if_in_xref)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-07-24 13:40:11 +00:00
|
|
|
bool check_og = true;
|
2022-07-23 12:47:18 +00:00
|
|
|
if (exp_og.getObj() == 0) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// This method uses an expect object ID of 0 to indicate that we don't know or don't care
|
|
|
|
// what the actual object ID is at this offset. This is true when we read the xref stream
|
|
|
|
// and linearization hint streams. In this case, we don't verify the expect object
|
|
|
|
// ID/generation against what was read from the file. There is also no reason to attempt
|
|
|
|
// xref recovery if we get a failure in this case since the read attempt was not triggered
|
|
|
|
// by an xref lookup.
|
2022-07-24 13:40:11 +00:00
|
|
|
check_og = false;
|
|
|
|
try_recovery = false;
|
|
|
|
}
|
2022-09-13 15:21:29 +00:00
|
|
|
setLastObjectDescription(description, exp_og);
|
2022-07-24 13:40:11 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->attempt_recovery) {
|
2017-07-28 02:27:00 +00:00
|
|
|
try_recovery = false;
|
|
|
|
}
|
2012-11-20 18:15:14 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Special case: if offset is 0, just return null. Some PDF writers, in particular
|
|
|
|
// "Mac OS X 10.7.5 Quartz PDFContext", may store deleted objects in the xref table as
|
2023-05-29 18:26:18 +00:00
|
|
|
// "0000000000 00000 n", which is not correct, but it won't hurt anything for us to ignore
|
|
|
|
// these.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (offset == 0) {
|
2012-11-20 18:15:14 +00:00
|
|
|
QTC::TC("qpdf", "QPDF bogus 0 offset", 0);
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF(0, "object has offset 0"));
|
2012-11-20 18:15:14 +00:00
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(offset, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFTokenizer::Token tobjid = readToken(m->file);
|
|
|
|
QPDFTokenizer::Token tgen = readToken(m->file);
|
|
|
|
QPDFTokenizer::Token tobj = readToken(m->file);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-09-29 11:52:19 +00:00
|
|
|
bool objidok = tobjid.isInteger();
|
2022-09-29 13:33:11 +00:00
|
|
|
bool genok = tgen.isInteger();
|
|
|
|
bool objok = tobj.isWord("obj");
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
|
|
|
if (!(objidok && genok && objok)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF expected n n obj");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(offset, "expected n n obj");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-07-23 12:47:18 +00:00
|
|
|
int objid = QUtil::string_to_int(tobjid.getValue().c_str());
|
|
|
|
int generation = QUtil::string_to_int(tgen.getValue().c_str());
|
|
|
|
og = QPDFObjGen(objid, generation);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (objid == 0) {
|
2017-07-26 08:30:32 +00:00
|
|
|
QTC::TC("qpdf", "QPDF object id 0");
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(offset, "object with ID 0");
|
2017-07-26 08:30:32 +00:00
|
|
|
}
|
2022-07-23 12:47:18 +00:00
|
|
|
if (check_og && (exp_og != og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF err wrong objid/generation");
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFExc e = damagedPDF(offset, "expected " + exp_og.unparse(' ') + " obj");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (try_recovery) {
|
2017-07-28 02:27:00 +00:00
|
|
|
// Will be retried below
|
|
|
|
throw e;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-24 15:28:17 +00:00
|
|
|
// We can try reading the object anyway even if the ID doesn't match.
|
2017-07-28 02:27:00 +00:00
|
|
|
warn(e);
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
2022-07-24 13:40:11 +00:00
|
|
|
if (try_recovery) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Try again after reconstructing xref table
|
|
|
|
reconstruct_xref(e);
|
2023-05-21 17:35:09 +00:00
|
|
|
if (m->xref_table.count(exp_og) && (m->xref_table[exp_og].getType() == 1)) {
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t new_offset = m->xref_table[exp_og].getOffset();
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFObjectHandle result =
|
|
|
|
readObjectAtOffset(false, new_offset, description, exp_og, og, false);
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF recovered in readObjectAtOffset");
|
|
|
|
return result;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF object gone after xref reconstruction");
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"",
|
|
|
|
0,
|
2022-09-28 11:25:52 +00:00
|
|
|
("object " + exp_og.unparse(' ') +
|
|
|
|
" not found in file after regenerating cross reference "
|
|
|
|
"table")));
|
2022-02-08 14:18:08 +00:00
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-03-19 16:57:27 +00:00
|
|
|
throw;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:37:13 +00:00
|
|
|
QPDFObjectHandle oh = readObject(description, og);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-08-10 12:16:06 +00:00
|
|
|
if (isUnresolved(og)) {
|
2023-05-24 15:28:17 +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.
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t end_before_space = m->file->tell();
|
2022-02-08 14:18:08 +00:00
|
|
|
|
|
|
|
// skip over spaces
|
2022-04-02 21:14:10 +00:00
|
|
|
while (true) {
|
2022-02-08 14:18:08 +00:00
|
|
|
char ch;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->file->read(&ch, 1)) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!isspace(static_cast<unsigned char>(ch))) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(-1, SEEK_CUR);
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(m->file->tell(), "EOF after endobj");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t end_after_space = m->file->tell();
|
|
|
|
if (skip_cache_if_in_xref && m->xref_table.count(og)) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Ordinarily, an object gets read here when resolved through xref table or stream. In
|
|
|
|
// the special case of the xref stream and linearization hint tables, the offset comes
|
|
|
|
// from another source. For the specific case of xref streams, the xref stream is read
|
|
|
|
// and loaded into the object cache very early in parsing. Ordinarily, when a file is
|
|
|
|
// updated by appending, items inserted into the xref table in later updates take
|
|
|
|
// precedence over earlier items. In the special case of reusing the object number
|
|
|
|
// previously used as the xref stream, we have the following order of events:
|
2022-11-19 21:01:54 +00:00
|
|
|
//
|
|
|
|
// * reused object gets loaded into the xref table
|
|
|
|
// * old object is read here while reading xref streams
|
|
|
|
// * original xref entry is ignored (since already in xref table)
|
|
|
|
//
|
2023-05-24 15:28:17 +00:00
|
|
|
// It is the second step that causes a problem. Even though the xref table is correct in
|
|
|
|
// this case, the old object is already in the cache and so effectively prevails over
|
|
|
|
// the reused object. To work around this issue, we have a special case for the xref
|
|
|
|
// stream (via the skip_cache_if_in_xref): if the object is already in the xref stream,
|
|
|
|
// don't cache what we read here.
|
2022-11-19 21:01:54 +00:00
|
|
|
//
|
2023-05-24 15:28:17 +00:00
|
|
|
// It is likely that the same bug may exist for linearization hint tables, but the
|
|
|
|
// existing code uses end_before_space and end_after_space from the cache, so fixing
|
|
|
|
// that would require more significant rework. The chances of a linearization hint
|
|
|
|
// stream being reused seems smaller because the xref stream is probably the highest
|
|
|
|
// object in the file and the linearization hint stream would be some random place in
|
|
|
|
// the middle, so I'm leaving that bug unfixed for now. If the bug were to be fixed, we
|
|
|
|
// could use !check_og in place of skip_cache_if_in_xref.
|
2022-11-19 21:01:54 +00:00
|
|
|
QTC::TC("qpdf", "QPDF skipping cache for known unchecked object");
|
|
|
|
} else {
|
2022-12-29 15:12:04 +00:00
|
|
|
updateCache(og, oh.getObj(), end_before_space, end_after_space);
|
2022-11-19 21:01:54 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return oh;
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:16:06 +00:00
|
|
|
void
|
2022-11-24 16:50:46 +00:00
|
|
|
QPDF::resolve(QPDFObjGen og)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-09-13 15:20:28 +00:00
|
|
|
if (!isUnresolved(og)) {
|
2022-08-10 12:16:06 +00:00
|
|
|
return;
|
2022-08-10 12:16:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->resolving.count(og)) {
|
2023-05-24 15:28:17 +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.
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF recursion loop in resolve");
|
2023-05-21 17:35:09 +00:00
|
|
|
warn(damagedPDF("", "loop detected resolving object " + og.unparse(' ')));
|
2022-08-10 12:16:06 +00:00
|
|
|
updateCache(og, QPDF_Null::create(), -1, -1);
|
2022-08-10 12:16:06 +00:00
|
|
|
return;
|
2017-07-26 09:03:38 +00:00
|
|
|
}
|
|
|
|
ResolveRecorder rr(this, og);
|
|
|
|
|
2022-08-10 12:16:06 +00:00
|
|
|
if (m->xref_table.count(og) != 0) {
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFXRefEntry const& entry = m->xref_table[og];
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
|
|
|
switch (entry.getType()) {
|
|
|
|
case 1:
|
2017-07-28 02:27:00 +00:00
|
|
|
{
|
|
|
|
qpdf_offset_t offset = entry.getOffset();
|
|
|
|
// Object stored in cache by readObjectAtOffset
|
2022-07-23 12:47:18 +00:00
|
|
|
QPDFObjGen a_og;
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFObjectHandle oh = readObjectAtOffset(true, offset, "", og, a_og, false);
|
2017-07-28 02:27:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case 2:
|
2017-07-28 02:27:00 +00:00
|
|
|
resolveObjectsInStream(entry.getObjStreamNumber());
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
"", 0, ("object " + og.unparse('/') + " has unexpected xref entry type"));
|
2017-07-28 02:27:00 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
2017-07-28 02:27:00 +00:00
|
|
|
warn(e);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
"", 0, ("object " + og.unparse('/') + ": error reading object: " + e.what())));
|
2022-04-02 21:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-10 12:16:06 +00:00
|
|
|
|
|
|
|
if (isUnresolved(og)) {
|
2019-01-04 15:17:33 +00:00
|
|
|
// PDF spec says unknown objects resolve to the null object.
|
2018-02-16 22:25:27 +00:00
|
|
|
QTC::TC("qpdf", "QPDF resolve failure to null");
|
2022-08-10 12:16:06 +00:00
|
|
|
updateCache(og, QPDF_Null::create(), -1, -1);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
auto result(m->obj_cache[og].object);
|
2022-12-16 16:19:15 +00:00
|
|
|
result->setDefaultDescription(this, og);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::resolveObjectsInStream(int obj_stream_number)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->resolved_object_streams.count(obj_stream_number)) {
|
2020-10-22 19:19:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->resolved_object_streams.insert(obj_stream_number);
|
2008-04-29 12:55:25 +00:00
|
|
|
// Force resolution of object stream
|
|
|
|
QPDFObjectHandle obj_stream = getObjectByID(obj_stream_number, 0);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!obj_stream.isStream()) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
"supposed object stream " + std::to_string(obj_stream_number) + " is not a stream");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// 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);
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t end_before_space = m->obj_cache[stream_og].end_before_space;
|
|
|
|
qpdf_offset_t end_after_space = m->obj_cache[stream_og].end_after_space;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QPDFObjectHandle dict = obj_stream.getDict();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!dict.isDictionaryOfType("/ObjStm")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF ERR object stream with wrong type");
|
2022-09-28 11:25:52 +00:00
|
|
|
warn(damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
"supposed object stream " + std::to_string(obj_stream_number) + " has wrong type"));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!(dict.getKey("/N").isInteger() && dict.getKey("/First").isInteger())) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
("object stream " + std::to_string(obj_stream_number) + " has incorrect keys"));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
int n = dict.getKey("/N").getIntValueAsInt();
|
|
|
|
int first = dict.getKey("/First").getIntValueAsInt();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::map<int, int> offsets;
|
|
|
|
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer> bp = obj_stream.getStreamData(qpdf_dl_specialized);
|
|
|
|
auto input = std::shared_ptr<InputSource>(
|
2022-04-03 20:10:27 +00:00
|
|
|
// line-break
|
|
|
|
new BufferInputSource(
|
2023-05-21 17:35:09 +00:00
|
|
|
(m->file->getName() + " object stream " + std::to_string(obj_stream_number)),
|
2022-04-03 20:10:27 +00:00
|
|
|
bp.get()));
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFTokenizer::Token tnum = readToken(input);
|
|
|
|
QPDFTokenizer::Token toffset = readToken(input);
|
2022-09-29 11:52:19 +00:00
|
|
|
if (!(tnum.isInteger() && toffset.isInteger())) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
|
|
|
input,
|
2023-05-21 13:42:34 +00:00
|
|
|
m->last_object_description,
|
2022-04-02 21:14:10 +00:00
|
|
|
input->getLastOffset(),
|
|
|
|
"expected integer in object stream header");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
int num = QUtil::string_to_int(tnum.getValue().c_str());
|
|
|
|
long long offset = QUtil::string_to_int(toffset.getValue().c_str());
|
2022-07-13 13:24:19 +00:00
|
|
|
offsets[num] = toI(offset + first);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +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.
|
2023-07-08 13:04:10 +00:00
|
|
|
m->last_object_description.clear();
|
|
|
|
m->last_object_description += "object ";
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& iter: offsets) {
|
2022-08-10 12:16:06 +00:00
|
|
|
QPDFObjGen og(iter.first, 0);
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFXRefEntry const& entry = m->xref_table[og];
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((entry.getType() == 2) && (entry.getObjStreamNumber() == obj_stream_number)) {
|
2022-04-30 17:23:18 +00:00
|
|
|
int offset = iter.second;
|
2013-02-23 22:41:27 +00:00
|
|
|
input->seek(offset, SEEK_SET);
|
2023-07-08 13:04:10 +00:00
|
|
|
QPDFObjectHandle oh = readObjectInStream(input, iter.first);
|
2022-12-29 15:12:04 +00:00
|
|
|
updateCache(og, oh.getObj(), end_before_space, end_after_space);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2013-02-23 22:41:27 +00:00
|
|
|
QTC::TC("qpdf", "QPDF not caching overridden objstm object");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:20:24 +00:00
|
|
|
QPDFObjectHandle
|
2022-09-08 15:29:23 +00:00
|
|
|
QPDF::newIndirect(QPDFObjGen const& og, std::shared_ptr<QPDFObject> const& obj)
|
2022-08-02 15:20:24 +00:00
|
|
|
{
|
2022-12-16 16:19:15 +00:00
|
|
|
obj->setDefaultDescription(this, og);
|
2022-12-29 15:12:04 +00:00
|
|
|
return {obj};
|
2022-08-02 15:20:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 12:16:06 +00:00
|
|
|
void
|
|
|
|
QPDF::updateCache(
|
|
|
|
QPDFObjGen const& og,
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject> const& object,
|
2022-08-10 12:16:06 +00:00
|
|
|
qpdf_offset_t end_before_space,
|
|
|
|
qpdf_offset_t end_after_space)
|
|
|
|
{
|
2022-08-14 00:05:32 +00:00
|
|
|
object->setObjGen(this, og);
|
2022-08-10 12:16:06 +00:00
|
|
|
if (isCached(og)) {
|
|
|
|
auto& cache = m->obj_cache[og];
|
|
|
|
cache.object->assign(object);
|
|
|
|
cache.end_before_space = end_before_space;
|
|
|
|
cache.end_after_space = end_after_space;
|
|
|
|
} else {
|
|
|
|
m->obj_cache[og] = ObjCache(object, end_before_space, end_after_space);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:16:06 +00:00
|
|
|
bool
|
|
|
|
QPDF::isCached(QPDFObjGen const& og)
|
|
|
|
{
|
|
|
|
return m->obj_cache.count(og) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::isUnresolved(QPDFObjGen const& og)
|
|
|
|
{
|
|
|
|
return !isCached(og) || m->obj_cache[og].object->isUnresolved();
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:43:28 +00:00
|
|
|
QPDFObjGen
|
|
|
|
QPDF::nextObjGen()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
int max_objid = toI(getObjectCount());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (max_objid == std::numeric_limits<int>::max()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::range_error("max object id is too high to create new objects");
|
2019-08-27 21:57:38 +00:00
|
|
|
}
|
2022-09-26 17:43:28 +00:00
|
|
|
return QPDFObjGen(max_objid + 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
2022-11-20 21:04:58 +00:00
|
|
|
QPDF::makeIndirectFromQPDFObject(std::shared_ptr<QPDFObject> const& obj)
|
2022-09-26 17:43:28 +00:00
|
|
|
{
|
|
|
|
QPDFObjGen next{nextObjGen()};
|
2022-09-26 17:27:25 +00:00
|
|
|
m->obj_cache[next] = ObjCache(obj, -1, -1);
|
2022-08-02 15:20:24 +00:00
|
|
|
return newIndirect(next, m->obj_cache[next].object);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:27:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::makeIndirectObject(QPDFObjectHandle oh)
|
|
|
|
{
|
2022-12-29 15:12:04 +00:00
|
|
|
if (!oh.isInitialized()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("attempted to make an uninitialized QPDFObjectHandle indirect");
|
2022-12-29 15:12:04 +00:00
|
|
|
}
|
|
|
|
return makeIndirectFromQPDFObject(oh.getObj());
|
2022-09-26 17:27:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 18:56:01 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newReserved()
|
|
|
|
{
|
|
|
|
return makeIndirectFromQPDFObject(QPDF_Reserved::create());
|
|
|
|
}
|
|
|
|
|
2023-08-25 12:28:48 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newIndirectNull()
|
|
|
|
{
|
|
|
|
return makeIndirectFromQPDFObject(QPDF_Null::create());
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:27:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newStream()
|
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
return makeIndirectFromQPDFObject(
|
|
|
|
QPDF_Stream::create(this, nextObjGen(), QPDFObjectHandle::newDictionary(), 0, 0));
|
2022-09-26 17:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newStream(std::shared_ptr<Buffer> data)
|
|
|
|
{
|
|
|
|
auto result = newStream();
|
2023-05-21 17:35:09 +00:00
|
|
|
result.replaceStreamData(data, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
|
2022-09-26 17:27:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newStream(std::string const& data)
|
|
|
|
{
|
|
|
|
auto result = newStream();
|
2023-05-21 17:35:09 +00:00
|
|
|
result.replaceStreamData(data, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
|
2022-09-26 17:27:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-15 17:10:10 +00:00
|
|
|
QPDFObjectHandle
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDF::reserveObjectIfNotExists(QPDFObjGen const& og)
|
2022-05-15 17:10:10 +00:00
|
|
|
{
|
2022-09-13 15:20:28 +00:00
|
|
|
if (!isCached(og) && m->xref_table.count(og) == 0) {
|
|
|
|
updateCache(og, QPDF_Reserved::create(), -1, -1);
|
2022-08-02 15:20:24 +00:00
|
|
|
return newIndirect(og, m->obj_cache[og].object);
|
|
|
|
} else {
|
|
|
|
return getObject(og);
|
2022-05-15 17:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:38:06 +00:00
|
|
|
QPDFObjectHandle
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDF::reserveStream(QPDFObjGen const& og)
|
2022-05-15 21:38:06 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
return {QPDF_Stream::create(this, og, QPDFObjectHandle::newDictionary(), 0, 0)};
|
2022-05-15 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjectHandle
|
2022-08-01 18:22:37 +00:00
|
|
|
QPDF::getObject(QPDFObjGen const& og)
|
2013-06-14 15:58:37 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// This method is called by the parser and therefore must not resolve any objects.
|
2022-08-12 14:14:11 +00:00
|
|
|
if (!isCached(og)) {
|
|
|
|
m->obj_cache[og] = ObjCache(QPDF_Unresolved::create(this, og), -1, -1);
|
2022-08-10 12:16:06 +00:00
|
|
|
}
|
|
|
|
return newIndirect(og, m->obj_cache[og].object);
|
2013-06-14 15:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 18:22:37 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getObject(int objid, int generation)
|
|
|
|
{
|
|
|
|
return getObject(QPDFObjGen(objid, generation));
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getObjectByObjGen(QPDFObjGen const& og)
|
|
|
|
{
|
|
|
|
return getObject(og);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getObjectByID(int objid, int generation)
|
|
|
|
{
|
2022-08-01 18:22:37 +00:00
|
|
|
return getObject(QPDFObjGen(objid, generation));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
void
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDF::replaceObject(int objid, int generation, QPDFObjectHandle oh)
|
2013-06-14 15:58:37 +00:00
|
|
|
{
|
2022-07-23 15:48:28 +00:00
|
|
|
replaceObject(QPDFObjGen(objid, generation), oh);
|
2013-06-14 15:58:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 16:42:48 +00:00
|
|
|
void
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDF::replaceObject(QPDFObjGen const& og, QPDFObjectHandle oh)
|
2011-08-10 16:42:48 +00:00
|
|
|
{
|
2022-12-29 15:12:04 +00:00
|
|
|
if (oh.isIndirect() || !oh.isInitialized()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF replaceObject called with indirect object");
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("QPDF::replaceObject called with indirect object handle");
|
2011-08-10 16:42:48 +00:00
|
|
|
}
|
2022-12-29 15:12:04 +00:00
|
|
|
updateCache(og, oh.getObj(), -1, -1);
|
2011-08-10 16:42:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 22:05:35 +00:00
|
|
|
void
|
2024-01-08 11:49:21 +00:00
|
|
|
QPDF::removeObject(QPDFObjGen og)
|
2024-01-07 22:05:35 +00:00
|
|
|
{
|
|
|
|
m->xref_table.erase(og);
|
2024-01-08 11:49:21 +00:00
|
|
|
if (auto cached = m->obj_cache.find(og); cached != m->obj_cache.end()) {
|
2024-01-07 22:05:35 +00:00
|
|
|
// Take care of any object handles that may be floating around.
|
2024-01-08 11:49:21 +00:00
|
|
|
cached->second.object->assign(QPDF_Null::create());
|
|
|
|
cached->second.object->setObjGen(nullptr, QPDFObjGen());
|
|
|
|
m->obj_cache.erase(cached);
|
2024-01-07 22:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-08 18:19:19 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::replaceReserved(QPDFObjectHandle reserved, QPDFObjectHandle replacement)
|
2012-07-08 18:19:19 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF replaceReserved");
|
2023-08-25 11:37:24 +00:00
|
|
|
auto tc = reserved.getTypeCode();
|
|
|
|
if (!(tc == ::ot_reserved || tc == ::ot_null)) {
|
2023-09-03 18:06:38 +00:00
|
|
|
throw std::logic_error("replaceReserved called with non-reserved object");
|
2023-08-25 11:37:24 +00:00
|
|
|
}
|
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
|
2019-01-29 02:53:55 +00:00
|
|
|
QPDF::copyForeignObject(QPDFObjectHandle foreign)
|
2012-07-11 19:29:41 +00:00
|
|
|
{
|
2020-10-31 15:57:28 +00:00
|
|
|
// Here's an explanation of what's going on here.
|
|
|
|
//
|
2023-05-24 15:28:17 +00:00
|
|
|
// A QPDFObjectHandle that is an indirect object has an owning QPDF. The object ID and
|
|
|
|
// generation refers to an object in the owning QPDF. When we copy the QPDFObjectHandle from a
|
|
|
|
// foreign QPDF into the local QPDF, we have to replace all indirect object references with
|
|
|
|
// references to the corresponding object in the local file.
|
2020-10-31 15:57:28 +00:00
|
|
|
//
|
2023-05-24 15:28:17 +00:00
|
|
|
// To do this, we maintain mappings from foreign object IDs to local object IDs for each foreign
|
|
|
|
// QPDF that we are copying from. The mapping is stored in an ObjCopier, which contains a
|
2020-10-31 15:57:28 +00:00
|
|
|
// mapping from the foreign ObjGen to the local QPDFObjectHandle.
|
|
|
|
//
|
2023-05-24 15:28:17 +00:00
|
|
|
// To copy, we do a deep traversal of the foreign object with loop detection to discover all
|
|
|
|
// indirect objects that are encountered, stopping at page boundaries. Whenever we encounter an
|
|
|
|
// indirect object, we check to see if we have already created a local copy of it. If not, we
|
|
|
|
// allocate a "reserved" object (or, for a stream, just a new stream) and store in the map the
|
2020-10-31 15:57:28 +00:00
|
|
|
// mapping from the foreign object ID to the new object. While we
|
|
|
|
// do this, we keep a list of objects to copy.
|
|
|
|
//
|
2023-05-24 15:28:17 +00:00
|
|
|
// Once we are done with the traversal, we copy all the objects that we need to copy. However,
|
|
|
|
// the copies will contain indirect object IDs that refer to objects in the foreign file. We
|
|
|
|
// need to replace them with references to objects in the local file. This is what
|
|
|
|
// replaceForeignIndirectObjects does. Once we have created a copy of the foreign object with
|
|
|
|
// all the indirect references replaced with new ones in the local context, we can replace the
|
|
|
|
// local reserved object with the copy. This mechanism allows us to copy objects with circular
|
|
|
|
// references in any order.
|
|
|
|
|
|
|
|
// For streams, rather than copying the objects, we set up the stream data to pull from the
|
|
|
|
// original stream by using a stream data provider. This is done in a manner that doesn't
|
|
|
|
// require the original QPDF object but may require the original source of the stream data with
|
|
|
|
// special handling for immediate_copy_from. This logic is also in
|
|
|
|
// replaceForeignIndirectObjects.
|
|
|
|
|
|
|
|
// Note that we explicitly allow use of copyForeignObject on page objects. It is a documented
|
|
|
|
// use case to copy pages this way if the intention is to not update the pages tree.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!foreign.isIndirect()) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF copyForeign direct");
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("QPDF::copyForeign called with direct object handle");
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
2022-09-07 15:29:17 +00:00
|
|
|
QPDF& other = foreign.getQPDF();
|
|
|
|
if (&other == this) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF copyForeign not foreign");
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("QPDF::copyForeign called with object from this QPDF");
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
ObjCopier& obj_copier = m->object_copiers[other.m->unique_id];
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!obj_copier.visiting.empty()) {
|
2012-07-11 19:29:41 +00:00
|
|
|
throw std::logic_error("obj_copier.visiting is not empty"
|
|
|
|
" at the beginning of copyForeignObject");
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Make sure we have an object in this file for every referenced object in the old file.
|
|
|
|
// obj_copier.object_map maps foreign QPDFObjGen to local objects. For everything new that we
|
|
|
|
// have to copy, the local object will be a reservation, unless it is a stream, in which case
|
|
|
|
// the local object will already be a stream.
|
2012-07-11 19:29:41 +00:00
|
|
|
reserveObjects(foreign, obj_copier, true);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!obj_copier.visiting.empty()) {
|
2023-08-25 12:28:48 +00:00
|
|
|
throw std::logic_error("obj_copier.visiting is not empty after reserving objects");
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy any new objects and replace the reservations.
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& to_copy: obj_copier.to_copy) {
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFObjectHandle copy = replaceForeignIndirectObjects(to_copy, obj_copier, true);
|
2022-04-02 21:14:10 +00:00
|
|
|
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();
|
|
|
|
|
2023-09-03 10:03:24 +00:00
|
|
|
auto og = foreign.getObjGen();
|
|
|
|
if (!obj_copier.object_map.count(og)) {
|
|
|
|
warn(damagedPDF("unexpected reference to /Pages object while copying foreign object; "
|
|
|
|
"replacing with null"));
|
|
|
|
return QPDFObjectHandle::newNull();
|
2023-07-25 13:34:14 +00:00
|
|
|
}
|
2023-09-03 10:03:24 +00:00
|
|
|
return obj_copier.object_map[foreign.getObjGen()];
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::reserveObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top)
|
2012-07-11 19:29:41 +00:00
|
|
|
{
|
2023-05-19 10:12:49 +00:00
|
|
|
auto foreign_tc = foreign.getTypeCode();
|
|
|
|
if (foreign_tc == ::ot_reserved) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("QPDF: attempting to copy a foreign reserved object");
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (foreign.isPagesObject()) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF not copying pages object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (foreign.isIndirect()) {
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen foreign_og(foreign.getObjGen());
|
2023-05-19 10:12:49 +00:00
|
|
|
if (!obj_copier.visiting.add(foreign_og)) {
|
2023-09-05 12:34:03 +00:00
|
|
|
QTC::TC("qpdf", "QPDF loop reserving objects");
|
2013-12-26 16:51:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-05 12:34:03 +00:00
|
|
|
if (obj_copier.object_map.count(foreign_og) > 0) {
|
|
|
|
QTC::TC("qpdf", "QPDF already reserved object");
|
|
|
|
if (!(top && foreign.isPageObject() && obj_copier.object_map[foreign_og].isNull())) {
|
|
|
|
obj_copier.visiting.erase(foreign);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QTC::TC("qpdf", "QPDF copy indirect");
|
2023-08-25 12:28:48 +00:00
|
|
|
obj_copier.object_map[foreign_og] =
|
|
|
|
foreign.isStream() ? newStream() : newIndirectNull();
|
2023-08-25 12:29:24 +00:00
|
|
|
if ((!top) && foreign.isPageObject()) {
|
|
|
|
QTC::TC("qpdf", "QPDF not crossing page boundary");
|
2023-09-05 12:34:03 +00:00
|
|
|
obj_copier.visiting.erase(foreign_og);
|
2023-08-25 12:29:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
2023-09-05 12:34:03 +00:00
|
|
|
obj_copier.to_copy.push_back(foreign);
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 10:12:49 +00:00
|
|
|
if (foreign_tc == ::ot_array) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF reserve array");
|
2022-02-08 14:18:08 +00:00
|
|
|
int n = foreign.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2012-07-11 19:29:41 +00:00
|
|
|
reserveObjects(foreign.getArrayItem(i), obj_copier, false);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-19 10:12:49 +00:00
|
|
|
} else if (foreign_tc == ::ot_dictionary) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF reserve dictionary");
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& key: foreign.getKeys()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
reserveObjects(foreign.getKey(key), obj_copier, false);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-19 10:12:49 +00:00
|
|
|
} else if (foreign_tc == ::ot_stream) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF reserve stream");
|
|
|
|
reserveObjects(foreign.getDict(), obj_copier, false);
|
|
|
|
}
|
|
|
|
|
2023-05-19 10:12:49 +00:00
|
|
|
obj_copier.visiting.erase(foreign);
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::replaceForeignIndirectObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top)
|
2012-07-11 19:29:41 +00:00
|
|
|
{
|
2023-05-19 10:12:49 +00:00
|
|
|
auto foreign_tc = foreign.getTypeCode();
|
2012-07-11 19:29:41 +00:00
|
|
|
QPDFObjectHandle result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((!top) && foreign.isIndirect()) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF replace indirect");
|
2023-05-19 10:12:49 +00:00
|
|
|
auto mapping = obj_copier.object_map.find(foreign.getObjGen());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (mapping == obj_copier.object_map.end()) {
|
2023-08-25 12:29:24 +00:00
|
|
|
// This case would occur if this is a reference to a Pages object that we didn't
|
2023-05-24 15:28:17 +00:00
|
|
|
// traverse into.
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF replace foreign indirect with null");
|
|
|
|
result = QPDFObjectHandle::newNull();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-19 10:12:49 +00:00
|
|
|
result = mapping->second;
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
2023-05-19 10:12:49 +00:00
|
|
|
} else if (foreign_tc == ::ot_array) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF replace array");
|
|
|
|
result = QPDFObjectHandle::newArray();
|
2022-02-08 14:18:08 +00:00
|
|
|
int n = foreign.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2022-04-03 20:10:27 +00:00
|
|
|
result.appendItem(
|
|
|
|
// line-break
|
2023-05-21 17:35:09 +00:00
|
|
|
replaceForeignIndirectObjects(foreign.getArrayItem(i), obj_copier, false));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-19 10:12:49 +00:00
|
|
|
} else if (foreign_tc == ::ot_dictionary) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF replace dictionary");
|
|
|
|
result = QPDFObjectHandle::newDictionary();
|
2022-02-08 14:18:08 +00:00
|
|
|
std::set<std::string> keys = foreign.getKeys();
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: keys) {
|
2012-07-11 19:29:41 +00:00
|
|
|
result.replaceKey(
|
2023-05-21 17:35:09 +00:00
|
|
|
iter, replaceForeignIndirectObjects(foreign.getKey(iter), obj_copier, false));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-19 10:12:49 +00:00
|
|
|
} else if (foreign_tc == ::ot_stream) {
|
2012-07-11 19:29:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF replace stream");
|
2023-05-19 10:12:49 +00:00
|
|
|
result = obj_copier.object_map[foreign.getObjGen()];
|
2012-07-11 19:29:41 +00:00
|
|
|
result.assertStream();
|
|
|
|
QPDFObjectHandle dict = result.getDict();
|
|
|
|
QPDFObjectHandle old_dict = foreign.getDict();
|
|
|
|
std::set<std::string> keys = old_dict.getKeys();
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: keys) {
|
2012-07-11 19:29:41 +00:00
|
|
|
dict.replaceKey(
|
2023-05-21 17:35:09 +00:00
|
|
|
iter, replaceForeignIndirectObjects(old_dict.getKey(iter), obj_copier, false));
|
2019-01-07 02:18:36 +00:00
|
|
|
}
|
2021-02-21 10:56:52 +00:00
|
|
|
copyStreamData(result, foreign);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-07-11 19:29:41 +00:00
|
|
|
foreign.assertScalar();
|
|
|
|
result = foreign;
|
|
|
|
result.makeDirect();
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (top && (!result.isStream()) && result.isIndirect()) {
|
2012-07-11 19:29:41 +00:00
|
|
|
throw std::logic_error("replacement for foreign object is indirect");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-07-08 18:19:19 +00:00
|
|
|
|
2021-02-21 10:56:52 +00:00
|
|
|
void
|
|
|
|
QPDF::copyStreamData(QPDFObjectHandle result, QPDFObjectHandle foreign)
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// This method was originally written for copying foreign streams, but it is used by
|
|
|
|
// QPDFObjectHandle to copy streams from the same QPDF object as well.
|
2021-02-21 11:35:53 +00:00
|
|
|
|
2021-02-21 10:56:52 +00:00
|
|
|
QPDFObjectHandle dict = result.getDict();
|
|
|
|
QPDFObjectHandle old_dict = foreign.getDict();
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->copied_stream_data_provider == nullptr) {
|
|
|
|
m->copied_stream_data_provider = new CopiedStreamDataProvider(*this);
|
|
|
|
m->copied_streams =
|
2023-05-21 17:35:09 +00:00
|
|
|
std::shared_ptr<QPDFObjectHandle::StreamDataProvider>(m->copied_stream_data_provider);
|
2021-02-21 10:56:52 +00:00
|
|
|
}
|
|
|
|
QPDFObjGen local_og(result.getObjGen());
|
2023-05-24 15:28:17 +00:00
|
|
|
// Copy information from the foreign stream so we can pipe its data later without keeping the
|
|
|
|
// original QPDF object around.
|
2022-08-02 18:13:29 +00:00
|
|
|
|
2022-09-07 15:29:17 +00:00
|
|
|
QPDF& foreign_stream_qpdf =
|
|
|
|
foreign.getQPDF("unable to retrieve owning qpdf from foreign stream");
|
2022-08-06 18:52:07 +00:00
|
|
|
|
2022-12-29 15:12:04 +00:00
|
|
|
auto stream = foreign.getObjectPtr()->as<QPDF_Stream>();
|
2022-08-02 18:13:29 +00:00
|
|
|
if (stream == nullptr) {
|
2021-02-21 10:56:52 +00:00
|
|
|
throw std::logic_error("unable to retrieve underlying"
|
|
|
|
" stream object from foreign stream");
|
|
|
|
}
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer> stream_buffer = stream->getStreamDataBuffer();
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((foreign_stream_qpdf.m->immediate_copy_from) && (stream_buffer == nullptr)) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Pull the stream data into a buffer before attempting the copy operation. Do it on the
|
|
|
|
// source stream so that if the source stream is copied multiple times, we don't have to
|
|
|
|
// keep duplicating the memory.
|
2021-02-21 10:56:52 +00:00
|
|
|
QTC::TC("qpdf", "QPDF immediate copy stream data");
|
2022-04-02 21:14:10 +00:00
|
|
|
foreign.replaceStreamData(
|
|
|
|
foreign.getRawStreamData(),
|
|
|
|
old_dict.getKey("/Filter"),
|
|
|
|
old_dict.getKey("/DecodeParms"));
|
2021-02-21 10:56:52 +00:00
|
|
|
stream_buffer = stream->getStreamDataBuffer();
|
|
|
|
}
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<QPDFObjectHandle::StreamDataProvider> stream_provider =
|
2021-02-21 10:56:52 +00:00
|
|
|
stream->getStreamDataProvider();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (stream_buffer.get()) {
|
2021-02-21 10:56:52 +00:00
|
|
|
QTC::TC("qpdf", "QPDF copy foreign stream with buffer");
|
2022-04-02 21:14:10 +00:00
|
|
|
result.replaceStreamData(
|
|
|
|
stream_buffer, dict.getKey("/Filter"), dict.getKey("/DecodeParms"));
|
|
|
|
} else if (stream_provider.get()) {
|
2021-02-21 10:56:52 +00:00
|
|
|
// In this case, the remote stream's QPDF must stay in scope.
|
|
|
|
QTC::TC("qpdf", "QPDF copy foreign stream with provider");
|
2023-05-21 17:35:09 +00:00
|
|
|
m->copied_stream_data_provider->registerForeignStream(local_og, foreign);
|
2022-04-02 21:14:10 +00:00
|
|
|
result.replaceStreamData(
|
2023-05-21 17:35:09 +00:00
|
|
|
m->copied_streams, dict.getKey("/Filter"), dict.getKey("/DecodeParms"));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-04-09 18:35:56 +00:00
|
|
|
auto foreign_stream_data = std::make_shared<ForeignStreamData>(
|
2022-09-07 15:29:17 +00:00
|
|
|
foreign_stream_qpdf.m->encp,
|
|
|
|
foreign_stream_qpdf.m->file,
|
2022-07-23 15:48:28 +00:00
|
|
|
foreign.getObjGen(),
|
2022-09-27 12:08:14 +00:00
|
|
|
stream->getParsedOffset(),
|
2022-04-02 21:14:10 +00:00
|
|
|
stream->getLength(),
|
|
|
|
dict);
|
2023-05-21 17:35:09 +00:00
|
|
|
m->copied_stream_data_provider->registerForeignStream(local_og, foreign_stream_data);
|
2022-04-02 21:14:10 +00:00
|
|
|
result.replaceStreamData(
|
2023-05-21 17:35:09 +00:00
|
|
|
m->copied_streams, dict.getKey("/Filter"), dict.getKey("/DecodeParms"));
|
2021-02-21 10:56:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
void
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDF::swapObjects(int objid1, int generation1, int objid2, int generation2)
|
2013-06-14 15:58:37 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
swapObjects(QPDFObjGen(objid1, generation1), QPDFObjGen(objid2, generation2));
|
2013-06-14 15:58:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 16:42:48 +00:00
|
|
|
void
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDF::swapObjects(QPDFObjGen const& og1, QPDFObjGen const& og2)
|
2011-08-10 16:42:48 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// Force objects to be read from the input source if needed, then swap them in the cache.
|
2022-07-23 15:48:28 +00:00
|
|
|
resolve(og1);
|
|
|
|
resolve(og2);
|
2022-08-02 21:57:33 +00:00
|
|
|
m->obj_cache[og1].object->swapWith(m->obj_cache[og2].object);
|
2011-08-10 16:42:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 14:14:20 +00:00
|
|
|
unsigned long long
|
|
|
|
QPDF::getUniqueId() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->unique_id;
|
2019-01-12 14:14:20 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF::getFilename() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->file->getName();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:07:37 +00:00
|
|
|
PDFVersion
|
|
|
|
QPDF::getVersionAsPDFVersion()
|
|
|
|
{
|
|
|
|
int major = 1;
|
|
|
|
int minor = 3;
|
|
|
|
int extension_level = getExtensionLevel();
|
|
|
|
|
|
|
|
std::regex v("^[[:space:]]*([0-9]+)\\.([0-9]+)");
|
2023-05-21 13:52:24 +00:00
|
|
|
std::smatch match;
|
|
|
|
if (std::regex_search(m->pdf_version, match, v)) {
|
|
|
|
major = QUtil::string_to_int(match[1].str().c_str());
|
|
|
|
minor = QUtil::string_to_int(match[2].str().c_str());
|
2022-02-08 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 20:04:32 +00:00
|
|
|
return {major, minor, extension_level};
|
2022-02-08 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF::getPDFVersion() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return 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();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.hasKey("/Extensions")) {
|
2012-12-27 20:17:39 +00:00
|
|
|
obj = obj.getKey("/Extensions");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.isDictionary() && obj.hasKey("/ADBE")) {
|
2012-12-27 20:17:39 +00:00
|
|
|
obj = obj.getKey("/ADBE");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.isDictionary() && obj.hasKey("/ExtensionLevel")) {
|
2012-12-27 20:17:39 +00:00
|
|
|
obj = obj.getKey("/ExtensionLevel");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.isInteger()) {
|
2019-06-21 03:35:23 +00:00
|
|
|
result = obj.getIntValueAsInt();
|
2012-12-27 20:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getTrailer()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->trailer;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getRoot()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFObjectHandle root = m->trailer.getKey("/Root");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!root.isDictionary()) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("", 0, "unable to find /Root dictionary");
|
2022-11-12 18:10:16 +00:00
|
|
|
} else if (
|
2023-05-24 15:28:17 +00:00
|
|
|
// Check_mode is an interim solution to request #810 pending a more comprehensive review of
|
|
|
|
// the approach to more extensive checks and warning levels.
|
2022-11-12 18:10:16 +00:00
|
|
|
m->check_mode && !root.getKey("/Type").isNameAndEquals("/Catalog")) {
|
|
|
|
warn(damagedPDF("", 0, "catalog /Type entry missing or invalid"));
|
|
|
|
root.replaceKey("/Type", "/Catalog"_qpdf);
|
2017-07-28 22:02:39 +00:00
|
|
|
}
|
|
|
|
return root;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 13:07:00 +00:00
|
|
|
std::map<QPDFObjGen, QPDFXRefEntry>
|
|
|
|
QPDF::getXRefTable()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->parsed) {
|
2019-09-28 13:07:00 +00:00
|
|
|
throw std::logic_error("QPDF::getXRefTable called before parsing.");
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->xref_table;
|
2019-09-28 13:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::getObjectStreamData(std::map<int, int>& omap)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->xref_table) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjGen const& og = iter.first;
|
|
|
|
QPDFXRefEntry const& entry = iter.second;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (entry.getType() == 2) {
|
2022-02-08 14:18:08 +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()
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// 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, particularly with linearized files. This is better than
|
|
|
|
// iterating through the xref table since it avoids preserving orphaned items.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Exclude encryption dictionary, if any
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFObjectHandle encryption_dict = 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
|
|
|
|
2024-01-06 14:37:05 +00:00
|
|
|
const size_t max_obj = getObjectCount();
|
|
|
|
std::vector<bool> visited(max_obj, false);
|
2024-01-04 12:20:15 +00:00
|
|
|
std::vector<QPDFObjectHandle> queue;
|
|
|
|
queue.reserve(512);
|
|
|
|
queue.push_back(m->trailer);
|
2013-06-14 18:53:47 +00:00
|
|
|
std::vector<QPDFObjGen> result;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!queue.empty()) {
|
2024-01-06 14:37:05 +00:00
|
|
|
auto obj = queue.back();
|
2024-01-04 12:20:15 +00:00
|
|
|
queue.pop_back();
|
2024-01-17 10:39:06 +00:00
|
|
|
if (obj.getObjectID() > 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen og = obj.getObjGen();
|
2024-01-06 14:37:05 +00:00
|
|
|
const size_t id = toS(og.getObj() - 1);
|
2024-02-04 21:11:53 +00:00
|
|
|
if (id >= max_obj) {
|
2024-01-07 20:21:38 +00:00
|
|
|
throw std::logic_error(
|
2024-01-06 14:37:05 +00:00
|
|
|
"unexpected object id encountered in getCompressibleObjGens");
|
2024-02-04 21:11:53 +00:00
|
|
|
}
|
2024-01-08 13:42:05 +00:00
|
|
|
if (visited[id]) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF loop detected traversing objects");
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-08 13:42:05 +00:00
|
|
|
|
|
|
|
// Check whether this is the current object. If not, remove it (which changes it into a
|
|
|
|
// direct null and therefore stops us from revisiting it) and move on to the next object
|
|
|
|
// in the queue.
|
|
|
|
auto upper = m->obj_cache.upper_bound(og);
|
|
|
|
if (upper != m->obj_cache.end() && upper->first.getObj() == og.getObj()) {
|
|
|
|
removeObject(og);
|
|
|
|
continue;
|
2024-01-07 20:21:38 +00:00
|
|
|
}
|
2024-01-08 13:42:05 +00:00
|
|
|
|
|
|
|
visited[id] = true;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (og == encryption_dict_og) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF exclude encryption dictionary");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (!(obj.isStream() ||
|
2023-05-21 17:35:09 +00:00
|
|
|
(obj.isDictionaryOfType("/Sig") && obj.hasKey("/ByteRange") &&
|
2022-04-02 21:14:10 +00:00
|
|
|
obj.hasKey("/Contents")))) {
|
2022-02-08 14:18:08 +00:00
|
|
|
result.push_back(og);
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.isStream()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle dict = obj.getDict();
|
|
|
|
std::set<std::string> keys = dict.getKeys();
|
2023-05-20 18:56:01 +00:00
|
|
|
for (auto iter = keys.rbegin(); iter != keys.rend(); ++iter) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::string const& key = *iter;
|
|
|
|
QPDFObjectHandle value = dict.getKey(key);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (key == "/Length") {
|
2022-02-08 14:18:08 +00:00
|
|
|
// omit stream lengths
|
2022-04-02 21:14:10 +00:00
|
|
|
if (value.isIndirect()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF exclude indirect length");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2024-01-04 12:20:15 +00:00
|
|
|
queue.push_back(value);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (obj.isDictionary()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::set<std::string> keys = obj.getKeys();
|
2023-05-20 18:56:01 +00:00
|
|
|
for (auto iter = keys.rbegin(); iter != keys.rend(); ++iter) {
|
2024-01-04 12:20:15 +00:00
|
|
|
queue.push_back(obj.getKey(*iter));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (obj.isArray()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int n = obj.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 1; i <= n; ++i) {
|
2024-01-04 12:20:15 +00:00
|
|
|
queue.push_back(obj.getArrayItem(n - i));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:42:27 +00:00
|
|
|
bool
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::pipeStreamData(
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<EncryptionParameters> encp,
|
|
|
|
std::shared_ptr<InputSource> file,
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF& qpdf_for_warning,
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDFObjGen const& og,
|
2022-04-02 21:14:10 +00:00
|
|
|
qpdf_offset_t offset,
|
|
|
|
size_t length,
|
|
|
|
QPDFObjectHandle stream_dict,
|
|
|
|
Pipeline* pipeline,
|
|
|
|
bool suppress_warnings,
|
|
|
|
bool will_retry)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-11-18 14:40:32 +00:00
|
|
|
std::unique_ptr<Pipeline> to_delete;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (encp->encrypted) {
|
2023-05-21 17:35:09 +00:00
|
|
|
decryptStream(encp, file, qpdf_for_warning, pipeline, og, stream_dict, to_delete);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 17:29:10 +00:00
|
|
|
bool attempted_finish = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
file->seek(offset, SEEK_SET);
|
2023-11-18 13:11:12 +00:00
|
|
|
auto buf = std::make_unique<char[]>(length);
|
|
|
|
if (auto read = file->read(buf.get(), length); read != length) {
|
2023-12-23 02:45:10 +00:00
|
|
|
throw damagedPDF(file, "", offset + toO(read), "unexpected EOF reading stream data");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-11-18 13:11:12 +00:00
|
|
|
pipeline->write(buf.get(), length);
|
2023-10-07 17:29:10 +00:00
|
|
|
attempted_finish = true;
|
2017-08-19 13:18:14 +00:00
|
|
|
pipeline->finish();
|
2023-11-18 13:11:12 +00:00
|
|
|
return true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
|
|
|
if (!suppress_warnings) {
|
2019-01-06 15:34:52 +00:00
|
|
|
qpdf_for_warning.warn(e);
|
2017-07-28 03:42:27 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
|
|
|
if (!suppress_warnings) {
|
2017-07-28 03:42:27 +00:00
|
|
|
QTC::TC("qpdf", "QPDF decoding error warning");
|
2022-04-03 20:10:27 +00:00
|
|
|
qpdf_for_warning.warn(
|
|
|
|
// line-break
|
2022-09-28 11:25:52 +00:00
|
|
|
damagedPDF(
|
|
|
|
file,
|
2022-04-02 21:14:10 +00:00
|
|
|
"",
|
|
|
|
file->getLastOffset(),
|
2023-05-21 17:35:09 +00:00
|
|
|
("error decoding stream data for object " + og.unparse(' ') + ": " +
|
|
|
|
e.what())));
|
2022-04-03 20:10:27 +00:00
|
|
|
if (will_retry) {
|
|
|
|
qpdf_for_warning.warn(
|
|
|
|
// line-break
|
2022-09-28 11:25:52 +00:00
|
|
|
damagedPDF(
|
|
|
|
file,
|
2022-04-03 20:10:27 +00:00
|
|
|
"",
|
|
|
|
file->getLastOffset(),
|
2023-11-18 13:11:12 +00:00
|
|
|
"stream will be re-processed without filtering to avoid data loss"));
|
2017-09-12 19:48:08 +00:00
|
|
|
}
|
2017-07-28 03:42:27 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-10-07 17:29:10 +00:00
|
|
|
if (!attempted_finish) {
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2017-08-19 13:18:14 +00:00
|
|
|
pipeline->finish();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception&) {
|
2017-08-19 13:18:14 +00:00
|
|
|
// ignore
|
|
|
|
}
|
2017-07-29 16:07:19 +00:00
|
|
|
}
|
2023-12-23 02:45:10 +00:00
|
|
|
return false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-12-30 00:00:05 +00:00
|
|
|
|
2019-01-06 15:34:52 +00:00
|
|
|
bool
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::pipeStreamData(
|
2022-07-23 15:48:28 +00:00
|
|
|
QPDFObjGen const& og,
|
2022-04-02 21:14:10 +00:00
|
|
|
qpdf_offset_t offset,
|
|
|
|
size_t length,
|
|
|
|
QPDFObjectHandle stream_dict,
|
|
|
|
Pipeline* pipeline,
|
|
|
|
bool suppress_warnings,
|
|
|
|
bool will_retry)
|
2019-01-06 15:34:52 +00:00
|
|
|
{
|
|
|
|
return pipeStreamData(
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encp,
|
|
|
|
m->file,
|
2022-04-02 21:14:10 +00:00
|
|
|
*this,
|
2022-07-23 15:48:28 +00:00
|
|
|
og,
|
2022-04-02 21:14:10 +00:00
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
stream_dict,
|
|
|
|
pipeline,
|
|
|
|
suppress_warnings,
|
|
|
|
will_retry);
|
2019-01-06 15:34:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 02:18:36 +00:00
|
|
|
bool
|
|
|
|
QPDF::pipeForeignStreamData(
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<ForeignStreamData> foreign,
|
2019-01-07 02:18:36 +00:00
|
|
|
Pipeline* pipeline,
|
2022-04-02 21:14:10 +00:00
|
|
|
bool suppress_warnings,
|
|
|
|
bool will_retry)
|
2019-01-07 02:18:36 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (foreign->encp->encrypted) {
|
2019-01-07 02:18:36 +00:00
|
|
|
QTC::TC("qpdf", "QPDF pipe foreign encrypted stream");
|
|
|
|
}
|
|
|
|
return pipeStreamData(
|
2022-04-02 21:14:10 +00:00
|
|
|
foreign->encp,
|
|
|
|
foreign->file,
|
|
|
|
*this,
|
2022-07-23 15:48:28 +00:00
|
|
|
foreign->foreign_og,
|
2022-04-02 21:14:10 +00:00
|
|
|
foreign->offset,
|
|
|
|
foreign->length,
|
|
|
|
foreign->local_dict,
|
|
|
|
pipeline,
|
|
|
|
suppress_warnings,
|
|
|
|
will_retry);
|
2012-12-30 00:00:05 +00:00
|
|
|
}
|
2019-01-04 16:50:02 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Throw a generic exception when we lack context for something more specific. New code should not
|
|
|
|
// use this. This method exists to improve somewhat from calling assert in very old code.
|
2019-01-04 16:50:02 +00:00
|
|
|
void
|
|
|
|
QPDF::stopOnError(std::string const& message)
|
|
|
|
{
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF("", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an exception of type qpdf_e_damaged_pdf.
|
|
|
|
QPDFExc
|
|
|
|
QPDF::damagedPDF(
|
|
|
|
std::shared_ptr<InputSource> const& input,
|
|
|
|
std::string const& object,
|
|
|
|
qpdf_offset_t offset,
|
|
|
|
std::string const& message)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {qpdf_e_damaged_pdf, input->getName(), object, offset, message};
|
2022-09-28 11:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an exception of type qpdf_e_damaged_pdf. The object is taken from
|
|
|
|
// m->last_object_description.
|
|
|
|
QPDFExc
|
|
|
|
QPDF::damagedPDF(
|
2023-05-21 17:35:09 +00:00
|
|
|
std::shared_ptr<InputSource> const& input, qpdf_offset_t offset, std::string const& message)
|
2022-09-28 11:25:52 +00:00
|
|
|
{
|
|
|
|
return damagedPDF(input, m->last_object_description, offset, message);
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Return an exception of type qpdf_e_damaged_pdf. The filename is taken from m->file.
|
2022-09-28 11:25:52 +00:00
|
|
|
QPDFExc
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::damagedPDF(std::string const& object, qpdf_offset_t offset, std::string const& message)
|
2022-09-28 11:25:52 +00:00
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {qpdf_e_damaged_pdf, m->file->getName(), object, offset, message};
|
2022-09-28 11:25:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Return an exception of type qpdf_e_damaged_pdf. The filename is taken from m->file and the
|
|
|
|
// offset from .m->file->getLastOffset().
|
2022-09-28 11:25:52 +00:00
|
|
|
QPDFExc
|
|
|
|
QPDF::damagedPDF(std::string const& object, std::string const& message)
|
|
|
|
{
|
|
|
|
return damagedPDF(object, m->file->getLastOffset(), message);
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Return an exception of type qpdf_e_damaged_pdf. The filename is taken from m->file and the object
|
|
|
|
// from .m->last_object_description.
|
2022-09-28 11:25:52 +00:00
|
|
|
QPDFExc
|
|
|
|
QPDF::damagedPDF(qpdf_offset_t offset, std::string const& message)
|
|
|
|
{
|
|
|
|
return damagedPDF(m->last_object_description, offset, message);
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Return an exception of type qpdf_e_damaged_pdf. The filename is taken from m->file, the object
|
|
|
|
// from m->last_object_description and the offset from m->file->getLastOffset().
|
2022-09-28 11:25:52 +00:00
|
|
|
QPDFExc
|
|
|
|
QPDF::damagedPDF(std::string const& message)
|
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
return damagedPDF(m->last_object_description, m->file->getLastOffset(), message);
|
2019-01-04 16:50:02 +00:00
|
|
|
}
|
2022-06-25 17:55:45 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::everCalledGetAllPages() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->ever_called_get_all_pages;
|
2022-06-25 17:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::everPushedInheritedAttributesToPages() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->ever_pushed_inherited_attributes_to_pages;
|
2022-06-25 17:55:45 +00:00
|
|
|
}
|
2023-01-28 18:41:58 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::removeSecurityRestrictions()
|
|
|
|
{
|
|
|
|
auto root = getRoot();
|
|
|
|
root.removeKey("/Perms");
|
|
|
|
auto acroform = root.getKey("/AcroForm");
|
|
|
|
if (acroform.isDictionary() && acroform.hasKey("/SigFlags")) {
|
|
|
|
acroform.replaceKey("/SigFlags", QPDFObjectHandle::newInteger(0));
|
|
|
|
}
|
|
|
|
}
|