2001-09-07 18:42:29 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
2003-09-18 21:35:48 +00:00
|
|
|
* Copyright (C) 1999-2003, International Business Machines
|
2001-09-07 18:42:29 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-09-25 16:43:20 +00:00
|
|
|
******************************************************************************/
|
2001-09-07 18:42:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* UCommonData An abstract interface for dealing with ICU Common Data Files.
|
|
|
|
* ICU Common Data Files are a grouping of a number of individual
|
|
|
|
* data items (resources, converters, tables, anything) into a
|
|
|
|
* single file or dll. The combined format includes a table of
|
|
|
|
* contents for locating the individual items by name.
|
|
|
|
*
|
|
|
|
* Two formats for the table of contents are supported, which is
|
|
|
|
* why there is an abstract inteface involved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/udata.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "ucmndata.h"
|
|
|
|
#include "udatamem.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* Pointer TOCs. TODO: This form of table-of-contents should be removed because *
|
|
|
|
* DLLs must be relocated on loading to correct the pointer values *
|
|
|
|
* and this operation makes shared memory mapping of the data *
|
|
|
|
* much less likely to work. *
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------------------*/
|
|
|
|
typedef struct {
|
2002-04-03 00:45:35 +00:00
|
|
|
const char *entryName;
|
2001-09-07 18:42:29 +00:00
|
|
|
const DataHeader *pHeader;
|
|
|
|
} PointerTOCEntry;
|
|
|
|
|
|
|
|
|
2002-04-03 00:45:35 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t reserved;
|
2002-04-03 19:52:08 +00:00
|
|
|
PointerTOCEntry entry[2]; /* Actual size is from count. */
|
2002-04-03 00:45:35 +00:00
|
|
|
} PointerTOC;
|
|
|
|
|
|
|
|
|
2003-09-18 21:35:48 +00:00
|
|
|
/* definition of OffsetTOC struct types moved to ucmndata.h */
|
2002-04-03 00:45:35 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/*----------------------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* entry point lookup implementations *
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------------------*/
|
|
|
|
static uint32_t offsetTOCEntryCount(const UDataMemory *pData) {
|
2002-04-03 00:45:35 +00:00
|
|
|
int32_t retVal=0;
|
2003-09-18 21:35:48 +00:00
|
|
|
const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
|
2002-04-03 00:45:35 +00:00
|
|
|
if (toc != NULL) {
|
|
|
|
retVal = toc->count;
|
|
|
|
}
|
|
|
|
return retVal;
|
2001-09-07 18:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const DataHeader *
|
|
|
|
offsetTOCLookupFn(const UDataMemory *pData,
|
|
|
|
const char *tocEntryName,
|
|
|
|
UErrorCode *pErrorCode) {
|
2003-09-18 21:35:48 +00:00
|
|
|
const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
|
2002-04-03 00:45:35 +00:00
|
|
|
if(toc!=NULL) {
|
2001-09-07 18:42:29 +00:00
|
|
|
const char *base=(const char *)pData->toc;
|
|
|
|
uint32_t start, limit, number;
|
|
|
|
|
|
|
|
/* perform a binary search for the data in the common data's table of contents */
|
|
|
|
start=0;
|
2002-04-03 00:45:35 +00:00
|
|
|
limit=toc->count; /* number of names in this table of contents */
|
2001-09-07 18:42:29 +00:00
|
|
|
if (limit == 0) { /* Stub common data library used during build is empty. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while(start<limit-1) {
|
|
|
|
number=(start+limit)/2;
|
2002-04-03 00:45:35 +00:00
|
|
|
if(uprv_strcmp(tocEntryName, &base[toc->entry[number].nameOffset])<0) {
|
2001-09-07 18:42:29 +00:00
|
|
|
limit=number;
|
|
|
|
} else {
|
|
|
|
start=number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-03 00:45:35 +00:00
|
|
|
if(uprv_strcmp(tocEntryName, &base[toc->entry[start].nameOffset])==0) {
|
2001-09-07 18:42:29 +00:00
|
|
|
/* found it */
|
|
|
|
#ifdef UDATA_DEBUG
|
2002-07-17 03:01:45 +00:00
|
|
|
/* fprintf(stderr, "Found: %p\n",(base+toc[2*start+1])) */
|
|
|
|
fprintf(stderr, "Found it\n");
|
2001-09-07 18:42:29 +00:00
|
|
|
#endif
|
2002-04-03 00:45:35 +00:00
|
|
|
return (const DataHeader *)&base[toc->entry[start].dataOffset];
|
2001-09-07 18:42:29 +00:00
|
|
|
} else {
|
|
|
|
#ifdef UDATA_DEBUG
|
|
|
|
fprintf(stderr, "Not found.\n");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#ifdef UDATA_DEBUG
|
|
|
|
fprintf(stderr, "returning header\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return pData->pHeader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t pointerTOCEntryCount(const UDataMemory *pData) {
|
2002-04-03 00:45:35 +00:00
|
|
|
const PointerTOC *toc = (PointerTOC *)pData->toc;
|
|
|
|
if (toc != NULL) {
|
|
|
|
return toc->count;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-07 18:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const DataHeader *pointerTOCLookupFn(const UDataMemory *pData,
|
2002-04-03 00:45:35 +00:00
|
|
|
const char *name,
|
2001-09-07 18:42:29 +00:00
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
if(pData->toc!=NULL) {
|
2002-04-03 00:45:35 +00:00
|
|
|
const PointerTOC *toc = (PointerTOC *)pData->toc;
|
2001-09-07 18:42:29 +00:00
|
|
|
uint32_t start, limit, number;
|
|
|
|
|
|
|
|
/* perform a binary search for the data in the common data's table of contents */
|
|
|
|
start=0;
|
2002-04-03 00:45:35 +00:00
|
|
|
limit=toc->count;
|
2001-09-07 18:42:29 +00:00
|
|
|
|
2002-04-03 00:45:35 +00:00
|
|
|
if (limit == 0) { /* Stub common data library used during build is empty. */
|
2001-09-07 18:42:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(start<limit-1) {
|
|
|
|
number=(start+limit)/2;
|
2002-04-03 00:45:35 +00:00
|
|
|
if(uprv_strcmp(name, toc->entry[number].entryName)<0) {
|
2001-09-07 18:42:29 +00:00
|
|
|
limit=number;
|
|
|
|
} else {
|
|
|
|
start=number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-03 00:45:35 +00:00
|
|
|
if(uprv_strcmp(name, toc->entry[start].entryName)==0) {
|
2001-09-07 18:42:29 +00:00
|
|
|
/* found it */
|
2002-04-03 00:45:35 +00:00
|
|
|
return UDataMemory_normalizeDataPointer(toc->entry[start].pHeader);
|
2001-09-07 18:42:29 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return pData->pHeader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-05 21:48:05 +00:00
|
|
|
static const commonDataFuncs CmnDFuncs = {offsetTOCLookupFn, offsetTOCEntryCount};
|
|
|
|
static const commonDataFuncs ToCPFuncs = {pointerTOCLookupFn, pointerTOCEntryCount};
|
2001-09-07 18:42:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* checkCommonData Validate the format of a common data file. *
|
|
|
|
* Fill in the virtual function ptr based on TOC type *
|
|
|
|
* If the data is invalid, close the UDataMemory *
|
|
|
|
* and set the appropriate error code. *
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------*/
|
2001-10-05 21:48:05 +00:00
|
|
|
void udata_checkCommonData(UDataMemory *udm, UErrorCode *err) {
|
2001-09-07 18:42:29 +00:00
|
|
|
if (U_FAILURE(*err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(udm->pHeader->dataHeader.magic1==0xda &&
|
|
|
|
udm->pHeader->dataHeader.magic2==0x27 &&
|
|
|
|
udm->pHeader->info.isBigEndian==U_IS_BIG_ENDIAN &&
|
|
|
|
udm->pHeader->info.charsetFamily==U_CHARSET_FAMILY)
|
|
|
|
) {
|
|
|
|
/* header not valid */
|
|
|
|
*err=U_INVALID_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
else if (udm->pHeader->info.dataFormat[0]==0x43 &&
|
|
|
|
udm->pHeader->info.dataFormat[1]==0x6d &&
|
|
|
|
udm->pHeader->info.dataFormat[2]==0x6e &&
|
|
|
|
udm->pHeader->info.dataFormat[3]==0x44 &&
|
|
|
|
udm->pHeader->info.formatVersion[0]==1
|
|
|
|
) {
|
|
|
|
/* dataFormat="CmnD" */
|
|
|
|
udm->vFuncs = &CmnDFuncs;
|
|
|
|
udm->toc=(const char *)udm->pHeader+udm->pHeader->dataHeader.headerSize;
|
|
|
|
}
|
|
|
|
else if(udm->pHeader->info.dataFormat[0]==0x54 &&
|
|
|
|
udm->pHeader->info.dataFormat[1]==0x6f &&
|
|
|
|
udm->pHeader->info.dataFormat[2]==0x43 &&
|
|
|
|
udm->pHeader->info.dataFormat[3]==0x50 &&
|
|
|
|
udm->pHeader->info.formatVersion[0]==1
|
|
|
|
) {
|
|
|
|
/* dataFormat="ToCP" */
|
|
|
|
udm->vFuncs = &ToCPFuncs;
|
|
|
|
udm->toc=(const char *)udm->pHeader+udm->pHeader->dataHeader.headerSize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* dataFormat not recognized */
|
|
|
|
*err=U_INVALID_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (U_FAILURE(*err)) {
|
|
|
|
/* If the data is no good and we memory-mapped it ourselves,
|
|
|
|
* close the memory mapping so it doesn't leak. Note that this has
|
|
|
|
* no effect on non-memory mapped data, other than clearing fields in udm.
|
|
|
|
*/
|
|
|
|
udata_close(udm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-18 21:35:48 +00:00
|
|
|
/*
|
|
|
|
* TODO: Add a udata_swapPackageHeader() function that swaps an ICU .dat package
|
|
|
|
* header but not its sub-items.
|
|
|
|
* This function will be needed for automatic runtime swapping.
|
|
|
|
* Sub-items should not be swapped to limit the swapping to the parts of the
|
|
|
|
* package that are actually used.
|
|
|
|
*
|
|
|
|
* Since lengths of items are implicit in the order and offsets of their
|
|
|
|
* ToC entries, and since offsets are relative to the start of the ToC,
|
|
|
|
* a swapped version may need to generate a different data structure
|
|
|
|
* with pointers to the original data items and with their lengths
|
|
|
|
* (-1 for the last one if it is not known), and maybe even pointers to the
|
|
|
|
* swapped versions of the items.
|
|
|
|
* These pointers to swapped versions would establish a cache;
|
|
|
|
* instead, each open data item could simply own the storage for its swapped
|
|
|
|
* data. This fits better with the current design.
|
|
|
|
*
|
|
|
|
* markus 2003sep18 Jitterbug 2235
|
|
|
|
*/
|