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