/*
*******************************************************************************
*
-* Copyright (C) 1999-2014, International Business Machines
+* Copyright (C) 1999-2015, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
readFile(const char *path, const char *name, int32_t &length, char &type) {
char filename[1024];
FILE *file;
- uint8_t *data;
UErrorCode errorCode;
int32_t fileLength, typeEnum;
/* allocate the buffer, pad to multiple of 16 */
length=(fileLength+0xf)&~0xf;
- data=(uint8_t *)uprv_malloc(length);
- if(data==NULL) {
+ icu::LocalMemory<uint8_t> data((uint8_t *)uprv_malloc(length));
+ if(data.isNull()) {
fclose(file);
fprintf(stderr, "icupkg: malloc error allocating %d bytes.\n", (int)length);
exit(U_MEMORY_ALLOCATION_ERROR);
}
/* read the file */
- if(fileLength!=(int32_t)fread(data, 1, fileLength, file)) {
+ if(fileLength!=(int32_t)fread(data.getAlias(), 1, fileLength, file)) {
fprintf(stderr, "icupkg: error reading \"%s\"\n", filename);
fclose(file);
- free(data);
exit(U_FILE_ACCESS_ERROR);
}
/* pad the file to a multiple of 16 using the usual padding byte */
if(fileLength<length) {
- memset(data+fileLength, 0xaa, length-fileLength);
+ memset(data.getAlias()+fileLength, 0xaa, length-fileLength);
}
fclose(file);
// minimum check for ICU-format data
errorCode=U_ZERO_ERROR;
- typeEnum=getTypeEnumForInputData(data, length, &errorCode);
+ typeEnum=getTypeEnumForInputData(data.getAlias(), length, &errorCode);
if(typeEnum<0 || U_FAILURE(errorCode)) {
fprintf(stderr, "icupkg: not an ICU data file: \"%s\"\n", filename);
- free(data);
#if !UCONFIG_NO_LEGACY_CONVERSION
exit(U_INVALID_FORMAT_ERROR);
#else
}
type=makeTypeLetter(typeEnum);
- return data;
+ return data.orphan();
}
// .dat package file representation ---------------------------------------- ***
Package::~Package() {
int32_t idx;
- free(inData);
+ uprv_free(inData);
for(idx=0; idx<itemCount; ++idx) {
if(items[idx].isDataOwned) {
- free(items[idx].data);
+ uprv_free(items[idx].data);
}
}
} else {
// same-name item found, replace it
if(items[idx].isDataOwned) {
- free(items[idx].data);
+ uprv_free(items[idx].data);
}
// keep the item's name since it is the same
if(idx>=0) {
// remove the item
if(items[idx].isDataOwned) {
- free(items[idx].data);
+ uprv_free(items[idx].data);
}
// move the following items up
exit(U_MEMORY_ALLOCATION_ERROR);
}
if(items && itemCount>0) {
- uprv_memcpy(newItems, items, itemCount*sizeof(items[0]));
+ uprv_memcpy(newItems, items, (size_t)itemCount*sizeof(items[0]));
}
itemMax = max;
items = newItems;