nls_koi8-ru.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * linux/fs/nls/nls_koi8-ru.c
  3. *
  4. * Charset koi8-ru translation based on charset koi8-u.
  5. * The Unicode to charset table has only exact mappings.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/string.h>
  10. #include <linux/nls.h>
  11. #include <linux/errno.h>
  12. static struct nls_table *p_nls;
  13. static int uni2char(const wchar_t uni,
  14. unsigned char *out, int boundlen)
  15. {
  16. if (boundlen <= 0)
  17. return -ENAMETOOLONG;
  18. if ((uni & 0xffaf) == 0x040e || (uni & 0xffce) == 0x254c) {
  19. /* koi8-ru and koi8-u differ only on two characters */
  20. if (uni == 0x040e)
  21. out[0] = 0xbe;
  22. else if (uni == 0x045e)
  23. out[0] = 0xae;
  24. else if (uni == 0x255d || uni == 0x256c)
  25. return 0;
  26. else
  27. return p_nls->uni2char(uni, out, boundlen);
  28. return 1;
  29. }
  30. else
  31. /* fast path */
  32. return p_nls->uni2char(uni, out, boundlen);
  33. }
  34. static int char2uni(const unsigned char *rawstring, int boundlen,
  35. wchar_t *uni)
  36. {
  37. int n;
  38. if ((*rawstring & 0xef) != 0xae) {
  39. /* koi8-ru and koi8-u differ only on two characters */
  40. *uni = (*rawstring & 0x10) ? 0x040e : 0x045e;
  41. return 1;
  42. }
  43. n = p_nls->char2uni(rawstring, boundlen, uni);
  44. return n;
  45. }
  46. static struct nls_table table = {
  47. .charset = "koi8-ru",
  48. .uni2char = uni2char,
  49. .char2uni = char2uni,
  50. };
  51. static int __init init_nls_koi8_ru(void)
  52. {
  53. p_nls = load_nls("koi8-u");
  54. if (p_nls) {
  55. table.charset2upper = p_nls->charset2upper;
  56. table.charset2lower = p_nls->charset2lower;
  57. return register_nls(&table);
  58. }
  59. return -EINVAL;
  60. }
  61. static void __exit exit_nls_koi8_ru(void)
  62. {
  63. unregister_nls(&table);
  64. unload_nls(p_nls);
  65. }
  66. module_init(init_nls_koi8_ru)
  67. module_exit(exit_nls_koi8_ru)
  68. MODULE_LICENSE("Dual BSD/GPL");