3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 * These routines are "dumb". Though they understand UTF-8,
11 * they don't grok Unicode. That is, they can push bits,
12 * but don't have a clue what the bits represent. That's
13 * good enough for use with the LDAP Client SDK.
15 * These routines are not optimized.
22 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
29 #include "ldap_defaults.h"
31 #define UTF8_ISASCII(u) ( (u) < 0x100 )
32 #define UCS4_INVALID 0x80000000U
35 * Basic UTF-8 routines
39 * return the number of bytes required to hold the
40 * NULL-terminated UTF-8 string INCLUDING the
43 ber_len_t ldap_utf8_bytes( const char * p )
47 if( p == NULL ) return bytes;
56 ber_len_t ldap_utf8_chars( const char * p )
58 /* could be optimized and could check for invalid sequences */
61 for( chars=0; *p ; chars++ ) {
62 int charlen = ldap_utf8_charlen( p );
64 if( !charlen ) return chars;
72 int ldap_utf8_charlen( const char * p )
74 unsigned c = * (const unsigned char *) p;
76 if ((c & 0xfe ) == 0xfc) {
80 if ((c & 0xfc ) == 0xf8) {
84 if ((c & 0xf8 ) == 0xf0) {
88 if ((c & 0xf0 ) == 0xe0) {
92 if ((c & 0xe0 ) == 0xc0) {
96 if ((c & 0x80 ) == 0x80) {
104 /* conv UTF-8 to UCS-4, useful for comparisons */
105 ber_int_t ldap_utf8_to_ucs4( const char * p )
108 ber_int_t c = * (const unsigned char *) p;
110 if ((c & 0xFE ) == 0xFC) {
114 } else if ((c & 0xFC ) == 0xF8) {
118 } else if ((c & 0xF8 ) == 0xF0) {
122 } else if ((c & 0xF0 ) == 0xE0) {
126 } else if ((c & 0xE0 ) == 0xC0) {
130 } else if ((c & 0x80 ) == 0x80) {
137 for(i=1; i < len; i++) {
138 ber_int_t ch = ((const unsigned char *) p)[i];
140 if ((ch & 0xc0) != 0x80) {
151 /* conv UCS-4 to UTF-8, not used */
152 int ldap_ucs4_to_utf8( ber_int_t c, char *buf )
155 unsigned char* p = buf;
156 if(buf == NULL) return 0;
159 /* not a valid Unicode character */
161 } else if( c < 0x80 ) {
164 } else if( c < 0x800 ) {
165 p[len++] = 0xc0 | ( c >> 6 );
166 p[len++] = 0x80 | ( c & 0x3F );
168 } else if( c < 0x10000 ) {
169 p[len++] = 0xe0 | ( c >> 12 );
170 p[len++] = 0x80 | ( (c >> 6) & 0x3F );
171 p[len++] = 0x80 | ( c & 0x3F );
173 } else if( c < 0x200000 ) {
174 p[len++] = 0xf0 | ( c >> 18 );
175 p[len++] = 0x80 | ( (c >> 12) & 0x3F );
176 p[len++] = 0x80 | ( (c >> 6) & 0x3F );
177 p[len++] = 0x80 | ( c & 0x3F );
179 } else if( c < 0x400000 ) {
180 p[len++] = 0xf8 | ( c >> 24 );
181 p[len++] = 0x80 | ( (c >> 18) & 0x3F );
182 p[len++] = 0x80 | ( (c >> 12) & 0x3F );
183 p[len++] = 0x80 | ( (c >> 6) & 0x3F );
184 p[len++] = 0x80 | ( c & 0x3F );
186 } else /* if( c < 0x80000000 ) */ {
187 p[len++] = 0xfc | ( c >> 30 );
188 p[len++] = 0x80 | ( (c >> 24) & 0x3F );
189 p[len++] = 0x80 | ( (c >> 18) & 0x3F );
190 p[len++] = 0x80 | ( (c >> 12) & 0x3F );
191 p[len++] = 0x80 | ( (c >> 6) & 0x3F );
192 p[len++] = 0x80 | ( c & 0x3F );
199 char* ldap_utf8_next( const char * p )
201 int len = ldap_utf8_charlen( p );
203 return len ? (char *) &p[len] : NULL;
206 char* ldap_utf8_prev( const char * p )
209 const unsigned char *u = p;
211 for( i = -1; i >= -6 ; i-- ) {
212 if ( u[i] & 0xC0 != 0x80 ) return (char *) &p[i];
219 * UTF-8 ctype routines
220 * Only deals with characters < 0x100 (ie: US-ASCII)
223 int ldap_utf8_isascii( const char * p )
225 unsigned c = * (const unsigned char *) p;
226 return UTF8_ISASCII(c);
229 int ldap_utf8_isdigit( const char * p )
231 unsigned c = * (const unsigned char *) p;
233 if(!UTF8_ISASCII(c)) return 0;
235 return c >= '0' && c <= '9';
238 int ldap_utf8_isxdigit( const char * p )
240 unsigned c = * (const unsigned char *) p;
242 if(!UTF8_ISASCII(c)) return 0;
244 return ( c >= '0' && c <= '9' )
245 || ( c >= 'A' && c <= 'F' )
246 || ( c >= 'a' && c <= 'f' );
249 int ldap_utf8_isspace( const char * p )
251 unsigned c = * (const unsigned char *) p;
253 if(!UTF8_ISASCII(c)) return 0;
268 #ifndef UTF8_ALPHA_CTYPE
270 * These are not needed by the C SDK and are
271 * not "good enough" for general use.
273 int ldap_utf8_isalpha( const char * p )
275 unsigned c = * (const unsigned char *) p;
277 if(!UTF8_ISASCII(c)) return 0;
279 return ( c >= 'A' && c <= 'Z' )
280 || ( c >= 'a' && c <= 'z' );
283 int ldap_utf8_isalnum( const char * p )
285 unsigned c = * (const unsigned char *) p;
287 if(!UTF8_ISASCII(c)) return 0;
289 return ( c >= '0' && c <= '9' )
290 || ( c >= 'A' && c <= 'Z' )
291 || ( c >= 'a' && c <= 'z' );
294 int ldap_utf8_islower( const char * p )
296 unsigned c = * (const unsigned char *) p;
298 if(!UTF8_ISASCII(c)) return 0;
300 return ( c >= 'a' && c <= 'z' );
303 int ldap_utf8_isupper( const char * p )
305 unsigned c = * (const unsigned char *) p;
307 if(!UTF8_ISASCII(c)) return 0;
309 return ( c >= 'A' && c <= 'Z' );
314 * get one UTF-8 character
316 char* ldap_utf8_fgetc( FILE *s, char *buf )
323 if( s == NULL ) return NULL;
335 len = ldap_utf8_charlen( buf );
337 if( len < 1 ) return NULL;
339 for( i = 1; i < len; i++ ) {
340 unsigned int c = fgetc( s );
345 if( c & 0xC0 != 0x80 ) {
358 * UTF-8 string routines
361 /* like strcspn() but returns number of bytes, not characters */
362 ber_len_t (ldap_utf8_strcspn)( const char *str, const char *set )
367 for( cstr = str; *cstr != '\0'; cstr += len ) {
370 for( cset = set; ; cset += len ) {
371 if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
375 len = ldap_utf8_charlen(cset);
379 len = ldap_utf8_charlen(cstr);
386 /* like strspn() but returns number of bytes, not characters */
387 ber_len_t (ldap_utf8_strspn)( const char *str, const char *set )
392 for( cstr = str; *cstr != '\0'; cstr += len ) {
395 for( cset = set; ; cset += len ) {
396 if( *cset == '\0' ) {
400 if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
404 len = ldap_utf8_charlen(cset);
408 len = ldap_utf8_charlen(cstr);
415 /* like strpbrk(), replaces strchr() as well */
416 char *(ldap_utf8_strpbrk)( const char *str, const char *set )
421 for( cstr = str; *cstr != '\0'; cstr += len ) {
424 for( cset = set; ; cset += len ) {
425 if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
429 len = ldap_utf8_charlen(cset);
433 len = ldap_utf8_charlen(cstr);
440 /* like strtok_r(), not strtok() */
441 char *(ldap_utf8_strtok)(char *str, const char *sep, char **last)
446 if( last == NULL ) return NULL;
448 begin = str ? str : *last;
450 begin += ldap_utf8_strspn( begin, sep );
452 if( *begin == '\0' ) {
457 end = &begin[ ldap_utf8_strcpn( begin, sep ) ];
460 int len = ldap_utf8_charlen( end );