1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1999-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: uresdata.cpp
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"
24 #include "unicode/ustring.h"
25 #include "unicode/utf16.h"
38 * Resource access helpers
41 /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
42 #define RES_GET_KEY16(pResData, keyOffset) \
43 ((keyOffset)<(pResData)->localKeyLimit ? \
44 (const char *)(pResData)->pRoot+(keyOffset) : \
45 (pResData)->poolBundleKeys+(keyOffset)-(pResData)->localKeyLimit)
47 #define RES_GET_KEY32(pResData, keyOffset) \
49 (const char *)(pResData)->pRoot+(keyOffset) : \
50 (pResData)->poolBundleKeys+((keyOffset)&0x7fffffff))
52 #define URESDATA_ITEM_NOT_FOUND -1
54 /* empty resources, returned when the resource offset is 0 */
55 static const uint16_t gEmpty16
=0;
66 } gEmptyString
={ 0, 0, 0 };
69 * All the type-access functions assume that
70 * the resource is of the expected type.
74 _res_findTableItem(const ResourceData
*pResData
, const uint16_t *keyOffsets
, int32_t length
,
75 const char *key
, const char **realKey
) {
77 int32_t mid
, start
, limit
;
80 /* do a binary search for the key */
84 mid
= (start
+ limit
) / 2;
85 tableKey
= RES_GET_KEY16(pResData
, keyOffsets
[mid
]);
86 if (pResData
->useNativeStrcmp
) {
87 result
= uprv_strcmp(key
, tableKey
);
89 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
93 } else if (result
> 0) {
101 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
105 _res_findTable32Item(const ResourceData
*pResData
, const int32_t *keyOffsets
, int32_t length
,
106 const char *key
, const char **realKey
) {
107 const char *tableKey
;
108 int32_t mid
, start
, limit
;
111 /* do a binary search for the key */
115 mid
= (start
+ limit
) / 2;
116 tableKey
= RES_GET_KEY32(pResData
, keyOffsets
[mid
]);
117 if (pResData
->useNativeStrcmp
) {
118 result
= uprv_strcmp(key
, tableKey
);
120 result
= uprv_compareInvCharsAsAscii(key
, tableKey
);
124 } else if (result
> 0) {
132 return URESDATA_ITEM_NOT_FOUND
; /* not found or table is empty. */
135 /* helper for res_load() ---------------------------------------------------- */
137 static UBool U_CALLCONV
138 isAcceptable(void *context
,
139 const char * /*type*/, const char * /*name*/,
140 const UDataInfo
*pInfo
) {
141 uprv_memcpy(context
, pInfo
->formatVersion
, 4);
144 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
145 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
146 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
147 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
148 pInfo
->dataFormat
[1]==0x65 &&
149 pInfo
->dataFormat
[2]==0x73 &&
150 pInfo
->dataFormat
[3]==0x42 &&
151 (1<=pInfo
->formatVersion
[0] && pInfo
->formatVersion
[0]<=3));
154 /* semi-public functions ---------------------------------------------------- */
157 res_init(ResourceData
*pResData
,
158 UVersionInfo formatVersion
, const void *inBytes
, int32_t length
,
159 UErrorCode
*errorCode
) {
162 /* get the root resource */
163 pResData
->pRoot
=(const int32_t *)inBytes
;
164 pResData
->rootRes
=(Resource
)*pResData
->pRoot
;
165 pResData
->p16BitUnits
=&gEmpty16
;
167 /* formatVersion 1.1 must have a root item and at least 5 indexes */
168 if(length
>=0 && (length
/4)<((formatVersion
[0]==1 && formatVersion
[1]==0) ? 1 : 1+5)) {
169 *errorCode
=U_INVALID_FORMAT_ERROR
;
170 res_unload(pResData
);
174 /* currently, we accept only resources that have a Table as their roots */
175 rootType
=(UResType
)RES_GET_TYPE(pResData
->rootRes
);
176 if(!URES_IS_TABLE(rootType
)) {
177 *errorCode
=U_INVALID_FORMAT_ERROR
;
178 res_unload(pResData
);
182 if(formatVersion
[0]==1 && formatVersion
[1]==0) {
183 pResData
->localKeyLimit
=0x10000; /* greater than any 16-bit key string offset */
185 /* bundles with formatVersion 1.1 and later contain an indexes[] array */
186 const int32_t *indexes
=pResData
->pRoot
+1;
187 int32_t indexLength
=indexes
[URES_INDEX_LENGTH
]&0xff;
188 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
189 *errorCode
=U_INVALID_FORMAT_ERROR
;
190 res_unload(pResData
);
194 (length
<((1+indexLength
)<<2) ||
195 length
<(indexes
[URES_INDEX_BUNDLE_TOP
]<<2))
197 *errorCode
=U_INVALID_FORMAT_ERROR
;
198 res_unload(pResData
);
201 if(indexes
[URES_INDEX_KEYS_TOP
]>(1+indexLength
)) {
202 pResData
->localKeyLimit
=indexes
[URES_INDEX_KEYS_TOP
]<<2;
204 if(formatVersion
[0]>=3) {
205 // In formatVersion 1, the indexLength took up this whole int.
206 // In version 2, bits 31..8 were reserved and always 0.
207 // In version 3, they contain bits 23..0 of the poolStringIndexLimit.
208 // Bits 27..24 are in indexes[URES_INDEX_ATTRIBUTES] bits 15..12.
209 pResData
->poolStringIndexLimit
=(int32_t)((uint32_t)indexes
[URES_INDEX_LENGTH
]>>8);
211 if(indexLength
>URES_INDEX_ATTRIBUTES
) {
212 int32_t att
=indexes
[URES_INDEX_ATTRIBUTES
];
213 pResData
->noFallback
=(UBool
)(att
&URES_ATT_NO_FALLBACK
);
214 pResData
->isPoolBundle
=(UBool
)((att
&URES_ATT_IS_POOL_BUNDLE
)!=0);
215 pResData
->usesPoolBundle
=(UBool
)((att
&URES_ATT_USES_POOL_BUNDLE
)!=0);
216 pResData
->poolStringIndexLimit
|=(att
&0xf000)<<12; // bits 15..12 -> 27..24
217 pResData
->poolStringIndex16Limit
=(int32_t)((uint32_t)att
>>16);
219 if((pResData
->isPoolBundle
|| pResData
->usesPoolBundle
) && indexLength
<=URES_INDEX_POOL_CHECKSUM
) {
220 *errorCode
=U_INVALID_FORMAT_ERROR
;
221 res_unload(pResData
);
224 if( indexLength
>URES_INDEX_16BIT_TOP
&&
225 indexes
[URES_INDEX_16BIT_TOP
]>indexes
[URES_INDEX_KEYS_TOP
]
227 pResData
->p16BitUnits
=(const uint16_t *)(pResData
->pRoot
+indexes
[URES_INDEX_KEYS_TOP
]);
231 if(formatVersion
[0]==1 || U_CHARSET_FAMILY
==U_ASCII_FAMILY
) {
233 * formatVersion 1: compare key strings in native-charset order
234 * formatVersion 2 and up: compare key strings in ASCII order
236 pResData
->useNativeStrcmp
=TRUE
;
240 U_CAPI
void U_EXPORT2
241 res_read(ResourceData
*pResData
,
242 const UDataInfo
*pInfo
, const void *inBytes
, int32_t length
,
243 UErrorCode
*errorCode
) {
244 UVersionInfo formatVersion
;
246 uprv_memset(pResData
, 0, sizeof(ResourceData
));
247 if(U_FAILURE(*errorCode
)) {
250 if(!isAcceptable(formatVersion
, NULL
, NULL
, pInfo
)) {
251 *errorCode
=U_INVALID_FORMAT_ERROR
;
254 res_init(pResData
, formatVersion
, inBytes
, length
, errorCode
);
258 res_load(ResourceData
*pResData
,
259 const char *path
, const char *name
, UErrorCode
*errorCode
) {
260 UVersionInfo formatVersion
;
262 uprv_memset(pResData
, 0, sizeof(ResourceData
));
264 /* load the ResourceBundle file */
265 pResData
->data
=udata_openChoice(path
, "res", name
, isAcceptable
, formatVersion
, errorCode
);
266 if(U_FAILURE(*errorCode
)) {
270 /* get its memory and initialize *pResData */
271 res_init(pResData
, formatVersion
, udata_getMemory(pResData
->data
), -1, errorCode
);
275 res_unload(ResourceData
*pResData
) {
276 if(pResData
->data
!=NULL
) {
277 udata_close(pResData
->data
);
282 static const int8_t gPublicTypes
[URES_LIMIT
] = {
288 URES_TABLE
, /* URES_TABLE32 */
289 URES_TABLE
, /* URES_TABLE16 */
290 URES_STRING
, /* URES_STRING_V2 */
294 URES_ARRAY
, /* URES_ARRAY16 */
304 U_CAPI UResType U_EXPORT2
305 res_getPublicType(Resource res
) {
306 return (UResType
)gPublicTypes
[RES_GET_TYPE(res
)];
309 U_CAPI
const UChar
* U_EXPORT2
310 res_getString(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
312 uint32_t offset
=RES_GET_OFFSET(res
);
314 if(RES_GET_TYPE(res
)==URES_STRING_V2
) {
316 if((int32_t)offset
<pResData
->poolStringIndexLimit
) {
317 p
=(const UChar
*)pResData
->poolBundleStrings
+offset
;
319 p
=(const UChar
*)pResData
->p16BitUnits
+(offset
-pResData
->poolStringIndexLimit
);
322 if(!U16_IS_TRAIL(first
)) {
324 } else if(first
<0xdfef) {
327 } else if(first
<0xdfff) {
328 length
=((first
-0xdfef)<<16)|p
[1];
331 length
=((int32_t)p
[1]<<16)|p
[2];
334 } else if(res
==offset
) /* RES_GET_TYPE(res)==URES_STRING */ {
335 const int32_t *p32
= res
==0 ? &gEmptyString
.length
: pResData
->pRoot
+res
;
337 p
=(const UChar
*)p32
;
351 * CLDR string value (three empty-set symbols)=={2205, 2205, 2205}
352 * prevents fallback to the parent bundle.
353 * TODO: combine with other code that handles this marker, use EMPTY_SET constant.
354 * TODO: maybe move to uresbund.cpp?
356 UBool
isNoInheritanceMarker(const ResourceData
*pResData
, Resource res
) {
357 uint32_t offset
=RES_GET_OFFSET(res
);
360 } else if (res
== offset
) {
361 const int32_t *p32
=pResData
->pRoot
+res
;
363 const UChar
*p
=(const UChar
*)p32
;
364 return length
== 3 && p
[2] == 0x2205 && p
[3] == 0x2205 && p
[4] == 0x2205;
365 } else if (RES_GET_TYPE(res
) == URES_STRING_V2
) {
367 if((int32_t)offset
<pResData
->poolStringIndexLimit
) {
368 p
=(const UChar
*)pResData
->poolBundleStrings
+offset
;
370 p
=(const UChar
*)pResData
->p16BitUnits
+(offset
-pResData
->poolStringIndexLimit
);
373 if (first
== 0x2205) { // implicit length
374 return p
[1] == 0x2205 && p
[2] == 0x2205 && p
[3] == 0;
375 } else if (first
== 0xdc03) { // explicit length 3 (should not occur)
376 return p
[1] == 0x2205 && p
[2] == 0x2205 && p
[3] == 0x2205;
378 // Assume that the string has not been stored with more length units than necessary.
385 int32_t getStringArray(const ResourceData
*pResData
, const icu::ResourceArray
&array
,
386 icu::UnicodeString
*dest
, int32_t capacity
,
387 UErrorCode
&errorCode
) {
388 if(U_FAILURE(errorCode
)) {
391 if(dest
== NULL
? capacity
!= 0 : capacity
< 0) {
392 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
395 int32_t length
= array
.getSize();
399 if(length
> capacity
) {
400 errorCode
= U_BUFFER_OVERFLOW_ERROR
;
403 for(int32_t i
= 0; i
< length
; ++i
) {
405 const UChar
*s
= res_getString(pResData
, array
.internalGetResource(pResData
, i
), &sLength
);
407 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
410 dest
[i
].setTo(TRUE
, s
, sLength
);
417 U_CAPI
const UChar
* U_EXPORT2
418 res_getAlias(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
420 uint32_t offset
=RES_GET_OFFSET(res
);
422 if(RES_GET_TYPE(res
)==URES_ALIAS
) {
423 const int32_t *p32
= offset
==0 ? &gEmptyString
.length
: pResData
->pRoot
+offset
;
425 p
=(const UChar
*)p32
;
436 U_CAPI
const uint8_t * U_EXPORT2
437 res_getBinary(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
439 uint32_t offset
=RES_GET_OFFSET(res
);
441 if(RES_GET_TYPE(res
)==URES_BINARY
) {
442 const int32_t *p32
= offset
==0 ? (const int32_t*)&gEmpty32
: pResData
->pRoot
+offset
;
444 p
=(const uint8_t *)p32
;
456 U_CAPI
const int32_t * U_EXPORT2
457 res_getIntVector(const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
459 uint32_t offset
=RES_GET_OFFSET(res
);
461 if(RES_GET_TYPE(res
)==URES_INT_VECTOR
) {
462 p
= offset
==0 ? (const int32_t *)&gEmpty32
: pResData
->pRoot
+offset
;
474 U_CAPI
int32_t U_EXPORT2
475 res_countArrayItems(const ResourceData
*pResData
, Resource res
) {
476 uint32_t offset
=RES_GET_OFFSET(res
);
477 switch(RES_GET_TYPE(res
)) {
483 case URES_INT_VECTOR
:
487 return offset
==0 ? 0 : *(pResData
->pRoot
+offset
);
489 return offset
==0 ? 0 : *((const uint16_t *)(pResData
->pRoot
+offset
));
492 return pResData
->p16BitUnits
[offset
];
500 ResourceDataValue::~ResourceDataValue() {}
502 UResType
ResourceDataValue::getType() const {
503 return res_getPublicType(res
);
506 const UChar
*ResourceDataValue::getString(int32_t &length
, UErrorCode
&errorCode
) const {
507 if(U_FAILURE(errorCode
)) {
510 const UChar
*s
= res_getString(pResData
, res
, &length
);
512 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
517 const UChar
*ResourceDataValue::getAliasString(int32_t &length
, UErrorCode
&errorCode
) const {
518 if(U_FAILURE(errorCode
)) {
521 const UChar
*s
= res_getAlias(pResData
, res
, &length
);
523 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
528 int32_t ResourceDataValue::getInt(UErrorCode
&errorCode
) const {
529 if(U_FAILURE(errorCode
)) {
532 if(RES_GET_TYPE(res
) != URES_INT
) {
533 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
535 return RES_GET_INT(res
);
538 uint32_t ResourceDataValue::getUInt(UErrorCode
&errorCode
) const {
539 if(U_FAILURE(errorCode
)) {
542 if(RES_GET_TYPE(res
) != URES_INT
) {
543 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
545 return RES_GET_UINT(res
);
548 const int32_t *ResourceDataValue::getIntVector(int32_t &length
, UErrorCode
&errorCode
) const {
549 if(U_FAILURE(errorCode
)) {
552 const int32_t *iv
= res_getIntVector(pResData
, res
, &length
);
554 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
559 const uint8_t *ResourceDataValue::getBinary(int32_t &length
, UErrorCode
&errorCode
) const {
560 if(U_FAILURE(errorCode
)) {
563 const uint8_t *b
= res_getBinary(pResData
, res
, &length
);
565 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
570 ResourceArray
ResourceDataValue::getArray(UErrorCode
&errorCode
) const {
571 if(U_FAILURE(errorCode
)) {
572 return ResourceArray();
574 const uint16_t *items16
= NULL
;
575 const Resource
*items32
= NULL
;
576 uint32_t offset
=RES_GET_OFFSET(res
);
578 switch(RES_GET_TYPE(res
)) {
580 if (offset
!=0) { // empty if offset==0
581 items32
= (const Resource
*)pResData
->pRoot
+offset
;
586 items16
= pResData
->p16BitUnits
+offset
;
590 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
591 return ResourceArray();
593 return ResourceArray(items16
, items32
, length
);
596 ResourceTable
ResourceDataValue::getTable(UErrorCode
&errorCode
) const {
597 if(U_FAILURE(errorCode
)) {
598 return ResourceTable();
600 const uint16_t *keys16
= NULL
;
601 const int32_t *keys32
= NULL
;
602 const uint16_t *items16
= NULL
;
603 const Resource
*items32
= NULL
;
604 uint32_t offset
= RES_GET_OFFSET(res
);
606 switch(RES_GET_TYPE(res
)) {
608 if (offset
!= 0) { // empty if offset==0
609 keys16
= (const uint16_t *)(pResData
->pRoot
+offset
);
611 items32
= (const Resource
*)(keys16
+length
+(~length
&1));
615 keys16
= pResData
->p16BitUnits
+offset
;
617 items16
= keys16
+ length
;
620 if (offset
!= 0) { // empty if offset==0
621 keys32
= pResData
->pRoot
+offset
;
623 items32
= (const Resource
*)keys32
+ length
;
627 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
628 return ResourceTable();
630 return ResourceTable(keys16
, keys32
, items16
, items32
, length
);
633 UBool
ResourceDataValue::isNoInheritanceMarker() const {
634 return ::isNoInheritanceMarker(pResData
, res
);
637 int32_t ResourceDataValue::getStringArray(UnicodeString
*dest
, int32_t capacity
,
638 UErrorCode
&errorCode
) const {
639 return ::getStringArray(pResData
, getArray(errorCode
), dest
, capacity
, errorCode
);
642 int32_t ResourceDataValue::getStringArrayOrStringAsArray(UnicodeString
*dest
, int32_t capacity
,
643 UErrorCode
&errorCode
) const {
644 if(URES_IS_ARRAY(res
)) {
645 return ::getStringArray(pResData
, getArray(errorCode
), dest
, capacity
, errorCode
);
647 if(U_FAILURE(errorCode
)) {
650 if(dest
== NULL
? capacity
!= 0 : capacity
< 0) {
651 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
655 errorCode
= U_BUFFER_OVERFLOW_ERROR
;
659 const UChar
*s
= res_getString(pResData
, res
, &sLength
);
661 dest
[0].setTo(TRUE
, s
, sLength
);
664 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
668 UnicodeString
ResourceDataValue::getStringOrFirstOfArray(UErrorCode
&errorCode
) const {
670 if(U_FAILURE(errorCode
)) {
674 const UChar
*s
= res_getString(pResData
, res
, &sLength
);
676 us
.setTo(TRUE
, s
, sLength
);
679 ResourceArray array
= getArray(errorCode
);
680 if(U_FAILURE(errorCode
)) {
683 if(array
.getSize() > 0) {
684 s
= res_getString(pResData
, array
.internalGetResource(pResData
, 0), &sLength
);
686 us
.setTo(TRUE
, s
, sLength
);
690 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
697 makeResourceFrom16(const ResourceData
*pResData
, int32_t res16
) {
698 if(res16
<pResData
->poolStringIndex16Limit
) {
699 // Pool string, nothing to do.
701 // Local string, adjust the 16-bit offset to a regular one,
702 // with a larger pool string index limit.
703 res16
=res16
-pResData
->poolStringIndex16Limit
+pResData
->poolStringIndexLimit
;
705 return URES_MAKE_RESOURCE(URES_STRING_V2
, res16
);
708 U_CAPI Resource U_EXPORT2
709 res_getTableItemByKey(const ResourceData
*pResData
, Resource table
,
710 int32_t *indexR
, const char **key
) {
711 uint32_t offset
=RES_GET_OFFSET(table
);
714 if(key
== NULL
|| *key
== NULL
) {
717 switch(RES_GET_TYPE(table
)) {
719 if (offset
!=0) { /* empty if offset==0 */
720 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
722 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
724 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
731 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
733 *indexR
=idx
=_res_findTableItem(pResData
, p
, length
, *key
, key
);
735 return makeResourceFrom16(pResData
, p
[length
+idx
]);
740 if (offset
!=0) { /* empty if offset==0 */
741 const int32_t *p
= pResData
->pRoot
+offset
;
743 *indexR
=idx
=_res_findTable32Item(pResData
, p
, length
, *key
, key
);
745 return (Resource
)p
[length
+idx
];
756 U_CAPI Resource U_EXPORT2
757 res_getTableItemByIndex(const ResourceData
*pResData
, Resource table
,
758 int32_t indexR
, const char **key
) {
759 uint32_t offset
=RES_GET_OFFSET(table
);
764 switch(RES_GET_TYPE(table
)) {
766 if (offset
!= 0) { /* empty if offset==0 */
767 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
770 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
772 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
780 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
784 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
786 return makeResourceFrom16(pResData
, p
[length
+indexR
]);
791 if (offset
!= 0) { /* empty if offset==0 */
792 const int32_t *p
= pResData
->pRoot
+offset
;
796 *key
=RES_GET_KEY32(pResData
, p
[indexR
]);
798 return (Resource
)p
[length
+indexR
];
809 U_CAPI Resource U_EXPORT2
810 res_getResource(const ResourceData
*pResData
, const char *key
) {
811 const char *realKey
=key
;
813 return res_getTableItemByKey(pResData
, pResData
->rootRes
, &idx
, &realKey
);
817 UBool
icu::ResourceTable::getKeyAndValue(int32_t i
,
818 const char *&key
, icu::ResourceValue
&value
) const {
819 if(0 <= i
&& i
< length
) {
820 icu::ResourceDataValue
&rdValue
= static_cast<icu::ResourceDataValue
&>(value
);
821 if (keys16
!= NULL
) {
822 key
= RES_GET_KEY16(rdValue
.pResData
, keys16
[i
]);
824 key
= RES_GET_KEY32(rdValue
.pResData
, keys32
[i
]);
827 if (items16
!= NULL
) {
828 res
= makeResourceFrom16(rdValue
.pResData
, items16
[i
]);
832 rdValue
.setResource(res
);
838 U_CAPI Resource U_EXPORT2
839 res_getArrayItem(const ResourceData
*pResData
, Resource array
, int32_t indexR
) {
840 uint32_t offset
=RES_GET_OFFSET(array
);
844 switch(RES_GET_TYPE(array
)) {
846 if (offset
!=0) { /* empty if offset==0 */
847 const int32_t *p
= pResData
->pRoot
+offset
;
849 return (Resource
)p
[1+indexR
];
855 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
857 return makeResourceFrom16(pResData
, p
[1+indexR
]);
867 uint32_t icu::ResourceArray::internalGetResource(const ResourceData
*pResData
, int32_t i
) const {
868 if (items16
!= NULL
) {
869 return makeResourceFrom16(pResData
, items16
[i
]);
875 UBool
icu::ResourceArray::getValue(int32_t i
, icu::ResourceValue
&value
) const {
876 if(0 <= i
&& i
< length
) {
877 icu::ResourceDataValue
&rdValue
= static_cast<icu::ResourceDataValue
&>(value
);
878 rdValue
.setResource(internalGetResource(rdValue
.pResData
, i
));
885 res_findResource(const ResourceData
*pResData
, Resource r
, char** path
, const char** key
) {
886 char *pathP
= *path
, *nextSepP
= *path
;
887 char *closeIndex
= NULL
;
891 UResType type
= (UResType
)RES_GET_TYPE(t1
);
893 /* if you come in with an empty path, you'll be getting back the same resource */
894 if(!uprv_strlen(pathP
)) {
898 /* one needs to have an aggregate resource in order to search in it */
899 if(!URES_IS_CONTAINER(type
)) {
903 while(nextSepP
&& *pathP
&& t1
!= RES_BOGUS
&& URES_IS_CONTAINER(type
)) {
904 /* Iteration stops if: the path has been consumed, we found a non-existing
905 * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
907 nextSepP
= uprv_strchr(pathP
, RES_PATH_SEPARATOR
);
908 /* if there are more separators, terminate string
909 * and set path to the remaining part of the string
911 if(nextSepP
!= NULL
) {
912 if(nextSepP
== pathP
) {
916 *nextSepP
= 0; /* overwrite the separator with a NUL to terminate the key */
919 *path
= uprv_strchr(pathP
, 0);
922 /* if the resource is a table */
923 /* try the key based access */
924 if(URES_IS_TABLE(type
)) {
926 t2
= res_getTableItemByKey(pResData
, t1
, &indexR
, key
);
927 if(t2
== RES_BOGUS
) {
928 /* if we fail to get the resource by key, maybe we got an index */
929 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
930 if(indexR
>= 0 && *closeIndex
== 0) {
931 /* if we indeed have an index, try to get the item by index */
932 t2
= res_getTableItemByIndex(pResData
, t1
, indexR
, key
);
933 } // else t2 is already RES_BOGUS
935 } else if(URES_IS_ARRAY(type
)) {
936 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
937 if(indexR
>= 0 && *closeIndex
== 0) {
938 t2
= res_getArrayItem(pResData
, t1
, indexR
);
940 t2
= RES_BOGUS
; /* have an array, but don't have a valid index */
943 } else { /* can't do much here, except setting t2 to bogus */
947 type
= (UResType
)RES_GET_TYPE(t1
);
948 /* position pathP to next resource key/index */
955 /* resource bundle swapping ------------------------------------------------- */
958 * Need to always enumerate the entire item tree,
959 * track the lowest address of any item to use as the limit for char keys[],
960 * track the highest address of any item to return the size of the data.
962 * We should have thought of storing those in the data...
963 * It is possible to extend the data structure by putting additional values
964 * in places that are inaccessible by ordinary enumeration of the item tree.
965 * For example, additional integers could be stored at the beginning or
966 * end of the key strings; this could be indicated by a minor version number,
967 * and the data swapping would have to know about these values.
969 * The data structure does not forbid keys to be shared, so we must swap
970 * all keys once instead of each key when it is referenced.
972 * These swapping functions assume that a resource bundle always has a length
973 * that is a multiple of 4 bytes.
974 * Currently, this is trivially true because genrb writes bundle tree leaves
975 * physically first, before their branches, so that the root table with its
976 * array of resource items (uint32_t values) is always last.
979 /* definitions for table sorting ------------------------ */
982 * row of a temporary array
984 * gets platform-endian key string indexes and sorting indexes;
985 * after sorting this array by keys, the actual key/value arrays are permutated
986 * according to the sorting indexes
989 int32_t keyIndex
, sortIndex
;
992 static int32_t U_CALLCONV
993 ures_compareRows(const void *context
, const void *left
, const void *right
) {
994 const char *keyChars
=(const char *)context
;
995 return (int32_t)uprv_strcmp(keyChars
+((const Row
*)left
)->keyIndex
,
996 keyChars
+((const Row
*)right
)->keyIndex
);
999 typedef struct TempTable
{
1000 const char *keyChars
;
1004 int32_t localKeyLimit
;
1005 uint8_t majorFormatVersion
;
1009 STACK_ROW_CAPACITY
=200
1012 /* The table item key string is not locally available. */
1013 static const char *const gUnknownKey
="";
1015 /* resource table key for collation binaries: "%%CollationBin" */
1016 static const UChar gCollationBinKey
[]={
1018 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1024 * swap one resource item
1027 ures_swapResource(const UDataSwapper
*ds
,
1028 const Resource
*inBundle
, Resource
*outBundle
,
1029 Resource res
, /* caller swaps res itself */
1031 TempTable
*pTempTable
,
1032 UErrorCode
*pErrorCode
) {
1035 int32_t offset
, count
;
1037 switch(RES_GET_TYPE(res
)) {
1039 case URES_STRING_V2
:
1042 /* integer, or points to 16-bit units, nothing to do here */
1048 /* all other types use an offset to point to their data */
1049 offset
=(int32_t)RES_GET_OFFSET(res
);
1051 /* special offset indicating an empty item */
1054 if(pTempTable
->resFlags
[offset
>>5]&((uint32_t)1<<(offset
&0x1f))) {
1055 /* we already swapped this resource item */
1058 /* mark it as swapped now */
1059 pTempTable
->resFlags
[offset
>>5]|=((uint32_t)1<<(offset
&0x1f));
1065 switch(RES_GET_TYPE(res
)) {
1067 /* physically same value layout as string, fall through */
1070 count
=udata_readInt32(ds
, (int32_t)*p
);
1072 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
1073 /* swap each UChar (the terminating NUL would not change) */
1074 ds
->swapArray16(ds
, p
+1, 2*count
, q
+1, pErrorCode
);
1077 count
=udata_readInt32(ds
, (int32_t)*p
);
1079 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
1080 /* no need to swap or copy bytes - ures_swap() copied them all */
1082 /* swap known formats */
1083 #if !UCONFIG_NO_COLLATION
1084 if( key
!=NULL
&& /* the binary is in a table */
1086 /* its table key string is "%%CollationBin" */
1087 0==ds
->compareInvChars(ds
, key
, -1,
1088 gCollationBinKey
, UPRV_LENGTHOF(gCollationBinKey
)-1) :
1089 /* its table key string is unknown but it looks like a collation binary */
1090 ucol_looksLikeCollationBinary(ds
, p
+1, count
))
1092 ucol_swap(ds
, p
+1, count
, q
+1, pErrorCode
);
1099 const uint16_t *pKey16
;
1102 const int32_t *pKey32
;
1106 int32_t i
, oldIndex
;
1108 if(RES_GET_TYPE(res
)==URES_TABLE
) {
1109 /* get table item count */
1110 pKey16
=(const uint16_t *)p
;
1111 qKey16
=(uint16_t *)q
;
1112 count
=ds
->readUInt16(*pKey16
);
1117 ds
->swapArray16(ds
, pKey16
++, 2, qKey16
++, pErrorCode
);
1119 offset
+=((1+count
)+1)/2;
1121 /* get table item count */
1122 pKey32
=(const int32_t *)p
;
1123 qKey32
=(int32_t *)q
;
1124 count
=udata_readInt32(ds
, *pKey32
);
1129 ds
->swapArray32(ds
, pKey32
++, 4, qKey32
++, pErrorCode
);
1138 p
=inBundle
+offset
; /* pointer to table resources */
1142 for(i
=0; i
<count
; ++i
) {
1143 const char *itemKey
=gUnknownKey
;
1145 int32_t keyOffset
=ds
->readUInt16(pKey16
[i
]);
1146 if(keyOffset
<pTempTable
->localKeyLimit
) {
1147 itemKey
=(const char *)outBundle
+keyOffset
;
1150 int32_t keyOffset
=udata_readInt32(ds
, pKey32
[i
]);
1152 itemKey
=(const char *)outBundle
+keyOffset
;
1155 item
=ds
->readUInt32(p
[i
]);
1156 ures_swapResource(ds
, inBundle
, outBundle
, item
, itemKey
, pTempTable
, pErrorCode
);
1157 if(U_FAILURE(*pErrorCode
)) {
1158 udata_printError(ds
, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
1164 if(pTempTable
->majorFormatVersion
>1 || ds
->inCharset
==ds
->outCharset
) {
1165 /* no need to sort, just swap the offset/value arrays */
1167 ds
->swapArray16(ds
, pKey16
, count
*2, qKey16
, pErrorCode
);
1168 ds
->swapArray32(ds
, p
, count
*4, q
, pErrorCode
);
1170 /* swap key offsets and items as one array */
1171 ds
->swapArray32(ds
, pKey32
, count
*2*4, qKey32
, pErrorCode
);
1177 * We need to sort tables by outCharset key strings because they
1178 * sort differently for different charset families.
1179 * ures_swap() already set pTempTable->keyChars appropriately.
1180 * First we set up a temporary table with the key indexes and
1181 * sorting indexes and sort that.
1182 * Then we permutate and copy/swap the actual values.
1185 for(i
=0; i
<count
; ++i
) {
1186 pTempTable
->rows
[i
].keyIndex
=ds
->readUInt16(pKey16
[i
]);
1187 pTempTable
->rows
[i
].sortIndex
=i
;
1190 for(i
=0; i
<count
; ++i
) {
1191 pTempTable
->rows
[i
].keyIndex
=udata_readInt32(ds
, pKey32
[i
]);
1192 pTempTable
->rows
[i
].sortIndex
=i
;
1195 uprv_sortArray(pTempTable
->rows
, count
, sizeof(Row
),
1196 ures_compareRows
, pTempTable
->keyChars
,
1198 if(U_FAILURE(*pErrorCode
)) {
1199 udata_printError(ds
, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
1205 * copy/swap/permutate items
1207 * If we swap in-place, then the permutation must use another
1208 * temporary array (pTempTable->resort)
1209 * before the results are copied to the outBundle.
1215 if(pKey16
!=qKey16
) {
1218 rKey16
=(uint16_t *)pTempTable
->resort
;
1220 for(i
=0; i
<count
; ++i
) {
1221 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1222 ds
->swapArray16(ds
, pKey16
+oldIndex
, 2, rKey16
+i
, pErrorCode
);
1224 if(qKey16
!=rKey16
) {
1225 uprv_memcpy(qKey16
, rKey16
, 2*count
);
1230 if(pKey32
!=qKey32
) {
1233 rKey32
=pTempTable
->resort
;
1235 for(i
=0; i
<count
; ++i
) {
1236 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1237 ds
->swapArray32(ds
, pKey32
+oldIndex
, 4, rKey32
+i
, pErrorCode
);
1239 if(qKey32
!=rKey32
) {
1240 uprv_memcpy(qKey32
, rKey32
, 4*count
);
1252 r
=(Resource
*)pTempTable
->resort
;
1254 for(i
=0; i
<count
; ++i
) {
1255 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1256 ds
->swapArray32(ds
, p
+oldIndex
, 4, r
+i
, pErrorCode
);
1259 uprv_memcpy(q
, r
, 4*count
);
1269 count
=udata_readInt32(ds
, (int32_t)*p
);
1271 ds
->swapArray32(ds
, p
++, 4, q
++, pErrorCode
);
1274 for(i
=0; i
<count
; ++i
) {
1275 item
=ds
->readUInt32(p
[i
]);
1276 ures_swapResource(ds
, inBundle
, outBundle
, item
, NULL
, pTempTable
, pErrorCode
);
1277 if(U_FAILURE(*pErrorCode
)) {
1278 udata_printError(ds
, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n",
1285 ds
->swapArray32(ds
, p
, 4*count
, q
, pErrorCode
);
1288 case URES_INT_VECTOR
:
1289 count
=udata_readInt32(ds
, (int32_t)*p
);
1290 /* swap length and each integer */
1291 ds
->swapArray32(ds
, p
, 4*(1+count
), q
, pErrorCode
);
1294 /* also catches RES_BOGUS */
1295 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1300 U_CAPI
int32_t U_EXPORT2
1301 ures_swap(const UDataSwapper
*ds
,
1302 const void *inData
, int32_t length
, void *outData
,
1303 UErrorCode
*pErrorCode
) {
1304 const UDataInfo
*pInfo
;
1305 const Resource
*inBundle
;
1307 int32_t headerSize
, maxTableLength
;
1309 Row rows
[STACK_ROW_CAPACITY
];
1310 int32_t resort
[STACK_ROW_CAPACITY
];
1311 TempTable tempTable
;
1313 const int32_t *inIndexes
;
1315 /* the following integers count Resource item offsets (4 bytes each), not bytes */
1316 int32_t bundleLength
, indexLength
, keysBottom
, keysTop
, resBottom
, top
;
1318 /* udata_swapDataHeader checks the arguments */
1319 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1320 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1324 /* check data format and format version */
1325 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1327 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
1328 pInfo
->dataFormat
[1]==0x65 &&
1329 pInfo
->dataFormat
[2]==0x73 &&
1330 pInfo
->dataFormat
[3]==0x42 &&
1331 /* formatVersion 1.1+ or 2.x or 3.x */
1332 ((pInfo
->formatVersion
[0]==1 && pInfo
->formatVersion
[1]>=1) ||
1333 pInfo
->formatVersion
[0]==2 || pInfo
->formatVersion
[0]==3)
1335 udata_printError(ds
, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n",
1336 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1337 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1338 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1339 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1342 tempTable
.majorFormatVersion
=pInfo
->formatVersion
[0];
1344 /* a resource bundle must contain at least one resource item */
1348 bundleLength
=(length
-headerSize
)/4;
1350 /* formatVersion 1.1 must have a root item and at least 5 indexes */
1351 if(bundleLength
<(1+5)) {
1352 udata_printError(ds
, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
1354 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1359 inBundle
=(const Resource
*)((const char *)inData
+headerSize
);
1360 rootRes
=ds
->readUInt32(*inBundle
);
1362 /* formatVersion 1.1 adds the indexes[] array */
1363 inIndexes
=(const int32_t *)(inBundle
+1);
1365 indexLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_LENGTH
])&0xff;
1366 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
1367 udata_printError(ds
, "ures_swap(): too few indexes for a 1.1+ resource bundle\n");
1368 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1371 keysBottom
=1+indexLength
;
1372 keysTop
=udata_readInt32(ds
, inIndexes
[URES_INDEX_KEYS_TOP
]);
1373 if(indexLength
>URES_INDEX_16BIT_TOP
) {
1374 resBottom
=udata_readInt32(ds
, inIndexes
[URES_INDEX_16BIT_TOP
]);
1378 top
=udata_readInt32(ds
, inIndexes
[URES_INDEX_BUNDLE_TOP
]);
1379 maxTableLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_MAX_TABLE_LENGTH
]);
1381 if(0<=bundleLength
&& bundleLength
<top
) {
1382 udata_printError(ds
, "ures_swap(): resource top %d exceeds bundle length %d\n",
1384 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1387 if(keysTop
>(1+indexLength
)) {
1388 tempTable
.localKeyLimit
=keysTop
<<2;
1390 tempTable
.localKeyLimit
=0;
1394 Resource
*outBundle
=(Resource
*)((char *)outData
+headerSize
);
1396 /* track which resources we have already swapped */
1397 uint32_t stackResFlags
[STACK_ROW_CAPACITY
];
1398 int32_t resFlagsLength
;
1401 * We need one bit per 4 resource bundle bytes so that we can track
1402 * every possible Resource for whether we have swapped it already.
1403 * Multiple Resource words can refer to the same bundle offsets
1404 * for sharing identical values.
1405 * We could optimize this by allocating only for locations above
1406 * where Resource values are stored (above keys & strings).
1408 resFlagsLength
=(length
+31)>>5; /* number of bytes needed */
1409 resFlagsLength
=(resFlagsLength
+3)&~3; /* multiple of 4 bytes for uint32_t */
1410 if(resFlagsLength
<=(int32_t)sizeof(stackResFlags
)) {
1411 tempTable
.resFlags
=stackResFlags
;
1413 tempTable
.resFlags
=(uint32_t *)uprv_malloc(resFlagsLength
);
1414 if(tempTable
.resFlags
==NULL
) {
1415 udata_printError(ds
, "ures_swap(): unable to allocate memory for tracking resources\n");
1416 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1420 uprv_memset(tempTable
.resFlags
, 0, resFlagsLength
);
1422 /* copy the bundle for binary and inaccessible data */
1423 if(inData
!=outData
) {
1424 uprv_memcpy(outBundle
, inBundle
, 4*top
);
1427 /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
1428 udata_swapInvStringBlock(ds
, inBundle
+keysBottom
, 4*(keysTop
-keysBottom
),
1429 outBundle
+keysBottom
, pErrorCode
);
1430 if(U_FAILURE(*pErrorCode
)) {
1431 udata_printError(ds
, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop
-keysBottom
));
1435 /* swap the 16-bit units (strings, table16, array16) */
1436 if(keysTop
<resBottom
) {
1437 ds
->swapArray16(ds
, inBundle
+keysTop
, (resBottom
-keysTop
)*4, outBundle
+keysTop
, pErrorCode
);
1438 if(U_FAILURE(*pErrorCode
)) {
1439 udata_printError(ds
, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom
-keysTop
));
1444 /* allocate the temporary table for sorting resource tables */
1445 tempTable
.keyChars
=(const char *)outBundle
; /* sort by outCharset */
1446 if(tempTable
.majorFormatVersion
>1 || maxTableLength
<=STACK_ROW_CAPACITY
) {
1447 tempTable
.rows
=rows
;
1448 tempTable
.resort
=resort
;
1450 tempTable
.rows
=(Row
*)uprv_malloc(maxTableLength
*sizeof(Row
)+maxTableLength
*4);
1451 if(tempTable
.rows
==NULL
) {
1452 udata_printError(ds
, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
1454 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1455 if(tempTable
.resFlags
!=stackResFlags
) {
1456 uprv_free(tempTable
.resFlags
);
1460 tempTable
.resort
=(int32_t *)(tempTable
.rows
+maxTableLength
);
1463 /* swap the resources */
1464 ures_swapResource(ds
, inBundle
, outBundle
, rootRes
, NULL
, &tempTable
, pErrorCode
);
1465 if(U_FAILURE(*pErrorCode
)) {
1466 udata_printError(ds
, "ures_swapResource(root res=%08x) failed\n",
1470 if(tempTable
.rows
!=rows
) {
1471 uprv_free(tempTable
.rows
);
1473 if(tempTable
.resFlags
!=stackResFlags
) {
1474 uprv_free(tempTable
.resFlags
);
1477 /* swap the root resource and indexes */
1478 ds
->swapArray32(ds
, inBundle
, keysBottom
*4, outBundle
, pErrorCode
);
1481 return headerSize
+4*top
;