charset.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 u16 after conversion to utf-16 without the
  43. * trailing \0. If an invalid UTF-8 sequence is hit one
  44. * u16 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. * @a: utf-8 string
  51. * Return: length in u16 after conversion to utf-16 without the
  52. * trailing \0. If an invalid UTF-8 sequence is hit one
  53. * u16 will be reserved for a replacement character.
  54. */
  55. #define utf8_utf16_strlen(a) utf8_utf16_strnlen((a), SIZE_MAX)
  56. /**
  57. * utf8_utf16_strncpy() - copy utf-8 string to utf-16 string
  58. *
  59. * @dst: destination buffer
  60. * @src: source buffer
  61. * @count: maximum number of code points to copy
  62. * Return: -1 if the input parameters are invalid
  63. */
  64. int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count);
  65. /**
  66. * utf8_utf16_strcpy() - copy utf-8 string to utf-16 string
  67. *
  68. * @d: destination buffer
  69. * @s: source buffer
  70. * Return: -1 if the input parameters are invalid
  71. */
  72. #define utf8_utf16_strcpy(d, s) utf8_utf16_strncpy((d), (s), SIZE_MAX)
  73. /**
  74. * utf16_get() - get next UTF-16 code point from buffer
  75. *
  76. * @src: pointer to current word, updated to point to next word
  77. * Return: code point, or 0 for end of string, or -1 if no legal
  78. * code point is found. In case of an error src points to
  79. * the incorrect word.
  80. */
  81. s32 utf16_get(const u16 **src);
  82. /**
  83. * utf16_put() - write UTF-16 code point to buffer
  84. *
  85. * @code: code point
  86. * @dst: pointer to destination buffer, updated to next position
  87. * Return: -1 if the input parameters are invalid
  88. */
  89. int utf16_put(s32 code, u16 **dst);
  90. /**
  91. * utf16_strnlen() - length of a truncated utf-16 string
  92. *
  93. * @src: utf-16 string
  94. * @count: maximum number of code points to convert
  95. * Return: length in code points. If an invalid UTF-16 sequence is
  96. * hit one position will be reserved for a replacement
  97. * character.
  98. */
  99. size_t utf16_strnlen(const u16 *src, size_t count);
  100. /**
  101. * utf16_utf8_strnlen() - length of a truncated utf-16 string after conversion
  102. * to utf-8
  103. *
  104. * @src: utf-16 string
  105. * @count: maximum number of code points to convert
  106. * Return: length in bytes after conversion to utf-8 without the
  107. * trailing \0. If an invalid UTF-16 sequence is hit one
  108. * byte will be reserved for a replacement character.
  109. */
  110. size_t utf16_utf8_strnlen(const u16 *src, size_t count);
  111. /**
  112. * utf16_utf8_strlen() - length of a utf-16 string after conversion to utf-8
  113. *
  114. * @a: utf-16 string
  115. * Return: length in bytes after conversion to utf-8 without the
  116. * trailing \0. If an invalid UTF-16 sequence is hit one
  117. * byte will be reserved for a replacement character.
  118. */
  119. #define utf16_utf8_strlen(a) utf16_utf8_strnlen((a), SIZE_MAX)
  120. /**
  121. * utf16_utf8_strncpy() - copy utf-16 string to utf-8 string
  122. *
  123. * @dst: destination buffer
  124. * @src: source buffer
  125. * @count: maximum number of code points to copy
  126. * Return: -1 if the input parameters are invalid
  127. */
  128. int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
  129. /**
  130. * utf16_utf8_strcpy() - copy utf-16 string to utf-8 string
  131. *
  132. * @d: destination buffer
  133. * @s: source buffer
  134. * Return: -1 if the input parameters are invalid
  135. */
  136. #define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
  137. /**
  138. * utf_to_lower() - convert a Unicode letter to lower case
  139. *
  140. * @code: letter to convert
  141. * Return: lower case letter or unchanged letter
  142. */
  143. s32 utf_to_lower(const s32 code);
  144. /**
  145. * utf_to_upper() - convert a Unicode letter to upper case
  146. *
  147. * @code: letter to convert
  148. * Return: upper case letter or unchanged letter
  149. */
  150. s32 utf_to_upper(const s32 code);
  151. /**
  152. * u16_strncmp() - compare two u16 string
  153. *
  154. * @s1: first string to compare
  155. * @s2: second string to compare
  156. * @n: maximum number of u16 to compare
  157. * Return: 0 if the first n u16 are the same in s1 and s2
  158. * < 0 if the first different u16 in s1 is less than the
  159. * corresponding u16 in s2
  160. * > 0 if the first different u16 in s1 is greater than the
  161. * corresponding u16 in s2
  162. */
  163. int u16_strncmp(const u16 *s1, const u16 *s2, size_t n);
  164. /**
  165. * u16_strcmp() - compare two u16 string
  166. *
  167. * @s1: first string to compare
  168. * @s2: second string to compare
  169. * Return: 0 if the first n u16 are the same in s1 and s2
  170. * < 0 if the first different u16 in s1 is less than the
  171. * corresponding u16 in s2
  172. * > 0 if the first different u16 in s1 is greater than the
  173. * corresponding u16 in s2
  174. */
  175. #define u16_strcmp(s1, s2) u16_strncmp((s1), (s2), SIZE_MAX)
  176. /**
  177. * u16_strlen - count non-zero words
  178. *
  179. * This function matches wsclen() if the -fshort-wchar compiler flag is set.
  180. * In the EFI context we explicitly need a function handling u16 strings.
  181. *
  182. * @in: null terminated u16 string
  183. * Return: number of non-zero words.
  184. * This is not the number of utf-16 letters!
  185. */
  186. size_t u16_strlen(const void *in);
  187. /**
  188. * u16_strsize() - count size of u16 string in bytes including the null
  189. * character
  190. *
  191. * Counts the number of bytes occupied by a u16 string
  192. *
  193. * @in: null terminated u16 string
  194. * Return: bytes in a u16 string
  195. */
  196. size_t u16_strsize(const void *in);
  197. /**
  198. * u16_strnlen() - count non-zero words
  199. *
  200. * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
  201. * In the EFI context we explicitly need a function handling u16 strings.
  202. *
  203. * @in: null terminated u16 string
  204. * @count: maximum number of words to count
  205. * Return: number of non-zero words.
  206. * This is not the number of utf-16 letters!
  207. */
  208. size_t u16_strnlen(const u16 *in, size_t count);
  209. /**
  210. * u16_strcpy() - copy u16 string
  211. *
  212. * Copy u16 string pointed to by src, including terminating null word, to
  213. * the buffer pointed to by dest.
  214. *
  215. * @dest: destination buffer
  216. * @src: source buffer (null terminated)
  217. * Return: 'dest' address
  218. */
  219. u16 *u16_strcpy(u16 *dest, const u16 *src);
  220. /**
  221. * u16_strdup() - duplicate u16 string
  222. *
  223. * Copy u16 string pointed to by src, including terminating null word, to a
  224. * newly allocated buffer.
  225. *
  226. * @src: source buffer (null terminated)
  227. * Return: allocated new buffer on success, NULL on failure
  228. */
  229. u16 *u16_strdup(const void *src);
  230. /**
  231. * utf16_to_utf8() - Convert an utf16 string to utf8
  232. *
  233. * Converts 'size' characters of the utf16 string 'src' to utf8
  234. * written to the 'dest' buffer.
  235. *
  236. * NOTE that a single utf16 character can generate up to 3 utf8
  237. * characters. See MAX_UTF8_PER_UTF16.
  238. *
  239. * @dest: the destination buffer to write the utf8 characters
  240. * @src: the source utf16 string
  241. * @size: the number of utf16 characters to convert
  242. * Return: the pointer to the first unwritten byte in 'dest'
  243. */
  244. uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
  245. #endif /* __CHARSET_H_ */