You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef UTF8_VALIDATOR_HPP
  29. #define UTF8_VALIDATOR_HPP
  30. #include <string>
  31. #include <websocketpp/common/stdint.hpp>
  32. namespace websocketpp
  33. {
  34. namespace utf8_validator
  35. {
  36. /// State that represents a valid utf8 input sequence
  37. static const unsigned int utf8_accept = 0;
  38. /// State that represents an invalid utf8 input sequence
  39. static const unsigned int utf8_reject = 1;
  40. /// Lookup table for the UTF8 decode state machine
  41. static const uint8_t utf8d[] = {
  42. 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
  43. 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
  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, // 40..5f
  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, // 60..7f
  46. 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
  47. 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
  48. 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
  49. 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
  50. 0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
  51. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  52. 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
  53. 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
  54. 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
  55. 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
  56. };
  57. /// Decode the next byte of a UTF8 sequence
  58. /**
  59. * @param [out] state The decoder state to advance
  60. * @param [out] codep The codepoint to fill in
  61. * @param [in] byte The byte to input
  62. * @return The ending state of the decode operation
  63. */
  64. inline uint32_t decode(uint32_t * state, uint32_t * codep, uint8_t byte)
  65. {
  66. uint32_t type = utf8d[byte];
  67. *codep = (*state != utf8_accept) ? (byte & 0x3fu) | (*codep << 6) : (0xff >> type) & (byte);
  68. *state = utf8d[256 + *state * 16 + type];
  69. return *state;
  70. }
  71. /// Provides streaming UTF8 validation functionality
  72. class validator
  73. {
  74. public:
  75. /// Construct and initialize the validator
  76. validator() :
  77. m_state(utf8_accept),
  78. m_codepoint(0)
  79. {
  80. }
  81. /// Advance the state of the validator with the next input byte
  82. /**
  83. * @param byte The byte to advance the validation state with
  84. * @return Whether or not the byte resulted in a validation error.
  85. */
  86. bool consume(uint8_t byte)
  87. {
  88. if (utf8_validator::decode(&m_state, &m_codepoint, byte) == utf8_reject)
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. /// Advance validator state with input from an iterator pair
  95. /**
  96. * @param begin Input iterator to the start of the input range
  97. * @param end Input iterator to the end of the input range
  98. * @return Whether or not decoding the bytes resulted in a validation error.
  99. */
  100. template <typename iterator_type> bool decode(iterator_type begin, iterator_type end)
  101. {
  102. for (iterator_type it = begin; it != end; ++it)
  103. {
  104. unsigned int result = utf8_validator::decode(&m_state, &m_codepoint, static_cast<uint8_t>(*it));
  105. if (result == utf8_reject)
  106. {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. /// Return whether the input sequence ended on a valid utf8 codepoint
  113. /**
  114. * @return Whether or not the input sequence ended on a valid codepoint.
  115. */
  116. bool complete()
  117. {
  118. return m_state == utf8_accept;
  119. }
  120. /// Reset the validator to decode another message
  121. void reset()
  122. {
  123. m_state = utf8_accept;
  124. m_codepoint = 0;
  125. }
  126. private:
  127. uint32_t m_state;
  128. uint32_t m_codepoint;
  129. };
  130. /// Validate a UTF8 string
  131. /**
  132. * convenience function that creates a validator, validates a complete string
  133. * and returns the result.
  134. */
  135. inline bool validate(const std::string & s)
  136. {
  137. validator v;
  138. if (! v.decode(s.begin(), s.end()))
  139. {
  140. return false;
  141. }
  142. return v.complete();
  143. }
  144. } // namespace utf8_validator
  145. } // namespace websocketpp
  146. #endif // UTF8_VALIDATOR_HPP