string.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * WMI string utility functions.
  4. *
  5. * Copyright (C) 2025 Armin Wolf <W_Armin@gmx.de>
  6. */
  7. #include <linux/build_bug.h>
  8. #include <linux/compiler_types.h>
  9. #include <linux/err.h>
  10. #include <linux/export.h>
  11. #include <linux/nls.h>
  12. #include <linux/limits.h>
  13. #include <linux/types.h>
  14. #include <linux/wmi.h>
  15. #include <asm/byteorder.h>
  16. static_assert(sizeof(__le16) == sizeof(wchar_t));
  17. /**
  18. * wmi_string_to_utf8s - Convert a WMI string into a UTF8 string.
  19. * @str: WMI string representation
  20. * @dst: Buffer to fill with UTF8 characters
  21. * @length: Length of the destination buffer
  22. *
  23. * Convert as WMI string into a standard UTF8 string. The conversion will stop
  24. * once a NUL character is detected or when the buffer is full. Any invalid UTF16
  25. * characters will be ignored. The resulting UTF8 string will always be NUL-terminated
  26. * when this function returns successfully.
  27. *
  28. * Return: Length of the resulting UTF8 string or negative errno code on failure.
  29. */
  30. ssize_t wmi_string_to_utf8s(const struct wmi_string *str, u8 *dst, size_t length)
  31. {
  32. /* Contains the maximum number of UTF16 code points to read */
  33. int inlen = le16_to_cpu(str->length) / 2;
  34. int ret;
  35. if (length < 1)
  36. return -EINVAL;
  37. /* We must leave room for the NUL character at the end of the destination buffer */
  38. ret = utf16s_to_utf8s((__force const wchar_t *)str->chars, inlen, UTF16_LITTLE_ENDIAN, dst,
  39. length - 1);
  40. if (ret < 0)
  41. return ret;
  42. dst[ret] = '\0';
  43. return ret;
  44. }
  45. EXPORT_SYMBOL_GPL(wmi_string_to_utf8s);
  46. /**
  47. * wmi_string_from_utf8s - Convert a UTF8 string into a WMI string.
  48. * @str: WMI string representation
  49. * @max_chars: Maximum number of UTF16 code points to store inside the WMI string
  50. * @src: UTF8 string to convert
  51. * @src_length: Length of the source string without any trailing NUL-characters
  52. *
  53. * Convert a UTF8 string into a WMI string. The conversion will stop when the WMI string is
  54. * full. The resulting WMI string will always be NUL-terminated and have its length field set
  55. * to and appropriate value when this function returns successfully.
  56. *
  57. * Return: Number of UTF16 code points inside the WMI string or negative errno code on failure.
  58. */
  59. ssize_t wmi_string_from_utf8s(struct wmi_string *str, size_t max_chars, const u8 *src,
  60. size_t src_length)
  61. {
  62. size_t str_length;
  63. int ret;
  64. if (max_chars < 1)
  65. return -EINVAL;
  66. /* We must leave room for the NUL character at the end of the WMI string */
  67. ret = utf8s_to_utf16s(src, src_length, UTF16_LITTLE_ENDIAN, (__force wchar_t *)str->chars,
  68. max_chars - 1);
  69. if (ret < 0)
  70. return ret;
  71. str_length = (ret + 1) * sizeof(u16);
  72. if (str_length > U16_MAX)
  73. return -EOVERFLOW;
  74. str->length = cpu_to_le16(str_length);
  75. str->chars[ret] = '\0';
  76. return ret;
  77. }
  78. EXPORT_SYMBOL_GPL(wmi_string_from_utf8s);