1 /*
2 ***************************************************************************
3 * Copyright (C) 1999-2007 International Business Machines Corporation *
4 * and others. All rights reserved. *
5 ***************************************************************************
6 */
7
8 #include "unicode/utypes.h"
9
10 #if !UCONFIG_NO_BREAK_ITERATION
11
12 #include "unicode/utypes.h"
13 #include "rbbidata.h"
14 #include "rbbirb.h"
15 #include "utrie.h"
16 #include "udatamem.h"
17 #include "cmemory.h"
18 #include "cstring.h"
19 #include "umutex.h"
20
21 #include "uassert.h"
22
23
24 //-----------------------------------------------------------------------------------
25 //
26 // Trie access folding function. Copied as-is from properties code in uchar.c
27 //
28 //-----------------------------------------------------------------------------------
29 U_CDECL_BEGIN
30 static int32_t U_CALLCONV
getFoldingOffset(uint32_t data)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 */
33 if(data&0x8000) {
34 return (int32_t)(data&0x7fff);
35 } else {
36 return 0;
37 }
38 }
39 U_CDECL_END
40
41 U_NAMESPACE_BEGIN
42
43 //-----------------------------------------------------------------------------
44 //
45 // Constructors.
46 //
47 //-----------------------------------------------------------------------------
RBBIDataWrapper(const RBBIDataHeader * data,UErrorCode & status)48 RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, UErrorCode &status) {
49 init(data, status);
50 }
51
RBBIDataWrapper(UDataMemory * udm,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);
57 init(d, status);
58 fUDataMem = udm;
59 }
60
61 //-----------------------------------------------------------------------------
62 //
63 // init(). Does most of the work of construction, shared between the
64 // constructors.
65 //
66 //-----------------------------------------------------------------------------
init(const RBBIDataHeader * data,UErrorCode & status)67 void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) {
68 if (U_FAILURE(status)) {
69 return;
70 }
71 fHeader = data;
72 if (fHeader->fMagic != 0xb1a0 ||
73 !(fHeader->fFormatVersion[0] == 3 || // ICU 3.4
74 *(int32_t *)fHeader->fFormatVersion == 1)) // ICU 3.2 and earlier.
75 {
76 status = U_INVALID_FORMAT_ERROR;
77 return;
78 }
79
80 fUDataMem = NULL;
81 fReverseTable = NULL;
82 fSafeFwdTable = NULL;
83 fSafeRevTable = NULL;
84 if (data->fFTableLen != 0) {
85 fForwardTable = (RBBIStateTable *)((char *)data + fHeader->fFTable);
86 }
87 if (data->fRTableLen != 0) {
88 fReverseTable = (RBBIStateTable *)((char *)data + fHeader->fRTable);
89 }
90 if (data->fSFTableLen != 0) {
91 fSafeFwdTable = (RBBIStateTable *)((char *)data + fHeader->fSFTable);
92 }
93 if (data->fSRTableLen != 0) {
94 fSafeRevTable = (RBBIStateTable *)((char *)data + fHeader->fSRTable);
95 }
96
97
98 utrie_unserialize(&fTrie,
99 (uint8_t *)data + fHeader->fTrie,
100 fHeader->fTrieLen,
101 &status);
102 if (U_FAILURE(status)) {
103 return;
104 }
105 fTrie.getFoldingOffset=getFoldingOffset;
106
107
108 fRuleSource = (UChar *)((char *)data + fHeader->fRuleSource);
109 fRuleString.setTo(TRUE, fRuleSource, -1);
110 U_ASSERT(data->fRuleSourceLen > 0);
111
112 fRuleStatusTable = (int32_t *)((char *)data + fHeader->fStatusTable);
113 fStatusMaxIdx = data->fStatusTableLen / sizeof(int32_t);
114
115 fRefCount = 1;
116
117 #ifdef RBBI_DEBUG
118 char *debugEnv = getenv("U_RBBIDEBUG");
119 if (debugEnv && uprv_strstr(debugEnv, "data")) {this->printData();}
120 #endif
121 }
122
123
124 //-----------------------------------------------------------------------------
125 //
126 // Destructor. Don't call this - use removeReference() instead.
127 //
128 //-----------------------------------------------------------------------------
~RBBIDataWrapper()129 RBBIDataWrapper::~RBBIDataWrapper() {
130 U_ASSERT(fRefCount == 0);
131 if (fUDataMem) {
132 udata_close(fUDataMem);
133 } else {
134 uprv_free((void *)fHeader);
135 }
136 }
137
138
139
140 //-----------------------------------------------------------------------------
141 //
142 // Operator == Consider two RBBIDataWrappers to be equal if they
143 // refer to the same underlying data. Although
144 // the data wrappers are normally shared between
145 // iterator instances, it's possible to independently
146 // open the same data twice, and get two instances, which
147 // should still be ==.
148 //
149 //-----------------------------------------------------------------------------
operator ==(const RBBIDataWrapper & other) const150 UBool RBBIDataWrapper::operator ==(const RBBIDataWrapper &other) const {
151 if (fHeader == other.fHeader) {
152 return TRUE;
153 }
154 if (fHeader->fLength != other.fHeader->fLength) {
155 return FALSE;
156 }
157 if (uprv_memcmp(fHeader, other.fHeader, fHeader->fLength) == 0) {
158 return TRUE;
159 }
160 return FALSE;
161 }
162
hashCode()163 int32_t RBBIDataWrapper::hashCode() {
164 return fHeader->fFTableLen;
165 }
166
167
168
169 //-----------------------------------------------------------------------------
170 //
171 // Reference Counting. A single RBBIDataWrapper object is shared among
172 // however many RulesBasedBreakIterator instances are
173 // referencing the same data.
174 //
175 //-----------------------------------------------------------------------------
removeReference()176 void RBBIDataWrapper::removeReference() {
177 if (umtx_atomic_dec(&fRefCount) == 0) {
178 delete this;
179 }
180 }
181
182
addReference()183 RBBIDataWrapper *RBBIDataWrapper::addReference() {
184 umtx_atomic_inc(&fRefCount);
185 return this;
186 }
187
188
189
190 //-----------------------------------------------------------------------------
191 //
192 // getRuleSourceString
193 //
194 //-----------------------------------------------------------------------------
getRuleSourceString() const195 const UnicodeString &RBBIDataWrapper::getRuleSourceString() const {
196 return fRuleString;
197 }
198
199
200 //-----------------------------------------------------------------------------
201 //
202 // print - debugging function to dump the runtime data tables.
203 //
204 //-----------------------------------------------------------------------------
205 #ifdef RBBI_DEBUG
printTable(const char * heading,const RBBIStateTable * table)206 void RBBIDataWrapper::printTable(const char *heading, const RBBIStateTable *table) {
207 uint32_t c;
208 uint32_t s;
209
210 RBBIDebugPrintf(" %s\n", heading);
211
212 RBBIDebugPrintf("State | Acc LA TagIx");
213 for (c=0; c<fHeader->fCatCount; c++) {RBBIDebugPrintf("%3d ", c);}
214 RBBIDebugPrintf("\n------|---------------"); for (c=0;c<fHeader->fCatCount; c++) {
215 RBBIDebugPrintf("----");
216 }
217 RBBIDebugPrintf("\n");
218
219 if (table == NULL) {
220 RBBIDebugPrintf(" N U L L T A B L E\n\n");
221 return;
222 }
223 for (s=0; s<table->fNumStates; s++) {
224 RBBIStateTableRow *row = (RBBIStateTableRow *)
225 (table->fTableData + (table->fRowLen * s));
226 RBBIDebugPrintf("%4d | %3d %3d %3d ", s, row->fAccepting, row->fLookAhead, row->fTagIdx);
227 for (c=0; c<fHeader->fCatCount; c++) {
228 RBBIDebugPrintf("%3d ", row->fNextState[c]);
229 }
230 RBBIDebugPrintf("\n");
231 }
232 RBBIDebugPrintf("\n");
233 }
234 #endif
235
236
237 #ifdef RBBI_DEBUG
printData()238 void RBBIDataWrapper::printData() {
239 RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader);
240 RBBIDebugPrintf(" Version = {%d %d %d %d}\n", fHeader->fFormatVersion[0], fHeader->fFormatVersion[1],
241 fHeader->fFormatVersion[2], fHeader->fFormatVersion[3]);
242 RBBIDebugPrintf(" total length of data = %d\n", fHeader->fLength);
243 RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader->fCatCount);
244
245 printTable("Forward State Transition Table", fForwardTable);
246 printTable("Reverse State Transition Table", fReverseTable);
247 printTable("Safe Forward State Transition Table", fSafeFwdTable);
248 printTable("Safe Reverse State Transition Table", fSafeRevTable);
249
250 RBBIDebugPrintf("\nOrignal Rules source:\n");
251 for (int32_t c=0; fRuleSource[c] != 0; c++) {
252 RBBIDebugPrintf("%c", fRuleSource[c]);
253 }
254 RBBIDebugPrintf("\n\n");
255 }
256 #endif
257
258
259 U_NAMESPACE_END
260 U_NAMESPACE_USE
261
262 //-----------------------------------------------------------------------------
263 //
264 // ubrk_swap - byte swap and char encoding swap of RBBI data
265 //
266 //-----------------------------------------------------------------------------
267
268 U_CAPI int32_t U_EXPORT2
ubrk_swap(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * status)269 ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData,
270 UErrorCode *status) {
271
272 if (status == NULL || U_FAILURE(*status)) {
273 return 0;
274 }
275 if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) {
276 *status=U_ILLEGAL_ARGUMENT_ERROR;
277 return 0;
278 }
279
280 //
281 // Check that the data header is for for break data.
282 // (Header contents are defined in genbrk.cpp)
283 //
284 const UDataInfo *pInfo = (const UDataInfo *)((const char *)inData+4);
285 if(!( pInfo->dataFormat[0]==0x42 && /* dataFormat="Brk " */
286 pInfo->dataFormat[1]==0x72 &&
287 pInfo->dataFormat[2]==0x6b &&
288 pInfo->dataFormat[3]==0x20 &&
289 pInfo->formatVersion[0]==3 )) {
290 udata_printError(ds, "ubrk_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized\n",
291 pInfo->dataFormat[0], pInfo->dataFormat[1],
292 pInfo->dataFormat[2], pInfo->dataFormat[3],
293 pInfo->formatVersion[0]);
294 *status=U_UNSUPPORTED_ERROR;
295 return 0;
296 }
297
298 //
299 // Swap the data header. (This is the generic ICU Data Header, not the RBBI Specific
300 // RBBIDataHeader). This swap also conveniently gets us
301 // the size of the ICU d.h., which lets us locate the start
302 // of the RBBI specific data.
303 //
304 int32_t headerSize=udata_swapDataHeader(ds, inData, length, outData, status);
305
306
307 //
308 // Get the RRBI Data Header, and check that it appears to be OK.
309 //
310 // Note: ICU 3.2 and earlier, RBBIDataHeader::fDataFormat was actually
311 // an int32_t with a value of 1. Starting with ICU 3.4,
312 // RBBI's fDataFormat matches the dataFormat field from the
313 // UDataInfo header, four int8_t bytes. The value is {3,1,0,0}
314 //
315 const uint8_t *inBytes =(const uint8_t *)inData+headerSize;
316 RBBIDataHeader *rbbiDH = (RBBIDataHeader *)inBytes;
317 UBool formatVersionOne = ds->readUInt32(*(int32_t *)rbbiDH->fFormatVersion) == 1;
318 if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 ||
319 !(formatVersionOne || rbbiDH->fFormatVersion[0] == 3) ||
320 ds->readUInt32(rbbiDH->fLength) < sizeof(RBBIDataHeader))
321 {
322 udata_printError(ds, "ubrk_swap(): RBBI Data header is invalid.\n");
323 *status=U_UNSUPPORTED_ERROR;
324 return 0;
325 }
326
327 //
328 // Prefight operation? Just return the size
329 //
330 int32_t breakDataLength = ds->readUInt32(rbbiDH->fLength);
331 int32_t totalSize = headerSize + breakDataLength;
332 if (length < 0) {
333 return totalSize;
334 }
335
336 //
337 // Check that length passed in is consistent with length from RBBI data header.
338 //
339 if (length < totalSize) {
340 udata_printError(ds, "ubrk_swap(): too few bytes (%d after ICU Data header) for break data.\n",
341 breakDataLength);
342 *status=U_INDEX_OUTOFBOUNDS_ERROR;
343 return 0;
344 }
345
346
347 //
348 // Swap the Data. Do the data itself first, then the RBBI Data Header, because
349 // we need to reference the header to locate the data, and an
350 // inplace swap of the header leaves it unusable.
351 //
352 uint8_t *outBytes = (uint8_t *)outData + headerSize;
353 RBBIDataHeader *outputDH = (RBBIDataHeader *)outBytes;
354
355 int32_t tableStartOffset;
356 int32_t tableLength;
357
358 //
359 // If not swapping in place, zero out the output buffer before starting.
360 // Individual tables and other data items within are aligned to 8 byte boundaries
361 // when originally created. Any unused space between items needs to be zero.
362 //
363 if (inBytes != outBytes) {
364 uprv_memset(outBytes, 0, breakDataLength);
365 }
366
367 //
368 // Each state table begins with several 32 bit fields. Calculate the size
369 // in bytes of these.
370 //
371 int32_t topSize = offsetof(RBBIStateTable, fTableData);
372
373 // Forward state table.
374 tableStartOffset = ds->readUInt32(rbbiDH->fFTable);
375 tableLength = ds->readUInt32(rbbiDH->fFTableLen);
376
377 if (tableLength > 0) {
378 ds->swapArray32(ds, inBytes+tableStartOffset, topSize,
379 outBytes+tableStartOffset, status);
380 ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize,
381 outBytes+tableStartOffset+topSize, status);
382 }
383
384 // Reverse state table. Same layout as forward table, above.
385 tableStartOffset = ds->readUInt32(rbbiDH->fRTable);
386 tableLength = ds->readUInt32(rbbiDH->fRTableLen);
387
388 if (tableLength > 0) {
389 ds->swapArray32(ds, inBytes+tableStartOffset, topSize,
390 outBytes+tableStartOffset, status);
391 ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize,
392 outBytes+tableStartOffset+topSize, status);
393 }
394
395 // Safe Forward state table. Same layout as forward table, above.
396 tableStartOffset = ds->readUInt32(rbbiDH->fSFTable);
397 tableLength = ds->readUInt32(rbbiDH->fSFTableLen);
398
399 if (tableLength > 0) {
400 ds->swapArray32(ds, inBytes+tableStartOffset, topSize,
401 outBytes+tableStartOffset, status);
402 ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize,
403 outBytes+tableStartOffset+topSize, status);
404 }
405
406 // Safe Reverse state table. Same layout as forward table, above.
407 tableStartOffset = ds->readUInt32(rbbiDH->fSRTable);
408 tableLength = ds->readUInt32(rbbiDH->fSRTableLen);
409
410 if (tableLength > 0) {
411 ds->swapArray32(ds, inBytes+tableStartOffset, topSize,
412 outBytes+tableStartOffset, status);
413 ds->swapArray16(ds, inBytes+tableStartOffset+topSize, tableLength-topSize,
414 outBytes+tableStartOffset+topSize, status);
415 }
416
417 // Trie table for character categories
418 utrie_swap(ds, inBytes+ds->readUInt32(rbbiDH->fTrie), ds->readUInt32(rbbiDH->fTrieLen),
419 outBytes+ds->readUInt32(rbbiDH->fTrie), status);
420
421 // Source Rules Text. It's UChar data
422 ds->swapArray16(ds, inBytes+ds->readUInt32(rbbiDH->fRuleSource), ds->readUInt32(rbbiDH->fRuleSourceLen),
423 outBytes+ds->readUInt32(rbbiDH->fRuleSource), status);
424
425 // Table of rule status values. It's all int_32 values
426 ds->swapArray32(ds, inBytes+ds->readUInt32(rbbiDH->fStatusTable), ds->readUInt32(rbbiDH->fStatusTableLen),
427 outBytes+ds->readUInt32(rbbiDH->fStatusTable), status);
428
429 // And, last, the header.
430 // For the old version one format, the entire header consists of int32_t values.
431 // For the newer formats, the fDataFormat field is an array of four bytes.
432 // Swap the whole thing as int32_t, then, for the newer format, re-swap the one field.
433 //
434 ds->swapArray32(ds, inBytes, sizeof(RBBIDataHeader), outBytes, status);
435 if (formatVersionOne == FALSE) {
436 ds->swapArray32(ds, outputDH->fFormatVersion, 4, outputDH->fFormatVersion, status);
437 }
438
439
440 return totalSize;
441 }
442
443
444 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
445