charset.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/types.h>
  10. #define MAX_UTF8_PER_UTF16 3
  11. /**
  12. * utf16_strlen() - Get the length of an utf16 string
  13. *
  14. * Returns the number of 16 bit characters in an utf16 string, not
  15. * including the terminating NULL character.
  16. *
  17. * @in the string to measure
  18. * @return the string length
  19. */
  20. size_t utf16_strlen(const uint16_t *in);
  21. /**
  22. * utf16_strnlen() - Get the length of a fixed-size utf16 string.
  23. *
  24. * Returns the number of 16 bit characters in an utf16 string,
  25. * not including the terminating NULL character, but at most
  26. * 'count' number of characters. In doing this, utf16_strnlen()
  27. * looks at only the first 'count' characters.
  28. *
  29. * @in the string to measure
  30. * @count the maximum number of characters to count
  31. * @return the string length, up to a maximum of 'count'
  32. */
  33. size_t utf16_strnlen(const uint16_t *in, size_t count);
  34. /**
  35. * utf16_strcpy() - UTF16 equivalent of strcpy()
  36. */
  37. uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src);
  38. /**
  39. * utf16_strdup() - UTF16 equivalent of strdup()
  40. */
  41. uint16_t *utf16_strdup(const uint16_t *s);
  42. /**
  43. * utf16_to_utf8() - Convert an utf16 string to utf8
  44. *
  45. * Converts 'size' characters of the utf16 string 'src' to utf8
  46. * written to the 'dest' buffer.
  47. *
  48. * NOTE that a single utf16 character can generate up to 3 utf8
  49. * characters. See MAX_UTF8_PER_UTF16.
  50. *
  51. * @dest the destination buffer to write the utf8 characters
  52. * @src the source utf16 string
  53. * @size the number of utf16 characters to convert
  54. * @return the pointer to the first unwritten byte in 'dest'
  55. */
  56. uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
  57. /**
  58. * utf8_to_utf16() - Convert an utf8 string to utf16
  59. *
  60. * Converts up to 'size' characters of the utf16 string 'src' to utf8
  61. * written to the 'dest' buffer. Stops at 0x00.
  62. *
  63. * @dest the destination buffer to write the utf8 characters
  64. * @src the source utf16 string
  65. * @size maximum number of utf16 characters to convert
  66. * @return the pointer to the first unwritten byte in 'dest'
  67. */
  68. uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size);
  69. #endif /* __CHARSET_H_ */