2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/stdlib.h>
25 #include <ldap_utf8.h>
26 #include <ldap_pvt_uc.h>
28 #define malloc(x) ber_memalloc_x(x,ctx)
29 #define realloc(x,y) ber_memrealloc_x(x,y,ctx)
30 #define free(x) ber_memfree_x(x,ctx)
33 const ldap_unicode_t *u1,
34 const ldap_unicode_t *u2,
37 for(; 0 < n; ++u1, ++u2, --n ) {
39 return *u1 < *u2 ? -1 : +1;
49 const ldap_unicode_t *u1,
50 const ldap_unicode_t *u2,
53 for(; 0 < n; ++u1, ++u2, --n ) {
54 ldap_unicode_t uu1 = uctolower( *u1 );
55 ldap_unicode_t uu2 = uctolower( *u2 );
58 return uu1 < uu2 ? -1 : +1;
67 ldap_unicode_t * ucstrnchr(
68 const ldap_unicode_t *u,
72 for(; 0 < n; ++u, --n ) {
74 return (ldap_unicode_t *) u;
81 ldap_unicode_t * ucstrncasechr(
82 const ldap_unicode_t *u,
87 for(; 0 < n; ++u, --n ) {
88 if( uctolower( *u ) == c ) {
89 return (ldap_unicode_t *) u;
100 for(; 0 < n; ++u, --n ) {
101 *u = uctoupper( *u );
105 struct berval * UTF8bvnormalize(
107 struct berval *newbv,
111 int i, j, len, clen, outpos, ucsoutlen, outsize, last;
112 char *out, *outtmp, *s;
113 ac_uint4 *ucs, *p, *ucsout;
115 static unsigned char mask[] = {
116 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
118 unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
119 unsigned approx = flags & LDAP_UTF8_APPROX;
129 return ber_dupbv_x( newbv, bv, ctx );
132 /* Should first check to see if string is already in proper
133 * normalized form. This is almost as time consuming as
134 * the normalization though.
137 /* finish off everything up to character before first non-ascii */
138 if ( LDAP_UTF8_ISASCII( s ) ) {
141 out = (char *) malloc( outsize );
147 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
148 out[outpos++] = TOLOWER( s[i-1] );
151 out[outpos++] = TOLOWER( s[len-1] );
153 return ber_str2bv( out, outpos, 0, newbv);
156 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
161 return ber_str2bv_x( s, len, 1, newbv, ctx );
165 out = (char *) malloc( outsize );
170 memcpy(out, s, outpos);
174 out = (char *) malloc( outsize );
182 p = ucs = malloc( len * sizeof(*ucs) );
188 /* convert character before first non-ascii to ucs-4 */
190 *p = casefold ? TOLOWER( s[i-1] ) : s[i-1];
194 /* s[i] is now first non-ascii character */
196 /* s[i] is non-ascii */
197 /* convert everything up to next ascii to ucs-4 */
199 clen = LDAP_UTF8_CHARLEN2( s + i, clen );
209 *p = s[i] & mask[clen];
211 for( j = 1; j < clen; j++ ) {
212 if ( (s[i] & 0xc0) != 0x80 ) {
222 *p = uctolower( *p );
226 /* normalize ucs of length p - ucs */
227 uccompatdecomp( ucs, p - ucs, &ucsout, &ucsoutlen, ctx );
229 for ( j = 0; j < ucsoutlen; j++ ) {
230 if ( ucsout[j] < 0x80 ) {
231 out[outpos++] = ucsout[j];
235 ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
236 /* convert ucs to utf-8 and store in out */
237 for ( j = 0; j < ucsoutlen; j++ ) {
238 /* allocate more space if not enough room for
239 6 bytes and terminator */
240 if ( outsize - outpos < 7 ) {
241 outsize = ucsoutlen - j + outpos + 6;
242 outtmp = (char *) realloc( out, outsize );
243 if ( outtmp == NULL ) {
251 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
264 /* Allocate more space in out if necessary */
265 if (len - i >= outsize - outpos) {
266 outsize += 1 + ((len - i) - (outsize - outpos));
267 outtmp = (char *) realloc(out, outsize);
268 if (outtmp == NULL) {
277 /* finish off everything up to char before next non-ascii */
278 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
279 out[outpos++] = casefold ? TOLOWER( s[i-1] ) : s[i-1];
282 out[outpos++] = casefold ? TOLOWER( s[len-1] ) : s[len-1];
286 /* convert character before next non-ascii to ucs-4 */
287 *ucs = casefold ? TOLOWER( s[i-1] ) : s[i-1];
293 return ber_str2bv( out, outpos, 0, newbv );
296 /* compare UTF8-strings, optionally ignore casing */
297 /* slow, should be optimized */
304 int i, l1, l2, len, ulen, res = 0;
305 char *s1, *s2, *done;
306 ac_uint4 *ucs, *ucsout1, *ucsout2;
308 unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
309 unsigned norm1 = flags & LDAP_UTF8_ARG1NFC;
310 unsigned norm2 = flags & LDAP_UTF8_ARG2NFC;
313 return bv2 == NULL ? 0 : -1;
315 } else if (bv2 == NULL) {
322 len = (l1 < l2) ? l1 : l2;
324 return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
331 while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
333 char c1 = TOLOWER(*s1);
334 char c2 = TOLOWER(*s2);
342 /* done unless next character in s1 or s2 is non-ascii */
344 if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
347 } else if (((len < l1) && !LDAP_UTF8_ISASCII(s1)) ||
348 ((len < l2) && !LDAP_UTF8_ISASCII(s2)))
356 /* We have encountered non-ascii or strings equal up to len */
358 /* set i to number of iterations */
360 /* passed through loop at least once? */
362 if (!res && (s1 == done) &&
363 ((len == l1) || LDAP_UTF8_ISASCII(s1)) &&
364 ((len == l2) || LDAP_UTF8_ISASCII(s2))) {
365 /* all ascii and equal up to len */
369 /* rewind one char, and do normalized compare from there */
376 /* Should first check to see if strings are already in
377 * proper normalized form.
379 ucs = malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
381 return l1 > l2 ? 1 : -1; /* what to do??? */
385 * XXYYZ: we convert to ucs4 even though -llunicode
386 * expects ucs2 in an ac_uint4
389 /* convert and normalize 1st string */
390 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
391 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
392 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
394 return -1; /* what to do??? */
396 len = LDAP_UTF8_CHARLEN( s1 + i );
402 ucs = malloc( l2 * sizeof(*ucs) );
405 return l1 > l2 ? 1 : -1; /* what to do??? */
408 uccompatdecomp( ucs, ulen, &ucsout1, &l1, ctx );
409 l1 = uccanoncomp( ucsout1, l1 );
412 /* convert and normalize 2nd string */
413 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
414 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
415 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
418 return 1; /* what to do??? */
420 len = LDAP_UTF8_CHARLEN( s2 + i );
427 uccompatdecomp( ucs, ulen, &ucsout2, &l2, ctx );
428 l2 = uccanoncomp( ucsout2, l2 );
433 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
434 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
444 return l1 > l2 ? 1 : -1;