]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/rbbidata.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / common / rbbidata.cpp
index 360290bd58455dcb6714473e320d139a425abde0..09d47964e61274bee6904925dfbd5766ebee6e0b 100644 (file)
@@ -1,6 +1,6 @@
 /*
 ***************************************************************************
-*   Copyright (C) 1999-2005 International Business Machines Corporation   *
+*   Copyright (C) 1999-2010 International Business Machines Corporation   *
 *   and others. All rights reserved.                                      *
 ***************************************************************************
 */
@@ -49,6 +49,11 @@ RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, UErrorCode &status)
     init(data, status);
 }
 
+RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, enum EDontAdopt, UErrorCode &status) {
+    init(data, status);
+    fDontFreeData = TRUE;
+}
+
 RBBIDataWrapper::RBBIDataWrapper(UDataMemory* udm, UErrorCode &status) {
     const RBBIDataHeader *d = (const RBBIDataHeader *)
         // ((char *)&(udm->pHeader->info) + udm->pHeader->info.size);
@@ -69,14 +74,16 @@ void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) {
         return;
     }
     fHeader = data;
-    if (fHeader->fMagic != 0xb1a0 || 
-        !(fHeader->fFormatVersion[0] == 3 ||         // ICU 3.4 
-          *(int32_t *)fHeader->fFormatVersion == 1))  // ICU 3.2 and earlier.
+    if (fHeader->fMagic != 0xb1a0 || fHeader->fFormatVersion[0] != 3) 
     {
         status = U_INVALID_FORMAT_ERROR;
         return;
     }
+    // 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.
 
+    fDontFreeData = FALSE;
     fUDataMem     = NULL;
     fReverseTable = NULL;
     fSafeFwdTable = NULL;
@@ -130,7 +137,7 @@ RBBIDataWrapper::~RBBIDataWrapper() {
     U_ASSERT(fRefCount == 0);
     if (fUDataMem) {
         udata_close(fUDataMem);
-    } else {
+    } else if (!fDontFreeData) {
         uprv_free((void *)fHeader);
     }
 }
@@ -257,6 +264,7 @@ void  RBBIDataWrapper::printData() {
 
 
 U_NAMESPACE_END
+U_NAMESPACE_USE
 
 //-----------------------------------------------------------------------------
 //
@@ -313,9 +321,8 @@ ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outD
     //
     const uint8_t  *inBytes =(const uint8_t *)inData+headerSize;
     RBBIDataHeader *rbbiDH = (RBBIDataHeader *)inBytes;
-    UBool           formatVersionOne = ds->readUInt32(*(int32_t *)rbbiDH->fFormatVersion) == 1;
-    if (ds->readUInt32(rbbiDH->fMagic)   != 0xb1a0 ||
-        !(formatVersionOne || rbbiDH->fFormatVersion[0] == 3)   ||
+    if (ds->readUInt32(rbbiDH->fMagic) != 0xb1a0 || 
+        rbbiDH->fFormatVersion[0] != 3 ||
         ds->readUInt32(rbbiDH->fLength)  <  sizeof(RBBIDataHeader)) 
     {
         udata_printError(ds, "ubrk_swap(): RBBI Data header is invalid.\n");
@@ -367,8 +374,7 @@ ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outD
     // Each state table begins with several 32 bit fields.  Calculate the size
     //   in bytes of these.
     //
-    RBBIStateTable *stp = NULL;
-    int32_t         topSize = (char *)stp->fTableData - (char *)stp;
+    int32_t         topSize = offsetof(RBBIStateTable, fTableData);
 
     // Forward state table.  
     tableStartOffset = ds->readUInt32(rbbiDH->fFTable);
@@ -427,15 +433,11 @@ ubrk_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outD
                         outBytes+ds->readUInt32(rbbiDH->fStatusTable), status);
 
     // And, last, the header.
-    //   For the old version one format, the entire header consists of int32_t values.
-    //   For the newer formats, the fDataFormat field is an array of four bytes.
-    //   Swap the whole thing as int32_t, then, for the newer format, re-swap the one field.
+    //   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.
     //
     ds->swapArray32(ds, inBytes, sizeof(RBBIDataHeader), outBytes, status);
-    if (formatVersionOne == FALSE) {
-        ds->swapArray32(ds, outputDH->fFormatVersion, 4, outputDH->fFormatVersion, status);
-    }
-
+    ds->swapArray32(ds, outputDH->fFormatVersion, 4, outputDH->fFormatVersion, status);
 
     return totalSize;
 }