diff --git a/icu4c/source/common/rbbidata.cpp b/icu4c/source/common/rbbidata.cpp index 09d47964e61..271553a89c1 100644 --- a/icu4c/source/common/rbbidata.cpp +++ b/icu4c/source/common/rbbidata.cpp @@ -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(dh); + const RBBIDataHeader *rbbidh = reinterpret_cast(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); } diff --git a/icu4c/source/common/rbbidata.h b/icu4c/source/common/rbbidata.h index 78a4ac762c8..65274c3adbb 100644 --- a/icu4c/source/common/rbbidata.h +++ b/icu4c/source/common/rbbidata.h @@ -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; diff --git a/icu4c/source/common/unicode/udata.h b/icu4c/source/common/unicode/udata.h index a95fe16bcb6..29e46630d53 100644 --- a/icu4c/source/common/unicode/udata.h +++ b/icu4c/source/common/unicode/udata.h @@ -96,7 +96,7 @@ U_CDECL_BEGIN * *

The formatVersion 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.