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