xref: /freebsd/sys/fs/msdosfs/msdosfs_conv.c (revision 4cdb1483528de7824a9aa57e364b36699c82aece)
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 
1044cdb1483SNate Lawson static char	*nambuf_ptr;
1054cdb1483SNate Lawson static size_t	nambuf_len;
1064cdb1483SNate Lawson static int	nambuf_max_id;
10713df76f2SAndrey A. Chernov 
10827a0bc89SDoug Rabson /*
10927a0bc89SDoug Rabson  * Convert the unix version of time to dos's idea of time to be used in
11027a0bc89SDoug Rabson  * file timestamps. The passed in unix time is assumed to be in GMT.
11127a0bc89SDoug Rabson  */
11227a0bc89SDoug Rabson void
113952a6212SJordan K. Hubbard unix2dostime(tsp, ddp, dtp, dhp)
11427a0bc89SDoug Rabson 	struct timespec *tsp;
115952a6212SJordan K. Hubbard 	u_int16_t *ddp;
116952a6212SJordan K. Hubbard 	u_int16_t *dtp;
117952a6212SJordan K. Hubbard 	u_int8_t *dhp;
11827a0bc89SDoug Rabson {
11927a0bc89SDoug Rabson 	u_long t;
12027a0bc89SDoug Rabson 	u_long days;
12127a0bc89SDoug Rabson 	u_long inc;
12227a0bc89SDoug Rabson 	u_long year;
12327a0bc89SDoug Rabson 	u_long month;
12427a0bc89SDoug Rabson 	u_short *months;
12527a0bc89SDoug Rabson 
12627a0bc89SDoug Rabson 	/*
12727a0bc89SDoug Rabson 	 * If the time from the last conversion is the same as now, then
12827a0bc89SDoug Rabson 	 * skip the computations and use the saved result.
12927a0bc89SDoug Rabson 	 */
13091f1c2b3SPoul-Henning Kamp 	t = tsp->tv_sec - (tz_minuteswest * 60)
131b0b32125SAndrey A. Chernov 	    - (wall_cmos_clock ? adjkerntz : 0);
132ef5fdc52SAndrey A. Chernov 	    /* - daylight savings time correction */
133952a6212SJordan K. Hubbard 	t &= ~1;
13427a0bc89SDoug Rabson 	if (lasttime != t) {
13527a0bc89SDoug Rabson 		lasttime = t;
136952a6212SJordan K. Hubbard 		lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
13727a0bc89SDoug Rabson 		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
13827a0bc89SDoug Rabson 		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
13927a0bc89SDoug Rabson 
14027a0bc89SDoug Rabson 		/*
14127a0bc89SDoug Rabson 		 * If the number of days since 1970 is the same as the last
14227a0bc89SDoug Rabson 		 * time we did the computation then skip all this leap year
14327a0bc89SDoug Rabson 		 * and month stuff.
14427a0bc89SDoug Rabson 		 */
14527a0bc89SDoug Rabson 		days = t / (24 * 60 * 60);
14627a0bc89SDoug Rabson 		if (days != lastday) {
14727a0bc89SDoug Rabson 			lastday = days;
14827a0bc89SDoug Rabson 			for (year = 1970;; year++) {
14927a0bc89SDoug Rabson 				inc = year & 0x03 ? 365 : 366;
15027a0bc89SDoug Rabson 				if (days < inc)
15127a0bc89SDoug Rabson 					break;
15227a0bc89SDoug Rabson 				days -= inc;
15327a0bc89SDoug Rabson 			}
15427a0bc89SDoug Rabson 			months = year & 0x03 ? regyear : leapyear;
155e73bdb51SBruce Evans 			for (month = 0; days >= months[month]; month++)
156aeab1b50SAndrey A. Chernov 				;
157aeab1b50SAndrey A. Chernov 			if (month > 0)
158aeab1b50SAndrey A. Chernov 				days -= months[month - 1];
15927a0bc89SDoug Rabson 			lastddate = ((days + 1) << DD_DAY_SHIFT)
16027a0bc89SDoug Rabson 			    + ((month + 1) << DD_MONTH_SHIFT);
16127a0bc89SDoug Rabson 			/*
16227a0bc89SDoug Rabson 			 * Remember dos's idea of time is relative to 1980.
16327a0bc89SDoug Rabson 			 * unix's is relative to 1970.  If somehow we get a
16427a0bc89SDoug Rabson 			 * time before 1980 then don't give totally crazy
16527a0bc89SDoug Rabson 			 * results.
16627a0bc89SDoug Rabson 			 */
16727a0bc89SDoug Rabson 			if (year > 1980)
16827a0bc89SDoug Rabson 				lastddate += (year - 1980) << DD_YEAR_SHIFT;
16927a0bc89SDoug Rabson 		}
17027a0bc89SDoug Rabson 	}
171952a6212SJordan K. Hubbard 	if (dtp)
17227a0bc89SDoug Rabson 		*dtp = lastdtime;
173952a6212SJordan K. Hubbard 	if (dhp)
174952a6212SJordan K. Hubbard 		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
175952a6212SJordan K. Hubbard 
17627a0bc89SDoug Rabson 	*ddp = lastddate;
17727a0bc89SDoug Rabson }
17827a0bc89SDoug Rabson 
17927a0bc89SDoug Rabson /*
18027a0bc89SDoug Rabson  * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
18127a0bc89SDoug Rabson  * interval there were 8 regular years and 2 leap years.
18227a0bc89SDoug Rabson  */
18327a0bc89SDoug Rabson #define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
18427a0bc89SDoug Rabson 
185303b270bSEivind Eklund static u_short lastdosdate;
186303b270bSEivind Eklund static u_long  lastseconds;
18727a0bc89SDoug Rabson 
18827a0bc89SDoug Rabson /*
18927a0bc89SDoug Rabson  * Convert from dos' idea of time to unix'. This will probably only be
19027a0bc89SDoug Rabson  * called from the stat(), and fstat() system calls and so probably need
19127a0bc89SDoug Rabson  * not be too efficient.
19227a0bc89SDoug Rabson  */
19327a0bc89SDoug Rabson void
194952a6212SJordan K. Hubbard dos2unixtime(dd, dt, dh, tsp)
195952a6212SJordan K. Hubbard 	u_int dd;
196952a6212SJordan K. Hubbard 	u_int dt;
197952a6212SJordan K. Hubbard 	u_int dh;
19827a0bc89SDoug Rabson 	struct timespec *tsp;
19927a0bc89SDoug Rabson {
20027a0bc89SDoug Rabson 	u_long seconds;
201a98ca469SPoul-Henning Kamp 	u_long month;
202a98ca469SPoul-Henning Kamp 	u_long year;
20327a0bc89SDoug Rabson 	u_long days;
20427a0bc89SDoug Rabson 	u_short *months;
20527a0bc89SDoug Rabson 
206952a6212SJordan K. Hubbard 	if (dd == 0) {
207952a6212SJordan K. Hubbard 		/*
208952a6212SJordan K. Hubbard 		 * Uninitialized field, return the epoch.
209952a6212SJordan K. Hubbard 		 */
210952a6212SJordan K. Hubbard 		tsp->tv_sec = 0;
211952a6212SJordan K. Hubbard 		tsp->tv_nsec = 0;
212952a6212SJordan K. Hubbard 		return;
213952a6212SJordan K. Hubbard 	}
21463e4f22aSBruce Evans 	seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
21527a0bc89SDoug Rabson 	    + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
216952a6212SJordan K. Hubbard 	    + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
217952a6212SJordan K. Hubbard 	    + dh / 100;
21827a0bc89SDoug Rabson 	/*
21927a0bc89SDoug Rabson 	 * If the year, month, and day from the last conversion are the
22027a0bc89SDoug Rabson 	 * same then use the saved value.
22127a0bc89SDoug Rabson 	 */
22227a0bc89SDoug Rabson 	if (lastdosdate != dd) {
22327a0bc89SDoug Rabson 		lastdosdate = dd;
22427a0bc89SDoug Rabson 		days = 0;
22527a0bc89SDoug Rabson 		year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
226aeab1b50SAndrey A. Chernov 		days = year * 365;
227aeab1b50SAndrey A. Chernov 		days += year / 4 + 1;	/* add in leap days */
228aeab1b50SAndrey A. Chernov 		if ((year & 0x03) == 0)
229aeab1b50SAndrey A. Chernov 			days--;		/* if year is a leap year */
23027a0bc89SDoug Rabson 		months = year & 0x03 ? regyear : leapyear;
23127a0bc89SDoug Rabson 		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
232aeab1b50SAndrey A. Chernov 		if (month < 1 || month > 12) {
233952a6212SJordan K. Hubbard 			printf("dos2unixtime(): month value out of range (%ld)\n",
23427a0bc89SDoug Rabson 			    month);
23527a0bc89SDoug Rabson 			month = 1;
23627a0bc89SDoug Rabson 		}
237aeab1b50SAndrey A. Chernov 		if (month > 1)
238aeab1b50SAndrey A. Chernov 			days += months[month - 2];
23927a0bc89SDoug Rabson 		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
24027a0bc89SDoug Rabson 		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
24127a0bc89SDoug Rabson 	}
24291f1c2b3SPoul-Henning Kamp 	tsp->tv_sec = seconds + lastseconds + (tz_minuteswest * 60)
243b0b32125SAndrey A. Chernov 	     + adjkerntz;
244ef5fdc52SAndrey A. Chernov 	     /* + daylight savings time correction */
245952a6212SJordan K. Hubbard 	tsp->tv_nsec = (dh % 100) * 10000000;
24627a0bc89SDoug Rabson }
24727a0bc89SDoug Rabson 
248cce0eefdSMike Smith /*
249cce0eefdSMike Smith  * 0 - character disallowed in long file name.
250cce0eefdSMike Smith  * 1 - character should be replaced by '_' in DOS file name,
251cce0eefdSMike Smith  *     and generation number inserted.
252cce0eefdSMike Smith  * 2 - character ('.' and ' ') should be skipped in DOS file name,
253cce0eefdSMike Smith  *     and generation number inserted.
254cce0eefdSMike Smith  */
255952a6212SJordan K. Hubbard static u_char
256952a6212SJordan K. Hubbard unix2dos[256] = {
257c4f02a89SMax Khon /* iso8859-1 -> cp850 */
258952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
259952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
260952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
261952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
262cce0eefdSMike Smith 	2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
263cce0eefdSMike Smith 	0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,	/* 28-2f */
264952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
265cce0eefdSMike Smith 	0x38, 0x39, 0,    1,    0,    1,    0,    0,	/* 38-3f */
266952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
267952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
268952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
269cce0eefdSMike Smith 	0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f,	/* 58-5f */
270952a6212SJordan K. Hubbard 	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
271952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
272952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
273952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
274952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
275952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
276952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
277952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
278952a6212SJordan K. Hubbard 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
279952a6212SJordan K. Hubbard 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
280952a6212SJordan K. Hubbard 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
281952a6212SJordan K. Hubbard 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
282952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
283952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
284952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
285952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
286952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
287952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
288952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
289952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
290952a6212SJordan K. Hubbard };
291952a6212SJordan K. Hubbard 
292952a6212SJordan K. Hubbard static u_char
293952a6212SJordan K. Hubbard dos2unix[256] = {
294c4f02a89SMax Khon /* cp850 -> iso8859-1 */
295952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
296952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
297952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
298952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
299952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
300952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
301952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
302952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
303952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
304952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
305952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
306952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
307952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
308952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
309952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
310952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
311952a6212SJordan K. Hubbard 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
312952a6212SJordan K. Hubbard 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
313952a6212SJordan K. Hubbard 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
314952a6212SJordan K. Hubbard 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
315952a6212SJordan K. Hubbard 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
316952a6212SJordan K. Hubbard 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
317952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
318952a6212SJordan K. Hubbard 	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
319952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
320952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
321952a6212SJordan K. Hubbard 	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
322952a6212SJordan K. Hubbard 	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
323952a6212SJordan K. Hubbard 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
324952a6212SJordan K. Hubbard 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
325952a6212SJordan K. Hubbard 	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
326952a6212SJordan K. Hubbard 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
327952a6212SJordan K. Hubbard };
328952a6212SJordan K. Hubbard 
329952a6212SJordan K. Hubbard static u_char
330952a6212SJordan K. Hubbard u2l[256] = {
331c4f02a89SMax Khon /* tolower */
332952a6212SJordan K. Hubbard 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
333952a6212SJordan K. Hubbard 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
334952a6212SJordan K. Hubbard 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
335952a6212SJordan K. Hubbard 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
336952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
337952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
338952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
339952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
340952a6212SJordan K. Hubbard 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
341952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
342952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
343952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
344952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
345952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
346952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
347952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
348952a6212SJordan K. Hubbard 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
349952a6212SJordan K. Hubbard 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
350952a6212SJordan K. Hubbard 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
351952a6212SJordan K. Hubbard 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
352952a6212SJordan K. Hubbard 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
353952a6212SJordan K. Hubbard 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
354952a6212SJordan K. Hubbard 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
355952a6212SJordan K. Hubbard 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
356952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
357952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
358952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
359952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
360952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
361952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
362952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
363952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
364952a6212SJordan K. Hubbard };
36527a0bc89SDoug Rabson 
3667391f611SAndrey A. Chernov static u_char
3677391f611SAndrey A. Chernov l2u[256] = {
368c4f02a89SMax Khon /* toupper */
3697391f611SAndrey A. Chernov 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
3707391f611SAndrey A. Chernov 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
3717391f611SAndrey A. Chernov 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
3727391f611SAndrey A. Chernov 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
3737391f611SAndrey A. Chernov 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
3747391f611SAndrey A. Chernov 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
3757391f611SAndrey A. Chernov 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
3767391f611SAndrey A. Chernov 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
3777391f611SAndrey A. Chernov 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
3787391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
3797391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
3807391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
3817391f611SAndrey A. Chernov 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
3827391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
3837391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
3847391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
3857391f611SAndrey A. Chernov 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
3867391f611SAndrey A. Chernov 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
3877391f611SAndrey A. Chernov 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
3887391f611SAndrey A. Chernov 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
3897391f611SAndrey A. Chernov 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
3907391f611SAndrey A. Chernov 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
3917391f611SAndrey A. Chernov 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
3927391f611SAndrey A. Chernov 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
3937391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
3947391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
3957391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
3967391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
3977391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
3987391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
3997391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
4007391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
4017391f611SAndrey A. Chernov };
4027391f611SAndrey A. Chernov 
40327a0bc89SDoug Rabson /*
40427a0bc89SDoug Rabson  * DOS filenames are made of 2 parts, the name part and the extension part.
40527a0bc89SDoug Rabson  * The name part is 8 characters long and the extension part is 3
40627a0bc89SDoug Rabson  * characters long.  They may contain trailing blanks if the name or
40727a0bc89SDoug Rabson  * extension are not long enough to fill their respective fields.
40827a0bc89SDoug Rabson  */
40927a0bc89SDoug Rabson 
41027a0bc89SDoug Rabson /*
41127a0bc89SDoug Rabson  * Convert a DOS filename to a unix filename. And, return the number of
41227a0bc89SDoug Rabson  * characters in the resulting unix filename excluding the terminating
41327a0bc89SDoug Rabson  * null.
41427a0bc89SDoug Rabson  */
41527a0bc89SDoug Rabson int
416c4f02a89SMax Khon dos2unixfn(dn, un, lower, pmp)
41727a0bc89SDoug Rabson 	u_char dn[11];
41827a0bc89SDoug Rabson 	u_char *un;
4195114f1d7SAndrey A. Chernov 	int lower;
420c4f02a89SMax Khon 	struct msdosfsmount *pmp;
42127a0bc89SDoug Rabson {
422bddcdc51STim J. Robbins 	size_t i;
423c4f02a89SMax Khon 	int thislong = 0;
424c4f02a89SMax Khon 	u_int16_t c;
42527a0bc89SDoug Rabson 
42627a0bc89SDoug Rabson 	/*
42727a0bc89SDoug Rabson 	 * If first char of the filename is SLOT_E5 (0x05), then the real
42827a0bc89SDoug Rabson 	 * first char of the filename should be 0xe5. But, they couldn't
42927a0bc89SDoug Rabson 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
43027a0bc89SDoug Rabson 	 * directory slot. Another dos quirk.
43127a0bc89SDoug Rabson 	 */
432952a6212SJordan K. Hubbard 	if (*dn == SLOT_E5)
433c4f02a89SMax Khon 		*dn = 0xe5;
43427a0bc89SDoug Rabson 
435952a6212SJordan K. Hubbard 	/*
436952a6212SJordan K. Hubbard 	 * Copy the name portion into the unix filename string.
437952a6212SJordan K. Hubbard 	 */
438c4f02a89SMax Khon 	for (i = 8; i > 0 && *dn != ' ';) {
439bddcdc51STim J. Robbins 		c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
440c4f02a89SMax Khon 		if (c & 0xff00) {
441c4f02a89SMax Khon 			*un++ = c >> 8;
442952a6212SJordan K. Hubbard 			thislong++;
443952a6212SJordan K. Hubbard 		}
444c4f02a89SMax Khon 		*un++ = c;
445c4f02a89SMax Khon 		thislong++;
446c4f02a89SMax Khon 	}
447c4f02a89SMax Khon 	dn += i;
448952a6212SJordan K. Hubbard 
449952a6212SJordan K. Hubbard 	/*
450952a6212SJordan K. Hubbard 	 * Now, if there is an extension then put in a period and copy in
451952a6212SJordan K. Hubbard 	 * the extension.
452952a6212SJordan K. Hubbard 	 */
453952a6212SJordan K. Hubbard 	if (*dn != ' ') {
454952a6212SJordan K. Hubbard 		*un++ = '.';
455952a6212SJordan K. Hubbard 		thislong++;
456c4f02a89SMax Khon 		for (i = 3; i > 0 && *dn != ' ';) {
457bddcdc51STim J. Robbins 			c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
458c4f02a89SMax Khon 			if (c & 0xff00) {
459c4f02a89SMax Khon 				*un++ = c >> 8;
460c4f02a89SMax Khon 				thislong++;
461c4f02a89SMax Khon 			}
462c4f02a89SMax Khon 			*un++ = c;
463952a6212SJordan K. Hubbard 			thislong++;
464952a6212SJordan K. Hubbard 		}
465952a6212SJordan K. Hubbard 	}
466952a6212SJordan K. Hubbard 	*un++ = 0;
467952a6212SJordan K. Hubbard 
468952a6212SJordan K. Hubbard 	return (thislong);
46927a0bc89SDoug Rabson }
47027a0bc89SDoug Rabson 
47127a0bc89SDoug Rabson /*
472952a6212SJordan K. Hubbard  * Convert a unix filename to a DOS filename according to Win95 rules.
473952a6212SJordan K. Hubbard  * If applicable and gen is not 0, it is inserted into the converted
474952a6212SJordan K. Hubbard  * filename as a generation number.
475952a6212SJordan K. Hubbard  * Returns
476952a6212SJordan K. Hubbard  *	0 if name couldn't be converted
477952a6212SJordan K. Hubbard  *	1 if the converted name is the same as the original
478952a6212SJordan K. Hubbard  *	  (no long filename entry necessary for Win95)
479952a6212SJordan K. Hubbard  *	2 if conversion was successful
480952a6212SJordan K. Hubbard  *	3 if conversion was successful and generation number was inserted
48127a0bc89SDoug Rabson  */
482952a6212SJordan K. Hubbard int
483c4f02a89SMax Khon unix2dosfn(un, dn, unlen, gen, pmp)
484952a6212SJordan K. Hubbard 	const u_char *un;
485952a6212SJordan K. Hubbard 	u_char dn[12];
486bddcdc51STim J. Robbins 	size_t unlen;
487952a6212SJordan K. Hubbard 	u_int gen;
488c4f02a89SMax Khon 	struct msdosfsmount *pmp;
48927a0bc89SDoug Rabson {
490bddcdc51STim J. Robbins 	ssize_t i, j;
491bddcdc51STim J. Robbins 	int l;
492952a6212SJordan K. Hubbard 	int conv = 1;
493952a6212SJordan K. Hubbard 	const u_char *cp, *dp, *dp1;
494952a6212SJordan K. Hubbard 	u_char gentext[6], *wcp;
495c4f02a89SMax Khon 	u_int16_t c;
49627a0bc89SDoug Rabson 
49727a0bc89SDoug Rabson 	/*
49827a0bc89SDoug Rabson 	 * Fill the dos filename string with blanks. These are DOS's pad
49927a0bc89SDoug Rabson 	 * characters.
50027a0bc89SDoug Rabson 	 */
501952a6212SJordan K. Hubbard 	for (i = 0; i < 11; i++)
50227a0bc89SDoug Rabson 		dn[i] = ' ';
503952a6212SJordan K. Hubbard 	dn[11] = 0;
50427a0bc89SDoug Rabson 
50527a0bc89SDoug Rabson 	/*
50627a0bc89SDoug Rabson 	 * The filenames "." and ".." are handled specially, since they
50727a0bc89SDoug Rabson 	 * don't follow dos filename rules.
50827a0bc89SDoug Rabson 	 */
50927a0bc89SDoug Rabson 	if (un[0] == '.' && unlen == 1) {
51027a0bc89SDoug Rabson 		dn[0] = '.';
511952a6212SJordan K. Hubbard 		return gen <= 1;
51227a0bc89SDoug Rabson 	}
51327a0bc89SDoug Rabson 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
51427a0bc89SDoug Rabson 		dn[0] = '.';
51527a0bc89SDoug Rabson 		dn[1] = '.';
516952a6212SJordan K. Hubbard 		return gen <= 1;
51727a0bc89SDoug Rabson 	}
51827a0bc89SDoug Rabson 
51927a0bc89SDoug Rabson 	/*
520952a6212SJordan K. Hubbard 	 * Filenames with only blanks and dots are not allowed!
52127a0bc89SDoug Rabson 	 */
522952a6212SJordan K. Hubbard 	for (cp = un, i = unlen; --i >= 0; cp++)
523952a6212SJordan K. Hubbard 		if (*cp != ' ' && *cp != '.')
524952a6212SJordan K. Hubbard 			break;
525952a6212SJordan K. Hubbard 	if (i < 0)
526952a6212SJordan K. Hubbard 		return 0;
527952a6212SJordan K. Hubbard 
528cce0eefdSMike Smith 
529cce0eefdSMike Smith 	/*
530cce0eefdSMike Smith 	 * Filenames with some characters are not allowed!
531cce0eefdSMike Smith 	 */
532c4f02a89SMax Khon 	for (cp = un, i = unlen; i > 0;)
533c4f02a89SMax Khon 		if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
534cce0eefdSMike Smith 			return 0;
535cce0eefdSMike Smith 
536952a6212SJordan K. Hubbard 	/*
537952a6212SJordan K. Hubbard 	 * Now find the extension
538952a6212SJordan K. Hubbard 	 * Note: dot as first char doesn't start extension
539952a6212SJordan K. Hubbard 	 *	 and trailing dots and blanks are ignored
540c4f02a89SMax Khon 	 * Note(2003/7): It seems recent Windows has
541c4f02a89SMax Khon 	 *	 defferent rule than this code, that Windows
542c4f02a89SMax Khon 	 *	 ignores all dots before extension, and use all
543c4f02a89SMax Khon 	 * 	 chars as filename except for dots.
544952a6212SJordan K. Hubbard 	 */
545952a6212SJordan K. Hubbard 	dp = dp1 = 0;
546952a6212SJordan K. Hubbard 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
547952a6212SJordan K. Hubbard 		switch (*cp++) {
548952a6212SJordan K. Hubbard 		case '.':
549952a6212SJordan K. Hubbard 			if (!dp1)
550952a6212SJordan K. Hubbard 				dp1 = cp;
551952a6212SJordan K. Hubbard 			break;
552952a6212SJordan K. Hubbard 		case ' ':
553952a6212SJordan K. Hubbard 			break;
554952a6212SJordan K. Hubbard 		default:
555952a6212SJordan K. Hubbard 			if (dp1)
556952a6212SJordan K. Hubbard 				dp = dp1;
557952a6212SJordan K. Hubbard 			dp1 = 0;
558952a6212SJordan K. Hubbard 			break;
559952a6212SJordan K. Hubbard 		}
56027a0bc89SDoug Rabson 	}
56127a0bc89SDoug Rabson 
56227a0bc89SDoug Rabson 	/*
563c4f02a89SMax Khon 	 * Now convert it (this part is for extension)
56427a0bc89SDoug Rabson 	 */
565952a6212SJordan K. Hubbard 	if (dp) {
566952a6212SJordan K. Hubbard 		if (dp1)
567952a6212SJordan K. Hubbard 			l = dp1 - dp;
568952a6212SJordan K. Hubbard 		else
569952a6212SJordan K. Hubbard 			l = unlen - (dp - un);
570c4f02a89SMax Khon 		for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
571c4f02a89SMax Khon 			c = unix2doschr(&cp, (size_t *)&i, pmp);
572c4f02a89SMax Khon 			if (c & 0xff00) {
573c4f02a89SMax Khon 				dn[j] = c >> 8;
574c4f02a89SMax Khon 				if (++j < 11) {
575c4f02a89SMax Khon 					dn[j] = c;
576c4f02a89SMax Khon 					continue;
577c4f02a89SMax Khon 				} else {
578c4f02a89SMax Khon 					conv = 3;
579c4f02a89SMax Khon 					dn[j-1] = ' ';
580c4f02a89SMax Khon 					break;
581c4f02a89SMax Khon 				}
582c4f02a89SMax Khon 			} else {
583c4f02a89SMax Khon 				dn[j] = c;
584c4f02a89SMax Khon 			}
585c4f02a89SMax Khon 			if (*(cp - 1) != dn[j] && conv != 3)
586952a6212SJordan K. Hubbard 				conv = 2;
587cce0eefdSMike Smith 			if (dn[j] == 1) {
588cce0eefdSMike Smith 				conv = 3;
589cce0eefdSMike Smith 				dn[j] = '_';
590cce0eefdSMike Smith 			}
591cce0eefdSMike Smith 			if (dn[j] == 2) {
592952a6212SJordan K. Hubbard 				conv = 3;
593952a6212SJordan K. Hubbard 				dn[j--] = ' ';
594952a6212SJordan K. Hubbard 			}
595952a6212SJordan K. Hubbard 		}
596c4f02a89SMax Khon 		if (i > 0)
597952a6212SJordan K. Hubbard 			conv = 3;
598952a6212SJordan K. Hubbard 		dp--;
599952a6212SJordan K. Hubbard 	} else {
600952a6212SJordan K. Hubbard 		for (dp = cp; *--dp == ' ' || *dp == '.';);
601952a6212SJordan K. Hubbard 		dp++;
602952a6212SJordan K. Hubbard 	}
603952a6212SJordan K. Hubbard 
604952a6212SJordan K. Hubbard 	/*
605952a6212SJordan K. Hubbard 	 * Now convert the rest of the name
606952a6212SJordan K. Hubbard 	 */
607c4f02a89SMax Khon 	for (i = dp - un, j = 0; un < dp && j < 8; j++) {
608bddcdc51STim J. Robbins 		c = unix2doschr(&un, &i, pmp);
609c4f02a89SMax Khon 		if (c & 0xff00) {
610c4f02a89SMax Khon 			dn[j] = c >> 8;
611c4f02a89SMax Khon 			if (++j < 8) {
612c4f02a89SMax Khon 				dn[j] = c;
613c4f02a89SMax Khon 				continue;
614c4f02a89SMax Khon 			} else {
615c4f02a89SMax Khon 				conv = 3;
616c4f02a89SMax Khon 				dn[j-1] = ' ';
617c4f02a89SMax Khon 				break;
618c4f02a89SMax Khon 			}
619c4f02a89SMax Khon 		} else {
620c4f02a89SMax Khon 			dn[j] = c;
621c4f02a89SMax Khon 		}
622c4f02a89SMax Khon 		if (*(un - 1) != dn[j] && conv != 3)
623952a6212SJordan K. Hubbard 			conv = 2;
624cce0eefdSMike Smith 		if (dn[j] == 1) {
625cce0eefdSMike Smith 			conv = 3;
626cce0eefdSMike Smith 			dn[j] = '_';
627cce0eefdSMike Smith 		}
628cce0eefdSMike Smith 		if (dn[j] == 2) {
629952a6212SJordan K. Hubbard 			conv = 3;
630952a6212SJordan K. Hubbard 			dn[j--] = ' ';
631952a6212SJordan K. Hubbard 		}
632952a6212SJordan K. Hubbard 	}
633952a6212SJordan K. Hubbard 	if (un < dp)
634952a6212SJordan K. Hubbard 		conv = 3;
635952a6212SJordan K. Hubbard 	/*
636952a6212SJordan K. Hubbard 	 * If we didn't have any chars in filename,
637952a6212SJordan K. Hubbard 	 * generate a default
638952a6212SJordan K. Hubbard 	 */
639952a6212SJordan K. Hubbard 	if (!j)
640952a6212SJordan K. Hubbard 		dn[0] = '_';
641952a6212SJordan K. Hubbard 
642952a6212SJordan K. Hubbard 	/*
643952a6212SJordan K. Hubbard 	 * If there wasn't any char dropped,
644952a6212SJordan K. Hubbard 	 * there is no place for generation numbers
64527a0bc89SDoug Rabson 	 */
646952a6212SJordan K. Hubbard 	if (conv != 3) {
647952a6212SJordan K. Hubbard 		if (gen > 1)
648c4f02a89SMax Khon 			conv = 0;
649c4f02a89SMax Khon 		goto done;
650952a6212SJordan K. Hubbard 	}
651952a6212SJordan K. Hubbard 
652952a6212SJordan K. Hubbard 	/*
653952a6212SJordan K. Hubbard 	 * Now insert the generation number into the filename part
654952a6212SJordan K. Hubbard 	 */
655011cdb57SDmitrij Tejblum 	if (gen == 0)
656c4f02a89SMax Khon 		goto done;
657952a6212SJordan K. Hubbard 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
658952a6212SJordan K. Hubbard 		*--wcp = gen % 10 + '0';
659c4f02a89SMax Khon 	if (gen) {
660c4f02a89SMax Khon 		conv = 0;
661c4f02a89SMax Khon 		goto done;
662c4f02a89SMax Khon 	}
663952a6212SJordan K. Hubbard 	for (i = 8; dn[--i] == ' ';);
664952a6212SJordan K. Hubbard 	i++;
665952a6212SJordan K. Hubbard 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
666952a6212SJordan K. Hubbard 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
667c4f02a89SMax Khon 	/*
668c4f02a89SMax Khon 	 * Correct posision to where insert the generation number
669c4f02a89SMax Khon 	 */
670c4f02a89SMax Khon 	cp = dn;
671c4f02a89SMax Khon 	i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
672c4f02a89SMax Khon 
673952a6212SJordan K. Hubbard 	dn[i++] = '~';
674952a6212SJordan K. Hubbard 	while (wcp < gentext + sizeof(gentext))
675952a6212SJordan K. Hubbard 		dn[i++] = *wcp++;
676c4f02a89SMax Khon 
677c4f02a89SMax Khon 	/*
678c4f02a89SMax Khon 	 * Tail of the filename should be space
679c4f02a89SMax Khon 	 */
680c4f02a89SMax Khon 	while (i < 8)
681c4f02a89SMax Khon 		dn[i++] = ' ';
682c4f02a89SMax Khon 	conv = 3;
683c4f02a89SMax Khon 
684c4f02a89SMax Khon done:
685c4f02a89SMax Khon 	/*
686c4f02a89SMax Khon 	 * The first character cannot be E5,
687c4f02a89SMax Khon 	 * because that means a deleted entry
688c4f02a89SMax Khon 	 */
689c4f02a89SMax Khon 	if (dn[0] == 0xe5)
690c4f02a89SMax Khon 		dn[0] = SLOT_E5;
691c4f02a89SMax Khon 
692c4f02a89SMax Khon 	return conv;
693952a6212SJordan K. Hubbard }
694952a6212SJordan K. Hubbard 
695952a6212SJordan K. Hubbard /*
696952a6212SJordan K. Hubbard  * Create a Win95 long name directory entry
697952a6212SJordan K. Hubbard  * Note: assumes that the filename is valid,
698952a6212SJordan K. Hubbard  *	 i.e. doesn't consist solely of blanks and dots
699952a6212SJordan K. Hubbard  */
700952a6212SJordan K. Hubbard int
701c4f02a89SMax Khon unix2winfn(un, unlen, wep, cnt, chksum, pmp)
702952a6212SJordan K. Hubbard 	const u_char *un;
703bddcdc51STim J. Robbins 	size_t unlen;
704952a6212SJordan K. Hubbard 	struct winentry *wep;
705952a6212SJordan K. Hubbard 	int cnt;
706952a6212SJordan K. Hubbard 	int chksum;
707c4f02a89SMax Khon 	struct msdosfsmount *pmp;
708952a6212SJordan K. Hubbard {
709952a6212SJordan K. Hubbard 	u_int8_t *wcp;
710c4f02a89SMax Khon 	int i, end;
71113df76f2SAndrey A. Chernov 	u_int16_t code;
712952a6212SJordan K. Hubbard 
713952a6212SJordan K. Hubbard 	/*
714952a6212SJordan K. Hubbard 	 * Drop trailing blanks and dots
715952a6212SJordan K. Hubbard 	 */
716c4f02a89SMax Khon 	unlen = winLenFixup(un, unlen);
717952a6212SJordan K. Hubbard 
718c4f02a89SMax Khon 	/*
719c4f02a89SMax Khon 	 * Cut *un for this slot
720c4f02a89SMax Khon 	 */
721c4f02a89SMax Khon 	unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
722c4f02a89SMax Khon 			  pmp->pm_flags, pmp->pm_u2w);
723952a6212SJordan K. Hubbard 
724952a6212SJordan K. Hubbard 	/*
725952a6212SJordan K. Hubbard 	 * Initialize winentry to some useful default
726952a6212SJordan K. Hubbard 	 */
727952a6212SJordan K. Hubbard 	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
728952a6212SJordan K. Hubbard 	wep->weCnt = cnt;
729952a6212SJordan K. Hubbard 	wep->weAttributes = ATTR_WIN95;
730952a6212SJordan K. Hubbard 	wep->weReserved1 = 0;
731952a6212SJordan K. Hubbard 	wep->weChksum = chksum;
732952a6212SJordan K. Hubbard 	wep->weReserved2 = 0;
733952a6212SJordan K. Hubbard 
734952a6212SJordan K. Hubbard 	/*
735952a6212SJordan K. Hubbard 	 * Now convert the filename parts
736952a6212SJordan K. Hubbard 	 */
737c4f02a89SMax Khon 	end = 0;
738c4f02a89SMax Khon 	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
739bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
74013df76f2SAndrey A. Chernov 		*wcp++ = code;
74113df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
742c4f02a89SMax Khon 		if (!code)
743c4f02a89SMax Khon 			end = WIN_LAST;
744952a6212SJordan K. Hubbard 	}
745c4f02a89SMax Khon 	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
746bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
74713df76f2SAndrey A. Chernov 		*wcp++ = code;
74813df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
749c4f02a89SMax Khon 		if (!code)
750c4f02a89SMax Khon 			end = WIN_LAST;
751952a6212SJordan K. Hubbard 	}
752c4f02a89SMax Khon 	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
753bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
75413df76f2SAndrey A. Chernov 		*wcp++ = code;
75513df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
756c4f02a89SMax Khon 		if (!code)
757c4f02a89SMax Khon 			end = WIN_LAST;
758952a6212SJordan K. Hubbard 	}
759c4f02a89SMax Khon 	if (*un == '\0')
760c4f02a89SMax Khon 		end = WIN_LAST;
761c4f02a89SMax Khon 	wep->weCnt |= end;
762c4f02a89SMax Khon 	return !end;
7634d63452fSAndrey A. Chernov }
7644d63452fSAndrey A. Chernov 
765952a6212SJordan K. Hubbard /*
766952a6212SJordan K. Hubbard  * Compare our filename to the one in the Win95 entry
767952a6212SJordan K. Hubbard  * Returns the checksum or -1 if no match
768952a6212SJordan K. Hubbard  */
769952a6212SJordan K. Hubbard int
770c4f02a89SMax Khon winChkName(un, unlen, chksum, pmp)
771952a6212SJordan K. Hubbard 	const u_char *un;
772bddcdc51STim J. Robbins 	size_t unlen;
773952a6212SJordan K. Hubbard 	int chksum;
774c4f02a89SMax Khon 	struct msdosfsmount *pmp;
775952a6212SJordan K. Hubbard {
776bddcdc51STim J. Robbins 	size_t len;
777c4f02a89SMax Khon 	u_int16_t c1, c2;
778c4f02a89SMax Khon 	u_char *np;
779c4f02a89SMax Khon 	struct dirent dirbuf;
780952a6212SJordan K. Hubbard 
781952a6212SJordan K. Hubbard 	/*
782c4f02a89SMax Khon 	 * We alread have winentry in mbnambuf
783952a6212SJordan K. Hubbard 	 */
784c4f02a89SMax Khon 	if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen)
785952a6212SJordan K. Hubbard 		return -1;
786952a6212SJordan K. Hubbard 
787c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG
788c4f02a89SMax Khon 	printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
789c4f02a89SMax Khon 							dirbuf.d_name,
790c4f02a89SMax Khon 							dirbuf.d_namlen);
791c4f02a89SMax Khon #endif
792952a6212SJordan K. Hubbard 
793952a6212SJordan K. Hubbard 	/*
794952a6212SJordan K. Hubbard 	 * Compare the name parts
795952a6212SJordan K. Hubbard 	 */
796c4f02a89SMax Khon 	len = dirbuf.d_namlen;
797c4f02a89SMax Khon 	if (unlen != len)
798c4f02a89SMax Khon 		return -2;
799c4f02a89SMax Khon 
800c4f02a89SMax Khon 	for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
801c4f02a89SMax Khon 		/*
8020f4e4130SMax Khon 		 * Comparison must be case insensitive, because FAT disallows
8030f4e4130SMax Khon 		 * to look up or create files in case sensitive even when
8040f4e4130SMax Khon 		 * it's a long file name.
805c4f02a89SMax Khon 		 */
806bddcdc51STim J. Robbins 		c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
807bddcdc51STim J. Robbins 		c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
808b998efa9SAndrey A. Chernov 		if (c1 != c2)
809c4f02a89SMax Khon 			return -2;
810952a6212SJordan K. Hubbard 	}
811952a6212SJordan K. Hubbard 	return chksum;
812952a6212SJordan K. Hubbard }
813952a6212SJordan K. Hubbard 
814952a6212SJordan K. Hubbard /*
815952a6212SJordan K. Hubbard  * Convert Win95 filename to dirbuf.
816952a6212SJordan K. Hubbard  * Returns the checksum or -1 if impossible
817952a6212SJordan K. Hubbard  */
818952a6212SJordan K. Hubbard int
819c4f02a89SMax Khon win2unixfn(wep, chksum, pmp)
820952a6212SJordan K. Hubbard 	struct winentry *wep;
821952a6212SJordan K. Hubbard 	int chksum;
822c4f02a89SMax Khon 	struct msdosfsmount *pmp;
823952a6212SJordan K. Hubbard {
824952a6212SJordan K. Hubbard 	u_int8_t *cp;
825c4f02a89SMax Khon 	u_int8_t *np, name[WIN_CHARS * 2 + 1];
8262b5b6623SAndrey A. Chernov 	u_int16_t code;
827952a6212SJordan K. Hubbard 	int i;
828952a6212SJordan K. Hubbard 
829952a6212SJordan K. Hubbard 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
830952a6212SJordan K. Hubbard 	    || !(wep->weCnt&WIN_CNT))
831952a6212SJordan K. Hubbard 		return -1;
832952a6212SJordan K. Hubbard 
833952a6212SJordan K. Hubbard 	/*
834952a6212SJordan K. Hubbard 	 * First compare checksums
835952a6212SJordan K. Hubbard 	 */
836952a6212SJordan K. Hubbard 	if (wep->weCnt&WIN_LAST) {
837952a6212SJordan K. Hubbard 		chksum = wep->weChksum;
838952a6212SJordan K. Hubbard 	} else if (chksum != wep->weChksum)
839952a6212SJordan K. Hubbard 		chksum = -1;
840952a6212SJordan K. Hubbard 	if (chksum == -1)
841952a6212SJordan K. Hubbard 		return -1;
842952a6212SJordan K. Hubbard 
843952a6212SJordan K. Hubbard 	/*
844952a6212SJordan K. Hubbard 	 * Convert the name parts
845952a6212SJordan K. Hubbard 	 */
846c4f02a89SMax Khon 	np = name;
847952a6212SJordan K. Hubbard 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
8482b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
8492b5b6623SAndrey A. Chernov 		switch (code) {
850952a6212SJordan K. Hubbard 		case 0:
8512b5b6623SAndrey A. Chernov 			*np = '\0';
852c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
853952a6212SJordan K. Hubbard 			return chksum;
854952a6212SJordan K. Hubbard 		case '/':
8552b5b6623SAndrey A. Chernov 			*np = '\0';
856952a6212SJordan K. Hubbard 			return -1;
8572b5b6623SAndrey A. Chernov 		default:
858c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
859c4f02a89SMax Khon 			if (code & 0xff00)
860c4f02a89SMax Khon 				*np++ = code >> 8;
8612b5b6623SAndrey A. Chernov 			*np++ = code;
8622b5b6623SAndrey A. Chernov 			break;
863952a6212SJordan K. Hubbard 		}
8642b5b6623SAndrey A. Chernov 		cp += 2;
865952a6212SJordan K. Hubbard 	}
866952a6212SJordan K. Hubbard 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
8672b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
8682b5b6623SAndrey A. Chernov 		switch (code) {
869952a6212SJordan K. Hubbard 		case 0:
8702b5b6623SAndrey A. Chernov 			*np = '\0';
871c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
872952a6212SJordan K. Hubbard 			return chksum;
873952a6212SJordan K. Hubbard 		case '/':
8742b5b6623SAndrey A. Chernov 			*np = '\0';
875952a6212SJordan K. Hubbard 			return -1;
8762b5b6623SAndrey A. Chernov 		default:
877c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
878c4f02a89SMax Khon 			if (code & 0xff00)
879c4f02a89SMax Khon 				*np++ = code >> 8;
8802b5b6623SAndrey A. Chernov 			*np++ = code;
8812b5b6623SAndrey A. Chernov 			break;
882952a6212SJordan K. Hubbard 		}
8832b5b6623SAndrey A. Chernov 		cp += 2;
884952a6212SJordan K. Hubbard 	}
885952a6212SJordan K. Hubbard 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
8862b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
8872b5b6623SAndrey A. Chernov 		switch (code) {
888952a6212SJordan K. Hubbard 		case 0:
8892b5b6623SAndrey A. Chernov 			*np = '\0';
890c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
891952a6212SJordan K. Hubbard 			return chksum;
892952a6212SJordan K. Hubbard 		case '/':
8932b5b6623SAndrey A. Chernov 			*np = '\0';
894952a6212SJordan K. Hubbard 			return -1;
8952b5b6623SAndrey A. Chernov 		default:
896c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
897c4f02a89SMax Khon 			if (code & 0xff00)
898c4f02a89SMax Khon 				*np++ = code >> 8;
8992b5b6623SAndrey A. Chernov 			*np++ = code;
9002b5b6623SAndrey A. Chernov 			break;
901952a6212SJordan K. Hubbard 		}
9022b5b6623SAndrey A. Chernov 		cp += 2;
903952a6212SJordan K. Hubbard 	}
904c4f02a89SMax Khon 	*np = '\0';
905c4f02a89SMax Khon 	mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
906952a6212SJordan K. Hubbard 	return chksum;
907952a6212SJordan K. Hubbard }
908952a6212SJordan K. Hubbard 
909952a6212SJordan K. Hubbard /*
9102a05fbb9SNate Lawson  * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
911952a6212SJordan K. Hubbard  */
912952a6212SJordan K. Hubbard u_int8_t
913952a6212SJordan K. Hubbard winChksum(name)
914952a6212SJordan K. Hubbard 	u_int8_t *name;
915952a6212SJordan K. Hubbard {
916952a6212SJordan K. Hubbard 	u_int8_t s;
917952a6212SJordan K. Hubbard 
9182a05fbb9SNate Lawson 	s = name[0];
9192a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[1];
9202a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[2];
9212a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[3];
9222a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[4];
9232a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[5];
9242a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[6];
9252a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[7];
9262a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[8];
9272a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[9];
9282a05fbb9SNate Lawson 	s = ((s << 7) | (s >> 1)) + name[10];
9292a05fbb9SNate Lawson 
9302a05fbb9SNate Lawson 	return (s);
931952a6212SJordan K. Hubbard }
932952a6212SJordan K. Hubbard 
933952a6212SJordan K. Hubbard /*
934952a6212SJordan K. Hubbard  * Determine the number of slots necessary for Win95 names
935952a6212SJordan K. Hubbard  */
936952a6212SJordan K. Hubbard int
937c4f02a89SMax Khon winSlotCnt(un, unlen, pmp)
938952a6212SJordan K. Hubbard 	const u_char *un;
939bddcdc51STim J. Robbins 	size_t unlen;
940c4f02a89SMax Khon 	struct msdosfsmount *pmp;
941952a6212SJordan K. Hubbard {
942c4f02a89SMax Khon 	size_t wlen;
943c4f02a89SMax Khon 	char wn[WIN_MAXLEN * 2 + 1], *wnp;
944c4f02a89SMax Khon 
94569a36f24SMike Smith 	unlen = winLenFixup(un, unlen);
946c4f02a89SMax Khon 
947c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
948c4f02a89SMax Khon 		wlen = WIN_MAXLEN * 2;
949c4f02a89SMax Khon 		wnp = wn;
950bddcdc51STim J. Robbins 		msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
951c4f02a89SMax Khon 		if (unlen > 0)
952c4f02a89SMax Khon 			return 0;
953c4f02a89SMax Khon 		return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
954c4f02a89SMax Khon 	}
955c4f02a89SMax Khon 
956952a6212SJordan K. Hubbard 	if (unlen > WIN_MAXLEN)
957952a6212SJordan K. Hubbard 		return 0;
958952a6212SJordan K. Hubbard 	return howmany(unlen, WIN_CHARS);
95927a0bc89SDoug Rabson }
96069a36f24SMike Smith 
96169a36f24SMike Smith /*
96269a36f24SMike Smith  * Determine the number of bytes neccesary for Win95 names
96369a36f24SMike Smith  */
964bddcdc51STim J. Robbins size_t
96569a36f24SMike Smith winLenFixup(un, unlen)
96669a36f24SMike Smith 	const u_char* un;
967bddcdc51STim J. Robbins 	size_t unlen;
96869a36f24SMike Smith {
96969a36f24SMike Smith 	for (un += unlen; unlen > 0; unlen--)
97069a36f24SMike Smith 		if (*--un != ' ' && *un != '.')
97169a36f24SMike Smith 			break;
97269a36f24SMike Smith 	return unlen;
97369a36f24SMike Smith }
974c4f02a89SMax Khon 
975c4f02a89SMax Khon /*
976c4f02a89SMax Khon  * Store an area with multi byte string instr, and reterns left
977c4f02a89SMax Khon  * byte of instr and moves pointer forward. The area's size is
978c4f02a89SMax Khon  * inlen or outlen.
979c4f02a89SMax Khon  */
980c4f02a89SMax Khon static int
981bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
982c4f02a89SMax Khon {
983c4f02a89SMax Khon 	char *outp, outstr[outlen * weight + 1];
984c4f02a89SMax Khon 
985c4f02a89SMax Khon 	if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
986c4f02a89SMax Khon 		outp = outstr;
987c4f02a89SMax Khon 		outlen *= weight;
988bddcdc51STim J. Robbins 		msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
989c4f02a89SMax Khon 		return (inlen);
990c4f02a89SMax Khon 	}
991c4f02a89SMax Khon 
992c4f02a89SMax Khon 	(*instr) += min(inlen, outlen);
993c4f02a89SMax Khon 	return (inlen - min(inlen, outlen));
994c4f02a89SMax Khon }
995c4f02a89SMax Khon 
996c4f02a89SMax Khon /*
997c4f02a89SMax Khon  * Convert DOS char to Local char
998c4f02a89SMax Khon  */
999c4f02a89SMax Khon static u_int16_t
1000c4f02a89SMax Khon dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1001c4f02a89SMax Khon {
1002c4f02a89SMax Khon 	u_char c;
1003c4f02a89SMax Khon 	char *outp, outbuf[3];
1004c4f02a89SMax Khon 	u_int16_t wc;
1005c4f02a89SMax Khon 	size_t len, olen;
1006c4f02a89SMax Khon 
1007c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1008c4f02a89SMax Khon 		olen = len = 2;
1009c4f02a89SMax Khon 		outp = outbuf;
1010c4f02a89SMax Khon 
1011c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
1012c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
1013c4f02a89SMax Khon 						  ilen, &outp, &olen, KICONV_LOWER);
1014c4f02a89SMax Khon 		else
1015c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
1016c4f02a89SMax Khon 					     ilen, &outp, &olen);
1017c4f02a89SMax Khon 		len -= olen;
1018c4f02a89SMax Khon 
1019c4f02a89SMax Khon 		/*
1020c4f02a89SMax Khon 		 * return '?' if failed to convert
1021c4f02a89SMax Khon 		 */
1022c4f02a89SMax Khon 		if (len == 0) {
1023c4f02a89SMax Khon 			(*ilen)--;
1024c4f02a89SMax Khon 			(*instr)++;
1025c4f02a89SMax Khon 			return ('?');
1026c4f02a89SMax Khon 		}
1027c4f02a89SMax Khon 
1028c4f02a89SMax Khon 		wc = 0;
1029c4f02a89SMax Khon 		while(len--)
1030c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1031c4f02a89SMax Khon 		return (wc);
1032c4f02a89SMax Khon 	}
1033c4f02a89SMax Khon 
1034c4f02a89SMax Khon 	(*ilen)--;
1035c4f02a89SMax Khon 	c = *(*instr)++;
1036c4f02a89SMax Khon 	c = dos2unix[c];
1037c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
1038c4f02a89SMax Khon 		c = u2l[c];
1039c4f02a89SMax Khon 	return ((u_int16_t)c);
1040c4f02a89SMax Khon }
1041c4f02a89SMax Khon 
1042c4f02a89SMax Khon /*
1043c4f02a89SMax Khon  * Convert Local char to DOS char
1044c4f02a89SMax Khon  */
1045c4f02a89SMax Khon static u_int16_t
1046c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
1047c4f02a89SMax Khon {
1048c4f02a89SMax Khon 	u_char c;
1049c4f02a89SMax Khon 	char *up, *outp, unicode[3], outbuf[3];
1050c4f02a89SMax Khon 	u_int16_t wc;
1051c4f02a89SMax Khon 	size_t len, ulen, olen;
1052c4f02a89SMax Khon 
1053c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1054c4f02a89SMax Khon 		/*
1055c4f02a89SMax Khon 		 * to hide an invisible character, using a unicode filter
1056c4f02a89SMax Khon 		 */
1057c4f02a89SMax Khon 		ulen = 2;
1058c4f02a89SMax Khon 		len = *ilen;
1059c4f02a89SMax Khon 		up = unicode;
1060c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1061c4f02a89SMax Khon 				     ilen, &up, &ulen);
1062c4f02a89SMax Khon 
1063c4f02a89SMax Khon 		/*
1064c4f02a89SMax Khon 		 * cannot be converted
1065c4f02a89SMax Khon 		 */
1066c4f02a89SMax Khon 		if (ulen == 2) {
1067c4f02a89SMax Khon 			(*ilen)--;
1068c4f02a89SMax Khon 			(*instr)++;
1069c4f02a89SMax Khon 			return (0);
1070c4f02a89SMax Khon 		}
1071c4f02a89SMax Khon 
1072c4f02a89SMax Khon 		/*
1073c4f02a89SMax Khon 		 * return magic number for ascii char
1074c4f02a89SMax Khon 		 */
1075c4f02a89SMax Khon 		if ((len - *ilen) == 1) {
1076c4f02a89SMax Khon 			c = *(*instr -1);
1077c4f02a89SMax Khon 			if (! (c & 0x80)) {
1078c4f02a89SMax Khon 				c = unix2dos[c];
1079c4f02a89SMax Khon 				if (c <= 2)
1080c4f02a89SMax Khon 					return (c);
1081c4f02a89SMax Khon 			}
1082c4f02a89SMax Khon 		}
1083c4f02a89SMax Khon 
1084c4f02a89SMax Khon 		/*
1085c4f02a89SMax Khon 		 * now convert using libiconv
1086c4f02a89SMax Khon 		 */
1087c4f02a89SMax Khon 		*instr -= len - *ilen;
1088c4f02a89SMax Khon 		*ilen = (int)len;
1089c4f02a89SMax Khon 
1090c4f02a89SMax Khon 		olen = len = 2;
1091c4f02a89SMax Khon 		outp = outbuf;
1092c4f02a89SMax Khon 		msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
1093c4f02a89SMax Khon 					  ilen, &outp, &olen, KICONV_FROM_UPPER);
1094c4f02a89SMax Khon 		len -= olen;
1095c4f02a89SMax Khon 		wc = 0;
1096c4f02a89SMax Khon 		while(len--)
1097c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1098c4f02a89SMax Khon 		return (wc);
1099c4f02a89SMax Khon 	}
1100c4f02a89SMax Khon 
1101c4f02a89SMax Khon 	(*ilen)--;
1102c4f02a89SMax Khon 	c = *(*instr)++;
1103c4f02a89SMax Khon 	c = l2u[c];
1104c4f02a89SMax Khon 	c = unix2dos[c];
1105c4f02a89SMax Khon 	return ((u_int16_t)c);
1106c4f02a89SMax Khon }
1107c4f02a89SMax Khon 
1108c4f02a89SMax Khon /*
1109c4f02a89SMax Khon  * Convert Windows char to Local char
1110c4f02a89SMax Khon  */
1111c4f02a89SMax Khon static u_int16_t
1112c4f02a89SMax Khon win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
1113c4f02a89SMax Khon {
1114c4f02a89SMax Khon 	u_char *inp, *outp, inbuf[3], outbuf[3];
1115c4f02a89SMax Khon 	size_t ilen, olen, len;
1116c4f02a89SMax Khon 
1117c4f02a89SMax Khon 	if (wc == 0)
1118c4f02a89SMax Khon 		return (0);
1119c4f02a89SMax Khon 
1120c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1121c4f02a89SMax Khon 		inbuf[0] = (u_char)(wc>>8);
1122c4f02a89SMax Khon 		inbuf[1] = (u_char)wc;
1123c4f02a89SMax Khon 		inbuf[2] = '\0';
1124c4f02a89SMax Khon 
1125c4f02a89SMax Khon 		ilen = olen = len = 2;
1126c4f02a89SMax Khon 		inp = inbuf;
1127c4f02a89SMax Khon 		outp = outbuf;
1128c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
1129c4f02a89SMax Khon 				     (char **)&outp, &olen);
1130c4f02a89SMax Khon 		len -= olen;
1131c4f02a89SMax Khon 
1132c4f02a89SMax Khon 		/*
1133c4f02a89SMax Khon 		 * return '?' if failed to convert
1134c4f02a89SMax Khon 		 */
1135c4f02a89SMax Khon 		if (len == 0) {
1136c4f02a89SMax Khon 			wc = '?';
1137c4f02a89SMax Khon 			return (wc);
1138c4f02a89SMax Khon 		}
1139c4f02a89SMax Khon 
1140c4f02a89SMax Khon 		wc = 0;
1141c4f02a89SMax Khon 		while(len--)
1142c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1143c4f02a89SMax Khon 		return (wc);
1144c4f02a89SMax Khon 	}
1145c4f02a89SMax Khon 
1146c4f02a89SMax Khon 	if (wc & 0xff00)
1147c4f02a89SMax Khon 		wc = '?';
1148c4f02a89SMax Khon 
1149c4f02a89SMax Khon 	return (wc);
1150c4f02a89SMax Khon }
1151c4f02a89SMax Khon 
1152c4f02a89SMax Khon /*
1153c4f02a89SMax Khon  * Convert Local char to Windows char
1154c4f02a89SMax Khon  */
1155c4f02a89SMax Khon static u_int16_t
1156c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1157c4f02a89SMax Khon {
1158c4f02a89SMax Khon 	u_char *outp, outbuf[3];
1159c4f02a89SMax Khon 	u_int16_t wc;
1160c4f02a89SMax Khon 	size_t olen;
1161c4f02a89SMax Khon 
1162c4f02a89SMax Khon 	if (*ilen == 0)
1163c4f02a89SMax Khon 		return (0);
1164c4f02a89SMax Khon 
1165c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1166c4f02a89SMax Khon 		outp = outbuf;
1167c4f02a89SMax Khon 		olen = 2;
1168c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
1169c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
1170c4f02a89SMax Khon 						  ilen, (char **)&outp, &olen,
1171c4f02a89SMax Khon 						  KICONV_FROM_LOWER);
1172c4f02a89SMax Khon 		else
1173c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1174c4f02a89SMax Khon 					     ilen, (char **)&outp, &olen);
1175c4f02a89SMax Khon 
1176c4f02a89SMax Khon 		/*
1177c4f02a89SMax Khon 		 * return '0' if end of filename
1178c4f02a89SMax Khon 		 */
1179c4f02a89SMax Khon 		if (olen == 2)
1180c4f02a89SMax Khon 			return (0);
1181c4f02a89SMax Khon 
1182c4f02a89SMax Khon 		wc = (outbuf[0]<<8) | outbuf[1];
1183c4f02a89SMax Khon 
1184c4f02a89SMax Khon 		return (wc);
1185c4f02a89SMax Khon 	}
1186c4f02a89SMax Khon 
1187c4f02a89SMax Khon 	(*ilen)--;
1188c4f02a89SMax Khon 	wc = (*instr)[0];
1189c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
1190c4f02a89SMax Khon 		wc = u2l[wc];
1191c4f02a89SMax Khon 	(*instr)++;
1192c4f02a89SMax Khon 	return (wc);
1193c4f02a89SMax Khon }
1194c4f02a89SMax Khon 
1195c4f02a89SMax Khon /*
11964cdb1483SNate Lawson  * Initialize the temporary concatenation buffer (once) and mark it as
11974cdb1483SNate Lawson  * empty on subsequent calls.
1198c4f02a89SMax Khon  */
1199c4f02a89SMax Khon void
1200c4f02a89SMax Khon mbnambuf_init(void)
1201c4f02a89SMax Khon {
1202c4f02a89SMax Khon 
12034cdb1483SNate Lawson         if (nambuf_ptr == NULL) {
12044cdb1483SNate Lawson 		nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK);
12054cdb1483SNate Lawson 		nambuf_ptr[MAXNAMLEN] = '\0';
1206c4f02a89SMax Khon 	}
12074cdb1483SNate Lawson 	nambuf_len = 0;
12084cdb1483SNate Lawson 	nambuf_max_id = -1;
1209c4f02a89SMax Khon }
1210c4f02a89SMax Khon 
1211c4f02a89SMax Khon /*
12124cdb1483SNate Lawson  * Fill out our concatenation buffer with the given substring, at the
12134cdb1483SNate Lawson  * offset specified by its id.  For the last substring (i.e., the one with
12144cdb1483SNate Lawson  * the maximum id), use it to calculate the length of the entire string.
12154cdb1483SNate Lawson  * Since this function is usually called with ids in descending order, this
12164cdb1483SNate Lawson  * reduces the number of times we need to call strlen() since we know that
12174cdb1483SNate Lawson  * all previous substrings are exactly WIN_CHARS in length.
1218c4f02a89SMax Khon  */
1219c4f02a89SMax Khon void
1220c4f02a89SMax Khon mbnambuf_write(char *name, int id)
1221c4f02a89SMax Khon {
12224cdb1483SNate Lawson 	size_t count;
12234cdb1483SNate Lawson 
12244cdb1483SNate Lawson 	count = WIN_CHARS;
12254cdb1483SNate Lawson 	if (id > nambuf_max_id) {
12264cdb1483SNate Lawson 		count = strlen(name);
12274cdb1483SNate Lawson 		nambuf_len = id * WIN_CHARS + count;
12284cdb1483SNate Lawson 		if (nambuf_len > MAXNAMLEN) {
12294cdb1483SNate Lawson 			printf("msdosfs: file name %d too long\n", nambuf_len);
12304cdb1483SNate Lawson 			return;
1231c4f02a89SMax Khon 		}
12324cdb1483SNate Lawson 		nambuf_max_id = id;
12334cdb1483SNate Lawson 	}
12344cdb1483SNate Lawson 	memcpy(nambuf_ptr + (id * WIN_CHARS), name, count);
1235c4f02a89SMax Khon }
1236c4f02a89SMax Khon 
1237c4f02a89SMax Khon /*
12384cdb1483SNate Lawson  * Take the completed string and use it to setup the struct dirent.
12394cdb1483SNate Lawson  * Be sure to always nul-terminate the d_name and then copy the string
12404cdb1483SNate Lawson  * from our buffer.  Note that this function assumes the full string has
12414cdb1483SNate Lawson  * been reassembled in the buffer.  If it's called before all substrings
12424cdb1483SNate Lawson  * have been written via mbnambuf_write(), the result will be incorrect.
1243c4f02a89SMax Khon  */
1244c4f02a89SMax Khon char *
1245c4f02a89SMax Khon mbnambuf_flush(struct dirent *dp)
1246c4f02a89SMax Khon {
1247c4f02a89SMax Khon 
12484cdb1483SNate Lawson 	if (nambuf_len > sizeof(dp->d_name) - 1) {
1249c4f02a89SMax Khon 		mbnambuf_init();
1250c4f02a89SMax Khon 		return (NULL);
1251c4f02a89SMax Khon 	}
12524cdb1483SNate Lawson 	nambuf_ptr[nambuf_len] = '\0';
12534cdb1483SNate Lawson 	memcpy(dp->d_name, nambuf_ptr, nambuf_len);
12544cdb1483SNate Lawson 	dp->d_namlen = nambuf_len;
12554cdb1483SNate Lawson 
1256c4f02a89SMax Khon 	mbnambuf_init();
1257c4f02a89SMax Khon 	return (dp->d_name);
1258c4f02a89SMax Khon }
1259