nls_utf8.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Module for handling utf8 just like any other charset.
  3. * By Urban Widmark 2000
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <linux/nls.h>
  9. #include <linux/errno.h>
  10. static unsigned char identity[256];
  11. static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
  12. {
  13. int n;
  14. if ( (n = utf8_wctomb(out, uni, boundlen)) == -1) {
  15. *out = '?';
  16. return -EINVAL;
  17. }
  18. return n;
  19. }
  20. static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
  21. {
  22. int n;
  23. if ( (n = utf8_mbtowc(uni, rawstring, boundlen)) == -1) {
  24. *uni = 0x003f; /* ? */
  25. n = -EINVAL;
  26. }
  27. return n;
  28. }
  29. static struct nls_table table = {
  30. .charset = "utf8",
  31. .uni2char = uni2char,
  32. .char2uni = char2uni,
  33. .charset2lower = identity, /* no conversion */
  34. .charset2upper = identity,
  35. .owner = THIS_MODULE,
  36. };
  37. static int __init init_nls_utf8(void)
  38. {
  39. int i;
  40. for (i=0; i<256; i++)
  41. identity[i] = i;
  42. return register_nls(&table);
  43. }
  44. static void __exit exit_nls_utf8(void)
  45. {
  46. unregister_nls(&table);
  47. }
  48. module_init(init_nls_utf8)
  49. module_exit(exit_nls_utf8)
  50. MODULE_LICENSE("Dual BSD/GPL");