]> git.sur5r.net Git - openldap/blobdiff - libraries/liblunicode/ucstr.c
Merge remote branch 'origin/mdb.master'
[openldap] / libraries / liblunicode / ucstr.c
index b5a98151d0a2cf3639d16d3cf7f5b2e128fcecd5..1d76834b67a2a1605b242190dc13743e8688ac81 100644 (file)
@@ -1,23 +1,33 @@
-/*
- * Copyright 2000-2002 The OpenLDAP Foundation
- * COPYING RESTRICTIONS APPLY.  See COPYRIGHT File in top level directory
- * of this package for details.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2011 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
 
+#include <ac/bytes.h>
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/stdlib.h>
 
-#include <lber.h>
+#include <lber_pvt.h>
 
 #include <ldap_utf8.h>
 #include <ldap_pvt_uc.h>
 
-#define        malloc(x)       ber_memalloc(x)
-#define        realloc(x,y)    ber_memrealloc(x,y)
-#define        free(x)         ber_memfree(x)
+#define        malloc(x)       ber_memalloc_x(x,ctx)
+#define        realloc(x,y)    ber_memrealloc_x(x,y,ctx)
+#define        free(x)         ber_memfree_x(x,ctx)
 
 int ucstrncmp(
        const ldap_unicode_t *u1,
@@ -41,8 +51,8 @@ int ucstrncasecmp(
        ber_len_t n )
 {
        for(; 0 < n; ++u1, ++u2, --n ) {
-               ldap_unicode_t uu1 = uctoupper( *u1 );
-               ldap_unicode_t uu2 = uctoupper( *u2 );
+               ldap_unicode_t uu1 = uctolower( *u1 );
+               ldap_unicode_t uu2 = uctolower( *u2 );
 
                if( uu1 != uu2 ) {
                        return uu1 < uu2 ? -1 : +1;
@@ -73,9 +83,9 @@ ldap_unicode_t * ucstrncasechr(
        ber_len_t n,
        ldap_unicode_t c )
 {
-       c = uctoupper( c );
+       c = uctolower( c );
        for(; 0 < n; ++u, --n ) {
-               if( uctoupper( *u ) == c ) {
+               if( uctolower( *u ) == c ) {
                        return (ldap_unicode_t *) u;
                }
        }
@@ -95,16 +105,18 @@ void ucstr2upper(
 struct berval * UTF8bvnormalize(
        struct berval *bv,
        struct berval *newbv,
-       unsigned flags )
+       unsigned flags,
+       void *ctx )
 {
        int i, j, len, clen, outpos, ucsoutlen, outsize, last;
-       char *out, *s;
-       unsigned long *ucs, *p, *ucsout;
+       char *out, *outtmp, *s;
+       ac_uint4 *ucs, *p, *ucsout;
+
+       static unsigned char mask[] = {
+               0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
 
        unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
        unsigned approx = flags & LDAP_UTF8_APPROX;
-       static unsigned char mask[] = {
-                0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
 
        if ( bv == NULL ) {
                return NULL;
@@ -114,31 +126,38 @@ struct berval * UTF8bvnormalize(
        len = bv->bv_len;
 
        if ( len == 0 ) {
-               return ber_dupbv( newbv, bv );
+               return ber_dupbv_x( newbv, bv, ctx );
        }
-       
-       /* FIXME: Should first check to see if string is already in
-        * proper normalized form. This is almost as time consuming
-        * as the normalization though.
+
+       if ( !newbv ) {
+               newbv = ber_memalloc_x( sizeof(struct berval), ctx );
+               if ( !newbv ) return NULL;
+       }
+
+       /* Should first check to see if string is already in proper
+        * normalized form. This is almost as time consuming as
+        * the normalization though.
         */
 
        /* finish off everything up to character before first non-ascii */
        if ( LDAP_UTF8_ISASCII( s ) ) {
                if ( casefold ) {
                        outsize = len + 7;
-                       out = (char *) malloc( outsize );
+                       out = (char *) ber_memalloc_x( outsize, ctx );
                        if ( out == NULL ) {
                                return NULL;
                        }
                        outpos = 0;
 
                        for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
-                               out[outpos++] = TOUPPER( s[i-1] );
+                               out[outpos++] = TOLOWER( s[i-1] );
                        }
                        if ( i == len ) {
-                               out[outpos++] = TOUPPER( s[len - 1] );
+                               out[outpos++] = TOLOWER( s[len-1] );
                                out[outpos] = '\0';
-                               return ber_str2bv( out, outpos, 0, newbv);
+                               newbv->bv_val = out;
+                               newbv->bv_len = outpos;
+                               return newbv;
                        }
                } else {
                        for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
@@ -146,11 +165,11 @@ struct berval * UTF8bvnormalize(
                        }
 
                        if ( i == len ) {
-                               return ber_str2bv( s, len, 1, newbv );
+                               return ber_str2bv_x( s, len, 1, newbv, ctx );
                        }
                                
                        outsize = len + 7;
-                       out = (char *) malloc( outsize );
+                       out = (char *) ber_memalloc_x( outsize, ctx );
                        if ( out == NULL ) {
                                return NULL;
                        }
@@ -159,7 +178,7 @@ struct berval * UTF8bvnormalize(
                }
        } else {
                outsize = len + 7;
-               out = (char *) malloc( outsize );
+               out = (char *) ber_memalloc_x( outsize, ctx );
                if ( out == NULL ) {
                        return NULL;
                }
@@ -167,15 +186,15 @@ struct berval * UTF8bvnormalize(
                i = 0;
        }
 
-       p = ucs = (long *) malloc( len * sizeof(*ucs) );
+       p = ucs = ber_memalloc_x( len * sizeof(*ucs), ctx );
        if ( ucs == NULL ) {
-               free(out);
+               ber_memfree_x(out, ctx);
                return NULL;
        }
 
        /* convert character before first non-ascii to ucs-4 */
        if ( i > 0 ) {
-               *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
+               *p = casefold ? TOLOWER( s[i-1] ) : s[i-1];
                p++;
        }
 
@@ -186,8 +205,8 @@ struct berval * UTF8bvnormalize(
                while ( i < len ) {
                        clen = LDAP_UTF8_CHARLEN2( s + i, clen );
                        if ( clen == 0 ) {
-                               free( ucs );
-                               free( out );
+                               ber_memfree_x( ucs, ctx );
+                               ber_memfree_x( out, ctx );
                                return NULL;
                        }
                        if ( clen == 1 ) {
@@ -198,8 +217,8 @@ struct berval * UTF8bvnormalize(
                        i++;
                        for( j = 1; j < clen; j++ ) {
                                if ( (s[i] & 0xc0) != 0x80 ) {
-                                       free( ucs );
-                                       free( out );
+                                       ber_memfree_x( ucs, ctx );
+                                       ber_memfree_x( out, ctx );
                                        return NULL;
                                }
                                *p <<= 6;
@@ -207,12 +226,12 @@ struct berval * UTF8bvnormalize(
                                i++;
                        }
                        if ( casefold ) {
-                               *p = uctoupper( *p );
+                               *p = uctolower( *p );
                        }
                        p++;
-                }
+               }
                /* normalize ucs of length p - ucs */
-               uccanondecomp( ucs, p - ucs, &ucsout, &ucsoutlen );    
+               uccompatdecomp( ucs, p - ucs, &ucsout, &ucsoutlen, ctx );
                if ( approx ) {
                        for ( j = 0; j < ucsoutlen; j++ ) {
                                if ( ucsout[j] < 0x80 ) {
@@ -227,15 +246,21 @@ struct berval * UTF8bvnormalize(
                                   6 bytes and terminator */
                                if ( outsize - outpos < 7 ) {
                                        outsize = ucsoutlen - j + outpos + 6;
-                                       out = (char *) realloc( out, outsize );
-                                       if ( out == NULL ) {
-                                               free( ucs );
+                                       outtmp = (char *) ber_memrealloc_x( out, outsize, ctx );
+                                       if ( outtmp == NULL ) {
+                                               ber_memfree_x( ucsout, ctx );
+                                               ber_memfree_x( ucs, ctx );
+                                               ber_memfree_x( out, ctx );
                                                return NULL;
                                        }
+                                       out = outtmp;
                                }
                                outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
                        }
                }
+
+               ber_memfree_x( ucsout, ctx );
+               ucsout = NULL;
                
                if ( i == len ) {
                        break;
@@ -243,23 +268,38 @@ struct berval * UTF8bvnormalize(
 
                last = i;
 
+               /* Allocate more space in out if necessary */
+               if (len - i >= outsize - outpos) {
+                       outsize += 1 + ((len - i) - (outsize - outpos));
+                       outtmp = (char *) ber_memrealloc_x(out, outsize, ctx);
+                       if (outtmp == NULL) {
+                               ber_memfree_x( ucs, ctx );
+                               ber_memfree_x( out, ctx );
+                               return NULL;
+                       }
+                       out = outtmp;
+               }
+
                /* s[i] is ascii */
                /* finish off everything up to char before next non-ascii */
                for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
-                       out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
+                       out[outpos++] = casefold ? TOLOWER( s[i-1] ) : s[i-1];
                }
                if ( i == len ) {
-                       out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
+                       out[outpos++] = casefold ? TOLOWER( s[len-1] ) : s[len-1];
                        break;
                }
 
                /* convert character before next non-ascii to ucs-4 */
-               *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
+               *ucs = casefold ? TOLOWER( s[i-1] ) : s[i-1];
                p = ucs + 1;
-       }               
-       free( ucs );
+       }
+
+       ber_memfree_x( ucs, ctx );
        out[outpos] = '\0';
-       return ber_str2bv( out, outpos, 0, newbv );
+       newbv->bv_val = out;
+       newbv->bv_len = outpos;
+       return newbv;
 }
 
 /* compare UTF8-strings, optionally ignore casing */
@@ -267,17 +307,20 @@ struct berval * UTF8bvnormalize(
 int UTF8bvnormcmp(
        struct berval *bv1,
        struct berval *bv2,
-       unsigned flags )
+       unsigned flags,
+       void *ctx )
 {
-       int i, l1, l2, len, ulen, res;
+       int i, l1, l2, len, ulen, res = 0;
        char *s1, *s2, *done;
-       unsigned long *ucs, *ucsout1, *ucsout2;
+       ac_uint4 *ucs, *ucsout1, *ucsout2;
+
        unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
        unsigned norm1 = flags & LDAP_UTF8_ARG1NFC;
        unsigned norm2 = flags & LDAP_UTF8_ARG2NFC;
 
        if (bv1 == NULL) {
                return bv2 == NULL ? 0 : -1;
+
        } else if (bv2 == NULL) {
                return 1;
        }
@@ -296,9 +339,9 @@ int UTF8bvnormcmp(
 
        while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
                if (casefold) {
-                       char c1 = TOUPPER(*s1);
-                       char c2 = TOUPPER(*s2);
-                       res = c1 - c2;
+                       char c1 = TOLOWER(*s1);
+                       char c2 = TOLOWER(*s2);
+                       res = c1 - c2;
                } else {
                        res = *s1 - *s2;
                }                       
@@ -310,8 +353,9 @@ int UTF8bvnormcmp(
                                if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
                                        break;
                                }
-                       } else if ((len < l1) && !LDAP_UTF8_ISASCII(s1) ||
-                                  (len < l2) && !LDAP_UTF8_ISASCII(s2)) {
+                       } else if (((len < l1) && !LDAP_UTF8_ISASCII(s1)) ||
+                               ((len < l2) && !LDAP_UTF8_ISASCII(s2)))
+                       {
                                break;
                        }
                        return res;
@@ -338,50 +382,50 @@ int UTF8bvnormcmp(
                l2 -= i - 1;
        }
                        
-       /* FIXME: Should first check to see if strings are already in
+       /* Should first check to see if strings are already in
         * proper normalized form.
         */
-
-       ucs = (long *) malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
+       ucs = malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
        if ( ucs == NULL ) {
                return l1 > l2 ? 1 : -1; /* what to do??? */
        }
        
        /*
         * XXYYZ: we convert to ucs4 even though -llunicode
-        * expects ucs2 in an unsigned long
+        * expects ucs2 in an ac_uint4
         */
        
        /* convert and normalize 1st string */
        for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
-                ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
-                if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
+               ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
+               if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
                        free( ucs );
-                        return -1; /* what to do??? */
-                }
+                       return -1; /* what to do??? */
+               }
                len = LDAP_UTF8_CHARLEN( s1 + i );
        }
 
        if ( norm1 ) {
                ucsout1 = ucs;
                l1 = ulen;
-               ucs = (long *) malloc( l2 * sizeof(*ucs) );
+               ucs = malloc( l2 * sizeof(*ucs) );
                if ( ucs == NULL ) {
+                       free( ucsout1 );
                        return l1 > l2 ? 1 : -1; /* what to do??? */
                }
        } else {
-               uccanondecomp( ucs, ulen, &ucsout1, &l1 );
+               uccompatdecomp( ucs, ulen, &ucsout1, &l1, ctx );
                l1 = uccanoncomp( ucsout1, l1 );
        }
 
        /* convert and normalize 2nd string */
        for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
-                ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
-                if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
+               ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
+               if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
                        free( ucsout1 );
                        free( ucs );
-                        return 1; /* what to do??? */
-                }
+                       return 1; /* what to do??? */
+               }
                len = LDAP_UTF8_CHARLEN( s2 + i );
        }
 
@@ -389,7 +433,7 @@ int UTF8bvnormcmp(
                ucsout2 = ucs;
                l2 = ulen;
        } else {
-               uccanondecomp( ucs, ulen, &ucsout2, &l2 );
+               uccompatdecomp( ucs, ulen, &ucsout2, &l2, ctx );
                l2 = uccanoncomp( ucsout2, l2 );
                free( ucs );
        }