9 #define malloc(x) ber_memalloc(x)
10 #define realloc(x,y) ber_memrealloc(x,y)
11 #define free(x) ber_memfree(x)
13 #include <ldap_utf8.h>
14 #include <ldap_pvt_uc.h>
18 const ldap_unicode_t *u1,
19 const ldap_unicode_t *u2,
22 for(; 0 < n; ++u1, ++u2, --n ) {
24 return *u1 < *u2 ? -1 : +1;
34 const ldap_unicode_t *u1,
35 const ldap_unicode_t *u2,
38 for(; 0 < n; ++u1, ++u2, --n ) {
39 ldap_unicode_t uu1 = uctoupper( *u1 );
40 ldap_unicode_t uu2 = uctoupper( *u2 );
43 return uu1 < uu2 ? -1 : +1;
52 ldap_unicode_t * ucstrnchr(
53 const ldap_unicode_t *u,
57 for(; 0 < n; ++u, --n ) {
59 return (ldap_unicode_t *) u;
66 ldap_unicode_t * ucstrncasechr(
67 const ldap_unicode_t *u,
72 for(; 0 < n; ++u, --n ) {
73 if( uctoupper( *u ) == c ) {
74 return (ldap_unicode_t *) u;
85 for(; 0 < n; ++u, --n ) {
94 int i, j, len, clen, outpos, ucsoutlen, outsize, last;
96 unsigned long *ucs, *p, *ucsout;
98 static unsigned char mask[] = {
99 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
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 */
114 /* It's pure ASCII or zero-len */
116 out = malloc( len + 1 );
117 if ( i && !casefold ) {
118 strncpy( out, bv->bv_val, len );
120 for ( j=0; j<i; j++ )
121 out[j] = TOUPPER( s[j] );
128 out = (char *) malloc( outsize );
133 /* FIXME: Should first check to see if string is already in
134 * proper normalized form.
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];
145 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
153 p = ucs = (long *) malloc( len * sizeof(*ucs) );
159 /* convert character before first non-ascii to ucs-4 */
161 *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
165 /* s[i] is now first non-ascii character */
167 /* s[i] is non-ascii */
168 /* convert everything up to next ascii to ucs-4 */
170 clen = LDAP_UTF8_CHARLEN( s + i );
180 *p = s[i] & mask[clen];
182 for( j = 1; j < clen; j++ ) {
183 if ( (s[i] & 0xc0) != 0x80 ) {
193 *p = uctoupper( *p );
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 );
212 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
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];
227 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
231 /* convert character before next non-ascii to ucs-4 */
232 *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
240 /* compare UTF8-strings, optionally ignore casing, string pointers must not be NULL */
241 /* slow, should be optimized */
247 int i, l1, l2, len, ulen, res;
248 unsigned long *ucs, *ucsout1, *ucsout2;
253 if ( ( l1 == 0 ) || ( l2 == 0 ) ) {
257 return *s1 - *s2 > 0 ? 1 : -1;
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))
267 char c1 = TOUPPER(s1[i]);
268 char c2 = TOUPPER(s2[i]);
276 /* Strings were ASCII, equal up to minlen */
280 /* FIXME: Should first check to see if strings are already in
281 * proper normalized form.
284 ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
286 return l1 > l2 ? 1 : -1; /* what to do??? */
290 * XXYYZ: we convert to ucs4 even though -llunicode
291 * expects ucs2 in an unsigned long
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 ) {
299 return -1; /* what to do??? */
301 len = LDAP_UTF8_CHARLEN( s1 + i );
303 uccanondecomp( ucs, ulen, &ucsout1, &l1 );
304 l1 = uccanoncomp( ucsout1, l1 );
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 ) {
312 return 1; /* what to do??? */
314 len = LDAP_UTF8_CHARLEN( s2 + i );
316 uccanondecomp( ucs, ulen, &ucsout2, &l2 );
317 l2 = uccanoncomp( ucsout2, l2 );
322 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
323 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
333 return l1 > l2 ? 1 : -1;