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] );
254 /* Allocate more space in out if necessary */
255 if (len - i >= outsize - outpos) {
256 outsize += 1 + ((len - i) - (outsize - outpos));
257 outtmp = (char *) realloc(out, outsize);
258 if (outtmp == NULL) {
267 /* finish off everything up to char before next non-ascii */
268 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
269 out[outpos++] = casefold ? TOLOWER( s[i-1] ) : s[i-1];
272 out[outpos++] = casefold ? TOLOWER( s[len-1] ) : s[len-1];
276 /* convert character before next non-ascii to ucs-4 */
277 *ucs = casefold ? TOLOWER( s[i-1] ) : s[i-1];
283 return ber_str2bv( out, outpos, 0, newbv );
286 /* compare UTF8-strings, optionally ignore casing */
287 /* slow, should be optimized */
294 int i, l1, l2, len, ulen, res = 0;
295 char *s1, *s2, *done;
296 unsigned long *ucs, *ucsout1, *ucsout2;
298 unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
299 unsigned norm1 = flags & LDAP_UTF8_ARG1NFC;
300 unsigned norm2 = flags & LDAP_UTF8_ARG2NFC;
303 return bv2 == NULL ? 0 : -1;
305 } else if (bv2 == NULL) {
312 len = (l1 < l2) ? l1 : l2;
314 return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
321 while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
323 char c1 = TOLOWER(*s1);
324 char c2 = TOLOWER(*s2);
332 /* done unless next character in s1 or s2 is non-ascii */
334 if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
337 } else if (((len < l1) && !LDAP_UTF8_ISASCII(s1)) ||
338 ((len < l2) && !LDAP_UTF8_ISASCII(s2)))
346 /* We have encountered non-ascii or strings equal up to len */
348 /* set i to number of iterations */
350 /* passed through loop at least once? */
352 if (!res && (s1 == done) &&
353 ((len == l1) || LDAP_UTF8_ISASCII(s1)) &&
354 ((len == l2) || LDAP_UTF8_ISASCII(s2))) {
355 /* all ascii and equal up to len */
359 /* rewind one char, and do normalized compare from there */
366 /* Should first check to see if strings are already in
367 * proper normalized form.
369 ucs = malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
371 return l1 > l2 ? 1 : -1; /* what to do??? */
375 * XXYYZ: we convert to ucs4 even though -llunicode
376 * expects ucs2 in an unsigned long
379 /* convert and normalize 1st string */
380 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
381 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
382 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
384 return -1; /* what to do??? */
386 len = LDAP_UTF8_CHARLEN( s1 + i );
392 ucs = malloc( l2 * sizeof(*ucs) );
394 return l1 > l2 ? 1 : -1; /* what to do??? */
397 uccompatdecomp( ucs, ulen, &ucsout1, &l1, ctx );
398 l1 = uccanoncomp( ucsout1, l1 );
401 /* convert and normalize 2nd string */
402 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
403 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
404 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
407 return 1; /* what to do??? */
409 len = LDAP_UTF8_CHARLEN( s2 + i );
416 uccompatdecomp( ucs, ulen, &ucsout2, &l2, ctx );
417 l2 = uccanoncomp( ucsout2, l2 );
422 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
423 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
433 return l1 > l2 ? 1 : -1;