/src/kcodecs/src/kcodecsbase64.cpp
Line | Count | Source |
1 | | /* -*- c++ -*- |
2 | | SPDX-FileCopyrightText: 2001 Marc Mutz <mutz@kde.org> |
3 | | |
4 | | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | | */ |
6 | | |
7 | | #include "kcodecsbase64.h" |
8 | | #include "kcodecs_p.h" |
9 | | |
10 | | #include <QDebug> |
11 | | |
12 | | #include <cassert> |
13 | | |
14 | | using namespace KCodecs; |
15 | | |
16 | | namespace KCodecs |
17 | | { |
18 | | // codec for base64 as specified in RFC 2045 |
19 | | // class Base64Codec; |
20 | | // class Base64Decoder; |
21 | | // class Base64Encoder; |
22 | | |
23 | | // codec for the B encoding as specified in RFC 2047 |
24 | | // class Rfc2047BEncodingCodec; |
25 | | // class Rfc2047BEncodingEncoder; |
26 | | // class Rfc2047BEncodingDecoder; |
27 | | |
28 | | //@cond PRIVATE |
29 | | static const uchar base64DecodeMap[128] = { |
30 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
31 | | |
32 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, |
33 | | |
34 | | 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, |
35 | | |
36 | | 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64}; |
37 | | |
38 | | static const char base64EncodeMap[64] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
39 | | 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', |
40 | | 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |
41 | | //@endcond |
42 | | |
43 | | class Base64Decoder : public Decoder |
44 | | { |
45 | | uint mStepNo; |
46 | | uchar mOutbits; |
47 | | bool mSawPadding : 1; |
48 | | |
49 | | protected: |
50 | | friend class Base64Codec; |
51 | | Base64Decoder(Codec::NewlineType newline = Codec::NewlineLF) |
52 | 146k | : Decoder(newline) |
53 | 146k | , mStepNo(0) |
54 | 146k | , mOutbits(0) |
55 | 146k | , mSawPadding(false) |
56 | 146k | { |
57 | 146k | } |
58 | | |
59 | | public: |
60 | | ~Base64Decoder() override |
61 | 0 | { |
62 | 0 | } |
63 | | |
64 | | bool decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) override; |
65 | | // ### really needs no finishing??? |
66 | | bool finish(char *&dcursor, const char *const dend) override |
67 | 1.48k | { |
68 | 1.48k | Q_UNUSED(dcursor); |
69 | 1.48k | Q_UNUSED(dend); |
70 | 1.48k | return true; |
71 | 1.48k | } |
72 | | }; |
73 | | |
74 | | class Base64Encoder : public Encoder |
75 | | { |
76 | | uint mStepNo; |
77 | | /** number of already written base64-quartets on current line */ |
78 | | uint mWrittenPacketsOnThisLine; |
79 | | uchar mNextbits; |
80 | | bool mInsideFinishing : 1; |
81 | | |
82 | | protected: |
83 | | friend class Rfc2047BEncodingCodec; |
84 | | friend class Rfc2047BEncodingEncoder; |
85 | | friend class Base64Codec; |
86 | | Base64Encoder(Codec::NewlineType newline = Codec::NewlineLF) |
87 | 125k | : Encoder(newline) |
88 | 125k | , mStepNo(0) |
89 | 125k | , mWrittenPacketsOnThisLine(0) |
90 | 125k | , mNextbits(0) |
91 | 125k | , mInsideFinishing(false) |
92 | 125k | { |
93 | 125k | } |
94 | | |
95 | | bool generic_finish(char *&dcursor, const char *const dend, bool withLFatEnd); |
96 | | |
97 | | public: |
98 | | ~Base64Encoder() override |
99 | 0 | { |
100 | 0 | } |
101 | | |
102 | | bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) override; |
103 | | |
104 | | bool finish(char *&dcursor, const char *const dend) override; |
105 | | |
106 | | protected: |
107 | | bool writeBase64(uchar ch, char *&dcursor, const char *const dend) |
108 | 529k | { |
109 | 529k | return write(base64EncodeMap[ch], dcursor, dend); |
110 | 529k | } |
111 | | }; |
112 | | |
113 | | class Rfc2047BEncodingEncoder : public Base64Encoder |
114 | | { |
115 | | protected: |
116 | | friend class Rfc2047BEncodingCodec; |
117 | | Rfc2047BEncodingEncoder(Codec::NewlineType newline = Codec::NewlineLF) |
118 | 0 | : Base64Encoder(newline) |
119 | 0 | { |
120 | 0 | } |
121 | | |
122 | | public: |
123 | | bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) override; |
124 | | bool finish(char *&dcursor, const char *const dend) override; |
125 | | }; |
126 | | |
127 | | Encoder *Base64Codec::makeEncoder(Codec::NewlineType newline) const |
128 | 125k | { |
129 | 125k | return new Base64Encoder(newline); |
130 | 125k | } |
131 | | |
132 | | Decoder *Base64Codec::makeDecoder(Codec::NewlineType newline) const |
133 | 146k | { |
134 | 146k | return new Base64Decoder(newline); |
135 | 146k | } |
136 | | |
137 | | Encoder *Rfc2047BEncodingCodec::makeEncoder(Codec::NewlineType newline) const |
138 | 0 | { |
139 | 0 | return new Rfc2047BEncodingEncoder(newline); |
140 | 0 | } |
141 | | |
142 | | /********************************************************/ |
143 | | /********************************************************/ |
144 | | /********************************************************/ |
145 | | |
146 | | bool Base64Decoder::decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) |
147 | 147k | { |
148 | 965k | while (dcursor != dend && scursor != send) { |
149 | 868k | uchar ch = *scursor++; |
150 | 868k | uchar value; |
151 | | |
152 | | // try converting ch to a 6-bit value: |
153 | 868k | if (ch < 128) { |
154 | 855k | value = base64DecodeMap[ch]; |
155 | 855k | } else { |
156 | 12.5k | value = 64; |
157 | 12.5k | } |
158 | | |
159 | | // ch isn't of the base64 alphabet, check for other significant chars: |
160 | 868k | if (value >= 64) { |
161 | 207k | if (ch == '=') { |
162 | | // padding: |
163 | 75.0k | if (mStepNo == 0 || mStepNo == 1) { |
164 | 11.3k | if (!mSawPadding) { |
165 | | // malformed |
166 | | // qWarning() << "Base64Decoder: unexpected padding" |
167 | | // "character in input stream"; |
168 | 10.5k | } |
169 | 11.3k | mSawPadding = true; |
170 | 11.3k | break; |
171 | 63.7k | } else if (mStepNo == 2) { |
172 | | // ok, there should be another one |
173 | 37.7k | } else if (mStepNo == 3) { |
174 | | // ok, end of encoded stream |
175 | 37.7k | mSawPadding = true; |
176 | 37.7k | break; |
177 | 37.7k | } |
178 | 26.0k | mSawPadding = true; |
179 | 26.0k | mStepNo = (mStepNo + 1) % 4; |
180 | 26.0k | continue; |
181 | 132k | } else { |
182 | | // non-base64 alphabet |
183 | 132k | continue; |
184 | 132k | } |
185 | 207k | } |
186 | | |
187 | 660k | if (mSawPadding) { |
188 | | // qWarning() << "Base64Decoder: Embedded padding character" |
189 | | // "encountered!"; |
190 | 1.22k | return true; |
191 | 1.22k | } |
192 | | |
193 | | // add the new bits to the output stream and flush full octets: |
194 | 659k | switch (mStepNo) { |
195 | 184k | case 0: |
196 | 184k | mOutbits = value << 2; |
197 | 184k | break; |
198 | 180k | case 1: |
199 | 180k | *dcursor++ = (char)(mOutbits | value >> 4); |
200 | 180k | mOutbits = value << 4; |
201 | 180k | break; |
202 | 154k | case 2: |
203 | 154k | *dcursor++ = (char)(mOutbits | value >> 2); |
204 | 154k | mOutbits = value << 6; |
205 | 154k | break; |
206 | 140k | case 3: |
207 | 140k | *dcursor++ = (char)(mOutbits | value); |
208 | 140k | mOutbits = 0; |
209 | 140k | break; |
210 | 0 | default: |
211 | 0 | assert(0); |
212 | 659k | } |
213 | 659k | mStepNo = (mStepNo + 1) % 4; |
214 | 659k | } |
215 | | |
216 | | // return false when caller should call us again: |
217 | 146k | return scursor == send; |
218 | 147k | } // Base64Decoder::decode() |
219 | | |
220 | | bool Base64Encoder::encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) |
221 | 125k | { |
222 | 125k | const uint maxPacketsPerLine = 76 / 4; |
223 | | |
224 | | // detect when the caller doesn't adhere to our rules: |
225 | 125k | if (mInsideFinishing) { |
226 | 0 | return true; |
227 | 0 | } |
228 | | |
229 | 507k | while (scursor != send && dcursor != dend) { |
230 | | // properly empty the output buffer before starting something new: |
231 | | // ### fixme: we can optimize this away, since the buffer isn't |
232 | | // written to anyway (most of the time) |
233 | 381k | if (d->outputBufferCursor && !flushOutputBuffer(dcursor, dend)) { |
234 | 0 | return scursor == send; |
235 | 0 | } |
236 | | |
237 | 381k | uchar ch = *scursor++; |
238 | | // mNextbits // (part of) value of next sextet |
239 | | |
240 | | // check for line length; |
241 | 381k | if (mStepNo == 0 && mWrittenPacketsOnThisLine >= maxPacketsPerLine) { |
242 | 3.39k | writeCRLF(dcursor, dend); |
243 | 3.39k | mWrittenPacketsOnThisLine = 0; |
244 | 3.39k | } |
245 | | |
246 | | // depending on mStepNo, extract value and mNextbits from the |
247 | | // octet stream: |
248 | 381k | switch (mStepNo) { |
249 | 147k | case 0: |
250 | 147k | assert(mNextbits == 0); |
251 | 147k | writeBase64(ch >> 2, dcursor, dend); // top-most 6 bits -> output |
252 | 147k | mNextbits = (ch & 0x3) << 4; // 0..1 bits -> 4..5 in mNextbits |
253 | 147k | break; |
254 | 123k | case 1: |
255 | 123k | assert((mNextbits & ~0x30) == 0); |
256 | 123k | writeBase64(mNextbits | ch >> 4, dcursor, dend); // 4..7 bits -> 0..3 in value |
257 | 123k | mNextbits = (ch & 0xf) << 2; // 0..3 bits -> 2..5 in mNextbits |
258 | 123k | break; |
259 | 111k | case 2: |
260 | 111k | assert((mNextbits & ~0x3C) == 0); |
261 | 111k | writeBase64(mNextbits | ch >> 6, dcursor, dend); // 6..7 bits -> 0..1 in value |
262 | 111k | writeBase64(ch & 0x3F, dcursor, dend); // 0..5 bits -> output |
263 | 111k | mNextbits = 0; |
264 | 111k | mWrittenPacketsOnThisLine++; |
265 | 111k | break; |
266 | 0 | default: |
267 | 0 | assert(0); |
268 | 381k | } |
269 | 381k | mStepNo = (mStepNo + 1) % 3; |
270 | 381k | } |
271 | | |
272 | 125k | if (d->outputBufferCursor) { |
273 | 0 | flushOutputBuffer(dcursor, dend); |
274 | 0 | } |
275 | | |
276 | 125k | return scursor == send; |
277 | 125k | } |
278 | | |
279 | | bool Rfc2047BEncodingEncoder::encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend) |
280 | 0 | { |
281 | | // detect when the caller doesn't adhere to our rules: |
282 | 0 | if (mInsideFinishing) { |
283 | 0 | return true; |
284 | 0 | } |
285 | | |
286 | 0 | while (scursor != send && dcursor != dend) { |
287 | | // properly empty the output buffer before starting something new: |
288 | | // ### fixme: we can optimize this away, since the buffer isn't |
289 | | // written to anyway (most of the time) |
290 | 0 | if (d->outputBufferCursor && !flushOutputBuffer(dcursor, dend)) { |
291 | 0 | return scursor == send; |
292 | 0 | } |
293 | | |
294 | 0 | uchar ch = *scursor++; |
295 | | // mNextbits // (part of) value of next sextet |
296 | | |
297 | | // depending on mStepNo, extract value and mNextbits from the |
298 | | // octet stream: |
299 | 0 | switch (mStepNo) { |
300 | 0 | case 0: |
301 | 0 | assert(mNextbits == 0); |
302 | 0 | writeBase64(ch >> 2, dcursor, dend); // top-most 6 bits -> output |
303 | 0 | mNextbits = (ch & 0x3) << 4; // 0..1 bits -> 4..5 in mNextbits |
304 | 0 | break; |
305 | 0 | case 1: |
306 | 0 | assert((mNextbits & ~0x30) == 0); |
307 | 0 | writeBase64(mNextbits | ch >> 4, dcursor, dend); // 4..7 bits -> 0..3 in value |
308 | 0 | mNextbits = (ch & 0xf) << 2; // 0..3 bits -> 2..5 in mNextbits |
309 | 0 | break; |
310 | 0 | case 2: |
311 | 0 | assert((mNextbits & ~0x3C) == 0); |
312 | 0 | writeBase64(mNextbits | ch >> 6, dcursor, dend); // 6..7 bits -> 0..1 in value |
313 | 0 | writeBase64(ch & 0x3F, dcursor, dend); // 0..5 bits -> output |
314 | 0 | mNextbits = 0; |
315 | 0 | break; |
316 | 0 | default: |
317 | 0 | assert(0); |
318 | 0 | } |
319 | 0 | mStepNo = (mStepNo + 1) % 3; |
320 | 0 | } |
321 | | |
322 | 0 | if (d->outputBufferCursor) { |
323 | 0 | flushOutputBuffer(dcursor, dend); |
324 | 0 | } |
325 | |
|
326 | 0 | return scursor == send; |
327 | 0 | } |
328 | | |
329 | | bool Base64Encoder::finish(char *&dcursor, const char *const dend) |
330 | 125k | { |
331 | 125k | return generic_finish(dcursor, dend, true); |
332 | 125k | } |
333 | | |
334 | | bool Rfc2047BEncodingEncoder::finish(char *&dcursor, const char *const dend) |
335 | 0 | { |
336 | 0 | return generic_finish(dcursor, dend, false); |
337 | 0 | } |
338 | | |
339 | | bool Base64Encoder::generic_finish(char *&dcursor, const char *const dend, bool withLFatEnd) |
340 | 125k | { |
341 | 125k | if (mInsideFinishing) { |
342 | 0 | return flushOutputBuffer(dcursor, dend); |
343 | 0 | } |
344 | | |
345 | 125k | if (d->outputBufferCursor && !flushOutputBuffer(dcursor, dend)) { |
346 | 0 | return false; |
347 | 0 | } |
348 | | |
349 | 125k | mInsideFinishing = true; |
350 | | |
351 | | // |
352 | | // writing out the last mNextbits... |
353 | | // |
354 | 125k | switch (mStepNo) { |
355 | 23.2k | case 1: // 2 mNextbits waiting to be written. Needs two padding chars: |
356 | 35.8k | case 2: // 4 or 6 mNextbits waiting to be written. Completes a block |
357 | 35.8k | writeBase64(mNextbits, dcursor, dend); |
358 | 35.8k | mNextbits = 0; |
359 | 35.8k | break; |
360 | 89.2k | case 0: // no padding, nothing to be written, except possibly the CRLF |
361 | 89.2k | assert(mNextbits == 0); |
362 | 89.2k | break; |
363 | 89.2k | default: |
364 | 0 | assert(0); |
365 | 125k | } |
366 | | |
367 | | // |
368 | | // adding padding... |
369 | | // |
370 | 125k | switch (mStepNo) { |
371 | 23.2k | case 1: |
372 | 23.2k | write('=', dcursor, dend); |
373 | 23.2k | Q_FALLTHROUGH(); |
374 | | // fall through: |
375 | 35.8k | case 2: |
376 | 35.8k | write('=', dcursor, dend); |
377 | 35.8k | Q_FALLTHROUGH(); |
378 | | // fall through: |
379 | 125k | case 0: // completed a quartet - add CRLF |
380 | 125k | if (withLFatEnd) { |
381 | 125k | writeCRLF(dcursor, dend); |
382 | 125k | } |
383 | 125k | return flushOutputBuffer(dcursor, dend); |
384 | 0 | default: |
385 | 0 | assert(0); |
386 | 125k | } |
387 | 0 | return true; // asserts get compiled out |
388 | 125k | } |
389 | | |
390 | | } // namespace KCodecs |