/* * Convert a UTF8 character to a UCS4 character. Return 0 on success, * -1 on failure. */ int krb5int_utf8_to_ucs4(const char *p, krb5_ucs4 *out) { const unsigned char *c = (const unsigned char *) p; krb5_ucs4 ch; int len, i; static unsigned char mask[] = { 0, 0x7f, 0x1f, 0x0f, 0x07 }; *out = 0; len = KRB5_UTF8_CHARLEN2(p, len); if (len == 0) return -1; ch = c[0] & mask[len]; for (i = 1; i < len; i++) { if ((c[i] & 0xc0) != 0x80) return -1; ch <<= 6; ch |= c[i] & 0x3f; } if (ch > 0x10ffff) return -1; *out = ch; return 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; }
static ssize_t k5_utf8s_to_ucs2s(krb5_ucs2 *ucs2str, const char *utf8str, size_t count, int little_endian) { size_t ucs2len = 0; size_t utflen, i; krb5_ucs2 ch; /* If input ptr is NULL or empty... */ if (utf8str == NULL || *utf8str == '\0') { if (ucs2str != NULL) *ucs2str = 0; return 0; } /* Examine next UTF-8 character. */ while (ucs2len < count && *utf8str != '\0') { /* Get UTF-8 sequence length from 1st byte */ utflen = KRB5_UTF8_CHARLEN2(utf8str, utflen); if (utflen == 0 || utflen > KRB5_MAX_UTF8_LEN) return -1; /* First byte minus length tag */ ch = (krb5_ucs2)(utf8str[0] & mask[utflen]); for (i = 1; i < utflen; i++) { /* Subsequent bytes must start with 10 */ if ((utf8str[i] & 0xc0) != 0x80) return -1; ch <<= 6; /* 6 bits of data in each subsequent byte */ ch |= (krb5_ucs2)(utf8str[i] & 0x3f); } if (ucs2str != NULL) { #ifdef K5_BE #ifndef SWAP16 #define SWAP16(X) ((((X) << 8) | ((X) >> 8)) & 0xFFFF) #endif if (little_endian) ucs2str[ucs2len] = SWAP16(ch); else #endif ucs2str[ucs2len] = ch; } utf8str += utflen; /* Move to next UTF-8 character */ ucs2len++; /* Count number of wide chars stored/required */ } if (ucs2str != NULL && ucs2len < count) { /* Add null terminator if there's room in the buffer. */ ucs2str[ucs2len] = 0; } return ucs2len; }