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