Commit 2033b3769a7405c77972012316e500ac6df4b006

Authored by m-holger
1 parent 9c7c7413

Amend pl::Count to use a numeric rather than string identifier

include/qpdf/QPDFWriter.hh
@@ -474,8 +474,8 @@ class QPDFWriter @@ -474,8 +474,8 @@ class QPDFWriter
474 ~PipelinePopper(); 474 ~PipelinePopper();
475 475
476 private: 476 private:
477 - QPDFWriter* qw;  
478 - std::string stack_id; 477 + QPDFWriter* qw{nullptr};
  478 + unsigned long stack_id{0};
479 }; 479 };
480 480
481 unsigned int bytesNeeded(long long n); 481 unsigned int bytesNeeded(long long n);
libqpdf/QPDFWriter.cc
@@ -911,7 +911,7 @@ QPDFWriter::pushPipeline(Pipeline* p) @@ -911,7 +911,7 @@ QPDFWriter::pushPipeline(Pipeline* p)
911 void 911 void
912 QPDFWriter::initializePipelineStack(Pipeline* p) 912 QPDFWriter::initializePipelineStack(Pipeline* p)
913 { 913 {
914 - m->pipeline = new pl::Count("pipeline stack base", p); 914 + m->pipeline = new pl::Count(1, p);
915 m->to_delete.emplace_back(std::shared_ptr<Pipeline>(m->pipeline)); 915 m->to_delete.emplace_back(std::shared_ptr<Pipeline>(m->pipeline));
916 m->pipeline_stack.emplace_back(m->pipeline); 916 m->pipeline_stack.emplace_back(m->pipeline);
917 } 917 }
@@ -933,26 +933,25 @@ void @@ -933,26 +933,25 @@ void
933 QPDFWriter::activatePipelineStack( 933 QPDFWriter::activatePipelineStack(
934 PipelinePopper& pp, bool discard, std::string* str, std::unique_ptr<pl::Link> link) 934 PipelinePopper& pp, bool discard, std::string* str, std::unique_ptr<pl::Link> link)
935 { 935 {
936 - std::string stack_id("stack " + std::to_string(m->next_stack_id));  
937 pl::Count* c; 936 pl::Count* c;
938 if (link) { 937 if (link) {
939 - c = new pl::Count(stack_id.c_str(), m->count_buffer, std::move(link)); 938 + c = new pl::Count(m->next_stack_id, m->count_buffer, std::move(link));
940 } else if (discard) { 939 } else if (discard) {
941 - c = new pl::Count(stack_id.c_str(), nullptr); 940 + c = new pl::Count(m->next_stack_id, nullptr);
942 } else if (!str) { 941 } else if (!str) {
943 - c = new pl::Count(stack_id.c_str(), m->pipeline_stack.back()); 942 + c = new pl::Count(m->next_stack_id, m->pipeline_stack.back());
944 } else { 943 } else {
945 - c = new pl::Count(stack_id.c_str(), *str); 944 + c = new pl::Count(m->next_stack_id, *str);
946 } 945 }
947 - ++m->next_stack_id; 946 + pp.stack_id = m->next_stack_id;
948 m->pipeline_stack.emplace_back(c); 947 m->pipeline_stack.emplace_back(c);
949 m->pipeline = c; 948 m->pipeline = c;
950 - pp.stack_id = stack_id; 949 + ++m->next_stack_id;
951 } 950 }
952 951
953 QPDFWriter::PipelinePopper::~PipelinePopper() 952 QPDFWriter::PipelinePopper::~PipelinePopper()
954 { 953 {
955 - if (stack_id.empty()) { 954 + if (!stack_id) {
956 return; 955 return;
957 } 956 }
958 qpdf_assert_debug(qw->m->pipeline_stack.size() >= 2); 957 qpdf_assert_debug(qw->m->pipeline_stack.size() >= 2);
@@ -962,7 +961,7 @@ QPDFWriter::PipelinePopper::~PipelinePopper() @@ -962,7 +961,7 @@ QPDFWriter::PipelinePopper::~PipelinePopper()
962 // deterministic ID, but I don't think so. As of this writing, this is the only case in which 961 // deterministic ID, but I don't think so. As of this writing, this is the only case in which
963 // two dynamically allocated PipelinePopper objects ever exist at the same time, so the 962 // two dynamically allocated PipelinePopper objects ever exist at the same time, so the
964 // assertion will fail if they get popped out of order from automatic destruction. 963 // assertion will fail if they get popped out of order from automatic destruction.
965 - qpdf_assert_debug(qw->m->pipeline->getIdentifier() == stack_id); 964 + qpdf_assert_debug(qw->m->pipeline->id() == stack_id);
966 delete qw->m->pipeline_stack.back(); 965 delete qw->m->pipeline_stack.back();
967 qw->m->pipeline_stack.pop_back(); 966 qw->m->pipeline_stack.pop_back();
968 while (!dynamic_cast<pl::Count*>(qw->m->pipeline_stack.back())) { 967 while (!dynamic_cast<pl::Count*>(qw->m->pipeline_stack.back())) {
libqpdf/QPDF_linearization.cc
@@ -1758,10 +1758,8 @@ QPDF::generateHintStream( @@ -1758,10 +1758,8 @@ QPDF::generateHintStream(
1758 std::string b; 1758 std::string b;
1759 auto c = compressed 1759 auto c = compressed
1760 ? std::make_unique<pl::Count>( 1760 ? std::make_unique<pl::Count>(
1761 - "count",  
1762 - b,  
1763 - pl::create<Pl_Flate>(pl::create<pl::String>(hint_buffer), Pl_Flate::a_deflate))  
1764 - : std::make_unique<pl::Count>("count", hint_buffer); 1761 + 0, b, pl::create<Pl_Flate>(pl::create<pl::String>(hint_buffer), Pl_Flate::a_deflate))
  1762 + : std::make_unique<pl::Count>(0, hint_buffer);
1765 1763
1766 BitWriter w(c.get()); 1764 BitWriter w(c.get());
1767 1765
libqpdf/qpdf/Pipeline_private.hh
@@ -69,27 +69,30 @@ namespace qpdf::pl @@ -69,27 +69,30 @@ namespace qpdf::pl
69 public: 69 public:
70 // Count the number of characters written. If 'next' is not set, the content written will be 70 // Count the number of characters written. If 'next' is not set, the content written will be
71 // discarded. 71 // discarded.
72 - Count(char const* identifier, Pipeline* next = nullptr) :  
73 - Pipeline(identifier, next), 72 + Count(unsigned long id, Pipeline* next = nullptr) :
  73 + Pipeline("", next),
  74 + id_(id),
74 pass_immediately_to_next(next) 75 pass_immediately_to_next(next)
75 { 76 {
76 } 77 }
77 78
78 // Count the number of characters written. If 'next' is not set, the content written will be 79 // Count the number of characters written. If 'next' is not set, the content written will be
79 // discarded. 80 // discarded.
80 - Count(char const* identifier, std::unique_ptr<Link> link = nullptr) :  
81 - Pipeline(identifier, link ? link->next_pl.get() : nullptr), 81 + Count(unsigned long id, std::unique_ptr<Link> link) :
  82 + Pipeline("", link ? link->next_pl.get() : nullptr),
82 link(std::move(link)), 83 link(std::move(link)),
  84 + id_(id),
83 pass_immediately_to_next(link) 85 pass_immediately_to_next(link)
84 { 86 {
85 } 87 }
86 88
87 // Write to 'str'. If 'next' is set, 'str' will be written to 'next' when 'finish' is 89 // Write to 'str'. If 'next' is set, 'str' will be written to 'next' when 'finish' is
88 // called. 90 // called.
89 - Count(char const* identifier, std::string& str, std::unique_ptr<Link> link = nullptr) :  
90 - Pipeline(identifier, link ? link->next_pl.get() : nullptr), 91 + Count(unsigned long id, std::string& str, std::unique_ptr<Link> link = nullptr) :
  92 + Pipeline("", link ? link->next_pl.get() : nullptr),
91 str(&str), 93 str(&str),
92 - link(std::move(link)) 94 + link(std::move(link)),
  95 + id_(id)
93 { 96 {
94 } 97 }
95 98
@@ -127,10 +130,17 @@ namespace qpdf::pl @@ -127,10 +130,17 @@ namespace qpdf::pl
127 return str ? static_cast<qpdf_offset_t>(str->size()) : count; 130 return str ? static_cast<qpdf_offset_t>(str->size()) : count;
128 } 131 }
129 132
  133 + unsigned long long
  134 + id() const
  135 + {
  136 + return id_;
  137 + }
  138 +
130 private: 139 private:
131 qpdf_offset_t count{0}; 140 qpdf_offset_t count{0};
132 std::string* str{nullptr}; 141 std::string* str{nullptr};
133 std::unique_ptr<Link> link{nullptr}; 142 std::unique_ptr<Link> link{nullptr};
  143 + unsigned long id_{0};
134 bool pass_immediately_to_next{false}; 144 bool pass_immediately_to_next{false};
135 }; 145 };
136 } // namespace qpdf::pl 146 } // namespace qpdf::pl
libqpdf/qpdf/QPDFWriter_private.hh
@@ -114,7 +114,7 @@ class QPDFWriter::Members @@ -114,7 +114,7 @@ class QPDFWriter::Members
114 std::map<QPDFObjGen, int> contents_to_page_seq; 114 std::map<QPDFObjGen, int> contents_to_page_seq;
115 std::map<int, std::vector<QPDFObjGen>> object_stream_to_objects; 115 std::map<int, std::vector<QPDFObjGen>> object_stream_to_objects;
116 std::vector<Pipeline*> pipeline_stack; 116 std::vector<Pipeline*> pipeline_stack;
117 - unsigned long long next_stack_id{0}; 117 + unsigned long next_stack_id{2};
118 std::string count_buffer; 118 std::string count_buffer;
119 bool deterministic_id{false}; 119 bool deterministic_id{false};
120 Pl_MD5* md5_pipeline{nullptr}; 120 Pl_MD5* md5_pipeline{nullptr};