3 #include <ldap_pvt_uc.h>
10 const ldap_unicode_t *u1,
11 const ldap_unicode_t *u2,
14 for(; 0 < n; ++u1, ++u2, --n ) {
16 return *u1 < *u2 ? -1 : +1;
26 const ldap_unicode_t *u1,
27 const ldap_unicode_t *u2,
30 for(; 0 < n; ++u1, ++u2, --n ) {
31 ldap_unicode_t uu1 = uctoupper( *u1 );
32 ldap_unicode_t uu2 = uctoupper( *u2 );
35 return uu1 < uu2 ? -1 : +1;
44 ldap_unicode_t * ucstrnchr(
45 const ldap_unicode_t *u,
49 for(; 0 < n; ++u, --n ) {
51 return (ldap_unicode_t *) u;
58 ldap_unicode_t * ucstrncasechr(
59 const ldap_unicode_t *u,
64 for(; 0 < n; ++u, --n ) {
65 if( uctoupper( *u ) == c ) {
66 return (ldap_unicode_t *) u;
77 for(; 0 < n; ++u, --n ) {
86 int i, j, len, clen, outpos, ucsoutlen, outsize, last;
88 unsigned long *ucs, *p, *ucsout;
90 static unsigned char mask[] = {
91 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
100 out = (char *) malloc( 1 );
106 out = (char *) malloc( outsize );
113 /* finish off everything up to character before first non-ascii */
114 if ( LDAP_UTF8_ISASCII( s ) ) {
115 for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
116 out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
119 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
127 p = ucs = (long *) malloc( len * sizeof(*ucs) );
133 /* convert character before first non-ascii to ucs-4 */
135 *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
139 /* s[i] is now first non-ascii character */
141 /* s[i] is non-ascii */
142 /* convert everything up to next ascii to ucs-4 */
144 clen = LDAP_UTF8_CHARLEN( s + i );
154 *p = s[i] & mask[clen];
156 for( j = 1; j < clen; j++ ) {
157 if ( (s[i] & 0xc0) != 0x80 ) {
167 *p = uctoupper( *p );
171 /* normalize ucs of length p - ucs */
172 uccanondecomp( ucs, p - ucs, &ucsout, &ucsoutlen );
173 ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
174 /* convert ucs to utf-8 and store in out */
175 for ( j = 0; j < ucsoutlen; j++ ) {
176 /* allocate more space if not enough room for
177 6 bytes and terminator */
178 if ( outsize - outpos < 7 ) {
179 outsize = ucsoutlen - j + outpos + 6;
180 out = (char *) realloc( out, outsize );
186 outpos += ldap_ucs4_to_utf8( ucsout[j], &out[outpos] );
196 /* finish off everything up to char before next non-ascii */
197 for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
198 out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
201 out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
205 /* convert character before next non-ascii to ucs-4 */
206 *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
214 /* compare UTF8-strings, optionally ignore casing, string pointers must not be NULL */
215 /* slow, should be optimized */
221 int i, l1, l2, len, ulen, res;
222 unsigned long *ucs, *ucsout1, *ucsout2;
227 if ( ( l1 == 0 ) || ( l2 == 0 ) ) {
231 return *s1 - *s2 > 0 ? 1 : -1;
234 ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
236 return l1 > l2 ? 1 : -1; /* what to do??? */
240 * XXYYZ: we convert to ucs4 even though -llunicode
241 * expects ucs2 in an unsigned long
244 /* convert and normalize 1st string */
245 for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
246 ucs[ulen] = ldap_utf8_to_ucs4( s1 + i );
247 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
249 return -1; /* what to do??? */
251 len = LDAP_UTF8_CHARLEN( s1 + i );
253 uccanondecomp( ucs, ulen, &ucsout1, &l1 );
254 l1 = uccanoncomp( ucsout1, l1 );
256 /* convert and normalize 2nd string */
257 for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
258 ucs[ulen] = ldap_utf8_to_ucs4( s2 + i );
259 if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
262 return 1; /* what to do??? */
264 len = LDAP_UTF8_CHARLEN( s2 + i );
266 uccanondecomp( ucs, ulen, &ucsout2, &l2 );
267 l2 = uccanoncomp( ucsout2, l2 );
272 ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
273 : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
283 return l1 > l2 ? 1 : -1;