Example #1
0
static int
test_decode(struct testcase *t, int high4)
{
    int len, status = 0;
    krb5_ucs4 u = 0;

    len = krb5int_utf8_charlen2(t->p);
    if (len != t->len) {
        printf("expected len=%d, got len=%d\n", t->len, len);
        status = 1;
    }
    if ((t->len == 0 || high4) && krb5int_utf8_to_ucs4(t->p, &u) != -1) {
        printf("unexpected success in utf8_to_ucs4\n");
        status = 1;
    }
    if (krb5int_utf8_to_ucs4(t->p, &u) != 0 && t->ucs != 0 && !high4) {
        printf("unexpected failure in utf8_to_ucs4\n");
        status = 1;
    }
    if (t->ucs != u && !high4) {
        printf("expected 0x%08lx, got 0x%08lx\n", (unsigned long)t->ucs,
               (unsigned long)u);
        status = 1;
    }
    return status;
}
Example #2
0
File: utf8.c Project: PADL/krb5
int krb5int_utf8_to_ucs2(const char *p, krb5_ucs2 *out)
{
    krb5_ucs4 ch;

    *out = 0;
    if (krb5int_utf8_to_ucs4(p, &ch) == -1 || ch > 0xFFFF)
        return -1;
    *out = (krb5_ucs2) ch;
    return 0;
}
Example #3
0
/* slow, should be optimized */
int 
krb5int_utf8_normcmp(
		     const krb5_data * data1,
		     const krb5_data * data2,
		     unsigned flags)
{
    int i, l1, l2, len, ulen, res = 0;
    char *s1, *s2, *done;
    krb5_ucs4 *ucs, *ucsout1, *ucsout2;

    unsigned casefold = flags & KRB5_UTF8_CASEFOLD;
    unsigned norm1 = flags & KRB5_UTF8_ARG1NFC;
    unsigned norm2 = flags & KRB5_UTF8_ARG2NFC;

    if (data1 == NULL) {
	return data2 == NULL ? 0 : -1;

    } else if (data2 == NULL) {
	return 1;
    }
    l1 = data1->length;
    l2 = data2->length;

    len = (l1 < l2) ? l1 : l2;
    if (len == 0) {
	return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
    }
    s1 = data1->data;
    s2 = data2->data;
    done = s1 + len;

    while ((s1 < done) && KRB5_UTF8_ISASCII(s1) && KRB5_UTF8_ISASCII(s2)) {
	if (casefold) {
	    char c1 = TOLOWER(*s1);
	    char c2 = TOLOWER(*s2);
	    res = c1 - c2;
	} else {
	    res = *s1 - *s2;
	}
	s1++;
	s2++;
	if (res) {
	    /* done unless next character in s1 or s2 is non-ascii */
	    if (s1 < done) {
		if (!KRB5_UTF8_ISASCII(s1) || !KRB5_UTF8_ISASCII(s2)) {
		    break;
		}
	    } else if (((len < l1) && !KRB5_UTF8_ISASCII(s1)) ||
		       ((len < l2) && !KRB5_UTF8_ISASCII(s2))) {
		break;
	    }
	    return res;
	}
    }

    /* We have encountered non-ascii or strings equal up to len */

    /* set i to number of iterations */
    i = s1 - done + len;
    /* passed through loop at least once? */
    if (i > 0) {
	if (!res && (s1 == done) &&
	    ((len == l1) || KRB5_UTF8_ISASCII(s1)) &&
	    ((len == l2) || KRB5_UTF8_ISASCII(s2))) {
	    /* all ascii and equal up to len */
	    return l1 - l2;
	}
	/* rewind one char, and do normalized compare from there */
	s1--;
	s2--;
	l1 -= i - 1;
	l2 -= i - 1;
    }
    /*
     * Should first check to see if strings are already in proper normalized
     * form.
     */
    ucs = malloc(((norm1 || l1 > l2) ? l1 : l2) * sizeof(*ucs));
    if (ucs == NULL) {
	return l1 > l2 ? 1 : -1;/* what to do??? */
    }
    /*
     * XXYYZ: we convert to ucs4 even though -llunicode
     * expects ucs2 in an ac_uint4
     */

    /* convert and normalize 1st string */
    for (i = 0, ulen = 0; i < l1; i += len, ulen++) {
	if (krb5int_utf8_to_ucs4(s1 + i, &ucs[ulen]) == -1) {
	    free(ucs);
	    return -1;		/* what to do??? */
	}
	len = KRB5_UTF8_CHARLEN(s1 + i);
    }

    if (norm1) {
	ucsout1 = ucs;
	l1 = ulen;
	ucs = malloc(l2 * sizeof(*ucs));
	if (ucs == NULL) {
	    free(ucsout1);
	    return l1 > l2 ? 1 : -1;	/* what to do??? */
	}
    } else {
	uccompatdecomp(ucs, ulen, &ucsout1, &l1);
	l1 = uccanoncomp(ucsout1, l1);
    }

    /* convert and normalize 2nd string */
    for (i = 0, ulen = 0; i < l2; i += len, ulen++) {
	if (krb5int_utf8_to_ucs4(s2 + i, &ucs[ulen]) == -1) {
	    free(ucsout1);
	    free(ucs);
	    return 1;		/* what to do??? */
	}
	len = KRB5_UTF8_CHARLEN(s2 + i);
    }

    if (norm2) {
	ucsout2 = ucs;
	l2 = ulen;
    } else {
	uccompatdecomp(ucs, ulen, &ucsout2, &l2);
	l2 = uccanoncomp(ucsout2, l2);
	free(ucs);
    }

    res = casefold
	? krb5int_ucstrncasecmp(ucsout1, ucsout2, l1 < l2 ? l1 : l2)
	: krb5int_ucstrncmp(ucsout1, ucsout2, l1 < l2 ? l1 : l2);
    free(ucsout1);
    free(ucsout2);

    if (res != 0) {
	return res;
    }
    if (l1 == l2) {
	return 0;
    }
    return l1 > l2 ? 1 : -1;
}