Coverage Report

Created: 2018-09-25 14:53

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