18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * linux/fs/hfs/trans.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 1995-1997 Paul H. Hargrove 58c2ecf20Sopenharmony_ci * This file may be distributed under the terms of the GNU General Public License. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This file contains routines for converting between the Macintosh 88c2ecf20Sopenharmony_ci * character set and various other encodings. This includes dealing 98c2ecf20Sopenharmony_ci * with ':' vs. '/' as the path-element separator. 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <linux/nls.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include "hfs_fs.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/*================ Global functions ================*/ 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* 208c2ecf20Sopenharmony_ci * hfs_mac2asc() 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * Given a 'Pascal String' (a string preceded by a length byte) in 238c2ecf20Sopenharmony_ci * the Macintosh character set produce the corresponding filename using 248c2ecf20Sopenharmony_ci * the 'trivial' name-mangling scheme, returning the length of the 258c2ecf20Sopenharmony_ci * mangled filename. Note that the output string is not NULL 268c2ecf20Sopenharmony_ci * terminated. 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * The name-mangling works as follows: 298c2ecf20Sopenharmony_ci * The character '/', which is illegal in Linux filenames is replaced 308c2ecf20Sopenharmony_ci * by ':' which never appears in HFS filenames. All other characters 318c2ecf20Sopenharmony_ci * are passed unchanged from input to output. 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ciint hfs_mac2asc(struct super_block *sb, char *out, const struct hfs_name *in) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci struct nls_table *nls_disk = HFS_SB(sb)->nls_disk; 368c2ecf20Sopenharmony_ci struct nls_table *nls_io = HFS_SB(sb)->nls_io; 378c2ecf20Sopenharmony_ci const char *src; 388c2ecf20Sopenharmony_ci char *dst; 398c2ecf20Sopenharmony_ci int srclen, dstlen, size; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci src = in->name; 428c2ecf20Sopenharmony_ci srclen = in->len; 438c2ecf20Sopenharmony_ci if (srclen > HFS_NAMELEN) 448c2ecf20Sopenharmony_ci srclen = HFS_NAMELEN; 458c2ecf20Sopenharmony_ci dst = out; 468c2ecf20Sopenharmony_ci dstlen = HFS_MAX_NAMELEN; 478c2ecf20Sopenharmony_ci if (nls_io) { 488c2ecf20Sopenharmony_ci wchar_t ch; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci while (srclen > 0) { 518c2ecf20Sopenharmony_ci if (nls_disk) { 528c2ecf20Sopenharmony_ci size = nls_disk->char2uni(src, srclen, &ch); 538c2ecf20Sopenharmony_ci if (size <= 0) { 548c2ecf20Sopenharmony_ci ch = '?'; 558c2ecf20Sopenharmony_ci size = 1; 568c2ecf20Sopenharmony_ci } 578c2ecf20Sopenharmony_ci src += size; 588c2ecf20Sopenharmony_ci srclen -= size; 598c2ecf20Sopenharmony_ci } else { 608c2ecf20Sopenharmony_ci ch = *src++; 618c2ecf20Sopenharmony_ci srclen--; 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci if (ch == '/') 648c2ecf20Sopenharmony_ci ch = ':'; 658c2ecf20Sopenharmony_ci size = nls_io->uni2char(ch, dst, dstlen); 668c2ecf20Sopenharmony_ci if (size < 0) { 678c2ecf20Sopenharmony_ci if (size == -ENAMETOOLONG) 688c2ecf20Sopenharmony_ci goto out; 698c2ecf20Sopenharmony_ci *dst = '?'; 708c2ecf20Sopenharmony_ci size = 1; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci dst += size; 738c2ecf20Sopenharmony_ci dstlen -= size; 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci } else { 768c2ecf20Sopenharmony_ci char ch; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci while (--srclen >= 0) 798c2ecf20Sopenharmony_ci *dst++ = (ch = *src++) == '/' ? ':' : ch; 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ciout: 828c2ecf20Sopenharmony_ci return dst - out; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* 868c2ecf20Sopenharmony_ci * hfs_asc2mac() 878c2ecf20Sopenharmony_ci * 888c2ecf20Sopenharmony_ci * Given an ASCII string (not null-terminated) and its length, 898c2ecf20Sopenharmony_ci * generate the corresponding filename in the Macintosh character set 908c2ecf20Sopenharmony_ci * using the 'trivial' name-mangling scheme, returning the length of 918c2ecf20Sopenharmony_ci * the mangled filename. Note that the output string is not NULL 928c2ecf20Sopenharmony_ci * terminated. 938c2ecf20Sopenharmony_ci * 948c2ecf20Sopenharmony_ci * This routine is a inverse to hfs_mac2triv(). 958c2ecf20Sopenharmony_ci * A ':' is replaced by a '/'. 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_civoid hfs_asc2mac(struct super_block *sb, struct hfs_name *out, const struct qstr *in) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci struct nls_table *nls_disk = HFS_SB(sb)->nls_disk; 1008c2ecf20Sopenharmony_ci struct nls_table *nls_io = HFS_SB(sb)->nls_io; 1018c2ecf20Sopenharmony_ci const char *src; 1028c2ecf20Sopenharmony_ci char *dst; 1038c2ecf20Sopenharmony_ci int srclen, dstlen, size; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci src = in->name; 1068c2ecf20Sopenharmony_ci srclen = in->len; 1078c2ecf20Sopenharmony_ci dst = out->name; 1088c2ecf20Sopenharmony_ci dstlen = HFS_NAMELEN; 1098c2ecf20Sopenharmony_ci if (nls_io) { 1108c2ecf20Sopenharmony_ci wchar_t ch; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci while (srclen > 0 && dstlen > 0) { 1138c2ecf20Sopenharmony_ci size = nls_io->char2uni(src, srclen, &ch); 1148c2ecf20Sopenharmony_ci if (size < 0) { 1158c2ecf20Sopenharmony_ci ch = '?'; 1168c2ecf20Sopenharmony_ci size = 1; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci src += size; 1198c2ecf20Sopenharmony_ci srclen -= size; 1208c2ecf20Sopenharmony_ci if (ch == ':') 1218c2ecf20Sopenharmony_ci ch = '/'; 1228c2ecf20Sopenharmony_ci if (nls_disk) { 1238c2ecf20Sopenharmony_ci size = nls_disk->uni2char(ch, dst, dstlen); 1248c2ecf20Sopenharmony_ci if (size < 0) { 1258c2ecf20Sopenharmony_ci if (size == -ENAMETOOLONG) 1268c2ecf20Sopenharmony_ci goto out; 1278c2ecf20Sopenharmony_ci *dst = '?'; 1288c2ecf20Sopenharmony_ci size = 1; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci dst += size; 1318c2ecf20Sopenharmony_ci dstlen -= size; 1328c2ecf20Sopenharmony_ci } else { 1338c2ecf20Sopenharmony_ci *dst++ = ch > 0xff ? '?' : ch; 1348c2ecf20Sopenharmony_ci dstlen--; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci } else { 1388c2ecf20Sopenharmony_ci char ch; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if (dstlen > srclen) 1418c2ecf20Sopenharmony_ci dstlen = srclen; 1428c2ecf20Sopenharmony_ci while (--dstlen >= 0) 1438c2ecf20Sopenharmony_ci *dst++ = (ch = *src++) == ':' ? '/' : ch; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ciout: 1468c2ecf20Sopenharmony_ci out->len = dst - (char *)out->name; 1478c2ecf20Sopenharmony_ci dstlen = HFS_NAMELEN - out->len; 1488c2ecf20Sopenharmony_ci while (--dstlen >= 0) 1498c2ecf20Sopenharmony_ci *dst++ = 0; 1508c2ecf20Sopenharmony_ci} 151