charset.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_strcasecmp() - compare two u16 strings case insensitively
  157. *
  158. * @s1: first string to compare
  159. * @s2: second string to compare
  160. * Return: 0 if the first n u16 are the same in s1 and s2
  161. * < 0 if the first different u16 in s1 is less than the
  162. * corresponding u16 in s2
  163. * > 0 if the first different u16 in s1 is greater than the
  164. */
  165. int u16_strcasecmp(const u16 *s1, const u16 *s2);
  166. /**
  167. * u16_strncmp() - compare two u16 string
  168. *
  169. * @s1: first string to compare
  170. * @s2: second string to compare
  171. * @n: maximum number of u16 to compare
  172. * Return: 0 if the first n u16 are the same in s1 and s2
  173. * < 0 if the first different u16 in s1 is less than the
  174. * corresponding u16 in s2
  175. * > 0 if the first different u16 in s1 is greater than the
  176. * corresponding u16 in s2
  177. */
  178. int u16_strncmp(const u16 *s1, const u16 *s2, size_t n);
  179. /**
  180. * u16_strcmp() - compare two u16 string
  181. *
  182. * @s1: first string to compare
  183. * @s2: second string to compare
  184. * Return: 0 if the first n u16 are the same in s1 and s2
  185. * < 0 if the first different u16 in s1 is less than the
  186. * corresponding u16 in s2
  187. * > 0 if the first different u16 in s1 is greater than the
  188. * corresponding u16 in s2
  189. */
  190. #define u16_strcmp(s1, s2) u16_strncmp((s1), (s2), SIZE_MAX)
  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_strlen - count non-zero words
  215. *
  216. * This function matches wsclen() if the -fshort-wchar compiler flag is set.
  217. * In the EFI context we explicitly need a function handling u16 strings.
  218. *
  219. * @in: null terminated u16 string
  220. * Return: number of non-zero words.
  221. * This is not the number of utf-16 letters!
  222. */
  223. size_t u16_strlen(const void *in);
  224. #define u16_strlen(in) u16_strnlen(in, SIZE_MAX)
  225. /**
  226. * u16_strcpy() - copy u16 string
  227. *
  228. * Copy u16 string pointed to by src, including terminating null word, to
  229. * the buffer pointed to by dest.
  230. *
  231. * @dest: destination buffer
  232. * @src: source buffer (null terminated)
  233. * Return: 'dest' address
  234. */
  235. u16 *u16_strcpy(u16 *dest, const u16 *src);
  236. /**
  237. * u16_strdup() - duplicate u16 string
  238. *
  239. * Copy u16 string pointed to by src, including terminating null word, to a
  240. * newly allocated buffer.
  241. *
  242. * @src: source buffer (null terminated)
  243. * Return: allocated new buffer on success, NULL on failure
  244. */
  245. u16 *u16_strdup(const void *src);
  246. /**
  247. * u16_strlcat() - Append a length-limited, %NUL-terminated string to another
  248. *
  249. * Append the source string @src to the destination string @dest, overwriting
  250. * null word at the end of @dest adding a terminating null word.
  251. *
  252. * @dest: zero terminated u16 destination string
  253. * @src: zero terminated u16 source string
  254. * @count: size of buffer in u16 words including taling 0x0000
  255. * Return: required size including trailing 0x0000 in u16 words
  256. * If return value >= count, truncation occurred.
  257. */
  258. size_t u16_strlcat(u16 *dest, const u16 *src, size_t count);
  259. /**
  260. * utf16_to_utf8() - Convert an utf16 string to utf8
  261. *
  262. * Converts 'size' characters of the utf16 string 'src' to utf8
  263. * written to the 'dest' buffer.
  264. *
  265. * NOTE that a single utf16 character can generate up to 3 utf8
  266. * characters. See MAX_UTF8_PER_UTF16.
  267. *
  268. * @dest: the destination buffer to write the utf8 characters
  269. * @src: the source utf16 string
  270. * @size: the number of utf16 characters to convert
  271. * Return: the pointer to the first unwritten byte in 'dest'
  272. */
  273. uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
  274. /**
  275. * utf_to_cp() - translate Unicode code point to 8bit codepage
  276. *
  277. * Codepoints that do not exist in the codepage are rendered as question mark.
  278. *
  279. * @c: pointer to Unicode code point to be translated
  280. * @codepage: Unicode to codepage translation table
  281. * Return: 0 on success, -ENOENT if codepoint cannot be translated
  282. */
  283. int utf_to_cp(s32 *c, const u16 *codepage);
  284. /**
  285. * utf8_to_cp437_stream() - convert UTF-8 stream to codepage 437
  286. *
  287. * @c: next UTF-8 character to convert
  288. * @buffer: buffer, at least 5 characters
  289. * Return: next codepage 437 character or 0
  290. */
  291. int utf8_to_cp437_stream(u8 c, char *buffer);
  292. /**
  293. * utf8_to_utf32_stream() - convert UTF-8 stream to UTF-32
  294. *
  295. * @c: next UTF-8 character to convert
  296. * @buffer: buffer, at least 5 characters
  297. * Return: next codepage 437 character or 0
  298. */
  299. int utf8_to_utf32_stream(u8 c, char *buffer);
  300. #endif /* __CHARSET_H_ */