michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * utf8.c michael@0: * michael@0: * This file contains some additional utility routines required for michael@0: * handling UTF8 strings. michael@0: */ michael@0: michael@0: #ifndef BASE_H michael@0: #include "base.h" michael@0: #endif /* BASE_H */ michael@0: michael@0: #include "plstr.h" michael@0: michael@0: /* michael@0: * NOTES: michael@0: * michael@0: * There's an "is hex string" function in pki1/atav.c. If we need michael@0: * it in more places, pull that one out. michael@0: */ michael@0: michael@0: /* michael@0: * nssUTF8_CaseIgnoreMatch michael@0: * michael@0: * Returns true if the two UTF8-encoded strings pointed to by the michael@0: * two specified NSSUTF8 pointers differ only in typcase. michael@0: * michael@0: * The error may be one of the following values: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * michael@0: * Return value: michael@0: * PR_TRUE if the strings match, ignoring case michael@0: * PR_FALSE if they don't michael@0: * PR_FALSE upon error michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRBool michael@0: nssUTF8_CaseIgnoreMatch michael@0: ( michael@0: const NSSUTF8 *a, michael@0: const NSSUTF8 *b, michael@0: PRStatus *statusOpt michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( ((const NSSUTF8 *)NULL == a) || michael@0: ((const NSSUTF8 *)NULL == b) ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: return PR_FALSE; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_SUCCESS; michael@0: } michael@0: michael@0: /* michael@0: * XXX fgmr michael@0: * michael@0: * This is, like, so wrong! michael@0: */ michael@0: if( 0 == PL_strcasecmp((const char *)a, (const char *)b) ) { michael@0: return PR_TRUE; michael@0: } else { michael@0: return PR_FALSE; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * nssUTF8_PrintableMatch michael@0: * michael@0: * Returns true if the two Printable strings pointed to by the michael@0: * two specified NSSUTF8 pointers match when compared with the michael@0: * rules for Printable String (leading and trailing spaces are michael@0: * disregarded, extents of whitespace match irregardless of length, michael@0: * and case is not significant), then PR_TRUE will be returned. michael@0: * Otherwise, PR_FALSE will be returned. Upon failure, PR_FALSE michael@0: * will be returned. If the optional statusOpt argument is not michael@0: * NULL, then PR_SUCCESS or PR_FAILURE will be stored in that michael@0: * location. michael@0: * michael@0: * The error may be one of the following values: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * michael@0: * Return value: michael@0: * PR_TRUE if the strings match, ignoring case michael@0: * PR_FALSE if they don't michael@0: * PR_FALSE upon error michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRBool michael@0: nssUTF8_PrintableMatch michael@0: ( michael@0: const NSSUTF8 *a, michael@0: const NSSUTF8 *b, michael@0: PRStatus *statusOpt michael@0: ) michael@0: { michael@0: PRUint8 *c; michael@0: PRUint8 *d; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( ((const NSSUTF8 *)NULL == a) || michael@0: ((const NSSUTF8 *)NULL == b) ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: return PR_FALSE; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_SUCCESS; michael@0: } michael@0: michael@0: c = (PRUint8 *)a; michael@0: d = (PRUint8 *)b; michael@0: michael@0: while( ' ' == *c ) { michael@0: c++; michael@0: } michael@0: michael@0: while( ' ' == *d ) { michael@0: d++; michael@0: } michael@0: michael@0: while( ('\0' != *c) && ('\0' != *d) ) { michael@0: PRUint8 e, f; michael@0: michael@0: e = *c; michael@0: f = *d; michael@0: michael@0: if( ('a' <= e) && (e <= 'z') ) { michael@0: e -= ('a' - 'A'); michael@0: } michael@0: michael@0: if( ('a' <= f) && (f <= 'z') ) { michael@0: f -= ('a' - 'A'); michael@0: } michael@0: michael@0: if( e != f ) { michael@0: return PR_FALSE; michael@0: } michael@0: michael@0: c++; michael@0: d++; michael@0: michael@0: if( ' ' == *c ) { michael@0: while( ' ' == *c ) { michael@0: c++; michael@0: } michael@0: c--; michael@0: } michael@0: michael@0: if( ' ' == *d ) { michael@0: while( ' ' == *d ) { michael@0: d++; michael@0: } michael@0: d--; michael@0: } michael@0: } michael@0: michael@0: while( ' ' == *c ) { michael@0: c++; michael@0: } michael@0: michael@0: while( ' ' == *d ) { michael@0: d++; michael@0: } michael@0: michael@0: if( *c == *d ) { michael@0: /* And both '\0', btw */ michael@0: return PR_TRUE; michael@0: } else { michael@0: return PR_FALSE; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * nssUTF8_Duplicate michael@0: * michael@0: * This routine duplicates the UTF8-encoded string pointed to by the michael@0: * specified NSSUTF8 pointer. If the optional arenaOpt argument is michael@0: * not null, the memory required will be obtained from that arena; michael@0: * otherwise, the memory required will be obtained from the heap. michael@0: * A pointer to the new string will be returned. In case of error, michael@0: * an error will be placed on the error stack and NULL will be michael@0: * returned. michael@0: * michael@0: * The error may be one of the following values: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * NSS_ERROR_INVALID_ARENA michael@0: * NSS_ERROR_NO_MEMORY michael@0: */ michael@0: michael@0: NSS_IMPLEMENT NSSUTF8 * michael@0: nssUTF8_Duplicate michael@0: ( michael@0: const NSSUTF8 *s, michael@0: NSSArena *arenaOpt michael@0: ) michael@0: { michael@0: NSSUTF8 *rv; michael@0: PRUint32 len; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( (const NSSUTF8 *)NULL == s ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: michael@0: if( (NSSArena *)NULL != arenaOpt ) { michael@0: if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: len = PL_strlen((const char *)s); michael@0: #ifdef PEDANTIC michael@0: if( '\0' != ((const char *)s)[ len ] ) { michael@0: /* must have wrapped, e.g., too big for PRUint32 */ michael@0: nss_SetError(NSS_ERROR_NO_MEMORY); michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: #endif /* PEDANTIC */ michael@0: len++; /* zero termination */ michael@0: michael@0: rv = nss_ZAlloc(arenaOpt, len); michael@0: if( (void *)NULL == rv ) { michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: michael@0: (void)nsslibc_memcpy(rv, s, len); michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: * nssUTF8_Size michael@0: * michael@0: * This routine returns the length in bytes (including the terminating michael@0: * null) of the UTF8-encoded string pointed to by the specified michael@0: * NSSUTF8 pointer. Zero is returned on error. michael@0: * michael@0: * The error may be one of the following values: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * NSS_ERROR_VALUE_TOO_LARGE michael@0: * michael@0: * Return value: michael@0: * 0 on error michael@0: * nonzero length of the string. michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRUint32 michael@0: nssUTF8_Size michael@0: ( michael@0: const NSSUTF8 *s, michael@0: PRStatus *statusOpt michael@0: ) michael@0: { michael@0: PRUint32 sv; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( (const NSSUTF8 *)NULL == s ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: return 0; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: sv = PL_strlen((const char *)s) + 1; michael@0: #ifdef PEDANTIC michael@0: if( '\0' != ((const char *)s)[ sv-1 ] ) { michael@0: /* wrapped */ michael@0: nss_SetError(NSS_ERROR_VALUE_TOO_LARGE); michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: return 0; michael@0: } michael@0: #endif /* PEDANTIC */ michael@0: michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_SUCCESS; michael@0: } michael@0: michael@0: return sv; michael@0: } michael@0: michael@0: /* michael@0: * nssUTF8_Length michael@0: * michael@0: * This routine returns the length in characters (not including the michael@0: * terminating null) of the UTF8-encoded string pointed to by the michael@0: * specified NSSUTF8 pointer. michael@0: * michael@0: * The error may be one of the following values: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * NSS_ERROR_VALUE_TOO_LARGE michael@0: * NSS_ERROR_INVALID_STRING michael@0: * michael@0: * Return value: michael@0: * length of the string (which may be zero) michael@0: * 0 on error michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRUint32 michael@0: nssUTF8_Length michael@0: ( michael@0: const NSSUTF8 *s, michael@0: PRStatus *statusOpt michael@0: ) michael@0: { michael@0: PRUint32 l = 0; michael@0: const PRUint8 *c = (const PRUint8 *)s; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( (const NSSUTF8 *)NULL == s ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: goto loser; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* michael@0: * From RFC 2044: michael@0: * michael@0: * UCS-4 range (hex.) UTF-8 octet sequence (binary) michael@0: * 0000 0000-0000 007F 0xxxxxxx michael@0: * 0000 0080-0000 07FF 110xxxxx 10xxxxxx michael@0: * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx michael@0: * 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx michael@0: * 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx michael@0: * 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx michael@0: */ michael@0: michael@0: while( 0 != *c ) { michael@0: PRUint32 incr; michael@0: if( (*c & 0x80) == 0 ) { michael@0: incr = 1; michael@0: } else if( (*c & 0xE0) == 0xC0 ) { michael@0: incr = 2; michael@0: } else if( (*c & 0xF0) == 0xE0 ) { michael@0: incr = 3; michael@0: } else if( (*c & 0xF8) == 0xF0 ) { michael@0: incr = 4; michael@0: } else if( (*c & 0xFC) == 0xF8 ) { michael@0: incr = 5; michael@0: } else if( (*c & 0xFE) == 0xFC ) { michael@0: incr = 6; michael@0: } else { michael@0: nss_SetError(NSS_ERROR_INVALID_STRING); michael@0: goto loser; michael@0: } michael@0: michael@0: l += incr; michael@0: michael@0: #ifdef PEDANTIC michael@0: if( l < incr ) { michael@0: /* Wrapped-- too big */ michael@0: nss_SetError(NSS_ERROR_VALUE_TOO_LARGE); michael@0: goto loser; michael@0: } michael@0: michael@0: { michael@0: PRUint8 *d; michael@0: for( d = &c[1]; d < &c[incr]; d++ ) { michael@0: if( (*d & 0xC0) != 0xF0 ) { michael@0: nss_SetError(NSS_ERROR_INVALID_STRING); michael@0: goto loser; michael@0: } michael@0: } michael@0: } michael@0: #endif /* PEDANTIC */ michael@0: michael@0: c += incr; michael@0: } michael@0: michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_SUCCESS; michael@0: } michael@0: michael@0: return l; michael@0: michael@0: loser: michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * nssUTF8_Create michael@0: * michael@0: * This routine creates a UTF8 string from a string in some other michael@0: * format. Some types of string may include embedded null characters, michael@0: * so for them the length parameter must be used. For string types michael@0: * that are null-terminated, the length parameter is optional; if it michael@0: * is zero, it will be ignored. If the optional arena argument is michael@0: * non-null, the memory used for the new string will be obtained from michael@0: * that arena, otherwise it will be obtained from the heap. This michael@0: * routine may return NULL upon error, in which case it will have michael@0: * placed an error on the error stack. michael@0: * michael@0: * The error may be one of the following: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * NSS_ERROR_NO_MEMORY michael@0: * NSS_ERROR_UNSUPPORTED_TYPE michael@0: * michael@0: * Return value: michael@0: * NULL upon error michael@0: * A non-null pointer to a new UTF8 string otherwise michael@0: */ michael@0: michael@0: extern const NSSError NSS_ERROR_INTERNAL_ERROR; /* XXX fgmr */ michael@0: michael@0: NSS_IMPLEMENT NSSUTF8 * michael@0: nssUTF8_Create michael@0: ( michael@0: NSSArena *arenaOpt, michael@0: nssStringType type, michael@0: const void *inputString, michael@0: PRUint32 size /* in bytes, not characters */ michael@0: ) michael@0: { michael@0: NSSUTF8 *rv = NULL; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( (NSSArena *)NULL != arenaOpt ) { michael@0: if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: } michael@0: michael@0: if( (const void *)NULL == inputString ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: switch( type ) { michael@0: case nssStringType_DirectoryString: michael@0: /* This is a composite type requiring BER */ michael@0: nss_SetError(NSS_ERROR_UNSUPPORTED_TYPE); michael@0: break; michael@0: case nssStringType_TeletexString: michael@0: /* michael@0: * draft-ietf-pkix-ipki-part1-11 says in part: michael@0: * michael@0: * In addition, many legacy implementations support names encoded michael@0: * in the ISO 8859-1 character set (Latin1String) but tag them as michael@0: * TeletexString. The Latin1String includes characters used in michael@0: * Western European countries which are not part of the michael@0: * TeletexString charcter set. Implementations that process michael@0: * TeletexString SHOULD be prepared to handle the entire ISO michael@0: * 8859-1 character set.[ISO 8859-1]. michael@0: */ michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_PrintableString: michael@0: /* michael@0: * PrintableString consists of A-Za-z0-9 ,()+,-./:=? michael@0: * This is a subset of ASCII, which is a subset of UTF8. michael@0: * So we can just duplicate the string over. michael@0: */ michael@0: michael@0: if( 0 == size ) { michael@0: rv = nssUTF8_Duplicate((const NSSUTF8 *)inputString, arenaOpt); michael@0: } else { michael@0: rv = nss_ZAlloc(arenaOpt, size+1); michael@0: if( (NSSUTF8 *)NULL == rv ) { michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: michael@0: (void)nsslibc_memcpy(rv, inputString, size); michael@0: } michael@0: michael@0: break; michael@0: case nssStringType_UniversalString: michael@0: /* 4-byte unicode */ michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_BMPString: michael@0: /* Base Multilingual Plane of Unicode */ michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_UTF8String: michael@0: if( 0 == size ) { michael@0: rv = nssUTF8_Duplicate((const NSSUTF8 *)inputString, arenaOpt); michael@0: } else { michael@0: rv = nss_ZAlloc(arenaOpt, size+1); michael@0: if( (NSSUTF8 *)NULL == rv ) { michael@0: return (NSSUTF8 *)NULL; michael@0: } michael@0: michael@0: (void)nsslibc_memcpy(rv, inputString, size); michael@0: } michael@0: michael@0: break; michael@0: case nssStringType_PHGString: michael@0: /* michael@0: * PHGString is an IA5String (with case-insensitive comparisons). michael@0: * IA5 is ~almost~ ascii; ascii has dollar-sign where IA5 has michael@0: * currency symbol. michael@0: */ michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_GeneralString: michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: default: michael@0: nss_SetError(NSS_ERROR_UNSUPPORTED_TYPE); michael@0: break; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NSS_IMPLEMENT NSSItem * michael@0: nssUTF8_GetEncoding michael@0: ( michael@0: NSSArena *arenaOpt, michael@0: NSSItem *rvOpt, michael@0: nssStringType type, michael@0: NSSUTF8 *string michael@0: ) michael@0: { michael@0: NSSItem *rv = (NSSItem *)NULL; michael@0: PRStatus status = PR_SUCCESS; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( (NSSArena *)NULL != arenaOpt ) { michael@0: if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { michael@0: return (NSSItem *)NULL; michael@0: } michael@0: } michael@0: michael@0: if( (NSSUTF8 *)NULL == string ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: return (NSSItem *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: switch( type ) { michael@0: case nssStringType_DirectoryString: michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_TeletexString: michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_PrintableString: michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_UniversalString: michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_BMPString: michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: case nssStringType_UTF8String: michael@0: { michael@0: NSSUTF8 *dup = nssUTF8_Duplicate(string, arenaOpt); michael@0: if( (NSSUTF8 *)NULL == dup ) { michael@0: return (NSSItem *)NULL; michael@0: } michael@0: michael@0: if( (NSSItem *)NULL == rvOpt ) { michael@0: rv = nss_ZNEW(arenaOpt, NSSItem); michael@0: if( (NSSItem *)NULL == rv ) { michael@0: (void)nss_ZFreeIf(dup); michael@0: return (NSSItem *)NULL; michael@0: } michael@0: } else { michael@0: rv = rvOpt; michael@0: } michael@0: michael@0: rv->data = dup; michael@0: dup = (NSSUTF8 *)NULL; michael@0: rv->size = nssUTF8_Size(rv->data, &status); michael@0: if( (0 == rv->size) && (PR_SUCCESS != status) ) { michael@0: if( (NSSItem *)NULL == rvOpt ) { michael@0: (void)nss_ZFreeIf(rv); michael@0: } michael@0: return (NSSItem *)NULL; michael@0: } michael@0: } michael@0: break; michael@0: case nssStringType_PHGString: michael@0: nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ michael@0: break; michael@0: default: michael@0: nss_SetError(NSS_ERROR_UNSUPPORTED_TYPE); michael@0: break; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: * nssUTF8_CopyIntoFixedBuffer michael@0: * michael@0: * This will copy a UTF8 string into a fixed-length buffer, making michael@0: * sure that the all characters are valid. Any remaining space will michael@0: * be padded with the specified ASCII character, typically either michael@0: * null or space. michael@0: * michael@0: * Blah, blah, blah. michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRStatus michael@0: nssUTF8_CopyIntoFixedBuffer michael@0: ( michael@0: NSSUTF8 *string, michael@0: char *buffer, michael@0: PRUint32 bufferSize, michael@0: char pad michael@0: ) michael@0: { michael@0: PRUint32 stringSize = 0; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( (char *)NULL == buffer ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: return PR_FALSE; michael@0: } michael@0: michael@0: if( 0 == bufferSize ) { michael@0: nss_SetError(NSS_ERROR_INVALID_ARGUMENT); michael@0: return PR_FALSE; michael@0: } michael@0: michael@0: if( (pad & 0x80) != 0x00 ) { michael@0: nss_SetError(NSS_ERROR_INVALID_ARGUMENT); michael@0: return PR_FALSE; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( (NSSUTF8 *)NULL == string ) { michael@0: string = (NSSUTF8 *) ""; michael@0: } michael@0: michael@0: stringSize = nssUTF8_Size(string, (PRStatus *)NULL); michael@0: stringSize--; /* don't count the trailing null */ michael@0: if( stringSize > bufferSize ) { michael@0: PRUint32 bs = bufferSize; michael@0: (void)nsslibc_memcpy(buffer, string, bufferSize); michael@0: michael@0: if( ( ((buffer[ bs-1 ] & 0x80) == 0x00)) || michael@0: ((bs > 1) && ((buffer[ bs-2 ] & 0xE0) == 0xC0)) || michael@0: ((bs > 2) && ((buffer[ bs-3 ] & 0xF0) == 0xE0)) || michael@0: ((bs > 3) && ((buffer[ bs-4 ] & 0xF8) == 0xF0)) || michael@0: ((bs > 4) && ((buffer[ bs-5 ] & 0xFC) == 0xF8)) || michael@0: ((bs > 5) && ((buffer[ bs-6 ] & 0xFE) == 0xFC)) ) { michael@0: /* It fit exactly */ michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* Too long. We have to trim the last character */ michael@0: for( /*bs*/; bs != 0; bs-- ) { michael@0: if( (buffer[bs-1] & 0xC0) != 0x80 ) { michael@0: buffer[bs-1] = pad; michael@0: break; michael@0: } else { michael@0: buffer[bs-1] = pad; michael@0: } michael@0: } michael@0: } else { michael@0: (void)nsslibc_memset(buffer, pad, bufferSize); michael@0: (void)nsslibc_memcpy(buffer, string, stringSize); michael@0: } michael@0: michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* michael@0: * nssUTF8_Equal michael@0: * michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRBool michael@0: nssUTF8_Equal michael@0: ( michael@0: const NSSUTF8 *a, michael@0: const NSSUTF8 *b, michael@0: PRStatus *statusOpt michael@0: ) michael@0: { michael@0: PRUint32 la, lb; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( ((const NSSUTF8 *)NULL == a) || michael@0: ((const NSSUTF8 *)NULL == b) ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: return PR_FALSE; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: la = nssUTF8_Size(a, statusOpt); michael@0: if( 0 == la ) { michael@0: return PR_FALSE; michael@0: } michael@0: michael@0: lb = nssUTF8_Size(b, statusOpt); michael@0: if( 0 == lb ) { michael@0: return PR_FALSE; michael@0: } michael@0: michael@0: if( la != lb ) { michael@0: return PR_FALSE; michael@0: } michael@0: michael@0: return nsslibc_memequal(a, b, la, statusOpt); michael@0: }