michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1999-2010, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: uinvchar.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:2 michael@0: * michael@0: * created on: 2004sep14 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Functions for handling invariant characters, moved here from putil.c michael@0: * for better modularization. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/ustring.h" michael@0: #include "udataswp.h" michael@0: #include "cstring.h" michael@0: #include "cmemory.h" michael@0: #include "uassert.h" michael@0: #include "uinvchar.h" michael@0: michael@0: /* invariant-character handling --------------------------------------------- */ michael@0: michael@0: /* michael@0: * These maps for ASCII to/from EBCDIC map invariant characters (see utypes.h) michael@0: * appropriately for most EBCDIC codepages. michael@0: * michael@0: * They currently also map most other ASCII graphic characters, michael@0: * appropriately for codepages 37 and 1047. michael@0: * Exceptions: The characters for []^ have different codes in 37 & 1047. michael@0: * Both versions are mapped to ASCII. michael@0: * michael@0: * ASCII 37 1047 michael@0: * [ 5B BA AD michael@0: * ] 5D BB BD michael@0: * ^ 5E B0 5F michael@0: * michael@0: * There are no mappings for variant characters from Unicode to EBCDIC. michael@0: * michael@0: * Currently, C0 control codes are also included in these maps. michael@0: * Exceptions: S/390 Open Edition swaps LF and NEL codes compared with other michael@0: * EBCDIC platforms; both codes (15 and 25) are mapped to ASCII LF (0A), michael@0: * but there is no mapping for ASCII LF back to EBCDIC. michael@0: * michael@0: * ASCII EBCDIC S/390-OE michael@0: * LF 0A 25 15 michael@0: * NEL 85 15 25 michael@0: * michael@0: * The maps below explicitly exclude the variant michael@0: * control and graphical characters that are in ASCII-based michael@0: * codepages at 0x80 and above. michael@0: * "No mapping" is expressed by mapping to a 00 byte. michael@0: * michael@0: * These tables do not establish a converter or a codepage. michael@0: */ michael@0: michael@0: static const uint8_t asciiFromEbcdic[256]={ michael@0: 0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, michael@0: 0x10, 0x11, 0x12, 0x13, 0x00, 0x0a, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x1c, 0x1d, 0x1e, 0x1f, michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x17, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, michael@0: 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1a, michael@0: michael@0: 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, michael@0: 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, michael@0: 0x2d, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, michael@0: michael@0: 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x00, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x00, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x5b, 0x00, 0x00, michael@0: 0x5e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5b, 0x5d, 0x00, 0x5d, 0x00, 0x00, michael@0: michael@0: 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x5c, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 michael@0: }; michael@0: michael@0: static const uint8_t ebcdicFromAscii[256]={ michael@0: 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, michael@0: 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, michael@0: 0x40, 0x00, 0x7f, 0x00, 0x00, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, michael@0: 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, michael@0: michael@0: 0x00, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, michael@0: 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x00, 0x00, 0x00, 0x00, 0x6d, michael@0: 0x00, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, michael@0: 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0x00, 0x00, 0x00, 0x00, 0x07, michael@0: michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 michael@0: }; michael@0: michael@0: /* Same as asciiFromEbcdic[] except maps all letters to lowercase. */ michael@0: static const uint8_t lowercaseAsciiFromEbcdic[256]={ michael@0: 0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, michael@0: 0x10, 0x11, 0x12, 0x13, 0x00, 0x0a, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x1c, 0x1d, 0x1e, 0x1f, michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x17, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, michael@0: 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1a, michael@0: michael@0: 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, michael@0: 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, michael@0: 0x2d, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, michael@0: michael@0: 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x00, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x00, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x5b, 0x00, 0x00, michael@0: 0x5e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5b, 0x5d, 0x00, 0x5d, 0x00, 0x00, michael@0: michael@0: 0x7b, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x7d, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x7c, 0x00, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, michael@0: 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 michael@0: }; michael@0: michael@0: /* michael@0: * Bit sets indicating which characters of the ASCII repertoire michael@0: * (by ASCII/Unicode code) are "invariant". michael@0: * See utypes.h for more details. michael@0: * michael@0: * As invariant are considered the characters of the ASCII repertoire except michael@0: * for the following: michael@0: * 21 '!' michael@0: * 23 '#' michael@0: * 24 '$' michael@0: * michael@0: * 40 '@' michael@0: * michael@0: * 5b '[' michael@0: * 5c '\' michael@0: * 5d ']' michael@0: * 5e '^' michael@0: * michael@0: * 60 '`' michael@0: * michael@0: * 7b '{' michael@0: * 7c '|' michael@0: * 7d '}' michael@0: * 7e '~' michael@0: */ michael@0: static const uint32_t invariantChars[4]={ michael@0: 0xfffffbff, /* 00..1f but not 0a */ michael@0: 0xffffffe5, /* 20..3f but not 21 23 24 */ michael@0: 0x87fffffe, /* 40..5f but not 40 5b..5e */ michael@0: 0x87fffffe /* 60..7f but not 60 7b..7e */ michael@0: }; michael@0: michael@0: /* michael@0: * test unsigned types (or values known to be non-negative) for invariant characters, michael@0: * tests ASCII-family character values michael@0: */ michael@0: #define UCHAR_IS_INVARIANT(c) (((c)<=0x7f) && (invariantChars[(c)>>5]&((uint32_t)1<<((c)&0x1f)))!=0) michael@0: michael@0: /* test signed types for invariant characters, adds test for positive values */ michael@0: #define SCHAR_IS_INVARIANT(c) ((0<=(c)) && UCHAR_IS_INVARIANT(c)) michael@0: michael@0: #if U_CHARSET_FAMILY==U_ASCII_FAMILY michael@0: #define CHAR_TO_UCHAR(c) c michael@0: #define UCHAR_TO_CHAR(c) c michael@0: #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY michael@0: #define CHAR_TO_UCHAR(u) asciiFromEbcdic[u] michael@0: #define UCHAR_TO_CHAR(u) ebcdicFromAscii[u] michael@0: #else michael@0: # error U_CHARSET_FAMILY is not valid michael@0: #endif michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: u_charsToUChars(const char *cs, UChar *us, int32_t length) { michael@0: UChar u; michael@0: uint8_t c; michael@0: michael@0: /* michael@0: * Allow the entire ASCII repertoire to be mapped _to_ Unicode. michael@0: * For EBCDIC systems, this works for characters with codes from michael@0: * codepages 37 and 1047 or compatible. michael@0: */ michael@0: while(length>0) { michael@0: c=(uint8_t)(*cs++); michael@0: u=(UChar)CHAR_TO_UCHAR(c); michael@0: U_ASSERT((u!=0 || c==0)); /* only invariant chars converted? */ michael@0: *us++=u; michael@0: --length; michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: u_UCharsToChars(const UChar *us, char *cs, int32_t length) { michael@0: UChar u; michael@0: michael@0: while(length>0) { michael@0: u=*us++; michael@0: if(!UCHAR_IS_INVARIANT(u)) { michael@0: U_ASSERT(FALSE); /* Variant characters were used. These are not portable in ICU. */ michael@0: u=0; michael@0: } michael@0: *cs++=(char)UCHAR_TO_CHAR(u); michael@0: --length; michael@0: } michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uprv_isInvariantString(const char *s, int32_t length) { michael@0: uint8_t c; michael@0: michael@0: for(;;) { michael@0: if(length<0) { michael@0: /* NUL-terminated */ michael@0: c=(uint8_t)*s++; michael@0: if(c==0) { michael@0: break; michael@0: } michael@0: } else { michael@0: /* count length */ michael@0: if(length==0) { michael@0: break; michael@0: } michael@0: --length; michael@0: c=(uint8_t)*s++; michael@0: if(c==0) { michael@0: continue; /* NUL is invariant */ michael@0: } michael@0: } michael@0: /* c!=0 now, one branch below checks c==0 for variant characters */ michael@0: michael@0: /* michael@0: * no assertions here because these functions are legitimately called michael@0: * for strings with variant characters michael@0: */ michael@0: #if U_CHARSET_FAMILY==U_ASCII_FAMILY michael@0: if(!UCHAR_IS_INVARIANT(c)) { michael@0: return FALSE; /* found a variant char */ michael@0: } michael@0: #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY michael@0: c=CHAR_TO_UCHAR(c); michael@0: if(c==0 || !UCHAR_IS_INVARIANT(c)) { michael@0: return FALSE; /* found a variant char */ michael@0: } michael@0: #else michael@0: # error U_CHARSET_FAMILY is not valid michael@0: #endif michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uprv_isInvariantUString(const UChar *s, int32_t length) { michael@0: UChar c; michael@0: michael@0: for(;;) { michael@0: if(length<0) { michael@0: /* NUL-terminated */ michael@0: c=*s++; michael@0: if(c==0) { michael@0: break; michael@0: } michael@0: } else { michael@0: /* count length */ michael@0: if(length==0) { michael@0: break; michael@0: } michael@0: --length; michael@0: c=*s++; michael@0: } michael@0: michael@0: /* michael@0: * no assertions here because these functions are legitimately called michael@0: * for strings with variant characters michael@0: */ michael@0: if(!UCHAR_IS_INVARIANT(c)) { michael@0: return FALSE; /* found a variant char */ michael@0: } michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: /* UDataSwapFn implementations used in udataswp.c ------- */ michael@0: michael@0: /* convert ASCII to EBCDIC and verify that all characters are invariant */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uprv_ebcdicFromAscii(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const uint8_t *s; michael@0: uint8_t *t; michael@0: uint8_t c; michael@0: michael@0: int32_t count; 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: /* setup and swapping */ michael@0: s=(const uint8_t *)inData; michael@0: t=(uint8_t *)outData; michael@0: count=length; michael@0: while(count>0) { michael@0: c=*s++; michael@0: if(!UCHAR_IS_INVARIANT(c)) { michael@0: udata_printError(ds, "uprv_ebcdicFromAscii() string[%d] contains a variant character in position %d\n", michael@0: length, length-count); michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: *t++=ebcdicFromAscii[c]; michael@0: --count; michael@0: } michael@0: michael@0: return length; michael@0: } michael@0: michael@0: /* this function only checks and copies ASCII strings without conversion */ michael@0: U_CFUNC int32_t michael@0: uprv_copyAscii(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const uint8_t *s; michael@0: uint8_t c; michael@0: michael@0: int32_t count; 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: /* setup and checking */ michael@0: s=(const uint8_t *)inData; michael@0: count=length; michael@0: while(count>0) { michael@0: c=*s++; michael@0: if(!UCHAR_IS_INVARIANT(c)) { michael@0: udata_printError(ds, "uprv_copyFromAscii() string[%d] contains a variant character in position %d\n", michael@0: length, length-count); michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: --count; michael@0: } michael@0: michael@0: if(length>0 && inData!=outData) { michael@0: uprv_memcpy(outData, inData, length); michael@0: } michael@0: michael@0: return length; michael@0: } michael@0: michael@0: /* convert EBCDIC to ASCII and verify that all characters are invariant */ michael@0: U_CFUNC int32_t michael@0: uprv_asciiFromEbcdic(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const uint8_t *s; michael@0: uint8_t *t; michael@0: uint8_t c; michael@0: michael@0: int32_t count; 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: /* setup and swapping */ michael@0: s=(const uint8_t *)inData; michael@0: t=(uint8_t *)outData; michael@0: count=length; michael@0: while(count>0) { michael@0: c=*s++; michael@0: if(c!=0 && ((c=asciiFromEbcdic[c])==0 || !UCHAR_IS_INVARIANT(c))) { michael@0: udata_printError(ds, "uprv_asciiFromEbcdic() string[%d] contains a variant character in position %d\n", michael@0: length, length-count); michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: *t++=c; michael@0: --count; michael@0: } michael@0: michael@0: return length; michael@0: } michael@0: michael@0: /* this function only checks and copies EBCDIC strings without conversion */ michael@0: U_CFUNC int32_t michael@0: uprv_copyEbcdic(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const uint8_t *s; michael@0: uint8_t c; michael@0: michael@0: int32_t count; 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: /* setup and checking */ michael@0: s=(const uint8_t *)inData; michael@0: count=length; michael@0: while(count>0) { michael@0: c=*s++; michael@0: if(c!=0 && ((c=asciiFromEbcdic[c])==0 || !UCHAR_IS_INVARIANT(c))) { michael@0: udata_printError(ds, "uprv_copyEbcdic() string[%] contains a variant character in position %d\n", michael@0: length, length-count); michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: --count; michael@0: } michael@0: michael@0: if(length>0 && inData!=outData) { michael@0: uprv_memcpy(outData, inData, length); michael@0: } michael@0: michael@0: return length; michael@0: } michael@0: michael@0: /* compare invariant strings; variant characters compare less than others and unlike each other */ michael@0: U_CFUNC int32_t michael@0: uprv_compareInvAscii(const UDataSwapper *ds, michael@0: const char *outString, int32_t outLength, michael@0: const UChar *localString, int32_t localLength) { michael@0: int32_t minLength; michael@0: UChar32 c1, c2; michael@0: uint8_t c; michael@0: michael@0: if(outString==NULL || outLength<-1 || localString==NULL || localLength<-1) { michael@0: return 0; michael@0: } michael@0: michael@0: if(outLength<0) { michael@0: outLength=(int32_t)uprv_strlen(outString); michael@0: } michael@0: if(localLength<0) { michael@0: localLength=u_strlen(localString); michael@0: } michael@0: michael@0: minLength= outLength0) { michael@0: c=(uint8_t)*outString++; michael@0: if(UCHAR_IS_INVARIANT(c)) { michael@0: c1=c; michael@0: } else { michael@0: c1=-1; michael@0: } michael@0: michael@0: c2=*localString++; michael@0: if(!UCHAR_IS_INVARIANT(c2)) { michael@0: c2=-2; michael@0: } michael@0: michael@0: if((c1-=c2)!=0) { michael@0: return c1; michael@0: } michael@0: michael@0: --minLength; michael@0: } michael@0: michael@0: /* strings start with same prefix, compare lengths */ michael@0: return outLength-localLength; michael@0: } michael@0: michael@0: U_CFUNC int32_t michael@0: uprv_compareInvEbcdic(const UDataSwapper *ds, michael@0: const char *outString, int32_t outLength, michael@0: const UChar *localString, int32_t localLength) { michael@0: int32_t minLength; michael@0: UChar32 c1, c2; michael@0: uint8_t c; michael@0: michael@0: if(outString==NULL || outLength<-1 || localString==NULL || localLength<-1) { michael@0: return 0; michael@0: } michael@0: michael@0: if(outLength<0) { michael@0: outLength=(int32_t)uprv_strlen(outString); michael@0: } michael@0: if(localLength<0) { michael@0: localLength=u_strlen(localString); michael@0: } michael@0: michael@0: minLength= outLength0) { michael@0: c=(uint8_t)*outString++; michael@0: if(c==0) { michael@0: c1=0; michael@0: } else if((c1=asciiFromEbcdic[c])!=0 && UCHAR_IS_INVARIANT(c1)) { michael@0: /* c1 is set */ michael@0: } else { michael@0: c1=-1; michael@0: } michael@0: michael@0: c2=*localString++; michael@0: if(!UCHAR_IS_INVARIANT(c2)) { michael@0: c2=-2; michael@0: } michael@0: michael@0: if((c1-=c2)!=0) { michael@0: return c1; michael@0: } michael@0: michael@0: --minLength; michael@0: } michael@0: michael@0: /* strings start with same prefix, compare lengths */ michael@0: return outLength-localLength; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uprv_compareInvEbcdicAsAscii(const char *s1, const char *s2) { michael@0: int32_t c1, c2; michael@0: michael@0: for(;; ++s1, ++s2) { michael@0: c1=(uint8_t)*s1; michael@0: c2=(uint8_t)*s2; michael@0: if(c1!=c2) { michael@0: if(c1!=0 && ((c1=asciiFromEbcdic[c1])==0 || !UCHAR_IS_INVARIANT(c1))) { michael@0: c1=-(int32_t)(uint8_t)*s1; michael@0: } michael@0: if(c2!=0 && ((c2=asciiFromEbcdic[c2])==0 || !UCHAR_IS_INVARIANT(c2))) { michael@0: c2=-(int32_t)(uint8_t)*s2; michael@0: } michael@0: return c1-c2; michael@0: } else if(c1==0) { michael@0: return 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: U_CAPI char U_EXPORT2 michael@0: uprv_ebcdicToLowercaseAscii(char c) { michael@0: return (char)lowercaseAsciiFromEbcdic[(uint8_t)c]; michael@0: } michael@0: michael@0: U_INTERNAL uint8_t* U_EXPORT2 michael@0: uprv_aestrncpy(uint8_t *dst, const uint8_t *src, int32_t n) michael@0: { michael@0: uint8_t *orig_dst = dst; michael@0: michael@0: if(n==-1) { michael@0: n = uprv_strlen((const char*)src)+1; /* copy NUL */ michael@0: } michael@0: /* copy non-null */ michael@0: while(*src && n>0) { michael@0: *(dst++) = asciiFromEbcdic[*(src++)]; michael@0: n--; michael@0: } michael@0: /* pad */ michael@0: while(n>0) { michael@0: *(dst++) = 0; michael@0: n--; michael@0: } michael@0: return orig_dst; michael@0: } michael@0: michael@0: U_INTERNAL uint8_t* U_EXPORT2 michael@0: uprv_eastrncpy(uint8_t *dst, const uint8_t *src, int32_t n) michael@0: { michael@0: uint8_t *orig_dst = dst; michael@0: michael@0: if(n==-1) { michael@0: n = uprv_strlen((const char*)src)+1; /* copy NUL */ michael@0: } michael@0: /* copy non-null */ michael@0: while(*src && n>0) { michael@0: char ch = ebcdicFromAscii[*(src++)]; michael@0: if(ch == 0) { michael@0: ch = ebcdicFromAscii[0x3f]; /* questionmark (subchar) */ michael@0: } michael@0: *(dst++) = ch; michael@0: n--; michael@0: } michael@0: /* pad */ michael@0: while(n>0) { michael@0: *(dst++) = 0; michael@0: n--; michael@0: } michael@0: return orig_dst; michael@0: } michael@0: