2 *******************************************************************************
3 * Copyright (C) 1999-2014, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: uresdata.c
8 * tab size: 8 (not used)
11 * created on: 1999dec08
12 * created by: Markus W. Scherer
13 * Modification History:
15 * Date Name Description
16 * 06/20/2000 helena OS/400 port changes; mostly typecast.
17 * 06/24/02 weiv Added support for resource sharing
20 #include "unicode/utypes.h"
21 #include "unicode/udata.h"
22 #include "unicode/ustring.h"
23 #include "unicode/utf16.h"
35 * Resource access helpers
38 /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
39 #define RES_GET_KEY16(pResData, keyOffset) \
40 ((keyOffset)<(pResData)->localKeyLimit ? \
41 (const char *)(pResData)->pRoot+(keyOffset) : \
42 (pResData)->poolBundleKeys+(keyOffset)-(pResData)->localKeyLimit)
44 #define RES_GET_KEY32(pResData, keyOffset) \
46 (const char *)(pResData)->pRoot+(keyOffset) : \
47 (pResData)->poolBundleKeys+((keyOffset)&0x7fffffff))
49 #define URESDATA_ITEM_NOT_FOUND -1
51 /* empty resources, returned when the resource offset is 0 */
52 static const uint16_t gEmpty16
=0;
63 } gEmptyString
={ 0, 0, 0 };
66 * All the type-access functions assume that
67 * the resource is of the expected type.
71 _res_findTableItem(const ResourceData
*pResData
, const uint16_t *keyOffsets
, int32_t length
,
72 const char *key
, const char **realKey
) {
74 int32_t mid
, start
, limit
;
77 /* do a binary search for the key */
81 mid
= (start
+ limit
) / 2;
82 tableKey
= RES_GET_KEY16(pResData
, keyOffsets
[mid
]);
83 if (pResData
->useNativeStrcmp
) {
84 result
= uprv_strcmp(key
, tableKey
);
86 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
90 } else if (result
> 0) {
98 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
102 _res_findTable32Item(const ResourceData
*pResData
, const int32_t *keyOffsets
, int32_t length
,
103 const char *key
, const char **realKey
) {
104 const char *tableKey
;
105 int32_t mid
, start
, limit
;
108 /* do a binary search for the key */
112 mid
= (start
+ limit
) / 2;
113 tableKey
= RES_GET_KEY32(pResData
, keyOffsets
[mid
]);
114 if (pResData
->useNativeStrcmp
) {
115 result
= uprv_strcmp(key
, tableKey
);
117 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
121 } else if (result
> 0) {
129 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
132 /* helper for res_load() ---------------------------------------------------- */
134 static UBool U_CALLCONV
135 isAcceptable(void *context
,
136 const char *type
, const char *name
,
137 const UDataInfo
*pInfo
) {
138 uprv_memcpy(context
, pInfo
->formatVersion
, 4);
141 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
142 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
143 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
144 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
145 pInfo
->dataFormat
[1]==0x65 &&
146 pInfo
->dataFormat
[2]==0x73 &&
147 pInfo
->dataFormat
[3]==0x42 &&
148 (pInfo
->formatVersion
[0]==1 || pInfo
->formatVersion
[0]==2));
151 /* semi-public functions ---------------------------------------------------- */
154 res_init(ResourceData
*pResData
,
155 UVersionInfo formatVersion
, const void *inBytes
, int32_t length
,
156 UErrorCode
*errorCode
) {
159 /* get the root resource */
160 pResData
->pRoot
=(const int32_t *)inBytes
;
161 pResData
->rootRes
=(Resource
)*pResData
->pRoot
;
162 pResData
->p16BitUnits
=&gEmpty16
;
164 /* formatVersion 1.1 must have a root item and at least 5 indexes */
165 if(length
>=0 && (length
/4)<((formatVersion
[0]==1 && formatVersion
[1]==0) ? 1 : 1+5)) {
166 *errorCode
=U_INVALID_FORMAT_ERROR
;
167 res_unload(pResData
);
171 /* currently, we accept only resources that have a Table as their roots */
172 rootType
=(UResType
)RES_GET_TYPE(pResData
->rootRes
);
173 if(!URES_IS_TABLE(rootType
)) {
174 *errorCode
=U_INVALID_FORMAT_ERROR
;
175 res_unload(pResData
);
179 if(formatVersion
[0]==1 && formatVersion
[1]==0) {
180 pResData
->localKeyLimit
=0x10000; /* greater than any 16-bit key string offset */
182 /* bundles with formatVersion 1.1 and later contain an indexes[] array */
183 const int32_t *indexes
=pResData
->pRoot
+1;
184 int32_t indexLength
=indexes
[URES_INDEX_LENGTH
]&0xff;
185 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
186 *errorCode
=U_INVALID_FORMAT_ERROR
;
187 res_unload(pResData
);
191 (length
<((1+indexLength
)<<2) ||
192 length
<(indexes
[URES_INDEX_BUNDLE_TOP
]<<2))
194 *errorCode
=U_INVALID_FORMAT_ERROR
;
195 res_unload(pResData
);
198 if(indexes
[URES_INDEX_KEYS_TOP
]>(1+indexLength
)) {
199 pResData
->localKeyLimit
=indexes
[URES_INDEX_KEYS_TOP
]<<2;
201 if(indexLength
>URES_INDEX_ATTRIBUTES
) {
202 int32_t att
=indexes
[URES_INDEX_ATTRIBUTES
];
203 pResData
->noFallback
=(UBool
)(att
&URES_ATT_NO_FALLBACK
);
204 pResData
->isPoolBundle
=(UBool
)((att
&URES_ATT_IS_POOL_BUNDLE
)!=0);
205 pResData
->usesPoolBundle
=(UBool
)((att
&URES_ATT_USES_POOL_BUNDLE
)!=0);
207 if((pResData
->isPoolBundle
|| pResData
->usesPoolBundle
) && indexLength
<=URES_INDEX_POOL_CHECKSUM
) {
208 *errorCode
=U_INVALID_FORMAT_ERROR
;
209 res_unload(pResData
);
212 if( indexLength
>URES_INDEX_16BIT_TOP
&&
213 indexes
[URES_INDEX_16BIT_TOP
]>indexes
[URES_INDEX_KEYS_TOP
]
215 pResData
->p16BitUnits
=(const uint16_t *)(pResData
->pRoot
+indexes
[URES_INDEX_KEYS_TOP
]);
219 if(formatVersion
[0]==1 || U_CHARSET_FAMILY
==U_ASCII_FAMILY
) {
221 * formatVersion 1: compare key strings in native-charset order
222 * formatVersion 2 and up: compare key strings in ASCII order
224 pResData
->useNativeStrcmp
=TRUE
;
228 U_CAPI
void U_EXPORT2
229 res_read(ResourceData
*pResData
,
230 const UDataInfo
*pInfo
, const void *inBytes
, int32_t length
,
231 UErrorCode
*errorCode
) {
232 UVersionInfo formatVersion
;
234 uprv_memset(pResData
, 0, sizeof(ResourceData
));
235 if(U_FAILURE(*errorCode
)) {
238 if(!isAcceptable(formatVersion
, NULL
, NULL
, pInfo
)) {
239 *errorCode
=U_INVALID_FORMAT_ERROR
;
242 res_init(pResData
, formatVersion
, inBytes
, length
, errorCode
);
246 res_load(ResourceData
*pResData
,
247 const char *path
, const char *name
, UErrorCode
*errorCode
) {
248 UVersionInfo formatVersion
;
250 uprv_memset(pResData
, 0, sizeof(ResourceData
));
252 /* load the ResourceBundle file */
253 pResData
->data
=udata_openChoice(path
, "res", name
, isAcceptable
, formatVersion
, errorCode
);
254 if(U_FAILURE(*errorCode
)) {
258 /* get its memory and initialize *pResData */
259 res_init(pResData
, formatVersion
, udata_getMemory(pResData
->data
), -1, errorCode
);
263 res_unload(ResourceData
*pResData
) {
264 if(pResData
->data
!=NULL
) {
265 udata_close(pResData
->data
);
270 static const int8_t gPublicTypes
[URES_LIMIT
] = {
276 URES_TABLE
, /* URES_TABLE32 */
277 URES_TABLE
, /* URES_TABLE16 */
278 URES_STRING
, /* URES_STRING_V2 */
282 URES_ARRAY
, /* URES_ARRAY16 */
292 U_CAPI UResType U_EXPORT2
293 res_getPublicType(Resource res
) {
294 return (UResType
)gPublicTypes
[RES_GET_TYPE(res
)];
297 U_CAPI
const UChar
* U_EXPORT2
298 res_getString(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
300 uint32_t offset
=RES_GET_OFFSET(res
);
302 if(RES_GET_TYPE(res
)==URES_STRING_V2
) {
304 p
=(const UChar
*)(pResData
->p16BitUnits
+offset
);
306 if(!U16_IS_TRAIL(first
)) {
308 } else if(first
<0xdfef) {
311 } else if(first
<0xdfff) {
312 length
=((first
-0xdfef)<<16)|p
[1];
315 length
=((int32_t)p
[1]<<16)|p
[2];
318 } else if(res
==offset
) /* RES_GET_TYPE(res)==URES_STRING */ {
319 const int32_t *p32
= res
==0 ? &gEmptyString
.length
: pResData
->pRoot
+res
;
321 p
=(const UChar
*)p32
;
332 U_CAPI
const UChar
* U_EXPORT2
333 res_getAlias(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
335 uint32_t offset
=RES_GET_OFFSET(res
);
337 if(RES_GET_TYPE(res
)==URES_ALIAS
) {
338 const int32_t *p32
= offset
==0 ? &gEmptyString
.length
: pResData
->pRoot
+offset
;
340 p
=(const UChar
*)p32
;
351 U_CAPI
const uint8_t * U_EXPORT2
352 res_getBinary(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
354 uint32_t offset
=RES_GET_OFFSET(res
);
356 if(RES_GET_TYPE(res
)==URES_BINARY
) {
357 const int32_t *p32
= offset
==0 ? (const int32_t*)&gEmpty32
: pResData
->pRoot
+offset
;
359 p
=(const uint8_t *)p32
;
371 U_CAPI
const int32_t * U_EXPORT2
372 res_getIntVector(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
374 uint32_t offset
=RES_GET_OFFSET(res
);
376 if(RES_GET_TYPE(res
)==URES_INT_VECTOR
) {
377 p
= offset
==0 ? (const int32_t *)&gEmpty32
: pResData
->pRoot
+offset
;
389 U_CAPI
int32_t U_EXPORT2
390 res_countArrayItems(const ResourceData
*pResData
, Resource res
) {
391 uint32_t offset
=RES_GET_OFFSET(res
);
392 switch(RES_GET_TYPE(res
)) {
398 case URES_INT_VECTOR
:
402 return offset
==0 ? 0 : *(pResData
->pRoot
+offset
);
404 return offset
==0 ? 0 : *((const uint16_t *)(pResData
->pRoot
+offset
));
407 return pResData
->p16BitUnits
[offset
];
413 U_CAPI Resource U_EXPORT2
414 res_getTableItemByKey(const ResourceData
*pResData
, Resource table
,
415 int32_t *indexR
, const char **key
) {
416 uint32_t offset
=RES_GET_OFFSET(table
);
419 if(key
== NULL
|| *key
== NULL
) {
422 switch(RES_GET_TYPE(table
)) {
424 if (offset
!=0) { /* empty if offset==0 */
425 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
427 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
429 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
436 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
438 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
440 return URES_MAKE_RESOURCE(URES_STRING_V2
, p
[length
+idx
]);
445 if (offset
!=0) { /* empty if offset==0 */
446 const int32_t *p
= pResData
->pRoot
+offset
;
448 *indexR
=idx
=_res_findTable32Item(pResData
, p
, length
, *key
, key
);
450 return (Resource
)p
[length
+idx
];
461 U_CAPI Resource U_EXPORT2
462 res_getTableItemByIndex(const ResourceData
*pResData
, Resource table
,
463 int32_t indexR
, const char **key
) {
464 uint32_t offset
=RES_GET_OFFSET(table
);
466 U_ASSERT(indexR
>=0); /* to ensure the index is not negative */
467 switch(RES_GET_TYPE(table
)) {
469 if (offset
!= 0) { /* empty if offset==0 */
470 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
473 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
475 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
483 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
487 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
489 return URES_MAKE_RESOURCE(URES_STRING_V2
, p
[length
+indexR
]);
494 if (offset
!= 0) { /* empty if offset==0 */
495 const int32_t *p
= pResData
->pRoot
+offset
;
499 *key
=RES_GET_KEY32(pResData
, p
[indexR
]);
501 return (Resource
)p
[length
+indexR
];
512 U_CAPI Resource U_EXPORT2
513 res_getResource(const ResourceData
*pResData
, const char *key
) {
514 const char *realKey
=key
;
516 return res_getTableItemByKey(pResData
, pResData
->rootRes
, &idx
, &realKey
);
519 U_CAPI Resource U_EXPORT2
520 res_getArrayItem(const ResourceData
*pResData
, Resource array
, int32_t indexR
) {
521 uint32_t offset
=RES_GET_OFFSET(array
);
522 U_ASSERT(indexR
>=0); /* to ensure the index is not negative */
523 switch(RES_GET_TYPE(array
)) {
525 if (offset
!=0) { /* empty if offset==0 */
526 const int32_t *p
= pResData
->pRoot
+offset
;
528 return (Resource
)p
[1+indexR
];
534 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
536 return URES_MAKE_RESOURCE(URES_STRING_V2
, p
[1+indexR
]);
547 res_findResource(const ResourceData
*pResData
, Resource r
, char** path
, const char** key
) {
548 /* we pass in a path. CollationElements/Sequence or zoneStrings/3/2 etc.
549 * iterates over a path and stops when a scalar resource is found. This
550 * CAN be an alias. Path gets set to the part that has not yet been processed.
553 char *pathP
= *path
, *nextSepP
= *path
;
554 char *closeIndex
= NULL
;
558 UResType type
= (UResType
)RES_GET_TYPE(t1
);
560 /* if you come in with an empty path, you'll be getting back the same resource */
561 if(!uprv_strlen(pathP
)) {
565 /* one needs to have an aggregate resource in order to search in it */
566 if(!URES_IS_CONTAINER(type
)) {
570 while(nextSepP
&& *pathP
&& t1
!= RES_BOGUS
&& URES_IS_CONTAINER(type
)) {
571 /* Iteration stops if: the path has been consumed, we found a non-existing
572 * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
574 nextSepP
= uprv_strchr(pathP
, RES_PATH_SEPARATOR
);
575 /* if there are more separators, terminate string
576 * and set path to the remaining part of the string
578 if(nextSepP
!= NULL
) {
579 *nextSepP
= 0; /* overwrite the separator with a NUL to terminate the key */
582 *path
= uprv_strchr(pathP
, 0);
585 /* if the resource is a table */
586 /* try the key based access */
587 if(URES_IS_TABLE(type
)) {
589 t2
= res_getTableItemByKey(pResData
, t1
, &indexR
, key
);
590 if(t2
== RES_BOGUS
) {
591 /* if we fail to get the resource by key, maybe we got an index */
592 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
593 if(closeIndex
!= pathP
) {
594 /* if we indeed have an index, try to get the item by index */
595 t2
= res_getTableItemByIndex(pResData
, t1
, indexR
, key
);
598 } else if(URES_IS_ARRAY(type
)) {
599 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
600 if(closeIndex
!= pathP
) {
601 t2
= res_getArrayItem(pResData
, t1
, indexR
);
603 t2
= RES_BOGUS
; /* have an array, but don't have a valid index */
606 } else { /* can't do much here, except setting t2 to bogus */
610 type
= (UResType
)RES_GET_TYPE(t1
);
611 /* position pathP to next resource key/index */
618 /* resource bundle swapping ------------------------------------------------- */
621 * Need to always enumerate the entire item tree,
622 * track the lowest address of any item to use as the limit for char keys[],
623 * track the highest address of any item to return the size of the data.
625 * We should have thought of storing those in the data...
626 * It is possible to extend the data structure by putting additional values
627 * in places that are inaccessible by ordinary enumeration of the item tree.
628 * For example, additional integers could be stored at the beginning or
629 * end of the key strings; this could be indicated by a minor version number,
630 * and the data swapping would have to know about these values.
632 * The data structure does not forbid keys to be shared, so we must swap
633 * all keys once instead of each key when it is referenced.
635 * These swapping functions assume that a resource bundle always has a length
636 * that is a multiple of 4 bytes.
637 * Currently, this is trivially true because genrb writes bundle tree leaves
638 * physically first, before their branches, so that the root table with its
639 * array of resource items (uint32_t values) is always last.
642 /* definitions for table sorting ------------------------ */
645 * row of a temporary array
647 * gets platform-endian key string indexes and sorting indexes;
648 * after sorting this array by keys, the actual key/value arrays are permutated
649 * according to the sorting indexes
652 int32_t keyIndex
, sortIndex
;
656 ures_compareRows(const void *context
, const void *left
, const void *right
) {
657 const char *keyChars
=(const char *)context
;
658 return (int32_t)uprv_strcmp(keyChars
+((const Row
*)left
)->keyIndex
,
659 keyChars
+((const Row
*)right
)->keyIndex
);
662 typedef struct TempTable
{
663 const char *keyChars
;
667 int32_t localKeyLimit
;
668 uint8_t majorFormatVersion
;
672 STACK_ROW_CAPACITY
=200
675 /* The table item key string is not locally available. */
676 static const char *const gUnknownKey
="";
678 /* resource table key for collation binaries: "%%CollationBin" */
679 static const UChar gCollationBinKey
[]={
681 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
687 * swap one resource item
690 ures_swapResource(const UDataSwapper
*ds
,
691 const Resource
*inBundle
, Resource
*outBundle
,
692 Resource res
, /* caller swaps res itself */
694 TempTable
*pTempTable
,
695 UErrorCode
*pErrorCode
) {
698 int32_t offset
, count
;
700 switch(RES_GET_TYPE(res
)) {
705 /* integer, or points to 16-bit units, nothing to do here */
711 /* all other types use an offset to point to their data */
712 offset
=(int32_t)RES_GET_OFFSET(res
);
714 /* special offset indicating an empty item */
717 if(pTempTable
->resFlags
[offset
>>5]&((uint32_t)1<<(offset
&0x1f))) {
718 /* we already swapped this resource item */
721 /* mark it as swapped now */
722 pTempTable
->resFlags
[offset
>>5]|=((uint32_t)1<<(offset
&0x1f));
728 switch(RES_GET_TYPE(res
)) {
730 /* physically same value layout as string, fall through */
732 count
=udata_readInt32(ds
, (int32_t)*p
);
734 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
735 /* swap each UChar (the terminating NUL would not change) */
736 ds
->swapArray16(ds
, p
+1, 2*count
, q
+1, pErrorCode
);
739 count
=udata_readInt32(ds
, (int32_t)*p
);
741 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
742 /* no need to swap or copy bytes - ures_swap() copied them all */
744 /* swap known formats */
745 #if !UCONFIG_NO_COLLATION
746 if( key
!=NULL
&& /* the binary is in a table */
748 /* its table key string is "%%CollationBin" */
749 0==ds
->compareInvChars(ds
, key
, -1,
750 gCollationBinKey
, UPRV_LENGTHOF(gCollationBinKey
)-1) :
751 /* its table key string is unknown but it looks like a collation binary */
752 ucol_looksLikeCollationBinary(ds
, p
+1, count
))
754 ucol_swap(ds
, p
+1, count
, q
+1, pErrorCode
);
761 const uint16_t *pKey16
;
764 const int32_t *pKey32
;
770 if(RES_GET_TYPE(res
)==URES_TABLE
) {
771 /* get table item count */
772 pKey16
=(const uint16_t *)p
;
773 qKey16
=(uint16_t *)q
;
774 count
=ds
->readUInt16(*pKey16
);
779 ds
->swapArray16(ds
, pKey16
++, 2, qKey16
++, pErrorCode
);
781 offset
+=((1+count
)+1)/2;
783 /* get table item count */
784 pKey32
=(const int32_t *)p
;
786 count
=udata_readInt32(ds
, *pKey32
);
791 ds
->swapArray32(ds
, pKey32
++, 4, qKey32
++, pErrorCode
);
800 p
=inBundle
+offset
; /* pointer to table resources */
804 for(i
=0; i
<count
; ++i
) {
805 const char *itemKey
=gUnknownKey
;
807 int32_t keyOffset
=ds
->readUInt16(pKey16
[i
]);
808 if(keyOffset
<pTempTable
->localKeyLimit
) {
809 itemKey
=(const char *)outBundle
+keyOffset
;
812 int32_t keyOffset
=udata_readInt32(ds
, pKey32
[i
]);
814 itemKey
=(const char *)outBundle
+keyOffset
;
817 item
=ds
->readUInt32(p
[i
]);
818 ures_swapResource(ds
, inBundle
, outBundle
, item
, itemKey
, pTempTable
, pErrorCode
);
819 if(U_FAILURE(*pErrorCode
)) {
820 udata_printError(ds
, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
826 if(pTempTable
->majorFormatVersion
>1 || ds
->inCharset
==ds
->outCharset
) {
827 /* no need to sort, just swap the offset/value arrays */
829 ds
->swapArray16(ds
, pKey16
, count
*2, qKey16
, pErrorCode
);
830 ds
->swapArray32(ds
, p
, count
*4, q
, pErrorCode
);
832 /* swap key offsets and items as one array */
833 ds
->swapArray32(ds
, pKey32
, count
*2*4, qKey32
, pErrorCode
);
839 * We need to sort tables by outCharset key strings because they
840 * sort differently for different charset families.
841 * ures_swap() already set pTempTable->keyChars appropriately.
842 * First we set up a temporary table with the key indexes and
843 * sorting indexes and sort that.
844 * Then we permutate and copy/swap the actual values.
847 for(i
=0; i
<count
; ++i
) {
848 pTempTable
->rows
[i
].keyIndex
=ds
->readUInt16(pKey16
[i
]);
849 pTempTable
->rows
[i
].sortIndex
=i
;
852 for(i
=0; i
<count
; ++i
) {
853 pTempTable
->rows
[i
].keyIndex
=udata_readInt32(ds
, pKey32
[i
]);
854 pTempTable
->rows
[i
].sortIndex
=i
;
857 uprv_sortArray(pTempTable
->rows
, count
, sizeof(Row
),
858 ures_compareRows
, pTempTable
->keyChars
,
860 if(U_FAILURE(*pErrorCode
)) {
861 udata_printError(ds
, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
867 * copy/swap/permutate items
869 * If we swap in-place, then the permutation must use another
870 * temporary array (pTempTable->resort)
871 * before the results are copied to the outBundle.
880 rKey16
=(uint16_t *)pTempTable
->resort
;
882 for(i
=0; i
<count
; ++i
) {
883 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
884 ds
->swapArray16(ds
, pKey16
+oldIndex
, 2, rKey16
+i
, pErrorCode
);
887 uprv_memcpy(qKey16
, rKey16
, 2*count
);
895 rKey32
=pTempTable
->resort
;
897 for(i
=0; i
<count
; ++i
) {
898 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
899 ds
->swapArray32(ds
, pKey32
+oldIndex
, 4, rKey32
+i
, pErrorCode
);
902 uprv_memcpy(qKey32
, rKey32
, 4*count
);
914 r
=(Resource
*)pTempTable
->resort
;
916 for(i
=0; i
<count
; ++i
) {
917 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
918 ds
->swapArray32(ds
, p
+oldIndex
, 4, r
+i
, pErrorCode
);
921 uprv_memcpy(q
, r
, 4*count
);
931 count
=udata_readInt32(ds
, (int32_t)*p
);
933 ds
->swapArray32(ds
, p
++, 4, q
++, pErrorCode
);
936 for(i
=0; i
<count
; ++i
) {
937 item
=ds
->readUInt32(p
[i
]);
938 ures_swapResource(ds
, inBundle
, outBundle
, item
, NULL
, pTempTable
, pErrorCode
);
939 if(U_FAILURE(*pErrorCode
)) {
940 udata_printError(ds
, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n",
947 ds
->swapArray32(ds
, p
, 4*count
, q
, pErrorCode
);
950 case URES_INT_VECTOR
:
951 count
=udata_readInt32(ds
, (int32_t)*p
);
952 /* swap length and each integer */
953 ds
->swapArray32(ds
, p
, 4*(1+count
), q
, pErrorCode
);
956 /* also catches RES_BOGUS */
957 *pErrorCode
=U_UNSUPPORTED_ERROR
;
962 U_CAPI
int32_t U_EXPORT2
963 ures_swap(const UDataSwapper
*ds
,
964 const void *inData
, int32_t length
, void *outData
,
965 UErrorCode
*pErrorCode
) {
966 const UDataInfo
*pInfo
;
967 const Resource
*inBundle
;
969 int32_t headerSize
, maxTableLength
;
971 Row rows
[STACK_ROW_CAPACITY
];
972 int32_t resort
[STACK_ROW_CAPACITY
];
975 const int32_t *inIndexes
;
977 /* the following integers count Resource item offsets (4 bytes each), not bytes */
978 int32_t bundleLength
, indexLength
, keysBottom
, keysTop
, resBottom
, top
;
980 /* udata_swapDataHeader checks the arguments */
981 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
982 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
986 /* check data format and format version */
987 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
989 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
990 pInfo
->dataFormat
[1]==0x65 &&
991 pInfo
->dataFormat
[2]==0x73 &&
992 pInfo
->dataFormat
[3]==0x42 &&
993 ((pInfo
->formatVersion
[0]==1 && pInfo
->formatVersion
[1]>=1) || /* formatVersion 1.1+ or 2.x */
994 pInfo
->formatVersion
[0]==2)
996 udata_printError(ds
, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n",
997 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
998 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
999 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1000 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1003 tempTable
.majorFormatVersion
=pInfo
->formatVersion
[0];
1005 /* a resource bundle must contain at least one resource item */
1009 bundleLength
=(length
-headerSize
)/4;
1011 /* formatVersion 1.1 must have a root item and at least 5 indexes */
1012 if(bundleLength
<(1+5)) {
1013 udata_printError(ds
, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
1015 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1020 inBundle
=(const Resource
*)((const char *)inData
+headerSize
);
1021 rootRes
=ds
->readUInt32(*inBundle
);
1023 /* formatVersion 1.1 adds the indexes[] array */
1024 inIndexes
=(const int32_t *)(inBundle
+1);
1026 indexLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_LENGTH
])&0xff;
1027 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
1028 udata_printError(ds
, "ures_swap(): too few indexes for a 1.1+ resource bundle\n");
1029 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1032 keysBottom
=1+indexLength
;
1033 keysTop
=udata_readInt32(ds
, inIndexes
[URES_INDEX_KEYS_TOP
]);
1034 if(indexLength
>URES_INDEX_16BIT_TOP
) {
1035 resBottom
=udata_readInt32(ds
, inIndexes
[URES_INDEX_16BIT_TOP
]);
1039 top
=udata_readInt32(ds
, inIndexes
[URES_INDEX_BUNDLE_TOP
]);
1040 maxTableLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_MAX_TABLE_LENGTH
]);
1042 if(0<=bundleLength
&& bundleLength
<top
) {
1043 udata_printError(ds
, "ures_swap(): resource top %d exceeds bundle length %d\n",
1045 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1048 if(keysTop
>(1+indexLength
)) {
1049 tempTable
.localKeyLimit
=keysTop
<<2;
1051 tempTable
.localKeyLimit
=0;
1055 Resource
*outBundle
=(Resource
*)((char *)outData
+headerSize
);
1057 /* track which resources we have already swapped */
1058 uint32_t stackResFlags
[STACK_ROW_CAPACITY
];
1059 int32_t resFlagsLength
;
1062 * We need one bit per 4 resource bundle bytes so that we can track
1063 * every possible Resource for whether we have swapped it already.
1064 * Multiple Resource words can refer to the same bundle offsets
1065 * for sharing identical values.
1066 * We could optimize this by allocating only for locations above
1067 * where Resource values are stored (above keys & strings).
1069 resFlagsLength
=(length
+31)>>5; /* number of bytes needed */
1070 resFlagsLength
=(resFlagsLength
+3)&~3; /* multiple of 4 bytes for uint32_t */
1071 if(resFlagsLength
<=sizeof(stackResFlags
)) {
1072 tempTable
.resFlags
=stackResFlags
;
1074 tempTable
.resFlags
=(uint32_t *)uprv_malloc(resFlagsLength
);
1075 if(tempTable
.resFlags
==NULL
) {
1076 udata_printError(ds
, "ures_swap(): unable to allocate memory for tracking resources\n");
1077 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1081 uprv_memset(tempTable
.resFlags
, 0, resFlagsLength
);
1083 /* copy the bundle for binary and inaccessible data */
1084 if(inData
!=outData
) {
1085 uprv_memcpy(outBundle
, inBundle
, 4*top
);
1088 /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
1089 udata_swapInvStringBlock(ds
, inBundle
+keysBottom
, 4*(keysTop
-keysBottom
),
1090 outBundle
+keysBottom
, pErrorCode
);
1091 if(U_FAILURE(*pErrorCode
)) {
1092 udata_printError(ds
, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop
-keysBottom
));
1096 /* swap the 16-bit units (strings, table16, array16) */
1097 if(keysTop
<resBottom
) {
1098 ds
->swapArray16(ds
, inBundle
+keysTop
, (resBottom
-keysTop
)*4, outBundle
+keysTop
, pErrorCode
);
1099 if(U_FAILURE(*pErrorCode
)) {
1100 udata_printError(ds
, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom
-keysTop
));
1105 /* allocate the temporary table for sorting resource tables */
1106 tempTable
.keyChars
=(const char *)outBundle
; /* sort by outCharset */
1107 if(tempTable
.majorFormatVersion
>1 || maxTableLength
<=STACK_ROW_CAPACITY
) {
1108 tempTable
.rows
=rows
;
1109 tempTable
.resort
=resort
;
1111 tempTable
.rows
=(Row
*)uprv_malloc(maxTableLength
*sizeof(Row
)+maxTableLength
*4);
1112 if(tempTable
.rows
==NULL
) {
1113 udata_printError(ds
, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
1115 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1116 if(tempTable
.resFlags
!=stackResFlags
) {
1117 uprv_free(tempTable
.resFlags
);
1121 tempTable
.resort
=(int32_t *)(tempTable
.rows
+maxTableLength
);
1124 /* swap the resources */
1125 ures_swapResource(ds
, inBundle
, outBundle
, rootRes
, NULL
, &tempTable
, pErrorCode
);
1126 if(U_FAILURE(*pErrorCode
)) {
1127 udata_printError(ds
, "ures_swapResource(root res=%08x) failed\n",
1131 if(tempTable
.rows
!=rows
) {
1132 uprv_free(tempTable
.rows
);
1134 if(tempTable
.resFlags
!=stackResFlags
) {
1135 uprv_free(tempTable
.resFlags
);
1138 /* swap the root resource and indexes */
1139 ds
->swapArray32(ds
, inBundle
, keysBottom
*4, outBundle
, pErrorCode
);
1142 return headerSize
+4*top
;