michael@0: /* michael@0: ****************************************************************************** michael@0: * * michael@0: * Copyright (C) 1999-2012, International Business Machines * michael@0: * Corporation and others. All Rights Reserved. * michael@0: * * michael@0: ****************************************************************************** michael@0: * file name: uresdata.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 1999dec08 michael@0: * created by: Markus W. Scherer michael@0: * 06/24/02 weiv Added support for resource sharing michael@0: */ michael@0: michael@0: #ifndef __RESDATA_H__ michael@0: #define __RESDATA_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/udata.h" michael@0: #include "unicode/ures.h" michael@0: #include "putilimp.h" michael@0: #include "udataswp.h" michael@0: michael@0: /** michael@0: * Numeric constants for internal-only types of resource items. michael@0: * These must use different numeric values than UResType constants michael@0: * because they are used together. michael@0: * Internal types are never returned by ures_getType(). michael@0: */ michael@0: typedef enum { michael@0: /** Include a negative value so that the compiler uses the same int type as for UResType. */ michael@0: URES_INTERNAL_NONE=-1, michael@0: michael@0: /** Resource type constant for tables with 32-bit count, key offsets and values. */ michael@0: URES_TABLE32=4, michael@0: michael@0: /** michael@0: * Resource type constant for tables with 16-bit count, key offsets and values. michael@0: * All values are URES_STRING_V2 strings. michael@0: */ michael@0: URES_TABLE16=5, michael@0: michael@0: /** Resource type constant for 16-bit Unicode strings in formatVersion 2. */ michael@0: URES_STRING_V2=6, michael@0: michael@0: /** michael@0: * Resource type constant for arrays with 16-bit count and values. michael@0: * All values are URES_STRING_V2 strings. michael@0: */ michael@0: URES_ARRAY16=9 michael@0: } UResInternalType; michael@0: michael@0: /* michael@0: * A Resource is a 32-bit value that has 2 bit fields: michael@0: * 31..28 4-bit type, see enum below michael@0: * 27..0 28-bit four-byte-offset or value according to the type michael@0: */ michael@0: typedef uint32_t Resource; michael@0: michael@0: #define RES_BOGUS 0xffffffff michael@0: michael@0: #define RES_GET_TYPE(res) ((int32_t)((res)>>28UL)) michael@0: #define RES_GET_OFFSET(res) ((res)&0x0fffffff) michael@0: #define RES_GET_POINTER(pRoot, res) ((pRoot)+RES_GET_OFFSET(res)) michael@0: michael@0: /* get signed and unsigned integer values directly from the Resource handle */ michael@0: #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC michael@0: # define RES_GET_INT(res) (((int32_t)((res)<<4L))>>4L) michael@0: #else michael@0: # define RES_GET_INT(res) (int32_t)(((res)&0x08000000) ? (res)|0xf0000000 : (res)&0x07ffffff) michael@0: #endif michael@0: michael@0: #define RES_GET_UINT(res) ((res)&0x0fffffff) michael@0: michael@0: #define URES_IS_ARRAY(type) ((int32_t)(type)==URES_ARRAY || (int32_t)(type)==URES_ARRAY16) michael@0: #define URES_IS_TABLE(type) ((int32_t)(type)==URES_TABLE || (int32_t)(type)==URES_TABLE16 || (int32_t)(type)==URES_TABLE32) michael@0: #define URES_IS_CONTAINER(type) (URES_IS_TABLE(type) || URES_IS_ARRAY(type)) michael@0: michael@0: #define URES_MAKE_RESOURCE(type, offset) (((Resource)(type)<<28)|(Resource)(offset)) michael@0: #define URES_MAKE_EMPTY_RESOURCE(type) ((Resource)(type)<<28) michael@0: michael@0: /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */ michael@0: enum { michael@0: URES_INDEX_LENGTH, /* [0] contains URES_INDEX_TOP==the length of indexes[]; michael@0: * formatVersion==1: all bits contain the length of indexes[] michael@0: * but the length is much less than 0xff; michael@0: * formatVersion>1: michael@0: * only bits 7..0 contain the length of indexes[], michael@0: * bits 31..8 are reserved and set to 0 */ michael@0: URES_INDEX_KEYS_TOP, /* [1] contains the top of the key strings, */ michael@0: /* same as the bottom of resources or UTF-16 strings, rounded up */ michael@0: URES_INDEX_RESOURCES_TOP, /* [2] contains the top of all resources */ michael@0: URES_INDEX_BUNDLE_TOP, /* [3] contains the top of the bundle, */ michael@0: /* in case it were ever different from [2] */ michael@0: URES_INDEX_MAX_TABLE_LENGTH,/* [4] max. length of any table */ michael@0: URES_INDEX_ATTRIBUTES, /* [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2) */ michael@0: URES_INDEX_16BIT_TOP, /* [6] top of the 16-bit units (UTF-16 string v2 UChars, URES_TABLE16, URES_ARRAY16), michael@0: * rounded up (new in formatVersion 2.0, ICU 4.4) */ michael@0: URES_INDEX_POOL_CHECKSUM, /* [7] checksum of the pool bundle (new in formatVersion 2.0, ICU 4.4) */ michael@0: URES_INDEX_TOP michael@0: }; michael@0: michael@0: /* michael@0: * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES]. michael@0: * New in formatVersion 1.2 (ICU 3.6). michael@0: * michael@0: * If set, then this resource bundle is a standalone bundle. michael@0: * If not set, then the bundle participates in locale fallback, eventually michael@0: * all the way to the root bundle. michael@0: * If indexes[] is missing or too short, then the attribute cannot be determined michael@0: * reliably. Dependency checking should ignore such bundles, and loading should michael@0: * use fallbacks. michael@0: */ michael@0: #define URES_ATT_NO_FALLBACK 1 michael@0: michael@0: /* michael@0: * Attributes for bundles that are, or use, a pool bundle. michael@0: * A pool bundle provides key strings that are shared among several other bundles michael@0: * to reduce their total size. michael@0: * New in formatVersion 2 (ICU 4.4). michael@0: */ michael@0: #define URES_ATT_IS_POOL_BUNDLE 2 michael@0: #define URES_ATT_USES_POOL_BUNDLE 4 michael@0: michael@0: /* michael@0: * File format for .res resource bundle files (formatVersion=2, ICU 4.4) michael@0: * michael@0: * New in formatVersion 2 compared with 1.3: ------------- michael@0: * michael@0: * Three new resource types -- String-v2, Table16 and Array16 -- have their michael@0: * values stored in a new array of 16-bit units between the table key strings michael@0: * and the start of the other resources. michael@0: * michael@0: * genrb eliminates duplicates among Unicode string-v2 values. michael@0: * Multiple Unicode strings may use the same offset and string data, michael@0: * or a short string may point to the suffix of a longer string. ("Suffix sharing") michael@0: * For example, one string "abc" may be reused for another string "bc" by pointing michael@0: * to the second character. (Short strings-v2 are NUL-terminated michael@0: * and not preceded by an explicit length value.) michael@0: * michael@0: * It is allowed for all resource types to share values. michael@0: * The swapper code (ures_swap()) has been modified so that it swaps each item michael@0: * exactly once. michael@0: * michael@0: * A resource bundle may use a special pool bundle. Some or all of the table key strings michael@0: * of the using-bundle are omitted, and the key string offsets for such key strings refer michael@0: * to offsets in the pool bundle. michael@0: * The using-bundle's and the pool-bundle's indexes[URES_INDEX_POOL_CHECKSUM] values michael@0: * must match. michael@0: * Two bits in indexes[URES_INDEX_ATTRIBUTES] indicate whether a resource bundle michael@0: * is or uses a pool bundle. michael@0: * michael@0: * Table key strings must be compared in ASCII order, even if they are not michael@0: * stored in ASCII. michael@0: * michael@0: * New in formatVersion 1.3 compared with 1.2: ------------- michael@0: * michael@0: * genrb eliminates duplicates among key strings. michael@0: * Multiple table items may share one key string, or one item may point michael@0: * to the suffix of another's key string. ("Suffix sharing") michael@0: * For example, one key "abc" may be reused for another key "bc" by pointing michael@0: * to the second character. (Key strings are NUL-terminated.) michael@0: * michael@0: * ------------- michael@0: * michael@0: * An ICU4C resource bundle file (.res) is a binary, memory-mappable file michael@0: * with nested, hierarchical data structures. michael@0: * It physically contains the following: michael@0: * michael@0: * Resource root; -- 32-bit Resource item, root item for this bundle's tree; michael@0: * currently, the root item must be a table or table32 resource item michael@0: * int32_t indexes[indexes[0]]; -- array of indexes for friendly michael@0: * reading and swapping; see URES_INDEX_* above michael@0: * new in formatVersion 1.1 (ICU 2.8) michael@0: * char keys[]; -- characters for key strings michael@0: * (formatVersion 1.0: up to 65k of characters; 1.1: <2G) michael@0: * (minus the space for root and indexes[]), michael@0: * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated; michael@0: * padded to multiple of 4 bytes for 4-alignment of the following data michael@0: * uint16_t 16BitUnits[]; -- resources that are stored entirely as sequences of 16-bit units michael@0: * (new in formatVersion 2/ICU 4.4) michael@0: * data is indexed by the offset values in 16-bit resource types, michael@0: * with offset 0 pointing to the beginning of this array; michael@0: * there is a 0 at offset 0, for empty resources; michael@0: * padded to multiple of 4 bytes for 4-alignment of the following data michael@0: * data; -- data directly and indirectly indexed by the root item; michael@0: * the structure is determined by walking the tree michael@0: * michael@0: * Each resource bundle item has a 32-bit Resource handle (see typedef above) michael@0: * which contains the item type number in its upper 4 bits (31..28) and either michael@0: * an offset or a direct value in its lower 28 bits (27..0). michael@0: * The order of items is undefined and only determined by walking the tree. michael@0: * Leaves of the tree may be stored first or last or anywhere in between, michael@0: * and it is in theory possible to have unreferenced holes in the file. michael@0: * michael@0: * 16-bit-unit values: michael@0: * Starting with formatVersion 2/ICU 4.4, some resources are stored in a special michael@0: * array of 16-bit units. Each resource value is a sequence of 16-bit units, michael@0: * with no per-resource padding to a 4-byte boundary. michael@0: * 16-bit container types (Table16 and Array16) contain Resource16 values michael@0: * which are offsets to String-v2 resources in the same 16-bit-units array. michael@0: * michael@0: * Direct values: michael@0: * - Empty Unicode strings have an offset value of 0 in the Resource handle itself. michael@0: * - Starting with formatVersion 2/ICU 4.4, an offset value of 0 for michael@0: * _any_ resource type indicates an empty value. michael@0: * - Integer values are 28-bit values stored in the Resource handle itself; michael@0: * the interpretation of unsigned vs. signed integers is up to the application. michael@0: * michael@0: * All other types and values use 28-bit offsets to point to the item's data. michael@0: * The offset is an index to the first 32-bit word of the value, relative to the michael@0: * start of the resource data (i.e., the root item handle is at offset 0). michael@0: * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits). michael@0: * All resource item values are 4-aligned. michael@0: * michael@0: * New in formatVersion 2/ICU 4.4: Some types use offsets into the 16-bit-units array, michael@0: * indexing 16-bit units in that array. michael@0: * michael@0: * The structures (memory layouts) for the values for each item type are listed michael@0: * in the table below. michael@0: * michael@0: * Nested, hierarchical structures: ------------- michael@0: * michael@0: * Table items contain key-value pairs where the keys are offsets to char * key strings. michael@0: * The values of these pairs are either Resource handles or michael@0: * offsets into the 16-bit-units array, depending on the table type. michael@0: * michael@0: * Array items are simple vectors of Resource handles, michael@0: * or of offsets into the 16-bit-units array, depending on the array type. michael@0: * michael@0: * Table key string offsets: ------- michael@0: * michael@0: * Key string offsets are relative to the start of the resource data (of the root handle), michael@0: * i.e., the first string has an offset of 4+sizeof(indexes). michael@0: * (After the 4-byte root handle and after the indexes array.) michael@0: * michael@0: * If the resource bundle uses a pool bundle, then some key strings are stored michael@0: * in the pool bundle rather than in the local bundle itself. michael@0: * - In a Table or Table16, the 16-bit key string offset is local if it is michael@0: * less than indexes[URES_INDEX_KEYS_TOP]<<2. michael@0: * Otherwise, subtract indexes[URES_INDEX_KEYS_TOP]<<2 to get the offset into michael@0: * the pool bundle key strings. michael@0: * - In a Table32, the 32-bit key string offset is local if it is non-negative. michael@0: * Otherwise, reset bit 31 to get the pool key string offset. michael@0: * michael@0: * Unlike the local offset, the pool key offset is relative to michael@0: * the start of the key strings, not to the start of the bundle. michael@0: * michael@0: * An alias item is special (and new in ICU 2.4): -------------- michael@0: * michael@0: * Its memory layout is just like for a UnicodeString, but at runtime it resolves to michael@0: * another resource bundle's item according to the path in the string. michael@0: * This is used to share items across bundles that are in different lookup/fallback michael@0: * chains (e.g., large collation data among zh_TW and zh_HK). michael@0: * This saves space (for large items) and maintenance effort (less duplication of data). michael@0: * michael@0: * -------------------------------------------------------------------------- michael@0: * michael@0: * Resource types: michael@0: * michael@0: * Most resources have their values stored at four-byte offsets from the start michael@0: * of the resource data. These values are at least 4-aligned. michael@0: * Some resource values are stored directly in the offset field of the Resource itself. michael@0: * See UResType in unicode/ures.h for enumeration constants for Resource types. michael@0: * michael@0: * Some resources have their values stored as sequences of 16-bit units, michael@0: * at 2-byte offsets from the start of a contiguous 16-bit-unit array between michael@0: * the table key strings and the other resources. (new in formatVersion 2/ICU 4.4) michael@0: * At offset 0 of that array is a 16-bit zero value for empty 16-bit resources. michael@0: * Resource16 values in Table16 and Array16 are 16-bit offsets to String-v2 michael@0: * resources, with the offsets relative to the start of the 16-bit-units array. michael@0: * michael@0: * Type Name Memory layout of values michael@0: * (in parentheses: scalar, non-offset values) michael@0: * michael@0: * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding) michael@0: * or (empty string ("") if offset==0) michael@0: * 1 Binary: int32_t length, uint8_t[length], (padding) michael@0: * - the start of the bytes is 16-aligned - michael@0: * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count] michael@0: * 3 Alias: (physically same value layout as string, new in ICU 2.4) michael@0: * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count] michael@0: * (new in formatVersion 1.1/ICU 2.8) michael@0: * 5 Table16: uint16_t count, uint16_t keyStringOffsets[count], Resource16[count] michael@0: * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4) michael@0: * 6 Unicode String-v2:UChar[length], (UChar)0; length determined by the first UChar: michael@0: * - if first is not a trail surrogate, then the length is implicit michael@0: * and u_strlen() needs to be called michael@0: * - if first<0xdfef then length=first&0x3ff (and skip first) michael@0: * - if first<0xdfff then length=((first-0xdfef)<<16) | second UChar michael@0: * - if first==0xdfff then length=((second UChar)<<16) | third UChar michael@0: * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4) michael@0: * 7 Integer: (28-bit offset is integer value) michael@0: * 8 Array: int32_t count, Resource[count] michael@0: * 9 Array16: uint16_t count, Resource16[count] michael@0: * (stored in the 16-bit-units array; new in formatVersion 2/ICU 4.4) michael@0: * 14 Integer Vector: int32_t length, int32_t[length] michael@0: * 15 Reserved: This value denotes special purpose resources and is for internal use. michael@0: * michael@0: * Note that there are 3 types with data vector values: michael@0: * - Vectors of 8-bit bytes stored as type Binary. michael@0: * - Vectors of 16-bit words stored as type Unicode String or Unicode String-v2 michael@0: * (no value restrictions, all values 0..ffff allowed!). michael@0: * - Vectors of 32-bit words stored as type Integer Vector. michael@0: */ michael@0: michael@0: /* michael@0: * Structure for a single, memory-mapped ResourceBundle. michael@0: */ michael@0: typedef struct { michael@0: UDataMemory *data; michael@0: const int32_t *pRoot; michael@0: const uint16_t *p16BitUnits; michael@0: const char *poolBundleKeys; michael@0: Resource rootRes; michael@0: int32_t localKeyLimit; michael@0: UBool noFallback; /* see URES_ATT_NO_FALLBACK */ michael@0: UBool isPoolBundle; michael@0: UBool usesPoolBundle; michael@0: UBool useNativeStrcmp; michael@0: } ResourceData; michael@0: michael@0: /* michael@0: * Read a resource bundle from memory. michael@0: */ michael@0: U_INTERNAL void U_EXPORT2 michael@0: res_read(ResourceData *pResData, michael@0: const UDataInfo *pInfo, const void *inBytes, int32_t length, michael@0: UErrorCode *errorCode); michael@0: michael@0: /* michael@0: * Load a resource bundle file. michael@0: * The ResourceData structure must be allocated externally. michael@0: */ michael@0: U_CFUNC void michael@0: res_load(ResourceData *pResData, michael@0: const char *path, const char *name, UErrorCode *errorCode); michael@0: michael@0: /* michael@0: * Release a resource bundle file. michael@0: * This does not release the ResourceData structure itself. michael@0: */ michael@0: U_CFUNC void michael@0: res_unload(ResourceData *pResData); michael@0: michael@0: U_INTERNAL UResType U_EXPORT2 michael@0: res_getPublicType(Resource res); michael@0: michael@0: /* michael@0: * Return a pointer to a zero-terminated, const UChar* string michael@0: * and set its length in *pLength. michael@0: * Returns NULL if not found. michael@0: */ michael@0: U_INTERNAL const UChar * U_EXPORT2 michael@0: res_getString(const ResourceData *pResData, Resource res, int32_t *pLength); michael@0: michael@0: U_INTERNAL const UChar * U_EXPORT2 michael@0: res_getAlias(const ResourceData *pResData, Resource res, int32_t *pLength); michael@0: michael@0: U_INTERNAL const uint8_t * U_EXPORT2 michael@0: res_getBinary(const ResourceData *pResData, Resource res, int32_t *pLength); michael@0: michael@0: U_INTERNAL const int32_t * U_EXPORT2 michael@0: res_getIntVector(const ResourceData *pResData, Resource res, int32_t *pLength); michael@0: michael@0: U_INTERNAL Resource U_EXPORT2 michael@0: res_getResource(const ResourceData *pResData, const char *key); michael@0: michael@0: U_INTERNAL int32_t U_EXPORT2 michael@0: res_countArrayItems(const ResourceData *pResData, Resource res); michael@0: michael@0: U_INTERNAL Resource U_EXPORT2 michael@0: res_getArrayItem(const ResourceData *pResData, Resource array, int32_t indexS); michael@0: michael@0: U_INTERNAL Resource U_EXPORT2 michael@0: res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key); michael@0: michael@0: U_INTERNAL Resource U_EXPORT2 michael@0: res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key); michael@0: michael@0: /* michael@0: * Modifies the contents of *path (replacing separators with NULs), michael@0: * and also moves *path forward while it finds items. michael@0: */ michael@0: U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key); michael@0: michael@0: /** michael@0: * Swap an ICU resource bundle. See udataswp.h. michael@0: * @internal michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ures_swap(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode); michael@0: michael@0: #endif