Пример #1
0
static int
test_encode(struct testcase *t, int high4)
{
    size_t size;
    char buf[7];

    memset(buf, 0, sizeof(buf));
    size = krb5int_ucs4_to_utf8(t->ucs, buf);
    if (high4 && size != 0) {
        printf("unexpected success beyond U+10FFFF\n");
        return 1;
    }
    if (!high4 && size == 0) {
        printf("unexpected zero size on encode\n");
        return 1;
    }
    if (size != 0 && strcmp(t->p, buf) != 0) {
        printf("expected ");
        printhex(t->p);
        printf("got ");
        printhex(buf);
        printf("\n");
        return 1;
    }
    return 0;
}
Пример #2
0
krb5_error_code
krb5int_utf8_normalize(
		       krb5_data * data,
		       krb5_data ** newdataptr,
		       unsigned flags)
{
    int i, j, len, clen, outpos, ucsoutlen, outsize, last;
    char *out = NULL, *outtmp, *s;
    krb5_ucs4 *ucs = NULL, *p, *ucsout = NULL;
    krb5_data *newdata;
    krb5_error_code retval = 0;

    static unsigned char mask[] = {
    0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};

    unsigned casefold = flags & KRB5_UTF8_CASEFOLD;
    unsigned approx = flags & KRB5_UTF8_APPROX;

    *newdataptr = NULL;

    s = data->data;
    len = data->length;

    newdata = malloc(sizeof(*newdata));
    if (newdata == NULL)
	return ENOMEM;

    /*
     * Should first check to see if string is already in proper normalized
     * form. This is almost as time consuming as the normalization though.
     */

    /* finish off everything up to character before first non-ascii */
    if (KRB5_UTF8_ISASCII(s)) {
	if (casefold) {
	    outsize = len + 7;
	    out = malloc(outsize);
	    if (out == NULL) {
		retval = ENOMEM;
		goto cleanup;
	    }
	    outpos = 0;

	    for (i = 1; (i < len) && KRB5_UTF8_ISASCII(s + i); i++) {
		out[outpos++] = TOLOWER(s[i - 1]);
	    }
	    if (i == len) {
		out[outpos++] = TOLOWER(s[len - 1]);
		goto cleanup;
	    }
	} else {
	    for (i = 1; (i < len) && KRB5_UTF8_ISASCII(s + i); i++) {
		/* empty */
	    }

	    if (i == len) {
		newdata->length = len;
		newdata->data = malloc(newdata->length + 1);
		if (newdata->data == NULL) {
		    retval = ENOMEM;
		    goto cleanup;
		}
		memcpy(newdata->data, s, len);
		newdata->data[len] = '\0';
		*newdataptr = newdata;
		return 0;
	    }
	    outsize = len + 7;
	    out = malloc(outsize);
	    if (out == NULL) {
		retval = ENOMEM;
		goto cleanup;
	    }
	    outpos = i - 1;
	    memcpy(out, s, outpos);
	}
    } else {
	outsize = len + 7;
	out = malloc(outsize);
	if (out == NULL) {
	    retval = ENOMEM;
	    goto cleanup;
	}
	outpos = 0;
	i = 0;
    }

    p = ucs = malloc(len * sizeof(*ucs));
    if (ucs == NULL) {
	retval = ENOMEM;
	goto cleanup;
    }
    /* convert character before first non-ascii to ucs-4 */
    if (i > 0) {
	*p = casefold ? TOLOWER(s[i - 1]) : s[i - 1];
	p++;
    }
    /* s[i] is now first non-ascii character */
    for (;;) {
	/* s[i] is non-ascii */
	/* convert everything up to next ascii to ucs-4 */
	while (i < len) {
	    clen = KRB5_UTF8_CHARLEN2(s + i, clen);
	    if (clen == 0) {
		retval = KRB5_ERR_INVALID_UTF8;
		goto cleanup;
	    }
	    if (clen == 1) {
		/* ascii */
		break;
	    }
	    *p = s[i] & mask[clen];
	    i++;
	    for (j = 1; j < clen; j++) {
		if ((s[i] & 0xc0) != 0x80) {
		    retval = KRB5_ERR_INVALID_UTF8;
		    goto cleanup;
		}
		*p <<= 6;
		*p |= s[i] & 0x3f;
		i++;
	    }
	    if (casefold) {
		*p = uctolower(*p);
	    }
	    p++;
	}
	/* normalize ucs of length p - ucs */
	uccompatdecomp(ucs, p - ucs, &ucsout, &ucsoutlen);
	if (approx) {
	    for (j = 0; j < ucsoutlen; j++) {
		if (ucsout[j] < 0x80) {
		    out[outpos++] = ucsout[j];
		}
	    }
	} else {
	    ucsoutlen = uccanoncomp(ucsout, ucsoutlen);
	    /* convert ucs to utf-8 and store in out */
	    for (j = 0; j < ucsoutlen; j++) {
		/*
		 * allocate more space if not enough room for 6 bytes and
		 * terminator
		 */
		if (outsize - outpos < 7) {
		    outsize = ucsoutlen - j + outpos + 6;
		    outtmp = realloc(out, outsize);
		    if (outtmp == NULL) {
			retval = ENOMEM;
			goto cleanup;
		    }
		    out = outtmp;
		}
		outpos += krb5int_ucs4_to_utf8(ucsout[j], &out[outpos]);
	    }
	}

	free(ucsout);
	ucsout = NULL;

	if (i == len) {
	    break;
	}
	last = i;

	/* Allocate more space in out if necessary */
	if (len - i >= outsize - outpos) {
	    outsize += 1 + ((len - i) - (outsize - outpos));
	    outtmp = realloc(out, outsize);
	    if (outtmp == NULL) {
		retval = ENOMEM;
		goto cleanup;
	    }
	    out = outtmp;
	}
	/* s[i] is ascii */
	/* finish off everything up to char before next non-ascii */
	for (i++; (i < len) && KRB5_UTF8_ISASCII(s + i); i++) {
	    out[outpos++] = casefold ? TOLOWER(s[i - 1]) : s[i - 1];
	}
	if (i == len) {
	    out[outpos++] = casefold ? TOLOWER(s[len - 1]) : s[len - 1];
	    break;
	}
	/* convert character before next non-ascii to ucs-4 */
	*ucs = casefold ? TOLOWER(s[i - 1]) : s[i - 1];
	p = ucs + 1;
    }

cleanup:
    free(ucs);
    free(ucsout);
    if (retval) {
	free(out);
	free(newdata);
	return retval;
    }
    out[outpos] = '\0';
    newdata->data = out;
    newdata->length = outpos;
    *newdataptr = newdata;
    return 0;
}