FreeRDP-WebConnect WebSockets gateway  1.0.0.167
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
wsutf8.hpp
1  /*
2  * This file has been derived from the WebSockets++ project at
3  * https://github.com/zaphoyd/websocketpp which is licensed under a BSD-license.
4  */
5 
6 // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
7 // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
8 
9 #ifndef UTF8_VALIDATOR_HPP
10 #define UTF8_VALIDATOR_HPP
11 
12 #include <stdint.h>
13 
14 namespace utf8_validator {
15 
16 static const unsigned int UTF8_ACCEPT = 0;
17 static const unsigned int UTF8_REJECT = 1;
18 
19 static const uint8_t utf8d[] = {
20  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, // 00..1f
21  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, // 20..3f
22  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, // 40..5f
23  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, // 60..7f
24  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, // 80..9f
25  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, // a0..bf
26  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, // c0..df
27  0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
28  0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
29  0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
30  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, // s1..s2
31  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, // s3..s4
32  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, // s5..s6
33  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, // s7..s8
34 };
35 
36 uint32_t inline
37 decode(uint32_t* state, uint32_t* codep, uint8_t byte) {
38  uint32_t type = utf8d[byte];
39 
40  *codep = (*state != UTF8_ACCEPT) ?
41  (byte & 0x3fu) | (*codep << 6) :
42  (0xff >> type) & (byte);
43 
44  *state = utf8d[256 + *state*16 + type];
45  return *state;
46 }
47 
51 class validator {
52  public:
54  validator() : m_state(UTF8_ACCEPT),m_codepoint(0) {}
55 
61  bool consume (uint32_t byte) {
62  if (utf8_validator::decode(&m_state,&m_codepoint,byte) == UTF8_REJECT) {
63  return false;
64  }
65  return true;
66  }
67 
68  template <typename iterator_type>
75  bool decode (iterator_type b, iterator_type e) {
76  for (iterator_type i = b; i != e; i++) {
77  if (utf8_validator::decode(&m_state,&m_codepoint,*i) == UTF8_REJECT) {
78  return false;
79  }
80  }
81  return true;
82  }
83 
88  bool complete() {
89  return m_state == UTF8_ACCEPT;
90  }
91 
93  void reset() {
94  m_state = UTF8_ACCEPT;
95  m_codepoint = 0;
96  }
97  private:
98  uint32_t m_state;
99  uint32_t m_codepoint;
100 };
101 
102 // convenience function that creates a validator, validates a complete string
103 // and returns the result.
104 // TODO: should this be inline?
105 inline bool validate(const std::string& s) {
106  validator v;
107  if (!v.decode(s.begin(),s.end())) {
108  return false;
109  }
110  return v.complete();
111 }
112 
113 } // namespace utf8_validator
114 
115 #endif // UTF8_VALIDATOR_HPP