29 #ifndef UTF8_VALIDATOR_HPP
30 #define UTF8_VALIDATOR_HPP
32 #include <websocketpp/common/stdint.hpp>
35 namespace utf8_validator {
38 static unsigned int const utf8_accept = 0;
40 static unsigned int const utf8_reject = 1;
43 static uint8_t
const utf8d[] = {
44 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,
45 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,
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 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,
49 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,
50 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,
51 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3,
52 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,
53 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1,
54 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,
55 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,
56 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,
57 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,
67 inline uint32_t decode(uint32_t * state, uint32_t * codep, uint8_t byte) {
68 uint32_t type = utf8d[byte];
70 *codep = (*state != utf8_accept) ?
71 (byte & 0x3fu) | (*codep << 6) :
72 (0xff >> type) & (byte);
74 *state = utf8d[256 + *state*16 + type];
82 validator() : m_state(utf8_accept),m_codepoint(0) {}
90 if (utf8_validator::decode(&m_state,&m_codepoint,byte) == utf8_reject) {
102 template <
typename iterator_type>
103 bool decode (iterator_type begin, iterator_type end) {
104 for (iterator_type it = begin; it != end; ++it) {
105 unsigned int result = utf8_validator::decode(
108 static_cast<uint8_t>(*it)
111 if (result == utf8_reject) {
123 return m_state == utf8_accept;
128 m_state = utf8_accept;
133 uint32_t m_codepoint;
141 inline bool validate(std::string
const & s) {
143 if (!v.decode(s.begin(),s.end())) {
152 #endif // UTF8_VALIDATOR_HPP
bool decode(iterator_type begin, iterator_type end)
Advance validator state with input from an iterator pair.
Provides streaming UTF8 validation functionality.
bool consume(uint8_t byte)
Advance the state of the validator with the next input byte.
void reset()
Reset the validator to decode another message.
Namespace for the WebSocket++ project.
bool complete()
Return whether the input sequence ended on a valid utf8 codepoint.
validator()
Construct and initialize the validator.