forked from PADrend/Util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoding.cpp
146 lines (137 loc) · 4.44 KB
/
Encoding.cpp
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
This file is part of the Util library.
Copyright (C) 2007-2012 Benjamin Eikel <benjamin@eikel.org>
Copyright (C) 2007-2012 Claudius Jähn <claudius@uni-paderborn.de>
Copyright (C) 2007-2012 Ralf Petring <ralf@petring.net>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "Encoding.h"
#include "Macros.h"
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
namespace Util {
static const char * const base64Symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char base64Padding = '=';
std::string encodeBase64(const std::vector<uint8_t> & source) {
const size_t inputSize = source.size();
const size_t outputSize = (inputSize + 2 - ((inputSize + 2) % 3)) * 4 / 3;
const uint8_t * const input = source.data();
std::string destination(outputSize, base64Padding);
size_t inputCursor = 0;
std::string::iterator outputCursor = destination.begin();
const size_t completeSize = inputSize - (inputSize % 3);
while (inputCursor < completeSize) {
const uint32_t triplet = static_cast<uint32_t>(input[inputCursor + 2]) | (static_cast<uint32_t>(input[inputCursor + 1]) << 8) | (static_cast<uint32_t>(input[inputCursor]) << 16);
inputCursor += 3;
*outputCursor++ = base64Symbols[(triplet >> 18) & 0x3f];
*outputCursor++ = base64Symbols[(triplet >> 12) & 0x3f];
*outputCursor++ = base64Symbols[(triplet >> 6) & 0x3f];
*outputCursor++ = base64Symbols[triplet & 0x3f];
}
switch (inputSize % 3) {
case 1: {
const uint32_t triplet = (static_cast<uint32_t>(input[inputCursor]) << 16);
*outputCursor++ = base64Symbols[(triplet >> 18) & 0x3f];
*outputCursor++ = base64Symbols[(triplet >> 12) & 0x3f];
// Two padding characters at the end.
break;
}
case 2: {
const uint32_t triplet = (static_cast<uint32_t>(input[inputCursor + 1]) << 8) | (static_cast<uint32_t>(input[inputCursor]) << 16);
*outputCursor++ = base64Symbols[(triplet >> 18) & 0x3f];
*outputCursor++ = base64Symbols[(triplet >> 12) & 0x3f];
*outputCursor++ = base64Symbols[(triplet >> 6) & 0x3f];
// One padding character at the end.
break;
}
default:
break;
}
return destination;
}
//! (static)
std::vector<uint8_t> decodeBase64(const std::string & source) {
static std::vector<uint8_t> symbolTable;
if (symbolTable.empty()) {
symbolTable.assign(256, 0xff);
for (uint_fast8_t i = 0; i < 64; ++i) {
const char c = base64Symbols[i];
symbolTable[static_cast<uint8_t> (c)] = i;
}
symbolTable[static_cast<uint8_t>(base64Padding)] = 0xfe;
}
// count valid chars
size_t numSymbols = 0;
const size_t inputSize = source.size();
const uint8_t * const input = reinterpret_cast<const uint8_t * const>(source.data());
const uint8_t * inputCursor = input;
for(size_t i = 0; i < inputSize; ++i) {
const uint8_t s = symbolTable[*inputCursor];
++inputCursor;
if (s == 0xff) { // other char
continue;
} else if (s == 0xfe) { // padding found '='; end of input
break;
}
++numSymbols;
}
// calculate data size
size_t outputSize = (numSymbols / 4) * 3;
switch (numSymbols % 4) {
case 1:
WARN("decodeBase64 source length is not multiple of 4");
break;
case 2:
++outputSize;
break;
case 3:
outputSize += 2;
break;
default:
break;
}
// allocate data
std::vector<uint8_t> destination(outputSize);
// decode source string
inputCursor = input;
uint8_t * outputCursor = destination.data();
uint_fast8_t count = 0;
uint32_t value = 0;
for(size_t i = 0; i < inputSize; ++i) {
const uint8_t s = symbolTable[*inputCursor];
++inputCursor;
if (s == 0xff) { // other char
continue;
} else if (s == 0xfe) { // '='
if (count == 2) {
outputCursor[0] = static_cast<uint8_t> (value >> 10);
break;
} else if (count == 3) {
outputCursor[0] = static_cast<uint8_t> (value >> 16);
outputCursor[1] = static_cast<uint8_t> (value >> 8);
break;
}
WARN("decodeBase64 padding error");
break;
} else {
value |= s;
// four input characters produce three output bytes
if ((++count) == 4) {
outputCursor[0] = static_cast<uint8_t> (value >> 16);
outputCursor[1] = static_cast<uint8_t> (value >> 8);
outputCursor[2] = static_cast<uint8_t> (value);
outputCursor += 3;
value = 0;
count = 0;
} else {
value = value << 6;
}
}
}
return destination;
}
}