Base64.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.2) (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` -> Prepare for division rounding up
  38. // - `/ 3` -> Number of 3-bytes chunks (rounded up)
  39. // - `4 *` -> 4 characters for each chunk
  40. // This is equivalent to: 4 * Math.ceil(data.length / 3)
  41. //
  42. // If padding is disabled, the final length should be `bytes` data length multiplied by 4/3 rounded up as
  43. // opposed to when padding is required to fill the last chunk.
  44. // - `4 * data.length` -> 4 characters for each chunk
  45. // - ` + 2` -> Prepare for division rounding up
  46. // - `/ 3` -> Number of 3-bytes chunks (rounded up)
  47. // This is equivalent to: Math.ceil((4 * data.length) / 3)
  48. uint256 resultLength = withPadding ? 4 * ((data.length + 2) / 3) : (4 * data.length + 2) / 3;
  49. string memory result = new string(resultLength);
  50. assembly ("memory-safe") {
  51. // Prepare the lookup table (skip the first "length" byte)
  52. let tablePtr := add(table, 1)
  53. // Prepare result pointer, jump over length
  54. let resultPtr := add(result, 0x20)
  55. let dataPtr := data
  56. let endPtr := add(data, mload(data))
  57. // In some cases, the last iteration will read bytes after the end of the data. We cache the value, and
  58. // set it to zero to make sure no dirty bytes are read in that section.
  59. let afterPtr := add(endPtr, 0x20)
  60. let afterCache := mload(afterPtr)
  61. mstore(afterPtr, 0x00)
  62. // Run over the input, 3 bytes at a time
  63. for {
  64. } lt(dataPtr, endPtr) {
  65. } {
  66. // Advance 3 bytes
  67. dataPtr := add(dataPtr, 3)
  68. let input := mload(dataPtr)
  69. // To write each character, shift the 3 byte (24 bits) chunk
  70. // 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
  71. // and apply logical AND with 0x3F to bitmask the least significant 6 bits.
  72. // Use this as an index into the lookup table, mload an entire word
  73. // so the desired character is in the least significant byte, and
  74. // mstore8 this least significant byte into the result and continue.
  75. mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
  76. resultPtr := add(resultPtr, 1) // Advance
  77. mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
  78. resultPtr := add(resultPtr, 1) // Advance
  79. mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
  80. resultPtr := add(resultPtr, 1) // Advance
  81. mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
  82. resultPtr := add(resultPtr, 1) // Advance
  83. }
  84. // Reset the value that was cached
  85. mstore(afterPtr, afterCache)
  86. if withPadding {
  87. // When data `bytes` is not exactly 3 bytes long
  88. // it is padded with `=` characters at the end
  89. switch mod(mload(data), 3)
  90. case 1 {
  91. mstore8(sub(resultPtr, 1), 0x3d)
  92. mstore8(sub(resultPtr, 2), 0x3d)
  93. }
  94. case 2 {
  95. mstore8(sub(resultPtr, 1), 0x3d)
  96. }
  97. }
  98. }
  99. return result;
  100. }
  101. }