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