]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/rbbidata.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / common / rbbidata.cpp
CommitLineData
b75a7d8f
A
1/*
2***************************************************************************
73c04bcf 3* Copyright (C) 1999-2005 International Business Machines Corporation *
b75a7d8f
A
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//-----------------------------------------------------------------------------------
29U_CDECL_BEGIN
30static int32_t U_CALLCONV
31getFoldingOffset(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}
39U_CDECL_END
40
41U_NAMESPACE_BEGIN
42
43//-----------------------------------------------------------------------------
44//
45// Constructors.
46//
47//-----------------------------------------------------------------------------
48RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, UErrorCode &status) {
49 init(data, status);
50}
51
52RBBIDataWrapper::RBBIDataWrapper(UDataMemory* udm, UErrorCode &status) {
53 const RBBIDataHeader *d = (const RBBIDataHeader *)
374ca955
A
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);
b75a7d8f
A
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//-----------------------------------------------------------------------------
67void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) {
68 if (U_FAILURE(status)) {
69 return;
70 }
71 fHeader = data;
73c04bcf
A
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;
b75a7d8f
A
77 return;
78 }
79
80 fUDataMem = NULL;
b75a7d8f 81 fReverseTable = NULL;
374ca955
A
82 fSafeFwdTable = NULL;
83 fSafeRevTable = NULL;
84 if (data->fFTableLen != 0) {
85 fForwardTable = (RBBIStateTable *)((char *)data + fHeader->fFTable);
86 }
b75a7d8f
A
87 if (data->fRTableLen != 0) {
88 fReverseTable = (RBBIStateTable *)((char *)data + fHeader->fRTable);
89 }
374ca955
A
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 }
b75a7d8f
A
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);
374ca955
A
110 U_ASSERT(data->fRuleSourceLen > 0);
111
112 fRuleStatusTable = (int32_t *)((char *)data + fHeader->fStatusTable);
113 fStatusMaxIdx = data->fStatusTableLen / sizeof(int32_t);
b75a7d8f
A
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//
374ca955 126// Destructor. Don't call this - use removeReference() instead.
b75a7d8f
A
127//
128//-----------------------------------------------------------------------------
129RBBIDataWrapper::~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//-----------------------------------------------------------------------------
150UBool 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
163int32_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//-----------------------------------------------------------------------------
176void RBBIDataWrapper::removeReference() {
177 if (umtx_atomic_dec(&fRefCount) == 0) {
178 delete this;
179 }
180}
181
182
183RBBIDataWrapper *RBBIDataWrapper::addReference() {
184 umtx_atomic_inc(&fRefCount);
185 return this;
186}
187
188
189
190//-----------------------------------------------------------------------------
191//
192// getRuleSourceString
193//
194//-----------------------------------------------------------------------------
374ca955 195const UnicodeString &RBBIDataWrapper::getRuleSourceString() const {
b75a7d8f
A
196 return fRuleString;
197}
198
199
200//-----------------------------------------------------------------------------
201//
202// print - debugging function to dump the runtime data tables.
203//
204//-----------------------------------------------------------------------------
b75a7d8f 205#ifdef RBBI_DEBUG
374ca955
A
206void RBBIDataWrapper::printTable(const char *heading, const RBBIStateTable *table) {
207 uint32_t c;
208 uint32_t s;
b75a7d8f 209
374ca955 210 RBBIDebugPrintf(" %s\n", heading);
b75a7d8f 211
374ca955 212 RBBIDebugPrintf("State | Acc LA TagIx");
b75a7d8f 213 for (c=0; c<fHeader->fCatCount; c++) {RBBIDebugPrintf("%3d ", c);}
374ca955
A
214 RBBIDebugPrintf("\n------|---------------"); for (c=0;c<fHeader->fCatCount; c++) {
215 RBBIDebugPrintf("----");
216 }
b75a7d8f
A
217 RBBIDebugPrintf("\n");
218
374ca955
A
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++) {
b75a7d8f 224 RBBIStateTableRow *row = (RBBIStateTableRow *)
374ca955
A
225 (table->fTableData + (table->fRowLen * s));
226 RBBIDebugPrintf("%4d | %3d %3d %3d ", s, row->fAccepting, row->fLookAhead, row->fTagIdx);
b75a7d8f
A
227 for (c=0; c<fHeader->fCatCount; c++) {
228 RBBIDebugPrintf("%3d ", row->fNextState[c]);
229 }
230 RBBIDebugPrintf("\n");
231 }
374ca955
A
232 RBBIDebugPrintf("\n");
233}
234#endif
235
236
237#ifdef RBBI_DEBUG
238void RBBIDataWrapper::printData() {
239 RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader);
73c04bcf
A
240 RBBIDebugPrintf(" Version = {%d %d %d %d}\n", fHeader->fFormatVersion[0], fHeader->fFormatVersion[1],
241 fHeader->fFormatVersion[2], fHeader->fFormatVersion[3]);
374ca955
A
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);
b75a7d8f
A
249
250 RBBIDebugPrintf("\nOrignal Rules source:\n");
374ca955 251 for (int32_t c=0; fRuleSource[c] != 0; c++) {
b75a7d8f 252 RBBIDebugPrintf("%c", fRuleSource[c]);
b75a7d8f
A
253 }
254 RBBIDebugPrintf("\n\n");
b75a7d8f 255}
374ca955 256#endif
b75a7d8f
A
257
258
259U_NAMESPACE_END
260
374ca955
A
261//-----------------------------------------------------------------------------
262//
263// ubrk_swap - byte swap and char encoding swap of RBBI data
264//
265//-----------------------------------------------------------------------------
266
267U_CAPI int32_t U_EXPORT2
268ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData,
269 UErrorCode *status) {
270
271 if (status == NULL || U_FAILURE(*status)) {
272 return 0;
273 }
73c04bcf
A
274 if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) {
275 *status=U_ILLEGAL_ARGUMENT_ERROR;
276 return 0;
277 }
374ca955
A
278
279 //
280 // Check that the data header is for for break data.
281 // (Header contents are defined in genbrk.cpp)
282 //
283 const UDataInfo *pInfo = (const UDataInfo *)((const char *)inData+4);
284 if(!( pInfo->dataFormat[0]==0x42 && /* dataFormat="Brk " */
285 pInfo->dataFormat[1]==0x72 &&
286 pInfo->dataFormat[2]==0x6b &&
287 pInfo->dataFormat[3]==0x20 &&
288 pInfo->formatVersion[0]==3 )) {
289 udata_printError(ds, "ubrk_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized\n",
290 pInfo->dataFormat[0], pInfo->dataFormat[1],
291 pInfo->dataFormat[2], pInfo->dataFormat[3],
292 pInfo->formatVersion[0]);
293 *status=U_UNSUPPORTED_ERROR;
294 return 0;
295 }
296
297 //
298 // Swap the data header. (This is the generic ICU Data Header, not the RBBI Specific
299 // RBBIDataHeader). This swap also conveniently gets us
300 // the size of the ICU d.h., which lets us locate the start
301 // of the RBBI specific data.
302 //
303 int32_t headerSize=udata_swapDataHeader(ds, inData, length, outData, status);
304
305
306 //
307 // Get the RRBI Data Header, and check that it appears to be OK.
308 //
73c04bcf
A
309 // Note: ICU 3.2 and earlier, RBBIDataHeader::fDataFormat was actually
310 // an int32_t with a value of 1. Starting with ICU 3.4,
311 // RBBI's fDataFormat matches the dataFormat field from the
312 // UDataInfo header, four int8_t bytes. The value is {3,1,0,0}
313 //
374ca955
A
314 const uint8_t *inBytes =(const uint8_t *)inData+headerSize;
315 RBBIDataHeader *rbbiDH = (RBBIDataHeader *)inBytes;
73c04bcf 316 UBool formatVersionOne = ds->readUInt32(*(int32_t *)rbbiDH->fFormatVersion) == 1;
374ca955 317 if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 ||
73c04bcf 318 !(formatVersionOne || rbbiDH->fFormatVersion[0] == 3) ||
374ca955
A
319 ds->readUInt32(rbbiDH->fLength) < sizeof(RBBIDataHeader))
320 {
321 udata_printError(ds, "ubrk_swap(): RBBI Data header is invalid.\n");
322 *status=U_UNSUPPORTED_ERROR;
323 return 0;
324 }
325
326 //
327 // Prefight operation? Just return the size
328 //
73c04bcf
A
329 int32_t breakDataLength = ds->readUInt32(rbbiDH->fLength);
330 int32_t totalSize = headerSize + breakDataLength;
374ca955
A
331 if (length < 0) {
332 return totalSize;
333 }
334
335 //
336 // Check that length passed in is consistent with length from RBBI data header.
337 //
73c04bcf
A
338 if (length < totalSize) {
339 udata_printError(ds, "ubrk_swap(): too few bytes (%d after ICU Data header) for break data.\n",
340 breakDataLength);
341 *status=U_INDEX_OUTOFBOUNDS_ERROR;
342 return 0;
374ca955 343 }
374ca955
A
344
345
346 //
347 // Swap the Data. Do the data itself first, then the RBBI Data Header, because
348 // we need to reference the header to locate the data, and an
349 // inplace swap of the header leaves it unusable.
350 //
73c04bcf
A
351 uint8_t *outBytes = (uint8_t *)outData + headerSize;
352 RBBIDataHeader *outputDH = (RBBIDataHeader *)outBytes;
353
374ca955
A
354 int32_t tableStartOffset;
355 int32_t tableLength;
356
357 //
358 // If not swapping in place, zero out the output buffer before starting.
359 // Individual tables and other data items within are aligned to 8 byte boundaries
360 // when originally created. Any unused space between items needs to be zero.
361 //
362 if (inBytes != outBytes) {
73c04bcf 363 uprv_memset(outBytes, 0, breakDataLength);
374ca955
A
364 }
365
366 //
367 // Each state table begins with several 32 bit fields. Calculate the size
368 // in bytes of these.
369 //
370 RBBIStateTable *stp = NULL;
371 int32_t topSize = (char *)stp->fTableData - (char *)stp;
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
73c04bcf
A
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
374ca955
A
439
440 return totalSize;
441}
442
443
b75a7d8f 444#endif /* #if !UCONFIG_NO_BREAK_ITERATION */