1from codecs import BOM_UTF8, BOM_UTF16_BE, BOM_UTF16_LE, BOM_UTF32_BE, BOM_UTF32_LE
2from collections import OrderedDict
3from encodings.aliases import aliases
4from re import IGNORECASE, compile as re_compile
5from typing import Dict, List, Set, Union
6
7from .assets import FREQUENCIES
8
9# Contain for each eligible encoding a list of/item bytes SIG/BOM
10ENCODING_MARKS = OrderedDict(
11    [
12        ("utf_8", BOM_UTF8),
13        (
14            "utf_7",
15            [
16                b"\x2b\x2f\x76\x38",
17                b"\x2b\x2f\x76\x39",
18                b"\x2b\x2f\x76\x2b",
19                b"\x2b\x2f\x76\x2f",
20                b"\x2b\x2f\x76\x38\x2d",
21            ],
22        ),
23        ("gb18030", b"\x84\x31\x95\x33"),
24        ("utf_32", [BOM_UTF32_BE, BOM_UTF32_LE]),
25        ("utf_16", [BOM_UTF16_BE, BOM_UTF16_LE]),
26    ]
27)  # type: Dict[str, Union[bytes, List[bytes]]]
28
29TOO_SMALL_SEQUENCE = 32  # type: int
30TOO_BIG_SEQUENCE = int(10e6)  # type: int
31
32UTF8_MAXIMAL_ALLOCATION = 1112064  # type: int
33
34UNICODE_RANGES_COMBINED = {
35    "Control character": range(31 + 1),
36    "Basic Latin": range(32, 127 + 1),
37    "Latin-1 Supplement": range(128, 255 + 1),
38    "Latin Extended-A": range(256, 383 + 1),
39    "Latin Extended-B": range(384, 591 + 1),
40    "IPA Extensions": range(592, 687 + 1),
41    "Spacing Modifier Letters": range(688, 767 + 1),
42    "Combining Diacritical Marks": range(768, 879 + 1),
43    "Greek and Coptic": range(880, 1023 + 1),
44    "Cyrillic": range(1024, 1279 + 1),
45    "Cyrillic Supplement": range(1280, 1327 + 1),
46    "Armenian": range(1328, 1423 + 1),
47    "Hebrew": range(1424, 1535 + 1),
48    "Arabic": range(1536, 1791 + 1),
49    "Syriac": range(1792, 1871 + 1),
50    "Arabic Supplement": range(1872, 1919 + 1),
51    "Thaana": range(1920, 1983 + 1),
52    "NKo": range(1984, 2047 + 1),
53    "Samaritan": range(2048, 2111 + 1),
54    "Mandaic": range(2112, 2143 + 1),
55    "Syriac Supplement": range(2144, 2159 + 1),
56    "Arabic Extended-A": range(2208, 2303 + 1),
57    "Devanagari": range(2304, 2431 + 1),
58    "Bengali": range(2432, 2559 + 1),
59    "Gurmukhi": range(2560, 2687 + 1),
60    "Gujarati": range(2688, 2815 + 1),
61    "Oriya": range(2816, 2943 + 1),
62    "Tamil": range(2944, 3071 + 1),
63    "Telugu": range(3072, 3199 + 1),
64    "Kannada": range(3200, 3327 + 1),
65    "Malayalam": range(3328, 3455 + 1),
66    "Sinhala": range(3456, 3583 + 1),
67    "Thai": range(3584, 3711 + 1),
68    "Lao": range(3712, 3839 + 1),
69    "Tibetan": range(3840, 4095 + 1),
70    "Myanmar": range(4096, 4255 + 1),
71    "Georgian": range(4256, 4351 + 1),
72    "Hangul Jamo": range(4352, 4607 + 1),
73    "Ethiopic": range(4608, 4991 + 1),
74    "Ethiopic Supplement": range(4992, 5023 + 1),
75    "Cherokee": range(5024, 5119 + 1),
76    "Unified Canadian Aboriginal Syllabics": range(5120, 5759 + 1),
77    "Ogham": range(5760, 5791 + 1),
78    "Runic": range(5792, 5887 + 1),
79    "Tagalog": range(5888, 5919 + 1),
80    "Hanunoo": range(5920, 5951 + 1),
81    "Buhid": range(5952, 5983 + 1),
82    "Tagbanwa": range(5984, 6015 + 1),
83    "Khmer": range(6016, 6143 + 1),
84    "Mongolian": range(6144, 6319 + 1),
85    "Unified Canadian Aboriginal Syllabics Extended": range(6320, 6399 + 1),
86    "Limbu": range(6400, 6479 + 1),
87    "Tai Le": range(6480, 6527 + 1),
88    "New Tai Lue": range(6528, 6623 + 1),
89    "Khmer Symbols": range(6624, 6655 + 1),
90    "Buginese": range(6656, 6687 + 1),
91    "Tai Tham": range(6688, 6831 + 1),
92    "Combining Diacritical Marks Extended": range(6832, 6911 + 1),
93    "Balinese": range(6912, 7039 + 1),
94    "Sundanese": range(7040, 7103 + 1),
95    "Batak": range(7104, 7167 + 1),
96    "Lepcha": range(7168, 7247 + 1),
97    "Ol Chiki": range(7248, 7295 + 1),
98    "Cyrillic Extended C": range(7296, 7311 + 1),
99    "Sundanese Supplement": range(7360, 7375 + 1),
100    "Vedic Extensions": range(7376, 7423 + 1),
101    "Phonetic Extensions": range(7424, 7551 + 1),
102    "Phonetic Extensions Supplement": range(7552, 7615 + 1),
103    "Combining Diacritical Marks Supplement": range(7616, 7679 + 1),
104    "Latin Extended Additional": range(7680, 7935 + 1),
105    "Greek Extended": range(7936, 8191 + 1),
106    "General Punctuation": range(8192, 8303 + 1),
107    "Superscripts and Subscripts": range(8304, 8351 + 1),
108    "Currency Symbols": range(8352, 8399 + 1),
109    "Combining Diacritical Marks for Symbols": range(8400, 8447 + 1),
110    "Letterlike Symbols": range(8448, 8527 + 1),
111    "Number Forms": range(8528, 8591 + 1),
112    "Arrows": range(8592, 8703 + 1),
113    "Mathematical Operators": range(8704, 8959 + 1),
114    "Miscellaneous Technical": range(8960, 9215 + 1),
115    "Control Pictures": range(9216, 9279 + 1),
116    "Optical Character Recognition": range(9280, 9311 + 1),
117    "Enclosed Alphanumerics": range(9312, 9471 + 1),
118    "Box Drawing": range(9472, 9599 + 1),
119    "Block Elements": range(9600, 9631 + 1),
120    "Geometric Shapes": range(9632, 9727 + 1),
121    "Miscellaneous Symbols": range(9728, 9983 + 1),
122    "Dingbats": range(9984, 10175 + 1),
123    "Miscellaneous Mathematical Symbols-A": range(10176, 10223 + 1),
124    "Supplemental Arrows-A": range(10224, 10239 + 1),
125    "Braille Patterns": range(10240, 10495 + 1),
126    "Supplemental Arrows-B": range(10496, 10623 + 1),
127    "Miscellaneous Mathematical Symbols-B": range(10624, 10751 + 1),
128    "Supplemental Mathematical Operators": range(10752, 11007 + 1),
129    "Miscellaneous Symbols and Arrows": range(11008, 11263 + 1),
130    "Glagolitic": range(11264, 11359 + 1),
131    "Latin Extended-C": range(11360, 11391 + 1),
132    "Coptic": range(11392, 11519 + 1),
133    "Georgian Supplement": range(11520, 11567 + 1),
134    "Tifinagh": range(11568, 11647 + 1),
135    "Ethiopic Extended": range(11648, 11743 + 1),
136    "Cyrillic Extended-A": range(11744, 11775 + 1),
137    "Supplemental Punctuation": range(11776, 11903 + 1),
138    "CJK Radicals Supplement": range(11904, 12031 + 1),
139    "Kangxi Radicals": range(12032, 12255 + 1),
140    "Ideographic Description Characters": range(12272, 12287 + 1),
141    "CJK Symbols and Punctuation": range(12288, 12351 + 1),
142    "Hiragana": range(12352, 12447 + 1),
143    "Katakana": range(12448, 12543 + 1),
144    "Bopomofo": range(12544, 12591 + 1),
145    "Hangul Compatibility Jamo": range(12592, 12687 + 1),
146    "Kanbun": range(12688, 12703 + 1),
147    "Bopomofo Extended": range(12704, 12735 + 1),
148    "CJK Strokes": range(12736, 12783 + 1),
149    "Katakana Phonetic Extensions": range(12784, 12799 + 1),
150    "Enclosed CJK Letters and Months": range(12800, 13055 + 1),
151    "CJK Compatibility": range(13056, 13311 + 1),
152    "CJK Unified Ideographs Extension A": range(13312, 19903 + 1),
153    "Yijing Hexagram Symbols": range(19904, 19967 + 1),
154    "CJK Unified Ideographs": range(19968, 40959 + 1),
155    "Yi Syllables": range(40960, 42127 + 1),
156    "Yi Radicals": range(42128, 42191 + 1),
157    "Lisu": range(42192, 42239 + 1),
158    "Vai": range(42240, 42559 + 1),
159    "Cyrillic Extended-B": range(42560, 42655 + 1),
160    "Bamum": range(42656, 42751 + 1),
161    "Modifier Tone Letters": range(42752, 42783 + 1),
162    "Latin Extended-D": range(42784, 43007 + 1),
163    "Syloti Nagri": range(43008, 43055 + 1),
164    "Common Indic Number Forms": range(43056, 43071 + 1),
165    "Phags-pa": range(43072, 43135 + 1),
166    "Saurashtra": range(43136, 43231 + 1),
167    "Devanagari Extended": range(43232, 43263 + 1),
168    "Kayah Li": range(43264, 43311 + 1),
169    "Rejang": range(43312, 43359 + 1),
170    "Hangul Jamo Extended-A": range(43360, 43391 + 1),
171    "Javanese": range(43392, 43487 + 1),
172    "Myanmar Extended-B": range(43488, 43519 + 1),
173    "Cham": range(43520, 43615 + 1),
174    "Myanmar Extended-A": range(43616, 43647 + 1),
175    "Tai Viet": range(43648, 43743 + 1),
176    "Meetei Mayek Extensions": range(43744, 43775 + 1),
177    "Ethiopic Extended-A": range(43776, 43823 + 1),
178    "Latin Extended-E": range(43824, 43887 + 1),
179    "Cherokee Supplement": range(43888, 43967 + 1),
180    "Meetei Mayek": range(43968, 44031 + 1),
181    "Hangul Syllables": range(44032, 55215 + 1),
182    "Hangul Jamo Extended-B": range(55216, 55295 + 1),
183    "High Surrogates": range(55296, 56191 + 1),
184    "High Private Use Surrogates": range(56192, 56319 + 1),
185    "Low Surrogates": range(56320, 57343 + 1),
186    "Private Use Area": range(57344, 63743 + 1),
187    "CJK Compatibility Ideographs": range(63744, 64255 + 1),
188    "Alphabetic Presentation Forms": range(64256, 64335 + 1),
189    "Arabic Presentation Forms-A": range(64336, 65023 + 1),
190    "Variation Selectors": range(65024, 65039 + 1),
191    "Vertical Forms": range(65040, 65055 + 1),
192    "Combining Half Marks": range(65056, 65071 + 1),
193    "CJK Compatibility Forms": range(65072, 65103 + 1),
194    "Small Form Variants": range(65104, 65135 + 1),
195    "Arabic Presentation Forms-B": range(65136, 65279 + 1),
196    "Halfwidth and Fullwidth Forms": range(65280, 65519 + 1),
197    "Specials": range(65520, 65535 + 1),
198    "Linear B Syllabary": range(65536, 65663 + 1),
199    "Linear B Ideograms": range(65664, 65791 + 1),
200    "Aegean Numbers": range(65792, 65855 + 1),
201    "Ancient Greek Numbers": range(65856, 65935 + 1),
202    "Ancient Symbols": range(65936, 65999 + 1),
203    "Phaistos Disc": range(66000, 66047 + 1),
204    "Lycian": range(66176, 66207 + 1),
205    "Carian": range(66208, 66271 + 1),
206    "Coptic Epact Numbers": range(66272, 66303 + 1),
207    "Old Italic": range(66304, 66351 + 1),
208    "Gothic": range(66352, 66383 + 1),
209    "Old Permic": range(66384, 66431 + 1),
210    "Ugaritic": range(66432, 66463 + 1),
211    "Old Persian": range(66464, 66527 + 1),
212    "Deseret": range(66560, 66639 + 1),
213    "Shavian": range(66640, 66687 + 1),
214    "Osmanya": range(66688, 66735 + 1),
215    "Osage": range(66736, 66815 + 1),
216    "Elbasan": range(66816, 66863 + 1),
217    "Caucasian Albanian": range(66864, 66927 + 1),
218    "Linear A": range(67072, 67455 + 1),
219    "Cypriot Syllabary": range(67584, 67647 + 1),
220    "Imperial Aramaic": range(67648, 67679 + 1),
221    "Palmyrene": range(67680, 67711 + 1),
222    "Nabataean": range(67712, 67759 + 1),
223    "Hatran": range(67808, 67839 + 1),
224    "Phoenician": range(67840, 67871 + 1),
225    "Lydian": range(67872, 67903 + 1),
226    "Meroitic Hieroglyphs": range(67968, 67999 + 1),
227    "Meroitic Cursive": range(68000, 68095 + 1),
228    "Kharoshthi": range(68096, 68191 + 1),
229    "Old South Arabian": range(68192, 68223 + 1),
230    "Old North Arabian": range(68224, 68255 + 1),
231    "Manichaean": range(68288, 68351 + 1),
232    "Avestan": range(68352, 68415 + 1),
233    "Inscriptional Parthian": range(68416, 68447 + 1),
234    "Inscriptional Pahlavi": range(68448, 68479 + 1),
235    "Psalter Pahlavi": range(68480, 68527 + 1),
236    "Old Turkic": range(68608, 68687 + 1),
237    "Old Hungarian": range(68736, 68863 + 1),
238    "Rumi Numeral Symbols": range(69216, 69247 + 1),
239    "Brahmi": range(69632, 69759 + 1),
240    "Kaithi": range(69760, 69839 + 1),
241    "Sora Sompeng": range(69840, 69887 + 1),
242    "Chakma": range(69888, 69967 + 1),
243    "Mahajani": range(69968, 70015 + 1),
244    "Sharada": range(70016, 70111 + 1),
245    "Sinhala Archaic Numbers": range(70112, 70143 + 1),
246    "Khojki": range(70144, 70223 + 1),
247    "Multani": range(70272, 70319 + 1),
248    "Khudawadi": range(70320, 70399 + 1),
249    "Grantha": range(70400, 70527 + 1),
250    "Newa": range(70656, 70783 + 1),
251    "Tirhuta": range(70784, 70879 + 1),
252    "Siddham": range(71040, 71167 + 1),
253    "Modi": range(71168, 71263 + 1),
254    "Mongolian Supplement": range(71264, 71295 + 1),
255    "Takri": range(71296, 71375 + 1),
256    "Ahom": range(71424, 71487 + 1),
257    "Warang Citi": range(71840, 71935 + 1),
258    "Zanabazar Square": range(72192, 72271 + 1),
259    "Soyombo": range(72272, 72367 + 1),
260    "Pau Cin Hau": range(72384, 72447 + 1),
261    "Bhaiksuki": range(72704, 72815 + 1),
262    "Marchen": range(72816, 72895 + 1),
263    "Masaram Gondi": range(72960, 73055 + 1),
264    "Cuneiform": range(73728, 74751 + 1),
265    "Cuneiform Numbers and Punctuation": range(74752, 74879 + 1),
266    "Early Dynastic Cuneiform": range(74880, 75087 + 1),
267    "Egyptian Hieroglyphs": range(77824, 78895 + 1),
268    "Anatolian Hieroglyphs": range(82944, 83583 + 1),
269    "Bamum Supplement": range(92160, 92735 + 1),
270    "Mro": range(92736, 92783 + 1),
271    "Bassa Vah": range(92880, 92927 + 1),
272    "Pahawh Hmong": range(92928, 93071 + 1),
273    "Miao": range(93952, 94111 + 1),
274    "Ideographic Symbols and Punctuation": range(94176, 94207 + 1),
275    "Tangut": range(94208, 100351 + 1),
276    "Tangut Components": range(100352, 101119 + 1),
277    "Kana Supplement": range(110592, 110847 + 1),
278    "Kana Extended-A": range(110848, 110895 + 1),
279    "Nushu": range(110960, 111359 + 1),
280    "Duployan": range(113664, 113823 + 1),
281    "Shorthand Format Controls": range(113824, 113839 + 1),
282    "Byzantine Musical Symbols": range(118784, 119039 + 1),
283    "Musical Symbols": range(119040, 119295 + 1),
284    "Ancient Greek Musical Notation": range(119296, 119375 + 1),
285    "Tai Xuan Jing Symbols": range(119552, 119647 + 1),
286    "Counting Rod Numerals": range(119648, 119679 + 1),
287    "Mathematical Alphanumeric Symbols": range(119808, 120831 + 1),
288    "Sutton SignWriting": range(120832, 121519 + 1),
289    "Glagolitic Supplement": range(122880, 122927 + 1),
290    "Mende Kikakui": range(124928, 125151 + 1),
291    "Adlam": range(125184, 125279 + 1),
292    "Arabic Mathematical Alphabetic Symbols": range(126464, 126719 + 1),
293    "Mahjong Tiles": range(126976, 127023 + 1),
294    "Domino Tiles": range(127024, 127135 + 1),
295    "Playing Cards": range(127136, 127231 + 1),
296    "Enclosed Alphanumeric Supplement": range(127232, 127487 + 1),
297    "Enclosed Ideographic Supplement": range(127488, 127743 + 1),
298    "Miscellaneous Symbols and Pictographs": range(127744, 128511 + 1),
299    "Emoticons range(Emoji)": range(128512, 128591 + 1),
300    "Ornamental Dingbats": range(128592, 128639 + 1),
301    "Transport and Map Symbols": range(128640, 128767 + 1),
302    "Alchemical Symbols": range(128768, 128895 + 1),
303    "Geometric Shapes Extended": range(128896, 129023 + 1),
304    "Supplemental Arrows-C": range(129024, 129279 + 1),
305    "Supplemental Symbols and Pictographs": range(129280, 129535 + 1),
306    "CJK Unified Ideographs Extension B": range(131072, 173791 + 1),
307    "CJK Unified Ideographs Extension C": range(173824, 177983 + 1),
308    "CJK Unified Ideographs Extension D": range(177984, 178207 + 1),
309    "CJK Unified Ideographs Extension E": range(178208, 183983 + 1),
310    "CJK Unified Ideographs Extension F": range(183984, 191471 + 1),
311    "CJK Compatibility Ideographs Supplement": range(194560, 195103 + 1),
312    "Tags": range(917504, 917631 + 1),
313    "Variation Selectors Supplement": range(917760, 917999 + 1),
314}  # type: Dict[str, range]
315
316
317UNICODE_SECONDARY_RANGE_KEYWORD = [
318    "Supplement",
319    "Extended",
320    "Extensions",
321    "Modifier",
322    "Marks",
323    "Punctuation",
324    "Symbols",
325    "Forms",
326    "Operators",
327    "Miscellaneous",
328    "Drawing",
329    "Block",
330    "Shapes",
331    "Supplemental",
332    "Tags",
333]  # type: List[str]
334
335RE_POSSIBLE_ENCODING_INDICATION = re_compile(
336    r"(?:(?:encoding)|(?:charset)|(?:coding))(?:[\:= ]{1,10})(?:[\"\']?)([a-zA-Z0-9\-_]+)(?:[\"\']?)",
337    IGNORECASE,
338)
339
340IANA_SUPPORTED = sorted(
341    filter(
342        lambda x: x.endswith("_codec") is False
343        and x not in {"rot_13", "tactis", "mbcs"},
344        list(set(aliases.values())),
345    )
346)  # type: List[str]
347
348IANA_SUPPORTED_COUNT = len(IANA_SUPPORTED)  # type: int
349
350# pre-computed code page that are similar using the function cp_similarity.
351IANA_SUPPORTED_SIMILAR = {
352    "cp037": ["cp1026", "cp1140", "cp273", "cp500"],
353    "cp1026": ["cp037", "cp1140", "cp273", "cp500"],
354    "cp1125": ["cp866"],
355    "cp1140": ["cp037", "cp1026", "cp273", "cp500"],
356    "cp1250": ["iso8859_2"],
357    "cp1251": ["kz1048", "ptcp154"],
358    "cp1252": ["iso8859_15", "iso8859_9", "latin_1"],
359    "cp1253": ["iso8859_7"],
360    "cp1254": ["iso8859_15", "iso8859_9", "latin_1"],
361    "cp1257": ["iso8859_13"],
362    "cp273": ["cp037", "cp1026", "cp1140", "cp500"],
363    "cp437": ["cp850", "cp858", "cp860", "cp861", "cp862", "cp863", "cp865"],
364    "cp500": ["cp037", "cp1026", "cp1140", "cp273"],
365    "cp850": ["cp437", "cp857", "cp858", "cp865"],
366    "cp857": ["cp850", "cp858", "cp865"],
367    "cp858": ["cp437", "cp850", "cp857", "cp865"],
368    "cp860": ["cp437", "cp861", "cp862", "cp863", "cp865"],
369    "cp861": ["cp437", "cp860", "cp862", "cp863", "cp865"],
370    "cp862": ["cp437", "cp860", "cp861", "cp863", "cp865"],
371    "cp863": ["cp437", "cp860", "cp861", "cp862", "cp865"],
372    "cp865": ["cp437", "cp850", "cp857", "cp858", "cp860", "cp861", "cp862", "cp863"],
373    "cp866": ["cp1125"],
374    "iso8859_10": ["iso8859_14", "iso8859_15", "iso8859_4", "iso8859_9", "latin_1"],
375    "iso8859_11": ["tis_620"],
376    "iso8859_13": ["cp1257"],
377    "iso8859_14": [
378        "iso8859_10",
379        "iso8859_15",
380        "iso8859_16",
381        "iso8859_3",
382        "iso8859_9",
383        "latin_1",
384    ],
385    "iso8859_15": [
386        "cp1252",
387        "cp1254",
388        "iso8859_10",
389        "iso8859_14",
390        "iso8859_16",
391        "iso8859_3",
392        "iso8859_9",
393        "latin_1",
394    ],
395    "iso8859_16": [
396        "iso8859_14",
397        "iso8859_15",
398        "iso8859_2",
399        "iso8859_3",
400        "iso8859_9",
401        "latin_1",
402    ],
403    "iso8859_2": ["cp1250", "iso8859_16", "iso8859_4"],
404    "iso8859_3": ["iso8859_14", "iso8859_15", "iso8859_16", "iso8859_9", "latin_1"],
405    "iso8859_4": ["iso8859_10", "iso8859_2", "iso8859_9", "latin_1"],
406    "iso8859_7": ["cp1253"],
407    "iso8859_9": [
408        "cp1252",
409        "cp1254",
410        "cp1258",
411        "iso8859_10",
412        "iso8859_14",
413        "iso8859_15",
414        "iso8859_16",
415        "iso8859_3",
416        "iso8859_4",
417        "latin_1",
418    ],
419    "kz1048": ["cp1251", "ptcp154"],
420    "latin_1": [
421        "cp1252",
422        "cp1254",
423        "cp1258",
424        "iso8859_10",
425        "iso8859_14",
426        "iso8859_15",
427        "iso8859_16",
428        "iso8859_3",
429        "iso8859_4",
430        "iso8859_9",
431    ],
432    "mac_iceland": ["mac_roman", "mac_turkish"],
433    "mac_roman": ["mac_iceland", "mac_turkish"],
434    "mac_turkish": ["mac_iceland", "mac_roman"],
435    "ptcp154": ["cp1251", "kz1048"],
436    "tis_620": ["iso8859_11"],
437}  # type: Dict[str, List[str]]
438
439
440CHARDET_CORRESPONDENCE = {
441    "iso2022_kr": "ISO-2022-KR",
442    "iso2022_jp": "ISO-2022-JP",
443    "euc_kr": "EUC-KR",
444    "tis_620": "TIS-620",
445    "utf_32": "UTF-32",
446    "euc_jp": "EUC-JP",
447    "koi8_r": "KOI8-R",
448    "iso8859_1": "ISO-8859-1",
449    "iso8859_2": "ISO-8859-2",
450    "iso8859_5": "ISO-8859-5",
451    "iso8859_6": "ISO-8859-6",
452    "iso8859_7": "ISO-8859-7",
453    "iso8859_8": "ISO-8859-8",
454    "utf_16": "UTF-16",
455    "cp855": "IBM855",
456    "mac_cyrillic": "MacCyrillic",
457    "gb2312": "GB2312",
458    "gb18030": "GB18030",
459    "cp932": "CP932",
460    "cp866": "IBM866",
461    "utf_8": "utf-8",
462    "utf_8_sig": "UTF-8-SIG",
463    "shift_jis": "SHIFT_JIS",
464    "big5": "Big5",
465    "cp1250": "windows-1250",
466    "cp1251": "windows-1251",
467    "cp1252": "Windows-1252",
468    "cp1253": "windows-1253",
469    "cp1255": "windows-1255",
470    "cp1256": "windows-1256",
471    "cp1254": "Windows-1254",
472    "cp949": "CP949",
473}  # type: Dict[str, str]
474
475
476COMMON_SAFE_ASCII_CHARACTERS = {
477    "<",
478    ">",
479    "=",
480    ":",
481    "/",
482    "&",
483    ";",
484    "{",
485    "}",
486    "[",
487    "]",
488    ",",
489    "|",
490    '"',
491    "-",
492}  # type: Set[str]
493
494
495KO_NAMES = {"johab", "cp949", "euc_kr"}  # type: Set[str]
496ZH_NAMES = {"big5", "cp950", "big5hkscs", "hz"}  # type: Set[str]
497
498NOT_PRINTABLE_PATTERN = re_compile(r"[0-9\W\n\r\t]+")
499
500LANGUAGE_SUPPORTED_COUNT = len(FREQUENCIES)  # type: int
501