2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
1999-11-20 01:11:12 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1999-2010, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
1999-11-20 01:11:12 +00:00
|
|
|
*******************************************************************************
|
|
|
|
* file name: unewdata.c
|
2017-02-03 18:57:23 +00:00
|
|
|
* encoding: UTF-8
|
1999-11-20 01:11:12 +00:00
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 1999oct25
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*/
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
#include <stdio.h>
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/utypes.h"
|
2001-01-03 00:18:57 +00:00
|
|
|
#include "unicode/putil.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/ustring.h"
|
1999-11-20 01:11:12 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "filestrm.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/udata.h"
|
1999-11-20 01:11:12 +00:00
|
|
|
#include "unewdata.h"
|
|
|
|
|
|
|
|
struct UNewDataMemory {
|
|
|
|
FileStream *file;
|
|
|
|
uint16_t headerSize;
|
|
|
|
uint8_t magic1, magic2;
|
|
|
|
};
|
|
|
|
|
|
|
|
U_CAPI UNewDataMemory * U_EXPORT2
|
2000-03-04 01:19:19 +00:00
|
|
|
udata_create(const char *dir, const char *type, const char *name,
|
1999-11-20 01:11:12 +00:00
|
|
|
const UDataInfo *pInfo,
|
|
|
|
const char *comment,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
UNewDataMemory *pData;
|
|
|
|
uint16_t headerSize, commentLength;
|
|
|
|
char filename[512];
|
|
|
|
uint8_t bytes[16];
|
2016-09-20 21:03:19 +00:00
|
|
|
int32_t length;
|
1999-11-20 01:11:12 +00:00
|
|
|
|
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
} else if(name==NULL || *name==0 || pInfo==NULL) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the data structure */
|
1999-12-28 23:57:50 +00:00
|
|
|
pData=(UNewDataMemory *)uprv_malloc(sizeof(UNewDataMemory));
|
1999-11-20 01:11:12 +00:00
|
|
|
if(pData==NULL) {
|
|
|
|
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-01-09 00:04:19 +00:00
|
|
|
|
|
|
|
/* Check that the full path won't be too long */
|
2008-01-09 18:20:25 +00:00
|
|
|
length = 0; /* Start with nothing */
|
2008-01-09 00:04:19 +00:00
|
|
|
if(dir != NULL && *dir !=0) /* Add directory length if one was given */
|
|
|
|
{
|
|
|
|
length += strlen(dir);
|
|
|
|
|
|
|
|
/* Add 1 if dir doesn't end with path sep */
|
|
|
|
if (dir[strlen(dir) - 1]!= U_FILE_SEP_CHAR) {
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
length += strlen(name); /* Add the filename length */
|
1999-11-20 01:11:12 +00:00
|
|
|
|
2008-01-09 00:04:19 +00:00
|
|
|
if(type != NULL && *type !=0) { /* Add directory length if given */
|
|
|
|
length += strlen(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* LDH buffer Length error check */
|
2016-09-20 21:03:19 +00:00
|
|
|
if(length > ((int32_t)sizeof(filename) - 1))
|
2008-01-09 00:04:19 +00:00
|
|
|
{
|
|
|
|
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
uprv_free(pData);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-11-20 01:11:12 +00:00
|
|
|
/* open the output file */
|
2000-06-06 19:26:16 +00:00
|
|
|
if(dir!=NULL && *dir!=0) { /* if dir has a value, we prepend it to the filename */
|
2000-04-18 20:31:01 +00:00
|
|
|
char *p=filename+strlen(dir);
|
2000-02-29 18:33:01 +00:00
|
|
|
uprv_strcpy(filename, dir);
|
2000-04-18 20:31:01 +00:00
|
|
|
if (*(p-1)!=U_FILE_SEP_CHAR) {
|
|
|
|
*p++=U_FILE_SEP_CHAR;
|
|
|
|
*p=0;
|
2000-02-29 18:33:01 +00:00
|
|
|
}
|
2000-06-06 19:26:16 +00:00
|
|
|
} else { /* otherwise, we'll output to the current dir */
|
1999-11-20 01:11:12 +00:00
|
|
|
filename[0]=0;
|
|
|
|
}
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_strcat(filename, name);
|
1999-11-20 01:11:12 +00:00
|
|
|
if(type!=NULL && *type!=0) {
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_strcat(filename, ".");
|
|
|
|
uprv_strcat(filename, type);
|
1999-11-20 01:11:12 +00:00
|
|
|
}
|
|
|
|
pData->file=T_FileStream_open(filename, "wb");
|
|
|
|
if(pData->file==NULL) {
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_free(pData);
|
1999-11-20 01:11:12 +00:00
|
|
|
*pErrorCode=U_FILE_ACCESS_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the header information */
|
2000-08-29 17:16:53 +00:00
|
|
|
headerSize=(uint16_t)(pInfo->size+4);
|
1999-11-20 01:11:12 +00:00
|
|
|
if(comment!=NULL && *comment!=0) {
|
2000-08-29 17:16:53 +00:00
|
|
|
commentLength=(uint16_t)(uprv_strlen(comment)+1);
|
1999-11-20 01:11:12 +00:00
|
|
|
headerSize+=commentLength;
|
|
|
|
} else {
|
|
|
|
commentLength=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the size of the header, take padding into account */
|
2000-08-29 17:16:53 +00:00
|
|
|
pData->headerSize=(uint16_t)((headerSize+15)&~0xf);
|
1999-11-20 01:11:12 +00:00
|
|
|
pData->magic1=0xda;
|
|
|
|
pData->magic2=0x27;
|
|
|
|
T_FileStream_write(pData->file, &pData->headerSize, 4);
|
|
|
|
|
|
|
|
/* write the information data */
|
|
|
|
T_FileStream_write(pData->file, pInfo, pInfo->size);
|
|
|
|
|
|
|
|
/* write the comment */
|
|
|
|
if(commentLength>0) {
|
|
|
|
T_FileStream_write(pData->file, comment, commentLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write padding bytes to align the data section to 16 bytes */
|
|
|
|
headerSize&=0xf;
|
|
|
|
if(headerSize!=0) {
|
2000-08-29 17:16:53 +00:00
|
|
|
headerSize=(uint16_t)(16-headerSize);
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memset(bytes, 0, headerSize);
|
1999-11-20 01:11:12 +00:00
|
|
|
T_FileStream_write(pData->file, bytes, headerSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pData;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI uint32_t U_EXPORT2
|
|
|
|
udata_finish(UNewDataMemory *pData, UErrorCode *pErrorCode) {
|
|
|
|
uint32_t fileLength=0;
|
|
|
|
|
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pData!=NULL) {
|
|
|
|
if(pData->file!=NULL) {
|
|
|
|
/* fflush(pData->file);*/
|
|
|
|
fileLength=T_FileStream_size(pData->file);
|
|
|
|
if(T_FileStream_error(pData->file)) {
|
|
|
|
*pErrorCode=U_FILE_ACCESS_ERROR;
|
|
|
|
} else {
|
|
|
|
fileLength-=pData->headerSize;
|
|
|
|
}
|
|
|
|
T_FileStream_close(pData->file);
|
|
|
|
}
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_free(pData);
|
1999-11-20 01:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileLength;
|
|
|
|
}
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
/* dummy UDataInfo cf. udata.h */
|
|
|
|
static const UDataInfo dummyDataInfo = {
|
|
|
|
sizeof(UDataInfo),
|
|
|
|
0,
|
|
|
|
|
|
|
|
U_IS_BIG_ENDIAN,
|
|
|
|
U_CHARSET_FAMILY,
|
|
|
|
U_SIZEOF_UCHAR,
|
|
|
|
0,
|
|
|
|
|
|
|
|
{ 0, 0, 0, 0 }, /* dummy dataFormat */
|
|
|
|
{ 0, 0, 0, 0 }, /* dummy formatVersion */
|
|
|
|
{ 0, 0, 0, 0 } /* dummy dataVersion */
|
|
|
|
};
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
udata_createDummy(const char *dir, const char *type, const char *name, UErrorCode *pErrorCode) {
|
|
|
|
if(U_SUCCESS(*pErrorCode)) {
|
|
|
|
udata_finish(udata_create(dir, type, name, &dummyDataInfo, NULL, pErrorCode), pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
fprintf(stderr, "error %s writing dummy data file %s" U_FILE_SEP_STRING "%s.%s\n",
|
|
|
|
u_errorName(*pErrorCode), dir, name, type);
|
|
|
|
exit(*pErrorCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-20 01:11:12 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
udata_write8(UNewDataMemory *pData, uint8_t byte) {
|
|
|
|
if(pData!=NULL && pData->file!=NULL) {
|
|
|
|
T_FileStream_write(pData->file, &byte, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
udata_write16(UNewDataMemory *pData, uint16_t word) {
|
|
|
|
if(pData!=NULL && pData->file!=NULL) {
|
|
|
|
T_FileStream_write(pData->file, &word, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
udata_write32(UNewDataMemory *pData, uint32_t wyde) {
|
|
|
|
if(pData!=NULL && pData->file!=NULL) {
|
|
|
|
T_FileStream_write(pData->file, &wyde, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
2002-03-12 01:32:42 +00:00
|
|
|
udata_writeBlock(UNewDataMemory *pData, const void *s, int32_t length) {
|
1999-11-20 01:11:12 +00:00
|
|
|
if(pData!=NULL && pData->file!=NULL) {
|
|
|
|
if(length>0) {
|
|
|
|
T_FileStream_write(pData->file, s, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-07 18:01:33 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2002-03-12 01:32:42 +00:00
|
|
|
udata_writePadding(UNewDataMemory *pData, int32_t length) {
|
2004-06-18 02:00:14 +00:00
|
|
|
static const uint8_t padding[16]={
|
1999-12-07 18:01:33 +00:00
|
|
|
0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa
|
|
|
|
};
|
|
|
|
if(pData!=NULL && pData->file!=NULL) {
|
|
|
|
while(length>=16) {
|
|
|
|
T_FileStream_write(pData->file, padding, 16);
|
|
|
|
length-=16;
|
|
|
|
}
|
|
|
|
if(length>0) {
|
|
|
|
T_FileStream_write(pData->file, padding, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-20 01:11:12 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2002-03-12 01:32:42 +00:00
|
|
|
udata_writeString(UNewDataMemory *pData, const char *s, int32_t length) {
|
1999-11-20 01:11:12 +00:00
|
|
|
if(pData!=NULL && pData->file!=NULL) {
|
|
|
|
if(length==-1) {
|
2002-05-23 22:10:23 +00:00
|
|
|
length=(int32_t)uprv_strlen(s);
|
1999-11-20 01:11:12 +00:00
|
|
|
}
|
|
|
|
if(length>0) {
|
|
|
|
T_FileStream_write(pData->file, s, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
2002-03-12 01:32:42 +00:00
|
|
|
udata_writeUString(UNewDataMemory *pData, const UChar *s, int32_t length) {
|
1999-11-20 01:11:12 +00:00
|
|
|
if(pData!=NULL && pData->file!=NULL) {
|
|
|
|
if(length==-1) {
|
|
|
|
length=u_strlen(s);
|
|
|
|
}
|
|
|
|
if(length>0) {
|
|
|
|
T_FileStream_write(pData->file, s, length*sizeof(UChar));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-29 18:33:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hey, Emacs, please set the following:
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
*/
|
2000-04-25 21:33:15 +00:00
|
|
|
|