1 #pragma once
2 
3 /* Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
4  * See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
13  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15  *
16  * ValidateUTF8String() is based on https://stackoverflow.com/a/22135005/8552466
17  */
18 
19 #include <array>
20 #include <cstdint>
21 
22 namespace UTFD
23 {
24 
25 constexpr uint32_t UTF8_ACCEPT { 0 };
26 constexpr uint32_t UTF8_REJECT { 1 };
27 
28 static constexpr std::array<uint8_t, 400> UTF8_DECODER
29 {
30 	// The first part of the table maps bytes to character classes that
31 	// to reduce the size of the transition table and create bitmasks.
32 	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
33 	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34 	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
35 	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
36 	 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,  9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
37 	 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
38 	 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
39 	10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
40 
41 	// The second part is a transition table that maps a combination
42 	// of a state of the automaton and a character class to a state.
43 	 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
44 	12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
45 	12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
46 	12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
47 	12,36,12,12,12,12,12,12,12,12,12,12,
48 };
49 
DecodeUTF8(uint32_t * const codep,const uint32_t byte)50 inline uint32_t DecodeUTF8(uint32_t* const codep, const uint32_t byte)
51 {
52 	if ( byte > UTF8_DECODER.size() )
53 	{
54 		return UTF8_REJECT;
55 	}
56 
57 	const uint32_t type = UTF8_DECODER[byte];
58 	uint32_t state = UTF8_ACCEPT;
59 
60 	*codep = (state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6) : (0xff >> type) & (byte);
61 	state = UTF8_DECODER[256 + state + type];
62 
63 	return state;
64 }
65 
66 // Returns 0 (UTF8_ACCEPT) if valid UTF8, 1 (UTF8_REJECT) if invalid, or greater than 1 if more processing is required (two byte character)
ValidateUTF8String(char * const str,const size_t length)67 inline uint32_t ValidateUTF8String(char* const str, const size_t length)
68 {
69 	uint32_t state = UTF8_ACCEPT;
70 
71 	for ( size_t charIndex = 0; charIndex < length; charIndex++ )
72 	{
73 		const uint32_t type = UTF8_DECODER[static_cast<uint8_t>(str[charIndex])];
74 		state = UTF8_DECODER[256 + state + type];
75 
76 		if ( state == UTF8_REJECT )
77 		{
78 			break;
79 		}
80 	}
81 
82 	return state;
83 }
84 
85 // Returns 0 (UTF8_ACCEPT) if valid UTF8, 1 (UTF8_REJECT) if invalid, or greater than 1 if more processing is required (two byte character)
ValidateUTF8Character(const char c)86 inline uint32_t ValidateUTF8Character(const char c)
87 {
88 	return UTF8_DECODER[256 + UTF8_ACCEPT + UTF8_DECODER[static_cast<uint8_t>(c)]];
89 }
90 
91 }
92 
93