charset.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. * codepage_437 - Unicode to codepage 437 translation table
  14. */
  15. extern const u16 codepage_437[128];
  16. /**
  17. * console_read_unicode() - read Unicode code point from console
  18. *
  19. * @code: pointer to store Unicode code point
  20. * Return: 0 = success
  21. */
  22. int console_read_unicode(s32 *code);
  23. /**
  24. * utf8_get() - get next UTF-8 code point from buffer
  25. *
  26. * @src: pointer to current byte, updated to point to next byte
  27. * Return: code point, or 0 for end of string, or -1 if no legal
  28. * code point is found. In case of an error src points to
  29. * the incorrect byte.
  30. */
  31. s32 utf8_get(const char **src);
  32. /**
  33. * utf8_put() - write UTF-8 code point to buffer
  34. *
  35. * @code: code point
  36. * @dst: pointer to destination buffer, updated to next position
  37. * Return: -1 if the input parameters are invalid
  38. */
  39. int utf8_put(s32 code, char **dst);
  40. /**
  41. * utf8_utf16_strnlen() - length of a truncated utf-8 string after conversion
  42. * to utf-16
  43. *
  44. * @src: utf-8 string
  45. * @count: maximum number of code points to convert
  46. * Return: length in u16 after conversion to utf-16 without the
  47. * trailing \0. If an invalid UTF-8 sequence is hit one
  48. * u16 will be reserved for a replacement character.
  49. */
  50. size_t utf8_utf16_strnlen(const char *src, size_t count);
  51. /**
  52. * utf8_utf16_strlen() - length of a utf-8 string after conversion to utf-16
  53. *
  54. * @a: utf-8 string
  55. * Return: length in u16 after conversion to utf-16 without the
  56. * trailing \0. If an invalid UTF-8 sequence is hit one
  57. * u16 will be reserved for a replacement character.
  58. */
  59. #define utf8_utf16_strlen(a) utf8_utf16_strnlen((a), SIZE_MAX)
  60. /**
  61. * utf8_utf16_strncpy() - copy utf-8 string to utf-16 string
  62. *
  63. * @dst: destination buffer
  64. * @src: source buffer
  65. * @count: maximum number of code points to copy
  66. * Return: -1 if the input parameters are invalid
  67. */
  68. int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count);
  69. /**
  70. * utf8_utf16_strcpy() - copy utf-8 string to utf-16 string
  71. *
  72. * @d: destination buffer
  73. * @s: source buffer
  74. * Return: -1 if the input parameters are invalid
  75. */
  76. #define utf8_utf16_strcpy(d, s) utf8_utf16_strncpy((d), (s), SIZE_MAX)
  77. /**
  78. * utf16_get() - get next UTF-16 code point from buffer
  79. *
  80. * @src: pointer to current word, updated to point to next word
  81. * Return: code point, or 0 for end of string, or -1 if no legal
  82. * code point is found. In case of an error src points to
  83. * the incorrect word.
  84. */
  85. s32 utf16_get(const u16 **src);
  86. /**
  87. * utf16_put() - write UTF-16 code point to buffer
  88. *
  89. * @code: code point
  90. * @dst: pointer to destination buffer, updated to next position
  91. * Return: -1 if the input parameters are invalid
  92. */
  93. int utf16_put(s32 code, u16 **dst);
  94. /**
  95. * utf16_strnlen() - length of a truncated utf-16 string
  96. *
  97. * @src: utf-16 string
  98. * @count: maximum number of code points to convert
  99. * Return: length in code points. If an invalid UTF-16 sequence is
  100. * hit one position will be reserved for a replacement
  101. * character.
  102. */
  103. size_t utf16_strnlen(const u16 *src, size_t count);
  104. /**
  105. * utf16_utf8_strnlen() - length of a truncated utf-16 string after conversion
  106. * to utf-8
  107. *
  108. * @src: utf-16 string
  109. * @count: maximum number of code points to convert
  110. * Return: length in bytes after conversion to utf-8 without the
  111. * trailing \0. If an invalid UTF-16 sequence is hit one
  112. * byte will be reserved for a replacement character.
  113. */
  114. size_t utf16_utf8_strnlen(const u16 *src, size_t count);
  115. /**
  116. * utf16_utf8_strlen() - length of a utf-16 string after conversion to utf-8
  117. *
  118. * @a: utf-16 string
  119. * Return: length in bytes after conversion to utf-8 without the
  120. * trailing \0. If an invalid UTF-16 sequence is hit one
  121. * byte will be reserved for a replacement character.
  122. */
  123. #define utf16_utf8_strlen(a) utf16_utf8_strnlen((a), SIZE_MAX)
  124. /**
  125. * utf16_utf8_strncpy() - copy utf-16 string to utf-8 string
  126. *
  127. * @dst: destination buffer
  128. * @src: source buffer
  129. * @count: maximum number of code points to copy
  130. * Return: -1 if the input parameters are invalid
  131. */
  132. int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
  133. /**
  134. * utf16_utf8_strcpy() - copy utf-16 string to utf-8 string
  135. *
  136. * @d: destination buffer
  137. * @s: source buffer
  138. * Return: -1 if the input parameters are invalid
  139. */
  140. #define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
  141. /**
  142. * utf_to_lower() - convert a Unicode letter to lower case
  143. *
  144. * @code: letter to convert
  145. * Return: lower case letter or unchanged letter
  146. */
  147. s32 utf_to_lower(const s32 code);
  148. /**
  149. * utf_to_upper() - convert a Unicode letter to upper case
  150. *
  151. * @code: letter to convert
  152. * Return: upper case letter or unchanged letter
  153. */
  154. s32 utf_to_upper(const s32 code);
  155. /**
  156. * u16_strncmp() - compare two u16 string
  157. *
  158. * @s1: first string to compare
  159. * @s2: second string to compare
  160. * @n: maximum number of u16 to compare
  161. * Return: 0 if the first n u16 are the same in s1 and s2
  162. * < 0 if the first different u16 in s1 is less than the
  163. * corresponding u16 in s2
  164. * > 0 if the first different u16 in s1 is greater than the
  165. * corresponding u16 in s2
  166. */
  167. int u16_strncmp(const u16 *s1, const u16 *s2, size_t n);
  168. /**
  169. * u16_strcmp() - compare two u16 string
  170. *
  171. * @s1: first string to compare
  172. * @s2: second string to compare
  173. * Return: 0 if the first n u16 are the same in s1 and s2
  174. * < 0 if the first different u16 in s1 is less than the
  175. * corresponding u16 in s2
  176. * > 0 if the first different u16 in s1 is greater than the
  177. * corresponding u16 in s2
  178. */
  179. #define u16_strcmp(s1, s2) u16_strncmp((s1), (s2), SIZE_MAX)
  180. /**
  181. * u16_strlen - count non-zero words
  182. *
  183. * This function matches wsclen() if the -fshort-wchar compiler flag is set.
  184. * In the EFI context we explicitly need a function handling u16 strings.
  185. *
  186. * @in: null terminated u16 string
  187. * Return: number of non-zero words.
  188. * This is not the number of utf-16 letters!
  189. */
  190. size_t u16_strlen(const void *in);
  191. /**
  192. * u16_strsize() - count size of u16 string in bytes including the null
  193. * character
  194. *
  195. * Counts the number of bytes occupied by a u16 string
  196. *
  197. * @in: null terminated u16 string
  198. * Return: bytes in a u16 string
  199. */
  200. size_t u16_strsize(const void *in);
  201. /**
  202. * u16_strnlen() - count non-zero words
  203. *
  204. * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
  205. * In the EFI context we explicitly need a function handling u16 strings.
  206. *
  207. * @in: null terminated u16 string
  208. * @count: maximum number of words to count
  209. * Return: number of non-zero words.
  210. * This is not the number of utf-16 letters!
  211. */
  212. size_t u16_strnlen(const u16 *in, size_t count);
  213. /**
  214. * u16_strcpy() - copy u16 string
  215. *
  216. * Copy u16 string pointed to by src, including terminating null word, to
  217. * the buffer pointed to by dest.
  218. *
  219. * @dest: destination buffer
  220. * @src: source buffer (null terminated)
  221. * Return: 'dest' address
  222. */
  223. u16 *u16_strcpy(u16 *dest, const u16 *src);
  224. /**
  225. * u16_strdup() - duplicate u16 string
  226. *
  227. * Copy u16 string pointed to by src, including terminating null word, to a
  228. * newly allocated buffer.
  229. *
  230. * @src: source buffer (null terminated)
  231. * Return: allocated new buffer on success, NULL on failure
  232. */
  233. u16 *u16_strdup(const void *src);
  234. /**
  235. * utf16_to_utf8() - Convert an utf16 string to utf8
  236. *
  237. * Converts 'size' characters of the utf16 string 'src' to utf8
  238. * written to the 'dest' buffer.
  239. *
  240. * NOTE that a single utf16 character can generate up to 3 utf8
  241. * characters. See MAX_UTF8_PER_UTF16.
  242. *
  243. * @dest: the destination buffer to write the utf8 characters
  244. * @src: the source utf16 string
  245. * @size: the number of utf16 characters to convert
  246. * Return: the pointer to the first unwritten byte in 'dest'
  247. */
  248. uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
  249. /**
  250. * utf_to_cp() - translate Unicode code point to 8bit codepage
  251. *
  252. * Codepoints that do not exist in the codepage are rendered as question mark.
  253. *
  254. * @c: pointer to Unicode code point to be translated
  255. * @codepage: Unicode to codepage translation table
  256. * Return: 0 on success, -ENOENT if codepoint cannot be translated
  257. */
  258. int utf_to_cp(s32 *c, const u16 *codepage);
  259. /**
  260. * utf8_to_cp437_stream() - convert UTF-8 stream to codepage 437
  261. *
  262. * @c: next UTF-8 character to convert
  263. * @buffer: buffer, at least 5 characters
  264. * Return: next codepage 437 character or 0
  265. */
  266. int utf8_to_cp437_stream(u8 c, char *buffer);
  267. /**
  268. * utf8_to_utf32_stream() - convert UTF-8 stream to UTF-32
  269. *
  270. * @c: next UTF-8 character to convert
  271. * @buffer: buffer, at least 5 characters
  272. * Return: next codepage 437 character or 0
  273. */
  274. int utf8_to_utf32_stream(u8 c, char *buffer);
  275. #endif /* __CHARSET_H_ */