| 130 | } |
| 131 | |
| 132 | static unsigned int utf8ToCodepoint(const char*& s, const char* e) { |
| 133 | const unsigned int REPLACEMENT_CHARACTER = 0xFFFD; |
| 134 | |
| 135 | unsigned int firstByte = static_cast<unsigned char>(*s); |
| 136 | |
| 137 | if (firstByte < 0x80) |
| 138 | return firstByte; |
| 139 | |
| 140 | if (firstByte < 0xE0) { |
| 141 | if (e - s < 2) |
| 142 | return REPLACEMENT_CHARACTER; |
| 143 | |
| 144 | unsigned int calculated = |
| 145 | ((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F); |
| 146 | s += 1; |
| 147 | // oversized encoded characters are invalid |
| 148 | return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated; |
| 149 | } |
| 150 | |
| 151 | if (firstByte < 0xF0) { |
| 152 | if (e - s < 3) |
| 153 | return REPLACEMENT_CHARACTER; |
| 154 | |
| 155 | unsigned int calculated = ((firstByte & 0x0F) << 12) | |
| 156 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) | |
| 157 | (static_cast<unsigned int>(s[2]) & 0x3F); |
| 158 | s += 2; |
| 159 | // surrogates aren't valid codepoints itself |
| 160 | // shouldn't be UTF-8 encoded |
| 161 | if (calculated >= 0xD800 && calculated <= 0xDFFF) |
| 162 | return REPLACEMENT_CHARACTER; |
| 163 | // oversized encoded characters are invalid |
| 164 | return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated; |
| 165 | } |
| 166 | |
| 167 | if (firstByte < 0xF8) { |
| 168 | if (e - s < 4) |
| 169 | return REPLACEMENT_CHARACTER; |
| 170 | |
| 171 | unsigned int calculated = ((firstByte & 0x07) << 18) | |
| 172 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) | |
| 173 | ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) | |
| 174 | (static_cast<unsigned int>(s[3]) & 0x3F); |
| 175 | s += 3; |
| 176 | // oversized encoded characters are invalid |
| 177 | return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated; |
| 178 | } |
| 179 | |
| 180 | return REPLACEMENT_CHARACTER; |
| 181 | } |
| 182 | |
| 183 | static const char hex2[] = "000102030405060708090a0b0c0d0e0f" |
| 184 | "101112131415161718191a1b1c1d1e1f" |
no outgoing calls
no test coverage detected