2 * Copyright 2000-2003 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 #include <ldap_utf8.h>
16 #include <ldap_pvt_uc.h>
18 #define malloc(x) ber_memalloc_x(x,ctx)
19 #define realloc(x,y) ber_memrealloc_x(x,y,ctx)
20 #define free(x) ber_memfree_x(x,ctx)
23 const ldap_unicode_t *u1,
24 const ldap_unicode_t *u2,
27 for(; 0 < n; ++u1, ++u2, --n ) {
29 return *u1 < *u2 ? -1 : +1;
39 const ldap_unicode_t *u1,
40 const ldap_unicode_t *u2,
43 for(; 0 < n; ++u1, ++u2, --n ) {
44 ldap_unicode_t uu1 = uctolower( *u1 );
45 ldap_unicode_t uu2 = uctolower( *u2 );
48 return uu1 < uu2 ? -1 : +1;
57 ldap_unicode_t * ucstrnchr(
58 const ldap_unicode_t *u,
62 for(; 0 < n; ++u, --n ) {
64 return (ldap_unicode_t *) u;
71 ldap_unicode_t * ucstrncasechr(
72 const ldap_unicode_t *u,
77 for(; 0 < n; ++u, --n ) {
78 if( uctolower( *u ) == c ) {
79 return (ldap_unicode_t *) u;
90 for(; 0 < n; ++u, --n ) {
95 struct berval * UTF8bvnormalize(
101 int i, j, len, clen, outpos, ucsoutlen, outsize, last;
102 char *out, *outtmp, *s;
103 unsigned long *ucs, *p, *ucsout;
105 static unsigned char mask[] = {
106 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
108 unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
109 unsigned approx = flags & LDAP_UTF8_APPROX;
119 return ber_dupbv_x( newbv, bv, ctx );
122 /* Should first check to see if string is already in proper
123 * normalized form. This is almost as time consuming as
124 * the normalization though.
127 /* finish off everything up to character before first non-ascii */
128 if ( LDAP_UTF8_ISASCII( s ) ) {
131 out = (char *) malloc( outsize );
137 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
138 out[outpos++] = TOLOWER( s[i-1] );
141 out[outpos++] = TOLOWER( s[len-1] );
143 return ber_str2bv( out, outpos, 0, newbv);
146 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
151 return ber_str2bv_x( s, len, 1, newbv, ctx );
155 out = (char *) malloc( outsize );
160 memcpy(out, s, outpos);
164 out = (char *) malloc( outsize );
172 p = ucs = malloc( len * sizeof(*ucs) );
178 /* convert character before first non-ascii to ucs-4 */
180 *p = casefold ? TOLOWER( s[i-1] ) : s[i-1];
184 /* s[i] is now first non-ascii character */
186 /* s[i] is non-ascii */
187 /* convert everything up to next ascii to ucs-4 */
189 clen = LDAP_UTF8_CHARLEN2( s + i, clen );
199 *p = s[i] & mask[clen];
201 for( j = 1; j < clen; j++ ) {
202 if ( (s[i] & 0xc0) != 0x80 ) {
212 *p = uctolower( *p );
216 /* normalize ucs of length p - ucs */
217 uccompatdecomp( ucs, p - ucs, &ucsout, &ucsoutlen, ctx );
219 for ( j = 0; j < ucsoutlen; j++ ) {
220 if ( ucsout[j] < 0x80 ) {
221 out[outpos++] = ucsout[j];
225 ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
226 /* convert ucs to utf-8 and store in out */
227 for ( j = 0; j < ucsoutlen; j++ ) {
228 /* allocate more space if not enough room for
229 6 bytes and terminator */
230 if ( outsize - outpos < 7 ) {
231 outsize = ucsoutlen - j + outpos + 6;
232 outtmp = (char *) realloc( out, outsize );
233 if ( outtmp == NULL ) {
241 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
255 /* finish off everything up to char before next non-ascii */
256 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
257 out[outpos++] = casefold ? TOLOWER( s[i-1] ) : s[i-1];
260 out[outpos++] = casefold ? TOLOWER( s[len-1] ) : s[len-1];
264 /* convert character before next non-ascii to ucs-4 */
265 *ucs = casefold ? TOLOWER( s[i-1] ) : s[i-1];
271 return ber_str2bv( out, outpos, 0, newbv );
274 /* compare UTF8-strings, optionally ignore casing */
275 /* slow, should be optimized */
282 int i, l1, l2, len, ulen, res = 0;
283 char *s1, *s2, *done;
284 unsigned long *ucs, *ucsout1, *ucsout2;
286 unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
287 unsigned norm1 = flags & LDAP_UTF8_ARG1NFC;
288 unsigned norm2 = flags & LDAP_UTF8_ARG2NFC;
291 return bv2 == NULL ? 0 : -1;
293 } else if (bv2 == NULL) {
300 len = (l1 < l2) ? l1 : l2;
302 return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
309 while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
311 char c1 = TOLOWER(*s1);
312 char c2 = TOLOWER(*s2);
320 /* done unless next character in s1 or s2 is non-ascii */
322 if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
325 } else if (((len < l1) && !LDAP_UTF8_ISASCII(s1)) ||
326 ((len < l2) && !LDAP_UTF8_ISASCII(s2)))
334 /* We have encountered non-ascii or strings equal up to len */
336 /* set i to number of iterations */
338 /* passed through loop at least once? */
340 if (!res && (s1 == done) &&
341 ((len == l1) || LDAP_UTF8_ISASCII(s1)) &&
342 ((len == l2) || LDAP_UTF8_ISASCII(s2))) {
343 /* all ascii and equal up to len */
347 /* rewind one char, and do normalized compare from there */
354 /* Should first check to see if strings are already in
355 * proper normalized form.
357 ucs = malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
359 return l1 > l2 ? 1 : -1; /* what to do??? */
363 * XXYYZ: we convert to ucs4 even though -llunicode
364 * expects ucs2 in an unsigned long
367 /* convert and normalize 1st string */
368 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
369 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
370 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
372 return -1; /* what to do??? */
374 len = LDAP_UTF8_CHARLEN( s1 + i );
380 ucs = malloc( l2 * sizeof(*ucs) );
382 return l1 > l2 ? 1 : -1; /* what to do??? */
385 uccompatdecomp( ucs, ulen, &ucsout1, &l1, ctx );
386 l1 = uccanoncomp( ucsout1, l1 );
389 /* convert and normalize 2nd string */
390 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
391 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
392 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
395 return 1; /* what to do??? */
397 len = LDAP_UTF8_CHARLEN( s2 + i );
404 uccompatdecomp( ucs, ulen, &ucsout2, &l2, ctx );
405 l2 = uccanoncomp( ucsout2, l2 );
410 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
411 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
421 return l1 > l2 ? 1 : -1;