8 #include <ldap_pvt_uc.h>
12 const ldap_unicode_t *u1,
13 const ldap_unicode_t *u2,
16 for(; 0 < n; ++u1, ++u2, --n ) {
18 return *u1 < *u2 ? -1 : +1;
28 const ldap_unicode_t *u1,
29 const ldap_unicode_t *u2,
32 for(; 0 < n; ++u1, ++u2, --n ) {
33 ldap_unicode_t uu1 = uctoupper( *u1 );
34 ldap_unicode_t uu2 = uctoupper( *u2 );
37 return uu1 < uu2 ? -1 : +1;
46 ldap_unicode_t * ucstrnchr(
47 const ldap_unicode_t *u,
51 for(; 0 < n; ++u, --n ) {
53 return (ldap_unicode_t *) u;
60 ldap_unicode_t * ucstrncasechr(
61 const ldap_unicode_t *u,
66 for(; 0 < n; ++u, --n ) {
67 if( uctoupper( *u ) == c ) {
68 return (ldap_unicode_t *) u;
79 for(; 0 < n; ++u, --n ) {
88 int i, j, len, clen, outpos, ucsoutlen, outsize, last;
90 unsigned long *ucs, *p, *ucsout;
92 static unsigned char mask[] = {
93 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
103 out = (char *) malloc( 1 );
109 out = (char *) malloc( outsize );
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];
122 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
130 p = ucs = (long *) malloc( len * sizeof(*ucs) );
136 /* convert character before first non-ascii to ucs-4 */
138 *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
142 /* s[i] is now first non-ascii character */
144 /* s[i] is non-ascii */
145 /* convert everything up to next ascii to ucs-4 */
147 clen = LDAP_UTF8_CHARLEN( s + i );
157 *p = s[i] & mask[clen];
159 for( j = 1; j < clen; j++ ) {
160 if ( (s[i] & 0xc0) != 0x80 ) {
170 *p = uctoupper( *p );
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 );
189 outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
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];
204 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
208 /* convert character before next non-ascii to ucs-4 */
209 *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
217 /* compare UTF8-strings, optionally ignore casing, string pointers must not be NULL */
218 /* slow, should be optimized */
224 int i, l1, l2, len, ulen, res;
225 unsigned long *ucs, *ucsout1, *ucsout2;
230 if ( ( l1 == 0 ) || ( l2 == 0 ) ) {
234 return *s1 - *s2 > 0 ? 1 : -1;
237 ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
239 return l1 > l2 ? 1 : -1; /* what to do??? */
243 * XXYYZ: we convert to ucs4 even though -llunicode
244 * expects ucs2 in an unsigned long
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 ) {
252 return -1; /* what to do??? */
254 len = LDAP_UTF8_CHARLEN( s1 + i );
256 uccanondecomp( ucs, ulen, &ucsout1, &l1 );
257 l1 = uccanoncomp( ucsout1, l1 );
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 ) {
265 return 1; /* what to do??? */
267 len = LDAP_UTF8_CHARLEN( s2 + i );
269 uccanondecomp( ucs, ulen, &ucsout2, &l2 );
270 l2 = uccanoncomp( ucsout2, l2 );
275 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
276 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
286 return l1 > l2 ? 1 : -1;