]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/utf-8.c
cleanup last commit, add commit
[openldap] / libraries / libldap / utf-8.c
index 6f281f303949b4d7d0acf07e706c90fa08198ea5..756c962672d50a81ac634cdea34be85249a9d89c 100644 (file)
@@ -1,11 +1,24 @@
+/* utf-8.c -- Basic UTF-8 routines */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2004 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 the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (C) The Internet Society (1998)
+ * UTF-8 encodings are derived from those in RFC 2279;
+ * see RFC for full legal notices.
  */
 
-/*
- * Basic UTF-8 routines
+/* Basic UTF-8 routines
  *
  * These routines are "dumb".  Though they understand UTF-8,
  * they don't grok Unicode.  That is, they can push bits,
 #include "ldap-int.h"
 #include "ldap_defaults.h"
 
-/*
- * Basic UTF-8 routines
- */
-
 /*
  * return the number of bytes required to hold the
  * NULL-terminated UTF-8 string NOT INCLUDING the
@@ -86,7 +95,7 @@ int ldap_utf8_charlen( const char * p )
        if (!(*p & 0x80))
                return 1;
 
-       return ldap_utf8_lentab[*(unsigned char *)p ^ 0x80];
+       return ldap_utf8_lentab[*(const unsigned char *)p ^ 0x80];
 }
 
 /*
@@ -112,11 +121,14 @@ int ldap_utf8_charlen( const char * p )
  */
 
 /* mask of required bits in second octet */
-const char ldap_utf8_mintab[] = {
-       0x20, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
-       0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
-       0x30, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
-       0x38, 0x80, 0x80, 0x80, 0x3c, 0x80, 0x00, 0x00 };
+#undef c
+#define c const char
+c ldap_utf8_mintab[] = {
+       (c)0x20, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80,
+       (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80,
+       (c)0x30, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80,
+       (c)0x38, (c)0x80, (c)0x80, (c)0x80, (c)0x3c, (c)0x80, (c)0x00, (c)0x00 };
+#undef c
 
 int ldap_utf8_charlen2( const char * p )
 {
@@ -132,7 +144,7 @@ int ldap_utf8_charlen2( const char * p )
 /* conv UTF-8 to UCS-4, useful for comparisons */
 ldap_ucs4_t ldap_x_utf8_to_ucs4( const char * p )
 {
-    const unsigned char *c = p;
+    const unsigned char *c = (const unsigned char *) p;
     ldap_ucs4_t ch;
        int len, i;
        static unsigned char mask[] = {
@@ -160,13 +172,22 @@ ldap_ucs4_t ldap_x_utf8_to_ucs4( const char * p )
 int ldap_x_ucs4_to_utf8( ldap_ucs4_t c, char *buf )
 {
        int len=0;
-       unsigned char* p = buf;
-       if(buf == NULL) return 0;
-
-       if ( c < 0 ) {
-               /* not a valid Unicode character */
+       unsigned char* p = (unsigned char *) buf;
+
+       /* not a valid Unicode character */
+       if ( c < 0 ) return 0;
+
+       /* Just return length, don't convert */
+       if(buf == NULL) {
+               if( c < 0x80 ) return 1;
+               else if( c < 0x800 ) return 2;
+               else if( c < 0x10000 ) return 3;
+               else if( c < 0x200000 ) return 4;
+               else if( c < 0x4000000 ) return 5;
+               else return 6;
+       }
 
-       } else if( c < 0x80 ) {
+       if( c < 0x80 ) {
                p[len++] = c;
 
        } else if( c < 0x800 ) {
@@ -200,10 +221,80 @@ int ldap_x_ucs4_to_utf8( ldap_ucs4_t c, char *buf )
                p[len++] = 0x80 | ( c & 0x3f );
        }
 
-       buf[len] = '\0';
        return len;
 }
 
+#define LDAP_UCS_UTF8LEN(c)    \
+       c < 0 ? 0 : (c < 0x80 ? 1 : (c < 0x800 ? 2 : (c < 0x10000 ? 3 : \
+       (c < 0x200000 ? 4 : (c < 0x4000000 ? 5 : 6)))))
+
+/* Convert a string to UTF-8 format. The input string is expected to
+ * have characters of 1, 2, or 4 octets (in network byte order)
+ * corresponding to the ASN.1 T61STRING, BMPSTRING, and UNIVERSALSTRING
+ * types respectively. (Here T61STRING just means that there is one
+ * octet per character and characters may use the high bit of the octet.
+ * The characters are assumed to use ISO mappings, no provision is made
+ * for converting from T.61 coding rules to Unicode.)
+ */
+
+int
+ldap_ucs_to_utf8s( struct berval *ucs, int csize, struct berval *utf8s )
+{
+       unsigned char *in, *end;
+       char *ptr;
+       ldap_ucs4_t u;
+       int i, l = 0;
+
+       utf8s->bv_val = NULL;
+       utf8s->bv_len = 0;
+
+       in = (unsigned char *)ucs->bv_val;
+
+       /* Make sure we stop at an even multiple of csize */
+       end = in + ( ucs->bv_len & ~(csize-1) );
+       
+       for (; in < end; ) {
+               u = *in++;
+               if (csize > 1) {
+                       u <<= 8;
+                       u |= *in++;
+               }
+               if (csize > 2) {
+                       u <<= 8;
+                       u |= *in++;
+                       u <<= 8;
+                       u |= *in++;
+               }
+               i = LDAP_UCS_UTF8LEN(u);
+               if (i == 0)
+                       return LDAP_INVALID_SYNTAX;
+               l += i;
+       }
+
+       utf8s->bv_val = LDAP_MALLOC( l+1 );
+       if (utf8s->bv_val == NULL)
+               return LDAP_NO_MEMORY;
+       utf8s->bv_len = l;
+
+       ptr = utf8s->bv_val;
+       for (in = (unsigned char *)ucs->bv_val; in < end; ) {
+               u = *in++;
+               if (csize > 1) {
+                       u <<= 8;
+                       u |= *in++;
+               }
+               if (csize > 2) {
+                       u <<= 8;
+                       u |= *in++;
+                       u <<= 8;
+                       u |= *in++;
+               }
+               ptr += ldap_x_ucs4_to_utf8(u, ptr);
+       }
+       *ptr = '\0';
+       return LDAP_SUCCESS;
+}
+
 /*
  * Advance to the next UTF-8 character
  *
@@ -216,7 +307,7 @@ int ldap_x_ucs4_to_utf8( ldap_ucs4_t c, char *buf )
 char* ldap_utf8_next( const char * p )
 {
        int i;
-       const unsigned char *u = p;
+       const unsigned char *u = (const unsigned char *) p;
 
        if( LDAP_UTF8_ISASCII(u) ) {
                return (char *) &p[1];
@@ -243,7 +334,7 @@ char* ldap_utf8_next( const char * p )
 char* ldap_utf8_prev( const char * p )
 {
        int i;
-       const unsigned char *u = p;
+       const unsigned char *u = (const unsigned char *) p;
 
        for( i=-1; i>-6 ; i-- ) {
                if ( ( u[i] & 0xc0 ) != 0x80 ) {
@@ -267,7 +358,7 @@ char* ldap_utf8_prev( const char * p )
 int ldap_utf8_copy( char* dst, const char *src )
 {
        int i;
-       const unsigned char *u = src;
+       const unsigned char *u = (const unsigned char *) src;
 
        dst[0] = src[0];