xref: /freebsd/sys/fs/msdosfs/msdosfs_conv.c (revision d167cf6f3a474d20001eddfb1d7c672407ef2630)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
2952a6212SJordan K. Hubbard /*	$NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $	*/
327a0bc89SDoug Rabson 
4952a6212SJordan K. Hubbard /*-
5952a6212SJordan K. Hubbard  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
6952a6212SJordan K. Hubbard  * Copyright (C) 1995, 1997 TooLs GmbH.
7952a6212SJordan K. Hubbard  * All rights reserved.
8952a6212SJordan K. Hubbard  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9952a6212SJordan K. Hubbard  *
10952a6212SJordan K. Hubbard  * Redistribution and use in source and binary forms, with or without
11952a6212SJordan K. Hubbard  * modification, are permitted provided that the following conditions
12952a6212SJordan K. Hubbard  * are met:
13952a6212SJordan K. Hubbard  * 1. Redistributions of source code must retain the above copyright
14952a6212SJordan K. Hubbard  *    notice, this list of conditions and the following disclaimer.
15952a6212SJordan K. Hubbard  * 2. Redistributions in binary form must reproduce the above copyright
16952a6212SJordan K. Hubbard  *    notice, this list of conditions and the following disclaimer in the
17952a6212SJordan K. Hubbard  *    documentation and/or other materials provided with the distribution.
18952a6212SJordan K. Hubbard  * 3. All advertising materials mentioning features or use of this software
19952a6212SJordan K. Hubbard  *    must display the following acknowledgement:
20952a6212SJordan K. Hubbard  *	This product includes software developed by TooLs GmbH.
21952a6212SJordan K. Hubbard  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22952a6212SJordan K. Hubbard  *    derived from this software without specific prior written permission.
23952a6212SJordan K. Hubbard  *
24952a6212SJordan K. Hubbard  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25952a6212SJordan K. Hubbard  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26952a6212SJordan K. Hubbard  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27952a6212SJordan K. Hubbard  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28952a6212SJordan K. Hubbard  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29952a6212SJordan K. Hubbard  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30952a6212SJordan K. Hubbard  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31952a6212SJordan K. Hubbard  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32952a6212SJordan K. Hubbard  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33952a6212SJordan K. Hubbard  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34952a6212SJordan K. Hubbard  */
35d167cf6fSWarner Losh /*-
3627a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3727a0bc89SDoug Rabson  *
3827a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
3927a0bc89SDoug Rabson  * it, and don't remove this notice.
4027a0bc89SDoug Rabson  *
4127a0bc89SDoug Rabson  * This software is provided "as is".
4227a0bc89SDoug Rabson  *
4327a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4427a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4527a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4627a0bc89SDoug Rabson  * any damages caused by this software.
4727a0bc89SDoug Rabson  *
4827a0bc89SDoug Rabson  * October 1992
4927a0bc89SDoug Rabson  */
5027a0bc89SDoug Rabson 
5127a0bc89SDoug Rabson /*
5227a0bc89SDoug Rabson  * System include files.
5327a0bc89SDoug Rabson  */
5427a0bc89SDoug Rabson #include <sys/param.h>
5527a0bc89SDoug Rabson #include <sys/time.h>
5627a0bc89SDoug Rabson #include <sys/kernel.h>		/* defines tz */
57952a6212SJordan K. Hubbard #include <sys/systm.h>
58aeab1b50SAndrey A. Chernov #include <machine/clock.h>
59952a6212SJordan K. Hubbard #include <sys/dirent.h>
60c4f02a89SMax Khon #include <sys/iconv.h>
61c4f02a89SMax Khon #include <sys/mount.h>
62c4f02a89SMax Khon #include <sys/malloc.h>
63c4f02a89SMax Khon 
64c4f02a89SMax Khon extern struct iconv_functions *msdosfs_iconv;
6527a0bc89SDoug Rabson 
6627a0bc89SDoug Rabson /*
6727a0bc89SDoug Rabson  * MSDOSFS include files.
6827a0bc89SDoug Rabson  */
69c4f02a89SMax Khon #include <fs/msdosfs/bpb.h>
70c4f02a89SMax Khon #include <fs/msdosfs/msdosfsmount.h>
711166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h>
7227a0bc89SDoug Rabson 
7327a0bc89SDoug Rabson /*
74aeab1b50SAndrey A. Chernov  * Total number of days that have passed for each month in a regular year.
7527a0bc89SDoug Rabson  */
767fefffeeSPoul-Henning Kamp static u_short regyear[] = {
77aeab1b50SAndrey A. Chernov 	31, 59, 90, 120, 151, 181,
78aeab1b50SAndrey A. Chernov 	212, 243, 273, 304, 334, 365
7927a0bc89SDoug Rabson };
8027a0bc89SDoug Rabson 
8127a0bc89SDoug Rabson /*
82aeab1b50SAndrey A. Chernov  * Total number of days that have passed for each month in a leap year.
8327a0bc89SDoug Rabson  */
847fefffeeSPoul-Henning Kamp static u_short leapyear[] = {
85aeab1b50SAndrey A. Chernov 	31, 60, 91, 121, 152, 182,
86aeab1b50SAndrey A. Chernov 	213, 244, 274, 305, 335, 366
8727a0bc89SDoug Rabson };
8827a0bc89SDoug Rabson 
8927a0bc89SDoug Rabson /*
9027a0bc89SDoug Rabson  * Variables used to remember parts of the last time conversion.  Maybe we
9127a0bc89SDoug Rabson  * can avoid a full conversion.
9227a0bc89SDoug Rabson  */
93303b270bSEivind Eklund static u_long  lasttime;
94303b270bSEivind Eklund static u_long  lastday;
95303b270bSEivind Eklund static u_short lastddate;
96303b270bSEivind Eklund static u_short lastdtime;
9727a0bc89SDoug Rabson 
98bddcdc51STim J. Robbins static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle);
99c4f02a89SMax Khon static u_int16_t dos2unixchr(const u_char **, size_t *, int, struct msdosfsmount *);
100c4f02a89SMax Khon static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
101c4f02a89SMax Khon static u_int16_t win2unixchr(u_int16_t, struct msdosfsmount *);
102c4f02a89SMax Khon static u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *);
103c4f02a89SMax Khon 
104c4f02a89SMax Khon struct mbnambuf {
105c4f02a89SMax Khon 	char *		p;
106c4f02a89SMax Khon 	size_t		n;
107c4f02a89SMax Khon };
108c4f02a89SMax Khon static struct mbnambuf subent[WIN_MAXSUBENTRIES];
10913df76f2SAndrey A. Chernov 
11027a0bc89SDoug Rabson /*
11127a0bc89SDoug Rabson  * Convert the unix version of time to dos's idea of time to be used in
11227a0bc89SDoug Rabson  * file timestamps. The passed in unix time is assumed to be in GMT.
11327a0bc89SDoug Rabson  */
11427a0bc89SDoug Rabson void
115952a6212SJordan K. Hubbard unix2dostime(tsp, ddp, dtp, dhp)
11627a0bc89SDoug Rabson 	struct timespec *tsp;
117952a6212SJordan K. Hubbard 	u_int16_t *ddp;
118952a6212SJordan K. Hubbard 	u_int16_t *dtp;
119952a6212SJordan K. Hubbard 	u_int8_t *dhp;
12027a0bc89SDoug Rabson {
12127a0bc89SDoug Rabson 	u_long t;
12227a0bc89SDoug Rabson 	u_long days;
12327a0bc89SDoug Rabson 	u_long inc;
12427a0bc89SDoug Rabson 	u_long year;
12527a0bc89SDoug Rabson 	u_long month;
12627a0bc89SDoug Rabson 	u_short *months;
12727a0bc89SDoug Rabson 
12827a0bc89SDoug Rabson 	/*
12927a0bc89SDoug Rabson 	 * If the time from the last conversion is the same as now, then
13027a0bc89SDoug Rabson 	 * skip the computations and use the saved result.
13127a0bc89SDoug Rabson 	 */
13291f1c2b3SPoul-Henning Kamp 	t = tsp->tv_sec - (tz_minuteswest * 60)
133b0b32125SAndrey A. Chernov 	    - (wall_cmos_clock ? adjkerntz : 0);
134ef5fdc52SAndrey A. Chernov 	    /* - daylight savings time correction */
135952a6212SJordan K. Hubbard 	t &= ~1;
13627a0bc89SDoug Rabson 	if (lasttime != t) {
13727a0bc89SDoug Rabson 		lasttime = t;
138952a6212SJordan K. Hubbard 		lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
13927a0bc89SDoug Rabson 		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
14027a0bc89SDoug Rabson 		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
14127a0bc89SDoug Rabson 
14227a0bc89SDoug Rabson 		/*
14327a0bc89SDoug Rabson 		 * If the number of days since 1970 is the same as the last
14427a0bc89SDoug Rabson 		 * time we did the computation then skip all this leap year
14527a0bc89SDoug Rabson 		 * and month stuff.
14627a0bc89SDoug Rabson 		 */
14727a0bc89SDoug Rabson 		days = t / (24 * 60 * 60);
14827a0bc89SDoug Rabson 		if (days != lastday) {
14927a0bc89SDoug Rabson 			lastday = days;
15027a0bc89SDoug Rabson 			for (year = 1970;; year++) {
15127a0bc89SDoug Rabson 				inc = year & 0x03 ? 365 : 366;
15227a0bc89SDoug Rabson 				if (days < inc)
15327a0bc89SDoug Rabson 					break;
15427a0bc89SDoug Rabson 				days -= inc;
15527a0bc89SDoug Rabson 			}
15627a0bc89SDoug Rabson 			months = year & 0x03 ? regyear : leapyear;
157e73bdb51SBruce Evans 			for (month = 0; days >= months[month]; month++)
158aeab1b50SAndrey A. Chernov 				;
159aeab1b50SAndrey A. Chernov 			if (month > 0)
160aeab1b50SAndrey A. Chernov 				days -= months[month - 1];
16127a0bc89SDoug Rabson 			lastddate = ((days + 1) << DD_DAY_SHIFT)
16227a0bc89SDoug Rabson 			    + ((month + 1) << DD_MONTH_SHIFT);
16327a0bc89SDoug Rabson 			/*
16427a0bc89SDoug Rabson 			 * Remember dos's idea of time is relative to 1980.
16527a0bc89SDoug Rabson 			 * unix's is relative to 1970.  If somehow we get a
16627a0bc89SDoug Rabson 			 * time before 1980 then don't give totally crazy
16727a0bc89SDoug Rabson 			 * results.
16827a0bc89SDoug Rabson 			 */
16927a0bc89SDoug Rabson 			if (year > 1980)
17027a0bc89SDoug Rabson 				lastddate += (year - 1980) << DD_YEAR_SHIFT;
17127a0bc89SDoug Rabson 		}
17227a0bc89SDoug Rabson 	}
173952a6212SJordan K. Hubbard 	if (dtp)
17427a0bc89SDoug Rabson 		*dtp = lastdtime;
175952a6212SJordan K. Hubbard 	if (dhp)
176952a6212SJordan K. Hubbard 		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
177952a6212SJordan K. Hubbard 
17827a0bc89SDoug Rabson 	*ddp = lastddate;
17927a0bc89SDoug Rabson }
18027a0bc89SDoug Rabson 
18127a0bc89SDoug Rabson /*
18227a0bc89SDoug Rabson  * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
18327a0bc89SDoug Rabson  * interval there were 8 regular years and 2 leap years.
18427a0bc89SDoug Rabson  */
18527a0bc89SDoug Rabson #define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
18627a0bc89SDoug Rabson 
187303b270bSEivind Eklund static u_short lastdosdate;
188303b270bSEivind Eklund static u_long  lastseconds;
18927a0bc89SDoug Rabson 
19027a0bc89SDoug Rabson /*
19127a0bc89SDoug Rabson  * Convert from dos' idea of time to unix'. This will probably only be
19227a0bc89SDoug Rabson  * called from the stat(), and fstat() system calls and so probably need
19327a0bc89SDoug Rabson  * not be too efficient.
19427a0bc89SDoug Rabson  */
19527a0bc89SDoug Rabson void
196952a6212SJordan K. Hubbard dos2unixtime(dd, dt, dh, tsp)
197952a6212SJordan K. Hubbard 	u_int dd;
198952a6212SJordan K. Hubbard 	u_int dt;
199952a6212SJordan K. Hubbard 	u_int dh;
20027a0bc89SDoug Rabson 	struct timespec *tsp;
20127a0bc89SDoug Rabson {
20227a0bc89SDoug Rabson 	u_long seconds;
203a98ca469SPoul-Henning Kamp 	u_long month;
204a98ca469SPoul-Henning Kamp 	u_long year;
20527a0bc89SDoug Rabson 	u_long days;
20627a0bc89SDoug Rabson 	u_short *months;
20727a0bc89SDoug Rabson 
208952a6212SJordan K. Hubbard 	if (dd == 0) {
209952a6212SJordan K. Hubbard 		/*
210952a6212SJordan K. Hubbard 		 * Uninitialized field, return the epoch.
211952a6212SJordan K. Hubbard 		 */
212952a6212SJordan K. Hubbard 		tsp->tv_sec = 0;
213952a6212SJordan K. Hubbard 		tsp->tv_nsec = 0;
214952a6212SJordan K. Hubbard 		return;
215952a6212SJordan K. Hubbard 	}
21663e4f22aSBruce Evans 	seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
21727a0bc89SDoug Rabson 	    + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
218952a6212SJordan K. Hubbard 	    + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
219952a6212SJordan K. Hubbard 	    + dh / 100;
22027a0bc89SDoug Rabson 	/*
22127a0bc89SDoug Rabson 	 * If the year, month, and day from the last conversion are the
22227a0bc89SDoug Rabson 	 * same then use the saved value.
22327a0bc89SDoug Rabson 	 */
22427a0bc89SDoug Rabson 	if (lastdosdate != dd) {
22527a0bc89SDoug Rabson 		lastdosdate = dd;
22627a0bc89SDoug Rabson 		days = 0;
22727a0bc89SDoug Rabson 		year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
228aeab1b50SAndrey A. Chernov 		days = year * 365;
229aeab1b50SAndrey A. Chernov 		days += year / 4 + 1;	/* add in leap days */
230aeab1b50SAndrey A. Chernov 		if ((year & 0x03) == 0)
231aeab1b50SAndrey A. Chernov 			days--;		/* if year is a leap year */
23227a0bc89SDoug Rabson 		months = year & 0x03 ? regyear : leapyear;
23327a0bc89SDoug Rabson 		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
234aeab1b50SAndrey A. Chernov 		if (month < 1 || month > 12) {
235952a6212SJordan K. Hubbard 			printf("dos2unixtime(): month value out of range (%ld)\n",
23627a0bc89SDoug Rabson 			    month);
23727a0bc89SDoug Rabson 			month = 1;
23827a0bc89SDoug Rabson 		}
239aeab1b50SAndrey A. Chernov 		if (month > 1)
240aeab1b50SAndrey A. Chernov 			days += months[month - 2];
24127a0bc89SDoug Rabson 		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
24227a0bc89SDoug Rabson 		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
24327a0bc89SDoug Rabson 	}
24491f1c2b3SPoul-Henning Kamp 	tsp->tv_sec = seconds + lastseconds + (tz_minuteswest * 60)
245b0b32125SAndrey A. Chernov 	     + adjkerntz;
246ef5fdc52SAndrey A. Chernov 	     /* + daylight savings time correction */
247952a6212SJordan K. Hubbard 	tsp->tv_nsec = (dh % 100) * 10000000;
24827a0bc89SDoug Rabson }
24927a0bc89SDoug Rabson 
250cce0eefdSMike Smith /*
251cce0eefdSMike Smith  * 0 - character disallowed in long file name.
252cce0eefdSMike Smith  * 1 - character should be replaced by '_' in DOS file name,
253cce0eefdSMike Smith  *     and generation number inserted.
254cce0eefdSMike Smith  * 2 - character ('.' and ' ') should be skipped in DOS file name,
255cce0eefdSMike Smith  *     and generation number inserted.
256cce0eefdSMike Smith  */
257952a6212SJordan K. Hubbard static u_char
258952a6212SJordan K. Hubbard unix2dos[256] = {
259c4f02a89SMax Khon /* iso8859-1 -> cp850 */
260952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
261952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
262952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
263952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
264cce0eefdSMike Smith 	2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
265cce0eefdSMike Smith 	0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,	/* 28-2f */
266952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
267cce0eefdSMike Smith 	0x38, 0x39, 0,    1,    0,    1,    0,    0,	/* 38-3f */
268952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
269952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
270952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
271cce0eefdSMike Smith 	0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f,	/* 58-5f */
272952a6212SJordan K. Hubbard 	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
273952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
274952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
275952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
276952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
277952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
278952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
279952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
280952a6212SJordan K. Hubbard 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
281952a6212SJordan K. Hubbard 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
282952a6212SJordan K. Hubbard 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
283952a6212SJordan K. Hubbard 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
284952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
285952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
286952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
287952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
288952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
289952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
290952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
291952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
292952a6212SJordan K. Hubbard };
293952a6212SJordan K. Hubbard 
294952a6212SJordan K. Hubbard static u_char
295952a6212SJordan K. Hubbard dos2unix[256] = {
296c4f02a89SMax Khon /* cp850 -> iso8859-1 */
297952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
298952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
299952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
300952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
301952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
302952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
303952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
304952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
305952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
306952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
307952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
308952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
309952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
310952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
311952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
312952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
313952a6212SJordan K. Hubbard 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
314952a6212SJordan K. Hubbard 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
315952a6212SJordan K. Hubbard 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
316952a6212SJordan K. Hubbard 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
317952a6212SJordan K. Hubbard 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
318952a6212SJordan K. Hubbard 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
319952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
320952a6212SJordan K. Hubbard 	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
321952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
322952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
323952a6212SJordan K. Hubbard 	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
324952a6212SJordan K. Hubbard 	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
325952a6212SJordan K. Hubbard 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
326952a6212SJordan K. Hubbard 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
327952a6212SJordan K. Hubbard 	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
328952a6212SJordan K. Hubbard 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
329952a6212SJordan K. Hubbard };
330952a6212SJordan K. Hubbard 
331952a6212SJordan K. Hubbard static u_char
332952a6212SJordan K. Hubbard u2l[256] = {
333c4f02a89SMax Khon /* tolower */
334952a6212SJordan K. Hubbard 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
335952a6212SJordan K. Hubbard 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
336952a6212SJordan K. Hubbard 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
337952a6212SJordan K. Hubbard 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
338952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
339952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
340952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
341952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
342952a6212SJordan K. Hubbard 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
343952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
344952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
345952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
346952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
347952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
348952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
349952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
350952a6212SJordan K. Hubbard 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
351952a6212SJordan K. Hubbard 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
352952a6212SJordan K. Hubbard 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
353952a6212SJordan K. Hubbard 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
354952a6212SJordan K. Hubbard 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
355952a6212SJordan K. Hubbard 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
356952a6212SJordan K. Hubbard 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
357952a6212SJordan K. Hubbard 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
358952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
359952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
360952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
361952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
362952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
363952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
364952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
365952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
366952a6212SJordan K. Hubbard };
36727a0bc89SDoug Rabson 
3687391f611SAndrey A. Chernov static u_char
3697391f611SAndrey A. Chernov l2u[256] = {
370c4f02a89SMax Khon /* toupper */
3717391f611SAndrey A. Chernov 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
3727391f611SAndrey A. Chernov 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
3737391f611SAndrey A. Chernov 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
3747391f611SAndrey A. Chernov 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
3757391f611SAndrey A. Chernov 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
3767391f611SAndrey A. Chernov 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
3777391f611SAndrey A. Chernov 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
3787391f611SAndrey A. Chernov 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
3797391f611SAndrey A. Chernov 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
3807391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
3817391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
3827391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
3837391f611SAndrey A. Chernov 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
3847391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
3857391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
3867391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
3877391f611SAndrey A. Chernov 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
3887391f611SAndrey A. Chernov 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
3897391f611SAndrey A. Chernov 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
3907391f611SAndrey A. Chernov 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
3917391f611SAndrey A. Chernov 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
3927391f611SAndrey A. Chernov 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
3937391f611SAndrey A. Chernov 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
3947391f611SAndrey A. Chernov 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
3957391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
3967391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
3977391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
3987391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
3997391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
4007391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
4017391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
4027391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
4037391f611SAndrey A. Chernov };
4047391f611SAndrey A. Chernov 
40527a0bc89SDoug Rabson /*
40627a0bc89SDoug Rabson  * DOS filenames are made of 2 parts, the name part and the extension part.
40727a0bc89SDoug Rabson  * The name part is 8 characters long and the extension part is 3
40827a0bc89SDoug Rabson  * characters long.  They may contain trailing blanks if the name or
40927a0bc89SDoug Rabson  * extension are not long enough to fill their respective fields.
41027a0bc89SDoug Rabson  */
41127a0bc89SDoug Rabson 
41227a0bc89SDoug Rabson /*
41327a0bc89SDoug Rabson  * Convert a DOS filename to a unix filename. And, return the number of
41427a0bc89SDoug Rabson  * characters in the resulting unix filename excluding the terminating
41527a0bc89SDoug Rabson  * null.
41627a0bc89SDoug Rabson  */
41727a0bc89SDoug Rabson int
418c4f02a89SMax Khon dos2unixfn(dn, un, lower, pmp)
41927a0bc89SDoug Rabson 	u_char dn[11];
42027a0bc89SDoug Rabson 	u_char *un;
4215114f1d7SAndrey A. Chernov 	int lower;
422c4f02a89SMax Khon 	struct msdosfsmount *pmp;
42327a0bc89SDoug Rabson {
424bddcdc51STim J. Robbins 	size_t i;
425c4f02a89SMax Khon 	int thislong = 0;
426c4f02a89SMax Khon 	u_int16_t c;
42727a0bc89SDoug Rabson 
42827a0bc89SDoug Rabson 	/*
42927a0bc89SDoug Rabson 	 * If first char of the filename is SLOT_E5 (0x05), then the real
43027a0bc89SDoug Rabson 	 * first char of the filename should be 0xe5. But, they couldn't
43127a0bc89SDoug Rabson 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
43227a0bc89SDoug Rabson 	 * directory slot. Another dos quirk.
43327a0bc89SDoug Rabson 	 */
434952a6212SJordan K. Hubbard 	if (*dn == SLOT_E5)
435c4f02a89SMax Khon 		*dn = 0xe5;
43627a0bc89SDoug Rabson 
437952a6212SJordan K. Hubbard 	/*
438952a6212SJordan K. Hubbard 	 * Copy the name portion into the unix filename string.
439952a6212SJordan K. Hubbard 	 */
440c4f02a89SMax Khon 	for (i = 8; i > 0 && *dn != ' ';) {
441bddcdc51STim J. Robbins 		c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
442c4f02a89SMax Khon 		if (c & 0xff00) {
443c4f02a89SMax Khon 			*un++ = c >> 8;
444952a6212SJordan K. Hubbard 			thislong++;
445952a6212SJordan K. Hubbard 		}
446c4f02a89SMax Khon 		*un++ = c;
447c4f02a89SMax Khon 		thislong++;
448c4f02a89SMax Khon 	}
449c4f02a89SMax Khon 	dn += i;
450952a6212SJordan K. Hubbard 
451952a6212SJordan K. Hubbard 	/*
452952a6212SJordan K. Hubbard 	 * Now, if there is an extension then put in a period and copy in
453952a6212SJordan K. Hubbard 	 * the extension.
454952a6212SJordan K. Hubbard 	 */
455952a6212SJordan K. Hubbard 	if (*dn != ' ') {
456952a6212SJordan K. Hubbard 		*un++ = '.';
457952a6212SJordan K. Hubbard 		thislong++;
458c4f02a89SMax Khon 		for (i = 3; i > 0 && *dn != ' ';) {
459bddcdc51STim J. Robbins 			c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
460c4f02a89SMax Khon 			if (c & 0xff00) {
461c4f02a89SMax Khon 				*un++ = c >> 8;
462c4f02a89SMax Khon 				thislong++;
463c4f02a89SMax Khon 			}
464c4f02a89SMax Khon 			*un++ = c;
465952a6212SJordan K. Hubbard 			thislong++;
466952a6212SJordan K. Hubbard 		}
467952a6212SJordan K. Hubbard 	}
468952a6212SJordan K. Hubbard 	*un++ = 0;
469952a6212SJordan K. Hubbard 
470952a6212SJordan K. Hubbard 	return (thislong);
47127a0bc89SDoug Rabson }
47227a0bc89SDoug Rabson 
47327a0bc89SDoug Rabson /*
474952a6212SJordan K. Hubbard  * Convert a unix filename to a DOS filename according to Win95 rules.
475952a6212SJordan K. Hubbard  * If applicable and gen is not 0, it is inserted into the converted
476952a6212SJordan K. Hubbard  * filename as a generation number.
477952a6212SJordan K. Hubbard  * Returns
478952a6212SJordan K. Hubbard  *	0 if name couldn't be converted
479952a6212SJordan K. Hubbard  *	1 if the converted name is the same as the original
480952a6212SJordan K. Hubbard  *	  (no long filename entry necessary for Win95)
481952a6212SJordan K. Hubbard  *	2 if conversion was successful
482952a6212SJordan K. Hubbard  *	3 if conversion was successful and generation number was inserted
48327a0bc89SDoug Rabson  */
484952a6212SJordan K. Hubbard int
485c4f02a89SMax Khon unix2dosfn(un, dn, unlen, gen, pmp)
486952a6212SJordan K. Hubbard 	const u_char *un;
487952a6212SJordan K. Hubbard 	u_char dn[12];
488bddcdc51STim J. Robbins 	size_t unlen;
489952a6212SJordan K. Hubbard 	u_int gen;
490c4f02a89SMax Khon 	struct msdosfsmount *pmp;
49127a0bc89SDoug Rabson {
492bddcdc51STim J. Robbins 	ssize_t i, j;
493bddcdc51STim J. Robbins 	int l;
494952a6212SJordan K. Hubbard 	int conv = 1;
495952a6212SJordan K. Hubbard 	const u_char *cp, *dp, *dp1;
496952a6212SJordan K. Hubbard 	u_char gentext[6], *wcp;
497c4f02a89SMax Khon 	u_int16_t c;
49827a0bc89SDoug Rabson 
49927a0bc89SDoug Rabson 	/*
50027a0bc89SDoug Rabson 	 * Fill the dos filename string with blanks. These are DOS's pad
50127a0bc89SDoug Rabson 	 * characters.
50227a0bc89SDoug Rabson 	 */
503952a6212SJordan K. Hubbard 	for (i = 0; i < 11; i++)
50427a0bc89SDoug Rabson 		dn[i] = ' ';
505952a6212SJordan K. Hubbard 	dn[11] = 0;
50627a0bc89SDoug Rabson 
50727a0bc89SDoug Rabson 	/*
50827a0bc89SDoug Rabson 	 * The filenames "." and ".." are handled specially, since they
50927a0bc89SDoug Rabson 	 * don't follow dos filename rules.
51027a0bc89SDoug Rabson 	 */
51127a0bc89SDoug Rabson 	if (un[0] == '.' && unlen == 1) {
51227a0bc89SDoug Rabson 		dn[0] = '.';
513952a6212SJordan K. Hubbard 		return gen <= 1;
51427a0bc89SDoug Rabson 	}
51527a0bc89SDoug Rabson 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
51627a0bc89SDoug Rabson 		dn[0] = '.';
51727a0bc89SDoug Rabson 		dn[1] = '.';
518952a6212SJordan K. Hubbard 		return gen <= 1;
51927a0bc89SDoug Rabson 	}
52027a0bc89SDoug Rabson 
52127a0bc89SDoug Rabson 	/*
522952a6212SJordan K. Hubbard 	 * Filenames with only blanks and dots are not allowed!
52327a0bc89SDoug Rabson 	 */
524952a6212SJordan K. Hubbard 	for (cp = un, i = unlen; --i >= 0; cp++)
525952a6212SJordan K. Hubbard 		if (*cp != ' ' && *cp != '.')
526952a6212SJordan K. Hubbard 			break;
527952a6212SJordan K. Hubbard 	if (i < 0)
528952a6212SJordan K. Hubbard 		return 0;
529952a6212SJordan K. Hubbard 
530cce0eefdSMike Smith 
531cce0eefdSMike Smith 	/*
532cce0eefdSMike Smith 	 * Filenames with some characters are not allowed!
533cce0eefdSMike Smith 	 */
534c4f02a89SMax Khon 	for (cp = un, i = unlen; i > 0;)
535c4f02a89SMax Khon 		if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
536cce0eefdSMike Smith 			return 0;
537cce0eefdSMike Smith 
538952a6212SJordan K. Hubbard 	/*
539952a6212SJordan K. Hubbard 	 * Now find the extension
540952a6212SJordan K. Hubbard 	 * Note: dot as first char doesn't start extension
541952a6212SJordan K. Hubbard 	 *	 and trailing dots and blanks are ignored
542c4f02a89SMax Khon 	 * Note(2003/7): It seems recent Windows has
543c4f02a89SMax Khon 	 *	 defferent rule than this code, that Windows
544c4f02a89SMax Khon 	 *	 ignores all dots before extension, and use all
545c4f02a89SMax Khon 	 * 	 chars as filename except for dots.
546952a6212SJordan K. Hubbard 	 */
547952a6212SJordan K. Hubbard 	dp = dp1 = 0;
548952a6212SJordan K. Hubbard 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
549952a6212SJordan K. Hubbard 		switch (*cp++) {
550952a6212SJordan K. Hubbard 		case '.':
551952a6212SJordan K. Hubbard 			if (!dp1)
552952a6212SJordan K. Hubbard 				dp1 = cp;
553952a6212SJordan K. Hubbard 			break;
554952a6212SJordan K. Hubbard 		case ' ':
555952a6212SJordan K. Hubbard 			break;
556952a6212SJordan K. Hubbard 		default:
557952a6212SJordan K. Hubbard 			if (dp1)
558952a6212SJordan K. Hubbard 				dp = dp1;
559952a6212SJordan K. Hubbard 			dp1 = 0;
560952a6212SJordan K. Hubbard 			break;
561952a6212SJordan K. Hubbard 		}
56227a0bc89SDoug Rabson 	}
56327a0bc89SDoug Rabson 
56427a0bc89SDoug Rabson 	/*
565c4f02a89SMax Khon 	 * Now convert it (this part is for extension)
56627a0bc89SDoug Rabson 	 */
567952a6212SJordan K. Hubbard 	if (dp) {
568952a6212SJordan K. Hubbard 		if (dp1)
569952a6212SJordan K. Hubbard 			l = dp1 - dp;
570952a6212SJordan K. Hubbard 		else
571952a6212SJordan K. Hubbard 			l = unlen - (dp - un);
572c4f02a89SMax Khon 		for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
573c4f02a89SMax Khon 			c = unix2doschr(&cp, (size_t *)&i, pmp);
574c4f02a89SMax Khon 			if (c & 0xff00) {
575c4f02a89SMax Khon 				dn[j] = c >> 8;
576c4f02a89SMax Khon 				if (++j < 11) {
577c4f02a89SMax Khon 					dn[j] = c;
578c4f02a89SMax Khon 					continue;
579c4f02a89SMax Khon 				} else {
580c4f02a89SMax Khon 					conv = 3;
581c4f02a89SMax Khon 					dn[j-1] = ' ';
582c4f02a89SMax Khon 					break;
583c4f02a89SMax Khon 				}
584c4f02a89SMax Khon 			} else {
585c4f02a89SMax Khon 				dn[j] = c;
586c4f02a89SMax Khon 			}
587c4f02a89SMax Khon 			if (*(cp - 1) != dn[j] && conv != 3)
588952a6212SJordan K. Hubbard 				conv = 2;
589cce0eefdSMike Smith 			if (dn[j] == 1) {
590cce0eefdSMike Smith 				conv = 3;
591cce0eefdSMike Smith 				dn[j] = '_';
592cce0eefdSMike Smith 			}
593cce0eefdSMike Smith 			if (dn[j] == 2) {
594952a6212SJordan K. Hubbard 				conv = 3;
595952a6212SJordan K. Hubbard 				dn[j--] = ' ';
596952a6212SJordan K. Hubbard 			}
597952a6212SJordan K. Hubbard 		}
598c4f02a89SMax Khon 		if (i > 0)
599952a6212SJordan K. Hubbard 			conv = 3;
600952a6212SJordan K. Hubbard 		dp--;
601952a6212SJordan K. Hubbard 	} else {
602952a6212SJordan K. Hubbard 		for (dp = cp; *--dp == ' ' || *dp == '.';);
603952a6212SJordan K. Hubbard 		dp++;
604952a6212SJordan K. Hubbard 	}
605952a6212SJordan K. Hubbard 
606952a6212SJordan K. Hubbard 	/*
607952a6212SJordan K. Hubbard 	 * Now convert the rest of the name
608952a6212SJordan K. Hubbard 	 */
609c4f02a89SMax Khon 	for (i = dp - un, j = 0; un < dp && j < 8; j++) {
610bddcdc51STim J. Robbins 		c = unix2doschr(&un, &i, pmp);
611c4f02a89SMax Khon 		if (c & 0xff00) {
612c4f02a89SMax Khon 			dn[j] = c >> 8;
613c4f02a89SMax Khon 			if (++j < 8) {
614c4f02a89SMax Khon 				dn[j] = c;
615c4f02a89SMax Khon 				continue;
616c4f02a89SMax Khon 			} else {
617c4f02a89SMax Khon 				conv = 3;
618c4f02a89SMax Khon 				dn[j-1] = ' ';
619c4f02a89SMax Khon 				break;
620c4f02a89SMax Khon 			}
621c4f02a89SMax Khon 		} else {
622c4f02a89SMax Khon 			dn[j] = c;
623c4f02a89SMax Khon 		}
624c4f02a89SMax Khon 		if (*(un - 1) != dn[j] && conv != 3)
625952a6212SJordan K. Hubbard 			conv = 2;
626cce0eefdSMike Smith 		if (dn[j] == 1) {
627cce0eefdSMike Smith 			conv = 3;
628cce0eefdSMike Smith 			dn[j] = '_';
629cce0eefdSMike Smith 		}
630cce0eefdSMike Smith 		if (dn[j] == 2) {
631952a6212SJordan K. Hubbard 			conv = 3;
632952a6212SJordan K. Hubbard 			dn[j--] = ' ';
633952a6212SJordan K. Hubbard 		}
634952a6212SJordan K. Hubbard 	}
635952a6212SJordan K. Hubbard 	if (un < dp)
636952a6212SJordan K. Hubbard 		conv = 3;
637952a6212SJordan K. Hubbard 	/*
638952a6212SJordan K. Hubbard 	 * If we didn't have any chars in filename,
639952a6212SJordan K. Hubbard 	 * generate a default
640952a6212SJordan K. Hubbard 	 */
641952a6212SJordan K. Hubbard 	if (!j)
642952a6212SJordan K. Hubbard 		dn[0] = '_';
643952a6212SJordan K. Hubbard 
644952a6212SJordan K. Hubbard 	/*
645952a6212SJordan K. Hubbard 	 * If there wasn't any char dropped,
646952a6212SJordan K. Hubbard 	 * there is no place for generation numbers
64727a0bc89SDoug Rabson 	 */
648952a6212SJordan K. Hubbard 	if (conv != 3) {
649952a6212SJordan K. Hubbard 		if (gen > 1)
650c4f02a89SMax Khon 			conv = 0;
651c4f02a89SMax Khon 		goto done;
652952a6212SJordan K. Hubbard 	}
653952a6212SJordan K. Hubbard 
654952a6212SJordan K. Hubbard 	/*
655952a6212SJordan K. Hubbard 	 * Now insert the generation number into the filename part
656952a6212SJordan K. Hubbard 	 */
657011cdb57SDmitrij Tejblum 	if (gen == 0)
658c4f02a89SMax Khon 		goto done;
659952a6212SJordan K. Hubbard 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
660952a6212SJordan K. Hubbard 		*--wcp = gen % 10 + '0';
661c4f02a89SMax Khon 	if (gen) {
662c4f02a89SMax Khon 		conv = 0;
663c4f02a89SMax Khon 		goto done;
664c4f02a89SMax Khon 	}
665952a6212SJordan K. Hubbard 	for (i = 8; dn[--i] == ' ';);
666952a6212SJordan K. Hubbard 	i++;
667952a6212SJordan K. Hubbard 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
668952a6212SJordan K. Hubbard 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
669c4f02a89SMax Khon 	/*
670c4f02a89SMax Khon 	 * Correct posision to where insert the generation number
671c4f02a89SMax Khon 	 */
672c4f02a89SMax Khon 	cp = dn;
673c4f02a89SMax Khon 	i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
674c4f02a89SMax Khon 
675952a6212SJordan K. Hubbard 	dn[i++] = '~';
676952a6212SJordan K. Hubbard 	while (wcp < gentext + sizeof(gentext))
677952a6212SJordan K. Hubbard 		dn[i++] = *wcp++;
678c4f02a89SMax Khon 
679c4f02a89SMax Khon 	/*
680c4f02a89SMax Khon 	 * Tail of the filename should be space
681c4f02a89SMax Khon 	 */
682c4f02a89SMax Khon 	while (i < 8)
683c4f02a89SMax Khon 		dn[i++] = ' ';
684c4f02a89SMax Khon 	conv = 3;
685c4f02a89SMax Khon 
686c4f02a89SMax Khon done:
687c4f02a89SMax Khon 	/*
688c4f02a89SMax Khon 	 * The first character cannot be E5,
689c4f02a89SMax Khon 	 * because that means a deleted entry
690c4f02a89SMax Khon 	 */
691c4f02a89SMax Khon 	if (dn[0] == 0xe5)
692c4f02a89SMax Khon 		dn[0] = SLOT_E5;
693c4f02a89SMax Khon 
694c4f02a89SMax Khon 	return conv;
695952a6212SJordan K. Hubbard }
696952a6212SJordan K. Hubbard 
697952a6212SJordan K. Hubbard /*
698952a6212SJordan K. Hubbard  * Create a Win95 long name directory entry
699952a6212SJordan K. Hubbard  * Note: assumes that the filename is valid,
700952a6212SJordan K. Hubbard  *	 i.e. doesn't consist solely of blanks and dots
701952a6212SJordan K. Hubbard  */
702952a6212SJordan K. Hubbard int
703c4f02a89SMax Khon unix2winfn(un, unlen, wep, cnt, chksum, pmp)
704952a6212SJordan K. Hubbard 	const u_char *un;
705bddcdc51STim J. Robbins 	size_t unlen;
706952a6212SJordan K. Hubbard 	struct winentry *wep;
707952a6212SJordan K. Hubbard 	int cnt;
708952a6212SJordan K. Hubbard 	int chksum;
709c4f02a89SMax Khon 	struct msdosfsmount *pmp;
710952a6212SJordan K. Hubbard {
711952a6212SJordan K. Hubbard 	u_int8_t *wcp;
712c4f02a89SMax Khon 	int i, end;
71313df76f2SAndrey A. Chernov 	u_int16_t code;
714952a6212SJordan K. Hubbard 
715952a6212SJordan K. Hubbard 	/*
716952a6212SJordan K. Hubbard 	 * Drop trailing blanks and dots
717952a6212SJordan K. Hubbard 	 */
718c4f02a89SMax Khon 	unlen = winLenFixup(un, unlen);
719952a6212SJordan K. Hubbard 
720c4f02a89SMax Khon 	/*
721c4f02a89SMax Khon 	 * Cut *un for this slot
722c4f02a89SMax Khon 	 */
723c4f02a89SMax Khon 	unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
724c4f02a89SMax Khon 			  pmp->pm_flags, pmp->pm_u2w);
725952a6212SJordan K. Hubbard 
726952a6212SJordan K. Hubbard 	/*
727952a6212SJordan K. Hubbard 	 * Initialize winentry to some useful default
728952a6212SJordan K. Hubbard 	 */
729952a6212SJordan K. Hubbard 	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
730952a6212SJordan K. Hubbard 	wep->weCnt = cnt;
731952a6212SJordan K. Hubbard 	wep->weAttributes = ATTR_WIN95;
732952a6212SJordan K. Hubbard 	wep->weReserved1 = 0;
733952a6212SJordan K. Hubbard 	wep->weChksum = chksum;
734952a6212SJordan K. Hubbard 	wep->weReserved2 = 0;
735952a6212SJordan K. Hubbard 
736952a6212SJordan K. Hubbard 	/*
737952a6212SJordan K. Hubbard 	 * Now convert the filename parts
738952a6212SJordan K. Hubbard 	 */
739c4f02a89SMax Khon 	end = 0;
740c4f02a89SMax Khon 	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
741bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
74213df76f2SAndrey A. Chernov 		*wcp++ = code;
74313df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
744c4f02a89SMax Khon 		if (!code)
745c4f02a89SMax Khon 			end = WIN_LAST;
746952a6212SJordan K. Hubbard 	}
747c4f02a89SMax Khon 	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
748bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
74913df76f2SAndrey A. Chernov 		*wcp++ = code;
75013df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
751c4f02a89SMax Khon 		if (!code)
752c4f02a89SMax Khon 			end = WIN_LAST;
753952a6212SJordan K. Hubbard 	}
754c4f02a89SMax Khon 	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
755bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
75613df76f2SAndrey A. Chernov 		*wcp++ = code;
75713df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
758c4f02a89SMax Khon 		if (!code)
759c4f02a89SMax Khon 			end = WIN_LAST;
760952a6212SJordan K. Hubbard 	}
761c4f02a89SMax Khon 	if (*un == '\0')
762c4f02a89SMax Khon 		end = WIN_LAST;
763c4f02a89SMax Khon 	wep->weCnt |= end;
764c4f02a89SMax Khon 	return !end;
7654d63452fSAndrey A. Chernov }
7664d63452fSAndrey A. Chernov 
767952a6212SJordan K. Hubbard /*
768952a6212SJordan K. Hubbard  * Compare our filename to the one in the Win95 entry
769952a6212SJordan K. Hubbard  * Returns the checksum or -1 if no match
770952a6212SJordan K. Hubbard  */
771952a6212SJordan K. Hubbard int
772c4f02a89SMax Khon winChkName(un, unlen, chksum, pmp)
773952a6212SJordan K. Hubbard 	const u_char *un;
774bddcdc51STim J. Robbins 	size_t unlen;
775952a6212SJordan K. Hubbard 	int chksum;
776c4f02a89SMax Khon 	struct msdosfsmount *pmp;
777952a6212SJordan K. Hubbard {
778bddcdc51STim J. Robbins 	size_t len;
779c4f02a89SMax Khon 	u_int16_t c1, c2;
780c4f02a89SMax Khon 	u_char *np;
781c4f02a89SMax Khon 	struct dirent dirbuf;
782952a6212SJordan K. Hubbard 
783952a6212SJordan K. Hubbard 	/*
784c4f02a89SMax Khon 	 * We alread have winentry in mbnambuf
785952a6212SJordan K. Hubbard 	 */
786c4f02a89SMax Khon 	if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen)
787952a6212SJordan K. Hubbard 		return -1;
788952a6212SJordan K. Hubbard 
789c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG
790c4f02a89SMax Khon 	printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
791c4f02a89SMax Khon 							dirbuf.d_name,
792c4f02a89SMax Khon 							dirbuf.d_namlen);
793c4f02a89SMax Khon #endif
794952a6212SJordan K. Hubbard 
795952a6212SJordan K. Hubbard 	/*
796952a6212SJordan K. Hubbard 	 * Compare the name parts
797952a6212SJordan K. Hubbard 	 */
798c4f02a89SMax Khon 	len = dirbuf.d_namlen;
799c4f02a89SMax Khon 	if (unlen != len)
800c4f02a89SMax Khon 		return -2;
801c4f02a89SMax Khon 
802c4f02a89SMax Khon 	for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
803c4f02a89SMax Khon 		/*
8040f4e4130SMax Khon 		 * Comparison must be case insensitive, because FAT disallows
8050f4e4130SMax Khon 		 * to look up or create files in case sensitive even when
8060f4e4130SMax Khon 		 * it's a long file name.
807c4f02a89SMax Khon 		 */
808bddcdc51STim J. Robbins 		c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
809bddcdc51STim J. Robbins 		c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
810b998efa9SAndrey A. Chernov 		if (c1 != c2)
811c4f02a89SMax Khon 			return -2;
812952a6212SJordan K. Hubbard 	}
813952a6212SJordan K. Hubbard 	return chksum;
814952a6212SJordan K. Hubbard }
815952a6212SJordan K. Hubbard 
816952a6212SJordan K. Hubbard /*
817952a6212SJordan K. Hubbard  * Convert Win95 filename to dirbuf.
818952a6212SJordan K. Hubbard  * Returns the checksum or -1 if impossible
819952a6212SJordan K. Hubbard  */
820952a6212SJordan K. Hubbard int
821c4f02a89SMax Khon win2unixfn(wep, chksum, pmp)
822952a6212SJordan K. Hubbard 	struct winentry *wep;
823952a6212SJordan K. Hubbard 	int chksum;
824c4f02a89SMax Khon 	struct msdosfsmount *pmp;
825952a6212SJordan K. Hubbard {
826952a6212SJordan K. Hubbard 	u_int8_t *cp;
827c4f02a89SMax Khon 	u_int8_t *np, name[WIN_CHARS * 2 + 1];
8282b5b6623SAndrey A. Chernov 	u_int16_t code;
829952a6212SJordan K. Hubbard 	int i;
830952a6212SJordan K. Hubbard 
831952a6212SJordan K. Hubbard 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
832952a6212SJordan K. Hubbard 	    || !(wep->weCnt&WIN_CNT))
833952a6212SJordan K. Hubbard 		return -1;
834952a6212SJordan K. Hubbard 
835952a6212SJordan K. Hubbard 	/*
836952a6212SJordan K. Hubbard 	 * First compare checksums
837952a6212SJordan K. Hubbard 	 */
838952a6212SJordan K. Hubbard 	if (wep->weCnt&WIN_LAST) {
839952a6212SJordan K. Hubbard 		chksum = wep->weChksum;
840952a6212SJordan K. Hubbard 	} else if (chksum != wep->weChksum)
841952a6212SJordan K. Hubbard 		chksum = -1;
842952a6212SJordan K. Hubbard 	if (chksum == -1)
843952a6212SJordan K. Hubbard 		return -1;
844952a6212SJordan K. Hubbard 
845952a6212SJordan K. Hubbard 	/*
846952a6212SJordan K. Hubbard 	 * Convert the name parts
847952a6212SJordan K. Hubbard 	 */
848c4f02a89SMax Khon 	np = name;
849952a6212SJordan K. Hubbard 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
8502b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
8512b5b6623SAndrey A. Chernov 		switch (code) {
852952a6212SJordan K. Hubbard 		case 0:
8532b5b6623SAndrey A. Chernov 			*np = '\0';
854c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
855952a6212SJordan K. Hubbard 			return chksum;
856952a6212SJordan K. Hubbard 		case '/':
8572b5b6623SAndrey A. Chernov 			*np = '\0';
858952a6212SJordan K. Hubbard 			return -1;
8592b5b6623SAndrey A. Chernov 		default:
860c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
861c4f02a89SMax Khon 			if (code & 0xff00)
862c4f02a89SMax Khon 				*np++ = code >> 8;
8632b5b6623SAndrey A. Chernov 			*np++ = code;
8642b5b6623SAndrey A. Chernov 			break;
865952a6212SJordan K. Hubbard 		}
8662b5b6623SAndrey A. Chernov 		cp += 2;
867952a6212SJordan K. Hubbard 	}
868952a6212SJordan K. Hubbard 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
8692b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
8702b5b6623SAndrey A. Chernov 		switch (code) {
871952a6212SJordan K. Hubbard 		case 0:
8722b5b6623SAndrey A. Chernov 			*np = '\0';
873c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
874952a6212SJordan K. Hubbard 			return chksum;
875952a6212SJordan K. Hubbard 		case '/':
8762b5b6623SAndrey A. Chernov 			*np = '\0';
877952a6212SJordan K. Hubbard 			return -1;
8782b5b6623SAndrey A. Chernov 		default:
879c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
880c4f02a89SMax Khon 			if (code & 0xff00)
881c4f02a89SMax Khon 				*np++ = code >> 8;
8822b5b6623SAndrey A. Chernov 			*np++ = code;
8832b5b6623SAndrey A. Chernov 			break;
884952a6212SJordan K. Hubbard 		}
8852b5b6623SAndrey A. Chernov 		cp += 2;
886952a6212SJordan K. Hubbard 	}
887952a6212SJordan K. Hubbard 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
8882b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
8892b5b6623SAndrey A. Chernov 		switch (code) {
890952a6212SJordan K. Hubbard 		case 0:
8912b5b6623SAndrey A. Chernov 			*np = '\0';
892c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
893952a6212SJordan K. Hubbard 			return chksum;
894952a6212SJordan K. Hubbard 		case '/':
8952b5b6623SAndrey A. Chernov 			*np = '\0';
896952a6212SJordan K. Hubbard 			return -1;
8972b5b6623SAndrey A. Chernov 		default:
898c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
899c4f02a89SMax Khon 			if (code & 0xff00)
900c4f02a89SMax Khon 				*np++ = code >> 8;
9012b5b6623SAndrey A. Chernov 			*np++ = code;
9022b5b6623SAndrey A. Chernov 			break;
903952a6212SJordan K. Hubbard 		}
9042b5b6623SAndrey A. Chernov 		cp += 2;
905952a6212SJordan K. Hubbard 	}
906c4f02a89SMax Khon 	*np = '\0';
907c4f02a89SMax Khon 	mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
908952a6212SJordan K. Hubbard 	return chksum;
909952a6212SJordan K. Hubbard }
910952a6212SJordan K. Hubbard 
911952a6212SJordan K. Hubbard /*
912952a6212SJordan K. Hubbard  * Compute the checksum of a DOS filename for Win95 use
913952a6212SJordan K. Hubbard  */
914952a6212SJordan K. Hubbard u_int8_t
915952a6212SJordan K. Hubbard winChksum(name)
916952a6212SJordan K. Hubbard 	u_int8_t *name;
917952a6212SJordan K. Hubbard {
918952a6212SJordan K. Hubbard 	int i;
919952a6212SJordan K. Hubbard 	u_int8_t s;
920952a6212SJordan K. Hubbard 
921952a6212SJordan K. Hubbard 	for (s = 0, i = 11; --i >= 0; s += *name++)
922952a6212SJordan K. Hubbard 		s = (s << 7)|(s >> 1);
923952a6212SJordan K. Hubbard 	return s;
924952a6212SJordan K. Hubbard }
925952a6212SJordan K. Hubbard 
926952a6212SJordan K. Hubbard /*
927952a6212SJordan K. Hubbard  * Determine the number of slots necessary for Win95 names
928952a6212SJordan K. Hubbard  */
929952a6212SJordan K. Hubbard int
930c4f02a89SMax Khon winSlotCnt(un, unlen, pmp)
931952a6212SJordan K. Hubbard 	const u_char *un;
932bddcdc51STim J. Robbins 	size_t unlen;
933c4f02a89SMax Khon 	struct msdosfsmount *pmp;
934952a6212SJordan K. Hubbard {
935c4f02a89SMax Khon 	size_t wlen;
936c4f02a89SMax Khon 	char wn[WIN_MAXLEN * 2 + 1], *wnp;
937c4f02a89SMax Khon 
93869a36f24SMike Smith 	unlen = winLenFixup(un, unlen);
939c4f02a89SMax Khon 
940c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
941c4f02a89SMax Khon 		wlen = WIN_MAXLEN * 2;
942c4f02a89SMax Khon 		wnp = wn;
943bddcdc51STim J. Robbins 		msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
944c4f02a89SMax Khon 		if (unlen > 0)
945c4f02a89SMax Khon 			return 0;
946c4f02a89SMax Khon 		return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
947c4f02a89SMax Khon 	}
948c4f02a89SMax Khon 
949952a6212SJordan K. Hubbard 	if (unlen > WIN_MAXLEN)
950952a6212SJordan K. Hubbard 		return 0;
951952a6212SJordan K. Hubbard 	return howmany(unlen, WIN_CHARS);
95227a0bc89SDoug Rabson }
95369a36f24SMike Smith 
95469a36f24SMike Smith /*
95569a36f24SMike Smith  * Determine the number of bytes neccesary for Win95 names
95669a36f24SMike Smith  */
957bddcdc51STim J. Robbins size_t
95869a36f24SMike Smith winLenFixup(un, unlen)
95969a36f24SMike Smith 	const u_char* un;
960bddcdc51STim J. Robbins 	size_t unlen;
96169a36f24SMike Smith {
96269a36f24SMike Smith 	for (un += unlen; unlen > 0; unlen--)
96369a36f24SMike Smith 		if (*--un != ' ' && *un != '.')
96469a36f24SMike Smith 			break;
96569a36f24SMike Smith 	return unlen;
96669a36f24SMike Smith }
967c4f02a89SMax Khon 
968c4f02a89SMax Khon /*
969c4f02a89SMax Khon  * Store an area with multi byte string instr, and reterns left
970c4f02a89SMax Khon  * byte of instr and moves pointer forward. The area's size is
971c4f02a89SMax Khon  * inlen or outlen.
972c4f02a89SMax Khon  */
973c4f02a89SMax Khon static int
974bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
975c4f02a89SMax Khon {
976c4f02a89SMax Khon 	char *outp, outstr[outlen * weight + 1];
977c4f02a89SMax Khon 
978c4f02a89SMax Khon 	if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
979c4f02a89SMax Khon 		outp = outstr;
980c4f02a89SMax Khon 		outlen *= weight;
981bddcdc51STim J. Robbins 		msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
982c4f02a89SMax Khon 		return (inlen);
983c4f02a89SMax Khon 	}
984c4f02a89SMax Khon 
985c4f02a89SMax Khon 	(*instr) += min(inlen, outlen);
986c4f02a89SMax Khon 	return (inlen - min(inlen, outlen));
987c4f02a89SMax Khon }
988c4f02a89SMax Khon 
989c4f02a89SMax Khon /*
990c4f02a89SMax Khon  * Convert DOS char to Local char
991c4f02a89SMax Khon  */
992c4f02a89SMax Khon static u_int16_t
993c4f02a89SMax Khon dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
994c4f02a89SMax Khon {
995c4f02a89SMax Khon 	u_char c;
996c4f02a89SMax Khon 	char *outp, outbuf[3];
997c4f02a89SMax Khon 	u_int16_t wc;
998c4f02a89SMax Khon 	size_t len, olen;
999c4f02a89SMax Khon 
1000c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1001c4f02a89SMax Khon 		olen = len = 2;
1002c4f02a89SMax Khon 		outp = outbuf;
1003c4f02a89SMax Khon 
1004c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
1005c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
1006c4f02a89SMax Khon 						  ilen, &outp, &olen, KICONV_LOWER);
1007c4f02a89SMax Khon 		else
1008c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
1009c4f02a89SMax Khon 					     ilen, &outp, &olen);
1010c4f02a89SMax Khon 		len -= olen;
1011c4f02a89SMax Khon 
1012c4f02a89SMax Khon 		/*
1013c4f02a89SMax Khon 		 * return '?' if failed to convert
1014c4f02a89SMax Khon 		 */
1015c4f02a89SMax Khon 		if (len == 0) {
1016c4f02a89SMax Khon 			(*ilen)--;
1017c4f02a89SMax Khon 			(*instr)++;
1018c4f02a89SMax Khon 			return ('?');
1019c4f02a89SMax Khon 		}
1020c4f02a89SMax Khon 
1021c4f02a89SMax Khon 		wc = 0;
1022c4f02a89SMax Khon 		while(len--)
1023c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1024c4f02a89SMax Khon 		return (wc);
1025c4f02a89SMax Khon 	}
1026c4f02a89SMax Khon 
1027c4f02a89SMax Khon 	(*ilen)--;
1028c4f02a89SMax Khon 	c = *(*instr)++;
1029c4f02a89SMax Khon 	c = dos2unix[c];
1030c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
1031c4f02a89SMax Khon 		c = u2l[c];
1032c4f02a89SMax Khon 	return ((u_int16_t)c);
1033c4f02a89SMax Khon }
1034c4f02a89SMax Khon 
1035c4f02a89SMax Khon /*
1036c4f02a89SMax Khon  * Convert Local char to DOS char
1037c4f02a89SMax Khon  */
1038c4f02a89SMax Khon static u_int16_t
1039c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
1040c4f02a89SMax Khon {
1041c4f02a89SMax Khon 	u_char c;
1042c4f02a89SMax Khon 	char *up, *outp, unicode[3], outbuf[3];
1043c4f02a89SMax Khon 	u_int16_t wc;
1044c4f02a89SMax Khon 	size_t len, ulen, olen;
1045c4f02a89SMax Khon 
1046c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1047c4f02a89SMax Khon 		/*
1048c4f02a89SMax Khon 		 * to hide an invisible character, using a unicode filter
1049c4f02a89SMax Khon 		 */
1050c4f02a89SMax Khon 		ulen = 2;
1051c4f02a89SMax Khon 		len = *ilen;
1052c4f02a89SMax Khon 		up = unicode;
1053c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1054c4f02a89SMax Khon 				     ilen, &up, &ulen);
1055c4f02a89SMax Khon 
1056c4f02a89SMax Khon 		/*
1057c4f02a89SMax Khon 		 * cannot be converted
1058c4f02a89SMax Khon 		 */
1059c4f02a89SMax Khon 		if (ulen == 2) {
1060c4f02a89SMax Khon 			(*ilen)--;
1061c4f02a89SMax Khon 			(*instr)++;
1062c4f02a89SMax Khon 			return (0);
1063c4f02a89SMax Khon 		}
1064c4f02a89SMax Khon 
1065c4f02a89SMax Khon 		/*
1066c4f02a89SMax Khon 		 * return magic number for ascii char
1067c4f02a89SMax Khon 		 */
1068c4f02a89SMax Khon 		if ((len - *ilen) == 1) {
1069c4f02a89SMax Khon 			c = *(*instr -1);
1070c4f02a89SMax Khon 			if (! (c & 0x80)) {
1071c4f02a89SMax Khon 				c = unix2dos[c];
1072c4f02a89SMax Khon 				if (c <= 2)
1073c4f02a89SMax Khon 					return (c);
1074c4f02a89SMax Khon 			}
1075c4f02a89SMax Khon 		}
1076c4f02a89SMax Khon 
1077c4f02a89SMax Khon 		/*
1078c4f02a89SMax Khon 		 * now convert using libiconv
1079c4f02a89SMax Khon 		 */
1080c4f02a89SMax Khon 		*instr -= len - *ilen;
1081c4f02a89SMax Khon 		*ilen = (int)len;
1082c4f02a89SMax Khon 
1083c4f02a89SMax Khon 		olen = len = 2;
1084c4f02a89SMax Khon 		outp = outbuf;
1085c4f02a89SMax Khon 		msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
1086c4f02a89SMax Khon 					  ilen, &outp, &olen, KICONV_FROM_UPPER);
1087c4f02a89SMax Khon 		len -= olen;
1088c4f02a89SMax Khon 		wc = 0;
1089c4f02a89SMax Khon 		while(len--)
1090c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1091c4f02a89SMax Khon 		return (wc);
1092c4f02a89SMax Khon 	}
1093c4f02a89SMax Khon 
1094c4f02a89SMax Khon 	(*ilen)--;
1095c4f02a89SMax Khon 	c = *(*instr)++;
1096c4f02a89SMax Khon 	c = l2u[c];
1097c4f02a89SMax Khon 	c = unix2dos[c];
1098c4f02a89SMax Khon 	return ((u_int16_t)c);
1099c4f02a89SMax Khon }
1100c4f02a89SMax Khon 
1101c4f02a89SMax Khon /*
1102c4f02a89SMax Khon  * Convert Windows char to Local char
1103c4f02a89SMax Khon  */
1104c4f02a89SMax Khon static u_int16_t
1105c4f02a89SMax Khon win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
1106c4f02a89SMax Khon {
1107c4f02a89SMax Khon 	u_char *inp, *outp, inbuf[3], outbuf[3];
1108c4f02a89SMax Khon 	size_t ilen, olen, len;
1109c4f02a89SMax Khon 
1110c4f02a89SMax Khon 	if (wc == 0)
1111c4f02a89SMax Khon 		return (0);
1112c4f02a89SMax Khon 
1113c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1114c4f02a89SMax Khon 		inbuf[0] = (u_char)(wc>>8);
1115c4f02a89SMax Khon 		inbuf[1] = (u_char)wc;
1116c4f02a89SMax Khon 		inbuf[2] = '\0';
1117c4f02a89SMax Khon 
1118c4f02a89SMax Khon 		ilen = olen = len = 2;
1119c4f02a89SMax Khon 		inp = inbuf;
1120c4f02a89SMax Khon 		outp = outbuf;
1121c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
1122c4f02a89SMax Khon 				     (char **)&outp, &olen);
1123c4f02a89SMax Khon 		len -= olen;
1124c4f02a89SMax Khon 
1125c4f02a89SMax Khon 		/*
1126c4f02a89SMax Khon 		 * return '?' if failed to convert
1127c4f02a89SMax Khon 		 */
1128c4f02a89SMax Khon 		if (len == 0) {
1129c4f02a89SMax Khon 			wc = '?';
1130c4f02a89SMax Khon 			return (wc);
1131c4f02a89SMax Khon 		}
1132c4f02a89SMax Khon 
1133c4f02a89SMax Khon 		wc = 0;
1134c4f02a89SMax Khon 		while(len--)
1135c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1136c4f02a89SMax Khon 		return (wc);
1137c4f02a89SMax Khon 	}
1138c4f02a89SMax Khon 
1139c4f02a89SMax Khon 	if (wc & 0xff00)
1140c4f02a89SMax Khon 		wc = '?';
1141c4f02a89SMax Khon 
1142c4f02a89SMax Khon 	return (wc);
1143c4f02a89SMax Khon }
1144c4f02a89SMax Khon 
1145c4f02a89SMax Khon /*
1146c4f02a89SMax Khon  * Convert Local char to Windows char
1147c4f02a89SMax Khon  */
1148c4f02a89SMax Khon static u_int16_t
1149c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1150c4f02a89SMax Khon {
1151c4f02a89SMax Khon 	u_char *outp, outbuf[3];
1152c4f02a89SMax Khon 	u_int16_t wc;
1153c4f02a89SMax Khon 	size_t olen;
1154c4f02a89SMax Khon 
1155c4f02a89SMax Khon 	if (*ilen == 0)
1156c4f02a89SMax Khon 		return (0);
1157c4f02a89SMax Khon 
1158c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1159c4f02a89SMax Khon 		outp = outbuf;
1160c4f02a89SMax Khon 		olen = 2;
1161c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
1162c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
1163c4f02a89SMax Khon 						  ilen, (char **)&outp, &olen,
1164c4f02a89SMax Khon 						  KICONV_FROM_LOWER);
1165c4f02a89SMax Khon 		else
1166c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1167c4f02a89SMax Khon 					     ilen, (char **)&outp, &olen);
1168c4f02a89SMax Khon 
1169c4f02a89SMax Khon 		/*
1170c4f02a89SMax Khon 		 * return '0' if end of filename
1171c4f02a89SMax Khon 		 */
1172c4f02a89SMax Khon 		if (olen == 2)
1173c4f02a89SMax Khon 			return (0);
1174c4f02a89SMax Khon 
1175c4f02a89SMax Khon 		wc = (outbuf[0]<<8) | outbuf[1];
1176c4f02a89SMax Khon 
1177c4f02a89SMax Khon 		return (wc);
1178c4f02a89SMax Khon 	}
1179c4f02a89SMax Khon 
1180c4f02a89SMax Khon 	(*ilen)--;
1181c4f02a89SMax Khon 	wc = (*instr)[0];
1182c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
1183c4f02a89SMax Khon 		wc = u2l[wc];
1184c4f02a89SMax Khon 	(*instr)++;
1185c4f02a89SMax Khon 	return (wc);
1186c4f02a89SMax Khon }
1187c4f02a89SMax Khon 
1188c4f02a89SMax Khon /*
1189c4f02a89SMax Khon  * Make subent empty
1190c4f02a89SMax Khon  */
1191c4f02a89SMax Khon void
1192c4f02a89SMax Khon mbnambuf_init(void)
1193c4f02a89SMax Khon {
1194c4f02a89SMax Khon 	int i;
1195c4f02a89SMax Khon 
1196c4f02a89SMax Khon 	for (i = 0; i < WIN_MAXSUBENTRIES; i++) {
1197c4f02a89SMax Khon 		if (subent[i].p) {
1198c4f02a89SMax Khon 			free(subent[i].p, M_MSDOSFSMNT);
1199c4f02a89SMax Khon 			subent[i].p = NULL;
1200c4f02a89SMax Khon 			subent[i].n = 0;
1201c4f02a89SMax Khon 		}
1202c4f02a89SMax Khon 	}
1203c4f02a89SMax Khon }
1204c4f02a89SMax Khon 
1205c4f02a89SMax Khon /*
1206c4f02a89SMax Khon  * Write a subent entry from a slot
1207c4f02a89SMax Khon  */
1208c4f02a89SMax Khon void
1209c4f02a89SMax Khon mbnambuf_write(char *name, int id)
1210c4f02a89SMax Khon {
1211c4f02a89SMax Khon 	if (subent[id].p) {
1212c4f02a89SMax Khon 		printf("mbnambuf_write(): %s -> %s\n", subent[id].p, name);
1213c4f02a89SMax Khon 		free(subent[id].p, M_MSDOSFSMNT);
1214c4f02a89SMax Khon 	}
1215c4f02a89SMax Khon 	subent[id].n = strlen(name);
1216c4f02a89SMax Khon 	subent[id].p = malloc(subent[id].n + 1, M_MSDOSFSMNT, M_WAITOK);
1217c4f02a89SMax Khon 	strcpy(subent[id].p, name);
1218c4f02a89SMax Khon }
1219c4f02a89SMax Khon 
1220c4f02a89SMax Khon /*
1221c4f02a89SMax Khon  * Combine each subents to the *dp and initialize subent
1222c4f02a89SMax Khon  */
1223c4f02a89SMax Khon char *
1224c4f02a89SMax Khon mbnambuf_flush(struct dirent *dp)
1225c4f02a89SMax Khon {
1226c4f02a89SMax Khon 	int i;
1227c4f02a89SMax Khon 	char *name = dp->d_name;
1228c4f02a89SMax Khon 
1229c4f02a89SMax Khon 	*name = '\0';
1230c4f02a89SMax Khon 	dp->d_namlen = 0;
1231c4f02a89SMax Khon 	for (i = 0; i < WIN_MAXSUBENTRIES; i++) {
1232c4f02a89SMax Khon 		if (subent[i].p) {
1233c4f02a89SMax Khon 			if (dp->d_namlen + subent[i].n > sizeof(dp->d_name) - 1) {
1234c4f02a89SMax Khon 				mbnambuf_init();
1235c4f02a89SMax Khon 				return (NULL);
1236c4f02a89SMax Khon 			}
1237c4f02a89SMax Khon 			strcpy(name, subent[i].p);
1238c4f02a89SMax Khon 			dp->d_namlen += subent[i].n;
1239c4f02a89SMax Khon 			name += subent[i].n;
1240c4f02a89SMax Khon 		}
1241c4f02a89SMax Khon 	}
1242c4f02a89SMax Khon 	mbnambuf_init();
1243c4f02a89SMax Khon 	return (dp->d_name);
1244c4f02a89SMax Khon }
1245