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
32LDAP_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 */
39typedef ac_uint4 ldap_unicode_t;
40
41/* Convert a string with csize octets per character to UTF-8 */
42LDAP_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 */
47LDAP_F (ber_len_t) ldap_utf8_bytes( const char * );
48/* returns the number of UTF-8 characters in the string */
49LDAP_F (ber_len_t) ldap_utf8_chars( const char * );
50/* returns the length (in bytes) of the UTF-8 character */
51LDAP_F (int) ldap_utf8_offset( const char * );
52/* returns the length (in bytes) indicated by the UTF-8 character */
53LDAP_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 */
58LDAP_F (int) ldap_utf8_charlen2( const char * );
59
60/* copies a UTF-8 character and returning number of bytes copied */
61LDAP_F (int) ldap_utf8_copy( char *, const char *);
62
63/* returns pointer of next UTF-8 character in string */
64LDAP_F (char*) ldap_utf8_next( const char * );
65/* returns pointer of previous UTF-8 character in string */
66LDAP_F (char*) ldap_utf8_prev( const char * );
67
68/* primitive ctype routines -- not aware of non-ascii characters */
69LDAP_F (int) ldap_utf8_isascii( const char * );
70LDAP_F (int) ldap_utf8_isalpha( const char * );
71LDAP_F (int) ldap_utf8_isalnum( const char * );
72LDAP_F (int) ldap_utf8_isdigit( const char * );
73LDAP_F (int) ldap_utf8_isxdigit( const char * );
74LDAP_F (int) ldap_utf8_isspace( const char * );
75
76/* span characters not in set, return bytes spanned */
77LDAP_F (ber_len_t) ldap_utf8_strcspn( const char* str, const char *set);
78/* span characters in set, return bytes spanned */
79LDAP_F (ber_len_t) ldap_utf8_strspn( const char* str, const char *set);
80/* return first occurrence of character in string */
81LDAP_F (char *) ldap_utf8_strchr( const char* str, const char *chr);
82/* return first character of set in string */
83LDAP_F (char *) ldap_utf8_strpbrk( const char* str, const char *set);
84/* reentrant tokenizer */
85LDAP_F (char*) ldap_utf8_strtok( char* sp, const char* sep, char **last);
86
87/* Optimizations */
88LDAP_V (const char) ldap_utf8_lentab[128];
89LDAP_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 */
120LDAP_LUNICODE_F(int) ucstrncmp(
121	const ldap_unicode_t *,
122	const ldap_unicode_t *,
123	ber_len_t );
124
125LDAP_LUNICODE_F(int) ucstrncasecmp(
126	const ldap_unicode_t *,
127	const ldap_unicode_t *,
128	ber_len_t );
129
130LDAP_LUNICODE_F(ldap_unicode_t *) ucstrnchr(
131	const ldap_unicode_t *,
132	ber_len_t,
133	ldap_unicode_t );
134
135LDAP_LUNICODE_F(ldap_unicode_t *) ucstrncasechr(
136	const ldap_unicode_t *,
137	ber_len_t,
138	ldap_unicode_t );
139
140LDAP_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
150LDAP_LUNICODE_F(struct berval *) UTF8bvnormalize(
151	struct berval *,
152	struct berval *,
153	unsigned,
154	void *memctx );
155
156LDAP_LUNICODE_F(int) UTF8bvnormcmp(
157	struct berval *,
158	struct berval *,
159	unsigned,
160	void *memctx );
161
162LDAP_END_DECL
163
164#endif
165
166