2 *******************************************************************************
4 * Copyright (C) 1999-2006, International Business Machines Corporation *
5 * and others. All Rights Reserved. *
7 *******************************************************************************
8 * file name: uresdata.c
10 * tab size: 8 (not used)
13 * created on: 1999dec08
14 * created by: Markus W. Scherer
15 * Modification History:
17 * Date Name Description
18 * 06/20/2000 helena OS/400 port changes; mostly typecast.
19 * 06/24/02 weiv Added support for resource sharing
22 #include "unicode/utypes.h"
23 #include "unicode/udata.h"
32 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
35 * Resource access helpers
38 /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
39 #define RES_GET_KEY(pRoot, keyOffset) ((const char *)(pRoot)+(keyOffset))
40 #define URESDATA_ITEM_NOT_FOUND -1
43 * All the type-access functions assume that
44 * the resource is of the expected type.
52 _res_getArrayItem(Resource
*pRoot
, Resource res
, int32_t indexR
) {
53 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pRoot
, res
);
55 return ((const Resource
*)(p
))[1+indexR
];
57 return RES_BOGUS
; /* indexR>itemCount */
64 * Important: the key offsets are 16-bit byte offsets from pRoot,
65 * and the itemCount is one more 16-bit, too.
66 * Thus, there are (count+1) uint16_t values.
67 * In order to 4-align the Resource item values, there is a padding
68 * word if count is even, i.e., there is exactly (~count&1)
69 * 16-bit padding words.
71 * For Table32, both the count and the key offsets are int32_t's
72 * and need not alignment.
75 _res_getTableKey(const Resource
*pRoot
, const Resource res
, int32_t indexS
) {
76 const uint16_t *p
=(const uint16_t *)RES_GET_POINTER(pRoot
, res
);
77 if((uint32_t)indexS
<(uint32_t)*p
) {
78 return RES_GET_KEY(pRoot
, p
[indexS
+1]);
80 return NULL
; /* indexS>itemCount */
85 _res_getTable32Key(const Resource
*pRoot
, const Resource res
, int32_t indexS
) {
86 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pRoot
, res
);
87 if((uint32_t)indexS
<(uint32_t)*p
) {
88 return RES_GET_KEY(pRoot
, p
[indexS
+1]);
90 return NULL
; /* indexS>itemCount */
96 _res_getTableItem(const Resource
*pRoot
, const Resource res
, int32_t indexR
) {
97 const uint16_t *p
=(const uint16_t *)RES_GET_POINTER(pRoot
, res
);
99 if((uint32_t)indexR
<(uint32_t)count
) {
100 return ((const Resource
*)(p
+1+count
+(~count
&1)))[indexR
];
102 return RES_BOGUS
; /* indexR>itemCount */
107 _res_getTable32Item(const Resource
*pRoot
, const Resource res
, int32_t indexR
) {
108 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pRoot
, res
);
110 if((uint32_t)indexR
<(uint32_t)count
) {
111 return ((const Resource
*)(p
+1+count
))[indexR
];
113 return RES_BOGUS
; /* indexR>itemCount */
119 _res_findTableItem(const Resource
*pRoot
, const Resource res
, const char *key
,
120 int32_t *index
, const char **realKey
) {
121 const uint16_t *p
=(const uint16_t *)RES_GET_POINTER(pRoot
, res
);
122 uint32_t mid
, start
, limit
;
126 limit
=*p
++; /* number of entries */
129 /* do a binary search for the key */
131 lastMid
= UINT32_MAX
;
133 mid
= (uint32_t)((start
+ limit
) / 2);
134 if (lastMid
== mid
) { /* Have we moved? */
135 break; /* We haven't moved, and it wasn't found. */
138 result
= uprv_strcmp(key
, RES_GET_KEY(pRoot
, p
[mid
]));
142 } else if (result
> 0) {
147 *realKey
=RES_GET_KEY(pRoot
, p
[mid
]);
148 limit
=*(p
-1); /* itemCount */
149 return ((const Resource
*)(p
+limit
+(~limit
&1)))[mid
];
154 *index
=URESDATA_ITEM_NOT_FOUND
;
155 return RES_BOGUS
; /* not found or table is empty. */
159 _res_findTable32Item(const Resource
*pRoot
, const Resource res
, const char *key
,
160 int32_t *index
, const char **realKey
) {
161 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pRoot
, res
);
162 int32_t mid
, start
, limit
;
166 limit
=*p
++; /* number of entries */
169 /* do a binary search for the key */
173 mid
= (uint32_t)((start
+ limit
) / 2);
174 if (lastMid
== mid
) { /* Have we moved? */
175 break; /* We haven't moved, and it wasn't found. */
178 result
= uprv_strcmp(key
, RES_GET_KEY(pRoot
, p
[mid
]));
182 } else if (result
> 0) {
187 *realKey
=RES_GET_KEY(pRoot
, p
[mid
]);
188 return ((const Resource
*)(p
+(*(p
-1))))[mid
];
193 *index
=URESDATA_ITEM_NOT_FOUND
;
194 return RES_BOGUS
; /* not found or table is empty. */
197 /* helper for res_load() ---------------------------------------------------- */
199 static UBool U_CALLCONV
200 isAcceptable(void *context
,
201 const char *type
, const char *name
,
202 const UDataInfo
*pInfo
) {
203 uprv_memcpy(context
, pInfo
->formatVersion
, 4);
206 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
207 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
208 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
209 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
210 pInfo
->dataFormat
[1]==0x65 &&
211 pInfo
->dataFormat
[2]==0x73 &&
212 pInfo
->dataFormat
[3]==0x42 &&
213 pInfo
->formatVersion
[0]==1);
216 /* semi-public functions ---------------------------------------------------- */
219 res_load(ResourceData
*pResData
,
220 const char *path
, const char *name
, UErrorCode
*errorCode
) {
221 UVersionInfo formatVersion
;
224 /* load the ResourceBundle file */
225 pResData
->data
=udata_openChoice(path
, "res", name
, isAcceptable
, formatVersion
, errorCode
);
226 if(U_FAILURE(*errorCode
)) {
230 /* get its memory and root resource */
231 pResData
->pRoot
=(Resource
*)udata_getMemory(pResData
->data
);
232 pResData
->rootRes
=*pResData
->pRoot
;
233 pResData
->noFallback
=FALSE
;
235 /* currently, we accept only resources that have a Table as their roots */
236 rootType
=RES_GET_TYPE(pResData
->rootRes
);
237 if(rootType
!=URES_TABLE
&& rootType
!=URES_TABLE32
) {
238 *errorCode
=U_INVALID_FORMAT_ERROR
;
239 udata_close(pResData
->data
);
244 if(formatVersion
[0]>1 || (formatVersion
[0]==1 && formatVersion
[1]>=1)) {
245 /* bundles with formatVersion 1.1 and later contain an indexes[] array */
246 const int32_t *indexes
=(const int32_t *)pResData
->pRoot
+1;
247 if(indexes
[URES_INDEX_LENGTH
]>URES_INDEX_ATTRIBUTES
) {
248 pResData
->noFallback
=(UBool
)(indexes
[URES_INDEX_ATTRIBUTES
]&URES_ATT_NO_FALLBACK
);
256 res_unload(ResourceData
*pResData
) {
257 if(pResData
->data
!=NULL
) {
258 udata_close(pResData
->data
);
263 U_CFUNC
const UChar
*
264 res_getString(const ResourceData
*pResData
, const Resource res
, int32_t *pLength
) {
266 * The data structure is documented as supporting res==0 for empty strings.
267 * Return a fixed pointer in such a case.
268 * This was dropped in uresdata.c 1.17 as part of Jitterbug 1005 work
269 * on code coverage for ICU 2.0.
270 * Re-added for consistency with the design and with other code.
272 static const int32_t emptyString
[2]={ 0, 0 };
273 if(res
!=RES_BOGUS
&& RES_GET_TYPE(res
)==URES_STRING
) {
274 const int32_t *p
= res
==0 ? emptyString
: (const int32_t *)RES_GET_POINTER(pResData
->pRoot
, res
);
278 return (const UChar
*)++p
;
287 U_CFUNC
const UChar
*
288 res_getAlias(const ResourceData
*pResData
, const Resource res
, int32_t *pLength
) {
289 if(res
!=RES_BOGUS
&& RES_GET_TYPE(res
)==URES_ALIAS
) {
290 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pResData
->pRoot
, res
);
294 return (const UChar
*)++p
;
303 U_CFUNC
const uint8_t *
304 res_getBinary(const ResourceData
*pResData
, const Resource res
, int32_t *pLength
) {
306 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pResData
->pRoot
, res
);
311 return (const uint8_t *)p
;
319 U_CFUNC
const int32_t *
320 res_getIntVector(const ResourceData
*pResData
, const Resource res
, int32_t *pLength
) {
321 if(res
!=RES_BOGUS
&& RES_GET_TYPE(res
)==URES_INT_VECTOR
) {
322 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pResData
->pRoot
, res
);
327 return (const int32_t *)p
;
335 res_countArrayItems(const ResourceData
*pResData
, const Resource res
) {
337 switch(RES_GET_TYPE(res
)) {
342 case URES_INT_VECTOR
:
346 const int32_t *p
=(const int32_t *)RES_GET_POINTER(pResData
->pRoot
, res
);
350 const uint16_t *p
=(const uint16_t *)RES_GET_POINTER(pResData
->pRoot
, res
);
361 res_getResource(const ResourceData
*pResData
, const char *key
) {
364 if(RES_GET_TYPE(pResData
->rootRes
)==URES_TABLE
) {
365 return _res_findTableItem(pResData
->pRoot
, pResData
->rootRes
, key
, &index
, &realKey
);
367 return _res_findTable32Item(pResData
->pRoot
, pResData
->rootRes
, key
, &index
, &realKey
);
372 res_getArrayItem(const ResourceData
*pResData
, Resource array
, const int32_t indexR
) {
373 return _res_getArrayItem(pResData
->pRoot
, array
, indexR
);
377 res_findResource(const ResourceData
*pResData
, Resource r
, char** path
, const char** key
) {
378 /* we pass in a path. CollationElements/Sequence or zoneStrings/3/2 etc.
379 * iterates over a path and stops when a scalar resource is found. This
380 * CAN be an alias. Path gets set to the part that has not yet been processed.
383 char *pathP
= *path
, *nextSepP
= *path
;
384 char *closeIndex
= NULL
;
388 UResType type
= RES_GET_TYPE(t1
);
390 /* if you come in with an empty path, you'll be getting back the same resource */
391 if(!uprv_strlen(pathP
)) {
395 /* one needs to have an aggregate resource in order to search in it */
396 if(!(type
== URES_TABLE
|| type
== URES_TABLE32
|| type
== URES_ARRAY
)) {
400 while(nextSepP
&& *pathP
&& t1
!= RES_BOGUS
&&
401 (type
== URES_TABLE
|| type
== URES_TABLE32
|| type
== URES_ARRAY
)
403 /* Iteration stops if: the path has been consumed, we found a non-existing
404 * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
406 nextSepP
= uprv_strchr(pathP
, RES_PATH_SEPARATOR
);
407 /* if there are more separators, terminate string
408 * and set path to the remaining part of the string
410 if(nextSepP
!= NULL
) {
411 *nextSepP
= 0; /* overwrite the separator with a NUL to terminate the key */
414 *path
= uprv_strchr(pathP
, 0);
417 /* if the resource is a table */
418 /* try the key based access */
419 if(type
== URES_TABLE
) {
420 t2
= _res_findTableItem(pResData
->pRoot
, t1
, pathP
, &indexR
, key
);
421 if(t2
== RES_BOGUS
) {
422 /* if we fail to get the resource by key, maybe we got an index */
423 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
424 if(closeIndex
!= pathP
) {
425 /* if we indeed have an index, try to get the item by index */
426 t2
= res_getTableItemByIndex(pResData
, t1
, indexR
, key
);
429 } else if(type
== URES_TABLE32
) {
430 t2
= _res_findTable32Item(pResData
->pRoot
, t1
, pathP
, &indexR
, key
);
431 if(t2
== RES_BOGUS
) {
432 /* if we fail to get the resource by key, maybe we got an index */
433 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
434 if(closeIndex
!= pathP
) {
435 /* if we indeed have an index, try to get the item by index */
436 t2
= res_getTableItemByIndex(pResData
, t1
, indexR
, key
);
439 } else if(type
== URES_ARRAY
) {
440 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
441 if(closeIndex
!= pathP
) {
442 t2
= _res_getArrayItem(pResData
->pRoot
, t1
, indexR
);
444 t2
= RES_BOGUS
; /* have an array, but don't have a valid index */
447 } else { /* can't do much here, except setting t2 to bogus */
451 type
= RES_GET_TYPE(t1
);
452 /* position pathP to next resource key/index */
460 res_getTableItemByKey(const ResourceData
*pResData
, Resource table
,
461 int32_t *indexR
, const char **key
){
462 if(key
!= NULL
&& *key
!= NULL
) {
463 if(RES_GET_TYPE(table
)==URES_TABLE
) {
464 return _res_findTableItem(pResData
->pRoot
, table
, *key
, indexR
, key
);
466 return _res_findTable32Item(pResData
->pRoot
, table
, *key
, indexR
, key
);
474 res_getTableItemByIndex(const ResourceData
*pResData
, Resource table
,
475 int32_t indexR
, const char **key
) {
477 if(RES_GET_TYPE(table
)==URES_TABLE
) {
479 *key
= _res_getTableKey(pResData
->pRoot
, table
, indexR
);
481 return _res_getTableItem(pResData
->pRoot
, table
, indexR
);
484 *key
= _res_getTable32Key(pResData
->pRoot
, table
, indexR
);
486 return _res_getTable32Item(pResData
->pRoot
, table
, indexR
);
493 /* resource bundle swapping ------------------------------------------------- */
496 * Need to always enumerate the entire item tree,
497 * track the lowest address of any item to use as the limit for char keys[],
498 * track the highest address of any item to return the size of the data.
500 * We should have thought of storing those in the data...
501 * It is possible to extend the data structure by putting additional values
502 * in places that are inaccessible by ordinary enumeration of the item tree.
503 * For example, additional integers could be stored at the beginning or
504 * end of the key strings; this could be indicated by a minor version number,
505 * and the data swapping would have to know about these values.
507 * The data structure does not forbid keys to be shared, so we must swap
508 * all keys once instead of each key when it is referenced.
510 * These swapping functions assume that a resource bundle always has a length
511 * that is a multiple of 4 bytes.
512 * Currently, this is trivially true because genrb writes bundle tree leaves
513 * physically first, before their branches, so that the root table with its
514 * array of resource items (uint32_t values) is always last.
517 /* definitions for table sorting ------------------------ */
520 * row of a temporary array
522 * gets platform-endian key string indexes and sorting indexes;
523 * after sorting this array by keys, the actual key/value arrays are permutated
524 * according to the sorting indexes
527 int32_t keyIndex
, sortIndex
;
531 ures_compareRows(const void *context
, const void *left
, const void *right
) {
532 const char *keyChars
=(const char *)context
;
533 return (int32_t)uprv_strcmp(keyChars
+((const Row
*)left
)->keyIndex
,
534 keyChars
+((const Row
*)right
)->keyIndex
);
537 typedef struct TempTable
{
538 const char *keyChars
;
544 STACK_ROW_CAPACITY
=200
547 /* binary data with known formats is swapped too */
548 typedef enum UResSpecialType
{
549 URES_NO_SPECIAL_TYPE
,
550 URES_COLLATION_BINARY
,
551 URES_SPECIAL_TYPE_COUNT
554 /* resource table key for collation binaries: "%%CollationBin" */
555 static const UChar gCollationBinKey
[]={
557 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
563 * preflight one resource item and set bottom and top values;
564 * length, bottom, and top count Resource item offsets (4 bytes each), not bytes
567 ures_preflightResource(const UDataSwapper
*ds
,
568 const Resource
*inBundle
, int32_t length
,
570 int32_t *pBottom
, int32_t *pTop
, int32_t *pMaxTableLength
,
571 UErrorCode
*pErrorCode
) {
575 if(res
==0 || RES_GET_TYPE(res
)==URES_INT
) {
576 /* empty string or integer, nothing to do */
580 /* all other types use an offset to point to their data */
581 offset
=(int32_t)RES_GET_OFFSET(res
);
582 if(0<=length
&& length
<=offset
) {
583 udata_printError(ds
, "ures_preflightResource(res=%08x) resource offset exceeds bundle length %d\n",
585 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
587 } else if(offset
<*pBottom
) {
592 switch(RES_GET_TYPE(res
)) {
594 /* physically same value layout as string, fall through */
596 /* top=offset+1+(string length +1)/2 rounded up */
597 offset
+=1+((udata_readInt32(ds
, (int32_t)*p
)+1)+1)/2;
600 /* top=offset+1+(binary length)/4 rounded up */
601 offset
+=1+(udata_readInt32(ds
, (int32_t)*p
)+3)/4;
609 if(RES_GET_TYPE(res
)==URES_TABLE
) {
610 /* get table item count */
611 const uint16_t *pKey16
=(const uint16_t *)p
;
612 count
=ds
->readUInt16(*pKey16
++);
614 /* top=((1+ table item count)/2 rounded up)+(table item count) */
615 offset
+=((1+count
)+1)/2;
617 /* get table item count */
618 const int32_t *pKey32
=(const int32_t *)p
;
619 count
=udata_readInt32(ds
, *pKey32
++);
621 /* top=(1+ table item count)+(table item count) */
625 if(count
>*pMaxTableLength
) {
626 *pMaxTableLength
=count
;
629 p
=inBundle
+offset
; /* pointer to table resources */
634 for(i
=0; i
<count
; ++i
) {
635 item
=ds
->readUInt32(*p
++);
636 ures_preflightResource(ds
, inBundle
, length
, item
,
637 pBottom
, pTop
, pMaxTableLength
,
639 if(U_FAILURE(*pErrorCode
)) {
640 udata_printError(ds
, "ures_preflightResource(table res=%08x)[%d].recurse(%08x) failed\n",
653 /* top=offset+1+(array length) */
654 count
=udata_readInt32(ds
, (int32_t)*p
++);
659 for(i
=0; i
<count
; ++i
) {
660 item
=ds
->readUInt32(*p
++);
661 ures_preflightResource(ds
, inBundle
, length
, item
,
662 pBottom
, pTop
, pMaxTableLength
,
664 if(U_FAILURE(*pErrorCode
)) {
665 udata_printError(ds
, "ures_preflightResource(array res=%08x)[%d].recurse(%08x) failed\n",
673 case URES_INT_VECTOR
:
674 /* top=offset+1+(vector length) */
675 offset
+=1+udata_readInt32(ds
, (int32_t)*p
);
678 /* also catches RES_BOGUS */
679 udata_printError(ds
, "ures_preflightResource(res=%08x) unknown resource type\n", res
);
680 *pErrorCode
=U_UNSUPPORTED_ERROR
;
684 if(U_FAILURE(*pErrorCode
)) {
686 } else if(0<=length
&& length
<offset
) {
687 udata_printError(ds
, "ures_preflightResource(res=%08x) resource limit exceeds bundle length %d\n",
689 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
690 } else if(offset
>*pTop
) {
696 * swap one resource item
697 * since preflighting succeeded, we need not check offsets against length any more
700 ures_swapResource(const UDataSwapper
*ds
,
701 const Resource
*inBundle
, Resource
*outBundle
,
702 Resource res
, /* caller swaps res itself */
703 UResSpecialType specialType
,
704 TempTable
*pTempTable
,
705 UErrorCode
*pErrorCode
) {
708 int32_t offset
, count
;
710 if(res
==0 || RES_GET_TYPE(res
)==URES_INT
) {
711 /* empty string or integer, nothing to do */
715 /* all other types use an offset to point to their data */
716 offset
=(int32_t)RES_GET_OFFSET(res
);
720 switch(RES_GET_TYPE(res
)) {
722 /* physically same value layout as string, fall through */
724 count
=udata_readInt32(ds
, (int32_t)*p
);
726 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
727 /* swap each UChar (the terminating NUL would not change) */
728 ds
->swapArray16(ds
, p
+1, 2*count
, q
+1, pErrorCode
);
731 count
=udata_readInt32(ds
, (int32_t)*p
);
733 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
734 /* no need to swap or copy bytes - ures_swap() copied them all */
736 /* swap known formats */
737 if(specialType
==URES_COLLATION_BINARY
) {
738 #if !UCONFIG_NO_COLLATION
739 ucol_swapBinary(ds
, p
+1, count
, q
+1, pErrorCode
);
746 const uint16_t *pKey16
;
749 const int32_t *pKey32
;
755 if(RES_GET_TYPE(res
)==URES_TABLE
) {
756 /* get table item count */
757 pKey16
=(const uint16_t *)p
;
758 qKey16
=(uint16_t *)q
;
759 count
=ds
->readUInt16(*pKey16
);
764 ds
->swapArray16(ds
, pKey16
++, 2, qKey16
++, pErrorCode
);
766 offset
+=((1+count
)+1)/2;
768 /* get table item count */
769 pKey32
=(const int32_t *)p
;
771 count
=udata_readInt32(ds
, *pKey32
);
776 ds
->swapArray32(ds
, pKey32
++, 4, qKey32
++, pErrorCode
);
785 p
=inBundle
+offset
; /* pointer to table resources */
789 for(i
=0; i
<count
; ++i
) {
791 * detect a collation binary that is to be swapped via
792 * ds->compareInvChars(ds, outData+readUInt16(pKey[i]), "%%CollationBin")
795 * use some UDataSwapFn pointer from somewhere for collation swapping
796 * because the common library cannot directly call into the i18n library
798 if(0==ds
->compareInvChars(ds
,
799 ((const char *)outBundle
)+
801 ds
->readUInt16(pKey16
[i
]) :
802 udata_readInt32(ds
, pKey32
[i
])),
804 gCollationBinKey
, LENGTHOF(gCollationBinKey
)-1)
806 specialType
=URES_COLLATION_BINARY
;
808 specialType
=URES_NO_SPECIAL_TYPE
;
811 item
=ds
->readUInt32(p
[i
]);
812 ures_swapResource(ds
, inBundle
, outBundle
, item
, specialType
, pTempTable
, pErrorCode
);
813 if(U_FAILURE(*pErrorCode
)) {
814 udata_printError(ds
, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
820 if(ds
->inCharset
==ds
->outCharset
) {
821 /* no need to sort, just swap the offset/value arrays */
823 ds
->swapArray16(ds
, pKey16
, count
*2, qKey16
, pErrorCode
);
824 ds
->swapArray32(ds
, p
, count
*4, q
, pErrorCode
);
826 /* swap key offsets and items as one array */
827 ds
->swapArray32(ds
, pKey32
, count
*2*4, qKey32
, pErrorCode
);
833 * We need to sort tables by outCharset key strings because they
834 * sort differently for different charset families.
835 * ures_swap() already set pTempTable->keyChars appropriately.
836 * First we set up a temporary table with the key indexes and
837 * sorting indexes and sort that.
838 * Then we permutate and copy/swap the actual values.
841 for(i
=0; i
<count
; ++i
) {
842 pTempTable
->rows
[i
].keyIndex
=ds
->readUInt16(pKey16
[i
]);
843 pTempTable
->rows
[i
].sortIndex
=i
;
846 for(i
=0; i
<count
; ++i
) {
847 pTempTable
->rows
[i
].keyIndex
=udata_readInt32(ds
, pKey32
[i
]);
848 pTempTable
->rows
[i
].sortIndex
=i
;
851 uprv_sortArray(pTempTable
->rows
, count
, sizeof(Row
),
852 ures_compareRows
, pTempTable
->keyChars
,
854 if(U_FAILURE(*pErrorCode
)) {
855 udata_printError(ds
, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
861 * copy/swap/permutate items
863 * If we swap in-place, then the permutation must use another
864 * temporary array (pTempTable->resort)
865 * before the results are copied to the outBundle.
874 rKey16
=(uint16_t *)pTempTable
->resort
;
876 for(i
=0; i
<count
; ++i
) {
877 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
878 ds
->swapArray16(ds
, pKey16
+oldIndex
, 2, rKey16
+i
, pErrorCode
);
881 uprv_memcpy(qKey16
, rKey16
, 2*count
);
889 rKey32
=pTempTable
->resort
;
891 for(i
=0; i
<count
; ++i
) {
892 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
893 ds
->swapArray32(ds
, pKey32
+oldIndex
, 4, rKey32
+i
, pErrorCode
);
896 uprv_memcpy(qKey32
, rKey32
, 4*count
);
908 r
=(Resource
*)pTempTable
->resort
;
910 for(i
=0; i
<count
; ++i
) {
911 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
912 ds
->swapArray32(ds
, p
+oldIndex
, 4, r
+i
, pErrorCode
);
915 uprv_memcpy(q
, r
, 4*count
);
925 count
=udata_readInt32(ds
, (int32_t)*p
);
927 ds
->swapArray32(ds
, p
++, 4, q
++, pErrorCode
);
930 for(i
=0; i
<count
; ++i
) {
931 item
=ds
->readUInt32(p
[i
]);
932 ures_swapResource(ds
, inBundle
, outBundle
, item
, URES_NO_SPECIAL_TYPE
, pTempTable
, pErrorCode
);
933 if(U_FAILURE(*pErrorCode
)) {
934 udata_printError(ds
, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n",
941 ds
->swapArray32(ds
, p
, 4*count
, q
, pErrorCode
);
944 case URES_INT_VECTOR
:
945 count
=udata_readInt32(ds
, (int32_t)*p
);
946 /* swap length and each integer */
947 ds
->swapArray32(ds
, p
, 4*(1+count
), q
, pErrorCode
);
950 /* also catches RES_BOGUS */
951 *pErrorCode
=U_UNSUPPORTED_ERROR
;
956 U_CAPI
int32_t U_EXPORT2
957 ures_swap(const UDataSwapper
*ds
,
958 const void *inData
, int32_t length
, void *outData
,
959 UErrorCode
*pErrorCode
) {
960 const UDataInfo
*pInfo
;
961 const Resource
*inBundle
;
963 int32_t headerSize
, maxTableLength
;
965 Row rows
[STACK_ROW_CAPACITY
];
966 int32_t resort
[STACK_ROW_CAPACITY
];
969 /* the following integers count Resource item offsets (4 bytes each), not bytes */
970 int32_t bundleLength
, stringsBottom
, bottom
, top
;
972 /* udata_swapDataHeader checks the arguments */
973 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
974 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
978 /* check data format and format version */
979 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
981 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
982 pInfo
->dataFormat
[1]==0x65 &&
983 pInfo
->dataFormat
[2]==0x73 &&
984 pInfo
->dataFormat
[3]==0x42 &&
985 pInfo
->formatVersion
[0]==1
987 udata_printError(ds
, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not a resource bundle\n",
988 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
989 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
990 pInfo
->formatVersion
[0]);
991 *pErrorCode
=U_UNSUPPORTED_ERROR
;
995 /* a resource bundle must contain at least one resource item */
999 bundleLength
=(length
-headerSize
)/4;
1001 /* formatVersion 1.1 must have a root item and at least 5 indexes */
1003 (pInfo
->formatVersion
[1]==0 ? 1 : 1+5)
1005 udata_printError(ds
, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
1007 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1012 inBundle
=(const Resource
*)((const char *)inData
+headerSize
);
1013 rootRes
=ds
->readUInt32(*inBundle
);
1015 if(pInfo
->formatVersion
[1]==0) {
1016 /* preflight to get the bottom, top and maxTableLength values */
1017 stringsBottom
=1; /* just past root */
1019 top
=maxTableLength
=0;
1020 ures_preflightResource(ds
, inBundle
, bundleLength
, rootRes
,
1021 &bottom
, &top
, &maxTableLength
,
1023 if(U_FAILURE(*pErrorCode
)) {
1024 udata_printError(ds
, "ures_preflightResource(root res=%08x) failed\n",
1029 /* formatVersion 1.1 adds the indexes[] array */
1030 const int32_t *inIndexes
;
1032 inIndexes
=(const int32_t *)(inBundle
+1);
1034 stringsBottom
=1+udata_readInt32(ds
, inIndexes
[URES_INDEX_LENGTH
]);
1035 bottom
=udata_readInt32(ds
, inIndexes
[URES_INDEX_STRINGS_TOP
]);
1036 top
=udata_readInt32(ds
, inIndexes
[URES_INDEX_BUNDLE_TOP
]);
1037 maxTableLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_MAX_TABLE_LENGTH
]);
1039 if(0<=bundleLength
&& bundleLength
<top
) {
1040 udata_printError(ds
, "ures_swap(): resource top %d exceeds bundle length %d\n",
1042 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1048 Resource
*outBundle
=(Resource
*)((char *)outData
+headerSize
);
1050 /* copy the bundle for binary and inaccessible data */
1051 if(inData
!=outData
) {
1052 uprv_memcpy(outBundle
, inBundle
, 4*top
);
1055 /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
1056 udata_swapInvStringBlock(ds
, inBundle
+stringsBottom
, 4*(bottom
-stringsBottom
),
1057 outBundle
+stringsBottom
, pErrorCode
);
1058 if(U_FAILURE(*pErrorCode
)) {
1059 udata_printError(ds
, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(bottom
-1));
1063 /* allocate the temporary table for sorting resource tables */
1064 tempTable
.keyChars
=(const char *)outBundle
; /* sort by outCharset */
1065 if(maxTableLength
<=STACK_ROW_CAPACITY
) {
1066 tempTable
.rows
=rows
;
1067 tempTable
.resort
=resort
;
1069 tempTable
.rows
=(Row
*)uprv_malloc(maxTableLength
*sizeof(Row
)+maxTableLength
*4);
1070 if(tempTable
.rows
==NULL
) {
1071 udata_printError(ds
, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
1073 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1076 tempTable
.resort
=(int32_t *)(tempTable
.rows
+maxTableLength
);
1079 /* swap the resources */
1080 ures_swapResource(ds
, inBundle
, outBundle
, rootRes
, URES_NO_SPECIAL_TYPE
, &tempTable
, pErrorCode
);
1081 if(U_FAILURE(*pErrorCode
)) {
1082 udata_printError(ds
, "ures_swapResource(root res=%08x) failed\n",
1086 if(tempTable
.rows
!=rows
) {
1087 uprv_free(tempTable
.rows
);
1090 /* swap the root resource and indexes */
1091 ds
->swapArray32(ds
, inBundle
, stringsBottom
*4, outBundle
, pErrorCode
);
1094 return headerSize
+4*top
;