ICU-2235 add udata_swapInvStringBlock() and writeUInt16() and writeUInt32()

X-SVN-Rev: 13139
This commit is contained in:
Markus Scherer 2003-09-18 21:40:30 +00:00
parent 695e199ff4
commit bc64b9469c
2 changed files with 105 additions and 0 deletions

View File

@ -143,6 +143,26 @@ uprv_readDirectUInt32(uint32_t x) {
return x;
}
static void U_CALLCONV
uprv_writeSwapUInt16(uint16_t *p, uint16_t x) {
*p=(uint16_t)((x<<8)|(x>>8));
}
static void U_CALLCONV
uprv_writeDirectUInt16(uint16_t *p, uint16_t x) {
*p=x;
}
static void U_CALLCONV
uprv_writeSwapUInt32(uint32_t *p, uint32_t x) {
*p=(uint32_t)((x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24));
}
static void U_CALLCONV
uprv_writeDirectUInt32(uint32_t *p, uint32_t x) {
*p=x;
}
U_CAPI int16_t U_EXPORT2
udata_readInt16(const UDataSwapper *ds, int16_t x) {
return (int16_t)ds->readUInt16((uint16_t)x);
@ -153,6 +173,49 @@ udata_readInt32(const UDataSwapper *ds, int32_t x) {
return (int32_t)ds->readUInt32((uint32_t)x);
}
/**
* Swap a block of invariant, NUL-terminated strings, but not padding
* bytes after the last string.
* @internal
*/
U_CAPI int32_t U_EXPORT2
udata_swapInvStringBlock(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const char *inChars;
int32_t stringsLength;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(ds==NULL || inData==NULL || length<0 || (length>0 && outData==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
/* reduce the strings length to not include bytes after the last NUL */
inChars=(const char *)inData;
stringsLength=length;
while(stringsLength>0 && inChars[stringsLength-1]!=0) {
--stringsLength;
}
/* swap up to the last NUL */
ds->swapInvChars(ds, inData, stringsLength, outData, pErrorCode);
/* copy the bytes after the last NUL */
if(inData!=outData && length>stringsLength) {
uprv_memcpy((char *)outData+stringsLength, inChars+stringsLength, length-stringsLength);
}
/* return the length including padding bytes */
if(U_SUCCESS(*pErrorCode)) {
return length;
} else {
return 0;
}
}
U_CAPI void U_EXPORT2
udata_printError(const UDataSwapper *ds,
const char *fmt,
@ -276,6 +339,9 @@ udata_openSwapper(UBool inIsBigEndian, uint8_t inCharset,
swapper->readUInt16= inIsBigEndian==U_IS_BIG_ENDIAN ? uprv_readDirectUInt16 : uprv_readSwapUInt16;
swapper->readUInt32= inIsBigEndian==U_IS_BIG_ENDIAN ? uprv_readDirectUInt32 : uprv_readSwapUInt32;
swapper->writeUInt16= outIsBigEndian==U_IS_BIG_ENDIAN ? uprv_writeDirectUInt16 : uprv_writeSwapUInt16;
swapper->writeUInt32= outIsBigEndian==U_IS_BIG_ENDIAN ? uprv_writeDirectUInt32 : uprv_writeSwapUInt32;
swapper->compareInvChars= outCharset==U_ASCII_FAMILY ? uprv_compareInvAscii : uprv_compareInvEbcdic;
swapper->swapArray16= inIsBigEndian==outIsBigEndian ? uprv_copyArray16 : uprv_swapArray16;

View File

@ -84,6 +84,20 @@ UDataReadUInt16(uint16_t x);
typedef uint32_t U_CALLCONV
UDataReadUInt32(uint32_t x);
/**
* Convert one uint16_t from platform to input endianness.
* @draft ICU 2.8
*/
typedef void U_CALLCONV
UDataWriteUInt16(uint16_t *p, uint16_t x);
/**
* Convert one uint32_t from platform to input endianness.
* @draft ICU 2.8
*/
typedef void U_CALLCONV
UDataWriteUInt32(uint32_t *p, uint32_t x);
/**
* Compare invariant-character strings, one in the output data and the
* other one caller-provided in Unicode.
@ -132,6 +146,13 @@ struct UDataSwapper {
/** Compare an invariant-character output string with a local one. @draft ICU 2.8 */
UDataCompareInvChars *compareInvChars;
/* basic functions for writing data values */
/** Convert one uint16_t from platform to input endianness. @draft ICU 2.8 */
UDataWriteUInt16 *writeUInt16;
/** Convert one uint32_t from platform to input endianness. @draft ICU 2.8 */
UDataWriteUInt32 *writeUInt32;
/* basic functions for data transformations */
/** Transform an array of 16-bit integers. @draft ICU 2.8 */
@ -187,12 +208,30 @@ udata_swapDataHeader(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode);
/**
* Convert one int16_t from input to platform endianness.
* @draft ICU 2.8
*/
U_CAPI int16_t U_EXPORT2
udata_readInt16(const UDataSwapper *ds, int16_t x);
/**
* Convert one int32_t from input to platform endianness.
* @draft ICU 2.8
*/
U_CAPI int32_t U_EXPORT2
udata_readInt32(const UDataSwapper *ds, int32_t x);
/**
* Swap a block of invariant, NUL-terminated strings, but not padding
* bytes after the last string.
* @internal
*/
U_CAPI int32_t U_EXPORT2
udata_swapInvStringBlock(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode);
U_CAPI void U_EXPORT2
udata_printError(const UDataSwapper *ds,
const char *fmt,