blob: 96272422886657911e384708eef06046fc64a87a [file] [log] [blame]
AKASHI Takahiro94074682020-10-29 13:47:46 +09001// 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 Apalodimas21575292020-12-31 12:26:46 +020026u16 *efi_create_indexed_name(u16 *buffer, size_t buffer_size, const char *name,
27 unsigned int index)
AKASHI Takahiro94074682020-10-29 13:47:46 +090028{
29 u16 *p = buffer;
30 char index_buf[5];
Ilias Apalodimas21575292020-12-31 12:26:46 +020031 size_t size;
AKASHI Takahiro94074682020-10-29 13:47:46 +090032
Ilias Apalodimas21575292020-12-31 12:26:46 +020033 size = (utf8_utf16_strlen(name) * sizeof(u16) +
34 sizeof(index_buf) * sizeof(u16));
35 if (buffer_size < size)
36 return NULL;
AKASHI Takahiro94074682020-10-29 13:47:46 +090037 utf8_utf16_strcpy(&p, name);
Ilias Apalodimas21575292020-12-31 12:26:46 +020038 snprintf(index_buf, sizeof(index_buf), "%04X", index);
AKASHI Takahiro94074682020-10-29 13:47:46 +090039 utf8_utf16_strcpy(&p, index_buf);
40
41 return p;
42}