Skip to content

[WTF] allow Base64EncodeMode::URL to include padding #29530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2024

Conversation

dcrousso
Copy link
Member

@dcrousso dcrousso commented Jun 5, 2024

1d7c9c3

[WTF] allow `Base64EncodeMode::URL` to include padding
https://bugs.webkit.org/show_bug.cgi?id=275141

Reviewed by Yusuke Suzuki.

Technically "base64url" is the exact same as "base64" but the alphabet slightly adjusted (e.g. `_` instead of `/`), and "base64" by default includes padding, so "base64url" should too.

This will assist with implementing `Uint8Array.prototype.toBase64` since that expects padding to be included regardless of the alphabet <https://webkit.org/b/274635>.

* Source/WTF/wtf/text/Base64.h:
(WTF::base64Encode):
(WTF::base64EncodeToVector):
(WTF::base64EncodeToString):
(WTF::base64EncodeToStringReturnNullIfOverflow):
(WTF::base64URLEncodeToVector):
(WTF::base64URLEncodeToString):
(WTF::base64Encoded):
(WTF::calculateBase64EncodedSize):
(WTF::base64URLEncoded):
(WTF::StringTypeAdapter<Base64Specification>::writeTo const):
* Source/WTF/wtf/text/Base64.cpp:
(WTF::base64EncodeInternal):
(WTF::base64Encode):
(WTF::base64EncodeToVector):
(WTF::base64EncodeToString):
(WTF::base64EncodeToStringReturnNullIfOverflow):
Introduce a `Base64EncodeOption` that's wrapped in an `OptionSet` (for future extensibility) as a parameter for adjusting base64 encoding.
Convert `Base64EncodeMode::URL` into `Base64EncodeOption::URL`.
Add a new `Base64EncodeOption::OmitPadding` that controls whether padding is included instead of assuming it should be omitted if `Base64EncodeOption::URL`.

* Tools/TestWebKitAPI/Tests/WTF/Base64.cpp: Added.
(TestWebKitAPI::TEST(Base64, Encode)):
(TestWebKitAPI::TEST(Base64, EncodeOmitPadding)):
(TestWebKitAPI::TEST(Base64, EncodeURL)):
(TestWebKitAPI::TEST(Base64, EncodeURLOmitPadding)):

* Tools/TestWebKitAPI/CMakeLists.txt:
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:

Canonical link: https://commits.webkit.org/280647@main

f082f21

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ❌ 🛠 wpe ✅ 🛠 wincairo
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ❌ 🧪 wpe-wk2 ✅ 🧪 wincairo-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ❌ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ✅ 🧪 mac-wk1 ✅ 🛠 wpe-cairo
✅ 🛠 🧪 jsc ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ❌ 🛠 gtk
❌ 🛠 🧪 jsc-arm64 ⏳ 🛠 vision ✅ 🧪 mac-AS-debug-wk2 ❌ 🧪 gtk-wk2
⏳ 🛠 vision-sim ✅ 🧪 mac-wk2-stress ❌ 🧪 api-gtk
✅ 🛠 🧪 merge ⏳ 🧪 vision-wk2 ✅ 🛠 jsc-armv7
✅ 🛠 tv ✅ 🧪 jsc-armv7-tests
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for cdcee4b. Live statuses available at the PR page, #29530

@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for 4189bd1. Live statuses available at the PR page, #29530

@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for c6a36bb. Live statuses available at the PR page, #29530

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jun 5, 2024
@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for c6a36bb. Live statuses available at the PR page, #29530

@dcrousso dcrousso removed the merging-blocked Applied to prevent a change from being merged label Jun 6, 2024
@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for c7922b9. Live statuses available at the PR page, #29530

@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for c7922b9. Live statuses available at the PR page, #29530

1 similar comment
@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for c7922b9. Live statuses available at the PR page, #29530

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jun 6, 2024
@annevk
Copy link
Contributor

annevk commented Jun 10, 2024

Hmm, part of the premise of this JS API was that it wouldn't require implementations to support more permutations. I guess this is not too bad, but it still seems unfortunate.

Any reason not to just make it three modes?

  • Default
  • URLWithoutPadding
  • URL

I don't think you ever want the Default without padding.

@annevk
Copy link
Contributor

annevk commented Jun 10, 2024

Also some test coverage would be good.

@dcrousso
Copy link
Member Author

Hmm, part of the premise of this JS API was that it wouldn't require implementations to support more permutations. I guess this is not too bad, but it still seems unfortunate.

Any reason not to just make it three modes?

  • Default
  • URLWithoutPadding
  • URL

I don't think you ever want the Default without padding.

actually, there's currently some discussion around having an omitPadding option for both alphabet: "base64" and alphabet: "base64url" tc39/proposal-arraybuffer-base64#59

i think an options set is probably preferred as it allows for far more flexibility in the future if new things are added

@dcrousso dcrousso removed the merging-blocked Applied to prevent a change from being merged label Jun 11, 2024
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jun 11, 2024
@dcrousso dcrousso removed the merging-blocked Applied to prevent a change from being merged label Jun 18, 2024
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jun 18, 2024
@dcrousso dcrousso removed the merging-blocked Applied to prevent a change from being merged label Jun 18, 2024
Copy link
Member

@Constellation Constellation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me

@dcrousso dcrousso added the merge-queue Applied to send a pull request to merge-queue label Jul 3, 2024
https://bugs.webkit.org/show_bug.cgi?id=275141

Reviewed by Yusuke Suzuki.

Technically "base64url" is the exact same as "base64" but the alphabet slightly adjusted (e.g. `_` instead of `/`), and "base64" by default includes padding, so "base64url" should too.

This will assist with implementing `Uint8Array.prototype.toBase64` since that expects padding to be included regardless of the alphabet <https://webkit.org/b/274635>.

* Source/WTF/wtf/text/Base64.h:
(WTF::base64Encode):
(WTF::base64EncodeToVector):
(WTF::base64EncodeToString):
(WTF::base64EncodeToStringReturnNullIfOverflow):
(WTF::base64URLEncodeToVector):
(WTF::base64URLEncodeToString):
(WTF::base64Encoded):
(WTF::calculateBase64EncodedSize):
(WTF::base64URLEncoded):
(WTF::StringTypeAdapter<Base64Specification>::writeTo const):
* Source/WTF/wtf/text/Base64.cpp:
(WTF::base64EncodeInternal):
(WTF::base64Encode):
(WTF::base64EncodeToVector):
(WTF::base64EncodeToString):
(WTF::base64EncodeToStringReturnNullIfOverflow):
Introduce a `Base64EncodeOption` that's wrapped in an `OptionSet` (for future extensibility) as a parameter for adjusting base64 encoding.
Convert `Base64EncodeMode::URL` into `Base64EncodeOption::URL`.
Add a new `Base64EncodeOption::OmitPadding` that controls whether padding is included instead of assuming it should be omitted if `Base64EncodeOption::URL`.

* Tools/TestWebKitAPI/Tests/WTF/Base64.cpp: Added.
(TestWebKitAPI::TEST(Base64, Encode)):
(TestWebKitAPI::TEST(Base64, EncodeOmitPadding)):
(TestWebKitAPI::TEST(Base64, EncodeURL)):
(TestWebKitAPI::TEST(Base64, EncodeURLOmitPadding)):

* Tools/TestWebKitAPI/CMakeLists.txt:
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:

Canonical link: https://commits.webkit.org/280647@main
@webkit-commit-queue
Copy link
Collaborator

Committed 280647@main (1d7c9c3): https://commits.webkit.org/280647@main

Reviewed commits have been landed. Closing PR #29530 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit 1d7c9c3 into WebKit:main Jul 4, 2024
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Jul 4, 2024
@dcrousso dcrousso deleted the eng/275141 branch July 4, 2024 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants