|
|
2aacef |
From c48d12fc0abb8f113ed386c47bf02451ea8f853d Mon Sep 17 00:00:00 2001
|
|
|
2aacef |
From: Jan Janssen <medhefgo@web.de>
|
|
|
2aacef |
Date: Tue, 22 Nov 2022 15:55:07 +0100
|
|
|
2aacef |
Subject: [PATCH] boot: Add xstrn8_to_16
|
|
|
2aacef |
|
|
|
2aacef |
(cherry picked from commit 8ad7deffa95d33b5849ad6589dd52ab12e645edc)
|
|
|
2aacef |
|
|
|
2aacef |
Related: #2138081
|
|
|
2aacef |
---
|
|
|
2aacef |
src/boot/efi/efi-string.c | 78 +++++++++++++++++++++++++++++++++-
|
|
|
2aacef |
src/boot/efi/efi-string.h | 5 +++
|
|
|
2aacef |
src/boot/efi/test-efi-string.c | 27 ++++++++++++
|
|
|
2aacef |
3 files changed, 109 insertions(+), 1 deletion(-)
|
|
|
2aacef |
|
|
|
2aacef |
diff --git a/src/boot/efi/efi-string.c b/src/boot/efi/efi-string.c
|
|
|
2aacef |
index b877c6f224..2ba15673c9 100644
|
|
|
2aacef |
--- a/src/boot/efi/efi-string.c
|
|
|
2aacef |
+++ b/src/boot/efi/efi-string.c
|
|
|
2aacef |
@@ -9,7 +9,8 @@
|
|
|
2aacef |
# include "util.h"
|
|
|
2aacef |
#else
|
|
|
2aacef |
# include <stdlib.h>
|
|
|
2aacef |
-# include "macro.h"
|
|
|
2aacef |
+# include "alloc-util.h"
|
|
|
2aacef |
+# define xnew(t, n) ASSERT_SE_PTR(new(t, n))
|
|
|
2aacef |
# define xmalloc(n) ASSERT_SE_PTR(malloc(n))
|
|
|
2aacef |
#endif
|
|
|
2aacef |
|
|
|
2aacef |
@@ -138,6 +139,81 @@ DEFINE_STRCHR(char16_t, strchr16);
|
|
|
2aacef |
DEFINE_STRNDUP(char, xstrndup8, strnlen8);
|
|
|
2aacef |
DEFINE_STRNDUP(char16_t, xstrndup16, strnlen16);
|
|
|
2aacef |
|
|
|
2aacef |
+static unsigned utf8_to_unichar(const char *utf8, size_t n, char32_t *c) {
|
|
|
2aacef |
+ char32_t unichar;
|
|
|
2aacef |
+ unsigned len;
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert(utf8);
|
|
|
2aacef |
+ assert(c);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ if (!(utf8[0] & 0x80)) {
|
|
|
2aacef |
+ *c = utf8[0];
|
|
|
2aacef |
+ return 1;
|
|
|
2aacef |
+ } else if ((utf8[0] & 0xe0) == 0xc0) {
|
|
|
2aacef |
+ len = 2;
|
|
|
2aacef |
+ unichar = utf8[0] & 0x1f;
|
|
|
2aacef |
+ } else if ((utf8[0] & 0xf0) == 0xe0) {
|
|
|
2aacef |
+ len = 3;
|
|
|
2aacef |
+ unichar = utf8[0] & 0x0f;
|
|
|
2aacef |
+ } else if ((utf8[0] & 0xf8) == 0xf0) {
|
|
|
2aacef |
+ len = 4;
|
|
|
2aacef |
+ unichar = utf8[0] & 0x07;
|
|
|
2aacef |
+ } else if ((utf8[0] & 0xfc) == 0xf8) {
|
|
|
2aacef |
+ len = 5;
|
|
|
2aacef |
+ unichar = utf8[0] & 0x03;
|
|
|
2aacef |
+ } else if ((utf8[0] & 0xfe) == 0xfc) {
|
|
|
2aacef |
+ len = 6;
|
|
|
2aacef |
+ unichar = utf8[0] & 0x01;
|
|
|
2aacef |
+ } else {
|
|
|
2aacef |
+ *c = UINT32_MAX;
|
|
|
2aacef |
+ return 1;
|
|
|
2aacef |
+ }
|
|
|
2aacef |
+
|
|
|
2aacef |
+ if (len > n) {
|
|
|
2aacef |
+ *c = UINT32_MAX;
|
|
|
2aacef |
+ return len;
|
|
|
2aacef |
+ }
|
|
|
2aacef |
+
|
|
|
2aacef |
+ for (unsigned i = 1; i < len; i++) {
|
|
|
2aacef |
+ if ((utf8[i] & 0xc0) != 0x80) {
|
|
|
2aacef |
+ *c = UINT32_MAX;
|
|
|
2aacef |
+ return len;
|
|
|
2aacef |
+ }
|
|
|
2aacef |
+ unichar <<= 6;
|
|
|
2aacef |
+ unichar |= utf8[i] & 0x3f;
|
|
|
2aacef |
+ }
|
|
|
2aacef |
+
|
|
|
2aacef |
+ *c = unichar;
|
|
|
2aacef |
+ return len;
|
|
|
2aacef |
+}
|
|
|
2aacef |
+
|
|
|
2aacef |
+/* Convert UTF-8 to UCS-2, skipping any invalid or short byte sequences. */
|
|
|
2aacef |
+char16_t *xstrn8_to_16(const char *str8, size_t n) {
|
|
|
2aacef |
+ if (!str8 || n == 0)
|
|
|
2aacef |
+ return NULL;
|
|
|
2aacef |
+
|
|
|
2aacef |
+ size_t i = 0;
|
|
|
2aacef |
+ char16_t *str16 = xnew(char16_t, n + 1);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ while (n > 0 && *str8 != '\0') {
|
|
|
2aacef |
+ char32_t unichar;
|
|
|
2aacef |
+
|
|
|
2aacef |
+ size_t utf8len = utf8_to_unichar(str8, n, &unichar);
|
|
|
2aacef |
+ str8 += utf8len;
|
|
|
2aacef |
+ n = LESS_BY(n, utf8len);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ switch (unichar) {
|
|
|
2aacef |
+ case 0 ... 0xd7ffU:
|
|
|
2aacef |
+ case 0xe000U ... 0xffffU:
|
|
|
2aacef |
+ str16[i++] = unichar;
|
|
|
2aacef |
+ break;
|
|
|
2aacef |
+ }
|
|
|
2aacef |
+ }
|
|
|
2aacef |
+
|
|
|
2aacef |
+ str16[i] = '\0';
|
|
|
2aacef |
+ return str16;
|
|
|
2aacef |
+}
|
|
|
2aacef |
+
|
|
|
2aacef |
static bool efi_fnmatch_prefix(const char16_t *p, const char16_t *h, const char16_t **ret_p, const char16_t **ret_h) {
|
|
|
2aacef |
assert(p);
|
|
|
2aacef |
assert(h);
|
|
|
2aacef |
diff --git a/src/boot/efi/efi-string.h b/src/boot/efi/efi-string.h
|
|
|
2aacef |
index 1ebd5fd6b7..9b2a9ad1c5 100644
|
|
|
2aacef |
--- a/src/boot/efi/efi-string.h
|
|
|
2aacef |
+++ b/src/boot/efi/efi-string.h
|
|
|
2aacef |
@@ -99,6 +99,11 @@ static inline char16_t *xstrdup16(const char16_t *s) {
|
|
|
2aacef |
return xstrndup16(s, SIZE_MAX);
|
|
|
2aacef |
}
|
|
|
2aacef |
|
|
|
2aacef |
+char16_t *xstrn8_to_16(const char *str8, size_t n);
|
|
|
2aacef |
+static inline char16_t *xstr8_to_16(const char *str8) {
|
|
|
2aacef |
+ return xstrn8_to_16(str8, strlen8(str8));
|
|
|
2aacef |
+}
|
|
|
2aacef |
+
|
|
|
2aacef |
bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack);
|
|
|
2aacef |
|
|
|
2aacef |
bool parse_number8(const char *s, uint64_t *ret_u, const char **ret_tail);
|
|
|
2aacef |
diff --git a/src/boot/efi/test-efi-string.c b/src/boot/efi/test-efi-string.c
|
|
|
2aacef |
index 2b2359fe5c..7b43e1d629 100644
|
|
|
2aacef |
--- a/src/boot/efi/test-efi-string.c
|
|
|
2aacef |
+++ b/src/boot/efi/test-efi-string.c
|
|
|
2aacef |
@@ -324,6 +324,33 @@ TEST(xstrdup16) {
|
|
|
2aacef |
free(s);
|
|
|
2aacef |
}
|
|
|
2aacef |
|
|
|
2aacef |
+TEST(xstrn8_to_16) {
|
|
|
2aacef |
+ char16_t *s = NULL;
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert_se(xstrn8_to_16(NULL, 1) == NULL);
|
|
|
2aacef |
+ assert_se(xstrn8_to_16("a", 0) == NULL);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert_se(s = xstrn8_to_16("", 1));
|
|
|
2aacef |
+ assert_se(streq16(s, u""));
|
|
|
2aacef |
+ free(s);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert_se(s = xstrn8_to_16("1", 1));
|
|
|
2aacef |
+ assert_se(streq16(s, u"1"));
|
|
|
2aacef |
+ free(s);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert_se(s = xstr8_to_16("abcxyzABCXYZ09 .,-_#*!\"§$%&/()=?`~"));
|
|
|
2aacef |
+ assert_se(streq16(s, u"abcxyzABCXYZ09 .,-_#*!\"§$%&/()=?`~"));
|
|
|
2aacef |
+ free(s);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert_se(s = xstr8_to_16("ÿⱿ𝇉 😺"));
|
|
|
2aacef |
+ assert_se(streq16(s, u"ÿⱿ "));
|
|
|
2aacef |
+ free(s);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert_se(s = xstrn8_to_16("¶¶", 3));
|
|
|
2aacef |
+ assert_se(streq16(s, u"¶"));
|
|
|
2aacef |
+ free(s);
|
|
|
2aacef |
+}
|
|
|
2aacef |
+
|
|
|
2aacef |
#define TEST_FNMATCH_ONE(pattern, haystack, expect) \
|
|
|
2aacef |
({ \
|
|
|
2aacef |
assert_se(fnmatch(pattern, haystack, 0) == (expect ? 0 : FNM_NOMATCH)); \
|