]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/uresdata.h
ICU-57163.0.1.tar.gz
[apple/icu.git] / icuSources / common / uresdata.h
1 /*
2 ******************************************************************************
3 * Copyright (C) 1999-2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
6 * file name: uresdata.h
7 * encoding: US-ASCII
8 * tab size: 8 (not used)
9 * indentation:4
10 *
11 * created on: 1999dec08
12 * created by: Markus W. Scherer
13 * 06/24/02 weiv Added support for resource sharing
14 */
15
16 #ifndef __RESDATA_H__
17 #define __RESDATA_H__
18
19 #include "unicode/utypes.h"
20 #include "unicode/udata.h"
21 #include "unicode/ures.h"
22 #include "putilimp.h"
23 #include "udataswp.h"
24
25 /**
26 * Numeric constants for internal-only types of resource items.
27 * These must use different numeric values than UResType constants
28 * because they are used together.
29 * Internal types are never returned by ures_getType().
30 */
31 typedef enum {
32 /** Include a negative value so that the compiler uses the same int type as for UResType. */
33 URES_INTERNAL_NONE=-1,
34
35 /** Resource type constant for tables with 32-bit count, key offsets and values. */
36 URES_TABLE32=4,
37
38 /**
39 * Resource type constant for tables with 16-bit count, key offsets and values.
40 * All values are URES_STRING_V2 strings.
41 */
42 URES_TABLE16=5,
43
44 /** Resource type constant for 16-bit Unicode strings in formatVersion 2. */
45 URES_STRING_V2=6,
46
47 /**
48 * Resource type constant for arrays with 16-bit count and values.
49 * All values are URES_STRING_V2 strings.
50 */
51 URES_ARRAY16=9
52
53 /* Resource type 15 is not defined but effectively used by RES_BOGUS=0xffffffff. */
54 } UResInternalType;
55
56 /*
57 * A Resource is a 32-bit value that has 2 bit fields:
58 * 31..28 4-bit type, see enum below
59 * 27..0 28-bit four-byte-offset or value according to the type
60 */
61 typedef uint32_t Resource;
62
63 #define RES_BOGUS 0xffffffff
64 #define RES_MAX_OFFSET 0x0fffffff
65
66 #define RES_GET_TYPE(res) ((int32_t)((res)>>28UL))
67 #define RES_GET_OFFSET(res) ((res)&0x0fffffff)
68 #define RES_GET_POINTER(pRoot, res) ((pRoot)+RES_GET_OFFSET(res))
69
70 /* get signed and unsigned integer values directly from the Resource handle */
71 #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
72 # define RES_GET_INT(res) (((int32_t)((res)<<4L))>>4L)
73 #else
74 # define RES_GET_INT(res) (int32_t)(((res)&0x08000000) ? (res)|0xf0000000 : (res)&0x07ffffff)
75 #endif
76
77 #define RES_GET_UINT(res) ((res)&0x0fffffff)
78
79 #define URES_IS_ARRAY(type) ((int32_t)(type)==URES_ARRAY || (int32_t)(type)==URES_ARRAY16)
80 #define URES_IS_TABLE(type) ((int32_t)(type)==URES_TABLE || (int32_t)(type)==URES_TABLE16 || (int32_t)(type)==URES_TABLE32)
81 #define URES_IS_CONTAINER(type) (URES_IS_TABLE(type) || URES_IS_ARRAY(type))
82
83 #define URES_MAKE_RESOURCE(type, offset) (((Resource)(type)<<28)|(Resource)(offset))
84 #define URES_MAKE_EMPTY_RESOURCE(type) ((Resource)(type)<<28)
85
86 /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
87 enum {
88 /**
89 * [0] contains the length of indexes[]
90 * which is at most URES_INDEX_TOP of the latest format version
91 *
92 * formatVersion==1: all bits contain the length of indexes[]
93 * but the length is much less than 0xff;
94 * formatVersion>1:
95 * only bits 7..0 contain the length of indexes[],
96 * bits 31..8 are reserved and set to 0
97 * formatVersion>=3:
98 * bits 31..8 poolStringIndexLimit bits 23..0
99 */
100 URES_INDEX_LENGTH,
101 /**
102 * [1] contains the top of the key strings,
103 * same as the bottom of resources or UTF-16 strings, rounded up
104 */
105 URES_INDEX_KEYS_TOP,
106 /** [2] contains the top of all resources */
107 URES_INDEX_RESOURCES_TOP,
108 /**
109 * [3] contains the top of the bundle,
110 * in case it were ever different from [2]
111 */
112 URES_INDEX_BUNDLE_TOP,
113 /** [4] max. length of any table */
114 URES_INDEX_MAX_TABLE_LENGTH,
115 /**
116 * [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2)
117 *
118 * formatVersion>=3:
119 * bits 31..16 poolStringIndex16Limit
120 * bits 15..12 poolStringIndexLimit bits 27..24
121 */
122 URES_INDEX_ATTRIBUTES,
123 /**
124 * [6] top of the 16-bit units (UTF-16 string v2 UChars, URES_TABLE16, URES_ARRAY16),
125 * rounded up (new in formatVersion 2.0, ICU 4.4)
126 */
127 URES_INDEX_16BIT_TOP,
128 /** [7] checksum of the pool bundle (new in formatVersion 2.0, ICU 4.4) */
129 URES_INDEX_POOL_CHECKSUM,
130 URES_INDEX_TOP
131 };
132
133 /*
134 * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES].
135 * New in formatVersion 1.2 (ICU 3.6).
136 *
137 * If set, then this resource bundle is a standalone bundle.
138 * If not set, then the bundle participates in locale fallback, eventually
139 * all the way to the root bundle.
140 * If indexes[] is missing or too short, then the attribute cannot be determined
141 * reliably. Dependency checking should ignore such bundles, and loading should
142 * use fallbacks.
143 */
144 #define URES_ATT_NO_FALLBACK 1
145
146 /*
147 * Attributes for bundles that are, or use, a pool bundle.
148 * A pool bundle provides key strings that are shared among several other bundles
149 * to reduce their total size.
150 * New in formatVersion 2 (ICU 4.4).
151 */
152 #define URES_ATT_IS_POOL_BUNDLE 2
153 #define URES_ATT_USES_POOL_BUNDLE 4
154
155 /*
156 * File format for .res resource bundle files
157 *
158 * ICU 56: New in formatVersion 3 compared with 2: -------------
159 *
160 * Resource bundles can optionally use shared string-v2 values
161 * stored in the pool bundle.
162 * If so, then the indexes[] contain two new values
163 * in previously-unused bits of existing indexes[] slots:
164 * - poolStringIndexLimit:
165 * String-v2 offsets (in 32-bit Resource words) below this limit
166 * point to pool bundle string-v2 values.
167 * - poolStringIndex16Limit:
168 * Resource16 string-v2 offsets below this limit
169 * point to pool bundle string-v2 values.
170 * Guarantee: poolStringIndex16Limit <= poolStringIndexLimit
171 *
172 * The local bundle's poolStringIndexLimit is greater than
173 * any pool bundle string index used in the local bundle.
174 * The poolStringIndexLimit should not be greater than
175 * the maximum possible pool bundle string index.
176 *
177 * The maximum possible pool bundle string index is the index to the last non-NUL
178 * pool string character, due to suffix sharing.
179 *
180 * In the pool bundle, there is no structure that lists the strings.
181 * (The root resource is an empty Table.)
182 * If the strings need to be enumerated (as genrb --usePoolBundle does),
183 * then iterate through the pool bundle's 16-bit-units array from the beginning.
184 * Stop at the end of the array, or when an explicit or implicit string length
185 * would lead beyond the end of the array,
186 * or when an apparent string is not NUL-terminated.
187 * (Future genrb version might terminate the strings with
188 * what looks like a large explicit string length.)
189 *
190 * ICU 4.4: New in formatVersion 2 compared with 1.3: -------------
191 *
192 * Three new resource types -- String-v2, Table16 and Array16 -- have their
193 * values stored in a new array of 16-bit units between the table key strings
194 * and the start of the other resources.
195 *
196 * genrb eliminates duplicates among Unicode string-v2 values.
197 * Multiple Unicode strings may use the same offset and string data,
198 * or a short string may point to the suffix of a longer string. ("Suffix sharing")
199 * For example, one string "abc" may be reused for another string "bc" by pointing
200 * to the second character. (Short strings-v2 are NUL-terminated
201 * and not preceded by an explicit length value.)
202 *
203 * It is allowed for all resource types to share values.
204 * The swapper code (ures_swap()) has been modified so that it swaps each item
205 * exactly once.
206 *
207 * A resource bundle may use a special pool bundle. Some or all of the table key strings
208 * of the using-bundle are omitted, and the key string offsets for such key strings refer
209 * to offsets in the pool bundle.
210 * The using-bundle's and the pool-bundle's indexes[URES_INDEX_POOL_CHECKSUM] values
211 * must match.
212 * Two bits in indexes[URES_INDEX_ATTRIBUTES] indicate whether a resource bundle
213 * is or uses a pool bundle.
214 *
215 * Table key strings must be compared in ASCII order, even if they are not
216 * stored in ASCII.
217 *
218 * New in formatVersion 1.3 compared with 1.2: -------------
219 *
220 * genrb eliminates duplicates among key strings.
221 * Multiple table items may share one key string, or one item may point
222 * to the suffix of another's key string. ("Suffix sharing")
223 * For example, one key "abc" may be reused for another key "bc" by pointing
224 * to the second character. (Key strings are NUL-terminated.)
225 *
226 * -------------
227 *
228 * An ICU4C resource bundle file (.res) is a binary, memory-mappable file
229 * with nested, hierarchical data structures.
230 * It physically contains the following:
231 *
232 * Resource root; -- 32-bit Resource item, root item for this bundle's tree;
233 * currently, the root item must be a table or table32 resource item
234 * int32_t indexes[indexes[0]]; -- array of indexes for friendly
235 * reading and swapping; see URES_INDEX_* above
236 * new in formatVersion 1.1 (ICU 2.8)
237 * char keys[]; -- characters for key strings
238 * (formatVersion 1.0: up to 65k of characters; 1.1: <2G)
239 * (minus the space for root and indexes[]),
240 * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated;
241 * padded to multiple of 4 bytes for 4-alignment of the following data
242 * uint16_t 16BitUnits[]; -- resources that are stored entirely as sequences of 16-bit units
243 * (new in formatVersion 2/ICU 4.4)
244 * data is indexed by the offset values in 16-bit resource types,
245 * with offset 0 pointing to the beginning of this array;
246 * there is a 0 at offset 0, for empty resources;
247 * padded to multiple of 4 bytes for 4-alignment of the following data
248 * data; -- data directly and indirectly indexed by the root item;
249 * the structure is determined by walking the tree
250 *
251 * Each resource bundle item has a 32-bit Resource handle (see typedef above)
252 * which contains the item type number in its upper 4 bits (31..28) and either
253 * an offset or a direct value in its lower 28 bits (27..0).
254 * The order of items is undefined and only determined by walking the tree.
255 * Leaves of the tree may be stored first or last or anywhere in between,
256 * and it is in theory possible to have unreferenced holes in the file.
257 *
258 * 16-bit-unit values:
259 * Starting with formatVersion 2/ICU 4.4, some resources are stored in a special
260 * array of 16-bit units. Each resource value is a sequence of 16-bit units,
261 * with no per-resource padding to a 4-byte boundary.
262 * 16-bit container types (Table16 and Array16) contain Resource16 values
263 * which are offsets to String-v2 resources in the same 16-bit-units array.
264 *
265 * Direct values:
266 * - Empty Unicode strings have an offset value of 0 in the Resource handle itself.
267 * - Starting with formatVersion 2/ICU 4.4, an offset value of 0 for
268 * _any_ resource type indicates an empty value.
269 * - Integer values are 28-bit values stored in the Resource handle itself;
270 * the interpretation of unsigned vs. signed integers is up to the application.
271 *
272 * All other types and values use 28-bit offsets to point to the item's data.
273 * The offset is an index to the first 32-bit word of the value, relative to the
274 * start of the resource data (i.e., the root item handle is at offset 0).
275 * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits).
276 * All resource item values are 4-aligned.
277 *
278 * New in formatVersion 2/ICU 4.4: Some types use offsets into the 16-bit-units array,
279 * indexing 16-bit units in that array.
280 *
281 * The structures (memory layouts) for the values for each item type are listed
282 * in the table below.
283 *
284 * Nested, hierarchical structures: -------------
285 *
286 * Table items contain key-value pairs where the keys are offsets to char * key strings.
287 * The values of these pairs are either Resource handles or
288 * offsets into the 16-bit-units array, depending on the table type.
289 *
290 * Array items are simple vectors of Resource handles,
291 * or of offsets into the 16-bit-units array, depending on the array type.
292 *
293 * Table key string offsets: -------
294 *
295 * Key string offsets are relative to the start of the resource data (of the root handle),
296 * i.e., the first string has an offset of 4+sizeof(indexes).
297 * (After the 4-byte root handle and after the indexes array.)
298 *
299 * If the resource bundle uses a pool bundle, then some key strings are stored
300 * in the pool bundle rather than in the local bundle itself.
301 * - In a Table or Table16, the 16-bit key string offset is local if it is
302 * less than indexes[URES_INDEX_KEYS_TOP]<<2.
303 * Otherwise, subtract indexes[URES_INDEX_KEYS_TOP]<<2 to get the offset into
304 * the pool bundle key strings.
305 * - In a Table32, the 32-bit key string offset is local if it is non-negative.
306 * Otherwise, reset bit 31 to get the pool key string offset.
307 *
308 * Unlike the local offset, the pool key offset is relative to
309 * the start of the key strings, not to the start of the bundle.
310 *
311 * An alias item is special (and new in ICU 2.4): --------------
312 *
313 * Its memory layout is just like for a UnicodeString, but at runtime it resolves to
314 * another resource bundle's item according to the path in the string.
315 * This is used to share items across bundles that are in different lookup/fallback
316 * chains (e.g., large collation data among zh_TW and zh_HK).
317 * This saves space (for large items) and maintenance effort (less duplication of data).
318 *
319 * --------------------------------------------------------------------------
320 *
321 * Resource types:
322 *
323 * Most resources have their values stored at four-byte offsets from the start
324 * of the resource data. These values are at least 4-aligned.
325 * Some resource values are stored directly in the offset field of the Resource itself.
326 * See UResType in unicode/ures.h for enumeration constants for Resource types.
327 *
328 * Some resources have their values stored as sequences of 16-bit units,
329 * at 2-byte offsets from the start of a contiguous 16-bit-unit array between
330 * the table key strings and the other resources. (new in formatVersion 2/ICU 4.4)
331 * At offset 0 of that array is a 16-bit zero value for empty 16-bit resources.
332 *
333 * Resource16 values in Table16 and Array16 are 16-bit offsets to String-v2
334 * resources, with the offsets relative to the start of the 16-bit-units array.
335 * Starting with formatVersion 3/ICU 56, if offset<poolStringIndex16Limit
336 * then use the pool bundle's 16-bit-units array,
337 * otherwise subtract that limit and use the local 16-bit-units array.
338 *
339 * Type Name Memory layout of values
340 * (in parentheses: scalar, non-offset values)
341 *
342 * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding)
343 * or (empty string ("") if offset==0)
344 * 1 Binary: int32_t length, uint8_t[length], (padding)
345 * - the start of the bytes is 16-aligned -
346 * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count]
347 * 3 Alias: (physically same value layout as string, new in ICU 2.4)
348 * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count]
349 * (new in formatVersion 1.1/ICU 2.8)
350 * 5 Table16: uint16_t count, uint16_t keyStringOffsets[count], Resource16[count]
351 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
352 * 6 Unicode String-v2:UChar[length], (UChar)0; length determined by the first UChar:
353 * - if first is not a trail surrogate, then the length is implicit
354 * and u_strlen() needs to be called
355 * - if first<0xdfef then length=first&0x3ff (and skip first)
356 * - if first<0xdfff then length=((first-0xdfef)<<16) | second UChar
357 * - if first==0xdfff then length=((second UChar)<<16) | third UChar
358 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
359 *
360 * Starting with formatVersion 3/ICU 56, if offset<poolStringIndexLimit
361 * then use the pool bundle's 16-bit-units array,
362 * otherwise subtract that limit and use the local 16-bit-units array.
363 * (Note different limits for Resource16 vs. Resource.)
364 *
365 * 7 Integer: (28-bit offset is integer value)
366 * 8 Array: int32_t count, Resource[count]
367 * 9 Array16: uint16_t count, Resource16[count]
368 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
369 * 14 Integer Vector: int32_t length, int32_t[length]
370 * 15 Reserved: This value denotes special purpose resources and is for internal use.
371 *
372 * Note that there are 3 types with data vector values:
373 * - Vectors of 8-bit bytes stored as type Binary.
374 * - Vectors of 16-bit words stored as type Unicode String or Unicode String-v2
375 * (no value restrictions, all values 0..ffff allowed!).
376 * - Vectors of 32-bit words stored as type Integer Vector.
377 */
378
379 /*
380 * Structure for a single, memory-mapped ResourceBundle.
381 */
382 typedef struct {
383 UDataMemory *data;
384 const int32_t *pRoot;
385 const uint16_t *p16BitUnits;
386 const char *poolBundleKeys;
387 Resource rootRes;
388 int32_t localKeyLimit;
389 const uint16_t *poolBundleStrings;
390 int32_t poolStringIndexLimit;
391 int32_t poolStringIndex16Limit;
392 UBool noFallback; /* see URES_ATT_NO_FALLBACK */
393 UBool isPoolBundle;
394 UBool usesPoolBundle;
395 UBool useNativeStrcmp;
396 } ResourceData;
397
398 /*
399 * Read a resource bundle from memory.
400 */
401 U_INTERNAL void U_EXPORT2
402 res_read(ResourceData *pResData,
403 const UDataInfo *pInfo, const void *inBytes, int32_t length,
404 UErrorCode *errorCode);
405
406 /*
407 * Load a resource bundle file.
408 * The ResourceData structure must be allocated externally.
409 */
410 U_CFUNC void
411 res_load(ResourceData *pResData,
412 const char *path, const char *name, UErrorCode *errorCode);
413
414 /*
415 * Release a resource bundle file.
416 * This does not release the ResourceData structure itself.
417 */
418 U_CFUNC void
419 res_unload(ResourceData *pResData);
420
421 U_INTERNAL UResType U_EXPORT2
422 res_getPublicType(Resource res);
423
424 /*
425 * Return a pointer to a zero-terminated, const UChar* string
426 * and set its length in *pLength.
427 * Returns NULL if not found.
428 */
429 U_INTERNAL const UChar * U_EXPORT2
430 res_getString(const ResourceData *pResData, Resource res, int32_t *pLength);
431
432 U_INTERNAL const UChar * U_EXPORT2
433 res_getAlias(const ResourceData *pResData, Resource res, int32_t *pLength);
434
435 U_INTERNAL const uint8_t * U_EXPORT2
436 res_getBinary(const ResourceData *pResData, Resource res, int32_t *pLength);
437
438 U_INTERNAL const int32_t * U_EXPORT2
439 res_getIntVector(const ResourceData *pResData, Resource res, int32_t *pLength);
440
441 U_INTERNAL Resource U_EXPORT2
442 res_getResource(const ResourceData *pResData, const char *key);
443
444 U_INTERNAL int32_t U_EXPORT2
445 res_countArrayItems(const ResourceData *pResData, Resource res);
446
447 U_INTERNAL Resource U_EXPORT2
448 res_getArrayItem(const ResourceData *pResData, Resource array, int32_t indexS);
449
450 U_INTERNAL Resource U_EXPORT2
451 res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key);
452
453 U_INTERNAL Resource U_EXPORT2
454 res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key);
455
456 /**
457 * Iterates over the path and stops when a scalar resource is found.
458 * Follows aliases.
459 * Modifies the contents of *path (replacing separators with NULs),
460 * and also moves *path forward while it finds items.
461 *
462 * @param path input: "CollationElements/Sequence" or "zoneStrings/3/2" etc.;
463 * output: points to the part that has not yet been processed
464 */
465 U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r,
466 char** path, const char** key);
467
468 #ifdef __cplusplus
469
470 #include "resource.h"
471
472 U_NAMESPACE_BEGIN
473
474 class ResourceDataValue : public ResourceValue {
475 public:
476 ResourceDataValue() : pResData(NULL), res(URES_NONE) {}
477 virtual ~ResourceDataValue();
478
479 void setData(const ResourceData *data) { pResData = data; }
480 void setResource(Resource r) { res = r; }
481
482 virtual UResType getType() const;
483 virtual const UChar *getString(int32_t &length, UErrorCode &errorCode) const;
484 virtual const UChar *getAliasString(int32_t &length, UErrorCode &errorCode) const;
485 virtual int32_t getInt(UErrorCode &errorCode) const;
486 virtual uint32_t getUInt(UErrorCode &errorCode) const;
487 virtual const int32_t *getIntVector(int32_t &length, UErrorCode &errorCode) const;
488 virtual const uint8_t *getBinary(int32_t &length, UErrorCode &errorCode) const;
489
490 const ResourceData *pResData;
491
492 private:
493 Resource res;
494 };
495
496 U_NAMESPACE_END
497
498 /**
499 * @param value will be set during enumeration; input contents is ignored
500 * @param sink receives all table item key-value pairs
501 */
502 U_CFUNC void
503 ures_getAllTableItems(const ResourceData *pResData, Resource table,
504 icu::ResourceDataValue &value, icu::ResourceTableSink &sink,
505 UErrorCode &errorCode);
506
507 /**
508 * @param value will be set during enumeration; input contents is ignored
509 * @param sink receives all array item values
510 */
511 U_CFUNC void
512 ures_getAllArrayItems(const ResourceData *pResData, Resource array,
513 icu::ResourceDataValue &value, icu::ResourceArraySink &sink,
514 UErrorCode &errorCode);
515
516 #endif /* __cplusplus */
517
518 /**
519 * Swap an ICU resource bundle. See udataswp.h.
520 * @internal
521 */
522 U_CAPI int32_t U_EXPORT2
523 ures_swap(const UDataSwapper *ds,
524 const void *inData, int32_t length, void *outData,
525 UErrorCode *pErrorCode);
526
527 #endif