+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
***************************************************************************
-* Copyright (C) 1999-2004 International Business Machines Corporation *
+* Copyright (C) 1999-2014 International Business Machines Corporation *
* and others. All rights reserved. *
***************************************************************************
*/
//
//-----------------------------------------------------------------------------
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;
}
// 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;
}
fHeader = data;
- if (fHeader->fMagic != 0xb1a0) {
- status = U_BRK_INTERNAL_ERROR;
+ if (fHeader->fMagic != 0xb1a0 || fHeader->fFormatVersion[0] != 3)
+ {
+ 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.
- fUDataMem = NULL;
- fReverseTable = NULL;
- fSafeFwdTable = NULL;
- fSafeRevTable = NULL;
+ fDontFreeData = FALSE;
if (data->fFTableLen != 0) {
fForwardTable = (RBBIStateTable *)((char *)data + fHeader->fFTable);
}
U_ASSERT(fRefCount == 0);
if (fUDataMem) {
udata_close(fUDataMem);
- } else {
+ } else if (!fDontFreeData) {
uprv_free((void *)fHeader);
}
}
#ifdef RBBI_DEBUG
void RBBIDataWrapper::printData() {
RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader);
- RBBIDebugPrintf(" Version = %d\n", fHeader->fVersion);
+ RBBIDebugPrintf(" Version = {%d %d %d %d}\n", fHeader->fFormatVersion[0], fHeader->fFormatVersion[1],
+ fHeader->fFormatVersion[2], fHeader->fFormatVersion[3]);
RBBIDebugPrintf(" total length of data = %d\n", fHeader->fLength);
RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader->fCatCount);
U_NAMESPACE_END
+U_NAMESPACE_USE
//-----------------------------------------------------------------------------
//
if (status == NULL || U_FAILURE(*status)) {
return 0;
}
+ if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) {
+ *status=U_ILLEGAL_ARGUMENT_ERROR;
+ return 0;
+ }
//
// Check that the data header is for for break data.
//
// 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;
- if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 ||
- ds->readUInt32(rbbiDH->fVersion) != 1 ||
+ if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 ||
+ rbbiDH->fFormatVersion[0] != 3 ||
ds->readUInt32(rbbiDH->fLength) < sizeof(RBBIDataHeader))
{
udata_printError(ds, "ubrk_swap(): RBBI Data header is invalid.\n");
//
// Prefight operation? Just return the size
//
- int32_t totalSize = headerSize + ds->readUInt32(rbbiDH->fLength);
+ int32_t breakDataLength = ds->readUInt32(rbbiDH->fLength);
+ int32_t totalSize = headerSize + breakDataLength;
if (length < 0) {
return totalSize;
}
//
// Check that length passed in is consistent with length from RBBI data header.
//
- if (length > 0) {
- length -= headerSize;
- if ((uint32_t)length < ds->readUInt32(rbbiDH->fLength)) {
- udata_printError(ds, "ubrk_swap(): too few bytes (%d after ICU Data header) for break data.\n",
- length);
- *status=U_INDEX_OUTOFBOUNDS_ERROR;
- return 0;
+ if (length < totalSize) {
+ udata_printError(ds, "ubrk_swap(): too few bytes (%d after ICU Data header) for break data.\n",
+ breakDataLength);
+ *status=U_INDEX_OUTOFBOUNDS_ERROR;
+ return 0;
}
- }
//
// we need to reference the header to locate the data, and an
// inplace swap of the header leaves it unusable.
//
- uint8_t *outBytes = (uint8_t *)outData + headerSize;
+ uint8_t *outBytes = (uint8_t *)outData + headerSize;
+ RBBIDataHeader *outputDH = (RBBIDataHeader *)outBytes;
+
int32_t tableStartOffset;
int32_t tableLength;
// when originally created. Any unused space between items needs to be zero.
//
if (inBytes != outBytes) {
- uprv_memset(outBytes, 0, length);
+ uprv_memset(outBytes, 0, breakDataLength);
}
//
// Each state table begins with several 32 bit fields. Calculate the size
// in bytes of these.
//
- RBBIStateTable *stp = NULL;
- int32_t topSize = (char *)stp->fTableData - (char *)stp;
+ int32_t topSize = offsetof(RBBIStateTable, fTableData);
// Forward state table.
tableStartOffset = ds->readUInt32(rbbiDH->fFTable);
ds->swapArray32(ds, inBytes+ds->readUInt32(rbbiDH->fStatusTable), ds->readUInt32(rbbiDH->fStatusTableLen),
outBytes+ds->readUInt32(rbbiDH->fStatusTable), status);
- // And, last, the header. All 32 bit values.
- ds->swapArray32(ds, inBytes, sizeof(RBBIDataHeader), outBytes, status);
+ // And, last, the header.
+ // 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);
+ ds->swapArray32(ds, outputDH->fFormatVersion, 4, outputDH->fFormatVersion, status);
return totalSize;
}