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 | |
| 8 | #include <common.h> |
| 9 | #include <charset.h> |
| 10 | |
| 11 | /** |
| 12 | * efi_create_indexed_name - create a string name with an index |
| 13 | * @buffer: Buffer |
| 14 | * @name: Name string |
| 15 | * @index: Index |
| 16 | * |
| 17 | * Create a utf-16 string with @name, appending @index. |
| 18 | * For example, L"Capsule0001" |
| 19 | * |
| 20 | * The caller must ensure that the buffer has enough space for the resulting |
| 21 | * string including the trailing L'\0'. |
| 22 | * |
| 23 | * Return: A pointer to the next position after the created string |
| 24 | * in @buffer, or NULL otherwise |
| 25 | */ |
Ilias Apalodimas | 2157529 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 26 | u16 *efi_create_indexed_name(u16 *buffer, size_t buffer_size, const char *name, |
| 27 | unsigned int index) |
AKASHI Takahiro | 9407468 | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 28 | { |
| 29 | u16 *p = buffer; |
| 30 | char index_buf[5]; |
Ilias Apalodimas | 2157529 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 31 | size_t size; |
AKASHI Takahiro | 9407468 | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 32 | |
Ilias Apalodimas | 2157529 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 33 | size = (utf8_utf16_strlen(name) * sizeof(u16) + |
| 34 | sizeof(index_buf) * sizeof(u16)); |
| 35 | if (buffer_size < size) |
| 36 | return NULL; |
AKASHI Takahiro | 9407468 | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 37 | utf8_utf16_strcpy(&p, name); |
Ilias Apalodimas | 2157529 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 38 | snprintf(index_buf, sizeof(index_buf), "%04X", index); |
AKASHI Takahiro | 9407468 | 2020-10-29 13:47:46 +0900 | [diff] [blame] | 39 | utf8_utf16_strcpy(&p, index_buf); |
| 40 | |
| 41 | return p; |
| 42 | } |