| 4315 | } |
| 4316 | |
| 4317 | static unsigned int utf8ToCodepoint(const char*& s, const char* e) { |
| 4318 | const unsigned int REPLACEMENT_CHARACTER = 0xFFFD; |
| 4319 | |
| 4320 | unsigned int firstByte = static_cast<unsigned char>(*s); |
| 4321 | |
| 4322 | if (firstByte < 0x80) |
| 4323 | return firstByte; |
| 4324 | |
| 4325 | if (firstByte < 0xE0) { |
| 4326 | if (e - s < 2) |
| 4327 | return REPLACEMENT_CHARACTER; |
| 4328 | |
| 4329 | unsigned int calculated = ((firstByte & 0x1F) << 6) |
| 4330 | | (static_cast<unsigned int>(s[1]) & 0x3F); |
| 4331 | s += 1; |
| 4332 | // oversized encoded characters are invalid |
| 4333 | return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated; |
| 4334 | } |
| 4335 | |
| 4336 | if (firstByte < 0xF0) { |
| 4337 | if (e - s < 3) |
| 4338 | return REPLACEMENT_CHARACTER; |
| 4339 | |
| 4340 | unsigned int calculated = ((firstByte & 0x0F) << 12) |
| 4341 | | ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) |
| 4342 | | (static_cast<unsigned int>(s[2]) & 0x3F); |
| 4343 | s += 2; |
| 4344 | // surrogates aren't valid codepoints itself |
| 4345 | // shouldn't be UTF-8 encoded |
| 4346 | if (calculated >= 0xD800 && calculated <= 0xDFFF) |
| 4347 | return REPLACEMENT_CHARACTER; |
| 4348 | // oversized encoded characters are invalid |
| 4349 | return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated; |
| 4350 | } |
| 4351 | |
| 4352 | if (firstByte < 0xF8) { |
| 4353 | if (e - s < 4) |
| 4354 | return REPLACEMENT_CHARACTER; |
| 4355 | |
| 4356 | unsigned int calculated = ((firstByte & 0x07) << 24) |
| 4357 | | ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) |
| 4358 | | ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) |
| 4359 | | (static_cast<unsigned int>(s[3]) & 0x3F); |
| 4360 | s += 3; |
| 4361 | // oversized encoded characters are invalid |
| 4362 | return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated; |
| 4363 | } |
| 4364 | |
| 4365 | return REPLACEMENT_CHARACTER; |
| 4366 | } |
| 4367 | |
| 4368 | static const char hex2[] = |
| 4369 | "000102030405060708090a0b0c0d0e0f" |
no outgoing calls
no test coverage detected