uresdata.cpp 50 KB

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