1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1999-2011, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************/
12 /*------------------------------------------------------------------------------
14 * UCommonData An abstract interface for dealing with ICU Common Data Files.
15 * ICU Common Data Files are a grouping of a number of individual
16 * data items (resources, converters, tables, anything) into a
17 * single file or dll. The combined format includes a table of
18 * contents for locating the individual items by name.
20 * Two formats for the table of contents are supported, which is
21 * why there is an abstract inteface involved.
25 #include "unicode/utypes.h"
26 #include "unicode/udata.h"
31 #if defined(UDATA_DEBUG) || defined(UDATA_DEBUG_DUMP)
36 udata_getHeaderSize(const DataHeader
*udh
) {
39 } else if(udh
->info
.isBigEndian
==U_IS_BIG_ENDIAN
) {
41 return udh
->dataHeader
.headerSize
;
43 /* opposite endianness */
44 uint16_t x
=udh
->dataHeader
.headerSize
;
45 return (uint16_t)((x
<<8)|(x
>>8));
50 udata_getInfoSize(const UDataInfo
*info
) {
53 } else if(info
->isBigEndian
==U_IS_BIG_ENDIAN
) {
57 /* opposite endianness */
58 uint16_t x
=info
->size
;
59 return (uint16_t)((x
<<8)|(x
>>8));
63 /*-----------------------------------------------------------------------------*
65 * Pointer TOCs. TODO: This form of table-of-contents should be removed *
66 * because DLLs must be relocated on loading to correct the *
67 * pointer values and this operation makes shared memory *
68 * mapping of the data much less likely to work. *
70 *-----------------------------------------------------------------------------*/
72 const char *entryName
;
73 const DataHeader
*pHeader
;
81 * Variable-length array declared with length 1 to disable bounds checkers.
82 * The actual array length is in the count field.
84 PointerTOCEntry entry
[1];
88 /* definition of OffsetTOC struct types moved to ucmndata.h */
90 /*-----------------------------------------------------------------------------*
92 * entry point lookup implementations *
94 *-----------------------------------------------------------------------------*/
97 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
101 * Compare strings where we know the shared prefix length,
102 * and advance the prefix length as we find that the strings share even more characters.
105 strcmpAfterPrefix(const char *s1
, const char *s2
, int32_t *pPrefixLength
) {
106 int32_t pl
=*pPrefixLength
;
111 int32_t c1
=(uint8_t)*s1
++;
112 int32_t c2
=(uint8_t)*s2
++;
114 if(cmp
!=0 || c1
==0) { /* different or done */
117 ++pl
; /* increment shared same-prefix length */
124 offsetTOCPrefixBinarySearch(const char *s
, const char *names
,
125 const UDataOffsetTOCEntry
*toc
, int32_t count
) {
129 * Remember the shared prefix between s, start and limit,
130 * and don't compare that shared prefix again.
131 * The shared prefix should get longer as we narrow the [start, limit[ range.
133 int32_t startPrefixLength
=0;
134 int32_t limitPrefixLength
=0;
139 * Prime the prefix lengths so that we don't keep prefixLength at 0 until
140 * both the start and limit indexes have moved.
141 * At the same time, we find if s is one of the start and (limit-1) names,
142 * and if not, exclude them from the actual binary search.
144 if(0==strcmpAfterPrefix(s
, names
+toc
[0].nameOffset
, &startPrefixLength
)) {
149 if(0==strcmpAfterPrefix(s
, names
+toc
[limit
].nameOffset
, &limitPrefixLength
)) {
153 int32_t i
=(start
+limit
)/2;
154 int32_t prefixLength
=MIN(startPrefixLength
, limitPrefixLength
);
155 int32_t cmp
=strcmpAfterPrefix(s
, names
+toc
[i
].nameOffset
, &prefixLength
);
158 limitPrefixLength
=prefixLength
;
163 startPrefixLength
=prefixLength
;
170 pointerTOCPrefixBinarySearch(const char *s
, const PointerTOCEntry
*toc
, int32_t count
) {
174 * Remember the shared prefix between s, start and limit,
175 * and don't compare that shared prefix again.
176 * The shared prefix should get longer as we narrow the [start, limit[ range.
178 int32_t startPrefixLength
=0;
179 int32_t limitPrefixLength
=0;
184 * Prime the prefix lengths so that we don't keep prefixLength at 0 until
185 * both the start and limit indexes have moved.
186 * At the same time, we find if s is one of the start and (limit-1) names,
187 * and if not, exclude them from the actual binary search.
189 if(0==strcmpAfterPrefix(s
, toc
[0].entryName
, &startPrefixLength
)) {
194 if(0==strcmpAfterPrefix(s
, toc
[limit
].entryName
, &limitPrefixLength
)) {
198 int32_t i
=(start
+limit
)/2;
199 int32_t prefixLength
=MIN(startPrefixLength
, limitPrefixLength
);
200 int32_t cmp
=strcmpAfterPrefix(s
, toc
[i
].entryName
, &prefixLength
);
203 limitPrefixLength
=prefixLength
;
208 startPrefixLength
=prefixLength
;
215 static uint32_t U_CALLCONV
216 offsetTOCEntryCount(const UDataMemory
*pData
) {
218 const UDataOffsetTOC
*toc
= (UDataOffsetTOC
*)pData
->toc
;
225 static const DataHeader
* U_CALLCONV
226 offsetTOCLookupFn(const UDataMemory
*pData
,
227 const char *tocEntryName
,
229 UErrorCode
*pErrorCode
) {
231 const UDataOffsetTOC
*toc
= (UDataOffsetTOC
*)pData
->toc
;
233 const char *base
=(const char *)toc
;
234 int32_t number
, count
=(int32_t)toc
->count
;
236 /* perform a binary search for the data in the common data's table of contents */
237 #if defined (UDATA_DEBUG_DUMP)
238 /* list the contents of the TOC each time .. not recommended */
239 for(number
=0; number
<count
; ++number
) {
240 fprintf(stderr
, "\tx%d: %s\n", number
, &base
[toc
->entry
[number
].nameOffset
]);
243 number
=offsetTOCPrefixBinarySearch(tocEntryName
, base
, toc
->entry
, count
);
246 const UDataOffsetTOCEntry
*entry
=toc
->entry
+number
;
248 fprintf(stderr
, "%s: Found.\n", tocEntryName
);
250 if((number
+1) < count
) {
251 *pLength
= (int32_t)(entry
[1].dataOffset
- entry
->dataOffset
);
255 return (const DataHeader
*)(base
+entry
->dataOffset
);
258 fprintf(stderr
, "%s: Not found.\n", tocEntryName
);
264 fprintf(stderr
, "returning header\n");
267 return pData
->pHeader
;
272 static uint32_t U_CALLCONV
pointerTOCEntryCount(const UDataMemory
*pData
) {
273 const PointerTOC
*toc
= (PointerTOC
*)pData
->toc
;
274 return (uint32_t)((toc
!= NULL
) ? (toc
->count
) : 0);
277 static const DataHeader
* U_CALLCONV
pointerTOCLookupFn(const UDataMemory
*pData
,
280 UErrorCode
*pErrorCode
) {
282 if(pData
->toc
!=NULL
) {
283 const PointerTOC
*toc
= (PointerTOC
*)pData
->toc
;
284 int32_t number
, count
=(int32_t)toc
->count
;
286 #if defined (UDATA_DEBUG_DUMP)
287 /* list the contents of the TOC each time .. not recommended */
288 for(number
=0; number
<count
; ++number
) {
289 fprintf(stderr
, "\tx%d: %s\n", number
, toc
->entry
[number
].entryName
);
292 number
=pointerTOCPrefixBinarySearch(name
, toc
->entry
, count
);
296 fprintf(stderr
, "%s: Found.\n", toc
->entry
[number
].entryName
);
299 return UDataMemory_normalizeDataPointer(toc
->entry
[number
].pHeader
);
302 fprintf(stderr
, "%s: Not found.\n", name
);
307 return pData
->pHeader
;
313 static const commonDataFuncs CmnDFuncs
= {offsetTOCLookupFn
, offsetTOCEntryCount
};
314 static const commonDataFuncs ToCPFuncs
= {pointerTOCLookupFn
, pointerTOCEntryCount
};
318 /*----------------------------------------------------------------------*
320 * checkCommonData Validate the format of a common data file. *
321 * Fill in the virtual function ptr based on TOC type *
322 * If the data is invalid, close the UDataMemory *
323 * and set the appropriate error code. *
325 *----------------------------------------------------------------------*/
326 U_CFUNC
void udata_checkCommonData(UDataMemory
*udm
, UErrorCode
*err
) {
327 if (U_FAILURE(*err
)) {
331 if(udm
==NULL
|| udm
->pHeader
==NULL
) {
332 *err
=U_INVALID_FORMAT_ERROR
;
333 } else if(!(udm
->pHeader
->dataHeader
.magic1
==0xda &&
334 udm
->pHeader
->dataHeader
.magic2
==0x27 &&
335 udm
->pHeader
->info
.isBigEndian
==U_IS_BIG_ENDIAN
&&
336 udm
->pHeader
->info
.charsetFamily
==U_CHARSET_FAMILY
)
338 /* header not valid */
339 *err
=U_INVALID_FORMAT_ERROR
;
341 else if (udm
->pHeader
->info
.dataFormat
[0]==0x43 &&
342 udm
->pHeader
->info
.dataFormat
[1]==0x6d &&
343 udm
->pHeader
->info
.dataFormat
[2]==0x6e &&
344 udm
->pHeader
->info
.dataFormat
[3]==0x44 &&
345 udm
->pHeader
->info
.formatVersion
[0]==1
347 /* dataFormat="CmnD" */
348 udm
->vFuncs
= &CmnDFuncs
;
349 udm
->toc
=(const char *)udm
->pHeader
+udata_getHeaderSize(udm
->pHeader
);
351 else if(udm
->pHeader
->info
.dataFormat
[0]==0x54 &&
352 udm
->pHeader
->info
.dataFormat
[1]==0x6f &&
353 udm
->pHeader
->info
.dataFormat
[2]==0x43 &&
354 udm
->pHeader
->info
.dataFormat
[3]==0x50 &&
355 udm
->pHeader
->info
.formatVersion
[0]==1
357 /* dataFormat="ToCP" */
358 udm
->vFuncs
= &ToCPFuncs
;
359 udm
->toc
=(const char *)udm
->pHeader
+udata_getHeaderSize(udm
->pHeader
);
362 /* dataFormat not recognized */
363 *err
=U_INVALID_FORMAT_ERROR
;
366 if (U_FAILURE(*err
)) {
367 /* If the data is no good and we memory-mapped it ourselves,
368 * close the memory mapping so it doesn't leak. Note that this has
369 * no effect on non-memory mapped data, other than clearing fields in udm.
376 * TODO: Add a udata_swapPackageHeader() function that swaps an ICU .dat package
377 * header but not its sub-items.
378 * This function will be needed for automatic runtime swapping.
379 * Sub-items should not be swapped to limit the swapping to the parts of the
380 * package that are actually used.
382 * Since lengths of items are implicit in the order and offsets of their
383 * ToC entries, and since offsets are relative to the start of the ToC,
384 * a swapped version may need to generate a different data structure
385 * with pointers to the original data items and with their lengths
386 * (-1 for the last one if it is not known), and maybe even pointers to the
387 * swapped versions of the items.
388 * These pointers to swapped versions would establish a cache;
389 * instead, each open data item could simply own the storage for its swapped
390 * data. This fits better with the current design.
392 * markus 2003sep18 Jitterbug 2235