1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/uresdata.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1147 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* * 1.7 +* Copyright (C) 1999-2012, International Business Machines Corporation * 1.8 +* and others. All Rights Reserved. * 1.9 +* * 1.10 +******************************************************************************* 1.11 +* file name: uresdata.c 1.12 +* encoding: US-ASCII 1.13 +* tab size: 8 (not used) 1.14 +* indentation:4 1.15 +* 1.16 +* created on: 1999dec08 1.17 +* created by: Markus W. Scherer 1.18 +* Modification History: 1.19 +* 1.20 +* Date Name Description 1.21 +* 06/20/2000 helena OS/400 port changes; mostly typecast. 1.22 +* 06/24/02 weiv Added support for resource sharing 1.23 +*/ 1.24 + 1.25 +#include "unicode/utypes.h" 1.26 +#include "unicode/udata.h" 1.27 +#include "unicode/ustring.h" 1.28 +#include "unicode/utf16.h" 1.29 +#include "cmemory.h" 1.30 +#include "cstring.h" 1.31 +#include "uarrsort.h" 1.32 +#include "udataswp.h" 1.33 +#include "ucol_swp.h" 1.34 +#include "uinvchar.h" 1.35 +#include "uresdata.h" 1.36 +#include "uresimp.h" 1.37 +#include "uassert.h" 1.38 + 1.39 +#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) 1.40 + 1.41 +/* 1.42 + * Resource access helpers 1.43 + */ 1.44 + 1.45 +/* get a const char* pointer to the key with the keyOffset byte offset from pRoot */ 1.46 +#define RES_GET_KEY16(pResData, keyOffset) \ 1.47 + ((keyOffset)<(pResData)->localKeyLimit ? \ 1.48 + (const char *)(pResData)->pRoot+(keyOffset) : \ 1.49 + (pResData)->poolBundleKeys+(keyOffset)-(pResData)->localKeyLimit) 1.50 + 1.51 +#define RES_GET_KEY32(pResData, keyOffset) \ 1.52 + ((keyOffset)>=0 ? \ 1.53 + (const char *)(pResData)->pRoot+(keyOffset) : \ 1.54 + (pResData)->poolBundleKeys+((keyOffset)&0x7fffffff)) 1.55 + 1.56 +#define URESDATA_ITEM_NOT_FOUND -1 1.57 + 1.58 +/* empty resources, returned when the resource offset is 0 */ 1.59 +static const uint16_t gEmpty16=0; 1.60 + 1.61 +static const struct { 1.62 + int32_t length; 1.63 + int32_t res; 1.64 +} gEmpty32={ 0, 0 }; 1.65 + 1.66 +static const struct { 1.67 + int32_t length; 1.68 + UChar nul; 1.69 + UChar pad; 1.70 +} gEmptyString={ 0, 0, 0 }; 1.71 + 1.72 +/* 1.73 + * All the type-access functions assume that 1.74 + * the resource is of the expected type. 1.75 + */ 1.76 + 1.77 +static int32_t 1.78 +_res_findTableItem(const ResourceData *pResData, const uint16_t *keyOffsets, int32_t length, 1.79 + const char *key, const char **realKey) { 1.80 + const char *tableKey; 1.81 + int32_t mid, start, limit; 1.82 + int result; 1.83 + 1.84 + /* do a binary search for the key */ 1.85 + start=0; 1.86 + limit=length; 1.87 + while(start<limit) { 1.88 + mid = (start + limit) / 2; 1.89 + tableKey = RES_GET_KEY16(pResData, keyOffsets[mid]); 1.90 + if (pResData->useNativeStrcmp) { 1.91 + result = uprv_strcmp(key, tableKey); 1.92 + } else { 1.93 + result = uprv_compareInvCharsAsAscii(key, tableKey); 1.94 + } 1.95 + if (result < 0) { 1.96 + limit = mid; 1.97 + } else if (result > 0) { 1.98 + start = mid + 1; 1.99 + } else { 1.100 + /* We found it! */ 1.101 + *realKey=tableKey; 1.102 + return mid; 1.103 + } 1.104 + } 1.105 + return URESDATA_ITEM_NOT_FOUND; /* not found or table is empty. */ 1.106 +} 1.107 + 1.108 +static int32_t 1.109 +_res_findTable32Item(const ResourceData *pResData, const int32_t *keyOffsets, int32_t length, 1.110 + const char *key, const char **realKey) { 1.111 + const char *tableKey; 1.112 + int32_t mid, start, limit; 1.113 + int result; 1.114 + 1.115 + /* do a binary search for the key */ 1.116 + start=0; 1.117 + limit=length; 1.118 + while(start<limit) { 1.119 + mid = (start + limit) / 2; 1.120 + tableKey = RES_GET_KEY32(pResData, keyOffsets[mid]); 1.121 + if (pResData->useNativeStrcmp) { 1.122 + result = uprv_strcmp(key, tableKey); 1.123 + } else { 1.124 + result = uprv_compareInvCharsAsAscii(key, tableKey); 1.125 + } 1.126 + if (result < 0) { 1.127 + limit = mid; 1.128 + } else if (result > 0) { 1.129 + start = mid + 1; 1.130 + } else { 1.131 + /* We found it! */ 1.132 + *realKey=tableKey; 1.133 + return mid; 1.134 + } 1.135 + } 1.136 + return URESDATA_ITEM_NOT_FOUND; /* not found or table is empty. */ 1.137 +} 1.138 + 1.139 +/* helper for res_load() ---------------------------------------------------- */ 1.140 + 1.141 +static UBool U_CALLCONV 1.142 +isAcceptable(void *context, 1.143 + const char *type, const char *name, 1.144 + const UDataInfo *pInfo) { 1.145 + uprv_memcpy(context, pInfo->formatVersion, 4); 1.146 + return (UBool)( 1.147 + pInfo->size>=20 && 1.148 + pInfo->isBigEndian==U_IS_BIG_ENDIAN && 1.149 + pInfo->charsetFamily==U_CHARSET_FAMILY && 1.150 + pInfo->sizeofUChar==U_SIZEOF_UCHAR && 1.151 + pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */ 1.152 + pInfo->dataFormat[1]==0x65 && 1.153 + pInfo->dataFormat[2]==0x73 && 1.154 + pInfo->dataFormat[3]==0x42 && 1.155 + (pInfo->formatVersion[0]==1 || pInfo->formatVersion[0]==2)); 1.156 +} 1.157 + 1.158 +/* semi-public functions ---------------------------------------------------- */ 1.159 + 1.160 +static void 1.161 +res_init(ResourceData *pResData, 1.162 + UVersionInfo formatVersion, const void *inBytes, int32_t length, 1.163 + UErrorCode *errorCode) { 1.164 + UResType rootType; 1.165 + 1.166 + /* get the root resource */ 1.167 + pResData->pRoot=(const int32_t *)inBytes; 1.168 + pResData->rootRes=(Resource)*pResData->pRoot; 1.169 + pResData->p16BitUnits=&gEmpty16; 1.170 + 1.171 + /* formatVersion 1.1 must have a root item and at least 5 indexes */ 1.172 + if(length>=0 && (length/4)<((formatVersion[0]==1 && formatVersion[1]==0) ? 1 : 1+5)) { 1.173 + *errorCode=U_INVALID_FORMAT_ERROR; 1.174 + res_unload(pResData); 1.175 + return; 1.176 + } 1.177 + 1.178 + /* currently, we accept only resources that have a Table as their roots */ 1.179 + rootType=(UResType)RES_GET_TYPE(pResData->rootRes); 1.180 + if(!URES_IS_TABLE(rootType)) { 1.181 + *errorCode=U_INVALID_FORMAT_ERROR; 1.182 + res_unload(pResData); 1.183 + return; 1.184 + } 1.185 + 1.186 + if(formatVersion[0]==1 && formatVersion[1]==0) { 1.187 + pResData->localKeyLimit=0x10000; /* greater than any 16-bit key string offset */ 1.188 + } else { 1.189 + /* bundles with formatVersion 1.1 and later contain an indexes[] array */ 1.190 + const int32_t *indexes=pResData->pRoot+1; 1.191 + int32_t indexLength=indexes[URES_INDEX_LENGTH]&0xff; 1.192 + if(indexLength<=URES_INDEX_MAX_TABLE_LENGTH) { 1.193 + *errorCode=U_INVALID_FORMAT_ERROR; 1.194 + res_unload(pResData); 1.195 + return; 1.196 + } 1.197 + if( length>=0 && 1.198 + (length<((1+indexLength)<<2) || 1.199 + length<(indexes[URES_INDEX_BUNDLE_TOP]<<2)) 1.200 + ) { 1.201 + *errorCode=U_INVALID_FORMAT_ERROR; 1.202 + res_unload(pResData); 1.203 + return; 1.204 + } 1.205 + if(indexes[URES_INDEX_KEYS_TOP]>(1+indexLength)) { 1.206 + pResData->localKeyLimit=indexes[URES_INDEX_KEYS_TOP]<<2; 1.207 + } 1.208 + if(indexLength>URES_INDEX_ATTRIBUTES) { 1.209 + int32_t att=indexes[URES_INDEX_ATTRIBUTES]; 1.210 + pResData->noFallback=(UBool)(att&URES_ATT_NO_FALLBACK); 1.211 + pResData->isPoolBundle=(UBool)((att&URES_ATT_IS_POOL_BUNDLE)!=0); 1.212 + pResData->usesPoolBundle=(UBool)((att&URES_ATT_USES_POOL_BUNDLE)!=0); 1.213 + } 1.214 + if((pResData->isPoolBundle || pResData->usesPoolBundle) && indexLength<=URES_INDEX_POOL_CHECKSUM) { 1.215 + *errorCode=U_INVALID_FORMAT_ERROR; 1.216 + res_unload(pResData); 1.217 + return; 1.218 + } 1.219 + if( indexLength>URES_INDEX_16BIT_TOP && 1.220 + indexes[URES_INDEX_16BIT_TOP]>indexes[URES_INDEX_KEYS_TOP] 1.221 + ) { 1.222 + pResData->p16BitUnits=(const uint16_t *)(pResData->pRoot+indexes[URES_INDEX_KEYS_TOP]); 1.223 + } 1.224 + } 1.225 + 1.226 + if(formatVersion[0]==1 || U_CHARSET_FAMILY==U_ASCII_FAMILY) { 1.227 + /* 1.228 + * formatVersion 1: compare key strings in native-charset order 1.229 + * formatVersion 2 and up: compare key strings in ASCII order 1.230 + */ 1.231 + pResData->useNativeStrcmp=TRUE; 1.232 + } 1.233 +} 1.234 + 1.235 +U_CAPI void U_EXPORT2 1.236 +res_read(ResourceData *pResData, 1.237 + const UDataInfo *pInfo, const void *inBytes, int32_t length, 1.238 + UErrorCode *errorCode) { 1.239 + UVersionInfo formatVersion; 1.240 + 1.241 + uprv_memset(pResData, 0, sizeof(ResourceData)); 1.242 + if(U_FAILURE(*errorCode)) { 1.243 + return; 1.244 + } 1.245 + if(!isAcceptable(formatVersion, NULL, NULL, pInfo)) { 1.246 + *errorCode=U_INVALID_FORMAT_ERROR; 1.247 + return; 1.248 + } 1.249 + res_init(pResData, formatVersion, inBytes, length, errorCode); 1.250 +} 1.251 + 1.252 +U_CFUNC void 1.253 +res_load(ResourceData *pResData, 1.254 + const char *path, const char *name, UErrorCode *errorCode) { 1.255 + UVersionInfo formatVersion; 1.256 + 1.257 + uprv_memset(pResData, 0, sizeof(ResourceData)); 1.258 + 1.259 + /* load the ResourceBundle file */ 1.260 + pResData->data=udata_openChoice(path, "res", name, isAcceptable, formatVersion, errorCode); 1.261 + if(U_FAILURE(*errorCode)) { 1.262 + return; 1.263 + } 1.264 + 1.265 + /* get its memory and initialize *pResData */ 1.266 + res_init(pResData, formatVersion, udata_getMemory(pResData->data), -1, errorCode); 1.267 +} 1.268 + 1.269 +U_CFUNC void 1.270 +res_unload(ResourceData *pResData) { 1.271 + if(pResData->data!=NULL) { 1.272 + udata_close(pResData->data); 1.273 + pResData->data=NULL; 1.274 + } 1.275 +} 1.276 + 1.277 +static const int8_t gPublicTypes[URES_LIMIT] = { 1.278 + URES_STRING, 1.279 + URES_BINARY, 1.280 + URES_TABLE, 1.281 + URES_ALIAS, 1.282 + 1.283 + URES_TABLE, /* URES_TABLE32 */ 1.284 + URES_TABLE, /* URES_TABLE16 */ 1.285 + URES_STRING, /* URES_STRING_V2 */ 1.286 + URES_INT, 1.287 + 1.288 + URES_ARRAY, 1.289 + URES_ARRAY, /* URES_ARRAY16 */ 1.290 + URES_NONE, 1.291 + URES_NONE, 1.292 + 1.293 + URES_NONE, 1.294 + URES_NONE, 1.295 + URES_INT_VECTOR, 1.296 + URES_NONE 1.297 +}; 1.298 + 1.299 +U_CAPI UResType U_EXPORT2 1.300 +res_getPublicType(Resource res) { 1.301 + return (UResType)gPublicTypes[RES_GET_TYPE(res)]; 1.302 +} 1.303 + 1.304 +U_CAPI const UChar * U_EXPORT2 1.305 +res_getString(const ResourceData *pResData, Resource res, int32_t *pLength) { 1.306 + const UChar *p; 1.307 + uint32_t offset=RES_GET_OFFSET(res); 1.308 + int32_t length; 1.309 + if(RES_GET_TYPE(res)==URES_STRING_V2) { 1.310 + int32_t first; 1.311 + p=(const UChar *)(pResData->p16BitUnits+offset); 1.312 + first=*p; 1.313 + if(!U16_IS_TRAIL(first)) { 1.314 + length=u_strlen(p); 1.315 + } else if(first<0xdfef) { 1.316 + length=first&0x3ff; 1.317 + ++p; 1.318 + } else if(first<0xdfff) { 1.319 + length=((first-0xdfef)<<16)|p[1]; 1.320 + p+=2; 1.321 + } else { 1.322 + length=((int32_t)p[1]<<16)|p[2]; 1.323 + p+=3; 1.324 + } 1.325 + } else if(res==offset) /* RES_GET_TYPE(res)==URES_STRING */ { 1.326 + const int32_t *p32= res==0 ? &gEmptyString.length : pResData->pRoot+res; 1.327 + length=*p32++; 1.328 + p=(const UChar *)p32; 1.329 + } else { 1.330 + p=NULL; 1.331 + length=0; 1.332 + } 1.333 + if(pLength) { 1.334 + *pLength=length; 1.335 + } 1.336 + return p; 1.337 +} 1.338 + 1.339 +U_CAPI const UChar * U_EXPORT2 1.340 +res_getAlias(const ResourceData *pResData, Resource res, int32_t *pLength) { 1.341 + const UChar *p; 1.342 + uint32_t offset=RES_GET_OFFSET(res); 1.343 + int32_t length; 1.344 + if(RES_GET_TYPE(res)==URES_ALIAS) { 1.345 + const int32_t *p32= offset==0 ? &gEmptyString.length : pResData->pRoot+offset; 1.346 + length=*p32++; 1.347 + p=(const UChar *)p32; 1.348 + } else { 1.349 + p=NULL; 1.350 + length=0; 1.351 + } 1.352 + if(pLength) { 1.353 + *pLength=length; 1.354 + } 1.355 + return p; 1.356 +} 1.357 + 1.358 +U_CAPI const uint8_t * U_EXPORT2 1.359 +res_getBinary(const ResourceData *pResData, Resource res, int32_t *pLength) { 1.360 + const uint8_t *p; 1.361 + uint32_t offset=RES_GET_OFFSET(res); 1.362 + int32_t length; 1.363 + if(RES_GET_TYPE(res)==URES_BINARY) { 1.364 + const int32_t *p32= offset==0 ? (const int32_t*)&gEmpty32 : pResData->pRoot+offset; 1.365 + length=*p32++; 1.366 + p=(const uint8_t *)p32; 1.367 + } else { 1.368 + p=NULL; 1.369 + length=0; 1.370 + } 1.371 + if(pLength) { 1.372 + *pLength=length; 1.373 + } 1.374 + return p; 1.375 +} 1.376 + 1.377 + 1.378 +U_CAPI const int32_t * U_EXPORT2 1.379 +res_getIntVector(const ResourceData *pResData, Resource res, int32_t *pLength) { 1.380 + const int32_t *p; 1.381 + uint32_t offset=RES_GET_OFFSET(res); 1.382 + int32_t length; 1.383 + if(RES_GET_TYPE(res)==URES_INT_VECTOR) { 1.384 + p= offset==0 ? (const int32_t *)&gEmpty32 : pResData->pRoot+offset; 1.385 + length=*p++; 1.386 + } else { 1.387 + p=NULL; 1.388 + length=0; 1.389 + } 1.390 + if(pLength) { 1.391 + *pLength=length; 1.392 + } 1.393 + return p; 1.394 +} 1.395 + 1.396 +U_CAPI int32_t U_EXPORT2 1.397 +res_countArrayItems(const ResourceData *pResData, Resource res) { 1.398 + uint32_t offset=RES_GET_OFFSET(res); 1.399 + switch(RES_GET_TYPE(res)) { 1.400 + case URES_STRING: 1.401 + case URES_STRING_V2: 1.402 + case URES_BINARY: 1.403 + case URES_ALIAS: 1.404 + case URES_INT: 1.405 + case URES_INT_VECTOR: 1.406 + return 1; 1.407 + case URES_ARRAY: 1.408 + case URES_TABLE32: 1.409 + return offset==0 ? 0 : *(pResData->pRoot+offset); 1.410 + case URES_TABLE: 1.411 + return offset==0 ? 0 : *((const uint16_t *)(pResData->pRoot+offset)); 1.412 + case URES_ARRAY16: 1.413 + case URES_TABLE16: 1.414 + return pResData->p16BitUnits[offset]; 1.415 + default: 1.416 + return 0; 1.417 + } 1.418 +} 1.419 + 1.420 +U_CAPI Resource U_EXPORT2 1.421 +res_getTableItemByKey(const ResourceData *pResData, Resource table, 1.422 + int32_t *indexR, const char **key) { 1.423 + uint32_t offset=RES_GET_OFFSET(table); 1.424 + int32_t length; 1.425 + int32_t idx; 1.426 + if(key == NULL || *key == NULL) { 1.427 + return RES_BOGUS; 1.428 + } 1.429 + switch(RES_GET_TYPE(table)) { 1.430 + case URES_TABLE: { 1.431 + if (offset!=0) { /* empty if offset==0 */ 1.432 + const uint16_t *p= (const uint16_t *)(pResData->pRoot+offset); 1.433 + length=*p++; 1.434 + *indexR=idx=_res_findTableItem(pResData, p, length, *key, key); 1.435 + if(idx>=0) { 1.436 + const Resource *p32=(const Resource *)(p+length+(~length&1)); 1.437 + return p32[idx]; 1.438 + } 1.439 + } 1.440 + break; 1.441 + } 1.442 + case URES_TABLE16: { 1.443 + const uint16_t *p=pResData->p16BitUnits+offset; 1.444 + length=*p++; 1.445 + *indexR=idx=_res_findTableItem(pResData, p, length, *key, key); 1.446 + if(idx>=0) { 1.447 + return URES_MAKE_RESOURCE(URES_STRING_V2, p[length+idx]); 1.448 + } 1.449 + break; 1.450 + } 1.451 + case URES_TABLE32: { 1.452 + if (offset!=0) { /* empty if offset==0 */ 1.453 + const int32_t *p= pResData->pRoot+offset; 1.454 + length=*p++; 1.455 + *indexR=idx=_res_findTable32Item(pResData, p, length, *key, key); 1.456 + if(idx>=0) { 1.457 + return (Resource)p[length+idx]; 1.458 + } 1.459 + } 1.460 + break; 1.461 + } 1.462 + default: 1.463 + break; 1.464 + } 1.465 + return RES_BOGUS; 1.466 +} 1.467 + 1.468 +U_CAPI Resource U_EXPORT2 1.469 +res_getTableItemByIndex(const ResourceData *pResData, Resource table, 1.470 + int32_t indexR, const char **key) { 1.471 + uint32_t offset=RES_GET_OFFSET(table); 1.472 + int32_t length; 1.473 + U_ASSERT(indexR>=0); /* to ensure the index is not negative */ 1.474 + switch(RES_GET_TYPE(table)) { 1.475 + case URES_TABLE: { 1.476 + if (offset != 0) { /* empty if offset==0 */ 1.477 + const uint16_t *p= (const uint16_t *)(pResData->pRoot+offset); 1.478 + length=*p++; 1.479 + if(indexR<length) { 1.480 + const Resource *p32=(const Resource *)(p+length+(~length&1)); 1.481 + if(key!=NULL) { 1.482 + *key=RES_GET_KEY16(pResData, p[indexR]); 1.483 + } 1.484 + return p32[indexR]; 1.485 + } 1.486 + } 1.487 + break; 1.488 + } 1.489 + case URES_TABLE16: { 1.490 + const uint16_t *p=pResData->p16BitUnits+offset; 1.491 + length=*p++; 1.492 + if(indexR<length) { 1.493 + if(key!=NULL) { 1.494 + *key=RES_GET_KEY16(pResData, p[indexR]); 1.495 + } 1.496 + return URES_MAKE_RESOURCE(URES_STRING_V2, p[length+indexR]); 1.497 + } 1.498 + break; 1.499 + } 1.500 + case URES_TABLE32: { 1.501 + if (offset != 0) { /* empty if offset==0 */ 1.502 + const int32_t *p= pResData->pRoot+offset; 1.503 + length=*p++; 1.504 + if(indexR<length) { 1.505 + if(key!=NULL) { 1.506 + *key=RES_GET_KEY32(pResData, p[indexR]); 1.507 + } 1.508 + return (Resource)p[length+indexR]; 1.509 + } 1.510 + } 1.511 + break; 1.512 + } 1.513 + default: 1.514 + break; 1.515 + } 1.516 + return RES_BOGUS; 1.517 +} 1.518 + 1.519 +U_CAPI Resource U_EXPORT2 1.520 +res_getResource(const ResourceData *pResData, const char *key) { 1.521 + const char *realKey=key; 1.522 + int32_t idx; 1.523 + return res_getTableItemByKey(pResData, pResData->rootRes, &idx, &realKey); 1.524 +} 1.525 + 1.526 +U_CAPI Resource U_EXPORT2 1.527 +res_getArrayItem(const ResourceData *pResData, Resource array, int32_t indexR) { 1.528 + uint32_t offset=RES_GET_OFFSET(array); 1.529 + U_ASSERT(indexR>=0); /* to ensure the index is not negative */ 1.530 + switch(RES_GET_TYPE(array)) { 1.531 + case URES_ARRAY: { 1.532 + if (offset!=0) { /* empty if offset==0 */ 1.533 + const int32_t *p= pResData->pRoot+offset; 1.534 + if(indexR<*p) { 1.535 + return (Resource)p[1+indexR]; 1.536 + } 1.537 + } 1.538 + break; 1.539 + } 1.540 + case URES_ARRAY16: { 1.541 + const uint16_t *p=pResData->p16BitUnits+offset; 1.542 + if(indexR<*p) { 1.543 + return URES_MAKE_RESOURCE(URES_STRING_V2, p[1+indexR]); 1.544 + } 1.545 + break; 1.546 + } 1.547 + default: 1.548 + break; 1.549 + } 1.550 + return RES_BOGUS; 1.551 +} 1.552 + 1.553 +U_CFUNC Resource 1.554 +res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key) { 1.555 + /* we pass in a path. CollationElements/Sequence or zoneStrings/3/2 etc. 1.556 + * iterates over a path and stops when a scalar resource is found. This 1.557 + * CAN be an alias. Path gets set to the part that has not yet been processed. 1.558 + */ 1.559 + 1.560 + char *pathP = *path, *nextSepP = *path; 1.561 + char *closeIndex = NULL; 1.562 + Resource t1 = r; 1.563 + Resource t2; 1.564 + int32_t indexR = 0; 1.565 + UResType type = (UResType)RES_GET_TYPE(t1); 1.566 + 1.567 + /* if you come in with an empty path, you'll be getting back the same resource */ 1.568 + if(!uprv_strlen(pathP)) { 1.569 + return r; 1.570 + } 1.571 + 1.572 + /* one needs to have an aggregate resource in order to search in it */ 1.573 + if(!URES_IS_CONTAINER(type)) { 1.574 + return RES_BOGUS; 1.575 + } 1.576 + 1.577 + while(nextSepP && *pathP && t1 != RES_BOGUS && URES_IS_CONTAINER(type)) { 1.578 + /* Iteration stops if: the path has been consumed, we found a non-existing 1.579 + * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias) 1.580 + */ 1.581 + nextSepP = uprv_strchr(pathP, RES_PATH_SEPARATOR); 1.582 + /* if there are more separators, terminate string 1.583 + * and set path to the remaining part of the string 1.584 + */ 1.585 + if(nextSepP != NULL) { 1.586 + *nextSepP = 0; /* overwrite the separator with a NUL to terminate the key */ 1.587 + *path = nextSepP+1; 1.588 + } else { 1.589 + *path = uprv_strchr(pathP, 0); 1.590 + } 1.591 + 1.592 + /* if the resource is a table */ 1.593 + /* try the key based access */ 1.594 + if(URES_IS_TABLE(type)) { 1.595 + *key = pathP; 1.596 + t2 = res_getTableItemByKey(pResData, t1, &indexR, key); 1.597 + if(t2 == RES_BOGUS) { 1.598 + /* if we fail to get the resource by key, maybe we got an index */ 1.599 + indexR = uprv_strtol(pathP, &closeIndex, 10); 1.600 + if(closeIndex != pathP) { 1.601 + /* if we indeed have an index, try to get the item by index */ 1.602 + t2 = res_getTableItemByIndex(pResData, t1, indexR, key); 1.603 + } 1.604 + } 1.605 + } else if(URES_IS_ARRAY(type)) { 1.606 + indexR = uprv_strtol(pathP, &closeIndex, 10); 1.607 + if(closeIndex != pathP) { 1.608 + t2 = res_getArrayItem(pResData, t1, indexR); 1.609 + } else { 1.610 + t2 = RES_BOGUS; /* have an array, but don't have a valid index */ 1.611 + } 1.612 + *key = NULL; 1.613 + } else { /* can't do much here, except setting t2 to bogus */ 1.614 + t2 = RES_BOGUS; 1.615 + } 1.616 + t1 = t2; 1.617 + type = (UResType)RES_GET_TYPE(t1); 1.618 + /* position pathP to next resource key/index */ 1.619 + pathP = *path; 1.620 + } 1.621 + 1.622 + return t1; 1.623 +} 1.624 + 1.625 +/* resource bundle swapping ------------------------------------------------- */ 1.626 + 1.627 +/* 1.628 + * Need to always enumerate the entire item tree, 1.629 + * track the lowest address of any item to use as the limit for char keys[], 1.630 + * track the highest address of any item to return the size of the data. 1.631 + * 1.632 + * We should have thought of storing those in the data... 1.633 + * It is possible to extend the data structure by putting additional values 1.634 + * in places that are inaccessible by ordinary enumeration of the item tree. 1.635 + * For example, additional integers could be stored at the beginning or 1.636 + * end of the key strings; this could be indicated by a minor version number, 1.637 + * and the data swapping would have to know about these values. 1.638 + * 1.639 + * The data structure does not forbid keys to be shared, so we must swap 1.640 + * all keys once instead of each key when it is referenced. 1.641 + * 1.642 + * These swapping functions assume that a resource bundle always has a length 1.643 + * that is a multiple of 4 bytes. 1.644 + * Currently, this is trivially true because genrb writes bundle tree leaves 1.645 + * physically first, before their branches, so that the root table with its 1.646 + * array of resource items (uint32_t values) is always last. 1.647 + */ 1.648 + 1.649 +/* definitions for table sorting ------------------------ */ 1.650 + 1.651 +/* 1.652 + * row of a temporary array 1.653 + * 1.654 + * gets platform-endian key string indexes and sorting indexes; 1.655 + * after sorting this array by keys, the actual key/value arrays are permutated 1.656 + * according to the sorting indexes 1.657 + */ 1.658 +typedef struct Row { 1.659 + int32_t keyIndex, sortIndex; 1.660 +} Row; 1.661 + 1.662 +static int32_t 1.663 +ures_compareRows(const void *context, const void *left, const void *right) { 1.664 + const char *keyChars=(const char *)context; 1.665 + return (int32_t)uprv_strcmp(keyChars+((const Row *)left)->keyIndex, 1.666 + keyChars+((const Row *)right)->keyIndex); 1.667 +} 1.668 + 1.669 +typedef struct TempTable { 1.670 + const char *keyChars; 1.671 + Row *rows; 1.672 + int32_t *resort; 1.673 + uint32_t *resFlags; 1.674 + int32_t localKeyLimit; 1.675 + uint8_t majorFormatVersion; 1.676 +} TempTable; 1.677 + 1.678 +enum { 1.679 + STACK_ROW_CAPACITY=200 1.680 +}; 1.681 + 1.682 +/* The table item key string is not locally available. */ 1.683 +static const char *const gUnknownKey=""; 1.684 + 1.685 +/* resource table key for collation binaries: "%%CollationBin" */ 1.686 +static const UChar gCollationBinKey[]={ 1.687 + 0x25, 0x25, 1.688 + 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 1.689 + 0x42, 0x69, 0x6e, 1.690 + 0 1.691 +}; 1.692 + 1.693 +/* 1.694 + * swap one resource item 1.695 + */ 1.696 +static void 1.697 +ures_swapResource(const UDataSwapper *ds, 1.698 + const Resource *inBundle, Resource *outBundle, 1.699 + Resource res, /* caller swaps res itself */ 1.700 + const char *key, 1.701 + TempTable *pTempTable, 1.702 + UErrorCode *pErrorCode) { 1.703 + const Resource *p; 1.704 + Resource *q; 1.705 + int32_t offset, count; 1.706 + 1.707 + switch(RES_GET_TYPE(res)) { 1.708 + case URES_TABLE16: 1.709 + case URES_STRING_V2: 1.710 + case URES_INT: 1.711 + case URES_ARRAY16: 1.712 + /* integer, or points to 16-bit units, nothing to do here */ 1.713 + return; 1.714 + default: 1.715 + break; 1.716 + } 1.717 + 1.718 + /* all other types use an offset to point to their data */ 1.719 + offset=(int32_t)RES_GET_OFFSET(res); 1.720 + if(offset==0) { 1.721 + /* special offset indicating an empty item */ 1.722 + return; 1.723 + } 1.724 + if(pTempTable->resFlags[offset>>5]&((uint32_t)1<<(offset&0x1f))) { 1.725 + /* we already swapped this resource item */ 1.726 + return; 1.727 + } else { 1.728 + /* mark it as swapped now */ 1.729 + pTempTable->resFlags[offset>>5]|=((uint32_t)1<<(offset&0x1f)); 1.730 + } 1.731 + 1.732 + p=inBundle+offset; 1.733 + q=outBundle+offset; 1.734 + 1.735 + switch(RES_GET_TYPE(res)) { 1.736 + case URES_ALIAS: 1.737 + /* physically same value layout as string, fall through */ 1.738 + case URES_STRING: 1.739 + count=udata_readInt32(ds, (int32_t)*p); 1.740 + /* swap length */ 1.741 + ds->swapArray32(ds, p, 4, q, pErrorCode); 1.742 + /* swap each UChar (the terminating NUL would not change) */ 1.743 + ds->swapArray16(ds, p+1, 2*count, q+1, pErrorCode); 1.744 + break; 1.745 + case URES_BINARY: 1.746 + count=udata_readInt32(ds, (int32_t)*p); 1.747 + /* swap length */ 1.748 + ds->swapArray32(ds, p, 4, q, pErrorCode); 1.749 + /* no need to swap or copy bytes - ures_swap() copied them all */ 1.750 + 1.751 + /* swap known formats */ 1.752 +#if !UCONFIG_NO_COLLATION 1.753 + if( key!=NULL && /* the binary is in a table */ 1.754 + (key!=gUnknownKey ? 1.755 + /* its table key string is "%%CollationBin" */ 1.756 + 0==ds->compareInvChars(ds, key, -1, 1.757 + gCollationBinKey, LENGTHOF(gCollationBinKey)-1) : 1.758 + /* its table key string is unknown but it looks like a collation binary */ 1.759 + ucol_looksLikeCollationBinary(ds, p+1, count)) 1.760 + ) { 1.761 + ucol_swapBinary(ds, p+1, count, q+1, pErrorCode); 1.762 + } 1.763 +#endif 1.764 + break; 1.765 + case URES_TABLE: 1.766 + case URES_TABLE32: 1.767 + { 1.768 + const uint16_t *pKey16; 1.769 + uint16_t *qKey16; 1.770 + 1.771 + const int32_t *pKey32; 1.772 + int32_t *qKey32; 1.773 + 1.774 + Resource item; 1.775 + int32_t i, oldIndex; 1.776 + 1.777 + if(RES_GET_TYPE(res)==URES_TABLE) { 1.778 + /* get table item count */ 1.779 + pKey16=(const uint16_t *)p; 1.780 + qKey16=(uint16_t *)q; 1.781 + count=ds->readUInt16(*pKey16); 1.782 + 1.783 + pKey32=qKey32=NULL; 1.784 + 1.785 + /* swap count */ 1.786 + ds->swapArray16(ds, pKey16++, 2, qKey16++, pErrorCode); 1.787 + 1.788 + offset+=((1+count)+1)/2; 1.789 + } else { 1.790 + /* get table item count */ 1.791 + pKey32=(const int32_t *)p; 1.792 + qKey32=(int32_t *)q; 1.793 + count=udata_readInt32(ds, *pKey32); 1.794 + 1.795 + pKey16=qKey16=NULL; 1.796 + 1.797 + /* swap count */ 1.798 + ds->swapArray32(ds, pKey32++, 4, qKey32++, pErrorCode); 1.799 + 1.800 + offset+=1+count; 1.801 + } 1.802 + 1.803 + if(count==0) { 1.804 + break; 1.805 + } 1.806 + 1.807 + p=inBundle+offset; /* pointer to table resources */ 1.808 + q=outBundle+offset; 1.809 + 1.810 + /* recurse */ 1.811 + for(i=0; i<count; ++i) { 1.812 + const char *itemKey=gUnknownKey; 1.813 + if(pKey16!=NULL) { 1.814 + int32_t keyOffset=ds->readUInt16(pKey16[i]); 1.815 + if(keyOffset<pTempTable->localKeyLimit) { 1.816 + itemKey=(const char *)outBundle+keyOffset; 1.817 + } 1.818 + } else { 1.819 + int32_t keyOffset=udata_readInt32(ds, pKey32[i]); 1.820 + if(keyOffset>=0) { 1.821 + itemKey=(const char *)outBundle+keyOffset; 1.822 + } 1.823 + } 1.824 + item=ds->readUInt32(p[i]); 1.825 + ures_swapResource(ds, inBundle, outBundle, item, itemKey, pTempTable, pErrorCode); 1.826 + if(U_FAILURE(*pErrorCode)) { 1.827 + udata_printError(ds, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n", 1.828 + res, i, item); 1.829 + return; 1.830 + } 1.831 + } 1.832 + 1.833 + if(pTempTable->majorFormatVersion>1 || ds->inCharset==ds->outCharset) { 1.834 + /* no need to sort, just swap the offset/value arrays */ 1.835 + if(pKey16!=NULL) { 1.836 + ds->swapArray16(ds, pKey16, count*2, qKey16, pErrorCode); 1.837 + ds->swapArray32(ds, p, count*4, q, pErrorCode); 1.838 + } else { 1.839 + /* swap key offsets and items as one array */ 1.840 + ds->swapArray32(ds, pKey32, count*2*4, qKey32, pErrorCode); 1.841 + } 1.842 + break; 1.843 + } 1.844 + 1.845 + /* 1.846 + * We need to sort tables by outCharset key strings because they 1.847 + * sort differently for different charset families. 1.848 + * ures_swap() already set pTempTable->keyChars appropriately. 1.849 + * First we set up a temporary table with the key indexes and 1.850 + * sorting indexes and sort that. 1.851 + * Then we permutate and copy/swap the actual values. 1.852 + */ 1.853 + if(pKey16!=NULL) { 1.854 + for(i=0; i<count; ++i) { 1.855 + pTempTable->rows[i].keyIndex=ds->readUInt16(pKey16[i]); 1.856 + pTempTable->rows[i].sortIndex=i; 1.857 + } 1.858 + } else { 1.859 + for(i=0; i<count; ++i) { 1.860 + pTempTable->rows[i].keyIndex=udata_readInt32(ds, pKey32[i]); 1.861 + pTempTable->rows[i].sortIndex=i; 1.862 + } 1.863 + } 1.864 + uprv_sortArray(pTempTable->rows, count, sizeof(Row), 1.865 + ures_compareRows, pTempTable->keyChars, 1.866 + FALSE, pErrorCode); 1.867 + if(U_FAILURE(*pErrorCode)) { 1.868 + udata_printError(ds, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n", 1.869 + res, count); 1.870 + return; 1.871 + } 1.872 + 1.873 + /* 1.874 + * copy/swap/permutate items 1.875 + * 1.876 + * If we swap in-place, then the permutation must use another 1.877 + * temporary array (pTempTable->resort) 1.878 + * before the results are copied to the outBundle. 1.879 + */ 1.880 + /* keys */ 1.881 + if(pKey16!=NULL) { 1.882 + uint16_t *rKey16; 1.883 + 1.884 + if(pKey16!=qKey16) { 1.885 + rKey16=qKey16; 1.886 + } else { 1.887 + rKey16=(uint16_t *)pTempTable->resort; 1.888 + } 1.889 + for(i=0; i<count; ++i) { 1.890 + oldIndex=pTempTable->rows[i].sortIndex; 1.891 + ds->swapArray16(ds, pKey16+oldIndex, 2, rKey16+i, pErrorCode); 1.892 + } 1.893 + if(qKey16!=rKey16) { 1.894 + uprv_memcpy(qKey16, rKey16, 2*count); 1.895 + } 1.896 + } else { 1.897 + int32_t *rKey32; 1.898 + 1.899 + if(pKey32!=qKey32) { 1.900 + rKey32=qKey32; 1.901 + } else { 1.902 + rKey32=pTempTable->resort; 1.903 + } 1.904 + for(i=0; i<count; ++i) { 1.905 + oldIndex=pTempTable->rows[i].sortIndex; 1.906 + ds->swapArray32(ds, pKey32+oldIndex, 4, rKey32+i, pErrorCode); 1.907 + } 1.908 + if(qKey32!=rKey32) { 1.909 + uprv_memcpy(qKey32, rKey32, 4*count); 1.910 + } 1.911 + } 1.912 + 1.913 + /* resources */ 1.914 + { 1.915 + Resource *r; 1.916 + 1.917 + 1.918 + if(p!=q) { 1.919 + r=q; 1.920 + } else { 1.921 + r=(Resource *)pTempTable->resort; 1.922 + } 1.923 + for(i=0; i<count; ++i) { 1.924 + oldIndex=pTempTable->rows[i].sortIndex; 1.925 + ds->swapArray32(ds, p+oldIndex, 4, r+i, pErrorCode); 1.926 + } 1.927 + if(q!=r) { 1.928 + uprv_memcpy(q, r, 4*count); 1.929 + } 1.930 + } 1.931 + } 1.932 + break; 1.933 + case URES_ARRAY: 1.934 + { 1.935 + Resource item; 1.936 + int32_t i; 1.937 + 1.938 + count=udata_readInt32(ds, (int32_t)*p); 1.939 + /* swap length */ 1.940 + ds->swapArray32(ds, p++, 4, q++, pErrorCode); 1.941 + 1.942 + /* recurse */ 1.943 + for(i=0; i<count; ++i) { 1.944 + item=ds->readUInt32(p[i]); 1.945 + ures_swapResource(ds, inBundle, outBundle, item, NULL, pTempTable, pErrorCode); 1.946 + if(U_FAILURE(*pErrorCode)) { 1.947 + udata_printError(ds, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n", 1.948 + res, i, item); 1.949 + return; 1.950 + } 1.951 + } 1.952 + 1.953 + /* swap items */ 1.954 + ds->swapArray32(ds, p, 4*count, q, pErrorCode); 1.955 + } 1.956 + break; 1.957 + case URES_INT_VECTOR: 1.958 + count=udata_readInt32(ds, (int32_t)*p); 1.959 + /* swap length and each integer */ 1.960 + ds->swapArray32(ds, p, 4*(1+count), q, pErrorCode); 1.961 + break; 1.962 + default: 1.963 + /* also catches RES_BOGUS */ 1.964 + *pErrorCode=U_UNSUPPORTED_ERROR; 1.965 + break; 1.966 + } 1.967 +} 1.968 + 1.969 +U_CAPI int32_t U_EXPORT2 1.970 +ures_swap(const UDataSwapper *ds, 1.971 + const void *inData, int32_t length, void *outData, 1.972 + UErrorCode *pErrorCode) { 1.973 + const UDataInfo *pInfo; 1.974 + const Resource *inBundle; 1.975 + Resource rootRes; 1.976 + int32_t headerSize, maxTableLength; 1.977 + 1.978 + Row rows[STACK_ROW_CAPACITY]; 1.979 + int32_t resort[STACK_ROW_CAPACITY]; 1.980 + TempTable tempTable; 1.981 + 1.982 + const int32_t *inIndexes; 1.983 + 1.984 + /* the following integers count Resource item offsets (4 bytes each), not bytes */ 1.985 + int32_t bundleLength, indexLength, keysBottom, keysTop, resBottom, top; 1.986 + 1.987 + /* udata_swapDataHeader checks the arguments */ 1.988 + headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode); 1.989 + if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { 1.990 + return 0; 1.991 + } 1.992 + 1.993 + /* check data format and format version */ 1.994 + pInfo=(const UDataInfo *)((const char *)inData+4); 1.995 + if(!( 1.996 + pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */ 1.997 + pInfo->dataFormat[1]==0x65 && 1.998 + pInfo->dataFormat[2]==0x73 && 1.999 + pInfo->dataFormat[3]==0x42 && 1.1000 + ((pInfo->formatVersion[0]==1 && pInfo->formatVersion[1]>=1) || /* formatVersion 1.1+ or 2.x */ 1.1001 + pInfo->formatVersion[0]==2) 1.1002 + )) { 1.1003 + udata_printError(ds, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n", 1.1004 + pInfo->dataFormat[0], pInfo->dataFormat[1], 1.1005 + pInfo->dataFormat[2], pInfo->dataFormat[3], 1.1006 + pInfo->formatVersion[0], pInfo->formatVersion[1]); 1.1007 + *pErrorCode=U_UNSUPPORTED_ERROR; 1.1008 + return 0; 1.1009 + } 1.1010 + tempTable.majorFormatVersion=pInfo->formatVersion[0]; 1.1011 + 1.1012 + /* a resource bundle must contain at least one resource item */ 1.1013 + if(length<0) { 1.1014 + bundleLength=-1; 1.1015 + } else { 1.1016 + bundleLength=(length-headerSize)/4; 1.1017 + 1.1018 + /* formatVersion 1.1 must have a root item and at least 5 indexes */ 1.1019 + if(bundleLength<(1+5)) { 1.1020 + udata_printError(ds, "ures_swap(): too few bytes (%d after header) for a resource bundle\n", 1.1021 + length-headerSize); 1.1022 + *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; 1.1023 + return 0; 1.1024 + } 1.1025 + } 1.1026 + 1.1027 + inBundle=(const Resource *)((const char *)inData+headerSize); 1.1028 + rootRes=ds->readUInt32(*inBundle); 1.1029 + 1.1030 + /* formatVersion 1.1 adds the indexes[] array */ 1.1031 + inIndexes=(const int32_t *)(inBundle+1); 1.1032 + 1.1033 + indexLength=udata_readInt32(ds, inIndexes[URES_INDEX_LENGTH])&0xff; 1.1034 + if(indexLength<=URES_INDEX_MAX_TABLE_LENGTH) { 1.1035 + udata_printError(ds, "ures_swap(): too few indexes for a 1.1+ resource bundle\n"); 1.1036 + *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; 1.1037 + return 0; 1.1038 + } 1.1039 + keysBottom=1+indexLength; 1.1040 + keysTop=udata_readInt32(ds, inIndexes[URES_INDEX_KEYS_TOP]); 1.1041 + if(indexLength>URES_INDEX_16BIT_TOP) { 1.1042 + resBottom=udata_readInt32(ds, inIndexes[URES_INDEX_16BIT_TOP]); 1.1043 + } else { 1.1044 + resBottom=keysTop; 1.1045 + } 1.1046 + top=udata_readInt32(ds, inIndexes[URES_INDEX_BUNDLE_TOP]); 1.1047 + maxTableLength=udata_readInt32(ds, inIndexes[URES_INDEX_MAX_TABLE_LENGTH]); 1.1048 + 1.1049 + if(0<=bundleLength && bundleLength<top) { 1.1050 + udata_printError(ds, "ures_swap(): resource top %d exceeds bundle length %d\n", 1.1051 + top, bundleLength); 1.1052 + *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; 1.1053 + return 0; 1.1054 + } 1.1055 + if(keysTop>(1+indexLength)) { 1.1056 + tempTable.localKeyLimit=keysTop<<2; 1.1057 + } else { 1.1058 + tempTable.localKeyLimit=0; 1.1059 + } 1.1060 + 1.1061 + if(length>=0) { 1.1062 + Resource *outBundle=(Resource *)((char *)outData+headerSize); 1.1063 + 1.1064 + /* track which resources we have already swapped */ 1.1065 + uint32_t stackResFlags[STACK_ROW_CAPACITY]; 1.1066 + int32_t resFlagsLength; 1.1067 + 1.1068 + /* 1.1069 + * We need one bit per 4 resource bundle bytes so that we can track 1.1070 + * every possible Resource for whether we have swapped it already. 1.1071 + * Multiple Resource words can refer to the same bundle offsets 1.1072 + * for sharing identical values. 1.1073 + * We could optimize this by allocating only for locations above 1.1074 + * where Resource values are stored (above keys & strings). 1.1075 + */ 1.1076 + resFlagsLength=(length+31)>>5; /* number of bytes needed */ 1.1077 + resFlagsLength=(resFlagsLength+3)&~3; /* multiple of 4 bytes for uint32_t */ 1.1078 + if(resFlagsLength<=sizeof(stackResFlags)) { 1.1079 + tempTable.resFlags=stackResFlags; 1.1080 + } else { 1.1081 + tempTable.resFlags=(uint32_t *)uprv_malloc(resFlagsLength); 1.1082 + if(tempTable.resFlags==NULL) { 1.1083 + udata_printError(ds, "ures_swap(): unable to allocate memory for tracking resources\n"); 1.1084 + *pErrorCode=U_MEMORY_ALLOCATION_ERROR; 1.1085 + return 0; 1.1086 + } 1.1087 + } 1.1088 + uprv_memset(tempTable.resFlags, 0, resFlagsLength); 1.1089 + 1.1090 + /* copy the bundle for binary and inaccessible data */ 1.1091 + if(inData!=outData) { 1.1092 + uprv_memcpy(outBundle, inBundle, 4*top); 1.1093 + } 1.1094 + 1.1095 + /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */ 1.1096 + udata_swapInvStringBlock(ds, inBundle+keysBottom, 4*(keysTop-keysBottom), 1.1097 + outBundle+keysBottom, pErrorCode); 1.1098 + if(U_FAILURE(*pErrorCode)) { 1.1099 + udata_printError(ds, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop-keysBottom)); 1.1100 + return 0; 1.1101 + } 1.1102 + 1.1103 + /* swap the 16-bit units (strings, table16, array16) */ 1.1104 + if(keysTop<resBottom) { 1.1105 + ds->swapArray16(ds, inBundle+keysTop, (resBottom-keysTop)*4, outBundle+keysTop, pErrorCode); 1.1106 + if(U_FAILURE(*pErrorCode)) { 1.1107 + udata_printError(ds, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom-keysTop)); 1.1108 + return 0; 1.1109 + } 1.1110 + } 1.1111 + 1.1112 + /* allocate the temporary table for sorting resource tables */ 1.1113 + tempTable.keyChars=(const char *)outBundle; /* sort by outCharset */ 1.1114 + if(tempTable.majorFormatVersion>1 || maxTableLength<=STACK_ROW_CAPACITY) { 1.1115 + tempTable.rows=rows; 1.1116 + tempTable.resort=resort; 1.1117 + } else { 1.1118 + tempTable.rows=(Row *)uprv_malloc(maxTableLength*sizeof(Row)+maxTableLength*4); 1.1119 + if(tempTable.rows==NULL) { 1.1120 + udata_printError(ds, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n", 1.1121 + maxTableLength); 1.1122 + *pErrorCode=U_MEMORY_ALLOCATION_ERROR; 1.1123 + if(tempTable.resFlags!=stackResFlags) { 1.1124 + uprv_free(tempTable.resFlags); 1.1125 + } 1.1126 + return 0; 1.1127 + } 1.1128 + tempTable.resort=(int32_t *)(tempTable.rows+maxTableLength); 1.1129 + } 1.1130 + 1.1131 + /* swap the resources */ 1.1132 + ures_swapResource(ds, inBundle, outBundle, rootRes, NULL, &tempTable, pErrorCode); 1.1133 + if(U_FAILURE(*pErrorCode)) { 1.1134 + udata_printError(ds, "ures_swapResource(root res=%08x) failed\n", 1.1135 + rootRes); 1.1136 + } 1.1137 + 1.1138 + if(tempTable.rows!=rows) { 1.1139 + uprv_free(tempTable.rows); 1.1140 + } 1.1141 + if(tempTable.resFlags!=stackResFlags) { 1.1142 + uprv_free(tempTable.resFlags); 1.1143 + } 1.1144 + 1.1145 + /* swap the root resource and indexes */ 1.1146 + ds->swapArray32(ds, inBundle, keysBottom*4, outBundle, pErrorCode); 1.1147 + } 1.1148 + 1.1149 + return headerSize+4*top; 1.1150 +}