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"
34 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
37 * Resource access helpers
40 /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
41 #define RES_GET_KEY16(pResData, keyOffset) \
42 ((keyOffset)<(pResData)->localKeyLimit ? \
43 (const char *)(pResData)->pRoot+(keyOffset) : \
44 (pResData)->poolBundleKeys+(keyOffset)-(pResData)->localKeyLimit)
46 #define RES_GET_KEY32(pResData, keyOffset) \
48 (const char *)(pResData)->pRoot+(keyOffset) : \
49 (pResData)->poolBundleKeys+((keyOffset)&0x7fffffff))
51 #define URESDATA_ITEM_NOT_FOUND -1
53 /* empty resources, returned when the resource offset is 0 */
54 static const uint16_t gEmpty16
=0;
65 } gEmptyString
={ 0, 0, 0 };
68 * All the type-access functions assume that
69 * the resource is of the expected type.
73 _res_findTableItem(const ResourceData
*pResData
, const uint16_t *keyOffsets
, int32_t length
,
74 const char *key
, const char **realKey
) {
76 int32_t mid
, start
, limit
;
79 /* do a binary search for the key */
83 mid
= (start
+ limit
) / 2;
84 tableKey
= RES_GET_KEY16(pResData
, keyOffsets
[mid
]);
85 if (pResData
->useNativeStrcmp
) {
86 result
= uprv_strcmp(key
, tableKey
);
88 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
92 } else if (result
> 0) {
100 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
104 _res_findTable32Item(const ResourceData
*pResData
, const int32_t *keyOffsets
, int32_t length
,
105 const char *key
, const char **realKey
) {
106 const char *tableKey
;
107 int32_t mid
, start
, limit
;
110 /* do a binary search for the key */
114 mid
= (start
+ limit
) / 2;
115 tableKey
= RES_GET_KEY32(pResData
, keyOffsets
[mid
]);
116 if (pResData
->useNativeStrcmp
) {
117 result
= uprv_strcmp(key
, tableKey
);
119 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
123 } else if (result
> 0) {
131 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
134 /* helper for res_load() ---------------------------------------------------- */
136 static UBool U_CALLCONV
137 isAcceptable(void *context
,
138 const char *type
, const char *name
,
139 const UDataInfo
*pInfo
) {
140 uprv_memcpy(context
, pInfo
->formatVersion
, 4);
143 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
144 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
145 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
146 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
147 pInfo
->dataFormat
[1]==0x65 &&
148 pInfo
->dataFormat
[2]==0x73 &&
149 pInfo
->dataFormat
[3]==0x42 &&
150 (pInfo
->formatVersion
[0]==1 || pInfo
->formatVersion
[0]==2));
153 /* semi-public functions ---------------------------------------------------- */
156 res_init(ResourceData
*pResData
,
157 UVersionInfo formatVersion
, const void *inBytes
, int32_t length
,
158 UErrorCode
*errorCode
) {
161 /* get the root resource */
162 pResData
->pRoot
=(const int32_t *)inBytes
;
163 pResData
->rootRes
=(Resource
)*pResData
->pRoot
;
164 pResData
->p16BitUnits
=&gEmpty16
;
166 /* formatVersion 1.1 must have a root item and at least 5 indexes */
167 if(length
>=0 && (length
/4)<((formatVersion
[0]==1 && formatVersion
[1]==0) ? 1 : 1+5)) {
168 *errorCode
=U_INVALID_FORMAT_ERROR
;
169 res_unload(pResData
);
173 /* currently, we accept only resources that have a Table as their roots */
174 rootType
=(UResType
)RES_GET_TYPE(pResData
->rootRes
);
175 if(!URES_IS_TABLE(rootType
)) {
176 *errorCode
=U_INVALID_FORMAT_ERROR
;
177 res_unload(pResData
);
181 if(formatVersion
[0]==1 && formatVersion
[1]==0) {
182 pResData
->localKeyLimit
=0x10000; /* greater than any 16-bit key string offset */
184 /* bundles with formatVersion 1.1 and later contain an indexes[] array */
185 const int32_t *indexes
=pResData
->pRoot
+1;
186 int32_t indexLength
=indexes
[URES_INDEX_LENGTH
]&0xff;
187 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
188 *errorCode
=U_INVALID_FORMAT_ERROR
;
189 res_unload(pResData
);
193 (length
<((1+indexLength
)<<2) ||
194 length
<(indexes
[URES_INDEX_BUNDLE_TOP
]<<2))
196 *errorCode
=U_INVALID_FORMAT_ERROR
;
197 res_unload(pResData
);
200 if(indexes
[URES_INDEX_KEYS_TOP
]>(1+indexLength
)) {
201 pResData
->localKeyLimit
=indexes
[URES_INDEX_KEYS_TOP
]<<2;
203 if(indexLength
>URES_INDEX_ATTRIBUTES
) {
204 int32_t att
=indexes
[URES_INDEX_ATTRIBUTES
];
205 pResData
->noFallback
=(UBool
)(att
&URES_ATT_NO_FALLBACK
);
206 pResData
->isPoolBundle
=(UBool
)((att
&URES_ATT_IS_POOL_BUNDLE
)!=0);
207 pResData
->usesPoolBundle
=(UBool
)((att
&URES_ATT_USES_POOL_BUNDLE
)!=0);
209 if((pResData
->isPoolBundle
|| pResData
->usesPoolBundle
) && indexLength
<=URES_INDEX_POOL_CHECKSUM
) {
210 *errorCode
=U_INVALID_FORMAT_ERROR
;
211 res_unload(pResData
);
214 if( indexLength
>URES_INDEX_16BIT_TOP
&&
215 indexes
[URES_INDEX_16BIT_TOP
]>indexes
[URES_INDEX_KEYS_TOP
]
217 pResData
->p16BitUnits
=(const uint16_t *)(pResData
->pRoot
+indexes
[URES_INDEX_KEYS_TOP
]);
221 if(formatVersion
[0]==1 || U_CHARSET_FAMILY
==U_ASCII_FAMILY
) {
223 * formatVersion 1: compare key strings in native-charset order
224 * formatVersion 2 and up: compare key strings in ASCII order
226 pResData
->useNativeStrcmp
=TRUE
;
230 U_CAPI
void U_EXPORT2
231 res_read(ResourceData
*pResData
,
232 const UDataInfo
*pInfo
, const void *inBytes
, int32_t length
,
233 UErrorCode
*errorCode
) {
234 UVersionInfo formatVersion
;
236 uprv_memset(pResData
, 0, sizeof(ResourceData
));
237 if(U_FAILURE(*errorCode
)) {
240 if(!isAcceptable(formatVersion
, NULL
, NULL
, pInfo
)) {
241 *errorCode
=U_INVALID_FORMAT_ERROR
;
244 res_init(pResData
, formatVersion
, inBytes
, length
, errorCode
);
248 res_load(ResourceData
*pResData
,
249 const char *path
, const char *name
, UErrorCode
*errorCode
) {
250 UVersionInfo formatVersion
;
252 uprv_memset(pResData
, 0, sizeof(ResourceData
));
254 /* load the ResourceBundle file */
255 pResData
->data
=udata_openChoice(path
, "res", name
, isAcceptable
, formatVersion
, errorCode
);
256 if(U_FAILURE(*errorCode
)) {
260 /* get its memory and initialize *pResData */
261 res_init(pResData
, formatVersion
, udata_getMemory(pResData
->data
), -1, errorCode
);
265 res_unload(ResourceData
*pResData
) {
266 if(pResData
->data
!=NULL
) {
267 udata_close(pResData
->data
);
272 static const int8_t gPublicTypes
[URES_LIMIT
] = {
278 URES_TABLE
, /* URES_TABLE32 */
279 URES_TABLE
, /* URES_TABLE16 */
280 URES_STRING
, /* URES_STRING_V2 */
284 URES_ARRAY
, /* URES_ARRAY16 */
294 U_CAPI UResType U_EXPORT2
295 res_getPublicType(Resource res
) {
296 return (UResType
)gPublicTypes
[RES_GET_TYPE(res
)];
299 U_CAPI
const UChar
* U_EXPORT2
300 res_getString(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
302 uint32_t offset
=RES_GET_OFFSET(res
);
304 if(RES_GET_TYPE(res
)==URES_STRING_V2
) {
306 p
=(const UChar
*)(pResData
->p16BitUnits
+offset
);
308 if(!U16_IS_TRAIL(first
)) {
310 } else if(first
<0xdfef) {
313 } else if(first
<0xdfff) {
314 length
=((first
-0xdfef)<<16)|p
[1];
317 length
=((int32_t)p
[1]<<16)|p
[2];
320 } else if(res
==offset
) /* RES_GET_TYPE(res)==URES_STRING */ {
321 const int32_t *p32
= res
==0 ? &gEmptyString
.length
: pResData
->pRoot
+res
;
323 p
=(const UChar
*)p32
;
334 U_CAPI
const UChar
* U_EXPORT2
335 res_getAlias(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
337 uint32_t offset
=RES_GET_OFFSET(res
);
339 if(RES_GET_TYPE(res
)==URES_ALIAS
) {
340 const int32_t *p32
= offset
==0 ? &gEmptyString
.length
: pResData
->pRoot
+offset
;
342 p
=(const UChar
*)p32
;
353 U_CAPI
const uint8_t * U_EXPORT2
354 res_getBinary(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
356 uint32_t offset
=RES_GET_OFFSET(res
);
358 if(RES_GET_TYPE(res
)==URES_BINARY
) {
359 const int32_t *p32
= offset
==0 ? (const int32_t*)&gEmpty32
: pResData
->pRoot
+offset
;
361 p
=(const uint8_t *)p32
;
373 U_CAPI
const int32_t * U_EXPORT2
374 res_getIntVector(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
376 uint32_t offset
=RES_GET_OFFSET(res
);
378 if(RES_GET_TYPE(res
)==URES_INT_VECTOR
) {
379 p
= offset
==0 ? (const int32_t *)&gEmpty32
: pResData
->pRoot
+offset
;
391 U_CAPI
int32_t U_EXPORT2
392 res_countArrayItems(const ResourceData
*pResData
, Resource res
) {
393 uint32_t offset
=RES_GET_OFFSET(res
);
394 switch(RES_GET_TYPE(res
)) {
400 case URES_INT_VECTOR
:
404 return offset
==0 ? 0 : *(pResData
->pRoot
+offset
);
406 return offset
==0 ? 0 : *((const uint16_t *)(pResData
->pRoot
+offset
));
409 return pResData
->p16BitUnits
[offset
];
415 U_CAPI Resource U_EXPORT2
416 res_getTableItemByKey(const ResourceData
*pResData
, Resource table
,
417 int32_t *indexR
, const char **key
) {
418 uint32_t offset
=RES_GET_OFFSET(table
);
421 if(key
== NULL
|| *key
== NULL
) {
424 switch(RES_GET_TYPE(table
)) {
426 if (offset
!=0) { /* empty if offset==0 */
427 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
429 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
431 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
438 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
440 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
442 return URES_MAKE_RESOURCE(URES_STRING_V2
, p
[length
+idx
]);
447 if (offset
!=0) { /* empty if offset==0 */
448 const int32_t *p
= pResData
->pRoot
+offset
;
450 *indexR
=idx
=_res_findTable32Item(pResData
, p
, length
, *key
, key
);
452 return (Resource
)p
[length
+idx
];
463 U_CAPI Resource U_EXPORT2
464 res_getTableItemByIndex(const ResourceData
*pResData
, Resource table
,
465 int32_t indexR
, const char **key
) {
466 uint32_t offset
=RES_GET_OFFSET(table
);
468 U_ASSERT(indexR
>=0); /* to ensure the index is not negative */
469 switch(RES_GET_TYPE(table
)) {
471 if (offset
!= 0) { /* empty if offset==0 */
472 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
475 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
477 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
485 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
489 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
491 return URES_MAKE_RESOURCE(URES_STRING_V2
, p
[length
+indexR
]);
496 if (offset
!= 0) { /* empty if offset==0 */
497 const int32_t *p
= pResData
->pRoot
+offset
;
501 *key
=RES_GET_KEY32(pResData
, p
[indexR
]);
503 return (Resource
)p
[length
+indexR
];
514 U_CAPI Resource U_EXPORT2
515 res_getResource(const ResourceData
*pResData
, const char *key
) {
516 const char *realKey
=key
;
518 return res_getTableItemByKey(pResData
, pResData
->rootRes
, &idx
, &realKey
);
521 U_CAPI Resource U_EXPORT2
522 res_getArrayItem(const ResourceData
*pResData
, Resource array
, int32_t indexR
) {
523 uint32_t offset
=RES_GET_OFFSET(array
);
524 U_ASSERT(indexR
>=0); /* to ensure the index is not negative */
525 switch(RES_GET_TYPE(array
)) {
527 if (offset
!=0) { /* empty if offset==0 */
528 const int32_t *p
= pResData
->pRoot
+offset
;
530 return (Resource
)p
[1+indexR
];
536 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
538 return URES_MAKE_RESOURCE(URES_STRING_V2
, p
[1+indexR
]);
549 res_findResource(const ResourceData
*pResData
, Resource r
, char** path
, const char** key
) {
550 /* we pass in a path. CollationElements/Sequence or zoneStrings/3/2 etc.
551 * iterates over a path and stops when a scalar resource is found. This
552 * CAN be an alias. Path gets set to the part that has not yet been processed.
555 char *pathP
= *path
, *nextSepP
= *path
;
556 char *closeIndex
= NULL
;
560 UResType type
= (UResType
)RES_GET_TYPE(t1
);
562 /* if you come in with an empty path, you'll be getting back the same resource */
563 if(!uprv_strlen(pathP
)) {
567 /* one needs to have an aggregate resource in order to search in it */
568 if(!URES_IS_CONTAINER(type
)) {
572 while(nextSepP
&& *pathP
&& t1
!= RES_BOGUS
&& URES_IS_CONTAINER(type
)) {
573 /* Iteration stops if: the path has been consumed, we found a non-existing
574 * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
576 nextSepP
= uprv_strchr(pathP
, RES_PATH_SEPARATOR
);
577 /* if there are more separators, terminate string
578 * and set path to the remaining part of the string
580 if(nextSepP
!= NULL
) {
581 *nextSepP
= 0; /* overwrite the separator with a NUL to terminate the key */
584 *path
= uprv_strchr(pathP
, 0);
587 /* if the resource is a table */
588 /* try the key based access */
589 if(URES_IS_TABLE(type
)) {
591 t2
= res_getTableItemByKey(pResData
, t1
, &indexR
, key
);
592 if(t2
== RES_BOGUS
) {
593 /* if we fail to get the resource by key, maybe we got an index */
594 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
595 if(closeIndex
!= pathP
) {
596 /* if we indeed have an index, try to get the item by index */
597 t2
= res_getTableItemByIndex(pResData
, t1
, indexR
, key
);
600 } else if(URES_IS_ARRAY(type
)) {
601 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
602 if(closeIndex
!= pathP
) {
603 t2
= res_getArrayItem(pResData
, t1
, indexR
);
605 t2
= RES_BOGUS
; /* have an array, but don't have a valid index */
608 } else { /* can't do much here, except setting t2 to bogus */
612 type
= (UResType
)RES_GET_TYPE(t1
);
613 /* position pathP to next resource key/index */
620 /* resource bundle swapping ------------------------------------------------- */
623 * Need to always enumerate the entire item tree,
624 * track the lowest address of any item to use as the limit for char keys[],
625 * track the highest address of any item to return the size of the data.
627 * We should have thought of storing those in the data...
628 * It is possible to extend the data structure by putting additional values
629 * in places that are inaccessible by ordinary enumeration of the item tree.
630 * For example, additional integers could be stored at the beginning or
631 * end of the key strings; this could be indicated by a minor version number,
632 * and the data swapping would have to know about these values.
634 * The data structure does not forbid keys to be shared, so we must swap
635 * all keys once instead of each key when it is referenced.
637 * These swapping functions assume that a resource bundle always has a length
638 * that is a multiple of 4 bytes.
639 * Currently, this is trivially true because genrb writes bundle tree leaves
640 * physically first, before their branches, so that the root table with its
641 * array of resource items (uint32_t values) is always last.
644 /* definitions for table sorting ------------------------ */
647 * row of a temporary array
649 * gets platform-endian key string indexes and sorting indexes;
650 * after sorting this array by keys, the actual key/value arrays are permutated
651 * according to the sorting indexes
654 int32_t keyIndex
, sortIndex
;
658 ures_compareRows(const void *context
, const void *left
, const void *right
) {
659 const char *keyChars
=(const char *)context
;
660 return (int32_t)uprv_strcmp(keyChars
+((const Row
*)left
)->keyIndex
,
661 keyChars
+((const Row
*)right
)->keyIndex
);
664 typedef struct TempTable
{
665 const char *keyChars
;
669 int32_t localKeyLimit
;
670 uint8_t majorFormatVersion
;
674 STACK_ROW_CAPACITY
=200
677 /* The table item key string is not locally available. */
678 static const char *const gUnknownKey
="";
680 /* resource table key for collation binaries: "%%CollationBin" */
681 static const UChar gCollationBinKey
[]={
683 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
689 * swap one resource item
692 ures_swapResource(const UDataSwapper
*ds
,
693 const Resource
*inBundle
, Resource
*outBundle
,
694 Resource res
, /* caller swaps res itself */
696 TempTable
*pTempTable
,
697 UErrorCode
*pErrorCode
) {
700 int32_t offset
, count
;
702 switch(RES_GET_TYPE(res
)) {
707 /* integer, or points to 16-bit units, nothing to do here */
713 /* all other types use an offset to point to their data */
714 offset
=(int32_t)RES_GET_OFFSET(res
);
716 /* special offset indicating an empty item */
719 if(pTempTable
->resFlags
[offset
>>5]&((uint32_t)1<<(offset
&0x1f))) {
720 /* we already swapped this resource item */
723 /* mark it as swapped now */
724 pTempTable
->resFlags
[offset
>>5]|=((uint32_t)1<<(offset
&0x1f));
730 switch(RES_GET_TYPE(res
)) {
732 /* physically same value layout as string, fall through */
734 count
=udata_readInt32(ds
, (int32_t)*p
);
736 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
737 /* swap each UChar (the terminating NUL would not change) */
738 ds
->swapArray16(ds
, p
+1, 2*count
, q
+1, pErrorCode
);
741 count
=udata_readInt32(ds
, (int32_t)*p
);
743 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
744 /* no need to swap or copy bytes - ures_swap() copied them all */
746 /* swap known formats */
747 #if !UCONFIG_NO_COLLATION
748 if( key
!=NULL
&& /* the binary is in a table */
750 /* its table key string is "%%CollationBin" */
751 0==ds
->compareInvChars(ds
, key
, -1,
752 gCollationBinKey
, LENGTHOF(gCollationBinKey
)-1) :
753 /* its table key string is unknown but it looks like a collation binary */
754 ucol_looksLikeCollationBinary(ds
, p
+1, count
))
756 ucol_swap(ds
, p
+1, count
, q
+1, pErrorCode
);
763 const uint16_t *pKey16
;
766 const int32_t *pKey32
;
772 if(RES_GET_TYPE(res
)==URES_TABLE
) {
773 /* get table item count */
774 pKey16
=(const uint16_t *)p
;
775 qKey16
=(uint16_t *)q
;
776 count
=ds
->readUInt16(*pKey16
);
781 ds
->swapArray16(ds
, pKey16
++, 2, qKey16
++, pErrorCode
);
783 offset
+=((1+count
)+1)/2;
785 /* get table item count */
786 pKey32
=(const int32_t *)p
;
788 count
=udata_readInt32(ds
, *pKey32
);
793 ds
->swapArray32(ds
, pKey32
++, 4, qKey32
++, pErrorCode
);
802 p
=inBundle
+offset
; /* pointer to table resources */
806 for(i
=0; i
<count
; ++i
) {
807 const char *itemKey
=gUnknownKey
;
809 int32_t keyOffset
=ds
->readUInt16(pKey16
[i
]);
810 if(keyOffset
<pTempTable
->localKeyLimit
) {
811 itemKey
=(const char *)outBundle
+keyOffset
;
814 int32_t keyOffset
=udata_readInt32(ds
, pKey32
[i
]);
816 itemKey
=(const char *)outBundle
+keyOffset
;
819 item
=ds
->readUInt32(p
[i
]);
820 ures_swapResource(ds
, inBundle
, outBundle
, item
, itemKey
, pTempTable
, pErrorCode
);
821 if(U_FAILURE(*pErrorCode
)) {
822 udata_printError(ds
, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
828 if(pTempTable
->majorFormatVersion
>1 || ds
->inCharset
==ds
->outCharset
) {
829 /* no need to sort, just swap the offset/value arrays */
831 ds
->swapArray16(ds
, pKey16
, count
*2, qKey16
, pErrorCode
);
832 ds
->swapArray32(ds
, p
, count
*4, q
, pErrorCode
);
834 /* swap key offsets and items as one array */
835 ds
->swapArray32(ds
, pKey32
, count
*2*4, qKey32
, pErrorCode
);
841 * We need to sort tables by outCharset key strings because they
842 * sort differently for different charset families.
843 * ures_swap() already set pTempTable->keyChars appropriately.
844 * First we set up a temporary table with the key indexes and
845 * sorting indexes and sort that.
846 * Then we permutate and copy/swap the actual values.
849 for(i
=0; i
<count
; ++i
) {
850 pTempTable
->rows
[i
].keyIndex
=ds
->readUInt16(pKey16
[i
]);
851 pTempTable
->rows
[i
].sortIndex
=i
;
854 for(i
=0; i
<count
; ++i
) {
855 pTempTable
->rows
[i
].keyIndex
=udata_readInt32(ds
, pKey32
[i
]);
856 pTempTable
->rows
[i
].sortIndex
=i
;
859 uprv_sortArray(pTempTable
->rows
, count
, sizeof(Row
),
860 ures_compareRows
, pTempTable
->keyChars
,
862 if(U_FAILURE(*pErrorCode
)) {
863 udata_printError(ds
, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
869 * copy/swap/permutate items
871 * If we swap in-place, then the permutation must use another
872 * temporary array (pTempTable->resort)
873 * before the results are copied to the outBundle.
882 rKey16
=(uint16_t *)pTempTable
->resort
;
884 for(i
=0; i
<count
; ++i
) {
885 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
886 ds
->swapArray16(ds
, pKey16
+oldIndex
, 2, rKey16
+i
, pErrorCode
);
889 uprv_memcpy(qKey16
, rKey16
, 2*count
);
897 rKey32
=pTempTable
->resort
;
899 for(i
=0; i
<count
; ++i
) {
900 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
901 ds
->swapArray32(ds
, pKey32
+oldIndex
, 4, rKey32
+i
, pErrorCode
);
904 uprv_memcpy(qKey32
, rKey32
, 4*count
);
916 r
=(Resource
*)pTempTable
->resort
;
918 for(i
=0; i
<count
; ++i
) {
919 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
920 ds
->swapArray32(ds
, p
+oldIndex
, 4, r
+i
, pErrorCode
);
923 uprv_memcpy(q
, r
, 4*count
);
933 count
=udata_readInt32(ds
, (int32_t)*p
);
935 ds
->swapArray32(ds
, p
++, 4, q
++, pErrorCode
);
938 for(i
=0; i
<count
; ++i
) {
939 item
=ds
->readUInt32(p
[i
]);
940 ures_swapResource(ds
, inBundle
, outBundle
, item
, NULL
, pTempTable
, pErrorCode
);
941 if(U_FAILURE(*pErrorCode
)) {
942 udata_printError(ds
, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n",
949 ds
->swapArray32(ds
, p
, 4*count
, q
, pErrorCode
);
952 case URES_INT_VECTOR
:
953 count
=udata_readInt32(ds
, (int32_t)*p
);
954 /* swap length and each integer */
955 ds
->swapArray32(ds
, p
, 4*(1+count
), q
, pErrorCode
);
958 /* also catches RES_BOGUS */
959 *pErrorCode
=U_UNSUPPORTED_ERROR
;
964 U_CAPI
int32_t U_EXPORT2
965 ures_swap(const UDataSwapper
*ds
,
966 const void *inData
, int32_t length
, void *outData
,
967 UErrorCode
*pErrorCode
) {
968 const UDataInfo
*pInfo
;
969 const Resource
*inBundle
;
971 int32_t headerSize
, maxTableLength
;
973 Row rows
[STACK_ROW_CAPACITY
];
974 int32_t resort
[STACK_ROW_CAPACITY
];
977 const int32_t *inIndexes
;
979 /* the following integers count Resource item offsets (4 bytes each), not bytes */
980 int32_t bundleLength
, indexLength
, keysBottom
, keysTop
, resBottom
, top
;
982 /* udata_swapDataHeader checks the arguments */
983 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
984 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
988 /* check data format and format version */
989 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
991 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
992 pInfo
->dataFormat
[1]==0x65 &&
993 pInfo
->dataFormat
[2]==0x73 &&
994 pInfo
->dataFormat
[3]==0x42 &&
995 ((pInfo
->formatVersion
[0]==1 && pInfo
->formatVersion
[1]>=1) || /* formatVersion 1.1+ or 2.x */
996 pInfo
->formatVersion
[0]==2)
998 udata_printError(ds
, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n",
999 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1000 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1001 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1002 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1005 tempTable
.majorFormatVersion
=pInfo
->formatVersion
[0];
1007 /* a resource bundle must contain at least one resource item */
1011 bundleLength
=(length
-headerSize
)/4;
1013 /* formatVersion 1.1 must have a root item and at least 5 indexes */
1014 if(bundleLength
<(1+5)) {
1015 udata_printError(ds
, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
1017 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1022 inBundle
=(const Resource
*)((const char *)inData
+headerSize
);
1023 rootRes
=ds
->readUInt32(*inBundle
);
1025 /* formatVersion 1.1 adds the indexes[] array */
1026 inIndexes
=(const int32_t *)(inBundle
+1);
1028 indexLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_LENGTH
])&0xff;
1029 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
1030 udata_printError(ds
, "ures_swap(): too few indexes for a 1.1+ resource bundle\n");
1031 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1034 keysBottom
=1+indexLength
;
1035 keysTop
=udata_readInt32(ds
, inIndexes
[URES_INDEX_KEYS_TOP
]);
1036 if(indexLength
>URES_INDEX_16BIT_TOP
) {
1037 resBottom
=udata_readInt32(ds
, inIndexes
[URES_INDEX_16BIT_TOP
]);
1041 top
=udata_readInt32(ds
, inIndexes
[URES_INDEX_BUNDLE_TOP
]);
1042 maxTableLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_MAX_TABLE_LENGTH
]);
1044 if(0<=bundleLength
&& bundleLength
<top
) {
1045 udata_printError(ds
, "ures_swap(): resource top %d exceeds bundle length %d\n",
1047 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1050 if(keysTop
>(1+indexLength
)) {
1051 tempTable
.localKeyLimit
=keysTop
<<2;
1053 tempTable
.localKeyLimit
=0;
1057 Resource
*outBundle
=(Resource
*)((char *)outData
+headerSize
);
1059 /* track which resources we have already swapped */
1060 uint32_t stackResFlags
[STACK_ROW_CAPACITY
];
1061 int32_t resFlagsLength
;
1064 * We need one bit per 4 resource bundle bytes so that we can track
1065 * every possible Resource for whether we have swapped it already.
1066 * Multiple Resource words can refer to the same bundle offsets
1067 * for sharing identical values.
1068 * We could optimize this by allocating only for locations above
1069 * where Resource values are stored (above keys & strings).
1071 resFlagsLength
=(length
+31)>>5; /* number of bytes needed */
1072 resFlagsLength
=(resFlagsLength
+3)&~3; /* multiple of 4 bytes for uint32_t */
1073 if(resFlagsLength
<=sizeof(stackResFlags
)) {
1074 tempTable
.resFlags
=stackResFlags
;
1076 tempTable
.resFlags
=(uint32_t *)uprv_malloc(resFlagsLength
);
1077 if(tempTable
.resFlags
==NULL
) {
1078 udata_printError(ds
, "ures_swap(): unable to allocate memory for tracking resources\n");
1079 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1083 uprv_memset(tempTable
.resFlags
, 0, resFlagsLength
);
1085 /* copy the bundle for binary and inaccessible data */
1086 if(inData
!=outData
) {
1087 uprv_memcpy(outBundle
, inBundle
, 4*top
);
1090 /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
1091 udata_swapInvStringBlock(ds
, inBundle
+keysBottom
, 4*(keysTop
-keysBottom
),
1092 outBundle
+keysBottom
, pErrorCode
);
1093 if(U_FAILURE(*pErrorCode
)) {
1094 udata_printError(ds
, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop
-keysBottom
));
1098 /* swap the 16-bit units (strings, table16, array16) */
1099 if(keysTop
<resBottom
) {
1100 ds
->swapArray16(ds
, inBundle
+keysTop
, (resBottom
-keysTop
)*4, outBundle
+keysTop
, pErrorCode
);
1101 if(U_FAILURE(*pErrorCode
)) {
1102 udata_printError(ds
, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom
-keysTop
));
1107 /* allocate the temporary table for sorting resource tables */
1108 tempTable
.keyChars
=(const char *)outBundle
; /* sort by outCharset */
1109 if(tempTable
.majorFormatVersion
>1 || maxTableLength
<=STACK_ROW_CAPACITY
) {
1110 tempTable
.rows
=rows
;
1111 tempTable
.resort
=resort
;
1113 tempTable
.rows
=(Row
*)uprv_malloc(maxTableLength
*sizeof(Row
)+maxTableLength
*4);
1114 if(tempTable
.rows
==NULL
) {
1115 udata_printError(ds
, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
1117 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1118 if(tempTable
.resFlags
!=stackResFlags
) {
1119 uprv_free(tempTable
.resFlags
);
1123 tempTable
.resort
=(int32_t *)(tempTable
.rows
+maxTableLength
);
1126 /* swap the resources */
1127 ures_swapResource(ds
, inBundle
, outBundle
, rootRes
, NULL
, &tempTable
, pErrorCode
);
1128 if(U_FAILURE(*pErrorCode
)) {
1129 udata_printError(ds
, "ures_swapResource(root res=%08x) failed\n",
1133 if(tempTable
.rows
!=rows
) {
1134 uprv_free(tempTable
.rows
);
1136 if(tempTable
.resFlags
!=stackResFlags
) {
1137 uprv_free(tempTable
.resFlags
);
1140 /* swap the root resource and indexes */
1141 ds
->swapArray32(ds
, inBundle
, keysBottom
*4, outBundle
, pErrorCode
);
1144 return headerSize
+4*top
;