X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/46f4442e9a5a4f3b98b7c1083586332f6a8a99a4..a01113dcd0f39d5da295ef82785beff9ed86fe38:/icuSources/common/rbbidata.cpp diff --git a/icuSources/common/rbbidata.cpp b/icuSources/common/rbbidata.cpp index 442fb3d4..1d4c9e58 100644 --- a/icuSources/common/rbbidata.cpp +++ b/icuSources/common/rbbidata.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* *************************************************************************** -* Copyright (C) 1999-2008 International Business Machines Corporation * +* Copyright (C) 1999-2014 International Business Machines Corporation * * and others. All rights reserved. * *************************************************************************** */ @@ -12,7 +14,7 @@ #include "unicode/utypes.h" #include "rbbidata.h" #include "rbbirb.h" -#include "utrie.h" +#include "utrie2.h" #include "udatamem.h" #include "cmemory.h" #include "cstring.h" @@ -21,23 +23,6 @@ #include "uassert.h" -//----------------------------------------------------------------------------------- -// -// Trie access folding function. Copied as-is from properties code in uchar.c -// -//----------------------------------------------------------------------------------- -U_CDECL_BEGIN -static int32_t U_CALLCONV -getFoldingOffset(uint32_t data) { - /* if bit 15 is set, then the folding offset is in bits 14..0 of the 16-bit trie result */ - if(data&0x8000) { - return (int32_t)(data&0x7fff); - } else { - return 0; - } -} -U_CDECL_END - U_NAMESPACE_BEGIN //----------------------------------------------------------------------------- @@ -46,70 +31,93 @@ 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 && + isDataVersionAcceptable(dh->info.formatVersion)) + ) { + status = U_INVALID_FORMAT_ERROR; + return; + } + const char *dataAsBytes = reinterpret_cast(dh); + const RBBIDataHeader *rbbidh = reinterpret_cast(dataAsBytes + headerSize); + init(rbbidh, status); fUDataMem = udm; } +UBool RBBIDataWrapper::isDataVersionAcceptable(const UVersionInfo version) { + return RBBI_DATA_FORMAT_VERSION[0] == version[0]; +} + + //----------------------------------------------------------------------------- // // init(). Does most of the work of construction, shared between the // constructors. // //----------------------------------------------------------------------------- +void RBBIDataWrapper::init0() { + fHeader = NULL; + fForwardTable = NULL; + fReverseTable = NULL; + fRuleSource = NULL; + fRuleStatusTable = NULL; + fTrie = NULL; + fUDataMem = NULL; + fRefCount = 0; + fDontFreeData = TRUE; +} + void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) { if (U_FAILURE(status)) { return; } fHeader = data; - if (fHeader->fMagic != 0xb1a0 || - !(fHeader->fFormatVersion[0] == 3 || // ICU 3.4 - *(int32_t *)fHeader->fFormatVersion == 1)) // ICU 3.2 and earlier. - { + if (fHeader->fMagic != 0xb1a0 || !isDataVersionAcceptable(fHeader->fFormatVersion)) { status = U_INVALID_FORMAT_ERROR; return; } + // Note: in ICU version 3.2 and earlier, there was a formatVersion 1 + // that is no longer supported. At that time fFormatVersion was + // 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); } if (data->fRTableLen != 0) { fReverseTable = (RBBIStateTable *)((char *)data + fHeader->fRTable); } - if (data->fSFTableLen != 0) { - fSafeFwdTable = (RBBIStateTable *)((char *)data + fHeader->fSFTable); - } - if (data->fSRTableLen != 0) { - fSafeRevTable = (RBBIStateTable *)((char *)data + fHeader->fSRTable); - } - - utrie_unserialize(&fTrie, - (uint8_t *)data + fHeader->fTrie, - fHeader->fTrieLen, - &status); + fTrie = utrie2_openFromSerialized(UTRIE2_16_VALUE_BITS, + (uint8_t *)data + fHeader->fTrie, + fHeader->fTrieLen, + NULL, // *actual length + &status); if (U_FAILURE(status)) { return; } - fTrie.getFoldingOffset=getFoldingOffset; - fRuleSource = (UChar *)((char *)data + fHeader->fRuleSource); fRuleString.setTo(TRUE, fRuleSource, -1); @@ -134,6 +142,8 @@ void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) { //----------------------------------------------------------------------------- RBBIDataWrapper::~RBBIDataWrapper() { U_ASSERT(fRefCount == 0); + utrie2_close(fTrie); + fTrie = NULL; if (fUDataMem) { udata_close(fUDataMem); } else if (!fDontFreeData) { @@ -240,8 +250,8 @@ void RBBIDataWrapper::printTable(const char *heading, const RBBIStateTable *tab #endif -#ifdef RBBI_DEBUG void RBBIDataWrapper::printData() { +#ifdef RBBI_DEBUG RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader); RBBIDebugPrintf(" Version = {%d %d %d %d}\n", fHeader->fFormatVersion[0], fHeader->fFormatVersion[1], fHeader->fFormatVersion[2], fHeader->fFormatVersion[3]); @@ -250,16 +260,14 @@ void RBBIDataWrapper::printData() { printTable("Forward State Transition Table", fForwardTable); printTable("Reverse State Transition Table", fReverseTable); - printTable("Safe Forward State Transition Table", fSafeFwdTable); - printTable("Safe Reverse State Transition Table", fSafeRevTable); RBBIDebugPrintf("\nOrignal Rules source:\n"); for (int32_t c=0; fRuleSource[c] != 0; c++) { RBBIDebugPrintf("%c", fRuleSource[c]); } RBBIDebugPrintf("\n\n"); -} #endif +} U_NAMESPACE_END @@ -292,7 +300,7 @@ ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outD pInfo->dataFormat[1]==0x72 && pInfo->dataFormat[2]==0x6b && pInfo->dataFormat[3]==0x20 && - pInfo->formatVersion[0]==3 )) { + RBBIDataWrapper::isDataVersionAcceptable(pInfo->formatVersion) )) { udata_printError(ds, "ubrk_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized\n", pInfo->dataFormat[0], pInfo->dataFormat[1], pInfo->dataFormat[2], pInfo->dataFormat[3], @@ -313,18 +321,11 @@ ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outD // // Get the RRBI Data Header, and check that it appears to be OK. // - // Note: ICU 3.2 and earlier, RBBIDataHeader::fDataFormat was actually - // an int32_t with a value of 1. Starting with ICU 3.4, - // RBBI's fDataFormat matches the dataFormat field from the - // UDataInfo header, four int8_t bytes. The value is {3,1,0,0} - // const uint8_t *inBytes =(const uint8_t *)inData+headerSize; RBBIDataHeader *rbbiDH = (RBBIDataHeader *)inBytes; - UBool formatVersionOne = ds->readUInt32(*(int32_t *)rbbiDH->fFormatVersion) == 1; - if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 || - !(formatVersionOne || rbbiDH->fFormatVersion[0] == 3) || - ds->readUInt32(rbbiDH->fLength) < sizeof(RBBIDataHeader)) - { + if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 || + !RBBIDataWrapper::isDataVersionAcceptable(rbbiDH->fFormatVersion) || + ds->readUInt32(rbbiDH->fLength) < sizeof(RBBIDataHeader)) { udata_printError(ds, "ubrk_swap(): RBBI Data header is invalid.\n"); *status=U_UNSUPPORTED_ERROR; return 0; @@ -398,31 +399,9 @@ ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outD outBytes+tableStartOffset+topSize, status); } - // Safe Forward state table. Same layout as forward table, above. - tableStartOffset = ds->readUInt32(rbbiDH->fSFTable); - tableLength = ds->readUInt32(rbbiDH->fSFTableLen); - - if (tableLength > 0) { - ds->swapArray32(ds, inBytes+tableStartOffset, topSize, - outBytes+tableStartOffset, status); - ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize, - outBytes+tableStartOffset+topSize, status); - } - - // Safe Reverse state table. Same layout as forward table, above. - tableStartOffset = ds->readUInt32(rbbiDH->fSRTable); - tableLength = ds->readUInt32(rbbiDH->fSRTableLen); - - if (tableLength > 0) { - ds->swapArray32(ds, inBytes+tableStartOffset, topSize, - outBytes+tableStartOffset, status); - ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize, - outBytes+tableStartOffset+topSize, status); - } - // Trie table for character categories - utrie_swap(ds, inBytes+ds->readUInt32(rbbiDH->fTrie), ds->readUInt32(rbbiDH->fTrieLen), - outBytes+ds->readUInt32(rbbiDH->fTrie), status); + utrie2_swap(ds, inBytes+ds->readUInt32(rbbiDH->fTrie), ds->readUInt32(rbbiDH->fTrieLen), + outBytes+ds->readUInt32(rbbiDH->fTrie), status); // Source Rules Text. It's UChar data ds->swapArray16(ds, inBytes+ds->readUInt32(rbbiDH->fRuleSource), ds->readUInt32(rbbiDH->fRuleSourceLen), @@ -433,15 +412,11 @@ ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outD outBytes+ds->readUInt32(rbbiDH->fStatusTable), status); // And, last, the header. - // For the old version one format, the entire header consists of int32_t values. - // For the newer formats, the fDataFormat field is an array of four bytes. - // Swap the whole thing as int32_t, then, for the newer format, re-swap the one field. + // It is all int32_t values except for fFormataVersion, which is an array of four bytes. + // Swap the whole thing as int32_t, then re-swap the one field. // ds->swapArray32(ds, inBytes, sizeof(RBBIDataHeader), outBytes, status); - if (formatVersionOne == FALSE) { - ds->swapArray32(ds, outputDH->fFormatVersion, 4, outputDH->fFormatVersion, status); - } - + ds->swapArray32(ds, outputDH->fFormatVersion, 4, outputDH->fFormatVersion, status); return totalSize; }