14 static const char DecodeTable[] = {
15 Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv,
16 Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv,
17 Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv,
18 Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv,
19 Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv,
20 Inv, Inv, Inv, 62, Inv, Inv, Inv, 63,
21 52, 53, 54, 55, 56, 57, 58, 59,
22 60, 61, Inv, Inv, Inv, 0, Inv, Inv,
23 Inv, 0, 1, 2, 3, 4, 5, 6,
24 7, 8, 9, 10, 11, 12, 13, 14,
25 15, 16, 17, 18, 19, 20, 21, 22,
26 23, 24, 25, Inv, Inv, Inv, Inv, Inv,
27 Inv, 26, 27, 28, 29, 30, 31, 32,
28 33, 34, 35, 36, 37, 38, 39, 40,
29 41, 42, 43, 44, 45, 46, 47, 48,
32 if (Ch >=
sizeof(DecodeTable))
34 return DecodeTable[Ch];
38 std::vector<char> &Output) {
48 if ((InputLength % 4) != 0)
50 "Base64 encoded strings must be a multiple of 4 "
52 const uint64_t FirstValidEqualIdx = InputLength - 2;
54 for (
uint64_t Idx = 0; Idx < InputLength; Idx += 4) {
55 for (
uint64_t ByteOffset = 0; ByteOffset < 4; ++ByteOffset) {
56 const uint64_t ByteIdx = Idx + ByteOffset;
57 const char Byte =
Input[ByteIdx];
59 bool Illegal = DecodedByte == Base64InvalidByte;
61 if (ByteIdx < FirstValidEqualIdx) {
65 }
else if (ByteIdx == FirstValidEqualIdx &&
Input[ByteIdx + 1] !=
'=') {
73 std::errc::illegal_byte_sequence,
74 "Invalid Base64 character %#2.2x at index %" PRIu64, Byte, ByteIdx);
75 Hex64Bytes[ByteOffset] = DecodedByte;
79 Output.push_back((Hex64Bytes[0] << 2) + ((Hex64Bytes[1] >> 4) & 0x03));
80 Output.push_back((Hex64Bytes[1] << 4) + ((Hex64Bytes[2] >> 2) & 0x0f));
81 Output.push_back((Hex64Bytes[2] << 6) + (Hex64Bytes[3] & 0x3f));
86 if (
Input.back() ==
'=') {
88 if (
Input[InputLength - 2] ==
'=')