2 *******************************************************************************
3 * Copyright (C) 1999-2016, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: uresdata.cpp
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"
36 * Resource access helpers
39 /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
40 #define RES_GET_KEY16(pResData, keyOffset) \
41 ((keyOffset)<(pResData)->localKeyLimit ? \
42 (const char *)(pResData)->pRoot+(keyOffset) : \
43 (pResData)->poolBundleKeys+(keyOffset)-(pResData)->localKeyLimit)
45 #define RES_GET_KEY32(pResData, keyOffset) \
47 (const char *)(pResData)->pRoot+(keyOffset) : \
48 (pResData)->poolBundleKeys+((keyOffset)&0x7fffffff))
50 #define URESDATA_ITEM_NOT_FOUND -1
52 /* empty resources, returned when the resource offset is 0 */
53 static const uint16_t gEmpty16
=0;
64 } gEmptyString
={ 0, 0, 0 };
67 * All the type-access functions assume that
68 * the resource is of the expected type.
72 _res_findTableItem(const ResourceData
*pResData
, const uint16_t *keyOffsets
, int32_t length
,
73 const char *key
, const char **realKey
) {
75 int32_t mid
, start
, limit
;
78 /* do a binary search for the key */
82 mid
= (start
+ limit
) / 2;
83 tableKey
= RES_GET_KEY16(pResData
, keyOffsets
[mid
]);
84 if (pResData
->useNativeStrcmp
) {
85 result
= uprv_strcmp(key
, tableKey
);
87 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
91 } else if (result
> 0) {
99 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
103 _res_findTable32Item(const ResourceData
*pResData
, const int32_t *keyOffsets
, int32_t length
,
104 const char *key
, const char **realKey
) {
105 const char *tableKey
;
106 int32_t mid
, start
, limit
;
109 /* do a binary search for the key */
113 mid
= (start
+ limit
) / 2;
114 tableKey
= RES_GET_KEY32(pResData
, keyOffsets
[mid
]);
115 if (pResData
->useNativeStrcmp
) {
116 result
= uprv_strcmp(key
, tableKey
);
118 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
122 } else if (result
> 0) {
130 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
133 /* helper for res_load() ---------------------------------------------------- */
135 static UBool U_CALLCONV
136 isAcceptable(void *context
,
137 const char * /*type*/, const char * /*name*/,
138 const UDataInfo
*pInfo
) {
139 uprv_memcpy(context
, pInfo
->formatVersion
, 4);
142 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
143 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
144 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
145 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
146 pInfo
->dataFormat
[1]==0x65 &&
147 pInfo
->dataFormat
[2]==0x73 &&
148 pInfo
->dataFormat
[3]==0x42 &&
149 (1<=pInfo
->formatVersion
[0] && pInfo
->formatVersion
[0]<=3));
152 /* semi-public functions ---------------------------------------------------- */
155 res_init(ResourceData
*pResData
,
156 UVersionInfo formatVersion
, const void *inBytes
, int32_t length
,
157 UErrorCode
*errorCode
) {
160 /* get the root resource */
161 pResData
->pRoot
=(const int32_t *)inBytes
;
162 pResData
->rootRes
=(Resource
)*pResData
->pRoot
;
163 pResData
->p16BitUnits
=&gEmpty16
;
165 /* formatVersion 1.1 must have a root item and at least 5 indexes */
166 if(length
>=0 && (length
/4)<((formatVersion
[0]==1 && formatVersion
[1]==0) ? 1 : 1+5)) {
167 *errorCode
=U_INVALID_FORMAT_ERROR
;
168 res_unload(pResData
);
172 /* currently, we accept only resources that have a Table as their roots */
173 rootType
=(UResType
)RES_GET_TYPE(pResData
->rootRes
);
174 if(!URES_IS_TABLE(rootType
)) {
175 *errorCode
=U_INVALID_FORMAT_ERROR
;
176 res_unload(pResData
);
180 if(formatVersion
[0]==1 && formatVersion
[1]==0) {
181 pResData
->localKeyLimit
=0x10000; /* greater than any 16-bit key string offset */
183 /* bundles with formatVersion 1.1 and later contain an indexes[] array */
184 const int32_t *indexes
=pResData
->pRoot
+1;
185 int32_t indexLength
=indexes
[URES_INDEX_LENGTH
]&0xff;
186 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
187 *errorCode
=U_INVALID_FORMAT_ERROR
;
188 res_unload(pResData
);
192 (length
<((1+indexLength
)<<2) ||
193 length
<(indexes
[URES_INDEX_BUNDLE_TOP
]<<2))
195 *errorCode
=U_INVALID_FORMAT_ERROR
;
196 res_unload(pResData
);
199 if(indexes
[URES_INDEX_KEYS_TOP
]>(1+indexLength
)) {
200 pResData
->localKeyLimit
=indexes
[URES_INDEX_KEYS_TOP
]<<2;
202 if(formatVersion
[0]>=3) {
203 // In formatVersion 1, the indexLength took up this whole int.
204 // In version 2, bits 31..8 were reserved and always 0.
205 // In version 3, they contain bits 23..0 of the poolStringIndexLimit.
206 // Bits 27..24 are in indexes[URES_INDEX_ATTRIBUTES] bits 15..12.
207 pResData
->poolStringIndexLimit
=(int32_t)((uint32_t)indexes
[URES_INDEX_LENGTH
]>>8);
209 if(indexLength
>URES_INDEX_ATTRIBUTES
) {
210 int32_t att
=indexes
[URES_INDEX_ATTRIBUTES
];
211 pResData
->noFallback
=(UBool
)(att
&URES_ATT_NO_FALLBACK
);
212 pResData
->isPoolBundle
=(UBool
)((att
&URES_ATT_IS_POOL_BUNDLE
)!=0);
213 pResData
->usesPoolBundle
=(UBool
)((att
&URES_ATT_USES_POOL_BUNDLE
)!=0);
214 pResData
->poolStringIndexLimit
|=(att
&0xf000)<<12; // bits 15..12 -> 27..24
215 pResData
->poolStringIndex16Limit
=(int32_t)((uint32_t)att
>>16);
217 if((pResData
->isPoolBundle
|| pResData
->usesPoolBundle
) && indexLength
<=URES_INDEX_POOL_CHECKSUM
) {
218 *errorCode
=U_INVALID_FORMAT_ERROR
;
219 res_unload(pResData
);
222 if( indexLength
>URES_INDEX_16BIT_TOP
&&
223 indexes
[URES_INDEX_16BIT_TOP
]>indexes
[URES_INDEX_KEYS_TOP
]
225 pResData
->p16BitUnits
=(const uint16_t *)(pResData
->pRoot
+indexes
[URES_INDEX_KEYS_TOP
]);
229 if(formatVersion
[0]==1 || U_CHARSET_FAMILY
==U_ASCII_FAMILY
) {
231 * formatVersion 1: compare key strings in native-charset order
232 * formatVersion 2 and up: compare key strings in ASCII order
234 pResData
->useNativeStrcmp
=TRUE
;
238 U_CAPI
void U_EXPORT2
239 res_read(ResourceData
*pResData
,
240 const UDataInfo
*pInfo
, const void *inBytes
, int32_t length
,
241 UErrorCode
*errorCode
) {
242 UVersionInfo formatVersion
;
244 uprv_memset(pResData
, 0, sizeof(ResourceData
));
245 if(U_FAILURE(*errorCode
)) {
248 if(!isAcceptable(formatVersion
, NULL
, NULL
, pInfo
)) {
249 *errorCode
=U_INVALID_FORMAT_ERROR
;
252 res_init(pResData
, formatVersion
, inBytes
, length
, errorCode
);
256 res_load(ResourceData
*pResData
,
257 const char *path
, const char *name
, UErrorCode
*errorCode
) {
258 UVersionInfo formatVersion
;
260 uprv_memset(pResData
, 0, sizeof(ResourceData
));
262 /* load the ResourceBundle file */
263 pResData
->data
=udata_openChoice(path
, "res", name
, isAcceptable
, formatVersion
, errorCode
);
264 if(U_FAILURE(*errorCode
)) {
268 /* get its memory and initialize *pResData */
269 res_init(pResData
, formatVersion
, udata_getMemory(pResData
->data
), -1, errorCode
);
273 res_unload(ResourceData
*pResData
) {
274 if(pResData
->data
!=NULL
) {
275 udata_close(pResData
->data
);
280 static const int8_t gPublicTypes
[URES_LIMIT
] = {
286 URES_TABLE
, /* URES_TABLE32 */
287 URES_TABLE
, /* URES_TABLE16 */
288 URES_STRING
, /* URES_STRING_V2 */
292 URES_ARRAY
, /* URES_ARRAY16 */
302 U_CAPI UResType U_EXPORT2
303 res_getPublicType(Resource res
) {
304 return (UResType
)gPublicTypes
[RES_GET_TYPE(res
)];
307 U_CAPI
const UChar
* U_EXPORT2
308 res_getString(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
310 uint32_t offset
=RES_GET_OFFSET(res
);
312 if(RES_GET_TYPE(res
)==URES_STRING_V2
) {
314 if((int32_t)offset
<pResData
->poolStringIndexLimit
) {
315 p
=(const UChar
*)pResData
->poolBundleStrings
+offset
;
317 p
=(const UChar
*)pResData
->p16BitUnits
+(offset
-pResData
->poolStringIndexLimit
);
320 if(!U16_IS_TRAIL(first
)) {
322 } else if(first
<0xdfef) {
325 } else if(first
<0xdfff) {
326 length
=((first
-0xdfef)<<16)|p
[1];
329 length
=((int32_t)p
[1]<<16)|p
[2];
332 } else if(res
==offset
) /* RES_GET_TYPE(res)==URES_STRING */ {
333 const int32_t *p32
= res
==0 ? &gEmptyString
.length
: pResData
->pRoot
+res
;
335 p
=(const UChar
*)p32
;
349 * CLDR string value (three empty-set symbols)=={2205, 2205, 2205}
350 * prevents fallback to the parent bundle.
351 * TODO: combine with other code that handles this marker, use EMPTY_SET constant.
352 * TODO: maybe move to uresbund.cpp?
354 UBool
isNoInheritanceMarker(const ResourceData
*pResData
, Resource res
) {
355 uint32_t offset
=RES_GET_OFFSET(res
);
358 } else if (res
== offset
) {
359 const int32_t *p32
=pResData
->pRoot
+res
;
361 const UChar
*p
=(const UChar
*)p32
;
362 return length
== 3 && p
[2] == 0x2205 && p
[3] == 0x2205 && p
[4] == 0x2205;
363 } else if (RES_GET_TYPE(res
) == URES_STRING_V2
) {
365 if((int32_t)offset
<pResData
->poolStringIndexLimit
) {
366 p
=(const UChar
*)pResData
->poolBundleStrings
+offset
;
368 p
=(const UChar
*)pResData
->p16BitUnits
+(offset
-pResData
->poolStringIndexLimit
);
371 if (first
== 0x2205) { // implicit length
372 return p
[1] == 0x2205 && p
[2] == 0x2205 && p
[3] == 0;
373 } else if (first
== 0xdc03) { // explicit length 3 (should not occur)
374 return p
[1] == 0x2205 && p
[2] == 0x2205 && p
[3] == 0x2205;
376 // Assume that the string has not been stored with more length units than necessary.
385 U_CAPI
const UChar
* U_EXPORT2
386 res_getAlias(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
388 uint32_t offset
=RES_GET_OFFSET(res
);
390 if(RES_GET_TYPE(res
)==URES_ALIAS
) {
391 const int32_t *p32
= offset
==0 ? &gEmptyString
.length
: pResData
->pRoot
+offset
;
393 p
=(const UChar
*)p32
;
404 U_CAPI
const uint8_t * U_EXPORT2
405 res_getBinary(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
407 uint32_t offset
=RES_GET_OFFSET(res
);
409 if(RES_GET_TYPE(res
)==URES_BINARY
) {
410 const int32_t *p32
= offset
==0 ? (const int32_t*)&gEmpty32
: pResData
->pRoot
+offset
;
412 p
=(const uint8_t *)p32
;
424 U_CAPI
const int32_t * U_EXPORT2
425 res_getIntVector(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
427 uint32_t offset
=RES_GET_OFFSET(res
);
429 if(RES_GET_TYPE(res
)==URES_INT_VECTOR
) {
430 p
= offset
==0 ? (const int32_t *)&gEmpty32
: pResData
->pRoot
+offset
;
442 U_CAPI
int32_t U_EXPORT2
443 res_countArrayItems(const ResourceData
*pResData
, Resource res
) {
444 uint32_t offset
=RES_GET_OFFSET(res
);
445 switch(RES_GET_TYPE(res
)) {
451 case URES_INT_VECTOR
:
455 return offset
==0 ? 0 : *(pResData
->pRoot
+offset
);
457 return offset
==0 ? 0 : *((const uint16_t *)(pResData
->pRoot
+offset
));
460 return pResData
->p16BitUnits
[offset
];
468 int32_t getArrayLength(const ResourceData
*pResData
, Resource res
) {
469 uint32_t offset
=RES_GET_OFFSET(res
);
473 int32_t type
= RES_GET_TYPE(res
);
474 if(type
== URES_ARRAY
) {
475 return *(pResData
->pRoot
+offset
);
476 } else if(type
== URES_ARRAY16
) {
477 return pResData
->p16BitUnits
[offset
];
483 int32_t getTableLength(const ResourceData
*pResData
, Resource res
) {
484 uint32_t offset
=RES_GET_OFFSET(res
);
488 int32_t type
= RES_GET_TYPE(res
);
489 if(type
== URES_TABLE
) {
490 return *((const uint16_t *)(pResData
->pRoot
+offset
));
491 } else if(type
== URES_TABLE16
) {
492 return pResData
->p16BitUnits
[offset
];
493 } else if(type
== URES_TABLE32
) {
494 return *(pResData
->pRoot
+offset
);
504 ResourceDataValue::~ResourceDataValue() {}
506 UResType
ResourceDataValue::getType() const {
507 return res_getPublicType(res
);
510 const UChar
*ResourceDataValue::getString(int32_t &length
, UErrorCode
&errorCode
) const {
511 if(U_FAILURE(errorCode
)) {
514 const UChar
*s
= res_getString(pResData
, res
, &length
);
516 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
521 const UChar
*ResourceDataValue::getAliasString(int32_t &length
, UErrorCode
&errorCode
) const {
522 if(U_FAILURE(errorCode
)) {
525 const UChar
*s
= res_getAlias(pResData
, res
, &length
);
527 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
532 int32_t ResourceDataValue::getInt(UErrorCode
&errorCode
) const {
533 if(U_FAILURE(errorCode
)) {
536 if(RES_GET_TYPE(res
) != URES_INT
) {
537 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
539 return RES_GET_INT(res
);
542 uint32_t ResourceDataValue::getUInt(UErrorCode
&errorCode
) const {
543 if(U_FAILURE(errorCode
)) {
546 if(RES_GET_TYPE(res
) != URES_INT
) {
547 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
549 return RES_GET_UINT(res
);
552 const int32_t *ResourceDataValue::getIntVector(int32_t &length
, UErrorCode
&errorCode
) const {
553 if(U_FAILURE(errorCode
)) {
556 const int32_t *iv
= res_getIntVector(pResData
, res
, &length
);
558 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
563 const uint8_t *ResourceDataValue::getBinary(int32_t &length
, UErrorCode
&errorCode
) const {
564 if(U_FAILURE(errorCode
)) {
567 const uint8_t *b
= res_getBinary(pResData
, res
, &length
);
569 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
577 makeResourceFrom16(const ResourceData
*pResData
, int32_t res16
) {
578 if(res16
<pResData
->poolStringIndex16Limit
) {
579 // Pool string, nothing to do.
581 // Local string, adjust the 16-bit offset to a regular one,
582 // with a larger pool string index limit.
583 res16
=res16
-pResData
->poolStringIndex16Limit
+pResData
->poolStringIndexLimit
;
585 return URES_MAKE_RESOURCE(URES_STRING_V2
, res16
);
588 U_CAPI Resource U_EXPORT2
589 res_getTableItemByKey(const ResourceData
*pResData
, Resource table
,
590 int32_t *indexR
, const char **key
) {
591 uint32_t offset
=RES_GET_OFFSET(table
);
594 if(key
== NULL
|| *key
== NULL
) {
597 switch(RES_GET_TYPE(table
)) {
599 if (offset
!=0) { /* empty if offset==0 */
600 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
602 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
604 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
611 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
613 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
615 return makeResourceFrom16(pResData
, p
[length
+idx
]);
620 if (offset
!=0) { /* empty if offset==0 */
621 const int32_t *p
= pResData
->pRoot
+offset
;
623 *indexR
=idx
=_res_findTable32Item(pResData
, p
, length
, *key
, key
);
625 return (Resource
)p
[length
+idx
];
636 U_CAPI Resource U_EXPORT2
637 res_getTableItemByIndex(const ResourceData
*pResData
, Resource table
,
638 int32_t indexR
, const char **key
) {
639 uint32_t offset
=RES_GET_OFFSET(table
);
641 //U_ASSERT(indexR>=0); /* to ensure the index is not negative */
645 switch(RES_GET_TYPE(table
)) {
647 if (offset
!= 0) { /* empty if offset==0 */
648 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
651 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
653 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
661 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
665 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
667 return makeResourceFrom16(pResData
, p
[length
+indexR
]);
672 if (offset
!= 0) { /* empty if offset==0 */
673 const int32_t *p
= pResData
->pRoot
+offset
;
677 *key
=RES_GET_KEY32(pResData
, p
[indexR
]);
679 return (Resource
)p
[length
+indexR
];
690 U_CAPI Resource U_EXPORT2
691 res_getResource(const ResourceData
*pResData
, const char *key
) {
692 const char *realKey
=key
;
694 return res_getTableItemByKey(pResData
, pResData
->rootRes
, &idx
, &realKey
);
697 // TODO: Ported from Java, but enumerating at this low level may prevent us
698 // from doing necessary things, like resolving aliases,
699 // which need access to higher-level UResourceBundle code.
700 // Consider porting the low-level Container/Array/Table classes from Java,
701 // with getters for keys and values,
702 // and doing the enumeration in the higher-level code on top of those accessors.
704 ures_getAllTableItems(const ResourceData
*pResData
, Resource table
,
705 icu::ResourceDataValue
&value
, icu::ResourceTableSink
&sink
,
706 UErrorCode
&errorCode
) {
707 if(U_FAILURE(errorCode
)) { return; }
708 const uint16_t *keys16
= NULL
;
709 const int32_t *keys32
= NULL
;
710 const uint16_t *items16
= NULL
;
711 const Resource
*items32
= NULL
;
712 uint32_t offset
= RES_GET_OFFSET(table
);
714 switch(RES_GET_TYPE(table
)) {
716 if (offset
!= 0) { /* empty if offset==0 */
717 keys16
= (const uint16_t *)(pResData
->pRoot
+offset
);
719 items32
= (const Resource
*)(keys16
+length
+(~length
&1));
724 keys16
= pResData
->p16BitUnits
+offset
;
726 items16
= keys16
+ length
;
730 if (offset
!= 0) { /* empty if offset==0 */
731 keys32
= pResData
->pRoot
+offset
;
733 items32
= (const Resource
*)keys32
+ length
;
738 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
742 for (int32_t i
= 0; i
< length
; ++i
) {
744 if (keys16
!= NULL
) {
745 key
=RES_GET_KEY16(pResData
, keys16
[i
]);
747 key
=RES_GET_KEY32(pResData
, keys32
[i
]);
750 if (items16
!= NULL
) {
751 res
= makeResourceFrom16(pResData
, items16
[i
]);
755 int32_t type
= RES_GET_TYPE(res
);
756 if (URES_IS_ARRAY(type
)) {
757 int32_t numItems
= getArrayLength(pResData
, res
);
758 icu::ResourceArraySink
*subSink
= sink
.getOrCreateArraySink(key
, numItems
, errorCode
);
759 if (subSink
!= NULL
) {
760 ures_getAllArrayItems(pResData
, res
, value
, *subSink
, errorCode
);
762 } else if (URES_IS_TABLE(type
)) {
763 int32_t numItems
= getTableLength(pResData
, res
);
764 icu::ResourceTableSink
*subSink
= sink
.getOrCreateTableSink(key
, numItems
, errorCode
);
765 if (subSink
!= NULL
) {
766 ures_getAllTableItems(pResData
, res
, value
, *subSink
, errorCode
);
768 /* TODO: settle on how to deal with aliases, port to Java
769 } else if (type == URES_ALIAS) {
770 // aliases not handled in resource enumeration
771 errorCode = U_UNSUPPORTED_ERROR;
773 } else if (isNoInheritanceMarker(pResData
, res
)) {
774 sink
.putNoFallback(key
, errorCode
);
776 value
.setResource(res
);
777 sink
.put(key
, value
, errorCode
);
779 if(U_FAILURE(errorCode
)) { return; }
781 sink
.leave(errorCode
);
784 U_CAPI Resource U_EXPORT2
785 res_getArrayItem(const ResourceData
*pResData
, Resource array
, int32_t indexR
) {
786 uint32_t offset
=RES_GET_OFFSET(array
);
787 //U_ASSERT(indexR>=0); /* to ensure the index is not negative */
791 switch(RES_GET_TYPE(array
)) {
793 if (offset
!=0) { /* empty if offset==0 */
794 const int32_t *p
= pResData
->pRoot
+offset
;
796 return (Resource
)p
[1+indexR
];
802 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
804 return makeResourceFrom16(pResData
, p
[1+indexR
]);
815 ures_getAllArrayItems(const ResourceData
*pResData
, Resource array
,
816 icu::ResourceDataValue
&value
, icu::ResourceArraySink
&sink
,
817 UErrorCode
&errorCode
) {
818 if(U_FAILURE(errorCode
)) { return; }
819 const uint16_t *items16
= NULL
;
820 const Resource
*items32
= NULL
;
821 uint32_t offset
=RES_GET_OFFSET(array
);
823 switch(RES_GET_TYPE(array
)) {
825 if (offset
!=0) { /* empty if offset==0 */
826 items32
= (const Resource
*)pResData
->pRoot
+offset
;
832 items16
= pResData
->p16BitUnits
+offset
;
837 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
841 for (int32_t i
= 0; i
< length
; ++i
) {
843 if (items16
!= NULL
) {
844 res
= makeResourceFrom16(pResData
, items16
[i
]);
848 int32_t type
= RES_GET_TYPE(res
);
849 if (URES_IS_ARRAY(type
)) {
850 int32_t numItems
= getArrayLength(pResData
, res
);
851 icu::ResourceArraySink
*subSink
= sink
.getOrCreateArraySink(i
, numItems
, errorCode
);
852 if (subSink
!= NULL
) {
853 ures_getAllArrayItems(pResData
, res
, value
, *subSink
, errorCode
);
855 } else if (URES_IS_TABLE(type
)) {
856 int32_t numItems
= getTableLength(pResData
, res
);
857 icu::ResourceTableSink
*subSink
= sink
.getOrCreateTableSink(i
, numItems
, errorCode
);
858 if (subSink
!= NULL
) {
859 ures_getAllTableItems(pResData
, res
, value
, *subSink
, errorCode
);
861 /* TODO: settle on how to deal with aliases, port to Java
862 } else if (type == URES_ALIAS) {
863 // aliases not handled in resource enumeration
864 errorCode = U_UNSUPPORTED_ERROR;
867 value
.setResource(res
);
868 sink
.put(i
, value
, errorCode
);
870 if(U_FAILURE(errorCode
)) { return; }
872 sink
.leave(errorCode
);
876 res_findResource(const ResourceData
*pResData
, Resource r
, char** path
, const char** key
) {
877 char *pathP
= *path
, *nextSepP
= *path
;
878 char *closeIndex
= NULL
;
882 UResType type
= (UResType
)RES_GET_TYPE(t1
);
884 /* if you come in with an empty path, you'll be getting back the same resource */
885 if(!uprv_strlen(pathP
)) {
889 /* one needs to have an aggregate resource in order to search in it */
890 if(!URES_IS_CONTAINER(type
)) {
894 while(nextSepP
&& *pathP
&& t1
!= RES_BOGUS
&& URES_IS_CONTAINER(type
)) {
895 /* Iteration stops if: the path has been consumed, we found a non-existing
896 * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
898 nextSepP
= uprv_strchr(pathP
, RES_PATH_SEPARATOR
);
899 /* if there are more separators, terminate string
900 * and set path to the remaining part of the string
902 if(nextSepP
!= NULL
) {
903 if(nextSepP
== pathP
) {
907 *nextSepP
= 0; /* overwrite the separator with a NUL to terminate the key */
910 *path
= uprv_strchr(pathP
, 0);
913 /* if the resource is a table */
914 /* try the key based access */
915 if(URES_IS_TABLE(type
)) {
917 t2
= res_getTableItemByKey(pResData
, t1
, &indexR
, key
);
918 if(t2
== RES_BOGUS
) {
919 /* if we fail to get the resource by key, maybe we got an index */
920 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
921 if(*closeIndex
== 0) {
922 /* if we indeed have an index, try to get the item by index */
923 t2
= res_getTableItemByIndex(pResData
, t1
, indexR
, key
);
926 } else if(URES_IS_ARRAY(type
)) {
927 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
928 if(*closeIndex
== 0) {
929 t2
= res_getArrayItem(pResData
, t1
, indexR
);
931 t2
= RES_BOGUS
; /* have an array, but don't have a valid index */
934 } else { /* can't do much here, except setting t2 to bogus */
938 type
= (UResType
)RES_GET_TYPE(t1
);
939 /* position pathP to next resource key/index */
946 /* resource bundle swapping ------------------------------------------------- */
949 * Need to always enumerate the entire item tree,
950 * track the lowest address of any item to use as the limit for char keys[],
951 * track the highest address of any item to return the size of the data.
953 * We should have thought of storing those in the data...
954 * It is possible to extend the data structure by putting additional values
955 * in places that are inaccessible by ordinary enumeration of the item tree.
956 * For example, additional integers could be stored at the beginning or
957 * end of the key strings; this could be indicated by a minor version number,
958 * and the data swapping would have to know about these values.
960 * The data structure does not forbid keys to be shared, so we must swap
961 * all keys once instead of each key when it is referenced.
963 * These swapping functions assume that a resource bundle always has a length
964 * that is a multiple of 4 bytes.
965 * Currently, this is trivially true because genrb writes bundle tree leaves
966 * physically first, before their branches, so that the root table with its
967 * array of resource items (uint32_t values) is always last.
970 /* definitions for table sorting ------------------------ */
973 * row of a temporary array
975 * gets platform-endian key string indexes and sorting indexes;
976 * after sorting this array by keys, the actual key/value arrays are permutated
977 * according to the sorting indexes
980 int32_t keyIndex
, sortIndex
;
984 ures_compareRows(const void *context
, const void *left
, const void *right
) {
985 const char *keyChars
=(const char *)context
;
986 return (int32_t)uprv_strcmp(keyChars
+((const Row
*)left
)->keyIndex
,
987 keyChars
+((const Row
*)right
)->keyIndex
);
990 typedef struct TempTable
{
991 const char *keyChars
;
995 int32_t localKeyLimit
;
996 uint8_t majorFormatVersion
;
1000 STACK_ROW_CAPACITY
=200
1003 /* The table item key string is not locally available. */
1004 static const char *const gUnknownKey
="";
1006 /* resource table key for collation binaries: "%%CollationBin" */
1007 static const UChar gCollationBinKey
[]={
1009 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1015 * swap one resource item
1018 ures_swapResource(const UDataSwapper
*ds
,
1019 const Resource
*inBundle
, Resource
*outBundle
,
1020 Resource res
, /* caller swaps res itself */
1022 TempTable
*pTempTable
,
1023 UErrorCode
*pErrorCode
) {
1026 int32_t offset
, count
;
1028 switch(RES_GET_TYPE(res
)) {
1030 case URES_STRING_V2
:
1033 /* integer, or points to 16-bit units, nothing to do here */
1039 /* all other types use an offset to point to their data */
1040 offset
=(int32_t)RES_GET_OFFSET(res
);
1042 /* special offset indicating an empty item */
1045 if(pTempTable
->resFlags
[offset
>>5]&((uint32_t)1<<(offset
&0x1f))) {
1046 /* we already swapped this resource item */
1049 /* mark it as swapped now */
1050 pTempTable
->resFlags
[offset
>>5]|=((uint32_t)1<<(offset
&0x1f));
1056 switch(RES_GET_TYPE(res
)) {
1058 /* physically same value layout as string, fall through */
1061 count
=udata_readInt32(ds
, (int32_t)*p
);
1063 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
1064 /* swap each UChar (the terminating NUL would not change) */
1065 ds
->swapArray16(ds
, p
+1, 2*count
, q
+1, pErrorCode
);
1068 count
=udata_readInt32(ds
, (int32_t)*p
);
1070 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
1071 /* no need to swap or copy bytes - ures_swap() copied them all */
1073 /* swap known formats */
1074 #if !UCONFIG_NO_COLLATION
1075 if( key
!=NULL
&& /* the binary is in a table */
1077 /* its table key string is "%%CollationBin" */
1078 0==ds
->compareInvChars(ds
, key
, -1,
1079 gCollationBinKey
, UPRV_LENGTHOF(gCollationBinKey
)-1) :
1080 /* its table key string is unknown but it looks like a collation binary */
1081 ucol_looksLikeCollationBinary(ds
, p
+1, count
))
1083 ucol_swap(ds
, p
+1, count
, q
+1, pErrorCode
);
1090 const uint16_t *pKey16
;
1093 const int32_t *pKey32
;
1097 int32_t i
, oldIndex
;
1099 if(RES_GET_TYPE(res
)==URES_TABLE
) {
1100 /* get table item count */
1101 pKey16
=(const uint16_t *)p
;
1102 qKey16
=(uint16_t *)q
;
1103 count
=ds
->readUInt16(*pKey16
);
1108 ds
->swapArray16(ds
, pKey16
++, 2, qKey16
++, pErrorCode
);
1110 offset
+=((1+count
)+1)/2;
1112 /* get table item count */
1113 pKey32
=(const int32_t *)p
;
1114 qKey32
=(int32_t *)q
;
1115 count
=udata_readInt32(ds
, *pKey32
);
1120 ds
->swapArray32(ds
, pKey32
++, 4, qKey32
++, pErrorCode
);
1129 p
=inBundle
+offset
; /* pointer to table resources */
1133 for(i
=0; i
<count
; ++i
) {
1134 const char *itemKey
=gUnknownKey
;
1136 int32_t keyOffset
=ds
->readUInt16(pKey16
[i
]);
1137 if(keyOffset
<pTempTable
->localKeyLimit
) {
1138 itemKey
=(const char *)outBundle
+keyOffset
;
1141 int32_t keyOffset
=udata_readInt32(ds
, pKey32
[i
]);
1143 itemKey
=(const char *)outBundle
+keyOffset
;
1146 item
=ds
->readUInt32(p
[i
]);
1147 ures_swapResource(ds
, inBundle
, outBundle
, item
, itemKey
, pTempTable
, pErrorCode
);
1148 if(U_FAILURE(*pErrorCode
)) {
1149 udata_printError(ds
, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
1155 if(pTempTable
->majorFormatVersion
>1 || ds
->inCharset
==ds
->outCharset
) {
1156 /* no need to sort, just swap the offset/value arrays */
1158 ds
->swapArray16(ds
, pKey16
, count
*2, qKey16
, pErrorCode
);
1159 ds
->swapArray32(ds
, p
, count
*4, q
, pErrorCode
);
1161 /* swap key offsets and items as one array */
1162 ds
->swapArray32(ds
, pKey32
, count
*2*4, qKey32
, pErrorCode
);
1168 * We need to sort tables by outCharset key strings because they
1169 * sort differently for different charset families.
1170 * ures_swap() already set pTempTable->keyChars appropriately.
1171 * First we set up a temporary table with the key indexes and
1172 * sorting indexes and sort that.
1173 * Then we permutate and copy/swap the actual values.
1176 for(i
=0; i
<count
; ++i
) {
1177 pTempTable
->rows
[i
].keyIndex
=ds
->readUInt16(pKey16
[i
]);
1178 pTempTable
->rows
[i
].sortIndex
=i
;
1181 for(i
=0; i
<count
; ++i
) {
1182 pTempTable
->rows
[i
].keyIndex
=udata_readInt32(ds
, pKey32
[i
]);
1183 pTempTable
->rows
[i
].sortIndex
=i
;
1186 uprv_sortArray(pTempTable
->rows
, count
, sizeof(Row
),
1187 ures_compareRows
, pTempTable
->keyChars
,
1189 if(U_FAILURE(*pErrorCode
)) {
1190 udata_printError(ds
, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
1196 * copy/swap/permutate items
1198 * If we swap in-place, then the permutation must use another
1199 * temporary array (pTempTable->resort)
1200 * before the results are copied to the outBundle.
1206 if(pKey16
!=qKey16
) {
1209 rKey16
=(uint16_t *)pTempTable
->resort
;
1211 for(i
=0; i
<count
; ++i
) {
1212 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1213 ds
->swapArray16(ds
, pKey16
+oldIndex
, 2, rKey16
+i
, pErrorCode
);
1215 if(qKey16
!=rKey16
) {
1216 uprv_memcpy(qKey16
, rKey16
, 2*count
);
1221 if(pKey32
!=qKey32
) {
1224 rKey32
=pTempTable
->resort
;
1226 for(i
=0; i
<count
; ++i
) {
1227 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1228 ds
->swapArray32(ds
, pKey32
+oldIndex
, 4, rKey32
+i
, pErrorCode
);
1230 if(qKey32
!=rKey32
) {
1231 uprv_memcpy(qKey32
, rKey32
, 4*count
);
1243 r
=(Resource
*)pTempTable
->resort
;
1245 for(i
=0; i
<count
; ++i
) {
1246 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1247 ds
->swapArray32(ds
, p
+oldIndex
, 4, r
+i
, pErrorCode
);
1250 uprv_memcpy(q
, r
, 4*count
);
1260 count
=udata_readInt32(ds
, (int32_t)*p
);
1262 ds
->swapArray32(ds
, p
++, 4, q
++, pErrorCode
);
1265 for(i
=0; i
<count
; ++i
) {
1266 item
=ds
->readUInt32(p
[i
]);
1267 ures_swapResource(ds
, inBundle
, outBundle
, item
, NULL
, pTempTable
, pErrorCode
);
1268 if(U_FAILURE(*pErrorCode
)) {
1269 udata_printError(ds
, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n",
1276 ds
->swapArray32(ds
, p
, 4*count
, q
, pErrorCode
);
1279 case URES_INT_VECTOR
:
1280 count
=udata_readInt32(ds
, (int32_t)*p
);
1281 /* swap length and each integer */
1282 ds
->swapArray32(ds
, p
, 4*(1+count
), q
, pErrorCode
);
1285 /* also catches RES_BOGUS */
1286 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1291 U_CAPI
int32_t U_EXPORT2
1292 ures_swap(const UDataSwapper
*ds
,
1293 const void *inData
, int32_t length
, void *outData
,
1294 UErrorCode
*pErrorCode
) {
1295 const UDataInfo
*pInfo
;
1296 const Resource
*inBundle
;
1298 int32_t headerSize
, maxTableLength
;
1300 Row rows
[STACK_ROW_CAPACITY
];
1301 int32_t resort
[STACK_ROW_CAPACITY
];
1302 TempTable tempTable
;
1304 const int32_t *inIndexes
;
1306 /* the following integers count Resource item offsets (4 bytes each), not bytes */
1307 int32_t bundleLength
, indexLength
, keysBottom
, keysTop
, resBottom
, top
;
1309 /* udata_swapDataHeader checks the arguments */
1310 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1311 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1315 /* check data format and format version */
1316 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1318 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
1319 pInfo
->dataFormat
[1]==0x65 &&
1320 pInfo
->dataFormat
[2]==0x73 &&
1321 pInfo
->dataFormat
[3]==0x42 &&
1322 /* formatVersion 1.1+ or 2.x or 3.x */
1323 ((pInfo
->formatVersion
[0]==1 && pInfo
->formatVersion
[1]>=1) ||
1324 pInfo
->formatVersion
[0]==2 || pInfo
->formatVersion
[0]==3)
1326 udata_printError(ds
, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n",
1327 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1328 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1329 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1330 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1333 tempTable
.majorFormatVersion
=pInfo
->formatVersion
[0];
1335 /* a resource bundle must contain at least one resource item */
1339 bundleLength
=(length
-headerSize
)/4;
1341 /* formatVersion 1.1 must have a root item and at least 5 indexes */
1342 if(bundleLength
<(1+5)) {
1343 udata_printError(ds
, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
1345 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1350 inBundle
=(const Resource
*)((const char *)inData
+headerSize
);
1351 rootRes
=ds
->readUInt32(*inBundle
);
1353 /* formatVersion 1.1 adds the indexes[] array */
1354 inIndexes
=(const int32_t *)(inBundle
+1);
1356 indexLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_LENGTH
])&0xff;
1357 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
1358 udata_printError(ds
, "ures_swap(): too few indexes for a 1.1+ resource bundle\n");
1359 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1362 keysBottom
=1+indexLength
;
1363 keysTop
=udata_readInt32(ds
, inIndexes
[URES_INDEX_KEYS_TOP
]);
1364 if(indexLength
>URES_INDEX_16BIT_TOP
) {
1365 resBottom
=udata_readInt32(ds
, inIndexes
[URES_INDEX_16BIT_TOP
]);
1369 top
=udata_readInt32(ds
, inIndexes
[URES_INDEX_BUNDLE_TOP
]);
1370 maxTableLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_MAX_TABLE_LENGTH
]);
1372 if(0<=bundleLength
&& bundleLength
<top
) {
1373 udata_printError(ds
, "ures_swap(): resource top %d exceeds bundle length %d\n",
1375 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1378 if(keysTop
>(1+indexLength
)) {
1379 tempTable
.localKeyLimit
=keysTop
<<2;
1381 tempTable
.localKeyLimit
=0;
1385 Resource
*outBundle
=(Resource
*)((char *)outData
+headerSize
);
1387 /* track which resources we have already swapped */
1388 uint32_t stackResFlags
[STACK_ROW_CAPACITY
];
1389 int32_t resFlagsLength
;
1392 * We need one bit per 4 resource bundle bytes so that we can track
1393 * every possible Resource for whether we have swapped it already.
1394 * Multiple Resource words can refer to the same bundle offsets
1395 * for sharing identical values.
1396 * We could optimize this by allocating only for locations above
1397 * where Resource values are stored (above keys & strings).
1399 resFlagsLength
=(length
+31)>>5; /* number of bytes needed */
1400 resFlagsLength
=(resFlagsLength
+3)&~3; /* multiple of 4 bytes for uint32_t */
1401 if(resFlagsLength
<=(int32_t)sizeof(stackResFlags
)) {
1402 tempTable
.resFlags
=stackResFlags
;
1404 tempTable
.resFlags
=(uint32_t *)uprv_malloc(resFlagsLength
);
1405 if(tempTable
.resFlags
==NULL
) {
1406 udata_printError(ds
, "ures_swap(): unable to allocate memory for tracking resources\n");
1407 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1411 uprv_memset(tempTable
.resFlags
, 0, resFlagsLength
);
1413 /* copy the bundle for binary and inaccessible data */
1414 if(inData
!=outData
) {
1415 uprv_memcpy(outBundle
, inBundle
, 4*top
);
1418 /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
1419 udata_swapInvStringBlock(ds
, inBundle
+keysBottom
, 4*(keysTop
-keysBottom
),
1420 outBundle
+keysBottom
, pErrorCode
);
1421 if(U_FAILURE(*pErrorCode
)) {
1422 udata_printError(ds
, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop
-keysBottom
));
1426 /* swap the 16-bit units (strings, table16, array16) */
1427 if(keysTop
<resBottom
) {
1428 ds
->swapArray16(ds
, inBundle
+keysTop
, (resBottom
-keysTop
)*4, outBundle
+keysTop
, pErrorCode
);
1429 if(U_FAILURE(*pErrorCode
)) {
1430 udata_printError(ds
, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom
-keysTop
));
1435 /* allocate the temporary table for sorting resource tables */
1436 tempTable
.keyChars
=(const char *)outBundle
; /* sort by outCharset */
1437 if(tempTable
.majorFormatVersion
>1 || maxTableLength
<=STACK_ROW_CAPACITY
) {
1438 tempTable
.rows
=rows
;
1439 tempTable
.resort
=resort
;
1441 tempTable
.rows
=(Row
*)uprv_malloc(maxTableLength
*sizeof(Row
)+maxTableLength
*4);
1442 if(tempTable
.rows
==NULL
) {
1443 udata_printError(ds
, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
1445 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1446 if(tempTable
.resFlags
!=stackResFlags
) {
1447 uprv_free(tempTable
.resFlags
);
1451 tempTable
.resort
=(int32_t *)(tempTable
.rows
+maxTableLength
);
1454 /* swap the resources */
1455 ures_swapResource(ds
, inBundle
, outBundle
, rootRes
, NULL
, &tempTable
, pErrorCode
);
1456 if(U_FAILURE(*pErrorCode
)) {
1457 udata_printError(ds
, "ures_swapResource(root res=%08x) failed\n",
1461 if(tempTable
.rows
!=rows
) {
1462 uprv_free(tempTable
.rows
);
1464 if(tempTable
.resFlags
!=stackResFlags
) {
1465 uprv_free(tempTable
.resFlags
);
1468 /* swap the root resource and indexes */
1469 ds
->swapArray32(ds
, inBundle
, keysBottom
*4, outBundle
, pErrorCode
);
1472 return headerSize
+4*top
;