sl@0: /* sl@0: ****************************************************************************** sl@0: * * sl@0: * Copyright (C) 1999-2003, International Business Machines * sl@0: * Corporation and others. All Rights Reserved. * sl@0: * * sl@0: ****************************************************************************** sl@0: * file name: uresdata.h sl@0: * encoding: US-ASCII sl@0: * tab size: 8 (not used) sl@0: * indentation:4 sl@0: * sl@0: * created on: 1999dec08 sl@0: * created by: Markus W. Scherer sl@0: * 06/24/02 weiv Added support for resource sharing sl@0: */ sl@0: sl@0: #ifndef __RESDATA_H__ sl@0: #define __RESDATA_H__ sl@0: sl@0: #include "unicode/utypes.h" sl@0: #include "unicode/udata.h" sl@0: #include "udataswp.h" sl@0: sl@0: /* sl@0: * A Resource is a 32-bit value that has 2 bit fields: sl@0: * 31..28 4-bit type, see enum below sl@0: * 27..0 28-bit four-byte-offset or value according to the type sl@0: */ sl@0: typedef uint32_t Resource; sl@0: sl@0: #define RES_BOGUS 0xffffffff sl@0: sl@0: #define RES_GET_TYPE(res) ((res)>>28UL) sl@0: #define RES_GET_OFFSET(res) ((res)&0x0fffffff) sl@0: #define RES_GET_POINTER(pRoot, res) ((pRoot)+RES_GET_OFFSET(res)) sl@0: sl@0: /* get signed and unsigned integer values directly from the Resource handle */ sl@0: #define RES_GET_INT(res) (((int32_t)((res)<<4L))>>4L) sl@0: #define RES_GET_UINT(res) ((res)&0x0fffffff) sl@0: sl@0: /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */ sl@0: enum { sl@0: URES_INDEX_LENGTH, /* [0] contains URES_INDEX_TOP==the length of indexes[] */ sl@0: URES_INDEX_STRINGS_TOP, /* [1] contains the top of the strings, */ sl@0: /* same as the bottom of resources, rounded up */ sl@0: URES_INDEX_RESOURCES_TOP, /* [2] contains the top of all resources */ sl@0: URES_INDEX_BUNDLE_TOP, /* [3] contains the top of the bundle, */ sl@0: /* in case it were ever different from [2] */ sl@0: URES_INDEX_MAX_TABLE_LENGTH,/* [4] max. length of any table */ sl@0: URES_INDEX_TOP sl@0: }; sl@0: sl@0: /* number of bytes at the beginning of the bundle before the strings start */ sl@0: enum { sl@0: URES_STRINGS_BOTTOM=(1+URES_INDEX_TOP)*4 sl@0: }; sl@0: sl@0: /* sl@0: * File format for .res resource bundle files (formatVersion=1.1) sl@0: * sl@0: * An ICU4C resource bundle file (.res) is a binary, memory-mappable file sl@0: * with nested, hierarchical data structures. sl@0: * It physically contains the following: sl@0: * sl@0: * Resource root; -- 32-bit Resource item, root item for this bundle's tree; sl@0: * currently, the root item must be a table or table32 resource item sl@0: * int32_t indexes[indexes[0]]; -- array of indexes for friendly sl@0: * reading and swapping; see URES_INDEX_* above sl@0: * new in formatVersion 1.1 sl@0: * char keys[]; -- characters for key strings sl@0: * (formatVersion 1.0: up to 65k of characters; 1.1: <2G) sl@0: * (minus the space for root and indexes[]), sl@0: * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated; sl@0: * padded to multiple of 4 bytes for 4-alignment of the following data sl@0: * data; -- data directly and indirectly indexed by the root item; sl@0: * the structure is determined by walking the tree sl@0: * sl@0: * Each resource bundle item has a 32-bit Resource handle (see typedef above) sl@0: * which contains the item type number in its upper 4 bits (31..28) and either sl@0: * an offset or a direct value in its lower 28 bits (27..0). sl@0: * The order of items is undefined and only determined by walking the tree. sl@0: * Leaves of the tree may be stored first or last or anywhere in between, sl@0: * and it is in theory possible to have unreferenced holes in the file. sl@0: * sl@0: * Direct values: sl@0: * - Empty Unicode strings have an offset value of 0 in the Resource handle itself. sl@0: * - Integer values are 28-bit values stored in the Resource handle itself; sl@0: * the interpretation of unsigned vs. signed integers is up to the application. sl@0: * sl@0: * All other types and values use 28-bit offsets to point to the item's data. sl@0: * The offset is an index to the first 32-bit word of the value, relative to the sl@0: * start of the resource data (i.e., the root item handle is at offset 0). sl@0: * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits). sl@0: * All resource item values are 4-aligned. sl@0: * sl@0: * The structures (memory layouts) for the values for each item type are listed sl@0: * in the table above. sl@0: * sl@0: * Nested, hierarchical structures: ------------- sl@0: * sl@0: * Table items contain key-value pairs where the keys are 16-bit offsets to char * key strings. sl@0: * Key string offsets are also relative to the start of the resource data (of the root handle), sl@0: * i.e., the first string has an offset of 4 (after the 4-byte root handle). sl@0: * sl@0: * The values of these pairs are Resource handles. sl@0: * sl@0: * Array items are simple vectors of Resource handles. sl@0: * sl@0: * An alias item is special (and new in ICU 2.4): -------------- sl@0: * sl@0: * Its memory layout is just like for a UnicodeString, but at runtime it resolves to sl@0: * another resource bundle's item according to the path in the string. sl@0: * This is used to share items across bundles that are in different lookup/fallback sl@0: * chains (e.g., large collation data among zh_TW and zh_HK). sl@0: * This saves space (for large items) and maintenance effort (less duplication of data). sl@0: * sl@0: * -------------------------------------------------------------------------- sl@0: * sl@0: * Resource types: sl@0: * sl@0: * Most resources have their values stored at four-byte offsets from the start sl@0: * of the resource data. These values are at least 4-aligned. sl@0: * Some resource values are stored directly in the offset field of the Resource itself. sl@0: * See UResType in unicode/ures.h for enumeration constants for Resource types. sl@0: * sl@0: * Type Name Memory layout of values sl@0: * (in parentheses: scalar, non-offset values) sl@0: * sl@0: * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding) sl@0: * or (empty string ("") if offset==0) sl@0: * 1 Binary: int32_t length, uint8_t[length], (padding) sl@0: * - this value should be 32-aligned - sl@0: * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count] sl@0: * 3 Alias: (physically same value layout as string, new in ICU 2.4) sl@0: * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count] sl@0: * (new in formatVersion 1.1/ICU 2.8) sl@0: * sl@0: * 7 Integer: (28-bit offset is integer value) sl@0: * 8 Array: int32_t count, Resource[count] sl@0: * sl@0: * 14 Integer Vector: int32_t length, int32_t[length] sl@0: * 15 Reserved: This value denotes special purpose resources and is for internal use. sl@0: * sl@0: * Note that there are 3 types with data vector values: sl@0: * - Vectors of 8-bit bytes stored as type Binary. sl@0: * - Vectors of 16-bit words stored as type Unicode String sl@0: * (no value restrictions, all values 0..ffff allowed!). sl@0: * - Vectors of 32-bit words stored as type Integer Vector. sl@0: */ sl@0: sl@0: /* sl@0: * Structure for a single, memory-mapped ResourceBundle. sl@0: */ sl@0: typedef struct { sl@0: UDataMemory *data; sl@0: Resource *pRoot; sl@0: Resource rootRes; sl@0: } ResourceData; sl@0: sl@0: /* sl@0: * Load a resource bundle file. sl@0: * The ResourceData structure must be allocated externally. sl@0: */ sl@0: U_CFUNC UBool sl@0: res_load(ResourceData *pResData, sl@0: const char *path, const char *name, UErrorCode *errorCode); sl@0: sl@0: /* sl@0: * Release a resource bundle file. sl@0: * This does not release the ResourceData structure itself. sl@0: */ sl@0: U_CFUNC void sl@0: res_unload(ResourceData *pResData); sl@0: sl@0: /* sl@0: * Return a pointer to a zero-terminated, const UChar* string sl@0: * and set its length in *pLength. sl@0: * Returns NULL if not found. sl@0: */ sl@0: U_CFUNC const UChar * sl@0: res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength); sl@0: sl@0: U_CFUNC const UChar * sl@0: res_getAlias(const ResourceData *pResData, const Resource res, int32_t *pLength); sl@0: sl@0: U_CFUNC const uint8_t * sl@0: res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength); sl@0: sl@0: U_CFUNC const int32_t * sl@0: res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength); sl@0: sl@0: U_CFUNC Resource sl@0: res_getResource(const ResourceData *pResData, const char *key); sl@0: sl@0: U_CFUNC int32_t sl@0: res_countArrayItems(const ResourceData *pResData, const Resource res); sl@0: sl@0: U_CFUNC Resource res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexS); sl@0: U_CFUNC Resource res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key); sl@0: U_CFUNC Resource res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key); sl@0: sl@0: /* sl@0: * Modifies the contents of *path (replacing separators with NULs), sl@0: * and also moves *path forward while it finds items. sl@0: */ sl@0: U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key); sl@0: sl@0: /** sl@0: * Swap an ICU resource bundle. See udataswp.h. sl@0: * @internal sl@0: */ sl@0: U_CAPI int32_t U_EXPORT2 sl@0: ures_swap(const UDataSwapper *ds, sl@0: const void *inData, int32_t length, void *outData, sl@0: UErrorCode *pErrorCode); sl@0: sl@0: #endif