ICU-11036 RBBI, check data header when opening.

X-SVN-Rev: 36366
This commit is contained in:
Andy Heninger 2014-09-05 21:23:48 +00:00
parent eed7616070
commit 9a2a9bea34
3 changed files with 42 additions and 13 deletions

View file

@ -1,6 +1,6 @@
/*
***************************************************************************
* Copyright (C) 1999-2010 International Business Machines Corporation *
* Copyright (C) 1999-2014 International Business Machines Corporation *
* and others. All rights reserved. *
***************************************************************************
*/
@ -46,20 +46,39 @@ U_NAMESPACE_BEGIN
//
//-----------------------------------------------------------------------------
RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, UErrorCode &status) {
init0();
init(data, status);
}
RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, enum EDontAdopt, UErrorCode &status) {
init0();
init(data, status);
fDontFreeData = TRUE;
}
RBBIDataWrapper::RBBIDataWrapper(UDataMemory* udm, UErrorCode &status) {
const RBBIDataHeader *d = (const RBBIDataHeader *)
// ((char *)&(udm->pHeader->info) + udm->pHeader->info.size);
// taking into consideration the padding added in by udata_write
((char *)(udm->pHeader) + udm->pHeader->dataHeader.headerSize);
init(d, status);
init0();
if (U_FAILURE(status)) {
return;
}
const DataHeader *dh = udm->pHeader;
int32_t headerSize = dh->dataHeader.headerSize;
if ( !(headerSize >= 20 &&
dh->info.isBigEndian == U_IS_BIG_ENDIAN &&
dh->info.charsetFamily == U_CHARSET_FAMILY &&
dh->info.dataFormat[0] == 0x42 && // dataFormat="Brk "
dh->info.dataFormat[1] == 0x72 &&
dh->info.dataFormat[2] == 0x6b &&
dh->info.dataFormat[3] == 0x20)
// Note: info.fFormatVersion is duplicated in the RBBIDataHeader, and is
// validated when checking that.
) {
status = U_INVALID_FORMAT_ERROR;
return;
}
const char *dataAsBytes = reinterpret_cast<const char *>(dh);
const RBBIDataHeader *rbbidh = reinterpret_cast<const RBBIDataHeader *>(dataAsBytes + headerSize);
init(rbbidh, status);
fUDataMem = udm;
}
@ -69,6 +88,19 @@ RBBIDataWrapper::RBBIDataWrapper(UDataMemory* udm, UErrorCode &status) {
// constructors.
//
//-----------------------------------------------------------------------------
void RBBIDataWrapper::init0() {
fHeader = NULL;
fForwardTable = NULL;
fReverseTable = NULL;
fSafeFwdTable = NULL;
fSafeRevTable = NULL;
fRuleSource = NULL;
fRuleStatusTable = NULL;
fUDataMem = NULL;
fRefCount = 0;
fDontFreeData = TRUE;
}
void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) {
if (U_FAILURE(status)) {
return;
@ -84,10 +116,6 @@ void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) {
// an int32_t field, rather than an array of 4 bytes.
fDontFreeData = FALSE;
fUDataMem = NULL;
fReverseTable = NULL;
fSafeFwdTable = NULL;
fSafeRevTable = NULL;
if (data->fFTableLen != 0) {
fForwardTable = (RBBIStateTable *)((char *)data + fHeader->fFTable);
}

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
*
* Copyright (C) 1999-2013 International Business Machines
* Copyright (C) 1999-2014 International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
@ -150,6 +150,7 @@ public:
RBBIDataWrapper(UDataMemory* udm, UErrorCode &status);
~RBBIDataWrapper();
void init0();
void init(const RBBIDataHeader *data, UErrorCode &status);
RBBIDataWrapper *addReference();
void removeReference();
@ -182,7 +183,7 @@ public:
private:
u_atomic_int32_t fRefCount;
UDataMemory *fUDataMem;
UDataMemory *fUDataMem;
UnicodeString fRuleString;
UBool fDontFreeData;

View file

@ -96,7 +96,7 @@ U_CDECL_BEGIN
*
* <p>The <code>formatVersion</code> field should be used to
* make sure that the format can be interpreted.
* I may be a good idea to check only for the one or two highest
* It may be a good idea to check only for the one or two highest
* of the version elements to allow the data memory to
* get more or somewhat rearranged contents, for as long
* as the using code can still interpret the older contents.</p>