const krb5_data *,
unsigned);
+krb5_boolean k5_utf8_validate(const krb5_data *data);
+
#endif /* K5_UNICODE_H */
char **message_out)
{
krb5_error_code ret;
- krb5_data *string;
char *msg;
*message_out = NULL;
/* If server_string contains a valid UTF-8 string, return that. */
if (server_string->length > 0 &&
memchr(server_string->data, 0, server_string->length) == NULL &&
- krb5int_utf8_normalize(server_string, &string,
- KRB5_UTF8_APPROX) == 0) {
- *message_out = string->data; /* already null terminated */
- free(string);
- return 0;
+ k5_utf8_validate(server_string)) {
+ *message_out = k5memdup0(server_string->data, server_string->length,
+ &ret);
+ return (*message_out == NULL) ? ENOMEM : 0;
}
/* server_string appears invalid, so try to be helpful. */
#include "k5-int.h"
#include "k5-utf8.h"
#include "k5-unicode.h"
+#include "k5-input.h"
#include "ucdata/ucdata.h"
#include <ctype.h>
}
}
+/* Return true if data contains valid UTF-8 sequences. */
+krb5_boolean
+k5_utf8_validate(const krb5_data *data)
+{
+ struct k5input in;
+ int len, tmplen, i;
+ const uint8_t *bytes;
+
+ k5_input_init(&in, data->data, data->length);
+ while (!in.status && in.len > 0) {
+ len = KRB5_UTF8_CHARLEN(in.ptr);
+ if (len < 1 || len > 4)
+ return FALSE;
+ bytes = k5_input_get_bytes(&in, len);
+ if (bytes == NULL)
+ return FALSE;
+ if (KRB5_UTF8_CHARLEN2(bytes, tmplen) != len)
+ return FALSE;
+ for (i = 1; i < len; i++) {
+ if ((bytes[i] & 0xc0) != 0x80)
+ return FALSE;
+ }
+ }
+ return !in.status;
+}
+
#define TOUPPER(c) (islower(c) ? toupper(c) : (c))
#define TOLOWER(c) (isupper(c) ? tolower(c) : (c))