#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "umutex.h"
#include "cmemory.h"
#include "cstring.h"
#include "unicode/udata.h"
#include "unicode/uversion.h"
#include "uhash.h"
#include "ucln_cmn.h"
#include "putilimp.h"
#include "udatamem.h"
#include "umapfile.h"
#include "ucmndata.h"
#if defined(UDATA_DEBUG)
# include <stdio.h>
#endif
static UDataMemory *gCommonICUData = NULL;
static UDataMemory *gStubICUData = NULL;
static UHashtable *gCommonDataCache = NULL;
static UBool U_CALLCONV
udata_cleanup(void)
{
if (gCommonDataCache) {
uhash_close(gCommonDataCache);
gCommonDataCache = NULL;
}
if (gCommonICUData != NULL) {
udata_close(gCommonICUData);
gCommonICUData = NULL;
}
if (gStubICUData != NULL) {
udata_close(gStubICUData);
gStubICUData = NULL;
}
return TRUE;
}
static void
setCommonICUData(UDataMemory *pData,
UDataMemory *oldData,
UBool warn,
UErrorCode *pErr)
{
UDataMemory *newCommonData = UDataMemory_createNewInstance(pErr);
if (U_FAILURE(*pErr)) {
return;
}
UDatamemory_assign(newCommonData, pData);
umtx_lock(NULL);
if (gCommonICUData==oldData) {
gStubICUData = gCommonICUData;
gCommonICUData = newCommonData;
ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup);
}
else {
if (warn==TRUE) {
*pErr = U_USING_DEFAULT_WARNING;
}
uprv_free(newCommonData);
}
umtx_unlock(NULL);
return;
}
static const char *
findBasename(const char *path) {
const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
if(basename==NULL) {
return path;
} else {
return basename+1;
}
}
#ifdef UDATA_DEBUG
static const char *
packageNameFromPath(const char *path)
{
if((path == NULL) || (*path == 0)) {
return U_ICUDATA_NAME;
}
path = findBasename(path);
if((path == NULL) || (*path == 0)) {
return U_ICUDATA_NAME;
}
return path;
}
#endif
typedef struct DataCacheElement {
char *name;
UDataMemory *item;
} DataCacheElement;
static void U_EXPORT2 U_CALLCONV DataCacheElement_deleter(void *pDCEl) {
DataCacheElement *p = (DataCacheElement *)pDCEl;
udata_close(p->item);
uprv_free(p->name);
uprv_free(pDCEl);
}
static UHashtable *udata_getHashTable() {
UErrorCode err = U_ZERO_ERROR;
UBool cacheIsInitialized;
UHashtable *tHT = NULL;
umtx_lock(NULL);
cacheIsInitialized = (gCommonDataCache != NULL);
umtx_unlock(NULL);
if (cacheIsInitialized) {
return gCommonDataCache;
}
tHT = uhash_open(uhash_hashChars, uhash_compareChars, &err);
uhash_setValueDeleter(tHT, DataCacheElement_deleter);
umtx_lock(NULL);
if (gCommonDataCache == NULL) {
gCommonDataCache = tHT;
tHT = NULL;
ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup);
}
umtx_unlock(NULL);
if (tHT != NULL) {
uhash_close(tHT);
}
if (U_FAILURE(err)) {
return NULL;
}
return gCommonDataCache;
}
static UDataMemory *udata_findCachedData(const char *path)
{
UHashtable *htable;
UDataMemory *retVal = NULL;
DataCacheElement *el;
const char *baseName;
baseName = findBasename(path);
htable = udata_getHashTable();
umtx_lock(NULL);
el = (DataCacheElement *)uhash_get(htable, baseName);
umtx_unlock(NULL);
if (el != NULL) {
retVal = el->item;
}
#ifdef UDATA_DEBUG
fprintf(stderr, "Cache: [%s] -> %p\n", baseName, retVal);
#endif
return retVal;
}
static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) {
DataCacheElement *newElement;
const char *baseName;
int32_t nameLen;
UHashtable *htable;
UDataMemory *oldValue = NULL;
UErrorCode subErr = U_ZERO_ERROR;
if (U_FAILURE(*pErr)) {
return NULL;
}
newElement = uprv_malloc(sizeof(DataCacheElement));
if (newElement == NULL) {
*pErr = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
newElement->item = UDataMemory_createNewInstance(pErr);
if (U_FAILURE(*pErr)) {
return NULL;
}
UDatamemory_assign(newElement->item, item);
baseName = findBasename(path);
nameLen = (int32_t)uprv_strlen(baseName);
newElement->name = uprv_malloc(nameLen+1);
if (newElement->name == NULL) {
*pErr = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
uprv_strcpy(newElement->name, baseName);
htable = udata_getHashTable();
umtx_lock(NULL);
oldValue = uhash_get(htable, path);
if (oldValue != NULL) {
subErr = U_USING_DEFAULT_WARNING;
}
else {
uhash_put(
htable,
newElement->name,
newElement,
&subErr);
}
umtx_unlock(NULL);
#ifdef UDATA_DEBUG
fprintf(stderr, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement->name,
newElement->item, u_errorName(subErr), newElement->item->vFuncs);
#endif
if (subErr == U_USING_DEFAULT_WARNING || U_FAILURE(subErr)) {
*pErr = subErr;
uprv_free(newElement->name);
uprv_free(newElement->item);
uprv_free(newElement);
return oldValue;
}
return newElement->item;
}
typedef struct TinyString {
char *s;
int32_t length;
char fStaticBuf[100];
int32_t fCapacity;
} TinyString;
static void TinyString_init(TinyString *This) {
This->s = This->fStaticBuf;
*This->s = 0;
This->length = 0;
This->fCapacity = sizeof(This->fStaticBuf)-1;
}
static void TinyString_append(TinyString *This, const char *what) {
int32_t newLen;
newLen = This->length + (int32_t)uprv_strlen(what);
if (newLen >= This->fCapacity) {
int32_t newCapacity = newLen * 2;
char *newBuf = (char *)uprv_malloc(newCapacity+1);
if (newBuf != NULL) {
uprv_strcpy(newBuf, This->s);
if (This->s != This->fStaticBuf) {
uprv_free(This->s);
}
This->s = newBuf;
This->fCapacity = newCapacity;
}
}
if (newLen < This->fCapacity) {
uprv_strcat(This->s+This->length, what);
This->length = newLen;
}
}
static void TinyString_appendn(TinyString *This, const char *what, int32_t n) {
int32_t newLen;
newLen = This->length + n;
if (newLen >= This->fCapacity) {
int32_t newCapacity = newLen * 2;
char *newBuf = (char *)uprv_malloc(newCapacity+1);
if (newBuf != NULL) {
uprv_strcpy(newBuf, This->s);
if (This->s != This->fStaticBuf) {
uprv_free(This->s);
}
This->s = newBuf;
This->fCapacity = newCapacity;
}
}
if (newLen < This->fCapacity) {
uprv_strncat(This->s+This->length, what, n);
This->length = newLen;
}
}
static void TinyString_dt(TinyString *This) {
if (This->s != This->fStaticBuf) {
uprv_free(This->s);
}
TinyString_init(This);
}
#define U_DATA_PATHITER_BUFSIZ 128
typedef struct
{
const char *path;
const char *nextPath;
const char *basename;
const char *suffix;
uint32_t basenameLen;
char *itemPath;
char itemPathBuf[U_DATA_PATHITER_BUFSIZ];
char *pathBuffer;
char pathBufferA[U_DATA_PATHITER_BUFSIZ];
char *packageStub;
char packageStubBuf[U_DATA_PATHITER_BUFSIZ];
uint32_t packageStubLen;
UBool checkLastFour;
} UDataPathIterator;
static void udata_pathiter_init(UDataPathIterator *iter, const char *path, const char *pkg,
const char *item, const char *suffix, UBool doCheckLastFour)
{
#ifdef UDATA_DEBUG
fprintf(stderr, "SUFFIX1=%s PATH=%s\n", suffix, path);
#endif
if(path == NULL) {
iter->path = u_getDataDirectory();
} else {
iter->path = path;
}
if(pkg == NULL) {
iter->packageStubLen = 0;
iter->packageStub=iter->packageStubBuf;
iter->packageStub[0] = 0;
} else {
if(uprv_strlen(pkg) + 2 > U_DATA_PATHITER_BUFSIZ) {
iter->packageStub = uprv_malloc(uprv_strlen(pkg)+2);
} else {
iter->packageStub = iter->packageStubBuf;
}
iter->packageStub[0] = U_FILE_SEP_CHAR;
uprv_strcpy(iter->packageStub+1, pkg);
iter->packageStubLen = (int32_t)uprv_strlen(iter->packageStub);
#ifdef UDATA_DEBUG
fprintf(stderr, "STUB=%s [%d]\n", iter->packageStub, iter->packageStubLen);
#endif
}
iter->basename = findBasename(item);
iter->basenameLen = (int32_t)uprv_strlen(iter->basename);
if(iter->basename == NULL) {
iter->nextPath = NULL;
return;
}
iter->itemPath = iter->itemPathBuf;
if(iter->basename == item) {
iter->itemPath[0] = 0;
iter->nextPath = iter->path;
} else {
int32_t itemPathLen = (int32_t)(iter->basename-item);
if (itemPathLen >= U_DATA_PATHITER_BUFSIZ) {
char *t = (char *)uprv_malloc(itemPathLen+1);
if (t != NULL) {
iter->itemPath = t;
} else {
itemPathLen = 0;
}
}
uprv_strncpy(iter->itemPath, item, itemPathLen);
iter->itemPath[itemPathLen]=0;
iter->nextPath = iter->itemPath;
}
#ifdef UDATA_DEBUG
fprintf(stderr, "SUFFIX=%s [%p]\n", suffix, suffix);
#endif
if(suffix != NULL) {
iter->suffix = suffix;
} else {
iter->suffix = "";
}
iter->checkLastFour = doCheckLastFour;
{
int32_t maxPathLen = (int32_t)uprv_strlen(iter->path) + uprv_strlen(item) + uprv_strlen(iter->suffix) + iter->packageStubLen + 3;
iter->pathBuffer = iter->pathBufferA;
if (maxPathLen >= U_DATA_PATHITER_BUFSIZ) {
iter->pathBuffer = (char *)uprv_malloc(maxPathLen);
if (iter->pathBuffer == NULL) {
iter->pathBuffer = iter->pathBufferA;
iter->path = "";
}
}
}
#ifdef UDATA_DEBUG
fprintf(stderr, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n",
iter,
item,
iter->path,
iter->basename,
iter->suffix,
iter->itemPath,
iter->nextPath,
iter->checkLastFour?"TRUE":"false");
#endif
}
static const char *udata_pathiter_next(UDataPathIterator *iter, int32_t *outPathLen)
{
const char *path = NULL;
uint32_t pathLen = 0;
const char *pathBasename;
if(outPathLen != NULL) {
*outPathLen = 0;
}
do
{
if( iter->nextPath == NULL ) {
return NULL;
}
path = iter->nextPath;
if(iter->nextPath == iter->itemPath) {
iter->nextPath = iter->path;
pathLen = (int32_t)uprv_strlen(path);
} else {
iter->nextPath = uprv_strchr(path, U_PATH_SEP_CHAR);
if(iter->nextPath == NULL) {
pathLen = (int32_t)uprv_strlen(path);
} else {
pathLen = (int32_t)(iter->nextPath - path);
if(*iter->nextPath) {
iter->nextPath ++;
}
}
}
if(pathLen == 0) {
continue;
}
#ifdef UDATA_DEBUG
fprintf(stderr, "rest of path (IDD) = %s\n", path);
fprintf(stderr, " ");
{
uint32_t qqq;
for(qqq=0;qqq<pathLen;qqq++)
{
fprintf(stderr, " ");
}
fprintf(stderr, "^\n");
}
#endif
uprv_strncpy(iter->pathBuffer, path, pathLen);
iter->pathBuffer[pathLen] = 0;
pathBasename = findBasename(iter->pathBuffer);
if(iter->checkLastFour == TRUE &&
(pathLen>=4) &&
uprv_strncmp(iter->pathBuffer +(pathLen-4),iter->suffix,4)==0 &&
uprv_strncmp(findBasename(iter->pathBuffer),iter->basename,iter->basenameLen)==0 &&
uprv_strlen(pathBasename)==(iter->basenameLen+4)) {
#ifdef UDATA_DEBUG
fprintf(stderr, "Have %s file on the path: %s\n", iter->suffix, iter->pathBuffer);
#endif
}
else
{
if(iter->pathBuffer[pathLen-1] != U_FILE_SEP_CHAR) {
if((pathLen>=4) &&
uprv_strncmp(iter->pathBuffer+(pathLen-4), ".dat", 4) == 0)
{
#ifdef UDATA_DEBUG
fprintf(stderr, "skipping non-directory .dat file %s\n", iter->pathBuffer);
#endif
continue;
}
if(iter->packageStubLen &&
(pathLen > iter->packageStubLen) &&
!uprv_strcmp(iter->pathBuffer + pathLen - iter->packageStubLen, iter->packageStub)) {
#ifdef UDATA_DEBUG
fprintf(stderr, "Found stub %s ( will add package %s of len %d)\n", iter->packageStub, iter->basename, iter->basenameLen);
#endif
pathLen -= iter->packageStubLen;
}
iter->pathBuffer[pathLen++] = U_FILE_SEP_CHAR;
}
uprv_strncpy(iter->pathBuffer + pathLen,
iter->packageStub+1,
iter->packageStubLen-1);
pathLen += iter->packageStubLen-1;
if(*iter->suffix)
{
uprv_strcpy(iter->pathBuffer + pathLen,
iter->suffix);
pathLen += (int32_t)uprv_strlen(iter->suffix);
}
}
if( outPathLen ) {
*outPathLen = pathLen;
}
#ifdef UDATA_DEBUG
fprintf(stderr, " --> %s\n", iter->pathBuffer);
#endif
return iter->pathBuffer;
} while(iter->path);
return NULL;
}
static void udata_pathiter_dt(UDataPathIterator *iter) {
if (iter->itemPath != iter->itemPathBuf) {
uprv_free(iter->itemPath);
iter->itemPath = NULL;
}
if (iter->pathBuffer != iter->pathBufferA) {
uprv_free(iter->pathBuffer);
iter->pathBuffer = NULL;
}
}
extern const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT;
static UDataMemory *
openCommonData(const char *path,
UBool isICUData,
UErrorCode *pErrorCode)
{
UDataMemory tData;
UDataPathIterator iter;
const char *pathBuffer;
const char *inBasename;
if (U_FAILURE(*pErrorCode)) {
return NULL;
}
UDataMemory_init(&tData);
if (isICUData) {
if(gCommonICUData != NULL) {
return gCommonICUData;
}
tData.pHeader = &U_ICUDATA_ENTRY_POINT;
udata_checkCommonData(&tData, pErrorCode);
setCommonICUData(&tData, NULL, FALSE, pErrorCode);
return gCommonICUData;
}
inBasename = findBasename(path);
#ifdef UDATA_DEBUG
fprintf(stderr, "inBasename = %s\n", inBasename);
#endif
if(*inBasename==0) {
#ifdef UDATA_DEBUG
fprintf(stderr, "ocd: no basename in %s, bailing.\n", path);
#endif
*pErrorCode=U_FILE_ACCESS_ERROR;
return NULL;
}
{
UDataMemory *dataToReturn = udata_findCachedData(inBasename);
if (dataToReturn != NULL) {
return dataToReturn;
}
}
udata_pathiter_init(&iter, u_getDataDirectory(), inBasename, path, ".dat", TRUE);
while((UDataMemory_isLoaded(&tData)==FALSE) &&
(pathBuffer = udata_pathiter_next(&iter, NULL)) != NULL)
{
#ifdef UDATA_DEBUG
fprintf(stderr, "ocd: trying path %s - ", pathBuffer);
#endif
uprv_mapFile(&tData, pathBuffer);
#ifdef UDATA_DEBUG
fprintf(stderr, "%s\n", UDataMemory_isLoaded(&tData)?"LOADED":"not loaded");
#endif
}
udata_pathiter_dt(&iter);
#if defined(OS390_STUBDATA) && defined(OS390BATCH)
if (!UDataMemory_isLoaded(&tData)) {
char ourPathBuffer[1024];
uprv_strncpy(ourPathBuffer, path, 1019);
ourPathBuffer[1019]=0;
uprv_strcat(ourPathBuffer, ".dat");
uprv_mapFile(&tData, ourPathBuffer);
}
#endif
if (!UDataMemory_isLoaded(&tData)) {
*pErrorCode=U_FILE_ACCESS_ERROR;
return NULL;
}
udata_checkCommonData(&tData, pErrorCode);
return udata_cacheDataItem(inBasename, &tData, pErrorCode);
}
#ifdef OS390
# define MAX_STUB_ENTRIES 8
#else
# define MAX_STUB_ENTRIES 0
#endif
static UBool extendICUData(UDataMemory *failedData, UErrorCode *pErr)
{
UDataMemory *pData;
UDataMemory copyPData;
if (failedData->vFuncs->NumEntries(failedData) > MAX_STUB_ENTRIES) {
return FALSE;
}
pData = openCommonData(
U_ICUDATA_NAME,
FALSE,
pErr);
UDataMemory_init(©PData);
if(pData != NULL) {
UDatamemory_assign(©PData, pData);
copyPData.map = 0;
copyPData.mapAddr = 0;
setCommonICUData(©PData,
failedData,
FALSE,
pErr);
}
return gCommonICUData != failedData;
}
U_CAPI void U_EXPORT2
udata_setCommonData(const void *data, UErrorCode *pErrorCode) {
UDataMemory dataMemory;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return;
}
if(data==NULL) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return;
}
if(gCommonICUData != NULL) {
*pErrorCode=U_USING_DEFAULT_WARNING;
return;
}
UDataMemory_init(&dataMemory);
UDataMemory_setData(&dataMemory, data);
udata_checkCommonData(&dataMemory, pErrorCode);
if (U_FAILURE(*pErrorCode)) {return;}
setCommonICUData(&dataMemory, NULL, TRUE, pErrorCode);
}
U_CAPI void U_EXPORT2
udata_setAppData(const char *path, const void *data, UErrorCode *err)
{
UDataMemory udm;
if(err==NULL || U_FAILURE(*err)) {
return;
}
if(data==NULL) {
*err=U_ILLEGAL_ARGUMENT_ERROR;
return;
}
UDataMemory_init(&udm);
udm.pHeader = data;
udata_checkCommonData(&udm, err);
udata_cacheDataItem(path, &udm, err);
}
static UDataMemory *
checkDataItem
(
const DataHeader *pHeader,
UDataMemoryIsAcceptable *isAcceptable,
void *context,
const char *type,
const char *name,
UErrorCode *nonFatalErr,
UErrorCode *fatalErr
)
{
UDataMemory *rDataMem = NULL;
if (U_FAILURE(*fatalErr)) {
return NULL;
}
if(pHeader->dataHeader.magic1==0xda &&
pHeader->dataHeader.magic2==0x27 &&
(isAcceptable==NULL || isAcceptable(context, type, name, &pHeader->info))
) {
rDataMem=UDataMemory_createNewInstance(fatalErr);
if (U_FAILURE(*fatalErr)) {
return NULL;
}
rDataMem->pHeader = pHeader;
} else {
*nonFatalErr=U_INVALID_FORMAT_ERROR;
}
return rDataMem;
}
static UDataMemory *
doOpenChoice(const char *path, const char *type, const char *name,
UDataMemoryIsAcceptable *isAcceptable, void *context,
UErrorCode *pErrorCode)
{
UDataMemory *retVal = NULL;
const char *pathBuffer;
TinyString tocEntryName;
TinyString tocEntryPath;
TinyString oldIndFileName;
TinyString oldStylePath;
TinyString oldStylePathBasename;
TinyString pkgName;
TinyString treeName;
#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
TinyString altSepPath;
#endif
const char *dataPath;
const char *tocEntrySuffix;
int32_t tocEntrySuffixIndex;
const char *tocEntryPathSuffix;
UDataMemory dataMemory;
UDataMemory *pCommonData;
UDataMemory *pEntryData;
const DataHeader *pHeader;
const char *inBasename;
UErrorCode errorCode=U_ZERO_ERROR;
const char *treeChar;
UBool isICUData = FALSE;
if(path == NULL ||
!strcmp(path, U_ICUDATA_ALIAS) ||
!uprv_strncmp(path, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING,
uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING)) ||
!uprv_strncmp(path, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING,
uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING))) {
isICUData = TRUE;
}
#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
TinyString_init(&altSepPath);
if(path) {
char *p;
if((p=uprv_strchr(path,U_FILE_ALT_SEP_CHAR))) {
TinyString_append(&altSepPath, path);
while((p=uprv_strchr(altSepPath.s,U_FILE_ALT_SEP_CHAR))) {
*p = U_FILE_SEP_CHAR;
}
#if defined (UDATA_DEBUG)
fprintf(stderr, "Changed path from [%s] to [%s]\n", path, altSepPath.s);
#endif
path = altSepPath.s;
}
}
#endif
TinyString_init(&oldIndFileName);
TinyString_init(&tocEntryName);
TinyString_init(&tocEntryPath);
TinyString_init(&oldStylePath);
TinyString_init(&oldStylePathBasename);
TinyString_init(&pkgName);
TinyString_init(&treeName);
if(path==NULL) {
TinyString_append(&pkgName, U_ICUDATA_NAME);
} else {
const char *pkg;
const char *first;
pkg = uprv_strrchr(path, U_FILE_SEP_CHAR);
first = uprv_strchr(path, U_FILE_SEP_CHAR);
if(uprv_pathIsAbsolute(path) || (pkg != first)) {
if(pkg) {
TinyString_append(&pkgName, pkg+1);
} else {
TinyString_append(&pkgName, path);
}
} else {
treeChar = uprv_strchr(path, U_TREE_SEPARATOR);
if(treeChar) {
TinyString_append(&treeName, treeChar+1);
if(!isICUData) {
TinyString_appendn(&pkgName, path, (int32_t)(treeChar-path));
} else {
TinyString_append(&pkgName, U_ICUDATA_NAME);
}
} else {
if(!isICUData) {
TinyString_append(&pkgName, path);
} else {
TinyString_append(&pkgName, U_ICUDATA_NAME);
}
}
}
}
#ifdef UDATA_DEBUG
fprintf(stderr, " P=%s T=%s\n", pkgName.s, treeName.s);
#endif
TinyString_append(&tocEntryName, pkgName.s);
TinyString_append(&tocEntryPath, pkgName.s);
TinyString_append(&oldIndFileName, pkgName.s);
tocEntrySuffixIndex = tocEntryName.length;
if(treeName.s[0]) {
TinyString_append(&tocEntryName, U_TREE_ENTRY_SEP_STRING);
TinyString_append(&tocEntryName, treeName.s);
TinyString_append(&tocEntryPath, U_FILE_SEP_STRING);
TinyString_append(&tocEntryPath, treeName.s);
}
TinyString_append(&oldIndFileName, "_");
TinyString_append(&tocEntryName, U_TREE_ENTRY_SEP_STRING);
TinyString_append(&tocEntryPath, U_FILE_SEP_STRING);
TinyString_append(&oldIndFileName, name);
TinyString_append(&tocEntryName, name);
TinyString_append(&tocEntryPath, name);
if(type!=NULL && *type!=0) {
TinyString_append(&tocEntryName, ".");
TinyString_append(&tocEntryName, type);
TinyString_append(&tocEntryPath, ".");
TinyString_append(&tocEntryPath, type);
TinyString_append(&oldIndFileName, ".");
TinyString_append(&oldIndFileName, type);
}
tocEntrySuffix = tocEntryName.s+tocEntrySuffixIndex;
tocEntryPathSuffix = tocEntryPath.s+tocEntrySuffixIndex;
#ifdef UDATA_DEBUG
fprintf(stderr, " tocEntryName = %s\n", tocEntryName.s);
fprintf(stderr, " tocEntryPath = %s\n", tocEntryName.s);
fprintf(stderr, " oldIndFileName = %s\n", oldIndFileName.s);
#endif
if(path == NULL) {
path = COMMON_DATA_NAME;
inBasename = COMMON_DATA_NAME;
} else {
if(isICUData) {
inBasename=COMMON_DATA_NAME;
} else {
inBasename=findBasename(path);
}
}
#ifdef UDATA_DEBUG
fprintf(stderr, "IND: inBasename = %s, pkg=%s\n", inBasename, packageNameFromPath(path));
#endif
if( (*inBasename==0) && (uprv_strlen(path) > 3) ) {
char *rightSlash;
TinyString_append(&oldStylePath, path);
oldStylePath.length--;
oldStylePath.s[oldStylePath.length] = 0;
rightSlash = (char*)uprv_strrchr(oldStylePath.s, U_FILE_SEP_CHAR);
if(rightSlash != NULL) {
rightSlash++;
TinyString_append(&oldStylePathBasename, rightSlash);
inBasename = oldStylePathBasename.s;
TinyString_append(&oldStylePath, U_FILE_SEP_STRING);
TinyString_append(&oldStylePath, inBasename);
path = oldStylePath.s;
} else {
*pErrorCode = U_FILE_ACCESS_ERROR;
retVal = NULL;
goto commonReturn;
}
}
dataPath = u_getDataDirectory();
if ((dataPath && *dataPath) || !isICUData) {
UDataPathIterator iter;
udata_pathiter_init(&iter, dataPath, pkgName.s, path, tocEntryPathSuffix, FALSE);
while((pathBuffer = udata_pathiter_next(&iter, NULL)))
{
#ifdef UDATA_DEBUG
fprintf(stderr, "UDATA: trying individual file %s\n", pathBuffer);
#endif
if( uprv_mapFile(&dataMemory, pathBuffer) ||
(inBasename!=pathBuffer && uprv_mapFile(&dataMemory, inBasename)))
{
pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, &errorCode, pErrorCode);
if (pEntryData != NULL) {
pEntryData->mapAddr = dataMemory.mapAddr;
pEntryData->map = dataMemory.map;
#ifdef UDATA_DEBUG
fprintf(stderr, "** Mapped file: %s\n", pathBuffer);
#endif
udata_pathiter_dt(&iter);
retVal = pEntryData;
goto commonReturn;
}
udata_close(&dataMemory);
if (U_FAILURE(*pErrorCode)) {
udata_pathiter_dt(&iter);
retVal = NULL;
goto commonReturn;
}
errorCode=U_INVALID_FORMAT_ERROR;
}
#ifdef UDATA_DEBUG
fprintf(stderr, "%s\n", UDataMemory_isLoaded(&dataMemory)?"LOADED":"not loaded");
#endif
}
udata_pathiter_dt(&iter);
udata_pathiter_init(&iter, dataPath, "", path, oldIndFileName.s, FALSE);
while((pathBuffer = udata_pathiter_next(&iter, NULL)))
{
#ifdef UDATA_DEBUG
fprintf(stderr, "UDATA: trying individual file %s\n", pathBuffer);
#endif
if( uprv_mapFile(&dataMemory, pathBuffer) ||
(inBasename!=pathBuffer && uprv_mapFile(&dataMemory, inBasename)))
{
pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, &errorCode, pErrorCode);
if (pEntryData != NULL) {
pEntryData->mapAddr = dataMemory.mapAddr;
pEntryData->map = dataMemory.map;
#ifdef UDATA_DEBUG
fprintf(stderr, "** Mapped file: %s\n", pathBuffer);
#endif
udata_pathiter_dt(&iter);
retVal = pEntryData;
goto commonReturn;
}
udata_close(&dataMemory);
if (U_FAILURE(*pErrorCode)) {
udata_pathiter_dt(&iter);
retVal = NULL;
goto commonReturn;
}
errorCode=U_INVALID_FORMAT_ERROR;
}
#ifdef UDATA_DEBUG
fprintf(stderr, "%s\n", UDataMemory_isLoaded(&dataMemory)?"LOADED":"not loaded");
#endif
}
udata_pathiter_dt(&iter);
}
for (;;) {
pCommonData=openCommonData(path, isICUData, &errorCode);
if(U_SUCCESS(errorCode)) {
int32_t length;
pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName.s, &length, &errorCode);
#ifdef UDATA_DEBUG
fprintf(stderr, "%s: pHeader=%p - %s\n", tocEntryName.s, pHeader, u_errorName(errorCode));
#endif
if((pHeader == NULL) && !U_FAILURE(errorCode)) {
pHeader=pCommonData->vFuncs->Lookup(pCommonData, oldIndFileName.s,
&length, &errorCode);
#ifdef UDATA_DEBUG
fprintf(stderr, "[OLD name] %s: pHeader=%p - %s\n", oldIndFileName.s, pHeader, u_errorName(errorCode));
#endif
}
if(pHeader!=NULL) {
pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, &errorCode, pErrorCode);
#ifdef UDATA_DEBUG
fprintf(stderr, "pEntryData=%p\n", pEntryData);
#endif
if (U_FAILURE(*pErrorCode)) {
retVal = NULL;
goto commonReturn;
}
if (pEntryData != NULL) {
pEntryData->length = length;
retVal = pEntryData;
goto commonReturn;
}
}
}
if (!(isICUData && pCommonData && extendICUData(pCommonData, &errorCode))) {
break;
}
}
if(U_SUCCESS(*pErrorCode)) {
if(U_SUCCESS(errorCode)) {
*pErrorCode=U_FILE_ACCESS_ERROR;
} else {
*pErrorCode=errorCode;
}
}
commonReturn:
TinyString_dt(&tocEntryName);
TinyString_dt(&tocEntryPath);
TinyString_dt(&oldIndFileName);
TinyString_dt(&oldStylePath);
TinyString_dt(&oldStylePathBasename);
TinyString_dt(&pkgName);
TinyString_dt(&treeName);
#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
TinyString_dt(&altSepPath);
#endif
return retVal;
}
U_CAPI UDataMemory * U_EXPORT2
udata_open(const char *path, const char *type, const char *name,
UErrorCode *pErrorCode) {
#ifdef UDATA_DEBUG
fprintf(stderr, "udata_open(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type);
fflush(stderr);
#endif
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return NULL;
} else if(name==NULL || *name==0) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
} else {
return doOpenChoice(path, type, name, NULL, NULL, pErrorCode);
}
}
U_CAPI UDataMemory * U_EXPORT2
udata_openChoice(const char *path, const char *type, const char *name,
UDataMemoryIsAcceptable *isAcceptable, void *context,
UErrorCode *pErrorCode) {
#ifdef UDATA_DEBUG
fprintf(stderr, "udata_openChoice(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type);
#endif
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return NULL;
} else if(name==NULL || *name==0 || isAcceptable==NULL) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
} else {
return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode);
}
}
U_CAPI void U_EXPORT2
udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) {
if(pInfo!=NULL) {
if(pData!=NULL && pData->pHeader!=NULL) {
const UDataInfo *info=&pData->pHeader->info;
uint16_t dataInfoSize=udata_getInfoSize(info);
if(pInfo->size>dataInfoSize) {
pInfo->size=dataInfoSize;
}
uprv_memcpy((uint16_t *)pInfo+1, (const uint16_t *)info+1, pInfo->size-2);
if(info->isBigEndian!=U_IS_BIG_ENDIAN) {
uint16_t x=info->reservedWord;
pInfo->reservedWord=(uint16_t)((x<<8)|(x>>8));
}
} else {
pInfo->size=0;
}
}
}