]> git.sur5r.net Git - openldap/blob - libraries/liblunicode/ucstr.c
d964649addb5bb79d5ff4c40e38e12bd42b3424b
[openldap] / libraries / liblunicode / ucstr.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <ac/bytes.h>
19 #include <ac/ctype.h>
20 #include <ac/string.h>
21 #include <ac/stdlib.h>
22
23 #include <lber_pvt.h>
24
25 #include <ldap_utf8.h>
26 #include <ldap_pvt_uc.h>
27
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)
31
32 int ucstrncmp(
33         const ldap_unicode_t *u1,
34         const ldap_unicode_t *u2,
35         ber_len_t n )
36 {
37         for(; 0 < n; ++u1, ++u2, --n ) {
38                 if( *u1 != *u2 ) {
39                         return *u1 < *u2 ? -1 : +1;
40                 }
41                 if ( *u1 == 0 ) {
42                         return 0;
43                 }
44         }
45         return 0;
46 }
47
48 int ucstrncasecmp(
49         const ldap_unicode_t *u1,
50         const ldap_unicode_t *u2,
51         ber_len_t n )
52 {
53         for(; 0 < n; ++u1, ++u2, --n ) {
54                 ldap_unicode_t uu1 = uctolower( *u1 );
55                 ldap_unicode_t uu2 = uctolower( *u2 );
56
57                 if( uu1 != uu2 ) {
58                         return uu1 < uu2 ? -1 : +1;
59                 }
60                 if ( uu1 == 0 ) {
61                         return 0;
62                 }
63         }
64         return 0;
65 }
66
67 ldap_unicode_t * ucstrnchr(
68         const ldap_unicode_t *u,
69         ber_len_t n,
70         ldap_unicode_t c )
71 {
72         for(; 0 < n; ++u, --n ) {
73                 if( *u == c ) {
74                         return (ldap_unicode_t *) u;
75                 }
76         }
77
78         return NULL;
79 }
80
81 ldap_unicode_t * ucstrncasechr(
82         const ldap_unicode_t *u,
83         ber_len_t n,
84         ldap_unicode_t c )
85 {
86         c = uctolower( c );
87         for(; 0 < n; ++u, --n ) {
88                 if( uctolower( *u ) == c ) {
89                         return (ldap_unicode_t *) u;
90                 }
91         }
92
93         return NULL;
94 }
95
96 void ucstr2upper(
97         ldap_unicode_t *u,
98         ber_len_t n )
99 {
100         for(; 0 < n; ++u, --n ) {
101                 *u = uctoupper( *u );
102         }
103 }
104
105 struct berval * UTF8bvnormalize(
106         struct berval *bv,
107         struct berval *newbv,
108         unsigned flags,
109         void *ctx )
110 {
111         int i, j, len, clen, outpos, ucsoutlen, outsize, last;
112         char *out, *outtmp, *s;
113         ac_uint4 *ucs, *p, *ucsout;
114
115         static unsigned char mask[] = {
116                 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
117
118         unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
119         unsigned approx = flags & LDAP_UTF8_APPROX;
120
121         if ( bv == NULL ) {
122                 return NULL;
123         }
124
125         s = bv->bv_val;
126         len = bv->bv_len;
127
128         if ( len == 0 ) {
129                 return ber_dupbv_x( newbv, bv, ctx );
130         }
131         
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.
135          */
136
137         /* finish off everything up to character before first non-ascii */
138         if ( LDAP_UTF8_ISASCII( s ) ) {
139                 if ( casefold ) {
140                         outsize = len + 7;
141                         out = (char *) malloc( outsize );
142                         if ( out == NULL ) {
143                                 return NULL;
144                         }
145                         outpos = 0;
146
147                         for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
148                                 out[outpos++] = TOLOWER( s[i-1] );
149                         }
150                         if ( i == len ) {
151                                 out[outpos++] = TOLOWER( s[len-1] );
152                                 out[outpos] = '\0';
153                                 return ber_str2bv( out, outpos, 0, newbv);
154                         }
155                 } else {
156                         for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
157                                 /* empty */
158                         }
159
160                         if ( i == len ) {
161                                 return ber_str2bv_x( s, len, 1, newbv, ctx );
162                         }
163                                 
164                         outsize = len + 7;
165                         out = (char *) malloc( outsize );
166                         if ( out == NULL ) {
167                                 return NULL;
168                         }
169                         outpos = i - 1;
170                         memcpy(out, s, outpos);
171                 }
172         } else {
173                 outsize = len + 7;
174                 out = (char *) malloc( outsize );
175                 if ( out == NULL ) {
176                         return NULL;
177                 }
178                 outpos = 0;
179                 i = 0;
180         }
181
182         p = ucs = malloc( len * sizeof(*ucs) );
183         if ( ucs == NULL ) {
184                 free(out);
185                 return NULL;
186         }
187
188         /* convert character before first non-ascii to ucs-4 */
189         if ( i > 0 ) {
190                 *p = casefold ? TOLOWER( s[i-1] ) : s[i-1];
191                 p++;
192         }
193
194         /* s[i] is now first non-ascii character */
195         for (;;) {
196                 /* s[i] is non-ascii */
197                 /* convert everything up to next ascii to ucs-4 */
198                 while ( i < len ) {
199                         clen = LDAP_UTF8_CHARLEN2( s + i, clen );
200                         if ( clen == 0 ) {
201                                 free( ucs );
202                                 free( out );
203                                 return NULL;
204                         }
205                         if ( clen == 1 ) {
206                                 /* ascii */
207                                 break;
208                         }
209                         *p = s[i] & mask[clen];
210                         i++;
211                         for( j = 1; j < clen; j++ ) {
212                                 if ( (s[i] & 0xc0) != 0x80 ) {
213                                         free( ucs );
214                                         free( out );
215                                         return NULL;
216                                 }
217                                 *p <<= 6;
218                                 *p |= s[i] & 0x3f;
219                                 i++;
220                         }
221                         if ( casefold ) {
222                                 *p = uctolower( *p );
223                         }
224                         p++;
225                 }
226                 /* normalize ucs of length p - ucs */
227                 uccompatdecomp( ucs, p - ucs, &ucsout, &ucsoutlen, ctx );
228                 if ( approx ) {
229                         for ( j = 0; j < ucsoutlen; j++ ) {
230                                 if ( ucsout[j] < 0x80 ) {
231                                         out[outpos++] = ucsout[j];
232                                 }
233                         }
234                 } else {
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 ) {
244                                                 free( out );
245                                                 free( ucs );
246                                                 free( ucsout );
247                                                 return NULL;
248                                         }
249                                         out = outtmp;
250                                 }
251                                 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
252                         }
253                 }
254
255                 free( ucsout );
256                 ucsout = NULL;
257                 
258                 if ( i == len ) {
259                         break;
260                 }
261
262                 last = i;
263
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) {
269                                 free(out);
270                                 free(ucs);
271                                 return NULL;
272                         }
273                         out = outtmp;
274                 }
275
276                 /* s[i] is ascii */
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];
280                 }
281                 if ( i == len ) {
282                         out[outpos++] = casefold ? TOLOWER( s[len-1] ) : s[len-1];
283                         break;
284                 }
285
286                 /* convert character before next non-ascii to ucs-4 */
287                 *ucs = casefold ? TOLOWER( s[i-1] ) : s[i-1];
288                 p = ucs + 1;
289         }
290
291         free( ucs );
292         out[outpos] = '\0';
293         return ber_str2bv( out, outpos, 0, newbv );
294 }
295
296 /* compare UTF8-strings, optionally ignore casing */
297 /* slow, should be optimized */
298 int UTF8bvnormcmp(
299         struct berval *bv1,
300         struct berval *bv2,
301         unsigned flags,
302         void *ctx )
303 {
304         int i, l1, l2, len, ulen, res = 0;
305         char *s1, *s2, *done;
306         ac_uint4 *ucs, *ucsout1, *ucsout2;
307
308         unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
309         unsigned norm1 = flags & LDAP_UTF8_ARG1NFC;
310         unsigned norm2 = flags & LDAP_UTF8_ARG2NFC;
311
312         if (bv1 == NULL) {
313                 return bv2 == NULL ? 0 : -1;
314
315         } else if (bv2 == NULL) {
316                 return 1;
317         }
318
319         l1 = bv1->bv_len;
320         l2 = bv2->bv_len;
321
322         len = (l1 < l2) ? l1 : l2;
323         if (len == 0) {
324                 return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
325         }
326
327         s1 = bv1->bv_val;
328         s2 = bv2->bv_val;
329         done = s1 + len;
330
331         while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
332                 if (casefold) {
333                         char c1 = TOLOWER(*s1);
334                         char c2 = TOLOWER(*s2);
335                         res = c1 - c2;
336                 } else {
337                         res = *s1 - *s2;
338                 }                       
339                 s1++;
340                 s2++;
341                 if (res) {
342                         /* done unless next character in s1 or s2 is non-ascii */
343                         if (s1 < done) {
344                                 if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
345                                         break;
346                                 }
347                         } else if (((len < l1) && !LDAP_UTF8_ISASCII(s1)) ||
348                                 ((len < l2) && !LDAP_UTF8_ISASCII(s2)))
349                         {
350                                 break;
351                         }
352                         return res;
353                 }
354         }
355
356         /* We have encountered non-ascii or strings equal up to len */
357
358         /* set i to number of iterations */
359         i = s1 - done + len;
360         /* passed through loop at least once? */
361         if (i > 0) {
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 */
366                         return l1 - l2;
367                 }
368
369                 /* rewind one char, and do normalized compare from there */
370                 s1--;
371                 s2--;
372                 l1 -= i - 1;
373                 l2 -= i - 1;
374         }
375                         
376         /* Should first check to see if strings are already in
377          * proper normalized form.
378          */
379         ucs = malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
380         if ( ucs == NULL ) {
381                 return l1 > l2 ? 1 : -1; /* what to do??? */
382         }
383         
384         /*
385          * XXYYZ: we convert to ucs4 even though -llunicode
386          * expects ucs2 in an ac_uint4
387          */
388         
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 ) {
393                         free( ucs );
394                         return -1; /* what to do??? */
395                 }
396                 len = LDAP_UTF8_CHARLEN( s1 + i );
397         }
398
399         if ( norm1 ) {
400                 ucsout1 = ucs;
401                 l1 = ulen;
402                 ucs = malloc( l2 * sizeof(*ucs) );
403                 if ( ucs == NULL ) {
404                         free( ucsout1 );
405                         return l1 > l2 ? 1 : -1; /* what to do??? */
406                 }
407         } else {
408                 uccompatdecomp( ucs, ulen, &ucsout1, &l1, ctx );
409                 l1 = uccanoncomp( ucsout1, l1 );
410         }
411
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 ) {
416                         free( ucsout1 );
417                         free( ucs );
418                         return 1; /* what to do??? */
419                 }
420                 len = LDAP_UTF8_CHARLEN( s2 + i );
421         }
422
423         if ( norm2 ) {
424                 ucsout2 = ucs;
425                 l2 = ulen;
426         } else {
427                 uccompatdecomp( ucs, ulen, &ucsout2, &l2, ctx );
428                 l2 = uccanoncomp( ucsout2, l2 );
429                 free( ucs );
430         }
431         
432         res = casefold
433                 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
434                 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
435         free( ucsout1 );
436         free( ucsout2 );
437
438         if ( res != 0 ) {
439                 return res;
440         }
441         if ( l1 == l2 ) {
442                 return 0;
443         }
444         return l1 > l2 ? 1 : -1;
445 }