LLVM: lib/Support/Base64.cpp Source File (original) (raw)
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,
30 49, 50, 51
31 };
32 if (Ch >= sizeof(DecodeTable))
33 return Inv;
34 return DecodeTable[Ch];
35}
38 std::vector &Output) {
40
41
42 Output.clear();
44 if (InputLength == 0)
46
47
48 if ((InputLength % 4) != 0)
50 "Base64 encoded strings must be a multiple of 4 "
51 "bytes in length");
52 const uint64_t FirstValidEqualIdx = InputLength - 2;
53 char Hex64Bytes[4];
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;
60 if ( && Byte == '=') {
61 if (ByteIdx < FirstValidEqualIdx) {
62
63
65 } else if (ByteIdx == FirstValidEqualIdx && Input[ByteIdx + 1] != '=') {
66
67
69 }
70 }
73 std::errc::illegal_byte_sequence,
74 "Invalid Base64 character %#2.2x at index %" PRIu64, Byte, ByteIdx);
75 Hex64Bytes[ByteOffset] = DecodedByte;
76 }
77
78
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));
82 }
83
84
85
86 if (Input.back() == '=') {
87 Output.pop_back();
88 if (Input[InputLength - 2] == '=')
89 Output.pop_back();
90 }
92}