Base64.cpp 4.1 KB

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