| 171 | } |
| 172 | |
| 173 | static unsigned int utf8ToCodepoint(const char*& s, const char* e) { |
| 174 | const unsigned int REPLACEMENT_CHARACTER = 0xFFFD; |
| 175 | |
| 176 | unsigned int firstByte = static_cast<unsigned char>(*s); |
| 177 | |
| 178 | if (firstByte < 0x80) |
| 179 | return firstByte; |
| 180 | |
| 181 | if (firstByte < 0xE0) { |
| 182 | if (e - s < 2) |
| 183 | return REPLACEMENT_CHARACTER; |
| 184 | |
| 185 | unsigned int calculated = ((firstByte & 0x1F) << 6) |
| 186 | | (static_cast<unsigned int>(s[1]) & 0x3F); |
| 187 | s += 1; |
| 188 | // oversized encoded characters are invalid |
| 189 | return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated; |
| 190 | } |
| 191 | |
| 192 | if (firstByte < 0xF0) { |
| 193 | if (e - s < 3) |
| 194 | return REPLACEMENT_CHARACTER; |
| 195 | |
| 196 | unsigned int calculated = ((firstByte & 0x0F) << 12) |
| 197 | | ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) |
| 198 | | (static_cast<unsigned int>(s[2]) & 0x3F); |
| 199 | s += 2; |
| 200 | // surrogates aren't valid codepoints itself |
| 201 | // shouldn't be UTF-8 encoded |
| 202 | if (calculated >= 0xD800 && calculated <= 0xDFFF) |
| 203 | return REPLACEMENT_CHARACTER; |
| 204 | // oversized encoded characters are invalid |
| 205 | return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated; |
| 206 | } |
| 207 | |
| 208 | if (firstByte < 0xF8) { |
| 209 | if (e - s < 4) |
| 210 | return REPLACEMENT_CHARACTER; |
| 211 | |
| 212 | unsigned int calculated = ((firstByte & 0x07) << 24) |
| 213 | | ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) |
| 214 | | ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) |
| 215 | | (static_cast<unsigned int>(s[3]) & 0x3F); |
| 216 | s += 3; |
| 217 | // oversized encoded characters are invalid |
| 218 | return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated; |
| 219 | } |
| 220 | |
| 221 | return REPLACEMENT_CHARACTER; |
| 222 | } |
| 223 | |
| 224 | static const char hex2[] = |
| 225 | "000102030405060708090a0b0c0d0e0f" |
no outgoing calls
no test coverage detected