2020-05-03 22:41:34 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
|
2024-11-01 12:14:53 +01:00
|
|
|
* Copyright (c) 2022, Jelle Raaijmakers <jelle@ladybird.org>
|
2023-02-17 17:45:08 +00:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
2020-05-03 22:41:34 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-03 22:41:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
2021-08-29 11:44:28 +00:00
|
|
|
#include <AK/Function.h>
|
2023-02-17 17:45:08 +00:00
|
|
|
#include <AK/Optional.h>
|
2023-02-17 20:15:10 +00:00
|
|
|
#include <AK/String.h>
|
2020-05-03 22:41:34 +02:00
|
|
|
|
|
|
|
namespace TextCodec {
|
|
|
|
|
|
|
|
class Decoder {
|
|
|
|
public:
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) = 0;
|
2023-11-17 15:08:44 +02:00
|
|
|
virtual bool validate(StringView);
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<String> to_utf8(StringView);
|
2021-04-15 10:43:29 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~Decoder() = default;
|
2020-05-03 22:41:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class UTF8Decoder final : public Decoder {
|
|
|
|
public:
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
2023-11-17 15:08:44 +02:00
|
|
|
virtual bool validate(StringView) override;
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<String> to_utf8(StringView) override;
|
2020-05-03 22:41:34 +02:00
|
|
|
};
|
|
|
|
|
2021-02-16 17:31:22 +01:00
|
|
|
class UTF16BEDecoder final : public Decoder {
|
|
|
|
public:
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
2023-11-17 15:08:44 +02:00
|
|
|
virtual bool validate(StringView) override;
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<String> to_utf8(StringView) override;
|
2021-02-16 17:31:22 +01:00
|
|
|
};
|
|
|
|
|
2022-03-08 14:27:11 +01:00
|
|
|
class UTF16LEDecoder final : public Decoder {
|
|
|
|
public:
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
2023-11-17 15:08:44 +02:00
|
|
|
virtual bool validate(StringView) override;
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<String> to_utf8(StringView) override;
|
2022-03-08 14:27:11 +01:00
|
|
|
};
|
|
|
|
|
2024-06-02 15:56:36 +02:00
|
|
|
template<Integral ArrayType = u32>
|
2024-05-27 17:57:12 +02:00
|
|
|
class SingleByteDecoder final : public Decoder {
|
|
|
|
public:
|
2024-06-02 15:56:36 +02:00
|
|
|
SingleByteDecoder(Array<ArrayType, 128> translation_table)
|
2024-05-27 17:57:12 +02:00
|
|
|
: m_translation_table(translation_table)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
|
|
|
|
private:
|
2024-06-02 15:56:36 +02:00
|
|
|
Array<ArrayType, 128> m_translation_table;
|
2024-05-27 17:57:12 +02:00
|
|
|
};
|
|
|
|
|
2020-05-03 22:41:34 +02:00
|
|
|
class Latin1Decoder final : public Decoder {
|
|
|
|
public:
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
2024-05-30 12:37:29 +02:00
|
|
|
virtual bool validate(StringView) override { return true; }
|
2020-05-03 22:41:34 +02:00
|
|
|
};
|
|
|
|
|
2023-11-21 21:07:13 -05:00
|
|
|
class PDFDocEncodingDecoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
2024-05-30 12:37:29 +02:00
|
|
|
virtual bool validate(StringView) override { return true; }
|
2023-11-21 21:07:13 -05:00
|
|
|
};
|
|
|
|
|
2022-02-11 20:38:44 +00:00
|
|
|
class XUserDefinedDecoder final : public Decoder {
|
|
|
|
public:
|
2023-02-17 20:15:10 +00:00
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
2024-05-30 12:37:29 +02:00
|
|
|
virtual bool validate(StringView) override { return true; }
|
2022-02-11 20:38:44 +00:00
|
|
|
};
|
|
|
|
|
2024-05-28 02:34:24 +02:00
|
|
|
class GB18030Decoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
};
|
|
|
|
|
2024-05-28 10:18:50 +02:00
|
|
|
class Big5Decoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
};
|
|
|
|
|
2024-05-28 23:41:40 +02:00
|
|
|
class EUCJPDecoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
};
|
|
|
|
|
2024-05-29 09:58:44 +02:00
|
|
|
class ISO2022JPDecoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
};
|
|
|
|
|
2024-05-30 13:17:36 +02:00
|
|
|
class ShiftJISDecoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
};
|
|
|
|
|
2024-05-30 13:43:10 +02:00
|
|
|
class EUCKRDecoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
};
|
|
|
|
|
2024-05-30 13:54:31 +02:00
|
|
|
class ReplacementDecoder final : public Decoder {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
|
|
|
|
virtual bool validate(StringView input) override { return input.is_empty(); }
|
|
|
|
};
|
|
|
|
|
2024-06-02 15:56:36 +02:00
|
|
|
// This will return a decoder for the exact name specified, skipping get_standardized_encoding.
|
|
|
|
// Use this when you want ISO-8859-1 instead of windows-1252.
|
|
|
|
Optional<Decoder&> decoder_for_exact_name(StringView encoding);
|
|
|
|
|
2023-02-17 17:45:08 +00:00
|
|
|
Optional<Decoder&> decoder_for(StringView encoding);
|
2022-03-21 00:09:28 +01:00
|
|
|
Optional<StringView> get_standardized_encoding(StringView encoding);
|
2020-05-03 22:41:34 +02:00
|
|
|
|
2023-02-17 19:53:51 +00:00
|
|
|
// This returns the appropriate Unicode decoder for the sniffed BOM or nothing if there is no appropriate decoder.
|
|
|
|
Optional<Decoder&> bom_sniff_to_decoder(StringView);
|
2022-02-11 20:58:06 +00:00
|
|
|
|
2022-02-11 21:02:29 +00:00
|
|
|
// NOTE: This has an obnoxious name to discourage usage. Only use this if you absolutely must! For example, XHR in LibWeb uses this.
|
|
|
|
// This will use the given decoder unless there is a byte order mark in the input, in which we will instead use the appropriate Unicode decoder.
|
2023-02-17 20:15:10 +00:00
|
|
|
ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView);
|
2022-02-11 21:02:29 +00:00
|
|
|
|
2023-06-18 16:25:33 +01:00
|
|
|
StringView get_output_encoding(StringView encoding);
|
|
|
|
|
2020-05-03 22:41:34 +02:00
|
|
|
}
|