Base64.sol 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (utils/Base64.sol)
  3. pragma solidity ^0.8.20;
  4. /**
  5. * @dev Provides a set of functions to operate with Base64 strings.
  6. */
  7. library Base64 {
  8. /**
  9. * @dev Base64 Encoding/Decoding Table
  10. * See sections 4 and 5 of https://datatracker.ietf.org/doc/html/rfc4648
  11. */
  12. string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  13. string internal constant _TABLE_URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  14. /**
  15. * @dev Converts a `bytes` to its Bytes64 `string` representation.
  16. */
  17. function encode(bytes memory data) internal pure returns (string memory) {
  18. return _encode(data, _TABLE, true);
  19. }
  20. /**
  21. * @dev Converts a `bytes` to its Bytes64Url `string` representation.
  22. */
  23. function encodeURL(bytes memory data) internal pure returns (string memory) {
  24. return _encode(data, _TABLE_URL, false);
  25. }
  26. /**
  27. * @dev Internal table-agnostic conversion
  28. */
  29. function _encode(bytes memory data, string memory table, bool withPadding) private pure returns (string memory) {
  30. /**
  31. * Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
  32. * https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
  33. */
  34. if (data.length == 0) return "";
  35. // If padding is enabled, the final length should be `bytes` data length divided by 3 rounded up and then
  36. // multiplied by 4 so that it leaves room for padding the last chunk
  37. // - `data.length + 2` -> Round up
  38. // - `/ 3` -> Number of 3-bytes chunks
  39. // - `4 *` -> 4 characters for each chunk
  40. // If padding is disabled, the final length should be `bytes` data length multiplied by 4/3 rounded up as
  41. // opposed to when padding is required to fill the last chunk.
  42. // - `4 *` -> 4 characters for each chunk
  43. // - `data.length + 2` -> Round up
  44. // - `/ 3` -> Number of 3-bytes chunks
  45. uint256 resultLength = withPadding ? 4 * ((data.length + 2) / 3) : (4 * data.length + 2) / 3;
  46. string memory result = new string(resultLength);
  47. /// @solidity memory-safe-assembly
  48. assembly {
  49. // Prepare the lookup table (skip the first "length" byte)
  50. let tablePtr := add(table, 1)
  51. // Prepare result pointer, jump over length
  52. let resultPtr := add(result, 0x20)
  53. let dataPtr := data
  54. let endPtr := add(data, mload(data))
  55. // In some cases, the last iteration will read bytes after the end of the data. We cache the value, and
  56. // set it to zero to make sure no dirty bytes are read in that section.
  57. let afterPtr := add(endPtr, 0x20)
  58. let afterCache := mload(afterPtr)
  59. mstore(afterPtr, 0x00)
  60. // Run over the input, 3 bytes at a time
  61. for {
  62. } lt(dataPtr, endPtr) {
  63. } {
  64. // Advance 3 bytes
  65. dataPtr := add(dataPtr, 3)
  66. let input := mload(dataPtr)
  67. // To write each character, shift the 3 byte (24 bits) chunk
  68. // 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
  69. // and apply logical AND with 0x3F to bitmask the least significant 6 bits.
  70. // Use this as an index into the lookup table, mload an entire word
  71. // so the desired character is in the least significant byte, and
  72. // mstore8 this least significant byte into the result and continue.
  73. mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
  74. resultPtr := add(resultPtr, 1) // Advance
  75. mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
  76. resultPtr := add(resultPtr, 1) // Advance
  77. mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
  78. resultPtr := add(resultPtr, 1) // Advance
  79. mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
  80. resultPtr := add(resultPtr, 1) // Advance
  81. }
  82. // Reset the value that was cached
  83. mstore(afterPtr, afterCache)
  84. if withPadding {
  85. // When data `bytes` is not exactly 3 bytes long
  86. // it is padded with `=` characters at the end
  87. switch mod(mload(data), 3)
  88. case 1 {
  89. mstore8(sub(resultPtr, 1), 0x3d)
  90. mstore8(sub(resultPtr, 2), 0x3d)
  91. }
  92. case 2 {
  93. mstore8(sub(resultPtr, 1), 0x3d)
  94. }
  95. }
  96. }
  97. return result;
  98. }
  99. }