michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2003, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: udataswp.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2003jun05 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Definitions for ICU data transformations for different platforms, michael@0: * changing between big- and little-endian data and/or between michael@0: * charset families (ASCII<->EBCDIC). michael@0: */ michael@0: michael@0: #include michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/udata.h" /* UDataInfo */ michael@0: #include "ucmndata.h" /* DataHeader */ michael@0: #include "cmemory.h" michael@0: #include "udataswp.h" michael@0: michael@0: /* swapping primitives ------------------------------------------------------ */ michael@0: michael@0: static int32_t U_CALLCONV michael@0: uprv_swapArray16(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const uint16_t *p; michael@0: uint16_t *q; michael@0: int32_t count; michael@0: uint16_t x; michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(ds==NULL || inData==NULL || length<0 || (length&1)!=0 || outData==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: /* setup and swapping */ michael@0: p=(const uint16_t *)inData; michael@0: q=(uint16_t *)outData; michael@0: count=length/2; michael@0: while(count>0) { michael@0: x=*p++; michael@0: *q++=(uint16_t)((x<<8)|(x>>8)); michael@0: --count; michael@0: } michael@0: michael@0: return length; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: uprv_copyArray16(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(ds==NULL || inData==NULL || length<0 || (length&1)!=0 || outData==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: if(length>0 && inData!=outData) { michael@0: uprv_memcpy(outData, inData, length); michael@0: } michael@0: return length; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: uprv_swapArray32(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const uint32_t *p; michael@0: uint32_t *q; michael@0: int32_t count; michael@0: uint32_t x; michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(ds==NULL || inData==NULL || length<0 || (length&3)!=0 || outData==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: /* setup and swapping */ michael@0: p=(const uint32_t *)inData; michael@0: q=(uint32_t *)outData; michael@0: count=length/4; michael@0: while(count>0) { michael@0: x=*p++; michael@0: *q++=(uint32_t)((x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24)); michael@0: --count; michael@0: } michael@0: michael@0: return length; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: uprv_copyArray32(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(ds==NULL || inData==NULL || length<0 || (length&3)!=0 || outData==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: if(length>0 && inData!=outData) { michael@0: uprv_memcpy(outData, inData, length); michael@0: } michael@0: return length; michael@0: } michael@0: michael@0: static uint16_t U_CALLCONV michael@0: uprv_readSwapUInt16(uint16_t x) { michael@0: return (uint16_t)((x<<8)|(x>>8)); michael@0: } michael@0: michael@0: static uint16_t U_CALLCONV michael@0: uprv_readDirectUInt16(uint16_t x) { michael@0: return x; michael@0: } michael@0: michael@0: static uint32_t U_CALLCONV michael@0: uprv_readSwapUInt32(uint32_t x) { michael@0: return (uint32_t)((x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24)); michael@0: } michael@0: michael@0: static uint32_t U_CALLCONV michael@0: uprv_readDirectUInt32(uint32_t x) { michael@0: return x; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: uprv_writeSwapUInt16(uint16_t *p, uint16_t x) { michael@0: *p=(uint16_t)((x<<8)|(x>>8)); michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: uprv_writeDirectUInt16(uint16_t *p, uint16_t x) { michael@0: *p=x; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: uprv_writeSwapUInt32(uint32_t *p, uint32_t x) { michael@0: *p=(uint32_t)((x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24)); michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: uprv_writeDirectUInt32(uint32_t *p, uint32_t x) { michael@0: *p=x; michael@0: } michael@0: michael@0: U_CAPI int16_t U_EXPORT2 michael@0: udata_readInt16(const UDataSwapper *ds, int16_t x) { michael@0: return (int16_t)ds->readUInt16((uint16_t)x); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udata_readInt32(const UDataSwapper *ds, int32_t x) { michael@0: return (int32_t)ds->readUInt32((uint32_t)x); michael@0: } michael@0: michael@0: /** michael@0: * Swap a block of invariant, NUL-terminated strings, but not padding michael@0: * bytes after the last string. michael@0: * @internal michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udata_swapInvStringBlock(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const char *inChars; michael@0: int32_t stringsLength; michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(ds==NULL || inData==NULL || length<0 || (length>0 && outData==NULL)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: /* reduce the strings length to not include bytes after the last NUL */ michael@0: inChars=(const char *)inData; michael@0: stringsLength=length; michael@0: while(stringsLength>0 && inChars[stringsLength-1]!=0) { michael@0: --stringsLength; michael@0: } michael@0: michael@0: /* swap up to the last NUL */ michael@0: ds->swapInvChars(ds, inData, stringsLength, outData, pErrorCode); michael@0: michael@0: /* copy the bytes after the last NUL */ michael@0: if(inData!=outData && length>stringsLength) { michael@0: uprv_memcpy((char *)outData+stringsLength, inChars+stringsLength, length-stringsLength); michael@0: } michael@0: michael@0: /* return the length including padding bytes */ michael@0: if(U_SUCCESS(*pErrorCode)) { michael@0: return length; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udata_printError(const UDataSwapper *ds, michael@0: const char *fmt, michael@0: ...) { michael@0: va_list args; michael@0: michael@0: if(ds->printError!=NULL) { michael@0: va_start(args, fmt); michael@0: ds->printError(ds->printErrorContext, fmt, args); michael@0: va_end(args); michael@0: } michael@0: } michael@0: michael@0: /* swap a data header ------------------------------------------------------- */ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udata_swapDataHeader(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const DataHeader *pHeader; michael@0: uint16_t headerSize, infoSize; michael@0: michael@0: /* argument checking */ michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: /* check minimum length and magic bytes */ michael@0: pHeader=(const DataHeader *)inData; michael@0: if( (length>=0 && lengthdataHeader.magic1!=0xda || michael@0: pHeader->dataHeader.magic2!=0x27 || michael@0: pHeader->info.sizeofUChar!=2 michael@0: ) { michael@0: udata_printError(ds, "udata_swapDataHeader(): initial bytes do not look like ICU data\n"); michael@0: *pErrorCode=U_UNSUPPORTED_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: headerSize=ds->readUInt16(pHeader->dataHeader.headerSize); michael@0: infoSize=ds->readUInt16(pHeader->info.size); michael@0: michael@0: if( headerSizedataHeader)+infoSize) || michael@0: (length>=0 && length0) { michael@0: DataHeader *outHeader; michael@0: const char *s; michael@0: int32_t maxLength; michael@0: michael@0: /* Most of the fields are just bytes and need no swapping. */ michael@0: if(inData!=outData) { michael@0: uprv_memcpy(outData, inData, headerSize); michael@0: } michael@0: outHeader=(DataHeader *)outData; michael@0: michael@0: outHeader->info.isBigEndian = ds->outIsBigEndian; michael@0: outHeader->info.charsetFamily = ds->outCharset; michael@0: michael@0: /* swap headerSize */ michael@0: ds->swapArray16(ds, &pHeader->dataHeader.headerSize, 2, &outHeader->dataHeader.headerSize, pErrorCode); michael@0: michael@0: /* swap UDataInfo size and reservedWord */ michael@0: ds->swapArray16(ds, &pHeader->info.size, 4, &outHeader->info.size, pErrorCode); michael@0: michael@0: /* swap copyright statement after the UDataInfo */ michael@0: infoSize+=sizeof(pHeader->dataHeader); michael@0: s=(const char *)inData+infoSize; michael@0: maxLength=headerSize-infoSize; michael@0: /* get the length of the string */ michael@0: for(length=0; lengthswapInvChars(ds, s, length, (char *)outData+infoSize, pErrorCode); michael@0: } michael@0: michael@0: return headerSize; michael@0: } michael@0: michael@0: /* API functions ------------------------------------------------------------ */ michael@0: michael@0: U_CAPI UDataSwapper * U_EXPORT2 michael@0: udata_openSwapper(UBool inIsBigEndian, uint8_t inCharset, michael@0: UBool outIsBigEndian, uint8_t outCharset, michael@0: UErrorCode *pErrorCode) { michael@0: UDataSwapper *swapper; michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: if(inCharset>U_EBCDIC_FAMILY || outCharset>U_EBCDIC_FAMILY) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: /* allocate the swapper */ michael@0: swapper=uprv_malloc(sizeof(UDataSwapper)); michael@0: if(swapper==NULL) { michael@0: *pErrorCode=U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: uprv_memset(swapper, 0, sizeof(UDataSwapper)); michael@0: michael@0: /* set values and functions pointers according to in/out parameters */ michael@0: swapper->inIsBigEndian=inIsBigEndian; michael@0: swapper->inCharset=inCharset; michael@0: swapper->outIsBigEndian=outIsBigEndian; michael@0: swapper->outCharset=outCharset; michael@0: michael@0: swapper->readUInt16= inIsBigEndian==U_IS_BIG_ENDIAN ? uprv_readDirectUInt16 : uprv_readSwapUInt16; michael@0: swapper->readUInt32= inIsBigEndian==U_IS_BIG_ENDIAN ? uprv_readDirectUInt32 : uprv_readSwapUInt32; michael@0: michael@0: swapper->writeUInt16= outIsBigEndian==U_IS_BIG_ENDIAN ? uprv_writeDirectUInt16 : uprv_writeSwapUInt16; michael@0: swapper->writeUInt32= outIsBigEndian==U_IS_BIG_ENDIAN ? uprv_writeDirectUInt32 : uprv_writeSwapUInt32; michael@0: michael@0: swapper->compareInvChars= outCharset==U_ASCII_FAMILY ? uprv_compareInvAscii : uprv_compareInvEbcdic; michael@0: michael@0: swapper->swapArray16= inIsBigEndian==outIsBigEndian ? uprv_copyArray16 : uprv_swapArray16; michael@0: swapper->swapArray32= inIsBigEndian==outIsBigEndian ? uprv_copyArray32 : uprv_swapArray32; michael@0: michael@0: if(inCharset==U_ASCII_FAMILY) { michael@0: swapper->swapInvChars= outCharset==U_ASCII_FAMILY ? uprv_copyAscii : uprv_ebcdicFromAscii; michael@0: } else /* U_EBCDIC_FAMILY */ { michael@0: swapper->swapInvChars= outCharset==U_EBCDIC_FAMILY ? uprv_copyEbcdic : uprv_asciiFromEbcdic; michael@0: } michael@0: michael@0: return swapper; michael@0: } michael@0: michael@0: U_CAPI UDataSwapper * U_EXPORT2 michael@0: udata_openSwapperForInputData(const void *data, int32_t length, michael@0: UBool outIsBigEndian, uint8_t outCharset, michael@0: UErrorCode *pErrorCode) { michael@0: const DataHeader *pHeader; michael@0: uint16_t headerSize, infoSize; michael@0: UBool inIsBigEndian; michael@0: int8_t inCharset; michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: if( data==NULL || michael@0: (length>=0 && lengthU_EBCDIC_FAMILY michael@0: ) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: pHeader=(const DataHeader *)data; michael@0: if( (length>=0 && lengthdataHeader.magic1!=0xda || michael@0: pHeader->dataHeader.magic2!=0x27 || michael@0: pHeader->info.sizeofUChar!=2 michael@0: ) { michael@0: *pErrorCode=U_UNSUPPORTED_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: inIsBigEndian=(UBool)pHeader->info.isBigEndian; michael@0: inCharset=pHeader->info.charsetFamily; michael@0: michael@0: if(inIsBigEndian==U_IS_BIG_ENDIAN) { michael@0: headerSize=pHeader->dataHeader.headerSize; michael@0: infoSize=pHeader->info.size; michael@0: } else { michael@0: headerSize=uprv_readSwapUInt16(pHeader->dataHeader.headerSize); michael@0: infoSize=uprv_readSwapUInt16(pHeader->info.size); michael@0: } michael@0: michael@0: if( headerSizedataHeader)+infoSize) || michael@0: (length>=0 && length