charset.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * charset conversion utils
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #ifndef __CHARSET_H_
  8. #define __CHARSET_H_
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #define MAX_UTF8_PER_UTF16 3
  12. /**
  13. * console_read_unicode() - read Unicode code point from console
  14. *
  15. * @code: pointer to store Unicode code point
  16. * Return: 0 = success
  17. */
  18. int console_read_unicode(s32 *code);
  19. /**
  20. * utf8_get() - get next UTF-8 code point from buffer
  21. *
  22. * @src: pointer to current byte, updated to point to next byte
  23. * Return: code point, or 0 for end of string, or -1 if no legal
  24. * code point is found. In case of an error src points to
  25. * the incorrect byte.
  26. */
  27. s32 utf8_get(const char **src);
  28. /**
  29. * utf8_put() - write UTF-8 code point to buffer
  30. *
  31. * @code: code point
  32. * @dst: pointer to destination buffer, updated to next position
  33. * Return: -1 if the input parameters are invalid
  34. */
  35. int utf8_put(s32 code, char **dst);
  36. /**
  37. * utf8_utf16_strnlen() - length of a truncated utf-8 string after conversion
  38. * to utf-16
  39. *
  40. * @src: utf-8 string
  41. * @count: maximum number of code points to convert
  42. * Return: length in bytes after conversion to utf-16 without the
  43. * trailing \0. If an invalid UTF-8 sequence is hit one
  44. * word will be reserved for a replacement character.
  45. */
  46. size_t utf8_utf16_strnlen(const char *src, size_t count);
  47. /**
  48. * utf8_utf16_strlen() - length of a utf-8 string after conversion to utf-16
  49. *
  50. * @src: utf-8 string
  51. * Return: length in bytes after conversion to utf-16 without the
  52. * trailing \0. -1 if the utf-8 string is not valid.
  53. */
  54. #define utf8_utf16_strlen(a) utf8_utf16_strnlen((a), SIZE_MAX)
  55. /**
  56. * utf8_utf16_strncpy() - copy utf-8 string to utf-16 string
  57. *
  58. * @dst: destination buffer
  59. * @src: source buffer
  60. * @count: maximum number of code points to copy
  61. * Return: -1 if the input parameters are invalid
  62. */
  63. int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count);
  64. /**
  65. * utf8_utf16_strcpy() - copy utf-8 string to utf-16 string
  66. *
  67. * @dst: destination buffer
  68. * @src: source buffer
  69. * Return: -1 if the input parameters are invalid
  70. */
  71. #define utf8_utf16_strcpy(d, s) utf8_utf16_strncpy((d), (s), SIZE_MAX)
  72. /**
  73. * utf16_get() - get next UTF-16 code point from buffer
  74. *
  75. * @src: pointer to current word, updated to point to next word
  76. * Return: code point, or 0 for end of string, or -1 if no legal
  77. * code point is found. In case of an error src points to
  78. * the incorrect word.
  79. */
  80. s32 utf16_get(const u16 **src);
  81. /**
  82. * utf16_put() - write UTF-16 code point to buffer
  83. *
  84. * @code: code point
  85. * @dst: pointer to destination buffer, updated to next position
  86. * Return: -1 if the input parameters are invalid
  87. */
  88. int utf16_put(s32 code, u16 **dst);
  89. /**
  90. * utf16_strnlen() - length of a truncated utf-16 string
  91. *
  92. * @src: utf-16 string
  93. * @count: maximum number of code points to convert
  94. * Return: length in code points. If an invalid UTF-16 sequence is
  95. * hit one position will be reserved for a replacement
  96. * character.
  97. */
  98. size_t utf16_strnlen(const u16 *src, size_t count);
  99. /**
  100. * utf16_utf8_strnlen() - length of a truncated utf-16 string after conversion
  101. * to utf-8
  102. *
  103. * @src: utf-16 string
  104. * @count: maximum number of code points to convert
  105. * Return: length in bytes after conversion to utf-8 without the
  106. * trailing \0. If an invalid UTF-16 sequence is hit one
  107. * byte will be reserved for a replacement character.
  108. */
  109. size_t utf16_utf8_strnlen(const u16 *src, size_t count);
  110. /**
  111. * utf16_utf8_strlen() - length of a utf-16 string after conversion to utf-8
  112. *
  113. * @src: utf-16 string
  114. * Return: length in bytes after conversion to utf-8 without the
  115. * trailing \0. -1 if the utf-16 string is not valid.
  116. */
  117. #define utf16_utf8_strlen(a) utf16_utf8_strnlen((a), SIZE_MAX)
  118. /**
  119. * utf16_utf8_strncpy() - copy utf-16 string to utf-8 string
  120. *
  121. * @dst: destination buffer
  122. * @src: source buffer
  123. * @count: maximum number of code points to copy
  124. * Return: -1 if the input parameters are invalid
  125. */
  126. int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
  127. /**
  128. * utf16_utf8_strcpy() - copy utf-16 string to utf-8 string
  129. *
  130. * @dst: destination buffer
  131. * @src: source buffer
  132. * Return: -1 if the input parameters are invalid
  133. */
  134. #define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
  135. /**
  136. * utf_to_lower() - convert a Unicode letter to lower case
  137. *
  138. * @code: letter to convert
  139. * Return: lower case letter or unchanged letter
  140. */
  141. s32 utf_to_lower(const s32 code);
  142. /**
  143. * utf_to_upper() - convert a Unicode letter to upper case
  144. *
  145. * @code: letter to convert
  146. * Return: upper case letter or unchanged letter
  147. */
  148. s32 utf_to_upper(const s32 code);
  149. /**
  150. * u16_strlen - count non-zero words
  151. *
  152. * This function matches wsclen() if the -fshort-wchar compiler flag is set.
  153. * In the EFI context we explicitly need a function handling u16 strings.
  154. *
  155. * @in: null terminated u16 string
  156. * ReturnValue: number of non-zero words.
  157. * This is not the number of utf-16 letters!
  158. */
  159. size_t u16_strlen(const u16 *in);
  160. /**
  161. * u16_strlen - count non-zero words
  162. *
  163. * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
  164. * In the EFI context we explicitly need a function handling u16 strings.
  165. *
  166. * @in: null terminated u16 string
  167. * @count: maximum number of words to count
  168. * ReturnValue: number of non-zero words.
  169. * This is not the number of utf-16 letters!
  170. */
  171. size_t u16_strnlen(const u16 *in, size_t count);
  172. /**
  173. * utf16_to_utf8() - Convert an utf16 string to utf8
  174. *
  175. * Converts 'size' characters of the utf16 string 'src' to utf8
  176. * written to the 'dest' buffer.
  177. *
  178. * NOTE that a single utf16 character can generate up to 3 utf8
  179. * characters. See MAX_UTF8_PER_UTF16.
  180. *
  181. * @dest the destination buffer to write the utf8 characters
  182. * @src the source utf16 string
  183. * @size the number of utf16 characters to convert
  184. * @return the pointer to the first unwritten byte in 'dest'
  185. */
  186. uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
  187. #endif /* __CHARSET_H_ */