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