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