2 * Copyright 2000-2002 The OpenLDAP Foundation
3 * COPYING RESTRICTIONS APPLY. See COPYRIGHT File in top level directory
4 * of this package for details.
10 #include <ac/string.h>
11 #include <ac/stdlib.h>
15 #define malloc(x) ber_memalloc(x)
16 #define realloc(x,y) ber_memrealloc(x,y)
17 #define free(x) ber_memfree(x)
19 #include <ldap_utf8.h>
20 #include <ldap_pvt_uc.h>
24 const ldap_unicode_t *u1,
25 const ldap_unicode_t *u2,
28 for(; 0 < n; ++u1, ++u2, --n ) {
30 return *u1 < *u2 ? -1 : +1;
40 const ldap_unicode_t *u1,
41 const ldap_unicode_t *u2,
44 for(; 0 < n; ++u1, ++u2, --n ) {
45 ldap_unicode_t uu1 = uctoupper( *u1 );
46 ldap_unicode_t uu2 = uctoupper( *u2 );
49 return uu1 < uu2 ? -1 : +1;
58 ldap_unicode_t * ucstrnchr(
59 const ldap_unicode_t *u,
63 for(; 0 < n; ++u, --n ) {
65 return (ldap_unicode_t *) u;
72 ldap_unicode_t * ucstrncasechr(
73 const ldap_unicode_t *u,
78 for(; 0 < n; ++u, --n ) {
79 if( uctoupper( *u ) == c ) {
80 return (ldap_unicode_t *) u;
91 for(; 0 < n; ++u, --n ) {
100 int i, j, len, clen, outpos, ucsoutlen, outsize, last;
102 unsigned long *ucs, *p, *ucsout;
104 static unsigned char mask[] = {
105 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
114 /* See if the string is pure ASCII so we can shortcut */
115 for ( i=0; i<len; i++ ) {
116 if ( s[i] & 0x80 ) /* non-ASCII */
120 /* It's pure ASCII or zero-len */
122 out = malloc( len + 1 );
123 if ( i && !casefold ) {
124 strncpy( out, bv->bv_val, len );
126 for ( j=0; j<i; j++ )
127 out[j] = TOUPPER( s[j] );
134 out = (char *) malloc( outsize );
139 /* FIXME: Should first check to see if string is already in
140 * proper normalized form.
145 /* finish off everything up to character before first non-ascii */
146 if ( LDAP_UTF8_ISASCII( s ) ) {
147 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
148 out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
151 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
159 p = ucs = (long *) malloc( len * sizeof(*ucs) );
165 /* convert character before first non-ascii to ucs-4 */
167 *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
171 /* s[i] is now first non-ascii character */
173 /* s[i] is non-ascii */
174 /* convert everything up to next ascii to ucs-4 */
176 clen = LDAP_UTF8_CHARLEN2( s + i, clen );
186 *p = s[i] & mask[clen];
188 for( j = 1; j < clen; j++ ) {
189 if ( (s[i] & 0xc0) != 0x80 ) {
199 *p = uctoupper( *p );
203 /* normalize ucs of length p - ucs */
204 uccanondecomp( ucs, p - ucs, &ucsout, &ucsoutlen );
205 ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
206 /* convert ucs to utf-8 and store in out */
207 for ( j = 0; j < ucsoutlen; j++ ) {
208 /* allocate more space if not enough room for
209 6 bytes and terminator */
210 if ( outsize - outpos < 7 ) {
211 outsize = ucsoutlen - j + outpos + 6;
212 out = (char *) realloc( out, outsize );
218 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
228 /* finish off everything up to char before next non-ascii */
229 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
230 out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
233 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
237 /* convert character before next non-ascii to ucs-4 */
238 *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
246 /* compare UTF8-strings, optionally ignore casing, string pointers must not be NULL */
247 /* slow, should be optimized */
253 int i, l1, l2, len, ulen, res;
254 unsigned long *ucs, *ucsout1, *ucsout2;
259 if ( ( l1 == 0 ) || ( l2 == 0 ) ) {
263 return *s1 - *s2 > 0 ? 1 : -1;
266 /* See if we can get away with a straight ASCII compare */
267 len = (l1 < l2) ? l1 : l2;
268 for ( i = 0; i<len; i++ ) {
269 /* Is either char non-ASCII? */
270 if ((s1[i] & 0x80) || (s2[i] & 0x80))
273 char c1 = TOUPPER(s1[i]);
274 char c2 = TOUPPER(s2[i]);
282 /* Strings were ASCII, equal up to minlen */
286 /* FIXME: Should first check to see if strings are already in
287 * proper normalized form.
290 ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
292 return l1 > l2 ? 1 : -1; /* what to do??? */
296 * XXYYZ: we convert to ucs4 even though -llunicode
297 * expects ucs2 in an unsigned long
300 /* convert and normalize 1st string */
301 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
302 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
303 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
305 return -1; /* what to do??? */
307 len = LDAP_UTF8_CHARLEN( s1 + i );
309 uccanondecomp( ucs, ulen, &ucsout1, &l1 );
310 l1 = uccanoncomp( ucsout1, l1 );
312 /* convert and normalize 2nd string */
313 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
314 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
315 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
318 return 1; /* what to do??? */
320 len = LDAP_UTF8_CHARLEN( s2 + i );
322 uccanondecomp( ucs, ulen, &ucsout2, &l2 );
323 l2 = uccanoncomp( ucsout2, l2 );
328 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
329 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
339 return l1 > l2 ? 1 : -1;