trans.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if (srclen > HFS_NAMELEN)
  39. srclen = HFS_NAMELEN;
  40. dst = out;
  41. dstlen = HFS_MAX_NAMELEN;
  42. if (nls_io) {
  43. wchar_t ch;
  44. while (srclen > 0) {
  45. if (nls_disk) {
  46. size = nls_disk->char2uni(src, srclen, &ch);
  47. if (size <= 0) {
  48. ch = '?';
  49. size = 1;
  50. }
  51. src += size;
  52. srclen -= size;
  53. } else {
  54. ch = *src++;
  55. srclen--;
  56. }
  57. if (ch == '/')
  58. ch = ':';
  59. size = nls_io->uni2char(ch, dst, dstlen);
  60. if (size < 0) {
  61. if (size == -ENAMETOOLONG)
  62. goto out;
  63. *dst = '?';
  64. size = 1;
  65. }
  66. dst += size;
  67. dstlen -= size;
  68. }
  69. } else {
  70. char ch;
  71. while (--srclen >= 0)
  72. *dst++ = (ch = *src++) == '/' ? ':' : ch;
  73. }
  74. out:
  75. return dst - out;
  76. }
  77. /*
  78. * hfs_asc2mac()
  79. *
  80. * Given an ASCII string (not null-terminated) and its length,
  81. * generate the corresponding filename in the Macintosh character set
  82. * using the 'trivial' name-mangling scheme, returning the length of
  83. * the mangled filename. Note that the output string is not NULL
  84. * terminated.
  85. *
  86. * This routine is a inverse to hfs_mac2triv().
  87. * A ':' is replaced by a '/'.
  88. */
  89. void hfs_asc2mac(struct super_block *sb, struct hfs_name *out, struct qstr *in)
  90. {
  91. struct nls_table *nls_disk = HFS_SB(sb)->nls_disk;
  92. struct nls_table *nls_io = HFS_SB(sb)->nls_io;
  93. const char *src;
  94. char *dst;
  95. int srclen, dstlen, size;
  96. src = in->name;
  97. srclen = in->len;
  98. dst = out->name;
  99. dstlen = HFS_NAMELEN;
  100. if (nls_io) {
  101. wchar_t ch;
  102. while (srclen > 0) {
  103. size = nls_io->char2uni(src, srclen, &ch);
  104. if (size < 0) {
  105. ch = '?';
  106. size = 1;
  107. }
  108. src += size;
  109. srclen -= size;
  110. if (ch == ':')
  111. ch = '/';
  112. if (nls_disk) {
  113. size = nls_disk->uni2char(ch, dst, dstlen);
  114. if (size < 0) {
  115. if (size == -ENAMETOOLONG)
  116. goto out;
  117. *dst = '?';
  118. size = 1;
  119. }
  120. dst += size;
  121. dstlen -= size;
  122. } else {
  123. *dst++ = ch > 0xff ? '?' : ch;
  124. dstlen--;
  125. }
  126. }
  127. } else {
  128. char ch;
  129. if (dstlen > srclen)
  130. dstlen = srclen;
  131. while (--dstlen >= 0)
  132. *dst++ = (ch = *src++) == ':' ? '/' : ch;
  133. }
  134. out:
  135. out->len = dst - (char *)out->name;
  136. dstlen = HFS_NAMELEN - out->len;
  137. while (--dstlen >= 0)
  138. *dst++ = 0;
  139. }