-/* swap the data ------------------------------------------------------------ */
-
-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" */
- { { 0x43, 0x76, 0x41, 0x6c }, ucnv_swapAliases }, /* dataFormat="CvAl" */
-#endif
- { { 0x43, 0x6d, 0x6e, 0x44 }, udata_swapPackage }, /* dataFormat="CmnD" */
-#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" */
-
-#if !UCONFIG_NO_NORMALIZATION
- { { 0x4e, 0x6f, 0x72, 0x6d }, unorm_swap }, /* dataFormat="Norm" */
-#endif
-#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 " */
-#endif
- { { 0x70, 0x6e, 0x61, 0x6d }, upname_swap }, /* dataFormat="pnam" */
- { { 0x75, 0x6e, 0x61, 0x6d }, uchar_swapNames } /* dataFormat="unam" */
-};
-
-static int32_t
-udata_swap(const UDataSwapper *ds,
- const void *inData, int32_t length, void *outData,
- UErrorCode *pErrorCode) {
- char dataFormatChars[4];
- const UDataInfo *pInfo;
- int32_t headerSize, i, swappedLength;
-
- if(pErrorCode==NULL || 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.
- */
- headerSize=udata_swapDataHeader(ds, inData, -1, NULL, 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 */
- UChar 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<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;
-}
-