1 | /* |
2 | * Copyright (C) 2004-2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <pal/text/UnencodableHandling.h> |
29 | #include <wtf/URL.h> |
30 | #include <wtf/text/WTFString.h> |
31 | |
32 | namespace WebCore { |
33 | |
34 | class TextEncoding : public WTF::URLTextEncoding { |
35 | public: |
36 | TextEncoding() = default; |
37 | WEBCORE_EXPORT TextEncoding(const char* name); |
38 | WEBCORE_EXPORT TextEncoding(const String& name); |
39 | |
40 | bool isValid() const { return m_name; } |
41 | const char* name() const { return m_name; } |
42 | WEBCORE_EXPORT const char* domName() const; // name exposed via DOM |
43 | bool usesVisualOrdering() const; |
44 | bool isJapanese() const; |
45 | |
46 | const TextEncoding& closestByteBasedEquivalent() const; |
47 | const TextEncoding& encodingForFormSubmissionOrURLParsing() const; |
48 | |
49 | WEBCORE_EXPORT String decode(const char*, size_t length, bool stopOnError, bool& sawError) const; |
50 | String decode(const char*, size_t length) const; |
51 | WEBCORE_EXPORT Vector<uint8_t> encode(StringView, UnencodableHandling) const; |
52 | Vector<uint8_t> encodeForURLParsing(StringView string) const final { return encode(string, UnencodableHandling::URLEncodedEntities); } |
53 | |
54 | UChar backslashAsCurrencySymbol() const; |
55 | bool isByteBasedEncoding() const { return !isNonByteBasedEncoding(); } |
56 | |
57 | private: |
58 | bool isNonByteBasedEncoding() const; |
59 | bool isUTF7Encoding() const; |
60 | |
61 | const char* m_name { nullptr }; |
62 | UChar m_backslashAsCurrencySymbol; |
63 | }; |
64 | |
65 | inline bool operator==(const TextEncoding& a, const TextEncoding& b) { return a.name() == b.name(); } |
66 | inline bool operator!=(const TextEncoding& a, const TextEncoding& b) { return a.name() != b.name(); } |
67 | |
68 | const TextEncoding& ASCIIEncoding(); |
69 | const TextEncoding& Latin1Encoding(); |
70 | const TextEncoding& UTF16BigEndianEncoding(); |
71 | const TextEncoding& UTF16LittleEndianEncoding(); |
72 | WEBCORE_EXPORT const TextEncoding& UTF8Encoding(); |
73 | WEBCORE_EXPORT const TextEncoding& WindowsLatin1Encoding(); |
74 | |
75 | // Unescapes the given string using URL escaping rules. |
76 | // DANGER: If the URL has "%00" in it, |
77 | // the resulting string will have embedded null characters! |
78 | WEBCORE_EXPORT String decodeURLEscapeSequences(const String&, const TextEncoding& = UTF8Encoding()); |
79 | |
80 | inline String TextEncoding::decode(const char* characters, size_t length) const |
81 | { |
82 | bool ignored; |
83 | return decode(characters, length, false, ignored); |
84 | } |
85 | |
86 | } // namespace WebCore |
87 | |