aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFObjectHandle.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2020-10-23 12:40:27 +0200
committerJay Berkenbilt <ejb@ql.org>2020-10-23 14:00:04 +0200
commitb30deaeeaba3941d7615bc2cc89c664b1273e5df (patch)
treea8dd4f2bec49e4a3e149f263a43d0dea2e8f9dc4 /libqpdf/QPDFObjectHandle.cc
parent0dea2769978d52e56e270fdc5ca868e0f2223cb0 (diff)
downloadqpdf-b30deaeeaba3941d7615bc2cc89c664b1273e5df.tar.zst
Avoid merging adjacent tokens when concatenating contents (fixes #444)
Diffstat (limited to 'libqpdf/QPDFObjectHandle.cc')
-rw-r--r--libqpdf/QPDFObjectHandle.cc53
1 files changed, 52 insertions, 1 deletions
diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc
index 85493680..472ff4e8 100644
--- a/libqpdf/QPDFObjectHandle.cc
+++ b/libqpdf/QPDFObjectHandle.cc
@@ -165,6 +165,47 @@ QPDFObjectHandle::ParserCallbacks::terminateParsing()
throw TerminateParsing();
}
+class LastChar: public Pipeline
+{
+ public:
+ LastChar(Pipeline* next);
+ virtual ~LastChar() = default;
+ virtual void write(unsigned char* data, size_t len);
+ virtual void finish();
+ unsigned char getLastChar();
+
+ private:
+ unsigned char last_char;
+};
+
+LastChar::LastChar(Pipeline* next) :
+ Pipeline("lastchar", next),
+ last_char(0)
+{
+}
+
+void
+LastChar::write(unsigned char* data, size_t len)
+{
+ if (len > 0)
+ {
+ this->last_char = data[len - 1];
+ }
+ getNext()->write(data, len);
+}
+
+void
+LastChar::finish()
+{
+ getNext()->finish();
+}
+
+unsigned char
+LastChar::getLastChar()
+{
+ return this->last_char;
+}
+
QPDFObjectHandle::QPDFObjectHandle() :
initialized(false),
qpdf(0),
@@ -1600,21 +1641,31 @@ QPDFObjectHandle::pipeContentStreams(
std::vector<QPDFObjectHandle> streams =
arrayOrStreamToStreamArray(
description, all_description);
+ bool need_newline = false;
for (std::vector<QPDFObjectHandle>::iterator iter = streams.begin();
iter != streams.end(); ++iter)
{
+ if (need_newline)
+ {
+ p->write(QUtil::unsigned_char_pointer("\n"), 1);
+ }
+ LastChar lc(p);
QPDFObjectHandle stream = *iter;
std::string og =
QUtil::int_to_string(stream.getObjectID()) + " " +
QUtil::int_to_string(stream.getGeneration());
std::string w_description = "content stream object " + og;
- if (! stream.pipeStreamData(p, 0, qpdf_dl_specialized))
+ if (! stream.pipeStreamData(&lc, 0, qpdf_dl_specialized))
{
QTC::TC("qpdf", "QPDFObjectHandle errors in parsecontent");
throw QPDFExc(qpdf_e_damaged_pdf, "content stream",
w_description, 0,
"errors while decoding content stream");
}
+ lc.finish();
+ need_newline = (lc.getLastChar() != static_cast<unsigned char>('\n'));
+ QTC::TC("qpdf", "QPDFObjectHandle need_newline",
+ need_newline ? 0 : 1);
}
}