2 ******************************************************************************
4 * Copyright (C) 1999-2010, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************/
10 /*------------------------------------------------------------------------------
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.
18 * Two formats for the table of contents are supported, which is
19 * why there is an abstract inteface involved.
23 #include "unicode/utypes.h"
24 #include "unicode/udata.h"
29 #if defined(UDATA_DEBUG) || defined(UDATA_DEBUG_DUMP)
34 udata_getHeaderSize(const DataHeader
*udh
) {
37 } else if(udh
->info
.isBigEndian
==U_IS_BIG_ENDIAN
) {
39 return udh
->dataHeader
.headerSize
;
41 /* opposite endianness */
42 uint16_t x
=udh
->dataHeader
.headerSize
;
43 return (uint16_t)((x
<<8)|(x
>>8));
48 udata_getInfoSize(const UDataInfo
*info
) {
51 } else if(info
->isBigEndian
==U_IS_BIG_ENDIAN
) {
55 /* opposite endianness */
56 uint16_t x
=info
->size
;
57 return (uint16_t)((x
<<8)|(x
>>8));
61 /*-----------------------------------------------------------------------------*
63 * Pointer TOCs. TODO: This form of table-of-contents should be removed *
64 * because DLLs must be relocated on loading to correct the *
65 * pointer values and this operation makes shared memory *
66 * mapping of the data much less likely to work. *
68 *-----------------------------------------------------------------------------*/
70 const char *entryName
;
71 const DataHeader
*pHeader
;
78 PointerTOCEntry entry
[2]; /* Actual size is from count. */
82 /* definition of OffsetTOC struct types moved to ucmndata.h */
84 /*-----------------------------------------------------------------------------*
86 * entry point lookup implementations *
88 *-----------------------------------------------------------------------------*/
89 static uint32_t offsetTOCEntryCount(const UDataMemory
*pData
) {
91 const UDataOffsetTOC
*toc
= (UDataOffsetTOC
*)pData
->toc
;
99 static const DataHeader
*
100 offsetTOCLookupFn(const UDataMemory
*pData
,
101 const char *tocEntryName
,
103 UErrorCode
*pErrorCode
) {
104 const UDataOffsetTOC
*toc
= (UDataOffsetTOC
*)pData
->toc
;
106 const char *base
=(const char *)toc
;
107 uint32_t start
, limit
, number
, lastNumber
;
109 const UDataOffsetTOCEntry
*entry
;
111 /* perform a binary search for the data in the common data's table of contents */
112 #if defined (UDATA_DEBUG_DUMP)
113 /* list the contents of the TOC each time .. not recommended */
114 for(start
=0;start
<toc
->count
;start
++) {
115 fprintf(stderr
, "\tx%d: %s\n", start
, &base
[toc
->entry
[start
].nameOffset
]);
120 limit
=toc
->count
; /* number of names in this table of contents */
124 number
= (start
+limit
)/2;
125 if (lastNumber
== number
) { /* Have we moved? */
126 break; /* We haven't moved, and it wasn't found; */
127 /* or the empty stub common data library was used during build. */
130 strResult
= uprv_strcmp(tocEntryName
, base
+entry
[number
].nameOffset
);
133 } else if (strResult
>0) {
139 fprintf(stderr
, "%s: Found.\n", tocEntryName
);
141 entry
+= number
; /* Alias the entry to the current entry. */
142 if((number
+1) < toc
->count
) {
143 *pLength
= (int32_t)(entry
[1].dataOffset
- entry
->dataOffset
);
147 return (const DataHeader
*)(base
+entry
->dataOffset
);
151 fprintf(stderr
, "%s: Not found.\n", tocEntryName
);
156 fprintf(stderr
, "returning header\n");
159 return pData
->pHeader
;
164 static uint32_t pointerTOCEntryCount(const UDataMemory
*pData
) {
165 const PointerTOC
*toc
= (PointerTOC
*)pData
->toc
;
166 return (uint32_t)((toc
!= NULL
) ? (toc
->count
) : 0);
170 static const DataHeader
*pointerTOCLookupFn(const UDataMemory
*pData
,
173 UErrorCode
*pErrorCode
) {
174 if(pData
->toc
!=NULL
) {
175 const PointerTOC
*toc
= (PointerTOC
*)pData
->toc
;
176 uint32_t start
, limit
, number
, lastNumber
;
179 #if defined (UDATA_DEBUG_DUMP)
180 /* list the contents of the TOC each time .. not recommended */
181 for(start
=0;start
<toc
->count
;start
++) {
182 fprintf(stderr
, "\tx%d: %s\n", start
, toc
->entry
[start
].entryName
);
186 /* perform a binary search for the data in the common data's table of contents */
192 number
= (start
+limit
)/2;
193 if (lastNumber
== number
) { /* Have we moved? */
194 break; /* We haven't moved, and it wasn't found, */
195 /* or the empty stub common data library was used during build. */
198 strResult
= uprv_strcmp(name
, toc
->entry
[number
].entryName
);
201 } else if (strResult
>0) {
207 fprintf(stderr
, "%s: Found.\n", toc
->entry
[number
].entryName
);
210 return UDataMemory_normalizeDataPointer(toc
->entry
[number
].pHeader
);
214 fprintf(stderr
, "%s: Not found.\n", name
);
218 return pData
->pHeader
;
222 static const commonDataFuncs CmnDFuncs
= {offsetTOCLookupFn
, offsetTOCEntryCount
};
223 static const commonDataFuncs ToCPFuncs
= {pointerTOCLookupFn
, pointerTOCEntryCount
};
227 /*----------------------------------------------------------------------*
229 * checkCommonData Validate the format of a common data file. *
230 * Fill in the virtual function ptr based on TOC type *
231 * If the data is invalid, close the UDataMemory *
232 * and set the appropriate error code. *
234 *----------------------------------------------------------------------*/
235 U_CFUNC
void udata_checkCommonData(UDataMemory
*udm
, UErrorCode
*err
) {
236 if (U_FAILURE(*err
)) {
240 if(!(udm
->pHeader
->dataHeader
.magic1
==0xda &&
241 udm
->pHeader
->dataHeader
.magic2
==0x27 &&
242 udm
->pHeader
->info
.isBigEndian
==U_IS_BIG_ENDIAN
&&
243 udm
->pHeader
->info
.charsetFamily
==U_CHARSET_FAMILY
)
245 /* header not valid */
246 *err
=U_INVALID_FORMAT_ERROR
;
248 else if (udm
->pHeader
->info
.dataFormat
[0]==0x43 &&
249 udm
->pHeader
->info
.dataFormat
[1]==0x6d &&
250 udm
->pHeader
->info
.dataFormat
[2]==0x6e &&
251 udm
->pHeader
->info
.dataFormat
[3]==0x44 &&
252 udm
->pHeader
->info
.formatVersion
[0]==1
254 /* dataFormat="CmnD" */
255 udm
->vFuncs
= &CmnDFuncs
;
256 udm
->toc
=(const char *)udm
->pHeader
+udata_getHeaderSize(udm
->pHeader
);
258 else if(udm
->pHeader
->info
.dataFormat
[0]==0x54 &&
259 udm
->pHeader
->info
.dataFormat
[1]==0x6f &&
260 udm
->pHeader
->info
.dataFormat
[2]==0x43 &&
261 udm
->pHeader
->info
.dataFormat
[3]==0x50 &&
262 udm
->pHeader
->info
.formatVersion
[0]==1
264 /* dataFormat="ToCP" */
265 udm
->vFuncs
= &ToCPFuncs
;
266 udm
->toc
=(const char *)udm
->pHeader
+udata_getHeaderSize(udm
->pHeader
);
269 /* dataFormat not recognized */
270 *err
=U_INVALID_FORMAT_ERROR
;
273 if (U_FAILURE(*err
)) {
274 /* If the data is no good and we memory-mapped it ourselves,
275 * close the memory mapping so it doesn't leak. Note that this has
276 * no effect on non-memory mapped data, other than clearing fields in udm.
283 * TODO: Add a udata_swapPackageHeader() function that swaps an ICU .dat package
284 * header but not its sub-items.
285 * This function will be needed for automatic runtime swapping.
286 * Sub-items should not be swapped to limit the swapping to the parts of the
287 * package that are actually used.
289 * Since lengths of items are implicit in the order and offsets of their
290 * ToC entries, and since offsets are relative to the start of the ToC,
291 * a swapped version may need to generate a different data structure
292 * with pointers to the original data items and with their lengths
293 * (-1 for the last one if it is not known), and maybe even pointers to the
294 * swapped versions of the items.
295 * These pointers to swapped versions would establish a cache;
296 * instead, each open data item could simply own the storage for its swapped
297 * data. This fits better with the current design.
299 * markus 2003sep18 Jitterbug 2235