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