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 struct berval * UTF8bvnormalize(
248 struct berval *newbv,
251 int i, j, len, clen, outpos, ucsoutlen, outsize, last;
253 unsigned long *ucs, *p, *ucsout;
255 static unsigned char mask[] = {
256 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
266 return ber_dupbv( newbv, bv );
269 /* FIXME: Should first check to see if string is already in
270 * proper normalized form. This is almost as time consuming
271 * as the normalization though.
274 /* finish off everything up to character before first non-ascii */
275 if ( LDAP_UTF8_ISASCII( s ) ) {
278 out = (char *) malloc( outsize );
284 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
285 out[outpos++] = TOUPPER( s[i-1] );
288 out[outpos++] = TOUPPER( s[len - 1] );
290 return ber_str2bv( out, outpos, 0, newbv);
293 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
298 return ber_str2bv( s, len, 1, newbv );
302 out = (char *) malloc( outsize );
307 memcpy(out, s, outpos);
311 out = (char *) malloc( outsize );
319 p = ucs = (long *) malloc( len * sizeof(*ucs) );
325 /* convert character before first non-ascii to ucs-4 */
327 *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
331 /* s[i] is now first non-ascii character */
333 /* s[i] is non-ascii */
334 /* convert everything up to next ascii to ucs-4 */
336 clen = LDAP_UTF8_CHARLEN2( s + i, clen );
346 *p = s[i] & mask[clen];
348 for( j = 1; j < clen; j++ ) {
349 if ( (s[i] & 0xc0) != 0x80 ) {
359 *p = uctoupper( *p );
363 /* normalize ucs of length p - ucs */
364 uccanondecomp( ucs, p - ucs, &ucsout, &ucsoutlen );
365 ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
366 /* convert ucs to utf-8 and store in out */
367 for ( j = 0; j < ucsoutlen; j++ ) {
368 /* allocate more space if not enough room for
369 6 bytes and terminator */
370 if ( outsize - outpos < 7 ) {
371 outsize = ucsoutlen - j + outpos + 6;
372 out = (char *) realloc( out, outsize );
378 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
388 /* finish off everything up to char before next non-ascii */
389 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
390 out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
393 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
397 /* convert character before next non-ascii to ucs-4 */
398 *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
403 return ber_str2bv( out, outpos, 0, newbv );
406 /* compare UTF8-strings, optionally ignore casing, string pointers must not be NULL */
407 /* slow, should be optimized */
413 int i, l1, l2, len, ulen, res;
414 unsigned long *ucs, *ucsout1, *ucsout2;
419 if ( ( l1 == 0 ) || ( l2 == 0 ) ) {
423 return *s1 - *s2 > 0 ? 1 : -1;
426 /* See if we can get away with a straight ASCII compare */
427 len = (l1 < l2) ? l1 : l2;
428 for ( i = 0; i<len; i++ ) {
429 /* Is either char non-ASCII? */
430 if ((s1[i] & 0x80) || (s2[i] & 0x80))
433 char c1 = TOUPPER(s1[i]);
434 char c2 = TOUPPER(s2[i]);
442 /* Strings were ASCII, equal up to minlen */
446 /* FIXME: Should first check to see if strings are already in
447 * proper normalized form.
450 ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
452 return l1 > l2 ? 1 : -1; /* what to do??? */
456 * XXYYZ: we convert to ucs4 even though -llunicode
457 * expects ucs2 in an unsigned long
460 /* convert and normalize 1st string */
461 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
462 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
463 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
465 return -1; /* what to do??? */
467 len = LDAP_UTF8_CHARLEN( s1 + i );
469 uccanondecomp( ucs, ulen, &ucsout1, &l1 );
470 l1 = uccanoncomp( ucsout1, l1 );
472 /* convert and normalize 2nd string */
473 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
474 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
475 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
478 return 1; /* what to do??? */
480 len = LDAP_UTF8_CHARLEN( s2 + i );
482 uccanondecomp( ucs, ulen, &ucsout2, &l2 );
483 l2 = uccanoncomp( ucsout2, l2 );
488 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
489 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
499 return l1 > l2 ? 1 : -1;
502 /* compare UTF8-strings, optionally ignore casing */
503 /* slow, should be optimized */
509 int i, l1, l2, len, ulen, res;
510 char *s1, *s2, *done;
511 unsigned long *ucs, *ucsout1, *ucsout2;
514 return bv2 == NULL ? 0 : -1;
515 } else if (bv2 == NULL) {
522 len = (l1 < l2) ? l1 : l2;
524 return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
531 while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
533 char c1 = TOUPPER(*s1);
534 char c2 = TOUPPER(*s2);
542 /* done unless next character in s1 or s2 is non-ascii */
544 if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
547 } else if ((len < l1) && !LDAP_UTF8_ISASCII(s1) ||
548 (len < l2) && !LDAP_UTF8_ISASCII(s2)) {
555 /* We have encountered non-ascii or strings equal up to len */
557 /* set i to number of iterations */
559 /* passed through loop at least once? */
561 if (!res && (s1 == done) &&
562 ((len == l1) || LDAP_UTF8_ISASCII(s1)) &&
563 ((len == l2) || LDAP_UTF8_ISASCII(s2))) {
564 /* all ascii and equal up to len */
568 /* rewind one char, and do normalized compare from there */
575 /* FIXME: Should first check to see if strings are already in
576 * proper normalized form.
579 ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
581 return l1 > l2 ? 1 : -1; /* what to do??? */
585 * XXYYZ: we convert to ucs4 even though -llunicode
586 * expects ucs2 in an unsigned long
589 /* convert and normalize 1st string */
590 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
591 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
592 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
594 return -1; /* what to do??? */
596 len = LDAP_UTF8_CHARLEN( s1 + i );
598 uccanondecomp( ucs, ulen, &ucsout1, &l1 );
599 l1 = uccanoncomp( ucsout1, l1 );
601 /* convert and normalize 2nd string */
602 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
603 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
604 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
607 return 1; /* what to do??? */
609 len = LDAP_UTF8_CHARLEN( s2 + i );
611 uccanondecomp( ucs, ulen, &ucsout2, &l2 );
612 l2 = uccanoncomp( ucsout2, l2 );
617 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
618 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
628 return l1 > l2 ? 1 : -1;