2008-04-29 12:55:25 +00:00
|
|
|
// See doc/linearization.
|
|
|
|
|
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/BitStream.hh>
|
|
|
|
#include <qpdf/BitWriter.hh>
|
|
|
|
#include <qpdf/Pl_Buffer.hh>
|
|
|
|
#include <qpdf/Pl_Count.hh>
|
|
|
|
#include <qpdf/Pl_Flate.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDFExc.hh>
|
2022-06-05 17:30:42 +00:00
|
|
|
#include <qpdf/QPDFLogger.hh>
|
2024-02-19 12:05:55 +00:00
|
|
|
#include <qpdf/QPDFWriter_private.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
template <class T, class int_type>
|
2008-04-29 12:55:25 +00:00
|
|
|
static void
|
2022-04-02 21:14:10 +00:00
|
|
|
load_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
BitStream& bit_stream, int nitems, std::vector<T>& vec, int bits_wanted, int_type T::*field)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-10-05 15:30:27 +00:00
|
|
|
bool append = vec.empty();
|
2023-05-24 15:28:17 +00:00
|
|
|
// nitems times, read bits_wanted from the given bit stream, storing results in the ith vector
|
|
|
|
// entry.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < QIntC::to_size(nitems); ++i) {
|
|
|
|
if (append) {
|
2013-10-05 15:30:27 +00:00
|
|
|
vec.push_back(T());
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
vec.at(i).*field = bit_stream.getBitsInt(QIntC::to_size(bits_wanted));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QIntC::to_int(vec.size()) != nitems) {
|
2013-10-05 15:30:27 +00:00
|
|
|
throw std::logic_error("vector has wrong size in load_vector_int");
|
|
|
|
}
|
2023-05-24 15:28:17 +00:00
|
|
|
// The PDF spec says that each hint table starts at a byte boundary. Each "row" actually must
|
|
|
|
// start on a byte boundary.
|
2008-04-29 12:55:25 +00:00
|
|
|
bit_stream.skipToNextByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static void
|
2022-04-02 21:14:10 +00:00
|
|
|
load_vector_vector(
|
|
|
|
BitStream& bit_stream,
|
|
|
|
int nitems1,
|
|
|
|
std::vector<T>& vec1,
|
|
|
|
int T::*nitems2,
|
|
|
|
int bits_wanted,
|
|
|
|
std::vector<int> T::*vec2)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// nitems1 times, read nitems2 (from the ith element of vec1) items into the vec2 vector field
|
|
|
|
// of the ith item of vec1.
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i1 = 0; i1 < QIntC::to_size(nitems1); ++i1) {
|
|
|
|
for (int i2 = 0; i2 < vec1.at(i1).*nitems2; ++i2) {
|
2023-05-21 17:35:09 +00:00
|
|
|
(vec1.at(i1).*vec2).push_back(bit_stream.getBitsInt(QIntC::to_size(bits_wanted)));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
bit_stream.skipToNextByte();
|
|
|
|
}
|
|
|
|
|
2023-02-19 00:37:38 +00:00
|
|
|
void
|
|
|
|
QPDF::linearizationWarning(std::string_view msg)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->linearization_warnings = true;
|
2023-02-19 00:37:38 +00:00
|
|
|
warn(qpdf_e_linearization, "", 0, std::string(msg));
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
bool
|
|
|
|
QPDF::checkLinearization()
|
|
|
|
{
|
2019-06-23 13:10:42 +00:00
|
|
|
bool result = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
readLinearizationData();
|
|
|
|
result = checkLinearizationInternal();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::runtime_error& e) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"error encountered while checking linearization data: " + std::string(e.what()));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-06-23 13:10:42 +00:00
|
|
|
return result;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::isLinearized()
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// If the first object in the file is a dictionary with a suitable /Linearized key and has an /L
|
|
|
|
// key that accurately indicates the file size, initialize m->lindict and return true.
|
|
|
|
|
|
|
|
// A linearized PDF spec's first object will be contained within the first 1024 bytes of the
|
|
|
|
// file and will be a dictionary with a valid /Linearized key. This routine looks for that and
|
|
|
|
// does no additional validation.
|
|
|
|
|
|
|
|
// The PDF spec says the linearization dictionary must be completely contained within the first
|
|
|
|
// 1024 bytes of the file. Add a byte for a null terminator.
|
2008-04-29 12:55:25 +00:00
|
|
|
static int const tbuf_size = 1025;
|
|
|
|
|
2022-02-06 18:53:16 +00:00
|
|
|
auto b = std::make_unique<char[]>(tbuf_size);
|
|
|
|
char* buf = b.get();
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(0, SEEK_SET);
|
2008-04-29 12:55:25 +00:00
|
|
|
memset(buf, '\0', tbuf_size);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->read(buf, tbuf_size - 1);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2011-01-31 14:59:42 +00:00
|
|
|
int lindict_obj = -1;
|
2008-04-29 12:55:25 +00:00
|
|
|
char* p = buf;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (lindict_obj == -1) {
|
2017-08-10 00:17:18 +00:00
|
|
|
// Find a digit or end of buffer
|
2022-04-02 21:14:10 +00:00
|
|
|
while (((p - buf) < tbuf_size) && (!QUtil::is_digit(*p))) {
|
2017-08-10 00:17:18 +00:00
|
|
|
++p;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (p - buf == tbuf_size) {
|
2017-08-10 00:17:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Seek to the digit. Then skip over digits for a potential
|
|
|
|
// next iteration.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(p - buf, SEEK_SET);
|
2022-04-02 21:14:10 +00:00
|
|
|
while (((p - buf) < tbuf_size) && QUtil::is_digit(*p)) {
|
2017-08-10 00:17:18 +00:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFTokenizer::Token t1 = readToken(m->file);
|
2023-05-21 17:35:09 +00:00
|
|
|
if (t1.isInteger() && readToken(m->file).isInteger() && readToken(m->file).isWord("obj") &&
|
2022-09-29 13:33:11 +00:00
|
|
|
(readToken(m->file).getType() == QPDFTokenizer::tt_dict_open)) {
|
2022-07-13 13:24:19 +00:00
|
|
|
lindict_obj = toI(QUtil::string_to_ll(t1.getValue().c_str()));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lindict_obj <= 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 11:33:07 +00:00
|
|
|
auto candidate = getObjectByID(lindict_obj, 0);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!candidate.isDictionary()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle linkey = candidate.getKey("/Linearized");
|
2022-07-13 13:24:19 +00:00
|
|
|
if (!(linkey.isNumber() && (toI(floor(linkey.getNumericValue())) == 1))) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle L = candidate.getKey("/L");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (L.isInteger()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t Li = L.getIntValue();
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(0, SEEK_END);
|
|
|
|
if (Li != m->file->tell()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF /L mismatch");
|
|
|
|
return false;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->linp.file_size = Li;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->lindict = candidate;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::readLinearizationData()
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// This function throws an exception (which is trapped by checkLinearization()) for any errors
|
|
|
|
// that prevent loading.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!isLinearized()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error("called readLinearizationData for file"
|
|
|
|
" that is not linearized");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// /L is read and stored in linp by isLinearized()
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFObjectHandle H = m->lindict.getKey("/H");
|
|
|
|
QPDFObjectHandle O = m->lindict.getKey("/O");
|
|
|
|
QPDFObjectHandle E = m->lindict.getKey("/E");
|
|
|
|
QPDFObjectHandle N = m->lindict.getKey("/N");
|
|
|
|
QPDFObjectHandle T = m->lindict.getKey("/T");
|
|
|
|
QPDFObjectHandle P = m->lindict.getKey("/P");
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
if (!(H.isArray() && O.isInteger() && E.isInteger() && N.isInteger() && T.isInteger() &&
|
|
|
|
(P.isInteger() || P.isNull()))) {
|
2022-09-28 11:25:52 +00:00
|
|
|
throw damagedPDF(
|
2022-04-02 21:14:10 +00:00
|
|
|
"linearization dictionary",
|
2022-09-28 11:25:52 +00:00
|
|
|
"some keys in linearization dictionary are of the wrong type");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hint table array: offset length [ offset length ]
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t n_H_items = toS(H.getArrayNItems());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!((n_H_items == 2) || (n_H_items == 4))) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("linearization dictionary", "H has the wrong number of items");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> H_items;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < n_H_items; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle oh(H.getArrayItem(toI(i)));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (oh.isInteger()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
H_items.push_back(oh.getIntValueAsInt());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("linearization dictionary", "some H items are of the wrong type");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// H: hint table offset/length for primary and overflow hint tables
|
2013-10-05 23:42:39 +00:00
|
|
|
int H0_offset = H_items.at(0);
|
|
|
|
int H0_length = H_items.at(1);
|
2008-04-29 12:55:25 +00:00
|
|
|
int H1_offset = 0;
|
|
|
|
int H1_length = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (H_items.size() == 4) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Acrobat doesn't read or write these (as PDF 1.4), so we don't have a way to generate a
|
|
|
|
// test case.
|
2022-02-08 14:18:08 +00:00
|
|
|
// QTC::TC("qpdf", "QPDF overflow hint table");
|
|
|
|
H1_offset = H_items.at(2);
|
|
|
|
H1_length = H_items.at(3);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// P: first page number
|
|
|
|
int first_page = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (P.isInteger()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF P present in lindict");
|
|
|
|
first_page = P.getIntValueAsInt();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF P absent in lindict");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store linearization parameter data
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Various places in the code use linp.npages, which is initialized from N, to pre-allocate
|
|
|
|
// memory, so make sure it's accurate and bail right now if it's not.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (N.getIntValue() != static_cast<long long>(getAllPages().size())) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("linearization hint table", "/N does not match number of pages");
|
2013-10-05 15:30:27 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// file_size initialized by isLinearized()
|
2023-05-21 13:42:34 +00:00
|
|
|
m->linp.first_page_object = O.getIntValueAsInt();
|
|
|
|
m->linp.first_page_end = E.getIntValue();
|
|
|
|
m->linp.npages = N.getIntValueAsInt();
|
|
|
|
m->linp.xref_zero_offset = T.getIntValue();
|
|
|
|
m->linp.first_page = first_page;
|
|
|
|
m->linp.H_offset = H0_offset;
|
|
|
|
m->linp.H_length = H0_length;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Read hint streams
|
|
|
|
|
|
|
|
Pl_Buffer pb("hint buffer");
|
2019-06-21 03:35:23 +00:00
|
|
|
QPDFObjectHandle H0 = readHintStream(pb, H0_offset, toS(H0_length));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (H1_offset) {
|
|
|
|
(void)readHintStream(pb, H1_offset, toS(H1_length));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PDF 1.4 hint tables that we ignore:
|
|
|
|
|
|
|
|
// /T thumbnail
|
|
|
|
// /A thread information
|
|
|
|
// /E named destination
|
|
|
|
// /V interactive form
|
|
|
|
// /I information dictionary
|
|
|
|
// /C logical structure
|
|
|
|
// /L page label
|
|
|
|
|
|
|
|
// Individual hint table offsets
|
|
|
|
QPDFObjectHandle HS = H0.getKey("/S"); // shared object
|
|
|
|
QPDFObjectHandle HO = H0.getKey("/O"); // outline
|
|
|
|
|
2022-02-06 16:40:24 +00:00
|
|
|
auto hbp = pb.getBufferSharedPointer();
|
2022-02-04 15:10:19 +00:00
|
|
|
Buffer* hb = hbp.get();
|
2008-04-29 12:55:25 +00:00
|
|
|
unsigned char const* h_buf = hb->getBuffer();
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t h_size = hb->getSize();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
readHPageOffset(BitStream(h_buf, h_size));
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
int HSi = HS.getIntValueAsInt();
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((HSi < 0) || (toS(HSi) >= h_size)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("linearization hint table", "/S (shared object) offset is out of bounds");
|
2013-10-05 10:26:06 +00:00
|
|
|
}
|
2019-06-21 03:35:23 +00:00
|
|
|
readHSharedObject(BitStream(h_buf + HSi, h_size - toS(HSi)));
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (HO.isInteger()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int HOi = HO.getIntValueAsInt();
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((HOi < 0) || (toS(HOi) >= h_size)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("linearization hint table", "/O (outline) offset is out of bounds");
|
2013-10-05 10:26:06 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
readHGeneric(BitStream(h_buf + HOi, h_size - toS(HOi)), m->outline_hints);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDF::readHintStream(Pipeline& pl, qpdf_offset_t offset, size_t length)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-07-23 12:47:18 +00:00
|
|
|
QPDFObjGen og;
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFObjectHandle H =
|
|
|
|
readObjectAtOffset(false, offset, "linearization hint stream", QPDFObjGen(0, 0), og, false);
|
2023-05-21 13:42:34 +00:00
|
|
|
ObjCache& oc = m->obj_cache[og];
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t min_end_offset = oc.end_before_space;
|
|
|
|
qpdf_offset_t max_end_offset = oc.end_after_space;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!H.isStream()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw damagedPDF("linearization dictionary", "hint table is not a stream");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle Hdict = H.getDict();
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Some versions of Acrobat make /Length indirect and place it immediately after the stream,
|
|
|
|
// increasing length to cover it, even though the specification says all objects in the
|
|
|
|
// linearization parameter dictionary must be direct. We have to get the file position of the
|
|
|
|
// end of length in this case.
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle length_obj = Hdict.getKey("/Length");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (length_obj.isIndirect()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF hint table length indirect");
|
|
|
|
// Force resolution
|
2022-04-02 21:14:10 +00:00
|
|
|
(void)length_obj.getIntValue();
|
2023-05-21 13:42:34 +00:00
|
|
|
ObjCache& oc2 = m->obj_cache[length_obj.getObjGen()];
|
2022-02-08 14:18:08 +00:00
|
|
|
min_end_offset = oc2.end_before_space;
|
|
|
|
max_end_offset = oc2.end_after_space;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF hint table length direct");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-06-21 03:35:23 +00:00
|
|
|
qpdf_offset_t computed_end = offset + toO(length);
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((computed_end < min_end_offset) || (computed_end > max_end_offset)) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
|
|
|
"expected = " + std::to_string(computed_end) +
|
2023-05-21 17:35:09 +00:00
|
|
|
"; actual = " + std::to_string(min_end_offset) + ".." + std::to_string(max_end_offset));
|
|
|
|
throw damagedPDF("linearization dictionary", "hint table length mismatch");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-19 13:18:14 +00:00
|
|
|
H.pipeStreamData(&pl, 0, qpdf_dl_specialized);
|
2008-04-29 12:55:25 +00:00
|
|
|
return Hdict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::readHPageOffset(BitStream h)
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// All comments referring to the PDF spec refer to the spec for version 1.4.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
HPageOffset& t = m->page_offset_hints;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
t.min_nobjects = h.getBitsInt(32); // 1
|
|
|
|
t.first_page_offset = h.getBitsInt(32); // 2
|
|
|
|
t.nbits_delta_nobjects = h.getBitsInt(16); // 3
|
|
|
|
t.min_page_length = h.getBitsInt(32); // 4
|
|
|
|
t.nbits_delta_page_length = h.getBitsInt(16); // 5
|
|
|
|
t.min_content_offset = h.getBitsInt(32); // 6
|
|
|
|
t.nbits_delta_content_offset = h.getBitsInt(16); // 7
|
|
|
|
t.min_content_length = h.getBitsInt(32); // 8
|
|
|
|
t.nbits_delta_content_length = h.getBitsInt(16); // 9
|
|
|
|
t.nbits_nshared_objects = h.getBitsInt(16); // 10
|
|
|
|
t.nbits_shared_identifier = h.getBitsInt(16); // 11
|
|
|
|
t.nbits_shared_numerator = h.getBitsInt(16); // 12
|
|
|
|
t.shared_denominator = h.getBitsInt(16); // 13
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::vector<HPageOffsetEntry>& entries = t.entries;
|
2013-10-05 15:30:27 +00:00
|
|
|
entries.clear();
|
2023-05-21 13:42:34 +00:00
|
|
|
int nitems = m->linp.npages;
|
2023-05-21 17:35:09 +00:00
|
|
|
load_vector_int(h, nitems, entries, t.nbits_delta_nobjects, &HPageOffsetEntry::delta_nobjects);
|
2022-04-02 21:14:10 +00:00
|
|
|
load_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
h, nitems, entries, t.nbits_delta_page_length, &HPageOffsetEntry::delta_page_length);
|
2022-04-02 21:14:10 +00:00
|
|
|
load_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
h, nitems, entries, t.nbits_nshared_objects, &HPageOffsetEntry::nshared_objects);
|
2022-04-02 21:14:10 +00:00
|
|
|
load_vector_vector(
|
|
|
|
h,
|
|
|
|
nitems,
|
|
|
|
entries,
|
|
|
|
&HPageOffsetEntry::nshared_objects,
|
|
|
|
t.nbits_shared_identifier,
|
|
|
|
&HPageOffsetEntry::shared_identifiers);
|
|
|
|
load_vector_vector(
|
|
|
|
h,
|
|
|
|
nitems,
|
|
|
|
entries,
|
|
|
|
&HPageOffsetEntry::nshared_objects,
|
|
|
|
t.nbits_shared_numerator,
|
|
|
|
&HPageOffsetEntry::shared_numerators);
|
|
|
|
load_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
h, nitems, entries, t.nbits_delta_content_offset, &HPageOffsetEntry::delta_content_offset);
|
2022-04-02 21:14:10 +00:00
|
|
|
load_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
h, nitems, entries, t.nbits_delta_content_length, &HPageOffsetEntry::delta_content_length);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::readHSharedObject(BitStream h)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
HSharedObject& t = m->shared_object_hints;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
t.first_shared_obj = h.getBitsInt(32); // 1
|
|
|
|
t.first_shared_offset = h.getBitsInt(32); // 2
|
|
|
|
t.nshared_first_page = h.getBitsInt(32); // 3
|
|
|
|
t.nshared_total = h.getBitsInt(32); // 4
|
|
|
|
t.nbits_nobjects = h.getBitsInt(16); // 5
|
|
|
|
t.min_group_length = h.getBitsInt(32); // 6
|
|
|
|
t.nbits_delta_group_length = h.getBitsInt(16); // 7
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDF lin nshared_total > nshared_first_page",
|
|
|
|
(t.nshared_total > t.nshared_first_page) ? 1 : 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::vector<HSharedObjectEntry>& entries = t.entries;
|
2013-10-05 15:30:27 +00:00
|
|
|
entries.clear();
|
|
|
|
int nitems = t.nshared_total;
|
2022-04-02 21:14:10 +00:00
|
|
|
load_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
h, nitems, entries, t.nbits_delta_group_length, &HSharedObjectEntry::delta_group_length);
|
|
|
|
load_vector_int(h, nitems, entries, 1, &HSharedObjectEntry::signature_present);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(nitems); ++i) {
|
|
|
|
if (entries.at(i).signature_present) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Skip 128-bit MD5 hash. These are not supported by acrobat, so they should probably
|
|
|
|
// never be there. We have no test case for this.
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int j = 0; j < 4; ++j) {
|
|
|
|
(void)h.getBits(32);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
load_vector_int(h, nitems, entries, t.nbits_nobjects, &HSharedObjectEntry::nobjects_minus_one);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::readHGeneric(BitStream h, HGeneric& t)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
t.first_object = h.getBitsInt(32); // 1
|
|
|
|
t.first_object_offset = h.getBitsInt(32); // 2
|
|
|
|
t.nobjects = h.getBitsInt(32); // 3
|
|
|
|
t.group_length = h.getBitsInt(32); // 4
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 13:10:42 +00:00
|
|
|
bool
|
|
|
|
QPDF::checkLinearizationInternal()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// All comments referring to the PDF spec refer to the spec for version 1.4.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Check all values in linearization parameter dictionary
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
LinParameters& p = m->linp;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// L: file size in bytes -- checked by isLinearized
|
|
|
|
|
|
|
|
// O: object number of first page
|
|
|
|
std::vector<QPDFObjectHandle> const& pages = getAllPages();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (p.first_page_object != pages.at(0).getObjectID()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF err /O mismatch");
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("first page object (/O) mismatch");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// N: number of pages
|
2019-06-21 03:35:23 +00:00
|
|
|
int npages = toI(pages.size());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (p.npages != npages) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Not tested in the test suite
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("page count (/N) mismatch");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(npages); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle const& page = pages.at(i);
|
|
|
|
QPDFObjGen og(page.getObjGen());
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->xref_table[og].getType() == 2) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"page dictionary for page " + std::to_string(i) + " is compressed");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// T: offset of whitespace character preceding xref entry for object 0
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(p.xref_zero_offset, SEEK_SET);
|
2022-07-26 11:37:50 +00:00
|
|
|
while (true) {
|
2022-02-08 14:18:08 +00:00
|
|
|
char ch;
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->read(&ch, 1);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!((ch == ' ') || (ch == '\r') || (ch == '\n'))) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->file->seek(-1, SEEK_CUR);
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->file->tell() != m->first_xref_item_offset) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF err /T mismatch");
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2022-04-02 21:14:10 +00:00
|
|
|
"space before first xref item (/T) mismatch "
|
|
|
|
"(computed = " +
|
2023-05-21 13:42:34 +00:00
|
|
|
std::to_string(m->first_xref_item_offset) +
|
|
|
|
"; file = " + std::to_string(m->file->tell()));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// P: first page number -- Implementation note 124 says Acrobat ignores this value, so we will
|
|
|
|
// too.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Check numbering of compressed objects in each xref section. For linearized files, all
|
|
|
|
// compressed objects are supposed to be at the end of the containing xref section if any object
|
|
|
|
// streams are in use.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->uncompressed_after_compressed) {
|
2023-05-24 15:28:17 +00:00
|
|
|
linearizationWarning("linearized file contains an uncompressed object after a compressed "
|
|
|
|
"one in a cross-reference stream");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Further checking requires optimization and order calculation. Don't allow optimization to
|
|
|
|
// make changes. If it has to, then the file is not properly linearized. We use the xref table
|
|
|
|
// to figure out which objects are compressed and which are uncompressed.
|
2008-04-29 12:55:25 +00:00
|
|
|
{ // local scope
|
2022-02-08 14:18:08 +00:00
|
|
|
std::map<int, int> object_stream_data;
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& iter: m->xref_table) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjGen const& og = iter.first;
|
|
|
|
QPDFXRefEntry const& entry = iter.second;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (entry.getType() == 2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
object_stream_data[og.getObj()] = entry.getObjStreamNumber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
optimize(object_stream_data, false);
|
|
|
|
calculateLinearizationData(object_stream_data);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// E: offset of end of first page -- Implementation note 123 says Acrobat includes on extra
|
|
|
|
// object here by mistake. pdlin fails to place thumbnail images in section 9, so when
|
|
|
|
// thumbnails are present, it also gets the wrong value for /E. It also doesn't count outlines
|
|
|
|
// here when it should even though it places them in part 6. This code fails to put thread
|
|
|
|
// information dictionaries in part 9, so it actually gets the wrong value for E when threads
|
|
|
|
// are present. In that case, it would probably agree with pdlin. As of this writing, the test
|
|
|
|
// suite doesn't contain any files with threads.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->part6.empty()) {
|
2019-06-21 21:37:33 +00:00
|
|
|
stopOnError("linearization part 6 unexpectedly empty");
|
2013-10-05 20:37:27 +00:00
|
|
|
}
|
2013-02-24 02:46:21 +00:00
|
|
|
qpdf_offset_t min_E = -1;
|
|
|
|
qpdf_offset_t max_E = -1;
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& oh: m->part6) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjGen og(oh.getObjGen());
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->obj_cache.count(og) == 0) {
|
2013-10-05 20:37:27 +00:00
|
|
|
// All objects have to have been dereferenced to be classified.
|
|
|
|
throw std::logic_error("linearization part6 object not in cache");
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
ObjCache const& oc = m->obj_cache[og];
|
2022-02-08 14:18:08 +00:00
|
|
|
min_E = std::max(min_E, oc.end_before_space);
|
|
|
|
max_E = std::max(max_E, oc.end_after_space);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((p.first_page_end < min_E) || (p.first_page_end > max_E)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF warn /E mismatch");
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"end of first page section (/E) mismatch: /E = " + std::to_string(p.first_page_end) +
|
|
|
|
"; computed = " + std::to_string(min_E) + ".." + std::to_string(max_E));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check hint tables
|
|
|
|
|
|
|
|
std::map<int, int> shared_idx_to_obj;
|
2023-02-19 00:37:38 +00:00
|
|
|
checkHSharedObject(pages, shared_idx_to_obj);
|
|
|
|
checkHPageOffset(pages, shared_idx_to_obj);
|
|
|
|
checkHOutlines();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
return !m->linearization_warnings;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-24 02:46:21 +00:00
|
|
|
qpdf_offset_t
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF::maxEnd(ObjUser const& ou)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->obj_user_to_objects.count(ou) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("no entry in object user table for requested object user");
|
|
|
|
}
|
2013-02-24 02:46:21 +00:00
|
|
|
qpdf_offset_t end = 0;
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& og: m->obj_user_to_objects[ou]) {
|
|
|
|
if (m->obj_cache.count(og) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("unknown object referenced in object user table");
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
end = std::max(end, m->obj_cache[og].end_after_space);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDF::getLinearizationOffset(QPDFObjGen const& og)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
QPDFXRefEntry entry = m->xref_table[og];
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t result = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (entry.getType()) {
|
|
|
|
case 1:
|
2022-02-08 14:18:08 +00:00
|
|
|
result = entry.getOffset();
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case 2:
|
2023-05-24 15:28:17 +00:00
|
|
|
// For compressed objects, return the offset of the object stream that contains them.
|
2023-05-21 17:35:09 +00:00
|
|
|
result = getLinearizationOffset(QPDFObjGen(entry.getObjStreamNumber(), 0));
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2023-05-21 17:35:09 +00:00
|
|
|
stopOnError("getLinearizationOffset called for xref entry not of type 1 or 2");
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::getUncompressedObject(QPDFObjectHandle& obj, std::map<int, int> const& object_stream_data)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.isNull() || (object_stream_data.count(obj.getObjectID()) == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return obj;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
int repl = (*(object_stream_data.find(obj.getObjectID()))).second;
|
2022-08-01 18:22:37 +00:00
|
|
|
return getObject(repl, 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-02-19 00:37:38 +00:00
|
|
|
QPDF::lengthNextN(int first_object, int n)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
int length = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen og(first_object + i, 0);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->xref_table.count(og) == 0) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"no xref table entry for " + std::to_string(first_object + i) + " 0");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->obj_cache.count(og) == 0) {
|
2023-05-24 15:28:17 +00:00
|
|
|
stopOnError("found unknown object while calculating length for linearization data");
|
2019-01-04 16:50:02 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
length += toI(m->obj_cache[og].end_after_space - getLinearizationOffset(og));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::checkHPageOffset(
|
2023-05-21 17:35:09 +00:00
|
|
|
std::vector<QPDFObjectHandle> const& pages, std::map<int, int>& shared_idx_to_obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// Implementation note 126 says Acrobat always sets delta_content_offset and
|
|
|
|
// delta_content_length in the page offset header dictionary to 0. It also states that
|
|
|
|
// min_content_offset in the per-page information is always 0, which is an incorrect value.
|
|
|
|
|
|
|
|
// Implementation note 127 explains that Acrobat always sets item 8 (min_content_length) to
|
|
|
|
// zero, item 9 (nbits_delta_content_length) to the value of item 5 (nbits_delta_page_length),
|
|
|
|
// and item 7 of each per-page hint table (delta_content_length) to item 2 (delta_page_length)
|
|
|
|
// of that entry. Acrobat ignores these values when reading files.
|
|
|
|
|
|
|
|
// Empirically, it also seems that Acrobat sometimes puts items under a page's /Resources
|
|
|
|
// dictionary in with shared objects even when they are private.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
int npages = toI(pages.size());
|
2023-05-21 17:35:09 +00:00
|
|
|
qpdf_offset_t table_offset = adjusted_offset(m->page_offset_hints.first_page_offset);
|
2013-10-05 23:42:39 +00:00
|
|
|
QPDFObjGen first_page_og(pages.at(0).getObjGen());
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->xref_table.count(first_page_og) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("supposed first page object is not known");
|
|
|
|
}
|
2019-01-07 13:49:14 +00:00
|
|
|
qpdf_offset_t offset = getLinearizationOffset(first_page_og);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (table_offset != offset) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("first page object offset mismatch");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int pageno = 0; pageno < npages; ++pageno) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen page_og(pages.at(toS(pageno)).getObjGen());
|
|
|
|
int first_object = page_og.getObj();
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->xref_table.count(page_og) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("unknown object in page offset hint table");
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
offset = getLinearizationOffset(page_og);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
HPageOffsetEntry& he = m->page_offset_hints.entries.at(toS(pageno));
|
|
|
|
CHPageOffsetEntry& ce = m->c_page_offset_data.entries.at(toS(pageno));
|
|
|
|
int h_nobjects = he.delta_nobjects + m->page_offset_hints.min_nobjects;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (h_nobjects != ce.nobjects) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// This happens with pdlin when there are thumbnails.
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"object count mismatch for page " + std::to_string(pageno) + ": hint table = " +
|
|
|
|
std::to_string(h_nobjects) + "; computed = " + std::to_string(ce.nobjects));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Use value for number of objects in hint table rather than computed value if there is a
|
|
|
|
// discrepancy.
|
2023-02-19 00:37:38 +00:00
|
|
|
int length = lengthNextN(first_object, h_nobjects);
|
2023-05-21 17:35:09 +00:00
|
|
|
int h_length = toI(he.delta_page_length + m->page_offset_hints.min_page_length);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (length != h_length) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// This condition almost certainly indicates a bad hint table or a bug in this code.
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"page length mismatch for page " + std::to_string(pageno) + ": hint table = " +
|
|
|
|
std::to_string(h_length) + "; computed length = " + std::to_string(length) +
|
2022-09-21 16:49:21 +00:00
|
|
|
" (offset = " + std::to_string(offset) + ")");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset += h_length;
|
|
|
|
|
|
|
|
// Translate shared object indexes to object numbers.
|
|
|
|
std::set<int> hint_shared;
|
|
|
|
std::set<int> computed_shared;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((pageno == 0) && (he.nshared_objects > 0)) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// pdlin and Acrobat both do this even though the spec states clearly and unambiguously
|
|
|
|
// that they should not.
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("page 0 has shared identifier entries");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(he.nshared_objects); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int idx = he.shared_identifiers.at(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (shared_idx_to_obj.count(idx) == 0) {
|
|
|
|
stopOnError("unable to get object for item in"
|
|
|
|
" shared objects hint table");
|
2013-10-05 20:37:27 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
hint_shared.insert(shared_idx_to_obj[idx]);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(ce.nshared_objects); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int idx = ce.shared_identifiers.at(i);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (idx >= m->c_shared_object_data.nshared_total) {
|
2022-04-02 21:14:10 +00:00
|
|
|
stopOnError("index out of bounds for shared object hint table");
|
2013-10-05 20:37:27 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
int obj = m->c_shared_object_data.entries.at(toS(idx)).object;
|
2022-02-08 14:18:08 +00:00
|
|
|
computed_shared.insert(obj);
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (int iter: hint_shared) {
|
|
|
|
if (!computed_shared.count(iter)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// pdlin puts thumbnails here even though it shouldn't
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"page " + std::to_string(pageno) + ": shared object " + std::to_string(iter) +
|
2022-02-08 14:18:08 +00:00
|
|
|
": in hint table but not computed list");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (int iter: computed_shared) {
|
|
|
|
if (!hint_shared.count(iter)) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Acrobat does not put some things including at least built-in fonts and procsets
|
|
|
|
// here, at least in some cases.
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
("page " + std::to_string(pageno) + ": shared object " + std::to_string(iter) +
|
2023-02-19 00:37:38 +00:00
|
|
|
": in computed list but not hint table"));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDF::checkHSharedObject(std::vector<QPDFObjectHandle> const& pages, std::map<int, int>& idx_to_obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// Implementation note 125 says shared object groups always contain only one object.
|
|
|
|
// Implementation note 128 says that Acrobat always nbits_nobjects to zero. Implementation note
|
|
|
|
// 130 says that Acrobat does not support more than one shared object per group. These are all
|
|
|
|
// consistent.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Implementation note 129 states that MD5 signatures are not implemented in Acrobat, so
|
|
|
|
// signature_present must always be zero.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Implementation note 131 states that first_shared_obj and first_shared_offset have meaningless
|
|
|
|
// values for single-page files.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Empirically, Acrobat and pdlin generate incorrect values for these whenever there are no
|
|
|
|
// shared objects not referenced by the first page (i.e., nshared_total == nshared_first_page).
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
HSharedObject& so = m->shared_object_hints;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (so.nshared_total < so.nshared_first_page) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("shared object hint table: ntotal < nfirst_page");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-24 15:28:17 +00:00
|
|
|
// The first nshared_first_page objects are consecutive objects starting with the first page
|
|
|
|
// object. The rest are consecutive starting from the first_shared_obj object.
|
2022-02-08 14:18:08 +00:00
|
|
|
int cur_object = pages.at(0).getObjectID();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < so.nshared_total; ++i) {
|
|
|
|
if (i == so.nshared_first_page) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF lin check shared past first page");
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->part8.empty()) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("part 8 is empty but nshared_total > "
|
|
|
|
"nshared_first_page");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 13:42:34 +00:00
|
|
|
int obj = m->part8.at(0).getObjectID();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj != so.first_shared_obj) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2022-02-08 14:18:08 +00:00
|
|
|
"first shared object number mismatch: "
|
|
|
|
"hint table = " +
|
2022-09-21 16:49:21 +00:00
|
|
|
std::to_string(so.first_shared_obj) +
|
|
|
|
"; computed = " + std::to_string(obj));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_object = so.first_shared_obj;
|
|
|
|
|
|
|
|
QPDFObjGen og(cur_object, 0);
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->xref_table.count(og) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("unknown object in shared object hint table");
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t offset = getLinearizationOffset(og);
|
2023-05-21 17:35:09 +00:00
|
|
|
qpdf_offset_t h_offset = adjusted_offset(so.first_shared_offset);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (offset != h_offset) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2022-02-08 14:18:08 +00:00
|
|
|
"first shared object offset mismatch: hint table = " +
|
2023-05-21 17:35:09 +00:00
|
|
|
std::to_string(h_offset) + "; computed = " + std::to_string(offset));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idx_to_obj[i] = cur_object;
|
|
|
|
HSharedObjectEntry& se = so.entries.at(toS(i));
|
|
|
|
int nobjects = se.nobjects_minus_one + 1;
|
2023-02-19 00:37:38 +00:00
|
|
|
int length = lengthNextN(cur_object, nobjects);
|
2022-02-08 14:18:08 +00:00
|
|
|
int h_length = so.min_group_length + se.delta_group_length;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (length != h_length) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2023-05-21 17:35:09 +00:00
|
|
|
"shared object " + std::to_string(i) + " length mismatch: hint table = " +
|
|
|
|
std::to_string(h_length) + "; computed = " + std::to_string(length));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
cur_object += nobjects;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-02-19 00:37:38 +00:00
|
|
|
QPDF::checkHOutlines()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// Empirically, Acrobat generates the correct value for the object number but incorrectly stores
|
|
|
|
// the next object number's offset as the offset, at least when outlines appear in part 6. It
|
|
|
|
// also generates an incorrect value for length (specifically, the length that would cover the
|
|
|
|
// correct number of objects from the wrong starting place). pdlin appears to generate correct
|
2008-04-29 12:55:25 +00:00
|
|
|
// values in those cases.
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->c_outline_data.nobjects == m->outline_hints.nobjects) {
|
|
|
|
if (m->c_outline_data.nobjects == 0) {
|
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
|
|
|
if (m->c_outline_data.first_object == m->outline_hints.first_object) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Check length and offset. Acrobat gets these wrong.
|
|
|
|
QPDFObjectHandle outlines = getRoot().getKey("/Outlines");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!outlines.isIndirect()) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// This case is not exercised in test suite since not permitted by the spec, but if
|
|
|
|
// this does occur, the code below would fail.
|
2023-05-21 17:35:09 +00:00
|
|
|
linearizationWarning("/Outlines key of root dictionary is not indirect");
|
2013-06-15 01:35:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen og(outlines.getObjGen());
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->xref_table.count(og) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("unknown object in outlines hint table");
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_offset_t offset = getLinearizationOffset(og);
|
|
|
|
ObjUser ou(ObjUser::ou_root_key, "/Outlines");
|
|
|
|
int length = toI(maxEnd(ou) - offset);
|
2023-05-21 17:35:09 +00:00
|
|
|
qpdf_offset_t table_offset = adjusted_offset(m->outline_hints.first_object_offset);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (offset != table_offset) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2022-02-08 14:18:08 +00:00
|
|
|
"incorrect offset in outlines table: hint table = " +
|
2023-05-21 17:35:09 +00:00
|
|
|
std::to_string(table_offset) + "; computed = " + std::to_string(offset));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
int table_length = m->outline_hints.group_length;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (length != table_length) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(
|
2022-02-08 14:18:08 +00:00
|
|
|
"incorrect length in outlines table: hint table = " +
|
2023-05-21 17:35:09 +00:00
|
|
|
std::to_string(table_length) + "; computed = " + std::to_string(length));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("incorrect first object number in outline "
|
|
|
|
"hints table.");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning("incorrect object count in outline hint table");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::showLinearizationData()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
readLinearizationData();
|
|
|
|
checkLinearizationInternal();
|
|
|
|
dumpLinearizationDataInternal();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
2023-02-19 00:37:38 +00:00
|
|
|
linearizationWarning(e.what());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::dumpLinearizationDataInternal()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
*m->log->getInfo() << m->file->getName() << ": linearization data:\n\n";
|
|
|
|
|
|
|
|
*m->log->getInfo() << "file_size: " << m->linp.file_size << "\n"
|
2023-05-21 17:35:09 +00:00
|
|
|
<< "first_page_object: " << m->linp.first_page_object << "\n"
|
2023-05-21 13:42:34 +00:00
|
|
|
<< "first_page_end: " << m->linp.first_page_end << "\n"
|
|
|
|
<< "npages: " << m->linp.npages << "\n"
|
2023-05-21 17:35:09 +00:00
|
|
|
<< "xref_zero_offset: " << m->linp.xref_zero_offset << "\n"
|
2023-05-21 13:42:34 +00:00
|
|
|
<< "first_page: " << m->linp.first_page << "\n"
|
|
|
|
<< "H_offset: " << m->linp.H_offset << "\n"
|
|
|
|
<< "H_length: " << m->linp.H_length << "\n"
|
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
*m->log->getInfo() << "Page Offsets Hint Table\n\n";
|
2008-04-29 12:55:25 +00:00
|
|
|
dumpHPageOffset();
|
2023-05-21 13:42:34 +00:00
|
|
|
*m->log->getInfo() << "\nShared Objects Hint Table\n\n";
|
2008-04-29 12:55:25 +00:00
|
|
|
dumpHSharedObject();
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->outline_hints.nobjects > 0) {
|
|
|
|
*m->log->getInfo() << "\nOutlines Hint Table\n\n";
|
|
|
|
dumpHGeneric(m->outline_hints);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
qpdf_offset_t
|
|
|
|
QPDF::adjusted_offset(qpdf_offset_t offset)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// All offsets >= H_offset have to be increased by H_length since all hint table location values
|
|
|
|
// disregard the hint table itself.
|
2023-05-21 13:42:34 +00:00
|
|
|
if (offset >= m->linp.H_offset) {
|
|
|
|
return offset + m->linp.H_length;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::dumpHPageOffset()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
HPageOffset& t = m->page_offset_hints;
|
2023-05-21 17:35:09 +00:00
|
|
|
*m->log->getInfo() << "min_nobjects: " << t.min_nobjects << "\n"
|
|
|
|
<< "first_page_offset: " << adjusted_offset(t.first_page_offset) << "\n"
|
|
|
|
<< "nbits_delta_nobjects: " << t.nbits_delta_nobjects << "\n"
|
|
|
|
<< "min_page_length: " << t.min_page_length << "\n"
|
|
|
|
<< "nbits_delta_page_length: " << t.nbits_delta_page_length << "\n"
|
|
|
|
<< "min_content_offset: " << t.min_content_offset << "\n"
|
|
|
|
<< "nbits_delta_content_offset: " << t.nbits_delta_content_offset << "\n"
|
|
|
|
<< "min_content_length: " << t.min_content_length << "\n"
|
|
|
|
<< "nbits_delta_content_length: " << t.nbits_delta_content_length << "\n"
|
|
|
|
<< "nbits_nshared_objects: " << t.nbits_nshared_objects << "\n"
|
|
|
|
<< "nbits_shared_identifier: " << t.nbits_shared_identifier << "\n"
|
|
|
|
<< "nbits_shared_numerator: " << t.nbits_shared_numerator << "\n"
|
|
|
|
<< "shared_denominator: " << t.shared_denominator << "\n";
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
for (size_t i1 = 0; i1 < toS(m->linp.npages); ++i1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
HPageOffsetEntry& pe = t.entries.at(i1);
|
2023-05-21 17:35:09 +00:00
|
|
|
*m->log->getInfo() << "Page " << i1 << ":\n"
|
|
|
|
<< " nobjects: " << pe.delta_nobjects + t.min_nobjects << "\n"
|
|
|
|
<< " length: " << pe.delta_page_length + t.min_page_length
|
|
|
|
<< "\n"
|
|
|
|
// content offset is relative to page, not file
|
|
|
|
<< " content_offset: " << pe.delta_content_offset + t.min_content_offset
|
|
|
|
<< "\n"
|
|
|
|
<< " content_length: " << pe.delta_content_length + t.min_content_length
|
|
|
|
<< "\n"
|
|
|
|
<< " nshared_objects: " << pe.nshared_objects << "\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i2 = 0; i2 < toS(pe.nshared_objects); ++i2) {
|
2023-05-21 17:35:09 +00:00
|
|
|
*m->log->getInfo() << " identifier " << i2 << ": " << pe.shared_identifiers.at(i2)
|
|
|
|
<< "\n";
|
|
|
|
*m->log->getInfo() << " numerator " << i2 << ": " << pe.shared_numerators.at(i2)
|
|
|
|
<< "\n";
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::dumpHSharedObject()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
HSharedObject& t = m->shared_object_hints;
|
|
|
|
*m->log->getInfo() << "first_shared_obj: " << t.first_shared_obj << "\n"
|
2023-05-21 17:35:09 +00:00
|
|
|
<< "first_shared_offset: " << adjusted_offset(t.first_shared_offset) << "\n"
|
2023-05-21 13:42:34 +00:00
|
|
|
<< "nshared_first_page: " << t.nshared_first_page << "\n"
|
|
|
|
<< "nshared_total: " << t.nshared_total << "\n"
|
|
|
|
<< "nbits_nobjects: " << t.nbits_nobjects << "\n"
|
|
|
|
<< "min_group_length: " << t.min_group_length << "\n"
|
2023-05-21 17:35:09 +00:00
|
|
|
<< "nbits_delta_group_length: " << t.nbits_delta_group_length << "\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < toS(t.nshared_total); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
HSharedObjectEntry& se = t.entries.at(i);
|
2023-05-21 13:42:34 +00:00
|
|
|
*m->log->getInfo() << "Shared Object " << i << ":\n"
|
2023-05-21 17:35:09 +00:00
|
|
|
<< " group length: " << se.delta_group_length + t.min_group_length
|
2023-05-21 13:42:34 +00:00
|
|
|
<< "\n";
|
2023-05-24 15:28:17 +00:00
|
|
|
// PDF spec says signature present nobjects_minus_one are always 0, so print them only if
|
|
|
|
// they have a non-zero value.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (se.signature_present) {
|
2023-05-21 13:42:34 +00:00
|
|
|
*m->log->getInfo() << " signature present\n";
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (se.nobjects_minus_one != 0) {
|
2023-05-21 17:35:09 +00:00
|
|
|
*m->log->getInfo() << " nobjects: " << se.nobjects_minus_one + 1 << "\n";
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::dumpHGeneric(HGeneric& t)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
*m->log->getInfo() << "first_object: " << t.first_object << "\n"
|
2023-05-21 17:35:09 +00:00
|
|
|
<< "first_object_offset: " << adjusted_offset(t.first_object_offset) << "\n"
|
2023-05-21 13:42:34 +00:00
|
|
|
<< "nobjects: " << t.nobjects << "\n"
|
|
|
|
<< "group_length: " << t.group_length << "\n";
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::calculateLinearizationData(std::map<int, int> const& object_stream_data)
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// This function calculates the ordering of objects, divides them into the appropriate parts,
|
|
|
|
// and computes some values for the linearization parameter dictionary and hint tables. The
|
|
|
|
// file must be optimized (via calling optimize()) prior to calling this function. Note that
|
|
|
|
// actual offsets and lengths are not computed here, but anything related to object ordering is.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->object_to_obj_users.empty()) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Note that we can't call optimize here because we don't know whether it should be called
|
|
|
|
// with or without allow changes.
|
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: QPDF::calculateLinearizationData called before optimize()");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Separate objects into the categories sufficient for us to determine which part of the
|
|
|
|
// linearized file should contain the object. This categorization is useful for other purposes
|
|
|
|
// as well. Part numbers refer to version 1.4 of the PDF spec.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Parts 1, 3, 5, 10, and 11 don't contain any objects from the original file (except the
|
|
|
|
// trailer dictionary in part 11).
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Part 4 is the document catalog (root) and the following root keys: /ViewerPreferences,
|
|
|
|
// /PageMode, /Threads, /OpenAction, /AcroForm, /Encrypt. Note that Thread information
|
|
|
|
// dictionaries are supposed to appear in part 9, but we are disregarding that recommendation
|
|
|
|
// for now.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Part 6 is the first page section. It includes all remaining objects referenced by the first
|
|
|
|
// page including shared objects but not including thumbnails. Additionally, if /PageMode is
|
2008-04-29 12:55:25 +00:00
|
|
|
// /Outlines, then information from /Outlines also appears here.
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Part 7 contains remaining objects private to pages other than the first page.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Part 8 contains all remaining shared objects except those that are shared only within
|
|
|
|
// thumbnails.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Part 9 contains all remaining objects.
|
|
|
|
|
|
|
|
// We sort objects into the following categories:
|
|
|
|
|
|
|
|
// * open_document: part 4
|
|
|
|
|
|
|
|
// * first_page_private: part 6
|
|
|
|
|
|
|
|
// * first_page_shared: part 6
|
|
|
|
|
|
|
|
// * other_page_private: part 7
|
|
|
|
|
|
|
|
// * other_page_shared: part 8
|
|
|
|
|
|
|
|
// * thumbnail_private: part 9
|
|
|
|
|
|
|
|
// * thumbnail_shared: part 9
|
|
|
|
|
|
|
|
// * other: part 9
|
|
|
|
|
|
|
|
// * outlines: part 6 or 9
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part4.clear();
|
|
|
|
m->part6.clear();
|
|
|
|
m->part7.clear();
|
|
|
|
m->part8.clear();
|
|
|
|
m->part9.clear();
|
|
|
|
m->c_linp = LinParameters();
|
|
|
|
m->c_page_offset_data = CHPageOffset();
|
|
|
|
m->c_shared_object_data = CHSharedObject();
|
|
|
|
m->c_outline_data = HGeneric();
|
2012-09-06 19:28:16 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle root = getRoot();
|
|
|
|
bool outlines_in_first_page = false;
|
|
|
|
QPDFObjectHandle pagemode = root.getKey("/PageMode");
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDF categorize pagemode present", pagemode.isName() ? 1 : 0);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pagemode.isName()) {
|
|
|
|
if (pagemode.getName() == "/UseOutlines") {
|
|
|
|
if (root.hasKey("/Outlines")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
outlines_in_first_page = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF UseOutlines but no Outlines");
|
|
|
|
}
|
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("qpdf", "QPDF categorize pagemode outlines", outlines_in_first_page ? 1 : 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string> open_document_keys;
|
|
|
|
open_document_keys.insert("/ViewerPreferences");
|
|
|
|
open_document_keys.insert("/PageMode");
|
|
|
|
open_document_keys.insert("/Threads");
|
|
|
|
open_document_keys.insert("/OpenAction");
|
|
|
|
open_document_keys.insert("/AcroForm");
|
|
|
|
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen> lc_open_document;
|
|
|
|
std::set<QPDFObjGen> lc_first_page_private;
|
|
|
|
std::set<QPDFObjGen> lc_first_page_shared;
|
|
|
|
std::set<QPDFObjGen> lc_other_page_private;
|
|
|
|
std::set<QPDFObjGen> lc_other_page_shared;
|
|
|
|
std::set<QPDFObjGen> lc_thumbnail_private;
|
|
|
|
std::set<QPDFObjGen> lc_thumbnail_shared;
|
|
|
|
std::set<QPDFObjGen> lc_other;
|
|
|
|
std::set<QPDFObjGen> lc_outlines;
|
|
|
|
std::set<QPDFObjGen> lc_root;
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto& oiter: m->object_to_obj_users) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjGen const& og = oiter.first;
|
|
|
|
std::set<ObjUser>& ous = oiter.second;
|
2022-02-08 14:18:08 +00:00
|
|
|
|
|
|
|
bool in_open_document = false;
|
|
|
|
bool in_first_page = false;
|
|
|
|
int other_pages = 0;
|
|
|
|
int thumbs = 0;
|
|
|
|
int others = 0;
|
|
|
|
bool in_outlines = false;
|
|
|
|
bool is_root = false;
|
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& ou: ous) {
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (ou.ou_type) {
|
|
|
|
case ObjUser::ou_trailer_key:
|
|
|
|
if (ou.key == "/Encrypt") {
|
2022-02-08 14:18:08 +00:00
|
|
|
in_open_document = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
++others;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ObjUser::ou_thumb:
|
2022-02-08 14:18:08 +00:00
|
|
|
++thumbs;
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ObjUser::ou_root_key:
|
|
|
|
if (open_document_keys.count(ou.key) > 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
in_open_document = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (ou.key == "/Outlines") {
|
2022-02-08 14:18:08 +00:00
|
|
|
in_outlines = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
++others;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ObjUser::ou_page:
|
|
|
|
if (ou.pageno == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
in_first_page = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
++other_pages;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ObjUser::ou_root:
|
2022-02-08 14:18:08 +00:00
|
|
|
is_root = true;
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ObjUser::ou_bad:
|
|
|
|
stopOnError("INTERNAL ERROR: QPDF::calculateLinearizationData: "
|
|
|
|
"invalid user type");
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (is_root) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_root.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (in_outlines) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_outlines.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (in_open_document) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_open_document.insert(og);
|
2023-05-21 17:35:09 +00:00
|
|
|
} else if ((in_first_page) && (others == 0) && (other_pages == 0) && (thumbs == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_first_page_private.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (in_first_page) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_first_page_shared.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((other_pages == 1) && (others == 0) && (thumbs == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_other_page_private.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (other_pages > 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_other_page_shared.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((thumbs == 1) && (others == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_thumbnail_private.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (thumbs > 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_thumbnail_shared.insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_other.insert(og);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Generate ordering for objects in the output file. Sometimes we just dump right from a set
|
|
|
|
// into a vector. Rather than optimizing this by going straight into the vector, we'll leave
|
|
|
|
// these phases separate for now. That way, this section can be concerned only with ordering,
|
|
|
|
// and the above section can be considered only with categorization. Note that sets of
|
|
|
|
// QPDFObjGens are sorted by QPDFObjGen. In a linearized file, objects appear in sequence with
|
|
|
|
// the possible exception of hints tables which we won't see here anyway. That means that
|
|
|
|
// running calculateLinearizationData() on a linearized file should give results identical to
|
|
|
|
// the original file ordering.
|
|
|
|
|
|
|
|
// We seem to traverse the page tree a lot in this code, but we can address this for a future
|
|
|
|
// code optimization if necessary. Premature optimization is the root of all evil.
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<QPDFObjectHandle> pages;
|
|
|
|
{ // local scope
|
2023-05-24 15:28:17 +00:00
|
|
|
// Map all page objects to the containing object stream. This should be a no-op in a
|
|
|
|
// properly linearized file.
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto oh: getAllPages()) {
|
|
|
|
pages.push_back(getUncompressedObject(oh, object_stream_data));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-06-21 03:35:23 +00:00
|
|
|
int npages = toI(pages.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// We will be initializing some values of the computed hint tables. Specifically, we can
|
|
|
|
// initialize any items that deal with object numbers or counts but not any items that deal with
|
|
|
|
// lengths or offsets. The code that writes linearized files will have to fill in these values
|
|
|
|
// during the first pass. The validation code can compute them relatively easily given the rest
|
|
|
|
// of the information.
|
|
|
|
|
|
|
|
// npages is the size of the existing pages vector, which has been created by traversing the
|
|
|
|
// pages tree, and as such is a reasonable size.
|
2023-05-21 13:42:34 +00:00
|
|
|
m->c_linp.npages = npages;
|
|
|
|
m->c_page_offset_data.entries = std::vector<CHPageOffsetEntry>(toS(npages));
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Part 4: open document objects. We don't care about the order.
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lc_root.size() != 1) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("found other than one root while"
|
|
|
|
" calculating linearization data");
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part4.push_back(getObject(*(lc_root.begin())));
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: lc_open_document) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part4.push_back(getObject(og));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Part 6: first page objects. Note: implementation note 124 states that Acrobat always treats
|
|
|
|
// page 0 as the first page for linearization regardless of /OpenAction. pdlin doesn't provide
|
|
|
|
// any option to set this and also disregards /OpenAction. We will do the same.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// First, place the actual first page object itself.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pages.empty()) {
|
2019-06-21 21:37:33 +00:00
|
|
|
stopOnError("no pages found while calculating linearization data");
|
|
|
|
}
|
2013-10-05 23:42:39 +00:00
|
|
|
QPDFObjGen first_page_og(pages.at(0).getObjGen());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!lc_first_page_private.count(first_page_og)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
stopOnError("INTERNAL ERROR: QPDF::calculateLinearizationData: first page "
|
|
|
|
"object not in lc_first_page_private");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
lc_first_page_private.erase(first_page_og);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->c_linp.first_page_object = pages.at(0).getObjectID();
|
|
|
|
m->part6.push_back(pages.at(0));
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// The PDF spec "recommends" an order for the rest of the objects, but we are going to disregard
|
|
|
|
// it except to the extent that it groups private and shared objects contiguously for the sake
|
|
|
|
// of hint tables.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: lc_first_page_private) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part6.push_back(getObject(og));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: lc_first_page_shared) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part6.push_back(getObject(og));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place the outline dictionary if it goes in the first page section.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (outlines_in_first_page) {
|
2023-05-21 13:42:34 +00:00
|
|
|
pushOutlinesToPart(m->part6, lc_outlines, object_stream_data);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Fill in page offset hint table information for the first page. The PDF spec says that
|
|
|
|
// nshared_objects should be zero for the first page. pdlin does not appear to obey this, but
|
|
|
|
// it fills in garbage values for all the shared object identifiers on the first page.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->c_page_offset_data.entries.at(0).nobjects = toI(m->part6.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Part 7: other pages' private objects
|
|
|
|
|
|
|
|
// For each page in order:
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 1; i < toS(npages); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Place this page's page object
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen page_og(pages.at(i).getObjGen());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!lc_other_page_private.count(page_og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
stopOnError(
|
|
|
|
"INTERNAL ERROR: "
|
|
|
|
"QPDF::calculateLinearizationData: page object for page " +
|
2022-09-21 16:49:21 +00:00
|
|
|
std::to_string(i) + " not in lc_other_page_private");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
lc_other_page_private.erase(page_og);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part7.push_back(pages.at(i));
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Place all non-shared objects referenced by this page, updating the page object count for
|
|
|
|
// the hint table.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->c_page_offset_data.entries.at(i).nobjects = 1;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
ObjUser ou(ObjUser::ou_page, toI(i));
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->obj_user_to_objects.count(ou) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("found unreferenced page while"
|
|
|
|
" calculating linearization data");
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& og: m->obj_user_to_objects[ou]) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lc_other_page_private.count(og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_other_page_private.erase(og);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part7.push_back(getObject(og));
|
|
|
|
++m->c_page_offset_data.entries.at(i).nobjects;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
// That should have covered all part7 objects.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!lc_other_page_private.empty()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
stopOnError("INTERNAL ERROR:"
|
|
|
|
" QPDF::calculateLinearizationData: lc_other_page_private is "
|
|
|
|
"not empty after generation of part7");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Part 8: other pages' shared objects
|
|
|
|
|
|
|
|
// Order is unimportant.
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: lc_other_page_shared) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part8.push_back(getObject(og));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Part 9: other objects
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// The PDF specification makes recommendations on ordering here. We follow them only to a
|
|
|
|
// limited extent. Specifically, we put the pages tree first, then private thumbnail objects in
|
|
|
|
// page order, then shared thumbnail objects, and then outlines (unless in part 6). After that,
|
|
|
|
// we throw all remaining objects in arbitrary order.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Place the pages tree.
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen> pages_ogs =
|
2023-05-21 13:42:34 +00:00
|
|
|
m->obj_user_to_objects[ObjUser(ObjUser::ou_root_key, "/Pages")];
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pages_ogs.empty()) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("found empty pages tree while"
|
|
|
|
" calculating linearization data");
|
|
|
|
}
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: pages_ogs) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lc_other.count(og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_other.erase(og);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part9.push_back(getObject(og));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Place private thumbnail images in page order. Slightly more information would be required if
|
|
|
|
// we were going to bother with thumbnail hint tables.
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(npages); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb");
|
|
|
|
thumb = getUncompressedObject(thumb, object_stream_data);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!thumb.isNull()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Output the thumbnail itself
|
|
|
|
QPDFObjGen thumb_og(thumb.getObjGen());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lc_thumbnail_private.count(thumb_og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_thumbnail_private.erase(thumb_og);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part9.push_back(thumb);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-24 15:28:17 +00:00
|
|
|
// No internal error this time...there's nothing to stop this object from having
|
|
|
|
// been referred to somewhere else outside of a page's /Thumb, and if it had been,
|
|
|
|
// there's nothing to prevent it from having been in some set other than
|
2022-02-08 14:18:08 +00:00
|
|
|
// lc_thumbnail_private.
|
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
std::set<QPDFObjGen>& ogs = m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, toI(i))];
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: ogs) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lc_thumbnail_private.count(og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lc_thumbnail_private.erase(og);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part9.push_back(getObject(og));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!lc_thumbnail_private.empty()) {
|
2023-05-24 15:28:17 +00:00
|
|
|
stopOnError("INTERNAL ERROR: QPDF::calculateLinearizationData: lc_thumbnail_private not "
|
|
|
|
"empty after placing thumbnails");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place shared thumbnail objects
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: lc_thumbnail_shared) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part9.push_back(getObject(og));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place outlines unless in first page
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!outlines_in_first_page) {
|
2023-05-21 13:42:34 +00:00
|
|
|
pushOutlinesToPart(m->part9, lc_outlines, object_stream_data);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place all remaining objects
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: lc_other) {
|
2023-05-21 13:42:34 +00:00
|
|
|
m->part9.push_back(getObject(og));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got everything exactly once.
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
size_t num_placed =
|
|
|
|
m->part4.size() + m->part6.size() + m->part7.size() + m->part8.size() + m->part9.size();
|
2023-05-21 13:42:34 +00:00
|
|
|
size_t num_wanted = m->object_to_obj_users.size();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (num_placed != num_wanted) {
|
2022-02-08 14:18:08 +00:00
|
|
|
stopOnError(
|
|
|
|
"INTERNAL ERROR: QPDF::calculateLinearizationData: wrong "
|
|
|
|
"number of objects placed (num_placed = " +
|
2023-05-21 17:35:09 +00:00
|
|
|
std::to_string(num_placed) + "; number of objects: " + std::to_string(num_wanted));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Calculate shared object hint table information including references to shared objects from
|
|
|
|
// page offset hint data.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// The shared object hint table consists of all part 6 (whether shared or not) in order followed
|
|
|
|
// by all part 8 objects in order. Add the objects to shared object data keeping a map of
|
|
|
|
// object number to index. Then populate the shared object information for the pages.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Note that two objects never have the same object number, so we can map from object number
|
|
|
|
// only without regards to generation.
|
2008-04-29 12:55:25 +00:00
|
|
|
std::map<int, int> obj_to_index;
|
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
m->c_shared_object_data.nshared_first_page = toI(m->part6.size());
|
|
|
|
m->c_shared_object_data.nshared_total =
|
|
|
|
m->c_shared_object_data.nshared_first_page + toI(m->part8.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
std::vector<CHSharedObjectEntry>& shared = m->c_shared_object_data.entries;
|
|
|
|
for (auto& oh: m->part6) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int obj = oh.getObjectID();
|
|
|
|
obj_to_index[obj] = toI(shared.size());
|
2023-05-27 22:49:18 +00:00
|
|
|
shared.emplace_back(obj);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
QTC::TC("qpdf", "QPDF lin part 8 empty", m->part8.empty() ? 1 : 0);
|
|
|
|
if (!m->part8.empty()) {
|
|
|
|
m->c_shared_object_data.first_shared_obj = m->part8.at(0).getObjectID();
|
|
|
|
for (auto& oh: m->part8) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int obj = oh.getObjectID();
|
|
|
|
obj_to_index[obj] = toI(shared.size());
|
2023-05-27 22:49:18 +00:00
|
|
|
shared.emplace_back(obj);
|
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 (static_cast<size_t>(m->c_shared_object_data.nshared_total) !=
|
|
|
|
m->c_shared_object_data.entries.size()) {
|
2022-04-02 21:14:10 +00:00
|
|
|
stopOnError("shared object hint table has wrong number of entries");
|
2013-10-05 20:37:27 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Now compute the list of shared objects for each page after the first page.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 1; i < toS(npages); ++i) {
|
2023-05-21 13:42:34 +00:00
|
|
|
CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i);
|
2022-02-08 14:18:08 +00:00
|
|
|
ObjUser ou(ObjUser::ou_page, toI(i));
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->obj_user_to_objects.count(ou) == 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("found unreferenced page while"
|
|
|
|
" calculating linearization data");
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
for (auto const& og: m->obj_user_to_objects[ou]) {
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((m->object_to_obj_users[og].size() > 1) && (obj_to_index.count(og.getObj()) > 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int idx = obj_to_index[og.getObj()];
|
|
|
|
++pe.nshared_objects;
|
|
|
|
pe.shared_identifiers.push_back(idx);
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::pushOutlinesToPart(
|
|
|
|
std::vector<QPDFObjectHandle>& part,
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen>& lc_outlines,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::map<int, int> const& object_stream_data)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle root = getRoot();
|
|
|
|
QPDFObjectHandle outlines = root.getKey("/Outlines");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (outlines.isNull()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
outlines = getUncompressedObject(outlines, object_stream_data);
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen outlines_og(outlines.getObjGen());
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDF lin outlines in part",
|
2023-05-21 13:42:34 +00:00
|
|
|
((&part == (&m->part6)) ? 0
|
|
|
|
: (&part == (&m->part9)) ? 1
|
|
|
|
: 9999)); // can't happen
|
|
|
|
m->c_outline_data.first_object = outlines_og.getObj();
|
|
|
|
m->c_outline_data.nobjects = 1;
|
2008-04-29 12:55:25 +00:00
|
|
|
lc_outlines.erase(outlines_og);
|
|
|
|
part.push_back(outlines);
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& og: lc_outlines) {
|
2022-08-01 18:22:37 +00:00
|
|
|
part.push_back(getObject(og));
|
2023-05-21 13:42:34 +00:00
|
|
|
++m->c_outline_data.nobjects;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::getLinearizedParts(
|
|
|
|
std::map<int, int> const& object_stream_data,
|
|
|
|
std::vector<QPDFObjectHandle>& part4,
|
|
|
|
std::vector<QPDFObjectHandle>& part6,
|
|
|
|
std::vector<QPDFObjectHandle>& part7,
|
|
|
|
std::vector<QPDFObjectHandle>& part8,
|
|
|
|
std::vector<QPDFObjectHandle>& part9)
|
|
|
|
{
|
|
|
|
calculateLinearizationData(object_stream_data);
|
2023-05-21 13:42:34 +00:00
|
|
|
part4 = m->part4;
|
|
|
|
part6 = m->part6;
|
|
|
|
part7 = m->part7;
|
|
|
|
part8 = m->part8;
|
|
|
|
part9 = m->part9;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static inline int
|
|
|
|
nbits(int val)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
return (val == 0 ? 0 : (1 + nbits(val >> 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
QPDF::outputLengthNextN(
|
2022-04-02 21:14:10 +00:00
|
|
|
int in_object,
|
|
|
|
int n,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2024-02-19 12:05:55 +00:00
|
|
|
QPDFWriter::ObjTable const& obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// Figure out the length of a series of n consecutive objects in the output file starting with
|
|
|
|
// whatever object in_object from the input file mapped to.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2024-02-19 12:05:55 +00:00
|
|
|
int first = obj[in_object].renumber;
|
|
|
|
if (first <= 0) {
|
2023-05-24 15:28:17 +00:00
|
|
|
stopOnError("found object that is not renumbered while writing linearization data");
|
2019-01-04 16:50:02 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
int length = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
if (lengths.count(first + i) == 0) {
|
2023-05-24 15:28:17 +00:00
|
|
|
stopOnError("found item with unknown length while writing linearization data");
|
2019-01-04 16:50:02 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
length += toI((*(lengths.find(first + toI(i)))).second);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::calculateHPageOffset(
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2024-02-19 12:05:55 +00:00
|
|
|
QPDFWriter::ObjTable const& obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Page Offset Hint Table
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// We are purposely leaving some values set to their initial zero values.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::vector<QPDFObjectHandle> const& pages = getAllPages();
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t npages = pages.size();
|
2023-05-21 13:42:34 +00:00
|
|
|
CHPageOffset& cph = m->c_page_offset_data;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<CHPageOffsetEntry>& cphe = cph.entries;
|
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Calculate minimum and maximum values for number of objects per page and page length.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2013-10-05 23:42:39 +00:00
|
|
|
int min_nobjects = cphe.at(0).nobjects;
|
2008-04-29 12:55:25 +00:00
|
|
|
int max_nobjects = min_nobjects;
|
2024-02-19 12:05:55 +00:00
|
|
|
int min_length = outputLengthNextN(pages.at(0).getObjectID(), min_nobjects, lengths, obj);
|
2008-04-29 12:55:25 +00:00
|
|
|
int max_length = min_length;
|
2013-10-05 23:42:39 +00:00
|
|
|
int max_shared = cphe.at(0).nshared_objects;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-21 13:42:34 +00:00
|
|
|
HPageOffset& ph = m->page_offset_hints;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<HPageOffsetEntry>& phe = ph.entries;
|
2013-10-05 15:30:27 +00:00
|
|
|
// npages is the size of the existing pages array.
|
2008-04-29 12:55:25 +00:00
|
|
|
phe = std::vector<HPageOffsetEntry>(npages);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (unsigned int i = 0; i < npages; ++i) {
|
2023-05-24 15:28:17 +00:00
|
|
|
// Calculate values for each page, assigning full values to the delta items. They will be
|
|
|
|
// adjusted later.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Repeat calculations for page 0 so we can assign to phe[i] without duplicating those
|
|
|
|
// assignments.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
int nobjects = cphe.at(i).nobjects;
|
2024-02-19 12:05:55 +00:00
|
|
|
int length = outputLengthNextN(pages.at(i).getObjectID(), nobjects, lengths, obj);
|
2022-02-08 14:18:08 +00:00
|
|
|
int nshared = cphe.at(i).nshared_objects;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
min_nobjects = std::min(min_nobjects, nobjects);
|
|
|
|
max_nobjects = std::max(max_nobjects, nobjects);
|
|
|
|
min_length = std::min(min_length, length);
|
|
|
|
max_length = std::max(max_length, length);
|
|
|
|
max_shared = std::max(max_shared, nshared);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
phe.at(i).delta_nobjects = nobjects;
|
|
|
|
phe.at(i).delta_page_length = length;
|
|
|
|
phe.at(i).nshared_objects = nshared;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ph.min_nobjects = min_nobjects;
|
2024-02-19 12:05:55 +00:00
|
|
|
int out_page0_id = obj[pages.at(0)].renumber;
|
2008-04-29 12:55:25 +00:00
|
|
|
ph.first_page_offset = (*(xref.find(out_page0_id))).second.getOffset();
|
|
|
|
ph.nbits_delta_nobjects = nbits(max_nobjects - min_nobjects);
|
|
|
|
ph.min_page_length = min_length;
|
|
|
|
ph.nbits_delta_page_length = nbits(max_length - min_length);
|
|
|
|
ph.nbits_nshared_objects = nbits(max_shared);
|
2023-05-21 13:42:34 +00:00
|
|
|
ph.nbits_shared_identifier = nbits(m->c_shared_object_data.nshared_total);
|
2022-04-02 21:14:10 +00:00
|
|
|
ph.shared_denominator = 4; // doesn't matter
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// It isn't clear how to compute content offset and content length. Since we are not
|
|
|
|
// interleaving page objects with the content stream, we'll use the same values for content
|
|
|
|
// length as page length. We will use 0 as content offset because this is what Adobe does
|
|
|
|
// (implementation note 127) and pdlin as well.
|
2008-04-29 12:55:25 +00:00
|
|
|
ph.nbits_delta_content_length = ph.nbits_delta_page_length;
|
|
|
|
ph.min_content_length = ph.min_page_length;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < npages; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Adjust delta entries
|
|
|
|
if ((phe.at(i).delta_nobjects < min_nobjects) ||
|
2022-04-02 21:14:10 +00:00
|
|
|
(phe.at(i).delta_page_length < min_length)) {
|
2023-05-24 15:28:17 +00:00
|
|
|
stopOnError("found too small delta nobjects or delta page length while writing "
|
|
|
|
"linearization data");
|
2019-01-04 16:50:02 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
phe.at(i).delta_nobjects -= min_nobjects;
|
|
|
|
phe.at(i).delta_page_length -= min_length;
|
|
|
|
phe.at(i).delta_content_length = phe.at(i).delta_page_length;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t j = 0; j < toS(cphe.at(i).nshared_objects); ++j) {
|
2023-05-21 17:35:09 +00:00
|
|
|
phe.at(i).shared_identifiers.push_back(cphe.at(i).shared_identifiers.at(j));
|
2022-02-08 14:18:08 +00:00
|
|
|
phe.at(i).shared_numerators.push_back(0);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::calculateHSharedObject(
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2024-02-19 12:05:55 +00:00
|
|
|
QPDFWriter::ObjTable const& obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
CHSharedObject& cso = m->c_shared_object_data;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<CHSharedObjectEntry>& csoe = cso.entries;
|
2023-05-21 13:42:34 +00:00
|
|
|
HSharedObject& so = m->shared_object_hints;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<HSharedObjectEntry>& soe = so.entries;
|
2013-10-05 15:30:27 +00:00
|
|
|
soe.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2024-02-19 12:05:55 +00:00
|
|
|
int min_length = outputLengthNextN(csoe.at(0).object, 1, lengths, obj);
|
2008-04-29 12:55:25 +00:00
|
|
|
int max_length = min_length;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(cso.nshared_total); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Assign absolute numbers to deltas; adjust later
|
2024-02-19 12:05:55 +00:00
|
|
|
int length = outputLengthNextN(csoe.at(i).object, 1, lengths, obj);
|
2022-02-08 14:18:08 +00:00
|
|
|
min_length = std::min(min_length, length);
|
|
|
|
max_length = std::max(max_length, length);
|
2023-05-27 22:49:18 +00:00
|
|
|
soe.emplace_back();
|
2022-02-08 14:18:08 +00:00
|
|
|
soe.at(i).delta_group_length = length;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-07-13 13:24:19 +00:00
|
|
|
if (soe.size() != toS(cso.nshared_total)) {
|
2019-06-21 21:37:33 +00:00
|
|
|
stopOnError("soe has wrong size after initialization");
|
2013-10-05 15:30:27 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
so.nshared_total = cso.nshared_total;
|
|
|
|
so.nshared_first_page = cso.nshared_first_page;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (so.nshared_total > so.nshared_first_page) {
|
2024-02-19 12:05:55 +00:00
|
|
|
so.first_shared_obj = obj[cso.first_shared_obj].renumber;
|
2023-05-21 17:35:09 +00:00
|
|
|
so.first_shared_offset = (*(xref.find(so.first_shared_obj))).second.getOffset();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
so.min_group_length = min_length;
|
|
|
|
so.nbits_delta_group_length = nbits(max_length - min_length);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(cso.nshared_total); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Adjust deltas
|
2022-04-02 21:14:10 +00:00
|
|
|
if (soe.at(i).delta_group_length < min_length) {
|
2023-05-24 15:28:17 +00:00
|
|
|
stopOnError("found too small group length while writing linearization data");
|
2019-01-04 16:50:02 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
soe.at(i).delta_group_length -= min_length;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::calculateHOutline(
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2024-02-19 12:05:55 +00:00
|
|
|
QPDFWriter::ObjTable const& obj)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
HGeneric& cho = m->c_outline_data;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cho.nobjects == 0) {
|
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
|
|
|
HGeneric& ho = m->outline_hints;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2024-02-19 12:05:55 +00:00
|
|
|
ho.first_object = obj[cho.first_object].renumber;
|
2022-04-02 21:14:10 +00:00
|
|
|
ho.first_object_offset = (*(xref.find(ho.first_object))).second.getOffset();
|
2008-04-29 12:55:25 +00:00
|
|
|
ho.nobjects = cho.nobjects;
|
2024-02-19 12:05:55 +00:00
|
|
|
ho.group_length = outputLengthNextN(cho.first_object, ho.nobjects, lengths, obj);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
template <class T, class int_type>
|
2008-04-29 12:55:25 +00:00
|
|
|
static void
|
2023-05-21 17:35:09 +00:00
|
|
|
write_vector_int(BitWriter& w, int nitems, std::vector<T>& vec, int bits, int_type T::*field)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// nitems times, write bits bits from the given field of the ith vector to the given bit writer.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < QIntC::to_size(nitems); ++i) {
|
2023-05-21 17:35:09 +00:00
|
|
|
w.writeBits(QIntC::to_ulonglong(vec.at(i).*field), QIntC::to_size(bits));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-24 15:28:17 +00:00
|
|
|
// The PDF spec says that each hint table starts at a byte boundary. Each "row" actually must
|
|
|
|
// start on a byte boundary.
|
2008-04-29 12:55:25 +00:00
|
|
|
w.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static void
|
2022-04-02 21:14:10 +00:00
|
|
|
write_vector_vector(
|
|
|
|
BitWriter& w,
|
|
|
|
int nitems1,
|
|
|
|
std::vector<T>& vec1,
|
|
|
|
int T::*nitems2,
|
|
|
|
int bits,
|
|
|
|
std::vector<int> T::*vec2)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// nitems1 times, write nitems2 (from the ith element of vec1) items from the vec2 vector field
|
|
|
|
// of the ith item of vec1.
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i1 = 0; i1 < QIntC::to_size(nitems1); ++i1) {
|
|
|
|
for (size_t i2 = 0; i2 < QIntC::to_size(vec1.at(i1).*nitems2); ++i2) {
|
2023-05-21 17:35:09 +00:00
|
|
|
w.writeBits(QIntC::to_ulonglong((vec1.at(i1).*vec2).at(i2)), QIntC::to_size(bits));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
w.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::writeHPageOffset(BitWriter& w)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
HPageOffset& t = m->page_offset_hints;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
w.writeBitsInt(t.min_nobjects, 32); // 1
|
2023-10-14 22:03:34 +00:00
|
|
|
w.writeBits(toULL(t.first_page_offset), 32); // 2
|
2022-04-02 21:14:10 +00:00
|
|
|
w.writeBitsInt(t.nbits_delta_nobjects, 16); // 3
|
|
|
|
w.writeBitsInt(t.min_page_length, 32); // 4
|
|
|
|
w.writeBitsInt(t.nbits_delta_page_length, 16); // 5
|
2023-10-14 22:03:34 +00:00
|
|
|
w.writeBits(toULL(t.min_content_offset), 32); // 6
|
2022-04-02 21:14:10 +00:00
|
|
|
w.writeBitsInt(t.nbits_delta_content_offset, 16); // 7
|
|
|
|
w.writeBitsInt(t.min_content_length, 32); // 8
|
|
|
|
w.writeBitsInt(t.nbits_delta_content_length, 16); // 9
|
|
|
|
w.writeBitsInt(t.nbits_nshared_objects, 16); // 10
|
|
|
|
w.writeBitsInt(t.nbits_shared_identifier, 16); // 11
|
|
|
|
w.writeBitsInt(t.nbits_shared_numerator, 16); // 12
|
|
|
|
w.writeBitsInt(t.shared_denominator, 16); // 13
|
2019-06-21 03:35:23 +00:00
|
|
|
|
|
|
|
int nitems = toI(getAllPages().size());
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<HPageOffsetEntry>& entries = t.entries;
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
write_vector_int(w, nitems, entries, t.nbits_delta_nobjects, &HPageOffsetEntry::delta_nobjects);
|
2022-04-02 21:14:10 +00:00
|
|
|
write_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
w, nitems, entries, t.nbits_delta_page_length, &HPageOffsetEntry::delta_page_length);
|
2022-04-02 21:14:10 +00:00
|
|
|
write_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
w, nitems, entries, t.nbits_nshared_objects, &HPageOffsetEntry::nshared_objects);
|
2022-04-02 21:14:10 +00:00
|
|
|
write_vector_vector(
|
|
|
|
w,
|
|
|
|
nitems,
|
|
|
|
entries,
|
|
|
|
&HPageOffsetEntry::nshared_objects,
|
|
|
|
t.nbits_shared_identifier,
|
|
|
|
&HPageOffsetEntry::shared_identifiers);
|
|
|
|
write_vector_vector(
|
|
|
|
w,
|
|
|
|
nitems,
|
|
|
|
entries,
|
|
|
|
&HPageOffsetEntry::nshared_objects,
|
|
|
|
t.nbits_shared_numerator,
|
|
|
|
&HPageOffsetEntry::shared_numerators);
|
|
|
|
write_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
w, nitems, entries, t.nbits_delta_content_offset, &HPageOffsetEntry::delta_content_offset);
|
2022-04-02 21:14:10 +00:00
|
|
|
write_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
w, nitems, entries, t.nbits_delta_content_length, &HPageOffsetEntry::delta_content_length);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::writeHSharedObject(BitWriter& w)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
HSharedObject& t = m->shared_object_hints;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
w.writeBitsInt(t.first_shared_obj, 32); // 1
|
2023-10-14 22:03:34 +00:00
|
|
|
w.writeBits(toULL(t.first_shared_offset), 32); // 2
|
2022-04-02 21:14:10 +00:00
|
|
|
w.writeBitsInt(t.nshared_first_page, 32); // 3
|
|
|
|
w.writeBitsInt(t.nshared_total, 32); // 4
|
|
|
|
w.writeBitsInt(t.nbits_nobjects, 16); // 5
|
|
|
|
w.writeBitsInt(t.min_group_length, 32); // 6
|
|
|
|
w.writeBitsInt(t.nbits_delta_group_length, 16); // 7
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDF lin write nshared_total > nshared_first_page",
|
|
|
|
(t.nshared_total > t.nshared_first_page) ? 1 : 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
int nitems = t.nshared_total;
|
|
|
|
std::vector<HSharedObjectEntry>& entries = t.entries;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
write_vector_int(
|
2023-05-21 17:35:09 +00:00
|
|
|
w, nitems, entries, t.nbits_delta_group_length, &HSharedObjectEntry::delta_group_length);
|
|
|
|
write_vector_int(w, nitems, entries, 1, &HSharedObjectEntry::signature_present);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < toS(nitems); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// If signature were present, we'd have to write a 128-bit hash.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (entries.at(i).signature_present != 0) {
|
2019-01-04 16:50:02 +00:00
|
|
|
stopOnError("found unexpected signature present"
|
|
|
|
" while writing linearization data");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
write_vector_int(w, nitems, entries, t.nbits_nobjects, &HSharedObjectEntry::nobjects_minus_one);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::writeHGeneric(BitWriter& w, HGeneric& t)
|
|
|
|
{
|
2023-12-20 16:08:36 +00:00
|
|
|
w.writeBitsInt(t.first_object, 32); // 1
|
|
|
|
w.writeBits(toULL(t.first_object_offset), 32); // 2
|
|
|
|
w.writeBitsInt(t.nobjects, 32); // 3
|
|
|
|
w.writeBitsInt(t.group_length, 32); // 4
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::generateHintStream(
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2024-02-19 12:05:55 +00:00
|
|
|
QPDFWriter::ObjTable const& obj,
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer>& hint_buffer,
|
2022-04-02 21:14:10 +00:00
|
|
|
int& S,
|
2023-12-20 16:08:36 +00:00
|
|
|
int& O,
|
|
|
|
bool compressed)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Populate actual hint table values
|
2024-02-19 12:05:55 +00:00
|
|
|
calculateHPageOffset(xref, lengths, obj);
|
|
|
|
calculateHSharedObject(xref, lengths, obj);
|
|
|
|
calculateHOutline(xref, lengths, obj);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-24 15:28:17 +00:00
|
|
|
// Write the hint stream itself into a compressed memory buffer. Write through a counter so we
|
|
|
|
// can get offsets.
|
2008-04-29 12:55:25 +00:00
|
|
|
Pl_Buffer hint_stream("hint stream");
|
2023-12-20 16:08:36 +00:00
|
|
|
Pipeline* next = &hint_stream;
|
|
|
|
std::shared_ptr<Pipeline> flate;
|
|
|
|
if (compressed) {
|
|
|
|
flate =
|
|
|
|
std::make_shared<Pl_Flate>("compress hint stream", &hint_stream, Pl_Flate::a_deflate);
|
|
|
|
next = flate.get();
|
|
|
|
}
|
|
|
|
Pl_Count c("count", next);
|
2008-04-29 12:55:25 +00:00
|
|
|
BitWriter w(&c);
|
|
|
|
|
|
|
|
writeHPageOffset(w);
|
2019-06-21 03:35:23 +00:00
|
|
|
S = toI(c.getCount());
|
2008-04-29 12:55:25 +00:00
|
|
|
writeHSharedObject(w);
|
|
|
|
O = 0;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->outline_hints.nobjects > 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
O = toI(c.getCount());
|
2023-05-21 13:42:34 +00:00
|
|
|
writeHGeneric(w, m->outline_hints);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
c.finish();
|
|
|
|
|
2022-02-06 16:40:24 +00:00
|
|
|
hint_buffer = hint_stream.getBufferSharedPointer();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|