-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathbase64.cpp
More file actions
168 lines (130 loc) · 3.46 KB
/
Copy pathbase64.cpp
File metadata and controls
168 lines (130 loc) · 3.46 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* base64.cpp
*
* Copyright (c) 2026 AMSOFTSWITCH LTD.
* All rights reserved.
*/
#include "base64.h"
#include <cstdint>
#include <stdexcept>
namespace {
int decode_value(unsigned char c)
{
if (c >= 'A' && c <= 'Z') {
return static_cast<int>(c - 'A');
}
if (c >= 'a' && c <= 'z') {
return static_cast<int>(c - 'a') + 26;
}
if (c >= '0' && c <= '9') {
return static_cast<int>(c - '0') + 52;
}
// Standard and URL-safe variants.
if (c == '+' || c == '-') {
return 62;
}
if (c == '/' || c == '_') {
return 63;
}
return -1;
}
bool is_padding(unsigned char c)
{
return c == '=' || c == '.';
}
} // namespace
std::string base64_decode(const std::string& input)
{
if (input.empty()) {
return {};
}
// A Base64 stream can never have a final group containing
// only one encoded character.
if (input.size() % 4 == 1) {
throw std::runtime_error("Invalid Base64 length");
}
std::string output;
output.reserve((input.size() * 3) / 4 + 3);
std::size_t pos = 0;
while (pos < input.size()) {
const std::size_t remaining = input.size() - pos;
const unsigned char c0 =
static_cast<unsigned char>(input[pos]);
const unsigned char c1 =
static_cast<unsigned char>(input[pos + 1]);
if (is_padding(c0) || is_padding(c1)) {
throw std::runtime_error("Invalid Base64 padding");
}
const int v0 = decode_value(c0);
const int v1 = decode_value(c1);
if (v0 < 0 || v1 < 0) {
throw std::runtime_error("Invalid Base64 character");
}
output.push_back(static_cast<char>(
(v0 << 2) | (v1 >> 4)));
/*
* Two-character final group:
*
* xx
*
* Equivalent to xx==.
*/
if (remaining == 2) {
return output;
}
const unsigned char c2 =
static_cast<unsigned char>(input[pos + 2]);
/*
* Padded one-byte final group:
*
* xx==
* xx..
*/
if (is_padding(c2)) {
if (remaining != 4 ||
!is_padding(
static_cast<unsigned char>(input[pos + 3]))) {
throw std::runtime_error("Invalid Base64 padding");
}
return output;
}
const int v2 = decode_value(c2);
if (v2 < 0) {
throw std::runtime_error("Invalid Base64 character");
}
output.push_back(static_cast<char>(
((v1 & 0x0f) << 4) | (v2 >> 2)));
/*
* Three-character final group:
*
* xxx
*
* Equivalent to xxx=.
*/
if (remaining == 3) {
return output;
}
const unsigned char c3 =
static_cast<unsigned char>(input[pos + 3]);
/*
* Padded two-byte final group:
*
* xxx=
* xxx.
*/
if (is_padding(c3)) {
if (remaining != 4) {
throw std::runtime_error("Invalid Base64 padding");
}
return output;
}
const int v3 = decode_value(c3);
if (v3 < 0) {
throw std::runtime_error("Invalid Base64 character");
}
output.push_back(static_cast<char>(
((v2 & 0x03) << 6) | v3));
pos += 4;
}
return output;
}