utf8_validator.hpp 5.4 KB

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