1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
5*0e209d39SAndroid Build Coastguard Worker *
6*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2003-2015, International Business Machines
7*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved.
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
10*0e209d39SAndroid Build Coastguard Worker * file name: ucol_swp.cpp
11*0e209d39SAndroid Build Coastguard Worker * encoding: UTF-8
12*0e209d39SAndroid Build Coastguard Worker * tab size: 8 (not used)
13*0e209d39SAndroid Build Coastguard Worker * indentation:4
14*0e209d39SAndroid Build Coastguard Worker *
15*0e209d39SAndroid Build Coastguard Worker * created on: 2003sep10
16*0e209d39SAndroid Build Coastguard Worker * created by: Markus W. Scherer
17*0e209d39SAndroid Build Coastguard Worker *
18*0e209d39SAndroid Build Coastguard Worker * Swap collation binaries.
19*0e209d39SAndroid Build Coastguard Worker */
20*0e209d39SAndroid Build Coastguard Worker
21*0e209d39SAndroid Build Coastguard Worker #include "unicode/udata.h" /* UDataInfo */
22*0e209d39SAndroid Build Coastguard Worker #include "utrie.h"
23*0e209d39SAndroid Build Coastguard Worker #include "utrie2.h"
24*0e209d39SAndroid Build Coastguard Worker #include "udataswp.h"
25*0e209d39SAndroid Build Coastguard Worker #include "cmemory.h"
26*0e209d39SAndroid Build Coastguard Worker #include "ucol_data.h"
27*0e209d39SAndroid Build Coastguard Worker #include "ucol_swp.h"
28*0e209d39SAndroid Build Coastguard Worker
29*0e209d39SAndroid Build Coastguard Worker /* swapping ----------------------------------------------------------------- */
30*0e209d39SAndroid Build Coastguard Worker
31*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_COLLATION
32*0e209d39SAndroid Build Coastguard Worker
33*0e209d39SAndroid Build Coastguard Worker U_CAPI UBool U_EXPORT2
ucol_looksLikeCollationBinary(const UDataSwapper * ds,const void * inData,int32_t length)34*0e209d39SAndroid Build Coastguard Worker ucol_looksLikeCollationBinary(const UDataSwapper *ds,
35*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length) {
36*0e209d39SAndroid Build Coastguard Worker if(ds==nullptr || inData==nullptr || length<-1) {
37*0e209d39SAndroid Build Coastguard Worker return false;
38*0e209d39SAndroid Build Coastguard Worker }
39*0e209d39SAndroid Build Coastguard Worker
40*0e209d39SAndroid Build Coastguard Worker // First check for format version 4+ which has a standard data header.
41*0e209d39SAndroid Build Coastguard Worker UErrorCode errorCode=U_ZERO_ERROR;
42*0e209d39SAndroid Build Coastguard Worker (void)udata_swapDataHeader(ds, inData, -1, nullptr, &errorCode);
43*0e209d39SAndroid Build Coastguard Worker if(U_SUCCESS(errorCode)) {
44*0e209d39SAndroid Build Coastguard Worker const UDataInfo &info=*(const UDataInfo *)((const char *)inData+4);
45*0e209d39SAndroid Build Coastguard Worker if(info.dataFormat[0]==0x55 && // dataFormat="UCol"
46*0e209d39SAndroid Build Coastguard Worker info.dataFormat[1]==0x43 &&
47*0e209d39SAndroid Build Coastguard Worker info.dataFormat[2]==0x6f &&
48*0e209d39SAndroid Build Coastguard Worker info.dataFormat[3]==0x6c) {
49*0e209d39SAndroid Build Coastguard Worker return true;
50*0e209d39SAndroid Build Coastguard Worker }
51*0e209d39SAndroid Build Coastguard Worker }
52*0e209d39SAndroid Build Coastguard Worker
53*0e209d39SAndroid Build Coastguard Worker // Else check for format version 3.
54*0e209d39SAndroid Build Coastguard Worker const UCATableHeader *inHeader=(const UCATableHeader *)inData;
55*0e209d39SAndroid Build Coastguard Worker
56*0e209d39SAndroid Build Coastguard Worker /*
57*0e209d39SAndroid Build Coastguard Worker * The collation binary must contain at least the UCATableHeader,
58*0e209d39SAndroid Build Coastguard Worker * starting with its size field.
59*0e209d39SAndroid Build Coastguard Worker * sizeof(UCATableHeader)==42*4 in ICU 2.8
60*0e209d39SAndroid Build Coastguard Worker * check the length against the header size before reading the size field
61*0e209d39SAndroid Build Coastguard Worker */
62*0e209d39SAndroid Build Coastguard Worker UCATableHeader header;
63*0e209d39SAndroid Build Coastguard Worker uprv_memset(&header, 0, sizeof(header));
64*0e209d39SAndroid Build Coastguard Worker if(length<0) {
65*0e209d39SAndroid Build Coastguard Worker header.size=udata_readInt32(ds, inHeader->size);
66*0e209d39SAndroid Build Coastguard Worker } else if((length<(42*4) || length<(header.size=udata_readInt32(ds, inHeader->size)))) {
67*0e209d39SAndroid Build Coastguard Worker return false;
68*0e209d39SAndroid Build Coastguard Worker }
69*0e209d39SAndroid Build Coastguard Worker
70*0e209d39SAndroid Build Coastguard Worker header.magic=ds->readUInt32(inHeader->magic);
71*0e209d39SAndroid Build Coastguard Worker if(!(
72*0e209d39SAndroid Build Coastguard Worker header.magic==UCOL_HEADER_MAGIC &&
73*0e209d39SAndroid Build Coastguard Worker inHeader->formatVersion[0]==3 /*&&
74*0e209d39SAndroid Build Coastguard Worker inHeader->formatVersion[1]>=0*/
75*0e209d39SAndroid Build Coastguard Worker )) {
76*0e209d39SAndroid Build Coastguard Worker return false;
77*0e209d39SAndroid Build Coastguard Worker }
78*0e209d39SAndroid Build Coastguard Worker
79*0e209d39SAndroid Build Coastguard Worker if(inHeader->isBigEndian!=ds->inIsBigEndian || inHeader->charSetFamily!=ds->inCharset) {
80*0e209d39SAndroid Build Coastguard Worker return false;
81*0e209d39SAndroid Build Coastguard Worker }
82*0e209d39SAndroid Build Coastguard Worker
83*0e209d39SAndroid Build Coastguard Worker return true;
84*0e209d39SAndroid Build Coastguard Worker }
85*0e209d39SAndroid Build Coastguard Worker
86*0e209d39SAndroid Build Coastguard Worker namespace {
87*0e209d39SAndroid Build Coastguard Worker
88*0e209d39SAndroid Build Coastguard Worker /* swap a header-less collation formatVersion=3 binary, inside a resource bundle or ucadata.icu */
89*0e209d39SAndroid Build Coastguard Worker int32_t
swapFormatVersion3(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * pErrorCode)90*0e209d39SAndroid Build Coastguard Worker swapFormatVersion3(const UDataSwapper *ds,
91*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
92*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
93*0e209d39SAndroid Build Coastguard Worker const uint8_t *inBytes;
94*0e209d39SAndroid Build Coastguard Worker uint8_t *outBytes;
95*0e209d39SAndroid Build Coastguard Worker
96*0e209d39SAndroid Build Coastguard Worker const UCATableHeader *inHeader;
97*0e209d39SAndroid Build Coastguard Worker UCATableHeader *outHeader;
98*0e209d39SAndroid Build Coastguard Worker UCATableHeader header;
99*0e209d39SAndroid Build Coastguard Worker
100*0e209d39SAndroid Build Coastguard Worker uint32_t count;
101*0e209d39SAndroid Build Coastguard Worker
102*0e209d39SAndroid Build Coastguard Worker /* argument checking in case we were not called from ucol_swap() */
103*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) {
104*0e209d39SAndroid Build Coastguard Worker return 0;
105*0e209d39SAndroid Build Coastguard Worker }
106*0e209d39SAndroid Build Coastguard Worker if(ds==nullptr || inData==nullptr || length<-1 || (length>0 && outData==nullptr)) {
107*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
108*0e209d39SAndroid Build Coastguard Worker return 0;
109*0e209d39SAndroid Build Coastguard Worker }
110*0e209d39SAndroid Build Coastguard Worker
111*0e209d39SAndroid Build Coastguard Worker inBytes=(const uint8_t *)inData;
112*0e209d39SAndroid Build Coastguard Worker outBytes=(uint8_t *)outData;
113*0e209d39SAndroid Build Coastguard Worker
114*0e209d39SAndroid Build Coastguard Worker inHeader=(const UCATableHeader *)inData;
115*0e209d39SAndroid Build Coastguard Worker outHeader=(UCATableHeader *)outData;
116*0e209d39SAndroid Build Coastguard Worker
117*0e209d39SAndroid Build Coastguard Worker /*
118*0e209d39SAndroid Build Coastguard Worker * The collation binary must contain at least the UCATableHeader,
119*0e209d39SAndroid Build Coastguard Worker * starting with its size field.
120*0e209d39SAndroid Build Coastguard Worker * sizeof(UCATableHeader)==42*4 in ICU 2.8
121*0e209d39SAndroid Build Coastguard Worker * check the length against the header size before reading the size field
122*0e209d39SAndroid Build Coastguard Worker */
123*0e209d39SAndroid Build Coastguard Worker uprv_memset(&header, 0, sizeof(header));
124*0e209d39SAndroid Build Coastguard Worker if(length<0) {
125*0e209d39SAndroid Build Coastguard Worker header.size=udata_readInt32(ds, inHeader->size);
126*0e209d39SAndroid Build Coastguard Worker } else if((length<(42*4) || length<(header.size=udata_readInt32(ds, inHeader->size)))) {
127*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=3): too few bytes (%d after header) for collation data\n",
128*0e209d39SAndroid Build Coastguard Worker length);
129*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
130*0e209d39SAndroid Build Coastguard Worker return 0;
131*0e209d39SAndroid Build Coastguard Worker }
132*0e209d39SAndroid Build Coastguard Worker
133*0e209d39SAndroid Build Coastguard Worker header.magic=ds->readUInt32(inHeader->magic);
134*0e209d39SAndroid Build Coastguard Worker if(!(
135*0e209d39SAndroid Build Coastguard Worker header.magic==UCOL_HEADER_MAGIC &&
136*0e209d39SAndroid Build Coastguard Worker inHeader->formatVersion[0]==3 /*&&
137*0e209d39SAndroid Build Coastguard Worker inHeader->formatVersion[1]>=0*/
138*0e209d39SAndroid Build Coastguard Worker )) {
139*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=3): magic 0x%08x or format version %02x.%02x is not a collation binary\n",
140*0e209d39SAndroid Build Coastguard Worker header.magic,
141*0e209d39SAndroid Build Coastguard Worker inHeader->formatVersion[0], inHeader->formatVersion[1]);
142*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_UNSUPPORTED_ERROR;
143*0e209d39SAndroid Build Coastguard Worker return 0;
144*0e209d39SAndroid Build Coastguard Worker }
145*0e209d39SAndroid Build Coastguard Worker
146*0e209d39SAndroid Build Coastguard Worker if(inHeader->isBigEndian!=ds->inIsBigEndian || inHeader->charSetFamily!=ds->inCharset) {
147*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=3): endianness %d or charset %d does not match the swapper\n",
148*0e209d39SAndroid Build Coastguard Worker inHeader->isBigEndian, inHeader->charSetFamily);
149*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_INVALID_FORMAT_ERROR;
150*0e209d39SAndroid Build Coastguard Worker return 0;
151*0e209d39SAndroid Build Coastguard Worker }
152*0e209d39SAndroid Build Coastguard Worker
153*0e209d39SAndroid Build Coastguard Worker if(length>=0) {
154*0e209d39SAndroid Build Coastguard Worker /* copy everything, takes care of data that needs no swapping */
155*0e209d39SAndroid Build Coastguard Worker if(inBytes!=outBytes) {
156*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(outBytes, inBytes, header.size);
157*0e209d39SAndroid Build Coastguard Worker }
158*0e209d39SAndroid Build Coastguard Worker
159*0e209d39SAndroid Build Coastguard Worker /* swap the necessary pieces in the order of their occurrence in the data */
160*0e209d39SAndroid Build Coastguard Worker
161*0e209d39SAndroid Build Coastguard Worker /* read more of the UCATableHeader (the size field was read above) */
162*0e209d39SAndroid Build Coastguard Worker header.options= ds->readUInt32(inHeader->options);
163*0e209d39SAndroid Build Coastguard Worker header.UCAConsts= ds->readUInt32(inHeader->UCAConsts);
164*0e209d39SAndroid Build Coastguard Worker header.contractionUCACombos= ds->readUInt32(inHeader->contractionUCACombos);
165*0e209d39SAndroid Build Coastguard Worker header.mappingPosition= ds->readUInt32(inHeader->mappingPosition);
166*0e209d39SAndroid Build Coastguard Worker header.expansion= ds->readUInt32(inHeader->expansion);
167*0e209d39SAndroid Build Coastguard Worker header.contractionIndex= ds->readUInt32(inHeader->contractionIndex);
168*0e209d39SAndroid Build Coastguard Worker header.contractionCEs= ds->readUInt32(inHeader->contractionCEs);
169*0e209d39SAndroid Build Coastguard Worker header.contractionSize= ds->readUInt32(inHeader->contractionSize);
170*0e209d39SAndroid Build Coastguard Worker header.endExpansionCE= ds->readUInt32(inHeader->endExpansionCE);
171*0e209d39SAndroid Build Coastguard Worker header.expansionCESize= ds->readUInt32(inHeader->expansionCESize);
172*0e209d39SAndroid Build Coastguard Worker header.endExpansionCECount= udata_readInt32(ds, inHeader->endExpansionCECount);
173*0e209d39SAndroid Build Coastguard Worker header.contractionUCACombosSize=udata_readInt32(ds, inHeader->contractionUCACombosSize);
174*0e209d39SAndroid Build Coastguard Worker header.scriptToLeadByte= ds->readUInt32(inHeader->scriptToLeadByte);
175*0e209d39SAndroid Build Coastguard Worker header.leadByteToScript= ds->readUInt32(inHeader->leadByteToScript);
176*0e209d39SAndroid Build Coastguard Worker
177*0e209d39SAndroid Build Coastguard Worker /* swap the 32-bit integers in the header */
178*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inHeader, (int32_t)((const char *)&inHeader->jamoSpecial-(const char *)inHeader),
179*0e209d39SAndroid Build Coastguard Worker outHeader, pErrorCode);
180*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, &(inHeader->scriptToLeadByte), sizeof(header.scriptToLeadByte) + sizeof(header.leadByteToScript),
181*0e209d39SAndroid Build Coastguard Worker &(outHeader->scriptToLeadByte), pErrorCode);
182*0e209d39SAndroid Build Coastguard Worker /* set the output platform properties */
183*0e209d39SAndroid Build Coastguard Worker outHeader->isBigEndian=ds->outIsBigEndian;
184*0e209d39SAndroid Build Coastguard Worker outHeader->charSetFamily=ds->outCharset;
185*0e209d39SAndroid Build Coastguard Worker
186*0e209d39SAndroid Build Coastguard Worker /* swap the options */
187*0e209d39SAndroid Build Coastguard Worker if(header.options!=0) {
188*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes+header.options, header.expansion-header.options,
189*0e209d39SAndroid Build Coastguard Worker outBytes+header.options, pErrorCode);
190*0e209d39SAndroid Build Coastguard Worker }
191*0e209d39SAndroid Build Coastguard Worker
192*0e209d39SAndroid Build Coastguard Worker /* swap the expansions */
193*0e209d39SAndroid Build Coastguard Worker if(header.mappingPosition!=0 && header.expansion!=0) {
194*0e209d39SAndroid Build Coastguard Worker if(header.contractionIndex!=0) {
195*0e209d39SAndroid Build Coastguard Worker /* expansions bounded by contractions */
196*0e209d39SAndroid Build Coastguard Worker count=header.contractionIndex-header.expansion;
197*0e209d39SAndroid Build Coastguard Worker } else {
198*0e209d39SAndroid Build Coastguard Worker /* no contractions: expansions bounded by the main trie */
199*0e209d39SAndroid Build Coastguard Worker count=header.mappingPosition-header.expansion;
200*0e209d39SAndroid Build Coastguard Worker }
201*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes+header.expansion, (int32_t)count,
202*0e209d39SAndroid Build Coastguard Worker outBytes+header.expansion, pErrorCode);
203*0e209d39SAndroid Build Coastguard Worker }
204*0e209d39SAndroid Build Coastguard Worker
205*0e209d39SAndroid Build Coastguard Worker /* swap the contractions */
206*0e209d39SAndroid Build Coastguard Worker if(header.contractionSize!=0) {
207*0e209d39SAndroid Build Coastguard Worker /* contractionIndex: char16_t[] */
208*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes+header.contractionIndex, header.contractionSize*2,
209*0e209d39SAndroid Build Coastguard Worker outBytes+header.contractionIndex, pErrorCode);
210*0e209d39SAndroid Build Coastguard Worker
211*0e209d39SAndroid Build Coastguard Worker /* contractionCEs: CEs[] */
212*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes+header.contractionCEs, header.contractionSize*4,
213*0e209d39SAndroid Build Coastguard Worker outBytes+header.contractionCEs, pErrorCode);
214*0e209d39SAndroid Build Coastguard Worker }
215*0e209d39SAndroid Build Coastguard Worker
216*0e209d39SAndroid Build Coastguard Worker /* swap the main trie */
217*0e209d39SAndroid Build Coastguard Worker if(header.mappingPosition!=0) {
218*0e209d39SAndroid Build Coastguard Worker count=header.endExpansionCE-header.mappingPosition;
219*0e209d39SAndroid Build Coastguard Worker utrie_swap(ds, inBytes+header.mappingPosition, (int32_t)count,
220*0e209d39SAndroid Build Coastguard Worker outBytes+header.mappingPosition, pErrorCode);
221*0e209d39SAndroid Build Coastguard Worker }
222*0e209d39SAndroid Build Coastguard Worker
223*0e209d39SAndroid Build Coastguard Worker /* swap the max expansion table */
224*0e209d39SAndroid Build Coastguard Worker if(header.endExpansionCECount!=0) {
225*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes+header.endExpansionCE, header.endExpansionCECount*4,
226*0e209d39SAndroid Build Coastguard Worker outBytes+header.endExpansionCE, pErrorCode);
227*0e209d39SAndroid Build Coastguard Worker }
228*0e209d39SAndroid Build Coastguard Worker
229*0e209d39SAndroid Build Coastguard Worker /* expansionCESize, unsafeCP, contrEndCP: uint8_t[], no need to swap */
230*0e209d39SAndroid Build Coastguard Worker
231*0e209d39SAndroid Build Coastguard Worker /* swap UCA constants */
232*0e209d39SAndroid Build Coastguard Worker if(header.UCAConsts!=0) {
233*0e209d39SAndroid Build Coastguard Worker /*
234*0e209d39SAndroid Build Coastguard Worker * if UCAConsts!=0 then contractionUCACombos because we are swapping
235*0e209d39SAndroid Build Coastguard Worker * the UCA data file, and we know that the UCA contains contractions
236*0e209d39SAndroid Build Coastguard Worker */
237*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes+header.UCAConsts, header.contractionUCACombos-header.UCAConsts,
238*0e209d39SAndroid Build Coastguard Worker outBytes+header.UCAConsts, pErrorCode);
239*0e209d39SAndroid Build Coastguard Worker }
240*0e209d39SAndroid Build Coastguard Worker
241*0e209d39SAndroid Build Coastguard Worker /* swap UCA contractions */
242*0e209d39SAndroid Build Coastguard Worker if(header.contractionUCACombosSize!=0) {
243*0e209d39SAndroid Build Coastguard Worker count=header.contractionUCACombosSize*inHeader->contractionUCACombosWidth*U_SIZEOF_UCHAR;
244*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes+header.contractionUCACombos, (int32_t)count,
245*0e209d39SAndroid Build Coastguard Worker outBytes+header.contractionUCACombos, pErrorCode);
246*0e209d39SAndroid Build Coastguard Worker }
247*0e209d39SAndroid Build Coastguard Worker
248*0e209d39SAndroid Build Coastguard Worker /* swap the script to lead bytes */
249*0e209d39SAndroid Build Coastguard Worker if(header.scriptToLeadByte!=0) {
250*0e209d39SAndroid Build Coastguard Worker int indexCount = ds->readUInt16(*((uint16_t*)(inBytes+header.scriptToLeadByte))); // each entry = 2 * uint16
251*0e209d39SAndroid Build Coastguard Worker int dataCount = ds->readUInt16(*((uint16_t*)(inBytes+header.scriptToLeadByte + 2))); // each entry = uint16
252*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes+header.scriptToLeadByte,
253*0e209d39SAndroid Build Coastguard Worker 4 + (4 * indexCount) + (2 * dataCount),
254*0e209d39SAndroid Build Coastguard Worker outBytes+header.scriptToLeadByte, pErrorCode);
255*0e209d39SAndroid Build Coastguard Worker }
256*0e209d39SAndroid Build Coastguard Worker
257*0e209d39SAndroid Build Coastguard Worker /* swap the lead byte to scripts */
258*0e209d39SAndroid Build Coastguard Worker if(header.leadByteToScript!=0) {
259*0e209d39SAndroid Build Coastguard Worker int indexCount = ds->readUInt16(*((uint16_t*)(inBytes+header.leadByteToScript))); // each entry = uint16
260*0e209d39SAndroid Build Coastguard Worker int dataCount = ds->readUInt16(*((uint16_t*)(inBytes+header.leadByteToScript + 2))); // each entry = uint16
261*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes+header.leadByteToScript,
262*0e209d39SAndroid Build Coastguard Worker 4 + (2 * indexCount) + (2 * dataCount),
263*0e209d39SAndroid Build Coastguard Worker outBytes+header.leadByteToScript, pErrorCode);
264*0e209d39SAndroid Build Coastguard Worker }
265*0e209d39SAndroid Build Coastguard Worker }
266*0e209d39SAndroid Build Coastguard Worker
267*0e209d39SAndroid Build Coastguard Worker return header.size;
268*0e209d39SAndroid Build Coastguard Worker }
269*0e209d39SAndroid Build Coastguard Worker
270*0e209d39SAndroid Build Coastguard Worker // swap formatVersion 4 or 5 ----------------------------------------------- ***
271*0e209d39SAndroid Build Coastguard Worker
272*0e209d39SAndroid Build Coastguard Worker // The following are copied from CollationDataReader, trading an awkward copy of constants
273*0e209d39SAndroid Build Coastguard Worker // for an awkward relocation of the i18n collationdatareader.h file into the common library.
274*0e209d39SAndroid Build Coastguard Worker // Keep them in sync!
275*0e209d39SAndroid Build Coastguard Worker
276*0e209d39SAndroid Build Coastguard Worker enum {
277*0e209d39SAndroid Build Coastguard Worker IX_INDEXES_LENGTH, // 0
278*0e209d39SAndroid Build Coastguard Worker IX_OPTIONS,
279*0e209d39SAndroid Build Coastguard Worker IX_RESERVED2,
280*0e209d39SAndroid Build Coastguard Worker IX_RESERVED3,
281*0e209d39SAndroid Build Coastguard Worker
282*0e209d39SAndroid Build Coastguard Worker IX_JAMO_CE32S_START, // 4
283*0e209d39SAndroid Build Coastguard Worker IX_REORDER_CODES_OFFSET,
284*0e209d39SAndroid Build Coastguard Worker IX_REORDER_TABLE_OFFSET,
285*0e209d39SAndroid Build Coastguard Worker IX_TRIE_OFFSET,
286*0e209d39SAndroid Build Coastguard Worker
287*0e209d39SAndroid Build Coastguard Worker IX_RESERVED8_OFFSET, // 8
288*0e209d39SAndroid Build Coastguard Worker IX_CES_OFFSET,
289*0e209d39SAndroid Build Coastguard Worker IX_RESERVED10_OFFSET,
290*0e209d39SAndroid Build Coastguard Worker IX_CE32S_OFFSET,
291*0e209d39SAndroid Build Coastguard Worker
292*0e209d39SAndroid Build Coastguard Worker IX_ROOT_ELEMENTS_OFFSET, // 12
293*0e209d39SAndroid Build Coastguard Worker IX_CONTEXTS_OFFSET,
294*0e209d39SAndroid Build Coastguard Worker IX_UNSAFE_BWD_OFFSET,
295*0e209d39SAndroid Build Coastguard Worker IX_FAST_LATIN_TABLE_OFFSET,
296*0e209d39SAndroid Build Coastguard Worker
297*0e209d39SAndroid Build Coastguard Worker IX_SCRIPTS_OFFSET, // 16
298*0e209d39SAndroid Build Coastguard Worker IX_COMPRESSIBLE_BYTES_OFFSET,
299*0e209d39SAndroid Build Coastguard Worker IX_RESERVED18_OFFSET,
300*0e209d39SAndroid Build Coastguard Worker IX_TOTAL_SIZE
301*0e209d39SAndroid Build Coastguard Worker };
302*0e209d39SAndroid Build Coastguard Worker
303*0e209d39SAndroid Build Coastguard Worker int32_t
swapFormatVersion4(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode & errorCode)304*0e209d39SAndroid Build Coastguard Worker swapFormatVersion4(const UDataSwapper *ds,
305*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
306*0e209d39SAndroid Build Coastguard Worker UErrorCode &errorCode) {
307*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(errorCode)) { return 0; }
308*0e209d39SAndroid Build Coastguard Worker
309*0e209d39SAndroid Build Coastguard Worker const uint8_t *inBytes=(const uint8_t *)inData;
310*0e209d39SAndroid Build Coastguard Worker uint8_t *outBytes=(uint8_t *)outData;
311*0e209d39SAndroid Build Coastguard Worker
312*0e209d39SAndroid Build Coastguard Worker const int32_t *inIndexes=(const int32_t *)inBytes;
313*0e209d39SAndroid Build Coastguard Worker int32_t indexes[IX_TOTAL_SIZE+1];
314*0e209d39SAndroid Build Coastguard Worker
315*0e209d39SAndroid Build Coastguard Worker // Need at least IX_INDEXES_LENGTH and IX_OPTIONS.
316*0e209d39SAndroid Build Coastguard Worker if(0<=length && length<8) {
317*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=4): too few bytes "
318*0e209d39SAndroid Build Coastguard Worker "(%d after header) for collation data\n",
319*0e209d39SAndroid Build Coastguard Worker length);
320*0e209d39SAndroid Build Coastguard Worker errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
321*0e209d39SAndroid Build Coastguard Worker return 0;
322*0e209d39SAndroid Build Coastguard Worker }
323*0e209d39SAndroid Build Coastguard Worker
324*0e209d39SAndroid Build Coastguard Worker int32_t indexesLength=indexes[0]=udata_readInt32(ds, inIndexes[0]);
325*0e209d39SAndroid Build Coastguard Worker if(0<=length && length<(indexesLength*4)) {
326*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=4): too few bytes "
327*0e209d39SAndroid Build Coastguard Worker "(%d after header) for collation data\n",
328*0e209d39SAndroid Build Coastguard Worker length);
329*0e209d39SAndroid Build Coastguard Worker errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
330*0e209d39SAndroid Build Coastguard Worker return 0;
331*0e209d39SAndroid Build Coastguard Worker }
332*0e209d39SAndroid Build Coastguard Worker
333*0e209d39SAndroid Build Coastguard Worker for(int32_t i=1; i<=IX_TOTAL_SIZE && i<indexesLength; ++i) {
334*0e209d39SAndroid Build Coastguard Worker indexes[i]=udata_readInt32(ds, inIndexes[i]);
335*0e209d39SAndroid Build Coastguard Worker }
336*0e209d39SAndroid Build Coastguard Worker for(int32_t i=indexesLength; i<=IX_TOTAL_SIZE; ++i) {
337*0e209d39SAndroid Build Coastguard Worker indexes[i]=-1;
338*0e209d39SAndroid Build Coastguard Worker }
339*0e209d39SAndroid Build Coastguard Worker inIndexes=nullptr; // Make sure we do not accidentally use these instead of indexes[].
340*0e209d39SAndroid Build Coastguard Worker
341*0e209d39SAndroid Build Coastguard Worker // Get the total length of the data.
342*0e209d39SAndroid Build Coastguard Worker int32_t size;
343*0e209d39SAndroid Build Coastguard Worker if(indexesLength>IX_TOTAL_SIZE) {
344*0e209d39SAndroid Build Coastguard Worker size=indexes[IX_TOTAL_SIZE];
345*0e209d39SAndroid Build Coastguard Worker } else if(indexesLength>IX_REORDER_CODES_OFFSET) {
346*0e209d39SAndroid Build Coastguard Worker size=indexes[indexesLength-1];
347*0e209d39SAndroid Build Coastguard Worker } else {
348*0e209d39SAndroid Build Coastguard Worker size=indexesLength*4;
349*0e209d39SAndroid Build Coastguard Worker }
350*0e209d39SAndroid Build Coastguard Worker if(length<0) { return size; }
351*0e209d39SAndroid Build Coastguard Worker
352*0e209d39SAndroid Build Coastguard Worker if(length<size) {
353*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=4): too few bytes "
354*0e209d39SAndroid Build Coastguard Worker "(%d after header) for collation data\n",
355*0e209d39SAndroid Build Coastguard Worker length);
356*0e209d39SAndroid Build Coastguard Worker errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
357*0e209d39SAndroid Build Coastguard Worker return 0;
358*0e209d39SAndroid Build Coastguard Worker }
359*0e209d39SAndroid Build Coastguard Worker
360*0e209d39SAndroid Build Coastguard Worker // Copy the data for inaccessible bytes and arrays of bytes.
361*0e209d39SAndroid Build Coastguard Worker if(inBytes!=outBytes) {
362*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(outBytes, inBytes, size);
363*0e209d39SAndroid Build Coastguard Worker }
364*0e209d39SAndroid Build Coastguard Worker
365*0e209d39SAndroid Build Coastguard Worker // Swap the int32_t indexes[].
366*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes, indexesLength * 4, outBytes, &errorCode);
367*0e209d39SAndroid Build Coastguard Worker
368*0e209d39SAndroid Build Coastguard Worker // The following is a modified version of CollationDataReader::read().
369*0e209d39SAndroid Build Coastguard Worker // Here we use indexes[] not inIndexes[] because
370*0e209d39SAndroid Build Coastguard Worker // the inIndexes[] may not be in this machine's endianness.
371*0e209d39SAndroid Build Coastguard Worker int32_t index; // one of the indexes[] slots
372*0e209d39SAndroid Build Coastguard Worker int32_t offset; // byte offset for the index part
373*0e209d39SAndroid Build Coastguard Worker // int32_t length; // number of bytes in the index part
374*0e209d39SAndroid Build Coastguard Worker
375*0e209d39SAndroid Build Coastguard Worker index = IX_REORDER_CODES_OFFSET;
376*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
377*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
378*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
379*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes + offset, length, outBytes + offset, &errorCode);
380*0e209d39SAndroid Build Coastguard Worker }
381*0e209d39SAndroid Build Coastguard Worker
382*0e209d39SAndroid Build Coastguard Worker // Skip the IX_REORDER_TABLE_OFFSET byte array.
383*0e209d39SAndroid Build Coastguard Worker
384*0e209d39SAndroid Build Coastguard Worker index = IX_TRIE_OFFSET;
385*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
386*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
387*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
388*0e209d39SAndroid Build Coastguard Worker utrie2_swap(ds, inBytes + offset, length, outBytes + offset, &errorCode);
389*0e209d39SAndroid Build Coastguard Worker }
390*0e209d39SAndroid Build Coastguard Worker
391*0e209d39SAndroid Build Coastguard Worker index = IX_RESERVED8_OFFSET;
392*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
393*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
394*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
395*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=4): unknown data at IX_RESERVED8_OFFSET\n", length);
396*0e209d39SAndroid Build Coastguard Worker errorCode = U_UNSUPPORTED_ERROR;
397*0e209d39SAndroid Build Coastguard Worker return 0;
398*0e209d39SAndroid Build Coastguard Worker }
399*0e209d39SAndroid Build Coastguard Worker
400*0e209d39SAndroid Build Coastguard Worker index = IX_CES_OFFSET;
401*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
402*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
403*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
404*0e209d39SAndroid Build Coastguard Worker ds->swapArray64(ds, inBytes + offset, length, outBytes + offset, &errorCode);
405*0e209d39SAndroid Build Coastguard Worker }
406*0e209d39SAndroid Build Coastguard Worker
407*0e209d39SAndroid Build Coastguard Worker index = IX_RESERVED10_OFFSET;
408*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
409*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
410*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
411*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=4): unknown data at IX_RESERVED10_OFFSET\n", length);
412*0e209d39SAndroid Build Coastguard Worker errorCode = U_UNSUPPORTED_ERROR;
413*0e209d39SAndroid Build Coastguard Worker return 0;
414*0e209d39SAndroid Build Coastguard Worker }
415*0e209d39SAndroid Build Coastguard Worker
416*0e209d39SAndroid Build Coastguard Worker index = IX_CE32S_OFFSET;
417*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
418*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
419*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
420*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes + offset, length, outBytes + offset, &errorCode);
421*0e209d39SAndroid Build Coastguard Worker }
422*0e209d39SAndroid Build Coastguard Worker
423*0e209d39SAndroid Build Coastguard Worker index = IX_ROOT_ELEMENTS_OFFSET;
424*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
425*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
426*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
427*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes + offset, length, outBytes + offset, &errorCode);
428*0e209d39SAndroid Build Coastguard Worker }
429*0e209d39SAndroid Build Coastguard Worker
430*0e209d39SAndroid Build Coastguard Worker index = IX_CONTEXTS_OFFSET;
431*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
432*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
433*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
434*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
435*0e209d39SAndroid Build Coastguard Worker }
436*0e209d39SAndroid Build Coastguard Worker
437*0e209d39SAndroid Build Coastguard Worker index = IX_UNSAFE_BWD_OFFSET;
438*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
439*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
440*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
441*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
442*0e209d39SAndroid Build Coastguard Worker }
443*0e209d39SAndroid Build Coastguard Worker
444*0e209d39SAndroid Build Coastguard Worker index = IX_FAST_LATIN_TABLE_OFFSET;
445*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
446*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
447*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
448*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
449*0e209d39SAndroid Build Coastguard Worker }
450*0e209d39SAndroid Build Coastguard Worker
451*0e209d39SAndroid Build Coastguard Worker index = IX_SCRIPTS_OFFSET;
452*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
453*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
454*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
455*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes + offset, length, outBytes + offset, &errorCode);
456*0e209d39SAndroid Build Coastguard Worker }
457*0e209d39SAndroid Build Coastguard Worker
458*0e209d39SAndroid Build Coastguard Worker // Skip the IX_COMPRESSIBLE_BYTES_OFFSET byte array.
459*0e209d39SAndroid Build Coastguard Worker
460*0e209d39SAndroid Build Coastguard Worker index = IX_RESERVED18_OFFSET;
461*0e209d39SAndroid Build Coastguard Worker offset = indexes[index];
462*0e209d39SAndroid Build Coastguard Worker length = indexes[index + 1] - offset;
463*0e209d39SAndroid Build Coastguard Worker if(length > 0) {
464*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(formatVersion=4): unknown data at IX_RESERVED18_OFFSET\n", length);
465*0e209d39SAndroid Build Coastguard Worker errorCode = U_UNSUPPORTED_ERROR;
466*0e209d39SAndroid Build Coastguard Worker return 0;
467*0e209d39SAndroid Build Coastguard Worker }
468*0e209d39SAndroid Build Coastguard Worker
469*0e209d39SAndroid Build Coastguard Worker return size;
470*0e209d39SAndroid Build Coastguard Worker }
471*0e209d39SAndroid Build Coastguard Worker
472*0e209d39SAndroid Build Coastguard Worker } // namespace
473*0e209d39SAndroid Build Coastguard Worker
474*0e209d39SAndroid Build Coastguard Worker /* swap ICU collation data like ucadata.icu */
475*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
ucol_swap(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * pErrorCode)476*0e209d39SAndroid Build Coastguard Worker ucol_swap(const UDataSwapper *ds,
477*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
478*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
479*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) { return 0; }
480*0e209d39SAndroid Build Coastguard Worker
481*0e209d39SAndroid Build Coastguard Worker /* udata_swapDataHeader checks the arguments */
482*0e209d39SAndroid Build Coastguard Worker int32_t headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
483*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) {
484*0e209d39SAndroid Build Coastguard Worker // Try to swap the old format version which did not have a standard data header.
485*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ZERO_ERROR;
486*0e209d39SAndroid Build Coastguard Worker return swapFormatVersion3(ds, inData, length, outData, pErrorCode);
487*0e209d39SAndroid Build Coastguard Worker }
488*0e209d39SAndroid Build Coastguard Worker
489*0e209d39SAndroid Build Coastguard Worker /* check data format and format version */
490*0e209d39SAndroid Build Coastguard Worker const UDataInfo &info=*(const UDataInfo *)((const char *)inData+4);
491*0e209d39SAndroid Build Coastguard Worker if(!(
492*0e209d39SAndroid Build Coastguard Worker info.dataFormat[0]==0x55 && // dataFormat="UCol"
493*0e209d39SAndroid Build Coastguard Worker info.dataFormat[1]==0x43 &&
494*0e209d39SAndroid Build Coastguard Worker info.dataFormat[2]==0x6f &&
495*0e209d39SAndroid Build Coastguard Worker info.dataFormat[3]==0x6c &&
496*0e209d39SAndroid Build Coastguard Worker (3<=info.formatVersion[0] && info.formatVersion[0]<=5)
497*0e209d39SAndroid Build Coastguard Worker )) {
498*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swap(): data format %02x.%02x.%02x.%02x "
499*0e209d39SAndroid Build Coastguard Worker "(format version %02x.%02x) is not recognized as collation data\n",
500*0e209d39SAndroid Build Coastguard Worker info.dataFormat[0], info.dataFormat[1],
501*0e209d39SAndroid Build Coastguard Worker info.dataFormat[2], info.dataFormat[3],
502*0e209d39SAndroid Build Coastguard Worker info.formatVersion[0], info.formatVersion[1]);
503*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_UNSUPPORTED_ERROR;
504*0e209d39SAndroid Build Coastguard Worker return 0;
505*0e209d39SAndroid Build Coastguard Worker }
506*0e209d39SAndroid Build Coastguard Worker
507*0e209d39SAndroid Build Coastguard Worker inData=(const char *)inData+headerSize;
508*0e209d39SAndroid Build Coastguard Worker if(length>=0) { length-=headerSize; }
509*0e209d39SAndroid Build Coastguard Worker outData=(outData == nullptr) ? nullptr : (char *)outData+headerSize;
510*0e209d39SAndroid Build Coastguard Worker int32_t collationSize;
511*0e209d39SAndroid Build Coastguard Worker if(info.formatVersion[0]>=4) {
512*0e209d39SAndroid Build Coastguard Worker collationSize=swapFormatVersion4(ds, inData, length, outData, *pErrorCode);
513*0e209d39SAndroid Build Coastguard Worker } else {
514*0e209d39SAndroid Build Coastguard Worker collationSize=swapFormatVersion3(ds, inData, length, outData, pErrorCode);
515*0e209d39SAndroid Build Coastguard Worker }
516*0e209d39SAndroid Build Coastguard Worker if(U_SUCCESS(*pErrorCode)) {
517*0e209d39SAndroid Build Coastguard Worker return headerSize+collationSize;
518*0e209d39SAndroid Build Coastguard Worker } else {
519*0e209d39SAndroid Build Coastguard Worker return 0;
520*0e209d39SAndroid Build Coastguard Worker }
521*0e209d39SAndroid Build Coastguard Worker }
522*0e209d39SAndroid Build Coastguard Worker
523*0e209d39SAndroid Build Coastguard Worker /* swap inverse UCA collation data (invuca.icu) */
524*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
ucol_swapInverseUCA(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * pErrorCode)525*0e209d39SAndroid Build Coastguard Worker ucol_swapInverseUCA(const UDataSwapper *ds,
526*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
527*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
528*0e209d39SAndroid Build Coastguard Worker const UDataInfo *pInfo;
529*0e209d39SAndroid Build Coastguard Worker int32_t headerSize;
530*0e209d39SAndroid Build Coastguard Worker
531*0e209d39SAndroid Build Coastguard Worker const uint8_t *inBytes;
532*0e209d39SAndroid Build Coastguard Worker uint8_t *outBytes;
533*0e209d39SAndroid Build Coastguard Worker
534*0e209d39SAndroid Build Coastguard Worker const InverseUCATableHeader *inHeader;
535*0e209d39SAndroid Build Coastguard Worker InverseUCATableHeader *outHeader;
536*0e209d39SAndroid Build Coastguard Worker InverseUCATableHeader header={ 0,0,0,0,0,{0,0,0,0},{0,0,0,0,0,0,0,0} };
537*0e209d39SAndroid Build Coastguard Worker
538*0e209d39SAndroid Build Coastguard Worker /* udata_swapDataHeader checks the arguments */
539*0e209d39SAndroid Build Coastguard Worker headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
540*0e209d39SAndroid Build Coastguard Worker if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
541*0e209d39SAndroid Build Coastguard Worker return 0;
542*0e209d39SAndroid Build Coastguard Worker }
543*0e209d39SAndroid Build Coastguard Worker
544*0e209d39SAndroid Build Coastguard Worker /* check data format and format version */
545*0e209d39SAndroid Build Coastguard Worker pInfo=(const UDataInfo *)((const char *)inData+4);
546*0e209d39SAndroid Build Coastguard Worker if(!(
547*0e209d39SAndroid Build Coastguard Worker pInfo->dataFormat[0]==0x49 && /* dataFormat="InvC" */
548*0e209d39SAndroid Build Coastguard Worker pInfo->dataFormat[1]==0x6e &&
549*0e209d39SAndroid Build Coastguard Worker pInfo->dataFormat[2]==0x76 &&
550*0e209d39SAndroid Build Coastguard Worker pInfo->dataFormat[3]==0x43 &&
551*0e209d39SAndroid Build Coastguard Worker pInfo->formatVersion[0]==2 &&
552*0e209d39SAndroid Build Coastguard Worker pInfo->formatVersion[1]>=1
553*0e209d39SAndroid Build Coastguard Worker )) {
554*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swapInverseUCA(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not an inverse UCA collation file\n",
555*0e209d39SAndroid Build Coastguard Worker pInfo->dataFormat[0], pInfo->dataFormat[1],
556*0e209d39SAndroid Build Coastguard Worker pInfo->dataFormat[2], pInfo->dataFormat[3],
557*0e209d39SAndroid Build Coastguard Worker pInfo->formatVersion[0], pInfo->formatVersion[1]);
558*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_UNSUPPORTED_ERROR;
559*0e209d39SAndroid Build Coastguard Worker return 0;
560*0e209d39SAndroid Build Coastguard Worker }
561*0e209d39SAndroid Build Coastguard Worker
562*0e209d39SAndroid Build Coastguard Worker inBytes=(const uint8_t *)inData+headerSize;
563*0e209d39SAndroid Build Coastguard Worker outBytes=(uint8_t *)outData+headerSize;
564*0e209d39SAndroid Build Coastguard Worker
565*0e209d39SAndroid Build Coastguard Worker inHeader=(const InverseUCATableHeader *)inBytes;
566*0e209d39SAndroid Build Coastguard Worker outHeader=(InverseUCATableHeader *)outBytes;
567*0e209d39SAndroid Build Coastguard Worker
568*0e209d39SAndroid Build Coastguard Worker /*
569*0e209d39SAndroid Build Coastguard Worker * The inverse UCA collation binary must contain at least the InverseUCATableHeader,
570*0e209d39SAndroid Build Coastguard Worker * starting with its size field.
571*0e209d39SAndroid Build Coastguard Worker * sizeof(UCATableHeader)==8*4 in ICU 2.8
572*0e209d39SAndroid Build Coastguard Worker * check the length against the header size before reading the size field
573*0e209d39SAndroid Build Coastguard Worker */
574*0e209d39SAndroid Build Coastguard Worker if(length<0) {
575*0e209d39SAndroid Build Coastguard Worker header.byteSize=udata_readInt32(ds, inHeader->byteSize);
576*0e209d39SAndroid Build Coastguard Worker } else if(
577*0e209d39SAndroid Build Coastguard Worker ((length-headerSize)<(8*4) ||
578*0e209d39SAndroid Build Coastguard Worker (uint32_t)(length-headerSize)<(header.byteSize=udata_readInt32(ds, inHeader->byteSize)))
579*0e209d39SAndroid Build Coastguard Worker ) {
580*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "ucol_swapInverseUCA(): too few bytes (%d after header) for inverse UCA collation data\n",
581*0e209d39SAndroid Build Coastguard Worker length);
582*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
583*0e209d39SAndroid Build Coastguard Worker return 0;
584*0e209d39SAndroid Build Coastguard Worker }
585*0e209d39SAndroid Build Coastguard Worker
586*0e209d39SAndroid Build Coastguard Worker if(length>=0) {
587*0e209d39SAndroid Build Coastguard Worker /* copy everything, takes care of data that needs no swapping */
588*0e209d39SAndroid Build Coastguard Worker if(inBytes!=outBytes) {
589*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(outBytes, inBytes, header.byteSize);
590*0e209d39SAndroid Build Coastguard Worker }
591*0e209d39SAndroid Build Coastguard Worker
592*0e209d39SAndroid Build Coastguard Worker /* swap the necessary pieces in the order of their occurrence in the data */
593*0e209d39SAndroid Build Coastguard Worker
594*0e209d39SAndroid Build Coastguard Worker /* read more of the InverseUCATableHeader (the byteSize field was read above) */
595*0e209d39SAndroid Build Coastguard Worker header.tableSize= ds->readUInt32(inHeader->tableSize);
596*0e209d39SAndroid Build Coastguard Worker header.contsSize= ds->readUInt32(inHeader->contsSize);
597*0e209d39SAndroid Build Coastguard Worker header.table= ds->readUInt32(inHeader->table);
598*0e209d39SAndroid Build Coastguard Worker header.conts= ds->readUInt32(inHeader->conts);
599*0e209d39SAndroid Build Coastguard Worker
600*0e209d39SAndroid Build Coastguard Worker /* swap the 32-bit integers in the header */
601*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inHeader, 5*4, outHeader, pErrorCode);
602*0e209d39SAndroid Build Coastguard Worker
603*0e209d39SAndroid Build Coastguard Worker /* swap the inverse table; tableSize counts uint32_t[3] rows */
604*0e209d39SAndroid Build Coastguard Worker ds->swapArray32(ds, inBytes+header.table, header.tableSize*3*4,
605*0e209d39SAndroid Build Coastguard Worker outBytes+header.table, pErrorCode);
606*0e209d39SAndroid Build Coastguard Worker
607*0e209d39SAndroid Build Coastguard Worker /* swap the continuation table; contsSize counts UChars */
608*0e209d39SAndroid Build Coastguard Worker ds->swapArray16(ds, inBytes+header.conts, header.contsSize*U_SIZEOF_UCHAR,
609*0e209d39SAndroid Build Coastguard Worker outBytes+header.conts, pErrorCode);
610*0e209d39SAndroid Build Coastguard Worker }
611*0e209d39SAndroid Build Coastguard Worker
612*0e209d39SAndroid Build Coastguard Worker return headerSize+header.byteSize;
613*0e209d39SAndroid Build Coastguard Worker }
614*0e209d39SAndroid Build Coastguard Worker
615*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_COLLATION */
616