| 4275 | } |
| 4276 | |
| 4277 | static unsigned int utf8ToCodepoint(const char*& s, const char* e) { |
| 4278 | const unsigned int REPLACEMENT_CHARACTER = 0xFFFD; |
| 4279 | |
| 4280 | unsigned int firstByte = static_cast<unsigned char>(*s); |
| 4281 | |
| 4282 | if (firstByte < 0x80) |
| 4283 | return firstByte; |
| 4284 | |
| 4285 | if (firstByte < 0xE0) { |
| 4286 | if (e - s < 2) |
| 4287 | return REPLACEMENT_CHARACTER; |
| 4288 | |
| 4289 | unsigned int calculated = |
| 4290 | ((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F); |
| 4291 | s += 1; |
| 4292 | // oversized encoded characters are invalid |
| 4293 | return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated; |
| 4294 | } |
| 4295 | |
| 4296 | if (firstByte < 0xF0) { |
| 4297 | if (e - s < 3) |
| 4298 | return REPLACEMENT_CHARACTER; |
| 4299 | |
| 4300 | unsigned int calculated = ((firstByte & 0x0F) << 12) | |
| 4301 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) | |
| 4302 | (static_cast<unsigned int>(s[2]) & 0x3F); |
| 4303 | s += 2; |
| 4304 | // surrogates aren't valid codepoints itself |
| 4305 | // shouldn't be UTF-8 encoded |
| 4306 | if (calculated >= 0xD800 && calculated <= 0xDFFF) |
| 4307 | return REPLACEMENT_CHARACTER; |
| 4308 | // oversized encoded characters are invalid |
| 4309 | return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated; |
| 4310 | } |
| 4311 | |
| 4312 | if (firstByte < 0xF8) { |
| 4313 | if (e - s < 4) |
| 4314 | return REPLACEMENT_CHARACTER; |
| 4315 | |
| 4316 | unsigned int calculated = ((firstByte & 0x07) << 18) | |
| 4317 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) | |
| 4318 | ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) | |
| 4319 | (static_cast<unsigned int>(s[3]) & 0x3F); |
| 4320 | s += 3; |
| 4321 | // oversized encoded characters are invalid |
| 4322 | return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated; |
| 4323 | } |
| 4324 | |
| 4325 | return REPLACEMENT_CHARACTER; |
| 4326 | } |
| 4327 | |
| 4328 | static const char hex2[] = "000102030405060708090a0b0c0d0e0f" |
| 4329 | "101112131415161718191a1b1c1d1e1f" |
no outgoing calls
no test coverage detected