]> git.sur5r.net Git - openldap/blob - libraries/liblunicode/ucstr.c
8ec9d2668f772ca3285c22f6533a499bae21c9ed
[openldap] / libraries / liblunicode / ucstr.c
1 #include "portable.h"
2
3 #include <ac/ctype.h>
4 #include <ac/string.h>
5 #include <ac/stdlib.h>
6
7 #include <lber.h>
8
9 #define malloc(x)       ber_memalloc(x)
10 #define realloc(x,y)    ber_memrealloc(x,y)
11 #define free(x)         ber_memfree(x)
12
13 #include <ldap_utf8.h>
14 #include <ldap_pvt_uc.h>
15
16
17 int ucstrncmp(
18         const ldap_unicode_t *u1,
19         const ldap_unicode_t *u2,
20         ber_len_t n )
21 {
22         for(; 0 < n; ++u1, ++u2, --n ) {
23                 if( *u1 != *u2 ) {
24                         return *u1 < *u2 ? -1 : +1;
25                 }
26                 if ( *u1 == 0 ) {
27                         return 0;
28                 }
29         }
30         return 0;
31 }
32
33 int ucstrncasecmp(
34         const ldap_unicode_t *u1,
35         const ldap_unicode_t *u2,
36         ber_len_t n )
37 {
38         for(; 0 < n; ++u1, ++u2, --n ) {
39                 ldap_unicode_t uu1 = uctoupper( *u1 );
40                 ldap_unicode_t uu2 = uctoupper( *u2 );
41
42                 if( uu1 != uu2 ) {
43                         return uu1 < uu2 ? -1 : +1;
44                 }
45                 if ( uu1 == 0 ) {
46                         return 0;
47                 }
48         }
49         return 0;
50 }
51
52 ldap_unicode_t * ucstrnchr(
53         const ldap_unicode_t *u,
54         ber_len_t n,
55         ldap_unicode_t c )
56 {
57         for(; 0 < n; ++u, --n ) {
58                 if( *u == c ) {
59                         return (ldap_unicode_t *) u;
60                 }
61         }
62
63         return NULL;
64 }
65
66 ldap_unicode_t * ucstrncasechr(
67         const ldap_unicode_t *u,
68         ber_len_t n,
69         ldap_unicode_t c )
70 {
71         c = uctoupper( c );
72         for(; 0 < n; ++u, --n ) {
73                 if( uctoupper( *u ) == c ) {
74                         return (ldap_unicode_t *) u;
75                 }
76         }
77
78         return NULL;
79 }
80
81 void ucstr2upper(
82         ldap_unicode_t *u,
83         ber_len_t n )
84 {
85         for(; 0 < n; ++u, --n ) {
86                 *u = uctoupper( *u );
87         }
88 }
89
90 char * UTF8normalize(
91         struct berval *bv,
92         unsigned casefold )
93 {
94         int i, j, len, clen, outpos, ucsoutlen, outsize, last;
95         char *out, *s;
96         unsigned long *ucs, *p, *ucsout;
97
98         static unsigned char mask[] = {
99                 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
100
101         if ( bv == NULL ) {
102                 return NULL;
103         }
104
105         s = bv->bv_val;
106         len = bv->bv_len;
107
108         /* See if the string is pure ASCII so we can shortcut */
109         for ( i=0; i<len; i++ ) {
110                 if ( s[i] & 0x80 )      /* non-ASCII */
111                         break;
112         }
113
114         /* It's pure ASCII or zero-len */
115         if ( i == len ) {
116                 out = malloc( len + 1 );
117                 if ( i && !casefold ) {
118                         strncpy( out, bv->bv_val, len );
119                 } else {
120                         for ( j=0; j<i; j++ )
121                                 out[j] = TOUPPER( s[j] );
122                 }
123                 out[len] = '\0';
124                 return out;
125         }
126
127         outsize = len + 7;
128         out = (char *) malloc( outsize );
129         if ( out == NULL ) {
130                 return NULL;
131         }
132
133         /* FIXME: Should first check to see if string is already in
134          * proper normalized form.
135          */
136
137         outpos = 0;
138
139         /* finish off everything up to character before first non-ascii */
140         if ( LDAP_UTF8_ISASCII( s ) ) {
141                 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
142                         out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
143                 }
144                 if ( i == len ) {
145                         out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
146                         out[outpos] = '\0';
147                         return out;
148                 }
149         } else {
150                 i = 0;
151         }
152
153         p = ucs = (long *) malloc( len * sizeof(*ucs) );
154         if ( ucs == NULL ) {
155                 free(out);
156                 return NULL;
157         }
158
159         /* convert character before first non-ascii to ucs-4 */
160         if ( i > 0 ) {
161                 *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
162                 p++;
163         }
164
165         /* s[i] is now first non-ascii character */
166         for (;;) {
167                 /* s[i] is non-ascii */
168                 /* convert everything up to next ascii to ucs-4 */
169                 while ( i < len ) {
170                         clen = LDAP_UTF8_CHARLEN( s + i );
171                         if ( clen == 0 ) {
172                                 free( ucs );
173                                 free( out );
174                                 return NULL;
175                         }
176                         if ( clen == 1 ) {
177                                 /* ascii */
178                                 break;
179                         }
180                         *p = s[i] & mask[clen];
181                         i++;
182                         for( j = 1; j < clen; j++ ) {
183                                 if ( (s[i] & 0xc0) != 0x80 ) {
184                                         free( ucs );
185                                         free( out );
186                                         return NULL;
187                                 }
188                                 *p <<= 6;
189                                 *p |= s[i] & 0x3f;
190                                 i++;
191                         }
192                         if ( casefold ) {
193                                 *p = uctoupper( *p );
194                         }
195                         p++;
196                 }
197                 /* normalize ucs of length p - ucs */
198                 uccanondecomp( ucs, p - ucs, &ucsout, &ucsoutlen );    
199                 ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
200                 /* convert ucs to utf-8 and store in out */
201                 for ( j = 0; j < ucsoutlen; j++ ) {
202                         /* allocate more space if not enough room for
203                            6 bytes and terminator */
204                         if ( outsize - outpos < 7 ) {
205                                 outsize = ucsoutlen - j + outpos + 6;
206                                 out = (char *) realloc( out, outsize );
207                                 if ( out == NULL ) {
208                                         free( ucs );
209                                         return NULL;
210                                 }
211                         }
212                         outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
213                 }
214                 
215                 if ( i == len ) {
216                         break;
217                 }
218
219                 last = i;
220
221                 /* s[i] is ascii */
222                 /* finish off everything up to char before next non-ascii */
223                 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
224                         out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
225                 }
226                 if ( i == len ) {
227                         out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
228                         break;
229                 }
230
231                 /* convert character before next non-ascii to ucs-4 */
232                 *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
233                 p = ucs + 1;
234         }               
235         free( ucs );
236         out[outpos] = '\0';
237         return out;
238 }
239
240 /* compare UTF8-strings, optionally ignore casing, string pointers must not be NULL */
241 /* slow, should be optimized */
242 int UTF8normcmp(
243         const char *s1,
244         const char *s2,
245         unsigned casefold )
246 {
247         int i, l1, l2, len, ulen, res;
248         unsigned long *ucs, *ucsout1, *ucsout2;
249
250         l1 = strlen( s1 );
251         l2 = strlen( s2 );
252
253         if ( ( l1 == 0 ) || ( l2 == 0 ) ) {
254                 if ( l1 == l2 ) {
255                         return 0;
256                 }
257                 return *s1 - *s2 > 0 ? 1 : -1;
258         }
259         
260         /* See if we can get away with a straight ASCII compare */
261         len = (l1 < l2) ? l1 : l2;
262         for ( i = 0; i<len; i++ ) {
263                 /* Is either char non-ASCII? */
264                 if ((s1[i] & 0x80) || (s2[i] & 0x80))
265                         break;
266                 if (casefold) {
267                         char c1 = TOUPPER(s1[i]);
268                         char c2 = TOUPPER(s2[i]);
269                         res = c1 - c2;
270                 } else {
271                         res = s1[i] - s2[i];
272                 }
273                 if (res)
274                         return res;
275         }
276         /* Strings were ASCII, equal up to minlen */
277         if (i == len)
278                 return l1 - l2;
279                 
280         /* FIXME: Should first check to see if strings are already in
281          * proper normalized form.
282          */
283
284         ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
285         if ( ucs == NULL ) {
286                 return l1 > l2 ? 1 : -1; /* what to do??? */
287         }
288         
289         /*
290          * XXYYZ: we convert to ucs4 even though -llunicode
291          * expects ucs2 in an unsigned long
292          */
293         
294         /* convert and normalize 1st string */
295         for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
296                 ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
297                 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
298                         free( ucs );
299                         return -1; /* what to do??? */
300                 }
301                 len = LDAP_UTF8_CHARLEN( s1 + i );
302         }
303         uccanondecomp( ucs, ulen, &ucsout1, &l1 );
304         l1 = uccanoncomp( ucsout1, l1 );
305
306         /* convert and normalize 2nd string */
307         for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
308                 ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
309                 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
310                         free( ucsout1 );
311                         free( ucs );
312                         return 1; /* what to do??? */
313                 }
314                 len = LDAP_UTF8_CHARLEN( s2 + i );
315         }
316         uccanondecomp( ucs, ulen, &ucsout2, &l2 );
317         l2 = uccanoncomp( ucsout2, l2 );
318
319         free( ucs );
320
321         res = casefold
322                 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
323                 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
324         free( ucsout1 );
325         free( ucsout2 );
326
327         if ( res != 0 ) {
328                 return res;
329         }
330         if ( l1 == l2 ) {
331                 return 0;
332         }
333         return l1 > l2 ? 1 : -1;
334 }