3 * Copyright 1998-2002 The OpenLDAP Foundation, Redwood City, California, USA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
8 * Public License. A copy of this license is available at
9 * http://www.OpenLDAP.org/license.html or in file LICENSE in the
10 * top-level directory of the distribution.
13 * ldap_pvt_uc.h - Header for Unicode functions.
14 * These are meant to be used by the OpenLDAP distribution only.
15 * These should be named ldap_pvt_....()
18 #ifndef _LDAP_PVT_UC_H
19 #define _LDAP_PVT_UC_H 1
21 #include <lber.h> /* get ber_slen_t */
24 #include "../libraries/liblunicode/ucdata/ucdata.h"
26 #include "../libraries/liblunicode/ucdata.h"
35 typedef short ldap_ucs2_t;
37 /* UCDATA uses UCS-2 passed in an unsigned long */
38 typedef unsigned long ldap_unicode_t;
39 #define ldap_utf8_to_unicode( p ) ldap_utf8_to_ucs4((p))
40 #define ldap_unicode_to_utf8( c, buf ) ldap_ucs4_to_ucs4((c),(buf))
42 /* Convert a string with csize octets per character to UTF-8 */
43 LDAP_F( int ) ldap_ucs_to_utf8s LDAP_P((
44 struct berval *ucs, int csize, struct berval *utf8s ));
47 /* returns the number of bytes in the UTF-8 string */
48 LDAP_F (ber_len_t) ldap_utf8_bytes( const char * );
49 /* returns the number of UTF-8 characters in the string */
50 LDAP_F (ber_len_t) ldap_utf8_chars( const char * );
51 /* returns the length (in bytes) of the UTF-8 character */
52 LDAP_F (int) ldap_utf8_offset( const char * );
53 /* returns the length (in bytes) indicated by the UTF-8 character */
54 LDAP_F (int) ldap_utf8_charlen( const char * );
56 /* returns the length (in bytes) indicated by the UTF-8 character
57 * also checks that shortest possible encoding was used
59 LDAP_F (int) ldap_utf8_charlen2( const char * );
61 /* copies a UTF-8 character and returning number of bytes copied */
62 LDAP_F (int) ldap_utf8_copy( char *, const char *);
64 /* returns pointer of next UTF-8 character in string */
65 LDAP_F (char*) ldap_utf8_next( const char * );
66 /* returns pointer of previous UTF-8 character in string */
67 LDAP_F (char*) ldap_utf8_prev( const char * );
69 /* primitive ctype routines -- not aware of non-ascii characters */
70 LDAP_F (int) ldap_utf8_isascii( const char * );
71 LDAP_F (int) ldap_utf8_isalpha( const char * );
72 LDAP_F (int) ldap_utf8_isalnum( const char * );
73 LDAP_F (int) ldap_utf8_isdigit( const char * );
74 LDAP_F (int) ldap_utf8_isxdigit( const char * );
75 LDAP_F (int) ldap_utf8_isspace( const char * );
77 /* span characters not in set, return bytes spanned */
78 LDAP_F (ber_len_t) ldap_utf8_strcspn( const char* str, const char *set);
79 /* span characters in set, return bytes spanned */
80 LDAP_F (ber_len_t) ldap_utf8_strspn( const char* str, const char *set);
81 /* return first occurance of character in string */
82 LDAP_F (char *) ldap_utf8_strchr( const char* str, const char *chr);
83 /* return first character of set in string */
84 LDAP_F (char *) ldap_utf8_strpbrk( const char* str, const char *set);
85 /* reentrant tokenizer */
86 LDAP_F (char*) ldap_utf8_strtok( char* sp, const char* sep, char **last);
89 LDAP_V (const char) ldap_utf8_lentab[128];
90 LDAP_V (const char) ldap_utf8_mintab[32];
92 #define LDAP_UTF8_ISASCII(p) ( !(*(const unsigned char *)(p) & 0x80 ) )
93 #define LDAP_UTF8_CHARLEN(p) ( LDAP_UTF8_ISASCII(p) \
94 ? 1 : ldap_utf8_lentab[*(const unsigned char *)(p) ^ 0x80] )
96 /* This is like CHARLEN but additionally validates to make sure
97 * the char used the shortest possible encoding.
98 * 'l' is used to temporarily hold the result of CHARLEN.
100 #define LDAP_UTF8_CHARLEN2(p, l) ( ( ( l = LDAP_UTF8_CHARLEN( p )) < 3 || \
101 ( ldap_utf8_mintab[*(const unsigned char *)(p) & 0x1f] & (p)[1] ) ) ? \
104 #define LDAP_UTF8_OFFSET(p) ( LDAP_UTF8_ISASCII(p) \
105 ? 1 : ldap_utf8_offset((p)) )
107 #define LDAP_UTF8_COPY(d,s) ( LDAP_UTF8_ISASCII(s) \
108 ? (*(d) = *(s), 1) : ldap_utf8_copy((d),(s)) )
110 #define LDAP_UTF8_NEXT(p) ( LDAP_UTF8_ISASCII(p) \
111 ? (char *)(p)+1 : ldap_utf8_next((p)) )
113 #define LDAP_UTF8_INCR(p) ((p) = LDAP_UTF8_NEXT(p))
116 #define LDAP_UTF8_PREV(p) (ldap_utf8_prev((p)))
117 #define LDAP_UTF8_DECR(p) ((p)=LDAP_UTF8_PREV((p)))
120 /* these probably should be renamed */
121 LDAP_LUNICODE_F(int) ucstrncmp(
122 const ldap_unicode_t *,
123 const ldap_unicode_t *,
126 LDAP_LUNICODE_F(int) ucstrncasecmp(
127 const ldap_unicode_t *,
128 const ldap_unicode_t *,
131 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrnchr(
132 const ldap_unicode_t *,
136 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrncasechr(
137 const ldap_unicode_t *,
141 LDAP_LUNICODE_F(void) ucstr2upper(
145 #define LDAP_UTF8_NOCASEFOLD 0x0U
146 #define LDAP_UTF8_CASEFOLD 0x1U
147 #define LDAP_UTF8_ARG1NFC 0x2U
148 #define LDAP_UTF8_ARG2NFC 0x4U
149 #define LDAP_UTF8_APPROX 0x8U
151 LDAP_LUNICODE_F(struct berval *) UTF8bvnormalize(
156 LDAP_LUNICODE_F(int) UTF8bvnormcmp(