diff options
author | Heinrich Schuchardt <xypron.glpk@gmx.de> | 2021-02-27 14:08:36 +0100 |
---|---|---|
committer | Heinrich Schuchardt <xypron.glpk@gmx.de> | 2021-03-07 17:37:13 +0100 |
commit | 73bb90cabcdffcd528d1002a12779779196bf200 (patch) | |
tree | 5c4fb2a3ebecf079f4cadacc41aa9e45354e69a0 /lib | |
parent | 70616a1ed8c7fe22aa19eb674915623bd236926f (diff) | |
download | u-boot-73bb90cabcdffcd528d1002a12779779196bf200.tar.gz |
efi_loader: carve out utf_to_cp()
Carve out a function to translate a Unicode code point to an 8bit codepage.
Provide a unit test for the new function.
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/charset.c | 28 | ||||
-rw-r--r-- | lib/efi_loader/efi_unicode_collation.c | 19 |
2 files changed, 31 insertions, 16 deletions
diff --git a/lib/charset.c b/lib/charset.c index 814847d1654..1345c8f9f05 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; +} diff --git a/lib/efi_loader/efi_unicode_collation.c b/lib/efi_loader/efi_unicode_collation.c index bf5314c4ff6..36be798f64b 100644 --- a/lib/efi_loader/efi_unicode_collation.c +++ b/lib/efi_loader/efi_unicode_collation.c @@ -300,23 +300,10 @@ static bool EFIAPI efi_str_to_fat(struct efi_unicode_collation_protocol *this, break; } c = utf_to_upper(c); - if (c >= 0x80) { - int j; - - /* Look for codepage translation */ - for (j = 0; j < 0x80; ++j) { - if (c == codepage[j]) { - c = j + 0x80; - break; - } - } - if (j >= 0x80) { - c = '_'; - ret = true; - } - } else if (c && (c < 0x20 || strchr(illegal, c))) { - c = '_'; + if (utf_to_cp(&c, codepage) || + (c && (c < 0x20 || strchr(illegal, c)))) { ret = true; + c = '_'; } fat[i] = c; |