scuffed-code/icu4c/source/common/ucol_swp.cpp
Markus Scherer fe3eb3ed5c
ICU-13530 add UCPTrie/CodePointTrie, switch normalization to use it (#48)
* ICU-13530 copy C/C++ files UTrie2 -> UTrie3

X-SVN-Rev: 40754

* ICU-13530 UTrie3 new files copied from UTrie2: rename types/functions/macros

X-SVN-Rev: 40755

* ICU-13530 debug-print building each UTrie2

X-SVN-Rev: 40756

* ICU-13530 remove two-byte-UTF-8 errorValue block; move highValue from end of data array into header; add errorValue to header

X-SVN-Rev: 40762

* ICU-13530 UTrie3 U16_NEXT/PREV: errorValue for unpaired surrogates

X-SVN-Rev: 40763

* ICU-13530 no more separate values for lead surrogate code units

X-SVN-Rev: 40764

* ICU-13530 change from 11:5 trie bits to 10:6 for simpler UTF-8 code

X-SVN-Rev: 40766

* ICU-13530 UTrie2 build UTrie3 as well, print sizes

X-SVN-Rev: 40767

* ICU-13530 debug-print countSame, sumOverlaps, countInitial

X-SVN-Rev: 40768

* ICU-13530 debug-print whether trie is for CanonIterData

X-SVN-Rev: 40769

* ICU-13530 no index-shift for BMP data, no separate index-2 for 2-byte UTF-8; builder changes incomplete

X-SVN-Rev: 40777

* ICU-13530 remove errorValue and highStart from UNewTrie3

X-SVN-Rev: 40778

* ICU-13530 rewrite UTrie3 builder code

X-SVN-Rev: 40783

* ICU-13530 UTrie3 bug fixes

X-SVN-Rev: 40788

* ICU-13530 fully re-inline _UTRIE3_U8_NEXT()

X-SVN-Rev: 40790

* ICU-13530 find most common all-same data block for dataNullBlock and initialValue

X-SVN-Rev: 40792

* ICU-13530 UTrie3 iterator functions take start and return the end of a range, rather than callback call for each range

X-SVN-Rev: 40800

* ICU-13530 mask off unused data value bits before building a UTrie3 with values less than 32 bits wide

X-SVN-Rev: 40803

* ICU-13530 split utrie3builder.h out of utrie3.h

X-SVN-Rev: 40804

* ICU-13530 separate types UTrie3 vs. UTrie3Builder, implement builder as wrapper over C++ class Trie3Builder in .cpp

X-SVN-Rev: 40809

* ICU-13530 function to make a UTrie3Builder from a UTrie3

X-SVN-Rev: 40810

* ICU-13530 debug-print some data; some cleanup

X-SVN-Rev: 40865

* ICU-13530 BMP 10:6 but supplementary 10:6:4

X-SVN-Rev: 40984

* ICU-13530 move errorValue & highValue to the end of the data table, minimal padding to 4 bytes

X-SVN-Rev: 41011

* ICU-13530 index-1 table gap of index-2 null blocks

X-SVN-Rev: 41018

* ICU-13530 test with more than 128k compacted data

X-SVN-Rev: 41034

* ICU-13530 supplementary bits 11:5:4 saves a little space

X-SVN-Rev: 41039

* ICU-13530 supplementary bits 6:5:5:4 instead of gap: about same size but simpler

X-SVN-Rev: 41050

* ICU-13530 remove unnecessary utrie3_clone(built trie)

X-SVN-Rev: 41058

* ICU-13530 remove unnecessary UTrie3StringIterator

X-SVN-Rev: 41059

* ICU-13530 back to UTRIE3_GET...() macros *returning* data values

X-SVN-Rev: 41060

* ICU-13530 fast vs. small

X-SVN-Rev: 41066

* ICU-13530 always load NFC data, add simple normalization performance test

X-SVN-Rev: 41110

* ICU-13530 change normalization main trie to UTrie3 with special values for lead surrogates; forbid non-inert surrogate code *points* because unable to store values different from code *units*; runtime code work around that for code point lookup and iteration; adjust UTS 46 for normalization no longer mapping unpaired surrogates to U+FFFD

X-SVN-Rev: 41122

* ICU-13530 simplenormperf bug fix and NFC base line

X-SVN-Rev: 41126

* ICU-13530 move normalization getRange skipping lead surrogates to API getRangeSkipLead()

X-SVN-Rev: 41182

* ICU-13530 switch CanonIterData and gennorm2 Norms to UTrie3

X-SVN-Rev: 41183

* ICU-13530 remove unused overwrite parameter from setRange()

X-SVN-Rev: 41184

* ICU-13530 getRange skip lead -> fixed surrogates

X-SVN-Rev: 41219

* ICU-13530 minor cleanup

X-SVN-Rev: 41221

* ICU-13530 UTS 46 code map unpaired surrogates to U+FFFD before normalization

X-SVN-Rev: 41224

* ICU-13530 minor internal-docs cleanup

X-SVN-Rev: 41225

* ICU-13530 rename UTrie3 to UCPTrie, and other name changes

X-SVN-Rev: 41226

* ICU-13530 add 8-bit data option; add type-any & valueBits-any for fromBinary(); macros consistently source type then data width

X-SVN-Rev: 41234

* ICU-13530 scrub the API docs for the proposal

X-SVN-Rev: 41319

* ICU-13530 tag internal definitions as such, or move them to an internal header

X-SVN-Rev: 41320

* ICU-13530 Java API skeleton

X-SVN-Rev: 41326

* ICU-13530 API feedback: ValueWidth, MutableCodePointTrie, base CodePointMap, ...

X-SVN-Rev: 41382

* ICU-13530 add UCPTrie valueWidth field and padding, and combine data pointers into a union

X-SVN-Rev: 41408

* ICU-13530 switch some macros to using dataAccess parameter: separate index vs. data lookups, no macro variant for each value width

X-SVN-Rev: 41409

* ICU-13530 StringIterator is no longer a java.util.Iterator (bad fit)

X-SVN-Rev: 41455

* ICU-13530 CodePointTrie.java code complete

X-SVN-Rev: 41518

* ICU-13530 finish Java port incl test; keep C++ parallel

* ICU-13530 adjust API for feedback: rename HandleValue to FilterValue, change getRange+getRangeFixedSurr(bool allSurr) to enum RangeOption+getRange(enum option); change remaining C macros to use dataAccess for 16/32/8-bit value widths; fix/clarify some API docs

* ICU-13530 add javadoc

* ICU-13530 document UCPTrie binary data format

* ICU-13530 update .nrm formatVersion 3->4, document change in surrogate handling with new trie

* ICU-13530 re-hardcode NFC data

* move trie swapper code into new file; add new files to Windows project files; turn off trie debugging

* ICU-13530 minor cleanup

* ICU-13530 test more range starts; fix a C test leak

* ICU-13530 regenerate Java data from scratch

* ICU-13530 review feedback changes: API docs typos, more @internal, C++11 field initializers, fix potential leak in MutableCodePointTrie::fromUCPTrie()

* ICU-13530 rename interface FilterValue to ValueFilter
2018-09-27 14:27:38 -07:00

616 lines
23 KiB
C++

// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2003-2015, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: ucol_swp.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2003sep10
* created by: Markus W. Scherer
*
* Swap collation binaries.
*/
#include "unicode/udata.h" /* UDataInfo */
#include "utrie.h"
#include "utrie2.h"
#include "udataswp.h"
#include "cmemory.h"
#include "ucol_data.h"
#include "ucol_swp.h"
/* swapping ----------------------------------------------------------------- */
#if !UCONFIG_NO_COLLATION
U_CAPI UBool U_EXPORT2
ucol_looksLikeCollationBinary(const UDataSwapper *ds,
const void *inData, int32_t length) {
if(ds==NULL || inData==NULL || length<-1) {
return FALSE;
}
// First check for format version 4+ which has a standard data header.
UErrorCode errorCode=U_ZERO_ERROR;
(void)udata_swapDataHeader(ds, inData, -1, NULL, &errorCode);
if(U_SUCCESS(errorCode)) {
const UDataInfo &info=*(const UDataInfo *)((const char *)inData+4);
if(info.dataFormat[0]==0x55 && // dataFormat="UCol"
info.dataFormat[1]==0x43 &&
info.dataFormat[2]==0x6f &&
info.dataFormat[3]==0x6c) {
return TRUE;
}
}
// Else check for format version 3.
const UCATableHeader *inHeader=(const UCATableHeader *)inData;
/*
* The collation binary must contain at least the UCATableHeader,
* starting with its size field.
* sizeof(UCATableHeader)==42*4 in ICU 2.8
* check the length against the header size before reading the size field
*/
UCATableHeader header;
uprv_memset(&header, 0, sizeof(header));
if(length<0) {
header.size=udata_readInt32(ds, inHeader->size);
} else if((length<(42*4) || length<(header.size=udata_readInt32(ds, inHeader->size)))) {
return FALSE;
}
header.magic=ds->readUInt32(inHeader->magic);
if(!(
header.magic==UCOL_HEADER_MAGIC &&
inHeader->formatVersion[0]==3 /*&&
inHeader->formatVersion[1]>=0*/
)) {
return FALSE;
}
if(inHeader->isBigEndian!=ds->inIsBigEndian || inHeader->charSetFamily!=ds->inCharset) {
return FALSE;
}
return TRUE;
}
namespace {
/* swap a header-less collation formatVersion=3 binary, inside a resource bundle or ucadata.icu */
int32_t
swapFormatVersion3(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const uint8_t *inBytes;
uint8_t *outBytes;
const UCATableHeader *inHeader;
UCATableHeader *outHeader;
UCATableHeader header;
uint32_t count;
/* argument checking in case we were not called from ucol_swap() */
if(U_FAILURE(*pErrorCode)) {
return 0;
}
if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
inBytes=(const uint8_t *)inData;
outBytes=(uint8_t *)outData;
inHeader=(const UCATableHeader *)inData;
outHeader=(UCATableHeader *)outData;
/*
* The collation binary must contain at least the UCATableHeader,
* starting with its size field.
* sizeof(UCATableHeader)==42*4 in ICU 2.8
* check the length against the header size before reading the size field
*/
uprv_memset(&header, 0, sizeof(header));
if(length<0) {
header.size=udata_readInt32(ds, inHeader->size);
} else if((length<(42*4) || length<(header.size=udata_readInt32(ds, inHeader->size)))) {
udata_printError(ds, "ucol_swap(formatVersion=3): too few bytes (%d after header) for collation data\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
header.magic=ds->readUInt32(inHeader->magic);
if(!(
header.magic==UCOL_HEADER_MAGIC &&
inHeader->formatVersion[0]==3 /*&&
inHeader->formatVersion[1]>=0*/
)) {
udata_printError(ds, "ucol_swap(formatVersion=3): magic 0x%08x or format version %02x.%02x is not a collation binary\n",
header.magic,
inHeader->formatVersion[0], inHeader->formatVersion[1]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
if(inHeader->isBigEndian!=ds->inIsBigEndian || inHeader->charSetFamily!=ds->inCharset) {
udata_printError(ds, "ucol_swap(formatVersion=3): endianness %d or charset %d does not match the swapper\n",
inHeader->isBigEndian, inHeader->charSetFamily);
*pErrorCode=U_INVALID_FORMAT_ERROR;
return 0;
}
if(length>=0) {
/* copy everything, takes care of data that needs no swapping */
if(inBytes!=outBytes) {
uprv_memcpy(outBytes, inBytes, header.size);
}
/* swap the necessary pieces in the order of their occurrence in the data */
/* read more of the UCATableHeader (the size field was read above) */
header.options= ds->readUInt32(inHeader->options);
header.UCAConsts= ds->readUInt32(inHeader->UCAConsts);
header.contractionUCACombos= ds->readUInt32(inHeader->contractionUCACombos);
header.mappingPosition= ds->readUInt32(inHeader->mappingPosition);
header.expansion= ds->readUInt32(inHeader->expansion);
header.contractionIndex= ds->readUInt32(inHeader->contractionIndex);
header.contractionCEs= ds->readUInt32(inHeader->contractionCEs);
header.contractionSize= ds->readUInt32(inHeader->contractionSize);
header.endExpansionCE= ds->readUInt32(inHeader->endExpansionCE);
header.expansionCESize= ds->readUInt32(inHeader->expansionCESize);
header.endExpansionCECount= udata_readInt32(ds, inHeader->endExpansionCECount);
header.contractionUCACombosSize=udata_readInt32(ds, inHeader->contractionUCACombosSize);
header.scriptToLeadByte= ds->readUInt32(inHeader->scriptToLeadByte);
header.leadByteToScript= ds->readUInt32(inHeader->leadByteToScript);
/* swap the 32-bit integers in the header */
ds->swapArray32(ds, inHeader, (int32_t)((const char *)&inHeader->jamoSpecial-(const char *)inHeader),
outHeader, pErrorCode);
ds->swapArray32(ds, &(inHeader->scriptToLeadByte), sizeof(header.scriptToLeadByte) + sizeof(header.leadByteToScript),
&(outHeader->scriptToLeadByte), pErrorCode);
/* set the output platform properties */
outHeader->isBigEndian=ds->outIsBigEndian;
outHeader->charSetFamily=ds->outCharset;
/* swap the options */
if(header.options!=0) {
ds->swapArray32(ds, inBytes+header.options, header.expansion-header.options,
outBytes+header.options, pErrorCode);
}
/* swap the expansions */
if(header.mappingPosition!=0 && header.expansion!=0) {
if(header.contractionIndex!=0) {
/* expansions bounded by contractions */
count=header.contractionIndex-header.expansion;
} else {
/* no contractions: expansions bounded by the main trie */
count=header.mappingPosition-header.expansion;
}
ds->swapArray32(ds, inBytes+header.expansion, (int32_t)count,
outBytes+header.expansion, pErrorCode);
}
/* swap the contractions */
if(header.contractionSize!=0) {
/* contractionIndex: UChar[] */
ds->swapArray16(ds, inBytes+header.contractionIndex, header.contractionSize*2,
outBytes+header.contractionIndex, pErrorCode);
/* contractionCEs: CEs[] */
ds->swapArray32(ds, inBytes+header.contractionCEs, header.contractionSize*4,
outBytes+header.contractionCEs, pErrorCode);
}
/* swap the main trie */
if(header.mappingPosition!=0) {
count=header.endExpansionCE-header.mappingPosition;
utrie_swap(ds, inBytes+header.mappingPosition, (int32_t)count,
outBytes+header.mappingPosition, pErrorCode);
}
/* swap the max expansion table */
if(header.endExpansionCECount!=0) {
ds->swapArray32(ds, inBytes+header.endExpansionCE, header.endExpansionCECount*4,
outBytes+header.endExpansionCE, pErrorCode);
}
/* expansionCESize, unsafeCP, contrEndCP: uint8_t[], no need to swap */
/* swap UCA constants */
if(header.UCAConsts!=0) {
/*
* if UCAConsts!=0 then contractionUCACombos because we are swapping
* the UCA data file, and we know that the UCA contains contractions
*/
ds->swapArray32(ds, inBytes+header.UCAConsts, header.contractionUCACombos-header.UCAConsts,
outBytes+header.UCAConsts, pErrorCode);
}
/* swap UCA contractions */
if(header.contractionUCACombosSize!=0) {
count=header.contractionUCACombosSize*inHeader->contractionUCACombosWidth*U_SIZEOF_UCHAR;
ds->swapArray16(ds, inBytes+header.contractionUCACombos, (int32_t)count,
outBytes+header.contractionUCACombos, pErrorCode);
}
/* swap the script to lead bytes */
if(header.scriptToLeadByte!=0) {
int indexCount = ds->readUInt16(*((uint16_t*)(inBytes+header.scriptToLeadByte))); // each entry = 2 * uint16
int dataCount = ds->readUInt16(*((uint16_t*)(inBytes+header.scriptToLeadByte + 2))); // each entry = uint16
ds->swapArray16(ds, inBytes+header.scriptToLeadByte,
4 + (4 * indexCount) + (2 * dataCount),
outBytes+header.scriptToLeadByte, pErrorCode);
}
/* swap the lead byte to scripts */
if(header.leadByteToScript!=0) {
int indexCount = ds->readUInt16(*((uint16_t*)(inBytes+header.leadByteToScript))); // each entry = uint16
int dataCount = ds->readUInt16(*((uint16_t*)(inBytes+header.leadByteToScript + 2))); // each entry = uint16
ds->swapArray16(ds, inBytes+header.leadByteToScript,
4 + (2 * indexCount) + (2 * dataCount),
outBytes+header.leadByteToScript, pErrorCode);
}
}
return header.size;
}
// swap formatVersion 4 or 5 ----------------------------------------------- ***
// The following are copied from CollationDataReader, trading an awkward copy of constants
// for an awkward relocation of the i18n collationdatareader.h file into the common library.
// Keep them in sync!
enum {
IX_INDEXES_LENGTH, // 0
IX_OPTIONS,
IX_RESERVED2,
IX_RESERVED3,
IX_JAMO_CE32S_START, // 4
IX_REORDER_CODES_OFFSET,
IX_REORDER_TABLE_OFFSET,
IX_TRIE_OFFSET,
IX_RESERVED8_OFFSET, // 8
IX_CES_OFFSET,
IX_RESERVED10_OFFSET,
IX_CE32S_OFFSET,
IX_ROOT_ELEMENTS_OFFSET, // 12
IX_CONTEXTS_OFFSET,
IX_UNSAFE_BWD_OFFSET,
IX_FAST_LATIN_TABLE_OFFSET,
IX_SCRIPTS_OFFSET, // 16
IX_COMPRESSIBLE_BYTES_OFFSET,
IX_RESERVED18_OFFSET,
IX_TOTAL_SIZE
};
int32_t
swapFormatVersion4(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode &errorCode) {
if(U_FAILURE(errorCode)) { return 0; }
const uint8_t *inBytes=(const uint8_t *)inData;
uint8_t *outBytes=(uint8_t *)outData;
const int32_t *inIndexes=(const int32_t *)inBytes;
int32_t indexes[IX_TOTAL_SIZE+1];
// Need at least IX_INDEXES_LENGTH and IX_OPTIONS.
if(0<=length && length<8) {
udata_printError(ds, "ucol_swap(formatVersion=4): too few bytes "
"(%d after header) for collation data\n",
length);
errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
int32_t indexesLength=indexes[0]=udata_readInt32(ds, inIndexes[0]);
if(0<=length && length<(indexesLength*4)) {
udata_printError(ds, "ucol_swap(formatVersion=4): too few bytes "
"(%d after header) for collation data\n",
length);
errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
for(int32_t i=1; i<=IX_TOTAL_SIZE && i<indexesLength; ++i) {
indexes[i]=udata_readInt32(ds, inIndexes[i]);
}
for(int32_t i=indexesLength; i<=IX_TOTAL_SIZE; ++i) {
indexes[i]=-1;
}
inIndexes=NULL; // Make sure we do not accidentally use these instead of indexes[].
// Get the total length of the data.
int32_t size;
if(indexesLength>IX_TOTAL_SIZE) {
size=indexes[IX_TOTAL_SIZE];
} else if(indexesLength>IX_REORDER_CODES_OFFSET) {
size=indexes[indexesLength-1];
} else {
size=indexesLength*4;
}
if(length<0) { return size; }
if(length<size) {
udata_printError(ds, "ucol_swap(formatVersion=4): too few bytes "
"(%d after header) for collation data\n",
length);
errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
// Copy the data for inaccessible bytes and arrays of bytes.
if(inBytes!=outBytes) {
uprv_memcpy(outBytes, inBytes, size);
}
// Swap the int32_t indexes[].
ds->swapArray32(ds, inBytes, indexesLength * 4, outBytes, &errorCode);
// The following is a modified version of CollationDataReader::read().
// Here we use indexes[] not inIndexes[] because
// the inIndexes[] may not be in this machine's endianness.
int32_t index; // one of the indexes[] slots
int32_t offset; // byte offset for the index part
// int32_t length; // number of bytes in the index part
index = IX_REORDER_CODES_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray32(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
// Skip the IX_REORDER_TABLE_OFFSET byte array.
index = IX_TRIE_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
utrie2_swap(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
index = IX_RESERVED8_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
udata_printError(ds, "ucol_swap(formatVersion=4): unknown data at IX_RESERVED8_OFFSET\n", length);
errorCode = U_UNSUPPORTED_ERROR;
return 0;
}
index = IX_CES_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray64(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
index = IX_RESERVED10_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
udata_printError(ds, "ucol_swap(formatVersion=4): unknown data at IX_RESERVED10_OFFSET\n", length);
errorCode = U_UNSUPPORTED_ERROR;
return 0;
}
index = IX_CE32S_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray32(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
index = IX_ROOT_ELEMENTS_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray32(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
index = IX_CONTEXTS_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
index = IX_UNSAFE_BWD_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
index = IX_FAST_LATIN_TABLE_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
index = IX_SCRIPTS_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
}
// Skip the IX_COMPRESSIBLE_BYTES_OFFSET byte array.
index = IX_RESERVED18_OFFSET;
offset = indexes[index];
length = indexes[index + 1] - offset;
if(length > 0) {
udata_printError(ds, "ucol_swap(formatVersion=4): unknown data at IX_RESERVED18_OFFSET\n", length);
errorCode = U_UNSUPPORTED_ERROR;
return 0;
}
return size;
}
} // namespace
/* swap ICU collation data like ucadata.icu */
U_CAPI int32_t U_EXPORT2
ucol_swap(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
if(U_FAILURE(*pErrorCode)) { return 0; }
/* udata_swapDataHeader checks the arguments */
int32_t headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(U_FAILURE(*pErrorCode)) {
// Try to swap the old format version which did not have a standard data header.
*pErrorCode=U_ZERO_ERROR;
return swapFormatVersion3(ds, inData, length, outData, pErrorCode);
}
/* check data format and format version */
const UDataInfo &info=*(const UDataInfo *)((const char *)inData+4);
if(!(
info.dataFormat[0]==0x55 && // dataFormat="UCol"
info.dataFormat[1]==0x43 &&
info.dataFormat[2]==0x6f &&
info.dataFormat[3]==0x6c &&
(3<=info.formatVersion[0] && info.formatVersion[0]<=5)
)) {
udata_printError(ds, "ucol_swap(): data format %02x.%02x.%02x.%02x "
"(format version %02x.%02x) is not recognized as collation data\n",
info.dataFormat[0], info.dataFormat[1],
info.dataFormat[2], info.dataFormat[3],
info.formatVersion[0], info.formatVersion[1]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
inData=(const char *)inData+headerSize;
if(length>=0) { length-=headerSize; }
outData=(char *)outData+headerSize;
int32_t collationSize;
if(info.formatVersion[0]>=4) {
collationSize=swapFormatVersion4(ds, inData, length, outData, *pErrorCode);
} else {
collationSize=swapFormatVersion3(ds, inData, length, outData, pErrorCode);
}
if(U_SUCCESS(*pErrorCode)) {
return headerSize+collationSize;
} else {
return 0;
}
}
/* swap inverse UCA collation data (invuca.icu) */
U_CAPI int32_t U_EXPORT2
ucol_swapInverseUCA(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 InverseUCATableHeader *inHeader;
InverseUCATableHeader *outHeader;
InverseUCATableHeader header={ 0,0,0,0,0,{0,0,0,0},{0,0,0,0,0,0,0,0} };
/* udata_swapDataHeader checks the arguments */
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
/* check data format and format version */
pInfo=(const UDataInfo *)((const char *)inData+4);
if(!(
pInfo->dataFormat[0]==0x49 && /* dataFormat="InvC" */
pInfo->dataFormat[1]==0x6e &&
pInfo->dataFormat[2]==0x76 &&
pInfo->dataFormat[3]==0x43 &&
pInfo->formatVersion[0]==2 &&
pInfo->formatVersion[1]>=1
)) {
udata_printError(ds, "ucol_swapInverseUCA(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not an inverse UCA collation file\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0], pInfo->formatVersion[1]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
inBytes=(const uint8_t *)inData+headerSize;
outBytes=(uint8_t *)outData+headerSize;
inHeader=(const InverseUCATableHeader *)inBytes;
outHeader=(InverseUCATableHeader *)outBytes;
/*
* The inverse UCA collation binary must contain at least the InverseUCATableHeader,
* starting with its size field.
* sizeof(UCATableHeader)==8*4 in ICU 2.8
* check the length against the header size before reading the size field
*/
if(length<0) {
header.byteSize=udata_readInt32(ds, inHeader->byteSize);
} else if(
((length-headerSize)<(8*4) ||
(uint32_t)(length-headerSize)<(header.byteSize=udata_readInt32(ds, inHeader->byteSize)))
) {
udata_printError(ds, "ucol_swapInverseUCA(): too few bytes (%d after header) for inverse UCA collation data\n",
length);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
if(length>=0) {
/* copy everything, takes care of data that needs no swapping */
if(inBytes!=outBytes) {
uprv_memcpy(outBytes, inBytes, header.byteSize);
}
/* swap the necessary pieces in the order of their occurrence in the data */
/* read more of the InverseUCATableHeader (the byteSize field was read above) */
header.tableSize= ds->readUInt32(inHeader->tableSize);
header.contsSize= ds->readUInt32(inHeader->contsSize);
header.table= ds->readUInt32(inHeader->table);
header.conts= ds->readUInt32(inHeader->conts);
/* swap the 32-bit integers in the header */
ds->swapArray32(ds, inHeader, 5*4, outHeader, pErrorCode);
/* swap the inverse table; tableSize counts uint32_t[3] rows */
ds->swapArray32(ds, inBytes+header.table, header.tableSize*3*4,
outBytes+header.table, pErrorCode);
/* swap the continuation table; contsSize counts UChars */
ds->swapArray16(ds, inBytes+header.conts, header.contsSize*U_SIZEOF_UCHAR,
outBytes+header.conts, pErrorCode);
}
return headerSize+header.byteSize;
}
#endif /* #if !UCONFIG_NO_COLLATION */