]> granicus.if.org Git - icu/commitdiff
ICU-11032 Incorporate review comments, use isAcceptable call-back with udata_open
authorAndy Heninger <andy.heninger@gmail.com>
Thu, 11 Sep 2014 18:28:05 +0000 (18:28 +0000)
committerAndy Heninger <andy.heninger@gmail.com>
Thu, 11 Sep 2014 18:28:05 +0000 (18:28 +0000)
X-SVN-Rev: 36466

icu4c/source/i18n/unicode/uspoof.h
icu4c/source/i18n/uspoof_impl.cpp

index c766f3fcabf3093a47c5b82b8e54bc3718572f7f..83dea3702a2a3b1103121fb7daf83a538f9d34ef 100644 (file)
@@ -329,7 +329,7 @@ uspoof_open(UErrorCode *status);
 
 
 /**
- * Open a Spoof checker from its serialized from, stored in 32-bit-aligned memory.
+ * Open a Spoof checker from its serialized form, stored in 32-bit-aligned memory.
  * Inverse of uspoof_serialize().
  * The memory containing the serialized data must remain valid and unchanged
  * as long as the spoof checker, or any cloned copies of the spoof checker,
@@ -355,9 +355,9 @@ uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLeng
 
 /**
   * Open a Spoof Checker from the source form of the spoof data.
-  * The Three inputs correspond to the Unicode data files confusables.txt
-  * confusablesWholeScript.txt and xidmdifications.txt as described in
-  * Unicode UAX #39.  The syntax of the source data is as described in UAX #39 for
+  * The two inputs correspond to the Unicode data files confusables.txt
+  * and confusablesWholeScript.txt as described in Unicode UAX #39.
+  * The syntax of the source data is as described in UAX #39 for
   * these files, and the content of these files is acceptable input.
   *
   * The character encoding of the (char *) input text is UTF-8.
index b53af83c4de5713d1e51d6d5863b7053d36908ab..ffb986cb6241ba2b4bd1605f2d6fdf50e9a0bbb3 100644 (file)
@@ -475,6 +475,30 @@ UBool SpoofData::validateDataVersion(const SpoofDataHeader *rawData, UErrorCode
     return TRUE;
 }
 
+static UBool U_CALLCONV
+spoofDataIsAcceptable(void *context,
+                        const char * /* type */, const char * /*name*/,
+                        const UDataInfo *pInfo) {
+    if(
+        pInfo->size >= 20 &&
+        pInfo->isBigEndian == U_IS_BIG_ENDIAN &&
+        pInfo->charsetFamily == U_CHARSET_FAMILY &&
+        pInfo->dataFormat[0] == 0x43 &&  // dataFormat="Cfu "
+        pInfo->dataFormat[1] == 0x66 &&
+        pInfo->dataFormat[2] == 0x75 &&
+        pInfo->dataFormat[3] == 0x20 &&
+        pInfo->formatVersion[0] == 1
+    ) {
+        UVersionInfo *version = static_cast<UVersionInfo *>(context);
+        if(version != NULL) {
+            uprv_memcpy(version, pInfo->dataVersion, 4);
+        }
+        return TRUE;
+    } else {
+        return FALSE;
+    }
+}
+
 //
 //  SpoofData::getDefault() - return a wrapper around the spoof data that is
 //                           baked into the default ICU data.
@@ -482,7 +506,10 @@ UBool SpoofData::validateDataVersion(const SpoofDataHeader *rawData, UErrorCode
 SpoofData *SpoofData::getDefault(UErrorCode &status) {
     // TODO:  Cache it.  Lazy create, keep until cleanup.
 
-    UDataMemory *udm = udata_open(NULL, "cfu", "confusables", &status);
+    UDataMemory *udm = udata_openChoice(NULL, "cfu", "confusables",
+                                        spoofDataIsAcceptable, 
+                                        NULL,       // context, would receive dataVersion if supplied.
+                                        &status);
     if (U_FAILURE(status)) {
         return NULL;
     }
@@ -497,30 +524,17 @@ SpoofData *SpoofData::getDefault(UErrorCode &status) {
     return This;
 }
 
-
 SpoofData::SpoofData(UDataMemory *udm, UErrorCode &status)
 {
     reset();
     if (U_FAILURE(status)) {
         return;
     }
+    fUDM = udm;
     const DataHeader *dh = udm->pHeader;
     int32_t headerSize = dh->dataHeader.headerSize;
-    if (  !(headerSize >= 20 &&
-            dh->info.isBigEndian == U_IS_BIG_ENDIAN &&
-            dh->info.charsetFamily == U_CHARSET_FAMILY &&
-            dh->info.dataFormat[0] == 0x43 &&  // dataFormat="Cfu "
-            dh->info.dataFormat[1] == 0x66 &&
-            dh->info.dataFormat[2] == 0x75 &&
-            dh->info.dataFormat[3] == 0x20)
-        ) {
-        status = U_INVALID_FORMAT_ERROR;
-        return;
-    }
-
     fRawData = reinterpret_cast<SpoofDataHeader *>
-                   ((char *)(udm->pHeader) + headerSize);
-    fUDM = udm;
+                   ((char *)dh + headerSize);
     validateDataVersion(fRawData, status);
     initPtrs(status);
 }