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
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: uresdata.h
10 * tab size: 8 (not used)
13 * created on: 1999dec08
14 * created by: Markus W. Scherer
15 * 06/24/02 weiv Added support for resource sharing
21 #include "unicode/utypes.h"
22 #include "unicode/udata.h"
23 #include "unicode/ures.h"
28 * Numeric constants for internal-only types of resource items.
29 * These must use different numeric values than UResType constants
30 * because they are used together.
31 * Internal types are never returned by ures_getType().
34 /** Include a negative value so that the compiler uses the same int type as for UResType. */
35 URES_INTERNAL_NONE
=-1,
37 /** Resource type constant for tables with 32-bit count, key offsets and values. */
41 * Resource type constant for tables with 16-bit count, key offsets and values.
42 * All values are URES_STRING_V2 strings.
46 /** Resource type constant for 16-bit Unicode strings in formatVersion 2. */
50 * Resource type constant for arrays with 16-bit count and values.
51 * All values are URES_STRING_V2 strings.
55 /* Resource type 15 is not defined but effectively used by RES_BOGUS=0xffffffff. */
59 * A Resource is a 32-bit value that has 2 bit fields:
60 * 31..28 4-bit type, see enum below
61 * 27..0 28-bit four-byte-offset or value according to the type
63 typedef uint32_t Resource
;
65 #define RES_BOGUS 0xffffffff
66 #define RES_MAX_OFFSET 0x0fffffff
68 #define RES_GET_TYPE(res) ((int32_t)((res)>>28UL))
69 #define RES_GET_OFFSET(res) ((res)&0x0fffffff)
70 #define RES_GET_POINTER(pRoot, res) ((pRoot)+RES_GET_OFFSET(res))
72 /* get signed and unsigned integer values directly from the Resource handle
73 * NOTE: For proper logging, please use the res_getInt() constexpr
75 #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
76 # define RES_GET_INT_NO_TRACE(res) (((int32_t)((res)<<4L))>>4L)
78 # define RES_GET_INT_NO_TRACE(res) (int32_t)(((res)&0x08000000) ? (res)|0xf0000000 : (res)&0x07ffffff)
81 #define RES_GET_UINT_NO_TRACE(res) ((res)&0x0fffffff)
83 #define URES_IS_ARRAY(type) ((int32_t)(type)==URES_ARRAY || (int32_t)(type)==URES_ARRAY16)
84 #define URES_IS_TABLE(type) ((int32_t)(type)==URES_TABLE || (int32_t)(type)==URES_TABLE16 || (int32_t)(type)==URES_TABLE32)
85 #define URES_IS_CONTAINER(type) (URES_IS_TABLE(type) || URES_IS_ARRAY(type))
87 #define URES_MAKE_RESOURCE(type, offset) (((Resource)(type)<<28)|(Resource)(offset))
88 #define URES_MAKE_EMPTY_RESOURCE(type) ((Resource)(type)<<28)
90 /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
93 * [0] contains the length of indexes[]
94 * which is at most URES_INDEX_TOP of the latest format version
96 * formatVersion==1: all bits contain the length of indexes[]
97 * but the length is much less than 0xff;
99 * only bits 7..0 contain the length of indexes[],
100 * bits 31..8 are reserved and set to 0
102 * bits 31..8 poolStringIndexLimit bits 23..0
106 * [1] contains the top of the key strings,
107 * same as the bottom of resources or UTF-16 strings, rounded up
110 /** [2] contains the top of all resources */
111 URES_INDEX_RESOURCES_TOP
,
113 * [3] contains the top of the bundle,
114 * in case it were ever different from [2]
116 URES_INDEX_BUNDLE_TOP
,
117 /** [4] max. length of any table */
118 URES_INDEX_MAX_TABLE_LENGTH
,
120 * [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2)
123 * bits 31..16 poolStringIndex16Limit
124 * bits 15..12 poolStringIndexLimit bits 27..24
126 URES_INDEX_ATTRIBUTES
,
128 * [6] top of the 16-bit units (UTF-16 string v2 UChars, URES_TABLE16, URES_ARRAY16),
129 * rounded up (new in formatVersion 2.0, ICU 4.4)
131 URES_INDEX_16BIT_TOP
,
132 /** [7] checksum of the pool bundle (new in formatVersion 2.0, ICU 4.4) */
133 URES_INDEX_POOL_CHECKSUM
,
138 * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES].
139 * New in formatVersion 1.2 (ICU 3.6).
141 * If set, then this resource bundle is a standalone bundle.
142 * If not set, then the bundle participates in locale fallback, eventually
143 * all the way to the root bundle.
144 * If indexes[] is missing or too short, then the attribute cannot be determined
145 * reliably. Dependency checking should ignore such bundles, and loading should
148 #define URES_ATT_NO_FALLBACK 1
151 * Attributes for bundles that are, or use, a pool bundle.
152 * A pool bundle provides key strings that are shared among several other bundles
153 * to reduce their total size.
154 * New in formatVersion 2 (ICU 4.4).
156 #define URES_ATT_IS_POOL_BUNDLE 2
157 #define URES_ATT_USES_POOL_BUNDLE 4
160 * File format for .res resource bundle files
162 * ICU 56: New in formatVersion 3 compared with 2: -------------
164 * Resource bundles can optionally use shared string-v2 values
165 * stored in the pool bundle.
166 * If so, then the indexes[] contain two new values
167 * in previously-unused bits of existing indexes[] slots:
168 * - poolStringIndexLimit:
169 * String-v2 offsets (in 32-bit Resource words) below this limit
170 * point to pool bundle string-v2 values.
171 * - poolStringIndex16Limit:
172 * Resource16 string-v2 offsets below this limit
173 * point to pool bundle string-v2 values.
174 * Guarantee: poolStringIndex16Limit <= poolStringIndexLimit
176 * The local bundle's poolStringIndexLimit is greater than
177 * any pool bundle string index used in the local bundle.
178 * The poolStringIndexLimit should not be greater than
179 * the maximum possible pool bundle string index.
181 * The maximum possible pool bundle string index is the index to the last non-NUL
182 * pool string character, due to suffix sharing.
184 * In the pool bundle, there is no structure that lists the strings.
185 * (The root resource is an empty Table.)
186 * If the strings need to be enumerated (as genrb --usePoolBundle does),
187 * then iterate through the pool bundle's 16-bit-units array from the beginning.
188 * Stop at the end of the array, or when an explicit or implicit string length
189 * would lead beyond the end of the array,
190 * or when an apparent string is not NUL-terminated.
191 * (Future genrb version might terminate the strings with
192 * what looks like a large explicit string length.)
194 * ICU 4.4: New in formatVersion 2 compared with 1.3: -------------
196 * Three new resource types -- String-v2, Table16 and Array16 -- have their
197 * values stored in a new array of 16-bit units between the table key strings
198 * and the start of the other resources.
200 * genrb eliminates duplicates among Unicode string-v2 values.
201 * Multiple Unicode strings may use the same offset and string data,
202 * or a short string may point to the suffix of a longer string. ("Suffix sharing")
203 * For example, one string "abc" may be reused for another string "bc" by pointing
204 * to the second character. (Short strings-v2 are NUL-terminated
205 * and not preceded by an explicit length value.)
207 * It is allowed for all resource types to share values.
208 * The swapper code (ures_swap()) has been modified so that it swaps each item
211 * A resource bundle may use a special pool bundle. Some or all of the table key strings
212 * of the using-bundle are omitted, and the key string offsets for such key strings refer
213 * to offsets in the pool bundle.
214 * The using-bundle's and the pool-bundle's indexes[URES_INDEX_POOL_CHECKSUM] values
216 * Two bits in indexes[URES_INDEX_ATTRIBUTES] indicate whether a resource bundle
217 * is or uses a pool bundle.
219 * Table key strings must be compared in ASCII order, even if they are not
222 * New in formatVersion 1.3 compared with 1.2: -------------
224 * genrb eliminates duplicates among key strings.
225 * Multiple table items may share one key string, or one item may point
226 * to the suffix of another's key string. ("Suffix sharing")
227 * For example, one key "abc" may be reused for another key "bc" by pointing
228 * to the second character. (Key strings are NUL-terminated.)
232 * An ICU4C resource bundle file (.res) is a binary, memory-mappable file
233 * with nested, hierarchical data structures.
234 * It physically contains the following:
236 * Resource root; -- 32-bit Resource item, root item for this bundle's tree;
237 * currently, the root item must be a table or table32 resource item
238 * int32_t indexes[indexes[0]]; -- array of indexes for friendly
239 * reading and swapping; see URES_INDEX_* above
240 * new in formatVersion 1.1 (ICU 2.8)
241 * char keys[]; -- characters for key strings
242 * (formatVersion 1.0: up to 65k of characters; 1.1: <2G)
243 * (minus the space for root and indexes[]),
244 * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated;
245 * padded to multiple of 4 bytes for 4-alignment of the following data
246 * uint16_t 16BitUnits[]; -- resources that are stored entirely as sequences of 16-bit units
247 * (new in formatVersion 2/ICU 4.4)
248 * data is indexed by the offset values in 16-bit resource types,
249 * with offset 0 pointing to the beginning of this array;
250 * there is a 0 at offset 0, for empty resources;
251 * padded to multiple of 4 bytes for 4-alignment of the following data
252 * data; -- data directly and indirectly indexed by the root item;
253 * the structure is determined by walking the tree
255 * Each resource bundle item has a 32-bit Resource handle (see typedef above)
256 * which contains the item type number in its upper 4 bits (31..28) and either
257 * an offset or a direct value in its lower 28 bits (27..0).
258 * The order of items is undefined and only determined by walking the tree.
259 * Leaves of the tree may be stored first or last or anywhere in between,
260 * and it is in theory possible to have unreferenced holes in the file.
262 * 16-bit-unit values:
263 * Starting with formatVersion 2/ICU 4.4, some resources are stored in a special
264 * array of 16-bit units. Each resource value is a sequence of 16-bit units,
265 * with no per-resource padding to a 4-byte boundary.
266 * 16-bit container types (Table16 and Array16) contain Resource16 values
267 * which are offsets to String-v2 resources in the same 16-bit-units array.
270 * - Empty Unicode strings have an offset value of 0 in the Resource handle itself.
271 * - Starting with formatVersion 2/ICU 4.4, an offset value of 0 for
272 * _any_ resource type indicates an empty value.
273 * - Integer values are 28-bit values stored in the Resource handle itself;
274 * the interpretation of unsigned vs. signed integers is up to the application.
276 * All other types and values use 28-bit offsets to point to the item's data.
277 * The offset is an index to the first 32-bit word of the value, relative to the
278 * start of the resource data (i.e., the root item handle is at offset 0).
279 * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits).
280 * All resource item values are 4-aligned.
282 * New in formatVersion 2/ICU 4.4: Some types use offsets into the 16-bit-units array,
283 * indexing 16-bit units in that array.
285 * The structures (memory layouts) for the values for each item type are listed
286 * in the table below.
288 * Nested, hierarchical structures: -------------
290 * Table items contain key-value pairs where the keys are offsets to char * key strings.
291 * The values of these pairs are either Resource handles or
292 * offsets into the 16-bit-units array, depending on the table type.
294 * Array items are simple vectors of Resource handles,
295 * or of offsets into the 16-bit-units array, depending on the array type.
297 * Table key string offsets: -------
299 * Key string offsets are relative to the start of the resource data (of the root handle),
300 * i.e., the first string has an offset of 4+sizeof(indexes).
301 * (After the 4-byte root handle and after the indexes array.)
303 * If the resource bundle uses a pool bundle, then some key strings are stored
304 * in the pool bundle rather than in the local bundle itself.
305 * - In a Table or Table16, the 16-bit key string offset is local if it is
306 * less than indexes[URES_INDEX_KEYS_TOP]<<2.
307 * Otherwise, subtract indexes[URES_INDEX_KEYS_TOP]<<2 to get the offset into
308 * the pool bundle key strings.
309 * - In a Table32, the 32-bit key string offset is local if it is non-negative.
310 * Otherwise, reset bit 31 to get the pool key string offset.
312 * Unlike the local offset, the pool key offset is relative to
313 * the start of the key strings, not to the start of the bundle.
315 * An alias item is special (and new in ICU 2.4): --------------
317 * Its memory layout is just like for a UnicodeString, but at runtime it resolves to
318 * another resource bundle's item according to the path in the string.
319 * This is used to share items across bundles that are in different lookup/fallback
320 * chains (e.g., large collation data among zh_TW and zh_HK).
321 * This saves space (for large items) and maintenance effort (less duplication of data).
323 * --------------------------------------------------------------------------
327 * Most resources have their values stored at four-byte offsets from the start
328 * of the resource data. These values are at least 4-aligned.
329 * Some resource values are stored directly in the offset field of the Resource itself.
330 * See UResType in unicode/ures.h for enumeration constants for Resource types.
332 * Some resources have their values stored as sequences of 16-bit units,
333 * at 2-byte offsets from the start of a contiguous 16-bit-unit array between
334 * the table key strings and the other resources. (new in formatVersion 2/ICU 4.4)
335 * At offset 0 of that array is a 16-bit zero value for empty 16-bit resources.
337 * Resource16 values in Table16 and Array16 are 16-bit offsets to String-v2
338 * resources, with the offsets relative to the start of the 16-bit-units array.
339 * Starting with formatVersion 3/ICU 56, if offset<poolStringIndex16Limit
340 * then use the pool bundle's 16-bit-units array,
341 * otherwise subtract that limit and use the local 16-bit-units array.
343 * Type Name Memory layout of values
344 * (in parentheses: scalar, non-offset values)
346 * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding)
347 * or (empty string ("") if offset==0)
348 * 1 Binary: int32_t length, uint8_t[length], (padding)
349 * - the start of the bytes is 16-aligned -
350 * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count]
351 * 3 Alias: (physically same value layout as string, new in ICU 2.4)
352 * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count]
353 * (new in formatVersion 1.1/ICU 2.8)
354 * 5 Table16: uint16_t count, uint16_t keyStringOffsets[count], Resource16[count]
355 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
356 * 6 Unicode String-v2:UChar[length], (UChar)0; length determined by the first UChar:
357 * - if first is not a trail surrogate, then the length is implicit
358 * and u_strlen() needs to be called
359 * - if first<0xdfef then length=first&0x3ff (and skip first)
360 * - if first<0xdfff then length=((first-0xdfef)<<16) | second UChar
361 * - if first==0xdfff then length=((second UChar)<<16) | third UChar
362 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
364 * Starting with formatVersion 3/ICU 56, if offset<poolStringIndexLimit
365 * then use the pool bundle's 16-bit-units array,
366 * otherwise subtract that limit and use the local 16-bit-units array.
367 * (Note different limits for Resource16 vs. Resource.)
369 * 7 Integer: (28-bit offset is integer value)
370 * 8 Array: int32_t count, Resource[count]
371 * 9 Array16: uint16_t count, Resource16[count]
372 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
373 * 14 Integer Vector: int32_t length, int32_t[length]
374 * 15 Reserved: This value denotes special purpose resources and is for internal use.
376 * Note that there are 3 types with data vector values:
377 * - Vectors of 8-bit bytes stored as type Binary.
378 * - Vectors of 16-bit words stored as type Unicode String or Unicode String-v2
379 * (no value restrictions, all values 0..ffff allowed!).
380 * - Vectors of 32-bit words stored as type Integer Vector.
384 * Structure for a single, memory-mapped ResourceBundle.
386 typedef struct ResourceData
{
388 const int32_t *pRoot
;
389 const uint16_t *p16BitUnits
;
390 const char *poolBundleKeys
;
392 int32_t localKeyLimit
;
393 const uint16_t *poolBundleStrings
;
394 int32_t poolStringIndexLimit
;
395 int32_t poolStringIndex16Limit
;
396 UBool noFallback
; /* see URES_ATT_NO_FALLBACK */
398 UBool usesPoolBundle
;
399 UBool useNativeStrcmp
;
403 * Read a resource bundle from memory.
405 U_INTERNAL
void U_EXPORT2
406 res_read(ResourceData
*pResData
,
407 const UDataInfo
*pInfo
, const void *inBytes
, int32_t length
,
408 UErrorCode
*errorCode
);
411 * Load a resource bundle file.
412 * The ResourceData structure must be allocated externally.
415 res_load(ResourceData
*pResData
,
416 const char *path
, const char *name
, UErrorCode
*errorCode
);
419 * Release a resource bundle file.
420 * This does not release the ResourceData structure itself.
423 res_unload(ResourceData
*pResData
);
425 U_INTERNAL UResType U_EXPORT2
426 res_getPublicType(Resource res
);
428 ///////////////////////////////////////////////////////////////////////////
429 // To enable tracing, use the inline versions of the res_get* functions. //
430 ///////////////////////////////////////////////////////////////////////////
433 * Return a pointer to a zero-terminated, const UChar* string
434 * and set its length in *pLength.
435 * Returns NULL if not found.
437 U_INTERNAL
const UChar
* U_EXPORT2
438 res_getStringNoTrace(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
440 U_INTERNAL
const uint8_t * U_EXPORT2
441 res_getBinaryNoTrace(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
443 U_INTERNAL
const int32_t * U_EXPORT2
444 res_getIntVectorNoTrace(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
446 U_INTERNAL
const UChar
* U_EXPORT2
447 res_getAlias(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
449 U_INTERNAL Resource U_EXPORT2
450 res_getResource(const ResourceData
*pResData
, const char *key
);
452 U_INTERNAL
int32_t U_EXPORT2
453 res_countArrayItems(const ResourceData
*pResData
, Resource res
);
455 U_INTERNAL Resource U_EXPORT2
456 res_getArrayItem(const ResourceData
*pResData
, Resource array
, int32_t indexS
);
458 U_INTERNAL Resource U_EXPORT2
459 res_getTableItemByIndex(const ResourceData
*pResData
, Resource table
, int32_t indexS
, const char ** key
);
461 U_INTERNAL Resource U_EXPORT2
462 res_getTableItemByKey(const ResourceData
*pResData
, Resource table
, int32_t *indexS
, const char* * key
);
465 * Iterates over the path and stops when a scalar resource is found.
467 * Modifies the contents of *path (replacing separators with NULs),
468 * and also moves *path forward while it finds items.
470 * @param path input: "CollationElements/Sequence" or "zoneStrings/3/2" etc.;
471 * output: points to the part that has not yet been processed
473 U_CFUNC Resource
res_findResource(const ResourceData
*pResData
, Resource r
,
474 char** path
, const char** key
);
478 #include "resource.h"
479 #include "restrace.h"
483 inline const UChar
* res_getString(const ResourceTracer
& traceInfo
,
484 const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
485 traceInfo
.trace("string");
486 return res_getStringNoTrace(pResData
, res
, pLength
);
489 inline const uint8_t* res_getBinary(const ResourceTracer
& traceInfo
,
490 const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
491 traceInfo
.trace("binary");
492 return res_getBinaryNoTrace(pResData
, res
, pLength
);
495 inline const int32_t* res_getIntVector(const ResourceTracer
& traceInfo
,
496 const ResourceData
*pResData
, Resource res
, int32_t *pLength
) {
497 traceInfo
.trace("intvector");
498 return res_getIntVectorNoTrace(pResData
, res
, pLength
);
501 inline int32_t res_getInt(const ResourceTracer
& traceInfo
, Resource res
) {
502 traceInfo
.trace("int");
503 return RES_GET_INT_NO_TRACE(res
);
506 inline uint32_t res_getUInt(const ResourceTracer
& traceInfo
, Resource res
) {
507 traceInfo
.trace("uint");
508 return RES_GET_UINT_NO_TRACE(res
);
511 class ResourceDataValue
: public ResourceValue
{
513 ResourceDataValue() :
514 res(static_cast<Resource
>(URES_NONE
)),
516 virtual ~ResourceDataValue();
518 void setData(const ResourceData
*data
) {
522 void setResource(Resource r
, ResourceTracer
&& traceInfo
) {
524 fTraceInfo
= traceInfo
;
527 const ResourceData
&getData() const { return resData
; }
528 virtual UResType
getType() const;
529 virtual const UChar
*getString(int32_t &length
, UErrorCode
&errorCode
) const;
530 virtual const UChar
*getAliasString(int32_t &length
, UErrorCode
&errorCode
) const;
531 virtual int32_t getInt(UErrorCode
&errorCode
) const;
532 virtual uint32_t getUInt(UErrorCode
&errorCode
) const;
533 virtual const int32_t *getIntVector(int32_t &length
, UErrorCode
&errorCode
) const;
534 virtual const uint8_t *getBinary(int32_t &length
, UErrorCode
&errorCode
) const;
535 virtual ResourceArray
getArray(UErrorCode
&errorCode
) const;
536 virtual ResourceTable
getTable(UErrorCode
&errorCode
) const;
537 virtual UBool
isNoInheritanceMarker() const;
538 virtual int32_t getStringArray(UnicodeString
*dest
, int32_t capacity
,
539 UErrorCode
&errorCode
) const;
540 virtual int32_t getStringArrayOrStringAsArray(UnicodeString
*dest
, int32_t capacity
,
541 UErrorCode
&errorCode
) const;
542 virtual UnicodeString
getStringOrFirstOfArray(UErrorCode
&errorCode
) const;
545 // TODO(ICU-20769): If UResourceBundle.fResData becomes a pointer,
546 // then remove this value field again and just store a pResData pointer.
547 ResourceData resData
;
549 ResourceTracer fTraceInfo
;
554 #endif /* __cplusplus */
557 * Swap an ICU resource bundle. See udataswp.h.
560 U_CAPI
int32_t U_EXPORT2
561 ures_swap(const UDataSwapper
*ds
,
562 const void *inData
, int32_t length
, void *outData
,
563 UErrorCode
*pErrorCode
);