1999-12-09 18:35:11 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
* *
|
2003-04-16 23:52:32 +00:00
|
|
|
* Copyright (C) 1999-2003, International Business Machines Corporation *
|
2001-03-21 20:44:20 +00:00
|
|
|
* and others. All Rights Reserved. *
|
1999-12-09 18:35:11 +00:00
|
|
|
* *
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: uresdata.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 1999dec08
|
|
|
|
* created by: Markus W. Scherer
|
2000-06-20 23:42:20 +00:00
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 06/20/2000 helena OS/400 port changes; mostly typecast.
|
2002-06-24 20:58:17 +00:00
|
|
|
* 06/24/02 weiv Added support for resource sharing
|
1999-12-09 18:35:11 +00:00
|
|
|
*/
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/udata.h"
|
2003-08-08 23:39:34 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "uarrsort.h"
|
|
|
|
#include "udataswp.h"
|
2003-09-12 02:46:15 +00:00
|
|
|
#include "ucol_swp.h"
|
1999-12-09 18:35:11 +00:00
|
|
|
#include "uresdata.h"
|
2000-05-15 18:39:17 +00:00
|
|
|
#include "uresimp.h"
|
1999-12-09 18:35:11 +00:00
|
|
|
|
2003-09-12 02:46:15 +00:00
|
|
|
#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
|
|
|
|
|
1999-12-09 18:35:11 +00:00
|
|
|
/*
|
|
|
|
* Resource access helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
|
|
|
|
#define RES_GET_KEY(pRoot, keyOffset) ((const char *)(pRoot)+(keyOffset))
|
2000-05-26 02:02:42 +00:00
|
|
|
#define URESDATA_ITEM_NOT_FOUND 0xFFFF
|
1999-12-09 18:35:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All the type-access functions assume that
|
|
|
|
* the resource is of the expected type.
|
|
|
|
*/
|
|
|
|
|
2000-05-18 21:25:51 +00:00
|
|
|
|
1999-12-09 18:35:11 +00:00
|
|
|
/*
|
|
|
|
* Array functions
|
|
|
|
*/
|
|
|
|
static Resource
|
2000-05-15 18:39:17 +00:00
|
|
|
_res_getArrayItem(Resource *pRoot, Resource res, int32_t indexR) {
|
1999-12-09 18:35:11 +00:00
|
|
|
int32_t *p=(int32_t *)RES_GET_POINTER(pRoot, res);
|
2000-05-15 18:39:17 +00:00
|
|
|
if(indexR<*p) {
|
|
|
|
return ((Resource *)(p))[1+indexR];
|
1999-12-09 18:35:11 +00:00
|
|
|
} else {
|
2000-05-15 18:39:17 +00:00
|
|
|
return RES_BOGUS; /* indexR>itemCount */
|
1999-12-09 18:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Table functions
|
|
|
|
*
|
|
|
|
* Important: the key offsets are 16-bit byte offsets from pRoot,
|
|
|
|
* and the itemCount is one more 16-bit, too.
|
|
|
|
* Thus, there are (count+1) uint16_t values.
|
|
|
|
* In order to 4-align the Resource item values, there is a padding
|
|
|
|
* word if count is even, i.e., there is exactly (~count&1)
|
|
|
|
* 16-bit padding words.
|
|
|
|
*/
|
|
|
|
static const char *
|
2000-05-15 18:39:17 +00:00
|
|
|
_res_getTableKey(const Resource *pRoot, const Resource res, uint16_t indexS) {
|
1999-12-09 18:35:11 +00:00
|
|
|
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
|
2000-05-15 18:39:17 +00:00
|
|
|
if(indexS<*p) {
|
|
|
|
return RES_GET_KEY(pRoot, p[indexS+1]);
|
1999-12-09 18:35:11 +00:00
|
|
|
} else {
|
2000-05-15 18:39:17 +00:00
|
|
|
return NULL; /* indexS>itemCount */
|
1999-12-09 18:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Resource
|
2000-05-15 18:39:17 +00:00
|
|
|
_res_getTableItem(const Resource *pRoot, const Resource res, uint16_t indexR) {
|
1999-12-09 18:35:11 +00:00
|
|
|
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
|
|
|
|
uint16_t count=*p;
|
2000-05-15 18:39:17 +00:00
|
|
|
if(indexR<count) {
|
|
|
|
return ((Resource *)(p+1+count+(~count&1)))[indexR];
|
1999-12-09 18:35:11 +00:00
|
|
|
} else {
|
2000-05-15 18:39:17 +00:00
|
|
|
return RES_BOGUS; /* indexR>itemCount */
|
1999-12-09 18:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Resource
|
2000-05-15 18:39:17 +00:00
|
|
|
_res_findTableItem(const Resource *pRoot, const Resource res, const char *key) {
|
1999-12-09 18:35:11 +00:00
|
|
|
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
|
|
|
|
uint16_t i, start, limit;
|
|
|
|
|
|
|
|
limit=*p++; /* number of entries */
|
|
|
|
|
2001-04-17 19:11:56 +00:00
|
|
|
if(limit == 0) { /* this table is empty */
|
|
|
|
return RES_BOGUS;
|
|
|
|
}
|
|
|
|
|
1999-12-09 18:35:11 +00:00
|
|
|
/* do a binary search for the key */
|
|
|
|
start=0;
|
|
|
|
while(start<limit-1) {
|
2000-08-10 01:59:18 +00:00
|
|
|
i=(uint16_t)((start+limit)/2);
|
1999-12-28 23:39:02 +00:00
|
|
|
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
|
1999-12-09 18:35:11 +00:00
|
|
|
limit=i;
|
|
|
|
} else {
|
|
|
|
start=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did we really find it? */
|
1999-12-28 23:39:02 +00:00
|
|
|
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
|
1999-12-09 18:35:11 +00:00
|
|
|
limit=*(p-1); /* itemCount */
|
|
|
|
return ((Resource *)(p+limit+(~limit&1)))[start];
|
|
|
|
} else {
|
|
|
|
return RES_BOGUS; /* not found */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-24 20:58:17 +00:00
|
|
|
static Resource
|
2002-07-30 20:49:30 +00:00
|
|
|
_res_findTableItemN(const Resource *pRoot, const Resource res, const char *key, int32_t keyLen, const char **realKey) {
|
2002-06-24 20:58:17 +00:00
|
|
|
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
|
|
|
|
uint16_t i, start, limit;
|
|
|
|
|
|
|
|
limit=*p++; /* number of entries */
|
|
|
|
|
|
|
|
if(limit == 0) { /* this table is empty */
|
|
|
|
return RES_BOGUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do a binary search for the key */
|
|
|
|
start=0;
|
|
|
|
while(start<limit-1) {
|
|
|
|
i=(uint16_t)((start+limit)/2);
|
|
|
|
if(uprv_strncmp(key, RES_GET_KEY(pRoot, p[i]), keyLen)<0) {
|
|
|
|
limit=i;
|
|
|
|
} else {
|
|
|
|
start=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did we really find it? */
|
|
|
|
if(uprv_strncmp(key, RES_GET_KEY(pRoot, p[start]), keyLen)==0) {
|
2002-07-30 20:49:30 +00:00
|
|
|
*realKey = RES_GET_KEY(pRoot, p[start]);
|
2002-06-24 20:58:17 +00:00
|
|
|
limit=*(p-1); /* itemCount */
|
|
|
|
return ((Resource *)(p+limit+(~limit&1)))[start];
|
|
|
|
} else {
|
2002-07-30 20:49:30 +00:00
|
|
|
*realKey = NULL;
|
2002-06-24 20:58:17 +00:00
|
|
|
return RES_BOGUS; /* not found */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
static uint16_t
|
|
|
|
_res_findTableIndex(const Resource *pRoot, const Resource res, const char *key) {
|
|
|
|
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
|
|
|
|
uint16_t i, start, limit;
|
|
|
|
|
|
|
|
limit=*p++; /* number of entries */
|
|
|
|
|
2001-08-17 22:14:17 +00:00
|
|
|
if(limit == 0) { /* this table is empty */
|
|
|
|
return URESDATA_ITEM_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
/* do a binary search for the key */
|
|
|
|
start=0;
|
|
|
|
while(start<limit-1) {
|
2000-08-10 01:59:18 +00:00
|
|
|
i=(uint16_t)((start+limit)/2);
|
2000-05-15 18:39:17 +00:00
|
|
|
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
|
|
|
|
limit=i;
|
|
|
|
} else {
|
|
|
|
start=i;
|
|
|
|
}
|
|
|
|
}
|
2001-08-17 22:14:17 +00:00
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
/* did we really find it? */
|
|
|
|
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
|
|
|
|
limit=*(p-1); /* itemCount */
|
|
|
|
return start;
|
|
|
|
} else {
|
2000-05-26 02:02:42 +00:00
|
|
|
return URESDATA_ITEM_NOT_FOUND; /* not found */
|
2000-05-15 18:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-09 18:35:11 +00:00
|
|
|
/* helper for res_load() ---------------------------------------------------- */
|
|
|
|
|
2002-07-23 23:01:08 +00:00
|
|
|
static UBool U_CALLCONV
|
1999-12-09 18:35:11 +00:00
|
|
|
isAcceptable(void *context,
|
|
|
|
const char *type, const char *name,
|
2000-05-03 23:15:02 +00:00
|
|
|
const UDataInfo *pInfo) {
|
2000-08-10 01:59:18 +00:00
|
|
|
return (UBool)(
|
1999-12-09 18:35:11 +00:00
|
|
|
pInfo->size>=20 &&
|
|
|
|
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
|
|
|
|
pInfo->charsetFamily==U_CHARSET_FAMILY &&
|
|
|
|
pInfo->sizeofUChar==U_SIZEOF_UCHAR &&
|
1999-12-10 01:45:15 +00:00
|
|
|
pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */
|
|
|
|
pInfo->dataFormat[1]==0x65 &&
|
|
|
|
pInfo->dataFormat[2]==0x73 &&
|
|
|
|
pInfo->dataFormat[3]==0x42 &&
|
2000-08-10 01:59:18 +00:00
|
|
|
pInfo->formatVersion[0]==1);
|
1999-12-09 18:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* semi-public functions ---------------------------------------------------- */
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
U_CFUNC UBool
|
1999-12-09 18:35:11 +00:00
|
|
|
res_load(ResourceData *pResData,
|
|
|
|
const char *path, const char *name, UErrorCode *errorCode) {
|
|
|
|
/* load the ResourceBundle file */
|
|
|
|
pResData->data=udata_openChoice(path, "res", name, isAcceptable, NULL, errorCode);
|
|
|
|
if(U_FAILURE(*errorCode)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get its memory and root resource */
|
|
|
|
pResData->pRoot=(Resource *)udata_getMemory(pResData->data);
|
|
|
|
pResData->rootRes=*pResData->pRoot;
|
|
|
|
|
|
|
|
/* currently, we accept only resources that have a Table as their roots */
|
2003-04-16 23:52:32 +00:00
|
|
|
if(RES_GET_TYPE(pResData->rootRes)!=URES_TABLE) {
|
1999-12-09 18:35:11 +00:00
|
|
|
udata_close(pResData->data);
|
2000-05-15 18:39:17 +00:00
|
|
|
pResData->data=NULL;
|
1999-12-09 18:35:11 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC void
|
|
|
|
res_unload(ResourceData *pResData) {
|
|
|
|
if(pResData->data!=NULL) {
|
|
|
|
udata_close(pResData->data);
|
|
|
|
pResData->data=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC const UChar *
|
2000-05-15 18:39:17 +00:00
|
|
|
res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength) {
|
2003-04-16 23:52:32 +00:00
|
|
|
if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_STRING) {
|
2001-07-27 17:15:30 +00:00
|
|
|
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
|
2002-01-30 06:19:28 +00:00
|
|
|
if (pLength) {
|
|
|
|
*pLength=*p;
|
|
|
|
}
|
|
|
|
return (UChar *)++p;
|
1999-12-09 18:35:11 +00:00
|
|
|
} else {
|
2002-01-30 06:19:28 +00:00
|
|
|
if (pLength) {
|
|
|
|
*pLength=0;
|
|
|
|
}
|
1999-12-09 18:35:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-14 06:03:08 +00:00
|
|
|
U_CFUNC const UChar *
|
|
|
|
res_getAlias(const ResourceData *pResData, const Resource res, int32_t *pLength) {
|
2003-04-16 23:52:32 +00:00
|
|
|
if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_ALIAS) {
|
2002-06-14 06:03:08 +00:00
|
|
|
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
|
|
|
|
if (pLength) {
|
|
|
|
*pLength=*p;
|
|
|
|
}
|
|
|
|
return (UChar *)++p;
|
|
|
|
} else {
|
|
|
|
if (pLength) {
|
|
|
|
*pLength=0;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-18 21:25:51 +00:00
|
|
|
U_CFUNC const uint8_t *
|
|
|
|
res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength) {
|
2001-08-27 18:51:43 +00:00
|
|
|
if(res!=RES_BOGUS) {
|
|
|
|
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
|
|
|
|
*pLength=*p++;
|
|
|
|
if (*pLength == 0) {
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
return (uint8_t *)p;
|
|
|
|
} else {
|
|
|
|
*pLength=0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-05-18 21:25:51 +00:00
|
|
|
}
|
|
|
|
|
2001-08-01 17:08:07 +00:00
|
|
|
|
|
|
|
U_CFUNC const int32_t *
|
|
|
|
res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength) {
|
2003-04-16 23:52:32 +00:00
|
|
|
if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_INT_VECTOR) {
|
2001-08-27 18:51:43 +00:00
|
|
|
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
|
|
|
|
*pLength=*p++;
|
|
|
|
if (*pLength == 0) {
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
return (const int32_t *)p;
|
|
|
|
} else {
|
|
|
|
*pLength=0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-01 17:08:07 +00:00
|
|
|
}
|
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
U_CFUNC int32_t
|
|
|
|
res_countArrayItems(const ResourceData *pResData, const Resource res) {
|
|
|
|
if(res!=RES_BOGUS) {
|
2003-04-16 23:52:32 +00:00
|
|
|
if(RES_GET_TYPE(res)==URES_STRING) {
|
2000-05-15 18:39:17 +00:00
|
|
|
return 1;
|
2003-04-16 23:52:32 +00:00
|
|
|
} else if(RES_GET_TYPE(res)==URES_ARRAY) {
|
2000-05-15 18:39:17 +00:00
|
|
|
Resource *p=RES_GET_POINTER(pResData->pRoot, res);
|
|
|
|
int32_t count=*(int32_t *)p;
|
|
|
|
return count;
|
2003-04-16 23:52:32 +00:00
|
|
|
} else if(RES_GET_TYPE(res)==URES_TABLE) {
|
2000-05-15 18:39:17 +00:00
|
|
|
return res_getTableSize(pResData, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC Resource
|
|
|
|
res_getResource(const ResourceData *pResData, const char *key) {
|
|
|
|
return _res_findTableItem(pResData->pRoot, pResData->rootRes, key);
|
|
|
|
}
|
|
|
|
|
2001-08-17 22:14:17 +00:00
|
|
|
U_CFUNC Resource
|
2002-06-24 20:58:17 +00:00
|
|
|
res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexR) {
|
2000-05-15 18:39:17 +00:00
|
|
|
return _res_getArrayItem(pResData->pRoot, array, indexR);
|
|
|
|
}
|
|
|
|
|
2002-06-24 20:58:17 +00:00
|
|
|
U_CFUNC Resource
|
2002-07-30 20:49:30 +00:00
|
|
|
res_findResource(const ResourceData *pResData, Resource r, const char** path, const char** key) {
|
2002-06-24 20:58:17 +00:00
|
|
|
/* we pass in a path. CollationElements/Sequence or zoneStrings/3/2 etc.
|
|
|
|
* iterates over a path and stops when a scalar resource is found. This
|
|
|
|
* CAN be an alias. Path gets set to the part that has not yet been processed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *pathP = *path, *nextSepP = *path;
|
|
|
|
char *closeIndex = NULL;
|
|
|
|
Resource t1 = r;
|
|
|
|
Resource t2;
|
|
|
|
int32_t indexR = 0, keyLen = 0;
|
|
|
|
UResType type = RES_GET_TYPE(t1);
|
|
|
|
|
2003-04-16 23:52:32 +00:00
|
|
|
while(nextSepP && *pathP && t1 != RES_BOGUS && (type == URES_TABLE || type == URES_ARRAY)) {
|
2002-06-24 20:58:17 +00:00
|
|
|
/* Iteration stops if: the path has been consumed, we found a non-existing
|
|
|
|
* resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
|
|
|
|
*/
|
|
|
|
nextSepP = uprv_strchr(pathP, RES_PATH_SEPARATOR);
|
|
|
|
/* if there are more separators, terminate string
|
|
|
|
* and set path to the remaining part of the string
|
|
|
|
*/
|
|
|
|
if(nextSepP != NULL) {
|
|
|
|
keyLen = nextSepP-pathP;
|
|
|
|
*path = nextSepP+1;
|
|
|
|
} else {
|
|
|
|
keyLen = uprv_strlen(pathP);
|
|
|
|
*path += keyLen;
|
|
|
|
}
|
|
|
|
|
2002-07-18 17:03:57 +00:00
|
|
|
/* if the resource is a table */
|
2002-06-24 20:58:17 +00:00
|
|
|
/* try the key based access */
|
2003-04-16 23:52:32 +00:00
|
|
|
if(type == URES_TABLE) {
|
2002-07-30 20:49:30 +00:00
|
|
|
t2 = _res_findTableItemN(pResData->pRoot, t1, pathP, keyLen, key);
|
2002-07-18 17:03:57 +00:00
|
|
|
if(t2 == RES_BOGUS) {
|
|
|
|
/* if we fail to get the resource by key, maybe we got an index */
|
|
|
|
indexR = uprv_strtol(pathP, &closeIndex, 10);
|
|
|
|
if(closeIndex != pathP) {
|
2002-06-24 20:58:17 +00:00
|
|
|
/* if we indeed have an index, try to get the item by index */
|
2002-07-30 20:49:30 +00:00
|
|
|
t2 = res_getTableItemByIndex(pResData, t1, indexR, key);
|
2002-07-18 17:03:57 +00:00
|
|
|
}
|
2002-06-24 20:58:17 +00:00
|
|
|
}
|
2003-04-16 23:52:32 +00:00
|
|
|
} else if(type == URES_ARRAY) {
|
2003-05-12 18:24:21 +00:00
|
|
|
indexR = uprv_strtol(pathP, &closeIndex, 10);
|
|
|
|
if(closeIndex != pathP) {
|
|
|
|
t2 = _res_getArrayItem(pResData->pRoot, t1, indexR);
|
|
|
|
} else {
|
|
|
|
t2 = RES_BOGUS; /* have an array, but don't have a valid index */
|
|
|
|
}
|
2002-07-30 20:49:30 +00:00
|
|
|
*key = NULL;
|
2002-07-18 17:03:57 +00:00
|
|
|
} else { /* can't do much here, except setting t2 to bogus */
|
|
|
|
t2 = RES_BOGUS;
|
2002-06-24 20:58:17 +00:00
|
|
|
}
|
|
|
|
t1 = t2;
|
|
|
|
type = RES_GET_TYPE(t1);
|
|
|
|
/* position pathP to next resource key/index */
|
|
|
|
pathP += keyLen+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t1;
|
|
|
|
}
|
|
|
|
|
2001-08-17 22:14:17 +00:00
|
|
|
U_CFUNC Resource
|
2002-06-24 20:58:17 +00:00
|
|
|
res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t* indexR, const char* * key) {
|
2000-08-11 21:25:19 +00:00
|
|
|
uint16_t tempIndex;
|
2000-05-15 18:39:17 +00:00
|
|
|
if(key != NULL) {
|
2000-05-25 22:16:48 +00:00
|
|
|
tempIndex = _res_findTableIndex(pResData->pRoot, table, *key);
|
2000-08-11 21:25:19 +00:00
|
|
|
if(tempIndex != URESDATA_ITEM_NOT_FOUND) {
|
|
|
|
*key = _res_getTableKey(pResData->pRoot, table, tempIndex);
|
|
|
|
*indexR = tempIndex;
|
|
|
|
return _res_getTableItem(pResData->pRoot, table, tempIndex);
|
|
|
|
} else {
|
|
|
|
return RES_BOGUS;
|
|
|
|
}
|
2000-05-15 18:39:17 +00:00
|
|
|
} else {
|
|
|
|
return RES_BOGUS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-17 22:14:17 +00:00
|
|
|
U_CFUNC Resource
|
2002-06-24 20:58:17 +00:00
|
|
|
res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexR, const char * * key) {
|
2000-05-15 18:39:17 +00:00
|
|
|
if(indexR>-1) {
|
|
|
|
if(key != NULL) {
|
|
|
|
*key = _res_getTableKey(pResData->pRoot, table, (uint16_t)indexR);
|
|
|
|
}
|
|
|
|
return _res_getTableItem(pResData->pRoot, table, (uint16_t)indexR);
|
|
|
|
} else {
|
|
|
|
return RES_BOGUS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-17 22:14:17 +00:00
|
|
|
U_CFUNC int32_t
|
|
|
|
res_getTableSize(const ResourceData *pResData, Resource table) {
|
2000-05-15 18:39:17 +00:00
|
|
|
uint16_t *p=(uint16_t *)RES_GET_POINTER(pResData->pRoot, table);
|
|
|
|
return *p;
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
|
|
|
|
/* resource bundle swapping ------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to always enumerate the entire item tree,
|
|
|
|
* track the lowest address of any item to use as the limit for char keys[],
|
|
|
|
* track the highest address of any item to return the size of the data.
|
|
|
|
*
|
|
|
|
* We should have thought of storing those in the data...
|
|
|
|
* It is possible to extend the data structure by putting additional values
|
|
|
|
* in places that are inaccessible by ordinary enumeration of the item tree.
|
|
|
|
* For example, additional integers could be stored at the beginning or
|
|
|
|
* end of the key strings; this could be indicated by a minor version number,
|
|
|
|
* and the data swapping would have to know about these values.
|
|
|
|
*
|
|
|
|
* The data structure does not forbid keys to be shared, so we must swap
|
|
|
|
* all keys once instead of each key when it is referenced.
|
|
|
|
*
|
|
|
|
* These swapping functions assume that a resource bundle always has a length
|
|
|
|
* that is a multiple of 4 bytes.
|
|
|
|
* Currently, this is trivially true because genrb writes bundle tree leaves
|
|
|
|
* physically first, before their branches, so that the root table with its
|
|
|
|
* array of resource items (uint32_t values) is always last.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* definitions for table sorting ------------------------ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* row of a temporary array
|
|
|
|
*
|
|
|
|
* gets platform-endian key string indexes and sorting indexes;
|
|
|
|
* after sorting this array by keys, the actual key/value arrays are permutated
|
|
|
|
* according to the sorting indexes
|
|
|
|
*
|
|
|
|
* TODO if and when we add another table type with 32-bit key string indexes,
|
|
|
|
* widen both values here to int32_t's
|
|
|
|
*/
|
|
|
|
typedef struct Row {
|
|
|
|
uint16_t keyIndex, sortIndex;
|
|
|
|
} Row;
|
|
|
|
|
|
|
|
static int32_t
|
|
|
|
ures_compareRows(const void *context, const void *left, const void *right) {
|
|
|
|
const char *keyChars=(const char *)context;
|
|
|
|
return (int32_t)uprv_strcmp(keyChars+((const Row *)left)->keyIndex,
|
|
|
|
keyChars+((const Row *)right)->keyIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct TempTable {
|
|
|
|
const char *keyChars;
|
|
|
|
Row *rows;
|
|
|
|
int32_t *resort;
|
|
|
|
} TempTable;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
STACK_ROW_CAPACITY=200
|
|
|
|
};
|
|
|
|
|
2003-09-12 02:46:15 +00:00
|
|
|
/* binary data with known formats is swapped too */
|
|
|
|
typedef enum UResSpecialType {
|
|
|
|
URES_NO_SPECIAL_TYPE,
|
|
|
|
URES_COLLATION_BINARY,
|
|
|
|
URES_SPECIAL_TYPE_COUNT
|
|
|
|
} UResSpecialType;
|
|
|
|
|
|
|
|
/* resource table key for collation binaries: "%%CollationBin" */
|
|
|
|
static const UChar gCollationBinKey[]={
|
|
|
|
0x25, 0x25,
|
|
|
|
0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
|
|
|
|
0x42, 0x69, 0x6e,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2003-08-08 23:39:34 +00:00
|
|
|
/*
|
|
|
|
* preflight one resource item and set bottom and top values;
|
|
|
|
* length, bottom, and top count Resource item offsets (4 bytes each), not bytes
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ures_preflightResource(const UDataSwapper *ds,
|
|
|
|
const Resource *inBundle, int32_t length,
|
|
|
|
Resource res,
|
|
|
|
int32_t *pBottom, int32_t *pTop, int32_t *pMaxTableLength,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const Resource *p;
|
|
|
|
int32_t offset;
|
|
|
|
|
|
|
|
if(res==0 || RES_GET_TYPE(res)==URES_INT) {
|
|
|
|
/* empty string or integer, nothing to do */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all other types use an offset to point to their data */
|
|
|
|
offset=(int32_t)RES_GET_OFFSET(res);
|
|
|
|
if(0<=length && length<=offset) {
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_preflightResource(res=%08x) resource offset exceeds bundle length %d\n",
|
|
|
|
res, length);
|
2003-08-08 23:39:34 +00:00
|
|
|
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return;
|
|
|
|
} else if(offset<*pBottom) {
|
|
|
|
*pBottom=offset;
|
|
|
|
}
|
|
|
|
p=inBundle+offset;
|
|
|
|
|
|
|
|
switch(RES_GET_TYPE(res)) {
|
|
|
|
case URES_ALIAS:
|
|
|
|
/* physically same value layout as string, fall through */
|
|
|
|
case URES_STRING:
|
|
|
|
/* top=offset+1+(string length +1)/2 rounded up */
|
|
|
|
offset+=1+((udata_readInt32(ds, (int32_t)*p)+1)+1)/2;
|
|
|
|
break;
|
|
|
|
case URES_BINARY:
|
|
|
|
/* top=offset+1+(binary length)/4 rounded up */
|
|
|
|
offset+=1+(udata_readInt32(ds, (int32_t)*p)+3)/4;
|
|
|
|
break;
|
|
|
|
case URES_TABLE:
|
|
|
|
{
|
|
|
|
const uint16_t *pKey;
|
|
|
|
Resource item;
|
2003-08-09 00:27:31 +00:00
|
|
|
int32_t i, count;
|
2003-08-08 23:39:34 +00:00
|
|
|
|
|
|
|
/* get table item count */
|
|
|
|
pKey=(const uint16_t *)p;
|
|
|
|
count=ds->readUInt16(*pKey++);
|
|
|
|
if(count>*pMaxTableLength) {
|
|
|
|
*pMaxTableLength=count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* top=((1+ table item count)/2 rounded up)+(table item count) */
|
|
|
|
offset+=((1+count)+1)/2;
|
|
|
|
p=inBundle+offset; /* pointer to table resources */
|
|
|
|
offset+=count;
|
|
|
|
|
|
|
|
/* recurse */
|
2003-08-09 00:27:31 +00:00
|
|
|
if(offset<=length) {
|
|
|
|
for(i=0; i<count; ++i) {
|
2003-08-08 23:39:34 +00:00
|
|
|
item=ds->readUInt32(*p++);
|
|
|
|
ures_preflightResource(ds, inBundle, length, item,
|
|
|
|
pBottom, pTop, pMaxTableLength,
|
|
|
|
pErrorCode);
|
2003-08-09 00:27:31 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
udata_printError(ds, "ures_preflightResource(table res=%08x)[%d].recurse(%08x) failed - %s\n",
|
|
|
|
res, i, item, u_errorName(*pErrorCode));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case URES_ARRAY:
|
|
|
|
{
|
|
|
|
Resource item;
|
2003-08-09 00:27:31 +00:00
|
|
|
int32_t i, count;
|
2003-08-08 23:39:34 +00:00
|
|
|
|
|
|
|
/* top=offset+1+(array length) */
|
|
|
|
count=udata_readInt32(ds, (int32_t)*p++);
|
|
|
|
offset+=1+count;
|
|
|
|
|
|
|
|
/* recurse */
|
2003-08-09 00:27:31 +00:00
|
|
|
if(offset<=length) {
|
|
|
|
for(i=0; i<count; ++i) {
|
2003-08-08 23:39:34 +00:00
|
|
|
item=ds->readUInt32(*p++);
|
|
|
|
ures_preflightResource(ds, inBundle, length, item,
|
|
|
|
pBottom, pTop, pMaxTableLength,
|
|
|
|
pErrorCode);
|
2003-08-09 00:27:31 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
udata_printError(ds, "ures_preflightResource(array res=%08x)[%d].recurse(%08x) failed - %s\n",
|
|
|
|
res, i, item, u_errorName(*pErrorCode));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case URES_INT_VECTOR:
|
|
|
|
/* top=offset+1+(vector length) */
|
|
|
|
offset+=1+udata_readInt32(ds, (int32_t)*p);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* also catches RES_BOGUS */
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_preflightResource(res=%08x) unknown resource type\n", res);
|
2003-08-08 23:39:34 +00:00
|
|
|
*pErrorCode=U_UNSUPPORTED_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
/* nothing to do */
|
|
|
|
} else if(0<=length && length<offset) {
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_preflightResource(res=%08x) resource limit exceeds bundle length %d\n",
|
|
|
|
res, length);
|
2003-08-08 23:39:34 +00:00
|
|
|
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
} else if(offset>*pTop) {
|
|
|
|
*pTop=offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swap one resource item
|
|
|
|
* since preflighting succeeded, we need not check offsets against length any more
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ures_swapResource(const UDataSwapper *ds,
|
|
|
|
const Resource *inBundle, Resource *outBundle,
|
|
|
|
Resource res, /* caller swaps res itself */
|
2003-09-12 02:46:15 +00:00
|
|
|
UResSpecialType specialType,
|
2003-08-08 23:39:34 +00:00
|
|
|
TempTable *pTempTable,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const Resource *p;
|
|
|
|
Resource *q;
|
|
|
|
int32_t offset, count;
|
|
|
|
|
|
|
|
if(res==0 || RES_GET_TYPE(res)==URES_INT) {
|
|
|
|
/* empty string or integer, nothing to do */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all other types use an offset to point to their data */
|
|
|
|
offset=(int32_t)RES_GET_OFFSET(res);
|
|
|
|
p=inBundle+offset;
|
|
|
|
q=outBundle+offset;
|
|
|
|
|
|
|
|
switch(RES_GET_TYPE(res)) {
|
|
|
|
case URES_ALIAS:
|
|
|
|
/* physically same value layout as string, fall through */
|
|
|
|
case URES_STRING:
|
|
|
|
count=udata_readInt32(ds, (int32_t)*p);
|
|
|
|
/* swap length */
|
|
|
|
ds->swapArray32(ds, p, 4, q, pErrorCode);
|
|
|
|
/* swap each UChar (the terminating NUL would not change) */
|
|
|
|
ds->swapArray16(ds, p+1, 2*count, q+1, pErrorCode);
|
|
|
|
break;
|
|
|
|
case URES_BINARY:
|
2003-09-12 02:46:15 +00:00
|
|
|
count=udata_readInt32(ds, (int32_t)*p);
|
2003-08-08 23:39:34 +00:00
|
|
|
/* swap length */
|
|
|
|
ds->swapArray32(ds, p, 4, q, pErrorCode);
|
|
|
|
/* no need to swap or copy bytes - ures_swap() copied them all */
|
2003-09-12 02:46:15 +00:00
|
|
|
|
|
|
|
/* swap known formats */
|
|
|
|
if(specialType==URES_COLLATION_BINARY) {
|
|
|
|
ucol_swapBinary(ds, p+1, count, q+1, pErrorCode);
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
break;
|
|
|
|
case URES_TABLE:
|
|
|
|
{
|
|
|
|
const uint16_t *pKey;
|
|
|
|
uint16_t *qKey;
|
|
|
|
Resource item;
|
|
|
|
int32_t i, oldIndex;
|
|
|
|
|
|
|
|
/* get table item count */
|
|
|
|
pKey=(const uint16_t *)p;
|
|
|
|
qKey=(uint16_t *)q;
|
|
|
|
count=ds->readUInt16(*pKey);
|
|
|
|
|
|
|
|
/* swap count */
|
|
|
|
ds->swapArray16(ds, pKey++, 2, qKey++, pErrorCode);
|
|
|
|
if(count==0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset+=((1+count)+1)/2;
|
|
|
|
p=inBundle+offset; /* pointer to table resources */
|
|
|
|
q=outBundle+offset;
|
|
|
|
|
|
|
|
/* recurse */
|
2003-08-09 00:27:31 +00:00
|
|
|
for(i=0; i<count; ++i) {
|
2003-08-08 23:39:34 +00:00
|
|
|
/*
|
2003-09-12 02:46:15 +00:00
|
|
|
* detect a collation binary that is to be swapped via
|
|
|
|
* ds->compareInvChars(ds, outData+readUInt16(pKey[i]), "%%CollationBin")
|
2003-08-08 23:39:34 +00:00
|
|
|
* etc.
|
|
|
|
*
|
|
|
|
* use some UDataSwapFn pointer from somewhere for collation swapping
|
|
|
|
* because the common library cannot directly call into the i18n library
|
|
|
|
*/
|
2003-09-12 02:46:15 +00:00
|
|
|
if(0==ds->compareInvChars(ds,
|
|
|
|
((const char *)outBundle)+ds->readUInt16(pKey[i]), -1,
|
|
|
|
gCollationBinKey, LENGTHOF(gCollationBinKey)-1)
|
|
|
|
) {
|
|
|
|
specialType=URES_COLLATION_BINARY;
|
|
|
|
} else {
|
|
|
|
specialType=URES_NO_SPECIAL_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
item=ds->readUInt32(p[i]);
|
|
|
|
ures_swapResource(ds, inBundle, outBundle, item, specialType, pTempTable, pErrorCode);
|
2003-08-09 00:27:31 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
udata_printError(ds, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed - %s\n",
|
|
|
|
res, i, item, u_errorName(*pErrorCode));
|
|
|
|
return;
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ### TODO optimize
|
|
|
|
* After some testing, add a test
|
|
|
|
* if(inCharset==outCharset) { only swap keys and items, do not sort; }
|
|
|
|
* else { sort/copy/swap/permutate as below; }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to sort tables by outCharset key strings because they
|
|
|
|
* sort differently for different charset families.
|
|
|
|
* ures_swap() already set pTempTable->keyChars appropriately.
|
|
|
|
* First we set up a temporary table with the key indexes and
|
|
|
|
* sorting indexes and sort that.
|
|
|
|
* Then we permutate and copy/swap the actual values.
|
|
|
|
*/
|
|
|
|
for(i=0; i<count; ++i) {
|
|
|
|
pTempTable->rows[i].keyIndex=ds->readUInt16(pKey[i]);
|
|
|
|
pTempTable->rows[i].sortIndex=(uint16_t)i;
|
|
|
|
}
|
|
|
|
uprv_sortArray(pTempTable->rows, count, sizeof(Row),
|
|
|
|
ures_compareRows, pTempTable->keyChars,
|
|
|
|
FALSE, pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed - %s\n",
|
|
|
|
res, count, u_errorName(*pErrorCode));
|
2003-08-08 23:39:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy/swap/permutate items */
|
|
|
|
if(pKey!=qKey) {
|
|
|
|
for(i=0; i<count; ++i) {
|
|
|
|
oldIndex=pTempTable->rows[i].sortIndex;
|
|
|
|
ds->swapArray16(ds, pKey+oldIndex, 2, qKey+i, pErrorCode);
|
|
|
|
ds->swapArray32(ds, p+oldIndex, 4, q+i, pErrorCode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If we swap in-place, then the permutation must use another
|
|
|
|
* temporary array (pTempTable->resort)
|
|
|
|
* before the results are copied to the outBundle.
|
|
|
|
*/
|
|
|
|
int32_t *r=pTempTable->resort;
|
|
|
|
uint16_t *rKey=(uint16_t *)r;
|
|
|
|
|
|
|
|
for(i=0; i<count; ++i) {
|
|
|
|
oldIndex=pTempTable->rows[i].sortIndex;
|
|
|
|
ds->swapArray16(ds, pKey+oldIndex, 2, rKey+i, pErrorCode);
|
|
|
|
}
|
|
|
|
uprv_memcpy(qKey, rKey, 2*count);
|
|
|
|
|
|
|
|
for(i=0; i<count; ++i) {
|
|
|
|
oldIndex=pTempTable->rows[i].sortIndex;
|
|
|
|
ds->swapArray32(ds, p+oldIndex, 4, r+i, pErrorCode);
|
|
|
|
}
|
|
|
|
uprv_memcpy(q, r, 4*count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case URES_ARRAY:
|
|
|
|
{
|
|
|
|
Resource item;
|
|
|
|
int32_t i;
|
|
|
|
|
|
|
|
count=udata_readInt32(ds, (int32_t)*p);
|
|
|
|
/* swap length */
|
|
|
|
ds->swapArray32(ds, p++, 4, q++, pErrorCode);
|
|
|
|
|
|
|
|
/* recurse */
|
2003-08-09 00:27:31 +00:00
|
|
|
for(i=0; i<count; ++i) {
|
2003-08-08 23:39:34 +00:00
|
|
|
item=ds->readUInt32(p[i]);
|
2003-09-12 02:46:15 +00:00
|
|
|
ures_swapResource(ds, inBundle, outBundle, item, URES_NO_SPECIAL_TYPE, pTempTable, pErrorCode);
|
2003-08-09 00:27:31 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
udata_printError(ds, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed - %s\n",
|
|
|
|
res, i, item, u_errorName(*pErrorCode));
|
|
|
|
return;
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* swap items */
|
|
|
|
ds->swapArray32(ds, p, 4*count, q, pErrorCode);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case URES_INT_VECTOR:
|
|
|
|
count=udata_readInt32(ds, (int32_t)*p);
|
|
|
|
/* swap length and each integer */
|
|
|
|
ds->swapArray32(ds, p, 4*(1+count), q, pErrorCode);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* also catches RES_BOGUS */
|
|
|
|
*pErrorCode=U_UNSUPPORTED_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
ures_swap(const UDataSwapper *ds,
|
|
|
|
const void *inData, int32_t length, void *outData,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const UDataInfo *pInfo;
|
|
|
|
const Resource *inBundle;
|
|
|
|
Resource rootRes;
|
2003-08-12 00:21:41 +00:00
|
|
|
int32_t headerSize, maxTableLength, stringsLength;
|
2003-08-08 23:39:34 +00:00
|
|
|
|
|
|
|
Row rows[STACK_ROW_CAPACITY];
|
|
|
|
int32_t resort[STACK_ROW_CAPACITY];
|
|
|
|
TempTable tempTable;
|
|
|
|
|
|
|
|
/* the following integers count Resource item offsets (4 bytes each), not bytes */
|
|
|
|
int32_t bundleLength, bottom, top;
|
|
|
|
|
|
|
|
/* udata_swapDataHeader checks the arguments */
|
|
|
|
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
|
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check data format and format version */
|
|
|
|
pInfo=(const UDataInfo *)((const char *)inData+4);
|
|
|
|
if(!(
|
|
|
|
pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */
|
|
|
|
pInfo->dataFormat[1]==0x65 &&
|
|
|
|
pInfo->dataFormat[2]==0x73 &&
|
|
|
|
pInfo->dataFormat[3]==0x42 &&
|
|
|
|
pInfo->formatVersion[0]==1
|
|
|
|
)) {
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not a resource bundle\n",
|
|
|
|
pInfo->dataFormat[0], pInfo->dataFormat[1],
|
|
|
|
pInfo->dataFormat[2], pInfo->dataFormat[3],
|
|
|
|
pInfo->formatVersion[0]);
|
2003-08-08 23:39:34 +00:00
|
|
|
*pErrorCode=U_UNSUPPORTED_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a resource bundle must contain at least one resource item */
|
|
|
|
if(length<0) {
|
|
|
|
bundleLength=-1;
|
|
|
|
} else {
|
|
|
|
bundleLength=(length-headerSize)/4;
|
|
|
|
if(bundleLength<1) {
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
|
|
|
|
length-headerSize);
|
2003-08-08 23:39:34 +00:00
|
|
|
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* preflight to get the bottom, top and maxTableLength values */
|
|
|
|
inBundle=(const Resource *)((const char *)inData+headerSize);
|
|
|
|
bottom=0x7fffffff;
|
|
|
|
top=maxTableLength=0;
|
2003-08-28 00:34:31 +00:00
|
|
|
rootRes=udata_readInt32(ds, *inBundle);
|
2003-08-08 23:39:34 +00:00
|
|
|
ures_preflightResource(ds, inBundle, bundleLength, rootRes,
|
|
|
|
&bottom, &top, &maxTableLength,
|
|
|
|
pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_preflightResource(root res=%08x) failed - %s\n",
|
|
|
|
rootRes, u_errorName(*pErrorCode));
|
2003-08-08 23:39:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(length>=0) {
|
|
|
|
Resource *outBundle=(Resource *)((char *)outData+headerSize);
|
2003-08-12 00:21:41 +00:00
|
|
|
const uint8_t *inChars;
|
2003-08-08 23:39:34 +00:00
|
|
|
|
|
|
|
/* copy the bundle for binary and inaccessible data */
|
|
|
|
if(inData!=outData) {
|
|
|
|
uprv_memcpy(outBundle, inBundle, 4*top);
|
|
|
|
}
|
|
|
|
|
2003-08-12 00:21:41 +00:00
|
|
|
/* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
|
|
|
|
inChars=(const uint8_t *)(inBundle+1);
|
|
|
|
stringsLength=4*(bottom-1);
|
|
|
|
while(stringsLength>0 && inChars[stringsLength-1]!=0) {
|
|
|
|
--stringsLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
ds->swapInvChars(ds, inChars, stringsLength, outBundle+1, pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
2003-09-12 02:46:15 +00:00
|
|
|
udata_printError(ds, "ures_swap().swapInvChars(keys[%d]) failed - %s\n", stringsLength,
|
2003-08-12 00:21:41 +00:00
|
|
|
u_errorName(*pErrorCode));
|
|
|
|
return 0;
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
|
|
|
|
/* allocate the temporary table for sorting resource tables */
|
|
|
|
tempTable.keyChars=(const char *)outBundle; /* sort by outCharset */
|
|
|
|
if(maxTableLength<=STACK_ROW_CAPACITY) {
|
|
|
|
tempTable.rows=rows;
|
|
|
|
tempTable.resort=resort;
|
|
|
|
} else {
|
|
|
|
tempTable.rows=(Row *)uprv_malloc(maxTableLength*sizeof(Row)+maxTableLength*4);
|
|
|
|
if(tempTable.rows==NULL) {
|
2003-08-09 00:27:31 +00:00
|
|
|
udata_printError(ds, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
|
|
|
|
maxTableLength);
|
2003-08-08 23:39:34 +00:00
|
|
|
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tempTable.resort=(int32_t *)(tempTable.rows+maxTableLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* swap the resources */
|
2003-09-12 02:46:15 +00:00
|
|
|
ures_swapResource(ds, inBundle, outBundle, rootRes, URES_NO_SPECIAL_TYPE, &tempTable, pErrorCode);
|
2003-08-09 00:27:31 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
udata_printError(ds, "ures_swapResource(root res=%08x) failed - %s\n",
|
|
|
|
rootRes, u_errorName(*pErrorCode));
|
|
|
|
}
|
2003-08-08 23:39:34 +00:00
|
|
|
|
|
|
|
if(tempTable.rows!=rows) {
|
|
|
|
uprv_free(tempTable.rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* swap the root resource */
|
|
|
|
ds->swapArray32(ds, inBundle, 4, outBundle, pErrorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return headerSize+4*top;
|
|
|
|
}
|