]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/toolutil/swapimpl.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / tools / toolutil / swapimpl.cpp
index 90c1eb41e059c4a9869c194a869122a3683bc7b9..926755a2aa8c365c17c33d14c7e3bef5b17b1367 100644 (file)
@@ -1,12 +1,14 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2005-2012, International Business Machines
+*   Copyright (C) 2005-2014, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 *   file name:  swapimpl.cpp
-*   encoding:   US-ASCII
+*   encoding:   UTF-8
 *   tab size:   8 (not used)
 *   indentation:4
 *
@@ -39,6 +41,7 @@
 #include "uarrsort.h"
 #include "ucmndata.h"
 #include "udataswp.h"
+#include "ulayout_props.h"
 
 /* swapping implementations in common */
 
@@ -54,8 +57,9 @@
 #include "sprpimpl.h"
 #include "propname.h"
 #include "rbbidata.h"
-#include "triedict.h"
+#include "utrie.h"
 #include "utrie2.h"
+#include "dictionarydata.h"
 
 /* swapping implementations in i18n */
 
@@ -67,8 +71,6 @@ U_NAMESPACE_USE
 
 /* definitions */
 
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-
 /* Unicode property (value) aliases data swapping --------------------------- */
 
 static int32_t U_CALLCONV
@@ -84,7 +86,7 @@ upname_swap(const UDataSwapper *ds,
     /* check data format and format version */
     const UDataInfo *pInfo=
         reinterpret_cast<const UDataInfo *>(
-            reinterpret_cast<const char *>(inData)+4);
+            static_cast<const char *>(inData)+4);
     if(!(
         pInfo->dataFormat[0]==0x70 &&   /* dataFormat="pnam" */
         pInfo->dataFormat[1]==0x6e &&
@@ -100,8 +102,8 @@ upname_swap(const UDataSwapper *ds,
         return 0;
     }
 
-    const uint8_t *inBytes=reinterpret_cast<const uint8_t *>(inData)+headerSize;
-    uint8_t *outBytes=reinterpret_cast<uint8_t *>(outData)+headerSize;
+    const uint8_t *inBytes=static_cast<const uint8_t *>(inData)+headerSize;
+    uint8_t *outBytes=static_cast<uint8_t *>(outData)+headerSize;
 
     if(length>=0) {
         length-=headerSize;
@@ -232,7 +234,7 @@ uprops_swap(const UDataSwapper *ds,
 
         /* copy everything for inaccessible data (padding) */
         if(inData32!=outData32) {
-            uprv_memcpy(outData32, inData32, 4*dataTop);
+            uprv_memcpy(outData32, inData32, 4*(size_t)dataTop);
         }
 
         /* swap the indexes[16] */
@@ -242,7 +244,7 @@ uprops_swap(const UDataSwapper *ds,
          * swap the main properties UTrie
          * PT serialized properties trie, see utrie.h (byte size: 4*(i0-16))
          */
-        utrie2_swapAnyVersion(ds,
+        utrie_swapAnyVersion(ds,
             inData32+UPROPS_INDEX_COUNT,
             4*(dataIndexes[UPROPS_PROPS32_INDEX]-UPROPS_INDEX_COUNT),
             outData32+UPROPS_INDEX_COUNT,
@@ -273,7 +275,7 @@ uprops_swap(const UDataSwapper *ds,
          * swap the additional UTrie
          * i3 additionalTrieIndex; -- 32-bit unit index to the additional trie for more properties
          */
-        utrie2_swapAnyVersion(ds,
+        utrie_swapAnyVersion(ds,
             inData32+dataIndexes[UPROPS_ADDITIONAL_TRIE_INDEX],
             4*(dataIndexes[UPROPS_ADDITIONAL_VECTORS_INDEX]-dataIndexes[UPROPS_ADDITIONAL_TRIE_INDEX]),
             outData32+dataIndexes[UPROPS_ADDITIONAL_TRIE_INDEX],
@@ -335,7 +337,7 @@ ucase_swap(const UDataSwapper *ds,
         ((pInfo->formatVersion[0]==1 &&
           pInfo->formatVersion[2]==UTRIE_SHIFT &&
           pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT) ||
-         pInfo->formatVersion[0]==2 || pInfo->formatVersion[0]==3)
+         (2<=pInfo->formatVersion[0] && pInfo->formatVersion[0]<=4))
     )) {
         udata_printError(ds, "ucase_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as case mapping data\n",
                          pInfo->dataFormat[0], pInfo->dataFormat[1],
@@ -390,7 +392,7 @@ ucase_swap(const UDataSwapper *ds,
 
         /* swap the UTrie */
         count=indexes[UCASE_IX_TRIE_SIZE];
-        utrie2_swapAnyVersion(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
+        utrie_swapAnyVersion(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
         offset+=count;
 
         /* swap the uint16_t exceptions[] and unfold[] */
@@ -492,7 +494,7 @@ ubidi_swap(const UDataSwapper *ds,
 
         /* swap the UTrie */
         count=indexes[UBIDI_IX_TRIE_SIZE];
-        utrie2_swapAnyVersion(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
+        utrie_swapAnyVersion(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
         offset+=count;
 
         /* swap the uint32_t mirrors[] */
@@ -500,9 +502,11 @@ ubidi_swap(const UDataSwapper *ds,
         ds->swapArray32(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
         offset+=count;
 
-        /* just skip the uint8_t jgArray[] */
+        /* just skip the uint8_t jgArray[] and jgArray2[] */
         count=indexes[UBIDI_IX_JG_LIMIT]-indexes[UBIDI_IX_JG_START];
         offset+=count;
+        count=indexes[UBIDI_IX_JG_LIMIT2]-indexes[UBIDI_IX_JG_START2];
+        offset+=count;
 
         U_ASSERT(offset==size);
     }
@@ -637,6 +641,106 @@ unorm_swap(const UDataSwapper *ds,
 
 #endif
 
+// Unicode text layout properties data swapping --------------------------------
+
+static int32_t U_CALLCONV
+ulayout_swap(const UDataSwapper *ds,
+             const void *inData, int32_t length, void *outData,
+             UErrorCode *pErrorCode) {
+    // udata_swapDataHeader checks the arguments.
+    int32_t headerSize = udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
+    if (pErrorCode == nullptr || U_FAILURE(*pErrorCode)) {
+        return 0;
+    }
+
+    // Check data format and format version.
+    const UDataInfo *pInfo = (const UDataInfo *)((const char *)inData + 4);
+    if (!(
+            pInfo->dataFormat[0] == ULAYOUT_FMT_0 &&    // dataFormat="Layo"
+            pInfo->dataFormat[1] == ULAYOUT_FMT_1 &&
+            pInfo->dataFormat[2] == ULAYOUT_FMT_2 &&
+            pInfo->dataFormat[3] == ULAYOUT_FMT_3 &&
+            pInfo->formatVersion[0] == 1)) {
+        udata_printError(ds,
+            "ulayout_swap(): data format %02x.%02x.%02x.%02x (format version %02x) "
+            "is not recognized as text layout properties data\n",
+            pInfo->dataFormat[0], pInfo->dataFormat[1],
+            pInfo->dataFormat[2], pInfo->dataFormat[3],
+            pInfo->formatVersion[0]);
+        *pErrorCode = U_UNSUPPORTED_ERROR;
+        return 0;
+    }
+
+    const uint8_t *inBytes = (const uint8_t *)inData + headerSize;
+    uint8_t *outBytes = (uint8_t *)outData + headerSize;
+
+    const int32_t *inIndexes = (const int32_t *)inBytes;
+
+    if (length >= 0) {
+        length -= headerSize;
+        if (length < 12 * 4) {
+            udata_printError(ds,
+                "ulayout_swap(): too few bytes (%d after header) for text layout properties data\n",
+                length);
+            *pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
+            return 0;
+        }
+    }
+
+    int32_t indexesLength = udata_readInt32(ds, inIndexes[ULAYOUT_IX_INDEXES_LENGTH]);
+    if (indexesLength < 12) {
+        udata_printError(ds,
+            "ulayout_swap(): too few indexes (%d) for text layout properties data\n",
+            indexesLength);
+        *pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
+        return 0;
+    }
+
+    // Read the data offsets before swapping anything.
+    int32_t indexes[ULAYOUT_IX_TRIES_TOP + 1];
+    for (int32_t i = ULAYOUT_IX_INPC_TRIE_TOP; i <= ULAYOUT_IX_TRIES_TOP; ++i) {
+        indexes[i] = udata_readInt32(ds, inIndexes[i]);
+    }
+    int32_t size = indexes[ULAYOUT_IX_TRIES_TOP];
+
+    if (length >= 0) {
+        if (length < size) {
+            udata_printError(ds,
+                "ulayout_swap(): too few bytes (%d after header) "
+                "for all of text layout properties data\n",
+                length);
+            *pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
+            return 0;
+        }
+
+        // Copy the data for inaccessible bytes.
+        if (inBytes != outBytes) {
+            uprv_memcpy(outBytes, inBytes, size);
+        }
+
+        // Swap the int32_t indexes[].
+        int32_t offset = 0;
+        int32_t count = indexesLength * 4;
+        ds->swapArray32(ds, inBytes, count, outBytes, pErrorCode);
+        offset += count;
+
+        // Swap each trie.
+        for (int32_t i = ULAYOUT_IX_INPC_TRIE_TOP; i <= ULAYOUT_IX_TRIES_TOP; ++i) {
+            int32_t top = indexes[i];
+            count = top - offset;
+            U_ASSERT(count >= 0);
+            if (count >= 16) {
+                utrie_swapAnyVersion(ds, inBytes + offset, count, outBytes + offset, pErrorCode);
+            }
+            offset = top;
+        }
+
+        U_ASSERT(offset == size);
+    }
+
+    return headerSize + size;
+}
+
 /* Swap 'Test' data from gentest */
 static int32_t U_CALLCONV
 test_swap(const UDataSwapper *ds,
@@ -653,7 +757,7 @@ test_swap(const UDataSwapper *ds,
     /* udata_swapDataHeader checks the arguments */
     headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
     if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
-        udata_printError(ds, "test_swap(): data header swap failed %s\n", u_errorName(*pErrorCode));
+        udata_printError(ds, "test_swap(): data header swap failed %s\n", pErrorCode != NULL ? u_errorName(*pErrorCode) : "pErrorCode is NULL");
         return 0;
     }
 
@@ -728,13 +832,17 @@ static const struct {
     { { 0x4e, 0x6f, 0x72, 0x6d }, unorm_swap },         /* dataFormat="Norm" */
     { { 0x4e, 0x72, 0x6d, 0x32 }, unorm2_swap },        /* dataFormat="Nrm2" */
 #endif
+
+    { { ULAYOUT_FMT_0, ULAYOUT_FMT_1, ULAYOUT_FMT_2, ULAYOUT_FMT_3 },
+                                  ulayout_swap },       // dataFormat="Layo"
+
 #if !UCONFIG_NO_COLLATION
     { { 0x55, 0x43, 0x6f, 0x6c }, ucol_swap },          /* dataFormat="UCol" */
     { { 0x49, 0x6e, 0x76, 0x43 }, ucol_swapInverseUCA },/* dataFormat="InvC" */
 #endif
 #if !UCONFIG_NO_BREAK_ITERATION
     { { 0x42, 0x72, 0x6b, 0x20 }, ubrk_swap },          /* dataFormat="Brk " */
-    { { 0x54, 0x72, 0x44, 0x63 }, triedict_swap },      /* dataFormat="TrDc " */
+    { { 0x44, 0x69, 0x63, 0x74 }, udict_swap },         /* dataFormat="Dict" */
 #endif
     { { 0x70, 0x6e, 0x61, 0x6d }, upname_swap },        /* dataFormat="pnam" */
     { { 0x75, 0x6e, 0x61, 0x6d }, uchar_swapNames },    /* dataFormat="unam" */
@@ -790,7 +898,7 @@ udata_swap(const UDataSwapper *ds,
     }
 
     /* dispatch to the swap function for the dataFormat */
-    for(i=0; i<LENGTHOF(swapFns); ++i) {
+    for(i=0; i<UPRV_LENGTHOF(swapFns); ++i) {
         if(0==memcmp(swapFns[i].dataFormat, pInfo->dataFormat, 4)) {
             swappedLength=swapFns[i].swapFn(ds, inData, length, outData, pErrorCode);