150477Speter/* $FreeBSD: stable/11/sys/fs/msdosfs/msdosfs_conv.c 333610 2018-05-14 19:20:37Z 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 237276887Semastedos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp) 2382893Sdfr{ 239125992Stjr size_t i; 240120492Sfjoe int thislong = 0; 241228796Skevlo u_char *c, tmpbuf[5]; 2422893Sdfr 2432893Sdfr /* 24433548Sjkh * If first char of the filename is SLOT_E5 (0x05), then the real 24533548Sjkh * first char of the filename should be 0xe5. But, they couldn't 24633548Sjkh * just have a 0xe5 mean 0xe5 because that is used to mean a freed 24733548Sjkh * directory slot. Another dos quirk. 2482893Sdfr */ 24933548Sjkh if (*dn == SLOT_E5) 250120492Sfjoe *dn = 0xe5; 2512893Sdfr 2522893Sdfr /* 25333548Sjkh * Copy the name portion into the unix filename string. 2542893Sdfr */ 255120492Sfjoe for (i = 8; i > 0 && *dn != ' ';) { 256277898Sdim c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), &i, 257228796Skevlo lower & LCASE_BASE, pmp); 258227650Skevlo while (*c != '\0') { 259227650Skevlo *un++ = *c++; 260120492Sfjoe thislong++; 261120492Sfjoe } 2622893Sdfr } 263120492Sfjoe dn += i; 2642893Sdfr 2652893Sdfr /* 2662893Sdfr * Now, if there is an extension then put in a period and copy in 2672893Sdfr * the extension. 2682893Sdfr */ 26933548Sjkh if (*dn != ' ') { 2702893Sdfr *un++ = '.'; 2712893Sdfr thislong++; 272120492Sfjoe for (i = 3; i > 0 && *dn != ' ';) { 273277898Sdim c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), 274277898Sdim &i, lower & LCASE_EXT, pmp); 275227650Skevlo while (*c != '\0') { 276227650Skevlo *un++ = *c++; 277120492Sfjoe thislong++; 278120492Sfjoe } 2792893Sdfr } 2802893Sdfr } 2812893Sdfr *un++ = 0; 2822893Sdfr 28333548Sjkh return (thislong); 2842893Sdfr} 2852893Sdfr 2862893Sdfr/* 28733548Sjkh * Convert a unix filename to a DOS filename according to Win95 rules. 28833548Sjkh * If applicable and gen is not 0, it is inserted into the converted 28933548Sjkh * filename as a generation number. 29033548Sjkh * Returns 29133548Sjkh * 0 if name couldn't be converted 29233548Sjkh * 1 if the converted name is the same as the original 29333548Sjkh * (no long filename entry necessary for Win95) 29433548Sjkh * 2 if conversion was successful 29533548Sjkh * 3 if conversion was successful and generation number was inserted 2962893Sdfr */ 29733548Sjkhint 298277092Semasteunix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen, 299277092Semaste struct msdosfsmount *pmp) 3002893Sdfr{ 301125992Stjr ssize_t i, j; 302125992Stjr int l; 30333548Sjkh int conv = 1; 30433548Sjkh const u_char *cp, *dp, *dp1; 30533548Sjkh u_char gentext[6], *wcp; 306120492Sfjoe u_int16_t c; 3072893Sdfr 3082893Sdfr /* 3092893Sdfr * Fill the dos filename string with blanks. These are DOS's pad 3102893Sdfr * characters. 3112893Sdfr */ 31233548Sjkh for (i = 0; i < 11; i++) 3132893Sdfr dn[i] = ' '; 31433548Sjkh dn[11] = 0; 3152893Sdfr 3162893Sdfr /* 3172893Sdfr * The filenames "." and ".." are handled specially, since they 3182893Sdfr * don't follow dos filename rules. 3192893Sdfr */ 3202893Sdfr if (un[0] == '.' && unlen == 1) { 3212893Sdfr dn[0] = '.'; 32233548Sjkh return gen <= 1; 3232893Sdfr } 3242893Sdfr if (un[0] == '.' && un[1] == '.' && unlen == 2) { 3252893Sdfr dn[0] = '.'; 3262893Sdfr dn[1] = '.'; 32733548Sjkh return gen <= 1; 3282893Sdfr } 3292893Sdfr 3302893Sdfr /* 33133548Sjkh * Filenames with only blanks and dots are not allowed! 3322893Sdfr */ 33333548Sjkh for (cp = un, i = unlen; --i >= 0; cp++) 33433548Sjkh if (*cp != ' ' && *cp != '.') 33533548Sjkh break; 33633548Sjkh if (i < 0) 33733548Sjkh return 0; 33833548Sjkh 33933872Smsmith 34033548Sjkh /* 34133872Smsmith * Filenames with some characters are not allowed! 34233872Smsmith */ 343120492Sfjoe for (cp = un, i = unlen; i > 0;) 344120492Sfjoe if (unix2doschr(&cp, (size_t *)&i, pmp) == 0) 34533872Smsmith return 0; 34633872Smsmith 34733872Smsmith /* 34833548Sjkh * Now find the extension 34933548Sjkh * Note: dot as first char doesn't start extension 35033548Sjkh * and trailing dots and blanks are ignored 351120492Sfjoe * Note(2003/7): It seems recent Windows has 352120492Sfjoe * defferent rule than this code, that Windows 353120492Sfjoe * ignores all dots before extension, and use all 354120492Sfjoe * chars as filename except for dots. 35533548Sjkh */ 356298074Spfg dp = dp1 = NULL; 35733548Sjkh for (cp = un + 1, i = unlen - 1; --i >= 0;) { 35833548Sjkh switch (*cp++) { 35933548Sjkh case '.': 36033548Sjkh if (!dp1) 36133548Sjkh dp1 = cp; 36233548Sjkh break; 36333548Sjkh case ' ': 36433548Sjkh break; 36533548Sjkh default: 36633548Sjkh if (dp1) 36733548Sjkh dp = dp1; 368298074Spfg dp1 = NULL; 36933548Sjkh break; 37033548Sjkh } 3712893Sdfr } 3722893Sdfr 3732893Sdfr /* 374148089Simura * Now convert it (this part is for extension). 375148089Simura * As Windows XP do, if it's not ascii char, 376148089Simura * this function should return 2 or 3, so that checkng out Unicode name. 3772893Sdfr */ 37833548Sjkh if (dp) { 37933548Sjkh if (dp1) 38033548Sjkh l = dp1 - dp; 38133548Sjkh else 38233548Sjkh l = unlen - (dp - un); 383120492Sfjoe for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) { 384120492Sfjoe c = unix2doschr(&cp, (size_t *)&i, pmp); 385120492Sfjoe if (c & 0xff00) { 386120492Sfjoe dn[j] = c >> 8; 387120492Sfjoe if (++j < 11) { 388120492Sfjoe dn[j] = c; 389148089Simura if (conv != 3) 390148089Simura conv = 2; 391120492Sfjoe continue; 392120492Sfjoe } else { 393120492Sfjoe conv = 3; 394120492Sfjoe dn[j-1] = ' '; 395120492Sfjoe break; 396120492Sfjoe } 397120492Sfjoe } else { 398120492Sfjoe dn[j] = c; 399120492Sfjoe } 400148089Simura if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3) 40133548Sjkh conv = 2; 40233872Smsmith if (dn[j] == 1) { 40333548Sjkh conv = 3; 40433872Smsmith dn[j] = '_'; 40533872Smsmith } 40633872Smsmith if (dn[j] == 2) { 40733872Smsmith conv = 3; 40833548Sjkh dn[j--] = ' '; 40933548Sjkh } 41033548Sjkh } 411120492Sfjoe if (i > 0) 41233548Sjkh conv = 3; 41333548Sjkh dp--; 41433548Sjkh } else { 41533548Sjkh for (dp = cp; *--dp == ' ' || *dp == '.';); 41633548Sjkh dp++; 41733548Sjkh } 41833548Sjkh 41933548Sjkh /* 42033548Sjkh * Now convert the rest of the name 42133548Sjkh */ 422120492Sfjoe for (i = dp - un, j = 0; un < dp && j < 8; j++) { 423125992Stjr c = unix2doschr(&un, &i, pmp); 424120492Sfjoe if (c & 0xff00) { 425120492Sfjoe dn[j] = c >> 8; 426120492Sfjoe if (++j < 8) { 427120492Sfjoe dn[j] = c; 428148089Simura if (conv != 3) 429148089Simura conv = 2; 430120492Sfjoe continue; 431120492Sfjoe } else { 432120492Sfjoe conv = 3; 433120492Sfjoe dn[j-1] = ' '; 434120492Sfjoe break; 435120492Sfjoe } 436120492Sfjoe } else { 437120492Sfjoe dn[j] = c; 438120492Sfjoe } 439148089Simura if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3) 44033548Sjkh conv = 2; 44133872Smsmith if (dn[j] == 1) { 44233548Sjkh conv = 3; 44333872Smsmith dn[j] = '_'; 44433872Smsmith } 44533872Smsmith if (dn[j] == 2) { 44633872Smsmith conv = 3; 44733548Sjkh dn[j--] = ' '; 44833548Sjkh } 44933548Sjkh } 45033548Sjkh if (un < dp) 45133548Sjkh conv = 3; 45233548Sjkh /* 45333548Sjkh * If we didn't have any chars in filename, 45433548Sjkh * generate a default 45533548Sjkh */ 45633548Sjkh if (!j) 45733548Sjkh dn[0] = '_'; 45833548Sjkh 45933548Sjkh /* 46033548Sjkh * If there wasn't any char dropped, 46133548Sjkh * there is no place for generation numbers 4622893Sdfr */ 46333548Sjkh if (conv != 3) { 46433548Sjkh if (gen > 1) 465120492Sfjoe conv = 0; 466120492Sfjoe goto done; 4672893Sdfr } 4682893Sdfr 4692893Sdfr /* 47033548Sjkh * Now insert the generation number into the filename part 4712893Sdfr */ 47236133Sdt if (gen == 0) 473120492Sfjoe goto done; 47433548Sjkh for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 47533548Sjkh *--wcp = gen % 10 + '0'; 476120492Sfjoe if (gen) { 477120492Sfjoe conv = 0; 478120492Sfjoe goto done; 479120492Sfjoe } 48033548Sjkh for (i = 8; dn[--i] == ' ';); 48133548Sjkh i++; 48233548Sjkh if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 48333548Sjkh i = 8 - (gentext + sizeof(gentext) - wcp + 1); 484120492Sfjoe /* 485120492Sfjoe * Correct posision to where insert the generation number 486120492Sfjoe */ 487120492Sfjoe cp = dn; 488120492Sfjoe i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u); 489120492Sfjoe 49033548Sjkh dn[i++] = '~'; 49133548Sjkh while (wcp < gentext + sizeof(gentext)) 49233548Sjkh dn[i++] = *wcp++; 493120492Sfjoe 494120492Sfjoe /* 495120492Sfjoe * Tail of the filename should be space 496120492Sfjoe */ 497120492Sfjoe while (i < 8) 498120492Sfjoe dn[i++] = ' '; 499120492Sfjoe conv = 3; 500120492Sfjoe 501120492Sfjoedone: 502120492Sfjoe /* 503120492Sfjoe * The first character cannot be E5, 504120492Sfjoe * because that means a deleted entry 505120492Sfjoe */ 506120492Sfjoe if (dn[0] == 0xe5) 507120492Sfjoe dn[0] = SLOT_E5; 508120492Sfjoe 509120492Sfjoe return conv; 51033548Sjkh} 51133548Sjkh 51233548Sjkh/* 51333548Sjkh * Create a Win95 long name directory entry 51433548Sjkh * Note: assumes that the filename is valid, 51533548Sjkh * i.e. doesn't consist solely of blanks and dots 51633548Sjkh */ 51733548Sjkhint 518277092Semasteunix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt, 519277092Semaste int chksum, struct msdosfsmount *pmp) 52033548Sjkh{ 52133548Sjkh u_int8_t *wcp; 522120492Sfjoe int i, end; 52333747Sache u_int16_t code; 52433548Sjkh 52533548Sjkh /* 52633548Sjkh * Drop trailing blanks and dots 52733548Sjkh */ 528120492Sfjoe unlen = winLenFixup(un, unlen); 52933548Sjkh 530120492Sfjoe /* 531120492Sfjoe * Cut *un for this slot 532120492Sfjoe */ 533120492Sfjoe unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2, 534120492Sfjoe pmp->pm_flags, pmp->pm_u2w); 53533548Sjkh 53633548Sjkh /* 53733548Sjkh * Initialize winentry to some useful default 53833548Sjkh */ 53933548Sjkh for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 54033548Sjkh wep->weCnt = cnt; 54133548Sjkh wep->weAttributes = ATTR_WIN95; 54233548Sjkh wep->weReserved1 = 0; 54333548Sjkh wep->weChksum = chksum; 54433548Sjkh wep->weReserved2 = 0; 54533548Sjkh 54633548Sjkh /* 54733548Sjkh * Now convert the filename parts 54833548Sjkh */ 549120492Sfjoe end = 0; 550120492Sfjoe for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) { 551125992Stjr code = unix2winchr(&un, &unlen, 0, pmp); 552120492Sfjoe *wcp++ = code; 553120492Sfjoe *wcp++ = code >> 8; 554120492Sfjoe if (!code) 555120492Sfjoe end = WIN_LAST; 5562893Sdfr } 557120492Sfjoe for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) { 558125992Stjr code = unix2winchr(&un, &unlen, 0, pmp); 559120492Sfjoe *wcp++ = code; 560120492Sfjoe *wcp++ = code >> 8; 561120492Sfjoe if (!code) 562120492Sfjoe end = WIN_LAST; 56333548Sjkh } 564120492Sfjoe for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) { 565125992Stjr code = unix2winchr(&un, &unlen, 0, pmp); 566120492Sfjoe *wcp++ = code; 567120492Sfjoe *wcp++ = code >> 8; 568120492Sfjoe if (!code) 569120492Sfjoe end = WIN_LAST; 57033548Sjkh } 571333610Spfg if (!unlen) 572120492Sfjoe end = WIN_LAST; 573120492Sfjoe wep->weCnt |= end; 574120492Sfjoe return !end; 5752893Sdfr} 5762893Sdfr 5772893Sdfr/* 57833548Sjkh * Compare our filename to the one in the Win95 entry 57933548Sjkh * Returns the checksum or -1 if no match 5802893Sdfr */ 58133548Sjkhint 582277092SemastewinChkName(struct mbnambuf *nbp, const u_char *un, size_t unlen, int chksum, 583277092Semaste struct msdosfsmount *pmp) 58433548Sjkh{ 585125992Stjr size_t len; 586120492Sfjoe u_int16_t c1, c2; 587120492Sfjoe u_char *np; 588120492Sfjoe struct dirent dirbuf; 58933548Sjkh 59033548Sjkh /* 591172027Sbde * We already have winentry in *nbp. 59233548Sjkh */ 593172027Sbde if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0) 59433548Sjkh return -1; 59533548Sjkh 596120492Sfjoe#ifdef MSDOSFS_DEBUG 597120492Sfjoe printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen, 598120492Sfjoe dirbuf.d_name, 599120492Sfjoe dirbuf.d_namlen); 600120492Sfjoe#endif 601106110Ssemenu 602106110Ssemenu /* 603120492Sfjoe * Compare the name parts 604106110Ssemenu */ 605120492Sfjoe len = dirbuf.d_namlen; 606120492Sfjoe if (unlen != len) 607120492Sfjoe return -2; 60833548Sjkh 609120492Sfjoe for (np = dirbuf.d_name; unlen > 0 && len > 0;) { 610120492Sfjoe /* 611123293Sfjoe * Comparison must be case insensitive, because FAT disallows 612123293Sfjoe * to look up or create files in case sensitive even when 613123293Sfjoe * it's a long file name. 614120492Sfjoe */ 615277898Sdim c1 = unix2winchr(__DECONST(const u_char **, &np), &len, 616277898Sdim LCASE_BASE, pmp); 617125992Stjr c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp); 61833760Sache if (c1 != c2) 619120492Sfjoe return -2; 62033548Sjkh } 62133548Sjkh return chksum; 62233548Sjkh} 62333548Sjkh 62433548Sjkh/* 62533548Sjkh * Convert Win95 filename to dirbuf. 62633548Sjkh * Returns the checksum or -1 if impossible 62733548Sjkh */ 62833548Sjkhint 629277092Semastewin2unixfn(struct mbnambuf *nbp, struct winentry *wep, int chksum, 630277092Semaste struct msdosfsmount *pmp) 63133548Sjkh{ 632228796Skevlo u_char *c, tmpbuf[5]; 63333548Sjkh u_int8_t *cp; 634227650Skevlo u_int8_t *np, name[WIN_CHARS * 3 + 1]; 63533744Sache u_int16_t code; 63633548Sjkh int i; 63733548Sjkh 63833548Sjkh if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 63933548Sjkh || !(wep->weCnt&WIN_CNT)) 64033548Sjkh return -1; 64133548Sjkh 64233548Sjkh /* 64333548Sjkh * First compare checksums 64433548Sjkh */ 64533548Sjkh if (wep->weCnt&WIN_LAST) { 64633548Sjkh chksum = wep->weChksum; 64733548Sjkh } else if (chksum != wep->weChksum) 64833548Sjkh chksum = -1; 64933548Sjkh if (chksum == -1) 65033548Sjkh return -1; 65133548Sjkh 65233548Sjkh /* 65333548Sjkh * Convert the name parts 65433548Sjkh */ 655120492Sfjoe np = name; 65633548Sjkh for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 65733744Sache code = (cp[1] << 8) | cp[0]; 65833744Sache switch (code) { 65933548Sjkh case 0: 66033744Sache *np = '\0'; 661298664Skp if (mbnambuf_write(nbp, name, 662298664Skp (wep->weCnt & WIN_CNT) - 1) != 0) 663298664Skp return -1; 66433548Sjkh return chksum; 66533548Sjkh case '/': 66633744Sache *np = '\0'; 66733548Sjkh return -1; 66833744Sache default: 669228796Skevlo c = win2unixchr(tmpbuf, code, pmp); 670227650Skevlo while (*c != '\0') 671227650Skevlo *np++ = *c++; 67233744Sache break; 67333548Sjkh } 67433744Sache cp += 2; 67533548Sjkh } 67633548Sjkh for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 67733744Sache code = (cp[1] << 8) | cp[0]; 67833744Sache switch (code) { 67933548Sjkh case 0: 68033744Sache *np = '\0'; 681298664Skp if (mbnambuf_write(nbp, name, 682298664Skp (wep->weCnt & WIN_CNT) - 1) != 0) 683298664Skp 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->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 69733744Sache code = (cp[1] << 8) | cp[0]; 69833744Sache switch (code) { 69933548Sjkh case 0: 70033744Sache *np = '\0'; 701298664Skp if (mbnambuf_write(nbp, name, 702298664Skp (wep->weCnt & WIN_CNT) - 1) != 0) 703298664Skp 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 } 716120492Sfjoe *np = '\0'; 717298664Skp if (mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1) != 0) 718298664Skp return -1; 71933548Sjkh return chksum; 72033548Sjkh} 72133548Sjkh 72233548Sjkh/* 723141497Snjl * Compute the unrolled checksum of a DOS filename for Win95 LFN use. 72433548Sjkh */ 72533548Sjkhu_int8_t 726203827SkibwinChksum(u_int8_t *name) 72733548Sjkh{ 728203827Skib int i; 72933548Sjkh u_int8_t s; 73033548Sjkh 731203827Skib for (s = 0, i = 11; --i >= 0; s += *name++) 732203827Skib s = (s << 7)|(s >> 1); 733141497Snjl return (s); 73433548Sjkh} 73533548Sjkh 73633548Sjkh/* 73733548Sjkh * Determine the number of slots necessary for Win95 names 73833548Sjkh */ 73933548Sjkhint 740277092SemastewinSlotCnt(const u_char *un, size_t unlen, struct msdosfsmount *pmp) 74133548Sjkh{ 742120492Sfjoe size_t wlen; 743120492Sfjoe char wn[WIN_MAXLEN * 2 + 1], *wnp; 744120492Sfjoe 74533848Smsmith unlen = winLenFixup(un, unlen); 746120492Sfjoe 747120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 748120492Sfjoe wlen = WIN_MAXLEN * 2; 749120492Sfjoe wnp = wn; 750125992Stjr msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen); 751120492Sfjoe if (unlen > 0) 752120492Sfjoe return 0; 753120492Sfjoe return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS); 754120492Sfjoe } 755120492Sfjoe 75633548Sjkh if (unlen > WIN_MAXLEN) 75733548Sjkh return 0; 75833548Sjkh return howmany(unlen, WIN_CHARS); 75933548Sjkh} 76033848Smsmith 76133848Smsmith/* 762298806Spfg * Determine the number of bytes necessary for Win95 names 76333848Smsmith */ 764125992Stjrsize_t 765277092SemastewinLenFixup(const u_char *un, size_t unlen) 76633848Smsmith{ 76733848Smsmith for (un += unlen; unlen > 0; unlen--) 76833848Smsmith if (*--un != ' ' && *un != '.') 76933848Smsmith break; 77033848Smsmith return unlen; 77133848Smsmith} 772120492Sfjoe 773120492Sfjoe/* 774249588Sgabor * Store an area with multi byte string instr, and returns left 775120492Sfjoe * byte of instr and moves pointer forward. The area's size is 776120492Sfjoe * inlen or outlen. 777120492Sfjoe */ 778120492Sfjoestatic int 779125992Stjrmbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle) 780120492Sfjoe{ 781120492Sfjoe char *outp, outstr[outlen * weight + 1]; 782120492Sfjoe 783120492Sfjoe if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) { 784120492Sfjoe outp = outstr; 785120492Sfjoe outlen *= weight; 786125992Stjr msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen); 787120492Sfjoe return (inlen); 788120492Sfjoe } 789120492Sfjoe 790120492Sfjoe (*instr) += min(inlen, outlen); 791120492Sfjoe return (inlen - min(inlen, outlen)); 792120492Sfjoe} 793120492Sfjoe 794120492Sfjoe/* 795120492Sfjoe * Convert DOS char to Local char 796120492Sfjoe */ 797227650Skevlostatic u_char * 798228796Skevlodos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 799120492Sfjoe{ 800228796Skevlo u_char c, *outp; 801120492Sfjoe size_t len, olen; 802120492Sfjoe 803227650Skevlo outp = outbuf; 804120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 805227650Skevlo olen = len = 4; 806120492Sfjoe 807120492Sfjoe if (lower & (LCASE_BASE | LCASE_EXT)) 808120492Sfjoe msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr, 809227650Skevlo ilen, (char **)&outp, &olen, KICONV_LOWER); 810120492Sfjoe else 811120492Sfjoe msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr, 812227650Skevlo ilen, (char **)&outp, &olen); 813120492Sfjoe len -= olen; 814120492Sfjoe 815120492Sfjoe /* 816120492Sfjoe * return '?' if failed to convert 817120492Sfjoe */ 818120492Sfjoe if (len == 0) { 819120492Sfjoe (*ilen)--; 820120492Sfjoe (*instr)++; 821227650Skevlo *outp++ = '?'; 822120492Sfjoe } 823227650Skevlo } else { 824227650Skevlo (*ilen)--; 825227650Skevlo c = *(*instr)++; 826227650Skevlo c = dos2unix[c]; 827227650Skevlo if (lower & (LCASE_BASE | LCASE_EXT)) 828227650Skevlo c = u2l[c]; 829227650Skevlo *outp++ = c; 830227650Skevlo outbuf[1] = '\0'; 831120492Sfjoe } 832120492Sfjoe 833227650Skevlo *outp = '\0'; 834227650Skevlo outp = outbuf; 835227650Skevlo return (outp); 836120492Sfjoe} 837120492Sfjoe 838120492Sfjoe/* 839120492Sfjoe * Convert Local char to DOS char 840120492Sfjoe */ 841120492Sfjoestatic u_int16_t 842120492Sfjoeunix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp) 843120492Sfjoe{ 844120492Sfjoe u_char c; 845120492Sfjoe char *up, *outp, unicode[3], outbuf[3]; 846120492Sfjoe u_int16_t wc; 847148089Simura size_t len, ucslen, unixlen, olen; 848120492Sfjoe 849120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 850120492Sfjoe /* 851120492Sfjoe * to hide an invisible character, using a unicode filter 852120492Sfjoe */ 853148089Simura ucslen = 2; 854120492Sfjoe len = *ilen; 855120492Sfjoe up = unicode; 856120492Sfjoe msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 857148089Simura ilen, &up, &ucslen); 858148089Simura unixlen = len - *ilen; 859120492Sfjoe 860120492Sfjoe /* 861120492Sfjoe * cannot be converted 862120492Sfjoe */ 863148089Simura if (unixlen == 0) { 864120492Sfjoe (*ilen)--; 865120492Sfjoe (*instr)++; 866120492Sfjoe return (0); 867120492Sfjoe } 868120492Sfjoe 869120492Sfjoe /* 870120492Sfjoe * return magic number for ascii char 871120492Sfjoe */ 872148089Simura if (unixlen == 1) { 873120492Sfjoe c = *(*instr -1); 874120492Sfjoe if (! (c & 0x80)) { 875120492Sfjoe c = unix2dos[c]; 876120492Sfjoe if (c <= 2) 877120492Sfjoe return (c); 878120492Sfjoe } 879120492Sfjoe } 880120492Sfjoe 881120492Sfjoe /* 882120492Sfjoe * now convert using libiconv 883120492Sfjoe */ 884148089Simura *instr -= unixlen; 885148089Simura *ilen = len; 886120492Sfjoe 887120492Sfjoe olen = len = 2; 888120492Sfjoe outp = outbuf; 889120492Sfjoe msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr, 890120492Sfjoe ilen, &outp, &olen, KICONV_FROM_UPPER); 891120492Sfjoe len -= olen; 892148089Simura 893148089Simura /* 894148089Simura * cannot be converted, but has unicode char, should return magic number 895148089Simura */ 896148089Simura if (len == 0) { 897148089Simura (*ilen) -= unixlen; 898148089Simura (*instr) += unixlen; 899148089Simura return (1); 900148089Simura } 901148089Simura 902120492Sfjoe wc = 0; 903120492Sfjoe while(len--) 904120492Sfjoe wc |= (*(outp - len - 1) & 0xff) << (len << 3); 905120492Sfjoe return (wc); 906120492Sfjoe } 907120492Sfjoe 908120492Sfjoe (*ilen)--; 909120492Sfjoe c = *(*instr)++; 910120492Sfjoe c = l2u[c]; 911120492Sfjoe c = unix2dos[c]; 912120492Sfjoe return ((u_int16_t)c); 913120492Sfjoe} 914120492Sfjoe 915120492Sfjoe/* 916120492Sfjoe * Convert Windows char to Local char 917120492Sfjoe */ 918227650Skevlostatic u_char * 919228796Skevlowin2unixchr(u_char *outbuf, u_int16_t wc, struct msdosfsmount *pmp) 920120492Sfjoe{ 921228796Skevlo u_char *inp, *outp, inbuf[3]; 922120492Sfjoe size_t ilen, olen, len; 923120492Sfjoe 924227650Skevlo outp = outbuf; 925120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 926120492Sfjoe inbuf[0] = (u_char)(wc>>8); 927120492Sfjoe inbuf[1] = (u_char)wc; 928120492Sfjoe inbuf[2] = '\0'; 929120492Sfjoe 930227650Skevlo ilen = 2; 931227650Skevlo olen = len = 4; 932120492Sfjoe inp = inbuf; 933277898Sdim msdosfs_iconv->convchr(pmp->pm_w2u, __DECONST(const char **, 934277898Sdim &inp), &ilen, (char **)&outp, &olen); 935120492Sfjoe len -= olen; 936120492Sfjoe 937120492Sfjoe /* 938120492Sfjoe * return '?' if failed to convert 939120492Sfjoe */ 940227650Skevlo if (len == 0) 941227650Skevlo *outp++ = '?'; 942227650Skevlo } else { 943227650Skevlo *outp++ = (wc & 0xff00) ? '?' : (u_char)(wc & 0xff); 944120492Sfjoe } 945120492Sfjoe 946227650Skevlo *outp = '\0'; 947227650Skevlo outp = outbuf; 948227650Skevlo return (outp); 949120492Sfjoe} 950120492Sfjoe 951120492Sfjoe/* 952120492Sfjoe * Convert Local char to Windows char 953120492Sfjoe */ 954120492Sfjoestatic u_int16_t 955120492Sfjoeunix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 956120492Sfjoe{ 957120492Sfjoe u_char *outp, outbuf[3]; 958120492Sfjoe u_int16_t wc; 959120492Sfjoe size_t olen; 960120492Sfjoe 961120492Sfjoe if (*ilen == 0) 962120492Sfjoe return (0); 963120492Sfjoe 964120492Sfjoe if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 965120492Sfjoe outp = outbuf; 966120492Sfjoe olen = 2; 967120492Sfjoe if (lower & (LCASE_BASE | LCASE_EXT)) 968120492Sfjoe msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr, 969120492Sfjoe ilen, (char **)&outp, &olen, 970120492Sfjoe KICONV_FROM_LOWER); 971120492Sfjoe else 972120492Sfjoe msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 973120492Sfjoe ilen, (char **)&outp, &olen); 974120492Sfjoe 975120492Sfjoe /* 976120492Sfjoe * return '0' if end of filename 977120492Sfjoe */ 978120492Sfjoe if (olen == 2) 979120492Sfjoe return (0); 980120492Sfjoe 981120492Sfjoe wc = (outbuf[0]<<8) | outbuf[1]; 982120492Sfjoe 983120492Sfjoe return (wc); 984120492Sfjoe } 985120492Sfjoe 986120492Sfjoe (*ilen)--; 987120492Sfjoe wc = (*instr)[0]; 988120492Sfjoe if (lower & (LCASE_BASE | LCASE_EXT)) 989120492Sfjoe wc = u2l[wc]; 990120492Sfjoe (*instr)++; 991120492Sfjoe return (wc); 992120492Sfjoe} 993120492Sfjoe 994120492Sfjoe/* 995172027Sbde * Initialize the temporary concatenation buffer. 996120492Sfjoe */ 997120492Sfjoevoid 998172027Sbdembnambuf_init(struct mbnambuf *nbp) 999120492Sfjoe{ 1000120492Sfjoe 1001172027Sbde nbp->nb_len = 0; 1002172027Sbde nbp->nb_last_id = -1; 1003172027Sbde nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0'; 1004120492Sfjoe} 1005120492Sfjoe 1006120492Sfjoe/* 1007145131Snjl * Fill out our concatenation buffer with the given substring, at the offset 1008145131Snjl * specified by its id. Since this function must be called with ids in 1009145131Snjl * descending order, we take advantage of the fact that ASCII substrings are 1010145131Snjl * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all 1011145131Snjl * previous (i.e. higher id) substrings upwards to make room for this one. 1012145131Snjl * This only penalizes portions of substrings that contain more than 1013145131Snjl * WIN_CHARS bytes when they are first encountered. 1014120492Sfjoe */ 1015298664Skpint 1016172027Sbdembnambuf_write(struct mbnambuf *nbp, char *name, int id) 1017120492Sfjoe{ 1018145131Snjl char *slot; 1019172027Sbde size_t count, newlen; 1020143435Snjl 1021187199Strasz if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) { 1022227817Skib#ifdef MSDOSFS_DEBUG 1023187199Strasz printf("msdosfs: non-decreasing id: id %d, last id %d\n", 1024187199Strasz id, nbp->nb_last_id); 1025227817Skib#endif 1026298664Skp return (EINVAL); 1027187199Strasz } 1028145131Snjl 1029172027Sbde /* Will store this substring in a WIN_CHARS-aligned slot. */ 1030172027Sbde slot = &nbp->nb_buf[id * WIN_CHARS]; 1031145131Snjl count = strlen(name); 1032172027Sbde newlen = nbp->nb_len + count; 1033172027Sbde if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) { 1034227817Skib#ifdef MSDOSFS_DEBUG 1035172027Sbde printf("msdosfs: file name length %zu too large\n", newlen); 1036227817Skib#endif 1037298664Skp return (ENAMETOOLONG); 1038120492Sfjoe } 1039145131Snjl 1040145131Snjl /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */ 1041298664Skp if (count > WIN_CHARS && nbp->nb_len != 0) { 1042298664Skp if ((id * WIN_CHARS + count + nbp->nb_len) > 1043298664Skp sizeof(nbp->nb_buf)) 1044298664Skp return (ENAMETOOLONG); 1045298664Skp 1046172027Sbde bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len); 1047298664Skp } 1048145131Snjl 1049145131Snjl /* Copy in the substring to its slot and update length so far. */ 1050145131Snjl bcopy(name, slot, count); 1051172027Sbde nbp->nb_len = newlen; 1052172027Sbde nbp->nb_last_id = id; 1053298664Skp 1054298664Skp return (0); 1055120492Sfjoe} 1056120492Sfjoe 1057120492Sfjoe/* 1058143435Snjl * Take the completed string and use it to setup the struct dirent. 1059143435Snjl * Be sure to always nul-terminate the d_name and then copy the string 1060143435Snjl * from our buffer. Note that this function assumes the full string has 1061143435Snjl * been reassembled in the buffer. If it's called before all substrings 1062143435Snjl * have been written via mbnambuf_write(), the result will be incorrect. 1063120492Sfjoe */ 1064120492Sfjoechar * 1065172027Sbdembnambuf_flush(struct mbnambuf *nbp, struct dirent *dp) 1066120492Sfjoe{ 1067120492Sfjoe 1068172027Sbde if (nbp->nb_len > sizeof(dp->d_name) - 1) { 1069172027Sbde mbnambuf_init(nbp); 1070143435Snjl return (NULL); 1071120492Sfjoe } 1072172027Sbde bcopy(&nbp->nb_buf[0], dp->d_name, nbp->nb_len); 1073172027Sbde dp->d_name[nbp->nb_len] = '\0'; 1074172027Sbde dp->d_namlen = nbp->nb_len; 1075143435Snjl 1076172027Sbde mbnambuf_init(nbp); 1077120492Sfjoe return (dp->d_name); 1078120492Sfjoe} 1079