• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 * Copyright (C) 1999-2016, International Business Machines
6 *                Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 *   file name:  uresdata.h
9 *   encoding:   UTF-8
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 1999dec08
14 *   created by: Markus W. Scherer
15 *   06/24/02    weiv        Added support for resource sharing
16 */
17 
18 #ifndef __RESDATA_H__
19 #define __RESDATA_H__
20 
21 #include "unicode/utypes.h"
22 #include "unicode/udata.h"
23 #include "unicode/ures.h"
24 #include "putilimp.h"
25 #include "udataswp.h"
26 
27 /**
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().
32  */
33 typedef enum {
34     /** Include a negative value so that the compiler uses the same int type as for UResType. */
35     URES_INTERNAL_NONE=-1,
36 
37     /** Resource type constant for tables with 32-bit count, key offsets and values. */
38     URES_TABLE32=4,
39 
40     /**
41      * Resource type constant for tables with 16-bit count, key offsets and values.
42      * All values are URES_STRING_V2 strings.
43      */
44     URES_TABLE16=5,
45 
46     /** Resource type constant for 16-bit Unicode strings in formatVersion 2. */
47     URES_STRING_V2=6,
48 
49     /**
50      * Resource type constant for arrays with 16-bit count and values.
51      * All values are URES_STRING_V2 strings.
52      */
53     URES_ARRAY16=9
54 
55     /* Resource type 15 is not defined but effectively used by RES_BOGUS=0xffffffff. */
56 } UResInternalType;
57 
58 /*
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
62  */
63 typedef uint32_t Resource;
64 
65 #define RES_BOGUS 0xffffffff
66 #define RES_MAX_OFFSET 0x0fffffff
67 
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))
71 
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)
75 #else
76 #   define RES_GET_INT(res) (int32_t)(((res)&0x08000000) ? (res)|0xf0000000 : (res)&0x07ffffff)
77 #endif
78 
79 #define RES_GET_UINT(res) ((res)&0x0fffffff)
80 
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))
84 
85 #define URES_MAKE_RESOURCE(type, offset) (((Resource)(type)<<28)|(Resource)(offset))
86 #define URES_MAKE_EMPTY_RESOURCE(type) ((Resource)(type)<<28)
87 
88 /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
89 enum {
90     /**
91      * [0] contains the length of indexes[]
92      * which is at most URES_INDEX_TOP of the latest format version
93      *
94      * formatVersion==1: all bits contain the length of indexes[]
95      *   but the length is much less than 0xff;
96      * formatVersion>1:
97      *   only bits  7..0 contain the length of indexes[],
98      *        bits 31..8 are reserved and set to 0
99      * formatVersion>=3:
100      *        bits 31..8 poolStringIndexLimit bits 23..0
101      */
102     URES_INDEX_LENGTH,
103     /**
104      * [1] contains the top of the key strings,
105      *     same as the bottom of resources or UTF-16 strings, rounded up
106      */
107     URES_INDEX_KEYS_TOP,
108     /** [2] contains the top of all resources */
109     URES_INDEX_RESOURCES_TOP,
110     /**
111      * [3] contains the top of the bundle,
112      *     in case it were ever different from [2]
113      */
114     URES_INDEX_BUNDLE_TOP,
115     /** [4] max. length of any table */
116     URES_INDEX_MAX_TABLE_LENGTH,
117     /**
118      * [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2)
119      *
120      * formatVersion>=3:
121      *   bits 31..16 poolStringIndex16Limit
122      *   bits 15..12 poolStringIndexLimit bits 27..24
123      */
124     URES_INDEX_ATTRIBUTES,
125     /**
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)
128      */
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,
132     URES_INDEX_TOP
133 };
134 
135 /*
136  * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES].
137  * New in formatVersion 1.2 (ICU 3.6).
138  *
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
144  * use fallbacks.
145  */
146 #define URES_ATT_NO_FALLBACK 1
147 
148 /*
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).
153  */
154 #define URES_ATT_IS_POOL_BUNDLE 2
155 #define URES_ATT_USES_POOL_BUNDLE 4
156 
157 /*
158  * File format for .res resource bundle files
159  *
160  * ICU 56: New in formatVersion 3 compared with 2: -------------
161  *
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
173  *
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.
178  *
179  * The maximum possible pool bundle string index is the index to the last non-NUL
180  * pool string character, due to suffix sharing.
181  *
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.)
191  *
192  * ICU 4.4: New in formatVersion 2 compared with 1.3: -------------
193  *
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.
197  *
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.)
204  *
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
207  * exactly once.
208  *
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
213  * must match.
214  * Two bits in indexes[URES_INDEX_ATTRIBUTES] indicate whether a resource bundle
215  * is or uses a pool bundle.
216  *
217  * Table key strings must be compared in ASCII order, even if they are not
218  * stored in ASCII.
219  *
220  * New in formatVersion 1.3 compared with 1.2: -------------
221  *
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.)
227  *
228  * -------------
229  *
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:
233  *
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
252  *
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.
259  *
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.
266  *
267  * Direct values:
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.
273  *
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.
279  *
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.
282  *
283  * The structures (memory layouts) for the values for each item type are listed
284  * in the table below.
285  *
286  * Nested, hierarchical structures: -------------
287  *
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.
291  *
292  * Array items are simple vectors of Resource handles,
293  * or of offsets into the 16-bit-units array, depending on the array type.
294  *
295  * Table key string offsets: -------
296  *
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.)
300  *
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.
309  *
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.
312  *
313  * An alias item is special (and new in ICU 2.4): --------------
314  *
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).
320  *
321  * --------------------------------------------------------------------------
322  *
323  * Resource types:
324  *
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.
329  *
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.
334  *
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.
340  *
341  * Type Name            Memory layout of values
342  *                      (in parentheses: scalar, non-offset values)
343  *
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)
361  *
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.)
366  *
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.
373  *
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.
379  */
380 
381 /*
382  * Structure for a single, memory-mapped ResourceBundle.
383  */
384 typedef struct ResourceData {
385     UDataMemory *data;
386     const int32_t *pRoot;
387     const uint16_t *p16BitUnits;
388     const char *poolBundleKeys;
389     Resource rootRes;
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 */
395     UBool isPoolBundle;
396     UBool usesPoolBundle;
397     UBool useNativeStrcmp;
398 } ResourceData;
399 
400 /*
401  * Read a resource bundle from memory.
402  */
403 U_INTERNAL void U_EXPORT2
404 res_read(ResourceData *pResData,
405          const UDataInfo *pInfo, const void *inBytes, int32_t length,
406          UErrorCode *errorCode);
407 
408 /*
409  * Load a resource bundle file.
410  * The ResourceData structure must be allocated externally.
411  */
412 U_CFUNC void
413 res_load(ResourceData *pResData,
414          const char *path, const char *name, UErrorCode *errorCode);
415 
416 /*
417  * Release a resource bundle file.
418  * This does not release the ResourceData structure itself.
419  */
420 U_CFUNC void
421 res_unload(ResourceData *pResData);
422 
423 U_INTERNAL UResType U_EXPORT2
424 res_getPublicType(Resource res);
425 
426 /*
427  * Return a pointer to a zero-terminated, const UChar* string
428  * and set its length in *pLength.
429  * Returns NULL if not found.
430  */
431 U_INTERNAL const UChar * U_EXPORT2
432 res_getString(const ResourceData *pResData, Resource res, int32_t *pLength);
433 
434 U_INTERNAL const UChar * U_EXPORT2
435 res_getAlias(const ResourceData *pResData, Resource res, int32_t *pLength);
436 
437 U_INTERNAL const uint8_t * U_EXPORT2
438 res_getBinary(const ResourceData *pResData, Resource res, int32_t *pLength);
439 
440 U_INTERNAL const int32_t * U_EXPORT2
441 res_getIntVector(const ResourceData *pResData, Resource res, int32_t *pLength);
442 
443 U_INTERNAL Resource U_EXPORT2
444 res_getResource(const ResourceData *pResData, const char *key);
445 
446 U_INTERNAL int32_t U_EXPORT2
447 res_countArrayItems(const ResourceData *pResData, Resource res);
448 
449 U_INTERNAL Resource U_EXPORT2
450 res_getArrayItem(const ResourceData *pResData, Resource array, int32_t indexS);
451 
452 U_INTERNAL Resource U_EXPORT2
453 res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key);
454 
455 U_INTERNAL Resource U_EXPORT2
456 res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key);
457 
458 /**
459  * Iterates over the path and stops when a scalar resource is found.
460  * Follows aliases.
461  * Modifies the contents of *path (replacing separators with NULs),
462  * and also moves *path forward while it finds items.
463  *
464  * @param path input: "CollationElements/Sequence" or "zoneStrings/3/2" etc.;
465  *             output: points to the part that has not yet been processed
466  */
467 U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r,
468                                   char** path, const char** key);
469 
470 #ifdef __cplusplus
471 
472 #include "resource.h"
473 
474 U_NAMESPACE_BEGIN
475 
476 class ResourceDataValue : public ResourceValue {
477 public:
ResourceDataValue()478     ResourceDataValue() : pResData(NULL), res(static_cast<Resource>(URES_NONE)) {}
479     virtual ~ResourceDataValue();
480 
setData(const ResourceData * data)481     void setData(const ResourceData *data) { pResData = data; }
setResource(Resource r)482     void setResource(Resource r) { res = r; }
483 
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;
499 
500     const ResourceData *pResData;
501 
502 private:
503     Resource res;
504 };
505 
506 U_NAMESPACE_END
507 
508 #endif  /* __cplusplus */
509 
510 /**
511  * Swap an ICU resource bundle. See udataswp.h.
512  * @internal
513  */
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);
518 
519 #endif
520