CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Encoding/Base64.cpp
Views: 1401
1
#include "Common/Data/Encoding/Base64.h"
2
3
// TODO: This is a simple but not very efficient implementation.
4
std::string Base64Encode(const uint8_t *p, size_t sz) {
5
static const char digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
6
7
size_t unpaddedLength = (4 * sz + 2) / 3;
8
std::string result;
9
result.resize((unpaddedLength + 3) & ~3, '=');
10
11
for (size_t i = 0; i < unpaddedLength; ++i) {
12
// This is the index into the original string.
13
size_t pos = (i * 3) / 4;
14
int8_t off = 2 * ((i * 3) % 4);
15
16
int c = p[pos];
17
if (off > 2) {
18
c <<= 8;
19
off -= 8;
20
21
// Grab more bits from the next character.
22
if (pos + 1 < sz) {
23
c |= p[pos + 1];
24
}
25
}
26
27
// Since we take from the big end, off starts at 2 and goes down.
28
int8_t shift = 2 - off;
29
30
// Now take the bits at off and encode the character.
31
result[i] = digits[(c >> shift) & 0x3F];
32
}
33
34
return result;
35
}
36
37
std::vector<uint8_t> Base64Decode(const char *s, size_t sz) {
38
static const uint8_t lookup[256] = {
39
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
40
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
41
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 63, 62, 255, 63,
42
// '0' starts here.
43
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255,
44
// 'A' after an invalid.
45
255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
46
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 63,
47
// 'a' after an invalid.
48
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
49
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
50
};
51
52
const uint8_t *p = (const uint8_t *)s;
53
std::vector<uint8_t> result;
54
result.reserve(3 * sz / 4);
55
56
for (size_t i = 0; i < sz; i += 4) {
57
uint8_t quad[4] = {
58
lookup[p[i]],
59
i + 1 < sz ? lookup[p[i + 1]] : (uint8_t)255,
60
i + 2 < sz ? lookup[p[i + 2]] : (uint8_t)255,
61
i + 3 < sz ? lookup[p[i + 3]] : (uint8_t)255,
62
};
63
64
// First: ABCDEF GHXXXX XXXXXX XXXXXX. Neither 6-bit value should be invalid.
65
result.push_back((quad[0] << 2) | ((quad[1] & 0x30) >> 4));
66
67
// Next: XXXXXX XXABCD EFGHXX XXXXXX. Invalid if quad[2] is invalid.
68
if (quad[2] == 255) {
69
continue;
70
}
71
result.push_back(((quad[1] & 0x0F) << 4) | ((quad[2] & 0x3C) >> 2));
72
73
// Last: XXXXXX XXXXXX XXXXAB CDEFGH. Invalid only if quad[3] is.
74
if (quad[3] == 255) {
75
continue;
76
}
77
result.push_back(((quad[2] & 0x03) << 6) | (quad[3] & 0x3F));
78
}
79
80
return result;
81
}
82
83