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
|
|
|
|
2024-07-05 13:09:43 +00:00
|
|
|
#include <qpdf/QPDF_private.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <atomic>
|
2023-05-20 18:56:01 +00:00
|
|
|
#include <cstring>
|
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-16 17:21:57 +00:00
|
|
|
namespace
|
2021-01-06 14:49:10 +00:00
|
|
|
{
|
2024-08-13 15:18:04 +00:00
|
|
|
class InvalidInputSource final: public InputSource
|
2021-01-06 14:49:10 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
public:
|
2024-08-13 15:18:04 +00:00
|
|
|
InvalidInputSource(std::string const& name) :
|
|
|
|
name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~InvalidInputSource() final = default;
|
2023-05-20 12:56:33 +00:00
|
|
|
qpdf_offset_t
|
2024-08-13 15:18:04 +00:00
|
|
|
findAndSkipNextEOL() final
|
2022-04-16 17:21:57 +00:00
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
std::string const&
|
2024-08-13 15:18:04 +00:00
|
|
|
getName() const final
|
2022-04-16 17:21:57 +00:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
qpdf_offset_t
|
2024-08-13 15:18:04 +00:00
|
|
|
tell() final
|
2022-04-16 17:21:57 +00:00
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
void
|
2024-08-13 15:18:04 +00:00
|
|
|
seek(qpdf_offset_t offset, int whence) final
|
2022-04-16 17:21:57 +00:00
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
void
|
2024-08-13 15:18:04 +00:00
|
|
|
rewind() final
|
2022-04-16 17:21:57 +00:00
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
size_t
|
2024-08-13 15:18:04 +00:00
|
|
|
read(char* buffer, size_t length) final
|
2022-04-16 17:21:57 +00:00
|
|
|
{
|
|
|
|
throwException();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
void
|
2024-08-13 15:18:04 +00:00
|
|
|
unreadCh(char ch) final
|
2022-04-16 17:21:57 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2024-08-13 15:18:04 +00:00
|
|
|
|
|
|
|
std::string const& name;
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-08-09 13:14:56 +00:00
|
|
|
QPDF::Members::Members(QPDF& qpdf) :
|
2022-06-05 17:30:42 +00:00
|
|
|
log(QPDFLogger::defaultLogger()),
|
2024-08-13 15:18:04 +00:00
|
|
|
file_sp(new InvalidInputSource(no_input_name)),
|
2024-09-05 13:50:38 +00:00
|
|
|
file(file_sp.get()),
|
2024-08-09 13:14:56 +00:00
|
|
|
encp(new EncryptionParameters),
|
2024-10-07 14:15:27 +00:00
|
|
|
objects(qpdf, this, file)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDF::QPDF() :
|
2024-08-09 13:14:56 +00:00
|
|
|
m(new Members(*this))
|
2017-08-22 01:33:44 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2024-10-07 17:24:37 +00:00
|
|
|
QPDF::~QPDF() = default;
|
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
|
|
|
{
|
2024-09-05 13:50:38 +00:00
|
|
|
m->file_sp = source;
|
|
|
|
m->file = source.get();
|
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()
|
|
|
|
{
|
2024-08-13 15:18:04 +00:00
|
|
|
m->no_input_name = "closed input source";
|
|
|
|
m->file_sp = std::shared_ptr<InputSource>(new InvalidInputSource(m->no_input_name));
|
2024-09-05 13:50:38 +00:00
|
|
|
m->file = m->file_sp.get();
|
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()
|
|
|
|
{
|
2024-08-13 15:18:04 +00:00
|
|
|
m->pdf_version = "1.3";
|
|
|
|
m->no_input_name = "empty PDF";
|
2024-10-07 14:15:27 +00:00
|
|
|
m->objects.xref_table().initialize_empty();
|
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)
|
|
|
|
{
|
2024-10-07 14:15:27 +00:00
|
|
|
m->objects.xref_table().ignore_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
|
|
|
}
|
|
|
|
|
2024-07-02 13:14:21 +00:00
|
|
|
void
|
2024-07-18 09:41:15 +00:00
|
|
|
QPDF::setMaxWarnings(size_t val)
|
2024-07-02 13:14:21 +00:00
|
|
|
{
|
2024-07-09 15:38:02 +00:00
|
|
|
m->max_warnings = val;
|
2024-07-02 13:14:21 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2024-10-07 14:15:27 +00:00
|
|
|
m->objects.xref_table().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");
|
2024-09-05 13:50:38 +00:00
|
|
|
m->file_sp =
|
|
|
|
std::shared_ptr<InputSource>(new OffsetInputSource(m->file_sp, global_offset));
|
|
|
|
m->file = m->file_sp.get();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-10-07 14:15:27 +00:00
|
|
|
m->objects.xref_table().initialize();
|
2008-04-29 12:55:25 +00:00
|
|
|
initializeEncryption();
|
2024-10-07 14:15:27 +00:00
|
|
|
if (m->objects.xref_table().size() > 0 && !getRoot().getKey("/Pages").isDictionary()) {
|
2024-06-25 14:10:09 +00:00
|
|
|
// QPDFs created from JSON have an empty xref table and no root object yet.
|
2024-06-25 14:46:47 +00:00
|
|
|
throw damagedPDF("", 0, "unable to find page tree");
|
2024-06-25 14:10:09 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2024-07-18 09:41:15 +00:00
|
|
|
if (m->max_warnings > 0 && m->warnings.size() >= m->max_warnings) {
|
|
|
|
stopOnError("Too many warnings - file is too badly damaged");
|
|
|
|
}
|
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::showXRefTable()
|
|
|
|
{
|
2024-10-07 14:15:27 +00:00
|
|
|
m->objects.xref_table().show();
|
2024-08-12 13:53:38 +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
|
|
|
}
|
2024-10-07 14:15:27 +00:00
|
|
|
if (!m->objects.xref_table().resolve()) {
|
2022-11-26 21:43:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDF fix dangling triggered xref reconstruction");
|
2024-10-07 14:15:27 +00:00
|
|
|
m->objects.xref_table().resolve();
|
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.
|
2024-10-07 16:57:06 +00:00
|
|
|
return toS(m->objects.next_id().getObj() - 1);
|
2018-06-22 18:52:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:44:19 +00:00
|
|
|
std::vector<QPDFObjectHandle>
|
|
|
|
QPDF::getAllObjects()
|
|
|
|
{
|
|
|
|
return m->objects.all();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFTokenizer::Token
|
2024-08-21 23:30:18 +00:00
|
|
|
QPDF::readToken(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
|
|
|
}
|
|
|
|
|
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-09-26 17:27:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::makeIndirectObject(QPDFObjectHandle oh)
|
|
|
|
{
|
2024-06-19 16:18:43 +00:00
|
|
|
if (!oh) {
|
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
|
|
|
}
|
2024-10-07 12:42:19 +00:00
|
|
|
return m->objects.make_indirect(oh.getObj());
|
2022-09-26 17:27:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 18:56:01 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newReserved()
|
|
|
|
{
|
2024-10-07 12:42:19 +00:00
|
|
|
return m->objects.make_indirect(QPDF_Reserved::create());
|
2023-05-20 18:56:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 12:28:48 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newIndirectNull()
|
|
|
|
{
|
2024-10-07 12:42:19 +00:00
|
|
|
return m->objects.make_indirect(QPDF_Null::create());
|
2023-08-25 12:28:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:27:25 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::newStream()
|
|
|
|
{
|
2024-10-07 12:42:19 +00:00
|
|
|
return m->objects.make_indirect(
|
|
|
|
QPDF_Stream::create(this, m->objects.next_id(), 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2024-10-07 15:39:27 +00:00
|
|
|
return m->objects.get(og);
|
2013-06-14 15:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 18:22:37 +00:00
|
|
|
QPDFObjectHandle
|
2024-10-07 15:39:27 +00:00
|
|
|
QPDF::getObject(int id, int gen)
|
2022-08-01 18:22:37 +00:00
|
|
|
{
|
2024-10-07 15:39:27 +00:00
|
|
|
return m->objects.get(id, gen);
|
2022-08-01 18:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getObjectByObjGen(QPDFObjGen const& og)
|
|
|
|
{
|
2024-10-07 15:39:27 +00:00
|
|
|
return m->objects.get(og);
|
2022-08-01 18:22:37 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
2024-10-07 15:39:27 +00:00
|
|
|
QPDF::getObjectByID(int id, int gen)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2024-10-07 15:39:27 +00:00
|
|
|
return m->objects.get(id, gen);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:58:37 +00:00
|
|
|
void
|
2024-10-07 16:18:00 +00:00
|
|
|
QPDF::replaceObject(int id, int gen, QPDFObjectHandle replacement)
|
2013-06-14 15:58:37 +00:00
|
|
|
{
|
2024-10-07 16:18:00 +00:00
|
|
|
m->objects.replace(QPDFObjGen(id, gen), replacement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::replaceObject(QPDFObjGen const& og, QPDFObjectHandle replacement)
|
|
|
|
{
|
|
|
|
m->objects.replace(og, replacement);
|
2013-06-14 15:58:37 +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
|
|
|
}
|
2024-10-07 16:18:00 +00:00
|
|
|
m->objects.replace(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,
|
2024-09-05 13:50:38 +00:00
|
|
|
foreign_stream_qpdf.m->file_sp,
|
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
|
|
|
{
|
2024-10-07 16:29:35 +00:00
|
|
|
m->objects.swap(QPDFObjGen(objid1, generation1), QPDFObjGen(objid2, generation2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::swapObjects(QPDFObjGen const& og1, QPDFObjGen const& og2)
|
|
|
|
{
|
|
|
|
m->objects.swap(og1, og2);
|
2013-06-14 15:58:37 +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()
|
|
|
|
{
|
2024-10-07 14:15:27 +00:00
|
|
|
return m->objects.trailer();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF::getRoot()
|
|
|
|
{
|
2024-10-07 14:15:27 +00:00
|
|
|
auto root = m->objects.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()
|
|
|
|
{
|
2024-10-07 14:15:27 +00:00
|
|
|
if (!m->objects.xref_table().initialized()) {
|
2019-09-28 13:07:00 +00:00
|
|
|
throw std::logic_error("QPDF::getXRefTable called before parsing.");
|
|
|
|
}
|
2024-10-07 14:15:27 +00:00
|
|
|
return m->objects.xref_table().as_map();
|
2019-09-28 13:07:00 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-08-21 10:14:39 +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(
|
2024-08-21 10:14:39 +00:00
|
|
|
*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(
|
2024-08-21 10:14:39 +00:00
|
|
|
*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,
|
2024-09-05 13:50:38 +00:00
|
|
|
m->file_sp,
|
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(
|
2024-08-21 10:14:39 +00:00
|
|
|
InputSource& input, std::string const& object, qpdf_offset_t offset, std::string const& message)
|
2022-09-28 11:25:52 +00:00
|
|
|
{
|
2024-08-21 10:14:39 +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
|
2024-08-21 10:14:39 +00:00
|
|
|
QPDF::damagedPDF(InputSource& 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));
|
|
|
|
}
|
|
|
|
}
|