Commit 80631cb00d32d2e1ebd9f7fba8997d82c4c48559

Authored by m-holger
1 parent 876c238e

Enhance pl::Count to incorporate an end-of-line Pl_String

include/qpdf/QPDFWriter.hh
@@ -597,12 +597,14 @@ class QPDFWriter @@ -597,12 +597,14 @@ class QPDFWriter
597 // activate the pipeline stack. When the passed in PipelinePopper goes out of scope, the stack 597 // activate the pipeline stack. When the passed in PipelinePopper goes out of scope, the stack
598 // is popped. 598 // is popped.
599 Pipeline* pushPipeline(Pipeline*); 599 Pipeline* pushPipeline(Pipeline*);
600 - void activatePipelineStack(PipelinePopper& pp, bool discard = false); 600 + void
  601 + activatePipelineStack(PipelinePopper& pp, bool discard = false, std::string* str = nullptr);
601 void initializePipelineStack(Pipeline*); 602 void initializePipelineStack(Pipeline*);
602 603
603 void adjustAESStreamLength(size_t& length); 604 void adjustAESStreamLength(size_t& length);
604 void pushEncryptionFilter(PipelinePopper&); 605 void pushEncryptionFilter(PipelinePopper&);
605 void pushDiscardFilter(PipelinePopper&); 606 void pushDiscardFilter(PipelinePopper&);
  607 + void pushStringPipeline(PipelinePopper&, std::string& str);
606 void pushMD5Pipeline(PipelinePopper&); 608 void pushMD5Pipeline(PipelinePopper&);
607 void computeDeterministicIDData(); 609 void computeDeterministicIDData();
608 610
libqpdf/QPDFWriter.cc
@@ -917,10 +917,11 @@ QPDFWriter::initializePipelineStack(Pipeline* p) @@ -917,10 +917,11 @@ QPDFWriter::initializePipelineStack(Pipeline* p)
917 } 917 }
918 918
919 void 919 void
920 -QPDFWriter::activatePipelineStack(PipelinePopper& pp, bool discard) 920 +QPDFWriter::activatePipelineStack(PipelinePopper& pp, bool discard, std::string* str)
921 { 921 {
922 std::string stack_id("stack " + std::to_string(m->next_stack_id)); 922 std::string stack_id("stack " + std::to_string(m->next_stack_id));
923 - auto* c = new pl::Count(stack_id.c_str(), discard ? nullptr : m->pipeline_stack.back()); 923 + auto* c = str ? new pl::Count(stack_id.c_str(), str)
  924 + : new pl::Count(stack_id.c_str(), discard ? nullptr : m->pipeline_stack.back());
924 ++m->next_stack_id; 925 ++m->next_stack_id;
925 m->pipeline_stack.emplace_back(c); 926 m->pipeline_stack.emplace_back(c);
926 m->pipeline = c; 927 m->pipeline = c;
@@ -996,6 +997,12 @@ QPDFWriter::pushDiscardFilter(PipelinePopper& pp) @@ -996,6 +997,12 @@ QPDFWriter::pushDiscardFilter(PipelinePopper& pp)
996 } 997 }
997 998
998 void 999 void
  1000 +QPDFWriter::pushStringPipeline(PipelinePopper& pp, std::string& str)
  1001 +{
  1002 + activatePipelineStack(pp, true, &str);
  1003 +}
  1004 +
  1005 +void
999 QPDFWriter::pushMD5Pipeline(PipelinePopper& pp) 1006 QPDFWriter::pushMD5Pipeline(PipelinePopper& pp)
1000 { 1007 {
1001 if (!m->id2.empty()) { 1008 if (!m->id2.empty()) {
@@ -1280,8 +1287,7 @@ QPDFWriter::willFilterStream( @@ -1280,8 +1287,7 @@ QPDFWriter::willFilterStream(
1280 for (bool first_attempt: {true, false}) { 1287 for (bool first_attempt: {true, false}) {
1281 PipelinePopper pp_stream_data(this); 1288 PipelinePopper pp_stream_data(this);
1282 if (stream_data != nullptr) { 1289 if (stream_data != nullptr) {
1283 - pushPipeline(new Pl_String("stream data", nullptr, *stream_data));  
1284 - activatePipelineStack(pp_stream_data); 1290 + pushStringPipeline(pp_stream_data, *stream_data);
1285 } else { 1291 } else {
1286 pushDiscardFilter(pp_stream_data); 1292 pushDiscardFilter(pp_stream_data);
1287 } 1293 }
@@ -1644,9 +1650,7 @@ QPDFWriter::writeObjectStream(QPDFObjectHandle object) @@ -1644,9 +1650,7 @@ QPDFWriter::writeObjectStream(QPDFObjectHandle object)
1644 { 1650 {
1645 // Pass 1 1651 // Pass 1
1646 PipelinePopper pp_ostream_pass1(this); 1652 PipelinePopper pp_ostream_pass1(this);
1647 -  
1648 - pushPipeline(new Pl_String("object stream", nullptr, stream_buffer_pass1));  
1649 - activatePipelineStack(pp_ostream_pass1); 1653 + pushStringPipeline(pp_ostream_pass1, stream_buffer_pass1);
1650 1654
1651 int count = -1; 1655 int count = -1;
1652 for (auto const& obj: m->object_stream_to_objects[old_id]) { 1656 for (auto const& obj: m->object_stream_to_objects[old_id]) {
@@ -2888,9 +2892,8 @@ QPDFWriter::writeLinearized() @@ -2888,9 +2892,8 @@ QPDFWriter::writeLinearized()
2888 2892
2889 // Write hint stream to a buffer 2893 // Write hint stream to a buffer
2890 { 2894 {
2891 - pushPipeline(new Pl_String("hint buffer", nullptr, hint_buffer));  
2892 PipelinePopper pp_hint(this); 2895 PipelinePopper pp_hint(this);
2893 - activatePipelineStack(pp_hint); 2896 + pushStringPipeline(pp_hint, hint_buffer);
2894 writeHintStream(hint_id); 2897 writeHintStream(hint_id);
2895 } 2898 }
2896 hint_length = QIntC::to_offset(hint_buffer.size()); 2899 hint_length = QIntC::to_offset(hint_buffer.size());
libqpdf/qpdf/Pipeline_private.hh
@@ -13,12 +13,22 @@ namespace qpdf::pl @@ -13,12 +13,22 @@ namespace qpdf::pl
13 { 13 {
14 } 14 }
15 15
  16 + Count(char const* identifier, std::string* str) :
  17 + Pipeline(identifier, nullptr),
  18 + str(str)
  19 + {
  20 + }
  21 +
16 ~Count() final = default; 22 ~Count() final = default;
17 23
18 void 24 void
19 write(unsigned char const* buf, size_t len) final 25 write(unsigned char const* buf, size_t len) final
20 { 26 {
21 if (len) { 27 if (len) {
  28 + if (str) {
  29 + str->append(reinterpret_cast<char const*>(buf), len);
  30 + return;
  31 + }
22 count += static_cast<qpdf_offset_t>(len); 32 count += static_cast<qpdf_offset_t>(len);
23 if (next()) { 33 if (next()) {
24 next()->write(buf, len); 34 next()->write(buf, len);
@@ -37,11 +47,12 @@ namespace qpdf::pl @@ -37,11 +47,12 @@ namespace qpdf::pl
37 qpdf_offset_t 47 qpdf_offset_t
38 getCount() const 48 getCount() const
39 { 49 {
40 - return count; 50 + return str ? static_cast<qpdf_offset_t>(str->size()) : count;
41 } 51 }
42 52
43 private: 53 private:
44 qpdf_offset_t count{0}; 54 qpdf_offset_t count{0};
  55 + std::string* str{nullptr};
45 }; 56 };
46 } // namespace qpdf::pl 57 } // namespace qpdf::pl
47 58