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