]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/uresdata.h
ICU-551.30.tar.gz
[apple/icu.git] / icuSources / common / uresdata.h
1 /*
2 ******************************************************************************
3 * *
4 * Copyright (C) 1999-2012, International Business Machines *
5 * Corporation and others. All Rights Reserved. *
6 * *
7 ******************************************************************************
8 * file name: uresdata.h
9 * encoding: US-ASCII
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 } 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
65 #define RES_GET_TYPE(res) ((int32_t)((res)>>28UL))
66 #define RES_GET_OFFSET(res) ((res)&0x0fffffff)
67 #define RES_GET_POINTER(pRoot, res) ((pRoot)+RES_GET_OFFSET(res))
68
69 /* get signed and unsigned integer values directly from the Resource handle */
70 #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
71 # define RES_GET_INT(res) (((int32_t)((res)<<4L))>>4L)
72 #else
73 # define RES_GET_INT(res) (int32_t)(((res)&0x08000000) ? (res)|0xf0000000 : (res)&0x07ffffff)
74 #endif
75
76 #define RES_GET_UINT(res) ((res)&0x0fffffff)
77
78 #define URES_IS_ARRAY(type) ((int32_t)(type)==URES_ARRAY || (int32_t)(type)==URES_ARRAY16)
79 #define URES_IS_TABLE(type) ((int32_t)(type)==URES_TABLE || (int32_t)(type)==URES_TABLE16 || (int32_t)(type)==URES_TABLE32)
80 #define URES_IS_CONTAINER(type) (URES_IS_TABLE(type) || URES_IS_ARRAY(type))
81
82 #define URES_MAKE_RESOURCE(type, offset) (((Resource)(type)<<28)|(Resource)(offset))
83 #define URES_MAKE_EMPTY_RESOURCE(type) ((Resource)(type)<<28)
84
85 /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
86 enum {
87 URES_INDEX_LENGTH, /* [0] contains URES_INDEX_TOP==the length of indexes[];
88 * formatVersion==1: all bits contain the length of indexes[]
89 * but the length is much less than 0xff;
90 * formatVersion>1:
91 * only bits 7..0 contain the length of indexes[],
92 * bits 31..8 are reserved and set to 0 */
93 URES_INDEX_KEYS_TOP, /* [1] contains the top of the key strings, */
94 /* same as the bottom of resources or UTF-16 strings, rounded up */
95 URES_INDEX_RESOURCES_TOP, /* [2] contains the top of all resources */
96 URES_INDEX_BUNDLE_TOP, /* [3] contains the top of the bundle, */
97 /* in case it were ever different from [2] */
98 URES_INDEX_MAX_TABLE_LENGTH,/* [4] max. length of any table */
99 URES_INDEX_ATTRIBUTES, /* [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2) */
100 URES_INDEX_16BIT_TOP, /* [6] top of the 16-bit units (UTF-16 string v2 UChars, URES_TABLE16, URES_ARRAY16),
101 * rounded up (new in formatVersion 2.0, ICU 4.4) */
102 URES_INDEX_POOL_CHECKSUM, /* [7] checksum of the pool bundle (new in formatVersion 2.0, ICU 4.4) */
103 URES_INDEX_TOP
104 };
105
106 /*
107 * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES].
108 * New in formatVersion 1.2 (ICU 3.6).
109 *
110 * If set, then this resource bundle is a standalone bundle.
111 * If not set, then the bundle participates in locale fallback, eventually
112 * all the way to the root bundle.
113 * If indexes[] is missing or too short, then the attribute cannot be determined
114 * reliably. Dependency checking should ignore such bundles, and loading should
115 * use fallbacks.
116 */
117 #define URES_ATT_NO_FALLBACK 1
118
119 /*
120 * Attributes for bundles that are, or use, a pool bundle.
121 * A pool bundle provides key strings that are shared among several other bundles
122 * to reduce their total size.
123 * New in formatVersion 2 (ICU 4.4).
124 */
125 #define URES_ATT_IS_POOL_BUNDLE 2
126 #define URES_ATT_USES_POOL_BUNDLE 4
127
128 /*
129 * File format for .res resource bundle files (formatVersion=2, ICU 4.4)
130 *
131 * New in formatVersion 2 compared with 1.3: -------------
132 *
133 * Three new resource types -- String-v2, Table16 and Array16 -- have their
134 * values stored in a new array of 16-bit units between the table key strings
135 * and the start of the other resources.
136 *
137 * genrb eliminates duplicates among Unicode string-v2 values.
138 * Multiple Unicode strings may use the same offset and string data,
139 * or a short string may point to the suffix of a longer string. ("Suffix sharing")
140 * For example, one string "abc" may be reused for another string "bc" by pointing
141 * to the second character. (Short strings-v2 are NUL-terminated
142 * and not preceded by an explicit length value.)
143 *
144 * It is allowed for all resource types to share values.
145 * The swapper code (ures_swap()) has been modified so that it swaps each item
146 * exactly once.
147 *
148 * A resource bundle may use a special pool bundle. Some or all of the table key strings
149 * of the using-bundle are omitted, and the key string offsets for such key strings refer
150 * to offsets in the pool bundle.
151 * The using-bundle's and the pool-bundle's indexes[URES_INDEX_POOL_CHECKSUM] values
152 * must match.
153 * Two bits in indexes[URES_INDEX_ATTRIBUTES] indicate whether a resource bundle
154 * is or uses a pool bundle.
155 *
156 * Table key strings must be compared in ASCII order, even if they are not
157 * stored in ASCII.
158 *
159 * New in formatVersion 1.3 compared with 1.2: -------------
160 *
161 * genrb eliminates duplicates among key strings.
162 * Multiple table items may share one key string, or one item may point
163 * to the suffix of another's key string. ("Suffix sharing")
164 * For example, one key "abc" may be reused for another key "bc" by pointing
165 * to the second character. (Key strings are NUL-terminated.)
166 *
167 * -------------
168 *
169 * An ICU4C resource bundle file (.res) is a binary, memory-mappable file
170 * with nested, hierarchical data structures.
171 * It physically contains the following:
172 *
173 * Resource root; -- 32-bit Resource item, root item for this bundle's tree;
174 * currently, the root item must be a table or table32 resource item
175 * int32_t indexes[indexes[0]]; -- array of indexes for friendly
176 * reading and swapping; see URES_INDEX_* above
177 * new in formatVersion 1.1 (ICU 2.8)
178 * char keys[]; -- characters for key strings
179 * (formatVersion 1.0: up to 65k of characters; 1.1: <2G)
180 * (minus the space for root and indexes[]),
181 * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated;
182 * padded to multiple of 4 bytes for 4-alignment of the following data
183 * uint16_t 16BitUnits[]; -- resources that are stored entirely as sequences of 16-bit units
184 * (new in formatVersion 2/ICU 4.4)
185 * data is indexed by the offset values in 16-bit resource types,
186 * with offset 0 pointing to the beginning of this array;
187 * there is a 0 at offset 0, for empty resources;
188 * padded to multiple of 4 bytes for 4-alignment of the following data
189 * data; -- data directly and indirectly indexed by the root item;
190 * the structure is determined by walking the tree
191 *
192 * Each resource bundle item has a 32-bit Resource handle (see typedef above)
193 * which contains the item type number in its upper 4 bits (31..28) and either
194 * an offset or a direct value in its lower 28 bits (27..0).
195 * The order of items is undefined and only determined by walking the tree.
196 * Leaves of the tree may be stored first or last or anywhere in between,
197 * and it is in theory possible to have unreferenced holes in the file.
198 *
199 * 16-bit-unit values:
200 * Starting with formatVersion 2/ICU 4.4, some resources are stored in a special
201 * array of 16-bit units. Each resource value is a sequence of 16-bit units,
202 * with no per-resource padding to a 4-byte boundary.
203 * 16-bit container types (Table16 and Array16) contain Resource16 values
204 * which are offsets to String-v2 resources in the same 16-bit-units array.
205 *
206 * Direct values:
207 * - Empty Unicode strings have an offset value of 0 in the Resource handle itself.
208 * - Starting with formatVersion 2/ICU 4.4, an offset value of 0 for
209 * _any_ resource type indicates an empty value.
210 * - Integer values are 28-bit values stored in the Resource handle itself;
211 * the interpretation of unsigned vs. signed integers is up to the application.
212 *
213 * All other types and values use 28-bit offsets to point to the item's data.
214 * The offset is an index to the first 32-bit word of the value, relative to the
215 * start of the resource data (i.e., the root item handle is at offset 0).
216 * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits).
217 * All resource item values are 4-aligned.
218 *
219 * New in formatVersion 2/ICU 4.4: Some types use offsets into the 16-bit-units array,
220 * indexing 16-bit units in that array.
221 *
222 * The structures (memory layouts) for the values for each item type are listed
223 * in the table below.
224 *
225 * Nested, hierarchical structures: -------------
226 *
227 * Table items contain key-value pairs where the keys are offsets to char * key strings.
228 * The values of these pairs are either Resource handles or
229 * offsets into the 16-bit-units array, depending on the table type.
230 *
231 * Array items are simple vectors of Resource handles,
232 * or of offsets into the 16-bit-units array, depending on the array type.
233 *
234 * Table key string offsets: -------
235 *
236 * Key string offsets are relative to the start of the resource data (of the root handle),
237 * i.e., the first string has an offset of 4+sizeof(indexes).
238 * (After the 4-byte root handle and after the indexes array.)
239 *
240 * If the resource bundle uses a pool bundle, then some key strings are stored
241 * in the pool bundle rather than in the local bundle itself.
242 * - In a Table or Table16, the 16-bit key string offset is local if it is
243 * less than indexes[URES_INDEX_KEYS_TOP]<<2.
244 * Otherwise, subtract indexes[URES_INDEX_KEYS_TOP]<<2 to get the offset into
245 * the pool bundle key strings.
246 * - In a Table32, the 32-bit key string offset is local if it is non-negative.
247 * Otherwise, reset bit 31 to get the pool key string offset.
248 *
249 * Unlike the local offset, the pool key offset is relative to
250 * the start of the key strings, not to the start of the bundle.
251 *
252 * An alias item is special (and new in ICU 2.4): --------------
253 *
254 * Its memory layout is just like for a UnicodeString, but at runtime it resolves to
255 * another resource bundle's item according to the path in the string.
256 * This is used to share items across bundles that are in different lookup/fallback
257 * chains (e.g., large collation data among zh_TW and zh_HK).
258 * This saves space (for large items) and maintenance effort (less duplication of data).
259 *
260 * --------------------------------------------------------------------------
261 *
262 * Resource types:
263 *
264 * Most resources have their values stored at four-byte offsets from the start
265 * of the resource data. These values are at least 4-aligned.
266 * Some resource values are stored directly in the offset field of the Resource itself.
267 * See UResType in unicode/ures.h for enumeration constants for Resource types.
268 *
269 * Some resources have their values stored as sequences of 16-bit units,
270 * at 2-byte offsets from the start of a contiguous 16-bit-unit array between
271 * the table key strings and the other resources. (new in formatVersion 2/ICU 4.4)
272 * At offset 0 of that array is a 16-bit zero value for empty 16-bit resources.
273 * Resource16 values in Table16 and Array16 are 16-bit offsets to String-v2
274 * resources, with the offsets relative to the start of the 16-bit-units array.
275 *
276 * Type Name Memory layout of values
277 * (in parentheses: scalar, non-offset values)
278 *
279 * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding)
280 * or (empty string ("") if offset==0)
281 * 1 Binary: int32_t length, uint8_t[length], (padding)
282 * - the start of the bytes is 16-aligned -
283 * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count]
284 * 3 Alias: (physically same value layout as string, new in ICU 2.4)
285 * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count]
286 * (new in formatVersion 1.1/ICU 2.8)
287 * 5 Table16: uint16_t count, uint16_t keyStringOffsets[count], Resource16[count]
288 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
289 * 6 Unicode String-v2:UChar[length], (UChar)0; length determined by the first UChar:
290 * - if first is not a trail surrogate, then the length is implicit
291 * and u_strlen() needs to be called
292 * - if first<0xdfef then length=first&0x3ff (and skip first)
293 * - if first<0xdfff then length=((first-0xdfef)<<16) | second UChar
294 * - if first==0xdfff then length=((second UChar)<<16) | third UChar
295 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
296 * 7 Integer: (28-bit offset is integer value)
297 * 8 Array: int32_t count, Resource[count]
298 * 9 Array16: uint16_t count, Resource16[count]
299 * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4)
300 * 14 Integer Vector: int32_t length, int32_t[length]
301 * 15 Reserved: This value denotes special purpose resources and is for internal use.
302 *
303 * Note that there are 3 types with data vector values:
304 * - Vectors of 8-bit bytes stored as type Binary.
305 * - Vectors of 16-bit words stored as type Unicode String or Unicode String-v2
306 * (no value restrictions, all values 0..ffff allowed!).
307 * - Vectors of 32-bit words stored as type Integer Vector.
308 */
309
310 /*
311 * Structure for a single, memory-mapped ResourceBundle.
312 */
313 typedef struct {
314 UDataMemory *data;
315 const int32_t *pRoot;
316 const uint16_t *p16BitUnits;
317 const char *poolBundleKeys;
318 Resource rootRes;
319 int32_t localKeyLimit;
320 UBool noFallback; /* see URES_ATT_NO_FALLBACK */
321 UBool isPoolBundle;
322 UBool usesPoolBundle;
323 UBool useNativeStrcmp;
324 } ResourceData;
325
326 /*
327 * Read a resource bundle from memory.
328 */
329 U_INTERNAL void U_EXPORT2
330 res_read(ResourceData *pResData,
331 const UDataInfo *pInfo, const void *inBytes, int32_t length,
332 UErrorCode *errorCode);
333
334 /*
335 * Load a resource bundle file.
336 * The ResourceData structure must be allocated externally.
337 */
338 U_CFUNC void
339 res_load(ResourceData *pResData,
340 const char *path, const char *name, UErrorCode *errorCode);
341
342 /*
343 * Release a resource bundle file.
344 * This does not release the ResourceData structure itself.
345 */
346 U_CFUNC void
347 res_unload(ResourceData *pResData);
348
349 U_INTERNAL UResType U_EXPORT2
350 res_getPublicType(Resource res);
351
352 /*
353 * Return a pointer to a zero-terminated, const UChar* string
354 * and set its length in *pLength.
355 * Returns NULL if not found.
356 */
357 U_INTERNAL const UChar * U_EXPORT2
358 res_getString(const ResourceData *pResData, Resource res, int32_t *pLength);
359
360 U_INTERNAL const UChar * U_EXPORT2
361 res_getAlias(const ResourceData *pResData, Resource res, int32_t *pLength);
362
363 U_INTERNAL const uint8_t * U_EXPORT2
364 res_getBinary(const ResourceData *pResData, Resource res, int32_t *pLength);
365
366 U_INTERNAL const int32_t * U_EXPORT2
367 res_getIntVector(const ResourceData *pResData, Resource res, int32_t *pLength);
368
369 U_INTERNAL Resource U_EXPORT2
370 res_getResource(const ResourceData *pResData, const char *key);
371
372 U_INTERNAL int32_t U_EXPORT2
373 res_countArrayItems(const ResourceData *pResData, Resource res);
374
375 U_INTERNAL Resource U_EXPORT2
376 res_getArrayItem(const ResourceData *pResData, Resource array, int32_t indexS);
377
378 U_INTERNAL Resource U_EXPORT2
379 res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key);
380
381 U_INTERNAL Resource U_EXPORT2
382 res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key);
383
384 /*
385 * Modifies the contents of *path (replacing separators with NULs),
386 * and also moves *path forward while it finds items.
387 */
388 U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key);
389
390 /**
391 * Swap an ICU resource bundle. See udataswp.h.
392 * @internal
393 */
394 U_CAPI int32_t U_EXPORT2
395 ures_swap(const UDataSwapper *ds,
396 const void *inData, int32_t length, void *outData,
397 UErrorCode *pErrorCode);
398
399 #endif