- for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
- out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
- }
- if ( i == len ) {
- out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
- out[outpos] = '\0';
- return out;
+ if ( casefold ) {
+ outsize = len + 7;
+ out = (char *) malloc( outsize );
+ if ( out == NULL ) {
+ return NULL;
+ }
+ outpos = 0;
+
+ for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
+ out[outpos++] = TOLOWER( s[i-1] );
+ }
+ if ( i == len ) {
+ out[outpos++] = TOLOWER( s[len-1] );
+ out[outpos] = '\0';
+ return ber_str2bv( out, outpos, 0, newbv);
+ }
+ } else {
+ for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
+ /* empty */
+ }
+
+ if ( i == len ) {
+ return ber_str2bv_x( s, len, 1, newbv, ctx );
+ }
+
+ outsize = len + 7;
+ out = (char *) malloc( outsize );
+ if ( out == NULL ) {
+ return NULL;
+ }
+ outpos = i - 1;
+ memcpy(out, s, outpos);