Back to index

openldap  2.4.31
ldap_pvt_uc.h
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 /*
00017  * ldap_pvt_uc.h - Header for Unicode functions.
00018  * These are meant to be used by the OpenLDAP distribution only.
00019  * These should be named ldap_pvt_....()
00020  */
00021 
00022 #ifndef _LDAP_PVT_UC_H
00023 #define _LDAP_PVT_UC_H 1
00024 
00025 #include <lber.h>                         /* get ber_slen_t */
00026 
00027 #include <ac/bytes.h>
00028 #include "../libraries/liblunicode/ucdata/ucdata.h"
00029 
00030 LDAP_BEGIN_DECL
00031 
00032 /*
00033  * UTF-8 (in utf-8.c)
00034  */
00035 
00036 /* UCDATA uses UCS-2 passed in a 4 byte unsigned int */
00037 typedef ac_uint4 ldap_unicode_t;
00038 
00039 /* Convert a string with csize octets per character to UTF-8 */
00040 LDAP_F( int ) ldap_ucs_to_utf8s LDAP_P((
00041        struct berval *ucs, int csize, struct berval *utf8s ));
00042 
00043 
00044 /* returns the number of bytes in the UTF-8 string */
00045 LDAP_F (ber_len_t) ldap_utf8_bytes( const char * );
00046 /* returns the number of UTF-8 characters in the string */
00047 LDAP_F (ber_len_t) ldap_utf8_chars( const char * );
00048 /* returns the length (in bytes) of the UTF-8 character */
00049 LDAP_F (int) ldap_utf8_offset( const char * );
00050 /* returns the length (in bytes) indicated by the UTF-8 character */
00051 LDAP_F (int) ldap_utf8_charlen( const char * );
00052 
00053 /* returns the length (in bytes) indicated by the UTF-8 character
00054  * also checks that shortest possible encoding was used
00055  */
00056 LDAP_F (int) ldap_utf8_charlen2( const char * );
00057 
00058 /* copies a UTF-8 character and returning number of bytes copied */
00059 LDAP_F (int) ldap_utf8_copy( char *, const char *);
00060 
00061 /* returns pointer of next UTF-8 character in string */
00062 LDAP_F (char*) ldap_utf8_next( const char * );
00063 /* returns pointer of previous UTF-8 character in string */
00064 LDAP_F (char*) ldap_utf8_prev( const char * );
00065 
00066 /* primitive ctype routines -- not aware of non-ascii characters */
00067 LDAP_F (int) ldap_utf8_isascii( const char * );
00068 LDAP_F (int) ldap_utf8_isalpha( const char * );
00069 LDAP_F (int) ldap_utf8_isalnum( const char * );
00070 LDAP_F (int) ldap_utf8_isdigit( const char * );
00071 LDAP_F (int) ldap_utf8_isxdigit( const char * );
00072 LDAP_F (int) ldap_utf8_isspace( const char * );
00073 
00074 /* span characters not in set, return bytes spanned */
00075 LDAP_F (ber_len_t) ldap_utf8_strcspn( const char* str, const char *set);
00076 /* span characters in set, return bytes spanned */
00077 LDAP_F (ber_len_t) ldap_utf8_strspn( const char* str, const char *set);
00078 /* return first occurance of character in string */
00079 LDAP_F (char *) ldap_utf8_strchr( const char* str, const char *chr);
00080 /* return first character of set in string */
00081 LDAP_F (char *) ldap_utf8_strpbrk( const char* str, const char *set);
00082 /* reentrant tokenizer */
00083 LDAP_F (char*) ldap_utf8_strtok( char* sp, const char* sep, char **last);
00084 
00085 /* Optimizations */
00086 LDAP_V (const char) ldap_utf8_lentab[128];
00087 LDAP_V (const char) ldap_utf8_mintab[32];
00088 
00089 #define LDAP_UTF8_ISASCII(p) ( !(*(const unsigned char *)(p) & 0x80 ) )
00090 #define LDAP_UTF8_CHARLEN(p) ( LDAP_UTF8_ISASCII(p) \
00091        ? 1 : ldap_utf8_lentab[*(const unsigned char *)(p) ^ 0x80] )
00092 
00093 /* This is like CHARLEN but additionally validates to make sure
00094  * the char used the shortest possible encoding.
00095  * 'l' is used to temporarily hold the result of CHARLEN.
00096  */
00097 #define LDAP_UTF8_CHARLEN2(p, l) ( ( ( l = LDAP_UTF8_CHARLEN( p )) < 3 || \
00098        ( ldap_utf8_mintab[*(const unsigned char *)(p) & 0x1f] & (p)[1] ) ) ? \
00099        l : 0 )
00100 
00101 #define LDAP_UTF8_OFFSET(p) ( LDAP_UTF8_ISASCII(p) \
00102        ? 1 : ldap_utf8_offset((p)) )
00103 
00104 #define LDAP_UTF8_COPY(d,s) ( LDAP_UTF8_ISASCII(s) \
00105        ? (*(d) = *(s), 1) : ldap_utf8_copy((d),(s)) )
00106 
00107 #define LDAP_UTF8_NEXT(p) ( LDAP_UTF8_ISASCII(p) \
00108        ? (char *)(p)+1 : ldap_utf8_next((p)) )
00109 
00110 #define LDAP_UTF8_INCR(p) ((p) = LDAP_UTF8_NEXT(p))
00111 
00112 /* For symmetry */
00113 #define LDAP_UTF8_PREV(p) (ldap_utf8_prev((p)))
00114 #define LDAP_UTF8_DECR(p) ((p)=LDAP_UTF8_PREV((p)))
00115 
00116 
00117 /* these probably should be renamed */
00118 LDAP_LUNICODE_F(int) ucstrncmp(
00119        const ldap_unicode_t *,
00120        const ldap_unicode_t *,
00121        ber_len_t );
00122 
00123 LDAP_LUNICODE_F(int) ucstrncasecmp(
00124        const ldap_unicode_t *,
00125        const ldap_unicode_t *,
00126        ber_len_t );
00127 
00128 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrnchr(
00129        const ldap_unicode_t *,
00130        ber_len_t,
00131        ldap_unicode_t );
00132 
00133 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrncasechr(
00134        const ldap_unicode_t *,
00135        ber_len_t,
00136        ldap_unicode_t );
00137 
00138 LDAP_LUNICODE_F(void) ucstr2upper(
00139        ldap_unicode_t *,
00140        ber_len_t );
00141 
00142 #define LDAP_UTF8_NOCASEFOLD       0x0U
00143 #define LDAP_UTF8_CASEFOLD  0x1U
00144 #define LDAP_UTF8_ARG1NFC   0x2U
00145 #define LDAP_UTF8_ARG2NFC   0x4U
00146 #define LDAP_UTF8_APPROX    0x8U
00147 
00148 LDAP_LUNICODE_F(struct berval *) UTF8bvnormalize(
00149        struct berval *,
00150        struct berval *,
00151        unsigned,
00152        void *memctx );
00153 
00154 LDAP_LUNICODE_F(int) UTF8bvnormcmp(
00155        struct berval *,
00156        struct berval *,
00157        unsigned,
00158        void *memctx );
00159 
00160 LDAP_END_DECL
00161 
00162 #endif
00163