nls_utf8.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 (boundlen <= 0)
  15. return -ENAMETOOLONG;
  16. n = utf32_to_utf8(uni, out, boundlen);
  17. if (n < 0) {
  18. *out = '?';
  19. return -EINVAL;
  20. }
  21. return n;
  22. }
  23. static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
  24. {
  25. int n;
  26. unicode_t u;
  27. n = utf8_to_utf32(rawstring, boundlen, &u);
  28. if (n < 0 || u > MAX_WCHAR_T) {
  29. *uni = 0x003f; /* ? */
  30. return -EINVAL;
  31. }
  32. *uni = (wchar_t) u;
  33. return n;
  34. }
  35. static struct nls_table table = {
  36. .charset = "utf8",
  37. .uni2char = uni2char,
  38. .char2uni = char2uni,
  39. .charset2lower = identity, /* no conversion */
  40. .charset2upper = identity,
  41. };
  42. static int __init init_nls_utf8(void)
  43. {
  44. int i;
  45. for (i=0; i<256; i++)
  46. identity[i] = i;
  47. return register_nls(&table);
  48. }
  49. static void __exit exit_nls_utf8(void)
  50. {
  51. unregister_nls(&table);
  52. }
  53. module_init(init_nls_utf8)
  54. module_exit(exit_nls_utf8)
  55. MODULE_LICENSE("Dual BSD/GPL");