0001
0002
0003
0004
0005
0006 #include <linux/module.h>
0007 #include <linux/kernel.h>
0008 #include <linux/string.h>
0009 #include <linux/nls.h>
0010 #include <linux/errno.h>
0011
0012 static unsigned char identity[256];
0013
0014 static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
0015 {
0016 int n;
0017
0018 if (boundlen <= 0)
0019 return -ENAMETOOLONG;
0020
0021 n = utf32_to_utf8(uni, out, boundlen);
0022 if (n < 0) {
0023 *out = '?';
0024 return -EINVAL;
0025 }
0026 return n;
0027 }
0028
0029 static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
0030 {
0031 int n;
0032 unicode_t u;
0033
0034 n = utf8_to_utf32(rawstring, boundlen, &u);
0035 if (n < 0 || u > MAX_WCHAR_T) {
0036 *uni = 0x003f;
0037 return -EINVAL;
0038 }
0039 *uni = (wchar_t) u;
0040 return n;
0041 }
0042
0043 static struct nls_table table = {
0044 .charset = "utf8",
0045 .uni2char = uni2char,
0046 .char2uni = char2uni,
0047 .charset2lower = identity,
0048 .charset2upper = identity,
0049 };
0050
0051 static int __init init_nls_utf8(void)
0052 {
0053 int i;
0054 for (i=0; i<256; i++)
0055 identity[i] = i;
0056
0057 return register_nls(&table);
0058 }
0059
0060 static void __exit exit_nls_utf8(void)
0061 {
0062 unregister_nls(&table);
0063 }
0064
0065 module_init(init_nls_utf8)
0066 module_exit(exit_nls_utf8)
0067 MODULE_LICENSE("Dual BSD/GPL");