xref: /netbsd-src/external/bsd/openldap/dist/include/ldap_pvt_uc.h (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: ldap_pvt_uc.h,v 1.3 2021/08/14 16:14:55 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2021 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 /*
19  * ldap_pvt_uc.h - Header for Unicode functions.
20  * These are meant to be used by the OpenLDAP distribution only.
21  * These should be named ldap_pvt_....()
22  */
23 
24 #ifndef _LDAP_PVT_UC_H
25 #define _LDAP_PVT_UC_H 1
26 
27 #include <lber.h>				/* get ber_slen_t */
28 
29 #include <ac/bytes.h>
30 #include "../libraries/liblunicode/ucdata/ucdata.h"
31 
32 LDAP_BEGIN_DECL
33 
34 /*
35  * UTF-8 (in utf-8.c)
36  */
37 
38 /* UCDATA uses UCS-2 passed in a 4 byte unsigned int */
39 typedef ac_uint4 ldap_unicode_t;
40 
41 /* Convert a string with csize octets per character to UTF-8 */
42 LDAP_F( int ) ldap_ucs_to_utf8s LDAP_P((
43 	struct berval *ucs, int csize, struct berval *utf8s ));
44 
45 
46 /* returns the number of bytes in the UTF-8 string */
47 LDAP_F (ber_len_t) ldap_utf8_bytes( const char * );
48 /* returns the number of UTF-8 characters in the string */
49 LDAP_F (ber_len_t) ldap_utf8_chars( const char * );
50 /* returns the length (in bytes) of the UTF-8 character */
51 LDAP_F (int) ldap_utf8_offset( const char * );
52 /* returns the length (in bytes) indicated by the UTF-8 character */
53 LDAP_F (int) ldap_utf8_charlen( const char * );
54 
55 /* returns the length (in bytes) indicated by the UTF-8 character
56  * also checks that shortest possible encoding was used
57  */
58 LDAP_F (int) ldap_utf8_charlen2( const char * );
59 
60 /* copies a UTF-8 character and returning number of bytes copied */
61 LDAP_F (int) ldap_utf8_copy( char *, const char *);
62 
63 /* returns pointer of next UTF-8 character in string */
64 LDAP_F (char*) ldap_utf8_next( const char * );
65 /* returns pointer of previous UTF-8 character in string */
66 LDAP_F (char*) ldap_utf8_prev( const char * );
67 
68 /* primitive ctype routines -- not aware of non-ascii characters */
69 LDAP_F (int) ldap_utf8_isascii( const char * );
70 LDAP_F (int) ldap_utf8_isalpha( const char * );
71 LDAP_F (int) ldap_utf8_isalnum( const char * );
72 LDAP_F (int) ldap_utf8_isdigit( const char * );
73 LDAP_F (int) ldap_utf8_isxdigit( const char * );
74 LDAP_F (int) ldap_utf8_isspace( const char * );
75 
76 /* span characters not in set, return bytes spanned */
77 LDAP_F (ber_len_t) ldap_utf8_strcspn( const char* str, const char *set);
78 /* span characters in set, return bytes spanned */
79 LDAP_F (ber_len_t) ldap_utf8_strspn( const char* str, const char *set);
80 /* return first occurrence of character in string */
81 LDAP_F (char *) ldap_utf8_strchr( const char* str, const char *chr);
82 /* return first character of set in string */
83 LDAP_F (char *) ldap_utf8_strpbrk( const char* str, const char *set);
84 /* reentrant tokenizer */
85 LDAP_F (char*) ldap_utf8_strtok( char* sp, const char* sep, char **last);
86 
87 /* Optimizations */
88 LDAP_V (const char) ldap_utf8_lentab[128];
89 LDAP_V (const char) ldap_utf8_mintab[32];
90 
91 #define LDAP_UTF8_ISASCII(p) ( !(*(const unsigned char *)(p) & 0x80 ) )
92 #define LDAP_UTF8_CHARLEN(p) ( LDAP_UTF8_ISASCII(p) \
93 	? 1 : ldap_utf8_lentab[*(const unsigned char *)(p) ^ 0x80] )
94 
95 /* This is like CHARLEN but additionally validates to make sure
96  * the char used the shortest possible encoding.
97  * 'l' is used to temporarily hold the result of CHARLEN.
98  */
99 #define LDAP_UTF8_CHARLEN2(p, l) ( ( ( l = LDAP_UTF8_CHARLEN( p )) < 3 || \
100 	( ldap_utf8_mintab[*(const unsigned char *)(p) & 0x1f] & (p)[1] ) ) ? \
101 	l : 0 )
102 
103 #define LDAP_UTF8_OFFSET(p) ( LDAP_UTF8_ISASCII(p) \
104 	? 1 : ldap_utf8_offset((p)) )
105 
106 #define LDAP_UTF8_COPY(d,s) ( LDAP_UTF8_ISASCII(s) \
107 	? (*(d) = *(s), 1) : ldap_utf8_copy((d),(s)) )
108 
109 #define LDAP_UTF8_NEXT(p) (	LDAP_UTF8_ISASCII(p) \
110 	? (char *)(p)+1 : ldap_utf8_next((p)) )
111 
112 #define LDAP_UTF8_INCR(p) ((p) = LDAP_UTF8_NEXT(p))
113 
114 /* For symmetry */
115 #define LDAP_UTF8_PREV(p) (ldap_utf8_prev((p)))
116 #define LDAP_UTF8_DECR(p) ((p)=LDAP_UTF8_PREV((p)))
117 
118 
119 /* these probably should be renamed */
120 LDAP_LUNICODE_F(int) ucstrncmp(
121 	const ldap_unicode_t *,
122 	const ldap_unicode_t *,
123 	ber_len_t );
124 
125 LDAP_LUNICODE_F(int) ucstrncasecmp(
126 	const ldap_unicode_t *,
127 	const ldap_unicode_t *,
128 	ber_len_t );
129 
130 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrnchr(
131 	const ldap_unicode_t *,
132 	ber_len_t,
133 	ldap_unicode_t );
134 
135 LDAP_LUNICODE_F(ldap_unicode_t *) ucstrncasechr(
136 	const ldap_unicode_t *,
137 	ber_len_t,
138 	ldap_unicode_t );
139 
140 LDAP_LUNICODE_F(void) ucstr2upper(
141 	ldap_unicode_t *,
142 	ber_len_t );
143 
144 #define LDAP_UTF8_NOCASEFOLD	0x0U
145 #define LDAP_UTF8_CASEFOLD	0x1U
146 #define LDAP_UTF8_ARG1NFC	0x2U
147 #define LDAP_UTF8_ARG2NFC	0x4U
148 #define LDAP_UTF8_APPROX	0x8U
149 
150 LDAP_LUNICODE_F(struct berval *) UTF8bvnormalize(
151 	struct berval *,
152 	struct berval *,
153 	unsigned,
154 	void *memctx );
155 
156 LDAP_LUNICODE_F(int) UTF8bvnormcmp(
157 	struct berval *,
158 	struct berval *,
159 	unsigned,
160 	void *memctx );
161 
162 LDAP_END_DECL
163 
164 #endif
165 
166