-
Notifications
You must be signed in to change notification settings - Fork 540
handle whatwg encoding standard overrides #6091
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
Open
anonrig
wants to merge
3
commits into
main
Choose a base branch
from
yagiz/fix-textdecoder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+900
−277
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,11 @@ | ||
| load("//:build/wd_rust_crate.bzl", "wd_rust_crate") | ||
|
|
||
| wd_rust_crate( | ||
| name = "encoding", | ||
| cxx_bridge_src = "lib.rs", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//src/rust/cxx-integration", | ||
| "@crates_vendor//:encoding_rs", | ||
| ], | ||
| ) |
This file contains hidden or 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,150 @@ | ||
| // Copyright (c) 2017-2022 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| //! WHATWG Encoding Standard legacy decoders via `encoding_rs`. | ||
| //! | ||
| //! Exposes a streaming decoder to C++ via CXX bridge. All legacy encodings | ||
| //! (CJK multi-byte, single-byte windows-1252, and x-user-defined) are handled | ||
| //! by a single opaque `Decoder` type backed by `encoding_rs::Decoder`. | ||
|
|
||
| #[cxx::bridge(namespace = "workerd::rust::encoding")] | ||
| mod ffi { | ||
| /// Legacy encoding types supported by the Rust decoder. | ||
| /// Shared between C++ and Rust. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| #[repr(u16)] | ||
| enum Encoding { | ||
| Big5, | ||
| EucJp, | ||
| EucKr, | ||
| Gb18030, | ||
| Gbk, | ||
| Iso2022Jp, | ||
| ShiftJis, | ||
| Windows1252, | ||
| XUserDefined, | ||
| } | ||
|
|
||
| /// Result of a decode operation. | ||
| struct DecodeResult { | ||
| /// UTF-16 output. | ||
| output: Vec<u16>, | ||
| /// True if a fatal decoding error was encountered. Only meaningful | ||
| /// when the caller requested fatal mode — in replacement mode errors | ||
| /// are silently replaced with U+FFFD and this flag is not set. | ||
| had_error: bool, | ||
| } | ||
|
|
||
| extern "Rust" { | ||
| type Decoder; | ||
|
|
||
| /// Create a new streaming decoder for the given encoding. | ||
| // CXX bridge requires Box for opaque types. | ||
| #[expect(clippy::unnecessary_box_returns)] | ||
| fn new_decoder(encoding: Encoding) -> Box<Decoder>; | ||
|
|
||
| /// Decode a chunk of bytes. Set `flush` to true on the final chunk. | ||
| /// When `fatal` is true and an error is encountered, `had_error` is | ||
| /// set and the output may be incomplete. | ||
| fn decode(decoder: &mut Decoder, input: &[u8], flush: bool, fatal: bool) -> DecodeResult; | ||
|
|
||
| /// Reset the decoder to its initial state. | ||
| fn reset(decoder: &mut Decoder); | ||
| } | ||
| } | ||
|
|
||
| /// Opaque decoder state exposed to C++ via `Box<Decoder>`. | ||
| pub struct Decoder { | ||
| encoding: &'static encoding_rs::Encoding, | ||
| inner: encoding_rs::Decoder, | ||
| } | ||
|
|
||
| /// Map a CXX-shared `Encoding` variant to the corresponding | ||
| /// `encoding_rs` static. | ||
| fn to_encoding(enc: ffi::Encoding) -> &'static encoding_rs::Encoding { | ||
| match enc { | ||
| ffi::Encoding::Big5 => encoding_rs::BIG5, | ||
| ffi::Encoding::EucJp => encoding_rs::EUC_JP, | ||
| ffi::Encoding::EucKr => encoding_rs::EUC_KR, | ||
| ffi::Encoding::Gb18030 => encoding_rs::GB18030, | ||
| ffi::Encoding::Gbk => encoding_rs::GBK, | ||
| ffi::Encoding::Iso2022Jp => encoding_rs::ISO_2022_JP, | ||
| ffi::Encoding::ShiftJis => encoding_rs::SHIFT_JIS, | ||
| ffi::Encoding::Windows1252 => encoding_rs::WINDOWS_1252, | ||
| ffi::Encoding::XUserDefined => encoding_rs::X_USER_DEFINED, | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
|
|
||
| pub fn new_decoder(encoding: ffi::Encoding) -> Box<Decoder> { | ||
| let encoding = to_encoding(encoding); | ||
| Box::new(Decoder { | ||
| inner: encoding.new_decoder_without_bom_handling(), | ||
| encoding, | ||
| }) | ||
| } | ||
|
|
||
| pub fn decode(state: &mut Decoder, input: &[u8], flush: bool, fatal: bool) -> ffi::DecodeResult { | ||
| let max_len = state | ||
| .inner | ||
| .max_utf16_buffer_length(input.len()) | ||
| .unwrap_or(input.len() + 4); | ||
| let mut output = vec![0u16; max_len]; | ||
| let mut total_read = 0usize; | ||
| let mut total_written = 0usize; | ||
|
|
||
| if fatal { | ||
| loop { | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let (result, read, written) = state.inner.decode_to_utf16_without_replacement( | ||
| &input[total_read..], | ||
| &mut output[total_written..], | ||
| flush, | ||
| ); | ||
| total_read += read; | ||
| total_written += written; | ||
|
|
||
| match result { | ||
| encoding_rs::DecoderResult::InputEmpty => break, | ||
| encoding_rs::DecoderResult::OutputFull => { | ||
| output.resize(output.len() * 2, 0); | ||
| } | ||
| encoding_rs::DecoderResult::Malformed(_, _) => { | ||
| state.inner = state.encoding.new_decoder_without_bom_handling(); | ||
| output.truncate(total_written); | ||
| return ffi::DecodeResult { | ||
| output, | ||
| had_error: true, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| loop { | ||
| let (result, read, written, _had_errors) = state.inner.decode_to_utf16( | ||
| &input[total_read..], | ||
| &mut output[total_written..], | ||
| flush, | ||
| ); | ||
| total_read += read; | ||
| total_written += written; | ||
|
|
||
| match result { | ||
| encoding_rs::CoderResult::InputEmpty => break, | ||
| encoding_rs::CoderResult::OutputFull => { | ||
| output.resize(output.len() * 2, 0); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| output.truncate(total_written); | ||
| ffi::DecodeResult { | ||
| output, | ||
| had_error: false, | ||
| } | ||
| } | ||
|
|
||
| pub fn reset(state: &mut Decoder) { | ||
| state.inner = state.encoding.new_decoder_without_bom_handling(); | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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,75 @@ | ||
| // Copyright (c) 2017-2022 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| #include "encoding-legacy.h" | ||
|
|
||
| #include <kj-rs/convert.h> | ||
| #include <rust/cxx.h> | ||
|
|
||
| #include <kj/common.h> | ||
|
|
||
| namespace workerd::api { | ||
|
|
||
| namespace { | ||
|
|
||
| // Map workerd::api::Encoding to the Rust-side RustEncoding enum. | ||
| ::workerd::rust::encoding::Encoding toRustEncoding(Encoding encoding) { | ||
| using RE = ::workerd::rust::encoding::Encoding; | ||
| switch (encoding) { | ||
| case Encoding::Big5: | ||
| return RE::Big5; | ||
| case Encoding::Euc_Jp: | ||
| return RE::EucJp; | ||
| case Encoding::Euc_Kr: | ||
| return RE::EucKr; | ||
| case Encoding::Gb18030: | ||
| return RE::Gb18030; | ||
| case Encoding::Gbk: | ||
| return RE::Gbk; | ||
| case Encoding::Iso2022_Jp: | ||
| return RE::Iso2022Jp; | ||
| case Encoding::Shift_Jis: | ||
| return RE::ShiftJis; | ||
| case Encoding::Windows_1252: | ||
| return RE::Windows1252; | ||
| case Encoding::X_User_Defined: | ||
| return RE::XUserDefined; | ||
| default: | ||
| KJ_UNREACHABLE; | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| LegacyDecoder::LegacyDecoder(Encoding encoding, DecoderFatal fatal) | ||
| : encoding(encoding), | ||
| fatal(fatal), | ||
| state(::workerd::rust::encoding::new_decoder(toRustEncoding(encoding))) {} | ||
|
|
||
| void LegacyDecoder::reset() { | ||
| ::workerd::rust::encoding::reset(*state); | ||
| } | ||
|
|
||
| kj::Maybe<jsg::JsString> LegacyDecoder::decode( | ||
| jsg::Lock& js, kj::ArrayPtr<const kj::byte> buffer, bool flush) { | ||
| // Reset decoder state after flush, matching IcuDecoder's KJ_DEFER contract. | ||
| // This ensures decodePtr() (used by TextDecoderStream) resets correctly on flush. | ||
| KJ_DEFER({ | ||
| if (flush) reset(); | ||
| }); | ||
|
|
||
| // kj_rs::RustMutable is used to avoid a copy of the underlying buffer. | ||
| auto result = ::workerd::rust::encoding::decode( | ||
| *state, buffer.as<kj_rs::RustMutable>(), flush, fatal.toBool()); | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (fatal.toBool() && result.had_error) { | ||
| // Decoder state already reset by the Rust side on fatal error. | ||
| return kj::none; | ||
| } | ||
|
|
||
| auto output = kj::from<kj_rs::Rust>(result.output); | ||
| return js.str(output); | ||
| } | ||
|
|
||
| } // namespace workerd::api | ||
This file contains hidden or 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,51 @@ | ||
| // Copyright (c) 2017-2022 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| // WHATWG-compliant legacy decoders (CJK multi-byte, windows-1252, | ||
| // x-user-defined) implemented via the encoding_rs Rust crate through | ||
| // a CXX bridge. A single LegacyDecoder class wraps an opaque Rust-side | ||
| // decoder that handles all the encoding-specific state machines. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "encoding-shared.h" | ||
|
|
||
| #include <workerd/rust/encoding/lib.rs.h> | ||
|
|
||
| #include <rust/cxx.h> | ||
|
|
||
| #include <kj/common.h> | ||
|
|
||
| namespace workerd::api { | ||
|
|
||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Unified legacy decoder using encoding_rs via Rust CXX bridge. | ||
| // encoding_rs implements the full WHATWG decoder algorithms for all | ||
| // legacy encodings, including streaming, error recovery, and ASCII | ||
| // byte pushback. | ||
| // | ||
| // According to WHATWG spec, any encoding except UTF-8 and UTF-16 is considered legacy. | ||
| class LegacyDecoder final: public Decoder { | ||
| public: | ||
| LegacyDecoder(Encoding encoding, DecoderFatal fatal); | ||
| ~LegacyDecoder() noexcept = default; | ||
| LegacyDecoder(LegacyDecoder&&) noexcept = default; | ||
| LegacyDecoder& operator=(LegacyDecoder&&) noexcept = default; | ||
| KJ_DISALLOW_COPY(LegacyDecoder); | ||
|
|
||
| Encoding getEncoding() override { | ||
| return encoding; | ||
| } | ||
|
|
||
| kj::Maybe<jsg::JsString> decode( | ||
| jsg::Lock& js, kj::ArrayPtr<const kj::byte> buffer, bool flush = false) override; | ||
|
|
||
| void reset() override; | ||
|
|
||
| private: | ||
| Encoding encoding; | ||
| DecoderFatal fatal; | ||
| ::rust::Box<::workerd::rust::encoding::Decoder> state; | ||
| }; | ||
|
|
||
| } // namespace workerd::api | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.