Commit efc70b475317ac1264fa5dbce5d45c4c167bc488
1 parent
e0f1be5a
In QPDFObjectHandle.cc remove `LastChar` class and refactor content stream proce…
…ssing to simplify buffer handling
Showing
2 changed files
with
18 additions
and
56 deletions
libqpdf/QPDFObjectHandle.cc
| 1 | +#include <qpdf/assert_debug.h> | |
| 2 | + | |
| 1 | 3 | #include <qpdf/QPDFObjectHandle_private.hh> |
| 2 | 4 | |
| 3 | -#include <qpdf/BufferInputSource.hh> | |
| 5 | +#include <qpdf/InputSource_private.hh> | |
| 4 | 6 | #include <qpdf/JSON_writer.hh> |
| 7 | +#include <qpdf/Pipeline_private.hh> | |
| 5 | 8 | #include <qpdf/Pl_Buffer.hh> |
| 6 | 9 | #include <qpdf/Pl_QPDFTokenizer.hh> |
| 10 | +#include <qpdf/QIntC.hh> | |
| 7 | 11 | #include <qpdf/QPDF.hh> |
| 8 | 12 | #include <qpdf/QPDFExc.hh> |
| 9 | 13 | #include <qpdf/QPDFLogger.hh> |
| ... | ... | @@ -11,13 +15,9 @@ |
| 11 | 15 | #include <qpdf/QPDFObject_private.hh> |
| 12 | 16 | #include <qpdf/QPDFPageObjectHelper.hh> |
| 13 | 17 | #include <qpdf/QPDFParser.hh> |
| 14 | - | |
| 15 | -#include <qpdf/QIntC.hh> | |
| 16 | 18 | #include <qpdf/QTC.hh> |
| 17 | -#include <qpdf/QUtil.hh> | |
| 18 | 19 | #include <qpdf/Util.hh> |
| 19 | 20 | |
| 20 | -#include <algorithm> | |
| 21 | 21 | #include <array> |
| 22 | 22 | #include <cctype> |
| 23 | 23 | #include <climits> |
| ... | ... | @@ -174,48 +174,6 @@ QPDFObjectHandle::ParserCallbacks::terminateParsing() |
| 174 | 174 | throw TerminateParsing(); |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | -namespace | |
| 178 | -{ | |
| 179 | - class LastChar final: public Pipeline | |
| 180 | - { | |
| 181 | - public: | |
| 182 | - LastChar(Pipeline& next); | |
| 183 | - ~LastChar() final = default; | |
| 184 | - void write(unsigned char const* data, size_t len) final; | |
| 185 | - void finish() final; | |
| 186 | - unsigned char getLastChar(); | |
| 187 | - | |
| 188 | - private: | |
| 189 | - unsigned char last_char{0}; | |
| 190 | - }; | |
| 191 | -} // namespace | |
| 192 | - | |
| 193 | -LastChar::LastChar(Pipeline& next) : | |
| 194 | - Pipeline("lastchar", &next) | |
| 195 | -{ | |
| 196 | -} | |
| 197 | - | |
| 198 | -void | |
| 199 | -LastChar::write(unsigned char const* data, size_t len) | |
| 200 | -{ | |
| 201 | - if (len > 0) { | |
| 202 | - last_char = data[len - 1]; | |
| 203 | - } | |
| 204 | - next()->write(data, len); | |
| 205 | -} | |
| 206 | - | |
| 207 | -void | |
| 208 | -LastChar::finish() | |
| 209 | -{ | |
| 210 | - next()->finish(); | |
| 211 | -} | |
| 212 | - | |
| 213 | -unsigned char | |
| 214 | -LastChar::getLastChar() | |
| 215 | -{ | |
| 216 | - return last_char; | |
| 217 | -} | |
| 218 | - | |
| 219 | 177 | std::pair<bool, bool> |
| 220 | 178 | Name::analyzeJSONEncoding(const std::string& name) |
| 221 | 179 | { |
| ... | ... | @@ -1527,16 +1485,14 @@ void |
| 1527 | 1485 | QPDFObjectHandle::pipeContentStreams( |
| 1528 | 1486 | Pipeline* p, std::string const& description, std::string& all_description) |
| 1529 | 1487 | { |
| 1530 | - std::vector<QPDFObjectHandle> streams = | |
| 1531 | - arrayOrStreamToStreamArray(description, all_description); | |
| 1532 | 1488 | bool need_newline = false; |
| 1533 | - Pl_Buffer buf("concatenated content stream buffer"); | |
| 1534 | - for (auto stream: streams) { | |
| 1489 | + std::string buffer; | |
| 1490 | + pl::String buf(buffer); | |
| 1491 | + for (auto stream: arrayOrStreamToStreamArray(description, all_description)) { | |
| 1535 | 1492 | if (need_newline) { |
| 1536 | 1493 | buf.writeCStr("\n"); |
| 1537 | 1494 | } |
| 1538 | - LastChar lc(buf); | |
| 1539 | - if (!stream.pipeStreamData(&lc, 0, qpdf_dl_specialized)) { | |
| 1495 | + if (!stream.pipeStreamData(&buf, 0, qpdf_dl_specialized)) { | |
| 1540 | 1496 | QTC::TC("qpdf", "QPDFObjectHandle errors in parsecontent"); |
| 1541 | 1497 | throw QPDFExc( |
| 1542 | 1498 | qpdf_e_damaged_pdf, |
| ... | ... | @@ -1545,11 +1501,11 @@ QPDFObjectHandle::pipeContentStreams( |
| 1545 | 1501 | 0, |
| 1546 | 1502 | "errors while decoding content stream"); |
| 1547 | 1503 | } |
| 1548 | - lc.finish(); | |
| 1549 | - need_newline = (lc.getLastChar() != static_cast<unsigned char>('\n')); | |
| 1504 | + need_newline = buffer.empty() || buffer.back() != '\n'; | |
| 1550 | 1505 | QTC::TC("qpdf", "QPDFObjectHandle need_newline", need_newline ? 0 : 1); |
| 1506 | + p->writeString(buffer); | |
| 1507 | + buffer.clear(); | |
| 1551 | 1508 | } |
| 1552 | - p->writeString(buf.getString()); | |
| 1553 | 1509 | p->finish(); |
| 1554 | 1510 | } |
| 1555 | 1511 | ... | ... |