os/textandloc/fontservices/textshaperplugin/IcuSource/common/uresdata.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
******************************************************************************
sl@0
     3
*                                                                            *
sl@0
     4
* Copyright (C) 1999-2003, International Business Machines                   *
sl@0
     5
*                Corporation and others. All Rights Reserved.                *
sl@0
     6
*                                                                            *
sl@0
     7
******************************************************************************
sl@0
     8
*   file name:  uresdata.h
sl@0
     9
*   encoding:   US-ASCII
sl@0
    10
*   tab size:   8 (not used)
sl@0
    11
*   indentation:4
sl@0
    12
*
sl@0
    13
*   created on: 1999dec08
sl@0
    14
*   created by: Markus W. Scherer
sl@0
    15
*   06/24/02    weiv        Added support for resource sharing
sl@0
    16
*/
sl@0
    17
sl@0
    18
#ifndef __RESDATA_H__
sl@0
    19
#define __RESDATA_H__
sl@0
    20
sl@0
    21
#include "unicode/utypes.h"
sl@0
    22
#include "unicode/udata.h"
sl@0
    23
#include "udataswp.h"
sl@0
    24
sl@0
    25
/*
sl@0
    26
 * A Resource is a 32-bit value that has 2 bit fields:
sl@0
    27
 * 31..28   4-bit type, see enum below
sl@0
    28
 * 27..0    28-bit four-byte-offset or value according to the type
sl@0
    29
 */
sl@0
    30
typedef uint32_t Resource;
sl@0
    31
sl@0
    32
#define RES_BOGUS 0xffffffff
sl@0
    33
sl@0
    34
#define RES_GET_TYPE(res) ((res)>>28UL)
sl@0
    35
#define RES_GET_OFFSET(res) ((res)&0x0fffffff)
sl@0
    36
#define RES_GET_POINTER(pRoot, res) ((pRoot)+RES_GET_OFFSET(res))
sl@0
    37
sl@0
    38
/* get signed and unsigned integer values directly from the Resource handle */
sl@0
    39
#define RES_GET_INT(res) (((int32_t)((res)<<4L))>>4L)
sl@0
    40
#define RES_GET_UINT(res) ((res)&0x0fffffff)
sl@0
    41
sl@0
    42
/* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
sl@0
    43
enum {
sl@0
    44
    URES_INDEX_LENGTH,          /* [0] contains URES_INDEX_TOP==the length of indexes[] */
sl@0
    45
    URES_INDEX_STRINGS_TOP,     /* [1] contains the top of the strings, */
sl@0
    46
                                /*     same as the bottom of resources, rounded up */
sl@0
    47
    URES_INDEX_RESOURCES_TOP,   /* [2] contains the top of all resources */
sl@0
    48
    URES_INDEX_BUNDLE_TOP,      /* [3] contains the top of the bundle, */
sl@0
    49
                                /*     in case it were ever different from [2] */
sl@0
    50
    URES_INDEX_MAX_TABLE_LENGTH,/* [4] max. length of any table */
sl@0
    51
    URES_INDEX_TOP
sl@0
    52
};
sl@0
    53
sl@0
    54
/* number of bytes at the beginning of the bundle before the strings start */
sl@0
    55
enum {
sl@0
    56
    URES_STRINGS_BOTTOM=(1+URES_INDEX_TOP)*4
sl@0
    57
};
sl@0
    58
sl@0
    59
/*
sl@0
    60
 * File format for .res resource bundle files (formatVersion=1.1)
sl@0
    61
 *
sl@0
    62
 * An ICU4C resource bundle file (.res) is a binary, memory-mappable file
sl@0
    63
 * with nested, hierarchical data structures.
sl@0
    64
 * It physically contains the following:
sl@0
    65
 *
sl@0
    66
 *   Resource root; -- 32-bit Resource item, root item for this bundle's tree;
sl@0
    67
 *                     currently, the root item must be a table or table32 resource item
sl@0
    68
 *   int32_t indexes[indexes[0]]; -- array of indexes for friendly
sl@0
    69
 *                                   reading and swapping; see URES_INDEX_* above
sl@0
    70
 *                                   new in formatVersion 1.1
sl@0
    71
 *   char keys[]; -- characters for key strings
sl@0
    72
 *                   (formatVersion 1.0: up to 65k of characters; 1.1: <2G)
sl@0
    73
 *                   (minus the space for root and indexes[]),
sl@0
    74
 *                   which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated;
sl@0
    75
 *                   padded to multiple of 4 bytes for 4-alignment of the following data
sl@0
    76
 *   data; -- data directly and indirectly indexed by the root item;
sl@0
    77
 *            the structure is determined by walking the tree
sl@0
    78
 *
sl@0
    79
 * Each resource bundle item has a 32-bit Resource handle (see typedef above)
sl@0
    80
 * which contains the item type number in its upper 4 bits (31..28) and either
sl@0
    81
 * an offset or a direct value in its lower 28 bits (27..0).
sl@0
    82
 * The order of items is undefined and only determined by walking the tree.
sl@0
    83
 * Leaves of the tree may be stored first or last or anywhere in between,
sl@0
    84
 * and it is in theory possible to have unreferenced holes in the file.
sl@0
    85
 *
sl@0
    86
 * Direct values:
sl@0
    87
 * - Empty Unicode strings have an offset value of 0 in the Resource handle itself.
sl@0
    88
 * - Integer values are 28-bit values stored in the Resource handle itself;
sl@0
    89
 *   the interpretation of unsigned vs. signed integers is up to the application.
sl@0
    90
 *
sl@0
    91
 * All other types and values use 28-bit offsets to point to the item's data.
sl@0
    92
 * The offset is an index to the first 32-bit word of the value, relative to the
sl@0
    93
 * start of the resource data (i.e., the root item handle is at offset 0).
sl@0
    94
 * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits).
sl@0
    95
 * All resource item values are 4-aligned.
sl@0
    96
 *
sl@0
    97
 * The structures (memory layouts) for the values for each item type are listed
sl@0
    98
 * in the table above.
sl@0
    99
 *
sl@0
   100
 * Nested, hierarchical structures: -------------
sl@0
   101
 *
sl@0
   102
 * Table items contain key-value pairs where the keys are 16-bit offsets to char * key strings.
sl@0
   103
 * Key string offsets are also relative to the start of the resource data (of the root handle),
sl@0
   104
 * i.e., the first string has an offset of 4 (after the 4-byte root handle).
sl@0
   105
 *
sl@0
   106
 * The values of these pairs are Resource handles.
sl@0
   107
 *
sl@0
   108
 * Array items are simple vectors of Resource handles.
sl@0
   109
 *
sl@0
   110
 * An alias item is special (and new in ICU 2.4): --------------
sl@0
   111
 *
sl@0
   112
 * Its memory layout is just like for a UnicodeString, but at runtime it resolves to
sl@0
   113
 * another resource bundle's item according to the path in the string.
sl@0
   114
 * This is used to share items across bundles that are in different lookup/fallback
sl@0
   115
 * chains (e.g., large collation data among zh_TW and zh_HK).
sl@0
   116
 * This saves space (for large items) and maintenance effort (less duplication of data).
sl@0
   117
 *
sl@0
   118
 * --------------------------------------------------------------------------
sl@0
   119
 *
sl@0
   120
 * Resource types:
sl@0
   121
 *
sl@0
   122
 * Most resources have their values stored at four-byte offsets from the start
sl@0
   123
 * of the resource data. These values are at least 4-aligned.
sl@0
   124
 * Some resource values are stored directly in the offset field of the Resource itself.
sl@0
   125
 * See UResType in unicode/ures.h for enumeration constants for Resource types.
sl@0
   126
 *
sl@0
   127
 * Type Name            Memory layout of values
sl@0
   128
 *                      (in parentheses: scalar, non-offset values)
sl@0
   129
 *
sl@0
   130
 * 0  Unicode String:   int32_t length, UChar[length], (UChar)0, (padding)
sl@0
   131
 *                  or  (empty string ("") if offset==0)
sl@0
   132
 * 1  Binary:           int32_t length, uint8_t[length], (padding)
sl@0
   133
 *                      - this value should be 32-aligned -
sl@0
   134
 * 2  Table:            uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count]
sl@0
   135
 * 3  Alias:            (physically same value layout as string, new in ICU 2.4)
sl@0
   136
 * 4  Table32:          int32_t count, int32_t keyStringOffsets[count], Resource[count]
sl@0
   137
 *                      (new in formatVersion 1.1/ICU 2.8)
sl@0
   138
 *
sl@0
   139
 * 7  Integer:          (28-bit offset is integer value)
sl@0
   140
 * 8  Array:            int32_t count, Resource[count]
sl@0
   141
 *
sl@0
   142
 * 14 Integer Vector:   int32_t length, int32_t[length]
sl@0
   143
 * 15 Reserved:         This value denotes special purpose resources and is for internal use.
sl@0
   144
 *
sl@0
   145
 * Note that there are 3 types with data vector values:
sl@0
   146
 * - Vectors of 8-bit bytes stored as type Binary.
sl@0
   147
 * - Vectors of 16-bit words stored as type Unicode String
sl@0
   148
 *                     (no value restrictions, all values 0..ffff allowed!).
sl@0
   149
 * - Vectors of 32-bit words stored as type Integer Vector.
sl@0
   150
 */
sl@0
   151
sl@0
   152
/*
sl@0
   153
 * Structure for a single, memory-mapped ResourceBundle.
sl@0
   154
 */
sl@0
   155
typedef struct {
sl@0
   156
    UDataMemory *data;
sl@0
   157
    Resource *pRoot;
sl@0
   158
    Resource rootRes;
sl@0
   159
} ResourceData;
sl@0
   160
sl@0
   161
/*
sl@0
   162
 * Load a resource bundle file.
sl@0
   163
 * The ResourceData structure must be allocated externally.
sl@0
   164
 */
sl@0
   165
U_CFUNC UBool
sl@0
   166
res_load(ResourceData *pResData,
sl@0
   167
         const char *path, const char *name, UErrorCode *errorCode);
sl@0
   168
sl@0
   169
/*
sl@0
   170
 * Release a resource bundle file.
sl@0
   171
 * This does not release the ResourceData structure itself.
sl@0
   172
 */
sl@0
   173
U_CFUNC void
sl@0
   174
res_unload(ResourceData *pResData);
sl@0
   175
sl@0
   176
/*
sl@0
   177
 * Return a pointer to a zero-terminated, const UChar* string
sl@0
   178
 * and set its length in *pLength.
sl@0
   179
 * Returns NULL if not found.
sl@0
   180
 */
sl@0
   181
U_CFUNC const UChar *
sl@0
   182
res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength);
sl@0
   183
sl@0
   184
U_CFUNC const UChar *
sl@0
   185
res_getAlias(const ResourceData *pResData, const Resource res, int32_t *pLength);
sl@0
   186
sl@0
   187
U_CFUNC const uint8_t *
sl@0
   188
res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength);
sl@0
   189
sl@0
   190
U_CFUNC const int32_t *
sl@0
   191
res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength);
sl@0
   192
sl@0
   193
U_CFUNC Resource
sl@0
   194
res_getResource(const ResourceData *pResData, const char *key);
sl@0
   195
sl@0
   196
U_CFUNC int32_t
sl@0
   197
res_countArrayItems(const ResourceData *pResData, const Resource res);
sl@0
   198
sl@0
   199
U_CFUNC Resource res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexS);
sl@0
   200
U_CFUNC Resource res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key);
sl@0
   201
U_CFUNC Resource res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key);
sl@0
   202
sl@0
   203
/*
sl@0
   204
 * Modifies the contents of *path (replacing separators with NULs),
sl@0
   205
 * and also moves *path forward while it finds items.
sl@0
   206
 */
sl@0
   207
U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key);
sl@0
   208
sl@0
   209
/**
sl@0
   210
 * Swap an ICU resource bundle. See udataswp.h.
sl@0
   211
 * @internal
sl@0
   212
 */
sl@0
   213
U_CAPI int32_t U_EXPORT2
sl@0
   214
ures_swap(const UDataSwapper *ds,
sl@0
   215
          const void *inData, int32_t length, void *outData,
sl@0
   216
          UErrorCode *pErrorCode);
sl@0
   217
sl@0
   218
#endif