1 /*
2     This file is part of the Okteta Kasten module, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2009-2010 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "bytearraybase64streamencoder.hpp"
10 
11 // Okteta core
12 #include <Okteta/AbstractByteArrayModel>
13 // KF
14 #include <KLocalizedString>
15 // Qt
16 #include <QTextStream>
17 
18 namespace Kasten {
19 
20 const char base64EncodeMap[64] = {
21     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
22     'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
23     'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
24     'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
25     'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
26     'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
27     'w', 'x', 'y', 'z', '0', '1', '2', '3',
28     '4', '5', '6', '7', '8', '9', '+', '/'
29 };
30 
31 static constexpr const char* base64PaddingData[2] = {
32     "==",
33     "="
34 };
base64Padding(ByteArrayBase64StreamEncoder::InputByteIndex index)35 static inline constexpr const char* base64Padding(ByteArrayBase64StreamEncoder::InputByteIndex index)
36 {
37     return base64PaddingData[static_cast<int>(index) - 1];
38 }
39 
ByteArrayBase64StreamEncoder()40 ByteArrayBase64StreamEncoder::ByteArrayBase64StreamEncoder()
41     : AbstractByteArrayStreamEncoder(i18nc("name of the encoding target", "Base64"), QStringLiteral("application/base64"))
42 {}
43 
44 ByteArrayBase64StreamEncoder::~ByteArrayBase64StreamEncoder() = default;
45 
encodeDataToStream(QIODevice * device,const ByteArrayView * byteArrayView,const Okteta::AbstractByteArrayModel * byteArrayModel,const Okteta::AddressRange & range)46 bool ByteArrayBase64StreamEncoder::encodeDataToStream(QIODevice* device,
47                                                       const ByteArrayView* byteArrayView,
48                                                       const Okteta::AbstractByteArrayModel* byteArrayModel,
49                                                       const Okteta::AddressRange& range)
50 {
51     Q_UNUSED(byteArrayView);
52 
53     bool success = true;
54 
55     // encode
56     QTextStream textStream(device);
57 
58     // prepare
59     InputByteIndex inputByteIndex = InputByteIndex::First;
60     int outputGroupsPerLine = 0;
61     unsigned char bitsFromLastByte;
62 
63     for (Okteta::Address i = range.start(); i <= range.end(); ++i) {
64         const Okteta::Byte byte = byteArrayModel->byte(i);
65 
66         switch (inputByteIndex)
67         {
68         case InputByteIndex::First:
69             // bits 7..2
70             textStream << base64EncodeMap[(byte >> 2)];
71             // bits 1..0 -> 5..4 for next
72             bitsFromLastByte = (byte & 0x3) << 4;
73             inputByteIndex = InputByteIndex::Second;
74             break;
75         case InputByteIndex::Second:
76             // from last and bits 7..4 as 3..0 from this
77             textStream << base64EncodeMap[(bitsFromLastByte | byte >> 4)];
78             // bits 3..0 -> 5..2 for next
79             bitsFromLastByte = (byte & 0xf) << 2;
80             inputByteIndex = InputByteIndex::Third;
81             break;
82         case InputByteIndex::Third:
83             // from last and bits 7..6 as 1..0 from this
84             textStream << base64EncodeMap[(bitsFromLastByte | byte >> 6)];
85             // bits 5..0
86             textStream << base64EncodeMap[(byte & 0x3F)];
87             inputByteIndex = InputByteIndex::First;
88             ++outputGroupsPerLine;
89             if (outputGroupsPerLine >= maxOutputGroupsPerLine && i < range.end()) {
90                 textStream << "\r\n";
91                 outputGroupsPerLine = 0;
92             }
93             break;
94         }
95     }
96 
97     const bool hasBitsLeft = (inputByteIndex != InputByteIndex::First);
98     if (hasBitsLeft) {
99         textStream << base64EncodeMap[bitsFromLastByte]
100                    << base64Padding(inputByteIndex);
101     }
102 
103     return success;
104 }
105 
106 }
107