Coverage Report

Created: 2025-06-24 06:54

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