]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/uresdata.c
ICU-531.30.tar.gz
[apple/icu.git] / icuSources / common / uresdata.c
index 4e3136f61342cc792d0520140e6886b0268daa42..dabde52b87eb324d566247e02c328d775d923f43 100644 (file)
@@ -1,9 +1,7 @@
 /*
 *******************************************************************************
-*                                                                             *
-* Copyright (C) 1999-2004, International Business Machines Corporation        *
-*               and others. All Rights Reserved.                              *
-*                                                                             *
+* Copyright (C) 1999-2014, International Business Machines Corporation
+*               and others. All Rights Reserved.
 *******************************************************************************
 *   file name:  uresdata.c
 *   encoding:   US-ASCII
 
 #include "unicode/utypes.h"
 #include "unicode/udata.h"
+#include "unicode/ustring.h"
+#include "unicode/utf16.h"
 #include "cmemory.h"
 #include "cstring.h"
 #include "uarrsort.h"
 #include "udataswp.h"
 #include "ucol_swp.h"
+#include "uinvchar.h"
 #include "uresdata.h"
 #include "uresimp.h"
+#include "uassert.h"
 
 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
 
  */
 
 /* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
-#define RES_GET_KEY(pRoot, keyOffset) ((const char *)(pRoot)+(keyOffset))
+#define RES_GET_KEY16(pResData, keyOffset) \
+    ((keyOffset)<(pResData)->localKeyLimit ? \
+        (const char *)(pResData)->pRoot+(keyOffset) : \
+        (pResData)->poolBundleKeys+(keyOffset)-(pResData)->localKeyLimit)
+
+#define RES_GET_KEY32(pResData, keyOffset) \
+    ((keyOffset)>=0 ? \
+        (const char *)(pResData)->pRoot+(keyOffset) : \
+        (pResData)->poolBundleKeys+((keyOffset)&0x7fffffff))
+
 #define URESDATA_ITEM_NOT_FOUND -1
 
-/*
- * All the type-access functions assume that
- * the resource is of the expected type.
- */
+/* empty resources, returned when the resource offset is 0 */
+static const uint16_t gEmpty16=0;
 
+static const struct {
+    int32_t length;
+    int32_t res;
+} gEmpty32={ 0, 0 };
 
-/*
- * Array functions
- */
-static Resource
-_res_getArrayItem(Resource *pRoot, Resource res, int32_t indexR) {
-    const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
-    if(indexR<*p) {
-        return ((const Resource *)(p))[1+indexR];
-    } else {
-        return RES_BOGUS;   /* indexR>itemCount */
-    }
-}
+static const struct {
+    int32_t length;
+    UChar nul;
+    UChar pad;
+} gEmptyString={ 0, 0, 0 };
 
 /*
- * Table functions
- *
- * Important: the key offsets are 16-bit byte offsets from pRoot,
- * and the itemCount is one more 16-bit, too.
- * Thus, there are (count+1) uint16_t values.
- * In order to 4-align the Resource item values, there is a padding
- * word if count is even, i.e., there is exactly (~count&1)
- * 16-bit padding words.
- *
- * For Table32, both the count and the key offsets are int32_t's
- * and need not alignment.
+ * All the type-access functions assume that
+ * the resource is of the expected type.
  */
-static const char *
-_res_getTableKey(const Resource *pRoot, const Resource res, int32_t indexS) {
-    const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pRoot, res);
-    if((uint32_t)indexS<(uint32_t)*p) {
-        return RES_GET_KEY(pRoot, p[indexS+1]);
-    } else {
-        return NULL;    /* indexS>itemCount */
-    }
-}
 
-static const char *
-_res_getTable32Key(const Resource *pRoot, const Resource res, int32_t indexS) {
-    const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
-    if((uint32_t)indexS<(uint32_t)*p) {
-        return RES_GET_KEY(pRoot, p[indexS+1]);
-    } else {
-        return NULL;    /* indexS>itemCount */
-    }
-}
-
-
-static Resource
-_res_getTableItem(const Resource *pRoot, const Resource res, int32_t indexR) {
-    const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pRoot, res);
-    int32_t count=*p;
-    if((uint32_t)indexR<(uint32_t)count) {
-        return ((const Resource *)(p+1+count+(~count&1)))[indexR];
-    } else {
-        return RES_BOGUS;   /* indexR>itemCount */
-    }
-}
-
-static Resource
-_res_getTable32Item(const Resource *pRoot, const Resource res, int32_t indexR) {
-    const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
-    int32_t count=*p;
-    if((uint32_t)indexR<(uint32_t)count) {
-        return ((const Resource *)(p+1+count))[indexR];
-    } else {
-        return RES_BOGUS;   /* indexR>itemCount */
-    }
-}
-
-
-static Resource
-_res_findTableItem(const Resource *pRoot, const Resource res, const char *key,
-                   int32_t *index, const char **realKey) {
-    const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pRoot, res);
-    int32_t i, start, limit;
-
-    limit=*p++; /* number of entries */
-
-    if(limit == 0) { /* this table is empty */
-        *index=URESDATA_ITEM_NOT_FOUND;
-        return RES_BOGUS;
-    }
+static int32_t
+_res_findTableItem(const ResourceData *pResData, const uint16_t *keyOffsets, int32_t length,
+                   const char *key, const char **realKey) {
+    const char *tableKey;
+    int32_t mid, start, limit;
+    int result;
 
     /* do a binary search for the key */
     start=0;
-    while(start<limit-1) {
-        i=(int32_t)((start+limit)/2);
-        if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
-            limit=i;
+    limit=length;
+    while(start<limit) {
+        mid = (start + limit) / 2;
+        tableKey = RES_GET_KEY16(pResData, keyOffsets[mid]);
+        if (pResData->useNativeStrcmp) {
+            result = uprv_strcmp(key, tableKey);
         } else {
-            start=i;
+            result = uprv_compareInvCharsAsAscii(key, tableKey);
+        }
+        if (result < 0) {
+            limit = mid;
+        } else if (result > 0) {
+            start = mid + 1;
+        } else {
+            /* We found it! */
+            *realKey=tableKey;
+            return mid;
         }
     }
-
-    /* did we really find it? */
-    if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
-        *index=start;
-        *realKey=RES_GET_KEY(pRoot, p[start]);
-        limit=*(p-1);   /* itemCount */
-        return ((const Resource *)(p+limit+(~limit&1)))[start];
-    } else {
-        *index=URESDATA_ITEM_NOT_FOUND;
-        return RES_BOGUS;   /* not found */
-    }
+    return URESDATA_ITEM_NOT_FOUND;  /* not found or table is empty. */
 }
 
-static Resource
-_res_findTable32Item(const Resource *pRoot, const Resource res, const char *key,
-                     int32_t *index, const char **realKey) {
-    const int32_t *p=(const int32_t *)RES_GET_POINTER(pRoot, res);
-    int32_t i, start, limit;
-
-    limit=*p++; /* number of entries */
-
-    if(limit == 0) { /* this table is empty */
-        *index=URESDATA_ITEM_NOT_FOUND;
-        return RES_BOGUS;
-    }
+static int32_t
+_res_findTable32Item(const ResourceData *pResData, const int32_t *keyOffsets, int32_t length,
+                     const char *key, const char **realKey) {
+    const char *tableKey;
+    int32_t mid, start, limit;
+    int result;
 
     /* do a binary search for the key */
     start=0;
-    while(start<limit-1) {
-        i=(int32_t)((start+limit)/2);
-        if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
-            limit=i;
+    limit=length;
+    while(start<limit) {
+        mid = (start + limit) / 2;
+        tableKey = RES_GET_KEY32(pResData, keyOffsets[mid]);
+        if (pResData->useNativeStrcmp) {
+            result = uprv_strcmp(key, tableKey);
         } else {
-            start=i;
+            result = uprv_compareInvCharsAsAscii(key, tableKey);
+        }
+        if (result < 0) {
+            limit = mid;
+        } else if (result > 0) {
+            start = mid + 1;
+        } else {
+            /* We found it! */
+            *realKey=tableKey;
+            return mid;
         }
     }
-
-    /* did we really find it? */
-    if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
-        *index=start;
-        *realKey=RES_GET_KEY(pRoot, p[start]);
-        limit=*(p-1);   /* itemCount */
-        return ((const Resource *)(p+limit))[start];
-    } else {
-        *index=URESDATA_ITEM_NOT_FOUND;
-        return RES_BOGUS;   /* not found */
-    }
+    return URESDATA_ITEM_NOT_FOUND;  /* not found or table is empty. */
 }
 
 /* helper for res_load() ---------------------------------------------------- */
@@ -193,6 +137,7 @@ static UBool U_CALLCONV
 isAcceptable(void *context,
              const char *type, const char *name,
              const UDataInfo *pInfo) {
+    uprv_memcpy(context, pInfo->formatVersion, 4);
     return (UBool)(
         pInfo->size>=20 &&
         pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
@@ -202,36 +147,118 @@ isAcceptable(void *context,
         pInfo->dataFormat[1]==0x65 &&
         pInfo->dataFormat[2]==0x73 &&
         pInfo->dataFormat[3]==0x42 &&
-        pInfo->formatVersion[0]==1);
+        (pInfo->formatVersion[0]==1 || pInfo->formatVersion[0]==2));
 }
 
 /* semi-public functions ---------------------------------------------------- */
 
-U_CFUNC UBool
-res_load(ResourceData *pResData,
-         const char *path, const char *name, UErrorCode *errorCode) {
+static void
+res_init(ResourceData *pResData,
+         UVersionInfo formatVersion, const void *inBytes, int32_t length,
+         UErrorCode *errorCode) {
     UResType rootType;
 
-    /* load the ResourceBundle file */
-    pResData->data=udata_openChoice(path, "res", name, isAcceptable, NULL, errorCode);
-    if(U_FAILURE(*errorCode)) {
-        return FALSE;
-    }
+    /* get the root resource */
+    pResData->pRoot=(const int32_t *)inBytes;
+    pResData->rootRes=(Resource)*pResData->pRoot;
+    pResData->p16BitUnits=&gEmpty16;
 
-    /* get its memory and root resource */
-    pResData->pRoot=(Resource *)udata_getMemory(pResData->data);
-    pResData->rootRes=*pResData->pRoot;
+    /* formatVersion 1.1 must have a root item and at least 5 indexes */
+    if(length>=0 && (length/4)<((formatVersion[0]==1 && formatVersion[1]==0) ? 1 : 1+5)) {
+        *errorCode=U_INVALID_FORMAT_ERROR;
+        res_unload(pResData);
+        return;
+    }
 
     /* currently, we accept only resources that have a Table as their roots */
-    rootType=RES_GET_TYPE(pResData->rootRes);
-    if(rootType!=URES_TABLE && rootType!=URES_TABLE32) {
+    rootType=(UResType)RES_GET_TYPE(pResData->rootRes);
+    if(!URES_IS_TABLE(rootType)) {
         *errorCode=U_INVALID_FORMAT_ERROR;
-        udata_close(pResData->data);
-        pResData->data=NULL; 
-        return FALSE;
+        res_unload(pResData);
+        return;
+    }
+
+    if(formatVersion[0]==1 && formatVersion[1]==0) {
+        pResData->localKeyLimit=0x10000;  /* greater than any 16-bit key string offset */
+    } else {
+        /* bundles with formatVersion 1.1 and later contain an indexes[] array */
+        const int32_t *indexes=pResData->pRoot+1;
+        int32_t indexLength=indexes[URES_INDEX_LENGTH]&0xff;
+        if(indexLength<=URES_INDEX_MAX_TABLE_LENGTH) {
+            *errorCode=U_INVALID_FORMAT_ERROR;
+            res_unload(pResData);
+            return;
+        }
+        if( length>=0 &&
+            (length<((1+indexLength)<<2) ||
+             length<(indexes[URES_INDEX_BUNDLE_TOP]<<2))
+        ) {
+            *errorCode=U_INVALID_FORMAT_ERROR;
+            res_unload(pResData);
+            return;
+        }
+        if(indexes[URES_INDEX_KEYS_TOP]>(1+indexLength)) {
+            pResData->localKeyLimit=indexes[URES_INDEX_KEYS_TOP]<<2;
+        }
+        if(indexLength>URES_INDEX_ATTRIBUTES) {
+            int32_t att=indexes[URES_INDEX_ATTRIBUTES];
+            pResData->noFallback=(UBool)(att&URES_ATT_NO_FALLBACK);
+            pResData->isPoolBundle=(UBool)((att&URES_ATT_IS_POOL_BUNDLE)!=0);
+            pResData->usesPoolBundle=(UBool)((att&URES_ATT_USES_POOL_BUNDLE)!=0);
+        }
+        if((pResData->isPoolBundle || pResData->usesPoolBundle) && indexLength<=URES_INDEX_POOL_CHECKSUM) {
+            *errorCode=U_INVALID_FORMAT_ERROR;
+            res_unload(pResData);
+            return;
+        }
+        if( indexLength>URES_INDEX_16BIT_TOP &&
+            indexes[URES_INDEX_16BIT_TOP]>indexes[URES_INDEX_KEYS_TOP]
+        ) {
+            pResData->p16BitUnits=(const uint16_t *)(pResData->pRoot+indexes[URES_INDEX_KEYS_TOP]);
+        }
     }
 
-    return TRUE;
+    if(formatVersion[0]==1 || U_CHARSET_FAMILY==U_ASCII_FAMILY) {
+        /*
+         * formatVersion 1: compare key strings in native-charset order
+         * formatVersion 2 and up: compare key strings in ASCII order
+         */
+        pResData->useNativeStrcmp=TRUE;
+    }
+}
+
+U_CAPI void U_EXPORT2
+res_read(ResourceData *pResData,
+         const UDataInfo *pInfo, const void *inBytes, int32_t length,
+         UErrorCode *errorCode) {
+    UVersionInfo formatVersion;
+
+    uprv_memset(pResData, 0, sizeof(ResourceData));
+    if(U_FAILURE(*errorCode)) {
+        return;
+    }
+    if(!isAcceptable(formatVersion, NULL, NULL, pInfo)) {
+        *errorCode=U_INVALID_FORMAT_ERROR;
+        return;
+    }
+    res_init(pResData, formatVersion, inBytes, length, errorCode);
+}
+
+U_CFUNC void
+res_load(ResourceData *pResData,
+         const char *path, const char *name, UErrorCode *errorCode) {
+    UVersionInfo formatVersion;
+
+    uprv_memset(pResData, 0, sizeof(ResourceData));
+
+    /* load the ResourceBundle file */
+    pResData->data=udata_openChoice(path, "res", name, isAcceptable, formatVersion, errorCode);
+    if(U_FAILURE(*errorCode)) {
+        return;
+    }
+
+    /* get its memory and initialize *pResData */
+    res_init(pResData, formatVersion, udata_getMemory(pResData->data), -1, errorCode);
 }
 
 U_CFUNC void
@@ -242,109 +269,280 @@ res_unload(ResourceData *pResData) {
     }
 }
 
-U_CFUNC const UChar *
-res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength) {
-    if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_STRING) {
-        const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
-        if (pLength) {
-            *pLength=*p;
+static const int8_t gPublicTypes[URES_LIMIT] = {
+    URES_STRING,
+    URES_BINARY,
+    URES_TABLE,
+    URES_ALIAS,
+
+    URES_TABLE,     /* URES_TABLE32 */
+    URES_TABLE,     /* URES_TABLE16 */
+    URES_STRING,    /* URES_STRING_V2 */
+    URES_INT,
+
+    URES_ARRAY,
+    URES_ARRAY,     /* URES_ARRAY16 */
+    URES_NONE,
+    URES_NONE,
+
+    URES_NONE,
+    URES_NONE,
+    URES_INT_VECTOR,
+    URES_NONE
+};
+
+U_CAPI UResType U_EXPORT2
+res_getPublicType(Resource res) {
+    return (UResType)gPublicTypes[RES_GET_TYPE(res)];
+}
+
+U_CAPI const UChar * U_EXPORT2
+res_getString(const ResourceData *pResData, Resource res, int32_t *pLength) {
+    const UChar *p;
+    uint32_t offset=RES_GET_OFFSET(res);
+    int32_t length;
+    if(RES_GET_TYPE(res)==URES_STRING_V2) {
+        int32_t first;
+        p=(const UChar *)(pResData->p16BitUnits+offset);
+        first=*p;
+        if(!U16_IS_TRAIL(first)) {
+            length=u_strlen(p);
+        } else if(first<0xdfef) {
+            length=first&0x3ff;
+            ++p;
+        } else if(first<0xdfff) {
+            length=((first-0xdfef)<<16)|p[1];
+            p+=2;
+        } else {
+            length=((int32_t)p[1]<<16)|p[2];
+            p+=3;
         }
-        return (const UChar *)++p;
+    } else if(res==offset) /* RES_GET_TYPE(res)==URES_STRING */ {
+        const int32_t *p32= res==0 ? &gEmptyString.length : pResData->pRoot+res;
+        length=*p32++;
+        p=(const UChar *)p32;
     } else {
-        if (pLength) {
-            *pLength=0;
-        }
-        return NULL;
+        p=NULL;
+        length=0;
+    }
+    if(pLength) {
+        *pLength=length;
     }
+    return p;
 }
 
-U_CFUNC const UChar *
-res_getAlias(const ResourceData *pResData, const Resource res, int32_t *pLength) {
-    if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_ALIAS) {
-        const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
-        if (pLength) {
-            *pLength=*p;
-        }
-        return (const UChar *)++p;
+U_CAPI const UChar * U_EXPORT2
+res_getAlias(const ResourceData *pResData, Resource res, int32_t *pLength) {
+    const UChar *p;
+    uint32_t offset=RES_GET_OFFSET(res);
+    int32_t length;
+    if(RES_GET_TYPE(res)==URES_ALIAS) {
+        const int32_t *p32= offset==0 ? &gEmptyString.length : pResData->pRoot+offset;
+        length=*p32++;
+        p=(const UChar *)p32;
     } else {
-        if (pLength) {
-            *pLength=0;
-        }
-        return NULL;
+        p=NULL;
+        length=0;
+    }
+    if(pLength) {
+        *pLength=length;
     }
+    return p;
 }
 
-U_CFUNC const uint8_t *
-res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength) {
-    if(res!=RES_BOGUS) {
-        const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
-        *pLength=*p++;
-        if (*pLength == 0) {
-            p = NULL;
-        }
-        return (const uint8_t *)p;
+U_CAPI const uint8_t * U_EXPORT2
+res_getBinary(const ResourceData *pResData, Resource res, int32_t *pLength) {
+    const uint8_t *p;
+    uint32_t offset=RES_GET_OFFSET(res);
+    int32_t length;
+    if(RES_GET_TYPE(res)==URES_BINARY) {
+        const int32_t *p32= offset==0 ? (const int32_t*)&gEmpty32 : pResData->pRoot+offset;
+        length=*p32++;
+        p=(const uint8_t *)p32;
     } else {
-        *pLength=0;
-        return NULL;
+        p=NULL;
+        length=0;
+    }
+    if(pLength) {
+        *pLength=length;
     }
+    return p;
 }
 
 
-U_CFUNC const int32_t *
-res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength) {
-    if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_INT_VECTOR) {
-        const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
-        *pLength=*p++;
-        if (*pLength == 0) {
-            p = NULL;
-        }
-        return (const int32_t *)p;
+U_CAPI const int32_t * U_EXPORT2
+res_getIntVector(const ResourceData *pResData, Resource res, int32_t *pLength) {
+    const int32_t *p;
+    uint32_t offset=RES_GET_OFFSET(res);
+    int32_t length;
+    if(RES_GET_TYPE(res)==URES_INT_VECTOR) {
+        p= offset==0 ? (const int32_t *)&gEmpty32 : pResData->pRoot+offset;
+        length=*p++;
     } else {
-        *pLength=0;
-        return NULL;
+        p=NULL;
+        length=0;
+    }
+    if(pLength) {
+        *pLength=length;
+    }
+    return p;
+}
+
+U_CAPI int32_t U_EXPORT2
+res_countArrayItems(const ResourceData *pResData, Resource res) {
+    uint32_t offset=RES_GET_OFFSET(res);
+    switch(RES_GET_TYPE(res)) {
+    case URES_STRING:
+    case URES_STRING_V2:
+    case URES_BINARY:
+    case URES_ALIAS:
+    case URES_INT:
+    case URES_INT_VECTOR:
+        return 1;
+    case URES_ARRAY:
+    case URES_TABLE32:
+        return offset==0 ? 0 : *(pResData->pRoot+offset);
+    case URES_TABLE:
+        return offset==0 ? 0 : *((const uint16_t *)(pResData->pRoot+offset));
+    case URES_ARRAY16:
+    case URES_TABLE16:
+        return pResData->p16BitUnits[offset];
+    default:
+        return 0;
     }
 }
 
-U_CFUNC int32_t
-res_countArrayItems(const ResourceData *pResData, const Resource res) {
-    if(res!=RES_BOGUS) {
-        switch(RES_GET_TYPE(res)) {
-        case URES_STRING:
-        case URES_BINARY:
-        case URES_ALIAS:
-        case URES_INT:
-        case URES_INT_VECTOR:
-            return 1;
-        case URES_ARRAY:
-        case URES_TABLE32: {
-            const int32_t *p=(const int32_t *)RES_GET_POINTER(pResData->pRoot, res);
-            return *p;
+U_CAPI Resource U_EXPORT2
+res_getTableItemByKey(const ResourceData *pResData, Resource table,
+                      int32_t *indexR, const char **key) {
+    uint32_t offset=RES_GET_OFFSET(table);
+    int32_t length;
+    int32_t idx;
+    if(key == NULL || *key == NULL) {
+        return RES_BOGUS;
+    }
+    switch(RES_GET_TYPE(table)) {
+    case URES_TABLE: {
+        if (offset!=0) { /* empty if offset==0 */
+            const uint16_t *p= (const uint16_t *)(pResData->pRoot+offset);
+            length=*p++;
+            *indexR=idx=_res_findTableItem(pResData, p, length, *key, key);
+            if(idx>=0) {
+                const Resource *p32=(const Resource *)(p+length+(~length&1));
+                return p32[idx];
+            }
         }
-        case URES_TABLE: {
-            const uint16_t *p=(const uint16_t *)RES_GET_POINTER(pResData->pRoot, res);
-            return *p;
+        break;
+    }
+    case URES_TABLE16: {
+        const uint16_t *p=pResData->p16BitUnits+offset;
+        length=*p++;
+        *indexR=idx=_res_findTableItem(pResData, p, length, *key, key);
+        if(idx>=0) {
+            return URES_MAKE_RESOURCE(URES_STRING_V2, p[length+idx]);
         }
-        default:
-            break;
+        break;
+    }
+    case URES_TABLE32: {
+        if (offset!=0) { /* empty if offset==0 */
+            const int32_t *p= pResData->pRoot+offset;
+            length=*p++;
+            *indexR=idx=_res_findTable32Item(pResData, p, length, *key, key);
+            if(idx>=0) {
+                return (Resource)p[length+idx];
+            }
         }
-    } 
-    return 0;
+        break;
+    }
+    default:
+        break;
+    }
+    return RES_BOGUS;
 }
 
-U_CFUNC Resource
-res_getResource(const ResourceData *pResData, const char *key) {
-    int32_t index;
-    const char *realKey;
-    if(RES_GET_TYPE(pResData->rootRes)==URES_TABLE) {
-        return _res_findTableItem(pResData->pRoot, pResData->rootRes, key, &index, &realKey);
-    } else {
-        return _res_findTable32Item(pResData->pRoot, pResData->rootRes, key, &index, &realKey);
+U_CAPI Resource U_EXPORT2
+res_getTableItemByIndex(const ResourceData *pResData, Resource table,
+                        int32_t indexR, const char **key) {
+    uint32_t offset=RES_GET_OFFSET(table);
+    int32_t length;
+    U_ASSERT(indexR>=0); /* to ensure the index is not negative */
+    switch(RES_GET_TYPE(table)) {
+    case URES_TABLE: {
+        if (offset != 0) { /* empty if offset==0 */
+            const uint16_t *p= (const uint16_t *)(pResData->pRoot+offset);
+            length=*p++;
+            if(indexR<length) {
+                const Resource *p32=(const Resource *)(p+length+(~length&1));
+                if(key!=NULL) {
+                    *key=RES_GET_KEY16(pResData, p[indexR]);
+                }
+                return p32[indexR];
+            }
+        }
+        break;
+    }
+    case URES_TABLE16: {
+        const uint16_t *p=pResData->p16BitUnits+offset;
+        length=*p++;
+        if(indexR<length) {
+            if(key!=NULL) {
+                *key=RES_GET_KEY16(pResData, p[indexR]);
+            }
+            return URES_MAKE_RESOURCE(URES_STRING_V2, p[length+indexR]);
+        }
+        break;
+    }
+    case URES_TABLE32: {
+        if (offset != 0) { /* empty if offset==0 */
+            const int32_t *p= pResData->pRoot+offset;
+            length=*p++;
+            if(indexR<length) {
+                if(key!=NULL) {
+                    *key=RES_GET_KEY32(pResData, p[indexR]);
+                }
+                return (Resource)p[length+indexR];
+            }
+        }
+        break;
     }
+    default:
+        break;
+    }
+    return RES_BOGUS;
 }
 
-U_CFUNC Resource 
-res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexR) {
-    return _res_getArrayItem(pResData->pRoot, array, indexR);
+U_CAPI Resource U_EXPORT2
+res_getResource(const ResourceData *pResData, const char *key) {
+    const char *realKey=key;
+    int32_t idx;
+    return res_getTableItemByKey(pResData, pResData->rootRes, &idx, &realKey);
+}
+
+U_CAPI Resource U_EXPORT2
+res_getArrayItem(const ResourceData *pResData, Resource array, int32_t indexR) {
+    uint32_t offset=RES_GET_OFFSET(array);
+    U_ASSERT(indexR>=0); /* to ensure the index is not negative */
+    switch(RES_GET_TYPE(array)) {
+    case URES_ARRAY: {
+        if (offset!=0) { /* empty if offset==0 */
+            const int32_t *p= pResData->pRoot+offset;
+            if(indexR<*p) {
+                return (Resource)p[1+indexR];
+            }
+        }
+        break;
+    }
+    case URES_ARRAY16: {
+        const uint16_t *p=pResData->p16BitUnits+offset;
+        if(indexR<*p) {
+            return URES_MAKE_RESOURCE(URES_STRING_V2, p[1+indexR]);
+        }
+        break;
+    }
+    default:
+        break;
+    }
+    return RES_BOGUS;
 }
 
 U_CFUNC Resource
@@ -359,7 +557,7 @@ res_findResource(const ResourceData *pResData, Resource r, char** path, const ch
   Resource t1 = r;
   Resource t2;
   int32_t indexR = 0;
-  UResType type = RES_GET_TYPE(t1);
+  UResType type = (UResType)RES_GET_TYPE(t1);
 
   /* if you come in with an empty path, you'll be getting back the same resource */
   if(!uprv_strlen(pathP)) {
@@ -367,13 +565,11 @@ res_findResource(const ResourceData *pResData, Resource r, char** path, const ch
   }
 
   /* one needs to have an aggregate resource in order to search in it */
-  if(!(type == URES_TABLE || type == URES_TABLE32 || type == URES_ARRAY)) {
+  if(!URES_IS_CONTAINER(type)) {
       return RES_BOGUS;
   }
   
-  while(nextSepP && *pathP && t1 != RES_BOGUS &&
-        (type == URES_TABLE || type == URES_TABLE32 || type == URES_ARRAY)
-  ) {
+  while(nextSepP && *pathP && t1 != RES_BOGUS && URES_IS_CONTAINER(type)) {
     /* Iteration stops if: the path has been consumed, we found a non-existing
      * resource (t1 == RES_BOGUS) or we found a scalar resource (including alias)
      */
@@ -390,18 +586,9 @@ res_findResource(const ResourceData *pResData, Resource r, char** path, const ch
 
     /* if the resource is a table */
     /* try the key based access */
-    if(type == URES_TABLE) {
-      t2 = _res_findTableItem(pResData->pRoot, t1, pathP, &indexR, key);
-      if(t2 == RES_BOGUS) { 
-        /* if we fail to get the resource by key, maybe we got an index */
-        indexR = uprv_strtol(pathP, &closeIndex, 10);
-        if(closeIndex != pathP) {
-          /* if we indeed have an index, try to get the item by index */
-          t2 = res_getTableItemByIndex(pResData, t1, indexR, key);
-        }
-      }
-    } else if(type == URES_TABLE32) {
-      t2 = _res_findTable32Item(pResData->pRoot, t1, pathP, &indexR, key);
+    if(URES_IS_TABLE(type)) {
+      *key = pathP;
+      t2 = res_getTableItemByKey(pResData, t1, &indexR, key);
       if(t2 == RES_BOGUS) { 
         /* if we fail to get the resource by key, maybe we got an index */
         indexR = uprv_strtol(pathP, &closeIndex, 10);
@@ -410,10 +597,10 @@ res_findResource(const ResourceData *pResData, Resource r, char** path, const ch
           t2 = res_getTableItemByIndex(pResData, t1, indexR, key);
         }
       }
-    } else if(type == URES_ARRAY) {
+    } else if(URES_IS_ARRAY(type)) {
       indexR = uprv_strtol(pathP, &closeIndex, 10);
       if(closeIndex != pathP) {
-        t2 = _res_getArrayItem(pResData->pRoot, t1, indexR);
+        t2 = res_getArrayItem(pResData, t1, indexR);
       } else {
         t2 = RES_BOGUS; /* have an array, but don't have a valid index */
       }
@@ -422,7 +609,7 @@ res_findResource(const ResourceData *pResData, Resource r, char** path, const ch
       t2 = RES_BOGUS;
     }
     t1 = t2;
-    type = RES_GET_TYPE(t1);
+    type = (UResType)RES_GET_TYPE(t1);
     /* position pathP to next resource key/index */
     pathP = *path;
   }
@@ -430,40 +617,6 @@ res_findResource(const ResourceData *pResData, Resource r, char** path, const ch
   return t1;
 }
 
-U_CFUNC Resource 
-res_getTableItemByKey(const ResourceData *pResData, Resource table,
-                      int32_t *indexR, const char **key) {
-    if(key != NULL && *key != NULL) {
-        if(RES_GET_TYPE(table)==URES_TABLE) {
-            return _res_findTableItem(pResData->pRoot, table, *key, indexR, key);
-        } else {
-            return _res_findTable32Item(pResData->pRoot, table, *key, indexR, key);
-        }
-    } else {
-        return RES_BOGUS;
-    }
-}
-
-U_CFUNC Resource 
-res_getTableItemByIndex(const ResourceData *pResData, Resource table,
-                        int32_t indexR, const char **key) {
-    if(indexR>-1) {
-        if(RES_GET_TYPE(table)==URES_TABLE) {
-            if(key != NULL) {
-                *key = _res_getTableKey(pResData->pRoot, table, indexR);
-            }
-            return _res_getTableItem(pResData->pRoot, table, indexR);
-        } else {
-            if(key != NULL) {
-                *key = _res_getTable32Key(pResData->pRoot, table, indexR);
-            }
-            return _res_getTable32Item(pResData->pRoot, table, indexR);
-        }
-    } else {
-        return RES_BOGUS;
-    }
-}
-
 /* resource bundle swapping ------------------------------------------------- */
 
 /*
@@ -512,18 +665,17 @@ typedef struct TempTable {
     const char *keyChars;
     Row *rows;
     int32_t *resort;
+    uint32_t *resFlags;
+    int32_t localKeyLimit;
+    uint8_t majorFormatVersion;
 } TempTable;
 
 enum {
     STACK_ROW_CAPACITY=200
 };
 
-/* binary data with known formats is swapped too */
-typedef enum UResSpecialType {
-    URES_NO_SPECIAL_TYPE,
-    URES_COLLATION_BINARY,
-    URES_SPECIAL_TYPE_COUNT
-} UResSpecialType;
+/* The table item key string is not locally available. */
+static const char *const gUnknownKey="";
 
 /* resource table key for collation binaries: "%%CollationBin" */
 static const UChar gCollationBinKey[]={
@@ -533,161 +685,45 @@ static const UChar gCollationBinKey[]={
     0
 };
 
-/*
- * preflight one resource item and set bottom and top values;
- * length, bottom, and top count Resource item offsets (4 bytes each), not bytes
- */
-static void
-ures_preflightResource(const UDataSwapper *ds,
-                       const Resource *inBundle, int32_t length,
-                       Resource res,
-                       int32_t *pBottom, int32_t *pTop, int32_t *pMaxTableLength,
-                       UErrorCode *pErrorCode) {
-    const Resource *p;
-    int32_t offset;
-
-    if(res==0 || RES_GET_TYPE(res)==URES_INT) {
-        /* empty string or integer, nothing to do */
-        return;
-    }
-
-    /* all other types use an offset to point to their data */
-    offset=(int32_t)RES_GET_OFFSET(res);
-    if(0<=length && length<=offset) {
-        udata_printError(ds, "ures_preflightResource(res=%08x) resource offset exceeds bundle length %d\n",
-                         res, length);
-        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
-        return;
-    } else if(offset<*pBottom) {
-        *pBottom=offset;
-    }
-    p=inBundle+offset;
-
-    switch(RES_GET_TYPE(res)) {
-    case URES_ALIAS:
-        /* physically same value layout as string, fall through */
-    case URES_STRING:
-        /* top=offset+1+(string length +1)/2 rounded up */
-        offset+=1+((udata_readInt32(ds, (int32_t)*p)+1)+1)/2;
-        break;
-    case URES_BINARY:
-        /* top=offset+1+(binary length)/4 rounded up */
-        offset+=1+(udata_readInt32(ds, (int32_t)*p)+3)/4;
-        break;
-    case URES_TABLE:
-    case URES_TABLE32:
-        {
-            Resource item;
-            int32_t i, count;
-
-            if(RES_GET_TYPE(res)==URES_TABLE) {
-                /* get table item count */
-                const uint16_t *pKey16=(const uint16_t *)p;
-                count=ds->readUInt16(*pKey16++);
-
-                /* top=((1+ table item count)/2 rounded up)+(table item count) */
-                offset+=((1+count)+1)/2;
-            } else {
-                /* get table item count */
-                const int32_t *pKey32=(const int32_t *)p;
-                count=udata_readInt32(ds, *pKey32++);
-
-                /* top=(1+ table item count)+(table item count) */
-                offset+=1+count;
-            }
-
-            if(count>*pMaxTableLength) {
-                *pMaxTableLength=count;
-            }
-
-            p=inBundle+offset; /* pointer to table resources */
-            offset+=count;
-
-            /* recurse */
-            if(offset<=length) {
-                for(i=0; i<count; ++i) {
-                    item=ds->readUInt32(*p++);
-                    ures_preflightResource(ds, inBundle, length, item,
-                                           pBottom, pTop, pMaxTableLength,
-                                           pErrorCode);
-                    if(U_FAILURE(*pErrorCode)) {
-                        udata_printError(ds, "ures_preflightResource(table res=%08x)[%d].recurse(%08x) failed - %s\n",
-                                         res, i, item, u_errorName(*pErrorCode));
-                        break;
-                    }
-                }
-            }
-        }
-        break;
-    case URES_ARRAY:
-        {
-            Resource item;
-            int32_t i, count;
-
-            /* top=offset+1+(array length) */
-            count=udata_readInt32(ds, (int32_t)*p++);
-            offset+=1+count;
-
-            /* recurse */
-            if(offset<=length) {
-                for(i=0; i<count; ++i) {
-                    item=ds->readUInt32(*p++);
-                    ures_preflightResource(ds, inBundle, length, item,
-                                           pBottom, pTop, pMaxTableLength,
-                                           pErrorCode);
-                    if(U_FAILURE(*pErrorCode)) {
-                        udata_printError(ds, "ures_preflightResource(array res=%08x)[%d].recurse(%08x) failed - %s\n",
-                                         res, i, item, u_errorName(*pErrorCode));
-                        break;
-                    }
-                }
-            }
-        }
-        break;
-    case URES_INT_VECTOR:
-        /* top=offset+1+(vector length) */
-        offset+=1+udata_readInt32(ds, (int32_t)*p);
-        break;
-    default:
-        /* also catches RES_BOGUS */
-        udata_printError(ds, "ures_preflightResource(res=%08x) unknown resource type\n", res);
-        *pErrorCode=U_UNSUPPORTED_ERROR;
-        break;
-    }
-
-    if(U_FAILURE(*pErrorCode)) {
-        /* nothing to do */
-    } else if(0<=length && length<offset) {
-        udata_printError(ds, "ures_preflightResource(res=%08x) resource limit exceeds bundle length %d\n",
-                         res, length);
-        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
-    } else if(offset>*pTop) {
-        *pTop=offset;
-    }
-}
-
 /*
  * swap one resource item
- * since preflighting succeeded, we need not check offsets against length any more
  */
 static void
 ures_swapResource(const UDataSwapper *ds,
                   const Resource *inBundle, Resource *outBundle,
                   Resource res, /* caller swaps res itself */
-                  UResSpecialType specialType,
+                  const char *key,
                   TempTable *pTempTable,
                   UErrorCode *pErrorCode) {
     const Resource *p;
     Resource *q;
     int32_t offset, count;
 
-    if(res==0 || RES_GET_TYPE(res)==URES_INT) {
-        /* empty string or integer, nothing to do */
+    switch(RES_GET_TYPE(res)) {
+    case URES_TABLE16:
+    case URES_STRING_V2:
+    case URES_INT:
+    case URES_ARRAY16:
+        /* integer, or points to 16-bit units, nothing to do here */
         return;
+    default:
+        break;
     }
 
     /* all other types use an offset to point to their data */
     offset=(int32_t)RES_GET_OFFSET(res);
+    if(offset==0) {
+        /* special offset indicating an empty item */
+        return;
+    }
+    if(pTempTable->resFlags[offset>>5]&((uint32_t)1<<(offset&0x1f))) {
+        /* we already swapped this resource item */
+        return;
+    } else {
+        /* mark it as swapped now */
+        pTempTable->resFlags[offset>>5]|=((uint32_t)1<<(offset&0x1f));
+    }
+
     p=inBundle+offset;
     q=outBundle+offset;
 
@@ -708,11 +744,18 @@ ures_swapResource(const UDataSwapper *ds,
         /* no need to swap or copy bytes - ures_swap() copied them all */
 
         /* swap known formats */
-        if(specialType==URES_COLLATION_BINARY) {
 #if !UCONFIG_NO_COLLATION
-            ucol_swapBinary(ds, p+1, count, q+1, pErrorCode);
-#endif
+        if( key!=NULL &&  /* the binary is in a table */
+            (key!=gUnknownKey ?
+                /* its table key string is "%%CollationBin" */
+                0==ds->compareInvChars(ds, key, -1,
+                                       gCollationBinKey, LENGTHOF(gCollationBinKey)-1) :
+                /* its table key string is unknown but it looks like a collation binary */
+                ucol_looksLikeCollationBinary(ds, p+1, count))
+        ) {
+            ucol_swap(ds, p+1, count, q+1, pErrorCode);
         }
+#endif
         break;
     case URES_TABLE:
     case URES_TABLE32:
@@ -761,37 +804,28 @@ ures_swapResource(const UDataSwapper *ds,
 
             /* recurse */
             for(i=0; i<count; ++i) {
-                /*
-                 * detect a collation binary that is to be swapped via
-                 * ds->compareInvChars(ds, outData+readUInt16(pKey[i]), "%%CollationBin")
-                 * etc.
-                 *
-                 * use some UDataSwapFn pointer from somewhere for collation swapping
-                 * because the common library cannot directly call into the i18n library
-                 */
-                if(0==ds->compareInvChars(ds,
-                            ((const char *)outBundle)+
-                                (pKey16!=NULL ?
-                                    ds->readUInt16(pKey16[i]) :
-                                    udata_readInt32(ds, pKey32[i])),
-                            -1,
-                            gCollationBinKey, LENGTHOF(gCollationBinKey)-1)
-                ) {
-                    specialType=URES_COLLATION_BINARY;
+                const char *itemKey=gUnknownKey;
+                if(pKey16!=NULL) {
+                    int32_t keyOffset=ds->readUInt16(pKey16[i]);
+                    if(keyOffset<pTempTable->localKeyLimit) {
+                        itemKey=(const char *)outBundle+keyOffset;
+                    }
                 } else {
-                    specialType=URES_NO_SPECIAL_TYPE;
+                    int32_t keyOffset=udata_readInt32(ds, pKey32[i]);
+                    if(keyOffset>=0) {
+                        itemKey=(const char *)outBundle+keyOffset;
+                    }
                 }
-
                 item=ds->readUInt32(p[i]);
-                ures_swapResource(ds, inBundle, outBundle, item, specialType, pTempTable, pErrorCode);
+                ures_swapResource(ds, inBundle, outBundle, item, itemKey, pTempTable, pErrorCode);
                 if(U_FAILURE(*pErrorCode)) {
-                    udata_printError(ds, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed - %s\n",
-                                     res, i, item, u_errorName(*pErrorCode));
+                    udata_printError(ds, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
+                                     res, i, item);
                     return;
                 }
             }
 
-            if(ds->inCharset==ds->outCharset) {
+            if(pTempTable->majorFormatVersion>1 || ds->inCharset==ds->outCharset) {
                 /* no need to sort, just swap the offset/value arrays */
                 if(pKey16!=NULL) {
                     ds->swapArray16(ds, pKey16, count*2, qKey16, pErrorCode);
@@ -826,8 +860,8 @@ ures_swapResource(const UDataSwapper *ds,
                            ures_compareRows, pTempTable->keyChars,
                            FALSE, pErrorCode);
             if(U_FAILURE(*pErrorCode)) {
-                udata_printError(ds, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed - %s\n",
-                                 res, count, u_errorName(*pErrorCode));
+                udata_printError(ds, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
+                                 res, count);
                 return;
             }
 
@@ -903,10 +937,10 @@ ures_swapResource(const UDataSwapper *ds,
             /* recurse */
             for(i=0; i<count; ++i) {
                 item=ds->readUInt32(p[i]);
-                ures_swapResource(ds, inBundle, outBundle, item, URES_NO_SPECIAL_TYPE, pTempTable, pErrorCode);
+                ures_swapResource(ds, inBundle, outBundle, item, NULL, pTempTable, pErrorCode);
                 if(U_FAILURE(*pErrorCode)) {
-                    udata_printError(ds, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed - %s\n",
-                                     res, i, item, u_errorName(*pErrorCode));
+                    udata_printError(ds, "ures_swapResource(array res=%08x)[%d].recurse(%08x) failed\n",
+                                     res, i, item);
                     return;
                 }
             }
@@ -940,8 +974,10 @@ ures_swap(const UDataSwapper *ds,
     int32_t resort[STACK_ROW_CAPACITY];
     TempTable tempTable;
 
+    const int32_t *inIndexes;
+
     /* the following integers count Resource item offsets (4 bytes each), not bytes */
-    int32_t bundleLength, stringsBottom, bottom, top;
+    int32_t bundleLength, indexLength, keysBottom, keysTop, resBottom, top;
 
     /* udata_swapDataHeader checks the arguments */
     headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
@@ -956,15 +992,17 @@ ures_swap(const UDataSwapper *ds,
         pInfo->dataFormat[1]==0x65 &&
         pInfo->dataFormat[2]==0x73 &&
         pInfo->dataFormat[3]==0x42 &&
-        pInfo->formatVersion[0]==1
+        ((pInfo->formatVersion[0]==1 && pInfo->formatVersion[1]>=1) ||  /* formatVersion 1.1+ or 2.x */
+         pInfo->formatVersion[0]==2)
     )) {
-        udata_printError(ds, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not a resource bundle\n",
+        udata_printError(ds, "ures_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not a resource bundle\n",
                          pInfo->dataFormat[0], pInfo->dataFormat[1],
                          pInfo->dataFormat[2], pInfo->dataFormat[3],
-                         pInfo->formatVersion[0]);
+                         pInfo->formatVersion[0], pInfo->formatVersion[1]);
         *pErrorCode=U_UNSUPPORTED_ERROR;
         return 0;
     }
+    tempTable.majorFormatVersion=pInfo->formatVersion[0];
 
     /* a resource bundle must contain at least one resource item */
     if(length<0) {
@@ -973,9 +1011,7 @@ ures_swap(const UDataSwapper *ds,
         bundleLength=(length-headerSize)/4;
 
         /* formatVersion 1.1 must have a root item and at least 5 indexes */
-        if( bundleLength<
-                (pInfo->formatVersion[1]==0 ? 1 : 1+5)
-        ) {
+        if(bundleLength<(1+5)) {
             udata_printError(ds, "ures_swap(): too few bytes (%d after header) for a resource bundle\n",
                              length-headerSize);
             *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
@@ -986,58 +1022,91 @@ ures_swap(const UDataSwapper *ds,
     inBundle=(const Resource *)((const char *)inData+headerSize);
     rootRes=ds->readUInt32(*inBundle);
 
-    if(pInfo->formatVersion[1]==0) {
-        /* preflight to get the bottom, top and maxTableLength values */
-        stringsBottom=1; /* just past root */
-        bottom=0x7fffffff;
-        top=maxTableLength=0;
-        ures_preflightResource(ds, inBundle, bundleLength, rootRes,
-                               &bottom, &top, &maxTableLength,
-                               pErrorCode);
-        if(U_FAILURE(*pErrorCode)) {
-            udata_printError(ds, "ures_preflightResource(root res=%08x) failed - %s\n",
-                             rootRes, u_errorName(*pErrorCode));
-            return 0;
-        }
-    } else {
-        /* formatVersion 1.1 adds the indexes[] array */
-        const int32_t *inIndexes;
-
-        inIndexes=(const int32_t *)(inBundle+1);
+    /* formatVersion 1.1 adds the indexes[] array */
+    inIndexes=(const int32_t *)(inBundle+1);
 
-        stringsBottom=1+udata_readInt32(ds, inIndexes[URES_INDEX_LENGTH]);
-        bottom=udata_readInt32(ds, inIndexes[URES_INDEX_STRINGS_TOP]);
-        top=udata_readInt32(ds, inIndexes[URES_INDEX_BUNDLE_TOP]);
-        maxTableLength=udata_readInt32(ds, inIndexes[URES_INDEX_MAX_TABLE_LENGTH]);
+    indexLength=udata_readInt32(ds, inIndexes[URES_INDEX_LENGTH])&0xff;
+    if(indexLength<=URES_INDEX_MAX_TABLE_LENGTH) {
+        udata_printError(ds, "ures_swap(): too few indexes for a 1.1+ resource bundle\n");
+        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
+        return 0;
+    }
+    keysBottom=1+indexLength;
+    keysTop=udata_readInt32(ds, inIndexes[URES_INDEX_KEYS_TOP]);
+    if(indexLength>URES_INDEX_16BIT_TOP) {
+        resBottom=udata_readInt32(ds, inIndexes[URES_INDEX_16BIT_TOP]);
+    } else {
+        resBottom=keysTop;
+    }
+    top=udata_readInt32(ds, inIndexes[URES_INDEX_BUNDLE_TOP]);
+    maxTableLength=udata_readInt32(ds, inIndexes[URES_INDEX_MAX_TABLE_LENGTH]);
 
-        if(0<=bundleLength && bundleLength<top) {
-            udata_printError(ds, "ures_swap(): resource top %d exceeds bundle length %d\n",
-                             top, bundleLength);
-            *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
-            return 0;
-        }
+    if(0<=bundleLength && bundleLength<top) {
+        udata_printError(ds, "ures_swap(): resource top %d exceeds bundle length %d\n",
+                         top, bundleLength);
+        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
+        return 0;
+    }
+    if(keysTop>(1+indexLength)) {
+        tempTable.localKeyLimit=keysTop<<2;
+    } else {
+        tempTable.localKeyLimit=0;
     }
 
     if(length>=0) {
         Resource *outBundle=(Resource *)((char *)outData+headerSize);
 
+        /* track which resources we have already swapped */
+        uint32_t stackResFlags[STACK_ROW_CAPACITY];
+        int32_t resFlagsLength;
+
+        /*
+         * We need one bit per 4 resource bundle bytes so that we can track
+         * every possible Resource for whether we have swapped it already.
+         * Multiple Resource words can refer to the same bundle offsets
+         * for sharing identical values.
+         * We could optimize this by allocating only for locations above
+         * where Resource values are stored (above keys & strings).
+         */
+        resFlagsLength=(length+31)>>5;          /* number of bytes needed */
+        resFlagsLength=(resFlagsLength+3)&~3;   /* multiple of 4 bytes for uint32_t */
+        if(resFlagsLength<=sizeof(stackResFlags)) {
+            tempTable.resFlags=stackResFlags;
+        } else {
+            tempTable.resFlags=(uint32_t *)uprv_malloc(resFlagsLength);
+            if(tempTable.resFlags==NULL) {
+                udata_printError(ds, "ures_swap(): unable to allocate memory for tracking resources\n");
+                *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
+                return 0;
+            }
+        }
+        uprv_memset(tempTable.resFlags, 0, resFlagsLength);
+
         /* copy the bundle for binary and inaccessible data */
         if(inData!=outData) {
             uprv_memcpy(outBundle, inBundle, 4*top);
         }
 
         /* swap the key strings, but not the padding bytes (0xaa) after the last string and its NUL */
-        udata_swapInvStringBlock(ds, inBundle+stringsBottom, 4*(bottom-stringsBottom),
-                                    outBundle+stringsBottom, pErrorCode);
+        udata_swapInvStringBlock(ds, inBundle+keysBottom, 4*(keysTop-keysBottom),
+                                    outBundle+keysBottom, pErrorCode);
         if(U_FAILURE(*pErrorCode)) {
-            udata_printError(ds, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed - %s\n", 4*(bottom-1),
-                             u_errorName(*pErrorCode));
+            udata_printError(ds, "ures_swap().udata_swapInvStringBlock(keys[%d]) failed\n", 4*(keysTop-keysBottom));
             return 0;
         }
 
+        /* swap the 16-bit units (strings, table16, array16) */
+        if(keysTop<resBottom) {
+            ds->swapArray16(ds, inBundle+keysTop, (resBottom-keysTop)*4, outBundle+keysTop, pErrorCode);
+            if(U_FAILURE(*pErrorCode)) {
+                udata_printError(ds, "ures_swap().swapArray16(16-bit units[%d]) failed\n", 2*(resBottom-keysTop));
+                return 0;
+            }
+        }
+
         /* allocate the temporary table for sorting resource tables */
         tempTable.keyChars=(const char *)outBundle; /* sort by outCharset */
-        if(maxTableLength<=STACK_ROW_CAPACITY) {
+        if(tempTable.majorFormatVersion>1 || maxTableLength<=STACK_ROW_CAPACITY) {
             tempTable.rows=rows;
             tempTable.resort=resort;
         } else {
@@ -1046,24 +1115,30 @@ ures_swap(const UDataSwapper *ds,
                 udata_printError(ds, "ures_swap(): unable to allocate memory for sorting tables (max length: %d)\n",
                                  maxTableLength);
                 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
+                if(tempTable.resFlags!=stackResFlags) {
+                    uprv_free(tempTable.resFlags);
+                }
                 return 0;
             }
             tempTable.resort=(int32_t *)(tempTable.rows+maxTableLength);
         }
 
         /* swap the resources */
-        ures_swapResource(ds, inBundle, outBundle, rootRes, URES_NO_SPECIAL_TYPE, &tempTable, pErrorCode);
+        ures_swapResource(ds, inBundle, outBundle, rootRes, NULL, &tempTable, pErrorCode);
         if(U_FAILURE(*pErrorCode)) {
-            udata_printError(ds, "ures_swapResource(root res=%08x) failed - %s\n",
-                             rootRes, u_errorName(*pErrorCode));
+            udata_printError(ds, "ures_swapResource(root res=%08x) failed\n",
+                             rootRes);
         }
 
         if(tempTable.rows!=rows) {
             uprv_free(tempTable.rows);
         }
+        if(tempTable.resFlags!=stackResFlags) {
+            uprv_free(tempTable.resFlags);
+        }
 
         /* swap the root resource and indexes */
-        ds->swapArray32(ds, inBundle, stringsBottom*4, outBundle, pErrorCode);
+        ds->swapArray32(ds, inBundle, keysBottom*4, outBundle, pErrorCode);
     }
 
     return headerSize+4*top;