nls.h 3.0 KB

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