websocketpp  0.3.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
utf8_validator.hpp
1 /*
2  * The following code is adapted from code originally written by Bjoern
3  * Hoehrmann <bjoern@hoehrmann.de>. See
4  * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
5  *
6  * The original license:
7  *
8  * Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27 */
28 
29 #ifndef UTF8_VALIDATOR_HPP
30 #define UTF8_VALIDATOR_HPP
31 
32 #include <websocketpp/common/stdint.hpp>
33 
34 namespace websocketpp {
35 namespace utf8_validator {
36 
38 static unsigned int const utf8_accept = 0;
40 static unsigned int const utf8_reject = 1;
41 
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, // 00..1f
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, // 20..3f
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, // 40..5f
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, // 60..7f
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, // 80..9f
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, // a0..bf
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, // c0..df
51  0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
52  0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
53  0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
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, // s1..s2
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, // s3..s4
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, // s5..s6
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, // s7..s8
58 };
59 
61 
67 inline uint32_t decode(uint32_t * state, uint32_t * codep, uint8_t byte) {
68  uint32_t type = utf8d[byte];
69 
70  *codep = (*state != utf8_accept) ?
71  (byte & 0x3fu) | (*codep << 6) :
72  (0xff >> type) & (byte);
73 
74  *state = utf8d[256 + *state*16 + type];
75  return *state;
76 }
77 
79 class validator {
80 public:
82  validator() : m_state(utf8_accept),m_codepoint(0) {}
83 
85 
89  bool consume (uint8_t byte) {
90  if (utf8_validator::decode(&m_state,&m_codepoint,byte) == utf8_reject) {
91  return false;
92  }
93  return true;
94  }
95 
97 
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(
106  &m_state,
107  &m_codepoint,
108  static_cast<uint8_t>(*it)
109  );
110 
111  if (result == utf8_reject) {
112  return false;
113  }
114  }
115  return true;
116  }
117 
119 
122  bool complete() {
123  return m_state == utf8_accept;
124  }
125 
127  void reset() {
128  m_state = utf8_accept;
129  m_codepoint = 0;
130  }
131 private:
132  uint32_t m_state;
133  uint32_t m_codepoint;
134 };
135 
137 
141 inline bool validate(std::string const & s) {
142  validator v;
143  if (!v.decode(s.begin(),s.end())) {
144  return false;
145  }
146  return v.complete();
147 }
148 
149 } // namespace utf8_validator
150 } // namespace websocketpp
151 
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.
Definition: base64.hpp:41
bool complete()
Return whether the input sequence ended on a valid utf8 codepoint.
validator()
Construct and initialize the validator.