X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futf8.c;h=8702ceb1b2f983b6a54e177f4191eac481885bf6;hp=732f0f00ca20d2003cd3c96122cf0099abec15bf;hb=6357ac664c9ce8ae5b83bdb98011da24185e4efa;hpb=7991ac34ab08421415b907e42775c5539a4a5bbb diff --git a/src/shared/utf8.c b/src/shared/utf8.c index 732f0f00c..8702ceb1b 100644 --- a/src/shared/utf8.c +++ b/src/shared/utf8.c @@ -3,6 +3,7 @@ /*** This file is part of systemd. + Copyright 2008-2011 Kay Sievers Copyright 2012 Lennart Poettering systemd is free software; you can redistribute it and/or modify it @@ -19,7 +20,7 @@ along with systemd; If not, see . ***/ -/* This file is based on the GLIB utf8 validation functions. The +/* Parts of this file are based on the GLIB utf8 validation functions. The * original license text follows. */ /* gutf8.c - Operations on UTF-8 strings. @@ -79,8 +80,11 @@ static bool is_unicode_control(uint32_t ch) { /* count of characters used to encode one unicode char */ static int utf8_encoded_expected_len(const char *str) { - unsigned char c = (unsigned char)str[0]; + unsigned char c; + assert(str); + + c = (unsigned char) str[0]; if (c < 0x80) return 1; if ((c & 0xe0) == 0xc0) @@ -93,16 +97,18 @@ static int utf8_encoded_expected_len(const char *str) { return 5; if ((c & 0xfe) == 0xfc) return 6; + return 0; } /* decode one unicode char */ -static int utf8_encoded_to_unichar(const char *str) { - int unichar; - int len; - int i; +int utf8_encoded_to_unichar(const char *str) { + int unichar, len, i; + + assert(str); len = utf8_encoded_expected_len(str); + switch (len) { case 1: return (int)str[0]; @@ -122,12 +128,12 @@ static int utf8_encoded_to_unichar(const char *str) { unichar = (int)str[0] & 0x01; break; default: - return -1; + return -EINVAL; } for (i = 1; i < len; i++) { if (((int)str[i] & 0xc0) != 0x80) - return -1; + return -EINVAL; unichar <<= 6; unichar |= (int)str[i] & 0x3f; } @@ -135,19 +141,27 @@ static int utf8_encoded_to_unichar(const char *str) { return unichar; } -bool utf8_is_printable(const char* str, size_t length) { +bool utf8_is_printable_newline(const char* str, size_t length, bool newline) { const uint8_t *p; assert(str); - for (p = (const uint8_t*) str; length; p++) { - int encoded_len = utf8_encoded_valid_unichar((const char *)p); - int32_t val = utf8_encoded_to_unichar((const char*)p); + for (p = (const uint8_t*) str; length;) { + int encoded_len, val; - if (encoded_len < 0 || val < 0 || is_unicode_control(val)) + encoded_len = utf8_encoded_valid_unichar((const char *) p); + if (encoded_len < 0 || + (size_t) encoded_len > length) + return false; + + val = utf8_encoded_to_unichar((const char*) p); + if (val < 0 || + is_unicode_control(val) || + (!newline && val == '\n')) return false; length -= encoded_len; + p += encoded_len; } return true; @@ -159,8 +173,9 @@ const char *utf8_is_valid(const char *str) { assert(str); for (p = (const uint8_t*) str; *p; ) { - int len = utf8_encoded_valid_unichar((const char *)p); + int len; + len = utf8_encoded_valid_unichar((const char *)p); if (len < 0) return NULL; @@ -170,37 +185,43 @@ const char *utf8_is_valid(const char *str) { return str; } -char *ascii_is_valid(const char *str) { - const char *p; +char *utf8_escape_invalid(const char *str) { + char *p, *s; assert(str); - for (p = str; *p; p++) - if ((unsigned char) *p >= 128) - return NULL; + p = s = malloc(strlen(str) * 4 + 1); + if (!p) + return NULL; - return (char*) str; -} + while (*str) { + int len; -char *ascii_filter(const char *str) { - const char *s; - char *r, *d; - size_t l; + len = utf8_encoded_valid_unichar(str); + if (len > 0) { + s = mempcpy(s, str, len); + str += len; + } else { + s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER)); + str += 1; + } + } - assert(str); + *s = '\0'; - l = strlen(str); - r = malloc(l + 1); - if (!r) - return NULL; + return p; +} - for (s = str, d = r; *s; s++) - if ((unsigned char) *s < 128) - *(d++) = *s; +char *ascii_is_valid(const char *str) { + const char *p; - *d = 0; + assert(str); - return r; + for (p = str; *p; p++) + if ((unsigned char) *p >= 128) + return NULL; + + return (char*) str; } char *utf16_to_utf8(const void *s, size_t length) { @@ -241,6 +262,7 @@ char *utf16_to_utf8(const void *s, size_t length) { /* expected size used to encode one unicode char */ static int utf8_unichar_to_encoded_len(int unichar) { + if (unichar < 0x80) return 1; if (unichar < 0x800) @@ -251,18 +273,19 @@ static int utf8_unichar_to_encoded_len(int unichar) { return 4; if (unichar < 0x4000000) return 5; + return 6; } /* validate one encoded unicode char and return its length */ int utf8_encoded_valid_unichar(const char *str) { - int len; - int unichar; - int i; + int len, unichar, i; + + assert(str); len = utf8_encoded_expected_len(str); if (len == 0) - return -1; + return -EINVAL; /* ascii is valid */ if (len == 1) @@ -271,63 +294,17 @@ int utf8_encoded_valid_unichar(const char *str) { /* check if expected encoded chars are available */ for (i = 0; i < len; i++) if ((str[i] & 0x80) != 0x80) - return -1; + return -EINVAL; unichar = utf8_encoded_to_unichar(str); /* check if encoded length matches encoded value */ if (utf8_unichar_to_encoded_len(unichar) != len) - return -1; + return -EINVAL; /* check if value has valid range */ if (!is_unicode_valid(unichar)) - return -1; + return -EINVAL; return len; } - -int is_utf8_encoding_whitelisted(char c, const char *white) { - if ((c >= '0' && c <= '9') || - (c >= 'A' && c <= 'Z') || - (c >= 'a' && c <= 'z') || - strchr("#+-.:=@_", c) != NULL || - (white != NULL && strchr(white, c) != NULL)) - return 1; - return 0; -} - -int udev_encode_string(const char *str, char *str_enc, size_t len) { - size_t i, j; - - if (str == NULL || str_enc == NULL) - return -1; - - for (i = 0, j = 0; str[i] != '\0'; i++) { - int seqlen; - - seqlen = utf8_encoded_valid_unichar(&str[i]); - if (seqlen > 1) { - if (len-j < (size_t)seqlen) - goto err; - memcpy(&str_enc[j], &str[i], seqlen); - j += seqlen; - i += (seqlen-1); - } else if (str[i] == '\\' || !is_utf8_encoding_whitelisted(str[i], NULL)) { - if (len-j < 4) - goto err; - sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]); - j += 4; - } else { - if (len-j < 1) - goto err; - str_enc[j] = str[i]; - j++; - } - } - if (len-j < 1) - goto err; - str_enc[j] = '\0'; - return 0; -err: - return -1; -}