| 420 | /* --------------------------------------------------------------------- */ |
| 421 | |
| 422 | static unsigned |
| 423 | findMaximalSubpartOfIllFormedUTF8Sequence(const UTF8 *source, |
| 424 | const UTF8 *sourceEnd) { |
| 425 | UTF8 b1, b2, b3; |
| 426 | |
| 427 | assert(!isLegalUTF8Sequence(source, sourceEnd)); |
| 428 | |
| 429 | /* |
| 430 | * Unicode 6.3.0, D93b: |
| 431 | * |
| 432 | * Maximal subpart of an ill-formed subsequence: The longest code unit |
| 433 | * subsequence starting at an unconvertible offset that is either: |
| 434 | * a. the initial subsequence of a well-formed code unit sequence, or |
| 435 | * b. a subsequence of length one. |
| 436 | */ |
| 437 | |
| 438 | if (source == sourceEnd) |
| 439 | return 0; |
| 440 | |
| 441 | /* |
| 442 | * Perform case analysis. See Unicode 6.3.0, Table 3-7. Well-Formed UTF-8 |
| 443 | * Byte Sequences. |
| 444 | */ |
| 445 | |
| 446 | b1 = *source; |
| 447 | ++source; |
| 448 | if (b1 >= 0xC2 && b1 <= 0xDF) { |
| 449 | /* |
| 450 | * First byte is valid, but we know that this code unit sequence is |
| 451 | * invalid, so the maximal subpart has to end after the first byte. |
| 452 | */ |
| 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | if (source == sourceEnd) |
| 457 | return 1; |
| 458 | |
| 459 | b2 = *source; |
| 460 | ++source; |
| 461 | |
| 462 | if (b1 == 0xE0) { |
| 463 | return (b2 >= 0xA0 && b2 <= 0xBF) ? 2 : 1; |
| 464 | } |
| 465 | if (b1 >= 0xE1 && b1 <= 0xEC) { |
| 466 | return (b2 >= 0x80 && b2 <= 0xBF) ? 2 : 1; |
| 467 | } |
| 468 | if (b1 == 0xED) { |
| 469 | return (b2 >= 0x80 && b2 <= 0x9F) ? 2 : 1; |
| 470 | } |
| 471 | if (b1 >= 0xEE && b1 <= 0xEF) { |
| 472 | return (b2 >= 0x80 && b2 <= 0xBF) ? 2 : 1; |
| 473 | } |
| 474 | if (b1 == 0xF0) { |
| 475 | if (b2 >= 0x90 && b2 <= 0xBF) { |
| 476 | if (source == sourceEnd) |
| 477 | return 2; |
| 478 | |
| 479 | b3 = *source; |
no test coverage detected