| 460 | /* --------------------------------------------------------------------- */ |
| 461 | |
| 462 | ConversionResult ConvertUTF8toUTF32 ( |
| 463 | const UTF8** sourceStart, const UTF8* sourceEnd, |
| 464 | UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) { |
| 465 | ConversionResult result = conversionOK; |
| 466 | const UTF8* source = *sourceStart; |
| 467 | UTF32* target = *targetStart; |
| 468 | while (source < sourceEnd) { |
| 469 | UTF32 ch = 0; |
| 470 | unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; |
| 471 | if (source + extraBytesToRead >= sourceEnd) { |
| 472 | result = sourceExhausted; break; |
| 473 | } |
| 474 | /* Do this check whether lenient or strict */ |
| 475 | if (! isLegalUTF8(source, extraBytesToRead+1)) { |
| 476 | result = sourceIllegal; |
| 477 | break; |
| 478 | } |
| 479 | /* |
| 480 | * The cases all fall through. See "Note A" below. |
| 481 | */ |
| 482 | switch (extraBytesToRead) { |
| 483 | case 5: ch += *source++; ch <<= 6; |
| 484 | case 4: ch += *source++; ch <<= 6; |
| 485 | case 3: ch += *source++; ch <<= 6; |
| 486 | case 2: ch += *source++; ch <<= 6; |
| 487 | case 1: ch += *source++; ch <<= 6; |
| 488 | case 0: ch += *source++; |
| 489 | } |
| 490 | ch -= offsetsFromUTF8[extraBytesToRead]; |
| 491 | |
| 492 | if (target >= targetEnd) { |
| 493 | source -= (extraBytesToRead+1); /* Back up the source pointer! */ |
| 494 | result = targetExhausted; break; |
| 495 | } |
| 496 | if (ch <= UNI_MAX_LEGAL_UTF32) { |
| 497 | /* |
| 498 | * UTF-16 surrogate values are illegal in UTF-32, and anything |
| 499 | * over Plane 17 (> 0x10FFFF) is illegal. |
| 500 | */ |
| 501 | if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { |
| 502 | if (flags == strictConversion) { |
| 503 | source -= (extraBytesToRead+1); /* return to the illegal value itself */ |
| 504 | result = sourceIllegal; |
| 505 | break; |
| 506 | } else { |
| 507 | *target++ = UNI_REPLACEMENT_CHAR; |
| 508 | } |
| 509 | } else { |
| 510 | *target++ = ch; |
| 511 | } |
| 512 | } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */ |
| 513 | result = sourceIllegal; |
| 514 | *target++ = UNI_REPLACEMENT_CHAR; |
| 515 | } |
| 516 | } |
| 517 | *sourceStart = source; |
| 518 | *targetStart = target; |
| 519 | return result; |
no test coverage detected