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
|
2002-06-25 17:23:07 +00:00
|
|
|
/*
|
2002-08-08 00:39:13 +00:00
|
|
|
***************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1999-2014 International Business Machines Corporation *
|
2002-08-08 00:39:13 +00:00
|
|
|
* and others. All rights reserved. *
|
|
|
|
***************************************************************************
|
2002-06-25 17:23:07 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
2020-05-19 20:44:14 +00:00
|
|
|
#include "unicode/ucptrie.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "rbbidata.h"
|
2002-08-01 16:17:41 +00:00
|
|
|
#include "rbbirb.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "udatamem.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "cmemory.h"
|
2002-08-01 16:17:41 +00:00
|
|
|
#include "cstring.h"
|
2002-08-01 17:41:06 +00:00
|
|
|
#include "umutex.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-07-22 23:24:55 +00:00
|
|
|
#include "uassert.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
2002-08-01 16:17:41 +00:00
|
|
|
// Constructors.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, UErrorCode &status) {
|
2014-09-05 21:23:48 +00:00
|
|
|
init0();
|
2002-06-25 17:23:07 +00:00
|
|
|
init(data, status);
|
|
|
|
}
|
|
|
|
|
2008-09-25 05:48:27 +00:00
|
|
|
RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, enum EDontAdopt, UErrorCode &status) {
|
2014-09-05 21:23:48 +00:00
|
|
|
init0();
|
2008-09-25 05:48:27 +00:00
|
|
|
init(data, status);
|
|
|
|
fDontFreeData = TRUE;
|
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
RBBIDataWrapper::RBBIDataWrapper(UDataMemory* udm, UErrorCode &status) {
|
2014-09-05 21:23:48 +00:00
|
|
|
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 &&
|
2017-06-20 22:11:08 +00:00
|
|
|
dh->info.dataFormat[3] == 0x20 &&
|
|
|
|
isDataVersionAcceptable(dh->info.formatVersion))
|
2014-09-05 21:23:48 +00:00
|
|
|
) {
|
|
|
|
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);
|
2002-06-25 17:23:07 +00:00
|
|
|
fUDataMem = udm;
|
|
|
|
}
|
|
|
|
|
2017-07-17 21:22:53 +00:00
|
|
|
UBool RBBIDataWrapper::isDataVersionAcceptable(const UVersionInfo version) {
|
|
|
|
return RBBI_DATA_FORMAT_VERSION[0] == version[0];
|
2017-06-20 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// init(). Does most of the work of construction, shared between the
|
2002-08-01 16:17:41 +00:00
|
|
|
// constructors.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-09-05 21:23:48 +00:00
|
|
|
void RBBIDataWrapper::init0() {
|
|
|
|
fHeader = NULL;
|
|
|
|
fForwardTable = NULL;
|
2018-03-28 01:20:13 +00:00
|
|
|
fReverseTable = NULL;
|
2017-09-19 18:17:22 +00:00
|
|
|
fRuleSource = NULL;
|
2014-09-05 21:23:48 +00:00
|
|
|
fRuleStatusTable = NULL;
|
2017-09-19 18:17:22 +00:00
|
|
|
fTrie = NULL;
|
|
|
|
fUDataMem = NULL;
|
|
|
|
fRefCount = 0;
|
2014-09-05 21:23:48 +00:00
|
|
|
fDontFreeData = TRUE;
|
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fHeader = data;
|
2017-06-20 22:11:08 +00:00
|
|
|
if (fHeader->fMagic != 0xb1a0 || !isDataVersionAcceptable(fHeader->fFormatVersion)) {
|
2005-03-28 05:21:50 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2002-06-25 17:23:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-07-12 18:03:29 +00:00
|
|
|
// 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.
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2008-09-25 05:48:27 +00:00
|
|
|
fDontFreeData = FALSE;
|
2003-11-05 02:03:44 +00:00
|
|
|
if (data->fFTableLen != 0) {
|
|
|
|
fForwardTable = (RBBIStateTable *)((char *)data + fHeader->fFTable);
|
|
|
|
}
|
2018-03-26 23:01:16 +00:00
|
|
|
if (data->fRTableLen != 0) {
|
|
|
|
fReverseTable = (RBBIStateTable *)((char *)data + fHeader->fRTable);
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2020-05-19 20:44:14 +00:00
|
|
|
fTrie = ucptrie_openFromBinary(UCPTRIE_TYPE_FAST,
|
|
|
|
UCPTRIE_VALUE_BITS_ANY,
|
|
|
|
(uint8_t *)data + fHeader->fTrie,
|
|
|
|
fHeader->fTrieLen,
|
|
|
|
nullptr, // *actual length
|
|
|
|
&status);
|
2002-06-25 17:23:07 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:44:14 +00:00
|
|
|
UCPTrieValueWidth width = ucptrie_getValueWidth(fTrie);
|
|
|
|
if (!(width == UCPTRIE_VALUE_BITS_8 || width == UCPTRIE_VALUE_BITS_16)) {
|
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fRuleSource = ((char *)data + fHeader->fRuleSource);
|
|
|
|
fRuleString = UnicodeString::fromUTF8(StringPiece(fRuleSource, fHeader->fRuleSourceLen));
|
2004-03-05 05:04:10 +00:00
|
|
|
U_ASSERT(data->fRuleSourceLen > 0);
|
|
|
|
|
|
|
|
fRuleStatusTable = (int32_t *)((char *)data + fHeader->fStatusTable);
|
|
|
|
fStatusMaxIdx = data->fStatusTableLen / sizeof(int32_t);
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
fRefCount = 1;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
char *debugEnv = getenv("U_RBBIDEBUG");
|
|
|
|
if (debugEnv && uprv_strstr(debugEnv, "data")) {this->printData();}
|
|
|
|
#endif
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
2004-03-05 05:04:10 +00:00
|
|
|
// Destructor. Don't call this - use removeReference() instead.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
RBBIDataWrapper::~RBBIDataWrapper() {
|
2002-07-22 23:24:55 +00:00
|
|
|
U_ASSERT(fRefCount == 0);
|
2020-05-19 20:44:14 +00:00
|
|
|
ucptrie_close(fTrie);
|
|
|
|
fTrie = nullptr;
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fUDataMem) {
|
|
|
|
udata_close(fUDataMem);
|
2008-09-25 05:48:27 +00:00
|
|
|
} else if (!fDontFreeData) {
|
2002-06-25 17:23:07 +00:00
|
|
|
uprv_free((void *)fHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Operator == Consider two RBBIDataWrappers to be equal if they
|
|
|
|
// refer to the same underlying data. Although
|
|
|
|
// the data wrappers are normally shared between
|
|
|
|
// iterator instances, it's possible to independently
|
|
|
|
// open the same data twice, and get two instances, which
|
|
|
|
// should still be ==.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
UBool RBBIDataWrapper::operator ==(const RBBIDataWrapper &other) const {
|
|
|
|
if (fHeader == other.fHeader) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (fHeader->fLength != other.fHeader->fLength) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (uprv_memcmp(fHeader, other.fHeader, fHeader->fLength) == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t RBBIDataWrapper::hashCode() {
|
|
|
|
return fHeader->fFTableLen;
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Reference Counting. A single RBBIDataWrapper object is shared among
|
|
|
|
// however many RulesBasedBreakIterator instances are
|
|
|
|
// referencing the same data.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void RBBIDataWrapper::removeReference() {
|
2002-08-08 00:39:13 +00:00
|
|
|
if (umtx_atomic_dec(&fRefCount) == 0) {
|
2002-06-25 17:23:07 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
RBBIDataWrapper *RBBIDataWrapper::addReference() {
|
2002-08-01 17:41:06 +00:00
|
|
|
umtx_atomic_inc(&fRefCount);
|
2002-06-25 17:23:07 +00:00
|
|
|
return this;
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// getRuleSourceString
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2004-03-27 07:05:32 +00:00
|
|
|
const UnicodeString &RBBIDataWrapper::getRuleSourceString() const {
|
2002-06-25 17:23:07 +00:00
|
|
|
return fRuleString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// print - debugging function to dump the runtime data tables.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2003-05-16 20:11:01 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
2003-12-02 02:23:08 +00:00
|
|
|
void RBBIDataWrapper::printTable(const char *heading, const RBBIStateTable *table) {
|
2003-11-05 02:03:44 +00:00
|
|
|
uint32_t c;
|
|
|
|
uint32_t s;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
ICU-13590 RBBI, improve handling of concurrent look-ahead rules.
Change the mapping from rule number to boundary position to use a simple array
instead of a linear search lookup map.
Look-ahead rules have a preceding context, a boundary position, and following context.
In the implementation, when the preceding context matches, the potential boundary
position is saved. Then, if the following context proves to match, the saved boundary is
returned as an actual boundary.
Look-ahead rules are numbered, and the implementation maintains a map from
rule number to the tentative saved boundary position.
In an earlier improvement to the rule builder, the rule numbering was changed to be a
contiguous sequence, from the original sparse numbering. In anticipation of
changing the mapping from number to position to use a simple array.
2020-06-27 00:52:40 +00:00
|
|
|
RBBIDebugPrintf("%s\n", heading);
|
2003-11-06 21:26:07 +00:00
|
|
|
|
ICU-13590 RBBI, improve handling of concurrent look-ahead rules.
Change the mapping from rule number to boundary position to use a simple array
instead of a linear search lookup map.
Look-ahead rules have a preceding context, a boundary position, and following context.
In the implementation, when the preceding context matches, the potential boundary
position is saved. Then, if the following context proves to match, the saved boundary is
returned as an actual boundary.
Look-ahead rules are numbered, and the implementation maintains a map from
rule number to the tentative saved boundary position.
In an earlier improvement to the rule builder, the rule numbering was changed to be a
contiguous sequence, from the original sparse numbering. In anticipation of
changing the mapping from number to position to use a simple array.
2020-06-27 00:52:40 +00:00
|
|
|
RBBIDebugPrintf(" fDictCategoriesStart: %d\n", table->fDictCategoriesStart);
|
|
|
|
RBBIDebugPrintf(" fLookAheadResultsSize: %d\n", table->fLookAheadResultsSize);
|
|
|
|
RBBIDebugPrintf(" Flags: %4x RBBI_LOOKAHEAD_HARD_BREAK=%s RBBI_BOF_REQUIRED=%s RBBI_8BITS_ROWS=%s\n",
|
2020-05-19 20:44:14 +00:00
|
|
|
table->fFlags,
|
|
|
|
table->fFlags & RBBI_LOOKAHEAD_HARD_BREAK ? "T" : "F",
|
|
|
|
table->fFlags & RBBI_BOF_REQUIRED ? "T" : "F",
|
|
|
|
table->fFlags & RBBI_8BITS_ROWS ? "T" : "F");
|
ICU-13590 RBBI, improve handling of concurrent look-ahead rules.
Change the mapping from rule number to boundary position to use a simple array
instead of a linear search lookup map.
Look-ahead rules have a preceding context, a boundary position, and following context.
In the implementation, when the preceding context matches, the potential boundary
position is saved. Then, if the following context proves to match, the saved boundary is
returned as an actual boundary.
Look-ahead rules are numbered, and the implementation maintains a map from
rule number to the tentative saved boundary position.
In an earlier improvement to the rule builder, the rule numbering was changed to be a
contiguous sequence, from the original sparse numbering. In anticipation of
changing the mapping from number to position to use a simple array.
2020-06-27 00:52:40 +00:00
|
|
|
RBBIDebugPrintf("\nState | Acc LA TagIx");
|
2002-11-30 04:41:53 +00:00
|
|
|
for (c=0; c<fHeader->fCatCount; c++) {RBBIDebugPrintf("%3d ", c);}
|
2003-11-05 02:03:44 +00:00
|
|
|
RBBIDebugPrintf("\n------|---------------"); for (c=0;c<fHeader->fCatCount; c++) {
|
|
|
|
RBBIDebugPrintf("----");
|
|
|
|
}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\n");
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2003-11-06 21:26:07 +00:00
|
|
|
if (table == NULL) {
|
|
|
|
RBBIDebugPrintf(" N U L L T A B L E\n\n");
|
|
|
|
return;
|
|
|
|
}
|
2020-05-19 20:44:14 +00:00
|
|
|
UBool use8Bits = table->fFlags & RBBI_8BITS_ROWS;
|
2003-11-05 02:03:44 +00:00
|
|
|
for (s=0; s<table->fNumStates; s++) {
|
2002-06-25 17:23:07 +00:00
|
|
|
RBBIStateTableRow *row = (RBBIStateTableRow *)
|
2003-11-05 02:03:44 +00:00
|
|
|
(table->fTableData + (table->fRowLen * s));
|
2020-05-19 20:44:14 +00:00
|
|
|
if (use8Bits) {
|
2020-05-27 23:36:22 +00:00
|
|
|
RBBIDebugPrintf("%4d | %3d %3d %3d ", s, row->r8.fAccepting, row->r8.fLookAhead, row->r8.fTagsIdx);
|
2020-05-19 20:44:14 +00:00
|
|
|
for (c=0; c<fHeader->fCatCount; c++) {
|
|
|
|
RBBIDebugPrintf("%3d ", row->r8.fNextState[c]);
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-27 23:36:22 +00:00
|
|
|
RBBIDebugPrintf("%4d | %3d %3d %3d ", s, row->r16.fAccepting, row->r16.fLookAhead, row->r16.fTagsIdx);
|
2020-05-19 20:44:14 +00:00
|
|
|
for (c=0; c<fHeader->fCatCount; c++) {
|
|
|
|
RBBIDebugPrintf("%3d ", row->r16.fNextState[c]);
|
|
|
|
}
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\n");
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2003-11-05 02:03:44 +00:00
|
|
|
RBBIDebugPrintf("\n");
|
|
|
|
}
|
2003-12-02 02:23:08 +00:00
|
|
|
#endif
|
2003-11-05 02:03:44 +00:00
|
|
|
|
|
|
|
|
2003-12-04 22:44:05 +00:00
|
|
|
void RBBIDataWrapper::printData() {
|
2018-02-08 01:42:04 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
2003-11-05 02:03:44 +00:00
|
|
|
RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader);
|
2005-03-28 05:21:50 +00:00
|
|
|
RBBIDebugPrintf(" Version = {%d %d %d %d}\n", fHeader->fFormatVersion[0], fHeader->fFormatVersion[1],
|
|
|
|
fHeader->fFormatVersion[2], fHeader->fFormatVersion[3]);
|
2003-11-05 02:03:44 +00:00
|
|
|
RBBIDebugPrintf(" total length of data = %d\n", fHeader->fLength);
|
|
|
|
RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader->fCatCount);
|
|
|
|
|
|
|
|
printTable("Forward State Transition Table", fForwardTable);
|
2018-03-28 01:20:13 +00:00
|
|
|
printTable("Reverse State Transition Table", fReverseTable);
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\nOrignal Rules source:\n");
|
2003-11-05 02:03:44 +00:00
|
|
|
for (int32_t c=0; fRuleSource[c] != 0; c++) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%c", fRuleSource[c]);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\n\n");
|
2003-12-04 22:44:05 +00:00
|
|
|
#endif
|
2018-02-08 01:42:04 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
|
2003-11-19 18:00:50 +00:00
|
|
|
U_NAMESPACE_END
|
2006-09-03 17:08:23 +00:00
|
|
|
U_NAMESPACE_USE
|
2003-11-19 18:00:50 +00:00
|
|
|
|
2003-09-29 17:24:15 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// ubrk_swap - byte swap and char encoding swap of RBBI data
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData,
|
|
|
|
UErrorCode *status) {
|
|
|
|
|
|
|
|
if (status == NULL || U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-31 08:29:56 +00:00
|
|
|
if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) {
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-29 17:24:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Check that the data header is for for break data.
|
|
|
|
// (Header contents are defined in genbrk.cpp)
|
|
|
|
//
|
|
|
|
const UDataInfo *pInfo = (const UDataInfo *)((const char *)inData+4);
|
|
|
|
if(!( pInfo->dataFormat[0]==0x42 && /* dataFormat="Brk " */
|
|
|
|
pInfo->dataFormat[1]==0x72 &&
|
|
|
|
pInfo->dataFormat[2]==0x6b &&
|
|
|
|
pInfo->dataFormat[3]==0x20 &&
|
2017-06-20 22:11:08 +00:00
|
|
|
RBBIDataWrapper::isDataVersionAcceptable(pInfo->formatVersion) )) {
|
2003-09-29 17:24:15 +00:00
|
|
|
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],
|
|
|
|
pInfo->formatVersion[0]);
|
|
|
|
*status=U_UNSUPPORTED_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Swap the data header. (This is the generic ICU Data Header, not the RBBI Specific
|
|
|
|
// RBBIDataHeader). This swap also conveniently gets us
|
|
|
|
// the size of the ICU d.h., which lets us locate the start
|
|
|
|
// of the RBBI specific data.
|
|
|
|
//
|
|
|
|
int32_t headerSize=udata_swapDataHeader(ds, inData, length, outData, status);
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the RRBI Data Header, and check that it appears to be OK.
|
|
|
|
//
|
|
|
|
const uint8_t *inBytes =(const uint8_t *)inData+headerSize;
|
|
|
|
RBBIDataHeader *rbbiDH = (RBBIDataHeader *)inBytes;
|
2010-07-12 18:03:29 +00:00
|
|
|
if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 ||
|
2017-06-20 22:11:08 +00:00
|
|
|
!RBBIDataWrapper::isDataVersionAcceptable(rbbiDH->fFormatVersion) ||
|
|
|
|
ds->readUInt32(rbbiDH->fLength) < sizeof(RBBIDataHeader)) {
|
2003-09-29 17:24:15 +00:00
|
|
|
udata_printError(ds, "ubrk_swap(): RBBI Data header is invalid.\n");
|
|
|
|
*status=U_UNSUPPORTED_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Prefight operation? Just return the size
|
|
|
|
//
|
2005-08-01 23:27:52 +00:00
|
|
|
int32_t breakDataLength = ds->readUInt32(rbbiDH->fLength);
|
|
|
|
int32_t totalSize = headerSize + breakDataLength;
|
2003-09-29 17:24:15 +00:00
|
|
|
if (length < 0) {
|
|
|
|
return totalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check that length passed in is consistent with length from RBBI data header.
|
|
|
|
//
|
2005-08-01 23:27:52 +00:00
|
|
|
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;
|
2003-09-29 17:24:15 +00:00
|
|
|
}
|
|
|
|
|
2003-11-20 00:23:25 +00:00
|
|
|
|
2003-09-29 17:24:15 +00:00
|
|
|
//
|
|
|
|
// Swap the Data. Do the data itself first, then the RBBI Data Header, because
|
|
|
|
// we need to reference the header to locate the data, and an
|
|
|
|
// inplace swap of the header leaves it unusable.
|
|
|
|
//
|
2005-03-28 05:21:50 +00:00
|
|
|
uint8_t *outBytes = (uint8_t *)outData + headerSize;
|
|
|
|
RBBIDataHeader *outputDH = (RBBIDataHeader *)outBytes;
|
|
|
|
|
2003-11-20 00:23:25 +00:00
|
|
|
int32_t tableStartOffset;
|
|
|
|
int32_t tableLength;
|
2003-09-29 17:24:15 +00:00
|
|
|
|
2003-11-20 00:23:25 +00:00
|
|
|
//
|
|
|
|
// If not swapping in place, zero out the output buffer before starting.
|
|
|
|
// Individual tables and other data items within are aligned to 8 byte boundaries
|
|
|
|
// when originally created. Any unused space between items needs to be zero.
|
|
|
|
//
|
|
|
|
if (inBytes != outBytes) {
|
2005-08-01 23:27:52 +00:00
|
|
|
uprv_memset(outBytes, 0, breakDataLength);
|
2003-11-20 00:23:25 +00:00
|
|
|
}
|
2003-09-29 17:24:15 +00:00
|
|
|
|
2003-12-04 02:12:42 +00:00
|
|
|
//
|
|
|
|
// Each state table begins with several 32 bit fields. Calculate the size
|
|
|
|
// in bytes of these.
|
|
|
|
//
|
2007-09-05 15:24:21 +00:00
|
|
|
int32_t topSize = offsetof(RBBIStateTable, fTableData);
|
2003-12-04 02:12:42 +00:00
|
|
|
|
2020-05-19 20:44:14 +00:00
|
|
|
// Forward state table.
|
2003-11-20 00:23:25 +00:00
|
|
|
tableStartOffset = ds->readUInt32(rbbiDH->fFTable);
|
|
|
|
tableLength = ds->readUInt32(rbbiDH->fFTableLen);
|
2004-01-14 21:52:34 +00:00
|
|
|
|
2004-11-11 23:34:58 +00:00
|
|
|
if (tableLength > 0) {
|
2020-05-19 20:44:14 +00:00
|
|
|
RBBIStateTable *rbbiST = (RBBIStateTable *)(inBytes+tableStartOffset);
|
|
|
|
UBool use8Bits = ds->readUInt32(rbbiST->fFlags) & RBBI_8BITS_ROWS;
|
|
|
|
|
|
|
|
ds->swapArray32(ds, inBytes+tableStartOffset, topSize,
|
2004-11-11 23:34:58 +00:00
|
|
|
outBytes+tableStartOffset, status);
|
2020-05-19 20:44:14 +00:00
|
|
|
|
|
|
|
// Swap the state table if the table is in 16 bits.
|
|
|
|
if (use8Bits) {
|
|
|
|
if (outBytes != inBytes) {
|
|
|
|
uprv_memmove(outBytes+tableStartOffset+topSize,
|
|
|
|
inBytes+tableStartOffset+topSize,
|
|
|
|
tableLength-topSize);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize,
|
|
|
|
outBytes+tableStartOffset+topSize, status);
|
|
|
|
}
|
2004-11-11 23:34:58 +00:00
|
|
|
}
|
2020-05-19 20:44:14 +00:00
|
|
|
|
2003-09-29 17:24:15 +00:00
|
|
|
// Reverse state table. Same layout as forward table, above.
|
2003-11-20 00:23:25 +00:00
|
|
|
tableStartOffset = ds->readUInt32(rbbiDH->fRTable);
|
|
|
|
tableLength = ds->readUInt32(rbbiDH->fRTableLen);
|
2004-01-14 21:52:34 +00:00
|
|
|
|
2004-11-11 23:34:58 +00:00
|
|
|
if (tableLength > 0) {
|
2020-05-19 20:44:14 +00:00
|
|
|
RBBIStateTable *rbbiST = (RBBIStateTable *)(inBytes+tableStartOffset);
|
|
|
|
UBool use8Bits = ds->readUInt32(rbbiST->fFlags) & RBBI_8BITS_ROWS;
|
|
|
|
|
|
|
|
ds->swapArray32(ds, inBytes+tableStartOffset, topSize,
|
2004-11-11 23:34:58 +00:00
|
|
|
outBytes+tableStartOffset, status);
|
2020-05-19 20:44:14 +00:00
|
|
|
|
|
|
|
// Swap the state table if the table is in 16 bits.
|
|
|
|
if (use8Bits) {
|
|
|
|
if (outBytes != inBytes) {
|
|
|
|
uprv_memmove(outBytes+tableStartOffset+topSize,
|
|
|
|
inBytes+tableStartOffset+topSize,
|
|
|
|
tableLength-topSize);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize,
|
|
|
|
outBytes+tableStartOffset+topSize, status);
|
|
|
|
}
|
2004-11-11 23:34:58 +00:00
|
|
|
}
|
2003-09-29 17:24:15 +00:00
|
|
|
|
|
|
|
// Trie table for character categories
|
2020-05-19 20:44:14 +00:00
|
|
|
ucptrie_swap(ds, inBytes+ds->readUInt32(rbbiDH->fTrie), ds->readUInt32(rbbiDH->fTrieLen),
|
|
|
|
outBytes+ds->readUInt32(rbbiDH->fTrie), status);
|
|
|
|
|
|
|
|
// Source Rules Text. It's UTF8 data
|
|
|
|
if (outBytes != inBytes) {
|
|
|
|
uprv_memmove(outBytes+ds->readUInt32(rbbiDH->fRuleSource),
|
|
|
|
inBytes+ds->readUInt32(rbbiDH->fRuleSource),
|
|
|
|
ds->readUInt32(rbbiDH->fRuleSourceLen));
|
|
|
|
}
|
2003-09-29 17:24:15 +00:00
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
// Table of rule status values. It's all int_32 values
|
|
|
|
ds->swapArray32(ds, inBytes+ds->readUInt32(rbbiDH->fStatusTable), ds->readUInt32(rbbiDH->fStatusTableLen),
|
|
|
|
outBytes+ds->readUInt32(rbbiDH->fStatusTable), status);
|
|
|
|
|
2005-03-28 05:21:50 +00:00
|
|
|
// And, last, the header.
|
2010-07-12 18:03:29 +00:00
|
|
|
// 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.
|
2005-03-28 05:21:50 +00:00
|
|
|
//
|
|
|
|
ds->swapArray32(ds, inBytes, sizeof(RBBIDataHeader), outBytes, status);
|
2010-07-12 18:03:29 +00:00
|
|
|
ds->swapArray32(ds, outputDH->fFormatVersion, 4, outputDH->fFormatVersion, status);
|
2003-09-29 17:24:15 +00:00
|
|
|
|
|
|
|
return totalSize;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
|