1 | /* -*- c++ -*- |
2 | SPDX-FileCopyrightText: 2001-2002 Marc Mutz <mutz@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | /* |
7 | @glossary @anchor Base64 @anchor base64 @b base64: |
8 | a binary to text encoding scheme based on @ref RFC1421. |
9 | |
10 | @glossary @anchor RFC1421 @anchor rfc1421 @b RFC @b 1421: |
11 | RFC that defines the <a href="http://tools.ietf.org/html/rfc1421"> |
12 | Privacy Enhancement for Internet Electronic Mail: Part I: |
13 | Message Encryption and Authentication Procedures</a>. |
14 | |
15 | @glossary @anchor RFC2045 @anchor rfc2045 @b RFC @b 2045: |
16 | RFC that defines the <a href="http://tools.ietf.org/html/rfc2045"> |
17 | MIME Part One: Format of Internet Message Bodies</a>. |
18 | |
19 | @glossary @anchor RFC2047 @anchor rfc2047 @b RFC @b 2047: |
20 | RFC that defines the <a href="http://tools.ietf.org/html/rfc2047"> |
21 | MIME Part Three: Message Header Extensions for Non-ASCII Text</a>. |
22 | |
23 | @glossary @anchor RFC2047B @anchor rfc2047b @b RFC @b 2047B: |
24 | Section 4.1 of @ref RFC2047. |
25 | */ |
26 | |
27 | #ifndef KCODECS_BASE64_H |
28 | #define KCODECS_BASE64_H |
29 | |
30 | #include "kcodecs.h" |
31 | |
32 | namespace KCodecs |
33 | { |
34 | class Base64Codec : public Codec |
35 | { |
36 | public: |
37 | Base64Codec() |
38 | : Codec() |
39 | { |
40 | } |
41 | |
42 | ~Base64Codec() override |
43 | { |
44 | } |
45 | |
46 | const char *name() const override |
47 | { |
48 | return "base64" ; |
49 | } |
50 | |
51 | qsizetype maxEncodedSizeFor(qsizetype insize, NewlineType newline) const override |
52 | { |
53 | // first, the total number of 4-char packets will be: |
54 | qsizetype totalNumPackets = (insize + 2) / 3; |
55 | // now, after every 76/4'th packet there needs to be a linebreak: |
56 | qsizetype numLineBreaks = totalNumPackets / (76 / 4); |
57 | // and at the very end, too: |
58 | ++numLineBreaks; |
59 | // putting it all together, we have: |
60 | return 4 * totalNumPackets + (newline == Codec::NewlineCRLF ? 2 : 1) * numLineBreaks; |
61 | } |
62 | |
63 | qsizetype maxDecodedSizeFor(qsizetype insize, NewlineType newline = Codec::NewlineLF) const override |
64 | { |
65 | // assuming all characters are part of the base64 stream (which |
66 | // does almost never hold due to required linebreaking; but |
67 | // additional non-base64 chars don't affect the output size), each |
68 | // 4-tupel of them becomes a 3-tupel in the decoded octet |
69 | // stream. So: |
70 | qsizetype result = ((insize + 3) / 4) * 3; |
71 | // but all of them may be \n, so |
72 | if (newline == Codec::NewlineCRLF) { |
73 | result *= 2; // :-o |
74 | } |
75 | |
76 | return result; |
77 | } |
78 | |
79 | Encoder *makeEncoder(NewlineType newline = Codec::NewlineLF) const override; |
80 | |
81 | Decoder *makeDecoder(NewlineType newline = Codec::NewlineLF) const override; |
82 | }; |
83 | |
84 | class Rfc2047BEncodingCodec : public Base64Codec |
85 | { |
86 | public: |
87 | Rfc2047BEncodingCodec() |
88 | : Base64Codec() |
89 | { |
90 | } |
91 | |
92 | ~Rfc2047BEncodingCodec() override |
93 | { |
94 | } |
95 | |
96 | const char *name() const override |
97 | { |
98 | return "b" ; |
99 | } |
100 | |
101 | qsizetype maxEncodedSizeFor(qsizetype insize, NewlineType newline = Codec::NewlineLF) const override |
102 | { |
103 | Q_UNUSED(newline); |
104 | // Each (begun) 3-octet triple becomes a 4 char quartet, so: |
105 | return ((insize + 2) / 3) * 4; |
106 | } |
107 | |
108 | qsizetype maxDecodedSizeFor(qsizetype insize, NewlineType newline = Codec::NewlineLF) const override |
109 | { |
110 | Q_UNUSED(newline); |
111 | // Each 4-char quartet becomes a 3-octet triple, the last one |
112 | // possibly even less. So: |
113 | return ((insize + 3) / 4) * 3; |
114 | } |
115 | |
116 | Encoder *makeEncoder(NewlineType newline = Codec::NewlineLF) const override; |
117 | }; |
118 | |
119 | } // namespace KCodecs |
120 | |
121 | #endif // KCODECS_BASE64_H |
122 | |