2022-05-03 12:21:01 +00:00
|
|
|
#include <qpdf/assert_debug.h>
|
|
|
|
|
|
|
|
#include <qpdf/qpdf-config.h> // include early for large file support
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDFWriter.hh>
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/MD5.hh>
|
|
|
|
#include <qpdf/Pl_AES_PDF.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Count.hh>
|
|
|
|
#include <qpdf/Pl_Discard.hh>
|
|
|
|
#include <qpdf/Pl_Flate.hh>
|
2015-10-25 15:09:25 +00:00
|
|
|
#include <qpdf/Pl_MD5.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Pl_PNGFilter.hh>
|
|
|
|
#include <qpdf/Pl_RC4.hh>
|
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
|
|
|
#include <qpdf/QIntC.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
|
|
|
#include <qpdf/QPDF_Name.hh>
|
|
|
|
#include <qpdf/QPDF_String.hh>
|
2022-05-03 11:41:07 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/RC4.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2013-11-29 15:20:50 +00:00
|
|
|
#include <algorithm>
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdlib>
|
2023-05-20 18:13:09 +00:00
|
|
|
#include <stdexcept>
|
2008-05-04 16:02:53 +00:00
|
|
|
|
2023-06-01 13:47:36 +00:00
|
|
|
QPDFWriter::ProgressReporter::~ProgressReporter() // NOLINT (modernize-use-equals-default)
|
2022-06-18 18:56:58 +00:00
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
|
2022-06-18 18:56:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::FunctionProgressReporter::FunctionProgressReporter(std::function<void(int)> handler) :
|
2022-06-18 18:56:58 +00:00
|
|
|
handler(handler)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:47:36 +00:00
|
|
|
QPDFWriter::FunctionProgressReporter::~FunctionProgressReporter() // NOLINT
|
|
|
|
// (modernize-use-equals-default)
|
2022-06-18 18:56:58 +00:00
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
|
2022-06-18 18:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::FunctionProgressReporter::reportProgress(int progress)
|
|
|
|
{
|
|
|
|
this->handler(progress);
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:52 +00:00
|
|
|
QPDFWriter::Members::Members(QPDF& pdf) :
|
|
|
|
pdf(pdf),
|
2023-05-21 17:35:09 +00:00
|
|
|
root_og(pdf.getRoot().getObjGen().isIndirect() ? pdf.getRoot().getObjGen() : QPDFObjGen(-1, 0))
|
2017-08-22 01:33:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFWriter::Members::~Members()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (file && close_file) {
|
2022-02-08 14:18:08 +00:00
|
|
|
fclose(file);
|
2017-08-22 01:33:52 +00:00
|
|
|
}
|
2018-08-12 16:58:39 +00:00
|
|
|
delete output_buffer;
|
2017-08-22 01:33:52 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 20:34:29 +00:00
|
|
|
QPDFWriter::QPDFWriter(QPDF& pdf) :
|
2017-08-22 01:33:52 +00:00
|
|
|
m(new Members(pdf))
|
2011-08-10 20:34:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFWriter::QPDFWriter(QPDF& pdf, char const* filename) :
|
2017-08-22 01:33:52 +00:00
|
|
|
m(new Members(pdf))
|
2011-08-10 20:34:29 +00:00
|
|
|
{
|
|
|
|
setOutputFilename(filename);
|
|
|
|
}
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::QPDFWriter(QPDF& pdf, char const* description, FILE* file, bool close_file) :
|
2017-08-22 01:33:52 +00:00
|
|
|
m(new Members(pdf))
|
2012-07-06 01:22:27 +00:00
|
|
|
{
|
|
|
|
setOutputFile(description, file, close_file);
|
|
|
|
}
|
|
|
|
|
2011-08-10 20:34:29 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setOutputFilename(char const* filename)
|
|
|
|
{
|
2012-07-06 01:22:27 +00:00
|
|
|
char const* description = filename;
|
2022-07-26 11:37:50 +00:00
|
|
|
FILE* f = nullptr;
|
2017-08-22 01:33:52 +00:00
|
|
|
bool close_file = false;
|
2022-07-26 11:37:50 +00:00
|
|
|
if (filename == nullptr) {
|
2022-02-08 14:18:08 +00:00
|
|
|
description = "standard output";
|
|
|
|
QTC::TC("qpdf", "QPDFWriter write to stdout");
|
|
|
|
f = stdout;
|
|
|
|
QUtil::binary_stdout();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter write to file");
|
|
|
|
f = QUtil::safe_fopen(filename, "wb+");
|
|
|
|
close_file = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-07-06 01:22:27 +00:00
|
|
|
setOutputFile(description, f, close_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::setOutputFile(char const* description, FILE* file, bool close_file)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->filename = description;
|
|
|
|
m->file = file;
|
|
|
|
m->close_file = close_file;
|
2023-05-21 17:35:09 +00:00
|
|
|
std::shared_ptr<Pipeline> p = std::make_shared<Pl_StdioFile>("qpdf output", file);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->to_delete.push_back(p);
|
2022-02-04 16:03:52 +00:00
|
|
|
initializePipelineStack(p.get());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 20:34:29 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setOutputMemory()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->filename = "memory buffer";
|
|
|
|
m->buffer_pipeline = new Pl_Buffer("qpdf output");
|
|
|
|
m->to_delete.push_back(std::shared_ptr<Pipeline>(m->buffer_pipeline));
|
|
|
|
initializePipelineStack(m->buffer_pipeline);
|
2011-08-10 20:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer*
|
|
|
|
QPDFWriter::getBuffer()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
Buffer* result = m->output_buffer;
|
|
|
|
m->output_buffer = nullptr;
|
2011-08-10 20:34:29 +00:00
|
|
|
return result;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer>
|
2022-02-06 17:30:18 +00:00
|
|
|
QPDFWriter::getBufferSharedPointer()
|
|
|
|
{
|
2022-04-09 18:35:56 +00:00
|
|
|
return std::shared_ptr<Buffer>(getBuffer());
|
2022-02-06 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2012-09-23 21:42:26 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setOutputPipeline(Pipeline* p)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->filename = "custom pipeline";
|
2012-09-23 21:42:26 +00:00
|
|
|
initializePipelineStack(p);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2009-10-19 20:17:14 +00:00
|
|
|
QPDFWriter::setObjectStreamMode(qpdf_object_stream_e mode)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_stream_mode = mode;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-19 20:17:14 +00:00
|
|
|
QPDFWriter::setStreamDataMode(qpdf_stream_data_e mode)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (mode) {
|
|
|
|
case qpdf_s_uncompress:
|
2023-05-21 17:35:09 +00:00
|
|
|
m->stream_decode_level = std::max(qpdf_dl_generalized, m->stream_decode_level);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->compress_streams = false;
|
2017-08-19 13:18:14 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_s_preserve:
|
2023-05-21 13:42:34 +00:00
|
|
|
m->stream_decode_level = qpdf_dl_none;
|
|
|
|
m->compress_streams = false;
|
2017-08-19 13:18:14 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_s_compress:
|
2023-05-21 17:35:09 +00:00
|
|
|
m->stream_decode_level = std::max(qpdf_dl_generalized, m->stream_decode_level);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->compress_streams = true;
|
2017-08-19 13:18:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->stream_decode_level_set = true;
|
|
|
|
m->compress_streams_set = true;
|
2017-08-19 13:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::setCompressStreams(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->compress_streams = val;
|
|
|
|
m->compress_streams_set = true;
|
2017-08-19 13:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::setDecodeLevel(qpdf_stream_decode_level_e val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->stream_decode_level = val;
|
|
|
|
m->stream_decode_level_set = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 23:59:38 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setRecompressFlate(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->recompress_flate = val;
|
2019-08-23 23:59:38 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setContentNormalization(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->normalize_content_set = true;
|
|
|
|
m->normalize_content = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::setQDFMode(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->qdf_mode = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 23:18:57 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setPreserveUnreferencedObjects(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->preserve_unreferenced_objects = val;
|
2017-07-28 23:18:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-29 16:18:03 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setNewlineBeforeEndstream(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->newline_before_endstream = val;
|
2017-07-29 16:18:03 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 20:17:39 +00:00
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::setMinimumPDFVersion(std::string const& version, int extension_level)
|
2009-10-05 00:42:48 +00:00
|
|
|
{
|
|
|
|
bool set_version = false;
|
2012-12-27 20:17:39 +00:00
|
|
|
bool set_extension_level = false;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->min_pdf_version.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
set_version = true;
|
2012-12-27 20:17:39 +00:00
|
|
|
set_extension_level = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
int old_major = 0;
|
|
|
|
int old_minor = 0;
|
|
|
|
int min_major = 0;
|
|
|
|
int min_minor = 0;
|
|
|
|
parseVersion(version, old_major, old_minor);
|
2023-05-21 13:42:34 +00:00
|
|
|
parseVersion(m->min_pdf_version, min_major, min_minor);
|
2023-05-21 17:35:09 +00:00
|
|
|
int compare = compareVersions(old_major, old_minor, min_major, min_minor);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compare > 0) {
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter increasing minimum version", extension_level == 0 ? 0 : 1);
|
2022-02-08 14:18:08 +00:00
|
|
|
set_version = true;
|
2012-12-27 20:17:39 +00:00
|
|
|
set_extension_level = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (compare == 0) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (extension_level > m->min_extension_level) {
|
2012-12-27 20:17:39 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter increasing extension level");
|
|
|
|
set_extension_level = true;
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2009-10-05 00:42:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (set_version) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->min_pdf_version = version;
|
2009-10-05 00:42:48 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (set_extension_level) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->min_extension_level = extension_level;
|
2012-12-27 20:17:39 +00:00
|
|
|
}
|
2009-10-05 00:42:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:27:38 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setMinimumPDFVersion(PDFVersion const& v)
|
|
|
|
{
|
|
|
|
std::string version;
|
|
|
|
int extension_level;
|
|
|
|
v.getVersion(version, extension_level);
|
|
|
|
setMinimumPDFVersion(version, extension_level);
|
|
|
|
}
|
|
|
|
|
2012-12-27 20:17:39 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::forcePDFVersion(std::string const& version, int extension_level)
|
2009-10-05 00:42:48 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->forced_pdf_version = version;
|
|
|
|
m->forced_extension_level = extension_level;
|
2009-10-05 00:42:48 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 19:30:13 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setExtraHeaderText(std::string const& text)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->extra_header_text = text;
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((m->extra_header_text.length() > 0) && (*(m->extra_header_text.rbegin()) != '\n')) {
|
2012-09-06 19:30:13 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter extra header text add newline");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->extra_header_text += "\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-09-06 19:30:13 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter extra header text no newline");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setStaticID(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->static_id = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 15:09:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setDeterministicID(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->deterministic_id = val;
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
|
|
|
|
2009-10-19 00:36:51 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setStaticAesIV(bool val)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val) {
|
2022-02-08 14:18:08 +00:00
|
|
|
Pl_AES_PDF::useStaticIV();
|
2009-10-19 00:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-09 16:27:13 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setSuppressOriginalObjectIDs(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->suppress_original_object_ids = val;
|
2009-03-09 16:27:13 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setPreserveEncryption(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->preserve_encryption = val;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::setLinearization(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->linearized = val;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pclm = false;
|
2017-08-01 12:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:45:52 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setLinearizationPass1Filename(std::string const& filename)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->lin_pass1_filename = filename;
|
2018-01-31 21:45:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:16:24 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setPCLm(bool val)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pclm = val;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->linearized = false;
|
2017-08-01 12:16:24 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-30 20:05:28 +00:00
|
|
|
QPDFWriter::setR2EncryptionParametersInsecure(
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* user_password,
|
|
|
|
char const* owner_password,
|
|
|
|
bool allow_print,
|
|
|
|
bool allow_modify,
|
|
|
|
bool allow_extract,
|
|
|
|
bool allow_annotate)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
std::set<int> clear;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_print) {
|
2022-02-08 14:18:08 +00:00
|
|
|
clear.insert(3);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_modify) {
|
2022-02-08 14:18:08 +00:00
|
|
|
clear.insert(4);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_extract) {
|
2022-02-08 14:18:08 +00:00
|
|
|
clear.insert(5);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_annotate) {
|
2022-02-08 14:18:08 +00:00
|
|
|
clear.insert(6);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setEncryptionParameters(user_password, owner_password, 1, 2, 5, clear);
|
|
|
|
}
|
|
|
|
|
2019-01-15 01:45:08 +00:00
|
|
|
void
|
2022-04-30 20:05:28 +00:00
|
|
|
QPDFWriter::setR3EncryptionParametersInsecure(
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* user_password,
|
|
|
|
char const* owner_password,
|
|
|
|
bool allow_accessibility,
|
|
|
|
bool allow_extract,
|
|
|
|
bool allow_assemble,
|
|
|
|
bool allow_annotate_and_form,
|
|
|
|
bool allow_form_filling,
|
|
|
|
bool allow_modify_other,
|
2019-01-15 01:45:08 +00:00
|
|
|
qpdf_r3_print_e print)
|
|
|
|
{
|
|
|
|
std::set<int> clear;
|
|
|
|
interpretR3EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
clear,
|
|
|
|
user_password,
|
|
|
|
owner_password,
|
|
|
|
allow_accessibility,
|
|
|
|
allow_extract,
|
|
|
|
allow_assemble,
|
|
|
|
allow_annotate_and_form,
|
|
|
|
allow_form_filling,
|
|
|
|
allow_modify_other,
|
|
|
|
print,
|
|
|
|
qpdf_r3m_all);
|
2009-10-18 02:03:18 +00:00
|
|
|
setEncryptionParameters(user_password, owner_password, 2, 3, 16, clear);
|
|
|
|
}
|
|
|
|
|
2019-01-15 01:45:08 +00:00
|
|
|
void
|
2022-04-30 20:05:28 +00:00
|
|
|
QPDFWriter::setR4EncryptionParametersInsecure(
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* user_password,
|
|
|
|
char const* owner_password,
|
|
|
|
bool allow_accessibility,
|
|
|
|
bool allow_extract,
|
|
|
|
bool allow_assemble,
|
|
|
|
bool allow_annotate_and_form,
|
|
|
|
bool allow_form_filling,
|
|
|
|
bool allow_modify_other,
|
2019-01-15 01:45:08 +00:00
|
|
|
qpdf_r3_print_e print,
|
2022-04-02 21:14:10 +00:00
|
|
|
bool encrypt_metadata,
|
|
|
|
bool use_aes)
|
2019-01-15 01:45:08 +00:00
|
|
|
{
|
|
|
|
std::set<int> clear;
|
|
|
|
interpretR3EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
clear,
|
|
|
|
user_password,
|
|
|
|
owner_password,
|
|
|
|
allow_accessibility,
|
|
|
|
allow_extract,
|
|
|
|
allow_assemble,
|
|
|
|
allow_annotate_and_form,
|
|
|
|
allow_form_filling,
|
|
|
|
allow_modify_other,
|
|
|
|
print,
|
|
|
|
qpdf_r3m_all);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encrypt_use_aes = use_aes;
|
|
|
|
m->encrypt_metadata = encrypt_metadata;
|
2009-10-18 02:03:18 +00:00
|
|
|
setEncryptionParameters(user_password, owner_password, 4, 4, 16, clear);
|
|
|
|
}
|
|
|
|
|
2019-01-15 01:45:08 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setR5EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* user_password,
|
|
|
|
char const* owner_password,
|
|
|
|
bool allow_accessibility,
|
|
|
|
bool allow_extract,
|
|
|
|
bool allow_assemble,
|
|
|
|
bool allow_annotate_and_form,
|
|
|
|
bool allow_form_filling,
|
|
|
|
bool allow_modify_other,
|
2019-01-15 01:45:08 +00:00
|
|
|
qpdf_r3_print_e print,
|
|
|
|
bool encrypt_metadata)
|
|
|
|
{
|
|
|
|
std::set<int> clear;
|
|
|
|
interpretR3EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
clear,
|
|
|
|
user_password,
|
|
|
|
owner_password,
|
|
|
|
allow_accessibility,
|
|
|
|
allow_extract,
|
|
|
|
allow_assemble,
|
|
|
|
allow_annotate_and_form,
|
|
|
|
allow_form_filling,
|
|
|
|
allow_modify_other,
|
|
|
|
print,
|
|
|
|
qpdf_r3m_all);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encrypt_use_aes = true;
|
|
|
|
m->encrypt_metadata = encrypt_metadata;
|
2012-12-30 02:31:21 +00:00
|
|
|
setEncryptionParameters(user_password, owner_password, 5, 5, 32, clear);
|
|
|
|
}
|
|
|
|
|
2019-01-15 01:45:08 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setR6EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* user_password,
|
|
|
|
char const* owner_password,
|
|
|
|
bool allow_accessibility,
|
|
|
|
bool allow_extract,
|
|
|
|
bool allow_assemble,
|
|
|
|
bool allow_annotate_and_form,
|
|
|
|
bool allow_form_filling,
|
|
|
|
bool allow_modify_other,
|
2019-01-15 01:45:08 +00:00
|
|
|
qpdf_r3_print_e print,
|
|
|
|
bool encrypt_metadata)
|
|
|
|
{
|
|
|
|
std::set<int> clear;
|
|
|
|
interpretR3EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
clear,
|
|
|
|
user_password,
|
|
|
|
owner_password,
|
|
|
|
allow_accessibility,
|
|
|
|
allow_extract,
|
|
|
|
allow_assemble,
|
|
|
|
allow_annotate_and_form,
|
|
|
|
allow_form_filling,
|
|
|
|
allow_modify_other,
|
|
|
|
print,
|
|
|
|
qpdf_r3m_all);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encrypt_use_aes = true;
|
|
|
|
m->encrypt_metadata = encrypt_metadata;
|
2012-12-30 02:31:21 +00:00
|
|
|
setEncryptionParameters(user_password, owner_password, 5, 6, 32, clear);
|
|
|
|
}
|
|
|
|
|
2009-10-18 02:03:18 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::interpretR3EncryptionParameters(
|
|
|
|
std::set<int>& clear,
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* user_password,
|
|
|
|
char const* owner_password,
|
|
|
|
bool allow_accessibility,
|
|
|
|
bool allow_extract,
|
|
|
|
bool allow_assemble,
|
|
|
|
bool allow_annotate_and_form,
|
|
|
|
bool allow_form_filling,
|
|
|
|
bool allow_modify_other,
|
|
|
|
qpdf_r3_print_e print,
|
|
|
|
qpdf_r3_modify_e modify)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Acrobat 5 security options:
|
|
|
|
|
|
|
|
// Checkboxes:
|
|
|
|
// Enable Content Access for the Visually Impaired
|
|
|
|
// Allow Content Copying and Extraction
|
|
|
|
|
|
|
|
// Allowed changes menu:
|
|
|
|
// None
|
|
|
|
// Only Document Assembly
|
|
|
|
// Only Form Field Fill-in or Signing
|
2009-02-21 02:54:31 +00:00
|
|
|
// Comment Authoring, Form Field Fill-in or Signing
|
2008-04-29 12:55:25 +00:00
|
|
|
// General Editing, Comment and Form Field Authoring
|
|
|
|
|
|
|
|
// Allowed printing menu:
|
|
|
|
// None
|
|
|
|
// Low Resolution
|
|
|
|
// Full printing
|
|
|
|
|
2019-01-15 01:45:08 +00:00
|
|
|
// Meanings of bits in P when R >= 3
|
|
|
|
//
|
|
|
|
// 3: low-resolution printing
|
|
|
|
// 4: document modification except as controlled by 6, 9, and 11
|
|
|
|
// 5: extraction
|
|
|
|
// 6: add/modify annotations (comment), fill in forms
|
|
|
|
// if 4+6 are set, also allows modification of form fields
|
|
|
|
// 9: fill in forms even if 6 is clear
|
|
|
|
// 10: accessibility; ignored by readers, should always be set
|
|
|
|
// 11: document assembly even if 4 is clear
|
|
|
|
// 12: high-resolution printing
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_accessibility) {
|
2019-01-15 01:45:08 +00:00
|
|
|
// setEncryptionParameters sets this if R > 3
|
2022-02-08 14:18:08 +00:00
|
|
|
clear.insert(10);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_extract) {
|
2022-02-08 14:18:08 +00:00
|
|
|
clear.insert(5);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Note: these switch statements all "fall through" (no break statements). Each option clears
|
|
|
|
// successively more access bits.
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (print) {
|
|
|
|
case qpdf_r3p_none:
|
|
|
|
clear.insert(3); // any printing
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_r3p_low:
|
|
|
|
clear.insert(12); // high resolution printing
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_r3p_full:
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// no default so gcc warns for missing cases
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Modify options. The qpdf_r3_modify_e options control groups of bits and lack the full
|
|
|
|
// flexibility of the spec. This is unfortunate, but it's been in the API for ages, and we're
|
|
|
|
// stuck with it. See also allow checks below to control the bits individually.
|
2019-01-15 01:45:08 +00:00
|
|
|
|
|
|
|
// NOT EXERCISED IN TEST SUITE
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (modify) {
|
|
|
|
case qpdf_r3m_none:
|
|
|
|
clear.insert(11); // document assembly
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_r3m_assembly:
|
|
|
|
clear.insert(9); // filling in form fields
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_r3m_form:
|
|
|
|
clear.insert(6); // modify annotations, fill in form fields
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_r3m_annotate:
|
|
|
|
clear.insert(4); // other modifications
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_r3m_all:
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// no default so gcc warns for missing cases
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-01-15 01:45:08 +00:00
|
|
|
// END NOT EXERCISED IN TEST SUITE
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_assemble) {
|
2019-01-15 01:45:08 +00:00
|
|
|
clear.insert(11);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_annotate_and_form) {
|
2019-01-15 01:45:08 +00:00
|
|
|
clear.insert(6);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_form_filling) {
|
2019-01-15 01:45:08 +00:00
|
|
|
clear.insert(9);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!allow_modify_other) {
|
2019-01-15 01:45:08 +00:00
|
|
|
clear.insert(4);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::setEncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* user_password,
|
|
|
|
char const* owner_password,
|
|
|
|
int V,
|
|
|
|
int R,
|
|
|
|
int key_len,
|
|
|
|
std::set<int>& bits_to_clear)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// PDF specification refers to bits with the low bit numbered 1.
|
|
|
|
// We have to convert this into a bit field.
|
|
|
|
|
2009-02-21 02:54:31 +00:00
|
|
|
// Specification always requires bits 1 and 2 to be cleared.
|
2008-04-29 12:55:25 +00:00
|
|
|
bits_to_clear.insert(1);
|
|
|
|
bits_to_clear.insert(2);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (R > 3) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Bit 10 is deprecated and should always be set. This used to mean accessibility. There
|
|
|
|
// is no way to disable accessibility with R > 3.
|
2012-12-30 02:31:21 +00:00
|
|
|
bits_to_clear.erase(10);
|
|
|
|
}
|
|
|
|
|
2008-05-05 02:22:40 +00:00
|
|
|
int P = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
// Create the complement of P, then invert.
|
2022-04-30 17:23:18 +00:00
|
|
|
for (int b: bits_to_clear) {
|
|
|
|
P |= (1 << (b - 1));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
P = ~P;
|
|
|
|
|
|
|
|
generateID();
|
|
|
|
std::string O;
|
|
|
|
std::string U;
|
2012-12-30 02:31:21 +00:00
|
|
|
std::string OE;
|
|
|
|
std::string UE;
|
|
|
|
std::string Perms;
|
|
|
|
std::string encryption_key;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (V < 5) {
|
2012-12-30 02:31:21 +00:00
|
|
|
QPDF::compute_encryption_O_U(
|
2023-05-21 17:35:09 +00:00
|
|
|
user_password, owner_password, V, R, key_len, P, m->encrypt_metadata, m->id1, O, U);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-12-30 02:31:21 +00:00
|
|
|
QPDF::compute_encryption_parameters_V5(
|
2022-04-02 21:14:10 +00:00
|
|
|
user_password,
|
|
|
|
owner_password,
|
|
|
|
V,
|
|
|
|
R,
|
|
|
|
key_len,
|
|
|
|
P,
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encrypt_metadata,
|
|
|
|
m->id1,
|
2022-04-02 21:14:10 +00:00
|
|
|
encryption_key,
|
|
|
|
O,
|
|
|
|
U,
|
|
|
|
OE,
|
|
|
|
UE,
|
|
|
|
Perms);
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
setEncryptionParametersInternal(
|
2023-05-21 17:35:09 +00:00
|
|
|
V, R, key_len, P, O, U, OE, UE, Perms, m->id1, user_password, encryption_key);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-14 13:13:49 +00:00
|
|
|
QPDFWriter::copyEncryptionParameters(QPDF& qpdf)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->preserve_encryption = false;
|
2012-07-14 13:13:49 +00:00
|
|
|
QPDFObjectHandle trailer = qpdf.getTrailer();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (trailer.hasKey("/Encrypt")) {
|
2015-10-25 15:09:25 +00:00
|
|
|
generateID();
|
2023-05-21 13:42:34 +00:00
|
|
|
m->id1 = trailer.getKey("/ID").getArrayItem(0).getStringValue();
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle encrypt = trailer.getKey("/Encrypt");
|
|
|
|
int V = encrypt.getKey("/V").getIntValueAsInt();
|
|
|
|
int key_len = 5;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (V > 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
key_len = encrypt.getKey("/Length").getIntValueAsInt() / 8;
|
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
if (encrypt.hasKey("/EncryptMetadata") && encrypt.getKey("/EncryptMetadata").isBool()) {
|
|
|
|
m->encrypt_metadata = encrypt.getKey("/EncryptMetadata").getBoolValue();
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (V >= 4) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// When copying encryption parameters, use AES even if the original file did not.
|
|
|
|
// Acrobat doesn't create files with V >= 4 that don't use AES, and the logic of
|
|
|
|
// figuring out whether AES is used or not is complicated with /StmF, /StrF, and /EFF
|
|
|
|
// all potentially having different values.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encrypt_use_aes = true;
|
2012-07-15 23:07:59 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter copy encrypt metadata", m->encrypt_metadata ? 0 : 1);
|
2023-05-21 13:42:34 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter copy use_aes", m->encrypt_use_aes ? 0 : 1);
|
2012-12-30 02:31:21 +00:00
|
|
|
std::string OE;
|
|
|
|
std::string UE;
|
|
|
|
std::string Perms;
|
|
|
|
std::string encryption_key;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (V >= 5) {
|
2012-12-30 02:31:21 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter copy V5");
|
2022-02-08 14:18:08 +00:00
|
|
|
OE = encrypt.getKey("/OE").getStringValue();
|
2012-12-30 02:31:21 +00:00
|
|
|
UE = encrypt.getKey("/UE").getStringValue();
|
2022-02-08 14:18:08 +00:00
|
|
|
Perms = encrypt.getKey("/Perms").getStringValue();
|
2012-12-30 02:31:21 +00:00
|
|
|
encryption_key = qpdf.getEncryptionKey();
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
setEncryptionParametersInternal(
|
|
|
|
V,
|
|
|
|
encrypt.getKey("/R").getIntValueAsInt(),
|
|
|
|
key_len,
|
|
|
|
static_cast<int>(encrypt.getKey("/P").getIntValue()),
|
|
|
|
encrypt.getKey("/O").getStringValue(),
|
|
|
|
encrypt.getKey("/U").getStringValue(),
|
2012-12-30 02:31:21 +00:00
|
|
|
OE,
|
|
|
|
UE,
|
|
|
|
Perms,
|
2023-05-21 13:42:34 +00:00
|
|
|
m->id1, // m->id1 == the other file's id1
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf.getPaddedUserPassword(),
|
2012-12-30 02:31:21 +00:00
|
|
|
encryption_key);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 00:17:11 +00:00
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::disableIncompatibleEncryption(int major, int minor, int extension_level)
|
2009-10-19 00:17:11 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->encrypted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2009-10-19 00:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool disable = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compareVersions(major, minor, 1, 3) < 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
disable = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
int V = QUtil::string_to_int(m->encryption_dictionary["/V"].c_str());
|
|
|
|
int R = QUtil::string_to_int(m->encryption_dictionary["/R"].c_str());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compareVersions(major, minor, 1, 4) < 0) {
|
|
|
|
if ((V > 1) || (R > 2)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
disable = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (compareVersions(major, minor, 1, 5) < 0) {
|
|
|
|
if ((V > 2) || (R > 3)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
disable = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (compareVersions(major, minor, 1, 6) < 0) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypt_use_aes) {
|
2022-02-08 14:18:08 +00:00
|
|
|
disable = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (
|
|
|
|
(compareVersions(major, minor, 1, 7) < 0) ||
|
2023-05-21 17:35:09 +00:00
|
|
|
((compareVersions(major, minor, 1, 7) == 0) && extension_level < 3)) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((V >= 5) || (R >= 5)) {
|
2012-12-27 20:17:39 +00:00
|
|
|
disable = true;
|
|
|
|
}
|
|
|
|
}
|
2009-10-19 00:17:11 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (disable) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter forced version disabled encryption");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encrypted = false;
|
2009-10-19 00:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 16:18:46 +00:00
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::parseVersion(std::string const& version, int& major, int& minor) const
|
2010-08-02 16:18:46 +00:00
|
|
|
{
|
2017-08-29 16:27:59 +00:00
|
|
|
major = QUtil::string_to_int(version.c_str());
|
2010-08-02 16:18:46 +00:00
|
|
|
minor = 0;
|
|
|
|
size_t p = version.find('.');
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((p != std::string::npos) && (version.length() > p)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
minor = QUtil::string_to_int(version.substr(p + 1).c_str());
|
2010-08-02 16:18:46 +00:00
|
|
|
}
|
2022-09-21 16:49:21 +00:00
|
|
|
std::string tmp = std::to_string(major) + "." + std::to_string(minor);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (tmp != version) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// The version number in the input is probably invalid. This happens with some files that
|
|
|
|
// are designed to exercise bugs, such as files in the fuzzer corpus. Unfortunately
|
|
|
|
// QPDFWriter doesn't have a way to give a warning, so we just ignore this case.
|
2010-08-02 16:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::compareVersions(int major1, int minor1, int major2, int minor2) const
|
2010-08-02 16:18:46 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (major1 < major2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return -1;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (major1 > major2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return 1;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (minor1 < minor2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return -1;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (minor1 > minor2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return 1;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
return 0;
|
2010-08-02 16:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::setEncryptionParametersInternal(
|
2022-04-02 21:14:10 +00:00
|
|
|
int V,
|
|
|
|
int R,
|
|
|
|
int key_len,
|
|
|
|
int P,
|
|
|
|
std::string const& O,
|
|
|
|
std::string const& U,
|
|
|
|
std::string const& OE,
|
|
|
|
std::string const& UE,
|
|
|
|
std::string const& Perms,
|
|
|
|
std::string const& id1,
|
|
|
|
std::string const& user_password,
|
2012-12-30 02:31:21 +00:00
|
|
|
std::string const& encryption_key)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encryption_V = V;
|
|
|
|
m->encryption_R = R;
|
|
|
|
m->encryption_dictionary["/Filter"] = "/Standard";
|
|
|
|
m->encryption_dictionary["/V"] = std::to_string(V);
|
|
|
|
m->encryption_dictionary["/Length"] = std::to_string(key_len * 8);
|
|
|
|
m->encryption_dictionary["/R"] = std::to_string(R);
|
|
|
|
m->encryption_dictionary["/P"] = std::to_string(P);
|
|
|
|
m->encryption_dictionary["/O"] = QPDF_String(O).unparse(true);
|
|
|
|
m->encryption_dictionary["/U"] = QPDF_String(U).unparse(true);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (V >= 5) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encryption_dictionary["/OE"] = QPDF_String(OE).unparse(true);
|
|
|
|
m->encryption_dictionary["/UE"] = QPDF_String(UE).unparse(true);
|
|
|
|
m->encryption_dictionary["/Perms"] = QPDF_String(Perms).unparse(true);
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (R >= 6) {
|
2012-12-30 02:31:21 +00:00
|
|
|
setMinimumPDFVersion("1.7", 8);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (R == 5) {
|
2012-12-27 20:17:39 +00:00
|
|
|
setMinimumPDFVersion("1.7", 3);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (R == 4) {
|
2023-05-21 13:42:34 +00:00
|
|
|
setMinimumPDFVersion(m->encrypt_use_aes ? "1.6" : "1.5");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (R == 3) {
|
2012-12-27 20:17:39 +00:00
|
|
|
setMinimumPDFVersion("1.4");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-12-27 20:17:39 +00:00
|
|
|
setMinimumPDFVersion("1.3");
|
|
|
|
}
|
2012-07-16 01:14:02 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if ((R >= 4) && (!m->encrypt_metadata)) {
|
|
|
|
m->encryption_dictionary["/EncryptMetadata"] = "false";
|
2009-10-18 02:03:18 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((V == 4) || (V == 5)) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// The spec says the value for the crypt filter key can be anything, and xpdf seems to
|
|
|
|
// agree. However, Adobe Reader won't open our files unless we use /StdCF.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encryption_dictionary["/StmF"] = "/StdCF";
|
|
|
|
m->encryption_dictionary["/StrF"] = "/StdCF";
|
2023-05-21 17:35:09 +00:00
|
|
|
std::string method = (m->encrypt_use_aes ? ((V < 5) ? "/AESV2" : "/AESV3") : "/V2");
|
2023-05-27 17:19:52 +00:00
|
|
|
// The PDF spec says the /Length key is optional, but the PDF previewer on some versions of
|
|
|
|
// MacOS won't open encrypted files without it.
|
2023-05-21 17:35:09 +00:00
|
|
|
m->encryption_dictionary["/CF"] = "<< /StdCF << /AuthEvent /DocOpen /CFM " + method +
|
|
|
|
" /Length " + std::string((V < 5) ? "16" : "32") + " >> >>";
|
2009-10-18 02:03:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encrypted = true;
|
2009-10-18 02:03:18 +00:00
|
|
|
QPDF::EncryptionData encryption_data(
|
2023-05-21 13:42:34 +00:00
|
|
|
V, R, key_len, P, O, U, OE, UE, Perms, id1, m->encrypt_metadata);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (V < 5) {
|
2023-05-21 17:35:09 +00:00
|
|
|
m->encryption_key = QPDF::compute_encryption_key(user_password, encryption_data);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encryption_key = encryption_key;
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::setDataKey(int objid)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->cur_data_key = QPDF::compute_data_key(
|
2023-05-21 17:35:09 +00:00
|
|
|
m->encryption_key, objid, 0, m->encrypt_use_aes, m->encryption_V, m->encryption_R);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
unsigned int
|
|
|
|
QPDFWriter::bytesNeeded(long long n)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
unsigned int bytes = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (n) {
|
2022-02-08 14:18:08 +00:00
|
|
|
++bytes;
|
|
|
|
n >>= 8;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-24 19:26:28 +00:00
|
|
|
QPDFWriter::writeBinary(unsigned long long val, unsigned int bytes)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bytes > sizeof(unsigned long long)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("QPDFWriter::writeBinary called with too many bytes");
|
2013-10-05 20:37:27 +00:00
|
|
|
}
|
2012-06-24 19:26:28 +00:00
|
|
|
unsigned char data[sizeof(unsigned long long)];
|
2022-04-02 21:14:10 +00:00
|
|
|
for (unsigned int i = 0; i < bytes; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
data[bytes - i - 1] = static_cast<unsigned char>(val & 0xff);
|
|
|
|
val >>= 8;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline->write(data, bytes);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-01-22 18:25:17 +00:00
|
|
|
QPDFWriter::writeString(std::string_view str)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
m->pipeline->write(reinterpret_cast<unsigned char const*>(str.data()), str.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-09 18:35:56 +00:00
|
|
|
QPDFWriter::writeBuffer(std::shared_ptr<Buffer>& b)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline->write(b->getBuffer(), b->getSize());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-01-22 18:25:17 +00:00
|
|
|
QPDFWriter::writeStringQDF(std::string_view str)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-01-22 18:25:17 +00:00
|
|
|
if (m->qdf_mode) {
|
2023-05-21 17:35:09 +00:00
|
|
|
m->pipeline->write(reinterpret_cast<unsigned char const*>(str.data()), str.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-01-22 18:25:17 +00:00
|
|
|
QPDFWriter::writeStringNoQDF(std::string_view str)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-01-22 18:25:17 +00:00
|
|
|
if (!m->qdf_mode) {
|
2023-05-21 17:35:09 +00:00
|
|
|
m->pipeline->write(reinterpret_cast<unsigned char const*>(str.data()), str.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-18 23:01:31 +00:00
|
|
|
void
|
2023-01-08 16:08:27 +00:00
|
|
|
QPDFWriter::writePad(size_t nspaces)
|
2010-04-18 23:01:31 +00:00
|
|
|
{
|
2023-01-08 16:08:27 +00:00
|
|
|
writeString(std::string(nspaces, ' '));
|
2010-04-18 23:01:31 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
Pipeline*
|
|
|
|
QPDFWriter::pushPipeline(Pipeline* p)
|
|
|
|
{
|
2022-07-26 11:37:50 +00:00
|
|
|
qpdf_assert_debug(dynamic_cast<Pl_Count*>(p) == nullptr);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline_stack.push_back(p);
|
2008-04-29 12:55:25 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-08-10 20:34:29 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::initializePipelineStack(Pipeline* p)
|
2011-08-10 20:34:29 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline = new Pl_Count("pipeline stack base", p);
|
|
|
|
m->to_delete.push_back(std::shared_ptr<Pipeline>(m->pipeline));
|
|
|
|
m->pipeline_stack.push_back(m->pipeline);
|
2011-08-10 20:34:29 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2019-08-28 02:10:11 +00:00
|
|
|
QPDFWriter::activatePipelineStack(PipelinePopper& pp)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
std::string stack_id("stack " + std::to_string(m->next_stack_id));
|
|
|
|
auto* c = new Pl_Count(stack_id.c_str(), m->pipeline_stack.back());
|
|
|
|
++m->next_stack_id;
|
|
|
|
m->pipeline_stack.push_back(c);
|
|
|
|
m->pipeline = c;
|
2019-08-28 02:10:11 +00:00
|
|
|
pp.stack_id = stack_id;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 02:10:11 +00:00
|
|
|
QPDFWriter::PipelinePopper::~PipelinePopper()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (stack_id.empty()) {
|
2019-08-28 02:10:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-03 12:21:01 +00:00
|
|
|
qpdf_assert_debug(qw->m->pipeline_stack.size() >= 2);
|
2019-08-28 02:10:11 +00:00
|
|
|
qw->m->pipeline->finish();
|
2023-05-21 17:35:09 +00:00
|
|
|
qpdf_assert_debug(dynamic_cast<Pl_Count*>(qw->m->pipeline_stack.back()) == qw->m->pipeline);
|
2023-05-27 17:19:52 +00:00
|
|
|
// It might be possible for this assertion to fail if writeLinearized exits by exception when
|
|
|
|
// deterministic ID, but I don't think so. As of this writing, this is the only case in which
|
|
|
|
// two dynamically allocated PipelinePopper objects ever exist at the same time, so the
|
|
|
|
// assertion will fail if they get popped out of order from automatic destruction.
|
2022-05-03 12:21:01 +00:00
|
|
|
qpdf_assert_debug(qw->m->pipeline->getIdentifier() == stack_id);
|
2019-08-28 02:10:11 +00:00
|
|
|
delete qw->m->pipeline_stack.back();
|
|
|
|
qw->m->pipeline_stack.pop_back();
|
2022-07-26 11:37:50 +00:00
|
|
|
while (dynamic_cast<Pl_Count*>(qw->m->pipeline_stack.back()) == nullptr) {
|
2022-02-08 14:18:08 +00:00
|
|
|
Pipeline* p = qw->m->pipeline_stack.back();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (dynamic_cast<Pl_MD5*>(p) == qw->m->md5_pipeline) {
|
2022-07-26 11:37:50 +00:00
|
|
|
qw->m->md5_pipeline = nullptr;
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
qw->m->pipeline_stack.pop_back();
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* buf = dynamic_cast<Pl_Buffer*>(p);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bp && buf) {
|
2022-02-08 14:18:08 +00:00
|
|
|
*bp = buf->getBufferSharedPointer();
|
|
|
|
}
|
|
|
|
delete p;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-08-28 02:10:11 +00:00
|
|
|
qw->m->pipeline = dynamic_cast<Pl_Count*>(qw->m->pipeline_stack.back());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 02:03:18 +00:00
|
|
|
void
|
2012-06-20 15:20:57 +00:00
|
|
|
QPDFWriter::adjustAESStreamLength(size_t& length)
|
2009-10-18 02:03:18 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted && (!m->cur_data_key.empty()) && m->encrypt_use_aes) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Stream length will be padded with 1 to 16 bytes to end up as a multiple of 16. It will
|
|
|
|
// also be prepended by 16 bits of random data.
|
2022-02-08 14:18:08 +00:00
|
|
|
length += 32 - (length & 0xf);
|
2009-10-18 02:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2019-08-28 02:10:11 +00:00
|
|
|
QPDFWriter::pushEncryptionFilter(PipelinePopper& pp)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted && (!m->cur_data_key.empty())) {
|
2022-07-26 11:37:50 +00:00
|
|
|
Pipeline* p = nullptr;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypt_use_aes) {
|
2022-02-08 14:18:08 +00:00
|
|
|
p = new Pl_AES_PDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"aes stream encryption",
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline,
|
2022-04-02 21:14:10 +00:00
|
|
|
true,
|
2023-05-21 13:42:34 +00:00
|
|
|
QUtil::unsigned_char_pointer(m->cur_data_key),
|
|
|
|
m->cur_data_key.length());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
p = new Pl_RC4(
|
|
|
|
"rc4 stream encryption",
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline,
|
|
|
|
QUtil::unsigned_char_pointer(m->cur_data_key),
|
|
|
|
QIntC::to_int(m->cur_data_key.length()));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
pushPipeline(p);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-27 17:19:52 +00:00
|
|
|
// Must call this unconditionally so we can call popPipelineStack to balance
|
|
|
|
// pushEncryptionFilter().
|
2019-08-28 02:10:11 +00:00
|
|
|
activatePipelineStack(pp);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-08-28 02:10:11 +00:00
|
|
|
QPDFWriter::pushDiscardFilter(PipelinePopper& pp)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
pushPipeline(new Pl_Discard());
|
2019-08-28 02:10:11 +00:00
|
|
|
activatePipelineStack(pp);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 15:09:25 +00:00
|
|
|
void
|
2019-08-28 02:10:11 +00:00
|
|
|
QPDFWriter::pushMD5Pipeline(PipelinePopper& pp)
|
2015-10-25 15:09:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->id2.empty()) {
|
2015-10-25 15:09:25 +00:00
|
|
|
// Can't happen in the code
|
2022-04-02 21:14:10 +00:00
|
|
|
throw std::logic_error("Deterministic ID computation enabled after ID"
|
|
|
|
" generation has already occurred.");
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_assert_debug(m->deterministic_id);
|
|
|
|
qpdf_assert_debug(m->md5_pipeline == nullptr);
|
|
|
|
qpdf_assert_debug(m->pipeline->getCount() == 0);
|
|
|
|
m->md5_pipeline = new Pl_MD5("qpdf md5", m->pipeline);
|
|
|
|
m->md5_pipeline->persistAcrossFinish(true);
|
2023-05-27 17:19:52 +00:00
|
|
|
// Special case code in popPipelineStack clears m->md5_pipeline upon deletion.
|
2023-05-21 13:42:34 +00:00
|
|
|
pushPipeline(m->md5_pipeline);
|
2019-08-28 02:10:11 +00:00
|
|
|
activatePipelineStack(pp);
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::computeDeterministicIDData()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_assert_debug(m->md5_pipeline != nullptr);
|
|
|
|
qpdf_assert_debug(m->deterministic_id_data.empty());
|
|
|
|
m->deterministic_id_data = m->md5_pipeline->getHexDigest();
|
|
|
|
m->md5_pipeline->enable(false);
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
int
|
|
|
|
QPDFWriter::openObject(int objid)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (objid == 0) {
|
2023-05-21 13:42:34 +00:00
|
|
|
objid = m->next_objid++;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->xref[objid] = QPDFXRefEntry(m->pipeline->getCount());
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(objid));
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString(" 0 obj\n");
|
|
|
|
return objid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::closeObject(int objid)
|
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Write a newline before endobj as it makes the file easier to repair.
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString("\nendobj\n");
|
|
|
|
writeStringQDF("\n");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->lengths[objid] = m->pipeline->getCount() - m->xref[objid].getOffset();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-14 18:53:47 +00:00
|
|
|
QPDFWriter::assignCompressedObjectNumbers(QPDFObjGen const& og)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-14 18:53:47 +00:00
|
|
|
int objid = og.getObj();
|
2023-05-21 13:42:34 +00:00
|
|
|
if ((og.getGen() != 0) || (m->object_stream_to_objects.count(objid) == 0)) {
|
2013-06-14 18:53:47 +00:00
|
|
|
// This is not an object stream.
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Reserve numbers for the objects that belong to this object stream.
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->object_stream_to_objects[objid]) {
|
|
|
|
m->obj_renumber[iter] = m->next_objid++;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::enqueueObject(QPDFObjectHandle object)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (object.isIndirect()) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// This owner check can only be done for indirect objects. It is possible for a direct
|
|
|
|
// object to have an owning QPDF that is from another file if a direct QPDFObjectHandle from
|
|
|
|
// one file was insert into another file without copying. Doing that is safe even if the
|
|
|
|
// original QPDF gets destroyed, which just disconnects the QPDFObjectHandle from its owner.
|
2023-05-21 13:42:34 +00:00
|
|
|
if (object.getOwningQPDF() != &(m->pdf)) {
|
2012-07-14 12:07:23 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter foreign object");
|
2023-05-27 17:19:52 +00:00
|
|
|
throw std::logic_error("QPDFObjectHandle from different QPDF found while writing. Use "
|
|
|
|
"QPDF::copyForeignObject to add objects from another file.");
|
2012-07-14 12:07:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->qdf_mode && object.isStreamOfType("/XRef")) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// As a special case, do not output any extraneous XRef streams in QDF mode. Doing so
|
|
|
|
// will confuse fix-qdf, which expects to see only one XRef stream at the end of the
|
|
|
|
// file. This case can occur when creating a QDF from a file with object streams when
|
|
|
|
// preserving unreferenced objects since the old cross reference streams are not
|
2020-01-26 21:43:55 +00:00
|
|
|
// actually referenced by object number.
|
|
|
|
QTC::TC("qpdf", "QPDFWriter ignore XRef in qdf mode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen og = object.getObjGen();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->obj_renumber.count(og) == 0) {
|
|
|
|
if (m->object_to_object_stream.count(og)) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// This is in an object stream. Don't process it here. Instead, enqueue the object
|
|
|
|
// stream. Object streams always have generation 0.
|
2023-05-21 13:42:34 +00:00
|
|
|
int stream_id = m->object_to_object_stream[og];
|
2023-05-27 17:19:52 +00:00
|
|
|
// Detect loops by storing invalid object ID 0, which will get overwritten later.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->obj_renumber[og] = 0;
|
|
|
|
enqueueObject(m->pdf.getObjectByID(stream_id, 0));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_queue.push_back(object);
|
|
|
|
m->obj_renumber[og] = m->next_objid++;
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((og.getGen() == 0) && m->object_stream_to_objects.count(og.getObj())) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// For linearized files, uncompressed objects go at end, and we take care of
|
|
|
|
// assigning numbers to them elsewhere.
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->linearized) {
|
2022-02-08 14:18:08 +00:00
|
|
|
assignCompressedObjectNumbers(og);
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
} else if ((!m->direct_stream_lengths) && object.isStream()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// reserve next object ID for length
|
2023-05-21 13:42:34 +00:00
|
|
|
++m->next_objid;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
} else if (m->obj_renumber[og] == 0) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// This can happen if a specially constructed file indicates that an object stream is
|
|
|
|
// inside itself.
|
2017-08-12 11:35:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter ignore self-referential object stream");
|
|
|
|
}
|
2023-01-06 13:24:11 +00:00
|
|
|
return;
|
2023-05-21 13:42:34 +00:00
|
|
|
} else if (!m->linearized) {
|
2023-01-06 13:24:11 +00:00
|
|
|
if (object.isArray()) {
|
|
|
|
for (auto& item: object.getArrayAsVector()) {
|
|
|
|
enqueueObject(item);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-01-06 13:24:11 +00:00
|
|
|
} else if (object.isDictionary()) {
|
|
|
|
for (auto& item: object.getDictAsMap()) {
|
|
|
|
if (!item.second.isNull()) {
|
|
|
|
enqueueObject(item.second);
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
// ignore
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::unparseChild(QPDFObjectHandle child, int level, int flags)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->linearized) {
|
2022-02-08 14:18:08 +00:00
|
|
|
enqueueObject(child);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (child.isIndirect()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen old_og = child.getObjGen();
|
2023-05-21 13:42:34 +00:00
|
|
|
int new_id = m->obj_renumber[old_og];
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(new_id));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" 0 R");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
unparseObject(child, level, flags);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:09:25 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::writeTrailer(
|
2023-05-21 17:35:09 +00:00
|
|
|
trailer_e which, int size, bool xref_stream, qpdf_offset_t prev, int linearization_pass)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-12-31 10:50:43 +00:00
|
|
|
QPDFObjectHandle trailer = getTrimmedTrailer();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (xref_stream) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->cur_data_key.clear();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("trailer <<");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
writeStringQDF("\n");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (which == t_lin_second) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /Size ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(size));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& key: trailer.getKeys()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeStringQDF(" ");
|
|
|
|
writeStringNoQDF(" ");
|
|
|
|
writeString(QPDF_Name::normalizeName(key));
|
|
|
|
writeString(" ");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (key == "/Size") {
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(size));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (which == t_lin_first) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /Prev ");
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t pos = m->pipeline->getCount();
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(prev));
|
2023-05-21 17:35:09 +00:00
|
|
|
writePad(QIntC::to_size(pos - m->pipeline->getCount() + 21));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
unparseChild(trailer.getKey(key), 1, 0);
|
|
|
|
}
|
|
|
|
writeStringQDF("\n");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write ID
|
|
|
|
writeStringQDF(" ");
|
|
|
|
writeString(" /ID [");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (linearization_pass == 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::string original_id1 = getOriginalID1();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (original_id1.empty()) {
|
2018-02-04 23:02:56 +00:00
|
|
|
writeString("<00000000000000000000000000000000>");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Write a string of zeroes equal in length to the representation of the original ID.
|
|
|
|
// While writing the original ID would have the same number of bytes, it would cause a
|
|
|
|
// change to the deterministic ID generated by older versions of the software that
|
|
|
|
// hard-coded the length of the ID to 16 bytes.
|
2018-02-04 23:02:56 +00:00
|
|
|
writeString("<");
|
|
|
|
size_t len = QPDF_String(original_id1).unparse(true).length() - 2;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2018-02-04 23:02:56 +00:00
|
|
|
writeString("0");
|
|
|
|
}
|
|
|
|
writeString(">");
|
|
|
|
}
|
|
|
|
writeString("<00000000000000000000000000000000>");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
if ((linearization_pass == 0) && (m->deterministic_id)) {
|
2015-10-25 15:09:25 +00:00
|
|
|
computeDeterministicIDData();
|
|
|
|
}
|
|
|
|
generateID();
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(QPDF_String(m->id1).unparse(true));
|
|
|
|
writeString(QPDF_String(m->id2).unparse(true));
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString("]");
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (which != t_lin_second) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Write reference to encryption dictionary
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /Encrypt ");
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(std::to_string(m->encryption_dict_objid));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" 0 R");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeStringQDF("\n");
|
|
|
|
writeStringNoQDF(" ");
|
|
|
|
writeString(">>");
|
|
|
|
}
|
|
|
|
|
2020-12-25 14:49:38 +00:00
|
|
|
bool
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::willFilterStream(
|
|
|
|
QPDFObjectHandle stream,
|
|
|
|
bool& compress_stream,
|
|
|
|
bool& is_metadata,
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer>* stream_data)
|
2020-12-25 14:49:38 +00:00
|
|
|
{
|
|
|
|
compress_stream = false;
|
|
|
|
is_metadata = false;
|
2020-12-27 00:45:01 +00:00
|
|
|
|
2020-12-25 14:49:38 +00:00
|
|
|
QPDFObjGen old_og = stream.getObjGen();
|
|
|
|
QPDFObjectHandle stream_dict = stream.getDict();
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (stream_dict.isDictionaryOfType("/Metadata")) {
|
2020-12-25 14:49:38 +00:00
|
|
|
is_metadata = true;
|
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
bool filter = (stream.isDataModified() || m->compress_streams || m->stream_decode_level);
|
2020-12-27 00:45:01 +00:00
|
|
|
bool filter_on_write = stream.getFilterOnWrite();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!filter_on_write) {
|
2020-12-27 00:45:01 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter getFilterOnWrite false");
|
|
|
|
filter = false;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (filter_on_write && m->compress_streams) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Don't filter if the stream is already compressed with FlateDecode. This way we don't make
|
|
|
|
// it worse if the original file used a better Flate algorithm, and we don't spend time and
|
|
|
|
// CPU cycles uncompressing and recompressing stuff. This can be overridden with
|
2020-12-25 14:49:38 +00:00
|
|
|
// setRecompressFlate(true).
|
|
|
|
QPDFObjectHandle filter_obj = stream_dict.getKey("/Filter");
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((!m->recompress_flate) && (!stream.isDataModified()) && filter_obj.isName() &&
|
|
|
|
((filter_obj.getName() == "/FlateDecode") || (filter_obj.getName() == "/Fl"))) {
|
2020-12-25 14:49:38 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter not recompressing /FlateDecode");
|
|
|
|
filter = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool normalize = false;
|
|
|
|
bool uncompress = false;
|
2023-05-21 17:35:09 +00:00
|
|
|
if (filter_on_write && is_metadata && ((!m->encrypted) || (m->encrypt_metadata == false))) {
|
2020-12-25 14:49:38 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter not compressing metadata");
|
|
|
|
filter = true;
|
|
|
|
compress_stream = false;
|
|
|
|
uncompress = true;
|
2023-05-21 17:35:09 +00:00
|
|
|
} else if (filter_on_write && m->normalize_content && m->normalized_streams.count(old_og)) {
|
2020-12-25 14:49:38 +00:00
|
|
|
normalize = true;
|
|
|
|
filter = true;
|
2023-05-21 13:42:34 +00:00
|
|
|
} else if (filter_on_write && filter && m->compress_streams) {
|
2020-12-25 14:49:38 +00:00
|
|
|
compress_stream = true;
|
|
|
|
QTC::TC("qpdf", "QPDFWriter compressing uncompressed stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filtered = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int attempt = 1; attempt <= 2; ++attempt) {
|
2020-12-25 14:49:38 +00:00
|
|
|
pushPipeline(new Pl_Buffer("stream data"));
|
|
|
|
PipelinePopper pp_stream_data(this, stream_data);
|
|
|
|
activatePipelineStack(pp_stream_data);
|
2022-05-20 17:32:59 +00:00
|
|
|
try {
|
|
|
|
filtered = stream.pipeStreamData(
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline,
|
2022-05-20 17:32:59 +00:00
|
|
|
(((filter && normalize) ? qpdf_ef_normalize : 0) |
|
|
|
|
((filter && compress_stream) ? qpdf_ef_compress : 0)),
|
2023-05-21 17:35:09 +00:00
|
|
|
(filter ? (uncompress ? qpdf_dl_all : m->stream_decode_level) : qpdf_dl_none),
|
2022-05-20 17:32:59 +00:00
|
|
|
false,
|
|
|
|
(attempt == 1));
|
|
|
|
} catch (std::runtime_error& e) {
|
|
|
|
throw std::runtime_error(
|
2023-05-21 17:35:09 +00:00
|
|
|
"error while getting stream data for " + stream.unparse() + ": " + e.what());
|
2022-05-20 17:32:59 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (filter && (!filtered)) {
|
2020-12-25 14:49:38 +00:00
|
|
|
// Try again
|
|
|
|
filter = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2020-12-25 14:49:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!filtered) {
|
2020-12-25 14:49:38 +00:00
|
|
|
compress_stream = false;
|
|
|
|
}
|
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::unparseObject(
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFObjectHandle object, int level, int flags, size_t stream_length, bool compress)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-14 18:53:47 +00:00
|
|
|
QPDFObjGen old_og = object.getObjGen();
|
2019-06-21 03:35:23 +00:00
|
|
|
int child_flags = flags & ~f_stream;
|
2023-01-06 14:38:08 +00:00
|
|
|
if (level < 0) {
|
|
|
|
throw std::logic_error("invalid level in QPDFWriter::unparseObject");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-27 17:19:52 +00:00
|
|
|
// For non-qdf, "indent" is a single space between tokens. For qdf, indent includes the
|
|
|
|
// preceding newline.
|
2023-03-20 22:53:51 +00:00
|
|
|
std::string indent = " ";
|
|
|
|
if (m->qdf_mode) {
|
|
|
|
indent.append(static_cast<size_t>(2 * level), ' ');
|
|
|
|
indent[0] = '\n';
|
|
|
|
}
|
2023-01-06 14:38:08 +00:00
|
|
|
|
|
|
|
if (auto const tc = object.getTypeCode(); tc == ::ot_array) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Note: PDF spec 1.4 implementation note 121 states that Acrobat requires a space after the
|
|
|
|
// [ in the /H key of the linearization parameter dictionary. We'll do this unconditionally
|
|
|
|
// for all arrays because it looks nicer and doesn't make the files that much bigger.
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("[");
|
2023-01-06 14:38:08 +00:00
|
|
|
for (auto const& item: object.getArrayAsVector()) {
|
2023-03-20 22:53:51 +00:00
|
|
|
writeString(indent);
|
2022-02-08 14:18:08 +00:00
|
|
|
writeStringQDF(" ");
|
2023-01-06 14:38:08 +00:00
|
|
|
unparseChild(item, level + 1, child_flags);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-03-20 22:53:51 +00:00
|
|
|
writeString(indent);
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("]");
|
2023-01-06 14:38:08 +00:00
|
|
|
} else if (tc == ::ot_dictionary) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Make a shallow copy of this object so we can modify it safely without affecting the
|
|
|
|
// original. This code has logic to skip certain keys in agreement with prepareFileForWrite
|
|
|
|
// and with skip_stream_parameters so that replacing them doesn't leave unreferenced objects
|
2023-05-29 18:26:18 +00:00
|
|
|
// in the output. We can use unsafeShallowCopy here because all we are doing is removing or
|
|
|
|
// replacing top-level keys.
|
2020-04-02 21:58:19 +00:00
|
|
|
object = object.unsafeShallowCopy();
|
2012-12-30 18:28:37 +00:00
|
|
|
|
2012-12-27 20:17:39 +00:00
|
|
|
// Handle special cases for specific dictionaries.
|
|
|
|
|
2012-12-30 18:28:37 +00:00
|
|
|
// Extensions dictionaries.
|
|
|
|
|
|
|
|
// We have one of several cases:
|
2012-12-27 20:17:39 +00:00
|
|
|
//
|
|
|
|
// * We need ADBE
|
|
|
|
// - We already have Extensions
|
|
|
|
// - If it has the right ADBE, preserve it
|
|
|
|
// - Otherwise, replace ADBE
|
|
|
|
// - We don't have Extensions: create one from scratch
|
|
|
|
// * We don't want ADBE
|
|
|
|
// - We already have Extensions
|
|
|
|
// - If it only has ADBE, remove it
|
|
|
|
// - If it has other things, keep those and remove ADBE
|
|
|
|
// - We have no extensions: no action required
|
|
|
|
//
|
2023-05-27 17:19:52 +00:00
|
|
|
// Before writing, we guarantee that /Extensions, if present, is direct through the ADBE
|
|
|
|
// dictionary, so we can modify in place.
|
2012-12-27 20:17:39 +00:00
|
|
|
|
2023-03-17 10:41:12 +00:00
|
|
|
const bool is_root = (old_og == m->root_og);
|
2012-12-27 20:17:39 +00:00
|
|
|
bool have_extensions_other = false;
|
|
|
|
bool have_extensions_adbe = false;
|
|
|
|
|
|
|
|
QPDFObjectHandle extensions;
|
2023-03-17 10:41:12 +00:00
|
|
|
if (is_root) {
|
2023-05-21 17:35:09 +00:00
|
|
|
if (object.hasKey("/Extensions") && object.getKey("/Extensions").isDictionary()) {
|
2012-12-27 20:17:39 +00:00
|
|
|
extensions = object.getKey("/Extensions");
|
|
|
|
}
|
|
|
|
}
|
2012-12-30 18:28:37 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (extensions.isInitialized()) {
|
2012-12-27 20:17:39 +00:00
|
|
|
std::set<std::string> keys = extensions.getKeys();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (keys.count("/ADBE") > 0) {
|
2012-12-27 20:17:39 +00:00
|
|
|
have_extensions_adbe = true;
|
|
|
|
keys.erase("/ADBE");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (keys.size() > 0) {
|
2012-12-27 20:17:39 +00:00
|
|
|
have_extensions_other = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
bool need_extensions_adbe = (m->final_extension_level > 0);
|
2012-12-27 20:17:39 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (is_root) {
|
|
|
|
if (need_extensions_adbe) {
|
|
|
|
if (!(have_extensions_other || have_extensions_adbe)) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// We need Extensions and don't have it. Create it here.
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter create Extensions", m->qdf_mode ? 0 : 1);
|
2022-07-24 19:42:23 +00:00
|
|
|
extensions = object.replaceKeyAndGetNew(
|
2022-04-30 00:21:07 +00:00
|
|
|
"/Extensions", QPDFObjectHandle::newDictionary());
|
2012-12-27 20:17:39 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (!have_extensions_other) {
|
2012-12-27 20:17:39 +00:00
|
|
|
// We have Extensions dictionary and don't want one.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (have_extensions_adbe) {
|
2012-12-27 20:17:39 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter remove existing Extensions");
|
2012-12-30 18:28:37 +00:00
|
|
|
object.removeKey("/Extensions");
|
|
|
|
extensions = QPDFObjectHandle(); // uninitialized
|
2012-12-27 20:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-30 18:28:37 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (extensions.isInitialized()) {
|
2012-12-27 20:17:39 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter preserve Extensions");
|
|
|
|
QPDFObjectHandle adbe = extensions.getKey("/ADBE");
|
|
|
|
if (adbe.isDictionary() &&
|
2023-05-21 17:35:09 +00:00
|
|
|
adbe.getKey("/BaseVersion").isNameAndEquals("/" + m->final_pdf_version) &&
|
2012-12-27 20:17:39 +00:00
|
|
|
adbe.getKey("/ExtensionLevel").isInteger() &&
|
2023-05-21 17:35:09 +00:00
|
|
|
(adbe.getKey("/ExtensionLevel").getIntValue() == m->final_extension_level)) {
|
2012-12-27 20:17:39 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter preserve ADBE");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
if (need_extensions_adbe) {
|
2012-12-30 18:28:37 +00:00
|
|
|
extensions.replaceKey(
|
|
|
|
"/ADBE",
|
|
|
|
QPDFObjectHandle::parse(
|
2023-05-21 17:35:09 +00:00
|
|
|
"<< /BaseVersion /" + m->final_pdf_version + " /ExtensionLevel " +
|
2023-05-21 13:42:34 +00:00
|
|
|
std::to_string(m->final_extension_level) + " >>"));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-12-30 18:28:37 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter remove ADBE");
|
|
|
|
extensions.removeKey("/ADBE");
|
2012-12-27 20:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-30 18:28:37 +00:00
|
|
|
// Stream dictionaries.
|
2012-12-27 20:17:39 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (flags & f_stream) {
|
2012-12-30 18:28:37 +00:00
|
|
|
// Suppress /Length since we will write it manually
|
|
|
|
object.removeKey("/Length");
|
|
|
|
|
2019-06-09 21:19:07 +00:00
|
|
|
// If /DecodeParms is an empty list, remove it.
|
|
|
|
if (object.getKey("/DecodeParms").isArray() &&
|
2022-04-02 21:14:10 +00:00
|
|
|
(0 == object.getKey("/DecodeParms").getArrayNItems())) {
|
2019-06-09 21:19:07 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter remove empty DecodeParms");
|
|
|
|
object.removeKey("/DecodeParms");
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (flags & f_filtered) {
|
2012-12-30 18:54:12 +00:00
|
|
|
// We will supply our own filter and decode
|
|
|
|
// parameters.
|
2012-12-30 18:28:37 +00:00
|
|
|
object.removeKey("/Filter");
|
|
|
|
object.removeKey("/DecodeParms");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Make sure, no matter what else we have, that we don't have /Crypt in the output
|
|
|
|
// filters.
|
2012-12-30 18:54:12 +00:00
|
|
|
QPDFObjectHandle filter = object.getKey("/Filter");
|
|
|
|
QPDFObjectHandle decode_parms = object.getKey("/DecodeParms");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (filter.isOrHasName("/Crypt")) {
|
|
|
|
if (filter.isName()) {
|
2012-12-30 18:54:12 +00:00
|
|
|
object.removeKey("/Filter");
|
|
|
|
object.removeKey("/DecodeParms");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-12-30 18:54:12 +00:00
|
|
|
int idx = -1;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < filter.getArrayNItems(); ++i) {
|
2012-12-30 18:54:12 +00:00
|
|
|
QPDFObjectHandle item = filter.getArrayItem(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (item.isNameAndEquals("/Crypt")) {
|
2012-12-30 18:54:12 +00:00
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx >= 0) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// If filter is an array, then the code in QPDF_Stream has already
|
|
|
|
// verified that DecodeParms and Filters are arrays of the same length,
|
|
|
|
// but if they weren't for some reason, eraseItem does type and bounds
|
|
|
|
// checking.
|
2012-12-30 18:54:12 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter remove Crypt");
|
|
|
|
filter.eraseItem(idx);
|
|
|
|
decode_parms.eraseItem(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-27 20:17:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("<<");
|
|
|
|
|
2023-01-06 14:38:08 +00:00
|
|
|
for (auto& item: object.getDictAsMap()) {
|
|
|
|
if (!item.second.isNull()) {
|
|
|
|
auto const& key = item.first;
|
2023-03-20 22:53:51 +00:00
|
|
|
writeString(indent);
|
2023-01-06 14:38:08 +00:00
|
|
|
writeStringQDF(" ");
|
|
|
|
writeString(QPDF_Name::normalizeName(key));
|
|
|
|
writeString(" ");
|
|
|
|
if (key == "/Contents" && object.isDictionaryOfType("/Sig") &&
|
|
|
|
object.hasKey("/ByteRange")) {
|
|
|
|
QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
|
|
|
|
unparseChild(
|
2023-05-21 17:35:09 +00:00
|
|
|
item.second, level + 1, child_flags | f_hex_string | f_no_encryption);
|
2023-01-06 14:38:08 +00:00
|
|
|
} else {
|
|
|
|
unparseChild(item.second, level + 1, child_flags);
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (flags & f_stream) {
|
2023-03-20 22:53:51 +00:00
|
|
|
writeString(indent);
|
|
|
|
writeStringQDF(" ");
|
|
|
|
writeString("/Length ");
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->direct_stream_lengths) {
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(stream_length));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(std::to_string(m->cur_stream_length_id));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" 0 R");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compress && (flags & f_filtered)) {
|
2023-03-20 22:53:51 +00:00
|
|
|
writeString(indent);
|
|
|
|
writeStringQDF(" ");
|
|
|
|
writeString("/Filter /FlateDecode");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 22:53:51 +00:00
|
|
|
writeString(indent);
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(">>");
|
2023-01-06 14:38:08 +00:00
|
|
|
} else if (tc == ::ot_stream) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Write stream data to a buffer.
|
2023-05-21 13:42:34 +00:00
|
|
|
int new_id = m->obj_renumber[old_og];
|
|
|
|
if (!m->direct_stream_lengths) {
|
|
|
|
m->cur_stream_length_id = new_id + 1;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
flags |= f_stream;
|
|
|
|
bool compress_stream = false;
|
2020-12-25 14:49:38 +00:00
|
|
|
bool is_metadata = false;
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer> stream_data;
|
2023-05-21 17:35:09 +00:00
|
|
|
if (willFilterStream(object, compress_stream, is_metadata, &stream_data)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
flags |= f_filtered;
|
|
|
|
}
|
2020-12-25 14:49:38 +00:00
|
|
|
QPDFObjectHandle stream_dict = object.getDict();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->cur_stream_length = stream_data->getSize();
|
|
|
|
if (is_metadata && m->encrypted && (!m->encrypt_metadata)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Don't encrypt stream data for the metadata stream
|
2023-05-21 13:42:34 +00:00
|
|
|
m->cur_data_key.clear();
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
adjustAESStreamLength(m->cur_stream_length);
|
2023-05-21 17:35:09 +00:00
|
|
|
unparseObject(stream_dict, 0, flags, m->cur_stream_length, compress_stream);
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned char last_char = '\0';
|
|
|
|
writeString("\nstream\n");
|
2019-08-28 02:10:11 +00:00
|
|
|
{
|
|
|
|
PipelinePopper pp_enc(this);
|
|
|
|
pushEncryptionFilter(pp_enc);
|
|
|
|
writeBuffer(stream_data);
|
2023-05-21 13:42:34 +00:00
|
|
|
last_char = m->pipeline->getLastChar();
|
2019-08-28 02:10:11 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
if (m->newline_before_endstream || (m->qdf_mode && (last_char != '\n'))) {
|
2017-08-12 00:57:05 +00:00
|
|
|
writeString("\n");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->added_newline = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->added_newline = false;
|
2017-08-12 00:57:05 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("endstream");
|
2023-01-06 14:38:08 +00:00
|
|
|
} else if (tc == ::ot_string) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::string val;
|
2023-05-21 17:35:09 +00:00
|
|
|
if (m->encrypted && (!(flags & f_in_ostream)) && (!(flags & f_no_encryption)) &&
|
|
|
|
(!m->cur_data_key.empty())) {
|
2022-02-08 14:18:08 +00:00
|
|
|
val = object.getStringValue();
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypt_use_aes) {
|
2022-02-08 14:18:08 +00:00
|
|
|
Pl_Buffer bufpl("encrypted string");
|
|
|
|
Pl_AES_PDF pl(
|
2022-04-02 21:14:10 +00:00
|
|
|
"aes encrypt string",
|
|
|
|
&bufpl,
|
|
|
|
true,
|
2023-05-21 13:42:34 +00:00
|
|
|
QUtil::unsigned_char_pointer(m->cur_data_key),
|
|
|
|
m->cur_data_key.length());
|
2022-05-03 22:09:49 +00:00
|
|
|
pl.writeString(val);
|
2022-02-08 14:18:08 +00:00
|
|
|
pl.finish();
|
2023-11-16 16:26:04 +00:00
|
|
|
val = QPDF_String(bufpl.getString()).unparse(true);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
auto tmp_ph = QUtil::make_unique_cstr(val);
|
2022-01-22 22:33:53 +00:00
|
|
|
char* tmp = tmp_ph.get();
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t vlen = val.length();
|
2022-04-02 21:14:10 +00:00
|
|
|
RC4 rc4(
|
2023-05-21 13:42:34 +00:00
|
|
|
QUtil::unsigned_char_pointer(m->cur_data_key),
|
|
|
|
QIntC::to_int(m->cur_data_key.length()));
|
2022-05-03 21:43:07 +00:00
|
|
|
auto data = QUtil::unsigned_char_pointer(tmp);
|
|
|
|
rc4.process(data, vlen, data);
|
2022-02-08 14:18:08 +00:00
|
|
|
val = QPDF_String(std::string(tmp, vlen)).unparse();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (flags & f_hex_string) {
|
2022-02-08 14:18:08 +00:00
|
|
|
val = QPDF_String(object.getStringValue()).unparse(true);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
val = object.unparseResolved();
|
|
|
|
}
|
|
|
|
writeString(val);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(object.unparseResolved());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::writeObjectStreamOffsets(std::vector<qpdf_offset_t>& offsets, int first_obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < offsets.size(); ++i) {
|
|
|
|
if (i != 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeStringQDF("\n");
|
|
|
|
writeStringNoQDF(" ");
|
|
|
|
}
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(i + QIntC::to_size(first_obj)));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(offsets.at(i)));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
writeString("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::writeObjectStream(QPDFObjectHandle object)
|
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Note: object might be null if this is a place-holder for an object stream that we are
|
|
|
|
// generating from scratch.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2013-06-14 18:53:47 +00:00
|
|
|
QPDFObjGen old_og = object.getObjGen();
|
2022-05-03 12:21:01 +00:00
|
|
|
qpdf_assert_debug(old_og.getGen() == 0);
|
2013-06-14 18:53:47 +00:00
|
|
|
int old_id = old_og.getObj();
|
2023-05-21 13:42:34 +00:00
|
|
|
int new_id = m->obj_renumber[old_og];
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-06-21 23:32:21 +00:00
|
|
|
std::vector<qpdf_offset_t> offsets;
|
|
|
|
qpdf_offset_t first = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Generate stream itself. We have to do this in two passes so we can calculate offsets in the
|
|
|
|
// first pass.
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer> stream_buffer;
|
2008-04-29 12:55:25 +00:00
|
|
|
int first_obj = -1;
|
|
|
|
bool compressed = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int pass = 1; pass <= 2; ++pass) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// stream_buffer will be initialized only for pass 2
|
2019-08-28 02:10:11 +00:00
|
|
|
PipelinePopper pp_ostream(this, &stream_buffer);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
pushDiscardFilter(pp_ostream);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Adjust offsets to skip over comment before first object
|
|
|
|
first = offsets.at(0);
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& iter: offsets) {
|
|
|
|
iter -= first;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Take one pass at writing pairs of numbers so we can get their size information
|
2019-08-28 02:10:11 +00:00
|
|
|
{
|
|
|
|
PipelinePopper pp_discard(this);
|
|
|
|
pushDiscardFilter(pp_discard);
|
|
|
|
writeObjectStreamOffsets(offsets, first_obj);
|
2023-05-21 13:42:34 +00:00
|
|
|
first += m->pipeline->getCount();
|
2019-08-28 02:10:11 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Set up a stream to write the stream data into a buffer.
|
|
|
|
Pipeline* next = pushPipeline(new Pl_Buffer("object stream"));
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((m->compress_streams || (m->stream_decode_level == qpdf_dl_none)) &&
|
2023-05-21 13:42:34 +00:00
|
|
|
(!m->qdf_mode)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
compressed = true;
|
2023-05-21 17:35:09 +00:00
|
|
|
next =
|
|
|
|
pushPipeline(new Pl_Flate("compress object stream", next, Pl_Flate::a_deflate));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
activatePipelineStack(pp_ostream);
|
|
|
|
writeObjectStreamOffsets(offsets, first_obj);
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
int count = -1;
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& obj: m->object_stream_to_objects[old_id]) {
|
2022-04-30 17:23:18 +00:00
|
|
|
++count;
|
2023-05-21 13:42:34 +00:00
|
|
|
int new_obj = m->obj_renumber[obj];
|
2022-04-02 21:14:10 +00:00
|
|
|
if (first_obj == -1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
first_obj = new_obj;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->qdf_mode) {
|
2022-04-02 21:14:10 +00:00
|
|
|
writeString(
|
2023-05-21 17:35:09 +00:00
|
|
|
"%% Object stream: object " + std::to_string(new_obj) + ", index " +
|
|
|
|
std::to_string(count));
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->suppress_original_object_ids) {
|
2023-05-21 17:35:09 +00:00
|
|
|
writeString("; original object ID: " + std::to_string(obj.getObj()));
|
2023-05-27 17:19:52 +00:00
|
|
|
// For compatibility, only write the generation if non-zero. While object
|
|
|
|
// streams only allow objects with generation 0, if we are generating object
|
|
|
|
// streams, the old object could have a non-zero generation.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.getGen() != 0) {
|
2013-06-14 18:53:47 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter original obj non-zero gen");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(" " + std::to_string(obj.getGen()));
|
2013-06-14 18:53:47 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
writeString("\n");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 1) {
|
2023-05-21 13:42:34 +00:00
|
|
|
offsets.push_back(m->pipeline->getCount());
|
2023-05-27 17:19:52 +00:00
|
|
|
// To avoid double-counting objects being written in object streams for progress
|
|
|
|
// reporting, decrement in pass 1.
|
2018-06-22 19:22:29 +00:00
|
|
|
indicateProgress(true, false);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFObjectHandle obj_to_write = m->pdf.getObject(obj);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj_to_write.isStream()) {
|
2023-05-29 18:26:18 +00:00
|
|
|
// This condition occurred in a fuzz input. Ideally we should block it at parse
|
2023-05-27 17:19:52 +00:00
|
|
|
// time, but it's not clear to me how to construct a case for this.
|
2019-08-28 13:32:58 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter stream in ostream");
|
2023-05-21 17:35:09 +00:00
|
|
|
obj_to_write.warnIfPossible("stream found inside object stream; treating as null");
|
2019-08-28 13:32:58 +00:00
|
|
|
obj_to_write = QPDFObjectHandle::newNull();
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
writeObject(obj_to_write, count);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->xref[new_obj] = QPDFXRefEntry(new_id, count);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the object
|
|
|
|
openObject(new_id);
|
|
|
|
setDataKey(new_id);
|
|
|
|
writeString("<<");
|
|
|
|
writeStringQDF("\n ");
|
|
|
|
writeString(" /Type /ObjStm");
|
|
|
|
writeStringQDF("\n ");
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t length = stream_buffer->getSize();
|
2009-10-18 02:03:18 +00:00
|
|
|
adjustAESStreamLength(length);
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(" /Length " + std::to_string(length));
|
2008-04-29 12:55:25 +00:00
|
|
|
writeStringQDF("\n ");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compressed) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /Filter /FlateDecode");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(" /N " + std::to_string(offsets.size()));
|
2008-04-29 12:55:25 +00:00
|
|
|
writeStringQDF("\n ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(" /First " + std::to_string(first));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!object.isNull()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// If the original object has an /Extends key, preserve it.
|
|
|
|
QPDFObjectHandle dict = object.getDict();
|
|
|
|
QPDFObjectHandle extends = dict.getKey("/Extends");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (extends.isIndirect()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter copy Extends");
|
|
|
|
writeStringQDF("\n ");
|
|
|
|
writeString(" /Extends ");
|
|
|
|
unparseChild(extends, 1, f_in_ostream);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
writeStringQDF("\n");
|
|
|
|
writeStringNoQDF(" ");
|
|
|
|
writeString(">>\nstream\n");
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter encrypt object stream");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-08-28 02:10:11 +00:00
|
|
|
{
|
|
|
|
PipelinePopper pp_enc(this);
|
|
|
|
pushEncryptionFilter(pp_enc);
|
|
|
|
writeBuffer(stream_buffer);
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->newline_before_endstream) {
|
2018-05-12 17:09:13 +00:00
|
|
|
writeString("\n");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString("endstream");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->cur_data_key.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
closeObject(new_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::writeObject(QPDFObjectHandle object, int object_stream_index)
|
|
|
|
{
|
2013-06-14 18:53:47 +00:00
|
|
|
QPDFObjGen old_og = object.getObjGen();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((object_stream_index == -1) && (old_og.getGen() == 0) &&
|
2023-05-21 13:42:34 +00:00
|
|
|
(m->object_stream_to_objects.count(old_og.getObj()))) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeObjectStream(object);
|
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 19:22:29 +00:00
|
|
|
indicateProgress(false, false);
|
2023-05-21 13:42:34 +00:00
|
|
|
int new_id = m->obj_renumber[old_og];
|
|
|
|
if (m->qdf_mode) {
|
|
|
|
if (m->page_object_to_seq.count(old_og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("%% Page ");
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(std::to_string(m->page_object_to_seq[old_og]));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("\n");
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->contents_to_page_seq.count(old_og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("%% Contents for page ");
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(std::to_string(m->contents_to_page_seq[old_og]));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("\n");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (object_stream_index == -1) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->qdf_mode && (!m->suppress_original_object_ids)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
writeString("%% Original object ID: " + object.getObjGen().unparse(' ') + "\n");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
openObject(new_id);
|
|
|
|
setDataKey(new_id);
|
|
|
|
unparseObject(object, 0, 0);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->cur_data_key.clear();
|
2022-02-08 14:18:08 +00:00
|
|
|
closeObject(new_id);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
unparseObject(object, 0, f_in_ostream);
|
|
|
|
writeString("\n");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if ((!m->direct_stream_lengths) && object.isStream()) {
|
|
|
|
if (m->qdf_mode) {
|
|
|
|
if (m->added_newline) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("%QDF: ignore_newline\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
openObject(new_id + 1);
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(std::to_string(m->cur_stream_length));
|
2022-02-08 14:18:08 +00:00
|
|
|
closeObject(new_id + 1);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 23:02:56 +00:00
|
|
|
std::string
|
|
|
|
QPDFWriter::getOriginalID1()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFObjectHandle trailer = m->pdf.getTrailer();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (trailer.hasKey("/ID")) {
|
2018-02-04 23:02:56 +00:00
|
|
|
return trailer.getKey("/ID").getArrayItem(0).getStringValue();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2018-02-04 23:02:56 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::generateID()
|
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Generate the ID lazily so that we can handle the user's preference to use static or
|
|
|
|
// deterministic ID generation.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->id2.empty()) {
|
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
|
|
|
QPDFObjectHandle trailer = m->pdf.getTrailer();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->static_id) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// For test suite use only...
|
2022-04-02 21:14:10 +00:00
|
|
|
static unsigned char tmp[] = {
|
|
|
|
0x31,
|
|
|
|
0x41,
|
|
|
|
0x59,
|
|
|
|
0x26,
|
|
|
|
0x53,
|
|
|
|
0x58,
|
|
|
|
0x97,
|
|
|
|
0x93,
|
|
|
|
0x23,
|
|
|
|
0x84,
|
|
|
|
0x62,
|
|
|
|
0x64,
|
|
|
|
0x33,
|
|
|
|
0x83,
|
|
|
|
0x27,
|
|
|
|
0x95,
|
|
|
|
0x00};
|
2022-02-08 14:18:08 +00:00
|
|
|
result = reinterpret_cast<char*>(tmp);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-27 17:19:52 +00:00
|
|
|
// The PDF specification has guidelines for creating IDs, but it states clearly that the
|
|
|
|
// only thing that's really important is that it is very likely to be unique. We can't
|
|
|
|
// really follow the guidelines in the spec exactly because we haven't written the file yet.
|
|
|
|
// This scheme should be fine though. The deterministic ID case uses a digest of a
|
|
|
|
// sufficient portion of the file's contents such no two non-matching files would match in
|
|
|
|
// the subsets used for this computation. Note that we explicitly omit the filename from
|
|
|
|
// the digest calculation for deterministic ID so that the same file converted with qpdf, in
|
|
|
|
// that case, would have the same ID regardless of the output file's name.
|
2022-02-08 14:18:08 +00:00
|
|
|
|
|
|
|
std::string seed;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->deterministic_id) {
|
|
|
|
if (m->deterministic_id_data.empty()) {
|
2015-10-25 15:09:25 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter deterministic with no data");
|
2023-05-27 17:19:52 +00:00
|
|
|
throw std::logic_error("INTERNAL ERROR: QPDFWriter::generateID has no data for "
|
|
|
|
"deterministic ID. This may happen if deterministic ID and "
|
|
|
|
"file encryption are requested together.");
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
seed += m->deterministic_id_data;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-09-21 16:49:21 +00:00
|
|
|
seed += std::to_string(QUtil::get_current_time());
|
2023-05-21 13:42:34 +00:00
|
|
|
seed += m->filename;
|
2015-10-25 15:09:25 +00:00
|
|
|
seed += " ";
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
seed += " QPDF ";
|
2022-04-02 21:14:10 +00:00
|
|
|
if (trailer.hasKey("/Info")) {
|
2012-06-21 19:52:53 +00:00
|
|
|
QPDFObjectHandle info = trailer.getKey("/Info");
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& key: info.getKeys()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjectHandle obj = info.getKey(key);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.isString()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
seed += " ";
|
|
|
|
seed += obj.getStringValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MD5 m;
|
|
|
|
m.encodeString(seed.c_str());
|
|
|
|
MD5::Digest digest;
|
|
|
|
m.digest(digest);
|
2023-05-21 17:35:09 +00:00
|
|
|
result = std::string(reinterpret_cast<char*>(digest), sizeof(MD5::Digest));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// If /ID already exists, follow the spec: use the original first word and generate a new second
|
|
|
|
// word. Otherwise, we'll use the generated ID for both.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->id2 = result;
|
2018-02-04 23:02:56 +00:00
|
|
|
// Note: keep /ID from old file even if --static-id was given.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->id1 = getOriginalID1();
|
|
|
|
if (m->id1.empty()) {
|
|
|
|
m->id1 = m->id2;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::initializeSpecialStreams()
|
|
|
|
{
|
|
|
|
// Mark all page content streams in case we are filtering or
|
|
|
|
// normalizing.
|
2023-05-21 13:42:34 +00:00
|
|
|
std::vector<QPDFObjectHandle> pages = m->pdf.getAllPages();
|
2008-04-29 12:55:25 +00:00
|
|
|
int num = 0;
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& page: pages) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->page_object_to_seq[page.getObjGen()] = ++num;
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle contents = page.getKey("/Contents");
|
|
|
|
std::vector<QPDFObjGen> contents_objects;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (contents.isArray()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int n = contents.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2023-05-21 17:35:09 +00:00
|
|
|
contents_objects.push_back(contents.getArrayItem(i).getObjGen());
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (contents.isStream()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
contents_objects.push_back(contents.getObjGen());
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& c: contents_objects) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->contents_to_page_seq[c] = num;
|
|
|
|
m->normalized_streams.insert(c);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::preserveObjectStreams()
|
|
|
|
{
|
2021-05-09 11:51:14 +00:00
|
|
|
std::map<int, int> omap;
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDF::Writer::getObjectStreamData(m->pdf, omap);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (omap.empty()) {
|
2021-05-09 11:51:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-05-27 17:19:52 +00:00
|
|
|
// Our object_to_object_stream map has to map ObjGen -> ObjGen since we may be generating object
|
|
|
|
// streams out of old objects that have generation numbers greater than zero. However in an
|
|
|
|
// existing PDF, all object stream objects and all objects in them must have generation 0
|
|
|
|
// because the PDF spec does not provide any way to do otherwise. This code filters out objects
|
|
|
|
// that are not allowed to be in object streams. In addition to removing objects that were
|
|
|
|
// erroneously included in object streams in the source PDF, it also prevents unreferenced
|
|
|
|
// objects from being included.
|
2021-05-08 13:07:59 +00:00
|
|
|
std::set<QPDFObjGen> eligible;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->preserve_unreferenced_objects) {
|
2023-05-21 17:35:09 +00:00
|
|
|
std::vector<QPDFObjGen> eligible_v = QPDF::Writer::getCompressibleObjGens(m->pdf);
|
2021-05-08 13:07:59 +00:00
|
|
|
eligible = std::set<QPDFObjGen>(eligible_v.begin(), eligible_v.end());
|
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter preserve object streams", m->preserve_unreferenced_objects ? 0 : 1);
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto iter: omap) {
|
2021-05-08 13:07:59 +00:00
|
|
|
QPDFObjGen og(iter.first, 0);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (eligible.count(og) || m->preserve_unreferenced_objects) {
|
|
|
|
m->object_to_object_stream[og] = iter.second;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2021-05-08 13:07:59 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter exclude from object stream");
|
|
|
|
}
|
2013-06-14 18:53:47 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::generateObjectStreams()
|
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Basic strategy: make a list of objects that can go into an object stream. Then figure out
|
|
|
|
// how many object streams are needed so that we can distribute objects approximately evenly
|
|
|
|
// without having any object stream exceed 100 members. We don't have to worry about linearized
|
|
|
|
// files here -- if the file is linearized, we take care of excluding things that aren't allowed
|
|
|
|
// here later.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// This code doesn't do anything with /Extends.
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
std::vector<QPDFObjGen> eligible = QPDF::Writer::getCompressibleObjGens(m->pdf);
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t n_object_streams = (eligible.size() + 99U) / 100U;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (n_object_streams == 0) {
|
2019-06-15 14:18:32 +00:00
|
|
|
return;
|
2017-08-12 00:10:28 +00:00
|
|
|
}
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t n_per = eligible.size() / n_object_streams;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (n_per * n_object_streams < eligible.size()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
++n_per;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
unsigned int n = 0;
|
|
|
|
int cur_ostream = 0;
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& iter: eligible) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((n % n_per) == 0) {
|
|
|
|
if (n > 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter generate >1 ostream");
|
|
|
|
}
|
|
|
|
n = 0;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (n == 0) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Construct a new null object as the "original" object stream. The rest of the code
|
|
|
|
// knows that this means we're creating the object stream from scratch.
|
2023-05-21 17:35:09 +00:00
|
|
|
cur_ostream = m->pdf.makeIndirectObject(QPDFObjectHandle::newNull()).getObjectID();
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_to_object_stream[iter] = cur_ostream;
|
2022-02-08 14:18:08 +00:00
|
|
|
++n;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 10:50:43 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFWriter::getTrimmedTrailer()
|
2012-12-27 16:39:01 +00:00
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Remove keys from the trailer that necessarily have to be replaced when writing the file.
|
2012-12-27 16:39:01 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFObjectHandle trailer = m->pdf.getTrailer().unsafeShallowCopy();
|
2012-12-27 16:39:01 +00:00
|
|
|
|
2012-12-31 10:50:43 +00:00
|
|
|
// Remove encryption keys
|
2012-12-27 16:39:01 +00:00
|
|
|
trailer.removeKey("/ID");
|
|
|
|
trailer.removeKey("/Encrypt");
|
2012-12-31 10:50:43 +00:00
|
|
|
|
|
|
|
// Remove modification information
|
2012-12-27 16:39:01 +00:00
|
|
|
trailer.removeKey("/Prev");
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Remove all trailer keys that potentially come from a cross-reference stream
|
2012-12-27 16:39:01 +00:00
|
|
|
trailer.removeKey("/Index");
|
|
|
|
trailer.removeKey("/W");
|
|
|
|
trailer.removeKey("/Length");
|
|
|
|
trailer.removeKey("/Filter");
|
|
|
|
trailer.removeKey("/DecodeParms");
|
|
|
|
trailer.removeKey("/Type");
|
|
|
|
trailer.removeKey("/XRefStm");
|
|
|
|
|
2012-12-31 10:50:43 +00:00
|
|
|
return trailer;
|
|
|
|
}
|
|
|
|
|
2023-06-08 23:34:19 +00:00
|
|
|
// Make document extension level information direct as required by the spec.
|
2012-12-31 10:50:43 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::prepareFileForWrite()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pdf.fixDanglingReferences();
|
2023-06-08 23:34:19 +00:00
|
|
|
auto root = m->pdf.getRoot();
|
|
|
|
auto oh = root.getKey("/Extensions");
|
|
|
|
if (oh.isDictionary()) {
|
|
|
|
const bool extensions_indirect = oh.isIndirect();
|
|
|
|
if (extensions_indirect) {
|
|
|
|
QTC::TC("qpdf", "QPDFWriter make Extensions direct");
|
|
|
|
oh = root.replaceKeyAndGetNew("/Extensions", oh.shallowCopy());
|
|
|
|
}
|
|
|
|
if (oh.hasKey("/ADBE")) {
|
|
|
|
auto adbe = oh.getKey("/ADBE");
|
|
|
|
if (adbe.isIndirect()) {
|
|
|
|
QTC::TC("qpdf", "QPDFWriter make ADBE direct", extensions_indirect ? 0 : 1);
|
|
|
|
adbe.makeDirect();
|
|
|
|
oh.replaceKey("/ADBE", adbe);
|
2020-12-25 14:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-27 16:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2019-01-04 17:32:02 +00:00
|
|
|
QPDFWriter::doWriteSetup()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->did_write_setup) {
|
2019-01-04 17:32:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->did_write_setup = true;
|
2019-01-04 17:32:02 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Do preliminary setup
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->linearized) {
|
|
|
|
m->qdf_mode = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->pclm) {
|
|
|
|
m->stream_decode_level = qpdf_dl_none;
|
|
|
|
m->compress_streams = false;
|
|
|
|
m->encrypted = false;
|
2017-08-01 12:16:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->qdf_mode) {
|
|
|
|
if (!m->normalize_content_set) {
|
|
|
|
m->normalize_content = true;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->compress_streams_set) {
|
|
|
|
m->compress_streams = false;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->stream_decode_level_set) {
|
|
|
|
m->stream_decode_level = qpdf_dl_generalized;
|
2017-08-19 13:18:14 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Encryption has been explicitly set
|
2023-05-21 13:42:34 +00:00
|
|
|
m->preserve_encryption = false;
|
2023-05-21 17:35:09 +00:00
|
|
|
} else if (m->normalize_content || m->stream_decode_level || m->pclm || m->qdf_mode) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Encryption makes looking at contents pretty useless. If the user explicitly encrypted
|
|
|
|
// though, we still obey that.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->preserve_encryption = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->preserve_encryption) {
|
|
|
|
copyEncryptionParameters(m->pdf);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->forced_pdf_version.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int major = 0;
|
|
|
|
int minor = 0;
|
2023-05-21 13:42:34 +00:00
|
|
|
parseVersion(m->forced_pdf_version, major, minor);
|
|
|
|
disableIncompatibleEncryption(major, minor, m->forced_extension_level);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compareVersions(major, minor, 1, 5) < 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter forcing object stream disable");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_stream_mode = qpdf_o_disable;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2009-10-19 00:17:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->qdf_mode || m->normalize_content || m->stream_decode_level) {
|
2022-02-08 14:18:08 +00:00
|
|
|
initializeSpecialStreams();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->qdf_mode) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Generate indirect stream lengths for qdf mode since fix-qdf uses them for storing
|
|
|
|
// recomputed stream length data. Certain streams such as object streams, xref streams, and
|
2022-02-08 14:18:08 +00:00
|
|
|
// hint streams always get direct stream lengths.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->direct_stream_lengths = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
switch (m->object_stream_mode) {
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_o_disable:
|
2022-02-08 14:18:08 +00:00
|
|
|
// no action required
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_o_preserve:
|
2022-02-08 14:18:08 +00:00
|
|
|
preserveObjectStreams();
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case qpdf_o_generate:
|
2022-02-08 14:18:08 +00:00
|
|
|
generateObjectStreams();
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// no default so gcc will warn for missing case tag
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->linearized) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Page dictionaries are not allowed to be compressed objects.
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto& page: m->pdf.getAllPages()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen og = page.getObjGen();
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->object_to_object_stream.count(og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter uncompressing page dictionary");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_to_object_stream.erase(og);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->linearized || m->encrypted) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// The document catalog is not allowed to be compressed in linearized files either. It also
|
|
|
|
// appears that Adobe Reader 8.0.0 has a bug that prevents it from being able to handle
|
|
|
|
// encrypted files with compressed document catalogs, so we disable them in that case as
|
|
|
|
// well.
|
2023-03-17 10:41:12 +00:00
|
|
|
if (m->object_to_object_stream.count(m->root_og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter uncompressing root");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_to_object_stream.erase(m->root_og);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate reverse mapping from object stream to objects
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->object_to_object_stream) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjGen const& obj = iter.first;
|
|
|
|
int stream = iter.second;
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_stream_to_objects[stream].insert(obj);
|
|
|
|
m->max_ostream_index = std::max(
|
2023-05-21 17:35:09 +00:00
|
|
|
m->max_ostream_index, QIntC::to_int(m->object_stream_to_objects[stream].size()) - 1);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->object_stream_to_objects.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
setMinimumPDFVersion("1.5");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
setMinimumPDFVersion(m->pdf.getPDFVersion(), m->pdf.getExtensionLevel());
|
|
|
|
m->final_pdf_version = m->min_pdf_version;
|
|
|
|
m->final_extension_level = m->min_extension_level;
|
|
|
|
if (!m->forced_pdf_version.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter using forced PDF version");
|
2023-05-21 13:42:34 +00:00
|
|
|
m->final_pdf_version = m->forced_pdf_version;
|
|
|
|
m->final_extension_level = m->forced_extension_level;
|
2019-01-04 17:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::write()
|
|
|
|
{
|
|
|
|
doWriteSetup();
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Set up progress reporting. For linearized files, we write two passes. events_expected is an
|
|
|
|
// approximation, but it's good enough for progress reporting, which is mostly a guess anyway.
|
2023-05-21 17:35:09 +00:00
|
|
|
m->events_expected = QIntC::to_int(m->pdf.getObjectCount() * (m->linearized ? 2 : 1));
|
2018-06-22 19:22:29 +00:00
|
|
|
|
2012-12-27 16:39:01 +00:00
|
|
|
prepareFileForWrite();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->linearized) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeLinearized();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeStandard();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pipeline->finish();
|
|
|
|
if (m->close_file) {
|
|
|
|
fclose(m->file);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file = nullptr;
|
|
|
|
if (m->buffer_pipeline) {
|
|
|
|
m->output_buffer = m->buffer_pipeline->getBuffer();
|
|
|
|
m->buffer_pipeline = nullptr;
|
2011-08-10 20:34:29 +00:00
|
|
|
}
|
2018-06-22 19:22:29 +00:00
|
|
|
indicateProgress(false, true);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 14:14:59 +00:00
|
|
|
QPDFObjGen
|
|
|
|
QPDFWriter::getRenumberedObjGen(QPDFObjGen og)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return QPDFObjGen(m->obj_renumber[og], 0);
|
2019-10-01 14:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 14:53:50 +00:00
|
|
|
std::map<QPDFObjGen, QPDFXRefEntry>
|
|
|
|
QPDFWriter::getWrittenXRefTable()
|
|
|
|
{
|
|
|
|
std::map<QPDFObjGen, QPDFXRefEntry> result;
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->xref) {
|
2022-04-30 17:23:18 +00:00
|
|
|
if (iter.first != 0 && iter.second.getType() != 0) {
|
|
|
|
result[QPDFObjGen(iter.first, 0)] = iter.second;
|
2019-10-01 14:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::enqueuePart(std::vector<QPDFObjectHandle>& part)
|
|
|
|
{
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& oh: part) {
|
|
|
|
enqueueObject(oh);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::writeEncryptionDictionary()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->encryption_dict_objid = openObject(m->encryption_dict_objid);
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString("<<");
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->encryption_dictionary) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" ");
|
2022-04-30 17:23:18 +00:00
|
|
|
writeString(iter.first);
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" ");
|
2022-04-30 17:23:18 +00:00
|
|
|
writeString(iter.second);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
writeString(" >>");
|
2023-05-21 13:42:34 +00:00
|
|
|
closeObject(m->encryption_dict_objid);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 17:32:02 +00:00
|
|
|
std::string
|
|
|
|
QPDFWriter::getFinalVersion()
|
|
|
|
{
|
|
|
|
doWriteSetup();
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->final_pdf_version;
|
2019-01-04 17:32:02 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::writeHeader()
|
|
|
|
{
|
|
|
|
writeString("%PDF-");
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(m->final_pdf_version);
|
|
|
|
if (m->pclm) {
|
2017-08-22 00:17:47 +00:00
|
|
|
// PCLm version
|
|
|
|
writeString("\n%PCLm 1.0\n");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-27 17:19:52 +00:00
|
|
|
// This string of binary characters would not be valid UTF-8, so it really should be treated
|
|
|
|
// as binary.
|
2017-08-22 00:17:47 +00:00
|
|
|
writeString("\n%\xbf\xf7\xa2\xfe\n");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
writeStringQDF("%QDF-1.0\n\n");
|
2012-09-06 19:30:13 +00:00
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Note: do not write extra header text here. Linearized PDFs must include the entire
|
|
|
|
// linearization parameter dictionary within the first 1024 characters of the PDF file, so for
|
|
|
|
// linearized files, we have to write extra header text after the linearization parameter
|
|
|
|
// dictionary.
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFWriter::writeHintStream(int hint_id)
|
|
|
|
{
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer> hint_buffer;
|
2008-04-29 12:55:25 +00:00
|
|
|
int S = 0;
|
|
|
|
int O = 0;
|
2013-07-10 15:07:40 +00:00
|
|
|
QPDF::Writer::generateHintStream(
|
2023-05-21 13:42:34 +00:00
|
|
|
m->pdf, m->xref, m->lengths, m->obj_renumber_no_gen, hint_buffer, S, O);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
openObject(hint_id);
|
|
|
|
setDataKey(hint_id);
|
|
|
|
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t hlen = hint_buffer->getSize();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
writeString("<< /Filter /FlateDecode /S ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(S));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (O) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /O ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(O));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
writeString(" /Length ");
|
2009-10-18 02:03:18 +00:00
|
|
|
adjustAESStreamLength(hlen);
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(hlen));
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString(" >>\nstream\n");
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter encrypted hint stream");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-08-28 02:10:11 +00:00
|
|
|
unsigned char last_char = '\0';
|
|
|
|
{
|
|
|
|
PipelinePopper pp_enc(this);
|
|
|
|
pushEncryptionFilter(pp_enc);
|
|
|
|
writeBuffer(hint_buffer);
|
2023-05-21 13:42:34 +00:00
|
|
|
last_char = m->pipeline->getLastChar();
|
2019-08-28 02:10:11 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (last_char != '\n') {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("\n");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
writeString("endstream");
|
|
|
|
closeObject(hint_id);
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFWriter::writeXRefTable(trailer_e which, int first, int last, int size)
|
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// There are too many extra arguments to replace overloaded function with defaults in the header
|
|
|
|
// file...too much risk of leaving something off.
|
2015-11-10 17:22:40 +00:00
|
|
|
return writeXRefTable(which, first, last, size, 0, false, 0, 0, 0, 0);
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::writeXRefTable(
|
|
|
|
trailer_e which,
|
|
|
|
int first,
|
|
|
|
int last,
|
|
|
|
int size,
|
|
|
|
qpdf_offset_t prev,
|
|
|
|
bool suppress_offsets,
|
|
|
|
int hint_id,
|
|
|
|
qpdf_offset_t hint_offset,
|
|
|
|
qpdf_offset_t hint_length,
|
|
|
|
int linearization_pass)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
writeString("xref\n");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(first));
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString(" ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(last - first + 1));
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t space_before_zero = m->pipeline->getCount();
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString("\n");
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = first; i <= last; ++i) {
|
|
|
|
if (i == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("0000000000 65535 f \n");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t offset = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!suppress_offsets) {
|
2023-05-21 13:42:34 +00:00
|
|
|
offset = m->xref[i].getOffset();
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((hint_id != 0) && (i != hint_id) && (offset >= hint_offset)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
offset += hint_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeString(QUtil::int_to_string(offset, 10));
|
|
|
|
writeString(" 00000 n \n");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2015-10-25 15:09:25 +00:00
|
|
|
writeTrailer(which, size, false, prev, linearization_pass);
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString("\n");
|
|
|
|
return space_before_zero;
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::writeXRefStream(
|
2023-05-21 17:35:09 +00:00
|
|
|
int objid, int max_id, qpdf_offset_t max_offset, trailer_e which, int first, int last, int size)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// There are too many extra arguments to replace overloaded function with defaults in the header
|
|
|
|
// file...too much risk of leaving something off.
|
2022-04-02 21:14:10 +00:00
|
|
|
return writeXRefStream(
|
2023-05-21 17:35:09 +00:00
|
|
|
objid, max_id, max_offset, which, first, last, size, 0, 0, 0, 0, false, 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFWriter::writeXRefStream(
|
|
|
|
int xref_id,
|
|
|
|
int max_id,
|
|
|
|
qpdf_offset_t max_offset,
|
|
|
|
trailer_e which,
|
|
|
|
int first,
|
|
|
|
int last,
|
|
|
|
int size,
|
|
|
|
qpdf_offset_t prev,
|
|
|
|
int hint_id,
|
|
|
|
qpdf_offset_t hint_offset,
|
|
|
|
qpdf_offset_t hint_length,
|
|
|
|
bool skip_compression,
|
|
|
|
int linearization_pass)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t xref_offset = m->pipeline->getCount();
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t space_before_zero = xref_offset - 1;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// field 1 contains offsets and object stream identifiers
|
2023-05-21 17:35:09 +00:00
|
|
|
unsigned int f1_size = std::max(bytesNeeded(max_offset + hint_length), bytesNeeded(max_id));
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// field 2 contains object stream indices
|
2023-05-21 13:42:34 +00:00
|
|
|
unsigned int f2_size = bytesNeeded(m->max_ostream_index);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
unsigned int esize = 1 + f1_size + f2_size;
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Must store in xref table in advance of writing the actual data rather than waiting for
|
|
|
|
// openObject to do it.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->xref[xref_id] = QPDFXRefEntry(m->pipeline->getCount());
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
Pipeline* p = pushPipeline(new Pl_Buffer("xref stream"));
|
|
|
|
bool compressed = false;
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((m->compress_streams || (m->stream_decode_level == qpdf_dl_none)) && (!m->qdf_mode)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
compressed = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!skip_compression) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Write the stream dictionary for compression but don't actually compress. This helps
|
|
|
|
// us with computation of padding for pass 1 of linearization.
|
2023-05-21 17:35:09 +00:00
|
|
|
p = pushPipeline(new Pl_Flate("compress xref", p, Pl_Flate::a_deflate));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
p = pushPipeline(new Pl_PNGFilter("pngify xref", p, Pl_PNGFilter::a_encode, esize));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer> xref_data;
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-08-28 02:10:11 +00:00
|
|
|
PipelinePopper pp_xref(this, &xref_data);
|
|
|
|
activatePipelineStack(pp_xref);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = first; i <= last; ++i) {
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFXRefEntry& e = m->xref[i];
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (e.getType()) {
|
|
|
|
case 0:
|
2019-08-28 02:10:11 +00:00
|
|
|
writeBinary(0, 1);
|
|
|
|
writeBinary(0, f1_size);
|
|
|
|
writeBinary(0, f2_size);
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case 1:
|
2019-08-28 02:10:11 +00:00
|
|
|
{
|
|
|
|
qpdf_offset_t offset = e.getOffset();
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((hint_id != 0) && (i != hint_id) && (offset >= hint_offset)) {
|
2019-08-28 02:10:11 +00:00
|
|
|
offset += hint_length;
|
|
|
|
}
|
|
|
|
writeBinary(1, 1);
|
|
|
|
writeBinary(QIntC::to_ulonglong(offset), f1_size);
|
|
|
|
writeBinary(0, f2_size);
|
|
|
|
}
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case 2:
|
2019-08-28 02:10:11 +00:00
|
|
|
writeBinary(2, 1);
|
2023-05-21 17:35:09 +00:00
|
|
|
writeBinary(QIntC::to_ulonglong(e.getObjStreamNumber()), f1_size);
|
|
|
|
writeBinary(QIntC::to_ulonglong(e.getObjStreamIndex()), f2_size);
|
2019-08-28 02:10:11 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2019-08-28 02:10:11 +00:00
|
|
|
throw std::logic_error("invalid type writing xref stream");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
openObject(xref_id);
|
|
|
|
writeString("<<");
|
|
|
|
writeStringQDF("\n ");
|
|
|
|
writeString(" /Type /XRef");
|
|
|
|
writeStringQDF("\n ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(" /Length " + std::to_string(xref_data->getSize()));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compressed) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeStringQDF("\n ");
|
|
|
|
writeString(" /Filter /FlateDecode");
|
|
|
|
writeStringQDF("\n ");
|
2023-05-21 17:35:09 +00:00
|
|
|
writeString(" /DecodeParms << /Columns " + std::to_string(esize) + " /Predictor 12 >>");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
writeStringQDF("\n ");
|
2023-05-21 17:35:09 +00:00
|
|
|
writeString(" /W [ 1 " + std::to_string(f1_size) + " " + std::to_string(f2_size) + " ]");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!((first == 0) && (last == size - 1))) {
|
|
|
|
writeString(
|
2023-05-21 17:35:09 +00:00
|
|
|
" /Index [ " + std::to_string(first) + " " + std::to_string(last - first + 1) + " ]");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2015-10-25 15:09:25 +00:00
|
|
|
writeTrailer(which, size, true, prev, linearization_pass);
|
2008-04-29 12:55:25 +00:00
|
|
|
writeString("\nstream\n");
|
|
|
|
writeBuffer(xref_data);
|
|
|
|
writeString("\nendstream");
|
|
|
|
closeObject(xref_id);
|
|
|
|
return space_before_zero;
|
|
|
|
}
|
|
|
|
|
2023-01-08 16:08:27 +00:00
|
|
|
size_t
|
2019-06-21 03:35:23 +00:00
|
|
|
QPDFWriter::calculateXrefStreamPadding(qpdf_offset_t xref_bytes)
|
2010-04-18 23:01:31 +00:00
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// This routine is called right after a linearization first pass xref stream has been written
|
|
|
|
// without compression. Calculate the amount of padding that would be required in the worst
|
|
|
|
// case, assuming the number of uncompressed bytes remains the same. The worst case for zlib is
|
|
|
|
// that the output is larger than the input by 6 bytes plus 5 bytes per 16K, and then we'll add
|
|
|
|
// 10 extra bytes for number length increases.
|
2010-04-18 23:01:31 +00:00
|
|
|
|
2023-01-08 16:08:27 +00:00
|
|
|
return QIntC::to_size(16 + (5 * ((xref_bytes + 16383) / 16384)));
|
2010-04-18 23:01:31 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 18:53:47 +00:00
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFWriter::discardGeneration(std::map<QPDFObjGen, int> const& in, std::map<int, int>& out)
|
2013-06-14 18:53:47 +00:00
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// There are deep assumptions in the linearization code in QPDF that there is only one object
|
|
|
|
// with each object number; i.e., you can't have two objects with the same object number and
|
|
|
|
// different generations. This is a pretty safe assumption because Adobe Reader and Acrobat
|
|
|
|
// can't actually handle this case. There is not much if any code in QPDF outside linearization
|
|
|
|
// that assumes this, but the linearization code as currently implemented would do weird things
|
|
|
|
// if we found such a case. In order to avoid breaking ABI changes in QPDF, we will first
|
|
|
|
// assert that this condition holds. Then we can create new maps for QPDF that throw away
|
|
|
|
// generation numbers.
|
2013-06-14 18:53:47 +00:00
|
|
|
|
|
|
|
out.clear();
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& iter: in) {
|
|
|
|
if (out.count(iter.first.getObj())) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::runtime_error("QPDF cannot currently linearize files that contain"
|
|
|
|
" multiple objects with the same object ID and different"
|
|
|
|
" generations. If you see this error message, please file"
|
|
|
|
" a bug report and attach the file if possible. As a"
|
|
|
|
" workaround, first convert the file with qpdf without"
|
|
|
|
" linearizing, and then linearize the result of that"
|
|
|
|
" conversion.");
|
2013-06-14 18:53:47 +00:00
|
|
|
}
|
2022-04-30 17:23:18 +00:00
|
|
|
out[iter.first.getObj()] = iter.second;
|
2013-06-14 18:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::writeLinearized()
|
|
|
|
{
|
|
|
|
// Optimize file and enqueue objects in order
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
discardGeneration(m->object_to_object_stream, m->object_to_object_stream_no_gen);
|
2013-06-14 18:53:47 +00:00
|
|
|
|
2020-12-25 14:51:20 +00:00
|
|
|
auto skip_stream_parameters = [this](QPDFObjectHandle& stream) {
|
|
|
|
bool compress_stream;
|
|
|
|
bool is_metadata;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (willFilterStream(stream, compress_stream, is_metadata, nullptr)) {
|
2020-12-25 14:51:20 +00:00
|
|
|
return 2;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2020-12-25 14:51:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
m->pdf.optimize(m->object_to_object_stream_no_gen, true, skip_stream_parameters);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::vector<QPDFObjectHandle> part4;
|
|
|
|
std::vector<QPDFObjectHandle> part6;
|
|
|
|
std::vector<QPDFObjectHandle> part7;
|
|
|
|
std::vector<QPDFObjectHandle> part8;
|
|
|
|
std::vector<QPDFObjectHandle> part9;
|
2013-07-10 15:07:40 +00:00
|
|
|
QPDF::Writer::getLinearizedParts(
|
2023-05-21 17:35:09 +00:00
|
|
|
m->pdf, m->object_to_object_stream_no_gen, part4, part6, part7, part8, part9);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Object number sequence:
|
|
|
|
//
|
|
|
|
// second half
|
|
|
|
// second half uncompressed objects
|
|
|
|
// second half xref stream, if any
|
|
|
|
// second half compressed objects
|
|
|
|
// first half
|
|
|
|
// linearization dictionary
|
|
|
|
// first half xref stream, if any
|
|
|
|
// part 4 uncompresesd objects
|
|
|
|
// encryption dictionary, if any
|
|
|
|
// hint stream
|
|
|
|
// part 6 uncompressed objects
|
|
|
|
// first half compressed objects
|
|
|
|
//
|
|
|
|
|
|
|
|
// Second half objects
|
2023-05-21 17:35:09 +00:00
|
|
|
int second_half_uncompressed = QIntC::to_int(part7.size() + part8.size() + part9.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
int second_half_first_obj = 1;
|
|
|
|
int after_second_half = 1 + second_half_uncompressed;
|
2023-05-21 13:42:34 +00:00
|
|
|
m->next_objid = after_second_half;
|
2008-04-29 12:55:25 +00:00
|
|
|
int second_half_xref = 0;
|
2023-05-21 13:42:34 +00:00
|
|
|
bool need_xref_stream = (!m->object_to_object_stream.empty());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (need_xref_stream) {
|
2023-05-21 13:42:34 +00:00
|
|
|
second_half_xref = m->next_objid++;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
// Assign numbers to all compressed objects in the second half.
|
|
|
|
std::vector<QPDFObjectHandle>* vecs2[] = {&part7, &part8, &part9};
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& oh: *vecs2[i]) {
|
|
|
|
assignCompressedObjectNumbers(oh.getObjGen());
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
int second_half_end = m->next_objid - 1;
|
|
|
|
int second_trailer_size = m->next_objid;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// First half objects
|
2023-05-21 13:42:34 +00:00
|
|
|
int first_half_start = m->next_objid;
|
|
|
|
int lindict_id = m->next_objid++;
|
2008-04-29 12:55:25 +00:00
|
|
|
int first_half_xref = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (need_xref_stream) {
|
2023-05-21 13:42:34 +00:00
|
|
|
first_half_xref = m->next_objid++;
|
|
|
|
}
|
|
|
|
int part4_first_obj = m->next_objid;
|
|
|
|
m->next_objid += QIntC::to_int(part4.size());
|
|
|
|
int after_part4 = m->next_objid;
|
|
|
|
if (m->encrypted) {
|
|
|
|
m->encryption_dict_objid = m->next_objid++;
|
|
|
|
}
|
|
|
|
int hint_id = m->next_objid++;
|
|
|
|
int part6_first_obj = m->next_objid;
|
|
|
|
m->next_objid += QIntC::to_int(part6.size());
|
|
|
|
int after_part6 = m->next_objid;
|
2008-04-29 12:55:25 +00:00
|
|
|
// Assign numbers to all compressed objects in the first half
|
|
|
|
std::vector<QPDFObjectHandle>* vecs1[] = {&part4, &part6};
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& oh: *vecs1[i]) {
|
|
|
|
assignCompressedObjectNumbers(oh.getObjGen());
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
int first_half_end = m->next_objid - 1;
|
|
|
|
int first_trailer_size = m->next_objid;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
int part4_end_marker = part4.back().getObjectID();
|
|
|
|
int part6_end_marker = part6.back().getObjectID();
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t space_before_zero = 0;
|
|
|
|
qpdf_offset_t file_size = 0;
|
|
|
|
qpdf_offset_t part6_end_offset = 0;
|
|
|
|
qpdf_offset_t first_half_max_obj_offset = 0;
|
|
|
|
qpdf_offset_t second_xref_offset = 0;
|
|
|
|
qpdf_offset_t first_xref_end = 0;
|
|
|
|
qpdf_offset_t second_xref_end = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->next_objid = part4_first_obj;
|
2008-04-29 12:55:25 +00:00
|
|
|
enqueuePart(part4);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->next_objid != after_part4) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// This can happen with very botched files as in the fuzzer test. There are likely some
|
|
|
|
// faulty assumptions in calculateLinearizationData
|
|
|
|
throw std::runtime_error("error encountered after writing part 4 of linearized data");
|
2019-06-21 21:37:33 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->next_objid = part6_first_obj;
|
2008-04-29 12:55:25 +00:00
|
|
|
enqueuePart(part6);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->next_objid != after_part6) {
|
2023-05-27 17:19:52 +00:00
|
|
|
throw std::runtime_error("error encountered after writing part 6 of linearized data");
|
2019-06-21 21:37:33 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->next_objid = second_half_first_obj;
|
2008-04-29 12:55:25 +00:00
|
|
|
enqueuePart(part7);
|
|
|
|
enqueuePart(part8);
|
|
|
|
enqueuePart(part9);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->next_objid != after_second_half) {
|
2023-05-27 17:19:52 +00:00
|
|
|
throw std::runtime_error("error encountered after writing part 9 of linearized data");
|
2019-06-21 21:37:33 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t hint_length = 0;
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer> hint_buffer;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Write file in two passes. Part numbers refer to PDF spec 1.4.
|
|
|
|
|
2022-07-26 11:37:50 +00:00
|
|
|
FILE* lin_pass1_file = nullptr;
|
2022-04-09 18:35:56 +00:00
|
|
|
auto pp_pass1 = std::make_shared<PipelinePopper>(this);
|
|
|
|
auto pp_md5 = std::make_shared<PipelinePopper>(this);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int pass = 1; pass <= 2; ++pass) {
|
|
|
|
if (pass == 1) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->lin_pass1_filename.empty()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
lin_pass1_file = QUtil::safe_fopen(m->lin_pass1_filename.c_str(), "wb");
|
|
|
|
pushPipeline(new Pl_StdioFile("linearization pass1", lin_pass1_file));
|
2019-08-28 02:10:11 +00:00
|
|
|
activatePipelineStack(*pp_pass1);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-08-28 02:10:11 +00:00
|
|
|
pushDiscardFilter(*pp_pass1);
|
2018-01-31 21:45:52 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->deterministic_id) {
|
2019-08-28 02:10:11 +00:00
|
|
|
pushMD5Pipeline(*pp_md5);
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Part 1: header
|
|
|
|
|
|
|
|
writeHeader();
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Part 2: linearization parameter dictionary. Save enough space to write real dictionary.
|
|
|
|
// 200 characters is enough space if all numerical values in the parameter dictionary that
|
|
|
|
// contain offsets are 20 digits long plus a few extra characters for safety. The entire
|
|
|
|
// linearization parameter dictionary must appear within the first 1024 characters of the
|
|
|
|
// file.
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t pos = m->pipeline->getCount();
|
2022-02-08 14:18:08 +00:00
|
|
|
openObject(lindict_id);
|
|
|
|
writeString("<<");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 2) {
|
2023-05-21 13:42:34 +00:00
|
|
|
std::vector<QPDFObjectHandle> const& pages = m->pdf.getAllPages();
|
|
|
|
int first_page_object = m->obj_renumber[pages.at(0).getObjGen()];
|
2022-02-08 14:18:08 +00:00
|
|
|
int npages = QIntC::to_int(pages.size());
|
|
|
|
|
|
|
|
writeString(" /Linearized 1 /L ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(file_size + hint_length));
|
2023-05-27 17:19:52 +00:00
|
|
|
// Implementation note 121 states that a space is mandatory after this open bracket.
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /H [ ");
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(std::to_string(m->xref[hint_id].getOffset()));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(hint_length));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" ] /O ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(first_page_object));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /E ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(part6_end_offset + hint_length));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /N ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(npages));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString(" /T ");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(space_before_zero + hint_length));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
writeString(" >>");
|
|
|
|
closeObject(lindict_id);
|
|
|
|
static int const pad = 200;
|
2023-05-21 13:42:34 +00:00
|
|
|
writePad(QIntC::to_size(pos - m->pipeline->getCount() + pad));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("\n");
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// If the user supplied any additional header text, write it here after the linearization
|
|
|
|
// parameter dictionary.
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(m->extra_header_text);
|
2012-09-06 19:30:13 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Part 3: first page cross reference table and trailer.
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t first_xref_offset = m->pipeline->getCount();
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t hint_offset = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 2) {
|
2023-05-21 13:42:34 +00:00
|
|
|
hint_offset = m->xref[hint_id].getOffset();
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (need_xref_stream) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Must pad here too.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 1) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Set first_half_max_obj_offset to a value large enough to force four bytes to be
|
|
|
|
// reserved for each file offset. This would provide adequate space for the xref
|
|
|
|
// stream as long as the last object in page 1 starts with in the first 4 GB of the
|
|
|
|
// file, which is extremely likely. In the second pass, we will know the actual
|
|
|
|
// value for this, but it's okay if it's smaller.
|
2022-02-08 14:18:08 +00:00
|
|
|
first_half_max_obj_offset = 1 << 25;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
pos = m->pipeline->getCount();
|
2022-04-02 21:14:10 +00:00
|
|
|
writeXRefStream(
|
|
|
|
first_half_xref,
|
|
|
|
first_half_end,
|
|
|
|
first_half_max_obj_offset,
|
|
|
|
t_lin_first,
|
|
|
|
first_half_start,
|
|
|
|
first_half_end,
|
|
|
|
first_trailer_size,
|
|
|
|
hint_length + second_xref_offset,
|
|
|
|
hint_id,
|
|
|
|
hint_offset,
|
|
|
|
hint_length,
|
|
|
|
(pass == 1),
|
|
|
|
pass);
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t endpos = m->pipeline->getCount();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 1) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Pad so we have enough room for the real xref stream.
|
2022-02-08 14:18:08 +00:00
|
|
|
writePad(calculateXrefStreamPadding(endpos - pos));
|
2023-05-21 13:42:34 +00:00
|
|
|
first_xref_end = m->pipeline->getCount();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Pad so that the next object starts at the same place as in pass 1.
|
2023-01-08 16:08:27 +00:00
|
|
|
writePad(QIntC::to_size(first_xref_end - endpos));
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->pipeline->getCount() != first_xref_end) {
|
2013-10-05 20:37:27 +00:00
|
|
|
throw std::logic_error(
|
2019-08-29 02:48:55 +00:00
|
|
|
"insufficient padding for first pass xref stream; "
|
|
|
|
"first_xref_end=" +
|
2023-05-21 17:35:09 +00:00
|
|
|
std::to_string(first_xref_end) + "; endpos=" + std::to_string(endpos));
|
2013-10-05 20:37:27 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
writeString("\n");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
writeXRefTable(
|
|
|
|
t_lin_first,
|
|
|
|
first_half_start,
|
|
|
|
first_half_end,
|
|
|
|
first_trailer_size,
|
|
|
|
hint_length + second_xref_offset,
|
|
|
|
(pass == 1),
|
|
|
|
hint_id,
|
|
|
|
hint_offset,
|
|
|
|
hint_length,
|
|
|
|
pass);
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("startxref\n0\n%%EOF\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parts 4 through 9
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& cur_object: m->object_queue) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cur_object.getObjectID() == part6_end_marker) {
|
2023-05-21 13:42:34 +00:00
|
|
|
first_half_max_obj_offset = m->pipeline->getCount();
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
writeObject(cur_object);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cur_object.getObjectID() == part4_end_marker) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeEncryptionDictionary();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 1) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->xref[hint_id] = QPDFXRefEntry(m->pipeline->getCount());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Part 5: hint stream
|
|
|
|
writeBuffer(hint_buffer);
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cur_object.getObjectID() == part6_end_marker) {
|
2023-05-21 13:42:34 +00:00
|
|
|
part6_end_offset = m->pipeline->getCount();
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Part 10: overflow hint stream -- not used
|
|
|
|
|
|
|
|
// Part 11: main cross reference table and trailer
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
second_xref_offset = m->pipeline->getCount();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (need_xref_stream) {
|
2023-05-21 13:42:34 +00:00
|
|
|
pos = m->pipeline->getCount();
|
2022-04-02 21:14:10 +00:00
|
|
|
space_before_zero = writeXRefStream(
|
|
|
|
second_half_xref,
|
|
|
|
second_half_end,
|
|
|
|
second_xref_offset,
|
|
|
|
t_lin_second,
|
|
|
|
0,
|
|
|
|
second_half_end,
|
|
|
|
second_trailer_size,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(pass == 1),
|
|
|
|
pass);
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t endpos = m->pipeline->getCount();
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 1) {
|
2023-05-27 17:19:52 +00:00
|
|
|
// Pad so we have enough room for the real xref stream. See comments for previous
|
|
|
|
// xref stream on how we calculate the padding.
|
2022-02-08 14:18:08 +00:00
|
|
|
writePad(calculateXrefStreamPadding(endpos - pos));
|
|
|
|
writeString("\n");
|
2023-05-21 13:42:34 +00:00
|
|
|
second_xref_end = m->pipeline->getCount();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Make the file size the same.
|
2023-05-21 17:35:09 +00:00
|
|
|
writePad(
|
|
|
|
QIntC::to_size(second_xref_end + hint_length - 1 - m->pipeline->getCount()));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("\n");
|
2010-04-18 23:01:31 +00:00
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// If this assertion fails, maybe we didn't have enough padding above.
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->pipeline->getCount() != second_xref_end + hint_length) {
|
2023-05-27 17:19:52 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"count mismatch after xref stream; possible insufficient padding?");
|
2013-10-05 20:37:27 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
space_before_zero = writeXRefTable(
|
2023-05-21 17:35:09 +00:00
|
|
|
t_lin_second, 0, second_half_end, second_trailer_size, 0, false, 0, 0, 0, pass);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
writeString("startxref\n");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(first_xref_offset));
|
2022-02-08 14:18:08 +00:00
|
|
|
writeString("\n%%EOF\n");
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
discardGeneration(m->obj_renumber, m->obj_renumber_no_gen);
|
2013-06-14 18:53:47 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pass == 1) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->deterministic_id) {
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter linearized deterministic ID", need_xref_stream ? 0 : 1);
|
2015-10-25 15:09:25 +00:00
|
|
|
computeDeterministicIDData();
|
2022-07-26 11:37:50 +00:00
|
|
|
pp_md5 = nullptr;
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_assert_debug(m->md5_pipeline == nullptr);
|
2015-10-25 15:09:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Close first pass pipeline
|
2023-05-21 13:42:34 +00:00
|
|
|
file_size = m->pipeline->getCount();
|
2022-07-26 11:37:50 +00:00
|
|
|
pp_pass1 = nullptr;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Save hint offset since it will be set to zero by calling openObject.
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t hint_offset1 = m->xref[hint_id].getOffset();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Write hint stream to a buffer
|
2019-08-28 02:10:11 +00:00
|
|
|
{
|
|
|
|
pushPipeline(new Pl_Buffer("hint buffer"));
|
|
|
|
PipelinePopper pp_hint(this, &hint_buffer);
|
|
|
|
activatePipelineStack(pp_hint);
|
|
|
|
writeHintStream(hint_id);
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
hint_length = QIntC::to_offset(hint_buffer->getSize());
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Restore hint offset
|
2023-05-21 13:42:34 +00:00
|
|
|
m->xref[hint_id] = QPDFXRefEntry(hint_offset1);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lin_pass1_file) {
|
2018-01-31 21:45:52 +00:00
|
|
|
// Write some debugging information
|
2022-04-02 21:14:10 +00:00
|
|
|
fprintf(
|
2023-05-21 17:35:09 +00:00
|
|
|
lin_pass1_file, "%% hint_offset=%s\n", std::to_string(hint_offset1).c_str());
|
|
|
|
fprintf(lin_pass1_file, "%% hint_length=%s\n", std::to_string(hint_length).c_str());
|
2022-04-02 21:14:10 +00:00
|
|
|
fprintf(
|
|
|
|
lin_pass1_file,
|
|
|
|
"%% second_xref_offset=%s\n",
|
2022-09-21 16:49:21 +00:00
|
|
|
std::to_string(second_xref_offset).c_str());
|
2022-04-02 21:14:10 +00:00
|
|
|
fprintf(
|
|
|
|
lin_pass1_file,
|
|
|
|
"%% second_xref_end=%s\n",
|
2022-09-21 16:49:21 +00:00
|
|
|
std::to_string(second_xref_end).c_str());
|
2018-01-31 21:45:52 +00:00
|
|
|
fclose(lin_pass1_file);
|
2022-07-26 11:37:50 +00:00
|
|
|
lin_pass1_file = nullptr;
|
2018-01-31 21:45:52 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-22 00:56:14 +00:00
|
|
|
QPDFWriter::enqueueObjectsStandard()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->preserve_unreferenced_objects) {
|
2017-07-28 23:18:57 +00:00
|
|
|
QTC::TC("qpdf", "QPDFWriter preserve unreferenced standard");
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& oh: m->pdf.getAllObjects()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
enqueueObject(oh);
|
2017-07-28 23:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Put root first on queue.
|
2012-12-31 10:50:43 +00:00
|
|
|
QPDFObjectHandle trailer = getTrimmedTrailer();
|
2008-04-29 12:55:25 +00:00
|
|
|
enqueueObject(trailer.getKey("/Root"));
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Next place any other objects referenced from the trailer dictionary into the queue, handling
|
|
|
|
// direct objects recursively. Root is already there, so enqueuing it a second time is a no-op.
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& key: trailer.getKeys()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
enqueueObject(trailer.getKey(key));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 12:16:24 +00:00
|
|
|
|
|
|
|
void
|
2017-08-22 00:56:14 +00:00
|
|
|
QPDFWriter::enqueueObjectsPCLm()
|
2017-08-01 12:16:24 +00:00
|
|
|
{
|
2023-05-27 17:19:52 +00:00
|
|
|
// Image transform stream content for page strip images. Each of this new stream has to come
|
|
|
|
// after every page image strip written in the pclm file.
|
2017-08-01 12:16:24 +00:00
|
|
|
std::string image_transform_content = "q /image Do Q\n";
|
|
|
|
|
|
|
|
// enqueue all pages first
|
2023-05-21 13:42:34 +00:00
|
|
|
std::vector<QPDFObjectHandle> all = m->pdf.getAllPages();
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& page: all) {
|
2017-08-01 12:16:24 +00:00
|
|
|
// enqueue page
|
2022-04-30 17:23:18 +00:00
|
|
|
enqueueObject(page);
|
2017-08-01 12:16:24 +00:00
|
|
|
|
|
|
|
// enqueue page contents stream
|
2022-04-30 17:23:18 +00:00
|
|
|
enqueueObject(page.getKey("/Contents"));
|
2017-08-01 12:16:24 +00:00
|
|
|
|
|
|
|
// enqueue all the strips for each page
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjectHandle strips = page.getKey("/Resources").getKey("/XObject");
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& image: strips.getKeys()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
enqueueObject(strips.getKey(image));
|
2023-05-21 17:35:09 +00:00
|
|
|
enqueueObject(QPDFObjectHandle::newStream(&m->pdf, image_transform_content));
|
2017-08-01 12:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put root in queue.
|
|
|
|
QPDFObjectHandle trailer = getTrimmedTrailer();
|
|
|
|
enqueueObject(trailer.getKey("/Root"));
|
2017-08-22 00:56:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 19:22:29 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::indicateProgress(bool decrement, bool finished)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (decrement) {
|
2023-05-21 13:42:34 +00:00
|
|
|
--m->events_seen;
|
2018-06-22 19:22:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
++m->events_seen;
|
2018-06-22 19:22:29 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->progress_reporter.get()) {
|
2018-06-22 19:22:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (finished || (m->events_seen >= m->next_progress_report)) {
|
2022-04-02 21:14:10 +00:00
|
|
|
int percentage =
|
|
|
|
(finished ? 100
|
2023-05-21 13:42:34 +00:00
|
|
|
: m->next_progress_report == 0
|
2022-04-02 21:14:10 +00:00
|
|
|
? 0
|
2023-05-21 17:35:09 +00:00
|
|
|
: std::min(99, 1 + ((100 * m->events_seen) / m->events_expected)));
|
2023-05-21 13:42:34 +00:00
|
|
|
m->progress_reporter->reportProgress(percentage);
|
2018-06-22 19:22:29 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
int increment = std::max(1, (m->events_expected / 100));
|
|
|
|
while (m->events_seen >= m->next_progress_report) {
|
|
|
|
m->next_progress_report += increment;
|
2018-06-22 19:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-09 18:35:56 +00:00
|
|
|
QPDFWriter::registerProgressReporter(std::shared_ptr<ProgressReporter> pr)
|
2018-06-22 19:22:29 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->progress_reporter = pr;
|
2018-06-22 19:22:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 00:56:14 +00:00
|
|
|
void
|
|
|
|
QPDFWriter::writeStandard()
|
|
|
|
{
|
2022-04-09 18:35:56 +00:00
|
|
|
auto pp_md5 = std::make_shared<PipelinePopper>(this);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->deterministic_id) {
|
2019-08-28 02:10:11 +00:00
|
|
|
pushMD5Pipeline(*pp_md5);
|
2017-08-22 00:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start writing
|
|
|
|
|
|
|
|
writeHeader();
|
2023-05-21 13:42:34 +00:00
|
|
|
writeString(m->extra_header_text);
|
2017-08-01 12:16:24 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->pclm) {
|
2017-08-22 00:56:14 +00:00
|
|
|
enqueueObjectsPCLm();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2017-08-22 00:56:14 +00:00
|
|
|
enqueueObjectsStandard();
|
|
|
|
}
|
|
|
|
|
2018-06-22 17:13:09 +00:00
|
|
|
// Now start walking queue, outputting each object.
|
2023-01-08 12:52:33 +00:00
|
|
|
while (m->object_queue_front < m->object_queue.size()) {
|
|
|
|
QPDFObjectHandle cur_object = m->object_queue.at(m->object_queue_front);
|
|
|
|
++m->object_queue_front;
|
2022-02-08 14:18:08 +00:00
|
|
|
writeObject(cur_object);
|
2017-08-22 00:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the encryption dictionary, if any
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->encrypted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
writeEncryptionDictionary();
|
2017-08-01 12:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now write out xref. next_objid is now the number of objects.
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_offset_t xref_offset = m->pipeline->getCount();
|
|
|
|
if (m->object_stream_to_objects.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Write regular cross-reference table
|
2023-05-21 13:42:34 +00:00
|
|
|
writeXRefTable(t_normal, 0, m->next_objid - 1, m->next_objid);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Write cross-reference stream.
|
2023-05-21 13:42:34 +00:00
|
|
|
int xref_id = m->next_objid++;
|
2022-04-02 21:14:10 +00:00
|
|
|
writeXRefStream(
|
2023-05-21 17:35:09 +00:00
|
|
|
xref_id, xref_id, xref_offset, t_normal, 0, m->next_objid - 1, m->next_objid);
|
2017-08-01 12:16:24 +00:00
|
|
|
}
|
|
|
|
writeString("startxref\n");
|
2022-09-21 16:49:21 +00:00
|
|
|
writeString(std::to_string(xref_offset));
|
2017-08-01 12:16:24 +00:00
|
|
|
writeString("\n%%EOF\n");
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->deterministic_id) {
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFWriter standard deterministic ID",
|
2023-05-21 13:42:34 +00:00
|
|
|
m->object_stream_to_objects.empty() ? 0 : 1);
|
2022-07-26 11:37:50 +00:00
|
|
|
pp_md5 = nullptr;
|
2023-05-21 13:42:34 +00:00
|
|
|
qpdf_assert_debug(m->md5_pipeline == nullptr);
|
2017-08-01 12:16:24 +00:00
|
|
|
}
|
|
|
|
}
|