ucmndata.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 1999-2011, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************/
  10. /*------------------------------------------------------------------------------
  11. *
  12. * UCommonData An abstract interface for dealing with ICU Common Data Files.
  13. * ICU Common Data Files are a grouping of a number of individual
  14. * data items (resources, converters, tables, anything) into a
  15. * single file or dll. The combined format includes a table of
  16. * contents for locating the individual items by name.
  17. *
  18. * Two formats for the table of contents are supported, which is
  19. * why there is an abstract interface involved.
  20. *
  21. */
  22. #include "unicode/utypes.h"
  23. #include "unicode/udata.h"
  24. #include "cstring.h"
  25. #include "ucmndata.h"
  26. #include "udatamem.h"
  27. #if defined(UDATA_DEBUG) || defined(UDATA_DEBUG_DUMP)
  28. # include <stdio.h>
  29. #endif
  30. U_CFUNC uint16_t
  31. udata_getHeaderSize(const DataHeader *udh) {
  32. if(udh==nullptr) {
  33. return 0;
  34. } else if(udh->info.isBigEndian==U_IS_BIG_ENDIAN) {
  35. /* same endianness */
  36. return udh->dataHeader.headerSize;
  37. } else {
  38. /* opposite endianness */
  39. uint16_t x=udh->dataHeader.headerSize;
  40. return (uint16_t)((x<<8)|(x>>8));
  41. }
  42. }
  43. U_CFUNC uint16_t
  44. udata_getInfoSize(const UDataInfo *info) {
  45. if(info==nullptr) {
  46. return 0;
  47. } else if(info->isBigEndian==U_IS_BIG_ENDIAN) {
  48. /* same endianness */
  49. return info->size;
  50. } else {
  51. /* opposite endianness */
  52. uint16_t x=info->size;
  53. return (uint16_t)((x<<8)|(x>>8));
  54. }
  55. }
  56. /*-----------------------------------------------------------------------------*
  57. * *
  58. * Pointer TOCs. TODO: This form of table-of-contents should be removed *
  59. * because DLLs must be relocated on loading to correct the *
  60. * pointer values and this operation makes shared memory *
  61. * mapping of the data much less likely to work. *
  62. * *
  63. *-----------------------------------------------------------------------------*/
  64. typedef struct {
  65. const char *entryName;
  66. const DataHeader *pHeader;
  67. } PointerTOCEntry;
  68. typedef struct {
  69. uint32_t count;
  70. uint32_t reserved;
  71. /**
  72. * Variable-length array declared with length 1 to disable bounds checkers.
  73. * The actual array length is in the count field.
  74. */
  75. PointerTOCEntry entry[1];
  76. } PointerTOC;
  77. /* definition of OffsetTOC struct types moved to ucmndata.h */
  78. /*-----------------------------------------------------------------------------*
  79. * *
  80. * entry point lookup implementations *
  81. * *
  82. *-----------------------------------------------------------------------------*/
  83. #ifndef MIN
  84. #define MIN(a,b) (((a)<(b)) ? (a) : (b))
  85. #endif
  86. /**
  87. * Compare strings where we know the shared prefix length,
  88. * and advance the prefix length as we find that the strings share even more characters.
  89. */
  90. static int32_t
  91. strcmpAfterPrefix(const char *s1, const char *s2, int32_t *pPrefixLength) {
  92. int32_t pl=*pPrefixLength;
  93. int32_t cmp=0;
  94. s1+=pl;
  95. s2+=pl;
  96. for(;;) {
  97. int32_t c1=(uint8_t)*s1++;
  98. int32_t c2=(uint8_t)*s2++;
  99. cmp=c1-c2;
  100. if(cmp!=0 || c1==0) { /* different or done */
  101. break;
  102. }
  103. ++pl; /* increment shared same-prefix length */
  104. }
  105. *pPrefixLength=pl;
  106. return cmp;
  107. }
  108. static int32_t
  109. offsetTOCPrefixBinarySearch(const char *s, const char *names,
  110. const UDataOffsetTOCEntry *toc, int32_t count) {
  111. int32_t start=0;
  112. int32_t limit=count;
  113. /*
  114. * Remember the shared prefix between s, start and limit,
  115. * and don't compare that shared prefix again.
  116. * The shared prefix should get longer as we narrow the [start, limit[ range.
  117. */
  118. int32_t startPrefixLength=0;
  119. int32_t limitPrefixLength=0;
  120. if(count==0) {
  121. return -1;
  122. }
  123. /*
  124. * Prime the prefix lengths so that we don't keep prefixLength at 0 until
  125. * both the start and limit indexes have moved.
  126. * At the same time, we find if s is one of the start and (limit-1) names,
  127. * and if not, exclude them from the actual binary search.
  128. */
  129. if(0==strcmpAfterPrefix(s, names+toc[0].nameOffset, &startPrefixLength)) {
  130. return 0;
  131. }
  132. ++start;
  133. --limit;
  134. if(0==strcmpAfterPrefix(s, names+toc[limit].nameOffset, &limitPrefixLength)) {
  135. return limit;
  136. }
  137. while(start<limit) {
  138. int32_t i=(start+limit)/2;
  139. int32_t prefixLength=MIN(startPrefixLength, limitPrefixLength);
  140. int32_t cmp=strcmpAfterPrefix(s, names+toc[i].nameOffset, &prefixLength);
  141. if(cmp<0) {
  142. limit=i;
  143. limitPrefixLength=prefixLength;
  144. } else if(cmp==0) {
  145. return i;
  146. } else {
  147. start=i+1;
  148. startPrefixLength=prefixLength;
  149. }
  150. }
  151. return -1;
  152. }
  153. static int32_t
  154. pointerTOCPrefixBinarySearch(const char *s, const PointerTOCEntry *toc, int32_t count) {
  155. int32_t start=0;
  156. int32_t limit=count;
  157. /*
  158. * Remember the shared prefix between s, start and limit,
  159. * and don't compare that shared prefix again.
  160. * The shared prefix should get longer as we narrow the [start, limit[ range.
  161. */
  162. int32_t startPrefixLength=0;
  163. int32_t limitPrefixLength=0;
  164. if(count==0) {
  165. return -1;
  166. }
  167. /*
  168. * Prime the prefix lengths so that we don't keep prefixLength at 0 until
  169. * both the start and limit indexes have moved.
  170. * At the same time, we find if s is one of the start and (limit-1) names,
  171. * and if not, exclude them from the actual binary search.
  172. */
  173. if(0==strcmpAfterPrefix(s, toc[0].entryName, &startPrefixLength)) {
  174. return 0;
  175. }
  176. ++start;
  177. --limit;
  178. if(0==strcmpAfterPrefix(s, toc[limit].entryName, &limitPrefixLength)) {
  179. return limit;
  180. }
  181. while(start<limit) {
  182. int32_t i=(start+limit)/2;
  183. int32_t prefixLength=MIN(startPrefixLength, limitPrefixLength);
  184. int32_t cmp=strcmpAfterPrefix(s, toc[i].entryName, &prefixLength);
  185. if(cmp<0) {
  186. limit=i;
  187. limitPrefixLength=prefixLength;
  188. } else if(cmp==0) {
  189. return i;
  190. } else {
  191. start=i+1;
  192. startPrefixLength=prefixLength;
  193. }
  194. }
  195. return -1;
  196. }
  197. U_CDECL_BEGIN
  198. static uint32_t U_CALLCONV
  199. offsetTOCEntryCount(const UDataMemory *pData) {
  200. int32_t retVal=0;
  201. const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
  202. if (toc != nullptr) {
  203. retVal = toc->count;
  204. }
  205. return retVal;
  206. }
  207. static const DataHeader * U_CALLCONV
  208. offsetTOCLookupFn(const UDataMemory *pData,
  209. const char *tocEntryName,
  210. int32_t *pLength,
  211. UErrorCode *pErrorCode) {
  212. (void)pErrorCode;
  213. const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
  214. if(toc!=nullptr) {
  215. const char *base=(const char *)toc;
  216. int32_t number, count=(int32_t)toc->count;
  217. /* perform a binary search for the data in the common data's table of contents */
  218. #if defined (UDATA_DEBUG_DUMP)
  219. /* list the contents of the TOC each time .. not recommended */
  220. for(number=0; number<count; ++number) {
  221. fprintf(stderr, "\tx%d: %s\n", number, &base[toc->entry[number].nameOffset]);
  222. }
  223. #endif
  224. number=offsetTOCPrefixBinarySearch(tocEntryName, base, toc->entry, count);
  225. if(number>=0) {
  226. /* found it */
  227. const UDataOffsetTOCEntry *entry=toc->entry+number;
  228. #ifdef UDATA_DEBUG
  229. fprintf(stderr, "%s: Found.\n", tocEntryName);
  230. #endif
  231. if((number+1) < count) {
  232. *pLength = (int32_t)(entry[1].dataOffset - entry->dataOffset);
  233. } else {
  234. *pLength = -1;
  235. }
  236. return (const DataHeader *)(base+entry->dataOffset);
  237. } else {
  238. #ifdef UDATA_DEBUG
  239. fprintf(stderr, "%s: Not found.\n", tocEntryName);
  240. #endif
  241. return nullptr;
  242. }
  243. } else {
  244. #ifdef UDATA_DEBUG
  245. fprintf(stderr, "returning header\n");
  246. #endif
  247. return pData->pHeader;
  248. }
  249. }
  250. static uint32_t U_CALLCONV pointerTOCEntryCount(const UDataMemory *pData) {
  251. const PointerTOC *toc = (PointerTOC *)pData->toc;
  252. return (uint32_t)((toc != nullptr) ? (toc->count) : 0);
  253. }
  254. static const DataHeader * U_CALLCONV pointerTOCLookupFn(const UDataMemory *pData,
  255. const char *name,
  256. int32_t *pLength,
  257. UErrorCode *pErrorCode) {
  258. (void)pErrorCode;
  259. if(pData->toc!=nullptr) {
  260. const PointerTOC *toc = (PointerTOC *)pData->toc;
  261. int32_t number, count=(int32_t)toc->count;
  262. #if defined (UDATA_DEBUG_DUMP)
  263. /* list the contents of the TOC each time .. not recommended */
  264. for(number=0; number<count; ++number) {
  265. fprintf(stderr, "\tx%d: %s\n", number, toc->entry[number].entryName);
  266. }
  267. #endif
  268. number=pointerTOCPrefixBinarySearch(name, toc->entry, count);
  269. if(number>=0) {
  270. /* found it */
  271. #ifdef UDATA_DEBUG
  272. fprintf(stderr, "%s: Found.\n", toc->entry[number].entryName);
  273. #endif
  274. *pLength=-1;
  275. return UDataMemory_normalizeDataPointer(toc->entry[number].pHeader);
  276. } else {
  277. #ifdef UDATA_DEBUG
  278. fprintf(stderr, "%s: Not found.\n", name);
  279. #endif
  280. return nullptr;
  281. }
  282. } else {
  283. return pData->pHeader;
  284. }
  285. }
  286. U_CDECL_END
  287. static const commonDataFuncs CmnDFuncs = {offsetTOCLookupFn, offsetTOCEntryCount};
  288. static const commonDataFuncs ToCPFuncs = {pointerTOCLookupFn, pointerTOCEntryCount};
  289. /*----------------------------------------------------------------------*
  290. * *
  291. * checkCommonData Validate the format of a common data file. *
  292. * Fill in the virtual function ptr based on TOC type *
  293. * If the data is invalid, close the UDataMemory *
  294. * and set the appropriate error code. *
  295. * *
  296. *----------------------------------------------------------------------*/
  297. U_CFUNC void udata_checkCommonData(UDataMemory *udm, UErrorCode *err) {
  298. if (U_FAILURE(*err)) {
  299. return;
  300. }
  301. if(udm==nullptr || udm->pHeader==nullptr) {
  302. *err=U_INVALID_FORMAT_ERROR;
  303. } else if(!(udm->pHeader->dataHeader.magic1==0xda &&
  304. udm->pHeader->dataHeader.magic2==0x27 &&
  305. udm->pHeader->info.isBigEndian==U_IS_BIG_ENDIAN &&
  306. udm->pHeader->info.charsetFamily==U_CHARSET_FAMILY)
  307. ) {
  308. /* header not valid */
  309. *err=U_INVALID_FORMAT_ERROR;
  310. }
  311. else if (udm->pHeader->info.dataFormat[0]==0x43 &&
  312. udm->pHeader->info.dataFormat[1]==0x6d &&
  313. udm->pHeader->info.dataFormat[2]==0x6e &&
  314. udm->pHeader->info.dataFormat[3]==0x44 &&
  315. udm->pHeader->info.formatVersion[0]==1
  316. ) {
  317. /* dataFormat="CmnD" */
  318. udm->vFuncs = &CmnDFuncs;
  319. udm->toc=(const char *)udm->pHeader+udata_getHeaderSize(udm->pHeader);
  320. }
  321. else if(udm->pHeader->info.dataFormat[0]==0x54 &&
  322. udm->pHeader->info.dataFormat[1]==0x6f &&
  323. udm->pHeader->info.dataFormat[2]==0x43 &&
  324. udm->pHeader->info.dataFormat[3]==0x50 &&
  325. udm->pHeader->info.formatVersion[0]==1
  326. ) {
  327. /* dataFormat="ToCP" */
  328. udm->vFuncs = &ToCPFuncs;
  329. udm->toc=(const char *)udm->pHeader+udata_getHeaderSize(udm->pHeader);
  330. }
  331. else {
  332. /* dataFormat not recognized */
  333. *err=U_INVALID_FORMAT_ERROR;
  334. }
  335. if (U_FAILURE(*err)) {
  336. /* If the data is no good and we memory-mapped it ourselves,
  337. * close the memory mapping so it doesn't leak. Note that this has
  338. * no effect on non-memory mapped data, other than clearing fields in udm.
  339. */
  340. udata_close(udm);
  341. }
  342. }
  343. /*
  344. * TODO: Add a udata_swapPackageHeader() function that swaps an ICU .dat package
  345. * header but not its sub-items.
  346. * This function will be needed for automatic runtime swapping.
  347. * Sub-items should not be swapped to limit the swapping to the parts of the
  348. * package that are actually used.
  349. *
  350. * Since lengths of items are implicit in the order and offsets of their
  351. * ToC entries, and since offsets are relative to the start of the ToC,
  352. * a swapped version may need to generate a different data structure
  353. * with pointers to the original data items and with their lengths
  354. * (-1 for the last one if it is not known), and maybe even pointers to the
  355. * swapped versions of the items.
  356. * These pointers to swapped versions would establish a cache;
  357. * instead, each open data item could simply own the storage for its swapped
  358. * data. This fits better with the current design.
  359. *
  360. * markus 2003sep18 Jitterbug 2235
  361. */