]> git.sur5r.net Git - openldap/blob - libraries/liblunicode/ucstr.c
Remove lint
[openldap] / libraries / liblunicode / ucstr.c
1 /*
2  * Copyright 2000-2003 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, *outtmp, *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                 uccompatdecomp( 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                                         outtmp = (char *) realloc( out, outsize );
231                                         if ( outtmp == NULL ) {
232                                                 free( out );
233                                                 free( ucs );
234                                                 free( ucsout );
235                                                 return NULL;
236                                         }
237                                         out = outtmp;
238                                 }
239                                 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
240                         }
241                 }
242
243                 free( ucsout );
244                 ucsout = NULL;
245                 
246                 if ( i == len ) {
247                         break;
248                 }
249
250                 last = i;
251
252                 /* s[i] is ascii */
253                 /* finish off everything up to char before next non-ascii */
254                 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
255                         out[outpos++] = casefold ? TOLOWER( s[i-1] ) : s[i-1];
256                 }
257                 if ( i == len ) {
258                         out[outpos++] = casefold ? TOLOWER( s[len - 1] ) : s[len - 1];
259                         break;
260                 }
261
262                 /* convert character before next non-ascii to ucs-4 */
263                 *ucs = casefold ? TOLOWER( s[i - 1] ) : s[i - 1];
264                 p = ucs + 1;
265         }               
266         free( ucs );
267         out[outpos] = '\0';
268         return ber_str2bv( out, outpos, 0, newbv );
269 }
270
271 /* compare UTF8-strings, optionally ignore casing */
272 /* slow, should be optimized */
273 int UTF8bvnormcmp(
274         struct berval *bv1,
275         struct berval *bv2,
276         unsigned flags )
277 {
278         int i, l1, l2, len, ulen, res = 0;
279         char *s1, *s2, *done;
280         unsigned long *ucs, *ucsout1, *ucsout2;
281         unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
282         unsigned norm1 = flags & LDAP_UTF8_ARG1NFC;
283         unsigned norm2 = flags & LDAP_UTF8_ARG2NFC;
284
285         if (bv1 == NULL) {
286                 return bv2 == NULL ? 0 : -1;
287         } else if (bv2 == NULL) {
288                 return 1;
289         }
290
291         l1 = bv1->bv_len;
292         l2 = bv2->bv_len;
293
294         len = (l1 < l2) ? l1 : l2;
295         if (len == 0) {
296                 return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
297         }
298
299         s1 = bv1->bv_val;
300         s2 = bv2->bv_val;
301         done = s1 + len;
302
303         while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
304                 if (casefold) {
305                         char c1 = TOLOWER(*s1);
306                         char c2 = TOLOWER(*s2);
307                         res = c1 - c2;
308                 } else {
309                         res = *s1 - *s2;
310                 }                       
311                 s1++;
312                 s2++;
313                 if (res) {
314                         /* done unless next character in s1 or s2 is non-ascii */
315                         if (s1 < done) {
316                                 if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
317                                         break;
318                                 }
319                         } else if (((len < l1) && !LDAP_UTF8_ISASCII(s1)) ||
320                                    ((len < l2) && !LDAP_UTF8_ISASCII(s2))) {
321                                 break;
322                         }
323                         return res;
324                 }
325         }
326
327         /* We have encountered non-ascii or strings equal up to len */
328
329         /* set i to number of iterations */
330         i = s1 - done + len;
331         /* passed through loop at least once? */
332         if (i > 0) {
333                 if (!res && (s1 == done) &&
334                     ((len == l1) || LDAP_UTF8_ISASCII(s1)) &&
335                     ((len == l2) || LDAP_UTF8_ISASCII(s2))) {
336                         /* all ascii and equal up to len */
337                         return l1 - l2;
338                 }
339
340                 /* rewind one char, and do normalized compare from there */
341                 s1--;
342                 s2--;
343                 l1 -= i - 1;
344                 l2 -= i - 1;
345         }
346                         
347         /* FIXME: Should first check to see if strings are already in
348          * proper normalized form.
349          */
350
351         ucs = malloc( ( ( norm1 || l1 > l2 ) ? l1 : l2 ) * sizeof(*ucs) );
352         if ( ucs == NULL ) {
353                 return l1 > l2 ? 1 : -1; /* what to do??? */
354         }
355         
356         /*
357          * XXYYZ: we convert to ucs4 even though -llunicode
358          * expects ucs2 in an unsigned long
359          */
360         
361         /* convert and normalize 1st string */
362         for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
363                 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
364                 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
365                         free( ucs );
366                         return -1; /* what to do??? */
367                 }
368                 len = LDAP_UTF8_CHARLEN( s1 + i );
369         }
370
371         if ( norm1 ) {
372                 ucsout1 = ucs;
373                 l1 = ulen;
374                 ucs = malloc( l2 * sizeof(*ucs) );
375                 if ( ucs == NULL ) {
376                         return l1 > l2 ? 1 : -1; /* what to do??? */
377                 }
378         } else {
379                 uccompatdecomp( ucs, ulen, &ucsout1, &l1 );
380                 l1 = uccanoncomp( ucsout1, l1 );
381         }
382
383         /* convert and normalize 2nd string */
384         for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
385                 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
386                 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
387                         free( ucsout1 );
388                         free( ucs );
389                         return 1; /* what to do??? */
390                 }
391                 len = LDAP_UTF8_CHARLEN( s2 + i );
392         }
393
394         if ( norm2 ) {
395                 ucsout2 = ucs;
396                 l2 = ulen;
397         } else {
398                 uccompatdecomp( ucs, ulen, &ucsout2, &l2 );
399                 l2 = uccanoncomp( ucsout2, l2 );
400                 free( ucs );
401         }
402         
403         res = casefold
404                 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
405                 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
406         free( ucsout1 );
407         free( ucsout2 );
408
409         if ( res != 0 ) {
410                 return res;
411         }
412         if ( l1 == l2 ) {
413                 return 0;
414         }
415         return l1 > l2 ? 1 : -1;
416 }