Commit a686cc5f4cff6d044766f89f709946a13552e957

Authored by m-holger
1 parent db7251f1

Refactor `Pl_Base64` to use `std::string` for input buffering, simplifying `writ…

…e` and `finish` logic
libqpdf/Pl_Base64.cc
... ... @@ -39,14 +39,7 @@ Pl_Base64::Pl_Base64(char const* identifier, Pipeline* next, action_e action) :
39 39 void
40 40 Pl_Base64::write(unsigned char const* data, size_t len)
41 41 {
42   - if (finished) {
43   - throw std::logic_error("Pl_Base64 used after finished");
44   - }
45   - if (action == a_decode) {
46   - decode(data, len);
47   - } else {
48   - encode(data, len);
49   - }
  42 + in_buffer.append(reinterpret_cast<const char*>(data), len);
50 43 }
51 44  
52 45 void
... ... @@ -57,7 +50,7 @@ Pl_Base64::decode(unsigned char const* data, size_t len)
57 50 if (!util::is_space(to_c(*p))) {
58 51 buf[pos++] = *p;
59 52 if (pos == 4) {
60   - flush();
  53 + flush_decode();
61 54 }
62 55 }
63 56 ++p;
... ... @@ -72,7 +65,7 @@ Pl_Base64::encode(unsigned char const* data, size_t len)
72 65 while (len > 0) {
73 66 buf[pos++] = *p;
74 67 if (pos == 3) {
75   - flush();
  68 + flush_encode();
76 69 }
77 70 ++p;
78 71 --len;
... ... @@ -80,17 +73,6 @@ Pl_Base64::encode(unsigned char const* data, size_t len)
80 73 }
81 74  
82 75 void
83   -Pl_Base64::flush()
84   -{
85   - if (action == a_decode) {
86   - flush_decode();
87   - } else {
88   - flush_encode();
89   - }
90   - reset();
91   -}
92   -
93   -void
94 76 Pl_Base64::flush_decode()
95 77 {
96 78 if (end_of_data) {
... ... @@ -129,6 +111,7 @@ Pl_Base64::flush_decode()
129 111 };
130 112  
131 113 next()->write(out, QIntC::to_size(3 - pad));
  114 + reset();
132 115 }
133 116  
134 117 void
... ... @@ -162,23 +145,28 @@ Pl_Base64::flush_encode()
162 145 out[3 - i] = '=';
163 146 }
164 147 next()->write(out, 4);
  148 + reset();
165 149 }
166 150  
167 151 void
168 152 Pl_Base64::finish()
169 153 {
170   - if (pos > 0) {
171   - if (finished) {
172   - throw std::logic_error("Pl_Base64 used after finished");
173   - }
174   - if (action == a_decode) {
  154 + if (action == a_decode) {
  155 + decode(reinterpret_cast<unsigned char const*>(in_buffer.data()), in_buffer.size());
  156 + if (pos > 0) {
175 157 for (size_t i = pos; i < 4; ++i) {
176 158 buf[i] = '=';
177 159 }
  160 + flush_decode();
  161 + }
  162 + } else {
  163 + encode(reinterpret_cast<unsigned char const*>(in_buffer.data()), in_buffer.size());
  164 + if (pos > 0) {
  165 + flush_encode();
178 166 }
179   - flush();
180 167 }
181   - finished = true;
  168 + in_buffer.clear();
  169 + in_buffer.shrink_to_fit();
182 170 next()->finish();
183 171 }
184 172  
... ...
libqpdf/qpdf/Pl_Base64.hh
... ... @@ -15,7 +15,6 @@ class Pl_Base64 final: public Pipeline
15 15 private:
16 16 void decode(unsigned char const* buf, size_t len);
17 17 void encode(unsigned char const* buf, size_t len);
18   - void flush();
19 18 void flush_decode();
20 19 void flush_encode();
21 20 void reset();
... ... @@ -23,6 +22,7 @@ class Pl_Base64 final: public Pipeline
23 22 action_e action;
24 23 unsigned char buf[4]{0, 0, 0, 0};
25 24 size_t pos{0};
  25 + std::string in_buffer;
26 26 bool end_of_data{false};
27 27 bool finished{false};
28 28 };
... ...