]> git.sur5r.net Git - openldap/blobdiff - libraries/liblunicode/ucstr.c
Fix includes for EBCDIC
[openldap] / libraries / liblunicode / ucstr.c
index f0cc6033221fdedb51faf08ce53657652c9f0abf..53b1ed349f01ee6d00acae2f925f6dd13828d9ed 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2002 The OpenLDAP Foundation
+ * Copyright 2000-2003 The OpenLDAP Foundation
  * COPYING RESTRICTIONS APPLY.  See COPYRIGHT File in top level directory
  * of this package for details.
  */
@@ -15,9 +15,9 @@
 #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,
@@ -95,10 +95,11 @@ 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;
+       char *out, *outtmp, *s;
        unsigned long *ucs, *p, *ucsout;
 
        unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
@@ -114,7 +115,7 @@ 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
@@ -146,7 +147,7 @@ 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;
@@ -167,7 +168,7 @@ struct berval * UTF8bvnormalize(
                i = 0;
        }
 
-       p = ucs = (long *) malloc( len * sizeof(*ucs) );
+       p = ucs = malloc( len * sizeof(*ucs) );
        if ( ucs == NULL ) {
                free(out);
                return NULL;
@@ -212,7 +213,7 @@ struct berval * UTF8bvnormalize(
                        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 +228,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 ) {
+                                       outtmp = (char *) realloc( out, outsize );
+                                       if ( outtmp == NULL ) {
+                                               free( out );
                                                free( ucs );
+                                               free( ucsout );
                                                return NULL;
                                        }
+                                       out = outtmp;
                                }
                                outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
                        }
                }
+
+               free( ucsout );
+               ucsout = NULL;
                
                if ( i == len ) {
                        break;
@@ -267,7 +274,8 @@ struct berval * UTF8bvnormalize(
 int UTF8bvnormcmp(
        struct berval *bv1,
        struct berval *bv2,
-       unsigned flags )
+       unsigned flags,
+       void *ctx )
 {
        int i, l1, l2, len, ulen, res = 0;
        char *s1, *s2, *done;
@@ -342,7 +350,7 @@ int UTF8bvnormcmp(
         * 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??? */
        }
@@ -365,12 +373,12 @@ int UTF8bvnormcmp(
        if ( norm1 ) {
                ucsout1 = ucs;
                l1 = ulen;
-               ucs = (long *) malloc( l2 * sizeof(*ucs) );
+               ucs = malloc( l2 * sizeof(*ucs) );
                if ( ucs == NULL ) {
                        return l1 > l2 ? 1 : -1; /* what to do??? */
                }
        } else {
-               uccanondecomp( ucs, ulen, &ucsout1, &l1 );
+               uccompatdecomp( ucs, ulen, &ucsout1, &l1, ctx );
                l1 = uccanoncomp( ucsout1, l1 );
        }
 
@@ -389,7 +397,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 );
        }