xref: /minix/sys/fs/msdosfs/msdosfs_conv.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: msdosfs_conv.c,v 1.10 2014/09/01 09:09:47 martin Exp $	*/
284d9c625SLionel Sambuc 
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
584d9c625SLionel Sambuc  * Copyright (C) 1995, 1997 TooLs GmbH.
684d9c625SLionel Sambuc  * All rights reserved.
784d9c625SLionel Sambuc  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
884d9c625SLionel Sambuc  *
984d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1084d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1184d9c625SLionel Sambuc  * are met:
1284d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1384d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1484d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1584d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1684d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1784d9c625SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
1884d9c625SLionel Sambuc  *    must display the following acknowledgement:
1984d9c625SLionel Sambuc  *	This product includes software developed by TooLs GmbH.
2084d9c625SLionel Sambuc  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2184d9c625SLionel Sambuc  *    derived from this software without specific prior written permission.
2284d9c625SLionel Sambuc  *
2384d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2484d9c625SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2584d9c625SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2684d9c625SLionel Sambuc  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2784d9c625SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2884d9c625SLionel Sambuc  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2984d9c625SLionel Sambuc  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3084d9c625SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3184d9c625SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3284d9c625SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3384d9c625SLionel Sambuc  */
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * Written by Paul Popelka (paulp@uts.amdahl.com)
3684d9c625SLionel Sambuc  *
3784d9c625SLionel Sambuc  * You can do anything you want with this software, just don't say you wrote
3884d9c625SLionel Sambuc  * it, and don't remove this notice.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * This software is provided "as is".
4184d9c625SLionel Sambuc  *
4284d9c625SLionel Sambuc  * The author supplies this software to be publicly redistributed on the
4384d9c625SLionel Sambuc  * understanding that the author is not responsible for the correct
4484d9c625SLionel Sambuc  * functioning of this software in any circumstances and is not liable for
4584d9c625SLionel Sambuc  * any damages caused by this software.
4684d9c625SLionel Sambuc  *
4784d9c625SLionel Sambuc  * October 1992
4884d9c625SLionel Sambuc  */
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
5184d9c625SLionel Sambuc #include "nbtool_config.h"
5284d9c625SLionel Sambuc #endif
5384d9c625SLionel Sambuc 
5484d9c625SLionel Sambuc #include <sys/cdefs.h>
55*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: msdosfs_conv.c,v 1.10 2014/09/01 09:09:47 martin Exp $");
5684d9c625SLionel Sambuc 
5784d9c625SLionel Sambuc /*
5884d9c625SLionel Sambuc  * System include files.
5984d9c625SLionel Sambuc  */
6084d9c625SLionel Sambuc #include <sys/param.h>
6184d9c625SLionel Sambuc #include <sys/time.h>
6284d9c625SLionel Sambuc #ifdef _KERNEL
6384d9c625SLionel Sambuc #include <sys/dirent.h>
6484d9c625SLionel Sambuc #include <sys/systm.h>
6584d9c625SLionel Sambuc #include <sys/kernel.h>
6684d9c625SLionel Sambuc #include <sys/vnode.h>
6784d9c625SLionel Sambuc #else
6884d9c625SLionel Sambuc #include <stdio.h>
69*0a6a1f1dSLionel Sambuc #include <string.h>
7084d9c625SLionel Sambuc #include <dirent.h>
7184d9c625SLionel Sambuc #include <sys/queue.h>
7284d9c625SLionel Sambuc #endif
73*0a6a1f1dSLionel Sambuc #include <dev/clock_subr.h>
7484d9c625SLionel Sambuc 
7584d9c625SLionel Sambuc /*
7684d9c625SLionel Sambuc  * MSDOSFS include files.
7784d9c625SLionel Sambuc  */
7884d9c625SLionel Sambuc #include <fs/msdosfs/direntry.h>
7984d9c625SLionel Sambuc #include <fs/msdosfs/denode.h>
8084d9c625SLionel Sambuc 
8184d9c625SLionel Sambuc /*
82*0a6a1f1dSLionel Sambuc  * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
83*0a6a1f1dSLionel Sambuc  * interval there were 8 regular years and 2 leap years.
8484d9c625SLionel Sambuc  */
85*0a6a1f1dSLionel Sambuc #define	DOSBIASYEAR	1980
86*0a6a1f1dSLionel Sambuc #define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
8784d9c625SLionel Sambuc /*
88*0a6a1f1dSLionel Sambuc  * msdos fs can not store dates beyound the year 2234
8984d9c625SLionel Sambuc  */
90*0a6a1f1dSLionel Sambuc #define DOSMAXYEAR	((DD_YEAR_MASK >> DD_YEAR_SHIFT) + DOSBIASYEAR)
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc /*
9384d9c625SLionel Sambuc  * Convert the unix version of time to dos's idea of time to be used in
9484d9c625SLionel Sambuc  * file timestamps. The passed in unix time is assumed to be in GMT.
9584d9c625SLionel Sambuc  */
9684d9c625SLionel Sambuc void
unix2dostime(const struct timespec * tsp,int gmtoff,u_int16_t * ddp,u_int16_t * dtp,u_int8_t * dhp)9784d9c625SLionel Sambuc unix2dostime(const struct timespec *tsp, int gmtoff, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp)
9884d9c625SLionel Sambuc {
9984d9c625SLionel Sambuc 	u_long t;
100*0a6a1f1dSLionel Sambuc 	struct clock_ymdhms ymd;
10184d9c625SLionel Sambuc 
10284d9c625SLionel Sambuc 	t = tsp->tv_sec + gmtoff; /* time zone correction */
10384d9c625SLionel Sambuc 
10484d9c625SLionel Sambuc 	/*
105*0a6a1f1dSLionel Sambuc 	 * DOS timestamps can not represent dates before 1980.
10684d9c625SLionel Sambuc 	 */
107*0a6a1f1dSLionel Sambuc 	if (t < SECONDSTO1980)
108*0a6a1f1dSLionel Sambuc 		goto invalid_dos_date;
109*0a6a1f1dSLionel Sambuc 
11084d9c625SLionel Sambuc 	/*
111*0a6a1f1dSLionel Sambuc 	 * DOS granularity is 2 seconds
11284d9c625SLionel Sambuc 	 */
113*0a6a1f1dSLionel Sambuc 	t &= ~1;
114*0a6a1f1dSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc 	/*
116*0a6a1f1dSLionel Sambuc 	 * Convert to year/month/day/.. format
117*0a6a1f1dSLionel Sambuc 	 */
118*0a6a1f1dSLionel Sambuc 	clock_secs_to_ymdhms(t, &ymd);
119*0a6a1f1dSLionel Sambuc 	if (ymd.dt_year > DOSMAXYEAR)
120*0a6a1f1dSLionel Sambuc 		goto invalid_dos_date;
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc 	/*
123*0a6a1f1dSLionel Sambuc 	 * Now transform to DOS format
124*0a6a1f1dSLionel Sambuc 	 */
125*0a6a1f1dSLionel Sambuc 	*ddp = (ymd.dt_day << DD_DAY_SHIFT)
126*0a6a1f1dSLionel Sambuc 	    + (ymd.dt_mon << DD_MONTH_SHIFT)
127*0a6a1f1dSLionel Sambuc 	    + ((ymd.dt_year - DOSBIASYEAR) << DD_YEAR_SHIFT);
12884d9c625SLionel Sambuc 	if (dhp)
12984d9c625SLionel Sambuc 		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
130*0a6a1f1dSLionel Sambuc 	if (dtp)
131*0a6a1f1dSLionel Sambuc 		*dtp = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
132*0a6a1f1dSLionel Sambuc 		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
133*0a6a1f1dSLionel Sambuc 		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
134*0a6a1f1dSLionel Sambuc 	return;
13584d9c625SLionel Sambuc 
136*0a6a1f1dSLionel Sambuc invalid_dos_date:
137*0a6a1f1dSLionel Sambuc 	*ddp = 0;
138*0a6a1f1dSLionel Sambuc 	if (dtp)
139*0a6a1f1dSLionel Sambuc 		*dtp = 0;
140*0a6a1f1dSLionel Sambuc 	if (dhp)
141*0a6a1f1dSLionel Sambuc 		*dhp = 0;
14284d9c625SLionel Sambuc }
14384d9c625SLionel Sambuc 
14484d9c625SLionel Sambuc /*
14584d9c625SLionel Sambuc  * Convert from dos' idea of time to unix'. This will probably only be
14684d9c625SLionel Sambuc  * called from the stat(), and fstat() system calls and so probably need
14784d9c625SLionel Sambuc  * not be too efficient.
14884d9c625SLionel Sambuc  */
14984d9c625SLionel Sambuc void
dos2unixtime(u_int dd,u_int dt,u_int dh,int gmtoff,struct timespec * tsp)15084d9c625SLionel Sambuc dos2unixtime(u_int dd, u_int dt, u_int dh, int gmtoff, struct timespec *tsp)
15184d9c625SLionel Sambuc {
152*0a6a1f1dSLionel Sambuc 	time_t seconds;
153*0a6a1f1dSLionel Sambuc 	struct clock_ymdhms ymd;
15484d9c625SLionel Sambuc 
15584d9c625SLionel Sambuc 	if (dd == 0) {
15684d9c625SLionel Sambuc 		/*
15784d9c625SLionel Sambuc 		 * Uninitialized field, return the epoch.
15884d9c625SLionel Sambuc 		 */
15984d9c625SLionel Sambuc 		tsp->tv_sec = 0;
16084d9c625SLionel Sambuc 		tsp->tv_nsec = 0;
16184d9c625SLionel Sambuc 		return;
16284d9c625SLionel Sambuc 	}
163*0a6a1f1dSLionel Sambuc 
164*0a6a1f1dSLionel Sambuc 	memset(&ymd, 0, sizeof(ymd));
165*0a6a1f1dSLionel Sambuc 	ymd.dt_year = ((dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT) + 1980 ;
166*0a6a1f1dSLionel Sambuc 	ymd.dt_mon = ((dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT);
167*0a6a1f1dSLionel Sambuc 	ymd.dt_day = ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT);
168*0a6a1f1dSLionel Sambuc 	ymd.dt_hour = (dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT;
169*0a6a1f1dSLionel Sambuc 	ymd.dt_min = (dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT;
170*0a6a1f1dSLionel Sambuc 	ymd.dt_sec = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2;
171*0a6a1f1dSLionel Sambuc 
172*0a6a1f1dSLionel Sambuc 	seconds = clock_ymdhms_to_secs(&ymd);
173*0a6a1f1dSLionel Sambuc 
174*0a6a1f1dSLionel Sambuc 	tsp->tv_sec = seconds;
17584d9c625SLionel Sambuc 	tsp->tv_sec -= gmtoff;	/* time zone correction */
17684d9c625SLionel Sambuc 	tsp->tv_nsec = (dh % 100) * 10000000;
17784d9c625SLionel Sambuc }
17884d9c625SLionel Sambuc 
17984d9c625SLionel Sambuc static const u_char
18084d9c625SLionel Sambuc unix2dos[256] = {
18184d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
18284d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
18384d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
18484d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
18584d9c625SLionel Sambuc 	0,    '!',  0,    '#',  '$',  '%',  '&',  '\'',	/* 20-27 */
18684d9c625SLionel Sambuc 	'(',  ')',  0,    '+',  0,    '-',  0,    0,	/* 28-2f */
18784d9c625SLionel Sambuc 	'0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
18884d9c625SLionel Sambuc 	'8',  '9',  0,    0,    0,    0,    0,    0,	/* 38-3f */
18984d9c625SLionel Sambuc 	'@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
19084d9c625SLionel Sambuc 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
19184d9c625SLionel Sambuc 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
19284d9c625SLionel Sambuc 	'X',  'Y',  'Z',  0,    0,    0,    '^',  '_',	/* 58-5f */
19384d9c625SLionel Sambuc 	'`',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 60-67 */
19484d9c625SLionel Sambuc 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 68-6f */
19584d9c625SLionel Sambuc 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 70-77 */
19684d9c625SLionel Sambuc 	'X',  'Y',  'Z',  '{',  0,    '}',  '~',  0,	/* 78-7f */
19784d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
19884d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
19984d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
20084d9c625SLionel Sambuc 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
20184d9c625SLionel Sambuc 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
20284d9c625SLionel Sambuc 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
20384d9c625SLionel Sambuc 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
20484d9c625SLionel Sambuc 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
20584d9c625SLionel Sambuc 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
20684d9c625SLionel Sambuc 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
20784d9c625SLionel Sambuc 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
20884d9c625SLionel Sambuc 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
20984d9c625SLionel Sambuc 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
21084d9c625SLionel Sambuc 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
21184d9c625SLionel Sambuc 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
21284d9c625SLionel Sambuc 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
21384d9c625SLionel Sambuc };
21484d9c625SLionel Sambuc 
21584d9c625SLionel Sambuc static const u_char
21684d9c625SLionel Sambuc dos2unix[256] = {
21784d9c625SLionel Sambuc 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 00-07 */
21884d9c625SLionel Sambuc 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 08-0f */
21984d9c625SLionel Sambuc 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 10-17 */
22084d9c625SLionel Sambuc 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 18-1f */
22184d9c625SLionel Sambuc 	 ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'',	/* 20-27 */
22284d9c625SLionel Sambuc 	 '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',	/* 28-2f */
22384d9c625SLionel Sambuc 	 '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
22484d9c625SLionel Sambuc 	 '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',	/* 38-3f */
22584d9c625SLionel Sambuc 	 '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
22684d9c625SLionel Sambuc 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
22784d9c625SLionel Sambuc 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
22884d9c625SLionel Sambuc 	 'X',  'Y',  'Z',  '[', '\\',  ']',  '^',  '_',	/* 58-5f */
22984d9c625SLionel Sambuc 	 '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',	/* 60-67 */
23084d9c625SLionel Sambuc 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',	/* 68-6f */
23184d9c625SLionel Sambuc 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w',	/* 70-77 */
23284d9c625SLionel Sambuc 	 'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f,	/* 78-7f */
23384d9c625SLionel Sambuc 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
23484d9c625SLionel Sambuc 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
23584d9c625SLionel Sambuc 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
23684d9c625SLionel Sambuc 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7,  '?',	/* 98-9f */
23784d9c625SLionel Sambuc 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
23884d9c625SLionel Sambuc 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
23984d9c625SLionel Sambuc 	 '?',  '?',  '?',  '?',  '?', 0xc1, 0xc2, 0xc0,	/* b0-b7 */
24084d9c625SLionel Sambuc 	0xa9,  '?',  '?',  '?',  '?', 0xa2, 0xa5,  '?',	/* b8-bf */
24184d9c625SLionel Sambuc 	 '?',  '?',  '?',  '?',  '?',  '?', 0xe3, 0xc3,	/* c0-c7 */
24284d9c625SLionel Sambuc 	 '?',  '?',  '?',  '?',  '?',  '?',  '?', 0xa4,	/* c8-cf */
24384d9c625SLionel Sambuc 	0xf0, 0xd0, 0xca, 0xcb, 0xc8,  '?', 0xcd, 0xce,	/* d0-d7 */
24484d9c625SLionel Sambuc 	0xcf,  '?',  '?',  '?',  '?', 0xa6, 0xcc,  '?',	/* d8-df */
24584d9c625SLionel Sambuc 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
24684d9c625SLionel Sambuc 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
24784d9c625SLionel Sambuc 	0xad, 0xb1,  '?', 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
24884d9c625SLionel Sambuc 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2,  '?',  '?',	/* f8-ff */
24984d9c625SLionel Sambuc };
25084d9c625SLionel Sambuc 
25184d9c625SLionel Sambuc static const u_char
25284d9c625SLionel Sambuc u2l[256] = {
25384d9c625SLionel Sambuc 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
25484d9c625SLionel Sambuc 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
25584d9c625SLionel Sambuc 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
25684d9c625SLionel Sambuc 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
25784d9c625SLionel Sambuc 	 ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'', /* 20-27 */
25884d9c625SLionel Sambuc 	 '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/', /* 28-2f */
25984d9c625SLionel Sambuc 	 '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7', /* 30-37 */
26084d9c625SLionel Sambuc 	 '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?', /* 38-3f */
26184d9c625SLionel Sambuc 	 '@',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 40-47 */
26284d9c625SLionel Sambuc 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 48-4f */
26384d9c625SLionel Sambuc 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 50-57 */
26484d9c625SLionel Sambuc 	 'x',  'y',  'z',  '[', '\\',  ']',  '^',  '_', /* 58-5f */
26584d9c625SLionel Sambuc 	 '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 60-67 */
26684d9c625SLionel Sambuc 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 68-6f */
26784d9c625SLionel Sambuc 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 70-77 */
26884d9c625SLionel Sambuc 	 'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f, /* 78-7f */
26984d9c625SLionel Sambuc 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
27084d9c625SLionel Sambuc 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
27184d9c625SLionel Sambuc 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
27284d9c625SLionel Sambuc 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
27384d9c625SLionel Sambuc 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
27484d9c625SLionel Sambuc 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
27584d9c625SLionel Sambuc 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
27684d9c625SLionel Sambuc 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
27784d9c625SLionel Sambuc 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
27884d9c625SLionel Sambuc 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
27984d9c625SLionel Sambuc 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
28084d9c625SLionel Sambuc 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
28184d9c625SLionel Sambuc 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
28284d9c625SLionel Sambuc 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
28384d9c625SLionel Sambuc 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
28484d9c625SLionel Sambuc 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
28584d9c625SLionel Sambuc };
28684d9c625SLionel Sambuc 
28784d9c625SLionel Sambuc /*
28884d9c625SLionel Sambuc  * DOS filenames are made of 2 parts, the name part and the extension part.
28984d9c625SLionel Sambuc  * The name part is 8 characters long and the extension part is 3
29084d9c625SLionel Sambuc  * characters long.  They may contain trailing blanks if the name or
29184d9c625SLionel Sambuc  * extension are not long enough to fill their respective fields.
29284d9c625SLionel Sambuc  */
29384d9c625SLionel Sambuc 
29484d9c625SLionel Sambuc /*
29584d9c625SLionel Sambuc  * Convert a DOS filename to a unix filename. And, return the number of
29684d9c625SLionel Sambuc  * characters in the resulting unix filename excluding the terminating
29784d9c625SLionel Sambuc  * null.
29884d9c625SLionel Sambuc  */
29984d9c625SLionel Sambuc int
dos2unixfn(u_char dn[11],u_char * un,int lower)30084d9c625SLionel Sambuc dos2unixfn(u_char dn[11], u_char *un, int lower)
30184d9c625SLionel Sambuc {
30284d9c625SLionel Sambuc 	int i, j;
30384d9c625SLionel Sambuc 	int thislong = 1;
30484d9c625SLionel Sambuc 	u_char c;
30584d9c625SLionel Sambuc 
30684d9c625SLionel Sambuc 	/*
30784d9c625SLionel Sambuc 	 * If first char of the filename is SLOT_E5 (0x05), then the real
30884d9c625SLionel Sambuc 	 * first char of the filename should be 0xe5. But, they couldn't
30984d9c625SLionel Sambuc 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
31084d9c625SLionel Sambuc 	 * directory slot. Another dos quirk.
31184d9c625SLionel Sambuc 	 */
31284d9c625SLionel Sambuc 	if (*dn == SLOT_E5)
31384d9c625SLionel Sambuc 		c = dos2unix[0xe5];
31484d9c625SLionel Sambuc 	else
31584d9c625SLionel Sambuc 		c = dos2unix[*dn];
31684d9c625SLionel Sambuc 	*un++ = lower ? u2l[c] : c;
31784d9c625SLionel Sambuc 
31884d9c625SLionel Sambuc 	/*
31984d9c625SLionel Sambuc 	 * Copy the rest into the unix filename string, ignoring
32084d9c625SLionel Sambuc 	 * trailing blanks.
32184d9c625SLionel Sambuc 	 */
32284d9c625SLionel Sambuc 
32384d9c625SLionel Sambuc 	for (j=7; (j >= 0) && (dn[j] == ' '); j--)
32484d9c625SLionel Sambuc 		;
32584d9c625SLionel Sambuc 
32684d9c625SLionel Sambuc 	for (i = 1; i <= j; i++) {
32784d9c625SLionel Sambuc 		c = dos2unix[dn[i]];
32884d9c625SLionel Sambuc 		*un++ = lower ? u2l[c] : c;
32984d9c625SLionel Sambuc 		thislong++;
33084d9c625SLionel Sambuc 	}
33184d9c625SLionel Sambuc 	dn += 8;
33284d9c625SLionel Sambuc 
33384d9c625SLionel Sambuc 	/*
33484d9c625SLionel Sambuc 	 * Now, if there is an extension then put in a period and copy in
33584d9c625SLionel Sambuc 	 * the extension.
33684d9c625SLionel Sambuc 	 */
33784d9c625SLionel Sambuc 	if (*dn != ' ') {
33884d9c625SLionel Sambuc 		*un++ = '.';
33984d9c625SLionel Sambuc 		thislong++;
34084d9c625SLionel Sambuc 		for (i = 0; i < 3 && *dn != ' '; i++) {
34184d9c625SLionel Sambuc 			c = dos2unix[*dn++];
34284d9c625SLionel Sambuc 			*un++ = lower ? u2l[c] : c;
34384d9c625SLionel Sambuc 			thislong++;
34484d9c625SLionel Sambuc 		}
34584d9c625SLionel Sambuc 	}
34684d9c625SLionel Sambuc 	*un++ = 0;
34784d9c625SLionel Sambuc 
34884d9c625SLionel Sambuc 	return (thislong);
34984d9c625SLionel Sambuc }
35084d9c625SLionel Sambuc 
35184d9c625SLionel Sambuc /*
35284d9c625SLionel Sambuc  * Convert a unix filename to a DOS filename according to Win95 rules.
35384d9c625SLionel Sambuc  * If applicable and gen is not 0, it is inserted into the converted
35484d9c625SLionel Sambuc  * filename as a generation number.
35584d9c625SLionel Sambuc  * Returns
35684d9c625SLionel Sambuc  *	0 if name couldn't be converted
35784d9c625SLionel Sambuc  *	1 if the converted name is the same as the original
35884d9c625SLionel Sambuc  *	  (no long filename entry necessary for Win95)
35984d9c625SLionel Sambuc  *	2 if conversion was successful
36084d9c625SLionel Sambuc  *	3 if conversion was successful and generation number was inserted
36184d9c625SLionel Sambuc  */
36284d9c625SLionel Sambuc int
unix2dosfn(const u_char * un,u_char dn[12],int unlen,u_int gen)36384d9c625SLionel Sambuc unix2dosfn(const u_char *un, u_char dn[12], int unlen, u_int gen)
36484d9c625SLionel Sambuc {
36584d9c625SLionel Sambuc 	int i, j, l;
36684d9c625SLionel Sambuc 	int conv = 1;
36784d9c625SLionel Sambuc 	const u_char *cp, *dp, *dp1;
36884d9c625SLionel Sambuc 	u_char gentext[6], *wcp;
36984d9c625SLionel Sambuc 	int shortlen;
37084d9c625SLionel Sambuc 
37184d9c625SLionel Sambuc 	/*
37284d9c625SLionel Sambuc 	 * Fill the dos filename string with blanks. These are DOS's pad
37384d9c625SLionel Sambuc 	 * characters.
37484d9c625SLionel Sambuc 	 */
37584d9c625SLionel Sambuc 	for (i = 0; i < 11; i++)
37684d9c625SLionel Sambuc 		dn[i] = ' ';
37784d9c625SLionel Sambuc 	dn[11] = 0;
37884d9c625SLionel Sambuc 
37984d9c625SLionel Sambuc 	/*
38084d9c625SLionel Sambuc 	 * The filenames "." and ".." are handled specially, since they
38184d9c625SLionel Sambuc 	 * don't follow dos filename rules.
38284d9c625SLionel Sambuc 	 */
38384d9c625SLionel Sambuc 	if (un[0] == '.' && unlen == 1) {
38484d9c625SLionel Sambuc 		dn[0] = '.';
38584d9c625SLionel Sambuc 		return gen <= 1;
38684d9c625SLionel Sambuc 	}
38784d9c625SLionel Sambuc 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
38884d9c625SLionel Sambuc 		dn[0] = '.';
38984d9c625SLionel Sambuc 		dn[1] = '.';
39084d9c625SLionel Sambuc 		return gen <= 1;
39184d9c625SLionel Sambuc 	}
39284d9c625SLionel Sambuc 
39384d9c625SLionel Sambuc 	/*
39484d9c625SLionel Sambuc 	 * Filenames with only blanks and dots are not allowed!
39584d9c625SLionel Sambuc 	 */
39684d9c625SLionel Sambuc 	for (cp = un, i = unlen; --i >= 0; cp++)
39784d9c625SLionel Sambuc 		if (*cp != ' ' && *cp != '.')
39884d9c625SLionel Sambuc 			break;
39984d9c625SLionel Sambuc 	if (i < 0)
40084d9c625SLionel Sambuc 		return 0;
40184d9c625SLionel Sambuc 
40284d9c625SLionel Sambuc 	/*
40384d9c625SLionel Sambuc 	 * Now find the extension
40484d9c625SLionel Sambuc 	 * Note: dot as first char doesn't start extension
40584d9c625SLionel Sambuc 	 *	 and trailing dots and blanks are ignored
40684d9c625SLionel Sambuc 	 */
40784d9c625SLionel Sambuc 	dp = dp1 = 0;
40884d9c625SLionel Sambuc 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
40984d9c625SLionel Sambuc 		switch (*cp++) {
41084d9c625SLionel Sambuc 		case '.':
41184d9c625SLionel Sambuc 			if (!dp1)
41284d9c625SLionel Sambuc 				dp1 = cp;
41384d9c625SLionel Sambuc 			break;
41484d9c625SLionel Sambuc 		case ' ':
41584d9c625SLionel Sambuc 			break;
41684d9c625SLionel Sambuc 		default:
41784d9c625SLionel Sambuc 			if (dp1)
41884d9c625SLionel Sambuc 				dp = dp1;
41984d9c625SLionel Sambuc 			dp1 = 0;
42084d9c625SLionel Sambuc 			break;
42184d9c625SLionel Sambuc 		}
42284d9c625SLionel Sambuc 	}
42384d9c625SLionel Sambuc 
42484d9c625SLionel Sambuc 	/*
42584d9c625SLionel Sambuc 	 * Now convert it
42684d9c625SLionel Sambuc 	 */
42784d9c625SLionel Sambuc 	if (dp) {
42884d9c625SLionel Sambuc 		if (dp1)
42984d9c625SLionel Sambuc 			l = dp1 - dp;
43084d9c625SLionel Sambuc 		else
43184d9c625SLionel Sambuc 			l = unlen - (dp - un);
43284d9c625SLionel Sambuc 		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
43384d9c625SLionel Sambuc 			if (dp[i] != (dn[j] = unix2dos[dp[i]])
43484d9c625SLionel Sambuc 			    && conv != 3)
43584d9c625SLionel Sambuc 				conv = 2;
43684d9c625SLionel Sambuc 			if (!dn[j]) {
43784d9c625SLionel Sambuc 				conv = 3;
43884d9c625SLionel Sambuc 				dn[j--] = ' ';
43984d9c625SLionel Sambuc 			}
44084d9c625SLionel Sambuc 		}
44184d9c625SLionel Sambuc 		if (i < l)
44284d9c625SLionel Sambuc 			conv = 3;
44384d9c625SLionel Sambuc 		dp--;
44484d9c625SLionel Sambuc 	} else {
44584d9c625SLionel Sambuc 		for (dp = cp; *--dp == ' ' || *dp == '.';);
44684d9c625SLionel Sambuc 		dp++;
44784d9c625SLionel Sambuc 	}
44884d9c625SLionel Sambuc 
44984d9c625SLionel Sambuc 	shortlen = (dp - un) <= 8;
45084d9c625SLionel Sambuc 
45184d9c625SLionel Sambuc 	/*
45284d9c625SLionel Sambuc 	 * Now convert the rest of the name
45384d9c625SLionel Sambuc 	 */
45484d9c625SLionel Sambuc 	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
45584d9c625SLionel Sambuc 		if ((*un == ' ') && shortlen)
45684d9c625SLionel Sambuc 			dn[j] = ' ';
45784d9c625SLionel Sambuc 		else
45884d9c625SLionel Sambuc 			dn[j] = unix2dos[*un];
45984d9c625SLionel Sambuc 		if ((*un != dn[j])
46084d9c625SLionel Sambuc 		    && conv != 3)
46184d9c625SLionel Sambuc 			conv = 2;
46284d9c625SLionel Sambuc 		if (!dn[j]) {
46384d9c625SLionel Sambuc 			conv = 3;
46484d9c625SLionel Sambuc 			dn[j--] = ' ';
46584d9c625SLionel Sambuc 		}
46684d9c625SLionel Sambuc 	}
46784d9c625SLionel Sambuc 	if (un < dp)
46884d9c625SLionel Sambuc 		conv = 3;
46984d9c625SLionel Sambuc 	/*
47084d9c625SLionel Sambuc 	 * If we didn't have any chars in filename,
47184d9c625SLionel Sambuc 	 * generate a default
47284d9c625SLionel Sambuc 	 */
47384d9c625SLionel Sambuc 	if (!j)
47484d9c625SLionel Sambuc 		dn[0] = '_';
47584d9c625SLionel Sambuc 
47684d9c625SLionel Sambuc 	/*
47784d9c625SLionel Sambuc 	 * The first character cannot be E5,
47884d9c625SLionel Sambuc 	 * because that means a deleted entry
47984d9c625SLionel Sambuc 	 */
48084d9c625SLionel Sambuc 	if (dn[0] == 0xe5)
48184d9c625SLionel Sambuc 		dn[0] = SLOT_E5;
48284d9c625SLionel Sambuc 
48384d9c625SLionel Sambuc 	/*
48484d9c625SLionel Sambuc 	 * If there wasn't any char dropped,
48584d9c625SLionel Sambuc 	 * there is no place for generation numbers
48684d9c625SLionel Sambuc 	 */
48784d9c625SLionel Sambuc 	if (conv != 3) {
48884d9c625SLionel Sambuc 		if (gen > 1)
48984d9c625SLionel Sambuc 			return 0;
49084d9c625SLionel Sambuc 		return conv;
49184d9c625SLionel Sambuc 	}
49284d9c625SLionel Sambuc 
49384d9c625SLionel Sambuc 	/*
49484d9c625SLionel Sambuc 	 * Now insert the generation number into the filename part
49584d9c625SLionel Sambuc 	 */
49684d9c625SLionel Sambuc 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
49784d9c625SLionel Sambuc 		*--wcp = gen % 10 + '0';
49884d9c625SLionel Sambuc 	if (gen)
49984d9c625SLionel Sambuc 		return 0;
50084d9c625SLionel Sambuc 	for (i = 8; dn[--i] == ' ';);
50184d9c625SLionel Sambuc 	i++;
50284d9c625SLionel Sambuc 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
50384d9c625SLionel Sambuc 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
50484d9c625SLionel Sambuc 	dn[i++] = '~';
50584d9c625SLionel Sambuc 	while (wcp < gentext + sizeof(gentext))
50684d9c625SLionel Sambuc 		dn[i++] = *wcp++;
50784d9c625SLionel Sambuc 	return 3;
50884d9c625SLionel Sambuc }
50984d9c625SLionel Sambuc 
51084d9c625SLionel Sambuc /*
51184d9c625SLionel Sambuc  * Create a Win95 long name directory entry
51284d9c625SLionel Sambuc  * Note: assumes that the filename is valid,
51384d9c625SLionel Sambuc  *	 i.e. doesn't consist solely of blanks and dots
51484d9c625SLionel Sambuc  */
51584d9c625SLionel Sambuc int
unix2winfn(const u_char * un,int unlen,struct winentry * wep,int cnt,int chksum)51684d9c625SLionel Sambuc unix2winfn(const u_char *un, int unlen, struct winentry *wep, int cnt, int chksum)
51784d9c625SLionel Sambuc {
51884d9c625SLionel Sambuc 	const u_int8_t *cp;
51984d9c625SLionel Sambuc 	u_int8_t *wcp;
52084d9c625SLionel Sambuc 	int i;
52184d9c625SLionel Sambuc 
52284d9c625SLionel Sambuc 	/*
52384d9c625SLionel Sambuc 	 * Drop trailing blanks and dots
52484d9c625SLionel Sambuc 	 */
52584d9c625SLionel Sambuc 	for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
52684d9c625SLionel Sambuc 
52784d9c625SLionel Sambuc 	un += (cnt - 1) * WIN_CHARS;
52884d9c625SLionel Sambuc 	unlen -= (cnt - 1) * WIN_CHARS;
52984d9c625SLionel Sambuc 
53084d9c625SLionel Sambuc 	/*
53184d9c625SLionel Sambuc 	 * Initialize winentry to some useful default
53284d9c625SLionel Sambuc 	 */
53384d9c625SLionel Sambuc 	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
53484d9c625SLionel Sambuc 	wep->weCnt = cnt;
53584d9c625SLionel Sambuc 	wep->weAttributes = ATTR_WIN95;
53684d9c625SLionel Sambuc 	wep->weReserved1 = 0;
53784d9c625SLionel Sambuc 	wep->weChksum = chksum;
53884d9c625SLionel Sambuc 	wep->weReserved2 = 0;
53984d9c625SLionel Sambuc 
54084d9c625SLionel Sambuc 	/*
54184d9c625SLionel Sambuc 	 * Now convert the filename parts
54284d9c625SLionel Sambuc 	 */
54384d9c625SLionel Sambuc 	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
54484d9c625SLionel Sambuc 		if (--unlen < 0)
54584d9c625SLionel Sambuc 			goto done;
54684d9c625SLionel Sambuc 		*wcp++ = *un++;
54784d9c625SLionel Sambuc 		*wcp++ = 0;
54884d9c625SLionel Sambuc 	}
54984d9c625SLionel Sambuc 	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
55084d9c625SLionel Sambuc 		if (--unlen < 0)
55184d9c625SLionel Sambuc 			goto done;
55284d9c625SLionel Sambuc 		*wcp++ = *un++;
55384d9c625SLionel Sambuc 		*wcp++ = 0;
55484d9c625SLionel Sambuc 	}
55584d9c625SLionel Sambuc 	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
55684d9c625SLionel Sambuc 		if (--unlen < 0)
55784d9c625SLionel Sambuc 			goto done;
55884d9c625SLionel Sambuc 		*wcp++ = *un++;
55984d9c625SLionel Sambuc 		*wcp++ = 0;
56084d9c625SLionel Sambuc 	}
56184d9c625SLionel Sambuc 	if (!unlen)
56284d9c625SLionel Sambuc 		wep->weCnt |= WIN_LAST;
56384d9c625SLionel Sambuc 	return unlen;
56484d9c625SLionel Sambuc 
56584d9c625SLionel Sambuc done:
56684d9c625SLionel Sambuc 	*wcp++ = 0;
56784d9c625SLionel Sambuc 	*wcp++ = 0;
56884d9c625SLionel Sambuc 	wep->weCnt |= WIN_LAST;
56984d9c625SLionel Sambuc 	return 0;
57084d9c625SLionel Sambuc }
57184d9c625SLionel Sambuc 
57284d9c625SLionel Sambuc /*
57384d9c625SLionel Sambuc  * Compare our filename to the one in the Win95 entry
57484d9c625SLionel Sambuc  * Returns the checksum or -1 if no match
57584d9c625SLionel Sambuc  */
57684d9c625SLionel Sambuc int
winChkName(const u_char * un,int unlen,struct winentry * wep,int chksum)57784d9c625SLionel Sambuc winChkName(const u_char *un, int unlen, struct winentry *wep, int chksum)
57884d9c625SLionel Sambuc {
57984d9c625SLionel Sambuc 	u_int8_t *cp;
58084d9c625SLionel Sambuc 	int i;
58184d9c625SLionel Sambuc 
58284d9c625SLionel Sambuc 	/*
58384d9c625SLionel Sambuc 	 * First compare checksums
58484d9c625SLionel Sambuc 	 */
58584d9c625SLionel Sambuc 	if (wep->weCnt&WIN_LAST)
58684d9c625SLionel Sambuc 		chksum = wep->weChksum;
58784d9c625SLionel Sambuc 	else if (chksum != wep->weChksum)
58884d9c625SLionel Sambuc 		chksum = -1;
58984d9c625SLionel Sambuc 	if (chksum == -1)
59084d9c625SLionel Sambuc 		return -1;
59184d9c625SLionel Sambuc 
59284d9c625SLionel Sambuc 	/*
59384d9c625SLionel Sambuc 	 * Offset of this entry
59484d9c625SLionel Sambuc 	 */
59584d9c625SLionel Sambuc 	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
59684d9c625SLionel Sambuc 	un += i;
59784d9c625SLionel Sambuc 	if ((unlen -= i) < 0)
59884d9c625SLionel Sambuc 		return -1;
59984d9c625SLionel Sambuc 
60084d9c625SLionel Sambuc 	/*
60184d9c625SLionel Sambuc 	 * Ignore redundant winentries (those with only \0\0 on start in them).
60284d9c625SLionel Sambuc 	 * An appearance of such entry is a bug; unknown if in NetBSD msdosfs
60384d9c625SLionel Sambuc 	 * or MS Windows.
60484d9c625SLionel Sambuc 	 */
60584d9c625SLionel Sambuc 	if (unlen == 0) {
60684d9c625SLionel Sambuc 		if (wep->wePart1[0] == '\0' && wep->wePart1[1] == '\0')
60784d9c625SLionel Sambuc 			return chksum;
60884d9c625SLionel Sambuc 		else
60984d9c625SLionel Sambuc 			return -1;
61084d9c625SLionel Sambuc 	}
61184d9c625SLionel Sambuc 
61284d9c625SLionel Sambuc 	if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
61384d9c625SLionel Sambuc 		return -1;
61484d9c625SLionel Sambuc 
61584d9c625SLionel Sambuc 	/*
61684d9c625SLionel Sambuc 	 * Compare the name parts
61784d9c625SLionel Sambuc 	 */
61884d9c625SLionel Sambuc 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
61984d9c625SLionel Sambuc 		if (--unlen < 0) {
62084d9c625SLionel Sambuc 			if (!*cp++ && !*cp)
62184d9c625SLionel Sambuc 				return chksum;
62284d9c625SLionel Sambuc 			return -1;
62384d9c625SLionel Sambuc 		}
62484d9c625SLionel Sambuc 		if (u2l[*cp++] != u2l[*un++] || *cp++)
62584d9c625SLionel Sambuc 			return -1;
62684d9c625SLionel Sambuc 	}
62784d9c625SLionel Sambuc 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
62884d9c625SLionel Sambuc 		if (--unlen < 0) {
62984d9c625SLionel Sambuc 			if (!*cp++ && !*cp)
63084d9c625SLionel Sambuc 				return chksum;
63184d9c625SLionel Sambuc 			return -1;
63284d9c625SLionel Sambuc 		}
63384d9c625SLionel Sambuc 		if (u2l[*cp++] != u2l[*un++] || *cp++)
63484d9c625SLionel Sambuc 			return -1;
63584d9c625SLionel Sambuc 	}
63684d9c625SLionel Sambuc 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
63784d9c625SLionel Sambuc 		if (--unlen < 0) {
63884d9c625SLionel Sambuc 			if (!*cp++ && !*cp)
63984d9c625SLionel Sambuc 				return chksum;
64084d9c625SLionel Sambuc 			return -1;
64184d9c625SLionel Sambuc 		}
64284d9c625SLionel Sambuc 		if (u2l[*cp++] != u2l[*un++] || *cp++)
64384d9c625SLionel Sambuc 			return -1;
64484d9c625SLionel Sambuc 	}
64584d9c625SLionel Sambuc 	return chksum;
64684d9c625SLionel Sambuc }
64784d9c625SLionel Sambuc 
64884d9c625SLionel Sambuc /*
64984d9c625SLionel Sambuc  * Convert Win95 filename to dirbuf.
65084d9c625SLionel Sambuc  * Returns the checksum or -1 if impossible
65184d9c625SLionel Sambuc  */
65284d9c625SLionel Sambuc int
win2unixfn(struct winentry * wep,struct dirent * dp,int chksum)65384d9c625SLionel Sambuc win2unixfn(struct winentry *wep, struct dirent *dp, int chksum)
65484d9c625SLionel Sambuc {
65584d9c625SLionel Sambuc 	u_int8_t *cp;
65684d9c625SLionel Sambuc 	u_int8_t *np, *ep = (u_int8_t *)dp->d_name + WIN_MAXLEN;
65784d9c625SLionel Sambuc 	int i;
65884d9c625SLionel Sambuc 
65984d9c625SLionel Sambuc 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
66084d9c625SLionel Sambuc 	    || !(wep->weCnt&WIN_CNT))
66184d9c625SLionel Sambuc 		return -1;
66284d9c625SLionel Sambuc 
66384d9c625SLionel Sambuc 	/*
66484d9c625SLionel Sambuc 	 * First compare checksums
66584d9c625SLionel Sambuc 	 */
66684d9c625SLionel Sambuc 	if (wep->weCnt&WIN_LAST) {
66784d9c625SLionel Sambuc 		chksum = wep->weChksum;
66884d9c625SLionel Sambuc 		/*
66984d9c625SLionel Sambuc 		 * This works even though d_namlen is one byte!
67084d9c625SLionel Sambuc 		 */
67184d9c625SLionel Sambuc #ifdef __NetBSD__
67284d9c625SLionel Sambuc 		dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
67384d9c625SLionel Sambuc #endif
67484d9c625SLionel Sambuc 	} else if (chksum != wep->weChksum)
67584d9c625SLionel Sambuc 		chksum = -1;
67684d9c625SLionel Sambuc 	if (chksum == -1)
67784d9c625SLionel Sambuc 		return -1;
67884d9c625SLionel Sambuc 
67984d9c625SLionel Sambuc 	/*
68084d9c625SLionel Sambuc 	 * Offset of this entry
68184d9c625SLionel Sambuc 	 */
68284d9c625SLionel Sambuc 	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
68384d9c625SLionel Sambuc 	np = (u_int8_t *)dp->d_name + i;
68484d9c625SLionel Sambuc 
68584d9c625SLionel Sambuc 	/*
68684d9c625SLionel Sambuc 	 * Convert the name parts
68784d9c625SLionel Sambuc 	 */
68884d9c625SLionel Sambuc 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
68984d9c625SLionel Sambuc 		switch (*np++ = *cp++) {
69084d9c625SLionel Sambuc 		case 0:
69184d9c625SLionel Sambuc #ifdef __NetBSD__
69284d9c625SLionel Sambuc 			dp->d_namlen -= sizeof(wep->wePart2)/2
69384d9c625SLionel Sambuc 			    + sizeof(wep->wePart3)/2 + i + 1;
69484d9c625SLionel Sambuc #endif
69584d9c625SLionel Sambuc 			return chksum;
69684d9c625SLionel Sambuc 		case '/':
69784d9c625SLionel Sambuc 			np[-1] = 0;
69884d9c625SLionel Sambuc 			return -1;
69984d9c625SLionel Sambuc 		}
70084d9c625SLionel Sambuc 		/*
70184d9c625SLionel Sambuc 		 * The size comparison should result in the compiler
70284d9c625SLionel Sambuc 		 * optimizing the whole if away
70384d9c625SLionel Sambuc 		 */
70484d9c625SLionel Sambuc 		if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
70584d9c625SLionel Sambuc 		    && np > ep) {
70684d9c625SLionel Sambuc 			np[-1] = 0;
70784d9c625SLionel Sambuc 			return -1;
70884d9c625SLionel Sambuc 		}
70984d9c625SLionel Sambuc 		if (*cp++)
71084d9c625SLionel Sambuc 			return -1;
71184d9c625SLionel Sambuc 	}
71284d9c625SLionel Sambuc 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
71384d9c625SLionel Sambuc 		switch (*np++ = *cp++) {
71484d9c625SLionel Sambuc 		case 0:
71584d9c625SLionel Sambuc #ifdef __NetBSD__
71684d9c625SLionel Sambuc 			dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
71784d9c625SLionel Sambuc #endif
71884d9c625SLionel Sambuc 			return chksum;
71984d9c625SLionel Sambuc 		case '/':
72084d9c625SLionel Sambuc 			np[-1] = 0;
72184d9c625SLionel Sambuc 			return -1;
72284d9c625SLionel Sambuc 		}
72384d9c625SLionel Sambuc 		/*
72484d9c625SLionel Sambuc 		 * The size comparisons should be optimized away
72584d9c625SLionel Sambuc 		 */
72684d9c625SLionel Sambuc 		if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
72784d9c625SLionel Sambuc 		    && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
72884d9c625SLionel Sambuc 		    && np > ep) {
72984d9c625SLionel Sambuc 			np[-1] = 0;
73084d9c625SLionel Sambuc 			return -1;
73184d9c625SLionel Sambuc 		}
73284d9c625SLionel Sambuc 		if (*cp++)
73384d9c625SLionel Sambuc 			return -1;
73484d9c625SLionel Sambuc 	}
73584d9c625SLionel Sambuc 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
73684d9c625SLionel Sambuc 		switch (*np++ = *cp++) {
73784d9c625SLionel Sambuc 		case 0:
73884d9c625SLionel Sambuc #ifdef __NetBSD__
73984d9c625SLionel Sambuc 			dp->d_namlen -= i + 1;
74084d9c625SLionel Sambuc #endif
74184d9c625SLionel Sambuc 			return chksum;
74284d9c625SLionel Sambuc 		case '/':
74384d9c625SLionel Sambuc 			np[-1] = 0;
74484d9c625SLionel Sambuc 			return -1;
74584d9c625SLionel Sambuc 		}
74684d9c625SLionel Sambuc 		/*
74784d9c625SLionel Sambuc 		 * See above
74884d9c625SLionel Sambuc 		 */
74984d9c625SLionel Sambuc 		if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
75084d9c625SLionel Sambuc 		    && np > ep) {
75184d9c625SLionel Sambuc 			np[-1] = 0;
75284d9c625SLionel Sambuc 			return -1;
75384d9c625SLionel Sambuc 		}
75484d9c625SLionel Sambuc 		if (*cp++)
75584d9c625SLionel Sambuc 			return -1;
75684d9c625SLionel Sambuc 	}
75784d9c625SLionel Sambuc 	return chksum;
75884d9c625SLionel Sambuc }
75984d9c625SLionel Sambuc 
76084d9c625SLionel Sambuc /*
76184d9c625SLionel Sambuc  * Compute the checksum of a DOS filename for Win95 use
76284d9c625SLionel Sambuc  */
76384d9c625SLionel Sambuc u_int8_t
winChksum(u_int8_t * name)76484d9c625SLionel Sambuc winChksum(u_int8_t *name)
76584d9c625SLionel Sambuc {
76684d9c625SLionel Sambuc 	int i;
76784d9c625SLionel Sambuc 	u_int8_t s;
76884d9c625SLionel Sambuc 
76984d9c625SLionel Sambuc 	for (s = 0, i = 11; --i >= 0; s += *name++)
77084d9c625SLionel Sambuc 		s = (s << 7)|(s >> 1);
77184d9c625SLionel Sambuc 	return s;
77284d9c625SLionel Sambuc }
77384d9c625SLionel Sambuc 
77484d9c625SLionel Sambuc /*
77584d9c625SLionel Sambuc  * Determine the number of slots necessary for Win95 names
77684d9c625SLionel Sambuc  */
77784d9c625SLionel Sambuc int
winSlotCnt(const u_char * un,int unlen)77884d9c625SLionel Sambuc winSlotCnt(const u_char *un, int unlen)
77984d9c625SLionel Sambuc {
78084d9c625SLionel Sambuc 	for (un += unlen; unlen > 0; unlen--)
78184d9c625SLionel Sambuc 		if (*--un != ' ' && *un != '.')
78284d9c625SLionel Sambuc 			break;
78384d9c625SLionel Sambuc 	if (unlen > WIN_MAXLEN)
78484d9c625SLionel Sambuc 		return 0;
78584d9c625SLionel Sambuc 	return howmany(unlen, WIN_CHARS);
78684d9c625SLionel Sambuc }
787