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"
32 #define ISASCII(uc) ((uc) < 0x100)
34 #define UCS4_INVALID 0x80000000U
37 * Basic UTF-8 routines
41 * return the number of bytes required to hold the
42 * NULL-terminated UTF-8 string INCLUDING the
45 ber_len_t ldap_utf8_bytes( const char * p )
49 if( p == NULL ) return bytes;
58 ber_len_t ldap_utf8_chars( const char * p )
60 /* could be optimized and could check for invalid sequences */
63 for( ; *p ; LDAP_UTF8_INCR(p) ) {
70 /* return offset to next character */
71 int ldap_utf8_offset( const char * p )
73 return LDAP_UTF8_NEXT(p) - p;
77 * Returns length indicated by first byte.
79 * This function should use a table lookup.
81 int ldap_utf8_charlen( const char * p )
83 unsigned c = * (const unsigned char *) p;
85 if ((c & 0xfe ) == 0xfc) {
89 if ((c & 0xfc ) == 0xf8) {
93 if ((c & 0xf8 ) == 0xf0) {
97 if ((c & 0xf0 ) == 0xe0) {
101 if ((c & 0xe0 ) == 0xc0) {
105 if ((c & 0x80 ) == 0x80) {
113 /* conv UTF-8 to UCS-4, useful for comparisons */
114 ber_int_t ldap_utf8_to_ucs4( const char * p )
116 const unsigned char *c = p;
119 static unsigned char mask[] = {
120 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
122 len = LDAP_UTF8_CHARLEN(p);
124 if( len == 0 ) return UCS4_INVALID;
126 ch = c[0] & mask[len];
128 for(i=1; i < len; i++) {
129 if ((c[i] & 0xc0) != 0x80) {
140 /* conv UCS-4 to UTF-8, not used */
141 int ldap_ucs4_to_utf8( ber_int_t c, char *buf )
144 unsigned char* p = buf;
145 if(buf == NULL) return 0;
148 /* not a valid Unicode character */
150 } else if( c < 0x80 ) {
153 } else if( c < 0x800 ) {
154 p[len++] = 0xc0 | ( c >> 6 );
155 p[len++] = 0x80 | ( c & 0x3f );
157 } else if( c < 0x10000 ) {
158 p[len++] = 0xe0 | ( c >> 12 );
159 p[len++] = 0x80 | ( (c >> 6) & 0x3f );
160 p[len++] = 0x80 | ( c & 0x3f );
162 } else if( c < 0x200000 ) {
163 p[len++] = 0xf0 | ( c >> 18 );
164 p[len++] = 0x80 | ( (c >> 12) & 0x3f );
165 p[len++] = 0x80 | ( (c >> 6) & 0x3f );
166 p[len++] = 0x80 | ( c & 0x3f );
168 } else if( c < 0x400000 ) {
169 p[len++] = 0xf8 | ( c >> 24 );
170 p[len++] = 0x80 | ( (c >> 18) & 0x3f );
171 p[len++] = 0x80 | ( (c >> 12) & 0x3f );
172 p[len++] = 0x80 | ( (c >> 6) & 0x3f );
173 p[len++] = 0x80 | ( c & 0x3f );
175 } else /* if( c < 0x80000000 ) */ {
176 p[len++] = 0xfc | ( c >> 30 );
177 p[len++] = 0x80 | ( (c >> 24) & 0x3f );
178 p[len++] = 0x80 | ( (c >> 18) & 0x3f );
179 p[len++] = 0x80 | ( (c >> 12) & 0x3f );
180 p[len++] = 0x80 | ( (c >> 6) & 0x3f );
181 p[len++] = 0x80 | ( c & 0x3f );
189 * Advance to the next UTF-8 character
191 * Ignores length of multibyte character, instead rely on
192 * continuation markers to find start of next character.
193 * This allows for "resyncing" of when invalid characters
194 * are provided provided the start of the next character
195 * is appears within the 6 bytes examined.
197 char* ldap_utf8_next( const char * p )
200 const unsigned char *u = p;
202 if( LDAP_UTF8_ISASCII(u) ) {
203 return (char *) &p[1];
206 for( i=1; i<6; i++ ) {
207 if ( u[i] & 0xc0 != 0x80 ) {
208 return (char *) &p[i];
212 return (char *) &p[i];
216 * Advance to the previous UTF-8 character
218 * Ignores length of multibyte character, instead rely on
219 * continuation markers to find start of next character.
220 * This allows for "resyncing" of when invalid characters
221 * are provided provided the start of the next character
222 * is appears within the 6 bytes examined.
224 char* ldap_utf8_prev( const char * p )
227 const unsigned char *u = p;
229 for( i=-1; i>-6 ; i-- ) {
230 if ( u[i] & 0xc0 != 0x80 ) {
231 return (char *) &p[i];
235 return (char *) &p[i];
239 * Copy one UTF-8 character from src to dst returning
240 * number of bytes copied.
242 * Ignores length of multibyte character, instead rely on
243 * continuation markers to find start of next character.
244 * This allows for "resyncing" of when invalid characters
245 * are provided provided the start of the next character
246 * is appears within the 6 bytes examined.
248 int ldap_utf8_copy( char* dst, const char *src )
251 const unsigned char *u = src;
255 if( LDAP_UTF8_ISASCII(u) ) {
259 for( i=1; i<6; i++ ) {
260 if ( u[i] & 0xc0 != 0x80 ) {
270 * UTF-8 ctype routines
271 * Only deals with characters < 0x100 (ie: US-ASCII)
274 int ldap_utf8_isascii( const char * p )
276 unsigned c = * (const unsigned char *) p;
280 int ldap_utf8_isdigit( const char * p )
282 unsigned c = * (const unsigned char *) p;
284 if(!ISASCII(c)) return 0;
286 return c >= '0' && c <= '9';
289 int ldap_utf8_isxdigit( const char * p )
291 unsigned c = * (const unsigned char *) p;
293 if(!ISASCII(c)) return 0;
295 return ( c >= '0' && c <= '9' )
296 || ( c >= 'A' && c <= 'F' )
297 || ( c >= 'a' && c <= 'f' );
300 int ldap_utf8_isspace( const char * p )
302 unsigned c = * (const unsigned char *) p;
304 if(!ISASCII(c)) return 0;
319 #ifndef UTF8_ALPHA_CTYPE
321 * These are not needed by the C SDK and are
322 * not "good enough" for general use.
324 int ldap_utf8_isalpha( const char * p )
326 unsigned c = * (const unsigned char *) p;
328 if(!ISASCII(c)) return 0;
330 return ( c >= 'A' && c <= 'Z' )
331 || ( c >= 'a' && c <= 'z' );
334 int ldap_utf8_isalnum( const char * p )
336 unsigned c = * (const unsigned char *) p;
338 if(!ISASCII(c)) return 0;
340 return ( c >= '0' && c <= '9' )
341 || ( c >= 'A' && c <= 'Z' )
342 || ( c >= 'a' && c <= 'z' );
345 int ldap_utf8_islower( const char * p )
347 unsigned c = * (const unsigned char *) p;
349 if(!ISASCII(c)) return 0;
351 return ( c >= 'a' && c <= 'z' );
354 int ldap_utf8_isupper( const char * p )
356 unsigned c = * (const unsigned char *) p;
358 if(!ISASCII(c)) return 0;
360 return ( c >= 'A' && c <= 'Z' );
366 * UTF-8 string routines
370 char * (ldap_utf8_strchr)( const char *str, const char *chr )
372 for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
373 if( ldap_utf8_to_ucs4( str ) == ldap_utf8_to_ucs4( chr ) ) {
381 /* like strcspn() but returns number of bytes, not characters */
382 ber_len_t (ldap_utf8_strcspn)( const char *str, const char *set )
387 for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
388 for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
389 if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
398 /* like strspn() but returns number of bytes, not characters */
399 ber_len_t (ldap_utf8_strspn)( const char *str, const char *set )
404 for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
406 for( cset = set; ; LDAP_UTF8_INCR(cset) ) {
407 if( *cset == '\0' ) {
411 if( ldap_utf8_to_ucs4( cstr ) == ldap_utf8_to_ucs4( cset ) ) {
420 /* like strpbrk(), replaces strchr() as well */
421 char *(ldap_utf8_strpbrk)( const char *str, const char *set )
425 for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
428 for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
429 if( ldap_utf8_to_ucs4( str ) == ldap_utf8_to_ucs4( cset ) ) {
438 /* like strtok_r(), not strtok() */
439 char *(ldap_utf8_strtok)(char *str, const char *sep, char **last)
444 if( last == NULL ) return NULL;
446 begin = str ? str : *last;
448 begin += ldap_utf8_strspn( begin, sep );
450 if( *begin == '\0' ) {
455 end = &begin[ ldap_utf8_strcspn( begin, sep ) ];
458 char *next = LDAP_UTF8_NEXT( end );