]> git.sur5r.net Git - openldap/blob - include/ldap_pvt_uc.h
c28216c2d6dee159119e2eda3af9babe4347c947
[openldap] / include / ldap_pvt_uc.h
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, Redwood City, California, USA
4  * All rights reserved.
5  *
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.
11  */
12 /*
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_....()
16  */
17
18 #ifndef _LDAP_PVT_UC_H
19 #define _LDAP_PVT_UC_H 1
20
21 #include <lber.h>                               /* get ber_slen_t */
22
23 #ifdef _MSC_VER
24 #include "../libraries/liblunicode/ucdata/ucdata.h"
25 #else
26 #include "../libraries/liblunicode/ucdata.h"
27 #endif
28
29 LDAP_BEGIN_DECL
30
31 /*
32  * UTF-8 (in utf-8.c)
33  */
34
35 typedef short ldap_ucs2_t;
36
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))
41
42 /* returns the number of bytes in the UTF-8 string */
43 LDAP_F (ber_len_t) ldap_utf8_bytes( const char * );
44 /* returns the number of UTF-8 characters in the string */
45 LDAP_F (ber_len_t) ldap_utf8_chars( const char * );
46 /* returns the length (in bytes) of the UTF-8 character */
47 LDAP_F (int) ldap_utf8_offset( const char * );
48 /* returns the length (in bytes) indicated by the UTF-8 character */
49 LDAP_F (int) ldap_utf8_charlen( const char * );
50
51 /* returns the length (in bytes) indicated by the UTF-8 character
52  * also checks that shortest possible encoding was used
53  */
54 LDAP_F (int) ldap_utf8_charlen2( const char * );
55
56 /* copies a UTF-8 character and returning number of bytes copied */
57 LDAP_F (int) ldap_utf8_copy( char *, const char *);
58
59 /* returns pointer of next UTF-8 character in string */
60 LDAP_F (char*) ldap_utf8_next( const char * );
61 /* returns pointer of previous UTF-8 character in string */
62 LDAP_F (char*) ldap_utf8_prev( const char * );
63
64 /* primitive ctype routines -- not aware of non-ascii characters */
65 LDAP_F (int) ldap_utf8_isascii( const char * );
66 LDAP_F (int) ldap_utf8_isalpha( const char * );
67 LDAP_F (int) ldap_utf8_isalnum( const char * );
68 LDAP_F (int) ldap_utf8_isdigit( const char * );
69 LDAP_F (int) ldap_utf8_isxdigit( const char * );
70 LDAP_F (int) ldap_utf8_isspace( const char * );
71
72 /* span characters not in set, return bytes spanned */
73 LDAP_F (ber_len_t) ldap_utf8_strcspn( const char* str, const char *set);
74 /* span characters in set, return bytes spanned */
75 LDAP_F (ber_len_t) ldap_utf8_strspn( const char* str, const char *set);
76 /* return first occurance of character in string */
77 LDAP_F (char *) ldap_utf8_strchr( const char* str, const char *chr);
78 /* return first character of set in string */
79 LDAP_F (char *) ldap_utf8_strpbrk( const char* str, const char *set);
80 /* reentrant tokenizer */
81 LDAP_F (char*) ldap_utf8_strtok( char* sp, const char* sep, char **last);
82
83 /* Optimizations */
84 LDAP_V (const char) ldap_utf8_lentab[128];
85 LDAP_V (const char) ldap_utf8_mintab[32];
86
87 #define LDAP_UTF8_ISASCII(p) ( !(*(unsigned char *)(p) & 0x80 ) )
88 #define LDAP_UTF8_CHARLEN(p) ( LDAP_UTF8_ISASCII(p) \
89         ? 1 : ldap_utf8_lentab[*(unsigned char *)(p) ^ 0x80] )
90
91 /* This is like CHARLEN but additionally validates to make sure
92  * the char used the shortest possible encoding.
93  * 'l' is used to temporarily hold the result of CHARLEN.
94  */
95 #define LDAP_UTF8_CHARLEN2(p, l) ( ( ( l = LDAP_UTF8_CHARLEN( p )) < 3 || \
96         ( ldap_utf8_mintab[*(unsigned char *)(p) & 0x1f] & (p)[1] ) ) ? \
97         l : 0 )
98
99 #define LDAP_UTF8_OFFSET(p) ( LDAP_UTF8_ISASCII(p) \
100         ? 1 : ldap_utf8_offset((p)) )
101
102 #define LDAP_UTF8_COPY(d,s) ( LDAP_UTF8_ISASCII(s) \
103         ? (*(d) = *(s), 1) : ldap_utf8_copy((d),(s)) )
104
105 #define LDAP_UTF8_NEXT(p) (     LDAP_UTF8_ISASCII(p) \
106         ? (char *)(p)+1 : ldap_utf8_next((p)) )
107
108 #define LDAP_UTF8_INCR(p) ((p) = LDAP_UTF8_NEXT(p))
109
110 /* For symmetry */
111 #define LDAP_UTF8_PREV(p) (ldap_utf8_prev((p)))
112 #define LDAP_UTF8_DECR(p) ((p)=LDAP_UTF8_PREV((p)))
113
114
115 /* these probably should be renamed */
116 LDAP_LUNICODE_F(int) ucstrncmp(
117         const ldap_unicode_t *,
118         const ldap_unicode_t *,
119         ber_len_t );
120
121 LDAP_LUNICODE_F(int) ucstrncasecmp(
122         const ldap_unicode_t *,
123         const ldap_unicode_t *,
124         ber_len_t );
125
126 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrnchr(
127         const ldap_unicode_t *,
128         ber_len_t,
129         ldap_unicode_t );
130
131 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrncasechr(
132         const ldap_unicode_t *,
133         ber_len_t,
134         ldap_unicode_t );
135
136 LDAP_LUNICODE_F(void) ucstr2upper(
137         ldap_unicode_t *,
138         ber_len_t );
139
140 #define LDAP_UTF8_CASEFOLD              0x1U
141 #define LDAP_UTF8_NOCASEFOLD    0x0U
142
143 LDAP_LUNICODE_F(char *) UTF8normalize(
144         struct berval *,
145         unsigned );
146
147 LDAP_LUNICODE_F(int) UTF8normcmp(
148         const char *,
149         const char *,
150         unsigned );
151
152 LDAP_END_DECL
153
154 #endif
155