2 ***************************************************************************
3 * Copyright (C) 1999-2004 International Business Machines Corporation *
4 * and others. All rights reserved. *
5 ***************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_BREAK_ITERATION
12 #include "unicode/utypes.h"
24 //-----------------------------------------------------------------------------------
26 // Trie access folding function. Copied as-is from properties code in uchar.c
28 //-----------------------------------------------------------------------------------
30 static int32_t U_CALLCONV
31 getFoldingOffset(uint32_t data
) {
32 /* if bit 15 is set, then the folding offset is in bits 14..0 of the 16-bit trie result */
34 return (int32_t)(data
&0x7fff);
43 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader
*data
, UErrorCode
&status
) {
52 RBBIDataWrapper::RBBIDataWrapper(UDataMemory
* udm
, UErrorCode
&status
) {
53 const RBBIDataHeader
*d
= (const RBBIDataHeader
*)
54 // ((char *)&(udm->pHeader->info) + udm->pHeader->info.size);
55 // taking into consideration the padding added in by udata_write
56 ((char *)(udm
->pHeader
) + udm
->pHeader
->dataHeader
.headerSize
);
61 //-----------------------------------------------------------------------------
63 // init(). Does most of the work of construction, shared between the
66 //-----------------------------------------------------------------------------
67 void RBBIDataWrapper::init(const RBBIDataHeader
*data
, UErrorCode
&status
) {
68 if (U_FAILURE(status
)) {
72 if (fHeader
->fMagic
!= 0xb1a0) {
73 status
= U_BRK_INTERNAL_ERROR
;
81 if (data
->fFTableLen
!= 0) {
82 fForwardTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fFTable
);
84 if (data
->fRTableLen
!= 0) {
85 fReverseTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fRTable
);
87 if (data
->fSFTableLen
!= 0) {
88 fSafeFwdTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fSFTable
);
90 if (data
->fSRTableLen
!= 0) {
91 fSafeRevTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fSRTable
);
95 utrie_unserialize(&fTrie
,
96 (uint8_t *)data
+ fHeader
->fTrie
,
99 if (U_FAILURE(status
)) {
102 fTrie
.getFoldingOffset
=getFoldingOffset
;
105 fRuleSource
= (UChar
*)((char *)data
+ fHeader
->fRuleSource
);
106 fRuleString
.setTo(TRUE
, fRuleSource
, -1);
107 U_ASSERT(data
->fRuleSourceLen
> 0);
109 fRuleStatusTable
= (int32_t *)((char *)data
+ fHeader
->fStatusTable
);
110 fStatusMaxIdx
= data
->fStatusTableLen
/ sizeof(int32_t);
115 char *debugEnv
= getenv("U_RBBIDEBUG");
116 if (debugEnv
&& uprv_strstr(debugEnv
, "data")) {this->printData();}
121 //-----------------------------------------------------------------------------
123 // Destructor. Don't call this - use removeReference() instead.
125 //-----------------------------------------------------------------------------
126 RBBIDataWrapper::~RBBIDataWrapper() {
127 U_ASSERT(fRefCount
== 0);
129 udata_close(fUDataMem
);
131 uprv_free((void *)fHeader
);
137 //-----------------------------------------------------------------------------
139 // Operator == Consider two RBBIDataWrappers to be equal if they
140 // refer to the same underlying data. Although
141 // the data wrappers are normally shared between
142 // iterator instances, it's possible to independently
143 // open the same data twice, and get two instances, which
144 // should still be ==.
146 //-----------------------------------------------------------------------------
147 UBool
RBBIDataWrapper::operator ==(const RBBIDataWrapper
&other
) const {
148 if (fHeader
== other
.fHeader
) {
151 if (fHeader
->fLength
!= other
.fHeader
->fLength
) {
154 if (uprv_memcmp(fHeader
, other
.fHeader
, fHeader
->fLength
) == 0) {
160 int32_t RBBIDataWrapper::hashCode() {
161 return fHeader
->fFTableLen
;
166 //-----------------------------------------------------------------------------
168 // Reference Counting. A single RBBIDataWrapper object is shared among
169 // however many RulesBasedBreakIterator instances are
170 // referencing the same data.
172 //-----------------------------------------------------------------------------
173 void RBBIDataWrapper::removeReference() {
174 if (umtx_atomic_dec(&fRefCount
) == 0) {
180 RBBIDataWrapper
*RBBIDataWrapper::addReference() {
181 umtx_atomic_inc(&fRefCount
);
187 //-----------------------------------------------------------------------------
189 // getRuleSourceString
191 //-----------------------------------------------------------------------------
192 const UnicodeString
&RBBIDataWrapper::getRuleSourceString() const {
197 //-----------------------------------------------------------------------------
199 // print - debugging function to dump the runtime data tables.
201 //-----------------------------------------------------------------------------
203 void RBBIDataWrapper::printTable(const char *heading
, const RBBIStateTable
*table
) {
207 RBBIDebugPrintf(" %s\n", heading
);
209 RBBIDebugPrintf("State | Acc LA TagIx");
210 for (c
=0; c
<fHeader
->fCatCount
; c
++) {RBBIDebugPrintf("%3d ", c
);}
211 RBBIDebugPrintf("\n------|---------------"); for (c
=0;c
<fHeader
->fCatCount
; c
++) {
212 RBBIDebugPrintf("----");
214 RBBIDebugPrintf("\n");
217 RBBIDebugPrintf(" N U L L T A B L E\n\n");
220 for (s
=0; s
<table
->fNumStates
; s
++) {
221 RBBIStateTableRow
*row
= (RBBIStateTableRow
*)
222 (table
->fTableData
+ (table
->fRowLen
* s
));
223 RBBIDebugPrintf("%4d | %3d %3d %3d ", s
, row
->fAccepting
, row
->fLookAhead
, row
->fTagIdx
);
224 for (c
=0; c
<fHeader
->fCatCount
; c
++) {
225 RBBIDebugPrintf("%3d ", row
->fNextState
[c
]);
227 RBBIDebugPrintf("\n");
229 RBBIDebugPrintf("\n");
235 void RBBIDataWrapper::printData() {
236 RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader
);
237 RBBIDebugPrintf(" Version = %d\n", fHeader
->fVersion
);
238 RBBIDebugPrintf(" total length of data = %d\n", fHeader
->fLength
);
239 RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader
->fCatCount
);
241 printTable("Forward State Transition Table", fForwardTable
);
242 printTable("Reverse State Transition Table", fReverseTable
);
243 printTable("Safe Forward State Transition Table", fSafeFwdTable
);
244 printTable("Safe Reverse State Transition Table", fSafeRevTable
);
246 RBBIDebugPrintf("\nOrignal Rules source:\n");
247 for (int32_t c
=0; fRuleSource
[c
] != 0; c
++) {
248 RBBIDebugPrintf("%c", fRuleSource
[c
]);
250 RBBIDebugPrintf("\n\n");
257 //-----------------------------------------------------------------------------
259 // ubrk_swap - byte swap and char encoding swap of RBBI data
261 //-----------------------------------------------------------------------------
263 U_CAPI
int32_t U_EXPORT2
264 ubrk_swap(const UDataSwapper
*ds
, const void *inData
, int32_t length
, void *outData
,
265 UErrorCode
*status
) {
267 if (status
== NULL
|| U_FAILURE(*status
)) {
272 // Check that the data header is for for break data.
273 // (Header contents are defined in genbrk.cpp)
275 const UDataInfo
*pInfo
= (const UDataInfo
*)((const char *)inData
+4);
276 if(!( pInfo
->dataFormat
[0]==0x42 && /* dataFormat="Brk " */
277 pInfo
->dataFormat
[1]==0x72 &&
278 pInfo
->dataFormat
[2]==0x6b &&
279 pInfo
->dataFormat
[3]==0x20 &&
280 pInfo
->formatVersion
[0]==3 )) {
281 udata_printError(ds
, "ubrk_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized\n",
282 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
283 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
284 pInfo
->formatVersion
[0]);
285 *status
=U_UNSUPPORTED_ERROR
;
290 // Swap the data header. (This is the generic ICU Data Header, not the RBBI Specific
291 // RBBIDataHeader). This swap also conveniently gets us
292 // the size of the ICU d.h., which lets us locate the start
293 // of the RBBI specific data.
295 int32_t headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, status
);
299 // Get the RRBI Data Header, and check that it appears to be OK.
301 const uint8_t *inBytes
=(const uint8_t *)inData
+headerSize
;
302 RBBIDataHeader
*rbbiDH
= (RBBIDataHeader
*)inBytes
;
303 if (ds
->readUInt32(rbbiDH
->fMagic
) != 0xb1a0 ||
304 ds
->readUInt32(rbbiDH
->fVersion
) != 1 ||
305 ds
->readUInt32(rbbiDH
->fLength
) < sizeof(RBBIDataHeader
))
307 udata_printError(ds
, "ubrk_swap(): RBBI Data header is invalid.\n");
308 *status
=U_UNSUPPORTED_ERROR
;
313 // Prefight operation? Just return the size
315 int32_t totalSize
= headerSize
+ ds
->readUInt32(rbbiDH
->fLength
);
321 // Check that length passed in is consistent with length from RBBI data header.
324 length
-= headerSize
;
325 if ((uint32_t)length
< ds
->readUInt32(rbbiDH
->fLength
)) {
326 udata_printError(ds
, "ubrk_swap(): too few bytes (%d after ICU Data header) for break data.\n",
328 *status
=U_INDEX_OUTOFBOUNDS_ERROR
;
335 // Swap the Data. Do the data itself first, then the RBBI Data Header, because
336 // we need to reference the header to locate the data, and an
337 // inplace swap of the header leaves it unusable.
339 uint8_t *outBytes
= (uint8_t *)outData
+ headerSize
;
340 int32_t tableStartOffset
;
344 // If not swapping in place, zero out the output buffer before starting.
345 // Individual tables and other data items within are aligned to 8 byte boundaries
346 // when originally created. Any unused space between items needs to be zero.
348 if (inBytes
!= outBytes
) {
349 uprv_memset(outBytes
, 0, length
);
353 // Each state table begins with several 32 bit fields. Calculate the size
354 // in bytes of these.
356 RBBIStateTable
*stp
= NULL
;
357 int32_t topSize
= (char *)stp
->fTableData
- (char *)stp
;
359 // Forward state table.
360 tableStartOffset
= ds
->readUInt32(rbbiDH
->fFTable
);
361 tableLength
= ds
->readUInt32(rbbiDH
->fFTableLen
);
363 if (tableLength
> 0) {
364 ds
->swapArray32(ds
, inBytes
+tableStartOffset
, topSize
,
365 outBytes
+tableStartOffset
, status
);
366 ds
->swapArray16(ds
, inBytes
+tableStartOffset
+topSize
, tableLength
-topSize
,
367 outBytes
+tableStartOffset
+topSize
, status
);
370 // Reverse state table. Same layout as forward table, above.
371 tableStartOffset
= ds
->readUInt32(rbbiDH
->fRTable
);
372 tableLength
= ds
->readUInt32(rbbiDH
->fRTableLen
);
374 if (tableLength
> 0) {
375 ds
->swapArray32(ds
, inBytes
+tableStartOffset
, topSize
,
376 outBytes
+tableStartOffset
, status
);
377 ds
->swapArray16(ds
, inBytes
+tableStartOffset
+topSize
, tableLength
-topSize
,
378 outBytes
+tableStartOffset
+topSize
, status
);
381 // Safe Forward state table. Same layout as forward table, above.
382 tableStartOffset
= ds
->readUInt32(rbbiDH
->fSFTable
);
383 tableLength
= ds
->readUInt32(rbbiDH
->fSFTableLen
);
385 if (tableLength
> 0) {
386 ds
->swapArray32(ds
, inBytes
+tableStartOffset
, topSize
,
387 outBytes
+tableStartOffset
, status
);
388 ds
->swapArray16(ds
, inBytes
+tableStartOffset
+topSize
, tableLength
-topSize
,
389 outBytes
+tableStartOffset
+topSize
, status
);
392 // Safe Reverse state table. Same layout as forward table, above.
393 tableStartOffset
= ds
->readUInt32(rbbiDH
->fSRTable
);
394 tableLength
= ds
->readUInt32(rbbiDH
->fSRTableLen
);
396 if (tableLength
> 0) {
397 ds
->swapArray32(ds
, inBytes
+tableStartOffset
, topSize
,
398 outBytes
+tableStartOffset
, status
);
399 ds
->swapArray16(ds
, inBytes
+tableStartOffset
+topSize
, tableLength
-topSize
,
400 outBytes
+tableStartOffset
+topSize
, status
);
403 // Trie table for character categories
404 utrie_swap(ds
, inBytes
+ds
->readUInt32(rbbiDH
->fTrie
), ds
->readUInt32(rbbiDH
->fTrieLen
),
405 outBytes
+ds
->readUInt32(rbbiDH
->fTrie
), status
);
407 // Source Rules Text. It's UChar data
408 ds
->swapArray16(ds
, inBytes
+ds
->readUInt32(rbbiDH
->fRuleSource
), ds
->readUInt32(rbbiDH
->fRuleSourceLen
),
409 outBytes
+ds
->readUInt32(rbbiDH
->fRuleSource
), status
);
411 // Table of rule status values. It's all int_32 values
412 ds
->swapArray32(ds
, inBytes
+ds
->readUInt32(rbbiDH
->fStatusTable
), ds
->readUInt32(rbbiDH
->fStatusTableLen
),
413 outBytes
+ds
->readUInt32(rbbiDH
->fStatusTable
), status
);
415 // And, last, the header. All 32 bit values.
416 ds
->swapArray32(ds
, inBytes
, sizeof(RBBIDataHeader
), outBytes
, status
);
422 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */