charset.h 2.2 KB

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