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 #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
74 # define RES_GET_INT(res) (((int32_t)((res)<<4L))>>4L)
76 # define RES_GET_INT(res) (int32_t)(((res)&0x08000000) ? (res)|0xf0000000 : (res)&0x07ffffff)
79 #define RES_GET_UINT(res) ((res)&0x0fffffff)
81 #define URES_IS_ARRAY(type) ((int32_t)(type)==URES_ARRAY || (int32_t)(type)==URES_ARRAY16)
82 #define URES_IS_TABLE(type) ((int32_t)(type)==URES_TABLE || (int32_t)(type)==URES_TABLE16 || (int32_t)(type)==URES_TABLE32)
83 #define URES_IS_CONTAINER(type) (URES_IS_TABLE(type) || URES_IS_ARRAY(type))
85 #define URES_MAKE_RESOURCE(type, offset) (((Resource)(type)<<28)|(Resource)(offset))
86 #define URES_MAKE_EMPTY_RESOURCE(type) ((Resource)(type)<<28)
88 /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
91 * [0] contains the length of indexes[]
92 * which is at most URES_INDEX_TOP of the latest format version
94 * formatVersion==1: all bits contain the length of indexes[]
95 * but the length is much less than 0xff;
97 * only bits 7..0 contain the length of indexes[],
98 * bits 31..8 are reserved and set to 0
100 * bits 31..8 poolStringIndexLimit bits 23..0
104 * [1] contains the top of the key strings,
105 * same as the bottom of resources or UTF-16 strings, rounded up
108 /** [2] contains the top of all resources */
109 URES_INDEX_RESOURCES_TOP
,
111 * [3] contains the top of the bundle,
112 * in case it were ever different from [2]
114 URES_INDEX_BUNDLE_TOP
,
115 /** [4] max. length of any table */
116 URES_INDEX_MAX_TABLE_LENGTH
,
118 * [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2)
121 * bits 31..16 poolStringIndex16Limit
122 * bits 15..12 poolStringIndexLimit bits 27..24
124 URES_INDEX_ATTRIBUTES
,
126 * [6] top of the 16-bit units (UTF-16 string v2 UChars, URES_TABLE16, URES_ARRAY16),
127 * rounded up (new in formatVersion 2.0, ICU 4.4)
129 URES_INDEX_16BIT_TOP
,
130 /** [7] checksum of the pool bundle (new in formatVersion 2.0, ICU 4.4) */
131 URES_INDEX_POOL_CHECKSUM
,
136 * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES].
137 * New in formatVersion 1.2 (ICU 3.6).
139 * If set, then this resource bundle is a standalone bundle.
140 * If not set, then the bundle participates in locale fallback, eventually
141 * all the way to the root bundle.
142 * If indexes[] is missing or too short, then the attribute cannot be determined
143 * reliably. Dependency checking should ignore such bundles, and loading should
146 #define URES_ATT_NO_FALLBACK 1
149 * Attributes for bundles that are, or use, a pool bundle.
150 * A pool bundle provides key strings that are shared among several other bundles
151 * to reduce their total size.
152 * New in formatVersion 2 (ICU 4.4).
154 #define URES_ATT_IS_POOL_BUNDLE 2
155 #define URES_ATT_USES_POOL_BUNDLE 4
158 * File format for .res resource bundle files
160 * ICU 56: New in formatVersion 3 compared with 2: -------------
162 * Resource bundles can optionally use shared string-v2 values
163 * stored in the pool bundle.
164 * If so, then the indexes[] contain two new values
165 * in previously-unused bits of existing indexes[] slots:
166 * - poolStringIndexLimit:
167 * String-v2 offsets (in 32-bit Resource words) below this limit
168 * point to pool bundle string-v2 values.
169 * - poolStringIndex16Limit:
170 * Resource16 string-v2 offsets below this limit
171 * point to pool bundle string-v2 values.
172 * Guarantee: poolStringIndex16Limit <= poolStringIndexLimit
174 * The local bundle's poolStringIndexLimit is greater than
175 * any pool bundle string index used in the local bundle.
176 * The poolStringIndexLimit should not be greater than
177 * the maximum possible pool bundle string index.
179 * The maximum possible pool bundle string index is the index to the last non-NUL
180 * pool string character, due to suffix sharing.
182 * In the pool bundle, there is no structure that lists the strings.
183 * (The root resource is an empty Table.)
184 * If the strings need to be enumerated (as genrb --usePoolBundle does),
185 * then iterate through the pool bundle's 16-bit-units array from the beginning.
186 * Stop at the end of the array, or when an explicit or implicit string length
187 * would lead beyond the end of the array,
188 * or when an apparent string is not NUL-terminated.
189 * (Future genrb version might terminate the strings with
190 * what looks like a large explicit string length.)
192 * ICU 4.4: New in formatVersion 2 compared with 1.3: -------------
194 * Three new resource types -- String-v2, Table16 and Array16 -- have their
195 * values stored in a new array of 16-bit units between the table key strings
196 * and the start of the other resources.
198 * genrb eliminates duplicates among Unicode string-v2 values.
199 * Multiple Unicode strings may use the same offset and string data,
200 * or a short string may point to the suffix of a longer string. ("Suffix sharing")
201 * For example, one string "abc" may be reused for another string "bc" by pointing
202 * to the second character. (Short strings-v2 are NUL-terminated
203 * and not preceded by an explicit length value.)
205 * It is allowed for all resource types to share values.
206 * The swapper code (ures_swap()) has been modified so that it swaps each item
209 * A resource bundle may use a special pool bundle. Some or all of the table key strings
210 * of the using-bundle are omitted, and the key string offsets for such key strings refer
211 * to offsets in the pool bundle.
212 * The using-bundle's and the pool-bundle's indexes[URES_INDEX_POOL_CHECKSUM] values
214 * Two bits in indexes[URES_INDEX_ATTRIBUTES] indicate whether a resource bundle
215 * is or uses a pool bundle.
217 * Table key strings must be compared in ASCII order, even if they are not
220 * New in formatVersion 1.3 compared with 1.2: -------------
222 * genrb eliminates duplicates among key strings.
223 * Multiple table items may share one key string, or one item may point
224 * to the suffix of another's key string. ("Suffix sharing")
225 * For example, one key "abc" may be reused for another key "bc" by pointing
226 * to the second character. (Key strings are NUL-terminated.)
230 * An ICU4C resource bundle file (.res) is a binary, memory-mappable file
231 * with nested, hierarchical data structures.
232 * It physically contains the following:
234 * Resource root; -- 32-bit Resource item, root item for this bundle's tree;
235 * currently, the root item must be a table or table32 resource item
236 * int32_t indexes[indexes[0]]; -- array of indexes for friendly
237 * reading and swapping; see URES_INDEX_* above
238 * new in formatVersion 1.1 (ICU 2.8)
239 * char keys[]; -- characters for key strings
240 * (formatVersion 1.0: up to 65k of characters; 1.1: <2G)
241 * (minus the space for root and indexes[]),
242 * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated;
243 * padded to multiple of 4 bytes for 4-alignment of the following data
244 * uint16_t 16BitUnits[]; -- resources that are stored entirely as sequences of 16-bit units
245 * (new in formatVersion 2/ICU 4.4)
246 * data is indexed by the offset values in 16-bit resource types,
247 * with offset 0 pointing to the beginning of this array;
248 * there is a 0 at offset 0, for empty resources;
249 * padded to multiple of 4 bytes for 4-alignment of the following data
250 * data; -- data directly and indirectly indexed by the root item;
251 * the structure is determined by walking the tree
253 * Each resource bundle item has a 32-bit Resource handle (see typedef above)
254 * which contains the item type number in its upper 4 bits (31..28) and either
255 * an offset or a direct value in its lower 28 bits (27..0).
256 * The order of items is undefined and only determined by walking the tree.
257 * Leaves of the tree may be stored first or last or anywhere in between,
258 * and it is in theory possible to have unreferenced holes in the file.
260 * 16-bit-unit values:
261 * Starting with formatVersion 2/ICU 4.4, some resources are stored in a special
262 * array of 16-bit units. Each resource value is a sequence of 16-bit units,
263 * with no per-resource padding to a 4-byte boundary.
264 * 16-bit container types (Table16 and Array16) contain Resource16 values
265 * which are offsets to String-v2 resources in the same 16-bit-units array.
268 * - Empty Unicode strings have an offset value of 0 in the Resource handle itself.
269 * - Starting with formatVersion 2/ICU 4.4, an offset value of 0 for
270 * _any_ resource type indicates an empty value.
271 * - Integer values are 28-bit values stored in the Resource handle itself;
272 * the interpretation of unsigned vs. signed integers is up to the application.
274 * All other types and values use 28-bit offsets to point to the item's data.
275 * The offset is an index to the first 32-bit word of the value, relative to the
276 * start of the resource data (i.e., the root item handle is at offset 0).
277 * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits).
278 * All resource item values are 4-aligned.
280 * New in formatVersion 2/ICU 4.4: Some types use offsets into the 16-bit-units array,
281 * indexing 16-bit units in that array.
283 * The structures (memory layouts) for the values for each item type are listed
284 * in the table below.
286 * Nested, hierarchical structures: -------------
288 * Table items contain key-value pairs where the keys are offsets to char * key strings.
289 * The values of these pairs are either Resource handles or
290 * offsets into the 16-bit-units array, depending on the table type.
292 * Array items are simple vectors of Resource handles,
293 * or of offsets into the 16-bit-units array, depending on the array type.
295 * Table key string offsets: -------
297 * Key string offsets are relative to the start of the resource data (of the root handle),
298 * i.e., the first string has an offset of 4+sizeof(indexes).
299 * (After the 4-byte root handle and after the indexes array.)
301 * If the resource bundle uses a pool bundle, then some key strings are stored
302 * in the pool bundle rather than in the local bundle itself.
303 * - In a Table or Table16, the 16-bit key string offset is local if it is
304 * less than indexes[URES_INDEX_KEYS_TOP]<<2.
305 * Otherwise, subtract indexes[URES_INDEX_KEYS_TOP]<<2 to get the offset into
306 * the pool bundle key strings.
307 * - In a Table32, the 32-bit key string offset is local if it is non-negative.
308 * Otherwise, reset bit 31 to get the pool key string offset.
310 * Unlike the local offset, the pool key offset is relative to
311 * the start of the key strings, not to the start of the bundle.
313 * An alias item is special (and new in ICU 2.4): --------------
315 * Its memory layout is just like for a UnicodeString, but at runtime it resolves to
316 * another resource bundle's item according to the path in the string.
317 * This is used to share items across bundles that are in different lookup/fallback
318 * chains (e.g., large collation data among zh_TW and zh_HK).
319 * This saves space (for large items) and maintenance effort (less duplication of data).
321 * --------------------------------------------------------------------------
325 * Most resources have their values stored at four-byte offsets from the start
326 * of the resource data. These values are at least 4-aligned.
327 * Some resource values are stored directly in the offset field of the Resource itself.
328 * See UResType in unicode/ures.h for enumeration constants for Resource types.
330 * Some resources have their values stored as sequences of 16-bit units,
331 * at 2-byte offsets from the start of a contiguous 16-bit-unit array between
332 * the table key strings and the other resources. (new in formatVersion 2/ICU 4.4)
333 * At offset 0 of that array is a 16-bit zero value for empty 16-bit resources.
335 * Resource16 values in Table16 and Array16 are 16-bit offsets to String-v2
336 * resources, with the offsets relative to the start of the 16-bit-units array.
337 * Starting with formatVersion 3/ICU 56, if offset<poolStringIndex16Limit
338 * then use the pool bundle's 16-bit-units array,
339 * otherwise subtract that limit and use the local 16-bit-units array.
341 * Type Name Memory layout of values
342 * (in parentheses: scalar, non-offset values)
344 * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding)
345 * or (empty string ("") if offset==0)
346 * 1 Binary: int32_t length, uint8_t[length], (padding)
347 * - the start of the bytes is 16-aligned -
348 * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count]
349 * 3 Alias: (physically same value layout as string, new in ICU 2.4)
350 * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count]
351 * (new in formatVersion 1.1/ICU 2.8)
352 * 5 Table16: uint16_t count, uint16_t keyStringOffsets[count], Resource16[count]
353 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
354 * 6 Unicode String-v2:UChar[length], (UChar)0; length determined by the first UChar:
355 * - if first is not a trail surrogate, then the length is implicit
356 * and u_strlen() needs to be called
357 * - if first<0xdfef then length=first&0x3ff (and skip first)
358 * - if first<0xdfff then length=((first-0xdfef)<<16) | second UChar
359 * - if first==0xdfff then length=((second UChar)<<16) | third UChar
360 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
362 * Starting with formatVersion 3/ICU 56, if offset<poolStringIndexLimit
363 * then use the pool bundle's 16-bit-units array,
364 * otherwise subtract that limit and use the local 16-bit-units array.
365 * (Note different limits for Resource16 vs. Resource.)
367 * 7 Integer: (28-bit offset is integer value)
368 * 8 Array: int32_t count, Resource[count]
369 * 9 Array16: uint16_t count, Resource16[count]
370 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
371 * 14 Integer Vector: int32_t length, int32_t[length]
372 * 15 Reserved: This value denotes special purpose resources and is for internal use.
374 * Note that there are 3 types with data vector values:
375 * - Vectors of 8-bit bytes stored as type Binary.
376 * - Vectors of 16-bit words stored as type Unicode String or Unicode String-v2
377 * (no value restrictions, all values 0..ffff allowed!).
378 * - Vectors of 32-bit words stored as type Integer Vector.
382 * Structure for a single, memory-mapped ResourceBundle.
384 typedef struct ResourceData
{
386 const int32_t *pRoot
;
387 const uint16_t *p16BitUnits
;
388 const char *poolBundleKeys
;
390 int32_t localKeyLimit
;
391 const uint16_t *poolBundleStrings
;
392 int32_t poolStringIndexLimit
;
393 int32_t poolStringIndex16Limit
;
394 UBool noFallback
; /* see URES_ATT_NO_FALLBACK */
396 UBool usesPoolBundle
;
397 UBool useNativeStrcmp
;
401 * Read a resource bundle from memory.
403 U_INTERNAL
void U_EXPORT2
404 res_read(ResourceData
*pResData
,
405 const UDataInfo
*pInfo
, const void *inBytes
, int32_t length
,
406 UErrorCode
*errorCode
);
409 * Load a resource bundle file.
410 * The ResourceData structure must be allocated externally.
413 res_load(ResourceData
*pResData
,
414 const char *path
, const char *name
, UErrorCode
*errorCode
);
417 * Release a resource bundle file.
418 * This does not release the ResourceData structure itself.
421 res_unload(ResourceData
*pResData
);
423 U_INTERNAL UResType U_EXPORT2
424 res_getPublicType(Resource res
);
427 * Return a pointer to a zero-terminated, const UChar* string
428 * and set its length in *pLength.
429 * Returns NULL if not found.
431 U_INTERNAL
const UChar
* U_EXPORT2
432 res_getString(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
434 U_INTERNAL
const UChar
* U_EXPORT2
435 res_getAlias(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
437 U_INTERNAL
const uint8_t * U_EXPORT2
438 res_getBinary(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
440 U_INTERNAL
const int32_t * U_EXPORT2
441 res_getIntVector(const ResourceData
*pResData
, Resource res
, int32_t *pLength
);
443 U_INTERNAL Resource U_EXPORT2
444 res_getResource(const ResourceData
*pResData
, const char *key
);
446 U_INTERNAL
int32_t U_EXPORT2
447 res_countArrayItems(const ResourceData
*pResData
, Resource res
);
449 U_INTERNAL Resource U_EXPORT2
450 res_getArrayItem(const ResourceData
*pResData
, Resource array
, int32_t indexS
);
452 U_INTERNAL Resource U_EXPORT2
453 res_getTableItemByIndex(const ResourceData
*pResData
, Resource table
, int32_t indexS
, const char ** key
);
455 U_INTERNAL Resource U_EXPORT2
456 res_getTableItemByKey(const ResourceData
*pResData
, Resource table
, int32_t *indexS
, const char* * key
);
459 * Iterates over the path and stops when a scalar resource is found.
461 * Modifies the contents of *path (replacing separators with NULs),
462 * and also moves *path forward while it finds items.
464 * @param path input: "CollationElements/Sequence" or "zoneStrings/3/2" etc.;
465 * output: points to the part that has not yet been processed
467 U_CFUNC Resource
res_findResource(const ResourceData
*pResData
, Resource r
,
468 char** path
, const char** key
);
472 #include "resource.h"
476 class ResourceDataValue
: public ResourceValue
{
478 ResourceDataValue() : pResData(NULL
), res(static_cast<Resource
>(URES_NONE
)) {}
479 virtual ~ResourceDataValue();
481 void setData(const ResourceData
*data
) { pResData
= data
; }
482 void setResource(Resource r
) { res
= r
; }
484 virtual UResType
getType() const;
485 virtual const UChar
*getString(int32_t &length
, UErrorCode
&errorCode
) const;
486 virtual const UChar
*getAliasString(int32_t &length
, UErrorCode
&errorCode
) const;
487 virtual int32_t getInt(UErrorCode
&errorCode
) const;
488 virtual uint32_t getUInt(UErrorCode
&errorCode
) const;
489 virtual const int32_t *getIntVector(int32_t &length
, UErrorCode
&errorCode
) const;
490 virtual const uint8_t *getBinary(int32_t &length
, UErrorCode
&errorCode
) const;
491 virtual ResourceArray
getArray(UErrorCode
&errorCode
) const;
492 virtual ResourceTable
getTable(UErrorCode
&errorCode
) const;
493 virtual UBool
isNoInheritanceMarker() const;
494 virtual int32_t getStringArray(UnicodeString
*dest
, int32_t capacity
,
495 UErrorCode
&errorCode
) const;
496 virtual int32_t getStringArrayOrStringAsArray(UnicodeString
*dest
, int32_t capacity
,
497 UErrorCode
&errorCode
) const;
498 virtual UnicodeString
getStringOrFirstOfArray(UErrorCode
&errorCode
) const;
500 const ResourceData
*pResData
;
508 #endif /* __cplusplus */
511 * Swap an ICU resource bundle. See udataswp.h.
514 U_CAPI
int32_t U_EXPORT2
515 ures_swap(const UDataSwapper
*ds
,
516 const void *inData
, int32_t length
, void *outData
,
517 UErrorCode
*pErrorCode
);