/src/icu/source/i18n/csrutf8.cpp
Line | Count | Source |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ********************************************************************** |
5 | | * Copyright (C) 2005-2014, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ********************************************************************** |
8 | | */ |
9 | | |
10 | | #include "unicode/utypes.h" |
11 | | |
12 | | #if !UCONFIG_NO_CONVERSION |
13 | | |
14 | | #include "csrutf8.h" |
15 | | #include "csmatch.h" |
16 | | |
17 | | U_NAMESPACE_BEGIN |
18 | | |
19 | | CharsetRecog_UTF8::~CharsetRecog_UTF8() |
20 | 0 | { |
21 | | // nothing to do |
22 | 0 | } |
23 | | |
24 | | const char *CharsetRecog_UTF8::getName() const |
25 | 31.2k | { |
26 | 31.2k | return "UTF-8"; |
27 | 31.2k | } |
28 | | |
29 | 31.2k | UBool CharsetRecog_UTF8::match(InputText* input, CharsetMatch *results) const { |
30 | 31.2k | bool hasBOM = FALSE; |
31 | 31.2k | int32_t numValid = 0; |
32 | 31.2k | int32_t numInvalid = 0; |
33 | 31.2k | const uint8_t *inputBytes = input->fRawInput; |
34 | 31.2k | int32_t i; |
35 | 31.2k | int32_t trailBytes = 0; |
36 | 31.2k | int32_t confidence; |
37 | | |
38 | 31.2k | if (input->fRawLength >= 3 && |
39 | 29.0k | inputBytes[0] == 0xEF && inputBytes[1] == 0xBB && inputBytes[2] == 0xBF) { |
40 | 649 | hasBOM = TRUE; |
41 | 649 | } |
42 | | |
43 | | // Scan for multi-byte sequences |
44 | 501M | for (i=0; i < input->fRawLength; i += 1) { |
45 | 501M | int32_t b = inputBytes[i]; |
46 | | |
47 | 501M | if ((b & 0x80) == 0) { |
48 | 361M | continue; // ASCII |
49 | 361M | } |
50 | | |
51 | | // Hi bit on char found. Figure out how long the sequence should be |
52 | 139M | if ((b & 0x0E0) == 0x0C0) { |
53 | 22.8M | trailBytes = 1; |
54 | 117M | } else if ((b & 0x0F0) == 0x0E0) { |
55 | 2.92M | trailBytes = 2; |
56 | 114M | } else if ((b & 0x0F8) == 0xF0) { |
57 | 2.27M | trailBytes = 3; |
58 | 111M | } else { |
59 | 111M | numInvalid += 1; |
60 | 111M | continue; |
61 | 111M | } |
62 | | |
63 | | // Verify that we've got the right number of trail bytes in the sequence |
64 | 32.7M | for (;;) { |
65 | 32.7M | i += 1; |
66 | | |
67 | 32.7M | if (i >= input->fRawLength) { |
68 | 2.83k | break; |
69 | 2.83k | } |
70 | | |
71 | 32.7M | b = inputBytes[i]; |
72 | | |
73 | 32.7M | if ((b & 0xC0) != 0x080) { |
74 | 19.4M | numInvalid += 1; |
75 | 19.4M | break; |
76 | 19.4M | } |
77 | | |
78 | 13.3M | if (--trailBytes == 0) { |
79 | 8.61M | numValid += 1; |
80 | 8.61M | break; |
81 | 8.61M | } |
82 | 13.3M | } |
83 | | |
84 | 28.0M | } |
85 | | |
86 | | // Cook up some sort of confidence score, based on presence of a BOM |
87 | | // and the existence of valid and/or invalid multi-byte sequences. |
88 | 31.2k | confidence = 0; |
89 | 31.2k | if (hasBOM && numInvalid == 0) { |
90 | 77 | confidence = 100; |
91 | 31.1k | } else if (hasBOM && numValid > numInvalid*10) { |
92 | 78 | confidence = 80; |
93 | 31.1k | } else if (numValid > 3 && numInvalid == 0) { |
94 | 237 | confidence = 100; |
95 | 30.8k | } else if (numValid > 0 && numInvalid == 0) { |
96 | 296 | confidence = 80; |
97 | 30.5k | } else if (numValid == 0 && numInvalid == 0) { |
98 | | // Plain ASCII. Confidence must be > 10, it's more likely than UTF-16, which |
99 | | // accepts ASCII with confidence = 10. |
100 | 4.68k | confidence = 15; |
101 | 25.8k | } else if (numValid > numInvalid*10) { |
102 | | // Probably corruput utf-8 data. Valid sequences aren't likely by chance. |
103 | 140 | confidence = 25; |
104 | 140 | } |
105 | | |
106 | 31.2k | results->set(input, this, confidence); |
107 | 31.2k | return (confidence > 0); |
108 | 31.2k | } |
109 | | |
110 | | U_NAMESPACE_END |
111 | | #endif |