nls.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_NLS_H
  3. #define _LINUX_NLS_H
  4. #include <linux/init.h>
  5. /* Unicode has changed over the years. Unicode code points no longer
  6. * fit into 16 bits; as of Unicode 5 valid code points range from 0
  7. * to 0x10ffff (17 planes, where each plane holds 65536 code points).
  8. *
  9. * The original decision to represent Unicode characters as 16-bit
  10. * wchar_t values is now outdated. But plane 0 still includes the
  11. * most commonly used characters, so we will retain it. The newer
  12. * 32-bit unicode_t type can be used when it is necessary to
  13. * represent the full Unicode character set.
  14. */
  15. /* Plane-0 Unicode character */
  16. typedef u16 wchar_t;
  17. #define MAX_WCHAR_T 0xffff
  18. /* Arbitrary Unicode character */
  19. typedef u32 unicode_t;
  20. struct nls_table {
  21. const char *charset;
  22. const char *alias;
  23. int (*uni2char) (wchar_t uni, unsigned char *out, int boundlen);
  24. int (*char2uni) (const unsigned char *rawstring, int boundlen,
  25. wchar_t *uni);
  26. const unsigned char *charset2lower;
  27. const unsigned char *charset2upper;
  28. struct module *owner;
  29. struct nls_table *next;
  30. };
  31. /* this value hold the maximum octet of charset */
  32. #define NLS_MAX_CHARSET_SIZE 6 /* for UTF-8 */
  33. /* Byte order for UTF-16 strings */
  34. enum utf16_endian {
  35. UTF16_HOST_ENDIAN,
  36. UTF16_LITTLE_ENDIAN,
  37. UTF16_BIG_ENDIAN
  38. };
  39. /* nls_base.c */
  40. extern int __register_nls(struct nls_table *, struct module *);
  41. extern int unregister_nls(struct nls_table *);
  42. extern struct nls_table *load_nls(char *);
  43. extern void unload_nls(struct nls_table *);
  44. extern struct nls_table *load_nls_default(void);
  45. #define register_nls(nls) __register_nls((nls), THIS_MODULE)
  46. extern int utf8_to_utf32(const u8 *s, int len, unicode_t *pu);
  47. extern int utf32_to_utf8(unicode_t u, u8 *s, int maxlen);
  48. extern int utf8s_to_utf16s(const u8 *s, int len,
  49. enum utf16_endian endian, wchar_t *pwcs, int maxlen);
  50. extern int utf16s_to_utf8s(const wchar_t *pwcs, int len,
  51. enum utf16_endian endian, u8 *s, int maxlen);
  52. static inline unsigned char nls_tolower(struct nls_table *t, unsigned char c)
  53. {
  54. unsigned char nc = t->charset2lower[c];
  55. return nc ? nc : c;
  56. }
  57. static inline unsigned char nls_toupper(struct nls_table *t, unsigned char c)
  58. {
  59. unsigned char nc = t->charset2upper[c];
  60. return nc ? nc : c;
  61. }
  62. static inline int nls_strnicmp(struct nls_table *t, const unsigned char *s1,
  63. const unsigned char *s2, int len)
  64. {
  65. while (len--) {
  66. if (nls_tolower(t, *s1++) != nls_tolower(t, *s2++))
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. /*
  72. * nls_nullsize - return length of null character for codepage
  73. * @codepage - codepage for which to return length of NULL terminator
  74. *
  75. * Since we can't guarantee that the null terminator will be a particular
  76. * length, we have to check against the codepage. If there's a problem
  77. * determining it, assume a single-byte NULL terminator.
  78. */
  79. static inline int
  80. nls_nullsize(const struct nls_table *codepage)
  81. {
  82. int charlen;
  83. char tmp[NLS_MAX_CHARSET_SIZE];
  84. charlen = codepage->uni2char(0, tmp, NLS_MAX_CHARSET_SIZE);
  85. return charlen > 0 ? charlen : 1;
  86. }
  87. #define MODULE_ALIAS_NLS(name) MODULE_ALIAS("nls_" __stringify(name))
  88. #endif /* _LINUX_NLS_H */