]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ******************************************************************************* | |
2ca993e8 | 5 | * Copyright (C) 1999-2016, International Business Machines Corporation |
57a6839d | 6 | * and others. All Rights Reserved. |
b75a7d8f | 7 | ******************************************************************************* |
2ca993e8 | 8 | * file name: uresdata.cpp |
f3c0d7a5 | 9 | * encoding: UTF-8 |
b75a7d8f A |
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" | |
b75a7d8f | 23 | #include "unicode/udata.h" |
729e4ab9 | 24 | #include "unicode/ustring.h" |
4388f060 | 25 | #include "unicode/utf16.h" |
374ca955 A |
26 | #include "cmemory.h" |
27 | #include "cstring.h" | |
2ca993e8 | 28 | #include "resource.h" |
374ca955 | 29 | #include "uarrsort.h" |
2ca993e8 | 30 | #include "uassert.h" |
374ca955 | 31 | #include "ucol_swp.h" |
2ca993e8 | 32 | #include "udataswp.h" |
729e4ab9 | 33 | #include "uinvchar.h" |
b75a7d8f A |
34 | #include "uresdata.h" |
35 | #include "uresimp.h" | |
340931cb | 36 | #include "utracimp.h" |
b75a7d8f A |
37 | |
38 | /* | |
39 | * Resource access helpers | |
40 | */ | |
41 | ||
42 | /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */ | |
729e4ab9 A |
43 | #define RES_GET_KEY16(pResData, keyOffset) \ |
44 | ((keyOffset)<(pResData)->localKeyLimit ? \ | |
45 | (const char *)(pResData)->pRoot+(keyOffset) : \ | |
46 | (pResData)->poolBundleKeys+(keyOffset)-(pResData)->localKeyLimit) | |
b75a7d8f | 47 | |
729e4ab9 A |
48 | #define RES_GET_KEY32(pResData, keyOffset) \ |
49 | ((keyOffset)>=0 ? \ | |
50 | (const char *)(pResData)->pRoot+(keyOffset) : \ | |
51 | (pResData)->poolBundleKeys+((keyOffset)&0x7fffffff)) | |
b75a7d8f | 52 | |
729e4ab9 | 53 | #define URESDATA_ITEM_NOT_FOUND -1 |
b75a7d8f | 54 | |
729e4ab9 A |
55 | /* empty resources, returned when the resource offset is 0 */ |
56 | static const uint16_t gEmpty16=0; | |
4388f060 A |
57 | |
58 | static const struct { | |
59 | int32_t length; | |
60 | int32_t res; | |
61 | } gEmpty32={ 0, 0 }; | |
62 | ||
729e4ab9 A |
63 | static const struct { |
64 | int32_t length; | |
65 | UChar nul; | |
66 | UChar pad; | |
67 | } gEmptyString={ 0, 0, 0 }; | |
b75a7d8f A |
68 | |
69 | /* | |
729e4ab9 A |
70 | * All the type-access functions assume that |
71 | * the resource is of the expected type. | |
b75a7d8f | 72 | */ |
374ca955 | 73 | |
729e4ab9 A |
74 | static int32_t |
75 | _res_findTableItem(const ResourceData *pResData, const uint16_t *keyOffsets, int32_t length, | |
76 | const char *key, const char **realKey) { | |
77 | const char *tableKey; | |
78 | int32_t mid, start, limit; | |
73c04bcf | 79 | int result; |
b75a7d8f | 80 | |
729e4ab9 A |
81 | /* do a binary search for the key */ |
82 | start=0; | |
83 | limit=length; | |
84 | while(start<limit) { | |
85 | mid = (start + limit) / 2; | |
86 | tableKey = RES_GET_KEY16(pResData, keyOffsets[mid]); | |
87 | if (pResData->useNativeStrcmp) { | |
88 | result = uprv_strcmp(key, tableKey); | |
89 | } else { | |
90 | result = uprv_compareInvCharsAsAscii(key, tableKey); | |
91 | } | |
92 | if (result < 0) { | |
93 | limit = mid; | |
94 | } else if (result > 0) { | |
95 | start = mid + 1; | |
96 | } else { | |
97 | /* We found it! */ | |
98 | *realKey=tableKey; | |
99 | return mid; | |
b75a7d8f A |
100 | } |
101 | } | |
729e4ab9 | 102 | return URESDATA_ITEM_NOT_FOUND; /* not found or table is empty. */ |
b75a7d8f A |
103 | } |
104 | ||
729e4ab9 A |
105 | static int32_t |
106 | _res_findTable32Item(const ResourceData *pResData, const int32_t *keyOffsets, int32_t length, | |
107 | const char *key, const char **realKey) { | |
108 | const char *tableKey; | |
73c04bcf | 109 | int32_t mid, start, limit; |
73c04bcf | 110 | int result; |
b75a7d8f | 111 | |
729e4ab9 A |
112 | /* do a binary search for the key */ |
113 | start=0; | |
114 | limit=length; | |
115 | while(start<limit) { | |
116 | mid = (start + limit) / 2; | |
117 | tableKey = RES_GET_KEY32(pResData, keyOffsets[mid]); | |
118 | if (pResData->useNativeStrcmp) { | |
119 | result = uprv_strcmp(key, tableKey); | |
120 | } else { | |
121 | result = uprv_compareInvCharsAsAscii(key, tableKey); | |
122 | } | |
123 | if (result < 0) { | |
124 | limit = mid; | |
125 | } else if (result > 0) { | |
126 | start = mid + 1; | |
127 | } else { | |
128 | /* We found it! */ | |
129 | *realKey=tableKey; | |
130 | return mid; | |
b75a7d8f A |
131 | } |
132 | } | |
729e4ab9 | 133 | return URESDATA_ITEM_NOT_FOUND; /* not found or table is empty. */ |
b75a7d8f A |
134 | } |
135 | ||
136 | /* helper for res_load() ---------------------------------------------------- */ | |
137 | ||
138 | static UBool U_CALLCONV | |
139 | isAcceptable(void *context, | |
2ca993e8 | 140 | const char * /*type*/, const char * /*name*/, |
b75a7d8f | 141 | const UDataInfo *pInfo) { |
73c04bcf | 142 | uprv_memcpy(context, pInfo->formatVersion, 4); |
b75a7d8f A |
143 | return (UBool)( |
144 | pInfo->size>=20 && | |
145 | pInfo->isBigEndian==U_IS_BIG_ENDIAN && | |
146 | pInfo->charsetFamily==U_CHARSET_FAMILY && | |
147 | pInfo->sizeofUChar==U_SIZEOF_UCHAR && | |
148 | pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */ | |
149 | pInfo->dataFormat[1]==0x65 && | |
150 | pInfo->dataFormat[2]==0x73 && | |
151 | pInfo->dataFormat[3]==0x42 && | |
2ca993e8 | 152 | (1<=pInfo->formatVersion[0] && pInfo->formatVersion[0]<=3)); |
b75a7d8f A |
153 | } |
154 | ||
155 | /* semi-public functions ---------------------------------------------------- */ | |
156 | ||
729e4ab9 A |
157 | static void |
158 | res_init(ResourceData *pResData, | |
159 | UVersionInfo formatVersion, const void *inBytes, int32_t length, | |
160 | UErrorCode *errorCode) { | |
374ca955 A |
161 | UResType rootType; |
162 | ||
729e4ab9 A |
163 | /* get the root resource */ |
164 | pResData->pRoot=(const int32_t *)inBytes; | |
165 | pResData->rootRes=(Resource)*pResData->pRoot; | |
166 | pResData->p16BitUnits=&gEmpty16; | |
b75a7d8f | 167 | |
729e4ab9 A |
168 | /* formatVersion 1.1 must have a root item and at least 5 indexes */ |
169 | if(length>=0 && (length/4)<((formatVersion[0]==1 && formatVersion[1]==0) ? 1 : 1+5)) { | |
170 | *errorCode=U_INVALID_FORMAT_ERROR; | |
171 | res_unload(pResData); | |
172 | return; | |
173 | } | |
b75a7d8f A |
174 | |
175 | /* currently, we accept only resources that have a Table as their roots */ | |
51004dcb | 176 | rootType=(UResType)RES_GET_TYPE(pResData->rootRes); |
729e4ab9 | 177 | if(!URES_IS_TABLE(rootType)) { |
374ca955 | 178 | *errorCode=U_INVALID_FORMAT_ERROR; |
729e4ab9 A |
179 | res_unload(pResData); |
180 | return; | |
b75a7d8f A |
181 | } |
182 | ||
729e4ab9 A |
183 | if(formatVersion[0]==1 && formatVersion[1]==0) { |
184 | pResData->localKeyLimit=0x10000; /* greater than any 16-bit key string offset */ | |
185 | } else { | |
73c04bcf | 186 | /* bundles with formatVersion 1.1 and later contain an indexes[] array */ |
729e4ab9 A |
187 | const int32_t *indexes=pResData->pRoot+1; |
188 | int32_t indexLength=indexes[URES_INDEX_LENGTH]&0xff; | |
189 | if(indexLength<=URES_INDEX_MAX_TABLE_LENGTH) { | |
190 | *errorCode=U_INVALID_FORMAT_ERROR; | |
191 | res_unload(pResData); | |
192 | return; | |
193 | } | |
194 | if( length>=0 && | |
195 | (length<((1+indexLength)<<2) || | |
196 | length<(indexes[URES_INDEX_BUNDLE_TOP]<<2)) | |
197 | ) { | |
198 | *errorCode=U_INVALID_FORMAT_ERROR; | |
199 | res_unload(pResData); | |
200 | return; | |
201 | } | |
202 | if(indexes[URES_INDEX_KEYS_TOP]>(1+indexLength)) { | |
203 | pResData->localKeyLimit=indexes[URES_INDEX_KEYS_TOP]<<2; | |
73c04bcf | 204 | } |
2ca993e8 A |
205 | if(formatVersion[0]>=3) { |
206 | // In formatVersion 1, the indexLength took up this whole int. | |
207 | // In version 2, bits 31..8 were reserved and always 0. | |
208 | // In version 3, they contain bits 23..0 of the poolStringIndexLimit. | |
209 | // Bits 27..24 are in indexes[URES_INDEX_ATTRIBUTES] bits 15..12. | |
210 | pResData->poolStringIndexLimit=(int32_t)((uint32_t)indexes[URES_INDEX_LENGTH]>>8); | |
211 | } | |
729e4ab9 A |
212 | if(indexLength>URES_INDEX_ATTRIBUTES) { |
213 | int32_t att=indexes[URES_INDEX_ATTRIBUTES]; | |
214 | pResData->noFallback=(UBool)(att&URES_ATT_NO_FALLBACK); | |
215 | pResData->isPoolBundle=(UBool)((att&URES_ATT_IS_POOL_BUNDLE)!=0); | |
216 | pResData->usesPoolBundle=(UBool)((att&URES_ATT_USES_POOL_BUNDLE)!=0); | |
2ca993e8 A |
217 | pResData->poolStringIndexLimit|=(att&0xf000)<<12; // bits 15..12 -> 27..24 |
218 | pResData->poolStringIndex16Limit=(int32_t)((uint32_t)att>>16); | |
729e4ab9 A |
219 | } |
220 | if((pResData->isPoolBundle || pResData->usesPoolBundle) && indexLength<=URES_INDEX_POOL_CHECKSUM) { | |
221 | *errorCode=U_INVALID_FORMAT_ERROR; | |
222 | res_unload(pResData); | |
223 | return; | |
224 | } | |
225 | if( indexLength>URES_INDEX_16BIT_TOP && | |
226 | indexes[URES_INDEX_16BIT_TOP]>indexes[URES_INDEX_KEYS_TOP] | |
227 | ) { | |
228 | pResData->p16BitUnits=(const uint16_t *)(pResData->pRoot+indexes[URES_INDEX_KEYS_TOP]); | |
229 | } | |
230 | } | |
231 | ||
232 | if(formatVersion[0]==1 || U_CHARSET_FAMILY==U_ASCII_FAMILY) { | |
233 | /* | |
234 | * formatVersion 1: compare key strings in native-charset order | |
235 | * formatVersion 2 and up: compare key strings in ASCII order | |
236 | */ | |
237 | pResData->useNativeStrcmp=TRUE; | |
238 | } | |
239 | } | |
240 | ||
241 | U_CAPI void U_EXPORT2 | |
242 | res_read(ResourceData *pResData, | |
243 | const UDataInfo *pInfo, const void *inBytes, int32_t length, | |
244 | UErrorCode *errorCode) { | |
245 | UVersionInfo formatVersion; | |
246 | ||
247 | uprv_memset(pResData, 0, sizeof(ResourceData)); | |
248 | if(U_FAILURE(*errorCode)) { | |
249 | return; | |
250 | } | |
251 | if(!isAcceptable(formatVersion, NULL, NULL, pInfo)) { | |
252 | *errorCode=U_INVALID_FORMAT_ERROR; | |
253 | return; | |
254 | } | |
255 | res_init(pResData, formatVersion, inBytes, length, errorCode); | |
256 | } | |
257 | ||
258 | U_CFUNC void | |
259 | res_load(ResourceData *pResData, | |
260 | const char *path, const char *name, UErrorCode *errorCode) { | |
261 | UVersionInfo formatVersion; | |
262 | ||
263 | uprv_memset(pResData, 0, sizeof(ResourceData)); | |
264 | ||
265 | /* load the ResourceBundle file */ | |
266 | pResData->data=udata_openChoice(path, "res", name, isAcceptable, formatVersion, errorCode); | |
267 | if(U_FAILURE(*errorCode)) { | |
268 | return; | |
73c04bcf A |
269 | } |
270 | ||
729e4ab9 A |
271 | /* get its memory and initialize *pResData */ |
272 | res_init(pResData, formatVersion, udata_getMemory(pResData->data), -1, errorCode); | |
b75a7d8f A |
273 | } |
274 | ||
275 | U_CFUNC void | |
276 | res_unload(ResourceData *pResData) { | |
277 | if(pResData->data!=NULL) { | |
278 | udata_close(pResData->data); | |
279 | pResData->data=NULL; | |
280 | } | |
281 | } | |
282 | ||
729e4ab9 A |
283 | static const int8_t gPublicTypes[URES_LIMIT] = { |
284 | URES_STRING, | |
285 | URES_BINARY, | |
286 | URES_TABLE, | |
287 | URES_ALIAS, | |
288 | ||
289 | URES_TABLE, /* URES_TABLE32 */ | |
290 | URES_TABLE, /* URES_TABLE16 */ | |
291 | URES_STRING, /* URES_STRING_V2 */ | |
292 | URES_INT, | |
293 | ||
294 | URES_ARRAY, | |
295 | URES_ARRAY, /* URES_ARRAY16 */ | |
296 | URES_NONE, | |
297 | URES_NONE, | |
298 | ||
299 | URES_NONE, | |
300 | URES_NONE, | |
301 | URES_INT_VECTOR, | |
302 | URES_NONE | |
303 | }; | |
304 | ||
305 | U_CAPI UResType U_EXPORT2 | |
306 | res_getPublicType(Resource res) { | |
307 | return (UResType)gPublicTypes[RES_GET_TYPE(res)]; | |
308 | } | |
309 | ||
310 | U_CAPI const UChar * U_EXPORT2 | |
340931cb | 311 | res_getStringNoTrace(const ResourceData *pResData, Resource res, int32_t *pLength) { |
729e4ab9 A |
312 | const UChar *p; |
313 | uint32_t offset=RES_GET_OFFSET(res); | |
314 | int32_t length; | |
315 | if(RES_GET_TYPE(res)==URES_STRING_V2) { | |
316 | int32_t first; | |
2ca993e8 A |
317 | if((int32_t)offset<pResData->poolStringIndexLimit) { |
318 | p=(const UChar *)pResData->poolBundleStrings+offset; | |
319 | } else { | |
320 | p=(const UChar *)pResData->p16BitUnits+(offset-pResData->poolStringIndexLimit); | |
321 | } | |
729e4ab9 A |
322 | first=*p; |
323 | if(!U16_IS_TRAIL(first)) { | |
324 | length=u_strlen(p); | |
325 | } else if(first<0xdfef) { | |
326 | length=first&0x3ff; | |
327 | ++p; | |
328 | } else if(first<0xdfff) { | |
329 | length=((first-0xdfef)<<16)|p[1]; | |
330 | p+=2; | |
331 | } else { | |
332 | length=((int32_t)p[1]<<16)|p[2]; | |
333 | p+=3; | |
b75a7d8f | 334 | } |
729e4ab9 A |
335 | } else if(res==offset) /* RES_GET_TYPE(res)==URES_STRING */ { |
336 | const int32_t *p32= res==0 ? &gEmptyString.length : pResData->pRoot+res; | |
337 | length=*p32++; | |
338 | p=(const UChar *)p32; | |
b75a7d8f | 339 | } else { |
729e4ab9 A |
340 | p=NULL; |
341 | length=0; | |
342 | } | |
343 | if(pLength) { | |
344 | *pLength=length; | |
b75a7d8f | 345 | } |
729e4ab9 | 346 | return p; |
b75a7d8f A |
347 | } |
348 | ||
2ca993e8 A |
349 | namespace { |
350 | ||
351 | /** | |
352 | * CLDR string value (three empty-set symbols)=={2205, 2205, 2205} | |
353 | * prevents fallback to the parent bundle. | |
354 | * TODO: combine with other code that handles this marker, use EMPTY_SET constant. | |
355 | * TODO: maybe move to uresbund.cpp? | |
356 | */ | |
357 | UBool isNoInheritanceMarker(const ResourceData *pResData, Resource res) { | |
358 | uint32_t offset=RES_GET_OFFSET(res); | |
359 | if (offset == 0) { | |
360 | // empty string | |
361 | } else if (res == offset) { | |
362 | const int32_t *p32=pResData->pRoot+res; | |
363 | int32_t length=*p32; | |
364 | const UChar *p=(const UChar *)p32; | |
365 | return length == 3 && p[2] == 0x2205 && p[3] == 0x2205 && p[4] == 0x2205; | |
366 | } else if (RES_GET_TYPE(res) == URES_STRING_V2) { | |
367 | const UChar *p; | |
368 | if((int32_t)offset<pResData->poolStringIndexLimit) { | |
369 | p=(const UChar *)pResData->poolBundleStrings+offset; | |
370 | } else { | |
371 | p=(const UChar *)pResData->p16BitUnits+(offset-pResData->poolStringIndexLimit); | |
372 | } | |
373 | int32_t first=*p; | |
374 | if (first == 0x2205) { // implicit length | |
375 | return p[1] == 0x2205 && p[2] == 0x2205 && p[3] == 0; | |
376 | } else if (first == 0xdc03) { // explicit length 3 (should not occur) | |
377 | return p[1] == 0x2205 && p[2] == 0x2205 && p[3] == 0x2205; | |
378 | } else { | |
379 | // Assume that the string has not been stored with more length units than necessary. | |
380 | return FALSE; | |
381 | } | |
382 | } | |
383 | return FALSE; | |
384 | } | |
385 | ||
f3c0d7a5 A |
386 | int32_t getStringArray(const ResourceData *pResData, const icu::ResourceArray &array, |
387 | icu::UnicodeString *dest, int32_t capacity, | |
388 | UErrorCode &errorCode) { | |
389 | if(U_FAILURE(errorCode)) { | |
390 | return 0; | |
391 | } | |
392 | if(dest == NULL ? capacity != 0 : capacity < 0) { | |
393 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
394 | return 0; | |
395 | } | |
396 | int32_t length = array.getSize(); | |
397 | if(length == 0) { | |
398 | return 0; | |
399 | } | |
400 | if(length > capacity) { | |
401 | errorCode = U_BUFFER_OVERFLOW_ERROR; | |
402 | return length; | |
403 | } | |
404 | for(int32_t i = 0; i < length; ++i) { | |
405 | int32_t sLength; | |
340931cb A |
406 | // No tracing: handled by the caller |
407 | const UChar *s = res_getStringNoTrace(pResData, array.internalGetResource(pResData, i), &sLength); | |
f3c0d7a5 A |
408 | if(s == NULL) { |
409 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
410 | return 0; | |
411 | } | |
412 | dest[i].setTo(TRUE, s, sLength); | |
413 | } | |
414 | return length; | |
415 | } | |
416 | ||
2ca993e8 A |
417 | } // namespace |
418 | ||
729e4ab9 A |
419 | U_CAPI const UChar * U_EXPORT2 |
420 | res_getAlias(const ResourceData *pResData, Resource res, int32_t *pLength) { | |
421 | const UChar *p; | |
422 | uint32_t offset=RES_GET_OFFSET(res); | |
423 | int32_t length; | |
424 | if(RES_GET_TYPE(res)==URES_ALIAS) { | |
425 | const int32_t *p32= offset==0 ? &gEmptyString.length : pResData->pRoot+offset; | |
426 | length=*p32++; | |
427 | p=(const UChar *)p32; | |
b75a7d8f | 428 | } else { |
729e4ab9 A |
429 | p=NULL; |
430 | length=0; | |
431 | } | |
432 | if(pLength) { | |
433 | *pLength=length; | |
b75a7d8f | 434 | } |
729e4ab9 | 435 | return p; |
b75a7d8f A |
436 | } |
437 | ||
729e4ab9 | 438 | U_CAPI const uint8_t * U_EXPORT2 |
340931cb | 439 | res_getBinaryNoTrace(const ResourceData *pResData, Resource res, int32_t *pLength) { |
729e4ab9 A |
440 | const uint8_t *p; |
441 | uint32_t offset=RES_GET_OFFSET(res); | |
442 | int32_t length; | |
443 | if(RES_GET_TYPE(res)==URES_BINARY) { | |
4388f060 | 444 | const int32_t *p32= offset==0 ? (const int32_t*)&gEmpty32 : pResData->pRoot+offset; |
729e4ab9 A |
445 | length=*p32++; |
446 | p=(const uint8_t *)p32; | |
b75a7d8f | 447 | } else { |
729e4ab9 A |
448 | p=NULL; |
449 | length=0; | |
450 | } | |
451 | if(pLength) { | |
452 | *pLength=length; | |
b75a7d8f | 453 | } |
729e4ab9 | 454 | return p; |
b75a7d8f A |
455 | } |
456 | ||
457 | ||
729e4ab9 | 458 | U_CAPI const int32_t * U_EXPORT2 |
340931cb | 459 | res_getIntVectorNoTrace(const ResourceData *pResData, Resource res, int32_t *pLength) { |
729e4ab9 A |
460 | const int32_t *p; |
461 | uint32_t offset=RES_GET_OFFSET(res); | |
462 | int32_t length; | |
463 | if(RES_GET_TYPE(res)==URES_INT_VECTOR) { | |
4388f060 | 464 | p= offset==0 ? (const int32_t *)&gEmpty32 : pResData->pRoot+offset; |
729e4ab9 | 465 | length=*p++; |
b75a7d8f | 466 | } else { |
729e4ab9 A |
467 | p=NULL; |
468 | length=0; | |
469 | } | |
470 | if(pLength) { | |
471 | *pLength=length; | |
472 | } | |
473 | return p; | |
474 | } | |
475 | ||
476 | U_CAPI int32_t U_EXPORT2 | |
477 | res_countArrayItems(const ResourceData *pResData, Resource res) { | |
478 | uint32_t offset=RES_GET_OFFSET(res); | |
479 | switch(RES_GET_TYPE(res)) { | |
480 | case URES_STRING: | |
481 | case URES_STRING_V2: | |
482 | case URES_BINARY: | |
483 | case URES_ALIAS: | |
484 | case URES_INT: | |
485 | case URES_INT_VECTOR: | |
486 | return 1; | |
487 | case URES_ARRAY: | |
488 | case URES_TABLE32: | |
489 | return offset==0 ? 0 : *(pResData->pRoot+offset); | |
490 | case URES_TABLE: | |
491 | return offset==0 ? 0 : *((const uint16_t *)(pResData->pRoot+offset)); | |
492 | case URES_ARRAY16: | |
493 | case URES_TABLE16: | |
494 | return pResData->p16BitUnits[offset]; | |
495 | default: | |
496 | return 0; | |
b75a7d8f A |
497 | } |
498 | } | |
499 | ||
2ca993e8 A |
500 | U_NAMESPACE_BEGIN |
501 | ||
502 | ResourceDataValue::~ResourceDataValue() {} | |
503 | ||
504 | UResType ResourceDataValue::getType() const { | |
505 | return res_getPublicType(res); | |
506 | } | |
507 | ||
508 | const UChar *ResourceDataValue::getString(int32_t &length, UErrorCode &errorCode) const { | |
509 | if(U_FAILURE(errorCode)) { | |
510 | return NULL; | |
511 | } | |
340931cb | 512 | const UChar *s = res_getString(fTraceInfo, &getData(), res, &length); |
2ca993e8 A |
513 | if(s == NULL) { |
514 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
515 | } | |
516 | return s; | |
517 | } | |
518 | ||
519 | const UChar *ResourceDataValue::getAliasString(int32_t &length, UErrorCode &errorCode) const { | |
520 | if(U_FAILURE(errorCode)) { | |
521 | return NULL; | |
522 | } | |
340931cb | 523 | const UChar *s = res_getAlias(&getData(), res, &length); |
2ca993e8 A |
524 | if(s == NULL) { |
525 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
526 | } | |
527 | return s; | |
528 | } | |
529 | ||
530 | int32_t ResourceDataValue::getInt(UErrorCode &errorCode) const { | |
531 | if(U_FAILURE(errorCode)) { | |
532 | return 0; | |
533 | } | |
534 | if(RES_GET_TYPE(res) != URES_INT) { | |
535 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
536 | } | |
340931cb | 537 | return res_getInt(fTraceInfo, res); |
2ca993e8 A |
538 | } |
539 | ||
540 | uint32_t ResourceDataValue::getUInt(UErrorCode &errorCode) const { | |
541 | if(U_FAILURE(errorCode)) { | |
542 | return 0; | |
543 | } | |
544 | if(RES_GET_TYPE(res) != URES_INT) { | |
545 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
546 | } | |
340931cb | 547 | return res_getUInt(fTraceInfo, res); |
2ca993e8 A |
548 | } |
549 | ||
550 | const int32_t *ResourceDataValue::getIntVector(int32_t &length, UErrorCode &errorCode) const { | |
551 | if(U_FAILURE(errorCode)) { | |
552 | return NULL; | |
553 | } | |
340931cb | 554 | const int32_t *iv = res_getIntVector(fTraceInfo, &getData(), res, &length); |
2ca993e8 A |
555 | if(iv == NULL) { |
556 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
557 | } | |
558 | return iv; | |
559 | } | |
560 | ||
561 | const uint8_t *ResourceDataValue::getBinary(int32_t &length, UErrorCode &errorCode) const { | |
562 | if(U_FAILURE(errorCode)) { | |
563 | return NULL; | |
564 | } | |
340931cb | 565 | const uint8_t *b = res_getBinary(fTraceInfo, &getData(), res, &length); |
2ca993e8 A |
566 | if(b == NULL) { |
567 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
568 | } | |
569 | return b; | |
570 | } | |
571 | ||
f3c0d7a5 A |
572 | ResourceArray ResourceDataValue::getArray(UErrorCode &errorCode) const { |
573 | if(U_FAILURE(errorCode)) { | |
574 | return ResourceArray(); | |
575 | } | |
576 | const uint16_t *items16 = NULL; | |
577 | const Resource *items32 = NULL; | |
578 | uint32_t offset=RES_GET_OFFSET(res); | |
579 | int32_t length = 0; | |
580 | switch(RES_GET_TYPE(res)) { | |
581 | case URES_ARRAY: | |
582 | if (offset!=0) { // empty if offset==0 | |
340931cb | 583 | items32 = (const Resource *)getData().pRoot+offset; |
f3c0d7a5 A |
584 | length = *items32++; |
585 | } | |
586 | break; | |
587 | case URES_ARRAY16: | |
340931cb | 588 | items16 = getData().p16BitUnits+offset; |
f3c0d7a5 A |
589 | length = *items16++; |
590 | break; | |
591 | default: | |
592 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
593 | return ResourceArray(); | |
594 | } | |
340931cb | 595 | return ResourceArray(items16, items32, length, fTraceInfo); |
f3c0d7a5 A |
596 | } |
597 | ||
598 | ResourceTable ResourceDataValue::getTable(UErrorCode &errorCode) const { | |
599 | if(U_FAILURE(errorCode)) { | |
600 | return ResourceTable(); | |
601 | } | |
602 | const uint16_t *keys16 = NULL; | |
603 | const int32_t *keys32 = NULL; | |
604 | const uint16_t *items16 = NULL; | |
605 | const Resource *items32 = NULL; | |
606 | uint32_t offset = RES_GET_OFFSET(res); | |
607 | int32_t length = 0; | |
608 | switch(RES_GET_TYPE(res)) { | |
609 | case URES_TABLE: | |
610 | if (offset != 0) { // empty if offset==0 | |
340931cb | 611 | keys16 = (const uint16_t *)(getData().pRoot+offset); |
f3c0d7a5 A |
612 | length = *keys16++; |
613 | items32 = (const Resource *)(keys16+length+(~length&1)); | |
614 | } | |
615 | break; | |
616 | case URES_TABLE16: | |
340931cb | 617 | keys16 = getData().p16BitUnits+offset; |
f3c0d7a5 A |
618 | length = *keys16++; |
619 | items16 = keys16 + length; | |
620 | break; | |
621 | case URES_TABLE32: | |
622 | if (offset != 0) { // empty if offset==0 | |
340931cb | 623 | keys32 = getData().pRoot+offset; |
f3c0d7a5 A |
624 | length = *keys32++; |
625 | items32 = (const Resource *)keys32 + length; | |
626 | } | |
627 | break; | |
628 | default: | |
629 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
630 | return ResourceTable(); | |
631 | } | |
340931cb | 632 | return ResourceTable(keys16, keys32, items16, items32, length, fTraceInfo); |
f3c0d7a5 A |
633 | } |
634 | ||
635 | UBool ResourceDataValue::isNoInheritanceMarker() const { | |
340931cb | 636 | return ::isNoInheritanceMarker(&getData(), res); |
f3c0d7a5 A |
637 | } |
638 | ||
639 | int32_t ResourceDataValue::getStringArray(UnicodeString *dest, int32_t capacity, | |
640 | UErrorCode &errorCode) const { | |
340931cb | 641 | return ::getStringArray(&getData(), getArray(errorCode), dest, capacity, errorCode); |
f3c0d7a5 A |
642 | } |
643 | ||
644 | int32_t ResourceDataValue::getStringArrayOrStringAsArray(UnicodeString *dest, int32_t capacity, | |
645 | UErrorCode &errorCode) const { | |
646 | if(URES_IS_ARRAY(res)) { | |
340931cb | 647 | return ::getStringArray(&getData(), getArray(errorCode), dest, capacity, errorCode); |
f3c0d7a5 A |
648 | } |
649 | if(U_FAILURE(errorCode)) { | |
650 | return 0; | |
651 | } | |
652 | if(dest == NULL ? capacity != 0 : capacity < 0) { | |
653 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; | |
654 | return 0; | |
655 | } | |
656 | if(capacity < 1) { | |
657 | errorCode = U_BUFFER_OVERFLOW_ERROR; | |
658 | return 1; | |
659 | } | |
660 | int32_t sLength; | |
340931cb | 661 | const UChar *s = res_getString(fTraceInfo, &getData(), res, &sLength); |
f3c0d7a5 A |
662 | if(s != NULL) { |
663 | dest[0].setTo(TRUE, s, sLength); | |
664 | return 1; | |
665 | } | |
666 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
667 | return 0; | |
668 | } | |
669 | ||
670 | UnicodeString ResourceDataValue::getStringOrFirstOfArray(UErrorCode &errorCode) const { | |
671 | UnicodeString us; | |
672 | if(U_FAILURE(errorCode)) { | |
673 | return us; | |
674 | } | |
675 | int32_t sLength; | |
340931cb | 676 | const UChar *s = res_getString(fTraceInfo, &getData(), res, &sLength); |
f3c0d7a5 A |
677 | if(s != NULL) { |
678 | us.setTo(TRUE, s, sLength); | |
679 | return us; | |
680 | } | |
681 | ResourceArray array = getArray(errorCode); | |
682 | if(U_FAILURE(errorCode)) { | |
683 | return us; | |
684 | } | |
685 | if(array.getSize() > 0) { | |
340931cb A |
686 | // Tracing is already performed above (unimportant for trace that this is an array) |
687 | s = res_getStringNoTrace(&getData(), array.internalGetResource(&getData(), 0), &sLength); | |
f3c0d7a5 A |
688 | if(s != NULL) { |
689 | us.setTo(TRUE, s, sLength); | |
690 | return us; | |
691 | } | |
692 | } | |
693 | errorCode = U_RESOURCE_TYPE_MISMATCH; | |
694 | return us; | |
695 | } | |
696 | ||
2ca993e8 A |
697 | U_NAMESPACE_END |
698 | ||
699 | static Resource | |
700 | makeResourceFrom16(const ResourceData *pResData, int32_t res16) { | |
701 | if(res16<pResData->poolStringIndex16Limit) { | |
702 | // Pool string, nothing to do. | |
703 | } else { | |
704 | // Local string, adjust the 16-bit offset to a regular one, | |
705 | // with a larger pool string index limit. | |
706 | res16=res16-pResData->poolStringIndex16Limit+pResData->poolStringIndexLimit; | |
707 | } | |
708 | return URES_MAKE_RESOURCE(URES_STRING_V2, res16); | |
709 | } | |
710 | ||
729e4ab9 A |
711 | U_CAPI Resource U_EXPORT2 |
712 | res_getTableItemByKey(const ResourceData *pResData, Resource table, | |
713 | int32_t *indexR, const char **key) { | |
714 | uint32_t offset=RES_GET_OFFSET(table); | |
715 | int32_t length; | |
716 | int32_t idx; | |
717 | if(key == NULL || *key == NULL) { | |
718 | return RES_BOGUS; | |
719 | } | |
720 | switch(RES_GET_TYPE(table)) { | |
721 | case URES_TABLE: { | |
4388f060 A |
722 | if (offset!=0) { /* empty if offset==0 */ |
723 | const uint16_t *p= (const uint16_t *)(pResData->pRoot+offset); | |
724 | length=*p++; | |
725 | *indexR=idx=_res_findTableItem(pResData, p, length, *key, key); | |
726 | if(idx>=0) { | |
727 | const Resource *p32=(const Resource *)(p+length+(~length&1)); | |
728 | return p32[idx]; | |
729 | } | |
374ca955 | 730 | } |
729e4ab9 A |
731 | break; |
732 | } | |
733 | case URES_TABLE16: { | |
734 | const uint16_t *p=pResData->p16BitUnits+offset; | |
735 | length=*p++; | |
736 | *indexR=idx=_res_findTableItem(pResData, p, length, *key, key); | |
737 | if(idx>=0) { | |
2ca993e8 | 738 | return makeResourceFrom16(pResData, p[length+idx]); |
374ca955 | 739 | } |
729e4ab9 A |
740 | break; |
741 | } | |
742 | case URES_TABLE32: { | |
4388f060 A |
743 | if (offset!=0) { /* empty if offset==0 */ |
744 | const int32_t *p= pResData->pRoot+offset; | |
745 | length=*p++; | |
746 | *indexR=idx=_res_findTable32Item(pResData, p, length, *key, key); | |
747 | if(idx>=0) { | |
748 | return (Resource)p[length+idx]; | |
749 | } | |
b75a7d8f | 750 | } |
729e4ab9 A |
751 | break; |
752 | } | |
753 | default: | |
754 | break; | |
755 | } | |
756 | return RES_BOGUS; | |
b75a7d8f A |
757 | } |
758 | ||
729e4ab9 A |
759 | U_CAPI Resource U_EXPORT2 |
760 | res_getTableItemByIndex(const ResourceData *pResData, Resource table, | |
761 | int32_t indexR, const char **key) { | |
762 | uint32_t offset=RES_GET_OFFSET(table); | |
763 | int32_t length; | |
5ea0322b A |
764 | if (indexR < 0) { |
765 | return RES_BOGUS; | |
766 | } | |
729e4ab9 A |
767 | switch(RES_GET_TYPE(table)) { |
768 | case URES_TABLE: { | |
4388f060 A |
769 | if (offset != 0) { /* empty if offset==0 */ |
770 | const uint16_t *p= (const uint16_t *)(pResData->pRoot+offset); | |
771 | length=*p++; | |
772 | if(indexR<length) { | |
773 | const Resource *p32=(const Resource *)(p+length+(~length&1)); | |
774 | if(key!=NULL) { | |
775 | *key=RES_GET_KEY16(pResData, p[indexR]); | |
776 | } | |
777 | return p32[indexR]; | |
729e4ab9 | 778 | } |
729e4ab9 A |
779 | } |
780 | break; | |
781 | } | |
782 | case URES_TABLE16: { | |
783 | const uint16_t *p=pResData->p16BitUnits+offset; | |
784 | length=*p++; | |
785 | if(indexR<length) { | |
786 | if(key!=NULL) { | |
787 | *key=RES_GET_KEY16(pResData, p[indexR]); | |
788 | } | |
2ca993e8 | 789 | return makeResourceFrom16(pResData, p[length+indexR]); |
729e4ab9 A |
790 | } |
791 | break; | |
792 | } | |
793 | case URES_TABLE32: { | |
4388f060 A |
794 | if (offset != 0) { /* empty if offset==0 */ |
795 | const int32_t *p= pResData->pRoot+offset; | |
796 | length=*p++; | |
797 | if(indexR<length) { | |
798 | if(key!=NULL) { | |
799 | *key=RES_GET_KEY32(pResData, p[indexR]); | |
800 | } | |
801 | return (Resource)p[length+indexR]; | |
729e4ab9 | 802 | } |
729e4ab9 A |
803 | } |
804 | break; | |
374ca955 | 805 | } |
729e4ab9 A |
806 | default: |
807 | break; | |
808 | } | |
809 | return RES_BOGUS; | |
b75a7d8f A |
810 | } |
811 | ||
729e4ab9 A |
812 | U_CAPI Resource U_EXPORT2 |
813 | res_getResource(const ResourceData *pResData, const char *key) { | |
814 | const char *realKey=key; | |
815 | int32_t idx; | |
816 | return res_getTableItemByKey(pResData, pResData->rootRes, &idx, &realKey); | |
817 | } | |
818 | ||
2ca993e8 | 819 | |
f3c0d7a5 A |
820 | UBool icu::ResourceTable::getKeyAndValue(int32_t i, |
821 | const char *&key, icu::ResourceValue &value) const { | |
822 | if(0 <= i && i < length) { | |
823 | icu::ResourceDataValue &rdValue = static_cast<icu::ResourceDataValue &>(value); | |
340931cb A |
824 | if (keys16 != nullptr) { |
825 | key = RES_GET_KEY16(&rdValue.getData(), keys16[i]); | |
2ca993e8 | 826 | } else { |
340931cb | 827 | key = RES_GET_KEY32(&rdValue.getData(), keys32[i]); |
2ca993e8 A |
828 | } |
829 | Resource res; | |
340931cb A |
830 | if (items16 != nullptr) { |
831 | res = makeResourceFrom16(&rdValue.getData(), items16[i]); | |
2ca993e8 A |
832 | } else { |
833 | res = items32[i]; | |
834 | } | |
340931cb A |
835 | // Note: the ResourceTracer keeps a reference to the field of this |
836 | // ResourceTable. This is OK because the ResourceTable should remain | |
837 | // alive for the duration that fields are being read from it | |
838 | // (including nested fields). | |
839 | rdValue.setResource(res, ResourceTracer(fTraceInfo, key)); | |
840 | return TRUE; | |
841 | } | |
842 | return FALSE; | |
843 | } | |
844 | ||
845 | UBool icu::ResourceTable::findValue(const char *key, ResourceValue &value) const { | |
846 | icu::ResourceDataValue &rdValue = static_cast<icu::ResourceDataValue &>(value); | |
847 | const char *realKey = nullptr; | |
848 | int32_t i; | |
849 | if (keys16 != nullptr) { | |
850 | i = _res_findTableItem(&rdValue.getData(), keys16, length, key, &realKey); | |
851 | } else { | |
852 | i = _res_findTable32Item(&rdValue.getData(), keys32, length, key, &realKey); | |
853 | } | |
854 | if (i >= 0) { | |
855 | Resource res; | |
856 | if (items16 != nullptr) { | |
857 | res = makeResourceFrom16(&rdValue.getData(), items16[i]); | |
858 | } else { | |
859 | res = items32[i]; | |
860 | } | |
861 | // Same note about lifetime as in getKeyAndValue(). | |
862 | rdValue.setResource(res, ResourceTracer(fTraceInfo, key)); | |
f3c0d7a5 | 863 | return TRUE; |
2ca993e8 | 864 | } |
f3c0d7a5 | 865 | return FALSE; |
2ca993e8 A |
866 | } |
867 | ||
729e4ab9 A |
868 | U_CAPI Resource U_EXPORT2 |
869 | res_getArrayItem(const ResourceData *pResData, Resource array, int32_t indexR) { | |
870 | uint32_t offset=RES_GET_OFFSET(array); | |
5ea0322b A |
871 | if (indexR < 0) { |
872 | return RES_BOGUS; | |
873 | } | |
729e4ab9 A |
874 | switch(RES_GET_TYPE(array)) { |
875 | case URES_ARRAY: { | |
4388f060 A |
876 | if (offset!=0) { /* empty if offset==0 */ |
877 | const int32_t *p= pResData->pRoot+offset; | |
878 | if(indexR<*p) { | |
879 | return (Resource)p[1+indexR]; | |
880 | } | |
729e4ab9 A |
881 | } |
882 | break; | |
883 | } | |
884 | case URES_ARRAY16: { | |
885 | const uint16_t *p=pResData->p16BitUnits+offset; | |
886 | if(indexR<*p) { | |
2ca993e8 | 887 | return makeResourceFrom16(pResData, p[1+indexR]); |
729e4ab9 A |
888 | } |
889 | break; | |
890 | } | |
891 | default: | |
892 | break; | |
893 | } | |
894 | return RES_BOGUS; | |
b75a7d8f A |
895 | } |
896 | ||
f3c0d7a5 A |
897 | uint32_t icu::ResourceArray::internalGetResource(const ResourceData *pResData, int32_t i) const { |
898 | if (items16 != NULL) { | |
899 | return makeResourceFrom16(pResData, items16[i]); | |
900 | } else { | |
901 | return items32[i]; | |
2ca993e8 | 902 | } |
f3c0d7a5 | 903 | } |
2ca993e8 | 904 | |
f3c0d7a5 A |
905 | UBool icu::ResourceArray::getValue(int32_t i, icu::ResourceValue &value) const { |
906 | if(0 <= i && i < length) { | |
907 | icu::ResourceDataValue &rdValue = static_cast<icu::ResourceDataValue &>(value); | |
340931cb A |
908 | // Note: the ResourceTracer keeps a reference to the field of this |
909 | // ResourceArray. This is OK because the ResourceArray should remain | |
910 | // alive for the duration that fields are being read from it | |
911 | // (including nested fields). | |
912 | rdValue.setResource( | |
913 | internalGetResource(&rdValue.getData(), i), | |
914 | ResourceTracer(fTraceInfo, i)); | |
f3c0d7a5 | 915 | return TRUE; |
2ca993e8 | 916 | } |
f3c0d7a5 | 917 | return FALSE; |
2ca993e8 A |
918 | } |
919 | ||
b75a7d8f | 920 | U_CFUNC Resource |
374ca955 | 921 | res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key) { |
374ca955 | 922 | char *pathP = *path, *nextSepP = *path; |
b75a7d8f A |
923 | char *closeIndex = NULL; |
924 | Resource t1 = r; | |
925 | Resource t2; | |
374ca955 | 926 | int32_t indexR = 0; |
51004dcb | 927 | UResType type = (UResType)RES_GET_TYPE(t1); |
374ca955 A |
928 | |
929 | /* if you come in with an empty path, you'll be getting back the same resource */ | |
930 | if(!uprv_strlen(pathP)) { | |
931 | return r; | |
932 | } | |
933 | ||
934 | /* one needs to have an aggregate resource in order to search in it */ | |
729e4ab9 | 935 | if(!URES_IS_CONTAINER(type)) { |
374ca955 A |
936 | return RES_BOGUS; |
937 | } | |
b75a7d8f | 938 | |
729e4ab9 | 939 | while(nextSepP && *pathP && t1 != RES_BOGUS && URES_IS_CONTAINER(type)) { |
b75a7d8f A |
940 | /* Iteration stops if: the path has been consumed, we found a non-existing |
941 | * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias) | |
942 | */ | |
943 | nextSepP = uprv_strchr(pathP, RES_PATH_SEPARATOR); | |
944 | /* if there are more separators, terminate string | |
945 | * and set path to the remaining part of the string | |
946 | */ | |
947 | if(nextSepP != NULL) { | |
2ca993e8 A |
948 | if(nextSepP == pathP) { |
949 | // Empty key string. | |
950 | return RES_BOGUS; | |
951 | } | |
374ca955 | 952 | *nextSepP = 0; /* overwrite the separator with a NUL to terminate the key */ |
b75a7d8f A |
953 | *path = nextSepP+1; |
954 | } else { | |
374ca955 | 955 | *path = uprv_strchr(pathP, 0); |
b75a7d8f A |
956 | } |
957 | ||
958 | /* if the resource is a table */ | |
959 | /* try the key based access */ | |
729e4ab9 A |
960 | if(URES_IS_TABLE(type)) { |
961 | *key = pathP; | |
962 | t2 = res_getTableItemByKey(pResData, t1, &indexR, key); | |
b75a7d8f A |
963 | if(t2 == RES_BOGUS) { |
964 | /* if we fail to get the resource by key, maybe we got an index */ | |
965 | indexR = uprv_strtol(pathP, &closeIndex, 10); | |
f3c0d7a5 | 966 | if(indexR >= 0 && *closeIndex == 0) { |
b75a7d8f A |
967 | /* if we indeed have an index, try to get the item by index */ |
968 | t2 = res_getTableItemByIndex(pResData, t1, indexR, key); | |
f3c0d7a5 | 969 | } // else t2 is already RES_BOGUS |
b75a7d8f | 970 | } |
729e4ab9 | 971 | } else if(URES_IS_ARRAY(type)) { |
b75a7d8f | 972 | indexR = uprv_strtol(pathP, &closeIndex, 10); |
f3c0d7a5 | 973 | if(indexR >= 0 && *closeIndex == 0) { |
729e4ab9 | 974 | t2 = res_getArrayItem(pResData, t1, indexR); |
b75a7d8f A |
975 | } else { |
976 | t2 = RES_BOGUS; /* have an array, but don't have a valid index */ | |
977 | } | |
978 | *key = NULL; | |
979 | } else { /* can't do much here, except setting t2 to bogus */ | |
980 | t2 = RES_BOGUS; | |
981 | } | |
982 | t1 = t2; | |
51004dcb | 983 | type = (UResType)RES_GET_TYPE(t1); |
b75a7d8f | 984 | /* position pathP to next resource key/index */ |
374ca955 | 985 | pathP = *path; |
b75a7d8f A |
986 | } |
987 | ||
988 | return t1; | |
989 | } | |
990 | ||
374ca955 A |
991 | /* resource bundle swapping ------------------------------------------------- */ |
992 | ||
993 | /* | |
994 | * Need to always enumerate the entire item tree, | |
995 | * track the lowest address of any item to use as the limit for char keys[], | |
996 | * track the highest address of any item to return the size of the data. | |
997 | * | |
998 | * We should have thought of storing those in the data... | |
999 | * It is possible to extend the data structure by putting additional values | |
1000 | * in places that are inaccessible by ordinary enumeration of the item tree. | |
1001 | * For example, additional integers could be stored at the beginning or | |
1002 | * end of the key strings; this could be indicated by a minor version number, | |
1003 | * and the data swapping would have to know about these values. | |
1004 | * | |
1005 | * The data structure does not forbid keys to be shared, so we must swap | |
1006 | * all keys once instead of each key when it is referenced. | |
1007 | * | |
1008 | * These swapping functions assume that a resource bundle always has a length | |
1009 | * that is a multiple of 4 bytes. | |
1010 | * Currently, this is trivially true because genrb writes bundle tree leaves | |
1011 | * physically first, before their branches, so that the root table with its | |
1012 | * array of resource items (uint32_t values) is always last. | |
1013 | */ | |
1014 | ||
1015 | /* definitions for table sorting ------------------------ */ | |
1016 | ||
1017 | /* | |
1018 | * row of a temporary array | |
1019 | * | |
1020 | * gets platform-endian key string indexes and sorting indexes; | |
1021 | * after sorting this array by keys, the actual key/value arrays are permutated | |
1022 | * according to the sorting indexes | |
1023 | */ | |
1024 | typedef struct Row { | |
1025 | int32_t keyIndex, sortIndex; | |
1026 | } Row; | |
1027 | ||
f3c0d7a5 | 1028 | static int32_t U_CALLCONV |
374ca955 A |
1029 | ures_compareRows(const void *context, const void *left, const void *right) { |
1030 | const char *keyChars=(const char *)context; | |
1031 | return (int32_t)uprv_strcmp(keyChars+((const Row *)left)->keyIndex, | |
1032 | keyChars+((const Row *)right)->keyIndex); | |
1033 | } | |
1034 | ||
1035 | typedef struct TempTable { | |
1036 | const char *keyChars; | |
1037 | Row *rows; | |
1038 | int32_t *resort; | |
729e4ab9 A |
1039 | uint32_t *resFlags; |
1040 | int32_t localKeyLimit; | |
1041 | uint8_t majorFormatVersion; | |
374ca955 A |
1042 | } TempTable; |
1043 | ||
1044 | enum { | |
1045 | STACK_ROW_CAPACITY=200 | |
1046 | }; | |
1047 | ||
729e4ab9 A |
1048 | /* The table item key string is not locally available. */ |
1049 | static const char *const gUnknownKey=""; | |
374ca955 A |
1050 | |
1051 | /* resource table key for collation binaries: "%%CollationBin" */ | |
1052 | static const UChar gCollationBinKey[]={ | |
1053 | 0x25, 0x25, | |
1054 | 0x43, 0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, | |
1055 | 0x42, 0x69, 0x6e, | |
1056 | 0 | |
1057 | }; | |
1058 | ||
374ca955 A |
1059 | /* |
1060 | * swap one resource item | |
374ca955 A |
1061 | */ |
1062 | static void | |
1063 | ures_swapResource(const UDataSwapper *ds, | |
1064 | const Resource *inBundle, Resource *outBundle, | |
1065 | Resource res, /* caller swaps res itself */ | |
729e4ab9 | 1066 | const char *key, |
374ca955 A |
1067 | TempTable *pTempTable, |
1068 | UErrorCode *pErrorCode) { | |
1069 | const Resource *p; | |
1070 | Resource *q; | |
1071 | int32_t offset, count; | |
1072 | ||
729e4ab9 A |
1073 | switch(RES_GET_TYPE(res)) { |
1074 | case URES_TABLE16: | |
1075 | case URES_STRING_V2: | |
1076 | case URES_INT: | |
1077 | case URES_ARRAY16: | |
1078 | /* integer, or points to 16-bit units, nothing to do here */ | |
374ca955 | 1079 | return; |
729e4ab9 A |
1080 | default: |
1081 | break; | |
374ca955 A |
1082 | } |
1083 | ||
1084 | /* all other types use an offset to point to their data */ | |
1085 | offset=(int32_t)RES_GET_OFFSET(res); | |
729e4ab9 A |
1086 | if(offset==0) { |
1087 | /* special offset indicating an empty item */ | |
1088 | return; | |
1089 | } | |
1090 | if(pTempTable->resFlags[offset>>5]&((uint32_t)1<<(offset&0x1f))) { | |
1091 | /* we already swapped this resource item */ | |
1092 | return; | |
1093 | } else { | |
1094 | /* mark it as swapped now */ | |
1095 | pTempTable->resFlags[offset>>5]|=((uint32_t)1<<(offset&0x1f)); | |
1096 | } | |
1097 | ||
374ca955 A |
1098 | p=inBundle+offset; |
1099 | q=outBundle+offset; | |
1100 | ||
1101 | switch(RES_GET_TYPE(res)) { | |
1102 | case URES_ALIAS: | |
1103 | /* physically same value layout as string, fall through */ | |
2ca993e8 | 1104 | U_FALLTHROUGH; |
374ca955 A |
1105 | case URES_STRING: |
1106 | count=udata_readInt32(ds, (int32_t)*p); | |
1107 | /* swap length */ | |
1108 | ds->swapArray32(ds, p, 4, q, pErrorCode); | |
1109 | /* swap each UChar (the terminating NUL would not change) */ | |
1110 | ds->swapArray16(ds, p+1, 2*count, q+1, pErrorCode); | |
1111 | break; | |
1112 | case URES_BINARY: | |
1113 | count=udata_readInt32(ds, (int32_t)*p); | |
1114 | /* swap length */ | |
1115 | ds->swapArray32(ds, p, 4, q, pErrorCode); | |
1116 | /* no need to swap or copy bytes - ures_swap() copied them all */ | |
1117 | ||
1118 | /* swap known formats */ | |
374ca955 | 1119 | #if !UCONFIG_NO_COLLATION |
729e4ab9 A |
1120 | if( key!=NULL && /* the binary is in a table */ |
1121 | (key!=gUnknownKey ? | |
1122 | /* its table key string is "%%CollationBin" */ | |
1123 | 0==ds->compareInvChars(ds, key, -1, | |
b331163b | 1124 | gCollationBinKey, UPRV_LENGTHOF(gCollationBinKey)-1) : |
729e4ab9 A |
1125 | /* its table key string is unknown but it looks like a collation binary */ |
1126 | ucol_looksLikeCollationBinary(ds, p+1, count)) | |
1127 | ) { | |
57a6839d | 1128 | ucol_swap(ds, p+1, count, q+1, pErrorCode); |
374ca955 | 1129 | } |
729e4ab9 | 1130 | #endif |
374ca955 A |
1131 | break; |
1132 | case URES_TABLE: | |
1133 | case URES_TABLE32: | |
1134 | { | |
1135 | const uint16_t *pKey16; | |
1136 | uint16_t *qKey16; | |
1137 | ||
1138 | const int32_t *pKey32; | |
1139 | int32_t *qKey32; | |
1140 | ||
1141 | Resource item; | |
1142 | int32_t i, oldIndex; | |
1143 | ||
1144 | if(RES_GET_TYPE(res)==URES_TABLE) { | |
1145 | /* get table item count */ | |
1146 | pKey16=(const uint16_t *)p; | |
1147 | qKey16=(uint16_t *)q; | |
1148 | count=ds->readUInt16(*pKey16); | |
1149 | ||
1150 | pKey32=qKey32=NULL; | |
1151 | ||
1152 | /* swap count */ | |
1153 | ds->swapArray16(ds, pKey16++, 2, qKey16++, pErrorCode); | |
1154 | ||
1155 | offset+=((1+count)+1)/2; | |
1156 | } else { | |
1157 | /* get table item count */ | |
1158 | pKey32=(const int32_t *)p; | |
1159 | qKey32=(int32_t *)q; | |
1160 | count=udata_readInt32(ds, *pKey32); | |
1161 | ||
1162 | pKey16=qKey16=NULL; | |
1163 | ||
1164 | /* swap count */ | |
1165 | ds->swapArray32(ds, pKey32++, 4, qKey32++, pErrorCode); | |
1166 | ||
1167 | offset+=1+count; | |
1168 | } | |
1169 | ||
1170 | if(count==0) { | |
1171 | break; | |
1172 | } | |
1173 | ||
1174 | p=inBundle+offset; /* pointer to table resources */ | |
1175 | q=outBundle+offset; | |
1176 | ||
1177 | /* recurse */ | |
1178 | for(i=0; i<count; ++i) { | |
729e4ab9 A |
1179 | const char *itemKey=gUnknownKey; |
1180 | if(pKey16!=NULL) { | |
1181 | int32_t keyOffset=ds->readUInt16(pKey16[i]); | |
1182 | if(keyOffset<pTempTable->localKeyLimit) { | |
1183 | itemKey=(const char *)outBundle+keyOffset; | |
1184 | } | |
374ca955 | 1185 | } else { |
729e4ab9 A |
1186 | int32_t keyOffset=udata_readInt32(ds, pKey32[i]); |
1187 | if(keyOffset>=0) { | |
1188 | itemKey=(const char *)outBundle+keyOffset; | |
1189 | } | |
374ca955 | 1190 | } |
374ca955 | 1191 | item=ds->readUInt32(p[i]); |
729e4ab9 | 1192 | ures_swapResource(ds, inBundle, outBundle, item, itemKey, pTempTable, pErrorCode); |
374ca955 | 1193 | if(U_FAILURE(*pErrorCode)) { |
73c04bcf A |
1194 | udata_printError(ds, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n", |
1195 | res, i, item); | |
374ca955 A |
1196 | return; |
1197 | } | |
1198 | } | |
1199 | ||
729e4ab9 | 1200 | if(pTempTable->majorFormatVersion>1 || ds->inCharset==ds->outCharset) { |
374ca955 A |
1201 | /* no need to sort, just swap the offset/value arrays */ |
1202 | if(pKey16!=NULL) { | |
1203 | ds->swapArray16(ds, pKey16, count*2, qKey16, pErrorCode); | |
1204 | ds->swapArray32(ds, p, count*4, q, pErrorCode); | |
1205 | } else { | |
1206 | /* swap key offsets and items as one array */ | |
1207 | ds->swapArray32(ds, pKey32, count*2*4, qKey32, pErrorCode); | |
1208 | } | |
1209 | break; | |
1210 | } | |
1211 | ||
1212 | /* | |
1213 | * We need to sort tables by outCharset key strings because they | |
1214 | * sort differently for different charset families. | |
1215 | * ures_swap() already set pTempTable->keyChars appropriately. | |
1216 | * First we set up a temporary table with the key indexes and | |
1217 | * sorting indexes and sort that. | |
1218 | * Then we permutate and copy/swap the actual values. | |
1219 | */ | |
1220 | if(pKey16!=NULL) { | |
1221 | for(i=0; i<count; ++i) { | |
1222 | pTempTable->rows[i].keyIndex=ds->readUInt16(pKey16[i]); | |
1223 | pTempTable->rows[i].sortIndex=i; | |
1224 | } | |
1225 | } else { | |
1226 | for(i=0; i<count; ++i) { | |
1227 | pTempTable->rows[i].keyIndex=udata_readInt32(ds, pKey32[i]); | |
1228 | pTempTable->rows[i].sortIndex=i; | |
1229 | } | |
1230 | } | |
1231 | uprv_sortArray(pTempTable->rows, count, sizeof(Row), | |
1232 | ures_compareRows, pTempTable->keyChars, | |
1233 | FALSE, pErrorCode); | |
1234 | if(U_FAILURE(*pErrorCode)) { | |
73c04bcf A |
1235 | udata_printError(ds, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n", |
1236 | res, count); | |
374ca955 A |
1237 | return; |
1238 | } | |
1239 | ||
1240 | /* | |
1241 | * copy/swap/permutate items | |
1242 | * | |
1243 | * If we swap in-place, then the permutation must use another | |
1244 | * temporary array (pTempTable->resort) | |
1245 | * before the results are copied to the outBundle. | |
1246 | */ | |
1247 | /* keys */ | |
1248 | if(pKey16!=NULL) { | |
1249 | uint16_t *rKey16; | |
1250 | ||
1251 | if(pKey16!=qKey16) { | |
1252 | rKey16=qKey16; | |
1253 | } else { | |
1254 | rKey16=(uint16_t *)pTempTable->resort; | |
1255 | } | |
1256 | for(i=0; i<count; ++i) { | |
1257 | oldIndex=pTempTable->rows[i].sortIndex; | |
1258 | ds->swapArray16(ds, pKey16+oldIndex, 2, rKey16+i, pErrorCode); | |
1259 | } | |
1260 | if(qKey16!=rKey16) { | |
1261 | uprv_memcpy(qKey16, rKey16, 2*count); | |
1262 | } | |
1263 | } else { | |
1264 | int32_t *rKey32; | |
1265 | ||
1266 | if(pKey32!=qKey32) { | |
1267 | rKey32=qKey32; | |
1268 | } else { | |
1269 | rKey32=pTempTable->resort; | |
1270 | } | |
1271 | for(i=0; i<count; ++i) { | |
1272 | oldIndex=pTempTable->rows[i].sortIndex; | |
1273 | ds->swapArray32(ds, pKey32+oldIndex, 4, rKey32+i, pErrorCode); | |
1274 | } | |
1275 | if(qKey32!=rKey32) { | |
1276 | uprv_memcpy(qKey32, rKey32, 4*count); | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | /* resources */ | |
1281 | { | |
1282 | Resource *r; | |
1283 | ||
1284 | ||
1285 | if(p!=q) { | |
1286 | r=q; | |
1287 | } else { | |
1288 | r=(Resource *)pTempTable->resort; | |
1289 | } | |
1290 | for(i=0; i<count; ++i) { | |
1291 | oldIndex=pTempTable->rows[i].sortIndex; | |
1292 | ds->swapArray32(ds, p+oldIndex, 4, r+i, pErrorCode); | |
1293 | } | |
1294 | if(q!=r) { | |
1295 | uprv_memcpy(q, r, 4*count); | |
1296 | } | |
1297 | } | |
1298 | } | |
1299 | break; | |
1300 | case URES_ARRAY: | |
1301 | { | |
1302 | Resource item; | |
1303 | int32_t i; | |
1304 | ||
1305 | count=udata_readInt32(ds, (int32_t)*p); | |
1306 | /* swap length */ | |
1307 | ds->swapArray32(ds, p++, 4, q++, pErrorCode); | |
1308 | ||
1309 | /* recurse */ | |
1310 | for(i=0; i<count; ++i) { | |
1311 | item=ds->readUInt32(p[i]); | |
729e4ab9 | 1312 | ures_swapResource(ds, inBundle, outBundle, item, NULL, pTempTable, pErrorCode); |
374ca955 | 1313 | if(U_FAILURE(*pErrorCode)) { |
73c04bcf A |
1314 | udata_printError(ds, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n", |
1315 | res, i, item); | |
374ca955 A |
1316 | return; |
1317 | } | |
1318 | } | |
1319 | ||
1320 | /* swap items */ | |
1321 | ds->swapArray32(ds, p, 4*count, q, pErrorCode); | |
1322 | } | |
1323 | break; | |
1324 | case URES_INT_VECTOR: | |
1325 | count=udata_readInt32(ds, (int32_t)*p); | |
1326 | /* swap length and each integer */ | |
1327 | ds->swapArray32(ds, p, 4*(1+count), q, pErrorCode); | |
1328 | break; | |
1329 | default: | |
1330 | /* also catches RES_BOGUS */ | |
1331 | *pErrorCode=U_UNSUPPORTED_ERROR; | |
1332 | break; | |
1333 | } | |
1334 | } | |
1335 | ||
1336 | U_CAPI int32_t U_EXPORT2 | |
1337 | ures_swap(const UDataSwapper *ds, | |
1338 | const void *inData, int32_t length, void *outData, | |
1339 | UErrorCode *pErrorCode) { | |
1340 | const UDataInfo *pInfo; | |
1341 | const Resource *inBundle; | |
1342 | Resource rootRes; | |
1343 | int32_t headerSize, maxTableLength; | |
1344 | ||
1345 | Row rows[STACK_ROW_CAPACITY]; | |
1346 | int32_t resort[STACK_ROW_CAPACITY]; | |
1347 | TempTable tempTable; | |
1348 | ||
729e4ab9 A |
1349 | const int32_t *inIndexes; |
1350 | ||
374ca955 | 1351 | /* the following integers count Resource item offsets (4 bytes each), not bytes */ |
729e4ab9 | 1352 | int32_t bundleLength, indexLength, keysBottom, keysTop, resBottom, top; |
374ca955 A |
1353 | |
1354 | /* udata_swapDataHeader checks the arguments */ | |
1355 | headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode); | |
1356 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | /* check data format and format version */ | |
1361 | pInfo=(const UDataInfo *)((const char *)inData+4); | |
1362 | if(!( | |
1363 | pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */ | |
1364 | pInfo->dataFormat[1]==0x65 && | |
1365 | pInfo->dataFormat[2]==0x73 && | |
1366 | pInfo->dataFormat[3]==0x42 && | |
2ca993e8 A |
1367 | /* formatVersion 1.1+ or 2.x or 3.x */ |
1368 | ((pInfo->formatVersion[0]==1 && pInfo->formatVersion[1]>=1) || | |
1369 | pInfo->formatVersion[0]==2 || pInfo->formatVersion[0]==3) | |
374ca955 | 1370 | )) { |
729e4ab9 | 1371 | udata_printError(ds, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n", |
374ca955 A |
1372 | pInfo->dataFormat[0], pInfo->dataFormat[1], |
1373 | pInfo->dataFormat[2], pInfo->dataFormat[3], | |
729e4ab9 | 1374 | pInfo->formatVersion[0], pInfo->formatVersion[1]); |
374ca955 A |
1375 | *pErrorCode=U_UNSUPPORTED_ERROR; |
1376 | return 0; | |
1377 | } | |
729e4ab9 | 1378 | tempTable.majorFormatVersion=pInfo->formatVersion[0]; |
374ca955 A |
1379 | |
1380 | /* a resource bundle must contain at least one resource item */ | |
1381 | if(length<0) { | |
1382 | bundleLength=-1; | |
1383 | } else { | |
1384 | bundleLength=(length-headerSize)/4; | |
1385 | ||
1386 | /* formatVersion 1.1 must have a root item and at least 5 indexes */ | |
729e4ab9 | 1387 | if(bundleLength<(1+5)) { |
374ca955 A |
1388 | udata_printError(ds, "ures_swap(): too few bytes (%d after header) for a resource bundle\n", |
1389 | length-headerSize); | |
1390 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
1391 | return 0; | |
1392 | } | |
1393 | } | |
1394 | ||
1395 | inBundle=(const Resource *)((const char *)inData+headerSize); | |
1396 | rootRes=ds->readUInt32(*inBundle); | |
1397 | ||
729e4ab9 A |
1398 | /* formatVersion 1.1 adds the indexes[] array */ |
1399 | inIndexes=(const int32_t *)(inBundle+1); | |
374ca955 | 1400 | |
729e4ab9 A |
1401 | indexLength=udata_readInt32(ds, inIndexes[URES_INDEX_LENGTH])&0xff; |
1402 | if(indexLength<=URES_INDEX_MAX_TABLE_LENGTH) { | |
1403 | udata_printError(ds, "ures_swap(): too few indexes for a 1.1+ resource bundle\n"); | |
1404 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
1405 | return 0; | |
1406 | } | |
1407 | keysBottom=1+indexLength; | |
1408 | keysTop=udata_readInt32(ds, inIndexes[URES_INDEX_KEYS_TOP]); | |
1409 | if(indexLength>URES_INDEX_16BIT_TOP) { | |
1410 | resBottom=udata_readInt32(ds, inIndexes[URES_INDEX_16BIT_TOP]); | |
1411 | } else { | |
1412 | resBottom=keysTop; | |
1413 | } | |
1414 | top=udata_readInt32(ds, inIndexes[URES_INDEX_BUNDLE_TOP]); | |
1415 | maxTableLength=udata_readInt32(ds, inIndexes[URES_INDEX_MAX_TABLE_LENGTH]); | |
374ca955 | 1416 | |
729e4ab9 A |
1417 | if(0<=bundleLength && bundleLength<top) { |
1418 | udata_printError(ds, "ures_swap(): resource top %d exceeds bundle length %d\n", | |
1419 | top, bundleLength); | |
1420 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
1421 | return 0; | |
1422 | } | |
1423 | if(keysTop>(1+indexLength)) { | |
1424 | tempTable.localKeyLimit=keysTop<<2; | |
1425 | } else { | |
1426 | tempTable.localKeyLimit=0; | |
374ca955 A |
1427 | } |
1428 | ||
1429 | if(length>=0) { | |
1430 | Resource *outBundle=(Resource *)((char *)outData+headerSize); | |
1431 | ||
729e4ab9 A |
1432 | /* track which resources we have already swapped */ |
1433 | uint32_t stackResFlags[STACK_ROW_CAPACITY]; | |
1434 | int32_t resFlagsLength; | |
1435 | ||
1436 | /* | |
1437 | * We need one bit per 4 resource bundle bytes so that we can track | |
1438 | * every possible Resource for whether we have swapped it already. | |
1439 | * Multiple Resource words can refer to the same bundle offsets | |
1440 | * for sharing identical values. | |
1441 | * We could optimize this by allocating only for locations above | |
1442 | * where Resource values are stored (above keys & strings). | |
1443 | */ | |
1444 | resFlagsLength=(length+31)>>5; /* number of bytes needed */ | |
1445 | resFlagsLength=(resFlagsLength+3)&~3; /* multiple of 4 bytes for uint32_t */ | |
2ca993e8 | 1446 | if(resFlagsLength<=(int32_t)sizeof(stackResFlags)) { |
729e4ab9 A |
1447 | tempTable.resFlags=stackResFlags; |
1448 | } else { | |
1449 | tempTable.resFlags=(uint32_t *)uprv_malloc(resFlagsLength); | |
1450 | if(tempTable.resFlags==NULL) { | |
1451 | udata_printError(ds, "ures_swap(): unable to allocate memory for tracking resources\n"); | |
1452 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
1453 | return 0; | |
1454 | } | |
1455 | } | |
1456 | uprv_memset(tempTable.resFlags, 0, resFlagsLength); | |
1457 | ||
374ca955 A |
1458 | /* copy the bundle for binary and inaccessible data */ |
1459 | if(inData!=outData) { | |
1460 | uprv_memcpy(outBundle, inBundle, 4*top); | |
1461 | } | |
1462 | ||
1463 | /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */ | |
729e4ab9 A |
1464 | udata_swapInvStringBlock(ds, inBundle+keysBottom, 4*(keysTop-keysBottom), |
1465 | outBundle+keysBottom, pErrorCode); | |
374ca955 | 1466 | if(U_FAILURE(*pErrorCode)) { |
729e4ab9 | 1467 | udata_printError(ds, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop-keysBottom)); |
374ca955 A |
1468 | return 0; |
1469 | } | |
1470 | ||
729e4ab9 A |
1471 | /* swap the 16-bit units (strings, table16, array16) */ |
1472 | if(keysTop<resBottom) { | |
1473 | ds->swapArray16(ds, inBundle+keysTop, (resBottom-keysTop)*4, outBundle+keysTop, pErrorCode); | |
1474 | if(U_FAILURE(*pErrorCode)) { | |
1475 | udata_printError(ds, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom-keysTop)); | |
1476 | return 0; | |
1477 | } | |
1478 | } | |
1479 | ||
374ca955 A |
1480 | /* allocate the temporary table for sorting resource tables */ |
1481 | tempTable.keyChars=(const char *)outBundle; /* sort by outCharset */ | |
729e4ab9 | 1482 | if(tempTable.majorFormatVersion>1 || maxTableLength<=STACK_ROW_CAPACITY) { |
374ca955 A |
1483 | tempTable.rows=rows; |
1484 | tempTable.resort=resort; | |
1485 | } else { | |
1486 | tempTable.rows=(Row *)uprv_malloc(maxTableLength*sizeof(Row)+maxTableLength*4); | |
1487 | if(tempTable.rows==NULL) { | |
1488 | udata_printError(ds, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n", | |
1489 | maxTableLength); | |
1490 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
729e4ab9 A |
1491 | if(tempTable.resFlags!=stackResFlags) { |
1492 | uprv_free(tempTable.resFlags); | |
1493 | } | |
374ca955 A |
1494 | return 0; |
1495 | } | |
1496 | tempTable.resort=(int32_t *)(tempTable.rows+maxTableLength); | |
1497 | } | |
1498 | ||
1499 | /* swap the resources */ | |
729e4ab9 | 1500 | ures_swapResource(ds, inBundle, outBundle, rootRes, NULL, &tempTable, pErrorCode); |
374ca955 | 1501 | if(U_FAILURE(*pErrorCode)) { |
73c04bcf A |
1502 | udata_printError(ds, "ures_swapResource(root res=%08x) failed\n", |
1503 | rootRes); | |
374ca955 A |
1504 | } |
1505 | ||
1506 | if(tempTable.rows!=rows) { | |
1507 | uprv_free(tempTable.rows); | |
1508 | } | |
729e4ab9 A |
1509 | if(tempTable.resFlags!=stackResFlags) { |
1510 | uprv_free(tempTable.resFlags); | |
1511 | } | |
374ca955 A |
1512 | |
1513 | /* swap the root resource and indexes */ | |
729e4ab9 | 1514 | ds->swapArray32(ds, inBundle, keysBottom*4, outBundle, pErrorCode); |
374ca955 A |
1515 | } |
1516 | ||
1517 | return headerSize+4*top; | |
b75a7d8f | 1518 | } |