ICU-2235 swap serialized UTries
X-SVN-Rev: 12809
This commit is contained in:
parent
8d31f3288e
commit
5709fddab7
@ -23,6 +23,7 @@
|
||||
#endif
|
||||
|
||||
#include "unicode/utypes.h"
|
||||
#include "udataswp.h"
|
||||
#include "cmemory.h"
|
||||
#include "utrie.h"
|
||||
|
||||
@ -705,7 +706,7 @@ struct UTrieHeader {
|
||||
*/
|
||||
uint32_t options;
|
||||
|
||||
/** indexLength is a multiple of 1024>>UTRIE_SHIFT */
|
||||
/** indexLength is a multiple of UTRIE_SURROGATE_BLOCK_COUNT */
|
||||
int32_t indexLength;
|
||||
|
||||
/** dataLength>=UTRIE_DATA_BLOCK_LENGTH */
|
||||
@ -905,6 +906,79 @@ utrie_unserialize(UTrie *trie, const void *data, int32_t length, UErrorCode *pEr
|
||||
}
|
||||
}
|
||||
|
||||
/* swapping ----------------------------------------------------------------- */
|
||||
|
||||
U_CAPI int32_t U_EXPORT2
|
||||
utrie_swap(const UDataSwapper *ds,
|
||||
const void *inData, int32_t length, void *outData,
|
||||
UErrorCode *pErrorCode) {
|
||||
const UTrieHeader *inTrie;
|
||||
UTrieHeader trie;
|
||||
int32_t size;
|
||||
UBool dataIs32;
|
||||
|
||||
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
||||
return 0;
|
||||
}
|
||||
if(ds==NULL || inData==NULL || (length>=0 && outData==NULL)) {
|
||||
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* setup and swapping */
|
||||
if(length>=0 && length<sizeof(UTrieHeader)) {
|
||||
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
inTrie=(const UTrieHeader *)inData;
|
||||
trie.signature=ds->readUInt32(inTrie->signature);
|
||||
trie.options=ds->readUInt32(inTrie->options);
|
||||
trie.indexLength=udata_readInt32(ds, inTrie->indexLength);
|
||||
trie.dataLength=udata_readInt32(ds, inTrie->dataLength);
|
||||
|
||||
if( trie.signature!=0x54726965 ||
|
||||
(trie.options&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_SHIFT ||
|
||||
((trie.options>>UTRIE_OPTIONS_INDEX_SHIFT)&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_INDEX_SHIFT ||
|
||||
trie.indexLength<UTRIE_BMP_INDEX_LENGTH ||
|
||||
(trie.indexLength&(UTRIE_SURROGATE_BLOCK_COUNT-1))!=0 ||
|
||||
trie.dataLength<UTRIE_DATA_BLOCK_LENGTH ||
|
||||
(trie.dataLength&(UTRIE_DATA_GRANULARITY-1))!=0 ||
|
||||
((trie.options&UTRIE_OPTIONS_LATIN1_IS_LINEAR)!=0 && trie.dataLength<(UTRIE_DATA_BLOCK_LENGTH+0x100))
|
||||
) {
|
||||
*pErrorCode=U_INVALID_FORMAT_ERROR; /* not a UTrie */
|
||||
return 0;
|
||||
}
|
||||
|
||||
dataIs32=(UBool)((trie.options&UTRIE_OPTIONS_DATA_IS_32_BIT)!=0);
|
||||
size=sizeof(UTrieHeader)+trie.indexLength*2+trie.dataLength*(dataIs32?4:2);
|
||||
|
||||
if(length>=0) {
|
||||
UTrieHeader *outTrie;
|
||||
|
||||
if(length<size) {
|
||||
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
outTrie=(UTrieHeader *)outData;
|
||||
|
||||
/* swap the header */
|
||||
ds->swapArray32(ds, inTrie, sizeof(UTrieHeader), outTrie, pErrorCode);
|
||||
|
||||
/* swap the index and the data */
|
||||
if(dataIs32) {
|
||||
ds->swapArray16(ds, inTrie+1, trie.indexLength*2, outTrie+1, pErrorCode);
|
||||
ds->swapArray32(ds, (const uint16_t *)(inTrie+1)+trie.indexLength, trie.dataLength*4,
|
||||
(uint16_t *)(outTrie+1)+trie.indexLength, pErrorCode);
|
||||
} else {
|
||||
ds->swapArray16(ds, inTrie+1, (trie.indexLength+trie.dataLength)*2, outTrie+1, pErrorCode);
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/* enumeration -------------------------------------------------------------- */
|
||||
|
||||
/* default UTrieEnumValue() returns the input value itself */
|
||||
|
@ -80,7 +80,7 @@ enum {
|
||||
|
||||
/**
|
||||
* Number of index (stage 1) entries per lead surrogate.
|
||||
* Same as number of indexe entries for 1024 trail surrogates,
|
||||
* Same as number of index entries for 1024 trail surrogates,
|
||||
* ==0x400>>UTRIE_SHIFT
|
||||
*/
|
||||
UTRIE_SURROGATE_BLOCK_COUNT=(1<<UTRIE_SURROGATE_BLOCK_BITS),
|
||||
@ -663,6 +663,19 @@ utrie_serialize(UNewTrie *trie, void *data, int32_t capacity,
|
||||
UBool reduceTo16Bits,
|
||||
UErrorCode *pErrorCode);
|
||||
|
||||
/* forward declaration */
|
||||
struct UDataSwapper;
|
||||
typedef struct UDataSwapper UDataSwapper;
|
||||
|
||||
/**
|
||||
* Swap a serialized UTrie.
|
||||
* @internal
|
||||
*/
|
||||
U_CAPI int32_t U_EXPORT2
|
||||
utrie_swap(const UDataSwapper *ds,
|
||||
const void *inData, int32_t length, void *outData,
|
||||
UErrorCode *pErrorCode);
|
||||
|
||||
U_CDECL_END
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user