#include "unicode/utypes.h"
#include "unicode/udata.h"
#include "cstring.h"
#include "ucmndata.h"
#include "udatamem.h"
#if defined(UDATA_DEBUG) || defined(UDATA_DEBUG_DUMP)
# include <stdio.h>
#endif
U_CFUNC uint16_t
udata_getHeaderSize(const DataHeader *udh) {
if(udh==NULL) {
return 0;
} else if(udh->info.isBigEndian==U_IS_BIG_ENDIAN) {
return udh->dataHeader.headerSize;
} else {
uint16_t x=udh->dataHeader.headerSize;
return (uint16_t)((x<<8)|(x>>8));
}
}
U_CFUNC uint16_t
udata_getInfoSize(const UDataInfo *info) {
if(info==NULL) {
return 0;
} else if(info->isBigEndian==U_IS_BIG_ENDIAN) {
return info->size;
} else {
uint16_t x=info->size;
return (uint16_t)((x<<8)|(x>>8));
}
}
typedef struct {
const char *entryName;
const DataHeader *pHeader;
} PointerTOCEntry;
typedef struct {
uint32_t count;
uint32_t reserved;
PointerTOCEntry entry[2];
} PointerTOC;
static uint32_t offsetTOCEntryCount(const UDataMemory *pData) {
int32_t retVal=0;
const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
if (toc != NULL) {
retVal = toc->count;
}
return retVal;
}
static const DataHeader *
offsetTOCLookupFn(const UDataMemory *pData,
const char *tocEntryName,
int32_t *pLength,
UErrorCode *pErrorCode) {
const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
if(toc!=NULL) {
const char *base=(const char *)toc;
uint32_t start, limit, number, lastNumber;
int32_t strResult;
const UDataOffsetTOCEntry *entry;
#if defined (UDATA_DEBUG_DUMP)
for(start=0;start<toc->count;start++) {
fprintf(stderr, "\tx%d: %s\n", start, &base[toc->entry[start].nameOffset]);
}
#endif
start=0;
limit=toc->count;
lastNumber=limit;
entry=toc->entry;
for (;;) {
number = (start+limit)/2;
if (lastNumber == number) {
break;
}
lastNumber = number;
strResult = uprv_strcmp(tocEntryName, base+entry[number].nameOffset);
if(strResult<0) {
limit=number;
} else if (strResult>0) {
start=number;
}
else {
#ifdef UDATA_DEBUG
fprintf(stderr, "%s: Found.\n", tocEntryName);
#endif
entry += number;
if((number+1) < toc->count) {
*pLength = (int32_t)(entry[1].dataOffset - entry->dataOffset);
} else {
*pLength = -1;
}
return (const DataHeader *)(base+entry->dataOffset);
}
}
#ifdef UDATA_DEBUG
fprintf(stderr, "%s: Not found.\n", tocEntryName);
#endif
return NULL;
} else {
#ifdef UDATA_DEBUG
fprintf(stderr, "returning header\n");
#endif
return pData->pHeader;
}
}
static uint32_t pointerTOCEntryCount(const UDataMemory *pData) {
const PointerTOC *toc = (PointerTOC *)pData->toc;
return (uint32_t)((toc != NULL) ? (toc->count) : 0);
}
static const DataHeader *pointerTOCLookupFn(const UDataMemory *pData,
const char *name,
int32_t *pLength,
UErrorCode *pErrorCode) {
if(pData->toc!=NULL) {
const PointerTOC *toc = (PointerTOC *)pData->toc;
uint32_t start, limit, number, lastNumber;
int32_t strResult;
#if defined (UDATA_DEBUG_DUMP)
for(start=0;start<toc->count;start++) {
fprintf(stderr, "\tx%d: %s\n", start, toc->entry[start].entryName);
}
#endif
start=0;
limit=toc->count;
lastNumber=limit;
for (;;) {
number = (start+limit)/2;
if (lastNumber == number) {
break;
}
lastNumber = number;
strResult = uprv_strcmp(name, toc->entry[number].entryName);
if(strResult<0) {
limit=number;
} else if (strResult>0) {
start=number;
}
else {
#ifdef UDATA_DEBUG
fprintf(STDErr, "%s: Found.\n", toc->entry[number].entryName);
#endif
*pLength=-1;
return UDataMemory_normalizeDataPointer(toc->entry[number].pHeader);
}
}
#ifdef UDATA_DEBUG
fprintf(stderr, "%s: Not found.\n", name);
#endif
return NULL;
} else {
return pData->pHeader;
}
}
static const commonDataFuncs CmnDFuncs = {offsetTOCLookupFn, offsetTOCEntryCount};
static const commonDataFuncs ToCPFuncs = {pointerTOCLookupFn, pointerTOCEntryCount};
void udata_checkCommonData(UDataMemory *udm, UErrorCode *err) {
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)
) {
*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
) {
udm->vFuncs = &CmnDFuncs;
udm->toc=(const char *)udm->pHeader+udata_getHeaderSize(udm->pHeader);
}
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
) {
udm->vFuncs = &ToCPFuncs;
udm->toc=(const char *)udm->pHeader+udata_getHeaderSize(udm->pHeader);
}
else {
*err=U_INVALID_FORMAT_ERROR;
}
if (U_FAILURE(*err)) {
udata_close(udm);
}
}