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  */
efi_create_indexed_name(u16 * buffer,size_t buffer_size,const char * name,unsigned int index)26 u16 *efi_create_indexed_name(u16 *buffer, size_t buffer_size, const char *name,
27 			     unsigned int index)
28 {
29 	u16 *p = buffer;
30 	char index_buf[5];
31 	size_t size;
32 
33 	size = (utf8_utf16_strlen(name) * sizeof(u16) +
34 		sizeof(index_buf) * sizeof(u16));
35 	if (buffer_size < size)
36 		return NULL;
37 	utf8_utf16_strcpy(&p, name);
38 	snprintf(index_buf, sizeof(index_buf), "%04X", index);
39 	utf8_utf16_strcpy(&p, index_buf);
40 
41 	return p;
42 }
43