-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbase64.cpp
More file actions
183 lines (154 loc) · 5.79 KB
/
base64.cpp
File metadata and controls
183 lines (154 loc) · 5.79 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http
//
#include "base64.hpp"
namespace boost {
namespace http {
namespace bcrypt {
namespace detail {
namespace {
// bcrypt's non-standard base64 alphabet
constexpr char encode_table[] =
"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// Decode table: maps ASCII char to 6-bit value, or 0xFF for invalid
constexpr std::uint8_t decode_table[256] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0-7
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 8-15
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 16-23
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 24-31
0xFF, 0xFF, 0xFF, 0xF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
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
} // namespace
std::size_t
base64_encode(
char* dest,
std::uint8_t const* src,
std::size_t n)
{
char* out = dest;
while (n >= 3)
{
std::uint32_t v =
(static_cast<std::uint32_t>(src[0]) << 16) |
(static_cast<std::uint32_t>(src[1]) << 8) |
static_cast<std::uint32_t>(src[2]);
*out++ = encode_table[(v >> 18) & 0x3F];
*out++ = encode_table[(v >> 12) & 0x3F];
*out++ = encode_table[(v >> 6) & 0x3F];
*out++ = encode_table[v & 0x3F];
src += 3;
n -= 3;
}
if (n == 2)
{
std::uint32_t v =
(static_cast<std::uint32_t>(src[0]) << 16) |
(static_cast<std::uint32_t>(src[1]) << 8);
*out++ = encode_table[(v >> 18) & 0x3F];
*out++ = encode_table[(v >> 12) & 0x3F];
*out++ = encode_table[(v >> 6) & 0x3F];
}
else if (n == 1)
{
std::uint32_t v =
static_cast<std::uint32_t>(src[0]) << 16;
*out++ = encode_table[(v >> 18) & 0x3F];
*out++ = encode_table[(v >> 12) & 0x3F];
}
return static_cast<std::size_t>(out - dest);
}
int
base64_decode(
std::uint8_t* dest,
char const* src,
std::size_t n)
{
std::uint8_t* out = dest;
std::size_t i = 0;
while (i + 4 <= n)
{
std::uint8_t a = decode_table[static_cast<unsigned char>(src[i])];
std::uint8_t b = decode_table[static_cast<unsigned char>(src[i + 1])];
std::uint8_t c = decode_table[static_cast<unsigned char>(src[i + 2])];
std::uint8_t d = decode_table[static_cast<unsigned char>(src[i + 3])];
if ((a | b | c | d) & 0x80)
return -1;
std::uint32_t v =
(static_cast<std::uint32_t>(a) << 18) |
(static_cast<std::uint32_t>(b) << 12) |
(static_cast<std::uint32_t>(c) << 6) |
static_cast<std::uint32_t>(d);
*out++ = static_cast<std::uint8_t>(v >> 16);
*out++ = static_cast<std::uint8_t>(v >> 8);
*out++ = static_cast<std::uint8_t>(v);
i += 4;
}
// Handle remaining 2 or 3 characters
if (i + 3 == n)
{
std::uint8_t a = decode_table[static_cast<unsigned char>(src[i])];
std::uint8_t b = decode_table[static_cast<unsigned char>(src[i + 1])];
std::uint8_t c = decode_table[static_cast<unsigned char>(src[i + 2])];
if ((a | b | c) & 0x80)
return -1;
std::uint32_t v =
(static_cast<std::uint32_t>(a) << 18) |
(static_cast<std::uint32_t>(b) << 12) |
(static_cast<std::uint32_t>(c) << 6);
*out++ = static_cast<std::uint8_t>(v >> 16);
*out++ = static_cast<std::uint8_t>(v >> 8);
}
else if (i + 2 == n)
{
std::uint8_t a = decode_table[static_cast<unsigned char>(src[i])];
std::uint8_t b = decode_table[static_cast<unsigned char>(src[i + 1])];
if ((a | b) & 0x80)
return -1;
std::uint32_t v =
(static_cast<std::uint32_t>(a) << 18) |
(static_cast<std::uint32_t>(b) << 12);
*out++ = static_cast<std::uint8_t>(v >> 16);
}
else if (i + 1 == n)
{
// Single trailing character is invalid
return -1;
}
return static_cast<int>(out - dest);
}
} // detail
} // bcrypt
} // http
} // boost