2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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;
113 char *out, *outtmp, *s;
114 ac_uint4 *ucs, *p, *ucsout;
116 static unsigned char mask[] = {
117 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
119 unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
120 unsigned approx = flags & LDAP_UTF8_APPROX;
130 return ber_dupbv_x( newbv, bv, ctx );
134 newbv = ber_memalloc_x( sizeof(struct berval), ctx );
135 if ( !newbv ) return NULL;
139 /* Should first check to see if string is already in proper
140 * normalized form. This is almost as time consuming as
141 * the normalization though.
144 /* finish off everything up to character before first non-ascii */
145 if ( LDAP_UTF8_ISASCII( s ) ) {
148 out = (char *) ber_memalloc_x( outsize, ctx );
152 ber_memfree_x( newbv, ctx );
157 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
158 out[outpos++] = TOLOWER( s[i-1] );
161 out[outpos++] = TOLOWER( s[len-1] );
164 newbv->bv_len = outpos;
168 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
173 return ber_str2bv_x( s, len, 1, newbv, ctx );
177 out = (char *) ber_memalloc_x( outsize, ctx );
182 memcpy(out, s, outpos);
186 out = (char *) ber_memalloc_x( outsize, ctx );
194 p = ucs = ber_memalloc_x( len * sizeof(*ucs), ctx );
196 ber_memfree_x(out, ctx);
200 /* convert character before first non-ascii to ucs-4 */
202 *p = casefold ? TOLOWER( s[i-1] ) : s[i-1];
206 /* s[i] is now first non-ascii character */
208 /* s[i] is non-ascii */
209 /* convert everything up to next ascii to ucs-4 */
211 clen = LDAP_UTF8_CHARLEN2( s + i, clen );
213 ber_memfree_x( ucs, ctx );
214 ber_memfree_x( out, ctx );
221 *p = s[i] & mask[clen];
223 for( j = 1; j < clen; j++ ) {
224 if ( (s[i] & 0xc0) != 0x80 ) {
225 ber_memfree_x( ucs, ctx );
226 ber_memfree_x( out, ctx );
234 *p = uctolower( *p );
238 /* normalize ucs of length p - ucs */
239 uccompatdecomp( ucs, p - ucs, &ucsout, &ucsoutlen, ctx );
241 for ( j = 0; j < ucsoutlen; j++ ) {
242 if ( ucsout[j] < 0x80 ) {
243 out[outpos++] = ucsout[j];
247 ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
248 /* convert ucs to utf-8 and store in out */
249 for ( j = 0; j < ucsoutlen; j++ ) {
250 /* allocate more space if not enough room for
251 6 bytes and terminator */
252 if ( outsize - outpos < 7 ) {
253 outsize = ucsoutlen - j + outpos + 6;
254 outtmp = (char *) ber_memrealloc_x( out, outsize, ctx );
255 if ( outtmp == NULL ) {
256 ber_memfree_x( ucsout, ctx );
257 ber_memfree_x( ucs, ctx );
258 ber_memfree_x( out, ctx );
263 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
267 ber_memfree_x( ucsout, ctx );
276 /* Allocate more space in out if necessary */
277 if (len - i >= outsize - outpos) {
278 outsize += 1 + ((len - i) - (outsize - outpos));
279 outtmp = (char *) ber_memrealloc_x(out, outsize, ctx);
280 if (outtmp == NULL) {
281 ber_memfree_x( ucs, ctx );
282 ber_memfree_x( out, ctx );
289 /* finish off everything up to char before next non-ascii */
290 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
291 out[outpos++] = casefold ? TOLOWER( s[i-1] ) : s[i-1];
294 out[outpos++] = casefold ? TOLOWER( s[len-1] ) : s[len-1];
298 /* convert character before next non-ascii to ucs-4 */
299 *ucs = casefold ? TOLOWER( s[i-1] ) : s[i-1];
303 ber_memfree_x( ucs, ctx );
306 newbv->bv_len = outpos;
310 /* compare UTF8-strings, optionally ignore casing */
311 /* slow, should be optimized */
318 int i, l1, l2, len, ulen, res = 0;
319 char *s1, *s2, *done;
320 ac_uint4 *ucs, *ucsout1, *ucsout2;
322 unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
323 unsigned norm1 = flags & LDAP_UTF8_ARG1NFC;
324 unsigned norm2 = flags & LDAP_UTF8_ARG2NFC;
327 return bv2 == NULL ? 0 : -1;
329 } else if (bv2 == NULL) {
336 len = (l1 < l2) ? l1 : l2;
338 return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
345 while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
347 char c1 = TOLOWER(*s1);
348 char c2 = TOLOWER(*s2);
356 /* done unless next character in s1 or s2 is non-ascii */
358 if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
361 } else if (((len < l1) && !LDAP_UTF8_ISASCII(s1)) ||
362 ((len < l2) && !LDAP_UTF8_ISASCII(s2)))
370 /* We have encountered non-ascii or strings equal up to len */
372 /* set i to number of iterations */
374 /* passed through loop at least once? */
376 if (!res && (s1 == done) &&
377 ((len == l1) || LDAP_UTF8_ISASCII(s1)) &&
378 ((len == l2) || LDAP_UTF8_ISASCII(s2))) {
379 /* all ascii and equal up to len */
383 /* rewind one char, and do normalized compare from there */
390 /* Should first check to see if strings are already in
391 * proper normalized form.
393 ucs = malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
395 return l1 > l2 ? 1 : -1; /* what to do??? */
399 * XXYYZ: we convert to ucs4 even though -llunicode
400 * expects ucs2 in an ac_uint4
403 /* convert and normalize 1st string */
404 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
405 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
406 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
408 return -1; /* what to do??? */
410 len = LDAP_UTF8_CHARLEN( s1 + i );
416 ucs = malloc( l2 * sizeof(*ucs) );
419 return l1 > l2 ? 1 : -1; /* what to do??? */
422 uccompatdecomp( ucs, ulen, &ucsout1, &l1, ctx );
423 l1 = uccanoncomp( ucsout1, l1 );
426 /* convert and normalize 2nd string */
427 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
428 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
429 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
432 return 1; /* what to do??? */
434 len = LDAP_UTF8_CHARLEN( s2 + i );
441 uccompatdecomp( ucs, ulen, &ucsout2, &l2, ctx );
442 l2 = uccanoncomp( ucsout2, l2 );
447 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
448 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
458 return l1 > l2 ? 1 : -1;