| 4603 | } |
| 4604 | |
| 4605 | static unsigned int utf8ToCodepoint(const char *&s, const char *e) |
| 4606 | { |
| 4607 | const unsigned int REPLACEMENT_CHARACTER = 0xFFFD; |
| 4608 | |
| 4609 | unsigned int firstByte = static_cast<unsigned char>(*s); |
| 4610 | |
| 4611 | if (firstByte < 0x80) |
| 4612 | return firstByte; |
| 4613 | |
| 4614 | if (firstByte < 0xE0) |
| 4615 | { |
| 4616 | if (e - s < 2) |
| 4617 | return REPLACEMENT_CHARACTER; |
| 4618 | |
| 4619 | unsigned int calculated = ((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F); |
| 4620 | s += 1; |
| 4621 | // oversized encoded characters are invalid |
| 4622 | return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated; |
| 4623 | } |
| 4624 | |
| 4625 | if (firstByte < 0xF0) |
| 4626 | { |
| 4627 | if (e - s < 3) |
| 4628 | return REPLACEMENT_CHARACTER; |
| 4629 | |
| 4630 | unsigned int calculated = ((firstByte & 0x0F) << 12) | |
| 4631 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) | |
| 4632 | (static_cast<unsigned int>(s[2]) & 0x3F); |
| 4633 | s += 2; |
| 4634 | // surrogates aren't valid codepoints itself |
| 4635 | // shouldn't be UTF-8 encoded |
| 4636 | if (calculated >= 0xD800 && calculated <= 0xDFFF) |
| 4637 | return REPLACEMENT_CHARACTER; |
| 4638 | // oversized encoded characters are invalid |
| 4639 | return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated; |
| 4640 | } |
| 4641 | |
| 4642 | if (firstByte < 0xF8) |
| 4643 | { |
| 4644 | if (e - s < 4) |
| 4645 | return REPLACEMENT_CHARACTER; |
| 4646 | |
| 4647 | unsigned int calculated = |
| 4648 | ((firstByte & 0x07) << 18) | ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) | |
| 4649 | ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) | (static_cast<unsigned int>(s[3]) & 0x3F); |
| 4650 | s += 3; |
| 4651 | // oversized encoded characters are invalid |
| 4652 | return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated; |
| 4653 | } |
| 4654 | |
| 4655 | return REPLACEMENT_CHARACTER; |
| 4656 | } |
| 4657 | |
| 4658 | static const char hex2[] = "000102030405060708090a0b0c0d0e0f" |
| 4659 | "101112131415161718191a1b1c1d1e1f" |
no outgoing calls
no test coverage detected