| 4366 | } |
| 4367 | |
| 4368 | static unsigned int utf8ToCodepoint(const char*& s, const char* e) { |
| 4369 | const unsigned int REPLACEMENT_CHARACTER = 0xFFFD; |
| 4370 | |
| 4371 | unsigned int firstByte = static_cast<unsigned char>(*s); |
| 4372 | |
| 4373 | if (firstByte < 0x80) |
| 4374 | return firstByte; |
| 4375 | |
| 4376 | if (firstByte < 0xE0) { |
| 4377 | if (e - s < 2) |
| 4378 | return REPLACEMENT_CHARACTER; |
| 4379 | |
| 4380 | unsigned int calculated = |
| 4381 | ((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F); |
| 4382 | s += 1; |
| 4383 | // oversized encoded characters are invalid |
| 4384 | return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated; |
| 4385 | } |
| 4386 | |
| 4387 | if (firstByte < 0xF0) { |
| 4388 | if (e - s < 3) |
| 4389 | return REPLACEMENT_CHARACTER; |
| 4390 | |
| 4391 | unsigned int calculated = ((firstByte & 0x0F) << 12) | |
| 4392 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) | |
| 4393 | (static_cast<unsigned int>(s[2]) & 0x3F); |
| 4394 | s += 2; |
| 4395 | // surrogates aren't valid codepoints itself |
| 4396 | // shouldn't be UTF-8 encoded |
| 4397 | if (calculated >= 0xD800 && calculated <= 0xDFFF) |
| 4398 | return REPLACEMENT_CHARACTER; |
| 4399 | // oversized encoded characters are invalid |
| 4400 | return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated; |
| 4401 | } |
| 4402 | |
| 4403 | if (firstByte < 0xF8) { |
| 4404 | if (e - s < 4) |
| 4405 | return REPLACEMENT_CHARACTER; |
| 4406 | |
| 4407 | unsigned int calculated = ((firstByte & 0x07) << 18) | |
| 4408 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) | |
| 4409 | ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) | |
| 4410 | (static_cast<unsigned int>(s[3]) & 0x3F); |
| 4411 | s += 3; |
| 4412 | // oversized encoded characters are invalid |
| 4413 | return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated; |
| 4414 | } |
| 4415 | |
| 4416 | return REPLACEMENT_CHARACTER; |
| 4417 | } |
| 4418 | |
| 4419 | static const char hex2[] = "000102030405060708090a0b0c0d0e0f" |
| 4420 | "101112131415161718191a1b1c1d1e1f" |
no outgoing calls
no test coverage detected