1 // SPDX-License-Identifier: GPL-2.0+
3 * charset conversion utils
5 * Copyright (c) 2017 Rob Clark
10 #include <capitalization.h>
12 #include <efi_loader.h>
16 * codepage_437 - Unicode to codepage 437 translation table
18 const u16 codepage_437[128] = CP437;
20 static struct capitalization_table capitalization_table[] =
21 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
22 UNICODE_CAPITALIZATION_TABLE;
23 #elif CONFIG_FAT_DEFAULT_CODEPAGE == 1250
24 CP1250_CAPITALIZATION_TABLE;
26 CP437_CAPITALIZATION_TABLE;
30 * get_code() - read Unicode code point from UTF-8 stream
32 * @read_u8: - stream reader
33 * @src: - string buffer passed to stream reader, optional
34 * Return: - Unicode code point
36 static int get_code(u8 (*read_u8)(void *data), void *data)
43 if (ch >= 0xc2 && ch <= 0xf4) {
52 if (ch < 0x80 || ch > 0xbf)
60 if ((code >= 0xD800 && code <= 0xDFFF) ||
64 if (ch < 0x80 || ch > 0xbf)
67 /* 0xc0 - 0xdf or continuation byte (0x80 - 0xbf) */
71 if (ch < 0x80 || ch > 0xbf)
75 } else if (ch >= 0x80) {
84 * read_string() - read byte from character string
86 * @data: - pointer to string
89 * The string pointer is incremented if it does not point to '\0'.
91 static u8 read_string(void *data)
94 const char **src = (const char **)data;
97 if (!src || !*src || !**src)
105 * read_console() - read byte from console
107 * @data - not used, needed to match interface
108 * Return: - byte read or 0 on error
110 static u8 read_console(void *data)
120 int console_read_unicode(s32 *code)
123 /* No input available */
127 /* Read Unicode code */
128 *code = get_code(read_console, NULL);
132 s32 utf8_get(const char **src)
134 return get_code(read_string, src);
137 int utf8_put(s32 code, char **dst)
141 if ((code >= 0xD800 && code <= 0xDFFF) || code >= 0x110000)
143 if (code <= 0x007F) {
146 if (code <= 0x07FF) {
147 **dst = code >> 6 | 0xC0;
149 if (code < 0x10000) {
150 **dst = code >> 12 | 0xE0;
152 **dst = code >> 18 | 0xF0;
154 **dst = (code >> 12 & 0x3F) | 0x80;
157 **dst = (code >> 6 & 0x3F) | 0x80;
160 **dst = (code & 0x3F) | 0x80;
166 size_t utf8_utf16_strnlen(const char *src, size_t count)
170 for (; *src && count; --count) {
171 s32 code = utf8_get(&src);
176 /* Reserve space for a replacement character */
178 } else if (code < 0x10000) {
187 int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count)
189 if (!src || !dst || !*dst)
192 for (; count && *src; --count) {
193 s32 code = utf8_get(&src);
197 utf16_put(code, dst);
203 s32 utf16_get(const u16 **src)
213 if (code >= 0xDC00 && code <= 0xDFFF)
215 if (code >= 0xD800 && code <= 0xDBFF) {
223 if (code2 <= 0xDC00 || code2 >= 0xDFFF)
231 int utf16_put(s32 code, u16 **dst)
235 if ((code >= 0xD800 && code <= 0xDFFF) || code >= 0x110000)
237 if (code < 0x10000) {
241 **dst = code >> 10 | 0xD800;
243 **dst = (code & 0x3ff) | 0xDC00;
249 size_t utf16_strnlen(const u16 *src, size_t count)
253 for (; *src && count; --count) {
254 s32 code = utf16_get(&src);
259 * In case of an illegal sequence still reserve space for a
260 * replacement character.
267 size_t utf16_utf8_strnlen(const u16 *src, size_t count)
271 for (; *src && count; --count) {
272 s32 code = utf16_get(&src);
277 /* Reserve space for a replacement character */
279 else if (code < 0x80)
281 else if (code < 0x800)
283 else if (code < 0x10000)
291 int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count)
293 if (!src || !dst || !*dst)
296 for (; count && *src; --count) {
297 s32 code = utf16_get(&src);
307 s32 utf_to_lower(const s32 code)
309 struct capitalization_table *pos = capitalization_table;
313 if (code >= 'A' && code <= 'Z')
317 for (; pos->upper; ++pos) {
318 if (pos->upper == code) {
326 s32 utf_to_upper(const s32 code)
328 struct capitalization_table *pos = capitalization_table;
332 if (code >= 'a' && code <= 'z')
336 for (; pos->lower; ++pos) {
337 if (pos->lower == code) {
346 * u16_strncmp() - compare two u16 string
348 * @s1: first string to compare
349 * @s2: second string to compare
350 * @n: maximum number of u16 to compare
351 * Return: 0 if the first n u16 are the same in s1 and s2
352 * < 0 if the first different u16 in s1 is less than the
353 * corresponding u16 in s2
354 * > 0 if the first different u16 in s1 is greater than the
355 * corresponding u16 in s2
357 int u16_strncmp(const u16 *s1, const u16 *s2, size_t n)
361 for (; n; --n, ++s1, ++s2) {
370 size_t u16_strlen(const void *in)
372 const char *pos = in;
375 for (; pos[0] || pos[1]; pos += 2)
377 ret = pos - (char *)in;
382 size_t __efi_runtime u16_strnlen(const u16 *in, size_t count)
385 for (i = 0; count-- && in[i]; i++);
389 size_t u16_strsize(const void *in)
391 return (u16_strlen(in) + 1) * sizeof(u16);
394 u16 *u16_strcpy(u16 *dest, const u16 *src)
398 for (;; dest++, src++) {
407 u16 *u16_strdup(const void *src)
414 len = (u16_strlen(src) + 1) * sizeof(u16);
418 memcpy(new, src, len);
423 /* Convert UTF-16 to UTF-8. */
424 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
426 uint32_t code_high = 0;
429 uint32_t code = *src++;
432 if (code >= 0xDC00 && code <= 0xDFFF) {
433 /* Surrogate pair. */
434 code = ((code_high - 0xD800) << 10) + (code - 0xDC00) + 0x10000;
436 *dest++ = (code >> 18) | 0xF0;
437 *dest++ = ((code >> 12) & 0x3F) | 0x80;
438 *dest++ = ((code >> 6) & 0x3F) | 0x80;
439 *dest++ = (code & 0x3F) | 0x80;
443 /* *src may be valid. Don't eat it. */
449 if (code <= 0x007F) {
451 } else if (code <= 0x07FF) {
452 *dest++ = (code >> 6) | 0xC0;
453 *dest++ = (code & 0x3F) | 0x80;
454 } else if (code >= 0xD800 && code <= 0xDBFF) {
457 } else if (code >= 0xDC00 && code <= 0xDFFF) {
460 } else if (code < 0x10000) {
461 *dest++ = (code >> 12) | 0xE0;
462 *dest++ = ((code >> 6) & 0x3F) | 0x80;
463 *dest++ = (code & 0x3F) | 0x80;
465 *dest++ = (code >> 18) | 0xF0;
466 *dest++ = ((code >> 12) & 0x3F) | 0x80;
467 *dest++ = ((code >> 6) & 0x3F) | 0x80;
468 *dest++ = (code & 0x3F) | 0x80;