Base64.sol 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. /**
  4. * @dev Provides a set of functions to operate with Base64 strings.
  5. */
  6. library Base64 {
  7. /**
  8. * @dev Base64 Encoding/Decoding Table
  9. */
  10. string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  11. /**
  12. * @dev Converts a `bytes` to its Bytes64 `string` representation.
  13. */
  14. function encode(bytes memory data) internal pure returns (string memory) {
  15. /**
  16. * Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
  17. * https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
  18. */
  19. if (data.length == 0) return "";
  20. // Loads the table into memory
  21. string memory table = _TABLE;
  22. // Encoding takes 3 bytes chunks of binary data from `bytes` data parameter
  23. // and split into 4 numbers of 6 bits.
  24. // The final Base64 length should be `bytes` data length multiplied by 4/3 rounded up
  25. // - `data.length + 2` -> Round up
  26. // - `/ 3` -> Number of 3-bytes chunks
  27. // - `4 *` -> 4 characters for each chunk
  28. string memory result = new string(4 * ((data.length + 2) / 3));
  29. assembly {
  30. // Prepare the lookup table (skip the first "length" byte)
  31. let tablePtr := add(table, 1)
  32. // Prepare result pointer, jump over length
  33. let resultPtr := add(result, 32)
  34. // Run over the input, 3 bytes at a time
  35. for {
  36. let dataPtr := data
  37. let endPtr := add(data, mload(data))
  38. } lt(dataPtr, endPtr) {
  39. } {
  40. // Advance 3 bytes
  41. dataPtr := add(dataPtr, 3)
  42. let input := mload(dataPtr)
  43. // To write each character, shift the 3 bytes (18 bits) chunk
  44. // 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
  45. // and apply logical AND with 0x3F which is the number of
  46. // the previous character in the ASCII table prior to the Base64 Table
  47. // The result is then added to the table to get the character to write,
  48. // and finally write it in the result pointer but with a left shift
  49. // of 256 (1 byte) - 8 (1 ASCII char) = 248 bits
  50. mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
  51. resultPtr := add(resultPtr, 1) // Advance
  52. mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
  53. resultPtr := add(resultPtr, 1) // Advance
  54. mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
  55. resultPtr := add(resultPtr, 1) // Advance
  56. mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
  57. resultPtr := add(resultPtr, 1) // Advance
  58. }
  59. // When data `bytes` is not exactly 3 bytes long
  60. // it is padded with `=` characters at the end
  61. switch mod(mload(data), 3)
  62. case 1 {
  63. mstore8(sub(resultPtr, 1), 0x3d)
  64. mstore8(sub(resultPtr, 2), 0x3d)
  65. }
  66. case 2 {
  67. mstore8(sub(resultPtr, 1), 0x3d)
  68. }
  69. }
  70. return result;
  71. }
  72. }