Path: blob/main/contrib/llvm-project/llvm/lib/Support/Base64.cpp
35233 views
//===- Base64.cpp ---------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#define INVALID_BASE64_BYTE 649#include "llvm/Support/Base64.h"1011static char decodeBase64Byte(uint8_t Ch) {12constexpr char Inv = INVALID_BASE64_BYTE;13static const char DecodeTable[] = {14Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, // ........15Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, // ........16Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, // ........17Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, // ........18Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, // ........19Inv, Inv, Inv, 62, Inv, Inv, Inv, 63, // ...+.../2052, 53, 54, 55, 56, 57, 58, 59, // 012345672160, 61, Inv, Inv, Inv, 0, Inv, Inv, // 89...=..22Inv, 0, 1, 2, 3, 4, 5, 6, // .ABCDEFG237, 8, 9, 10, 11, 12, 13, 14, // HIJKLMNO2415, 16, 17, 18, 19, 20, 21, 22, // PQRSTUVW2523, 24, 25, Inv, Inv, Inv, Inv, Inv, // XYZ.....26Inv, 26, 27, 28, 29, 30, 31, 32, // .abcdefg2733, 34, 35, 36, 37, 38, 39, 40, // hijklmno2841, 42, 43, 44, 45, 46, 47, 48, // pqrstuvw2949, 50, 51 // xyz.....30};31if (Ch >= sizeof(DecodeTable))32return Inv;33return DecodeTable[Ch];34}3536llvm::Error llvm::decodeBase64(llvm::StringRef Input,37std::vector<char> &Output) {38constexpr char Base64InvalidByte = INVALID_BASE64_BYTE;39// Invalid table value with short name to fit in the table init below. The40// invalid value is 64 since valid base64 values are 0 - 63.41Output.clear();42const uint64_t InputLength = Input.size();43if (InputLength == 0)44return Error::success();45// Make sure we have a valid input string length which must be a multiple46// of 4.47if ((InputLength % 4) != 0)48return createStringError(std::errc::illegal_byte_sequence,49"Base64 encoded strings must be a multiple of 4 "50"bytes in length");51const uint64_t FirstValidEqualIdx = InputLength - 2;52char Hex64Bytes[4];53for (uint64_t Idx = 0; Idx < InputLength; Idx += 4) {54for (uint64_t ByteOffset = 0; ByteOffset < 4; ++ByteOffset) {55const uint64_t ByteIdx = Idx + ByteOffset;56const char Byte = Input[ByteIdx];57const char DecodedByte = decodeBase64Byte(Byte);58bool Illegal = DecodedByte == Base64InvalidByte;59if (!Illegal && Byte == '=') {60if (ByteIdx < FirstValidEqualIdx) {61// We have an '=' in the middle of the string which is invalid, only62// the last two characters can be '=' characters.63Illegal = true;64} else if (ByteIdx == FirstValidEqualIdx && Input[ByteIdx + 1] != '=') {65// We have an equal second to last from the end and the last character66// is not also an equal, so the '=' character is invalid67Illegal = true;68}69}70if (Illegal)71return createStringError(72std::errc::illegal_byte_sequence,73"Invalid Base64 character %#2.2x at index %" PRIu64, Byte, ByteIdx);74Hex64Bytes[ByteOffset] = DecodedByte;75}76// Now we have 6 bits of 3 bytes in value in each of the Hex64Bytes bytes.77// Extract the right bytes into the Output buffer.78Output.push_back((Hex64Bytes[0] << 2) + ((Hex64Bytes[1] >> 4) & 0x03));79Output.push_back((Hex64Bytes[1] << 4) + ((Hex64Bytes[2] >> 2) & 0x0f));80Output.push_back((Hex64Bytes[2] << 6) + (Hex64Bytes[3] & 0x3f));81}82// If we had valid trailing '=' characters strip the right number of bytes83// from the end of the output buffer. We already know that the Input length84// it a multiple of 4 and is not zero, so direct character access is safe.85if (Input.back() == '=') {86Output.pop_back();87if (Input[InputLength - 2] == '=')88Output.pop_back();89}90return Error::success();91}929394