From 23afcd5c6e7e92009838cd07301b6872171d1c5d Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Sat, 22 Jan 2000 01:55:34 +0000 Subject: [PATCH] Initial UTF-8 routines. --- libraries/libldap/Makefile.in | 6 +- libraries/libldap/ldap-int.h | 18 ++++ libraries/libldap/utf-8.c | 181 ++++++++++++++++++++++++++++++++ libraries/libldap_r/Makefile.in | 6 +- 4 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 libraries/libldap/utf-8.c diff --git a/libraries/libldap/Makefile.in b/libraries/libldap/Makefile.in index eb92ea5d18..826ad0da70 100644 --- a/libraries/libldap/Makefile.in +++ b/libraries/libldap/Makefile.in @@ -17,7 +17,8 @@ SRCS = bind.c open.c result.c error.c compare.c search.c \ getdn.c getentry.c getattr.c getvalues.c addentry.c \ request.c getdxbyname.c os-ip.c url.c charset.c \ init.c options.c print.c string.c util-int.c schema.c \ - charray.c digest.c tls.c dn.c os-local.c dnssrv.c + charray.c digest.c tls.c dn.c os-local.c dnssrv.c \ + utf-8.c OBJS = bind.lo open.lo result.lo error.lo compare.lo search.lo \ controls.lo messages.lo references.lo extended.lo \ modify.lo add.lo modrdn.lo delete.lo abandon.lo ufn.lo cache.lo \ @@ -26,7 +27,8 @@ OBJS = bind.lo open.lo result.lo error.lo compare.lo search.lo \ getdn.lo getentry.lo getattr.lo getvalues.lo addentry.lo \ request.lo getdxbyname.lo os-ip.lo url.lo charset.lo \ init.lo options.lo print.lo string.lo util-int.lo schema.lo \ - charray.lo digest.lo tls.lo dn.lo os-local.lo dnssrv.lo + charray.lo digest.lo tls.lo dn.lo os-local.lo dnssrv.lo \ + utf-8.lo LDAP_INCDIR= ../../include LDAP_LIBDIR= ../../libraries diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index 9f292a6596..69be28ebf8 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -506,6 +506,24 @@ LIBLDAP_F (int) ldap_8859_to_t61( char **bufp, ber_len_t *buflenp, int free_inpu #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */ #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */ +/* + * UTF-8 (in utf-8.c) + */ + +LIBLDAP_F (ber_len_t) ldap_utf8_bytes( const char * ); +LIBLDAP_F (ber_len_t) ldap_utf8_chars( const char * ); +LIBLDAP_F (int) ldap_utf8_charlen( const char * ); + +LIBLDAP_F (char*) ldap_utf8_next(char *); +LIBLDAP_F (char*) ldap_utf8_prev(char *); + +LIBLDAP_F (int) ldap_utf8_isascii( const char * ); +LIBLDAP_F (int) ldap_utf8_isalpa( const char * ); +LIBLDAP_F (int) ldap_utf8_isalnum( const char * ); +LIBLDAP_F (int) ldap_utf8_isdigit( const char * ); +LIBLDAP_F (int) ldap_utf8_isxdigit( const char * ); +LIBLDAP_F (int) ldap_utf8_isspace( const char * ); + LDAP_END_DECL #endif /* _LDAP_INT_H */ diff --git a/libraries/libldap/utf-8.c b/libraries/libldap/utf-8.c new file mode 100644 index 0000000000..7051b6dfdd --- /dev/null +++ b/libraries/libldap/utf-8.c @@ -0,0 +1,181 @@ +/* $OpenLDAP$ */ +/* + * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ + +/* + * Basic UTF-8 routines + * + * These routines are not optimized. + */ + +#include "portable.h" + +#include + +#include + +#include +#include +#include + +#include "ldap-int.h" +#include "ldap_defaults.h" + +#define UTF8_ISASCII(u) ( !((u) & ~0x7f) ) + +ber_len_t ldap_utf8_bytes( const char * p ) +{ + ber_len_t bytes; + + for( bytes=0; p[bytes] ; bytes++ ) { + /* EMPTY */ ; + } + + return bytes; +} + +ber_len_t ldap_utf8_chars( const char * p ) +{ + /* could be optimized */ + int chars=0; + int i=0; + unsigned char *u; + + for( i=0; u[i]; i++) { + if ( u[i] & 0xC0 != 0x80 ) chars++; + } + + return i; +} + +int ldap_utf8_charlen( const char * p ) +{ + unsigned c; + + 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; + } + + return 1; +} + +char* ldap_utf8_next( char * p ) +{ + int len = ldap_utf8_charlen( p ); + + return len ? &p[len] : NULL; +} + +char* ldap_utf8_prev( char * p ) +{ + int i; + unsigned char *u = p; + + for( i = -1; i >= -6 ; i-- ) { + if ( u[i] & 0xC0 != 0x80 ) return &p[i]; + } + + return NULL; +} + +int ldap_utf8_isascii( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + return UTF8_ISASCII(c); +} + +int ldap_utf8_isdigit( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + + if(!UTF8_ISASCII(c)) return 0; + + return c >= '0' && c <= '9'; +} + +int ldap_utf8_isxdigit( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + + if(!UTF8_ISASCII(c)) return 0; + + return ( c >= '0' && c <= '9' ) + || ( c >= 'A' && c <= 'F' ) + || ( c >= 'a' && c <= 'f' ); +} + +int ldap_utf8_isalpha( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + + if(!UTF8_ISASCII(c)) return 0; + + return ( c >= 'A' && c <= 'Z' ) + || ( c >= 'a' && c <= 'z' ); +} + +int ldap_utf8_isalnum( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + + if(!UTF8_ISASCII(c)) return 0; + + return ( c >= '0' && c <= '9' ) + || ( c >= 'A' && c <= 'Z' ) + || ( c >= 'a' && c <= 'z' ); +} + +int ldap_utf8_islower( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + + if(!UTF8_ISASCII(c)) return 0; + + return ( c >= 'a' && c <= 'z' ); +} + +int ldap_utf8_isupper( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + + if(!UTF8_ISASCII(c)) return 0; + + return ( c >= 'A' && c <= 'Z' ); +} + +int ldap_utf8_isspace( const char * p ) +{ + unsigned c = * (const unsigned char *) p; + + if(!UTF8_ISASCII(c)) return 0; + + switch(c) { + case ' ': + case '\t': + case '\n': + case '\r': + case '\v': + case '\f': + return 1; + } + + return 0; +} diff --git a/libraries/libldap_r/Makefile.in b/libraries/libldap_r/Makefile.in index 43cf54d086..beddcf0f03 100644 --- a/libraries/libldap_r/Makefile.in +++ b/libraries/libldap_r/Makefile.in @@ -16,7 +16,8 @@ XXSRCS = apitest.c test.c tmpltest.c extended.c \ getdn.c getentry.c getattr.c getvalues.c addentry.c \ request.c getdxbyname.c os-ip.c url.c charset.c \ init.c options.c print.c string.c util-int.c schema.c \ - charray.c digest.c tls.c dn.c os-local.c dnssrv.c + charray.c digest.c tls.c dn.c os-local.c dnssrv.c \ + utf-8.c SRCS = thr_posix.c thr_cthreads.c thr_thr.c thr_lwp.c thr_nt.c \ thr_pth.c thr_sleep.c thr_stub.c rdwr.c OBJS = extended.lo \ @@ -29,7 +30,8 @@ OBJS = extended.lo \ init.lo options.lo print.lo string.lo util-int.lo schema.lo \ thr_posix.lo thr_cthreads.lo thr_thr.lo thr_lwp.lo thr_nt.lo \ thr_pth.lo thr_sleep.lo thr_stub.lo rdwr.lo \ - charray.lo digest.lo tls.lo dn.lo os-local.lo dnssrv.lo + charray.lo digest.lo tls.lo dn.lo os-local.lo dnssrv.lo \ + utf-8.lo LDAP_INCDIR= ../../include LDAP_LIBDIR= ../../libraries -- 2.39.5