diff options
Diffstat (limited to 'src/shared/utf8.c')
-rw-r--r-- | src/shared/utf8.c | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/src/shared/utf8.c b/src/shared/utf8.c index 1044fb6489..403166d7f9 100644 --- a/src/shared/utf8.c +++ b/src/shared/utf8.c @@ -78,8 +78,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) @@ -92,16 +95,18 @@ static int utf8_encoded_expected_len(const char *str) { return 5; if ((c & 0xfe) == 0xfc) return 6; + return 0; } /* decode one unicode char */ int utf8_encoded_to_unichar(const char *str) { - int unichar; - int len; - int i; + int unichar, len, i; + + assert(str); len = utf8_encoded_expected_len(str); + switch (len) { case 1: return (int)str[0]; @@ -121,12 +126,12 @@ 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; } @@ -134,16 +139,21 @@ 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;) { - int encoded_len = utf8_encoded_valid_unichar((const char *)p); - int val = utf8_encoded_to_unichar((const char*)p); + int encoded_len, val; - if (encoded_len < 0 || val < 0 || is_unicode_control(val)) + encoded_len = utf8_encoded_valid_unichar((const char *) p); + val = utf8_encoded_to_unichar((const char*) p); + + if (encoded_len < 0 || + val < 0 || + is_unicode_control(val) || + (!newline && val == '\n')) return false; length -= encoded_len; @@ -203,6 +213,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) @@ -213,18 +224,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) @@ -233,17 +245,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; } |