29 #ifndef UTF8_VALIDATOR_HPP
30 #define UTF8_VALIDATOR_HPP
32 #include <websocketpp/common/stdint.hpp>
37 namespace utf8_validator {
40 static unsigned int const utf8_accept = 0;
42 static unsigned int const utf8_reject = 1;
45 static uint8_t
const utf8d[] = {
46 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,
47 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,
48 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,
49 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,
50 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,
51 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,
52 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,
53 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3,
54 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,
55 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1,
56 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1,
57 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,
58 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1,
59 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
69 inline uint32_t decode(uint32_t * state, uint32_t * codep, uint8_t byte) {
70 uint32_t type = utf8d[byte];
72 *codep = (*state != utf8_accept) ?
73 (byte & 0x3fu) | (*codep << 6) :
74 (0xff >> type) & (byte);
76 *state = utf8d[256 + *state*16 + type];
92 if (utf8_validator::decode(&m_state,&m_codepoint,byte) == utf8_reject) {
104 template <
typename iterator_type>
105 bool decode (iterator_type begin, iterator_type end) {
106 for (iterator_type it = begin; it != end; ++it) {
107 unsigned int result = utf8_validator::decode(
110 static_cast<uint8_t>(*it)
113 if (result == utf8_reject) {
125 return m_state == utf8_accept;
130 m_state = utf8_accept;
135 uint32_t m_codepoint;
143 inline bool validate(std::string
const & s) {
145 if (!v.decode(s.begin(),s.end())) {