AKASHI Takahiro | 077153e | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * String functions |
| 4 | * |
| 5 | * Copyright (c) 2020 AKASHI Takahiro, Linaro Limited |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <charset.h> |
Heinrich Schuchardt | a07ee3c | 2021-04-21 12:39:15 +0200 | [diff] [blame] | 10 | #include <efi_loader.h> |
Paul Barker | 39434a9 | 2022-10-05 13:18:35 +0100 | [diff] [blame] | 11 | #include <malloc.h> |
AKASHI Takahiro | 077153e | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * efi_create_indexed_name - create a string name with an index |
| 15 | * @buffer: Buffer |
| 16 | * @name: Name string |
| 17 | * @index: Index |
| 18 | * |
| 19 | * Create a utf-16 string with @name, appending @index. |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 20 | * For example, u"Capsule0001" |
AKASHI Takahiro | 077153e | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 21 | * |
| 22 | * The caller must ensure that the buffer has enough space for the resulting |
| 23 | * string including the trailing L'\0'. |
| 24 | * |
| 25 | * Return: A pointer to the next position after the created string |
| 26 | * in @buffer, or NULL otherwise |
| 27 | */ |
Ilias Apalodimas | fe179d7 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 28 | u16 *efi_create_indexed_name(u16 *buffer, size_t buffer_size, const char *name, |
| 29 | unsigned int index) |
AKASHI Takahiro | 077153e | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 30 | { |
| 31 | u16 *p = buffer; |
| 32 | char index_buf[5]; |
Ilias Apalodimas | fe179d7 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 33 | size_t size; |
AKASHI Takahiro | 077153e | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 34 | |
Ilias Apalodimas | fe179d7 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 35 | size = (utf8_utf16_strlen(name) * sizeof(u16) + |
| 36 | sizeof(index_buf) * sizeof(u16)); |
| 37 | if (buffer_size < size) |
| 38 | return NULL; |
AKASHI Takahiro | 077153e | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 39 | utf8_utf16_strcpy(&p, name); |
Ilias Apalodimas | fe179d7 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 40 | snprintf(index_buf, sizeof(index_buf), "%04X", index); |
AKASHI Takahiro | 077153e | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 41 | utf8_utf16_strcpy(&p, index_buf); |
| 42 | |
| 43 | return p; |
| 44 | } |
Paul Barker | 39434a9 | 2022-10-05 13:18:35 +0100 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * efi_convert_string - Convert an ASCII or UTF-8 string to UTF-16 |
| 48 | * @str: String to be converted |
| 49 | * |
| 50 | * Return: Converted string in UTF-16 format. The caller is responsible for |
| 51 | * freeing this string when it is no longer needed. |
| 52 | */ |
| 53 | efi_string_t efi_convert_string(const char *str) |
| 54 | { |
| 55 | efi_string_t str_16, tmp; |
| 56 | size_t sz_16; |
| 57 | |
| 58 | sz_16 = utf8_utf16_strlen(str); |
| 59 | str_16 = calloc(sz_16 + 1, sizeof(u16)); |
| 60 | if (!str_16) |
| 61 | return NULL; |
| 62 | |
| 63 | tmp = str_16; |
| 64 | utf8_utf16_strcpy(&tmp, str); |
| 65 | |
| 66 | return str_16; |
| 67 | } |