Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 2 | /* |
| 3 | * charset conversion utils |
| 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 8 | #include <common.h> |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 9 | #include <charset.h> |
Heinrich Schuchardt | b5130a8 | 2018-09-04 19:34:56 +0200 | [diff] [blame] | 10 | #include <capitalization.h> |
Heinrich Schuchardt | 70616a1 | 2021-02-27 14:08:35 +0100 | [diff] [blame] | 11 | #include <cp437.h> |
Ilias Apalodimas | 6974a4a | 2020-11-22 15:10:26 +0200 | [diff] [blame] | 12 | #include <efi_loader.h> |
Heinrich Schuchardt | 73bb90c | 2021-02-27 14:08:36 +0100 | [diff] [blame] | 13 | #include <errno.h> |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | |
Heinrich Schuchardt | 70616a1 | 2021-02-27 14:08:35 +0100 | [diff] [blame] | 16 | /** |
| 17 | * codepage_437 - Unicode to codepage 437 translation table |
| 18 | */ |
| 19 | const u16 codepage_437[128] = CP437; |
| 20 | |
Heinrich Schuchardt | b5130a8 | 2018-09-04 19:34:56 +0200 | [diff] [blame] | 21 | static struct capitalization_table capitalization_table[] = |
| 22 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| 23 | UNICODE_CAPITALIZATION_TABLE; |
| 24 | #elif CONFIG_FAT_DEFAULT_CODEPAGE == 1250 |
| 25 | CP1250_CAPITALIZATION_TABLE; |
| 26 | #else |
| 27 | CP437_CAPITALIZATION_TABLE; |
| 28 | #endif |
| 29 | |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 30 | /** |
| 31 | * get_code() - read Unicode code point from UTF-8 stream |
| 32 | * |
| 33 | * @read_u8: - stream reader |
| 34 | * @src: - string buffer passed to stream reader, optional |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 35 | * Return: - Unicode code point, or -1 |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 36 | */ |
| 37 | static int get_code(u8 (*read_u8)(void *data), void *data) |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 38 | { |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 39 | s32 ch = 0; |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 40 | |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 41 | ch = read_u8(data); |
| 42 | if (!ch) |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 43 | return 0; |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 44 | if (ch >= 0xc2 && ch <= 0xf4) { |
| 45 | int code = 0; |
| 46 | |
| 47 | if (ch >= 0xe0) { |
| 48 | if (ch >= 0xf0) { |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 49 | /* 0xf0 - 0xf4 */ |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 50 | ch &= 0x07; |
| 51 | code = ch << 18; |
| 52 | ch = read_u8(data); |
| 53 | if (ch < 0x80 || ch > 0xbf) |
| 54 | goto error; |
| 55 | ch &= 0x3f; |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 56 | } else { |
| 57 | /* 0xe0 - 0xef */ |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 58 | ch &= 0x0f; |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 59 | } |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 60 | code += ch << 12; |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 61 | if ((code >= 0xD800 && code <= 0xDFFF) || |
| 62 | code >= 0x110000) |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 63 | goto error; |
| 64 | ch = read_u8(data); |
| 65 | if (ch < 0x80 || ch > 0xbf) |
| 66 | goto error; |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 67 | } |
| 68 | /* 0xc0 - 0xdf or continuation byte (0x80 - 0xbf) */ |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 69 | ch &= 0x3f; |
| 70 | code += ch << 6; |
| 71 | ch = read_u8(data); |
| 72 | if (ch < 0x80 || ch > 0xbf) |
| 73 | goto error; |
| 74 | ch &= 0x3f; |
| 75 | ch += code; |
| 76 | } else if (ch >= 0x80) { |
| 77 | goto error; |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 78 | } |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 79 | return ch; |
| 80 | error: |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 81 | return -1; |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * read_string() - read byte from character string |
| 86 | * |
| 87 | * @data: - pointer to string |
| 88 | * Return: - byte read |
| 89 | * |
| 90 | * The string pointer is incremented if it does not point to '\0'. |
| 91 | */ |
| 92 | static u8 read_string(void *data) |
| 93 | |
| 94 | { |
| 95 | const char **src = (const char **)data; |
| 96 | u8 c; |
| 97 | |
| 98 | if (!src || !*src || !**src) |
| 99 | return 0; |
| 100 | c = **src; |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 101 | ++*src; |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 102 | return c; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * read_console() - read byte from console |
| 107 | * |
Heinrich Schuchardt | 60d7987 | 2018-10-02 06:43:38 +0200 | [diff] [blame] | 108 | * @data - not used, needed to match interface |
| 109 | * Return: - byte read or 0 on error |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 110 | */ |
| 111 | static u8 read_console(void *data) |
| 112 | { |
Heinrich Schuchardt | 60d7987 | 2018-10-02 06:43:38 +0200 | [diff] [blame] | 113 | int ch; |
| 114 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 115 | ch = getchar(); |
Heinrich Schuchardt | 60d7987 | 2018-10-02 06:43:38 +0200 | [diff] [blame] | 116 | if (ch < 0) |
| 117 | ch = 0; |
| 118 | return ch; |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int console_read_unicode(s32 *code) |
| 122 | { |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 123 | for (;;) { |
| 124 | s32 c; |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 125 | |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 126 | if (!tstc()) { |
| 127 | /* No input available */ |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | /* Read Unicode code */ |
| 132 | c = get_code(read_console, NULL); |
| 133 | if (c > 0) { |
| 134 | *code = c; |
| 135 | return 0; |
| 136 | } |
| 137 | } |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | s32 utf8_get(const char **src) |
| 141 | { |
| 142 | return get_code(read_string, src); |
Heinrich Schuchardt | d8c2823 | 2018-08-31 21:31:27 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int utf8_put(s32 code, char **dst) |
| 146 | { |
| 147 | if (!dst || !*dst) |
| 148 | return -1; |
| 149 | if ((code >= 0xD800 && code <= 0xDFFF) || code >= 0x110000) |
| 150 | return -1; |
| 151 | if (code <= 0x007F) { |
| 152 | **dst = code; |
| 153 | } else { |
| 154 | if (code <= 0x07FF) { |
| 155 | **dst = code >> 6 | 0xC0; |
| 156 | } else { |
| 157 | if (code < 0x10000) { |
| 158 | **dst = code >> 12 | 0xE0; |
| 159 | } else { |
| 160 | **dst = code >> 18 | 0xF0; |
| 161 | ++*dst; |
| 162 | **dst = (code >> 12 & 0x3F) | 0x80; |
| 163 | } |
| 164 | ++*dst; |
| 165 | **dst = (code >> 6 & 0x3F) | 0x80; |
| 166 | } |
| 167 | ++*dst; |
| 168 | **dst = (code & 0x3F) | 0x80; |
| 169 | } |
| 170 | ++*dst; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | size_t utf8_utf16_strnlen(const char *src, size_t count) |
| 175 | { |
| 176 | size_t len = 0; |
| 177 | |
| 178 | for (; *src && count; --count) { |
| 179 | s32 code = utf8_get(&src); |
| 180 | |
| 181 | if (!code) |
| 182 | break; |
| 183 | if (code < 0) { |
| 184 | /* Reserve space for a replacement character */ |
| 185 | len += 1; |
| 186 | } else if (code < 0x10000) { |
| 187 | len += 1; |
| 188 | } else { |
| 189 | len += 2; |
| 190 | } |
| 191 | } |
| 192 | return len; |
| 193 | } |
| 194 | |
| 195 | int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count) |
| 196 | { |
| 197 | if (!src || !dst || !*dst) |
| 198 | return -1; |
| 199 | |
| 200 | for (; count && *src; --count) { |
| 201 | s32 code = utf8_get(&src); |
| 202 | |
| 203 | if (code < 0) |
| 204 | code = '?'; |
| 205 | utf16_put(code, dst); |
| 206 | } |
| 207 | **dst = 0; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | s32 utf16_get(const u16 **src) |
| 212 | { |
| 213 | s32 code, code2; |
| 214 | |
| 215 | if (!src || !*src) |
| 216 | return -1; |
| 217 | if (!**src) |
| 218 | return 0; |
| 219 | code = **src; |
| 220 | ++*src; |
| 221 | if (code >= 0xDC00 && code <= 0xDFFF) |
| 222 | return -1; |
| 223 | if (code >= 0xD800 && code <= 0xDBFF) { |
| 224 | if (!**src) |
| 225 | return -1; |
| 226 | code &= 0x3ff; |
| 227 | code <<= 10; |
| 228 | code += 0x10000; |
| 229 | code2 = **src; |
| 230 | ++*src; |
| 231 | if (code2 <= 0xDC00 || code2 >= 0xDFFF) |
| 232 | return -1; |
| 233 | code2 &= 0x3ff; |
| 234 | code += code2; |
| 235 | } |
| 236 | return code; |
| 237 | } |
| 238 | |
| 239 | int utf16_put(s32 code, u16 **dst) |
| 240 | { |
| 241 | if (!dst || !*dst) |
| 242 | return -1; |
| 243 | if ((code >= 0xD800 && code <= 0xDFFF) || code >= 0x110000) |
| 244 | return -1; |
| 245 | if (code < 0x10000) { |
| 246 | **dst = code; |
| 247 | } else { |
| 248 | code -= 0x10000; |
| 249 | **dst = code >> 10 | 0xD800; |
| 250 | ++*dst; |
| 251 | **dst = (code & 0x3ff) | 0xDC00; |
| 252 | } |
| 253 | ++*dst; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | size_t utf16_strnlen(const u16 *src, size_t count) |
| 258 | { |
| 259 | size_t len = 0; |
| 260 | |
| 261 | for (; *src && count; --count) { |
| 262 | s32 code = utf16_get(&src); |
| 263 | |
| 264 | if (!code) |
| 265 | break; |
| 266 | /* |
| 267 | * In case of an illegal sequence still reserve space for a |
| 268 | * replacement character. |
| 269 | */ |
| 270 | ++len; |
| 271 | } |
| 272 | return len; |
| 273 | } |
| 274 | |
| 275 | size_t utf16_utf8_strnlen(const u16 *src, size_t count) |
| 276 | { |
| 277 | size_t len = 0; |
| 278 | |
| 279 | for (; *src && count; --count) { |
| 280 | s32 code = utf16_get(&src); |
| 281 | |
| 282 | if (!code) |
| 283 | break; |
| 284 | if (code < 0) |
| 285 | /* Reserve space for a replacement character */ |
| 286 | len += 1; |
| 287 | else if (code < 0x80) |
| 288 | len += 1; |
| 289 | else if (code < 0x800) |
| 290 | len += 2; |
| 291 | else if (code < 0x10000) |
| 292 | len += 3; |
| 293 | else |
| 294 | len += 4; |
| 295 | } |
| 296 | return len; |
| 297 | } |
| 298 | |
| 299 | int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count) |
| 300 | { |
| 301 | if (!src || !dst || !*dst) |
| 302 | return -1; |
| 303 | |
| 304 | for (; count && *src; --count) { |
| 305 | s32 code = utf16_get(&src); |
| 306 | |
| 307 | if (code < 0) |
| 308 | code = '?'; |
| 309 | utf8_put(code, dst); |
| 310 | } |
| 311 | **dst = 0; |
| 312 | return 0; |
| 313 | } |
| 314 | |
Heinrich Schuchardt | b5130a8 | 2018-09-04 19:34:56 +0200 | [diff] [blame] | 315 | s32 utf_to_lower(const s32 code) |
| 316 | { |
| 317 | struct capitalization_table *pos = capitalization_table; |
| 318 | s32 ret = code; |
| 319 | |
| 320 | if (code <= 0x7f) { |
| 321 | if (code >= 'A' && code <= 'Z') |
| 322 | ret += 0x20; |
| 323 | return ret; |
| 324 | } |
| 325 | for (; pos->upper; ++pos) { |
| 326 | if (pos->upper == code) { |
| 327 | ret = pos->lower; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | s32 utf_to_upper(const s32 code) |
| 335 | { |
| 336 | struct capitalization_table *pos = capitalization_table; |
| 337 | s32 ret = code; |
| 338 | |
| 339 | if (code <= 0x7f) { |
| 340 | if (code >= 'a' && code <= 'z') |
| 341 | ret -= 0x20; |
| 342 | return ret; |
| 343 | } |
| 344 | for (; pos->lower; ++pos) { |
| 345 | if (pos->lower == code) { |
| 346 | ret = pos->upper; |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | return ret; |
| 351 | } |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 352 | |
AKASHI Takahiro | f8062c9 | 2019-09-18 10:26:29 +0900 | [diff] [blame] | 353 | /* |
| 354 | * u16_strncmp() - compare two u16 string |
| 355 | * |
| 356 | * @s1: first string to compare |
| 357 | * @s2: second string to compare |
| 358 | * @n: maximum number of u16 to compare |
| 359 | * Return: 0 if the first n u16 are the same in s1 and s2 |
| 360 | * < 0 if the first different u16 in s1 is less than the |
| 361 | * corresponding u16 in s2 |
| 362 | * > 0 if the first different u16 in s1 is greater than the |
| 363 | * corresponding u16 in s2 |
| 364 | */ |
| 365 | int u16_strncmp(const u16 *s1, const u16 *s2, size_t n) |
| 366 | { |
| 367 | int ret = 0; |
| 368 | |
| 369 | for (; n; --n, ++s1, ++s2) { |
| 370 | ret = *s1 - *s2; |
| 371 | if (ret || !*s1) |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
Ilias Apalodimas | 6974a4a | 2020-11-22 15:10:26 +0200 | [diff] [blame] | 378 | size_t __efi_runtime u16_strnlen(const u16 *in, size_t count) |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 379 | { |
| 380 | size_t i; |
| 381 | for (i = 0; count-- && in[i]; i++); |
| 382 | return i; |
| 383 | } |
| 384 | |
Sughosh Ganu | 4835d35 | 2020-05-06 22:12:41 +0300 | [diff] [blame] | 385 | size_t u16_strsize(const void *in) |
| 386 | { |
| 387 | return (u16_strlen(in) + 1) * sizeof(u16); |
| 388 | } |
| 389 | |
Akashi, Takahiro | 2a3537a | 2018-12-14 19:10:38 +0900 | [diff] [blame] | 390 | u16 *u16_strcpy(u16 *dest, const u16 *src) |
| 391 | { |
| 392 | u16 *tmp = dest; |
| 393 | |
| 394 | for (;; dest++, src++) { |
| 395 | *dest = *src; |
| 396 | if (!*src) |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | return tmp; |
| 401 | } |
| 402 | |
Heinrich Schuchardt | 317068b | 2019-07-14 17:28:49 +0200 | [diff] [blame] | 403 | u16 *u16_strdup(const void *src) |
Akashi, Takahiro | 2a3537a | 2018-12-14 19:10:38 +0900 | [diff] [blame] | 404 | { |
| 405 | u16 *new; |
Heinrich Schuchardt | 317068b | 2019-07-14 17:28:49 +0200 | [diff] [blame] | 406 | size_t len; |
Akashi, Takahiro | 2a3537a | 2018-12-14 19:10:38 +0900 | [diff] [blame] | 407 | |
| 408 | if (!src) |
| 409 | return NULL; |
Heinrich Schuchardt | 967407d | 2022-04-02 11:46:59 +0200 | [diff] [blame] | 410 | len = u16_strsize(src); |
Heinrich Schuchardt | 317068b | 2019-07-14 17:28:49 +0200 | [diff] [blame] | 411 | new = malloc(len); |
Akashi, Takahiro | 2a3537a | 2018-12-14 19:10:38 +0900 | [diff] [blame] | 412 | if (!new) |
| 413 | return NULL; |
Heinrich Schuchardt | 317068b | 2019-07-14 17:28:49 +0200 | [diff] [blame] | 414 | memcpy(new, src, len); |
Akashi, Takahiro | 2a3537a | 2018-12-14 19:10:38 +0900 | [diff] [blame] | 415 | |
| 416 | return new; |
| 417 | } |
| 418 | |
Masahisa Kojima | eca08ce | 2022-04-28 17:09:34 +0900 | [diff] [blame] | 419 | size_t u16_strlcat(u16 *dest, const u16 *src, size_t count) |
| 420 | { |
| 421 | size_t destlen = u16_strlen(dest); |
| 422 | size_t srclen = u16_strlen(src); |
| 423 | size_t ret = destlen + srclen + 1; |
| 424 | |
| 425 | if (destlen >= count) |
| 426 | return ret; |
| 427 | if (ret > count) |
| 428 | srclen -= ret - count; |
| 429 | memcpy(&dest[destlen], src, 2 * srclen); |
| 430 | dest[destlen + srclen] = 0x0000; |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 435 | /* Convert UTF-16 to UTF-8. */ |
| 436 | uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size) |
| 437 | { |
| 438 | uint32_t code_high = 0; |
| 439 | |
| 440 | while (size--) { |
| 441 | uint32_t code = *src++; |
| 442 | |
| 443 | if (code_high) { |
| 444 | if (code >= 0xDC00 && code <= 0xDFFF) { |
| 445 | /* Surrogate pair. */ |
| 446 | code = ((code_high - 0xD800) << 10) + (code - 0xDC00) + 0x10000; |
| 447 | |
| 448 | *dest++ = (code >> 18) | 0xF0; |
| 449 | *dest++ = ((code >> 12) & 0x3F) | 0x80; |
| 450 | *dest++ = ((code >> 6) & 0x3F) | 0x80; |
| 451 | *dest++ = (code & 0x3F) | 0x80; |
| 452 | } else { |
| 453 | /* Error... */ |
| 454 | *dest++ = '?'; |
| 455 | /* *src may be valid. Don't eat it. */ |
| 456 | src--; |
| 457 | } |
| 458 | |
| 459 | code_high = 0; |
| 460 | } else { |
| 461 | if (code <= 0x007F) { |
| 462 | *dest++ = code; |
| 463 | } else if (code <= 0x07FF) { |
| 464 | *dest++ = (code >> 6) | 0xC0; |
| 465 | *dest++ = (code & 0x3F) | 0x80; |
| 466 | } else if (code >= 0xD800 && code <= 0xDBFF) { |
| 467 | code_high = code; |
| 468 | continue; |
| 469 | } else if (code >= 0xDC00 && code <= 0xDFFF) { |
| 470 | /* Error... */ |
| 471 | *dest++ = '?'; |
| 472 | } else if (code < 0x10000) { |
| 473 | *dest++ = (code >> 12) | 0xE0; |
| 474 | *dest++ = ((code >> 6) & 0x3F) | 0x80; |
| 475 | *dest++ = (code & 0x3F) | 0x80; |
| 476 | } else { |
| 477 | *dest++ = (code >> 18) | 0xF0; |
| 478 | *dest++ = ((code >> 12) & 0x3F) | 0x80; |
| 479 | *dest++ = ((code >> 6) & 0x3F) | 0x80; |
| 480 | *dest++ = (code & 0x3F) | 0x80; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return dest; |
| 486 | } |
Heinrich Schuchardt | 73bb90c | 2021-02-27 14:08:36 +0100 | [diff] [blame] | 487 | |
Heinrich Schuchardt | 73bb90c | 2021-02-27 14:08:36 +0100 | [diff] [blame] | 488 | int utf_to_cp(s32 *c, const u16 *codepage) |
| 489 | { |
| 490 | if (*c >= 0x80) { |
| 491 | int j; |
| 492 | |
| 493 | /* Look up codepage translation */ |
| 494 | for (j = 0; j < 0x80; ++j) { |
| 495 | if (*c == codepage[j]) { |
| 496 | *c = j + 0x80; |
| 497 | return 0; |
| 498 | } |
| 499 | } |
| 500 | *c = '?'; |
| 501 | return -ENOENT; |
| 502 | } |
| 503 | return 0; |
| 504 | } |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 505 | |
| 506 | int utf8_to_cp437_stream(u8 c, char *buffer) |
| 507 | { |
| 508 | char *end; |
| 509 | const char *pos; |
| 510 | s32 s; |
| 511 | int ret; |
| 512 | |
| 513 | for (;;) { |
| 514 | pos = buffer; |
| 515 | end = buffer + strlen(buffer); |
| 516 | *end++ = c; |
| 517 | *end = 0; |
| 518 | s = utf8_get(&pos); |
| 519 | if (s > 0) { |
| 520 | *buffer = 0; |
| 521 | ret = utf_to_cp(&s, codepage_437); |
| 522 | return s; |
| 523 | } |
| 524 | if (pos == end) |
| 525 | return 0; |
| 526 | *buffer = 0; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | int utf8_to_utf32_stream(u8 c, char *buffer) |
| 531 | { |
| 532 | char *end; |
| 533 | const char *pos; |
| 534 | s32 s; |
| 535 | |
| 536 | for (;;) { |
| 537 | pos = buffer; |
| 538 | end = buffer + strlen(buffer); |
| 539 | *end++ = c; |
| 540 | *end = 0; |
| 541 | s = utf8_get(&pos); |
| 542 | if (s > 0) { |
| 543 | *buffer = 0; |
| 544 | return s; |
| 545 | } |
| 546 | if (pos == end) |
| 547 | return 0; |
| 548 | *buffer = 0; |
| 549 | } |
| 550 | } |