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 */
642 switch(RES_GET_TYPE(table
)) {
644 if (offset
!= 0) { /* empty if offset==0 */
645 const uint16_t *p
= (const uint16_t *)(pResData
->pRoot
+offset
);
648 const Resource
*p32
=(const Resource
*)(p
+length
+(~length
&1));
650 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
658 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
662 *key
=RES_GET_KEY16(pResData
, p
[indexR
]);
664 return makeResourceFrom16(pResData
, p
[length
+indexR
]);
669 if (offset
!= 0) { /* empty if offset==0 */
670 const int32_t *p
= pResData
->pRoot
+offset
;
674 *key
=RES_GET_KEY32(pResData
, p
[indexR
]);
676 return (Resource
)p
[length
+indexR
];
687 U_CAPI Resource U_EXPORT2
688 res_getResource(const ResourceData
*pResData
, const char *key
) {
689 const char *realKey
=key
;
691 return res_getTableItemByKey(pResData
, pResData
->rootRes
, &idx
, &realKey
);
694 // TODO: Ported from Java, but enumerating at this low level may prevent us
695 // from doing necessary things, like resolving aliases,
696 // which need access to higher-level UResourceBundle code.
697 // Consider porting the low-level Container/Array/Table classes from Java,
698 // with getters for keys and values,
699 // and doing the enumeration in the higher-level code on top of those accessors.
701 ures_getAllTableItems(const ResourceData
*pResData
, Resource table
,
702 icu::ResourceDataValue
&value
, icu::ResourceTableSink
&sink
,
703 UErrorCode
&errorCode
) {
704 if(U_FAILURE(errorCode
)) { return; }
705 const uint16_t *keys16
= NULL
;
706 const int32_t *keys32
= NULL
;
707 const uint16_t *items16
= NULL
;
708 const Resource
*items32
= NULL
;
709 uint32_t offset
= RES_GET_OFFSET(table
);
711 switch(RES_GET_TYPE(table
)) {
713 if (offset
!= 0) { /* empty if offset==0 */
714 keys16
= (const uint16_t *)(pResData
->pRoot
+offset
);
716 items32
= (const Resource
*)(keys16
+length
+(~length
&1));
721 keys16
= pResData
->p16BitUnits
+offset
;
723 items16
= keys16
+ length
;
727 if (offset
!= 0) { /* empty if offset==0 */
728 keys32
= pResData
->pRoot
+offset
;
730 items32
= (const Resource
*)keys32
+ length
;
735 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
739 for (int32_t i
= 0; i
< length
; ++i
) {
741 if (keys16
!= NULL
) {
742 key
=RES_GET_KEY16(pResData
, keys16
[i
]);
744 key
=RES_GET_KEY32(pResData
, keys32
[i
]);
747 if (items16
!= NULL
) {
748 res
= makeResourceFrom16(pResData
, items16
[i
]);
752 int32_t type
= RES_GET_TYPE(res
);
753 if (URES_IS_ARRAY(type
)) {
754 int32_t numItems
= getArrayLength(pResData
, res
);
755 icu::ResourceArraySink
*subSink
= sink
.getOrCreateArraySink(key
, numItems
, errorCode
);
756 if (subSink
!= NULL
) {
757 ures_getAllArrayItems(pResData
, res
, value
, *subSink
, errorCode
);
759 } else if (URES_IS_TABLE(type
)) {
760 int32_t numItems
= getTableLength(pResData
, res
);
761 icu::ResourceTableSink
*subSink
= sink
.getOrCreateTableSink(key
, numItems
, errorCode
);
762 if (subSink
!= NULL
) {
763 ures_getAllTableItems(pResData
, res
, value
, *subSink
, errorCode
);
765 /* TODO: settle on how to deal with aliases, port to Java
766 } else if (type == URES_ALIAS) {
767 // aliases not handled in resource enumeration
768 errorCode = U_UNSUPPORTED_ERROR;
770 } else if (isNoInheritanceMarker(pResData
, res
)) {
771 sink
.putNoFallback(key
, errorCode
);
773 value
.setResource(res
);
774 sink
.put(key
, value
, errorCode
);
776 if(U_FAILURE(errorCode
)) { return; }
778 sink
.leave(errorCode
);
781 U_CAPI Resource U_EXPORT2
782 res_getArrayItem(const ResourceData
*pResData
, Resource array
, int32_t indexR
) {
783 uint32_t offset
=RES_GET_OFFSET(array
);
784 U_ASSERT(indexR
>=0); /* to ensure the index is not negative */
785 switch(RES_GET_TYPE(array
)) {
787 if (offset
!=0) { /* empty if offset==0 */
788 const int32_t *p
= pResData
->pRoot
+offset
;
790 return (Resource
)p
[1+indexR
];
796 const uint16_t *p
=pResData
->p16BitUnits
+offset
;
798 return makeResourceFrom16(pResData
, p
[1+indexR
]);
809 ures_getAllArrayItems(const ResourceData
*pResData
, Resource array
,
810 icu::ResourceDataValue
&value
, icu::ResourceArraySink
&sink
,
811 UErrorCode
&errorCode
) {
812 if(U_FAILURE(errorCode
)) { return; }
813 const uint16_t *items16
= NULL
;
814 const Resource
*items32
= NULL
;
815 uint32_t offset
=RES_GET_OFFSET(array
);
817 switch(RES_GET_TYPE(array
)) {
819 if (offset
!=0) { /* empty if offset==0 */
820 items32
= (const Resource
*)pResData
->pRoot
+offset
;
826 items16
= pResData
->p16BitUnits
+offset
;
831 errorCode
= U_RESOURCE_TYPE_MISMATCH
;
835 for (int32_t i
= 0; i
< length
; ++i
) {
837 if (items16
!= NULL
) {
838 res
= makeResourceFrom16(pResData
, items16
[i
]);
842 int32_t type
= RES_GET_TYPE(res
);
843 if (URES_IS_ARRAY(type
)) {
844 int32_t numItems
= getArrayLength(pResData
, res
);
845 icu::ResourceArraySink
*subSink
= sink
.getOrCreateArraySink(i
, numItems
, errorCode
);
846 if (subSink
!= NULL
) {
847 ures_getAllArrayItems(pResData
, res
, value
, *subSink
, errorCode
);
849 } else if (URES_IS_TABLE(type
)) {
850 int32_t numItems
= getTableLength(pResData
, res
);
851 icu::ResourceTableSink
*subSink
= sink
.getOrCreateTableSink(i
, numItems
, errorCode
);
852 if (subSink
!= NULL
) {
853 ures_getAllTableItems(pResData
, res
, value
, *subSink
, errorCode
);
855 /* TODO: settle on how to deal with aliases, port to Java
856 } else if (type == URES_ALIAS) {
857 // aliases not handled in resource enumeration
858 errorCode = U_UNSUPPORTED_ERROR;
861 value
.setResource(res
);
862 sink
.put(i
, value
, errorCode
);
864 if(U_FAILURE(errorCode
)) { return; }
866 sink
.leave(errorCode
);
870 res_findResource(const ResourceData
*pResData
, Resource r
, char** path
, const char** key
) {
871 char *pathP
= *path
, *nextSepP
= *path
;
872 char *closeIndex
= NULL
;
876 UResType type
= (UResType
)RES_GET_TYPE(t1
);
878 /* if you come in with an empty path, you'll be getting back the same resource */
879 if(!uprv_strlen(pathP
)) {
883 /* one needs to have an aggregate resource in order to search in it */
884 if(!URES_IS_CONTAINER(type
)) {
888 while(nextSepP
&& *pathP
&& t1
!= RES_BOGUS
&& URES_IS_CONTAINER(type
)) {
889 /* Iteration stops if: the path has been consumed, we found a non-existing
890 * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
892 nextSepP
= uprv_strchr(pathP
, RES_PATH_SEPARATOR
);
893 /* if there are more separators, terminate string
894 * and set path to the remaining part of the string
896 if(nextSepP
!= NULL
) {
897 if(nextSepP
== pathP
) {
901 *nextSepP
= 0; /* overwrite the separator with a NUL to terminate the key */
904 *path
= uprv_strchr(pathP
, 0);
907 /* if the resource is a table */
908 /* try the key based access */
909 if(URES_IS_TABLE(type
)) {
911 t2
= res_getTableItemByKey(pResData
, t1
, &indexR
, key
);
912 if(t2
== RES_BOGUS
) {
913 /* if we fail to get the resource by key, maybe we got an index */
914 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
915 if(*closeIndex
== 0) {
916 /* if we indeed have an index, try to get the item by index */
917 t2
= res_getTableItemByIndex(pResData
, t1
, indexR
, key
);
920 } else if(URES_IS_ARRAY(type
)) {
921 indexR
= uprv_strtol(pathP
, &closeIndex
, 10);
922 if(*closeIndex
== 0) {
923 t2
= res_getArrayItem(pResData
, t1
, indexR
);
925 t2
= RES_BOGUS
; /* have an array, but don't have a valid index */
928 } else { /* can't do much here, except setting t2 to bogus */
932 type
= (UResType
)RES_GET_TYPE(t1
);
933 /* position pathP to next resource key/index */
940 /* resource bundle swapping ------------------------------------------------- */
943 * Need to always enumerate the entire item tree,
944 * track the lowest address of any item to use as the limit for char keys[],
945 * track the highest address of any item to return the size of the data.
947 * We should have thought of storing those in the data...
948 * It is possible to extend the data structure by putting additional values
949 * in places that are inaccessible by ordinary enumeration of the item tree.
950 * For example, additional integers could be stored at the beginning or
951 * end of the key strings; this could be indicated by a minor version number,
952 * and the data swapping would have to know about these values.
954 * The data structure does not forbid keys to be shared, so we must swap
955 * all keys once instead of each key when it is referenced.
957 * These swapping functions assume that a resource bundle always has a length
958 * that is a multiple of 4 bytes.
959 * Currently, this is trivially true because genrb writes bundle tree leaves
960 * physically first, before their branches, so that the root table with its
961 * array of resource items (uint32_t values) is always last.
964 /* definitions for table sorting ------------------------ */
967 * row of a temporary array
969 * gets platform-endian key string indexes and sorting indexes;
970 * after sorting this array by keys, the actual key/value arrays are permutated
971 * according to the sorting indexes
974 int32_t keyIndex
, sortIndex
;
978 ures_compareRows(const void *context
, const void *left
, const void *right
) {
979 const char *keyChars
=(const char *)context
;
980 return (int32_t)uprv_strcmp(keyChars
+((const Row
*)left
)->keyIndex
,
981 keyChars
+((const Row
*)right
)->keyIndex
);
984 typedef struct TempTable
{
985 const char *keyChars
;
989 int32_t localKeyLimit
;
990 uint8_t majorFormatVersion
;
994 STACK_ROW_CAPACITY
=200
997 /* The table item key string is not locally available. */
998 static const char *const gUnknownKey
="";
1000 /* resource table key for collation binaries: "%%CollationBin" */
1001 static const UChar gCollationBinKey
[]={
1003 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1009 * swap one resource item
1012 ures_swapResource(const UDataSwapper
*ds
,
1013 const Resource
*inBundle
, Resource
*outBundle
,
1014 Resource res
, /* caller swaps res itself */
1016 TempTable
*pTempTable
,
1017 UErrorCode
*pErrorCode
) {
1020 int32_t offset
, count
;
1022 switch(RES_GET_TYPE(res
)) {
1024 case URES_STRING_V2
:
1027 /* integer, or points to 16-bit units, nothing to do here */
1033 /* all other types use an offset to point to their data */
1034 offset
=(int32_t)RES_GET_OFFSET(res
);
1036 /* special offset indicating an empty item */
1039 if(pTempTable
->resFlags
[offset
>>5]&((uint32_t)1<<(offset
&0x1f))) {
1040 /* we already swapped this resource item */
1043 /* mark it as swapped now */
1044 pTempTable
->resFlags
[offset
>>5]|=((uint32_t)1<<(offset
&0x1f));
1050 switch(RES_GET_TYPE(res
)) {
1052 /* physically same value layout as string, fall through */
1055 count
=udata_readInt32(ds
, (int32_t)*p
);
1057 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
1058 /* swap each UChar (the terminating NUL would not change) */
1059 ds
->swapArray16(ds
, p
+1, 2*count
, q
+1, pErrorCode
);
1062 count
=udata_readInt32(ds
, (int32_t)*p
);
1064 ds
->swapArray32(ds
, p
, 4, q
, pErrorCode
);
1065 /* no need to swap or copy bytes - ures_swap() copied them all */
1067 /* swap known formats */
1068 #if !UCONFIG_NO_COLLATION
1069 if( key
!=NULL
&& /* the binary is in a table */
1071 /* its table key string is "%%CollationBin" */
1072 0==ds
->compareInvChars(ds
, key
, -1,
1073 gCollationBinKey
, UPRV_LENGTHOF(gCollationBinKey
)-1) :
1074 /* its table key string is unknown but it looks like a collation binary */
1075 ucol_looksLikeCollationBinary(ds
, p
+1, count
))
1077 ucol_swap(ds
, p
+1, count
, q
+1, pErrorCode
);
1084 const uint16_t *pKey16
;
1087 const int32_t *pKey32
;
1091 int32_t i
, oldIndex
;
1093 if(RES_GET_TYPE(res
)==URES_TABLE
) {
1094 /* get table item count */
1095 pKey16
=(const uint16_t *)p
;
1096 qKey16
=(uint16_t *)q
;
1097 count
=ds
->readUInt16(*pKey16
);
1102 ds
->swapArray16(ds
, pKey16
++, 2, qKey16
++, pErrorCode
);
1104 offset
+=((1+count
)+1)/2;
1106 /* get table item count */
1107 pKey32
=(const int32_t *)p
;
1108 qKey32
=(int32_t *)q
;
1109 count
=udata_readInt32(ds
, *pKey32
);
1114 ds
->swapArray32(ds
, pKey32
++, 4, qKey32
++, pErrorCode
);
1123 p
=inBundle
+offset
; /* pointer to table resources */
1127 for(i
=0; i
<count
; ++i
) {
1128 const char *itemKey
=gUnknownKey
;
1130 int32_t keyOffset
=ds
->readUInt16(pKey16
[i
]);
1131 if(keyOffset
<pTempTable
->localKeyLimit
) {
1132 itemKey
=(const char *)outBundle
+keyOffset
;
1135 int32_t keyOffset
=udata_readInt32(ds
, pKey32
[i
]);
1137 itemKey
=(const char *)outBundle
+keyOffset
;
1140 item
=ds
->readUInt32(p
[i
]);
1141 ures_swapResource(ds
, inBundle
, outBundle
, item
, itemKey
, pTempTable
, pErrorCode
);
1142 if(U_FAILURE(*pErrorCode
)) {
1143 udata_printError(ds
, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
1149 if(pTempTable
->majorFormatVersion
>1 || ds
->inCharset
==ds
->outCharset
) {
1150 /* no need to sort, just swap the offset/value arrays */
1152 ds
->swapArray16(ds
, pKey16
, count
*2, qKey16
, pErrorCode
);
1153 ds
->swapArray32(ds
, p
, count
*4, q
, pErrorCode
);
1155 /* swap key offsets and items as one array */
1156 ds
->swapArray32(ds
, pKey32
, count
*2*4, qKey32
, pErrorCode
);
1162 * We need to sort tables by outCharset key strings because they
1163 * sort differently for different charset families.
1164 * ures_swap() already set pTempTable->keyChars appropriately.
1165 * First we set up a temporary table with the key indexes and
1166 * sorting indexes and sort that.
1167 * Then we permutate and copy/swap the actual values.
1170 for(i
=0; i
<count
; ++i
) {
1171 pTempTable
->rows
[i
].keyIndex
=ds
->readUInt16(pKey16
[i
]);
1172 pTempTable
->rows
[i
].sortIndex
=i
;
1175 for(i
=0; i
<count
; ++i
) {
1176 pTempTable
->rows
[i
].keyIndex
=udata_readInt32(ds
, pKey32
[i
]);
1177 pTempTable
->rows
[i
].sortIndex
=i
;
1180 uprv_sortArray(pTempTable
->rows
, count
, sizeof(Row
),
1181 ures_compareRows
, pTempTable
->keyChars
,
1183 if(U_FAILURE(*pErrorCode
)) {
1184 udata_printError(ds
, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
1190 * copy/swap/permutate items
1192 * If we swap in-place, then the permutation must use another
1193 * temporary array (pTempTable->resort)
1194 * before the results are copied to the outBundle.
1200 if(pKey16
!=qKey16
) {
1203 rKey16
=(uint16_t *)pTempTable
->resort
;
1205 for(i
=0; i
<count
; ++i
) {
1206 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1207 ds
->swapArray16(ds
, pKey16
+oldIndex
, 2, rKey16
+i
, pErrorCode
);
1209 if(qKey16
!=rKey16
) {
1210 uprv_memcpy(qKey16
, rKey16
, 2*count
);
1215 if(pKey32
!=qKey32
) {
1218 rKey32
=pTempTable
->resort
;
1220 for(i
=0; i
<count
; ++i
) {
1221 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1222 ds
->swapArray32(ds
, pKey32
+oldIndex
, 4, rKey32
+i
, pErrorCode
);
1224 if(qKey32
!=rKey32
) {
1225 uprv_memcpy(qKey32
, rKey32
, 4*count
);
1237 r
=(Resource
*)pTempTable
->resort
;
1239 for(i
=0; i
<count
; ++i
) {
1240 oldIndex
=pTempTable
->rows
[i
].sortIndex
;
1241 ds
->swapArray32(ds
, p
+oldIndex
, 4, r
+i
, pErrorCode
);
1244 uprv_memcpy(q
, r
, 4*count
);
1254 count
=udata_readInt32(ds
, (int32_t)*p
);
1256 ds
->swapArray32(ds
, p
++, 4, q
++, pErrorCode
);
1259 for(i
=0; i
<count
; ++i
) {
1260 item
=ds
->readUInt32(p
[i
]);
1261 ures_swapResource(ds
, inBundle
, outBundle
, item
, NULL
, pTempTable
, pErrorCode
);
1262 if(U_FAILURE(*pErrorCode
)) {
1263 udata_printError(ds
, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n",
1270 ds
->swapArray32(ds
, p
, 4*count
, q
, pErrorCode
);
1273 case URES_INT_VECTOR
:
1274 count
=udata_readInt32(ds
, (int32_t)*p
);
1275 /* swap length and each integer */
1276 ds
->swapArray32(ds
, p
, 4*(1+count
), q
, pErrorCode
);
1279 /* also catches RES_BOGUS */
1280 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1285 U_CAPI
int32_t U_EXPORT2
1286 ures_swap(const UDataSwapper
*ds
,
1287 const void *inData
, int32_t length
, void *outData
,
1288 UErrorCode
*pErrorCode
) {
1289 const UDataInfo
*pInfo
;
1290 const Resource
*inBundle
;
1292 int32_t headerSize
, maxTableLength
;
1294 Row rows
[STACK_ROW_CAPACITY
];
1295 int32_t resort
[STACK_ROW_CAPACITY
];
1296 TempTable tempTable
;
1298 const int32_t *inIndexes
;
1300 /* the following integers count Resource item offsets (4 bytes each), not bytes */
1301 int32_t bundleLength
, indexLength
, keysBottom
, keysTop
, resBottom
, top
;
1303 /* udata_swapDataHeader checks the arguments */
1304 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1305 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1309 /* check data format and format version */
1310 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1312 pInfo
->dataFormat
[0]==0x52 && /* dataFormat="ResB" */
1313 pInfo
->dataFormat
[1]==0x65 &&
1314 pInfo
->dataFormat
[2]==0x73 &&
1315 pInfo
->dataFormat
[3]==0x42 &&
1316 /* formatVersion 1.1+ or 2.x or 3.x */
1317 ((pInfo
->formatVersion
[0]==1 && pInfo
->formatVersion
[1]>=1) ||
1318 pInfo
->formatVersion
[0]==2 || pInfo
->formatVersion
[0]==3)
1320 udata_printError(ds
, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n",
1321 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1322 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1323 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1324 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1327 tempTable
.majorFormatVersion
=pInfo
->formatVersion
[0];
1329 /* a resource bundle must contain at least one resource item */
1333 bundleLength
=(length
-headerSize
)/4;
1335 /* formatVersion 1.1 must have a root item and at least 5 indexes */
1336 if(bundleLength
<(1+5)) {
1337 udata_printError(ds
, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
1339 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1344 inBundle
=(const Resource
*)((const char *)inData
+headerSize
);
1345 rootRes
=ds
->readUInt32(*inBundle
);
1347 /* formatVersion 1.1 adds the indexes[] array */
1348 inIndexes
=(const int32_t *)(inBundle
+1);
1350 indexLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_LENGTH
])&0xff;
1351 if(indexLength
<=URES_INDEX_MAX_TABLE_LENGTH
) {
1352 udata_printError(ds
, "ures_swap(): too few indexes for a 1.1+ resource bundle\n");
1353 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1356 keysBottom
=1+indexLength
;
1357 keysTop
=udata_readInt32(ds
, inIndexes
[URES_INDEX_KEYS_TOP
]);
1358 if(indexLength
>URES_INDEX_16BIT_TOP
) {
1359 resBottom
=udata_readInt32(ds
, inIndexes
[URES_INDEX_16BIT_TOP
]);
1363 top
=udata_readInt32(ds
, inIndexes
[URES_INDEX_BUNDLE_TOP
]);
1364 maxTableLength
=udata_readInt32(ds
, inIndexes
[URES_INDEX_MAX_TABLE_LENGTH
]);
1366 if(0<=bundleLength
&& bundleLength
<top
) {
1367 udata_printError(ds
, "ures_swap(): resource top %d exceeds bundle length %d\n",
1369 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1372 if(keysTop
>(1+indexLength
)) {
1373 tempTable
.localKeyLimit
=keysTop
<<2;
1375 tempTable
.localKeyLimit
=0;
1379 Resource
*outBundle
=(Resource
*)((char *)outData
+headerSize
);
1381 /* track which resources we have already swapped */
1382 uint32_t stackResFlags
[STACK_ROW_CAPACITY
];
1383 int32_t resFlagsLength
;
1386 * We need one bit per 4 resource bundle bytes so that we can track
1387 * every possible Resource for whether we have swapped it already.
1388 * Multiple Resource words can refer to the same bundle offsets
1389 * for sharing identical values.
1390 * We could optimize this by allocating only for locations above
1391 * where Resource values are stored (above keys & strings).
1393 resFlagsLength
=(length
+31)>>5; /* number of bytes needed */
1394 resFlagsLength
=(resFlagsLength
+3)&~3; /* multiple of 4 bytes for uint32_t */
1395 if(resFlagsLength
<=(int32_t)sizeof(stackResFlags
)) {
1396 tempTable
.resFlags
=stackResFlags
;
1398 tempTable
.resFlags
=(uint32_t *)uprv_malloc(resFlagsLength
);
1399 if(tempTable
.resFlags
==NULL
) {
1400 udata_printError(ds
, "ures_swap(): unable to allocate memory for tracking resources\n");
1401 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1405 uprv_memset(tempTable
.resFlags
, 0, resFlagsLength
);
1407 /* copy the bundle for binary and inaccessible data */
1408 if(inData
!=outData
) {
1409 uprv_memcpy(outBundle
, inBundle
, 4*top
);
1412 /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
1413 udata_swapInvStringBlock(ds
, inBundle
+keysBottom
, 4*(keysTop
-keysBottom
),
1414 outBundle
+keysBottom
, pErrorCode
);
1415 if(U_FAILURE(*pErrorCode
)) {
1416 udata_printError(ds
, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop
-keysBottom
));
1420 /* swap the 16-bit units (strings, table16, array16) */
1421 if(keysTop
<resBottom
) {
1422 ds
->swapArray16(ds
, inBundle
+keysTop
, (resBottom
-keysTop
)*4, outBundle
+keysTop
, pErrorCode
);
1423 if(U_FAILURE(*pErrorCode
)) {
1424 udata_printError(ds
, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom
-keysTop
));
1429 /* allocate the temporary table for sorting resource tables */
1430 tempTable
.keyChars
=(const char *)outBundle
; /* sort by outCharset */
1431 if(tempTable
.majorFormatVersion
>1 || maxTableLength
<=STACK_ROW_CAPACITY
) {
1432 tempTable
.rows
=rows
;
1433 tempTable
.resort
=resort
;
1435 tempTable
.rows
=(Row
*)uprv_malloc(maxTableLength
*sizeof(Row
)+maxTableLength
*4);
1436 if(tempTable
.rows
==NULL
) {
1437 udata_printError(ds
, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
1439 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1440 if(tempTable
.resFlags
!=stackResFlags
) {
1441 uprv_free(tempTable
.resFlags
);
1445 tempTable
.resort
=(int32_t *)(tempTable
.rows
+maxTableLength
);
1448 /* swap the resources */
1449 ures_swapResource(ds
, inBundle
, outBundle
, rootRes
, NULL
, &tempTable
, pErrorCode
);
1450 if(U_FAILURE(*pErrorCode
)) {
1451 udata_printError(ds
, "ures_swapResource(root res=%08x) failed\n",
1455 if(tempTable
.rows
!=rows
) {
1456 uprv_free(tempTable
.rows
);
1458 if(tempTable
.resFlags
!=stackResFlags
) {
1459 uprv_free(tempTable
.resFlags
);
1462 /* swap the root resource and indexes */
1463 ds
->swapArray32(ds
, inBundle
, keysBottom
*4, outBundle
, pErrorCode
);
1466 return headerSize
+4*top
;