+// 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,
+ const void *inData, int32_t length, void *outData,
+ UErrorCode *pErrorCode) {
+ const UDataInfo *pInfo;
+ int32_t headerSize;
+
+ const uint8_t *inBytes;
+ uint8_t *outBytes;
+
+ int32_t offset;
+
+ /* 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", pErrorCode != NULL ? u_errorName(*pErrorCode) : "pErrorCode is NULL");
+ return 0;
+ }
+
+ /* check data format and format version */
+ pInfo=(const UDataInfo *)((const char *)inData+4);
+ if(!(
+ pInfo->dataFormat[0]==0x54 && /* dataFormat="Norm" */
+ pInfo->dataFormat[1]==0x65 &&
+ pInfo->dataFormat[2]==0x73 &&
+ pInfo->dataFormat[3]==0x74 &&
+ pInfo->formatVersion[0]==1
+ )) {
+ udata_printError(ds, "test_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as testdata\n",
+ pInfo->dataFormat[0], pInfo->dataFormat[1],
+ pInfo->dataFormat[2], pInfo->dataFormat[3],
+ pInfo->formatVersion[0]);
+ *pErrorCode=U_UNSUPPORTED_ERROR;
+ return 0;
+ }
+
+ inBytes=(const uint8_t *)inData+headerSize;
+ outBytes=(uint8_t *)outData+headerSize;
+
+ int32_t size16 = 2; // 16bit plus padding
+ int32_t sizeStr = 5; // 4 char inv-str plus null
+ int32_t size = size16 + sizeStr;
+
+ if(length>=0) {
+ if(length<size) {
+ udata_printError(ds, "test_swap(): too few bytes (%d after header, wanted %d) for all of testdata\n",
+ length, size);
+ *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
+ return 0;
+ }
+
+ offset =0;
+ /* swap a 1 entry array */
+ ds->swapArray16(ds, inBytes+offset, size16, outBytes+offset, pErrorCode);
+ offset+=size16;
+ ds->swapInvChars(ds, inBytes+offset, sizeStr, outBytes+offset, pErrorCode);
+ }
+
+ return headerSize+size;
+}
+