]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/uresdata.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / common / uresdata.c
1 /*
2 *******************************************************************************
3 * *
4 * Copyright (C) 1999-2004, International Business Machines Corporation *
5 * and others. All Rights Reserved. *
6 * *
7 *******************************************************************************
8 * file name: uresdata.c
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 1999dec08
14 * created by: Markus W. Scherer
15 * Modification History:
16 *
17 * Date Name Description
18 * 06/20/2000 helena OS/400 port changes; mostly typecast.
19 * 06/24/02 weiv Added support for resource sharing
20 */
21
22 #include "unicode/utypes.h"
23 #include "unicode/udata.h"
24 #include "cmemory.h"
25 #include "cstring.h"
26 #include "uarrsort.h"
27 #include "udataswp.h"
28 #include "ucol_swp.h"
29 #include "uresdata.h"
30 #include "uresimp.h"
31
32 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
33
34 /*
35 * Resource access helpers
36 */
37
38 /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
39 #define RES_GET_KEY(pRoot, keyOffset) ((const char *)(pRoot)+(keyOffset))
40 #define URESDATA_ITEM_NOT_FOUND -1
41
42 /*
43 * All the type-access functions assume that
44 * the resource is of the expected type.
45 */
46
47
48 /*
49 * Array functions
50 */
51 static Resource
52 _res_getArrayItem(Resource *pRoot, Resource res, int32_t indexR) {
53 const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
54 if(indexR<*p) {
55 return ((const Resource *)(p))[1+indexR];
56 } else {
57 return RES_BOGUS; /* indexR>itemCount */
58 }
59 }
60
61 /*
62 * Table functions
63 *
64 * Important: the key offsets are 16-bit byte offsets from pRoot,
65 * and the itemCount is one more 16-bit, too.
66 * Thus, there are (count+1) uint16_t values.
67 * In order to 4-align the Resource item values, there is a padding
68 * word if count is even, i.e., there is exactly (~count&1)
69 * 16-bit padding words.
70 *
71 * For Table32, both the count and the key offsets are int32_t's
72 * and need not alignment.
73 */
74 static const char *
75 _res_getTableKey(const Resource *pRoot, const Resource res, int32_t indexS) {
76 const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pRoot, res);
77 if((uint32_t)indexS<(uint32_t)*p) {
78 return RES_GET_KEY(pRoot, p[indexS+1]);
79 } else {
80 return NULL; /* indexS>itemCount */
81 }
82 }
83
84 static const char *
85 _res_getTable32Key(const Resource *pRoot, const Resource res, int32_t indexS) {
86 const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
87 if((uint32_t)indexS<(uint32_t)*p) {
88 return RES_GET_KEY(pRoot, p[indexS+1]);
89 } else {
90 return NULL; /* indexS>itemCount */
91 }
92 }
93
94
95 static Resource
96 _res_getTableItem(const Resource *pRoot, const Resource res, int32_t indexR) {
97 const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pRoot, res);
98 int32_t count=*p;
99 if((uint32_t)indexR<(uint32_t)count) {
100 return ((const Resource *)(p+1+count+(~count&1)))[indexR];
101 } else {
102 return RES_BOGUS; /* indexR>itemCount */
103 }
104 }
105
106 static Resource
107 _res_getTable32Item(const Resource *pRoot, const Resource res, int32_t indexR) {
108 const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
109 int32_t count=*p;
110 if((uint32_t)indexR<(uint32_t)count) {
111 return ((const Resource *)(p+1+count))[indexR];
112 } else {
113 return RES_BOGUS; /* indexR>itemCount */
114 }
115 }
116
117
118 static Resource
119 _res_findTableItem(const Resource *pRoot, const Resource res, const char *key,
120 int32_t *index, const char **realKey) {
121 const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pRoot, res);
122 int32_t i, start, limit;
123
124 limit=*p++; /* number of entries */
125
126 if(limit == 0) { /* this table is empty */
127 *index=URESDATA_ITEM_NOT_FOUND;
128 return RES_BOGUS;
129 }
130
131 /* do a binary search for the key */
132 start=0;
133 while(start<limit-1) {
134 i=(int32_t)((start+limit)/2);
135 if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
136 limit=i;
137 } else {
138 start=i;
139 }
140 }
141
142 /* did we really find it? */
143 if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
144 *index=start;
145 *realKey=RES_GET_KEY(pRoot, p[start]);
146 limit=*(p-1); /* itemCount */
147 return ((const Resource *)(p+limit+(~limit&1)))[start];
148 } else {
149 *index=URESDATA_ITEM_NOT_FOUND;
150 return RES_BOGUS; /* not found */
151 }
152 }
153
154 static Resource
155 _res_findTable32Item(const Resource *pRoot, const Resource res, const char *key,
156 int32_t *index, const char **realKey) {
157 const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
158 int32_t i, start, limit;
159
160 limit=*p++; /* number of entries */
161
162 if(limit == 0) { /* this table is empty */
163 *index=URESDATA_ITEM_NOT_FOUND;
164 return RES_BOGUS;
165 }
166
167 /* do a binary search for the key */
168 start=0;
169 while(start<limit-1) {
170 i=(int32_t)((start+limit)/2);
171 if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
172 limit=i;
173 } else {
174 start=i;
175 }
176 }
177
178 /* did we really find it? */
179 if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
180 *index=start;
181 *realKey=RES_GET_KEY(pRoot, p[start]);
182 limit=*(p-1); /* itemCount */
183 return ((const Resource *)(p+limit))[start];
184 } else {
185 *index=URESDATA_ITEM_NOT_FOUND;
186 return RES_BOGUS; /* not found */
187 }
188 }
189
190 /* helper for res_load() ---------------------------------------------------- */
191
192 static UBool U_CALLCONV
193 isAcceptable(void *context,
194 const char *type, const char *name,
195 const UDataInfo *pInfo) {
196 return (UBool)(
197 pInfo->size>=20 &&
198 pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
199 pInfo->charsetFamily==U_CHARSET_FAMILY &&
200 pInfo->sizeofUChar==U_SIZEOF_UCHAR &&
201 pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */
202 pInfo->dataFormat[1]==0x65 &&
203 pInfo->dataFormat[2]==0x73 &&
204 pInfo->dataFormat[3]==0x42 &&
205 pInfo->formatVersion[0]==1);
206 }
207
208 /* semi-public functions ---------------------------------------------------- */
209
210 U_CFUNC UBool
211 res_load(ResourceData *pResData,
212 const char *path, const char *name, UErrorCode *errorCode) {
213 UResType rootType;
214
215 /* load the ResourceBundle file */
216 pResData->data=udata_openChoice(path, "res", name, isAcceptable, NULL, errorCode);
217 if(U_FAILURE(*errorCode)) {
218 return FALSE;
219 }
220
221 /* get its memory and root resource */
222 pResData->pRoot=(Resource *)udata_getMemory(pResData->data);
223 pResData->rootRes=*pResData->pRoot;
224
225 /* currently, we accept only resources that have a Table as their roots */
226 rootType=RES_GET_TYPE(pResData->rootRes);
227 if(rootType!=URES_TABLE && rootType!=URES_TABLE32) {
228 *errorCode=U_INVALID_FORMAT_ERROR;
229 udata_close(pResData->data);
230 pResData->data=NULL;
231 return FALSE;
232 }
233
234 return TRUE;
235 }
236
237 U_CFUNC void
238 res_unload(ResourceData *pResData) {
239 if(pResData->data!=NULL) {
240 udata_close(pResData->data);
241 pResData->data=NULL;
242 }
243 }
244
245 U_CFUNC const UChar *
246 res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength) {
247 if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_STRING) {
248 const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
249 if (pLength) {
250 *pLength=*p;
251 }
252 return (const UChar *)++p;
253 } else {
254 if (pLength) {
255 *pLength=0;
256 }
257 return NULL;
258 }
259 }
260
261 U_CFUNC const UChar *
262 res_getAlias(const ResourceData *pResData, const Resource res, int32_t *pLength) {
263 if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_ALIAS) {
264 const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
265 if (pLength) {
266 *pLength=*p;
267 }
268 return (const UChar *)++p;
269 } else {
270 if (pLength) {
271 *pLength=0;
272 }
273 return NULL;
274 }
275 }
276
277 U_CFUNC const uint8_t *
278 res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength) {
279 if(res!=RES_BOGUS) {
280 const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
281 *pLength=*p++;
282 if (*pLength == 0) {
283 p = NULL;
284 }
285 return (const uint8_t *)p;
286 } else {
287 *pLength=0;
288 return NULL;
289 }
290 }
291
292
293 U_CFUNC const int32_t *
294 res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength) {
295 if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_INT_VECTOR) {
296 const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
297 *pLength=*p++;
298 if (*pLength == 0) {
299 p = NULL;
300 }
301 return (const int32_t *)p;
302 } else {
303 *pLength=0;
304 return NULL;
305 }
306 }
307
308 U_CFUNC int32_t
309 res_countArrayItems(const ResourceData *pResData, const Resource res) {
310 if(res!=RES_BOGUS) {
311 switch(RES_GET_TYPE(res)) {
312 case URES_STRING:
313 case URES_BINARY:
314 case URES_ALIAS:
315 case URES_INT:
316 case URES_INT_VECTOR:
317 return 1;
318 case URES_ARRAY:
319 case URES_TABLE32: {
320 const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
321 return *p;
322 }
323 case URES_TABLE: {
324 const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pResData->pRoot, res);
325 return *p;
326 }
327 default:
328 break;
329 }
330 }
331 return 0;
332 }
333
334 U_CFUNC Resource
335 res_getResource(const ResourceData *pResData, const char *key) {
336 int32_t index;
337 const char *realKey;
338 if(RES_GET_TYPE(pResData->rootRes)==URES_TABLE) {
339 return _res_findTableItem(pResData->pRoot, pResData->rootRes, key, &index, &realKey);
340 } else {
341 return _res_findTable32Item(pResData->pRoot, pResData->rootRes, key, &index, &realKey);
342 }
343 }
344
345 U_CFUNC Resource
346 res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexR) {
347 return _res_getArrayItem(pResData->pRoot, array, indexR);
348 }
349
350 U_CFUNC Resource
351 res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key) {
352 /* we pass in a path. CollationElements/Sequence or zoneStrings/3/2 etc.
353 * iterates over a path and stops when a scalar resource is found. This
354 * CAN be an alias. Path gets set to the part that has not yet been processed.
355 */
356
357 char *pathP = *path, *nextSepP = *path;
358 char *closeIndex = NULL;
359 Resource t1 = r;
360 Resource t2;
361 int32_t indexR = 0;
362 UResType type = RES_GET_TYPE(t1);
363
364 /* if you come in with an empty path, you'll be getting back the same resource */
365 if(!uprv_strlen(pathP)) {
366 return r;
367 }
368
369 /* one needs to have an aggregate resource in order to search in it */
370 if(!(type == URES_TABLE || type == URES_TABLE32 || type == URES_ARRAY)) {
371 return RES_BOGUS;
372 }
373
374 while(nextSepP && *pathP && t1 != RES_BOGUS &&
375 (type == URES_TABLE || type == URES_TABLE32 || type == URES_ARRAY)
376 ) {
377 /* Iteration stops if: the path has been consumed, we found a non-existing
378 * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
379 */
380 nextSepP = uprv_strchr(pathP, RES_PATH_SEPARATOR);
381 /* if there are more separators, terminate string
382 * and set path to the remaining part of the string
383 */
384 if(nextSepP != NULL) {
385 *nextSepP = 0; /* overwrite the separator with a NUL to terminate the key */
386 *path = nextSepP+1;
387 } else {
388 *path = uprv_strchr(pathP, 0);
389 }
390
391 /* if the resource is a table */
392 /* try the key based access */
393 if(type == URES_TABLE) {
394 t2 = _res_findTableItem(pResData->pRoot, t1, pathP, &indexR, key);
395 if(t2 == RES_BOGUS) {
396 /* if we fail to get the resource by key, maybe we got an index */
397 indexR = uprv_strtol(pathP, &closeIndex, 10);
398 if(closeIndex != pathP) {
399 /* if we indeed have an index, try to get the item by index */
400 t2 = res_getTableItemByIndex(pResData, t1, indexR, key);
401 }
402 }
403 } else if(type == URES_TABLE32) {
404 t2 = _res_findTable32Item(pResData->pRoot, t1, pathP, &indexR, key);
405 if(t2 == RES_BOGUS) {
406 /* if we fail to get the resource by key, maybe we got an index */
407 indexR = uprv_strtol(pathP, &closeIndex, 10);
408 if(closeIndex != pathP) {
409 /* if we indeed have an index, try to get the item by index */
410 t2 = res_getTableItemByIndex(pResData, t1, indexR, key);
411 }
412 }
413 } else if(type == URES_ARRAY) {
414 indexR = uprv_strtol(pathP, &closeIndex, 10);
415 if(closeIndex != pathP) {
416 t2 = _res_getArrayItem(pResData->pRoot, t1, indexR);
417 } else {
418 t2 = RES_BOGUS; /* have an array, but don't have a valid index */
419 }
420 *key = NULL;
421 } else { /* can't do much here, except setting t2 to bogus */
422 t2 = RES_BOGUS;
423 }
424 t1 = t2;
425 type = RES_GET_TYPE(t1);
426 /* position pathP to next resource key/index */
427 pathP = *path;
428 }
429
430 return t1;
431 }
432
433 U_CFUNC Resource
434 res_getTableItemByKey(const ResourceData *pResData, Resource table,
435 int32_t *indexR, const char **key) {
436 if(key != NULL && *key != NULL) {
437 if(RES_GET_TYPE(table)==URES_TABLE) {
438 return _res_findTableItem(pResData->pRoot, table, *key, indexR, key);
439 } else {
440 return _res_findTable32Item(pResData->pRoot, table, *key, indexR, key);
441 }
442 } else {
443 return RES_BOGUS;
444 }
445 }
446
447 U_CFUNC Resource
448 res_getTableItemByIndex(const ResourceData *pResData, Resource table,
449 int32_t indexR, const char **key) {
450 if(indexR>-1) {
451 if(RES_GET_TYPE(table)==URES_TABLE) {
452 if(key != NULL) {
453 *key = _res_getTableKey(pResData->pRoot, table, indexR);
454 }
455 return _res_getTableItem(pResData->pRoot, table, indexR);
456 } else {
457 if(key != NULL) {
458 *key = _res_getTable32Key(pResData->pRoot, table, indexR);
459 }
460 return _res_getTable32Item(pResData->pRoot, table, indexR);
461 }
462 } else {
463 return RES_BOGUS;
464 }
465 }
466
467 /* resource bundle swapping ------------------------------------------------- */
468
469 /*
470 * Need to always enumerate the entire item tree,
471 * track the lowest address of any item to use as the limit for char keys[],
472 * track the highest address of any item to return the size of the data.
473 *
474 * We should have thought of storing those in the data...
475 * It is possible to extend the data structure by putting additional values
476 * in places that are inaccessible by ordinary enumeration of the item tree.
477 * For example, additional integers could be stored at the beginning or
478 * end of the key strings; this could be indicated by a minor version number,
479 * and the data swapping would have to know about these values.
480 *
481 * The data structure does not forbid keys to be shared, so we must swap
482 * all keys once instead of each key when it is referenced.
483 *
484 * These swapping functions assume that a resource bundle always has a length
485 * that is a multiple of 4 bytes.
486 * Currently, this is trivially true because genrb writes bundle tree leaves
487 * physically first, before their branches, so that the root table with its
488 * array of resource items (uint32_t values) is always last.
489 */
490
491 /* definitions for table sorting ------------------------ */
492
493 /*
494 * row of a temporary array
495 *
496 * gets platform-endian key string indexes and sorting indexes;
497 * after sorting this array by keys, the actual key/value arrays are permutated
498 * according to the sorting indexes
499 */
500 typedef struct Row {
501 int32_t keyIndex, sortIndex;
502 } Row;
503
504 static int32_t
505 ures_compareRows(const void *context, const void *left, const void *right) {
506 const char *keyChars=(const char *)context;
507 return (int32_t)uprv_strcmp(keyChars+((const Row *)left)->keyIndex,
508 keyChars+((const Row *)right)->keyIndex);
509 }
510
511 typedef struct TempTable {
512 const char *keyChars;
513 Row *rows;
514 int32_t *resort;
515 } TempTable;
516
517 enum {
518 STACK_ROW_CAPACITY=200
519 };
520
521 /* binary data with known formats is swapped too */
522 typedef enum UResSpecialType {
523 URES_NO_SPECIAL_TYPE,
524 URES_COLLATION_BINARY,
525 URES_SPECIAL_TYPE_COUNT
526 } UResSpecialType;
527
528 /* resource table key for collation binaries: "%%CollationBin" */
529 static const UChar gCollationBinKey[]={
530 0x25, 0x25,
531 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
532 0x42, 0x69, 0x6e,
533 0
534 };
535
536 /*
537 * preflight one resource item and set bottom and top values;
538 * length, bottom, and top count Resource item offsets (4 bytes each), not bytes
539 */
540 static void
541 ures_preflightResource(const UDataSwapper *ds,
542 const Resource *inBundle, int32_t length,
543 Resource res,
544 int32_t *pBottom, int32_t *pTop, int32_t *pMaxTableLength,
545 UErrorCode *pErrorCode) {
546 const Resource *p;
547 int32_t offset;
548
549 if(res==0 || RES_GET_TYPE(res)==URES_INT) {
550 /* empty string or integer, nothing to do */
551 return;
552 }
553
554 /* all other types use an offset to point to their data */
555 offset=(int32_t)RES_GET_OFFSET(res);
556 if(0<=length && length<=offset) {
557 udata_printError(ds, "ures_preflightResource(res=%08x) resource offset exceeds bundle length %d\n",
558 res, length);
559 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
560 return;
561 } else if(offset<*pBottom) {
562 *pBottom=offset;
563 }
564 p=inBundle+offset;
565
566 switch(RES_GET_TYPE(res)) {
567 case URES_ALIAS:
568 /* physically same value layout as string, fall through */
569 case URES_STRING:
570 /* top=offset+1+(string length +1)/2 rounded up */
571 offset+=1+((udata_readInt32(ds, (int32_t)*p)+1)+1)/2;
572 break;
573 case URES_BINARY:
574 /* top=offset+1+(binary length)/4 rounded up */
575 offset+=1+(udata_readInt32(ds, (int32_t)*p)+3)/4;
576 break;
577 case URES_TABLE:
578 case URES_TABLE32:
579 {
580 Resource item;
581 int32_t i, count;
582
583 if(RES_GET_TYPE(res)==URES_TABLE) {
584 /* get table item count */
585 const uint16_t *pKey16=(const uint16_t *)p;
586 count=ds->readUInt16(*pKey16++);
587
588 /* top=((1+ table item count)/2 rounded up)+(table item count) */
589 offset+=((1+count)+1)/2;
590 } else {
591 /* get table item count */
592 const int32_t *pKey32=(const int32_t *)p;
593 count=udata_readInt32(ds, *pKey32++);
594
595 /* top=(1+ table item count)+(table item count) */
596 offset+=1+count;
597 }
598
599 if(count>*pMaxTableLength) {
600 *pMaxTableLength=count;
601 }
602
603 p=inBundle+offset; /* pointer to table resources */
604 offset+=count;
605
606 /* recurse */
607 if(offset<=length) {
608 for(i=0; i<count; ++i) {
609 item=ds->readUInt32(*p++);
610 ures_preflightResource(ds, inBundle, length, item,
611 pBottom, pTop, pMaxTableLength,
612 pErrorCode);
613 if(U_FAILURE(*pErrorCode)) {
614 udata_printError(ds, "ures_preflightResource(table res=%08x)[%d].recurse(%08x) failed - %s\n",
615 res, i, item, u_errorName(*pErrorCode));
616 break;
617 }
618 }
619 }
620 }
621 break;
622 case URES_ARRAY:
623 {
624 Resource item;
625 int32_t i, count;
626
627 /* top=offset+1+(array length) */
628 count=udata_readInt32(ds, (int32_t)*p++);
629 offset+=1+count;
630
631 /* recurse */
632 if(offset<=length) {
633 for(i=0; i<count; ++i) {
634 item=ds->readUInt32(*p++);
635 ures_preflightResource(ds, inBundle, length, item,
636 pBottom, pTop, pMaxTableLength,
637 pErrorCode);
638 if(U_FAILURE(*pErrorCode)) {
639 udata_printError(ds, "ures_preflightResource(array res=%08x)[%d].recurse(%08x) failed - %s\n",
640 res, i, item, u_errorName(*pErrorCode));
641 break;
642 }
643 }
644 }
645 }
646 break;
647 case URES_INT_VECTOR:
648 /* top=offset+1+(vector length) */
649 offset+=1+udata_readInt32(ds, (int32_t)*p);
650 break;
651 default:
652 /* also catches RES_BOGUS */
653 udata_printError(ds, "ures_preflightResource(res=%08x) unknown resource type\n", res);
654 *pErrorCode=U_UNSUPPORTED_ERROR;
655 break;
656 }
657
658 if(U_FAILURE(*pErrorCode)) {
659 /* nothing to do */
660 } else if(0<=length && length<offset) {
661 udata_printError(ds, "ures_preflightResource(res=%08x) resource limit exceeds bundle length %d\n",
662 res, length);
663 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
664 } else if(offset>*pTop) {
665 *pTop=offset;
666 }
667 }
668
669 /*
670 * swap one resource item
671 * since preflighting succeeded, we need not check offsets against length any more
672 */
673 static void
674 ures_swapResource(const UDataSwapper *ds,
675 const Resource *inBundle, Resource *outBundle,
676 Resource res, /* caller swaps res itself */
677 UResSpecialType specialType,
678 TempTable *pTempTable,
679 UErrorCode *pErrorCode) {
680 const Resource *p;
681 Resource *q;
682 int32_t offset, count;
683
684 if(res==0 || RES_GET_TYPE(res)==URES_INT) {
685 /* empty string or integer, nothing to do */
686 return;
687 }
688
689 /* all other types use an offset to point to their data */
690 offset=(int32_t)RES_GET_OFFSET(res);
691 p=inBundle+offset;
692 q=outBundle+offset;
693
694 switch(RES_GET_TYPE(res)) {
695 case URES_ALIAS:
696 /* physically same value layout as string, fall through */
697 case URES_STRING:
698 count=udata_readInt32(ds, (int32_t)*p);
699 /* swap length */
700 ds->swapArray32(ds, p, 4, q, pErrorCode);
701 /* swap each UChar (the terminating NUL would not change) */
702 ds->swapArray16(ds, p+1, 2*count, q+1, pErrorCode);
703 break;
704 case URES_BINARY:
705 count=udata_readInt32(ds, (int32_t)*p);
706 /* swap length */
707 ds->swapArray32(ds, p, 4, q, pErrorCode);
708 /* no need to swap or copy bytes - ures_swap() copied them all */
709
710 /* swap known formats */
711 if(specialType==URES_COLLATION_BINARY) {
712 #if !UCONFIG_NO_COLLATION
713 ucol_swapBinary(ds, p+1, count, q+1, pErrorCode);
714 #endif
715 }
716 break;
717 case URES_TABLE:
718 case URES_TABLE32:
719 {
720 const uint16_t *pKey16;
721 uint16_t *qKey16;
722
723 const int32_t *pKey32;
724 int32_t *qKey32;
725
726 Resource item;
727 int32_t i, oldIndex;
728
729 if(RES_GET_TYPE(res)==URES_TABLE) {
730 /* get table item count */
731 pKey16=(const uint16_t *)p;
732 qKey16=(uint16_t *)q;
733 count=ds->readUInt16(*pKey16);
734
735 pKey32=qKey32=NULL;
736
737 /* swap count */
738 ds->swapArray16(ds, pKey16++, 2, qKey16++, pErrorCode);
739
740 offset+=((1+count)+1)/2;
741 } else {
742 /* get table item count */
743 pKey32=(const int32_t *)p;
744 qKey32=(int32_t *)q;
745 count=udata_readInt32(ds, *pKey32);
746
747 pKey16=qKey16=NULL;
748
749 /* swap count */
750 ds->swapArray32(ds, pKey32++, 4, qKey32++, pErrorCode);
751
752 offset+=1+count;
753 }
754
755 if(count==0) {
756 break;
757 }
758
759 p=inBundle+offset; /* pointer to table resources */
760 q=outBundle+offset;
761
762 /* recurse */
763 for(i=0; i<count; ++i) {
764 /*
765 * detect a collation binary that is to be swapped via
766 * ds->compareInvChars(ds, outData+readUInt16(pKey[i]), "%%CollationBin")
767 * etc.
768 *
769 * use some UDataSwapFn pointer from somewhere for collation swapping
770 * because the common library cannot directly call into the i18n library
771 */
772 if(0==ds->compareInvChars(ds,
773 ((const char *)outBundle)+
774 (pKey16!=NULL ?
775 ds->readUInt16(pKey16[i]) :
776 udata_readInt32(ds, pKey32[i])),
777 -1,
778 gCollationBinKey, LENGTHOF(gCollationBinKey)-1)
779 ) {
780 specialType=URES_COLLATION_BINARY;
781 } else {
782 specialType=URES_NO_SPECIAL_TYPE;
783 }
784
785 item=ds->readUInt32(p[i]);
786 ures_swapResource(ds, inBundle, outBundle, item, specialType, pTempTable, pErrorCode);
787 if(U_FAILURE(*pErrorCode)) {
788 udata_printError(ds, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed - %s\n",
789 res, i, item, u_errorName(*pErrorCode));
790 return;
791 }
792 }
793
794 if(ds->inCharset==ds->outCharset) {
795 /* no need to sort, just swap the offset/value arrays */
796 if(pKey16!=NULL) {
797 ds->swapArray16(ds, pKey16, count*2, qKey16, pErrorCode);
798 ds->swapArray32(ds, p, count*4, q, pErrorCode);
799 } else {
800 /* swap key offsets and items as one array */
801 ds->swapArray32(ds, pKey32, count*2*4, qKey32, pErrorCode);
802 }
803 break;
804 }
805
806 /*
807 * We need to sort tables by outCharset key strings because they
808 * sort differently for different charset families.
809 * ures_swap() already set pTempTable->keyChars appropriately.
810 * First we set up a temporary table with the key indexes and
811 * sorting indexes and sort that.
812 * Then we permutate and copy/swap the actual values.
813 */
814 if(pKey16!=NULL) {
815 for(i=0; i<count; ++i) {
816 pTempTable->rows[i].keyIndex=ds->readUInt16(pKey16[i]);
817 pTempTable->rows[i].sortIndex=i;
818 }
819 } else {
820 for(i=0; i<count; ++i) {
821 pTempTable->rows[i].keyIndex=udata_readInt32(ds, pKey32[i]);
822 pTempTable->rows[i].sortIndex=i;
823 }
824 }
825 uprv_sortArray(pTempTable->rows, count, sizeof(Row),
826 ures_compareRows, pTempTable->keyChars,
827 FALSE, pErrorCode);
828 if(U_FAILURE(*pErrorCode)) {
829 udata_printError(ds, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed - %s\n",
830 res, count, u_errorName(*pErrorCode));
831 return;
832 }
833
834 /*
835 * copy/swap/permutate items
836 *
837 * If we swap in-place, then the permutation must use another
838 * temporary array (pTempTable->resort)
839 * before the results are copied to the outBundle.
840 */
841 /* keys */
842 if(pKey16!=NULL) {
843 uint16_t *rKey16;
844
845 if(pKey16!=qKey16) {
846 rKey16=qKey16;
847 } else {
848 rKey16=(uint16_t *)pTempTable->resort;
849 }
850 for(i=0; i<count; ++i) {
851 oldIndex=pTempTable->rows[i].sortIndex;
852 ds->swapArray16(ds, pKey16+oldIndex, 2, rKey16+i, pErrorCode);
853 }
854 if(qKey16!=rKey16) {
855 uprv_memcpy(qKey16, rKey16, 2*count);
856 }
857 } else {
858 int32_t *rKey32;
859
860 if(pKey32!=qKey32) {
861 rKey32=qKey32;
862 } else {
863 rKey32=pTempTable->resort;
864 }
865 for(i=0; i<count; ++i) {
866 oldIndex=pTempTable->rows[i].sortIndex;
867 ds->swapArray32(ds, pKey32+oldIndex, 4, rKey32+i, pErrorCode);
868 }
869 if(qKey32!=rKey32) {
870 uprv_memcpy(qKey32, rKey32, 4*count);
871 }
872 }
873
874 /* resources */
875 {
876 Resource *r;
877
878
879 if(p!=q) {
880 r=q;
881 } else {
882 r=(Resource *)pTempTable->resort;
883 }
884 for(i=0; i<count; ++i) {
885 oldIndex=pTempTable->rows[i].sortIndex;
886 ds->swapArray32(ds, p+oldIndex, 4, r+i, pErrorCode);
887 }
888 if(q!=r) {
889 uprv_memcpy(q, r, 4*count);
890 }
891 }
892 }
893 break;
894 case URES_ARRAY:
895 {
896 Resource item;
897 int32_t i;
898
899 count=udata_readInt32(ds, (int32_t)*p);
900 /* swap length */
901 ds->swapArray32(ds, p++, 4, q++, pErrorCode);
902
903 /* recurse */
904 for(i=0; i<count; ++i) {
905 item=ds->readUInt32(p[i]);
906 ures_swapResource(ds, inBundle, outBundle, item, URES_NO_SPECIAL_TYPE, pTempTable, pErrorCode);
907 if(U_FAILURE(*pErrorCode)) {
908 udata_printError(ds, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed - %s\n",
909 res, i, item, u_errorName(*pErrorCode));
910 return;
911 }
912 }
913
914 /* swap items */
915 ds->swapArray32(ds, p, 4*count, q, pErrorCode);
916 }
917 break;
918 case URES_INT_VECTOR:
919 count=udata_readInt32(ds, (int32_t)*p);
920 /* swap length and each integer */
921 ds->swapArray32(ds, p, 4*(1+count), q, pErrorCode);
922 break;
923 default:
924 /* also catches RES_BOGUS */
925 *pErrorCode=U_UNSUPPORTED_ERROR;
926 break;
927 }
928 }
929
930 U_CAPI int32_t U_EXPORT2
931 ures_swap(const UDataSwapper *ds,
932 const void *inData, int32_t length, void *outData,
933 UErrorCode *pErrorCode) {
934 const UDataInfo *pInfo;
935 const Resource *inBundle;
936 Resource rootRes;
937 int32_t headerSize, maxTableLength;
938
939 Row rows[STACK_ROW_CAPACITY];
940 int32_t resort[STACK_ROW_CAPACITY];
941 TempTable tempTable;
942
943 /* the following integers count Resource item offsets (4 bytes each), not bytes */
944 int32_t bundleLength, stringsBottom, bottom, top;
945
946 /* udata_swapDataHeader checks the arguments */
947 headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
948 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
949 return 0;
950 }
951
952 /* check data format and format version */
953 pInfo=(const UDataInfo *)((const char *)inData+4);
954 if(!(
955 pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */
956 pInfo->dataFormat[1]==0x65 &&
957 pInfo->dataFormat[2]==0x73 &&
958 pInfo->dataFormat[3]==0x42 &&
959 pInfo->formatVersion[0]==1
960 )) {
961 udata_printError(ds, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not a resource bundle\n",
962 pInfo->dataFormat[0], pInfo->dataFormat[1],
963 pInfo->dataFormat[2], pInfo->dataFormat[3],
964 pInfo->formatVersion[0]);
965 *pErrorCode=U_UNSUPPORTED_ERROR;
966 return 0;
967 }
968
969 /* a resource bundle must contain at least one resource item */
970 if(length<0) {
971 bundleLength=-1;
972 } else {
973 bundleLength=(length-headerSize)/4;
974
975 /* formatVersion 1.1 must have a root item and at least 5 indexes */
976 if( bundleLength<
977 (pInfo->formatVersion[1]==0 ? 1 : 1+5)
978 ) {
979 udata_printError(ds, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
980 length-headerSize);
981 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
982 return 0;
983 }
984 }
985
986 inBundle=(const Resource *)((const char *)inData+headerSize);
987 rootRes=ds->readUInt32(*inBundle);
988
989 if(pInfo->formatVersion[1]==0) {
990 /* preflight to get the bottom, top and maxTableLength values */
991 stringsBottom=1; /* just past root */
992 bottom=0x7fffffff;
993 top=maxTableLength=0;
994 ures_preflightResource(ds, inBundle, bundleLength, rootRes,
995 &bottom, &top, &maxTableLength,
996 pErrorCode);
997 if(U_FAILURE(*pErrorCode)) {
998 udata_printError(ds, "ures_preflightResource(root res=%08x) failed - %s\n",
999 rootRes, u_errorName(*pErrorCode));
1000 return 0;
1001 }
1002 } else {
1003 /* formatVersion 1.1 adds the indexes[] array */
1004 const int32_t *inIndexes;
1005
1006 inIndexes=(const int32_t *)(inBundle+1);
1007
1008 stringsBottom=1+udata_readInt32(ds, inIndexes[URES_INDEX_LENGTH]);
1009 bottom=udata_readInt32(ds, inIndexes[URES_INDEX_STRINGS_TOP]);
1010 top=udata_readInt32(ds, inIndexes[URES_INDEX_BUNDLE_TOP]);
1011 maxTableLength=udata_readInt32(ds, inIndexes[URES_INDEX_MAX_TABLE_LENGTH]);
1012
1013 if(0<=bundleLength && bundleLength<top) {
1014 udata_printError(ds, "ures_swap(): resource top %d exceeds bundle length %d\n",
1015 top, bundleLength);
1016 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
1017 return 0;
1018 }
1019 }
1020
1021 if(length>=0) {
1022 Resource *outBundle=(Resource *)((char *)outData+headerSize);
1023
1024 /* copy the bundle for binary and inaccessible data */
1025 if(inData!=outData) {
1026 uprv_memcpy(outBundle, inBundle, 4*top);
1027 }
1028
1029 /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
1030 udata_swapInvStringBlock(ds, inBundle+stringsBottom, 4*(bottom-stringsBottom),
1031 outBundle+stringsBottom, pErrorCode);
1032 if(U_FAILURE(*pErrorCode)) {
1033 udata_printError(ds, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed - %s\n", 4*(bottom-1),
1034 u_errorName(*pErrorCode));
1035 return 0;
1036 }
1037
1038 /* allocate the temporary table for sorting resource tables */
1039 tempTable.keyChars=(const char *)outBundle; /* sort by outCharset */
1040 if(maxTableLength<=STACK_ROW_CAPACITY) {
1041 tempTable.rows=rows;
1042 tempTable.resort=resort;
1043 } else {
1044 tempTable.rows=(Row *)uprv_malloc(maxTableLength*sizeof(Row)+maxTableLength*4);
1045 if(tempTable.rows==NULL) {
1046 udata_printError(ds, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
1047 maxTableLength);
1048 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1049 return 0;
1050 }
1051 tempTable.resort=(int32_t *)(tempTable.rows+maxTableLength);
1052 }
1053
1054 /* swap the resources */
1055 ures_swapResource(ds, inBundle, outBundle, rootRes, URES_NO_SPECIAL_TYPE, &tempTable, pErrorCode);
1056 if(U_FAILURE(*pErrorCode)) {
1057 udata_printError(ds, "ures_swapResource(root res=%08x) failed - %s\n",
1058 rootRes, u_errorName(*pErrorCode));
1059 }
1060
1061 if(tempTable.rows!=rows) {
1062 uprv_free(tempTable.rows);
1063 }
1064
1065 /* swap the root resource and indexes */
1066 ds->swapArray32(ds, inBundle, stringsBottom*4, outBundle, pErrorCode);
1067 }
1068
1069 return headerSize+4*top;
1070 }