Commit efc70b475317ac1264fa5dbce5d45c4c167bc488

Authored by m-holger
1 parent e0f1be5a

In QPDFObjectHandle.cc remove `LastChar` class and refactor content stream proce…

…ssing to simplify buffer handling
libqpdf/QPDFObjectHandle.cc
  1 +#include <qpdf/assert_debug.h>
  2 +
1 #include <qpdf/QPDFObjectHandle_private.hh> 3 #include <qpdf/QPDFObjectHandle_private.hh>
2 4
3 -#include <qpdf/BufferInputSource.hh> 5 +#include <qpdf/InputSource_private.hh>
4 #include <qpdf/JSON_writer.hh> 6 #include <qpdf/JSON_writer.hh>
  7 +#include <qpdf/Pipeline_private.hh>
5 #include <qpdf/Pl_Buffer.hh> 8 #include <qpdf/Pl_Buffer.hh>
6 #include <qpdf/Pl_QPDFTokenizer.hh> 9 #include <qpdf/Pl_QPDFTokenizer.hh>
  10 +#include <qpdf/QIntC.hh>
7 #include <qpdf/QPDF.hh> 11 #include <qpdf/QPDF.hh>
8 #include <qpdf/QPDFExc.hh> 12 #include <qpdf/QPDFExc.hh>
9 #include <qpdf/QPDFLogger.hh> 13 #include <qpdf/QPDFLogger.hh>
@@ -11,13 +15,9 @@ @@ -11,13 +15,9 @@
11 #include <qpdf/QPDFObject_private.hh> 15 #include <qpdf/QPDFObject_private.hh>
12 #include <qpdf/QPDFPageObjectHelper.hh> 16 #include <qpdf/QPDFPageObjectHelper.hh>
13 #include <qpdf/QPDFParser.hh> 17 #include <qpdf/QPDFParser.hh>
14 -  
15 -#include <qpdf/QIntC.hh>  
16 #include <qpdf/QTC.hh> 18 #include <qpdf/QTC.hh>
17 -#include <qpdf/QUtil.hh>  
18 #include <qpdf/Util.hh> 19 #include <qpdf/Util.hh>
19 20
20 -#include <algorithm>  
21 #include <array> 21 #include <array>
22 #include <cctype> 22 #include <cctype>
23 #include <climits> 23 #include <climits>
@@ -174,48 +174,6 @@ QPDFObjectHandle::ParserCallbacks::terminateParsing() @@ -174,48 +174,6 @@ QPDFObjectHandle::ParserCallbacks::terminateParsing()
174 throw TerminateParsing(); 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 std::pair<bool, bool> 177 std::pair<bool, bool>
220 Name::analyzeJSONEncoding(const std::string& name) 178 Name::analyzeJSONEncoding(const std::string& name)
221 { 179 {
@@ -1527,16 +1485,14 @@ void @@ -1527,16 +1485,14 @@ void
1527 QPDFObjectHandle::pipeContentStreams( 1485 QPDFObjectHandle::pipeContentStreams(
1528 Pipeline* p, std::string const& description, std::string& all_description) 1486 Pipeline* p, std::string const& description, std::string& all_description)
1529 { 1487 {
1530 - std::vector<QPDFObjectHandle> streams =  
1531 - arrayOrStreamToStreamArray(description, all_description);  
1532 bool need_newline = false; 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 if (need_newline) { 1492 if (need_newline) {
1536 buf.writeCStr("\n"); 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 QTC::TC("qpdf", "QPDFObjectHandle errors in parsecontent"); 1496 QTC::TC("qpdf", "QPDFObjectHandle errors in parsecontent");
1541 throw QPDFExc( 1497 throw QPDFExc(
1542 qpdf_e_damaged_pdf, 1498 qpdf_e_damaged_pdf,
@@ -1545,11 +1501,11 @@ QPDFObjectHandle::pipeContentStreams( @@ -1545,11 +1501,11 @@ QPDFObjectHandle::pipeContentStreams(
1545 0, 1501 0,
1546 "errors while decoding content stream"); 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 QTC::TC("qpdf", "QPDFObjectHandle need_newline", need_newline ? 0 : 1); 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 p->finish(); 1509 p->finish();
1554 } 1510 }
1555 1511
libqpdf/qpdf/Pipeline_private.hh
@@ -46,6 +46,12 @@ namespace qpdf::pl @@ -46,6 +46,12 @@ namespace qpdf::pl
46 { 46 {
47 } 47 }
48 48
  49 + String(std::string& str) :
  50 + Pipeline("", nullptr),
  51 + str(str)
  52 + {
  53 + }
  54 +
49 ~String() final = default; 55 ~String() final = default;
50 56
51 void 57 void