trans.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * linux/fs/hfs/trans.c
  3. *
  4. * Copyright (C) 1995-1997 Paul H. Hargrove
  5. * This file may be distributed under the terms of the GNU General Public License.
  6. *
  7. * This file contains routines for converting between the Macintosh
  8. * character set and various other encodings. This includes dealing
  9. * with ':' vs. '/' as the path-element separator.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/nls.h>
  13. #include "hfs_fs.h"
  14. /*================ Global functions ================*/
  15. /*
  16. * hfs_mac2asc()
  17. *
  18. * Given a 'Pascal String' (a string preceded by a length byte) in
  19. * the Macintosh character set produce the corresponding filename using
  20. * the 'trivial' name-mangling scheme, returning the length of the
  21. * mangled filename. Note that the output string is not NULL
  22. * terminated.
  23. *
  24. * The name-mangling works as follows:
  25. * The character '/', which is illegal in Linux filenames is replaced
  26. * by ':' which never appears in HFS filenames. All other characters
  27. * are passed unchanged from input to output.
  28. */
  29. int hfs_mac2asc(struct super_block *sb, char *out, const struct hfs_name *in)
  30. {
  31. struct nls_table *nls_disk = HFS_SB(sb)->nls_disk;
  32. struct nls_table *nls_io = HFS_SB(sb)->nls_io;
  33. const char *src;
  34. char *dst;
  35. int srclen, dstlen, size;
  36. src = in->name;
  37. srclen = in->len;
  38. dst = out;
  39. dstlen = HFS_MAX_NAMELEN;
  40. if (nls_io) {
  41. wchar_t ch;
  42. while (srclen > 0) {
  43. if (nls_disk) {
  44. size = nls_disk->char2uni(src, srclen, &ch);
  45. if (size <= 0) {
  46. ch = '?';
  47. size = 1;
  48. }
  49. src += size;
  50. srclen -= size;
  51. } else {
  52. ch = *src++;
  53. srclen--;
  54. }
  55. if (ch == '/')
  56. ch = ':';
  57. size = nls_io->uni2char(ch, dst, dstlen);
  58. if (size < 0) {
  59. if (size == -ENAMETOOLONG)
  60. goto out;
  61. *dst = '?';
  62. size = 1;
  63. }
  64. dst += size;
  65. dstlen -= size;
  66. }
  67. } else {
  68. char ch;
  69. while (--srclen >= 0)
  70. *dst++ = (ch = *src++) == '/' ? ':' : ch;
  71. }
  72. out:
  73. return dst - out;
  74. }
  75. /*
  76. * hfs_asc2mac()
  77. *
  78. * Given an ASCII string (not null-terminated) and its length,
  79. * generate the corresponding filename in the Macintosh character set
  80. * using the 'trivial' name-mangling scheme, returning the length of
  81. * the mangled filename. Note that the output string is not NULL
  82. * terminated.
  83. *
  84. * This routine is a inverse to hfs_mac2triv().
  85. * A ':' is replaced by a '/'.
  86. */
  87. void hfs_asc2mac(struct super_block *sb, struct hfs_name *out, struct qstr *in)
  88. {
  89. struct nls_table *nls_disk = HFS_SB(sb)->nls_disk;
  90. struct nls_table *nls_io = HFS_SB(sb)->nls_io;
  91. const char *src;
  92. char *dst;
  93. int srclen, dstlen, size;
  94. src = in->name;
  95. srclen = in->len;
  96. dst = out->name;
  97. dstlen = HFS_NAMELEN;
  98. if (nls_io) {
  99. wchar_t ch;
  100. while (srclen > 0) {
  101. size = nls_io->char2uni(src, srclen, &ch);
  102. if (size < 0) {
  103. ch = '?';
  104. size = 1;
  105. }
  106. src += size;
  107. srclen -= size;
  108. if (ch == ':')
  109. ch = '/';
  110. if (nls_disk) {
  111. size = nls_disk->uni2char(ch, dst, dstlen);
  112. if (size < 0) {
  113. if (size == -ENAMETOOLONG)
  114. goto out;
  115. *dst = '?';
  116. size = 1;
  117. }
  118. dst += size;
  119. dstlen -= size;
  120. } else {
  121. *dst++ = ch > 0xff ? '?' : ch;
  122. dstlen--;
  123. }
  124. }
  125. } else {
  126. char ch;
  127. if (dstlen > srclen)
  128. dstlen = srclen;
  129. while (--dstlen >= 0)
  130. *dst++ = (ch = *src++) == ':' ? '/' : ch;
  131. }
  132. out:
  133. out->len = dst - (char *)out->name;
  134. dstlen = HFS_NAMELEN - out->len;
  135. while (--dstlen >= 0)
  136. *dst++ = 0;
  137. }