diff --git a/Userland/Libraries/LibTextCodec/Decoder.cpp b/Userland/Libraries/LibTextCodec/Decoder.cpp index c6cd6445c52..e1f6c57f11a 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.cpp +++ b/Userland/Libraries/LibTextCodec/Decoder.cpp @@ -531,10 +531,15 @@ StringView get_output_encoding(StringView encoding) return encoding; } -bool Decoder::validate(StringView) +bool Decoder::validate(StringView input) { - // By-default we assume that any input sequence is valid, character encodings that do not accept all inputs may override this - return true; + auto result = this->process(input, [](auto code_point) -> ErrorOr { + if (code_point == replacement_code_point) + return Error::from_errno(EINVAL); + return {}; + }); + + return !result.is_error(); } ErrorOr Decoder::to_utf8(StringView input) diff --git a/Userland/Libraries/LibTextCodec/Decoder.h b/Userland/Libraries/LibTextCodec/Decoder.h index 26f161bbac6..0c8d23b0d39 100644 --- a/Userland/Libraries/LibTextCodec/Decoder.h +++ b/Userland/Libraries/LibTextCodec/Decoder.h @@ -62,31 +62,37 @@ private: class Latin1Decoder final : public Decoder { public: virtual ErrorOr process(StringView, Function(u32)> on_code_point) override; + virtual bool validate(StringView) override { return true; } }; class Latin2Decoder final : public Decoder { public: virtual ErrorOr process(StringView, Function(u32)> on_code_point) override; + virtual bool validate(StringView) override { return true; } }; class Latin9Decoder final : public Decoder { public: virtual ErrorOr process(StringView, Function(u32)> on_code_point) override; + virtual bool validate(StringView) override { return true; } }; class PDFDocEncodingDecoder final : public Decoder { public: virtual ErrorOr process(StringView, Function(u32)> on_code_point) override; + virtual bool validate(StringView) override { return true; } }; class TurkishDecoder final : public Decoder { public: virtual ErrorOr process(StringView, Function(u32)> on_code_point) override; + virtual bool validate(StringView) override { return true; } }; class XUserDefinedDecoder final : public Decoder { public: virtual ErrorOr process(StringView, Function(u32)> on_code_point) override; + virtual bool validate(StringView) override { return true; } }; Optional decoder_for(StringView encoding);