Coverage Report

Created: 2025-12-18 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibTextCodec/Decoder.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
3
 * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
4
 * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 */
8
9
#pragma once
10
11
#include <AK/Forward.h>
12
#include <AK/Function.h>
13
#include <AK/Optional.h>
14
#include <AK/String.h>
15
16
namespace TextCodec {
17
18
class Decoder {
19
public:
20
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) = 0;
21
    virtual bool validate(StringView);
22
    virtual ErrorOr<String> to_utf8(StringView);
23
24
protected:
25
0
    virtual ~Decoder() = default;
26
};
27
28
class UTF8Decoder final : public Decoder {
29
public:
30
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
31
    virtual bool validate(StringView) override;
32
    virtual ErrorOr<String> to_utf8(StringView) override;
33
};
34
35
class UTF16BEDecoder final : public Decoder {
36
public:
37
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
38
    virtual bool validate(StringView) override;
39
    virtual ErrorOr<String> to_utf8(StringView) override;
40
};
41
42
class UTF16LEDecoder final : public Decoder {
43
public:
44
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
45
    virtual bool validate(StringView) override;
46
    virtual ErrorOr<String> to_utf8(StringView) override;
47
};
48
49
template<Integral ArrayType = u32>
50
class SingleByteDecoder final : public Decoder {
51
public:
52
    SingleByteDecoder(Array<ArrayType, 128> translation_table)
53
1.72k
        : m_translation_table(translation_table)
54
1.72k
    {
55
1.72k
    }
56
57
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
58
59
private:
60
    Array<ArrayType, 128> m_translation_table;
61
};
62
63
class Latin1Decoder final : public Decoder {
64
public:
65
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
66
0
    virtual bool validate(StringView) override { return true; }
67
};
68
69
class PDFDocEncodingDecoder final : public Decoder {
70
public:
71
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
72
0
    virtual bool validate(StringView) override { return true; }
73
};
74
75
class XUserDefinedDecoder final : public Decoder {
76
public:
77
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
78
0
    virtual bool validate(StringView) override { return true; }
79
};
80
81
class GB18030Decoder final : public Decoder {
82
public:
83
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
84
};
85
86
class Big5Decoder final : public Decoder {
87
public:
88
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
89
};
90
91
class EUCJPDecoder final : public Decoder {
92
public:
93
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
94
};
95
96
class ISO2022JPDecoder final : public Decoder {
97
public:
98
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
99
};
100
101
class ShiftJISDecoder final : public Decoder {
102
public:
103
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
104
};
105
106
class EUCKRDecoder final : public Decoder {
107
public:
108
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
109
};
110
111
class ReplacementDecoder final : public Decoder {
112
public:
113
    virtual ErrorOr<void> process(StringView, Function<ErrorOr<void>(u32)> on_code_point) override;
114
0
    virtual bool validate(StringView input) override { return input.is_empty(); }
115
};
116
117
// This will return a decoder for the exact name specified, skipping get_standardized_encoding.
118
// Use this when you want ISO-8859-1 instead of windows-1252.
119
Optional<Decoder&> decoder_for_exact_name(StringView encoding);
120
121
Optional<Decoder&> decoder_for(StringView encoding);
122
Optional<StringView> get_standardized_encoding(StringView encoding);
123
124
// This returns the appropriate Unicode decoder for the sniffed BOM or nothing if there is no appropriate decoder.
125
Optional<Decoder&> bom_sniff_to_decoder(StringView);
126
127
// NOTE: This has an obnoxious name to discourage usage. Only use this if you absolutely must! For example, XHR in LibWeb uses this.
128
// 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.
129
ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder&, StringView);
130
131
StringView get_output_encoding(StringView encoding);
132
133
}