1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
* Copyright (C) 2002-2016, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* file name: ucnv_u8.c
* encoding: US-ASCII
* tab size: 8 (not used)
* indentation:4
*
* created on: 2002jul01
* created by: Markus W. Scherer
*
* UTF-8 converter implementation. Used to be in ucnv_utf.c.
*
* Also, CESU-8 implementation, see UTR 26.
* The CESU-8 converter uses all the same functions as the
* UTF-8 converter, with a branch for converting supplementary code points.
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_CONVERSION
#include "unicode/ucnv.h"
#include "unicode/utf.h"
#include "unicode/utf8.h"
#include "unicode/utf16.h"
#include "ucnv_bld.h"
#include "ucnv_cnv.h"
#include "cmemory.h"
/* Prototypes --------------------------------------------------------------- */
/* Keep these here to make finicky compilers happy */
U_CFUNC void ucnv_fromUnicode_UTF8(UConverterFromUnicodeArgs *args,
UErrorCode *err);
U_CFUNC void ucnv_fromUnicode_UTF8_OFFSETS_LOGIC(UConverterFromUnicodeArgs *args,
UErrorCode *err);
/* UTF-8 -------------------------------------------------------------------- */
/* UTF-8 Conversion DATA
* for more information see Unicode Standard 2.0, Transformation Formats Appendix A-9
*/
/*static const uint32_t REPLACEMENT_CHARACTER = 0x0000FFFD;*/
#define MAXIMUM_UCS2 0x0000FFFF
#define MAXIMUM_UTF 0x0010FFFF
#define MAXIMUM_UCS4 0x7FFFFFFF
#define HALF_SHIFT 10
#define HALF_BASE 0x0010000
#define HALF_MASK 0x3FF
#define SURROGATE_HIGH_START 0xD800
#define SURROGATE_HIGH_END 0xDBFF
#define SURROGATE_LOW_START 0xDC00
#define SURROGATE_LOW_END 0xDFFF
/* -SURROGATE_LOW_START + HALF_BASE */
#define SURROGATE_LOW_BASE 9216
static const uint32_t offsetsFromUTF8[7] = {0,
(uint32_t) 0x00000000, (uint32_t) 0x00003080, (uint32_t) 0x000E2080,
(uint32_t) 0x03C82080, (uint32_t) 0xFA082080, (uint32_t) 0x82082080
};
/* END OF UTF-8 Conversion DATA */
static const int8_t bytesFromUTF8[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0
};
/*
* Starting with Unicode 3.0.1:
* UTF-8 byte sequences of length N _must_ encode code points of or above utf8_minChar32[N];
* byte sequences with more than 4 bytes are illegal in UTF-8,
* which is tested with impossible values for them
*/
static const uint32_t
utf8_minChar32[7]={ 0, 0, 0x80, 0x800, 0x10000, 0xffffffff, 0xffffffff };
static UBool hasCESU8Data(const UConverter *cnv)
{
#if UCONFIG_ONLY_HTML_CONVERSION
return FALSE;
#else
return (UBool)(cnv->sharedData == &_CESU8Data);
#endif
}
static void ucnv_toUnicode_UTF8 (UConverterToUnicodeArgs * args,
UErrorCode * err)
{
UConverter *cnv = args->converter;
const unsigned char *mySource = (unsigned char *) args->source;
UChar *myTarget = args->target;
const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit;
const UChar *targetLimit = args->targetLimit;
unsigned char *toUBytes = cnv->toUBytes;
UBool isCESU8 = hasCESU8Data(cnv);
uint32_t ch, ch2 = 0;
int32_t i, inBytes;
/* Restore size of current sequence */
if (cnv->toUnicodeStatus && myTarget < targetLimit)
{
inBytes = cnv->mode; /* restore # of bytes to consume */
i = cnv->toULength; /* restore # of bytes consumed */
cnv->toULength = 0;
ch = cnv->toUnicodeStatus;/*Stores the previously calculated ch from a previous call*/
cnv->toUnicodeStatus = 0;
goto morebytes;
}
while (mySource < sourceLimit && myTarget < targetLimit)
{
ch = *(mySource++);
if (ch < 0x80) /* Simple case */
{
*(myTarget++) = (UChar) ch;
}
else
{
/* store the first char */
toUBytes[0] = (char)ch;
inBytes = bytesFromUTF8[ch]; /* lookup current sequence length */
i = 1;
morebytes:
while (i < inBytes)
{
if (mySource < sourceLimit)
{
toUBytes[i] = (char) (ch2 = *mySource);
if (!U8_IS_TRAIL(ch2))
{
break; /* i < inBytes */
}
ch = (ch << 6) + ch2;
++mySource;
i++;
}
else
{
/* stores a partially calculated target*/
cnv->toUnicodeStatus = ch;
cnv->mode = inBytes;
cnv->toULength = (int8_t) i;
goto donefornow;
}
}
/* Remove the accumulated high bits */
ch -= offsetsFromUTF8[inBytes];
/*
* Legal UTF-8 byte sequences in Unicode 3.0.1 and up:
* - use only trail bytes after a lead byte (checked above)
* - use the right number of trail bytes for a given lead byte
* - encode a code point <= U+10ffff
* - use the fewest possible number of bytes for their code points
* - use at most 4 bytes (for i>=5 it is 0x10ffff<utf8_minChar32[])
*
* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8.
* There are no irregular sequences any more.
* In CESU-8, only surrogates, not supplementary code points, are encoded directly.
*/
if (i == inBytes && ch <= MAXIMUM_UTF && ch >= utf8_minChar32[i] &&
(isCESU8 ? i <= 3 : !U_IS_SURROGATE(ch)))
{
/* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */
if (ch <= MAXIMUM_UCS2)
{
/* fits in 16 bits */
*(myTarget++) = (UChar) ch;
}
else
{
/* write out the surrogates */
ch -= HALF_BASE;
*(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START);
ch = (ch & HALF_MASK) + SURROGATE_LOW_START;
if (myTarget < targetLimit)
{
*(myTarget++) = (UChar)ch;
}
else
{
/* Put in overflow buffer (not handled here) */
cnv->UCharErrorBuffer[0] = (UChar) ch;
cnv->UCharErrorBufferLength = 1;
*err = U_BUFFER_OVERFLOW_ERROR;
break;
}
}
}
else
{
cnv->toULength = (int8_t)i;
*err = U_ILLEGAL_CHAR_FOUND;
break;
}
}
}
donefornow:
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
{
/* End of target buffer */
*err = U_BUFFER_OVERFLOW_ERROR;
}
args->target = myTarget;
args->source = (const char *) mySource;
}
static void ucnv_toUnicode_UTF8_OFFSETS_LOGIC (UConverterToUnicodeArgs * args,
UErrorCode * err)
{
UConverter *cnv = args->converter;
const unsigned char *mySource = (unsigned char *) args->source;
UChar *myTarget = args->target;
int32_t *myOffsets = args->offsets;
int32_t offsetNum = 0;
const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit;
const UChar *targetLimit = args->targetLimit;
unsigned char *toUBytes = cnv->toUBytes;
UBool isCESU8 = hasCESU8Data(cnv);
uint32_t ch, ch2 = 0;
int32_t i, inBytes;
/* Restore size of current sequence */
if (cnv->toUnicodeStatus && myTarget < targetLimit)
{
inBytes = cnv->mode; /* restore # of bytes to consume */
i = cnv->toULength; /* restore # of bytes consumed */
cnv->toULength = 0;
ch = cnv->toUnicodeStatus;/*Stores the previously calculated ch from a previous call*/
cnv->toUnicodeStatus = 0;
goto morebytes;
}
while (mySource < sourceLimit && myTarget < targetLimit)
{
ch = *(mySource++);
if (ch < 0x80) /* Simple case */
{
*(myTarget++) = (UChar) ch;
*(myOffsets++) = offsetNum++;
}
else
{
toUBytes[0] = (char)ch;
inBytes = bytesFromUTF8[ch];
i = 1;
morebytes:
while (i < inBytes)
{
if (mySource < sourceLimit)
{
toUBytes[i] = (char) (ch2 = *mySource);
if (!U8_IS_TRAIL(ch2))
{
break; /* i < inBytes */
}
ch = (ch << 6) + ch2;
++mySource;
i++;
}
else
{
cnv->toUnicodeStatus = ch;
cnv->mode = inBytes;
cnv->toULength = (int8_t)i;
goto donefornow;
}
}
/* Remove the accumulated high bits */
ch -= offsetsFromUTF8[inBytes];
/*
* Legal UTF-8 byte sequences in Unicode 3.0.1 and up:
* - use only trail bytes after a lead byte (checked above)
* - use the right number of trail bytes for a given lead byte
* - encode a code point <= U+10ffff
* - use the fewest possible number of bytes for their code points
* - use at most 4 bytes (for i>=5 it is 0x10ffff<utf8_minChar32[])
*
* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8.
* There are no irregular sequences any more.
* In CESU-8, only surrogates, not supplementary code points, are encoded directly.
*/
if (i == inBytes && ch <= MAXIMUM_UTF && ch >= utf8_minChar32[i] &&
(isCESU8 ? i <= 3 : !U_IS_SURROGATE(ch)))
{
/* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */
if (ch <= MAXIMUM_UCS2)
{
/* fits in 16 bits */
*(myTarget++) = (UChar) ch;
*(myOffsets++) = offsetNum;
}
else
{
/* write out the surrogates */
ch -= HALF_BASE;
*(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START);
*(myOffsets++) = offsetNum;
ch = (ch & HALF_MASK) + SURROGATE_LOW_START;
if (myTarget < targetLimit)
{
*(myTarget++) = (UChar)ch;
*(myOffsets++) = offsetNum;
}
else
{
cnv->UCharErrorBuffer[0] = (UChar) ch;
cnv->UCharErrorBufferLength = 1;
*err = U_BUFFER_OVERFLOW_ERROR;
}
}
offsetNum += i;
}
else
{
cnv->toULength = (int8_t)i;
*err = U_ILLEGAL_CHAR_FOUND;
break;
}
}
}
donefornow:
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
{ /* End of target buffer */
*err = U_BUFFER_OVERFLOW_ERROR;
}
args->target = myTarget;
args->source = (const char *) mySource;
args->offsets = myOffsets;
}
U_CFUNC void ucnv_fromUnicode_UTF8 (UConverterFromUnicodeArgs * args,
UErrorCode * err)
{
UConverter *cnv = args->converter;
const UChar *mySource = args->source;
const UChar *sourceLimit = args->sourceLimit;
uint8_t *myTarget = (uint8_t *) args->target;
const uint8_t *targetLimit = (uint8_t *) args->targetLimit;
uint8_t *tempPtr;
UChar32 ch;
uint8_t tempBuf[4];
int32_t indexToWrite;
UBool isNotCESU8 = !hasCESU8Data(cnv);
if (cnv->fromUChar32 && myTarget < targetLimit)
{
ch = cnv->fromUChar32;
cnv->fromUChar32 = 0;
goto lowsurrogate;
}
while (mySource < sourceLimit && myTarget < targetLimit)
{
ch = *(mySource++);
if (ch < 0x80) /* Single byte */
{
*(myTarget++) = (uint8_t) ch;
}
else if (ch < 0x800) /* Double byte */
{
*(myTarget++) = (uint8_t) ((ch >> 6) | 0xc0);
if (myTarget < targetLimit)
{
*(myTarget++) = (uint8_t) ((ch & 0x3f) | 0x80);
}
else
{
cnv->charErrorBuffer[0] = (uint8_t) ((ch & 0x3f) | 0x80);
cnv->charErrorBufferLength = 1;
*err = U_BUFFER_OVERFLOW_ERROR;
}
}
else {
/* Check for surrogates */
if(U16_IS_SURROGATE(ch) && isNotCESU8) {
lowsurrogate:
if (mySource < sourceLimit) {
/* test both code units */
if(U16_IS_SURROGATE_LEAD(ch) && U16_IS_TRAIL(*mySource)) {
/* convert and consume this supplementary code point */
ch=U16_GET_SUPPLEMENTARY(ch, *mySource);
++mySource;
/* exit this condition tree */
}
else {
/* this is an unpaired trail or lead code unit */
/* callback(illegal) */
cnv->fromUChar32 = ch;
*err = U_ILLEGAL_CHAR_FOUND;
break;
}
}
else {
/* no more input */
cnv->fromUChar32 = ch;
break;
}
}
/* Do we write the buffer directly for speed,
or do we have to be careful about target buffer space? */
tempPtr = (((targetLimit - myTarget) >= 4) ? myTarget : tempBuf);
if (ch <= MAXIMUM_UCS2) {
indexToWrite = 2;
tempPtr[0] = (uint8_t) ((ch >> 12) | 0xe0);
}
else {
indexToWrite = 3;
tempPtr[0] = (uint8_t) ((ch >> 18) | 0xf0);
tempPtr[1] = (uint8_t) (((ch >> 12) & 0x3f) | 0x80);
}
tempPtr[indexToWrite-1] = (uint8_t) (((ch >> 6) & 0x3f) | 0x80);
tempPtr[indexToWrite] = (uint8_t) ((ch & 0x3f) | 0x80);
if (tempPtr == myTarget) {
/* There was enough space to write the codepoint directly. */
myTarget += (indexToWrite + 1);
}
else {
/* We might run out of room soon. Write it slowly. */
for (; tempPtr <= (tempBuf + indexToWrite); tempPtr++) {
if (myTarget < targetLimit) {
*(myTarget++) = *tempPtr;
}
else {
cnv->charErrorBuffer[cnv->charErrorBufferLength++] = *tempPtr;
*err = U_BUFFER_OVERFLOW_ERROR;
}
}
}
}
}
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
{
*err = U_BUFFER_OVERFLOW_ERROR;
}
args->target = (char *) myTarget;
args->source = mySource;
}
U_CFUNC void ucnv_fromUnicode_UTF8_OFFSETS_LOGIC (UConverterFromUnicodeArgs * args,
UErrorCode * err)
{
UConverter *cnv = args->converter;
const UChar *mySource = args->source;
int32_t *myOffsets = args->offsets;
const UChar *sourceLimit = args->sourceLimit;
uint8_t *myTarget = (uint8_t *) args->target;
const uint8_t *targetLimit = (uint8_t *) args->targetLimit;
uint8_t *tempPtr;
UChar32 ch;
int32_t offsetNum, nextSourceIndex;
int32_t indexToWrite;
uint8_t tempBuf[4];
UBool isNotCESU8 = !hasCESU8Data(cnv);
if (cnv->fromUChar32 && myTarget < targetLimit)
{
ch = cnv->fromUChar32;
cnv->fromUChar32 = 0;
offsetNum = -1;
nextSourceIndex = 0;
goto lowsurrogate;
} else {
offsetNum = 0;
}
while (mySource < sourceLimit && myTarget < targetLimit)
{
ch = *(mySource++);
if (ch < 0x80) /* Single byte */
{
*(myOffsets++) = offsetNum++;
*(myTarget++) = (char) ch;
}
else if (ch < 0x800) /* Double byte */
{
*(myOffsets++) = offsetNum;
*(myTarget++) = (uint8_t) ((ch >> 6) | 0xc0);
if (myTarget < targetLimit)
{
*(myOffsets++) = offsetNum++;
*(myTarget++) = (uint8_t) ((ch & 0x3f) | 0x80);
}
else
{
cnv->charErrorBuffer[0] = (uint8_t) ((ch & 0x3f) | 0x80);
cnv->charErrorBufferLength = 1;
*err = U_BUFFER_OVERFLOW_ERROR;
}
}
else
/* Check for surrogates */
{
nextSourceIndex = offsetNum + 1;
if(U16_IS_SURROGATE(ch) && isNotCESU8) {
lowsurrogate:
if (mySource < sourceLimit) {
/* test both code units */
if(U16_IS_SURROGATE_LEAD(ch) && U16_IS_TRAIL(*mySource)) {
/* convert and consume this supplementary code point */
ch=U16_GET_SUPPLEMENTARY(ch, *mySource);
++mySource;
++nextSourceIndex;
/* exit this condition tree */
}
else {
/* this is an unpaired trail or lead code unit */
/* callback(illegal) */
cnv->fromUChar32 = ch;
*err = U_ILLEGAL_CHAR_FOUND;
break;
}
}
else {
/* no more input */
cnv->fromUChar32 = ch;
break;
}
}
/* Do we write the buffer directly for speed,
or do we have to be careful about target buffer space? */
tempPtr = (((targetLimit - myTarget) >= 4) ? myTarget : tempBuf);
if (ch <= MAXIMUM_UCS2) {
indexToWrite = 2;
tempPtr[0] = (uint8_t) ((ch >> 12) | 0xe0);
}
else {
indexToWrite = 3;
tempPtr[0] = (uint8_t) ((ch >> 18) | 0xf0);
tempPtr[1] = (uint8_t) (((ch >> 12) & 0x3f) | 0x80);
}
tempPtr[indexToWrite-1] = (uint8_t) (((ch >> 6) & 0x3f) | 0x80);
tempPtr[indexToWrite] = (uint8_t) ((ch & 0x3f) | 0x80);
if (tempPtr == myTarget) {
/* There was enough space to write the codepoint directly. */
myTarget += (indexToWrite + 1);
myOffsets[0] = offsetNum;
myOffsets[1] = offsetNum;
myOffsets[2] = offsetNum;
if (indexToWrite >= 3) {
myOffsets[3] = offsetNum;
}
myOffsets += (indexToWrite + 1);
}
else {
/* We might run out of room soon. Write it slowly. */
for (; tempPtr <= (tempBuf + indexToWrite); tempPtr++) {
if (myTarget < targetLimit)
{
*(myOffsets++) = offsetNum;
*(myTarget++) = *tempPtr;
}
else
{
cnv->charErrorBuffer[cnv->charErrorBufferLength++] = *tempPtr;
*err = U_BUFFER_OVERFLOW_ERROR;
}
}
}
offsetNum = nextSourceIndex;
}
}
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
{
*err = U_BUFFER_OVERFLOW_ERROR;
}
args->target = (char *) myTarget;
args->source = mySource;
args->offsets = myOffsets;
}
static UChar32 ucnv_getNextUChar_UTF8(UConverterToUnicodeArgs *args,
UErrorCode *err) {
UConverter *cnv;
const uint8_t *sourceInitial;
const uint8_t *source;
uint16_t extraBytesToWrite;
uint8_t myByte;
UChar32 ch;
int8_t i, isLegalSequence;
/* UTF-8 only here, the framework handles CESU-8 to combine surrogate pairs */
cnv = args->converter;
sourceInitial = source = (const uint8_t *)args->source;
if (source >= (const uint8_t *)args->sourceLimit)
{
/* no input */
*err = U_INDEX_OUTOFBOUNDS_ERROR;
return 0xffff;
}
myByte = (uint8_t)*(source++);
if (myByte < 0x80)
{
args->source = (const char *)source;
return (UChar32)myByte;
}
extraBytesToWrite = (uint16_t)bytesFromUTF8[myByte];
if (extraBytesToWrite == 0) {
cnv->toUBytes[0] = myByte;
cnv->toULength = 1;
*err = U_ILLEGAL_CHAR_FOUND;
args->source = (const char *)source;
return 0xffff;
}
/*The byte sequence is longer than the buffer area passed*/
if (((const char *)source + extraBytesToWrite - 1) > args->sourceLimit)
{
/* check if all of the remaining bytes are trail bytes */
cnv->toUBytes[0] = myByte;
i = 1;
*err = U_TRUNCATED_CHAR_FOUND;
while(source < (const uint8_t *)args->sourceLimit) {
if(U8_IS_TRAIL(myByte = *source)) {
cnv->toUBytes[i++] = myByte;
++source;
} else {
/* error even before we run out of input */
*err = U_ILLEGAL_CHAR_FOUND;
break;
}
}
cnv->toULength = i;
args->source = (const char *)source;
return 0xffff;
}
isLegalSequence = 1;
ch = myByte << 6;
switch(extraBytesToWrite)
{
/* note: code falls through cases! (sic)*/
case 6:
ch += (myByte = *source);
ch <<= 6;
if (!U8_IS_TRAIL(myByte))
{
isLegalSequence = 0;
break;
}
++source;
U_FALLTHROUGH;
case 5:
ch += (myByte = *source);
ch <<= 6;
if (!U8_IS_TRAIL(myByte))
{
isLegalSequence = 0;
break;
}
++source;
U_FALLTHROUGH;
case 4:
ch += (myByte = *source);
ch <<= 6;
if (!U8_IS_TRAIL(myByte))
{
isLegalSequence = 0;
break;
}
++source;
U_FALLTHROUGH;
case 3:
ch += (myByte = *source);
ch <<= 6;
if (!U8_IS_TRAIL(myByte))
{
isLegalSequence = 0;
break;
}
++source;
U_FALLTHROUGH;
case 2:
ch += (myByte = *source);
if (!U8_IS_TRAIL(myByte))
{
isLegalSequence = 0;
break;
}
++source;
};
ch -= offsetsFromUTF8[extraBytesToWrite];
args->source = (const char *)source;
/*
* Legal UTF-8 byte sequences in Unicode 3.0.1 and up:
* - use only trail bytes after a lead byte (checked above)
* - use the right number of trail bytes for a given lead byte
* - encode a code point <= U+10ffff
* - use the fewest possible number of bytes for their code points
* - use at most 4 bytes (for i>=5 it is 0x10ffff<utf8_minChar32[])
*
* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8.
* There are no irregular sequences any more.
*/
if (isLegalSequence &&
(uint32_t)ch <= MAXIMUM_UTF &&
(uint32_t)ch >= utf8_minChar32[extraBytesToWrite] &&
!U_IS_SURROGATE(ch)
) {
return ch; /* return the code point */
}
for(i = 0; sourceInitial < source; ++i) {
cnv->toUBytes[i] = *sourceInitial++;
}
cnv->toULength = i;
*err = U_ILLEGAL_CHAR_FOUND;
return 0xffff;
}
/* UTF-8-from-UTF-8 conversion functions ------------------------------------ */
/* minimum code point values for n-byte UTF-8 sequences, n=0..4 */
static const UChar32
utf8_minLegal[5]={ 0, 0, 0x80, 0x800, 0x10000 };
/* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */
static const UChar32
utf8_offsets[7]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 };
/* "Convert" UTF-8 to UTF-8: Validate and copy. Modified from ucnv_DBCSFromUTF8(). */
static void
ucnv_UTF8FromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
UConverterToUnicodeArgs *pToUArgs,
UErrorCode *pErrorCode) {
UConverter *utf8;
const uint8_t *source, *sourceLimit;
uint8_t *target;
int32_t targetCapacity;
int32_t count;
int8_t oldToULength, toULength, toULimit;
UChar32 c;
uint8_t b, t1, t2;
/* set up the local pointers */
utf8=pToUArgs->converter;
source=(uint8_t *)pToUArgs->source;
sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
target=(uint8_t *)pFromUArgs->target;
targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target);
/* get the converter state from the UTF-8 UConverter */
c=(UChar32)utf8->toUnicodeStatus;
if(c!=0) {
toULength=oldToULength=utf8->toULength;
toULimit=(int8_t)utf8->mode;
} else {
toULength=oldToULength=toULimit=0;
}
count=(int32_t)(sourceLimit-source)+oldToULength;
if(count<toULimit) {
/*
* Not enough input to complete the partial character.
* Jump to moreBytes below - it will not output to target.
*/
} else if(targetCapacity<toULimit) {
/*
* Not enough target capacity to output the partial character.
* Let the standard converter handle this.
*/
*pErrorCode=U_USING_DEFAULT_WARNING;
return;
} else {
/*
* Use a single counter for source and target, counting the minimum of
* the source length and the target capacity.
* As a result, the source length is checked only once per multi-byte
* character instead of twice.
*
* Make sure that the last byte sequence is complete, or else
* stop just before it.
* (The longest legal byte sequence has 3 trail bytes.)
* Count oldToULength (number of source bytes from a previous buffer)
* into the source length but reduce the source index by toULimit
* while going back over trail bytes in order to not go back into
* the bytes that will be read for finishing a partial
* sequence from the previous buffer.
* Let the standard converter handle edge cases.
*/
int32_t i;
if(count>targetCapacity) {
count=targetCapacity;
}
i=0;
while(i<3 && i<(count-toULimit)) {
b=source[count-oldToULength-i-1];
if(U8_IS_TRAIL(b)) {
++i;
} else {
if(i<U8_COUNT_TRAIL_BYTES(b)) {
/* stop converting before the lead byte if there are not enough trail bytes for it */
count-=i+1;
}
break;
}
}
}
if(c!=0) {
utf8->toUnicodeStatus=0;
utf8->toULength=0;
goto moreBytes;
/* See note in ucnv_SBCSFromUTF8() about this goto. */
}
/* conversion loop */
while(count>0) {
b=*source++;
if((int8_t)b>=0) {
/* convert ASCII */
*target++=b;
--count;
continue;
} else {
if(b>0xe0) {
if( /* handle U+1000..U+D7FF inline */
(t1=source[0]) >= 0x80 && ((b<0xed && (t1 <= 0xbf)) ||
(b==0xed && (t1 <= 0x9f))) &&
(t2=source[1]) >= 0x80 && t2 <= 0xbf
) {
source+=2;
*target++=b;
*target++=t1;
*target++=t2;
count-=3;
continue;
}
} else if(b<0xe0) {
if( /* handle U+0080..U+07FF inline */
b>=0xc2 &&
(t1=*source) >= 0x80 && t1 <= 0xbf
) {
++source;
*target++=b;
*target++=t1;
count-=2;
continue;
}
} else if(b==0xe0) {
if( /* handle U+0800..U+0FFF inline */
(t1=source[0]) >= 0xa0 && t1 <= 0xbf &&
(t2=source[1]) >= 0x80 && t2 <= 0xbf
) {
source+=2;
*target++=b;
*target++=t1;
*target++=t2;
count-=3;
continue;
}
}
/* handle "complicated" and error cases, and continuing partial characters */
oldToULength=0;
toULength=1;
toULimit=U8_COUNT_TRAIL_BYTES(b)+1;
c=b;
moreBytes:
while(toULength<toULimit) {
if(source<sourceLimit) {
b=*source;
if(U8_IS_TRAIL(b)) {
++source;
++toULength;
c=(c<<6)+b;
} else {
break; /* sequence too short, stop with toULength<toULimit */
}
} else {
/* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
source-=(toULength-oldToULength);
while(oldToULength<toULength) {
utf8->toUBytes[oldToULength++]=*source++;
}
utf8->toUnicodeStatus=c;
utf8->toULength=toULength;
utf8->mode=toULimit;
pToUArgs->source=(char *)source;
pFromUArgs->target=(char *)target;
return;
}
}
if( toULength==toULimit && /* consumed all trail bytes */
(toULength==3 || toULength==2) && /* BMP */
(c-=utf8_offsets[toULength])>=utf8_minLegal[toULength] &&
(c<=0xd7ff || 0xe000<=c) /* not a surrogate */
) {
/* legal byte sequence for BMP code point */
} else if(
toULength==toULimit && toULength==4 &&
(0x10000<=(c-=utf8_offsets[4]) && c<=0x10ffff)
) {
/* legal byte sequence for supplementary code point */
} else {
/* error handling: illegal UTF-8 byte sequence */
source-=(toULength-oldToULength);
while(oldToULength<toULength) {
utf8->toUBytes[oldToULength++]=*source++;
}
utf8->toULength=toULength;
pToUArgs->source=(char *)source;
pFromUArgs->target=(char *)target;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
return;
}
/* copy the legal byte sequence to the target */
{
int8_t i;
for(i=0; i<oldToULength; ++i) {
*target++=utf8->toUBytes[i];
}
source-=(toULength-oldToULength);
for(; i<toULength; ++i) {
*target++=*source++;
}
count-=toULength;
}
}
}
if(U_SUCCESS(*pErrorCode) && source<sourceLimit) {
if(target==(const uint8_t *)pFromUArgs->targetLimit) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
} else {
b=*source;
toULimit=U8_COUNT_TRAIL_BYTES(b)+1;
if(toULimit>(sourceLimit-source)) {
/* collect a truncated byte sequence */
toULength=0;
c=b;
for(;;) {
utf8->toUBytes[toULength++]=b;
if(++source==sourceLimit) {
/* partial byte sequence at end of source */
utf8->toUnicodeStatus=c;
utf8->toULength=toULength;
utf8->mode=toULimit;
break;
} else if(!U8_IS_TRAIL(b=*source)) {
/* lead byte in trail byte position */
utf8->toULength=toULength;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
break;
}
c=(c<<6)+b;
}
} else {
/* partial-sequence target overflow: fall back to the pivoting implementation */
*pErrorCode=U_USING_DEFAULT_WARNING;
}
}
}
/* write back the updated pointers */
pToUArgs->source=(char *)source;
pFromUArgs->target=(char *)target;
}
/* UTF-8 converter data ----------------------------------------------------- */
static const UConverterImpl _UTF8Impl={
UCNV_UTF8,
NULL,
NULL,
NULL,
NULL,
NULL,
ucnv_toUnicode_UTF8,
ucnv_toUnicode_UTF8_OFFSETS_LOGIC,
ucnv_fromUnicode_UTF8,
ucnv_fromUnicode_UTF8_OFFSETS_LOGIC,
ucnv_getNextUChar_UTF8,
NULL,
NULL,
NULL,
NULL,
ucnv_getNonSurrogateUnicodeSet,
ucnv_UTF8FromUTF8,
ucnv_UTF8FromUTF8
};
/* The 1208 CCSID refers to any version of Unicode of UTF-8 */
static const UConverterStaticData _UTF8StaticData={
sizeof(UConverterStaticData),
"UTF-8",
1208, UCNV_IBM, UCNV_UTF8,
1, 3, /* max 3 bytes per UChar from UTF-8 (4 bytes from surrogate _pair_) */
{ 0xef, 0xbf, 0xbd, 0 },3,FALSE,FALSE,
0,
0,
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
};
const UConverterSharedData _UTF8Data=
UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_UTF8StaticData, &_UTF8Impl);
/* CESU-8 converter data ---------------------------------------------------- */
static const UConverterImpl _CESU8Impl={
UCNV_CESU8,
NULL,
NULL,
NULL,
NULL,
NULL,
ucnv_toUnicode_UTF8,
ucnv_toUnicode_UTF8_OFFSETS_LOGIC,
ucnv_fromUnicode_UTF8,
ucnv_fromUnicode_UTF8_OFFSETS_LOGIC,
NULL,
NULL,
NULL,
NULL,
NULL,
ucnv_getCompleteUnicodeSet,
NULL,
NULL
};
static const UConverterStaticData _CESU8StaticData={
sizeof(UConverterStaticData),
"CESU-8",
9400, /* CCSID for CESU-8 */
UCNV_UNKNOWN, UCNV_CESU8, 1, 3,
{ 0xef, 0xbf, 0xbd, 0 },3,FALSE,FALSE,
0,
0,
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
};
const UConverterSharedData _CESU8Data=
UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_CESU8StaticData, &_CESU8Impl);
#endif
|