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