/src/qpdf/libqpdf/qpdf/Pipeline_private.hh
Line | Count | Source |
1 | | #ifndef PIPELINE_PRIVATE_HH |
2 | | #define PIPELINE_PRIVATE_HH |
3 | | |
4 | | #include <qpdf/Types.h> |
5 | | |
6 | | #include <qpdf/Pipeline.hh> |
7 | | |
8 | | #include <qpdf/Pl_Flate.hh> |
9 | | #include <qpdf/Util.hh> |
10 | | |
11 | | namespace qpdf::pl |
12 | | { |
13 | | class String final: public Pipeline |
14 | | { |
15 | | public: |
16 | | String(char const* identifier, Pipeline*, std::string& str) : |
17 | 2.76k | Pipeline(identifier, nullptr), |
18 | 2.76k | str(str) |
19 | 2.76k | { |
20 | 2.76k | } |
21 | | |
22 | | String(std::string& str) : |
23 | 7.74k | Pipeline("", nullptr), |
24 | 7.74k | str(str) |
25 | 7.74k | { |
26 | 7.74k | } |
27 | | |
28 | | ~String() final = default; |
29 | | |
30 | | void |
31 | | write(unsigned char const* buf, size_t len) final |
32 | 8.28M | { |
33 | 8.28M | if (len) { |
34 | 8.28M | str.append(reinterpret_cast<char const*>(buf), len); |
35 | 8.28M | } |
36 | 8.28M | } |
37 | | |
38 | | void |
39 | | finish() final |
40 | 7.36k | { |
41 | 7.36k | } |
42 | | |
43 | | private: |
44 | | std::string& str; |
45 | | }; |
46 | | |
47 | | class Count final: public Pipeline |
48 | | { |
49 | | public: |
50 | | // Count the number of characters written. If 'next' is not set, the content written will be |
51 | | // discarded. |
52 | | Count(unsigned long id, Pipeline* next = nullptr) : |
53 | 18.5k | Pipeline("", next), |
54 | 18.5k | id_(id), |
55 | 18.5k | pass_immediately_to_next(next) |
56 | 18.5k | { |
57 | 18.5k | } |
58 | | |
59 | | // Write to 'str'. If 'next' is set, 'str' will be written to 'next' when 'finish' is |
60 | | // called. |
61 | | Count(unsigned long id, std::string& str, std::unique_ptr<Pipeline> link = nullptr) : |
62 | 38.3k | Pipeline("", link.get()), |
63 | 38.3k | str(&str), |
64 | 38.3k | link(std::move(link)), |
65 | 38.3k | id_(id) |
66 | 38.3k | { |
67 | 38.3k | } |
68 | | |
69 | 56.9k | ~Count() final = default; |
70 | | |
71 | | void |
72 | | write(unsigned char const* buf, size_t len) final |
73 | 6.22M | { |
74 | 6.22M | if (len) { |
75 | 6.21M | Count::write(std::string_view(reinterpret_cast<char const*>(buf), len)); |
76 | 6.21M | } |
77 | 6.22M | } |
78 | | |
79 | | void |
80 | | write(std::string_view sv) |
81 | 51.0M | { |
82 | 51.0M | if (sv.size()) { |
83 | 51.0M | if (str) { |
84 | 43.6M | str->append(sv); |
85 | 43.6M | return; |
86 | 43.6M | } |
87 | 7.32M | count += static_cast<qpdf_offset_t>(sv.size()); |
88 | 7.32M | if (pass_immediately_to_next) { |
89 | 7.26M | next()->write(reinterpret_cast<char const*>(sv.data()), sv.size()); |
90 | 7.26M | } |
91 | 7.32M | } |
92 | 51.0M | } |
93 | | |
94 | | void |
95 | | write(size_t len, char c) |
96 | 0 | { |
97 | 0 | if (len) { |
98 | 0 | if (str) { |
99 | 0 | str->append(len, c); |
100 | 0 | return; |
101 | 0 | } |
102 | 0 | count += static_cast<qpdf_offset_t>(len); |
103 | 0 | if (pass_immediately_to_next) { |
104 | 0 | next()->writeString(std::string(len, c)); |
105 | 0 | } |
106 | 0 | } |
107 | 0 | } |
108 | | |
109 | | void |
110 | | finish() final |
111 | 91.7k | { |
112 | 91.7k | if (next()) { |
113 | 24.5k | if (!pass_immediately_to_next) { |
114 | 0 | next()->write(reinterpret_cast<unsigned char const*>(str->data()), str->size()); |
115 | 0 | } |
116 | 24.5k | next()->finish(); |
117 | 24.5k | } |
118 | 91.7k | } |
119 | | |
120 | | qpdf_offset_t |
121 | | getCount() const |
122 | 257k | { |
123 | 257k | return str ? static_cast<qpdf_offset_t>(str->size()) : count; |
124 | 257k | } |
125 | | |
126 | | unsigned long long |
127 | | id() const |
128 | 48.6k | { |
129 | 48.6k | return id_; |
130 | 48.6k | } |
131 | | |
132 | | private: |
133 | | qpdf_offset_t count{0}; |
134 | | std::string* str{nullptr}; |
135 | | std::unique_ptr<Pipeline> link{nullptr}; |
136 | | unsigned long id_{0}; |
137 | | bool pass_immediately_to_next{false}; |
138 | | }; |
139 | | |
140 | | template <typename P, typename... Args> |
141 | | std::string |
142 | | pipe(std::string_view data, Args&&... args) |
143 | 0 | { |
144 | 0 | std::string result; |
145 | 0 | String s("", nullptr, result); |
146 | 0 | P pl("", &s, std::forward<Args>(args)...); |
147 | 0 | pl.write(reinterpret_cast<unsigned char const*>(data.data()), data.size()); |
148 | 0 | pl.finish(); |
149 | 0 | return result; |
150 | 0 | } Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > qpdf::pl::pipe<Pl_AES_PDF, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, bool&&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > qpdf::pl::pipe<Pl_RC4, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > qpdf::pl::pipe<Pl_Flate, Pl_Flate::action_e>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, Pl_Flate::action_e&&) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > qpdf::pl::pipe<Pl_PNGFilter, Pl_PNGFilter::action_e, unsigned int&>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, Pl_PNGFilter::action_e&&, unsigned int&) |
151 | | } // namespace qpdf::pl |
152 | | |
153 | | #endif // PIPELINE_PRIVATE_HH |