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