|
|
a4b143 |
From 3fde78dba9ad834ab264ad6888672833874b9101 Mon Sep 17 00:00:00 2001
|
|
|
a4b143 |
From: Dave Reisner <dreisner@archlinux.org>
|
|
|
a4b143 |
Date: Wed, 18 Sep 2013 11:52:14 -0400
|
|
|
a4b143 |
Subject: [PATCH] shared/utf8: merge implementations, remove cruft
|
|
|
a4b143 |
|
|
|
a4b143 |
This unifies the utf8 handling code which was previously duplicated in
|
|
|
a4b143 |
udev and systemd.
|
|
|
a4b143 |
---
|
|
|
a4b143 |
TODO | 1 -
|
|
|
a4b143 |
src/shared/utf8.c | 279 +++++++++++++--------------------------------------
|
|
|
a4b143 |
src/shared/utf8.h | 3 +-
|
|
|
a4b143 |
src/test/test-utf8.c | 9 ++
|
|
|
a4b143 |
4 files changed, 81 insertions(+), 211 deletions(-)
|
|
|
a4b143 |
|
|
|
a4b143 |
diff --git a/TODO b/TODO
|
|
|
a4b143 |
index 4c3e14f..8b7325e 100644
|
|
|
a4b143 |
--- a/TODO
|
|
|
a4b143 |
+++ b/TODO
|
|
|
a4b143 |
@@ -599,7 +599,6 @@ Features:
|
|
|
a4b143 |
* udev:
|
|
|
a4b143 |
- remove src/udev/udev-builtin-firmware.c (CONFIG_FW_LOADER_USER_HELPER=n)
|
|
|
a4b143 |
- move to LGPL
|
|
|
a4b143 |
- - unify utf8 validator code with shared/
|
|
|
a4b143 |
- kill scsi_id
|
|
|
a4b143 |
- add trigger --subsystem-match=usb/usb_device device
|
|
|
a4b143 |
|
|
|
a4b143 |
diff --git a/src/shared/utf8.c b/src/shared/utf8.c
|
|
|
a4b143 |
index 1a68394..732f0f0 100644
|
|
|
a4b143 |
--- a/src/shared/utf8.c
|
|
|
a4b143 |
+++ b/src/shared/utf8.c
|
|
|
a4b143 |
@@ -51,8 +51,6 @@
|
|
|
a4b143 |
#include "utf8.h"
|
|
|
a4b143 |
#include "util.h"
|
|
|
a4b143 |
|
|
|
a4b143 |
-#define FILTER_CHAR '_'
|
|
|
a4b143 |
-
|
|
|
a4b143 |
static inline bool is_unicode_valid(uint32_t ch) {
|
|
|
a4b143 |
|
|
|
a4b143 |
if (ch >= 0x110000) /* End of unicode space */
|
|
|
a4b143 |
@@ -67,17 +65,6 @@ static inline bool is_unicode_valid(uint32_t ch) {
|
|
|
a4b143 |
return true;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
-static inline bool is_continuation_char(uint8_t ch) {
|
|
|
a4b143 |
- if ((ch & 0xc0) != 0x80) /* 10xxxxxx */
|
|
|
a4b143 |
- return false;
|
|
|
a4b143 |
- return true;
|
|
|
a4b143 |
-}
|
|
|
a4b143 |
-
|
|
|
a4b143 |
-static inline void merge_continuation_char(uint32_t *u_ch, uint8_t ch) {
|
|
|
a4b143 |
- *u_ch <<= 6;
|
|
|
a4b143 |
- *u_ch |= ch & 0x3f;
|
|
|
a4b143 |
-}
|
|
|
a4b143 |
-
|
|
|
a4b143 |
static bool is_unicode_control(uint32_t ch) {
|
|
|
a4b143 |
|
|
|
a4b143 |
/*
|
|
|
a4b143 |
@@ -90,163 +77,97 @@ static bool is_unicode_control(uint32_t ch) {
|
|
|
a4b143 |
(0x7F <= ch && ch <= 0x9F);
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
-bool utf8_is_printable(const char* str, size_t length) {
|
|
|
a4b143 |
- uint32_t val = 0;
|
|
|
a4b143 |
- uint32_t min = 0;
|
|
|
a4b143 |
- const uint8_t *p;
|
|
|
a4b143 |
+/* count of characters used to encode one unicode char */
|
|
|
a4b143 |
+static int utf8_encoded_expected_len(const char *str) {
|
|
|
a4b143 |
+ unsigned char c = (unsigned char)str[0];
|
|
|
a4b143 |
|
|
|
a4b143 |
- assert(str);
|
|
|
a4b143 |
+ if (c < 0x80)
|
|
|
a4b143 |
+ return 1;
|
|
|
a4b143 |
+ if ((c & 0xe0) == 0xc0)
|
|
|
a4b143 |
+ return 2;
|
|
|
a4b143 |
+ if ((c & 0xf0) == 0xe0)
|
|
|
a4b143 |
+ return 3;
|
|
|
a4b143 |
+ if ((c & 0xf8) == 0xf0)
|
|
|
a4b143 |
+ return 4;
|
|
|
a4b143 |
+ if ((c & 0xfc) == 0xf8)
|
|
|
a4b143 |
+ return 5;
|
|
|
a4b143 |
+ if ((c & 0xfe) == 0xfc)
|
|
|
a4b143 |
+ return 6;
|
|
|
a4b143 |
+ return 0;
|
|
|
a4b143 |
+}
|
|
|
a4b143 |
|
|
|
a4b143 |
- for (p = (const uint8_t*) str; length; p++, length--) {
|
|
|
a4b143 |
- if (*p < 128) {
|
|
|
a4b143 |
- val = *p;
|
|
|
a4b143 |
- } else {
|
|
|
a4b143 |
- if ((*p & 0xe0) == 0xc0) { /* 110xxxxx two-char seq. */
|
|
|
a4b143 |
- min = 128;
|
|
|
a4b143 |
- val = (uint32_t) (*p & 0x1e);
|
|
|
a4b143 |
- goto ONE_REMAINING;
|
|
|
a4b143 |
- } else if ((*p & 0xf0) == 0xe0) { /* 1110xxxx three-char seq.*/
|
|
|
a4b143 |
- min = (1 << 11);
|
|
|
a4b143 |
- val = (uint32_t) (*p & 0x0f);
|
|
|
a4b143 |
- goto TWO_REMAINING;
|
|
|
a4b143 |
- } else if ((*p & 0xf8) == 0xf0) { /* 11110xxx four-char seq */
|
|
|
a4b143 |
- min = (1 << 16);
|
|
|
a4b143 |
- val = (uint32_t) (*p & 0x07);
|
|
|
a4b143 |
- } else
|
|
|
a4b143 |
- return false;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- p++;
|
|
|
a4b143 |
- length--;
|
|
|
a4b143 |
- if (!length || !is_continuation_char(*p))
|
|
|
a4b143 |
- return false;
|
|
|
a4b143 |
- merge_continuation_char(&val, *p);
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- TWO_REMAINING:
|
|
|
a4b143 |
- p++;
|
|
|
a4b143 |
- length--;
|
|
|
a4b143 |
- if (!is_continuation_char(*p))
|
|
|
a4b143 |
- return false;
|
|
|
a4b143 |
- merge_continuation_char(&val, *p);
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- ONE_REMAINING:
|
|
|
a4b143 |
- p++;
|
|
|
a4b143 |
- length--;
|
|
|
a4b143 |
- if (!is_continuation_char(*p))
|
|
|
a4b143 |
- return false;
|
|
|
a4b143 |
- merge_continuation_char(&val, *p);
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- if (val < min)
|
|
|
a4b143 |
- return false;
|
|
|
a4b143 |
- }
|
|
|
a4b143 |
+/* decode one unicode char */
|
|
|
a4b143 |
+static int utf8_encoded_to_unichar(const char *str) {
|
|
|
a4b143 |
+ int unichar;
|
|
|
a4b143 |
+ int len;
|
|
|
a4b143 |
+ int i;
|
|
|
a4b143 |
|
|
|
a4b143 |
- if (is_unicode_control(val))
|
|
|
a4b143 |
- return false;
|
|
|
a4b143 |
+ len = utf8_encoded_expected_len(str);
|
|
|
a4b143 |
+ switch (len) {
|
|
|
a4b143 |
+ case 1:
|
|
|
a4b143 |
+ return (int)str[0];
|
|
|
a4b143 |
+ case 2:
|
|
|
a4b143 |
+ unichar = str[0] & 0x1f;
|
|
|
a4b143 |
+ break;
|
|
|
a4b143 |
+ case 3:
|
|
|
a4b143 |
+ unichar = (int)str[0] & 0x0f;
|
|
|
a4b143 |
+ break;
|
|
|
a4b143 |
+ case 4:
|
|
|
a4b143 |
+ unichar = (int)str[0] & 0x07;
|
|
|
a4b143 |
+ break;
|
|
|
a4b143 |
+ case 5:
|
|
|
a4b143 |
+ unichar = (int)str[0] & 0x03;
|
|
|
a4b143 |
+ break;
|
|
|
a4b143 |
+ case 6:
|
|
|
a4b143 |
+ unichar = (int)str[0] & 0x01;
|
|
|
a4b143 |
+ break;
|
|
|
a4b143 |
+ default:
|
|
|
a4b143 |
+ return -1;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
- return true;
|
|
|
a4b143 |
+ for (i = 1; i < len; i++) {
|
|
|
a4b143 |
+ if (((int)str[i] & 0xc0) != 0x80)
|
|
|
a4b143 |
+ return -1;
|
|
|
a4b143 |
+ unichar <<= 6;
|
|
|
a4b143 |
+ unichar |= (int)str[i] & 0x3f;
|
|
|
a4b143 |
+ }
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ return unichar;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
-static char* utf8_validate(const char *str, char *output) {
|
|
|
a4b143 |
- uint32_t val = 0;
|
|
|
a4b143 |
- uint32_t min = 0;
|
|
|
a4b143 |
- const uint8_t *p, *last;
|
|
|
a4b143 |
- int size;
|
|
|
a4b143 |
- uint8_t *o;
|
|
|
a4b143 |
+bool utf8_is_printable(const char* str, size_t length) {
|
|
|
a4b143 |
+ const uint8_t *p;
|
|
|
a4b143 |
|
|
|
a4b143 |
assert(str);
|
|
|
a4b143 |
|
|
|
a4b143 |
- o = (uint8_t*) output;
|
|
|
a4b143 |
- for (p = (const uint8_t*) str; *p; p++) {
|
|
|
a4b143 |
- if (*p < 128) {
|
|
|
a4b143 |
- if (o)
|
|
|
a4b143 |
- *o = *p;
|
|
|
a4b143 |
- } else {
|
|
|
a4b143 |
- last = p;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- if ((*p & 0xe0) == 0xc0) { /* 110xxxxx two-char seq. */
|
|
|
a4b143 |
- size = 2;
|
|
|
a4b143 |
- min = 128;
|
|
|
a4b143 |
- val = (uint32_t) (*p & 0x1e);
|
|
|
a4b143 |
- goto ONE_REMAINING;
|
|
|
a4b143 |
- } else if ((*p & 0xf0) == 0xe0) { /* 1110xxxx three-char seq.*/
|
|
|
a4b143 |
- size = 3;
|
|
|
a4b143 |
- min = (1 << 11);
|
|
|
a4b143 |
- val = (uint32_t) (*p & 0x0f);
|
|
|
a4b143 |
- goto TWO_REMAINING;
|
|
|
a4b143 |
- } else if ((*p & 0xf8) == 0xf0) { /* 11110xxx four-char seq */
|
|
|
a4b143 |
- size = 4;
|
|
|
a4b143 |
- min = (1 << 16);
|
|
|
a4b143 |
- val = (uint32_t) (*p & 0x07);
|
|
|
a4b143 |
- } else
|
|
|
a4b143 |
- goto error;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- p++;
|
|
|
a4b143 |
- if (!is_continuation_char(*p))
|
|
|
a4b143 |
- goto error;
|
|
|
a4b143 |
- merge_continuation_char(&val, *p);
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- TWO_REMAINING:
|
|
|
a4b143 |
- p++;
|
|
|
a4b143 |
- if (!is_continuation_char(*p))
|
|
|
a4b143 |
- goto error;
|
|
|
a4b143 |
- merge_continuation_char(&val, *p);
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- ONE_REMAINING:
|
|
|
a4b143 |
- p++;
|
|
|
a4b143 |
- if (!is_continuation_char(*p))
|
|
|
a4b143 |
- goto error;
|
|
|
a4b143 |
- merge_continuation_char(&val, *p);
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- if (val < min)
|
|
|
a4b143 |
- goto error;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- if (!is_unicode_valid(val))
|
|
|
a4b143 |
- goto error;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- if (o) {
|
|
|
a4b143 |
- memcpy(o, last, (size_t) size);
|
|
|
a4b143 |
- o += size;
|
|
|
a4b143 |
- }
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- continue;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- error:
|
|
|
a4b143 |
- if (o) {
|
|
|
a4b143 |
- *o = FILTER_CHAR;
|
|
|
a4b143 |
- p = last; /* We retry at the next character */
|
|
|
a4b143 |
- } else
|
|
|
a4b143 |
- goto failure;
|
|
|
a4b143 |
- }
|
|
|
a4b143 |
+ for (p = (const uint8_t*) str; length; p++) {
|
|
|
a4b143 |
+ int encoded_len = utf8_encoded_valid_unichar((const char *)p);
|
|
|
a4b143 |
+ int32_t val = utf8_encoded_to_unichar((const char*)p);
|
|
|
a4b143 |
|
|
|
a4b143 |
- if (o)
|
|
|
a4b143 |
- o++;
|
|
|
a4b143 |
- }
|
|
|
a4b143 |
+ if (encoded_len < 0 || val < 0 || is_unicode_control(val))
|
|
|
a4b143 |
+ return false;
|
|
|
a4b143 |
|
|
|
a4b143 |
- if (o) {
|
|
|
a4b143 |
- *o = '\0';
|
|
|
a4b143 |
- return output;
|
|
|
a4b143 |
+ length -= encoded_len;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
- return (char*) str;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
-failure:
|
|
|
a4b143 |
- return NULL;
|
|
|
a4b143 |
-}
|
|
|
a4b143 |
-
|
|
|
a4b143 |
-char* utf8_is_valid (const char *str) {
|
|
|
a4b143 |
- return utf8_validate(str, NULL);
|
|
|
a4b143 |
+ return true;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
-char* utf8_filter (const char *str) {
|
|
|
a4b143 |
- char *new_str;
|
|
|
a4b143 |
+const char *utf8_is_valid(const char *str) {
|
|
|
a4b143 |
+ const uint8_t *p;
|
|
|
a4b143 |
|
|
|
a4b143 |
assert(str);
|
|
|
a4b143 |
|
|
|
a4b143 |
- new_str = malloc(strlen(str) + 1);
|
|
|
a4b143 |
- if (!new_str)
|
|
|
a4b143 |
- return NULL;
|
|
|
a4b143 |
+ for (p = (const uint8_t*) str; *p; ) {
|
|
|
a4b143 |
+ int len = utf8_encoded_valid_unichar((const char *)p);
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ if (len < 0)
|
|
|
a4b143 |
+ return NULL;
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ p += len;
|
|
|
a4b143 |
+ }
|
|
|
a4b143 |
|
|
|
a4b143 |
- return utf8_validate(str, new_str);
|
|
|
a4b143 |
+ return str;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
char *ascii_is_valid(const char *str) {
|
|
|
a4b143 |
@@ -318,64 +239,6 @@ char *utf16_to_utf8(const void *s, size_t length) {
|
|
|
a4b143 |
return r;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
-/* count of characters used to encode one unicode char */
|
|
|
a4b143 |
-static int utf8_encoded_expected_len(const char *str) {
|
|
|
a4b143 |
- unsigned char c = (unsigned char)str[0];
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- if (c < 0x80)
|
|
|
a4b143 |
- return 1;
|
|
|
a4b143 |
- if ((c & 0xe0) == 0xc0)
|
|
|
a4b143 |
- return 2;
|
|
|
a4b143 |
- if ((c & 0xf0) == 0xe0)
|
|
|
a4b143 |
- return 3;
|
|
|
a4b143 |
- if ((c & 0xf8) == 0xf0)
|
|
|
a4b143 |
- return 4;
|
|
|
a4b143 |
- if ((c & 0xfc) == 0xf8)
|
|
|
a4b143 |
- return 5;
|
|
|
a4b143 |
- if ((c & 0xfe) == 0xfc)
|
|
|
a4b143 |
- return 6;
|
|
|
a4b143 |
- return 0;
|
|
|
a4b143 |
-}
|
|
|
a4b143 |
-
|
|
|
a4b143 |
-/* decode one unicode char */
|
|
|
a4b143 |
-static int utf8_encoded_to_unichar(const char *str) {
|
|
|
a4b143 |
- int unichar;
|
|
|
a4b143 |
- int len;
|
|
|
a4b143 |
- int i;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- len = utf8_encoded_expected_len(str);
|
|
|
a4b143 |
- switch (len) {
|
|
|
a4b143 |
- case 1:
|
|
|
a4b143 |
- return (int)str[0];
|
|
|
a4b143 |
- case 2:
|
|
|
a4b143 |
- unichar = str[0] & 0x1f;
|
|
|
a4b143 |
- break;
|
|
|
a4b143 |
- case 3:
|
|
|
a4b143 |
- unichar = (int)str[0] & 0x0f;
|
|
|
a4b143 |
- break;
|
|
|
a4b143 |
- case 4:
|
|
|
a4b143 |
- unichar = (int)str[0] & 0x07;
|
|
|
a4b143 |
- break;
|
|
|
a4b143 |
- case 5:
|
|
|
a4b143 |
- unichar = (int)str[0] & 0x03;
|
|
|
a4b143 |
- break;
|
|
|
a4b143 |
- case 6:
|
|
|
a4b143 |
- unichar = (int)str[0] & 0x01;
|
|
|
a4b143 |
- break;
|
|
|
a4b143 |
- default:
|
|
|
a4b143 |
- return -1;
|
|
|
a4b143 |
- }
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- for (i = 1; i < len; i++) {
|
|
|
a4b143 |
- if (((int)str[i] & 0xc0) != 0x80)
|
|
|
a4b143 |
- return -1;
|
|
|
a4b143 |
- unichar <<= 6;
|
|
|
a4b143 |
- unichar |= (int)str[i] & 0x3f;
|
|
|
a4b143 |
- }
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- return unichar;
|
|
|
a4b143 |
-}
|
|
|
a4b143 |
-
|
|
|
a4b143 |
/* expected size used to encode one unicode char */
|
|
|
a4b143 |
static int utf8_unichar_to_encoded_len(int unichar) {
|
|
|
a4b143 |
if (unichar < 0x80)
|
|
|
a4b143 |
diff --git a/src/shared/utf8.h b/src/shared/utf8.h
|
|
|
a4b143 |
index 7a5608c..22e1346 100644
|
|
|
a4b143 |
--- a/src/shared/utf8.h
|
|
|
a4b143 |
+++ b/src/shared/utf8.h
|
|
|
a4b143 |
@@ -25,12 +25,11 @@
|
|
|
a4b143 |
|
|
|
a4b143 |
#include "macro.h"
|
|
|
a4b143 |
|
|
|
a4b143 |
-char *utf8_is_valid(const char *s) _pure_;
|
|
|
a4b143 |
+const char *utf8_is_valid(const char *s) _pure_;
|
|
|
a4b143 |
char *ascii_is_valid(const char *s) _pure_;
|
|
|
a4b143 |
|
|
|
a4b143 |
bool utf8_is_printable(const char* str, size_t length) _pure_;
|
|
|
a4b143 |
|
|
|
a4b143 |
-char *utf8_filter(const char *s);
|
|
|
a4b143 |
char *ascii_filter(const char *s);
|
|
|
a4b143 |
|
|
|
a4b143 |
char *utf16_to_utf8(const void *s, size_t length);
|
|
|
a4b143 |
diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c
|
|
|
a4b143 |
index d2b9771..26cc37b 100644
|
|
|
a4b143 |
--- a/src/test/test-utf8.c
|
|
|
a4b143 |
+++ b/src/test/test-utf8.c
|
|
|
a4b143 |
@@ -47,6 +47,12 @@ static void test_udev_encode_string(void) {
|
|
|
a4b143 |
assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng"));
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
+static void test_utf8_is_printable(void) {
|
|
|
a4b143 |
+ assert_se(utf8_is_printable("ascii is valid\tunicode", 22));
|
|
|
a4b143 |
+ assert_se(utf8_is_printable("\342\204\242", 3));
|
|
|
a4b143 |
+ assert_se(!utf8_is_printable("\341\204", 2));
|
|
|
a4b143 |
+}
|
|
|
a4b143 |
+
|
|
|
a4b143 |
static void test_utf8_is_valid(void) {
|
|
|
a4b143 |
assert_se(utf8_is_valid("ascii is valid unicode"));
|
|
|
a4b143 |
assert_se(utf8_is_valid("\341\204\242"));
|
|
|
a4b143 |
@@ -55,5 +61,8 @@ static void test_utf8_is_valid(void) {
|
|
|
a4b143 |
|
|
|
a4b143 |
int main(int argc, char *argv[]) {
|
|
|
a4b143 |
test_utf8_is_valid();
|
|
|
a4b143 |
+ test_utf8_is_printable();
|
|
|
a4b143 |
test_udev_encode_string();
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ return 0;
|
|
|
a4b143 |
}
|