]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/utf-8.c
More cleanup in ldap_pvt_tls_destroy()
[openldap] / libraries / libldap / utf-8.c
index eadf1de1183b19d32eafccb6f3cab9e9abcdc553..542814410acf7ddaf9035d6e18b0d01d97c6627c 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
 #include <ac/string.h>
 #include <ac/time.h>
 
+#include "ldap_utf8.h"
+
 #include "ldap-int.h"
 #include "ldap_defaults.h"
 
-#undef ISASCII
-#define ISASCII(uc)    ((uc) < 0x100)
-#undef UCS4_INVALID
-#define UCS4_INVALID   0x80000000U
-
 /*
  * Basic UTF-8 routines
  */
 
 /*
  * return the number of bytes required to hold the
- * NULL-terminated UTF-8 string INCLUDING the
+ * NULL-terminated UTF-8 string NOT INCLUDING the
  * termination.
  */
 ber_len_t ldap_utf8_bytes( const char * p )
 {
-       ber_len_t bytes = 0;
-
-       if( p == NULL ) return bytes;
+       ber_len_t bytes;
 
-       while( p[bytes++] ) {
+       for( bytes=0; p[bytes]; bytes++ ) {
                /* EMPTY */ ;
        }
 
@@ -60,68 +55,60 @@ ber_len_t ldap_utf8_chars( const char * p )
        /* could be optimized and could check for invalid sequences */
        ber_len_t chars=0;
 
-       for( ; *p ; p=LDAP_UTF8_NEXT(p) ) {
+       for( ; *p ; LDAP_UTF8_INCR(p) ) {
                chars++;
-       };
+       }
 
        return chars;
 }
 
+/* return offset to next character */
+int ldap_utf8_offset( const char * p )
+{
+       return LDAP_UTF8_NEXT(p) - p;
+}
+
 /*
  * Returns length indicated by first byte.
  *
  * This function should use a table lookup.
  */
+const char ldap_utf8_lentab[] = {
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+       4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 };
+
 int ldap_utf8_charlen( const char * p )
 {
-       unsigned c = * (const unsigned char *) p;
-
-       if ((c & 0xfe ) == 0xfc) {
-               return 6;
-       }
-
-       if ((c & 0xfc ) == 0xf8) {
-               return 5;
-       }
-
-       if ((c & 0xf8 ) == 0xf0) {
-               return 4;
-       }
-
-       if ((c & 0xf0 ) == 0xe0) {
-               return 3;
-       }
-
-       if ((c & 0xe0 ) == 0xc0) {
-               return 2;
-       }
-
-       if ((c & 0x80 ) == 0x80) {
-               /* INVALID */
-               return 0;
-       }
+       if (!(*p & 0x80))
+               return 1;
 
-       return 1;
+       return ldap_utf8_lentab[*(unsigned char *)p ^ 0x80];
 }
 
 /* conv UTF-8 to UCS-4, useful for comparisons */
-ber_int_t ldap_utf8_to_ucs4( const char * p )
+ldap_ucs4_t ldap_x_utf8_to_ucs4( const char * p )
 {
     const unsigned char *c = p;
-    ber_int_t ch;
+    ldap_ucs4_t ch;
        int len, i;
        static unsigned char mask[] = {
-               0, 0x7f, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
+               0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
 
        len = LDAP_UTF8_CHARLEN(p);
 
-       if( len == 0 ) return UCS4_INVALID;
+       if( len == 0 ) return LDAP_UCS4_INVALID;
 
        ch = c[0] & mask[len];
 
        for(i=1; i < len; i++) {
                if ((c[i] & 0xc0) != 0x80) {
-                       return UCS4_INVALID;
+                       return LDAP_UCS4_INVALID;
                }
 
                ch <<= 6;
@@ -132,7 +119,7 @@ ber_int_t ldap_utf8_to_ucs4( const char * p )
 }
 
 /* conv UCS-4 to UTF-8, not used */
-int ldap_ucs4_to_utf8( ber_int_t c, char *buf )
+int ldap_x_ucs4_to_utf8( ldap_ucs4_t c, char *buf )
 {
        int len=0;
        unsigned char* p = buf;
@@ -146,33 +133,33 @@ int ldap_ucs4_to_utf8( ber_int_t c, char *buf )
 
        } else if( c < 0x800 ) {
                p[len++] = 0xc0 | ( c >> 6 );
-               p[len++] = 0x80 | ( c & 0x3F );
+               p[len++] = 0x80 | ( c & 0x3f );
 
        } else if( c < 0x10000 ) {
                p[len++] = 0xe0 | ( c >> 12 );
-               p[len++] = 0x80 | ( (c >> 6) & 0x3F );
-               p[len++] = 0x80 | ( c & 0x3F );
+               p[len++] = 0x80 | ( (c >> 6) & 0x3f );
+               p[len++] = 0x80 | ( c & 0x3f );
 
        } else if( c < 0x200000 ) {
                p[len++] = 0xf0 | ( c >> 18 );
-               p[len++] = 0x80 | ( (c >> 12) & 0x3F );
-               p[len++] = 0x80 | ( (c >> 6) & 0x3F );
-               p[len++] = 0x80 | ( c & 0x3F );
+               p[len++] = 0x80 | ( (c >> 12) & 0x3f );
+               p[len++] = 0x80 | ( (c >> 6) & 0x3f );
+               p[len++] = 0x80 | ( c & 0x3f );
 
-       } else if( c < 0x400000 ) {
+       } else if( c < 0x4000000 ) {
                p[len++] = 0xf8 | ( c >> 24 );
-               p[len++] = 0x80 | ( (c >> 18) & 0x3F );
-               p[len++] = 0x80 | ( (c >> 12) & 0x3F );
-               p[len++] = 0x80 | ( (c >> 6) & 0x3F );
-               p[len++] = 0x80 | ( c & 0x3F );
+               p[len++] = 0x80 | ( (c >> 18) & 0x3f );
+               p[len++] = 0x80 | ( (c >> 12) & 0x3f );
+               p[len++] = 0x80 | ( (c >> 6) & 0x3f );
+               p[len++] = 0x80 | ( c & 0x3f );
 
        } else /* if( c < 0x80000000 ) */ {
                p[len++] = 0xfc | ( c >> 30 );
-               p[len++] = 0x80 | ( (c >> 24) & 0x3F );
-               p[len++] = 0x80 | ( (c >> 18) & 0x3F );
-               p[len++] = 0x80 | ( (c >> 12) & 0x3F );
-               p[len++] = 0x80 | ( (c >> 6) & 0x3F );
-               p[len++] = 0x80 | ( c & 0x3F );
+               p[len++] = 0x80 | ( (c >> 24) & 0x3f );
+               p[len++] = 0x80 | ( (c >> 18) & 0x3f );
+               p[len++] = 0x80 | ( (c >> 12) & 0x3f );
+               p[len++] = 0x80 | ( (c >> 6) & 0x3f );
+               p[len++] = 0x80 | ( c & 0x3f );
        }
 
        buf[len] = '\0';
@@ -198,7 +185,7 @@ char* ldap_utf8_next( const char * p )
        }
 
        for( i=1; i<6; i++ ) {
-               if ( u[i] & 0xC0 != 0x80 ) {
+               if ( ( u[i] & 0xc0 ) != 0x80 ) {
                        return (char *) &p[i];
                }
        }
@@ -221,7 +208,7 @@ char* ldap_utf8_prev( const char * p )
        const unsigned char *u = p;
 
        for( i=-1; i>-6 ; i-- ) {
-               if ( u[i] & 0xC0 != 0x80 ) {
+               if ( ( u[i] & 0xc0 ) != 0x80 ) {
                        return (char *) &p[i];
                }
        }
@@ -251,7 +238,7 @@ int ldap_utf8_copy( char* dst, const char *src )
        }
 
        for( i=1; i<6; i++ ) {
-               if ( u[i] & 0xC0 != 0x80 ) {
+               if ( ( u[i] & 0xc0 ) != 0x80 ) {
                        return i; 
                }
                dst[i] = src[i];
@@ -260,42 +247,41 @@ int ldap_utf8_copy( char* dst, const char *src )
        return i;
 }
 
+#ifndef UTF8_ALPHA_CTYPE
 /*
  * UTF-8 ctype routines
- * Only deals with characters < 0x100 (ie: US-ASCII)
+ * Only deals with characters < 0x80 (ie: US-ASCII)
  */
 
 int ldap_utf8_isascii( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
-       return ISASCII(c);
+       return LDAP_ASCII(c);
 }
 
 int ldap_utf8_isdigit( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!ISASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return c >= '0' && c <= '9';
+       return LDAP_DIGIT( c );
 }
 
 int ldap_utf8_isxdigit( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!ISASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= '0' && c <= '9' )
-               || ( c >= 'A' && c <= 'F' )
-               || ( c >= 'a' && c <= 'f' );
+       return LDAP_HEX(c);
 }
 
 int ldap_utf8_isspace( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!ISASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
        switch(c) {
        case ' ':
@@ -310,7 +296,6 @@ int ldap_utf8_isspace( const char * p )
        return 0;
 }
 
-#ifndef UTF8_ALPHA_CTYPE
 /*
  * These are not needed by the C SDK and are
  * not "good enough" for general use.
@@ -319,39 +304,36 @@ int ldap_utf8_isalpha( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!ISASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= 'A' && c <= 'Z' )
-               || ( c >= 'a' && c <= 'z' );
+       return LDAP_ALPHA(c);
 }
 
 int ldap_utf8_isalnum( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!ISASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= '0' && c <= '9' )
-               || ( c >= 'A' && c <= 'Z' )
-               || ( c >= 'a' && c <= 'z' );
+       return LDAP_ALNUM(c);
 }
 
 int ldap_utf8_islower( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!UTF8_ISASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= 'a' && c <= 'z' );
+       return LDAP_LOWER(c);
 }
 
 int ldap_utf8_isupper( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!ISASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= 'A' && c <= 'Z' );
+       return LDAP_UPPER(c);
 }
 #endif
 
@@ -360,16 +342,27 @@ int ldap_utf8_isupper( const char * p )
  * UTF-8 string routines
  */
 
+/* like strchr() */
+char * (ldap_utf8_strchr)( const char *str, const char *chr )
+{
+       for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
+               if( ldap_x_utf8_to_ucs4( str ) == ldap_x_utf8_to_ucs4( chr ) ) {
+                       return (char *) str;
+               } 
+       }
+
+       return NULL;
+}
+
 /* like strcspn() but returns number of bytes, not characters */
 ber_len_t (ldap_utf8_strcspn)( const char *str, const char *set )
 {
        const char *cstr;
+       const char *cset;
 
-       for( cstr = str; *cstr != '\0'; cstr = LDAP_UTF8_NEXT(cstr) ) {
-               const char *cset;
-
-               for( cset = set; ; cset = LDAP_UTF8_NEXT(cset) ) {
-                       if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
+       for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
+               for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
+                       if( ldap_x_utf8_to_ucs4( cstr ) == ldap_x_utf8_to_ucs4( cset ) ) {
                                return cstr - str;
                        } 
                }
@@ -382,16 +375,15 @@ ber_len_t (ldap_utf8_strcspn)( const char *str, const char *set )
 ber_len_t (ldap_utf8_strspn)( const char *str, const char *set )
 {
        const char *cstr;
+       const char *cset;
 
        for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
-               const char *cset;
-
                for( cset = set; ; LDAP_UTF8_INCR(cset) ) {
                        if( *cset == '\0' ) {
                                return cstr - str;
                        }
 
-                       if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
+                       if( ldap_x_utf8_to_ucs4( cstr ) == ldap_x_utf8_to_ucs4( cset ) ) {
                                break;
                        } 
                }
@@ -403,15 +395,12 @@ ber_len_t (ldap_utf8_strspn)( const char *str, const char *set )
 /* like strpbrk(), replaces strchr() as well */
 char *(ldap_utf8_strpbrk)( const char *str, const char *set )
 {
-       int len;
-       const char *cstr;
-
-       for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
+       for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
                const char *cset;
 
-               for( cset = set; ; LDAP_UTF8_INCR(cset) ) {
-                       if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
-                               return (char *) cstr;
+               for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
+                       if( ldap_x_utf8_to_ucs4( str ) == ldap_x_utf8_to_ucs4( cset ) ) {
+                               return (char *) str;
                        } 
                }
        }
@@ -436,7 +425,7 @@ char *(ldap_utf8_strtok)(char *str, const char *sep, char **last)
                return NULL;
        }
 
-       end = &begin[ ldap_utf8_strcpn( begin, sep ) ];
+       end = &begin[ ldap_utf8_strcspn( begin, sep ) ];
 
        if( *end != '\0' ) {
                char *next = LDAP_UTF8_NEXT( end );