1/*
2*******************************************************************************
3*
4*   Copyright (C) 2003-2010, International Business Machines
5*   Corporation and others.  All Rights Reserved.
6*
7*******************************************************************************
8*   file name:  ucol_swp.c
9*   encoding:   US-ASCII
10*   tab size:   8 (not used)
11*   indentation:4
12*
13*   created on: 2003sep10
14*   created by: Markus W. Scherer
15*
16*   Swap collation binaries.
17*/
18
19#include "unicode/udata.h" /* UDataInfo */
20#include "utrie.h"
21#include "udataswp.h"
22#include "cmemory.h"
23#include "ucol_imp.h"
24#include "ucol_swp.h"
25
26/* swapping ----------------------------------------------------------------- */
27
28/*
29 * This performs data swapping for a folded trie (see utrie.c for details).
30 */
31
32U_CAPI int32_t U_EXPORT2
33utrie_swap(const UDataSwapper *ds,
34           const void *inData, int32_t length, void *outData,
35           UErrorCode *pErrorCode) {
36    const UTrieHeader *inTrie;
37    UTrieHeader trie;
38    int32_t size;
39    UBool dataIs32;
40
41    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
42        return 0;
43    }
44    if(ds==NULL || inData==NULL || (length>=0 && outData==NULL)) {
45        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
46        return 0;
47    }
48
49    /* setup and swapping */
50    if(length>=0 && length<sizeof(UTrieHeader)) {
51        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
52        return 0;
53    }
54
55    inTrie=(const UTrieHeader *)inData;
56    trie.signature=ds->readUInt32(inTrie->signature);
57    trie.options=ds->readUInt32(inTrie->options);
58    trie.indexLength=udata_readInt32(ds, inTrie->indexLength);
59    trie.dataLength=udata_readInt32(ds, inTrie->dataLength);
60
61    if( trie.signature!=0x54726965 ||
62        (trie.options&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_SHIFT ||
63        ((trie.options>>UTRIE_OPTIONS_INDEX_SHIFT)&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_INDEX_SHIFT ||
64        trie.indexLength<UTRIE_BMP_INDEX_LENGTH ||
65        (trie.indexLength&(UTRIE_SURROGATE_BLOCK_COUNT-1))!=0 ||
66        trie.dataLength<UTRIE_DATA_BLOCK_LENGTH ||
67        (trie.dataLength&(UTRIE_DATA_GRANULARITY-1))!=0 ||
68        ((trie.options&UTRIE_OPTIONS_LATIN1_IS_LINEAR)!=0 && trie.dataLength<(UTRIE_DATA_BLOCK_LENGTH+0x100))
69    ) {
70        *pErrorCode=U_INVALID_FORMAT_ERROR; /* not a UTrie */
71        return 0;
72    }
73
74    dataIs32=(UBool)((trie.options&UTRIE_OPTIONS_DATA_IS_32_BIT)!=0);
75    size=sizeof(UTrieHeader)+trie.indexLength*2+trie.dataLength*(dataIs32?4:2);
76
77    if(length>=0) {
78        UTrieHeader *outTrie;
79
80        if(length<size) {
81            *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
82            return 0;
83        }
84
85        outTrie=(UTrieHeader *)outData;
86
87        /* swap the header */
88        ds->swapArray32(ds, inTrie, sizeof(UTrieHeader), outTrie, pErrorCode);
89
90        /* swap the index and the data */
91        if(dataIs32) {
92            ds->swapArray16(ds, inTrie+1, trie.indexLength*2, outTrie+1, pErrorCode);
93            ds->swapArray32(ds, (const uint16_t *)(inTrie+1)+trie.indexLength, trie.dataLength*4,
94                                     (uint16_t *)(outTrie+1)+trie.indexLength, pErrorCode);
95        } else {
96            ds->swapArray16(ds, inTrie+1, (trie.indexLength+trie.dataLength)*2, outTrie+1, pErrorCode);
97        }
98    }
99
100    return size;
101}
102
103#if !UCONFIG_NO_COLLATION
104
105/* Modified copy of the beginning of ucol_swapBinary(). */
106U_CAPI UBool U_EXPORT2
107ucol_looksLikeCollationBinary(const UDataSwapper *ds,
108                              const void *inData, int32_t length) {
109    const uint8_t *inBytes;
110    const UCATableHeader *inHeader;
111    UCATableHeader header={ 0 };
112
113    if(ds==NULL || inData==NULL || length<-1) {
114        return FALSE;
115    }
116
117    inBytes=(const uint8_t *)inData;
118    inHeader=(const UCATableHeader *)inData;
119
120    /*
121     * The collation binary must contain at least the UCATableHeader,
122     * starting with its size field.
123     * sizeof(UCATableHeader)==42*4 in ICU 2.8
124     * check the length against the header size before reading the size field
125     */
126    if(length<0) {
127        header.size=udata_readInt32(ds, inHeader->size);
128    } else if((length<(42*4) || length<(header.size=udata_readInt32(ds, inHeader->size)))) {
129        return FALSE;
130    }
131
132    header.magic=ds->readUInt32(inHeader->magic);
133    if(!(
134        header.magic==UCOL_HEADER_MAGIC &&
135        inHeader->formatVersion[0]==2 &&
136        inHeader->formatVersion[1]>=3
137    )) {
138        return FALSE;
139    }
140
141    if(inHeader->isBigEndian!=ds->inIsBigEndian || inHeader->charSetFamily!=ds->inCharset) {
142        return FALSE;
143    }
144
145    return TRUE;
146}
147
148/* swap a header-less collation binary, inside a resource bundle or ucadata.icu */
149U_CAPI int32_t U_EXPORT2
150ucol_swapBinary(const UDataSwapper *ds,
151                const void *inData, int32_t length, void *outData,
152                UErrorCode *pErrorCode) {
153    const uint8_t *inBytes;
154    uint8_t *outBytes;
155
156    const UCATableHeader *inHeader;
157    UCATableHeader *outHeader;
158    UCATableHeader header={ 0 };
159
160    uint32_t count;
161
162    /* argument checking in case we were not called from ucol_swap() */
163    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
164        return 0;
165    }
166    if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) {
167        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
168        return 0;
169    }
170
171    inBytes=(const uint8_t *)inData;
172    outBytes=(uint8_t *)outData;
173
174    inHeader=(const UCATableHeader *)inData;
175    outHeader=(UCATableHeader *)outData;
176
177    /*
178     * The collation binary must contain at least the UCATableHeader,
179     * starting with its size field.
180     * sizeof(UCATableHeader)==42*4 in ICU 2.8
181     * check the length against the header size before reading the size field
182     */
183    if(length<0) {
184        header.size=udata_readInt32(ds, inHeader->size);
185    } else if((length<(42*4) || length<(header.size=udata_readInt32(ds, inHeader->size)))) {
186        udata_printError(ds, "ucol_swapBinary(): too few bytes (%d after header) for collation data\n",
187                         length);
188        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
189        return 0;
190    }
191
192    header.magic=ds->readUInt32(inHeader->magic);
193    if(!(
194        header.magic==UCOL_HEADER_MAGIC &&
195        inHeader->formatVersion[0]==2 &&
196        inHeader->formatVersion[1]>=3
197    )) {
198        udata_printError(ds, "ucol_swapBinary(): magic 0x%08x or format version %02x.%02x is not a collation binary\n",
199                         header.magic,
200                         inHeader->formatVersion[0], inHeader->formatVersion[1]);
201        *pErrorCode=U_UNSUPPORTED_ERROR;
202        return 0;
203    }
204
205    if(inHeader->isBigEndian!=ds->inIsBigEndian || inHeader->charSetFamily!=ds->inCharset) {
206        udata_printError(ds, "ucol_swapBinary(): endianness %d or charset %d does not match the swapper\n",
207                         inHeader->isBigEndian, inHeader->charSetFamily);
208        *pErrorCode=U_INVALID_FORMAT_ERROR;
209        return 0;
210    }
211
212    if(length>=0) {
213        /* copy everything, takes care of data that needs no swapping */
214        if(inBytes!=outBytes) {
215            uprv_memcpy(outBytes, inBytes, header.size);
216        }
217
218        /* swap the necessary pieces in the order of their occurrence in the data */
219
220        /* read more of the UCATableHeader (the size field was read above) */
221        header.options=                 ds->readUInt32(inHeader->options);
222        header.UCAConsts=               ds->readUInt32(inHeader->UCAConsts);
223        header.contractionUCACombos=    ds->readUInt32(inHeader->contractionUCACombos);
224        header.mappingPosition=         ds->readUInt32(inHeader->mappingPosition);
225        header.expansion=               ds->readUInt32(inHeader->expansion);
226        header.contractionIndex=        ds->readUInt32(inHeader->contractionIndex);
227        header.contractionCEs=          ds->readUInt32(inHeader->contractionCEs);
228        header.contractionSize=         ds->readUInt32(inHeader->contractionSize);
229        header.endExpansionCE=          ds->readUInt32(inHeader->endExpansionCE);
230        header.expansionCESize=         ds->readUInt32(inHeader->expansionCESize);
231        header.endExpansionCECount=     udata_readInt32(ds, inHeader->endExpansionCECount);
232        header.contractionUCACombosSize=udata_readInt32(ds, inHeader->contractionUCACombosSize);
233
234        /* swap the 32-bit integers in the header */
235        ds->swapArray32(ds, inHeader, (int32_t)((const char *)&inHeader->jamoSpecial-(const char *)inHeader),
236                           outHeader, pErrorCode);
237
238        /* set the output platform properties */
239        outHeader->isBigEndian=ds->outIsBigEndian;
240        outHeader->charSetFamily=ds->outCharset;
241
242        /* swap the options */
243        if(header.options!=0) {
244            ds->swapArray32(ds, inBytes+header.options, header.expansion-header.options,
245                               outBytes+header.options, pErrorCode);
246        }
247
248        /* swap the expansions */
249        if(header.mappingPosition!=0 && header.expansion!=0) {
250            if(header.contractionIndex!=0) {
251                /* expansions bounded by contractions */
252                count=header.contractionIndex-header.expansion;
253            } else {
254                /* no contractions: expansions bounded by the main trie */
255                count=header.mappingPosition-header.expansion;
256            }
257            ds->swapArray32(ds, inBytes+header.expansion, (int32_t)count,
258                               outBytes+header.expansion, pErrorCode);
259        }
260
261        /* swap the contractions */
262        if(header.contractionSize!=0) {
263            /* contractionIndex: UChar[] */
264            ds->swapArray16(ds, inBytes+header.contractionIndex, header.contractionSize*2,
265                               outBytes+header.contractionIndex, pErrorCode);
266
267            /* contractionCEs: CEs[] */
268            ds->swapArray32(ds, inBytes+header.contractionCEs, header.contractionSize*4,
269                               outBytes+header.contractionCEs, pErrorCode);
270        }
271
272        /* swap the main trie */
273        if(header.mappingPosition!=0) {
274            count=header.endExpansionCE-header.mappingPosition;
275            utrie_swap(ds, inBytes+header.mappingPosition, (int32_t)count,
276                          outBytes+header.mappingPosition, pErrorCode);
277        }
278
279        /* swap the max expansion table */
280        if(header.endExpansionCECount!=0) {
281            ds->swapArray32(ds, inBytes+header.endExpansionCE, header.endExpansionCECount*4,
282                               outBytes+header.endExpansionCE, pErrorCode);
283        }
284
285        /* expansionCESize, unsafeCP, contrEndCP: uint8_t[], no need to swap */
286
287        /* swap UCA constants */
288        if(header.UCAConsts!=0) {
289            /*
290             * if UCAConsts!=0 then contractionUCACombos because we are swapping
291             * the UCA data file, and we know that the UCA contains contractions
292             */
293            count=header.contractionUCACombos-header.UCAConsts;
294            ds->swapArray32(ds, inBytes+header.UCAConsts, header.contractionUCACombos-header.UCAConsts,
295                               outBytes+header.UCAConsts, pErrorCode);
296        }
297
298        /* swap UCA contractions */
299        if(header.contractionUCACombosSize!=0) {
300            count=header.contractionUCACombosSize*inHeader->contractionUCACombosWidth*U_SIZEOF_UCHAR;
301            ds->swapArray16(ds, inBytes+header.contractionUCACombos, (int32_t)count,
302                               outBytes+header.contractionUCACombos, pErrorCode);
303        }
304    }
305
306    return header.size;
307}
308
309/* swap ICU collation data like ucadata.icu */
310U_CAPI int32_t U_EXPORT2
311ucol_swap(const UDataSwapper *ds,
312          const void *inData, int32_t length, void *outData,
313          UErrorCode *pErrorCode) {
314    const UDataInfo *pInfo;
315    int32_t headerSize, collationSize;
316
317    /* udata_swapDataHeader checks the arguments */
318    headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
319    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
320        return 0;
321    }
322
323    /* check data format and format version */
324    pInfo=(const UDataInfo *)((const char *)inData+4);
325    if(!(
326        pInfo->dataFormat[0]==0x55 &&   /* dataFormat="UCol" */
327        pInfo->dataFormat[1]==0x43 &&
328        pInfo->dataFormat[2]==0x6f &&
329        pInfo->dataFormat[3]==0x6c &&
330        pInfo->formatVersion[0]==2 &&
331        pInfo->formatVersion[1]>=3
332    )) {
333        udata_printError(ds, "ucol_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a collation file\n",
334                         pInfo->dataFormat[0], pInfo->dataFormat[1],
335                         pInfo->dataFormat[2], pInfo->dataFormat[3],
336                         pInfo->formatVersion[0], pInfo->formatVersion[1]);
337        *pErrorCode=U_UNSUPPORTED_ERROR;
338        return 0;
339    }
340
341    collationSize=ucol_swapBinary(ds,
342                        (const char *)inData+headerSize,
343                        length>=0 ? length-headerSize : -1,
344                        (char *)outData+headerSize,
345                        pErrorCode);
346    if(U_SUCCESS(*pErrorCode)) {
347        return headerSize+collationSize;
348    } else {
349        return 0;
350    }
351}
352
353/* swap inverse UCA collation data (invuca.icu) */
354U_CAPI int32_t U_EXPORT2
355ucol_swapInverseUCA(const UDataSwapper *ds,
356                    const void *inData, int32_t length, void *outData,
357                    UErrorCode *pErrorCode) {
358    const UDataInfo *pInfo;
359    int32_t headerSize;
360
361    const uint8_t *inBytes;
362    uint8_t *outBytes;
363
364    const InverseUCATableHeader *inHeader;
365    InverseUCATableHeader *outHeader;
366    InverseUCATableHeader header={ 0 };
367
368    /* udata_swapDataHeader checks the arguments */
369    headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
370    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
371        return 0;
372    }
373
374    /* check data format and format version */
375    pInfo=(const UDataInfo *)((const char *)inData+4);
376    if(!(
377        pInfo->dataFormat[0]==0x49 &&   /* dataFormat="InvC" */
378        pInfo->dataFormat[1]==0x6e &&
379        pInfo->dataFormat[2]==0x76 &&
380        pInfo->dataFormat[3]==0x43 &&
381        pInfo->formatVersion[0]==2 &&
382        pInfo->formatVersion[1]>=1
383    )) {
384        udata_printError(ds, "ucol_swapInverseUCA(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not an inverse UCA collation file\n",
385                         pInfo->dataFormat[0], pInfo->dataFormat[1],
386                         pInfo->dataFormat[2], pInfo->dataFormat[3],
387                         pInfo->formatVersion[0], pInfo->formatVersion[1]);
388        *pErrorCode=U_UNSUPPORTED_ERROR;
389        return 0;
390    }
391
392    inBytes=(const uint8_t *)inData+headerSize;
393    outBytes=(uint8_t *)outData+headerSize;
394
395    inHeader=(const InverseUCATableHeader *)inBytes;
396    outHeader=(InverseUCATableHeader *)outBytes;
397
398    /*
399     * The inverse UCA collation binary must contain at least the InverseUCATableHeader,
400     * starting with its size field.
401     * sizeof(UCATableHeader)==8*4 in ICU 2.8
402     * check the length against the header size before reading the size field
403     */
404    if(length<0) {
405        header.byteSize=udata_readInt32(ds, inHeader->byteSize);
406    } else if(
407        ((length-headerSize)<(8*4) ||
408         (uint32_t)(length-headerSize)<(header.byteSize=udata_readInt32(ds, inHeader->byteSize)))
409    ) {
410        udata_printError(ds, "ucol_swapInverseUCA(): too few bytes (%d after header) for inverse UCA collation data\n",
411                         length);
412        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
413        return 0;
414    }
415
416    if(length>=0) {
417        /* copy everything, takes care of data that needs no swapping */
418        if(inBytes!=outBytes) {
419            uprv_memcpy(outBytes, inBytes, header.byteSize);
420        }
421
422        /* swap the necessary pieces in the order of their occurrence in the data */
423
424        /* read more of the InverseUCATableHeader (the byteSize field was read above) */
425        header.tableSize=   ds->readUInt32(inHeader->tableSize);
426        header.contsSize=   ds->readUInt32(inHeader->contsSize);
427        header.table=       ds->readUInt32(inHeader->table);
428        header.conts=       ds->readUInt32(inHeader->conts);
429
430        /* swap the 32-bit integers in the header */
431        ds->swapArray32(ds, inHeader, 5*4, outHeader, pErrorCode);
432
433        /* swap the inverse table; tableSize counts uint32_t[3] rows */
434        ds->swapArray32(ds, inBytes+header.table, header.tableSize*3*4,
435                           outBytes+header.table, pErrorCode);
436
437        /* swap the continuation table; contsSize counts UChars */
438        ds->swapArray16(ds, inBytes+header.conts, header.contsSize*U_SIZEOF_UCHAR,
439                           outBytes+header.conts, pErrorCode);
440    }
441
442    return headerSize+header.byteSize;
443}
444
445#endif /* #if !UCONFIG_NO_COLLATION */
446