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