162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  linux/fs/hfs/trans.c
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (C) 1995-1997  Paul H. Hargrove
562306a36Sopenharmony_ci * This file may be distributed under the terms of the GNU General Public License.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * This file contains routines for converting between the Macintosh
862306a36Sopenharmony_ci * character set and various other encodings.  This includes dealing
962306a36Sopenharmony_ci * with ':' vs. '/' as the path-element separator.
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/types.h>
1362306a36Sopenharmony_ci#include <linux/nls.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include "hfs_fs.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/*================ Global functions ================*/
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/*
2062306a36Sopenharmony_ci * hfs_mac2asc()
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Given a 'Pascal String' (a string preceded by a length byte) in
2362306a36Sopenharmony_ci * the Macintosh character set produce the corresponding filename using
2462306a36Sopenharmony_ci * the 'trivial' name-mangling scheme, returning the length of the
2562306a36Sopenharmony_ci * mangled filename.  Note that the output string is not NULL
2662306a36Sopenharmony_ci * terminated.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * The name-mangling works as follows:
2962306a36Sopenharmony_ci * The character '/', which is illegal in Linux filenames is replaced
3062306a36Sopenharmony_ci * by ':' which never appears in HFS filenames.	 All other characters
3162306a36Sopenharmony_ci * are passed unchanged from input to output.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ciint hfs_mac2asc(struct super_block *sb, char *out, const struct hfs_name *in)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	struct nls_table *nls_disk = HFS_SB(sb)->nls_disk;
3662306a36Sopenharmony_ci	struct nls_table *nls_io = HFS_SB(sb)->nls_io;
3762306a36Sopenharmony_ci	const char *src;
3862306a36Sopenharmony_ci	char *dst;
3962306a36Sopenharmony_ci	int srclen, dstlen, size;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	src = in->name;
4262306a36Sopenharmony_ci	srclen = in->len;
4362306a36Sopenharmony_ci	if (srclen > HFS_NAMELEN)
4462306a36Sopenharmony_ci		srclen = HFS_NAMELEN;
4562306a36Sopenharmony_ci	dst = out;
4662306a36Sopenharmony_ci	dstlen = HFS_MAX_NAMELEN;
4762306a36Sopenharmony_ci	if (nls_io) {
4862306a36Sopenharmony_ci		wchar_t ch;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci		while (srclen > 0) {
5162306a36Sopenharmony_ci			if (nls_disk) {
5262306a36Sopenharmony_ci				size = nls_disk->char2uni(src, srclen, &ch);
5362306a36Sopenharmony_ci				if (size <= 0) {
5462306a36Sopenharmony_ci					ch = '?';
5562306a36Sopenharmony_ci					size = 1;
5662306a36Sopenharmony_ci				}
5762306a36Sopenharmony_ci				src += size;
5862306a36Sopenharmony_ci				srclen -= size;
5962306a36Sopenharmony_ci			} else {
6062306a36Sopenharmony_ci				ch = *src++;
6162306a36Sopenharmony_ci				srclen--;
6262306a36Sopenharmony_ci			}
6362306a36Sopenharmony_ci			if (ch == '/')
6462306a36Sopenharmony_ci				ch = ':';
6562306a36Sopenharmony_ci			size = nls_io->uni2char(ch, dst, dstlen);
6662306a36Sopenharmony_ci			if (size < 0) {
6762306a36Sopenharmony_ci				if (size == -ENAMETOOLONG)
6862306a36Sopenharmony_ci					goto out;
6962306a36Sopenharmony_ci				*dst = '?';
7062306a36Sopenharmony_ci				size = 1;
7162306a36Sopenharmony_ci			}
7262306a36Sopenharmony_ci			dst += size;
7362306a36Sopenharmony_ci			dstlen -= size;
7462306a36Sopenharmony_ci		}
7562306a36Sopenharmony_ci	} else {
7662306a36Sopenharmony_ci		char ch;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci		while (--srclen >= 0)
7962306a36Sopenharmony_ci			*dst++ = (ch = *src++) == '/' ? ':' : ch;
8062306a36Sopenharmony_ci	}
8162306a36Sopenharmony_ciout:
8262306a36Sopenharmony_ci	return dst - out;
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/*
8662306a36Sopenharmony_ci * hfs_asc2mac()
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * Given an ASCII string (not null-terminated) and its length,
8962306a36Sopenharmony_ci * generate the corresponding filename in the Macintosh character set
9062306a36Sopenharmony_ci * using the 'trivial' name-mangling scheme, returning the length of
9162306a36Sopenharmony_ci * the mangled filename.  Note that the output string is not NULL
9262306a36Sopenharmony_ci * terminated.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * This routine is a inverse to hfs_mac2triv().
9562306a36Sopenharmony_ci * A ':' is replaced by a '/'.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_civoid hfs_asc2mac(struct super_block *sb, struct hfs_name *out, const struct qstr *in)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	struct nls_table *nls_disk = HFS_SB(sb)->nls_disk;
10062306a36Sopenharmony_ci	struct nls_table *nls_io = HFS_SB(sb)->nls_io;
10162306a36Sopenharmony_ci	const char *src;
10262306a36Sopenharmony_ci	char *dst;
10362306a36Sopenharmony_ci	int srclen, dstlen, size;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	src = in->name;
10662306a36Sopenharmony_ci	srclen = in->len;
10762306a36Sopenharmony_ci	dst = out->name;
10862306a36Sopenharmony_ci	dstlen = HFS_NAMELEN;
10962306a36Sopenharmony_ci	if (nls_io) {
11062306a36Sopenharmony_ci		wchar_t ch;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci		while (srclen > 0 && dstlen > 0) {
11362306a36Sopenharmony_ci			size = nls_io->char2uni(src, srclen, &ch);
11462306a36Sopenharmony_ci			if (size < 0) {
11562306a36Sopenharmony_ci				ch = '?';
11662306a36Sopenharmony_ci				size = 1;
11762306a36Sopenharmony_ci			}
11862306a36Sopenharmony_ci			src += size;
11962306a36Sopenharmony_ci			srclen -= size;
12062306a36Sopenharmony_ci			if (ch == ':')
12162306a36Sopenharmony_ci				ch = '/';
12262306a36Sopenharmony_ci			if (nls_disk) {
12362306a36Sopenharmony_ci				size = nls_disk->uni2char(ch, dst, dstlen);
12462306a36Sopenharmony_ci				if (size < 0) {
12562306a36Sopenharmony_ci					if (size == -ENAMETOOLONG)
12662306a36Sopenharmony_ci						goto out;
12762306a36Sopenharmony_ci					*dst = '?';
12862306a36Sopenharmony_ci					size = 1;
12962306a36Sopenharmony_ci				}
13062306a36Sopenharmony_ci				dst += size;
13162306a36Sopenharmony_ci				dstlen -= size;
13262306a36Sopenharmony_ci			} else {
13362306a36Sopenharmony_ci				*dst++ = ch > 0xff ? '?' : ch;
13462306a36Sopenharmony_ci				dstlen--;
13562306a36Sopenharmony_ci			}
13662306a36Sopenharmony_ci		}
13762306a36Sopenharmony_ci	} else {
13862306a36Sopenharmony_ci		char ch;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci		if (dstlen > srclen)
14162306a36Sopenharmony_ci			dstlen = srclen;
14262306a36Sopenharmony_ci		while (--dstlen >= 0)
14362306a36Sopenharmony_ci			*dst++ = (ch = *src++) == ':' ? '/' : ch;
14462306a36Sopenharmony_ci	}
14562306a36Sopenharmony_ciout:
14662306a36Sopenharmony_ci	out->len = dst - (char *)out->name;
14762306a36Sopenharmony_ci	dstlen = HFS_NAMELEN - out->len;
14862306a36Sopenharmony_ci	while (--dstlen >= 0)
14962306a36Sopenharmony_ci		*dst++ = 0;
15062306a36Sopenharmony_ci}
151