]> git.sur5r.net Git - openldap/blobdiff - libraries/liblunicode/ucstr.c
ITS#6933
[openldap] / libraries / liblunicode / ucstr.c
index a8c65df8616e8de76045f0ad346dd01837e7eb69..1d76834b67a2a1605b242190dc13743e8688ac81 100644 (file)
@@ -1,11 +1,21 @@
-/*
- * Copyright 2000-2003 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>
@@ -100,7 +110,7 @@ struct berval * UTF8bvnormalize(
 {
        int i, j, len, clen, outpos, ucsoutlen, outsize, last;
        char *out, *outtmp, *s;
-       unsigned long *ucs, *p, *ucsout;
+       ac_uint4 *ucs, *p, *ucsout;
 
        static unsigned char mask[] = {
                0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
@@ -118,7 +128,12 @@ struct berval * UTF8bvnormalize(
        if ( len == 0 ) {
                return ber_dupbv_x( newbv, bv, ctx );
        }
-       
+
+       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.
@@ -128,7 +143,7 @@ struct berval * UTF8bvnormalize(
        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;
                        }
@@ -140,7 +155,9 @@ struct berval * UTF8bvnormalize(
                        if ( i == len ) {
                                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++ ) {
@@ -152,7 +169,7 @@ struct berval * UTF8bvnormalize(
                        }
                                
                        outsize = len + 7;
-                       out = (char *) malloc( outsize );
+                       out = (char *) ber_memalloc_x( outsize, ctx );
                        if ( out == NULL ) {
                                return NULL;
                        }
@@ -161,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;
                }
@@ -169,9 +186,9 @@ struct berval * UTF8bvnormalize(
                i = 0;
        }
 
-       p = ucs = 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;
        }
 
@@ -188,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 ) {
@@ -200,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;
@@ -229,11 +246,11 @@ struct berval * UTF8bvnormalize(
                                   6 bytes and terminator */
                                if ( outsize - outpos < 7 ) {
                                        outsize = ucsoutlen - j + outpos + 6;
-                                       outtmp = (char *) realloc( out, outsize );
+                                       outtmp = (char *) ber_memrealloc_x( out, outsize, ctx );
                                        if ( outtmp == NULL ) {
-                                               free( out );
-                                               free( ucs );
-                                               free( ucsout );
+                                               ber_memfree_x( ucsout, ctx );
+                                               ber_memfree_x( ucs, ctx );
+                                               ber_memfree_x( out, ctx );
                                                return NULL;
                                        }
                                        out = outtmp;
@@ -242,7 +259,7 @@ struct berval * UTF8bvnormalize(
                        }
                }
 
-               free( ucsout );
+               ber_memfree_x( ucsout, ctx );
                ucsout = NULL;
                
                if ( i == len ) {
@@ -251,6 +268,18 @@ 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++ ) {
@@ -266,9 +295,11 @@ struct berval * UTF8bvnormalize(
                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 */
@@ -281,7 +312,7 @@ int UTF8bvnormcmp(
 {
        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;
@@ -361,7 +392,7 @@ int UTF8bvnormcmp(
        
        /*
         * 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 */
@@ -379,6 +410,7 @@ int UTF8bvnormcmp(
                l1 = ulen;
                ucs = malloc( l2 * sizeof(*ucs) );
                if ( ucs == NULL ) {
+                       free( ucsout1 );
                        return l1 > l2 ? 1 : -1; /* what to do??? */
                }
        } else {