150477Speter/* $FreeBSD: stable/10/sys/fs/msdosfs/msdosfs_conv.c 333611 2018-05-14 19:21:57Z pfg $ */ 233548Sjkh/* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */ 32893Sdfr 433548Sjkh/*- 533548Sjkh * Copyright (C) 1995, 1997 Wolfgang Solfrank. 633548Sjkh * Copyright (C) 1995, 1997 TooLs GmbH. 733548Sjkh * All rights reserved. 833548Sjkh * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 933548Sjkh * 1033548Sjkh * Redistribution and use in source and binary forms, with or without 1133548Sjkh * modification, are permitted provided that the following conditions 1233548Sjkh * are met: 1333548Sjkh * 1. Redistributions of source code must retain the above copyright 1433548Sjkh * notice, this list of conditions and the following disclaimer. 1533548Sjkh * 2. Redistributions in binary form must reproduce the above copyright 1633548Sjkh * notice, this list of conditions and the following disclaimer in the 1733548Sjkh * documentation and/or other materials provided with the distribution. 1833548Sjkh * 3. All advertising materials mentioning features or use of this software 1933548Sjkh * must display the following acknowledgement: 2033548Sjkh * This product includes software developed by TooLs GmbH. 2133548Sjkh * 4. The name of TooLs GmbH may not be used to endorse or promote products 2233548Sjkh * derived from this software without specific prior written permission. 2333548Sjkh * 2433548Sjkh * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2533548Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2633548Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2733548Sjkh * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2833548Sjkh * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2933548Sjkh * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3033548Sjkh * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3133548Sjkh * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3233548Sjkh * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3333548Sjkh * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3433548Sjkh */ 35139776Simp/*- 362893Sdfr * Written by Paul Popelka (paulp@uts.amdahl.com) 378876Srgrimes * 382893Sdfr * You can do anything you want with this software, just don't say you wrote 392893Sdfr * it, and don't remove this notice. 408876Srgrimes * 412893Sdfr * This software is provided "as is". 428876Srgrimes * 432893Sdfr * The author supplies this software to be publicly redistributed on the 442893Sdfr * understanding that the author is not responsible for the correct 452893Sdfr * functioning of this software in any circumstances and is not liable for 462893Sdfr * any damages caused by this software. 478876Srgrimes * 482893Sdfr * October 1992 492893Sdfr */ 502893Sdfr 512893Sdfr#include <sys/param.h> 5233548Sjkh#include <sys/systm.h> 5333548Sjkh#include <sys/dirent.h> 54120492Sfjoe#include <sys/iconv.h> 55120492Sfjoe#include <sys/mount.h> 562893Sdfr 57120492Sfjoe#include <fs/msdosfs/bpb.h> 58171751Sbde#include <fs/msdosfs/direntry.h> 59120492Sfjoe#include <fs/msdosfs/msdosfsmount.h> 602893Sdfr 61171751Sbdeextern struct iconv_functions *msdosfs_iconv; 62171751Sbde 63125992Stjrstatic int mbsadjpos(const char **, size_t, size_t, int, int, void *handle); 64228796Skevlostatic u_char * dos2unixchr(u_char *, const u_char **, size_t *, int, struct msdosfsmount *); 65120492Sfjoestatic u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *); 66228796Skevlostatic u_char * win2unixchr(u_char *, u_int16_t, struct msdosfsmount *); 67120492Sfjoestatic u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *); 6833747Sache 692893Sdfr/* 7033872Smsmith * 0 - character disallowed in long file name. 7133872Smsmith * 1 - character should be replaced by '_' in DOS file name, 7233872Smsmith * and generation number inserted. 7333872Smsmith * 2 - character ('.' and ' ') should be skipped in DOS file name, 7433872Smsmith * and generation number inserted. 7533872Smsmith */ 7633548Sjkhstatic u_char 7733548Sjkhunix2dos[256] = { 78120492Sfjoe/* iso8859-1 -> cp850 */ 7933548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 8033548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 8133548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 8233548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 8333872Smsmith 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 8433872Smsmith 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */ 8533548Sjkh 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 8633872Smsmith 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */ 8733548Sjkh 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 8833548Sjkh 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 8933548Sjkh 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 9033872Smsmith 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */ 9133548Sjkh 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 9233548Sjkh 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 9333548Sjkh 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 9433548Sjkh 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 9533548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 9633548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 9733548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 9833548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 9933548Sjkh 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 10033548Sjkh 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 10133548Sjkh 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 10233548Sjkh 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 10333548Sjkh 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 10433548Sjkh 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 10533548Sjkh 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 10633548Sjkh 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 10733548Sjkh 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 10833548Sjkh 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 10933548Sjkh 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 11033548Sjkh 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 11133548Sjkh}; 1122893Sdfr 11333548Sjkhstatic u_char 11433548Sjkhdos2unix[256] = { 115120492Sfjoe/* cp850 -> iso8859-1 */ 11633548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 11733548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 11833548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 11933548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 12033548Sjkh 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 12133548Sjkh 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 12233548Sjkh 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 12333548Sjkh 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 12433548Sjkh 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 12533548Sjkh 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 12633548Sjkh 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 12733548Sjkh 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 12833548Sjkh 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 12933548Sjkh 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 13033548Sjkh 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 13133548Sjkh 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 13233548Sjkh 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 13333548Sjkh 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 13433548Sjkh 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 13533548Sjkh 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 13633548Sjkh 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 13733548Sjkh 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 13833548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 13933548Sjkh 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 14033548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 14133548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 14233548Sjkh 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 14333548Sjkh 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 14433548Sjkh 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 14533548Sjkh 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 14633548Sjkh 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 14733548Sjkh 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 14833548Sjkh}; 14933548Sjkh 15033548Sjkhstatic u_char 15133548Sjkhu2l[256] = { 152120492Sfjoe/* tolower */ 15333548Sjkh 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 15433548Sjkh 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 15533548Sjkh 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 15633548Sjkh 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 15733548Sjkh 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 15833548Sjkh 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 15933548Sjkh 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 16033548Sjkh 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 16133548Sjkh 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 16233548Sjkh 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 16333548Sjkh 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 16433548Sjkh 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 16533548Sjkh 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 16633548Sjkh 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 16733548Sjkh 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 16833548Sjkh 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 16933548Sjkh 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 17033548Sjkh 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 17133548Sjkh 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 17233548Sjkh 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 17333548Sjkh 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 17433548Sjkh 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 17533548Sjkh 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 17633548Sjkh 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 17733548Sjkh 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 17833548Sjkh 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 17933548Sjkh 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 18033548Sjkh 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 18133548Sjkh 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 18233548Sjkh 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 18333548Sjkh 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 18433548Sjkh 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 18533548Sjkh}; 18633548Sjkh 18733768Sachestatic u_char 18833768Sachel2u[256] = { 189120492Sfjoe/* toupper */ 19033768Sache 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 19133768Sache 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 19233768Sache 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 19333768Sache 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 19433768Sache 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 19533768Sache 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 19633768Sache 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 19733768Sache 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 19833768Sache 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 19933768Sache 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 20033768Sache 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 20133768Sache 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 20233768Sache 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 20333768Sache 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 20433768Sache 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 20533768Sache 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 20633768Sache 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 20733768Sache 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 20833768Sache 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 20933768Sache 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 21033768Sache 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 21133768Sache 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 21233768Sache 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 21333768Sache 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 21433768Sache 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 21533768Sache 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 21633768Sache 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 21733768Sache 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 21833768Sache 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 21933768Sache 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 22033768Sache 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 22133768Sache 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 22233768Sache}; 22333768Sache 2242893Sdfr/* 2252893Sdfr * DOS filenames are made of 2 parts, the name part and the extension part. 2262893Sdfr * The name part is 8 characters long and the extension part is 3 2272893Sdfr * characters long. They may contain trailing blanks if the name or 2282893Sdfr * extension are not long enough to fill their respective fields. 2292893Sdfr */ 2302893Sdfr 2312893Sdfr/* 2322893Sdfr * Convert a DOS filename to a unix filename. And, return the number of 2332893Sdfr * characters in the resulting unix filename excluding the terminating 2342893Sdfr * null. 2352893Sdfr */ 2362893Sdfrint 237120492Sfjoedos2unixfn(dn, un, lower, pmp) 2382893Sdfr u_char dn[11]; 2392893Sdfr u_char *un; 24033791Sache int lower; 241120492Sfjoe struct msdosfsmount *pmp; 2422893Sdfr{ 243125992Stjr size_t i; 244120492Sfjoe int thislong = 0; 245228796Skevlo u_char *c, tmpbuf[5]; 2462893Sdfr 2472893Sdfr /* 24833548Sjkh * If first char of the filename is SLOT_E5 (0x05), then the real 24933548Sjkh * first char of the filename should be 0xe5. But, they couldn't 25033548Sjkh * just have a 0xe5 mean 0xe5 because that is used to mean a freed 25133548Sjkh * directory slot. Another dos quirk. 2522893Sdfr */ 25333548Sjkh if (*dn == SLOT_E5) 254120492Sfjoe *dn = 0xe5; 2552893Sdfr 2562893Sdfr /* 25733548Sjkh * Copy the name portion into the unix filename string. 2582893Sdfr */ 259120492Sfjoe for (i = 8; i > 0 && *dn != ' ';) { 260278059Sdim c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), &i, 261228796Skevlo lower & LCASE_BASE, pmp); 262227650Skevlo while (*c != '\0') { 263227650Skevlo *un++ = *c++; 264120492Sfjoe thislong++; 265120492Sfjoe } 2662893Sdfr } 267120492Sfjoe dn += i; 2682893Sdfr 2692893Sdfr /* 2702893Sdfr * Now, if there is an extension then put in a period and copy in 2712893Sdfr * the extension. 2722893Sdfr */ 27333548Sjkh if (*dn != ' ') { 2742893Sdfr *un++ = '.'; 2752893Sdfr thislong++; 276120492Sfjoe for (i = 3; i > 0 && *dn != ' ';) { 277278059Sdim c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), 278278059Sdim &i, lower & LCASE_EXT, pmp); 279227650Skevlo while (*c != '\0') { 280227650Skevlo *un++ = *c++; 281120492Sfjoe thislong++; 282120492Sfjoe } 2832893Sdfr } 2842893Sdfr } 2852893Sdfr *un++ = 0; 2862893Sdfr 28733548Sjkh return (thislong); 2882893Sdfr} 2892893Sdfr 2902893Sdfr/* 29133548Sjkh * Convert a unix filename to a DOS filename according to Win95 rules. 29233548Sjkh * If applicable and gen is not 0, it is inserted into the converted 29333548Sjkh * filename as a generation number. 29433548Sjkh * Returns 29533548Sjkh * 0 if name couldn't be converted 29633548Sjkh * 1 if the converted name is the same as the original 29733548Sjkh * (no long filename entry necessary for Win95) 29833548Sjkh * 2 if conversion was successful 29933548Sjkh * 3 if conversion was successful and generation number was inserted 3002893Sdfr */ 30133548Sjkhint 302120492Sfjoeunix2dosfn(un, dn, unlen, gen, pmp) 30333548Sjkh const u_char *un; 30433548Sjkh u_char dn[12]; 305125992Stjr size_t unlen; 30633548Sjkh u_int gen; 307120492Sfjoe struct msdosfsmount *pmp; 3082893Sdfr{ 309125992Stjr ssize_t i, j; 310125992Stjr int l; 31133548Sjkh int conv = 1; 31233548Sjkh const u_char *cp, *dp, *dp1; 31333548Sjkh u_char gentext[6], *wcp; 314120492Sfjoe u_int16_t c; 3152893Sdfr 3162893Sdfr /* 3172893Sdfr * Fill the dos filename string with blanks. These are DOS's pad 3182893Sdfr * characters. 3192893Sdfr */ 32033548Sjkh for (i = 0; i < 11; i++) 3212893Sdfr dn[i] = ' '; 32233548Sjkh dn[11] = 0; 3232893Sdfr 3242893Sdfr /* 3252893Sdfr * The filenames "." and ".." are handled specially, since they 3262893Sdfr * don't follow dos filename rules. 3272893Sdfr */ 3282893Sdfr if (un[0] == '.' && unlen == 1) { 3292893Sdfr dn[0] = '.'; 33033548Sjkh return gen <= 1; 3312893Sdfr } 3322893Sdfr if (un[0] == '.' && un[1] == '.' && unlen == 2) { 3332893Sdfr dn[0] = '.'; 3342893Sdfr dn[1] = '.'; 33533548Sjkh return gen <= 1; 3362893Sdfr } 3372893Sdfr 3382893Sdfr /* 33933548Sjkh * Filenames with only blanks and dots are not allowed! 3402893Sdfr */ 34133548Sjkh for (cp = un, i = unlen; --i >= 0; cp++) 34233548Sjkh if (*cp != ' ' && *cp != '.') 34333548Sjkh break; 34433548Sjkh if (i < 0) 34533548Sjkh return 0; 34633548Sjkh 34733872Smsmith 34833548Sjkh /* 34933872Smsmith * Filenames with some characters are not allowed! 35033872Smsmith */ 351120492Sfjoe for (cp = un, i = unlen; i > 0;) 352120492Sfjoe if (unix2doschr(&cp, (size_t *)&i, pmp) == 0) 35333872Smsmith return 0; 35433872Smsmith 35533872Smsmith /* 35633548Sjkh * Now find the extension 35733548Sjkh * Note: dot as first char doesn't start extension 35833548Sjkh * and trailing dots and blanks are ignored 359120492Sfjoe * Note(2003/7): It seems recent Windows has 360120492Sfjoe * defferent rule than this code, that Windows 361120492Sfjoe * ignores all dots before extension, and use all 362120492Sfjoe * chars as filename except for dots. 36333548Sjkh */ 36433548Sjkh dp = dp1 = 0; 36533548Sjkh for (cp = un + 1, i = unlen - 1; --i >= 0;) { 36633548Sjkh switch (*cp++) { 36733548Sjkh case '.': 36833548Sjkh if (!dp1) 36933548Sjkh dp1 = cp; 37033548Sjkh break; 37133548Sjkh case ' ': 37233548Sjkh break; 37333548Sjkh default: 37433548Sjkh if (dp1) 37533548Sjkh dp = dp1; 37633548Sjkh dp1 = 0; 37733548Sjkh break; 37833548Sjkh } 3792893Sdfr } 3802893Sdfr 3812893Sdfr /* 382148089Simura * Now convert it (this part is for extension). 383148089Simura * As Windows XP do, if it's not ascii char, 384148089Simura * this function should return 2 or 3, so that checkng out Unicode name. 3852893Sdfr */ 38633548Sjkh if (dp) { 38733548Sjkh if (dp1) 38833548Sjkh l = dp1 - dp; 38933548Sjkh else 39033548Sjkh l = unlen - (dp - un); 391120492Sfjoe for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) { 392120492Sfjoe c = unix2doschr(&cp, (size_t *)&i, pmp); 393120492Sfjoe if (c & 0xff00) { 394120492Sfjoe dn[j] = c >> 8; 395120492Sfjoe if (++j < 11) { 396120492Sfjoe dn[j] = c; 397148089Simura if (conv != 3) 398148089Simura conv = 2; 399120492Sfjoe continue; 400120492Sfjoe } else { 401120492Sfjoe conv = 3; 402120492Sfjoe dn[j-1] = ' '; 403120492Sfjoe break; 404120492Sfjoe } 405120492Sfjoe } else { 406120492Sfjoe dn[j] = c; 407120492Sfjoe } 408148089Simura if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3) 40933548Sjkh conv = 2; 41033872Smsmith if (dn[j] == 1) { 41133548Sjkh conv = 3; 41233872Smsmith dn[j] = '_'; 41333872Smsmith } 41433872Smsmith if (dn[j] == 2) { 41533872Smsmith conv = 3; 41633548Sjkh dn[j--] = ' '; 41733548Sjkh } 41833548Sjkh } 419120492Sfjoe if (i > 0) 42033548Sjkh conv = 3; 42133548Sjkh dp--; 42233548Sjkh } else { 42333548Sjkh for (dp = cp; *--dp == ' ' || *dp == '.';); 42433548Sjkh dp++; 42533548Sjkh } 42633548Sjkh 42733548Sjkh /* 42833548Sjkh * Now convert the rest of the name 42933548Sjkh */ 430120492Sfjoe for (i = dp - un, j = 0; un < dp && j < 8; j++) { 431125992Stjr c = unix2doschr(&un, &i, pmp); 432120492Sfjoe if (c & 0xff00) { 433120492Sfjoe dn[j] = c >> 8; 434120492Sfjoe if (++j < 8) { 435120492Sfjoe dn[j] = c; 436148089Simura if (conv != 3) 437148089Simura conv = 2; 438120492Sfjoe continue; 439120492Sfjoe } else { 440120492Sfjoe conv = 3; 441120492Sfjoe dn[j-1] = ' '; 442120492Sfjoe break; 443120492Sfjoe } 444120492Sfjoe } else { 445120492Sfjoe dn[j] = c; 446120492Sfjoe } 447148089Simura if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3) 44833548Sjkh conv = 2; 44933872Smsmith if (dn[j] == 1) { 45033548Sjkh conv = 3; 45133872Smsmith dn[j] = '_'; 45233872Smsmith } 45333872Smsmith if (dn[j] == 2) { 45433872Smsmith conv = 3; 45533548Sjkh dn[j--] = ' '; 45633548Sjkh } 45733548Sjkh } 45833548Sjkh if (un < dp) 45933548Sjkh conv = 3; 46033548Sjkh /* 46133548Sjkh * If we didn't have any chars in filename, 46233548Sjkh * generate a default 46333548Sjkh */ 46433548Sjkh if (!j) 46533548Sjkh dn[0] = '_'; 46633548Sjkh 46733548Sjkh /* 46833548Sjkh * If there wasn't any char dropped, 46933548Sjkh * there is no place for generation numbers 4702893Sdfr */ 47133548Sjkh if (conv != 3) { 47233548Sjkh if (gen > 1) 473120492Sfjoe conv = 0; 474120492Sfjoe goto done; 4752893Sdfr } 4762893Sdfr 4772893Sdfr /* 47833548Sjkh * Now insert the generation number into the filename part 4792893Sdfr */ 48036133Sdt if (gen == 0) 481120492Sfjoe goto done; 48233548Sjkh for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 48333548Sjkh *--wcp = gen % 10 + '0'; 484120492Sfjoe if (gen) { 485120492Sfjoe conv = 0; 486120492Sfjoe goto done; 487120492Sfjoe } 48833548Sjkh for (i = 8; dn[--i] == ' ';); 48933548Sjkh i++; 49033548Sjkh if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 49133548Sjkh i = 8 - (gentext + sizeof(gentext) - wcp + 1); 492120492Sfjoe /* 493120492Sfjoe * Correct posision to where insert the generation number 494120492Sfjoe */ 495120492Sfjoe cp = dn; 496120492Sfjoe i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u); 497120492Sfjoe 49833548Sjkh dn[i++] = '~'; 49933548Sjkh while (wcp < gentext + sizeof(gentext)) 50033548Sjkh dn[i++] = *wcp++; 501120492Sfjoe 502120492Sfjoe /* 503120492Sfjoe * Tail of the filename should be space 504120492Sfjoe */ 505120492Sfjoe while (i < 8) 506120492Sfjoe dn[i++] = ' '; 507120492Sfjoe conv = 3; 508120492Sfjoe 509120492Sfjoedone: 510120492Sfjoe /* 511120492Sfjoe * The first character cannot be E5, 512120492Sfjoe * because that means a deleted entry 513120492Sfjoe */ 514120492Sfjoe if (dn[0] == 0xe5) 515120492Sfjoe dn[0] = SLOT_E5; 516120492Sfjoe 517120492Sfjoe return conv; 51833548Sjkh} 51933548Sjkh 52033548Sjkh/* 52133548Sjkh * Create a Win95 long name directory entry 52233548Sjkh * Note: assumes that the filename is valid, 52333548Sjkh * i.e. doesn't consist solely of blanks and dots 52433548Sjkh */ 52533548Sjkhint 526120492Sfjoeunix2winfn(un, unlen, wep, cnt, chksum, pmp) 52733548Sjkh const u_char *un; 528125992Stjr size_t unlen; 52933548Sjkh struct winentry *wep; 53033548Sjkh int cnt; 53133548Sjkh int chksum; 532120492Sfjoe struct msdosfsmount *pmp; 53333548Sjkh{ 53433548Sjkh u_int8_t *wcp; 535120492Sfjoe int i, end; 53633747Sache u_int16_t code; 53733548Sjkh 53833548Sjkh /* 53933548Sjkh * Drop trailing blanks and dots 54033548Sjkh */ 541120492Sfjoe unlen = winLenFixup(un, unlen); 54233548Sjkh 543120492Sfjoe /* 544120492Sfjoe * Cut *un for this slot 545120492Sfjoe */ 546120492Sfjoe unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2, 547120492Sfjoe pmp->pm_flags, pmp->pm_u2w); 54833548Sjkh 54933548Sjkh /* 55033548Sjkh * Initialize winentry to some useful default 55133548Sjkh */ 55233548Sjkh for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 55333548Sjkh wep->weCnt = cnt; 55433548Sjkh wep->weAttributes = ATTR_WIN95; 55533548Sjkh wep->weReserved1 = 0; 55633548Sjkh wep->weChksum = chksum; 55733548Sjkh wep->weReserved2 = 0; 55833548Sjkh 55933548Sjkh /* 56033548Sjkh * Now convert the filename parts 56133548Sjkh */ 562120492Sfjoe end = 0; 563120492Sfjoe for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) { 564125992Stjr code = unix2winchr(&un, &unlen, 0, pmp); 565120492Sfjoe *wcp++ = code; 566120492Sfjoe *wcp++ = code >> 8; 567120492Sfjoe if (!code) 568120492Sfjoe end = WIN_LAST; 5692893Sdfr } 570120492Sfjoe for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) { 571125992Stjr code = unix2winchr(&un, &unlen, 0, pmp); 572120492Sfjoe *wcp++ = code; 573120492Sfjoe *wcp++ = code >> 8; 574120492Sfjoe if (!code) 575120492Sfjoe end = WIN_LAST; 57633548Sjkh } 577120492Sfjoe for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) { 578125992Stjr code = unix2winchr(&un, &unlen, 0, pmp); 579120492Sfjoe *wcp++ = code; 580120492Sfjoe *wcp++ = code >> 8; 581120492Sfjoe if (!code) 582120492Sfjoe end = WIN_LAST; 58333548Sjkh } 584333611Spfg if (!unlen) 585120492Sfjoe end = WIN_LAST; 586120492Sfjoe wep->weCnt |= end; 587120492Sfjoe return !end; 5882893Sdfr} 5892893Sdfr 5902893Sdfr/* 59133548Sjkh * Compare our filename to the one in the Win95 entry 59233548Sjkh * Returns the checksum or -1 if no match 5932893Sdfr */ 59433548Sjkhint 595172027SbdewinChkName(nbp, un, unlen, chksum, pmp) 596172027Sbde struct mbnambuf *nbp; 59733548Sjkh const u_char *un; 598125992Stjr size_t unlen; 59933548Sjkh int chksum; 600120492Sfjoe struct msdosfsmount *pmp; 60133548Sjkh{ 602125992Stjr size_t len; 603120492Sfjoe u_int16_t c1, c2; 604120492Sfjoe u_char *np; 605120492Sfjoe struct dirent dirbuf; 60633548Sjkh 60733548Sjkh /* 608172027Sbde * We already have winentry in *nbp. 60933548Sjkh */ 610172027Sbde if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0) 61133548Sjkh return -1; 61233548Sjkh 613120492Sfjoe#ifdef MSDOSFS_DEBUG 614120492Sfjoe printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen, 615120492Sfjoe dirbuf.d_name, 616120492Sfjoe dirbuf.d_namlen); 617120492Sfjoe#endif 618106110Ssemenu 619106110Ssemenu /* 620120492Sfjoe * Compare the name parts 621106110Ssemenu */ 622120492Sfjoe len = dirbuf.d_namlen; 623120492Sfjoe if (unlen != len) 624120492Sfjoe return -2; 62533548Sjkh 626120492Sfjoe for (np = dirbuf.d_name; unlen > 0 && len > 0;) { 627120492Sfjoe /* 628123293Sfjoe * Comparison must be case insensitive, because FAT disallows 629123293Sfjoe * to look up or create files in case sensitive even when 630123293Sfjoe * it's a long file name. 631120492Sfjoe */ 632278059Sdim c1 = unix2winchr(__DECONST(const u_char **, &np), &len, 633278059Sdim LCASE_BASE, pmp); 634125992Stjr c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp); 63533760Sache if (c1 != c2) 636120492Sfjoe return -2; 63733548Sjkh } 63833548Sjkh return chksum; 63933548Sjkh} 64033548Sjkh 64133548Sjkh/* 64233548Sjkh * Convert Win95 filename to dirbuf. 64333548Sjkh * Returns the checksum or -1 if impossible 64433548Sjkh */ 64533548Sjkhint 646172027Sbdewin2unixfn(nbp, wep, chksum, pmp) 647172027Sbde struct mbnambuf *nbp; 64833548Sjkh struct winentry *wep; 64933548Sjkh int chksum; 650120492Sfjoe struct msdosfsmount *pmp; 65133548Sjkh{ 652228796Skevlo u_char *c, tmpbuf[5]; 65333548Sjkh u_int8_t *cp; 654227650Skevlo u_int8_t *np, name[WIN_CHARS * 3 + 1]; 65533744Sache u_int16_t code; 65633548Sjkh int i; 65733548Sjkh 65833548Sjkh if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 65933548Sjkh || !(wep->weCnt&WIN_CNT)) 66033548Sjkh return -1; 66133548Sjkh 66233548Sjkh /* 66333548Sjkh * First compare checksums 66433548Sjkh */ 66533548Sjkh if (wep->weCnt&WIN_LAST) { 66633548Sjkh chksum = wep->weChksum; 66733548Sjkh } else if (chksum != wep->weChksum) 66833548Sjkh chksum = -1; 66933548Sjkh if (chksum == -1) 67033548Sjkh return -1; 67133548Sjkh 67233548Sjkh /* 67333548Sjkh * Convert the name parts 67433548Sjkh */ 675120492Sfjoe np = name; 67633548Sjkh for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 67733744Sache code = (cp[1] << 8) | cp[0]; 67833744Sache switch (code) { 67933548Sjkh case 0: 68033744Sache *np = '\0'; 681298799Skp if (mbnambuf_write(nbp, name, 682298799Skp (wep->weCnt & WIN_CNT) - 1) != 0) 683298799Skp return -1; 68433548Sjkh return chksum; 68533548Sjkh case '/': 68633744Sache *np = '\0'; 68733548Sjkh return -1; 68833744Sache default: 689228796Skevlo c = win2unixchr(tmpbuf, code, pmp); 690227650Skevlo while (*c != '\0') 691227650Skevlo *np++ = *c++; 69233744Sache break; 69333548Sjkh } 69433744Sache cp += 2; 69533548Sjkh } 69633548Sjkh for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 69733744Sache code = (cp[1] << 8) | cp[0]; 69833744Sache switch (code) { 69933548Sjkh case 0: 70033744Sache *np = '\0'; 701298799Skp if (mbnambuf_write(nbp, name, 702298799Skp (wep->weCnt & WIN_CNT) - 1) != 0) 703298799Skp return -1; 70433548Sjkh return chksum; 70533548Sjkh case '/': 70633744Sache *np = '\0'; 70733548Sjkh return -1; 70833744Sache default: 709228796Skevlo c = win2unixchr(tmpbuf, code, pmp); 710227650Skevlo while (*c != '\0') 711227650Skevlo *np++ = *c++; 71233744Sache break; 71333548Sjkh } 71433744Sache cp += 2; 71533548Sjkh } 71633548Sjkh for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 71733744Sache code = (cp[1] << 8) | cp[0]; 71833744Sache switch (code) { 71933548Sjkh case 0: 72033744Sache *np = '\0'; 721298799Skp if (mbnambuf_write(nbp, name, 722298799Skp (wep->weCnt & WIN_CNT) - 1) != 0) 723298799Skp return -1; 72433548Sjkh return chksum; 72533548Sjkh case '/': 72633744Sache *np = '\0'; 72733548Sjkh return -1; 72833744Sache default: 729228796Skevlo c = win2unixchr(tmpbuf, code, pmp); 730227650Skevlo while (*c != '\0') 731227650Skevlo *np++ = *c++; 73233744Sache break; 73333548Sjkh } 73433744Sache cp += 2; 73533548Sjkh } 736120492Sfjoe *np = '\0'; 737298799Skp if (mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1) != 0) 738298799Skp return -1; 73933548Sjkh return chksum; 74033548Sjkh} 74133548Sjkh 74233548Sjkh/* 743141497Snjl * Compute the unrolled checksum of a DOS filename for Win95 LFN use. 74433548Sjkh */ 74533548Sjkhu_int8_t 746203827SkibwinChksum(u_int8_t *name) 74733548Sjkh{ 748203827Skib int i; 74933548Sjkh u_int8_t s; 75033548Sjkh 751203827Skib for (s = 0, i = 11; --i >= 0; s += *name++) 752203827Skib s = (s << 7)|(s >> 1); 753141497Snjl return (s); 75433548Sjkh} 75533548Sjkh 75633548Sjkh/* 75733548Sjkh * Determine the number of slots necessary for Win95 names 75833548Sjkh */ 75933548Sjkhint 760120492SfjoewinSlotCnt(un, unlen, pmp) 76133548Sjkh const u_char *un; 762125992Stjr size_t unlen; 763120492Sfjoe struct msdosfsmount *pmp; 76433548Sjkh{ 765120492Sfjoe size_t wlen; 766120492Sfjoe char wn[WIN_MAXLEN * 2 + 1], *wnp; 767120492Sfjoe 76833848Smsmith unlen = winLenFixup(un, unlen); 769120492Sfjoe 770120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 771120492Sfjoe wlen = WIN_MAXLEN * 2; 772120492Sfjoe wnp = wn; 773125992Stjr msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen); 774120492Sfjoe if (unlen > 0) 775120492Sfjoe return 0; 776120492Sfjoe return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS); 777120492Sfjoe } 778120492Sfjoe 77933548Sjkh if (unlen > WIN_MAXLEN) 78033548Sjkh return 0; 78133548Sjkh return howmany(unlen, WIN_CHARS); 78233548Sjkh} 78333848Smsmith 78433848Smsmith/* 785249583Sgabor * Determine the number of bytes neccessary for Win95 names 78633848Smsmith */ 787125992Stjrsize_t 78833848SmsmithwinLenFixup(un, unlen) 78933848Smsmith const u_char* un; 790125992Stjr size_t unlen; 79133848Smsmith{ 79233848Smsmith for (un += unlen; unlen > 0; unlen--) 79333848Smsmith if (*--un != ' ' && *un != '.') 79433848Smsmith break; 79533848Smsmith return unlen; 79633848Smsmith} 797120492Sfjoe 798120492Sfjoe/* 799249588Sgabor * Store an area with multi byte string instr, and returns left 800120492Sfjoe * byte of instr and moves pointer forward. The area's size is 801120492Sfjoe * inlen or outlen. 802120492Sfjoe */ 803120492Sfjoestatic int 804125992Stjrmbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle) 805120492Sfjoe{ 806120492Sfjoe char *outp, outstr[outlen * weight + 1]; 807120492Sfjoe 808120492Sfjoe if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) { 809120492Sfjoe outp = outstr; 810120492Sfjoe outlen *= weight; 811125992Stjr msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen); 812120492Sfjoe return (inlen); 813120492Sfjoe } 814120492Sfjoe 815120492Sfjoe (*instr) += min(inlen, outlen); 816120492Sfjoe return (inlen - min(inlen, outlen)); 817120492Sfjoe} 818120492Sfjoe 819120492Sfjoe/* 820120492Sfjoe * Convert DOS char to Local char 821120492Sfjoe */ 822227650Skevlostatic u_char * 823228796Skevlodos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 824120492Sfjoe{ 825228796Skevlo u_char c, *outp; 826120492Sfjoe size_t len, olen; 827120492Sfjoe 828227650Skevlo outp = outbuf; 829120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 830227650Skevlo olen = len = 4; 831120492Sfjoe 832120492Sfjoe if (lower & (LCASE_BASE | LCASE_EXT)) 833120492Sfjoe msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr, 834227650Skevlo ilen, (char **)&outp, &olen, KICONV_LOWER); 835120492Sfjoe else 836120492Sfjoe msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr, 837227650Skevlo ilen, (char **)&outp, &olen); 838120492Sfjoe len -= olen; 839120492Sfjoe 840120492Sfjoe /* 841120492Sfjoe * return '?' if failed to convert 842120492Sfjoe */ 843120492Sfjoe if (len == 0) { 844120492Sfjoe (*ilen)--; 845120492Sfjoe (*instr)++; 846227650Skevlo *outp++ = '?'; 847120492Sfjoe } 848227650Skevlo } else { 849227650Skevlo (*ilen)--; 850227650Skevlo c = *(*instr)++; 851227650Skevlo c = dos2unix[c]; 852227650Skevlo if (lower & (LCASE_BASE | LCASE_EXT)) 853227650Skevlo c = u2l[c]; 854227650Skevlo *outp++ = c; 855227650Skevlo outbuf[1] = '\0'; 856120492Sfjoe } 857120492Sfjoe 858227650Skevlo *outp = '\0'; 859227650Skevlo outp = outbuf; 860227650Skevlo return (outp); 861120492Sfjoe} 862120492Sfjoe 863120492Sfjoe/* 864120492Sfjoe * Convert Local char to DOS char 865120492Sfjoe */ 866120492Sfjoestatic u_int16_t 867120492Sfjoeunix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp) 868120492Sfjoe{ 869120492Sfjoe u_char c; 870120492Sfjoe char *up, *outp, unicode[3], outbuf[3]; 871120492Sfjoe u_int16_t wc; 872148089Simura size_t len, ucslen, unixlen, olen; 873120492Sfjoe 874120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 875120492Sfjoe /* 876120492Sfjoe * to hide an invisible character, using a unicode filter 877120492Sfjoe */ 878148089Simura ucslen = 2; 879120492Sfjoe len = *ilen; 880120492Sfjoe up = unicode; 881120492Sfjoe msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 882148089Simura ilen, &up, &ucslen); 883148089Simura unixlen = len - *ilen; 884120492Sfjoe 885120492Sfjoe /* 886120492Sfjoe * cannot be converted 887120492Sfjoe */ 888148089Simura if (unixlen == 0) { 889120492Sfjoe (*ilen)--; 890120492Sfjoe (*instr)++; 891120492Sfjoe return (0); 892120492Sfjoe } 893120492Sfjoe 894120492Sfjoe /* 895120492Sfjoe * return magic number for ascii char 896120492Sfjoe */ 897148089Simura if (unixlen == 1) { 898120492Sfjoe c = *(*instr -1); 899120492Sfjoe if (! (c & 0x80)) { 900120492Sfjoe c = unix2dos[c]; 901120492Sfjoe if (c <= 2) 902120492Sfjoe return (c); 903120492Sfjoe } 904120492Sfjoe } 905120492Sfjoe 906120492Sfjoe /* 907120492Sfjoe * now convert using libiconv 908120492Sfjoe */ 909148089Simura *instr -= unixlen; 910148089Simura *ilen = len; 911120492Sfjoe 912120492Sfjoe olen = len = 2; 913120492Sfjoe outp = outbuf; 914120492Sfjoe msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr, 915120492Sfjoe ilen, &outp, &olen, KICONV_FROM_UPPER); 916120492Sfjoe len -= olen; 917148089Simura 918148089Simura /* 919148089Simura * cannot be converted, but has unicode char, should return magic number 920148089Simura */ 921148089Simura if (len == 0) { 922148089Simura (*ilen) -= unixlen; 923148089Simura (*instr) += unixlen; 924148089Simura return (1); 925148089Simura } 926148089Simura 927120492Sfjoe wc = 0; 928120492Sfjoe while(len--) 929120492Sfjoe wc |= (*(outp - len - 1) & 0xff) << (len << 3); 930120492Sfjoe return (wc); 931120492Sfjoe } 932120492Sfjoe 933120492Sfjoe (*ilen)--; 934120492Sfjoe c = *(*instr)++; 935120492Sfjoe c = l2u[c]; 936120492Sfjoe c = unix2dos[c]; 937120492Sfjoe return ((u_int16_t)c); 938120492Sfjoe} 939120492Sfjoe 940120492Sfjoe/* 941120492Sfjoe * Convert Windows char to Local char 942120492Sfjoe */ 943227650Skevlostatic u_char * 944228796Skevlowin2unixchr(u_char *outbuf, u_int16_t wc, struct msdosfsmount *pmp) 945120492Sfjoe{ 946228796Skevlo u_char *inp, *outp, inbuf[3]; 947120492Sfjoe size_t ilen, olen, len; 948120492Sfjoe 949227650Skevlo outp = outbuf; 950120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 951120492Sfjoe inbuf[0] = (u_char)(wc>>8); 952120492Sfjoe inbuf[1] = (u_char)wc; 953120492Sfjoe inbuf[2] = '\0'; 954120492Sfjoe 955227650Skevlo ilen = 2; 956227650Skevlo olen = len = 4; 957120492Sfjoe inp = inbuf; 958278059Sdim msdosfs_iconv->convchr(pmp->pm_w2u, __DECONST(const char **, 959278059Sdim &inp), &ilen, (char **)&outp, &olen); 960120492Sfjoe len -= olen; 961120492Sfjoe 962120492Sfjoe /* 963120492Sfjoe * return '?' if failed to convert 964120492Sfjoe */ 965227650Skevlo if (len == 0) 966227650Skevlo *outp++ = '?'; 967227650Skevlo } else { 968227650Skevlo *outp++ = (wc & 0xff00) ? '?' : (u_char)(wc & 0xff); 969120492Sfjoe } 970120492Sfjoe 971227650Skevlo *outp = '\0'; 972227650Skevlo outp = outbuf; 973227650Skevlo return (outp); 974120492Sfjoe} 975120492Sfjoe 976120492Sfjoe/* 977120492Sfjoe * Convert Local char to Windows char 978120492Sfjoe */ 979120492Sfjoestatic u_int16_t 980120492Sfjoeunix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 981120492Sfjoe{ 982120492Sfjoe u_char *outp, outbuf[3]; 983120492Sfjoe u_int16_t wc; 984120492Sfjoe size_t olen; 985120492Sfjoe 986120492Sfjoe if (*ilen == 0) 987120492Sfjoe return (0); 988120492Sfjoe 989120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 990120492Sfjoe outp = outbuf; 991120492Sfjoe olen = 2; 992120492Sfjoe if (lower & (LCASE_BASE | LCASE_EXT)) 993120492Sfjoe msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr, 994120492Sfjoe ilen, (char **)&outp, &olen, 995120492Sfjoe KICONV_FROM_LOWER); 996120492Sfjoe else 997120492Sfjoe msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 998120492Sfjoe ilen, (char **)&outp, &olen); 999120492Sfjoe 1000120492Sfjoe /* 1001120492Sfjoe * return '0' if end of filename 1002120492Sfjoe */ 1003120492Sfjoe if (olen == 2) 1004120492Sfjoe return (0); 1005120492Sfjoe 1006120492Sfjoe wc = (outbuf[0]<<8) | outbuf[1]; 1007120492Sfjoe 1008120492Sfjoe return (wc); 1009120492Sfjoe } 1010120492Sfjoe 1011120492Sfjoe (*ilen)--; 1012120492Sfjoe wc = (*instr)[0]; 1013120492Sfjoe if (lower & (LCASE_BASE | LCASE_EXT)) 1014120492Sfjoe wc = u2l[wc]; 1015120492Sfjoe (*instr)++; 1016120492Sfjoe return (wc); 1017120492Sfjoe} 1018120492Sfjoe 1019120492Sfjoe/* 1020172027Sbde * Initialize the temporary concatenation buffer. 1021120492Sfjoe */ 1022120492Sfjoevoid 1023172027Sbdembnambuf_init(struct mbnambuf *nbp) 1024120492Sfjoe{ 1025120492Sfjoe 1026172027Sbde nbp->nb_len = 0; 1027172027Sbde nbp->nb_last_id = -1; 1028172027Sbde nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0'; 1029120492Sfjoe} 1030120492Sfjoe 1031120492Sfjoe/* 1032145131Snjl * Fill out our concatenation buffer with the given substring, at the offset 1033145131Snjl * specified by its id. Since this function must be called with ids in 1034145131Snjl * descending order, we take advantage of the fact that ASCII substrings are 1035145131Snjl * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all 1036145131Snjl * previous (i.e. higher id) substrings upwards to make room for this one. 1037145131Snjl * This only penalizes portions of substrings that contain more than 1038145131Snjl * WIN_CHARS bytes when they are first encountered. 1039120492Sfjoe */ 1040298799Skpint 1041172027Sbdembnambuf_write(struct mbnambuf *nbp, char *name, int id) 1042120492Sfjoe{ 1043145131Snjl char *slot; 1044172027Sbde size_t count, newlen; 1045143435Snjl 1046187199Strasz if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) { 1047227817Skib#ifdef MSDOSFS_DEBUG 1048187199Strasz printf("msdosfs: non-decreasing id: id %d, last id %d\n", 1049187199Strasz id, nbp->nb_last_id); 1050227817Skib#endif 1051298799Skp return (EINVAL); 1052187199Strasz } 1053145131Snjl 1054172027Sbde /* Will store this substring in a WIN_CHARS-aligned slot. */ 1055172027Sbde slot = &nbp->nb_buf[id * WIN_CHARS]; 1056145131Snjl count = strlen(name); 1057172027Sbde newlen = nbp->nb_len + count; 1058172027Sbde if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) { 1059227817Skib#ifdef MSDOSFS_DEBUG 1060172027Sbde printf("msdosfs: file name length %zu too large\n", newlen); 1061227817Skib#endif 1062298799Skp return (ENAMETOOLONG); 1063120492Sfjoe } 1064145131Snjl 1065145131Snjl /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */ 1066298799Skp if (count > WIN_CHARS && nbp->nb_len != 0) { 1067298799Skp if ((id * WIN_CHARS + count + nbp->nb_len) > 1068298799Skp sizeof(nbp->nb_buf)) 1069298799Skp return (ENAMETOOLONG); 1070298799Skp 1071172027Sbde bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len); 1072298799Skp } 1073145131Snjl 1074145131Snjl /* Copy in the substring to its slot and update length so far. */ 1075145131Snjl bcopy(name, slot, count); 1076172027Sbde nbp->nb_len = newlen; 1077172027Sbde nbp->nb_last_id = id; 1078298799Skp 1079298799Skp return (0); 1080120492Sfjoe} 1081120492Sfjoe 1082120492Sfjoe/* 1083143435Snjl * Take the completed string and use it to setup the struct dirent. 1084143435Snjl * Be sure to always nul-terminate the d_name and then copy the string 1085143435Snjl * from our buffer. Note that this function assumes the full string has 1086143435Snjl * been reassembled in the buffer. If it's called before all substrings 1087143435Snjl * have been written via mbnambuf_write(), the result will be incorrect. 1088120492Sfjoe */ 1089120492Sfjoechar * 1090172027Sbdembnambuf_flush(struct mbnambuf *nbp, struct dirent *dp) 1091120492Sfjoe{ 1092120492Sfjoe 1093172027Sbde if (nbp->nb_len > sizeof(dp->d_name) - 1) { 1094172027Sbde mbnambuf_init(nbp); 1095143435Snjl return (NULL); 1096120492Sfjoe } 1097172027Sbde bcopy(&nbp->nb_buf[0], dp->d_name, nbp->nb_len); 1098172027Sbde dp->d_name[nbp->nb_len] = '\0'; 1099172027Sbde dp->d_namlen = nbp->nb_len; 1100143435Snjl 1101172027Sbde mbnambuf_init(nbp); 1102120492Sfjoe return (dp->d_name); 1103120492Sfjoe} 1104