]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/toolutil/package.cpp
ICU-57166.0.1.tar.gz
[apple/icu.git] / icuSources / tools / toolutil / package.cpp
index c5db3017338662545586d746d65f70fd933450f4..47e766cabe5d53c86336dfddbcb641a2cf185798 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 1999-2007, International Business Machines
+*   Copyright (C) 1999-2015, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 #include "swapimpl.h"
 #include "toolutil.h"
 #include "package.h"
+#include "cmemory.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-// general definitions ----------------------------------------------------- ***
 
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
+static const int32_t kItemsChunk = 256; /* How much to increase the filesarray by each time */
+
+// general definitions ----------------------------------------------------- ***
 
 /* UDataInfo cf. udata.h */
 static const UDataInfo dataInfo={
@@ -302,7 +304,6 @@ static uint8_t *
 readFile(const char *path, const char *name, int32_t &length, char &type) {
     char filename[1024];
     FILE *file;
-    uint8_t *data;
     UErrorCode errorCode;
     int32_t fileLength, typeEnum;
 
@@ -325,38 +326,42 @@ readFile(const char *path, const char *name, int32_t &length, char &type) {
 
     /* allocate the buffer, pad to multiple of 16 */
     length=(fileLength+0xf)&~0xf;
-    data=(uint8_t *)malloc(length);
-    if(data==NULL) {
+    icu::LocalMemory<uint8_t> data((uint8_t *)uprv_malloc(length));
+    if(data.isNull()) {
         fclose(file);
+        fprintf(stderr, "icupkg: malloc error allocating %d bytes.\n", (int)length);
         exit(U_MEMORY_ALLOCATION_ERROR);
     }
 
     /* read the file */
-    if(fileLength!=(int32_t)fread(data, 1, fileLength, file)) {
+    if(fileLength!=(int32_t)fread(data.getAlias(), 1, fileLength, file)) {
         fprintf(stderr, "icupkg: error reading \"%s\"\n", filename);
         fclose(file);
-        free(data);
         exit(U_FILE_ACCESS_ERROR);
     }
 
     /* pad the file to a multiple of 16 using the usual padding byte */
     if(fileLength<length) {
-        memset(data+fileLength, 0xaa, length-fileLength);
+        memset(data.getAlias()+fileLength, 0xaa, length-fileLength);
     }
 
     fclose(file);
 
     // minimum check for ICU-format data
     errorCode=U_ZERO_ERROR;
-    typeEnum=getTypeEnumForInputData(data, length, &errorCode);
+    typeEnum=getTypeEnumForInputData(data.getAlias(), length, &errorCode);
     if(typeEnum<0 || U_FAILURE(errorCode)) {
         fprintf(stderr, "icupkg: not an ICU data file: \"%s\"\n", filename);
-        free(data);
+#if !UCONFIG_NO_LEGACY_CONVERSION
         exit(U_INVALID_FORMAT_ERROR);
+#else
+        fprintf(stderr, "U_INVALID_FORMAT_ERROR occurred but UCONFIG_NO_LEGACY_CONVERSION is on so this is expected.\n");
+        exit(0);
+#endif
     }
     type=makeTypeLetter(typeEnum);
 
-    return data;
+    return data.orphan();
 }
 
 // .dat package file representation ---------------------------------------- ***
@@ -374,14 +379,19 @@ U_CDECL_END
 
 U_NAMESPACE_BEGIN
 
-Package::Package() {
+Package::Package() 
+        : doAutoPrefix(FALSE), prefixEndsWithType(FALSE) {
     inPkgName[0]=0;
+    pkgPrefix[0]=0;
     inData=NULL;
     inLength=0;
     inCharset=U_CHARSET_FAMILY;
     inIsBigEndian=U_IS_BIG_ENDIAN;
 
     itemCount=0;
+    itemMax=0;
+    items=NULL;
+
     inStringTop=outStringTop=0;
 
     matchMode=0;
@@ -406,15 +416,26 @@ Package::Package() {
 }
 
 Package::~Package() {
-    int32_t index;
+    int32_t idx;
 
-    free(inData);
+    uprv_free(inData);
 
-    for(index=0; index<itemCount; ++index) {
-        if(items[index].isDataOwned) {
-            free(items[index].data);
+    for(idx=0; idx<itemCount; ++idx) {
+        if(items[idx].isDataOwned) {
+            uprv_free(items[idx].data);
         }
     }
+
+    uprv_free((void*)items);
+}
+
+void
+Package::setPrefix(const char *p) {
+    if(strlen(p)>=sizeof(pkgPrefix)) {
+        fprintf(stderr, "icupkg: --toc_prefix %s too long\n", p);
+        exit(U_ILLEGAL_ARGUMENT_ERROR);
+    }
+    strcpy(pkgPrefix, p);
 }
 
 void
@@ -490,6 +511,7 @@ Package::readPackage(const char *filename) {
         offset=0x7fffffff;
     } else {
         itemCount=udata_readInt32(ds, *(const int32_t *)inBytes);
+        setItemCapacity(itemCount); /* resize so there's space */
         if(itemCount==0) {
             offset=4;
         } else if(length<(4+8*itemCount)) {
@@ -507,18 +529,22 @@ Package::readPackage(const char *filename) {
     }
     /* do not modify the package length variable until the last item's length is set */
 
-    if(itemCount>0) {
+    if(itemCount<=0) {
+        if(doAutoPrefix) {
+            fprintf(stderr, "icupkg: --auto_toc_prefix[_with_type] but the input package is empty\n");
+            exit(U_INVALID_FORMAT_ERROR);
+        }
+    } else {
         char prefix[MAX_PKG_NAME_LENGTH+4];
         char *s, *inItemStrings;
-        int32_t inPkgNameLength, prefixLength, stringsOffset;
 
-        if(itemCount>MAX_FILE_COUNT) {
-            fprintf(stderr, "icupkg: too many items, maximum is %d\n", MAX_FILE_COUNT);
+        if(itemCount>itemMax) {
+            fprintf(stderr, "icupkg: too many items, maximum is %d\n", itemMax);
             exit(U_BUFFER_OVERFLOW_ERROR);
         }
 
         /* swap the item name strings */
-        stringsOffset=4+8*itemCount;
+        int32_t stringsOffset=4+8*itemCount;
         itemLength=(int32_t)(ds->readUInt32(inEntries[0].dataOffset))-stringsOffset;
 
         // don't include padding bytes at the end of the item names
@@ -542,10 +568,6 @@ Package::readPackage(const char *filename) {
         // reset the Item entries
         memset(items, 0, itemCount*sizeof(Item));
 
-        inPkgNameLength=strlen(inPkgName);
-        memcpy(prefix, inPkgName, inPkgNameLength);
-        prefixLength=inPkgNameLength;
-
         /*
          * Get the common prefix of the items.
          * New-style ICU .dat packages use tree separators ('/') between package names,
@@ -554,18 +576,54 @@ Package::readPackage(const char *filename) {
          * use an underscore ('_') between package and item names.
          */
         offset=(int32_t)ds->readUInt32(inEntries[0].nameOffset)-stringsOffset;
-        s=inItemStrings+offset;
-        if( (int32_t)strlen(s)>=(inPkgNameLength+2) &&
-            0==memcmp(s, inPkgName, inPkgNameLength) &&
-            s[inPkgNameLength]=='_'
-        ) {
-            // old-style .dat package
-            prefix[prefixLength++]='_';
+        s=inItemStrings+offset;  // name of the first entry
+        int32_t prefixLength;
+        if(doAutoPrefix) {
+            // Use the first entry's prefix. Must be a new-style package.
+            const char *prefixLimit=strchr(s, U_TREE_ENTRY_SEP_CHAR);
+            if(prefixLimit==NULL) {
+                fprintf(stderr,
+                        "icupkg: --auto_toc_prefix[_with_type] but "
+                        "the first entry \"%s\" does not contain a '%c'\n",
+                        s, U_TREE_ENTRY_SEP_CHAR);
+                exit(U_INVALID_FORMAT_ERROR);
+            }
+            prefixLength=(int32_t)(prefixLimit-s);
+            if(prefixLength==0 || prefixLength>=UPRV_LENGTHOF(pkgPrefix)) {
+                fprintf(stderr,
+                        "icupkg: --auto_toc_prefix[_with_type] but "
+                        "the prefix of the first entry \"%s\" is empty or too long\n",
+                        s);
+                exit(U_INVALID_FORMAT_ERROR);
+            }
+            if(prefixEndsWithType && s[prefixLength-1]!=type) {
+                fprintf(stderr,
+                        "icupkg: --auto_toc_prefix_with_type but "
+                        "the prefix of the first entry \"%s\" does not end with '%c'\n",
+                        s, type);
+                exit(U_INVALID_FORMAT_ERROR);
+            }
+            memcpy(pkgPrefix, s, prefixLength);
+            pkgPrefix[prefixLength]=0;
+            memcpy(prefix, s, ++prefixLength);  // include the /
         } else {
-            // new-style .dat package
-            prefix[prefixLength++]=U_TREE_ENTRY_SEP_CHAR;
-            // if it turns out to not contain U_TREE_ENTRY_SEP_CHAR
-            // then the test in the loop below will fail
+            // Use the package basename as prefix.
+            int32_t inPkgNameLength=strlen(inPkgName);
+            memcpy(prefix, inPkgName, inPkgNameLength);
+            prefixLength=inPkgNameLength;
+
+            if( (int32_t)strlen(s)>=(inPkgNameLength+2) &&
+                0==memcmp(s, inPkgName, inPkgNameLength) &&
+                s[inPkgNameLength]=='_'
+            ) {
+                // old-style .dat package
+                prefix[prefixLength++]='_';
+            } else {
+                // new-style .dat package
+                prefix[prefixLength++]=U_TREE_ENTRY_SEP_CHAR;
+                // if it turns out to not contain U_TREE_ENTRY_SEP_CHAR
+                // then the test in the loop below will fail
+            }
         }
         prefix[prefixLength]=0;
 
@@ -578,7 +636,7 @@ Package::readPackage(const char *filename) {
             if(0!=strncmp(s, prefix, prefixLength) || s[prefixLength]==0) {
                 fprintf(stderr, "icupkg: input .dat item name \"%s\" does not start with \"%s\"\n",
                         s, prefix);
-                exit(U_UNSUPPORTED_ERROR);
+                exit(U_INVALID_FORMAT_ERROR);
             }
             items[i].name=s+prefixLength;
 
@@ -708,8 +766,17 @@ Package::writePackage(const char *filename, char outType, const char *comment) {
 
     // prepare and swap the package name with a tree separator
     // for prepending to item names
-    strcat(prefix, U_TREE_ENTRY_SEP_STRING);
-    prefixLength=(int32_t)strlen(prefix);
+    if(pkgPrefix[0]==0) {
+        prefixLength=(int32_t)strlen(prefix);
+    } else {
+        prefixLength=(int32_t)strlen(pkgPrefix);
+        memcpy(prefix, pkgPrefix, prefixLength);
+        if(prefixEndsWithType) {
+            prefix[prefixLength-1]=outType;
+        }
+    }
+    prefix[prefixLength++]=U_TREE_ENTRY_SEP_CHAR;
+    prefix[prefixLength]=0;
     if(dsLocalToOut!=NULL) {
         dsLocalToOut->swapInvChars(dsLocalToOut, prefix, prefixLength, prefix, &errorCode);
         if(U_FAILURE(errorCode)) {
@@ -902,15 +969,15 @@ Package::findItems(const char *pattern) {
 int32_t
 Package::findNextItem() {
     const char *name, *middle, *treeSep;
-    int32_t index, nameLength, middleLength;
+    int32_t idx, nameLength, middleLength;
 
     if(findNextIndex<0) {
         return -1;
     }
 
     while(findNextIndex<itemCount) {
-        index=findNextIndex++;
-        name=items[index].name;
+        idx=findNextIndex++;
+        name=items[idx].name;
         nameLength=(int32_t)strlen(name);
         if(nameLength<(findPrefixLength+findSuffixLength)) {
             // item name too short for prefix & suffix
@@ -937,7 +1004,7 @@ Package::findNextItem() {
         }
 
         // found a matching item
-        return index;
+        return idx;
     }
 
     // no more items
@@ -957,43 +1024,40 @@ Package::addItem(const char *name) {
 
 void
 Package::addItem(const char *name, uint8_t *data, int32_t length, UBool isDataOwned, char type) {
-    int32_t index;
+    int32_t idx;
 
-    index=findItem(name);
-    if(index<0) {
+    idx=findItem(name);
+    if(idx<0) {
         // new item, make space at the insertion point
-        if(itemCount>=MAX_FILE_COUNT) {
-            fprintf(stderr, "icupkg: too many items, maximum is %d\n", MAX_FILE_COUNT);
-            exit(U_BUFFER_OVERFLOW_ERROR);
-        }
+        ensureItemCapacity();
         // move the following items down
-        index=~index;
-        if(index<itemCount) {
-            memmove(items+index+1, items+index, (itemCount-index)*sizeof(Item));
+        idx=~idx;
+        if(idx<itemCount) {
+            memmove(items+idx+1, items+idx, (itemCount-idx)*sizeof(Item));
         }
         ++itemCount;
 
         // reset this Item entry
-        memset(items+index, 0, sizeof(Item));
+        memset(items+idx, 0, sizeof(Item));
 
         // copy the item's name
-        items[index].name=allocString(TRUE, strlen(name));
-        strcpy(items[index].name, name);
-        pathToTree(items[index].name);
+        items[idx].name=allocString(TRUE, strlen(name));
+        strcpy(items[idx].name, name);
+        pathToTree(items[idx].name);
     } else {
         // same-name item found, replace it
-        if(items[index].isDataOwned) {
-            free(items[index].data);
+        if(items[idx].isDataOwned) {
+            uprv_free(items[idx].data);
         }
 
         // keep the item's name since it is the same
     }
 
     // set the item's data
-    items[index].data=data;
-    items[index].length=length;
-    items[index].isDataOwned=isDataOwned;
-    items[index].type=type;
+    items[idx].data=data;
+    items[idx].length=length;
+    items[idx].isDataOwned=isDataOwned;
+    items[idx].type=type;
 }
 
 void
@@ -1018,20 +1082,20 @@ Package::addItems(const Package &listPkg) {
 }
 
 void
-Package::removeItem(int32_t index) {
-    if(index>=0) {
+Package::removeItem(int32_t idx) {
+    if(idx>=0) {
         // remove the item
-        if(items[index].isDataOwned) {
-            free(items[index].data);
+        if(items[idx].isDataOwned) {
+            uprv_free(items[idx].data);
         }
 
         // move the following items up
-        if((index+1)<itemCount) {
-            memmove(items+index, items+index+1, (itemCount-(index+1))*sizeof(Item));
+        if((idx+1)<itemCount) {
+            memmove(items+idx, items+idx+1, (itemCount-(idx+1))*sizeof(Item));
         }
         --itemCount;
 
-        if(index<=findNextIndex) {
+        if(idx<=findNextIndex) {
             --findNextIndex;
         }
     }
@@ -1039,11 +1103,11 @@ Package::removeItem(int32_t index) {
 
 void
 Package::removeItems(const char *pattern) {
-    int32_t index;
+    int32_t idx;
 
     findItems(pattern);
-    while((index=findNextItem())>=0) {
-        removeItem(index);
+    while((idx=findNextItem())>=0) {
+        removeItem(idx);
     }
 }
 
@@ -1058,7 +1122,7 @@ Package::removeItems(const Package &listPkg) {
 }
 
 void
-Package::extractItem(const char *filesPath, const char *outName, int32_t index, char outType) {
+Package::extractItem(const char *filesPath, const char *outName, int32_t idx, char outType) {
     char filename[1024];
     UDataSwapper *ds;
     FILE *file;
@@ -1067,10 +1131,10 @@ Package::extractItem(const char *filesPath, const char *outName, int32_t index,
     uint8_t itemCharset, outCharset;
     UBool itemIsBigEndian, outIsBigEndian;
 
-    if(index<0 || itemCount<=index) {
+    if(idx<0 || itemCount<=idx) {
         return;
     }
-    pItem=items+index;
+    pItem=items+idx;
 
     // swap the data to the outType
     // outType==0: don't swap
@@ -1082,7 +1146,7 @@ Package::extractItem(const char *filesPath, const char *outName, int32_t index,
         ds=udata_openSwapper(itemIsBigEndian, itemCharset, outIsBigEndian, outCharset, &errorCode);
         if(U_FAILURE(errorCode)) {
             fprintf(stderr, "icupkg: udata_openSwapper(item %ld) failed - %s\n",
-                    (long)index, u_errorName(errorCode));
+                    (long)idx, u_errorName(errorCode));
             exit(errorCode);
         }
 
@@ -1092,10 +1156,11 @@ Package::extractItem(const char *filesPath, const char *outName, int32_t index,
         // swap the item from its platform properties to the desired ones
         udata_swap(ds, pItem->data, pItem->length, pItem->data, &errorCode);
         if(U_FAILURE(errorCode)) {
-            fprintf(stderr, "icupkg: udata_swap(item %ld) failed - %s\n", (long)index, u_errorName(errorCode));
+            fprintf(stderr, "icupkg: udata_swap(item %ld) failed - %s\n", (long)idx, u_errorName(errorCode));
             exit(errorCode);
         }
         udata_closeSwapper(ds);
+        pItem->type=outType;
     }
 
     // create the file and write its contents
@@ -1115,17 +1180,17 @@ Package::extractItem(const char *filesPath, const char *outName, int32_t index,
 }
 
 void
-Package::extractItem(const char *filesPath, int32_t index, char outType) {
-    extractItem(filesPath, items[index].name, index, outType);
+Package::extractItem(const char *filesPath, int32_t idx, char outType) {
+    extractItem(filesPath, items[idx].name, idx, outType);
 }
 
 void
 Package::extractItems(const char *filesPath, const char *pattern, char outType) {
-    int32_t index;
+    int32_t idx;
 
     findItems(pattern);
-    while((index=findNextItem())>=0) {
-        extractItem(filesPath, index, outType);
+    while((idx=findNextItem())>=0) {
+        extractItem(filesPath, idx, outType);
     }
 }
 
@@ -1214,4 +1279,31 @@ Package::sortItems() {
     }
 }
 
+void Package::setItemCapacity(int32_t max) 
+{
+  if(max<=itemMax) {
+    return;
+  }
+  Item *newItems = (Item*)uprv_malloc(max * sizeof(items[0]));
+  Item *oldItems = items;
+  if(newItems == NULL) {
+    fprintf(stderr, "icupkg: Out of memory trying to allocate %lu bytes for %d items\n", 
+        (unsigned long)max*sizeof(items[0]), max);
+    exit(U_MEMORY_ALLOCATION_ERROR);
+  }
+  if(items && itemCount>0) {
+    uprv_memcpy(newItems, items, (size_t)itemCount*sizeof(items[0]));
+  }
+  itemMax = max;
+  items = newItems;
+  uprv_free(oldItems);
+}
+
+void Package::ensureItemCapacity()
+{
+  if((itemCount+1)>itemMax) {
+    setItemCapacity(itemCount+kItemsChunk);
+  }
+}
+
 U_NAMESPACE_END