11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * linux/fs/hfs/trans.c
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Copyright (C) 1995-1997 Paul H. Hargrove
51da177e4SLinus Torvalds * This file may be distributed under the terms of the GNU General Public License.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * This file contains routines for converting between the Macintosh
81da177e4SLinus Torvalds * character set and various other encodings. This includes dealing
91da177e4SLinus Torvalds * with ':' vs. '/' as the path-element separator.
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
12328b9227SRoman Zippel #include <linux/types.h>
13328b9227SRoman Zippel #include <linux/nls.h>
14328b9227SRoman Zippel
151da177e4SLinus Torvalds #include "hfs_fs.h"
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds /*================ Global functions ================*/
181da177e4SLinus Torvalds
191da177e4SLinus Torvalds /*
20328b9227SRoman Zippel * hfs_mac2asc()
211da177e4SLinus Torvalds *
221da177e4SLinus Torvalds * Given a 'Pascal String' (a string preceded by a length byte) in
231da177e4SLinus Torvalds * the Macintosh character set produce the corresponding filename using
241da177e4SLinus Torvalds * the 'trivial' name-mangling scheme, returning the length of the
251da177e4SLinus Torvalds * mangled filename. Note that the output string is not NULL
261da177e4SLinus Torvalds * terminated.
271da177e4SLinus Torvalds *
281da177e4SLinus Torvalds * The name-mangling works as follows:
291da177e4SLinus Torvalds * The character '/', which is illegal in Linux filenames is replaced
301da177e4SLinus Torvalds * by ':' which never appears in HFS filenames. All other characters
311da177e4SLinus Torvalds * are passed unchanged from input to output.
321da177e4SLinus Torvalds */
hfs_mac2asc(struct super_block * sb,char * out,const struct hfs_name * in)33328b9227SRoman Zippel int hfs_mac2asc(struct super_block *sb, char *out, const struct hfs_name *in)
341da177e4SLinus Torvalds {
35328b9227SRoman Zippel struct nls_table *nls_disk = HFS_SB(sb)->nls_disk;
36328b9227SRoman Zippel struct nls_table *nls_io = HFS_SB(sb)->nls_io;
37328b9227SRoman Zippel const char *src;
38328b9227SRoman Zippel char *dst;
39328b9227SRoman Zippel int srclen, dstlen, size;
401da177e4SLinus Torvalds
41328b9227SRoman Zippel src = in->name;
42328b9227SRoman Zippel srclen = in->len;
43bc5b8a90SDan Carpenter if (srclen > HFS_NAMELEN)
44bc5b8a90SDan Carpenter srclen = HFS_NAMELEN;
45328b9227SRoman Zippel dst = out;
46328b9227SRoman Zippel dstlen = HFS_MAX_NAMELEN;
47328b9227SRoman Zippel if (nls_io) {
48328b9227SRoman Zippel wchar_t ch;
49328b9227SRoman Zippel
50328b9227SRoman Zippel while (srclen > 0) {
51328b9227SRoman Zippel if (nls_disk) {
52328b9227SRoman Zippel size = nls_disk->char2uni(src, srclen, &ch);
53328b9227SRoman Zippel if (size <= 0) {
54328b9227SRoman Zippel ch = '?';
55328b9227SRoman Zippel size = 1;
561da177e4SLinus Torvalds }
57328b9227SRoman Zippel src += size;
58328b9227SRoman Zippel srclen -= size;
59328b9227SRoman Zippel } else {
60328b9227SRoman Zippel ch = *src++;
61328b9227SRoman Zippel srclen--;
62328b9227SRoman Zippel }
63328b9227SRoman Zippel if (ch == '/')
64328b9227SRoman Zippel ch = ':';
65328b9227SRoman Zippel size = nls_io->uni2char(ch, dst, dstlen);
66328b9227SRoman Zippel if (size < 0) {
67328b9227SRoman Zippel if (size == -ENAMETOOLONG)
68328b9227SRoman Zippel goto out;
69328b9227SRoman Zippel *dst = '?';
70328b9227SRoman Zippel size = 1;
71328b9227SRoman Zippel }
72328b9227SRoman Zippel dst += size;
73328b9227SRoman Zippel dstlen -= size;
74328b9227SRoman Zippel }
75328b9227SRoman Zippel } else {
76328b9227SRoman Zippel char ch;
77328b9227SRoman Zippel
78328b9227SRoman Zippel while (--srclen >= 0)
79328b9227SRoman Zippel *dst++ = (ch = *src++) == '/' ? ':' : ch;
80328b9227SRoman Zippel }
81328b9227SRoman Zippel out:
82328b9227SRoman Zippel return dst - out;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds
851da177e4SLinus Torvalds /*
86328b9227SRoman Zippel * hfs_asc2mac()
871da177e4SLinus Torvalds *
881da177e4SLinus Torvalds * Given an ASCII string (not null-terminated) and its length,
891da177e4SLinus Torvalds * generate the corresponding filename in the Macintosh character set
901da177e4SLinus Torvalds * using the 'trivial' name-mangling scheme, returning the length of
911da177e4SLinus Torvalds * the mangled filename. Note that the output string is not NULL
921da177e4SLinus Torvalds * terminated.
931da177e4SLinus Torvalds *
941da177e4SLinus Torvalds * This routine is a inverse to hfs_mac2triv().
951da177e4SLinus Torvalds * A ':' is replaced by a '/'.
961da177e4SLinus Torvalds */
hfs_asc2mac(struct super_block * sb,struct hfs_name * out,const struct qstr * in)9771e93963SAl Viro void hfs_asc2mac(struct super_block *sb, struct hfs_name *out, const struct qstr *in)
981da177e4SLinus Torvalds {
99328b9227SRoman Zippel struct nls_table *nls_disk = HFS_SB(sb)->nls_disk;
100328b9227SRoman Zippel struct nls_table *nls_io = HFS_SB(sb)->nls_io;
1011da177e4SLinus Torvalds const char *src;
102328b9227SRoman Zippel char *dst;
103328b9227SRoman Zippel int srclen, dstlen, size;
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds src = in->name;
106328b9227SRoman Zippel srclen = in->len;
1071da177e4SLinus Torvalds dst = out->name;
108328b9227SRoman Zippel dstlen = HFS_NAMELEN;
109328b9227SRoman Zippel if (nls_io) {
110328b9227SRoman Zippel wchar_t ch;
111328b9227SRoman Zippel
112*c53ed55cSZhangPeng while (srclen > 0 && dstlen > 0) {
113328b9227SRoman Zippel size = nls_io->char2uni(src, srclen, &ch);
114328b9227SRoman Zippel if (size < 0) {
115328b9227SRoman Zippel ch = '?';
116328b9227SRoman Zippel size = 1;
1171da177e4SLinus Torvalds }
118328b9227SRoman Zippel src += size;
119328b9227SRoman Zippel srclen -= size;
120328b9227SRoman Zippel if (ch == ':')
121328b9227SRoman Zippel ch = '/';
122328b9227SRoman Zippel if (nls_disk) {
123328b9227SRoman Zippel size = nls_disk->uni2char(ch, dst, dstlen);
124328b9227SRoman Zippel if (size < 0) {
125328b9227SRoman Zippel if (size == -ENAMETOOLONG)
126328b9227SRoman Zippel goto out;
127328b9227SRoman Zippel *dst = '?';
128328b9227SRoman Zippel size = 1;
129328b9227SRoman Zippel }
130328b9227SRoman Zippel dst += size;
131328b9227SRoman Zippel dstlen -= size;
132328b9227SRoman Zippel } else {
133328b9227SRoman Zippel *dst++ = ch > 0xff ? '?' : ch;
134328b9227SRoman Zippel dstlen--;
135328b9227SRoman Zippel }
136328b9227SRoman Zippel }
137328b9227SRoman Zippel } else {
138328b9227SRoman Zippel char ch;
139328b9227SRoman Zippel
140328b9227SRoman Zippel if (dstlen > srclen)
141328b9227SRoman Zippel dstlen = srclen;
142328b9227SRoman Zippel while (--dstlen >= 0)
143328b9227SRoman Zippel *dst++ = (ch = *src++) == ':' ? '/' : ch;
144328b9227SRoman Zippel }
145328b9227SRoman Zippel out:
146328b9227SRoman Zippel out->len = dst - (char *)out->name;
147328b9227SRoman Zippel dstlen = HFS_NAMELEN - out->len;
148328b9227SRoman Zippel while (--dstlen >= 0)
1491da177e4SLinus Torvalds *dst++ = 0;
1501da177e4SLinus Torvalds }
151