1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2005-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: swapimpl.cpp
12 * tab size: 8 (not used)
15 * created on: 2005may05
16 * created by: Markus W. Scherer
18 * Data file swapping functions moved here from the common library
19 * because some data is hardcoded in ICU4C and needs not be swapped any more.
20 * Moving the functions here simplifies testing (for code coverage) because
21 * we need not jump through hoops (like adding snapshots of these files
24 * The declarations for these functions remain in the internal header files
25 * in icu/source/common/
28 #include "unicode/utypes.h"
29 #include "unicode/putil.h"
30 #include "unicode/udata.h"
32 /* Explicit include statement for std_string.h is needed
33 * for compilation on certain platforms. (e.g. AIX/VACPP)
35 #include "unicode/std_string.h"
45 /* swapping implementations in common */
51 #include "ubidi_props.h"
55 #include "normalizer2impl.h"
60 #include "dictionarydata.h"
62 /* swapping implementations in i18n */
64 #if !UCONFIG_NO_NORMALIZATION
65 #include "uspoof_impl.h"
72 /* Unicode property (value) aliases data swapping --------------------------- */
74 static int32_t U_CALLCONV
75 upname_swap(const UDataSwapper
*ds
,
76 const void *inData
, int32_t length
, void *outData
,
77 UErrorCode
*pErrorCode
) {
78 /* udata_swapDataHeader checks the arguments */
79 int32_t headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
80 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
84 /* check data format and format version */
85 const UDataInfo
*pInfo
=
86 reinterpret_cast<const UDataInfo
*>(
87 static_cast<const char *>(inData
)+4);
89 pInfo
->dataFormat
[0]==0x70 && /* dataFormat="pnam" */
90 pInfo
->dataFormat
[1]==0x6e &&
91 pInfo
->dataFormat
[2]==0x61 &&
92 pInfo
->dataFormat
[3]==0x6d &&
93 pInfo
->formatVersion
[0]==2
95 udata_printError(ds
, "upname_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as pnames.icu\n",
96 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
97 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
98 pInfo
->formatVersion
[0]);
99 *pErrorCode
=U_UNSUPPORTED_ERROR
;
103 const uint8_t *inBytes
=static_cast<const uint8_t *>(inData
)+headerSize
;
104 uint8_t *outBytes
=static_cast<uint8_t *>(outData
)+headerSize
;
108 // formatVersion 2 initially has indexes[8], 32 bytes.
110 udata_printError(ds
, "upname_swap(): too few bytes (%d after header) for pnames.icu\n",
112 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
117 const int32_t *inIndexes
=reinterpret_cast<const int32_t *>(inBytes
);
118 int32_t totalSize
=udata_readInt32(ds
, inIndexes
[PropNameData::IX_TOTAL_SIZE
]);
120 if(length
<totalSize
) {
121 udata_printError(ds
, "upname_swap(): too few bytes (%d after header, should be %d) "
123 (int)length
, (int)totalSize
);
124 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
128 int32_t numBytesIndexesAndValueMaps
=
129 udata_readInt32(ds
, inIndexes
[PropNameData::IX_BYTE_TRIES_OFFSET
]);
131 // Swap the indexes[] and the valueMaps[].
132 ds
->swapArray32(ds
, inBytes
, numBytesIndexesAndValueMaps
, outBytes
, pErrorCode
);
134 // Copy the rest of the data.
135 if(inBytes
!=outBytes
) {
136 uprv_memcpy(outBytes
+numBytesIndexesAndValueMaps
,
137 inBytes
+numBytesIndexesAndValueMaps
,
138 totalSize
-numBytesIndexesAndValueMaps
);
141 // We need not swap anything else:
143 // The ByteTries are already byte-serialized, and are fixed on ASCII.
144 // (On an EBCDIC machine, the input string is converted to lowercase ASCII
147 // The name groups are mostly invariant characters, but since we only
148 // generate, and keep in subversion, ASCII versions of pnames.icu,
149 // and since only ICU4J uses the pnames.icu data file
150 // (the data is hardcoded in ICU4C) and ICU4J uses ASCII data files,
151 // we just copy those bytes too.
154 return headerSize
+totalSize
;
157 /* Unicode properties data swapping ----------------------------------------- */
159 static int32_t U_CALLCONV
160 uprops_swap(const UDataSwapper
*ds
,
161 const void *inData
, int32_t length
, void *outData
,
162 UErrorCode
*pErrorCode
) {
163 const UDataInfo
*pInfo
;
164 int32_t headerSize
, i
;
166 int32_t dataIndexes
[UPROPS_INDEX_COUNT
];
167 const int32_t *inData32
;
169 /* udata_swapDataHeader checks the arguments */
170 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
171 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
175 /* check data format and format version */
176 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
178 pInfo
->dataFormat
[0]==0x55 && /* dataFormat="UPro" */
179 pInfo
->dataFormat
[1]==0x50 &&
180 pInfo
->dataFormat
[2]==0x72 &&
181 pInfo
->dataFormat
[3]==0x6f &&
182 (3<=pInfo
->formatVersion
[0] && pInfo
->formatVersion
[0]<=7) &&
183 (pInfo
->formatVersion
[0]>=7 ||
184 (pInfo
->formatVersion
[2]==UTRIE_SHIFT
&&
185 pInfo
->formatVersion
[3]==UTRIE_INDEX_SHIFT
))
187 udata_printError(ds
, "uprops_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not a Unicode properties file\n",
188 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
189 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
190 pInfo
->formatVersion
[0]);
191 *pErrorCode
=U_UNSUPPORTED_ERROR
;
195 /* the properties file must contain at least the indexes array */
196 if(length
>=0 && (length
-headerSize
)<(int32_t)sizeof(dataIndexes
)) {
197 udata_printError(ds
, "uprops_swap(): too few bytes (%d after header) for a Unicode properties file\n",
199 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
203 /* read the indexes */
204 inData32
=(const int32_t *)((const char *)inData
+headerSize
);
205 for(i
=0; i
<UPROPS_INDEX_COUNT
; ++i
) {
206 dataIndexes
[i
]=udata_readInt32(ds
, inData32
[i
]);
210 * comments are copied from the data format description in genprops/store.c
211 * indexes[] constants are in uprops.h
218 * In formatVersion 7, UPROPS_DATA_TOP_INDEX has the post-header data size.
219 * In earlier formatVersions, it is 0 and a lower dataIndexes entry
220 * has the top of the last item.
222 for(i
=UPROPS_DATA_TOP_INDEX
; i
>0 && (dataTop
=dataIndexes
[i
])==0; --i
) {}
224 if((length
-headerSize
)<(4*dataTop
)) {
225 udata_printError(ds
, "uprops_swap(): too few bytes (%d after header) for a Unicode properties file\n",
227 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
231 outData32
=(int32_t *)((char *)outData
+headerSize
);
233 /* copy everything for inaccessible data (padding) */
234 if(inData32
!=outData32
) {
235 uprv_memcpy(outData32
, inData32
, 4*(size_t)dataTop
);
238 /* swap the indexes[16] */
239 ds
->swapArray32(ds
, inData32
, 4*UPROPS_INDEX_COUNT
, outData32
, pErrorCode
);
242 * swap the main properties UTrie
243 * PT serialized properties trie, see utrie.h (byte size: 4*(i0-16))
245 utrie2_swapAnyVersion(ds
,
246 inData32
+UPROPS_INDEX_COUNT
,
247 4*(dataIndexes
[UPROPS_PROPS32_INDEX
]-UPROPS_INDEX_COUNT
),
248 outData32
+UPROPS_INDEX_COUNT
,
252 * swap the properties and exceptions words
253 * P const uint32_t props32[i1-i0];
254 * E const uint32_t exceptions[i2-i1];
257 inData32
+dataIndexes
[UPROPS_PROPS32_INDEX
],
258 4*(dataIndexes
[UPROPS_EXCEPTIONS_TOP_INDEX
]-dataIndexes
[UPROPS_PROPS32_INDEX
]),
259 outData32
+dataIndexes
[UPROPS_PROPS32_INDEX
],
264 * U const UChar uchars[2*(i3-i2)];
267 inData32
+dataIndexes
[UPROPS_EXCEPTIONS_TOP_INDEX
],
268 4*(dataIndexes
[UPROPS_ADDITIONAL_TRIE_INDEX
]-dataIndexes
[UPROPS_EXCEPTIONS_TOP_INDEX
]),
269 outData32
+dataIndexes
[UPROPS_EXCEPTIONS_TOP_INDEX
],
273 * swap the additional UTrie
274 * i3 additionalTrieIndex; -- 32-bit unit index to the additional trie for more properties
276 utrie2_swapAnyVersion(ds
,
277 inData32
+dataIndexes
[UPROPS_ADDITIONAL_TRIE_INDEX
],
278 4*(dataIndexes
[UPROPS_ADDITIONAL_VECTORS_INDEX
]-dataIndexes
[UPROPS_ADDITIONAL_TRIE_INDEX
]),
279 outData32
+dataIndexes
[UPROPS_ADDITIONAL_TRIE_INDEX
],
283 * swap the properties vectors
284 * PV const uint32_t propsVectors[(i6-i4)/i5][i5]==uint32_t propsVectors[i6-i4];
287 inData32
+dataIndexes
[UPROPS_ADDITIONAL_VECTORS_INDEX
],
288 4*(dataIndexes
[UPROPS_SCRIPT_EXTENSIONS_INDEX
]-dataIndexes
[UPROPS_ADDITIONAL_VECTORS_INDEX
]),
289 outData32
+dataIndexes
[UPROPS_ADDITIONAL_VECTORS_INDEX
],
292 // swap the Script_Extensions data
293 // SCX const uint16_t scriptExtensions[2*(i7-i6)];
295 inData32
+dataIndexes
[UPROPS_SCRIPT_EXTENSIONS_INDEX
],
296 4*(dataIndexes
[UPROPS_RESERVED_INDEX_7
]-dataIndexes
[UPROPS_SCRIPT_EXTENSIONS_INDEX
]),
297 outData32
+dataIndexes
[UPROPS_SCRIPT_EXTENSIONS_INDEX
],
301 /* i7 reservedIndex7; -- 32-bit unit index to the top of the Script_Extensions data */
302 return headerSize
+4*dataIndexes
[UPROPS_RESERVED_INDEX_7
];
305 /* Unicode case mapping data swapping --------------------------------------- */
307 static int32_t U_CALLCONV
308 ucase_swap(const UDataSwapper
*ds
,
309 const void *inData
, int32_t length
, void *outData
,
310 UErrorCode
*pErrorCode
) {
311 const UDataInfo
*pInfo
;
314 const uint8_t *inBytes
;
317 const int32_t *inIndexes
;
320 int32_t i
, offset
, count
, size
;
322 /* udata_swapDataHeader checks the arguments */
323 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
324 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
328 /* check data format and format version */
329 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
331 pInfo
->dataFormat
[0]==UCASE_FMT_0
&& /* dataFormat="cAsE" */
332 pInfo
->dataFormat
[1]==UCASE_FMT_1
&&
333 pInfo
->dataFormat
[2]==UCASE_FMT_2
&&
334 pInfo
->dataFormat
[3]==UCASE_FMT_3
&&
335 ((pInfo
->formatVersion
[0]==1 &&
336 pInfo
->formatVersion
[2]==UTRIE_SHIFT
&&
337 pInfo
->formatVersion
[3]==UTRIE_INDEX_SHIFT
) ||
338 pInfo
->formatVersion
[0]==2 || pInfo
->formatVersion
[0]==3)
340 udata_printError(ds
, "ucase_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as case mapping data\n",
341 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
342 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
343 pInfo
->formatVersion
[0]);
344 *pErrorCode
=U_UNSUPPORTED_ERROR
;
348 inBytes
=(const uint8_t *)inData
+headerSize
;
349 outBytes
=(uint8_t *)outData
+headerSize
;
351 inIndexes
=(const int32_t *)inBytes
;
356 udata_printError(ds
, "ucase_swap(): too few bytes (%d after header) for case mapping data\n",
358 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
363 /* read the first 16 indexes (ICU 3.2/format version 1: UCASE_IX_TOP==16, might grow) */
364 for(i
=0; i
<16; ++i
) {
365 indexes
[i
]=udata_readInt32(ds
, inIndexes
[i
]);
368 /* get the total length of the data */
369 size
=indexes
[UCASE_IX_LENGTH
];
373 udata_printError(ds
, "ucase_swap(): too few bytes (%d after header) for all of case mapping data\n",
375 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
379 /* copy the data for inaccessible bytes */
380 if(inBytes
!=outBytes
) {
381 uprv_memcpy(outBytes
, inBytes
, size
);
386 /* swap the int32_t indexes[] */
387 count
=indexes
[UCASE_IX_INDEX_TOP
]*4;
388 ds
->swapArray32(ds
, inBytes
, count
, outBytes
, pErrorCode
);
392 count
=indexes
[UCASE_IX_TRIE_SIZE
];
393 utrie2_swapAnyVersion(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
396 /* swap the uint16_t exceptions[] and unfold[] */
397 count
=(indexes
[UCASE_IX_EXC_LENGTH
]+indexes
[UCASE_IX_UNFOLD_LENGTH
])*2;
398 ds
->swapArray16(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
401 U_ASSERT(offset
==size
);
404 return headerSize
+size
;
407 /* Unicode bidi/shaping data swapping --------------------------------------- */
409 static int32_t U_CALLCONV
410 ubidi_swap(const UDataSwapper
*ds
,
411 const void *inData
, int32_t length
, void *outData
,
412 UErrorCode
*pErrorCode
) {
413 const UDataInfo
*pInfo
;
416 const uint8_t *inBytes
;
419 const int32_t *inIndexes
;
422 int32_t i
, offset
, count
, size
;
424 /* udata_swapDataHeader checks the arguments */
425 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
426 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
430 /* check data format and format version */
431 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
433 pInfo
->dataFormat
[0]==UBIDI_FMT_0
&& /* dataFormat="BiDi" */
434 pInfo
->dataFormat
[1]==UBIDI_FMT_1
&&
435 pInfo
->dataFormat
[2]==UBIDI_FMT_2
&&
436 pInfo
->dataFormat
[3]==UBIDI_FMT_3
&&
437 ((pInfo
->formatVersion
[0]==1 &&
438 pInfo
->formatVersion
[2]==UTRIE_SHIFT
&&
439 pInfo
->formatVersion
[3]==UTRIE_INDEX_SHIFT
) ||
440 pInfo
->formatVersion
[0]==2)
442 udata_printError(ds
, "ubidi_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as bidi/shaping data\n",
443 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
444 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
445 pInfo
->formatVersion
[0]);
446 *pErrorCode
=U_UNSUPPORTED_ERROR
;
450 inBytes
=(const uint8_t *)inData
+headerSize
;
451 outBytes
=(uint8_t *)outData
+headerSize
;
453 inIndexes
=(const int32_t *)inBytes
;
458 udata_printError(ds
, "ubidi_swap(): too few bytes (%d after header) for bidi/shaping data\n",
460 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
465 /* read the first 16 indexes (ICU 3.4/format version 1: UBIDI_IX_TOP==16, might grow) */
466 for(i
=0; i
<16; ++i
) {
467 indexes
[i
]=udata_readInt32(ds
, inIndexes
[i
]);
470 /* get the total length of the data */
471 size
=indexes
[UBIDI_IX_LENGTH
];
475 udata_printError(ds
, "ubidi_swap(): too few bytes (%d after header) for all of bidi/shaping data\n",
477 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
481 /* copy the data for inaccessible bytes */
482 if(inBytes
!=outBytes
) {
483 uprv_memcpy(outBytes
, inBytes
, size
);
488 /* swap the int32_t indexes[] */
489 count
=indexes
[UBIDI_IX_INDEX_TOP
]*4;
490 ds
->swapArray32(ds
, inBytes
, count
, outBytes
, pErrorCode
);
494 count
=indexes
[UBIDI_IX_TRIE_SIZE
];
495 utrie2_swapAnyVersion(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
498 /* swap the uint32_t mirrors[] */
499 count
=indexes
[UBIDI_IX_MIRROR_LENGTH
]*4;
500 ds
->swapArray32(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
503 /* just skip the uint8_t jgArray[] and jgArray2[] */
504 count
=indexes
[UBIDI_IX_JG_LIMIT
]-indexes
[UBIDI_IX_JG_START
];
506 count
=indexes
[UBIDI_IX_JG_LIMIT2
]-indexes
[UBIDI_IX_JG_START2
];
509 U_ASSERT(offset
==size
);
512 return headerSize
+size
;
515 /* Unicode normalization data swapping -------------------------------------- */
517 #if !UCONFIG_NO_NORMALIZATION
519 static int32_t U_CALLCONV
520 unorm_swap(const UDataSwapper
*ds
,
521 const void *inData
, int32_t length
, void *outData
,
522 UErrorCode
*pErrorCode
) {
523 const UDataInfo
*pInfo
;
526 const uint8_t *inBytes
;
529 const int32_t *inIndexes
;
532 int32_t i
, offset
, count
, size
;
534 /* udata_swapDataHeader checks the arguments */
535 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
536 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
540 /* check data format and format version */
541 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
543 pInfo
->dataFormat
[0]==0x4e && /* dataFormat="Norm" */
544 pInfo
->dataFormat
[1]==0x6f &&
545 pInfo
->dataFormat
[2]==0x72 &&
546 pInfo
->dataFormat
[3]==0x6d &&
547 pInfo
->formatVersion
[0]==2
549 udata_printError(ds
, "unorm_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as unorm.icu\n",
550 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
551 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
552 pInfo
->formatVersion
[0]);
553 *pErrorCode
=U_UNSUPPORTED_ERROR
;
557 inBytes
=(const uint8_t *)inData
+headerSize
;
558 outBytes
=(uint8_t *)outData
+headerSize
;
560 inIndexes
=(const int32_t *)inBytes
;
565 udata_printError(ds
, "unorm_swap(): too few bytes (%d after header) for unorm.icu\n",
567 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
572 /* read the first 32 indexes (ICU 2.8/format version 2.2: _NORM_INDEX_TOP==32, might grow) */
573 for(i
=0; i
<32; ++i
) {
574 indexes
[i
]=udata_readInt32(ds
, inIndexes
[i
]);
577 /* calculate the total length of the data */
579 32*4+ /* size of indexes[] */
580 indexes
[_NORM_INDEX_TRIE_SIZE
]+
581 indexes
[_NORM_INDEX_UCHAR_COUNT
]*2+
582 indexes
[_NORM_INDEX_COMBINE_DATA_COUNT
]*2+
583 indexes
[_NORM_INDEX_FCD_TRIE_SIZE
]+
584 indexes
[_NORM_INDEX_AUX_TRIE_SIZE
]+
585 indexes
[_NORM_INDEX_CANON_SET_COUNT
]*2;
589 udata_printError(ds
, "unorm_swap(): too few bytes (%d after header) for all of unorm.icu\n",
591 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
595 /* copy the data for inaccessible bytes */
596 if(inBytes
!=outBytes
) {
597 uprv_memcpy(outBytes
, inBytes
, size
);
602 /* swap the indexes[] */
604 ds
->swapArray32(ds
, inBytes
, count
, outBytes
, pErrorCode
);
607 /* swap the main UTrie */
608 count
=indexes
[_NORM_INDEX_TRIE_SIZE
];
609 utrie_swap(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
612 /* swap the uint16_t extraData[] and the uint16_t combiningTable[] */
613 count
=(indexes
[_NORM_INDEX_UCHAR_COUNT
]+indexes
[_NORM_INDEX_COMBINE_DATA_COUNT
])*2;
614 ds
->swapArray16(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
617 /* swap the FCD UTrie */
618 count
=indexes
[_NORM_INDEX_FCD_TRIE_SIZE
];
620 utrie_swap(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
624 /* swap the aux UTrie */
625 count
=indexes
[_NORM_INDEX_AUX_TRIE_SIZE
];
627 utrie_swap(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
631 /* swap the uint16_t combiningTable[] */
632 count
=indexes
[_NORM_INDEX_CANON_SET_COUNT
]*2;
633 ds
->swapArray16(ds
, inBytes
+offset
, count
, outBytes
+offset
, pErrorCode
);
637 return headerSize
+size
;
642 /* Swap 'Test' data from gentest */
643 static int32_t U_CALLCONV
644 test_swap(const UDataSwapper
*ds
,
645 const void *inData
, int32_t length
, void *outData
,
646 UErrorCode
*pErrorCode
) {
647 const UDataInfo
*pInfo
;
650 const uint8_t *inBytes
;
655 /* udata_swapDataHeader checks the arguments */
656 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
657 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
658 udata_printError(ds
, "test_swap(): data header swap failed %s\n", pErrorCode
!= NULL
? u_errorName(*pErrorCode
) : "pErrorCode is NULL");
662 /* check data format and format version */
663 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
665 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="Norm" */
666 pInfo
->dataFormat
[1]==0x65 &&
667 pInfo
->dataFormat
[2]==0x73 &&
668 pInfo
->dataFormat
[3]==0x74 &&
669 pInfo
->formatVersion
[0]==1
671 udata_printError(ds
, "test_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as testdata\n",
672 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
673 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
674 pInfo
->formatVersion
[0]);
675 *pErrorCode
=U_UNSUPPORTED_ERROR
;
679 inBytes
=(const uint8_t *)inData
+headerSize
;
680 outBytes
=(uint8_t *)outData
+headerSize
;
682 int32_t size16
= 2; // 16bit plus padding
683 int32_t sizeStr
= 5; // 4 char inv-str plus null
684 int32_t size
= size16
+ sizeStr
;
688 udata_printError(ds
, "test_swap(): too few bytes (%d after header, wanted %d) for all of testdata\n",
690 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
695 /* swap a 1 entry array */
696 ds
->swapArray16(ds
, inBytes
+offset
, size16
, outBytes
+offset
, pErrorCode
);
698 ds
->swapInvChars(ds
, inBytes
+offset
, sizeStr
, outBytes
+offset
, pErrorCode
);
701 return headerSize
+size
;
704 /* swap any data (except a .dat package) ------------------------------------ */
706 static const struct {
707 uint8_t dataFormat
[4];
710 { { 0x52, 0x65, 0x73, 0x42 }, ures_swap
}, /* dataFormat="ResB" */
711 #if !UCONFIG_NO_LEGACY_CONVERSION
712 { { 0x63, 0x6e, 0x76, 0x74 }, ucnv_swap
}, /* dataFormat="cnvt" */
714 #if !UCONFIG_NO_CONVERSION
715 { { 0x43, 0x76, 0x41, 0x6c }, ucnv_swapAliases
}, /* dataFormat="CvAl" */
718 { { 0x53, 0x50, 0x52, 0x50 }, usprep_swap
}, /* dataFormat="SPRP" */
720 /* insert data formats here, descending by expected frequency of occurrence */
721 { { 0x55, 0x50, 0x72, 0x6f }, uprops_swap
}, /* dataFormat="UPro" */
723 { { UCASE_FMT_0
, UCASE_FMT_1
, UCASE_FMT_2
, UCASE_FMT_3
},
724 ucase_swap
}, /* dataFormat="cAsE" */
726 { { UBIDI_FMT_0
, UBIDI_FMT_1
, UBIDI_FMT_2
, UBIDI_FMT_3
},
727 ubidi_swap
}, /* dataFormat="BiDi" */
729 #if !UCONFIG_NO_NORMALIZATION
730 { { 0x4e, 0x6f, 0x72, 0x6d }, unorm_swap
}, /* dataFormat="Norm" */
731 { { 0x4e, 0x72, 0x6d, 0x32 }, unorm2_swap
}, /* dataFormat="Nrm2" */
733 #if !UCONFIG_NO_COLLATION
734 { { 0x55, 0x43, 0x6f, 0x6c }, ucol_swap
}, /* dataFormat="UCol" */
735 { { 0x49, 0x6e, 0x76, 0x43 }, ucol_swapInverseUCA
},/* dataFormat="InvC" */
737 #if !UCONFIG_NO_BREAK_ITERATION
738 { { 0x42, 0x72, 0x6b, 0x20 }, ubrk_swap
}, /* dataFormat="Brk " */
739 { { 0x44, 0x69, 0x63, 0x74 }, udict_swap
}, /* dataFormat="Dict" */
741 { { 0x70, 0x6e, 0x61, 0x6d }, upname_swap
}, /* dataFormat="pnam" */
742 { { 0x75, 0x6e, 0x61, 0x6d }, uchar_swapNames
}, /* dataFormat="unam" */
743 #if !UCONFIG_NO_NORMALIZATION
744 { { 0x43, 0x66, 0x75, 0x20 }, uspoof_swap
}, /* dataFormat="Cfu " */
746 { { 0x54, 0x65, 0x73, 0x74 }, test_swap
} /* dataFormat="Test" */
749 U_CAPI
int32_t U_EXPORT2
750 udata_swap(const UDataSwapper
*ds
,
751 const void *inData
, int32_t length
, void *outData
,
752 UErrorCode
*pErrorCode
) {
753 char dataFormatChars
[4];
754 const UDataInfo
*pInfo
;
755 int32_t i
, swappedLength
;
757 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
762 * Preflight the header first; checks for illegal arguments, too.
763 * Do not swap the header right away because the format-specific swapper
764 * will swap it, get the headerSize again, and also use the header
765 * information. Otherwise we would have to pass some of the information
766 * and not be able to use the UDataSwapFn signature.
768 udata_swapDataHeader(ds
, inData
, -1, NULL
, pErrorCode
);
771 * If we wanted udata_swap() to also handle non-loadable data like a UTrie,
772 * then we could check here for further known magic values and structures.
774 if(U_FAILURE(*pErrorCode
)) {
775 return 0; /* the data format was not recognized */
778 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
781 /* convert the data format from ASCII to Unicode to the system charset */
783 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
784 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3]
787 if(uprv_isInvariantUString(u
, 4)) {
788 u_UCharsToChars(u
, dataFormatChars
, 4);
790 dataFormatChars
[0]=dataFormatChars
[1]=dataFormatChars
[2]=dataFormatChars
[3]='?';
794 /* dispatch to the swap function for the dataFormat */
795 for(i
=0; i
<UPRV_LENGTHOF(swapFns
); ++i
) {
796 if(0==memcmp(swapFns
[i
].dataFormat
, pInfo
->dataFormat
, 4)) {
797 swappedLength
=swapFns
[i
].swapFn(ds
, inData
, length
, outData
, pErrorCode
);
799 if(U_FAILURE(*pErrorCode
)) {
800 udata_printError(ds
, "udata_swap(): failure swapping data format %02x.%02x.%02x.%02x (\"%c%c%c%c\") - %s\n",
801 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
802 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
803 dataFormatChars
[0], dataFormatChars
[1],
804 dataFormatChars
[2], dataFormatChars
[3],
805 u_errorName(*pErrorCode
));
806 } else if(swappedLength
<(length
-15)) {
807 /* swapped less than expected */
808 udata_printError(ds
, "udata_swap() warning: swapped only %d out of %d bytes - data format %02x.%02x.%02x.%02x (\"%c%c%c%c\")\n",
809 swappedLength
, length
,
810 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
811 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
812 dataFormatChars
[0], dataFormatChars
[1],
813 dataFormatChars
[2], dataFormatChars
[3],
814 u_errorName(*pErrorCode
));
817 return swappedLength
;
821 /* the dataFormat was not recognized */
822 udata_printError(ds
, "udata_swap(): unknown data format %02x.%02x.%02x.%02x (\"%c%c%c%c\")\n",
823 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
824 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
825 dataFormatChars
[0], dataFormatChars
[1],
826 dataFormatChars
[2], dataFormatChars
[3]);
828 *pErrorCode
=U_UNSUPPORTED_ERROR
;