[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 08/16] include/grub/charset.h: Add grub_utf16_to_utf8_alloc()
From: |
Vitaly Kuzmichev |
Subject: |
[PATCH v5 08/16] include/grub/charset.h: Add grub_utf16_to_utf8_alloc() |
Date: |
Tue, 22 Aug 2023 23:39:16 +0200 |
This patch adds a wrapper function to perform conversion of UTF-16
string to UTF-8 string using inline function grub_utf16_to_utf8(), but
pre-allocating necessary space for the target buffer. This behavior is
similar to existing inversed function grub_utf8_to_utf16_alloc()
in kern/misc.c.
Signed-off-by: Vitaly Kuzmichev <vitaly.kuzmichev@rtsoft.de>
---
include/grub/charset.h | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/include/grub/charset.h b/include/grub/charset.h
index 099f23fbe..4324fefb8 100644
--- a/include/grub/charset.h
+++ b/include/grub/charset.h
@@ -20,6 +20,9 @@
#define GRUB_CHARSET_HEADER 1
#include <grub/types.h>
+#include <grub/safemath.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
#define GRUB_UINT8_1_LEADINGBIT 0x80
#define GRUB_UINT8_2_LEADINGBITS 0xc0
@@ -304,6 +307,31 @@ grub_utf16_strnlen (const void *s, grub_size_t maxlen)
return len;
}
+/* Convert UTF-16 to UTF-8. Allocates buffer of necessary space. */
+static inline grub_uint8_t *
+grub_utf16_to_utf8_alloc (const void *utf16, grub_size_t maxlen,
+ grub_utf16_type_t type)
+{
+ grub_size_t len16, size;
+ grub_uint8_t *utf8;
+
+ len16 = grub_utf16_strnlen (utf16, maxlen);
+
+ /* Check for integer overflow */
+ if (grub_mul (len16, GRUB_MAX_UTF8_PER_UTF16, &size) ||
+ grub_add (size, 1, &size))
+ {
+ grub_error (GRUB_ERR_BAD_ARGUMENT, N_("string too long"));
+ return NULL;
+ }
+
+ utf8 = grub_malloc (size);
+ if (utf8)
+ *grub_utf16_to_utf8 (utf8, utf16, len16, type) = '\0';
+
+ return utf8;
+}
+
#define GRUB_MAX_UTF8_PER_LATIN1 2
/* Convert Latin1 to UTF-8. */
--
2.34.1
- [PATCH v5 13/16] fs/f2fs: Simplify to use grub_utf16_to_utf8_alloc(), (continued)
- [PATCH v5 13/16] fs/f2fs: Simplify to use grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 07/16] include/grub/charset.h: Add grub_utf16_{strlen, strnlen}(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 06/16] include/grub/charset.h: Enhance grub_utf16_to_utf8(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 14/16] fs/fat: Simplify to use grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 08/16] include/grub/charset.h: Add grub_utf16_to_utf8_alloc(),
Vitaly Kuzmichev <=
- [PATCH v5 15/16] fs/iso9660: Simplify to use grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 09/16] partmap/gpt: Add grub_gpt_partition_get_label(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 16/16] fs/ntfs: Simplify to use grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22