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