summaryrefslogtreecommitdiffstats
path: root/lib/charset.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/charset.c')
-rw-r--r--lib/charset.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/charset.c b/lib/charset.c
index 814847d165..1345c8f9f0 100644
--- a/lib/charset.c
+++ b/lib/charset.c
@@ -10,6 +10,7 @@
#include <capitalization.h>
#include <cp437.h>
#include <efi_loader.h>
+#include <errno.h>
#include <malloc.h>
/**
@@ -472,3 +473,30 @@ uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
return dest;
}
+
+/**
+ * utf_to_cp() - translate Unicode code point to 8bit codepage
+ *
+ * Codepoints that do not exist in the codepage are rendered as question mark.
+ *
+ * @c: pointer to Unicode code point to be translated
+ * @codepage: Unicode to codepage translation table
+ * Return: 0 on success, -ENOENT if codepoint cannot be translated
+ */
+int utf_to_cp(s32 *c, const u16 *codepage)
+{
+ if (*c >= 0x80) {
+ int j;
+
+ /* Look up codepage translation */
+ for (j = 0; j < 0x80; ++j) {
+ if (*c == codepage[j]) {
+ *c = j + 0x80;
+ return 0;
+ }
+ }
+ *c = '?';
+ return -ENOENT;
+ }
+ return 0;
+}