Add explicit IgnoreBOM and ErrorMode options to LibTextCodec decoders, and thread them through TextDecoder and TextDecoderStream. This lets Web-facing decoder APIs preserve BOMs when requested and use fatal error handling without post-processing decoded output. NB: RemoveBOM was renamed to IgnoreBOM as "RemoveBOM" is the name used by encoding_rs and was previously an implementation detail. The new name matches what is used by the encoding standard as it is now also used in LibWeb.
29 lines
936 B
C++
29 lines
936 B
C++
/*
|
|
* Copyright (c) 2021-2023, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibTextCodec/Decoder.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
|
|
{
|
|
AK::set_debug_enabled(false);
|
|
|
|
static constexpr StringView MAGIC_SEPARATOR = "|DATA|"sv;
|
|
StringView data_string_view { data, size };
|
|
auto separator_index = data_string_view.find(MAGIC_SEPARATOR);
|
|
if (!separator_index.has_value())
|
|
return 0;
|
|
|
|
auto encoding = data_string_view.substring_view(0, separator_index.value());
|
|
auto encoded_data = data_string_view.substring_view(separator_index.value() + MAGIC_SEPARATOR.length());
|
|
auto decoder = TextCodec::decoder_for(encoding);
|
|
if (!decoder.has_value())
|
|
return 0;
|
|
|
|
(void)decoder->to_utf8(encoded_data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement);
|
|
return 0;
|
|
}
|