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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2005-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: swapimpl.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2005may05
* created by: Markus W. Scherer
*
* Data file swapping functions moved here from the common library
* because some data is hardcoded in ICU4C and needs not be swapped any more.
* Moving the functions here simplifies testing (for code coverage) because
* we need not jump through hoops (like adding snapshots of these files
* to testdata).
*
* The declarations for these functions remain in the internal header files
* in icu/source/common/
*/
#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "unicode/udata.h"
/* Explicit include statement for std_string.h is needed
* for compilation on certain platforms. (e.g. AIX/VACPP)
*/
#include "unicode/std_string.h"
#include "cmemory.h"
#include "cstring.h"
#include "uinvchar.h"
#include "uassert.h"
#include "uarrsort.h"
#include "ucmndata.h"
#include "udataswp.h"
#include "ulayout_props.h"
/* swapping implementations in common */
#include "emojiprops.h"
#include "uresdata.h"
#include "ucnv_io.h"
#include "uprops.h"
#include "ucase.h"
#include "ubidi_props.h"
#include "ucol_swp.h"
#include "ucnv_bld.h"
#include "unormimp.h"
#include "normalizer2impl.h"
#include "sprpimpl.h"
#include "propname.h"
#include "rbbidata.h"
#include "utrie.h"
#include "utrie2.h"
#include "dictionarydata.h"
/* swapping implementations in i18n */
#if !UCONFIG_NO_NORMALIZATION
#include "uspoof_impl.h"
#endif
U_NAMESPACE_USE
/* definitions */
/* Unicode property (value) aliases data swapping --------------------------- */
static int32_t U_CALLCONV
upname_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
/* udata_swapDataHeader checks the arguments */
int32_t headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
/* check data format and format version */
const UDataInfo *pInfo=
reinterpret_cast<const UDataInfo *>(
static_cast<const char *>(inData)+4);
if(!(
pInfo->dataFormat[0]==0x70 && /* dataFormat="pnam" */
pInfo->dataFormat[1]==0x6e &&
pInfo->dataFormat[2]==0x61 &&
pInfo->dataFormat[3]==0x6d &&
pInfo->formatVersion[0]==2
)) {
udata_printError(ds, "upname_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as pnames.icu\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
const uint8_t *inBytes=static_cast<const uint8_t *>(inData)+headerSize;
uint8_t *outBytes=static_cast<uint8_t *>(outData)+headerSize;
if(length>=0) {
length-=headerSize;
// formatVersion 2 initially has indexes[8], 32 bytes.
if(length<32) {
udata_printError(ds, "upname_swap(): too few bytes (%d after header) for pnames.icu\n",
(int)length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
}
const int32_t *inIndexes=reinterpret_cast<const int32_t *>(inBytes);
int32_t totalSize=udata_readInt32(ds, inIndexes[PropNameData::IX_TOTAL_SIZE]);
if(length>=0) {
if(length<totalSize) {
udata_printError(ds, "upname_swap(): too few bytes (%d after header, should be %d) "
"for pnames.icu\n",
(int)length, (int)totalSize);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
int32_t numBytesIndexesAndValueMaps=
udata_readInt32(ds, inIndexes[PropNameData::IX_BYTE_TRIES_OFFSET]);
// Swap the indexes[] and the valueMaps[].
ds->swapArray32(ds, inBytes, numBytesIndexesAndValueMaps, outBytes, pErrorCode);
// Copy the rest of the data.
if(inBytes!=outBytes) {
uprv_memcpy(outBytes+numBytesIndexesAndValueMaps,
inBytes+numBytesIndexesAndValueMaps,
totalSize-numBytesIndexesAndValueMaps);
}
// We need not swap anything else:
//
// The ByteTries are already byte-serialized, and are fixed on ASCII.
// (On an EBCDIC machine, the input string is converted to lowercase ASCII
// while matching.)
//
// The name groups are mostly invariant characters, but since we only
// generate, and keep in subversion, ASCII versions of pnames.icu,
// and since only ICU4J uses the pnames.icu data file
// (the data is hardcoded in ICU4C) and ICU4J uses ASCII data files,
// we just copy those bytes too.
}
return headerSize+totalSize;
}
/* Unicode properties data swapping ----------------------------------------- */
static int32_t U_CALLCONV
uprops_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const UDataInfo *pInfo;
int32_t headerSize, i;
int32_t dataIndexes[UPROPS_INDEX_COUNT];
const int32_t *inData32;
/* udata_swapDataHeader checks the arguments */
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
/* check data format and format version */
pInfo=(const UDataInfo *)((const char *)inData+4);
if(!(
pInfo->dataFormat[0]==0x55 && /* dataFormat="UPro" */
pInfo->dataFormat[1]==0x50 &&
pInfo->dataFormat[2]==0x72 &&
pInfo->dataFormat[3]==0x6f &&
(3<=pInfo->formatVersion[0] && pInfo->formatVersion[0]<=7) &&
(pInfo->formatVersion[0]>=7 ||
(pInfo->formatVersion[2]==UTRIE_SHIFT &&
pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT))
)) {
udata_printError(ds, "uprops_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not a Unicode properties file\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
/* the properties file must contain at least the indexes array */
if(length>=0 && (length-headerSize)<(int32_t)sizeof(dataIndexes)) {
udata_printError(ds, "uprops_swap(): too few bytes (%d after header) for a Unicode properties file\n",
length-headerSize);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
/* read the indexes */
inData32=(const int32_t *)((const char *)inData+headerSize);
for(i=0; i<UPROPS_INDEX_COUNT; ++i) {
dataIndexes[i]=udata_readInt32(ds, inData32[i]);
}
/*
* comments are copied from the data format description in genprops/store.c
* indexes[] constants are in uprops.h
*/
int32_t dataTop;
if(length>=0) {
int32_t *outData32;
/*
* In formatVersion 7, UPROPS_DATA_TOP_INDEX has the post-header data size.
* In earlier formatVersions, it is 0 and a lower dataIndexes entry
* has the top of the last item.
*/
for(i=UPROPS_DATA_TOP_INDEX; i>0 && (dataTop=dataIndexes[i])==0; --i) {}
if((length-headerSize)<(4*dataTop)) {
udata_printError(ds, "uprops_swap(): too few bytes (%d after header) for a Unicode properties file\n",
length-headerSize);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
outData32=(int32_t *)((char *)outData+headerSize);
/* copy everything for inaccessible data (padding) */
if(inData32!=outData32) {
uprv_memcpy(outData32, inData32, 4*(size_t)dataTop);
}
/* swap the indexes[16] */
ds->swapArray32(ds, inData32, 4*UPROPS_INDEX_COUNT, outData32, pErrorCode);
/*
* swap the main properties UTrie
* PT serialized properties trie, see utrie.h (byte size: 4*(i0-16))
*/
utrie_swapAnyVersion(ds,
inData32+UPROPS_INDEX_COUNT,
4*(dataIndexes[UPROPS_PROPS32_INDEX]-UPROPS_INDEX_COUNT),
outData32+UPROPS_INDEX_COUNT,
pErrorCode);
/*
* swap the properties and exceptions words
* P const uint32_t props32[i1-i0];
* E const uint32_t exceptions[i2-i1];
*/
ds->swapArray32(ds,
inData32+dataIndexes[UPROPS_PROPS32_INDEX],
4*(dataIndexes[UPROPS_EXCEPTIONS_TOP_INDEX]-dataIndexes[UPROPS_PROPS32_INDEX]),
outData32+dataIndexes[UPROPS_PROPS32_INDEX],
pErrorCode);
/*
* swap the UChars
* U const char16_t uchars[2*(i3-i2)];
*/
ds->swapArray16(ds,
inData32+dataIndexes[UPROPS_EXCEPTIONS_TOP_INDEX],
4*(dataIndexes[UPROPS_ADDITIONAL_TRIE_INDEX]-dataIndexes[UPROPS_EXCEPTIONS_TOP_INDEX]),
outData32+dataIndexes[UPROPS_EXCEPTIONS_TOP_INDEX],
pErrorCode);
/*
* swap the additional UTrie
* i3 additionalTrieIndex; -- 32-bit unit index to the additional trie for more properties
*/
utrie_swapAnyVersion(ds,
inData32+dataIndexes[UPROPS_ADDITIONAL_TRIE_INDEX],
4*(dataIndexes[UPROPS_ADDITIONAL_VECTORS_INDEX]-dataIndexes[UPROPS_ADDITIONAL_TRIE_INDEX]),
outData32+dataIndexes[UPROPS_ADDITIONAL_TRIE_INDEX],
pErrorCode);
/*
* swap the properties vectors
* PV const uint32_t propsVectors[(i6-i4)/i5][i5]==uint32_t propsVectors[i6-i4];
*/
ds->swapArray32(ds,
inData32+dataIndexes[UPROPS_ADDITIONAL_VECTORS_INDEX],
4*(dataIndexes[UPROPS_SCRIPT_EXTENSIONS_INDEX]-dataIndexes[UPROPS_ADDITIONAL_VECTORS_INDEX]),
outData32+dataIndexes[UPROPS_ADDITIONAL_VECTORS_INDEX],
pErrorCode);
// swap the Script_Extensions data
// SCX const uint16_t scriptExtensions[2*(i7-i6)];
ds->swapArray16(ds,
inData32+dataIndexes[UPROPS_SCRIPT_EXTENSIONS_INDEX],
4*(dataIndexes[UPROPS_RESERVED_INDEX_7]-dataIndexes[UPROPS_SCRIPT_EXTENSIONS_INDEX]),
outData32+dataIndexes[UPROPS_SCRIPT_EXTENSIONS_INDEX],
pErrorCode);
}
/* i7 reservedIndex7; -- 32-bit unit index to the top of the Script_Extensions data */
return headerSize+4*dataIndexes[UPROPS_RESERVED_INDEX_7];
}
/* Unicode case mapping data swapping --------------------------------------- */
static int32_t U_CALLCONV
ucase_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const UDataInfo *pInfo;
int32_t headerSize;
const uint8_t *inBytes;
uint8_t *outBytes;
const int32_t *inIndexes;
int32_t indexes[16];
int32_t i, offset, count, size;
/* udata_swapDataHeader checks the arguments */
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
/* check data format and format version */
pInfo=(const UDataInfo *)((const char *)inData+4);
if(!(
pInfo->dataFormat[0]==UCASE_FMT_0 && /* dataFormat="cAsE" */
pInfo->dataFormat[1]==UCASE_FMT_1 &&
pInfo->dataFormat[2]==UCASE_FMT_2 &&
pInfo->dataFormat[3]==UCASE_FMT_3 &&
((pInfo->formatVersion[0]==1 &&
pInfo->formatVersion[2]==UTRIE_SHIFT &&
pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT) ||
(2<=pInfo->formatVersion[0] && pInfo->formatVersion[0]<=4))
)) {
udata_printError(ds, "ucase_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as case mapping data\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
inBytes=(const uint8_t *)inData+headerSize;
outBytes=(uint8_t *)outData+headerSize;
inIndexes=(const int32_t *)inBytes;
if(length>=0) {
length-=headerSize;
if(length<16*4) {
udata_printError(ds, "ucase_swap(): too few bytes (%d after header) for case mapping data\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
}
/* read the first 16 indexes (ICU 3.2/format version 1: UCASE_IX_TOP==16, might grow) */
for(i=0; i<16; ++i) {
indexes[i]=udata_readInt32(ds, inIndexes[i]);
}
/* get the total length of the data */
size=indexes[UCASE_IX_LENGTH];
if(length>=0) {
if(length<size) {
udata_printError(ds, "ucase_swap(): too few bytes (%d after header) for all of case mapping data\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
/* copy the data for inaccessible bytes */
if(inBytes!=outBytes) {
uprv_memcpy(outBytes, inBytes, size);
}
offset=0;
/* swap the int32_t indexes[] */
count=indexes[UCASE_IX_INDEX_TOP]*4;
ds->swapArray32(ds, inBytes, count, outBytes, pErrorCode);
offset+=count;
/* swap the UTrie */
count=indexes[UCASE_IX_TRIE_SIZE];
utrie_swapAnyVersion(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
/* swap the uint16_t exceptions[] and unfold[] */
count=(indexes[UCASE_IX_EXC_LENGTH]+indexes[UCASE_IX_UNFOLD_LENGTH])*2;
ds->swapArray16(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
U_ASSERT(offset==size);
}
return headerSize+size;
}
/* Unicode bidi/shaping data swapping --------------------------------------- */
static int32_t U_CALLCONV
ubidi_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const UDataInfo *pInfo;
int32_t headerSize;
const uint8_t *inBytes;
uint8_t *outBytes;
const int32_t *inIndexes;
int32_t indexes[16];
int32_t i, offset, count, size;
/* udata_swapDataHeader checks the arguments */
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
/* check data format and format version */
pInfo=(const UDataInfo *)((const char *)inData+4);
if(!(
pInfo->dataFormat[0]==UBIDI_FMT_0 && /* dataFormat="BiDi" */
pInfo->dataFormat[1]==UBIDI_FMT_1 &&
pInfo->dataFormat[2]==UBIDI_FMT_2 &&
pInfo->dataFormat[3]==UBIDI_FMT_3 &&
((pInfo->formatVersion[0]==1 &&
pInfo->formatVersion[2]==UTRIE_SHIFT &&
pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT) ||
pInfo->formatVersion[0]==2)
)) {
udata_printError(ds, "ubidi_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as bidi/shaping data\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
inBytes=(const uint8_t *)inData+headerSize;
outBytes=(uint8_t *)outData+headerSize;
inIndexes=(const int32_t *)inBytes;
if(length>=0) {
length-=headerSize;
if(length<16*4) {
udata_printError(ds, "ubidi_swap(): too few bytes (%d after header) for bidi/shaping data\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
}
/* read the first 16 indexes (ICU 3.4/format version 1: UBIDI_IX_TOP==16, might grow) */
for(i=0; i<16; ++i) {
indexes[i]=udata_readInt32(ds, inIndexes[i]);
}
/* get the total length of the data */
size=indexes[UBIDI_IX_LENGTH];
if(length>=0) {
if(length<size) {
udata_printError(ds, "ubidi_swap(): too few bytes (%d after header) for all of bidi/shaping data\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
/* copy the data for inaccessible bytes */
if(inBytes!=outBytes) {
uprv_memcpy(outBytes, inBytes, size);
}
offset=0;
/* swap the int32_t indexes[] */
count=indexes[UBIDI_IX_INDEX_TOP]*4;
ds->swapArray32(ds, inBytes, count, outBytes, pErrorCode);
offset+=count;
/* swap the UTrie */
count=indexes[UBIDI_IX_TRIE_SIZE];
utrie_swapAnyVersion(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
/* swap the uint32_t mirrors[] */
count=indexes[UBIDI_IX_MIRROR_LENGTH]*4;
ds->swapArray32(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
/* just skip the uint8_t jgArray[] and jgArray2[] */
count=indexes[UBIDI_IX_JG_LIMIT]-indexes[UBIDI_IX_JG_START];
offset+=count;
count=indexes[UBIDI_IX_JG_LIMIT2]-indexes[UBIDI_IX_JG_START2];
offset+=count;
U_ASSERT(offset==size);
}
return headerSize+size;
}
/* Unicode normalization data swapping -------------------------------------- */
#if !UCONFIG_NO_NORMALIZATION
static int32_t U_CALLCONV
unorm_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const UDataInfo *pInfo;
int32_t headerSize;
const uint8_t *inBytes;
uint8_t *outBytes;
const int32_t *inIndexes;
int32_t indexes[32];
int32_t i, offset, count, size;
/* udata_swapDataHeader checks the arguments */
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
/* check data format and format version */
pInfo=(const UDataInfo *)((const char *)inData+4);
if(!(
pInfo->dataFormat[0]==0x4e && /* dataFormat="Norm" */
pInfo->dataFormat[1]==0x6f &&
pInfo->dataFormat[2]==0x72 &&
pInfo->dataFormat[3]==0x6d &&
pInfo->formatVersion[0]==2
)) {
udata_printError(ds, "unorm_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as unorm.icu\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
inBytes=(const uint8_t *)inData+headerSize;
outBytes=(uint8_t *)outData+headerSize;
inIndexes=(const int32_t *)inBytes;
if(length>=0) {
length-=headerSize;
if(length<32*4) {
udata_printError(ds, "unorm_swap(): too few bytes (%d after header) for unorm.icu\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
}
/* read the first 32 indexes (ICU 2.8/format version 2.2: _NORM_INDEX_TOP==32, might grow) */
for(i=0; i<32; ++i) {
indexes[i]=udata_readInt32(ds, inIndexes[i]);
}
/* calculate the total length of the data */
size=
32*4+ /* size of indexes[] */
indexes[_NORM_INDEX_TRIE_SIZE]+
indexes[_NORM_INDEX_UCHAR_COUNT]*2+
indexes[_NORM_INDEX_COMBINE_DATA_COUNT]*2+
indexes[_NORM_INDEX_FCD_TRIE_SIZE]+
indexes[_NORM_INDEX_AUX_TRIE_SIZE]+
indexes[_NORM_INDEX_CANON_SET_COUNT]*2;
if(length>=0) {
if(length<size) {
udata_printError(ds, "unorm_swap(): too few bytes (%d after header) for all of unorm.icu\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
/* copy the data for inaccessible bytes */
if(inBytes!=outBytes) {
uprv_memcpy(outBytes, inBytes, size);
}
offset=0;
/* swap the indexes[] */
count=32*4;
ds->swapArray32(ds, inBytes, count, outBytes, pErrorCode);
offset+=count;
/* swap the main UTrie */
count=indexes[_NORM_INDEX_TRIE_SIZE];
utrie_swap(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
/* swap the uint16_t extraData[] and the uint16_t combiningTable[] */
count=(indexes[_NORM_INDEX_UCHAR_COUNT]+indexes[_NORM_INDEX_COMBINE_DATA_COUNT])*2;
ds->swapArray16(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
/* swap the FCD UTrie */
count=indexes[_NORM_INDEX_FCD_TRIE_SIZE];
if(count!=0) {
utrie_swap(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
}
/* swap the aux UTrie */
count=indexes[_NORM_INDEX_AUX_TRIE_SIZE];
if(count!=0) {
utrie_swap(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
}
/* swap the uint16_t combiningTable[] */
count=indexes[_NORM_INDEX_CANON_SET_COUNT]*2;
ds->swapArray16(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
offset+=count;
}
return headerSize+size;
}
#endif
// Unicode text layout properties data swapping --------------------------------
static int32_t U_CALLCONV
ulayout_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
// udata_swapDataHeader checks the arguments.
int32_t headerSize = udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if (pErrorCode == nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
// Check data format and format version.
const UDataInfo *pInfo = (const UDataInfo *)((const char *)inData + 4);
if (!(
pInfo->dataFormat[0] == ULAYOUT_FMT_0 && // dataFormat="Layo"
pInfo->dataFormat[1] == ULAYOUT_FMT_1 &&
pInfo->dataFormat[2] == ULAYOUT_FMT_2 &&
pInfo->dataFormat[3] == ULAYOUT_FMT_3 &&
pInfo->formatVersion[0] == 1)) {
udata_printError(ds,
"ulayout_swap(): data format %02x.%02x.%02x.%02x (format version %02x) "
"is not recognized as text layout properties data\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode = U_UNSUPPORTED_ERROR;
return 0;
}
const uint8_t *inBytes = (const uint8_t *)inData + headerSize;
uint8_t *outBytes = (uint8_t *)outData + headerSize;
const int32_t *inIndexes = (const int32_t *)inBytes;
if (length >= 0) {
length -= headerSize;
if (length < 12 * 4) {
udata_printError(ds,
"ulayout_swap(): too few bytes (%d after header) for text layout properties data\n",
length);
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
}
int32_t indexesLength = udata_readInt32(ds, inIndexes[ULAYOUT_IX_INDEXES_LENGTH]);
if (indexesLength < 12) {
udata_printError(ds,
"ulayout_swap(): too few indexes (%d) for text layout properties data\n",
indexesLength);
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
// Read the data offsets before swapping anything.
int32_t indexes[ULAYOUT_IX_TRIES_TOP + 1];
for (int32_t i = ULAYOUT_IX_INPC_TRIE_TOP; i <= ULAYOUT_IX_TRIES_TOP; ++i) {
indexes[i] = udata_readInt32(ds, inIndexes[i]);
}
int32_t size = indexes[ULAYOUT_IX_TRIES_TOP];
if (length >= 0) {
if (length < size) {
udata_printError(ds,
"ulayout_swap(): too few bytes (%d after header) "
"for all of text layout properties data\n",
length);
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
// Copy the data for inaccessible bytes.
if (inBytes != outBytes) {
uprv_memcpy(outBytes, inBytes, size);
}
// Swap the int32_t indexes[].
int32_t offset = 0;
int32_t count = indexesLength * 4;
ds->swapArray32(ds, inBytes, count, outBytes, pErrorCode);
offset += count;
// Swap each trie.
for (int32_t i = ULAYOUT_IX_INPC_TRIE_TOP; i <= ULAYOUT_IX_TRIES_TOP; ++i) {
int32_t top = indexes[i];
count = top - offset;
U_ASSERT(count >= 0);
if (count >= 16) {
utrie_swapAnyVersion(ds, inBytes + offset, count, outBytes + offset, pErrorCode);
}
offset = top;
}
U_ASSERT(offset == size);
}
return headerSize + size;
}
// Unicode emoji properties data swapping --------------------------------------
static int32_t U_CALLCONV
uemoji_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
// udata_swapDataHeader checks the arguments.
int32_t headerSize = udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if (pErrorCode == nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
// Check data format and format version.
const UDataInfo *pInfo = (const UDataInfo *)((const char *)inData + 4);
if (!(
pInfo->dataFormat[0] == u'E' &&
pInfo->dataFormat[1] == u'm' &&
pInfo->dataFormat[2] == u'o' &&
pInfo->dataFormat[3] == u'j' &&
pInfo->formatVersion[0] == 1)) {
udata_printError(ds,
"uemoji_swap(): data format %02x.%02x.%02x.%02x (format version %02x) "
"is not recognized as emoji properties data\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode = U_UNSUPPORTED_ERROR;
return 0;
}
const uint8_t *inBytes = (const uint8_t *)inData + headerSize;
uint8_t *outBytes = (uint8_t *)outData + headerSize;
const int32_t *inIndexes = (const int32_t *)inBytes;
if (length >= 0) {
length -= headerSize;
// We expect to read at least EmojiProps::IX_TOTAL_SIZE.
if (length < 14 * 4) {
udata_printError(ds,
"uemoji_swap(): too few bytes (%d after header) for emoji properties data\n",
length);
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
}
// First offset after indexes[].
int32_t cpTrieOffset = udata_readInt32(ds, inIndexes[EmojiProps::IX_CPTRIE_OFFSET]);
int32_t indexesLength = cpTrieOffset / 4;
if (indexesLength < 14) {
udata_printError(ds,
"uemoji_swap(): too few indexes (%d) for emoji properties data\n",
indexesLength);
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
// Read the data offsets before swapping anything.
int32_t indexes[EmojiProps::IX_TOTAL_SIZE + 1];
indexes[0] = cpTrieOffset;
for (int32_t i = 1; i <= EmojiProps::IX_TOTAL_SIZE; ++i) {
indexes[i] = udata_readInt32(ds, inIndexes[i]);
}
int32_t size = indexes[EmojiProps::IX_TOTAL_SIZE];
if (length >= 0) {
if (length < size) {
udata_printError(ds,
"uemoji_swap(): too few bytes (%d after header) "
"for all of emoji properties data\n",
length);
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
// Copy the data for inaccessible bytes.
if (inBytes != outBytes) {
uprv_memcpy(outBytes, inBytes, size);
}
// Swap the int32_t indexes[].
int32_t offset = 0;
int32_t top = cpTrieOffset;
ds->swapArray32(ds, inBytes, top - offset, outBytes, pErrorCode);
offset = top;
// Swap the code point trie.
top = indexes[EmojiProps::IX_CPTRIE_OFFSET + 1];
int32_t count = top - offset;
U_ASSERT(count >= 0);
if (count >= 16) {
utrie_swapAnyVersion(ds, inBytes + offset, count, outBytes + offset, pErrorCode);
}
offset = top;
// Swap all of the string tries.
// They are all serialized as arrays of 16-bit units.
offset = indexes[EmojiProps::IX_BASIC_EMOJI_TRIE_OFFSET];
top = indexes[EmojiProps::IX_RGI_EMOJI_ZWJ_SEQUENCE_TRIE_OFFSET + 1];
ds->swapArray16(ds, inBytes + offset, top - offset, outBytes + offset, pErrorCode);
offset = top;
U_ASSERT(offset == size);
}
return headerSize + size;
}
/* Swap 'Test' data from gentest */
static int32_t U_CALLCONV
test_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const UDataInfo *pInfo;
int32_t headerSize;
const uint8_t *inBytes;
uint8_t *outBytes;
int32_t offset;
/* udata_swapDataHeader checks the arguments */
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
udata_printError(ds, "test_swap(): data header swap failed %s\n", pErrorCode != nullptr ? u_errorName(*pErrorCode) : "pErrorCode is nullptr");
return 0;
}
/* check data format and format version */
pInfo=(const UDataInfo *)((const char *)inData+4);
if(!(
pInfo->dataFormat[0]==0x54 && /* dataFormat="Norm" */
pInfo->dataFormat[1]==0x65 &&
pInfo->dataFormat[2]==0x73 &&
pInfo->dataFormat[3]==0x74 &&
pInfo->formatVersion[0]==1
)) {
udata_printError(ds, "test_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as testdata\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
inBytes=(const uint8_t *)inData+headerSize;
outBytes=(uint8_t *)outData+headerSize;
int32_t size16 = 2; // 16bit plus padding
int32_t sizeStr = 5; // 4 char inv-str plus null
int32_t size = size16 + sizeStr;
if(length>=0) {
if(length<size) {
udata_printError(ds, "test_swap(): too few bytes (%d after header, wanted %d) for all of testdata\n",
length, size);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
offset =0;
/* swap a 1 entry array */
ds->swapArray16(ds, inBytes+offset, size16, outBytes+offset, pErrorCode);
offset+=size16;
ds->swapInvChars(ds, inBytes+offset, sizeStr, outBytes+offset, pErrorCode);
}
return headerSize+size;
}
/* swap any data (except a .dat package) ------------------------------------ */
static const struct {
uint8_t dataFormat[4];
UDataSwapFn *swapFn;
} swapFns[]={
{ { 0x52, 0x65, 0x73, 0x42 }, ures_swap }, /* dataFormat="ResB" */
#if !UCONFIG_NO_LEGACY_CONVERSION
{ { 0x63, 0x6e, 0x76, 0x74 }, ucnv_swap }, /* dataFormat="cnvt" */
#endif
#if !UCONFIG_NO_CONVERSION
{ { 0x43, 0x76, 0x41, 0x6c }, ucnv_swapAliases }, /* dataFormat="CvAl" */
#endif
#if !UCONFIG_NO_IDNA
{ { 0x53, 0x50, 0x52, 0x50 }, usprep_swap }, /* dataFormat="SPRP" */
#endif
/* insert data formats here, descending by expected frequency of occurrence */
{ { 0x55, 0x50, 0x72, 0x6f }, uprops_swap }, /* dataFormat="UPro" */
{ { UCASE_FMT_0, UCASE_FMT_1, UCASE_FMT_2, UCASE_FMT_3 },
ucase_swap }, /* dataFormat="cAsE" */
{ { UBIDI_FMT_0, UBIDI_FMT_1, UBIDI_FMT_2, UBIDI_FMT_3 },
ubidi_swap }, /* dataFormat="BiDi" */
#if !UCONFIG_NO_NORMALIZATION
{ { 0x4e, 0x6f, 0x72, 0x6d }, unorm_swap }, /* dataFormat="Norm" */
{ { 0x4e, 0x72, 0x6d, 0x32 }, unorm2_swap }, /* dataFormat="Nrm2" */
#endif
{ { ULAYOUT_FMT_0, ULAYOUT_FMT_1, ULAYOUT_FMT_2, ULAYOUT_FMT_3 },
ulayout_swap }, // dataFormat="Layo"
{ { u'E', u'm', u'o', u'j' }, uemoji_swap },
#if !UCONFIG_NO_COLLATION
{ { 0x55, 0x43, 0x6f, 0x6c }, ucol_swap }, /* dataFormat="UCol" */
{ { 0x49, 0x6e, 0x76, 0x43 }, ucol_swapInverseUCA },/* dataFormat="InvC" */
#endif
#if !UCONFIG_NO_BREAK_ITERATION
{ { 0x42, 0x72, 0x6b, 0x20 }, ubrk_swap }, /* dataFormat="Brk " */
{ { 0x44, 0x69, 0x63, 0x74 }, udict_swap }, /* dataFormat="Dict" */
#endif
{ { 0x70, 0x6e, 0x61, 0x6d }, upname_swap }, /* dataFormat="pnam" */
{ { 0x75, 0x6e, 0x61, 0x6d }, uchar_swapNames }, /* dataFormat="unam" */
#if !UCONFIG_NO_NORMALIZATION
{ { 0x43, 0x66, 0x75, 0x20 }, uspoof_swap }, /* dataFormat="Cfu " */
#endif
{ { 0x54, 0x65, 0x73, 0x74 }, test_swap } /* dataFormat="Test" */
};
U_CAPI int32_t U_EXPORT2
udata_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
char dataFormatChars[4];
const UDataInfo *pInfo;
int32_t i, swappedLength;
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
/*
* Preflight the header first; checks for illegal arguments, too.
* Do not swap the header right away because the format-specific swapper
* will swap it, get the headerSize again, and also use the header
* information. Otherwise we would have to pass some of the information
* and not be able to use the UDataSwapFn signature.
*/
udata_swapDataHeader(ds, inData, -1, nullptr, pErrorCode);
/*
* If we wanted udata_swap() to also handle non-loadable data like a UTrie,
* then we could check here for further known magic values and structures.
*/
if(U_FAILURE(*pErrorCode)) {
return 0; /* the data format was not recognized */
}
pInfo=(const UDataInfo *)((const char *)inData+4);
{
/* convert the data format from ASCII to Unicode to the system charset */
char16_t u[4]={
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3]
};
if(uprv_isInvariantUString(u, 4)) {
u_UCharsToChars(u, dataFormatChars, 4);
} else {
dataFormatChars[0]=dataFormatChars[1]=dataFormatChars[2]=dataFormatChars[3]='?';
}
}
/* dispatch to the swap function for the dataFormat */
for(i=0; i<UPRV_LENGTHOF(swapFns); ++i) {
if(0==memcmp(swapFns[i].dataFormat, pInfo->dataFormat, 4)) {
swappedLength=swapFns[i].swapFn(ds, inData, length, outData, pErrorCode);
if(U_FAILURE(*pErrorCode)) {
udata_printError(ds, "udata_swap(): failure swapping data format %02x.%02x.%02x.%02x (\"%c%c%c%c\") - %s\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
dataFormatChars[0], dataFormatChars[1],
dataFormatChars[2], dataFormatChars[3],
u_errorName(*pErrorCode));
} else if(swappedLength<(length-15)) {
/* swapped less than expected */
udata_printError(ds, "udata_swap() warning: swapped only %d out of %d bytes - data format %02x.%02x.%02x.%02x (\"%c%c%c%c\")\n",
swappedLength, length,
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
dataFormatChars[0], dataFormatChars[1],
dataFormatChars[2], dataFormatChars[3],
u_errorName(*pErrorCode));
}
return swappedLength;
}
}
/* the dataFormat was not recognized */
udata_printError(ds, "udata_swap(): unknown data format %02x.%02x.%02x.%02x (\"%c%c%c%c\")\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
dataFormatChars[0], dataFormatChars[1],
dataFormatChars[2], dataFormatChars[3]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
|