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