diff options
author | Heinrich Schuchardt <xypron.glpk@gmx.de> | 2021-02-27 14:08:38 +0100 |
---|---|---|
committer | Heinrich Schuchardt <xypron.glpk@gmx.de> | 2021-03-07 17:37:13 +0100 |
commit | e91789e2f6611c0d7f3510691c154e524e7cfa43 (patch) | |
tree | 98e84ebbc76d03a249e8db3794b54ef834ba7a26 /lib/charset.c | |
parent | ddbaff53da5b99563fa371db0b09544e139fdabb (diff) | |
download | u-boot-e91789e2f6611c0d7f3510691c154e524e7cfa43.tar.gz u-boot-e91789e2f6611c0d7f3510691c154e524e7cfa43.tar.bz2 u-boot-e91789e2f6611c0d7f3510691c154e524e7cfa43.zip |
lib/charset: UTF-8 stream conversion
Provide functions to convert an UTF-8 stream to code page 437 or UTF-32.
Add unit tests.
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Diffstat (limited to 'lib/charset.c')
-rw-r--r-- | lib/charset.c | 55 |
1 files changed, 46 insertions, 9 deletions
diff --git a/lib/charset.c b/lib/charset.c index 946d5ee23e..f44c58d9d8 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -481,15 +481,6 @@ 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) { @@ -507,3 +498,49 @@ int utf_to_cp(s32 *c, const u16 *codepage) } return 0; } + +int utf8_to_cp437_stream(u8 c, char *buffer) +{ + char *end; + const char *pos; + s32 s; + int ret; + + for (;;) { + pos = buffer; + end = buffer + strlen(buffer); + *end++ = c; + *end = 0; + s = utf8_get(&pos); + if (s > 0) { + *buffer = 0; + ret = utf_to_cp(&s, codepage_437); + return s; + } + if (pos == end) + return 0; + *buffer = 0; + } +} + +int utf8_to_utf32_stream(u8 c, char *buffer) +{ + char *end; + const char *pos; + s32 s; + + for (;;) { + pos = buffer; + end = buffer + strlen(buffer); + *end++ = c; + *end = 0; + s = utf8_get(&pos); + if (s > 0) { + *buffer = 0; + return s; + } + if (pos == end) + return 0; + *buffer = 0; + } +} |