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.

178 lines
7.1 KiB

4 months ago
  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. * IXUtf8Validator.h
  30. * Author: Benjamin Sergeant
  31. * Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
  32. *
  33. * From websocketpp. Tiny modifications made for code style, function names etc...
  34. */
  35. #pragma once
  36. #include <cstdint>
  37. #include <string>
  38. namespace ix
  39. {
  40. /// State that represents a valid utf8 input sequence
  41. static unsigned int const utf8_accept = 0;
  42. /// State that represents an invalid utf8 input sequence
  43. static unsigned int const utf8_reject = 1;
  44. /// Lookup table for the UTF8 decode state machine
  45. static uint8_t const utf8d[] = {
  46. 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, // 00..1f
  48. 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, // 20..3f
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5f
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7f
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9f
  56. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  57. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // a0..bf
  58. 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  59. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0..df
  60. 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
  61. 0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
  62. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  63. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  64. 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
  65. 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1,
  66. 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
  67. 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
  68. 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
  69. 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1,
  70. 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // s7..s8
  71. };
  72. /// Decode the next byte of a UTF8 sequence
  73. /**
  74. * @param [out] state The decoder state to advance
  75. * @param [out] codep The codepoint to fill in
  76. * @param [in] byte The byte to input
  77. * @return The ending state of the decode operation
  78. */
  79. inline uint32_t decodeNextByte(uint32_t* state, uint32_t* codep, uint8_t byte)
  80. {
  81. uint32_t type = utf8d[byte];
  82. *codep = (*state != utf8_accept) ? (byte & 0x3fu) | (*codep << 6) : (0xff >> type) & (byte);
  83. *state = utf8d[256 + *state * 16 + type];
  84. return *state;
  85. }
  86. /// Provides streaming UTF8 validation functionality
  87. class Utf8Validator
  88. {
  89. public:
  90. /// Construct and initialize the validator
  91. Utf8Validator()
  92. : m_state(utf8_accept)
  93. , m_codepoint(0)
  94. {
  95. }
  96. /// Advance the state of the validator with the next input byte
  97. /**
  98. * @param byte The byte to advance the validation state with
  99. * @return Whether or not the byte resulted in a validation error.
  100. */
  101. bool consume(uint8_t byte)
  102. {
  103. if (decodeNextByte(&m_state, &m_codepoint, byte) == utf8_reject)
  104. {
  105. return false;
  106. }
  107. return true;
  108. }
  109. /// Advance Validator state with input from an iterator pair
  110. /**
  111. * @param begin Input iterator to the start of the input range
  112. * @param end Input iterator to the end of the input range
  113. * @return Whether or not decoding the bytes resulted in a validation error.
  114. */
  115. template<typename iterator_type>
  116. bool decode(iterator_type begin, iterator_type end)
  117. {
  118. for (iterator_type it = begin; it != end; ++it)
  119. {
  120. unsigned int result =
  121. decodeNextByte(&m_state, &m_codepoint, static_cast<uint8_t>(*it));
  122. if (result == utf8_reject)
  123. {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. /// Return whether the input sequence ended on a valid utf8 codepoint
  130. /**
  131. * @return Whether or not the input sequence ended on a valid codepoint.
  132. */
  133. bool complete()
  134. {
  135. return m_state == utf8_accept;
  136. }
  137. /// Reset the Validator to decode another message
  138. void reset()
  139. {
  140. m_state = utf8_accept;
  141. m_codepoint = 0;
  142. }
  143. private:
  144. uint32_t m_state;
  145. uint32_t m_codepoint;
  146. };
  147. /// Validate a UTF8 string
  148. /**
  149. * convenience function that creates a Validator, validates a complete string
  150. * and returns the result.
  151. */
  152. inline bool validateUtf8(std::string const& s)
  153. {
  154. Utf8Validator v;
  155. if (!v.decode(s.begin(), s.end()))
  156. {
  157. return false;
  158. }
  159. return v.complete();
  160. }
  161. } // namespace ix