efi_string.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * String functions
  4. *
  5. * Copyright (c) 2020 AKASHI Takahiro, Linaro Limited
  6. */
  7. #include <common.h>
  8. #include <charset.h>
  9. #include <efi_loader.h>
  10. /**
  11. * efi_create_indexed_name - create a string name with an index
  12. * @buffer: Buffer
  13. * @name: Name string
  14. * @index: Index
  15. *
  16. * Create a utf-16 string with @name, appending @index.
  17. * For example, L"Capsule0001"
  18. *
  19. * The caller must ensure that the buffer has enough space for the resulting
  20. * string including the trailing L'\0'.
  21. *
  22. * Return: A pointer to the next position after the created string
  23. * in @buffer, or NULL otherwise
  24. */
  25. u16 *efi_create_indexed_name(u16 *buffer, size_t buffer_size, const char *name,
  26. unsigned int index)
  27. {
  28. u16 *p = buffer;
  29. char index_buf[5];
  30. size_t size;
  31. size = (utf8_utf16_strlen(name) * sizeof(u16) +
  32. sizeof(index_buf) * sizeof(u16));
  33. if (buffer_size < size)
  34. return NULL;
  35. utf8_utf16_strcpy(&p, name);
  36. snprintf(index_buf, sizeof(index_buf), "%04X", index);
  37. utf8_utf16_strcpy(&p, index_buf);
  38. return p;
  39. }