This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update base64url library to match latest FCL lib
- Loading branch information
1 parent
96add0b
commit fd24668
Showing
3 changed files
with
80 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @dev Encode (without '=' padding) | ||
* @author evmbrahmin, adapted from hiromin's Base64URL libraries | ||
*/ | ||
library Base64Url { | ||
/** | ||
* @dev Base64Url Encoding Table | ||
*/ | ||
string internal constant ENCODING_TABLE = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | ||
|
||
function encode(bytes memory data) internal pure returns (string memory) { | ||
if (data.length == 0) return ""; | ||
|
||
// Load the table into memory | ||
string memory table = ENCODING_TABLE; | ||
|
||
string memory result = new string(4 * ((data.length + 2) / 3)); | ||
|
||
// @solidity memory-safe-assembly | ||
assembly { | ||
let tablePtr := add(table, 1) | ||
let resultPtr := add(result, 32) | ||
|
||
for { | ||
let dataPtr := data | ||
let endPtr := add(data, mload(data)) | ||
} lt(dataPtr, endPtr) { | ||
|
||
} { | ||
dataPtr := add(dataPtr, 3) | ||
let input := mload(dataPtr) | ||
|
||
mstore8( | ||
resultPtr, | ||
mload(add(tablePtr, and(shr(18, input), 0x3F))) | ||
) | ||
resultPtr := add(resultPtr, 1) | ||
|
||
mstore8( | ||
resultPtr, | ||
mload(add(tablePtr, and(shr(12, input), 0x3F))) | ||
) | ||
resultPtr := add(resultPtr, 1) | ||
|
||
mstore8( | ||
resultPtr, | ||
mload(add(tablePtr, and(shr(6, input), 0x3F))) | ||
) | ||
resultPtr := add(resultPtr, 1) | ||
|
||
mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F)))) | ||
resultPtr := add(resultPtr, 1) | ||
} | ||
|
||
// Remove the padding adjustment logic | ||
switch mod(mload(data), 3) | ||
case 1 { | ||
// Adjust for the last byte of data | ||
resultPtr := sub(resultPtr, 2) | ||
} | ||
case 2 { | ||
// Adjust for the last two bytes of data | ||
resultPtr := sub(resultPtr, 1) | ||
} | ||
|
||
// Set the correct length of the result string | ||
mstore(result, sub(resultPtr, add(result, 32))) | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters