9#define INVALID_BASE64_BYTE 64
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) {
45 return Error::success();
48 if ((InputLength % 4) != 0)
50 "Base64 encoded strings must be a multiple of 4 "
52 const uint64_t FirstValidEqualIdx = InputLength - 2;
55 for (
uint64_t ByteOffset = 0; ByteOffset < 4; ++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] ==
'=')
91 return Error::success();
static char decodeBase64Byte(uint8_t Ch)
#define INVALID_BASE64_BYTE
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
char back() const
back - Get the last character in the string.
constexpr size_t size() const
size - Get the string size.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
llvm::Error decodeBase64(llvm::StringRef Input, std::vector< char > &Output)