+ /* mark it as swapped now */
+ pTempTable->resFlags[offset>>5]|=((uint32_t)1<<(offset&0x1f));
+ }
+
+ p=inBundle+offset;
+ q=outBundle+offset;
+
+ switch(RES_GET_TYPE(res)) {
+ case URES_ALIAS:
+ /* physically same value layout as string, fall through */
+ case URES_STRING:
+ count=udata_readInt32(ds, (int32_t)*p);
+ /* swap length */
+ ds->swapArray32(ds, p, 4, q, pErrorCode);
+ /* swap each UChar (the terminating NUL would not change) */
+ ds->swapArray16(ds, p+1, 2*count, q+1, pErrorCode);
+ break;
+ case URES_BINARY:
+ count=udata_readInt32(ds, (int32_t)*p);
+ /* swap length */
+ ds->swapArray32(ds, p, 4, q, pErrorCode);
+ /* no need to swap or copy bytes - ures_swap() copied them all */
+
+ /* swap known formats */
+#if !UCONFIG_NO_COLLATION
+ 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_swapBinary(ds, p+1, count, q+1, pErrorCode);
+ }
+#endif
+ break;
+ case URES_TABLE:
+ case URES_TABLE32:
+ {
+ const uint16_t *pKey16;
+ uint16_t *qKey16;
+
+ const int32_t *pKey32;
+ int32_t *qKey32;
+
+ Resource item;
+ int32_t i, oldIndex;
+
+ if(RES_GET_TYPE(res)==URES_TABLE) {
+ /* get table item count */
+ pKey16=(const uint16_t *)p;
+ qKey16=(uint16_t *)q;
+ count=ds->readUInt16(*pKey16);
+
+ pKey32=qKey32=NULL;
+
+ /* swap count */
+ ds->swapArray16(ds, pKey16++, 2, qKey16++, pErrorCode);
+
+ offset+=((1+count)+1)/2;
+ } else {
+ /* get table item count */
+ pKey32=(const int32_t *)p;
+ qKey32=(int32_t *)q;
+ count=udata_readInt32(ds, *pKey32);
+
+ pKey16=qKey16=NULL;
+
+ /* swap count */
+ ds->swapArray32(ds, pKey32++, 4, qKey32++, pErrorCode);
+
+ offset+=1+count;
+ }
+
+ if(count==0) {
+ break;
+ }
+
+ p=inBundle+offset; /* pointer to table resources */
+ q=outBundle+offset;
+
+ /* recurse */
+ for(i=0; i<count; ++i) {
+ const char *itemKey=gUnknownKey;
+ if(pKey16!=NULL) {
+ int32_t keyOffset=ds->readUInt16(pKey16[i]);
+ if(keyOffset<pTempTable->localKeyLimit) {
+ itemKey=(const char *)outBundle+keyOffset;
+ }
+ } else {
+ 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, itemKey, pTempTable, pErrorCode);
+ if(U_FAILURE(*pErrorCode)) {
+ udata_printError(ds, "ures_swapResource(table res=%08x)[%d].recurse(%08x) failed\n",
+ res, i, item);
+ return;
+ }
+ }
+
+ 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);
+ ds->swapArray32(ds, p, count*4, q, pErrorCode);
+ } else {
+ /* swap key offsets and items as one array */
+ ds->swapArray32(ds, pKey32, count*2*4, qKey32, pErrorCode);
+ }
+ break;
+ }
+
+ /*
+ * We need to sort tables by outCharset key strings because they
+ * sort differently for different charset families.
+ * ures_swap() already set pTempTable->keyChars appropriately.
+ * First we set up a temporary table with the key indexes and
+ * sorting indexes and sort that.
+ * Then we permutate and copy/swap the actual values.
+ */
+ if(pKey16!=NULL) {
+ for(i=0; i<count; ++i) {
+ pTempTable->rows[i].keyIndex=ds->readUInt16(pKey16[i]);
+ pTempTable->rows[i].sortIndex=i;
+ }
+ } else {
+ for(i=0; i<count; ++i) {
+ pTempTable->rows[i].keyIndex=udata_readInt32(ds, pKey32[i]);
+ pTempTable->rows[i].sortIndex=i;
+ }
+ }
+ uprv_sortArray(pTempTable->rows, count, sizeof(Row),
+ ures_compareRows, pTempTable->keyChars,
+ FALSE, pErrorCode);
+ if(U_FAILURE(*pErrorCode)) {
+ udata_printError(ds, "ures_swapResource(table res=%08x).uprv_sortArray(%d items) failed\n",
+ res, count);
+ return;
+ }
+
+ /*
+ * copy/swap/permutate items
+ *
+ * If we swap in-place, then the permutation must use another
+ * temporary array (pTempTable->resort)
+ * before the results are copied to the outBundle.
+ */
+ /* keys */
+ if(pKey16!=NULL) {
+ uint16_t *rKey16;
+
+ if(pKey16!=qKey16) {
+ rKey16=qKey16;
+ } else {
+ rKey16=(uint16_t *)pTempTable->resort;
+ }
+ for(i=0; i<count; ++i) {
+ oldIndex=pTempTable->rows[i].sortIndex;
+ ds->swapArray16(ds, pKey16+oldIndex, 2, rKey16+i, pErrorCode);
+ }
+ if(qKey16!=rKey16) {
+ uprv_memcpy(qKey16, rKey16, 2*count);
+ }
+ } else {
+ int32_t *rKey32;
+
+ if(pKey32!=qKey32) {
+ rKey32=qKey32;
+ } else {
+ rKey32=pTempTable->resort;
+ }
+ for(i=0; i<count; ++i) {
+ oldIndex=pTempTable->rows[i].sortIndex;
+ ds->swapArray32(ds, pKey32+oldIndex, 4, rKey32+i, pErrorCode);
+ }
+ if(qKey32!=rKey32) {
+ uprv_memcpy(qKey32, rKey32, 4*count);
+ }
+ }
+
+ /* resources */
+ {
+ Resource *r;
+
+
+ if(p!=q) {
+ r=q;
+ } else {
+ r=(Resource *)pTempTable->resort;
+ }
+ for(i=0; i<count; ++i) {
+ oldIndex=pTempTable->rows[i].sortIndex;
+ ds->swapArray32(ds, p+oldIndex, 4, r+i, pErrorCode);
+ }
+ if(q!=r) {
+ uprv_memcpy(q, r, 4*count);
+ }
+ }
+ }
+ break;
+ case URES_ARRAY:
+ {
+ Resource item;
+ int32_t i;
+
+ count=udata_readInt32(ds, (int32_t)*p);
+ /* swap length */
+ ds->swapArray32(ds, p++, 4, q++, pErrorCode);
+
+ /* recurse */
+ for(i=0; i<count; ++i) {
+ item=ds->readUInt32(p[i]);
+ 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\n",
+ res, i, item);
+ return;
+ }
+ }
+
+ /* swap items */
+ ds->swapArray32(ds, p, 4*count, q, pErrorCode);
+ }
+ break;
+ case URES_INT_VECTOR:
+ count=udata_readInt32(ds, (int32_t)*p);
+ /* swap length and each integer */
+ ds->swapArray32(ds, p, 4*(1+count), q, pErrorCode);
+ break;
+ default:
+ /* also catches RES_BOGUS */
+ *pErrorCode=U_UNSUPPORTED_ERROR;
+ break;