msdosfs_conv.c revision 33750
133750Sache/*	$Id: msdosfs_conv.c,v 1.18 1998/02/22 15:09:39 ache Exp $ */
233548Sjkh/*	$NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $	*/
32893Sdfr
433548Sjkh/*-
533548Sjkh * Copyright (C) 1995, 1997 Wolfgang Solfrank.
633548Sjkh * Copyright (C) 1995, 1997 TooLs GmbH.
733548Sjkh * All rights reserved.
833548Sjkh * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
933548Sjkh *
1033548Sjkh * Redistribution and use in source and binary forms, with or without
1133548Sjkh * modification, are permitted provided that the following conditions
1233548Sjkh * are met:
1333548Sjkh * 1. Redistributions of source code must retain the above copyright
1433548Sjkh *    notice, this list of conditions and the following disclaimer.
1533548Sjkh * 2. Redistributions in binary form must reproduce the above copyright
1633548Sjkh *    notice, this list of conditions and the following disclaimer in the
1733548Sjkh *    documentation and/or other materials provided with the distribution.
1833548Sjkh * 3. All advertising materials mentioning features or use of this software
1933548Sjkh *    must display the following acknowledgement:
2033548Sjkh *	This product includes software developed by TooLs GmbH.
2133548Sjkh * 4. The name of TooLs GmbH may not be used to endorse or promote products
2233548Sjkh *    derived from this software without specific prior written permission.
2333548Sjkh *
2433548Sjkh * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2533548Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2633548Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2733548Sjkh * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2833548Sjkh * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2933548Sjkh * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3033548Sjkh * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3133548Sjkh * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3233548Sjkh * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3333548Sjkh * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3433548Sjkh */
352893Sdfr/*
362893Sdfr * Written by Paul Popelka (paulp@uts.amdahl.com)
378876Srgrimes *
382893Sdfr * You can do anything you want with this software, just don't say you wrote
392893Sdfr * it, and don't remove this notice.
408876Srgrimes *
412893Sdfr * This software is provided "as is".
428876Srgrimes *
432893Sdfr * The author supplies this software to be publicly redistributed on the
442893Sdfr * understanding that the author is not responsible for the correct
452893Sdfr * functioning of this software in any circumstances and is not liable for
462893Sdfr * any damages caused by this software.
478876Srgrimes *
482893Sdfr * October 1992
492893Sdfr */
502893Sdfr
512893Sdfr/*
522893Sdfr * System include files.
532893Sdfr */
542893Sdfr#include <sys/param.h>
552893Sdfr#include <sys/time.h>
562893Sdfr#include <sys/kernel.h>		/* defines tz */
5733548Sjkh#include <sys/systm.h>
587465Sache#include <machine/clock.h>
5933548Sjkh#include <sys/dirent.h>
602893Sdfr
612893Sdfr/*
622893Sdfr * MSDOSFS include files.
632893Sdfr */
642893Sdfr#include <msdosfs/direntry.h>
652893Sdfr
662893Sdfr/*
677465Sache * Total number of days that have passed for each month in a regular year.
682893Sdfr */
6912144Sphkstatic u_short regyear[] = {
707465Sache	31, 59, 90, 120, 151, 181,
717465Sache	212, 243, 273, 304, 334, 365
722893Sdfr};
732893Sdfr
742893Sdfr/*
757465Sache * Total number of days that have passed for each month in a leap year.
762893Sdfr */
7712144Sphkstatic u_short leapyear[] = {
787465Sache	31, 60, 91, 121, 152, 182,
797465Sache	213, 244, 274, 305, 335, 366
802893Sdfr};
812893Sdfr
822893Sdfr/*
832893Sdfr * Variables used to remember parts of the last time conversion.  Maybe we
842893Sdfr * can avoid a full conversion.
852893Sdfr */
8633181Seivindstatic u_long  lasttime;
8733181Seivindstatic u_long  lastday;
8833181Seivindstatic u_short lastddate;
8933181Seivindstatic u_short lastdtime;
902893Sdfr
9133747Sachestatic inline u_int8_t find_lcode __P((u_int16_t code, u_int16_t *u2w));
9233747Sache
932893Sdfr/*
942893Sdfr * Convert the unix version of time to dos's idea of time to be used in
952893Sdfr * file timestamps. The passed in unix time is assumed to be in GMT.
962893Sdfr */
972893Sdfrvoid
9833548Sjkhunix2dostime(tsp, ddp, dtp, dhp)
992893Sdfr	struct timespec *tsp;
10033548Sjkh	u_int16_t *ddp;
10133548Sjkh	u_int16_t *dtp;
10233548Sjkh	u_int8_t *dhp;
1032893Sdfr{
1042893Sdfr	u_long t;
1052893Sdfr	u_long days;
1062893Sdfr	u_long inc;
1072893Sdfr	u_long year;
1082893Sdfr	u_long month;
1092893Sdfr	u_short *months;
1102893Sdfr
1112893Sdfr	/*
1122893Sdfr	 * If the time from the last conversion is the same as now, then
1132893Sdfr	 * skip the computations and use the saved result.
1142893Sdfr	 */
11518397Snate	t = tsp->tv_sec - (tz.tz_minuteswest * 60)
11615055Sache	    - (wall_cmos_clock ? adjkerntz : 0);
11715053Sache	    /* - daylight savings time correction */
11833548Sjkh	t &= ~1;
1192893Sdfr	if (lasttime != t) {
1202893Sdfr		lasttime = t;
12133548Sjkh		lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
1222893Sdfr		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
1232893Sdfr		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
1242893Sdfr
1252893Sdfr		/*
1262893Sdfr		 * If the number of days since 1970 is the same as the last
1272893Sdfr		 * time we did the computation then skip all this leap year
1282893Sdfr		 * and month stuff.
1292893Sdfr		 */
1302893Sdfr		days = t / (24 * 60 * 60);
1312893Sdfr		if (days != lastday) {
1322893Sdfr			lastday = days;
1332893Sdfr			for (year = 1970;; year++) {
1342893Sdfr				inc = year & 0x03 ? 365 : 366;
1352893Sdfr				if (days < inc)
1362893Sdfr					break;
1372893Sdfr				days -= inc;
1382893Sdfr			}
1392893Sdfr			months = year & 0x03 ? regyear : leapyear;
14020138Sbde			for (month = 0; days >= months[month]; month++)
1417465Sache				;
1427465Sache			if (month > 0)
1437465Sache				days -= months[month - 1];
1442893Sdfr			lastddate = ((days + 1) << DD_DAY_SHIFT)
1452893Sdfr			    + ((month + 1) << DD_MONTH_SHIFT);
1462893Sdfr			/*
1472893Sdfr			 * Remember dos's idea of time is relative to 1980.
1482893Sdfr			 * unix's is relative to 1970.  If somehow we get a
1492893Sdfr			 * time before 1980 then don't give totally crazy
1502893Sdfr			 * results.
1512893Sdfr			 */
1522893Sdfr			if (year > 1980)
1532893Sdfr				lastddate += (year - 1980) << DD_YEAR_SHIFT;
1542893Sdfr		}
1552893Sdfr	}
15633548Sjkh	if (dtp)
15733548Sjkh		*dtp = lastdtime;
15833548Sjkh	if (dhp)
15933548Sjkh		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
16033548Sjkh
1612893Sdfr	*ddp = lastddate;
1622893Sdfr}
1632893Sdfr
1642893Sdfr/*
1652893Sdfr * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
1662893Sdfr * interval there were 8 regular years and 2 leap years.
1672893Sdfr */
1682893Sdfr#define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
1692893Sdfr
17033181Seivindstatic u_short lastdosdate;
17133181Seivindstatic u_long  lastseconds;
1722893Sdfr
1732893Sdfr/*
1742893Sdfr * Convert from dos' idea of time to unix'. This will probably only be
1752893Sdfr * called from the stat(), and fstat() system calls and so probably need
1762893Sdfr * not be too efficient.
1772893Sdfr */
1782893Sdfrvoid
17933548Sjkhdos2unixtime(dd, dt, dh, tsp)
18033548Sjkh	u_int dd;
18133548Sjkh	u_int dt;
18233548Sjkh	u_int dh;
1832893Sdfr	struct timespec *tsp;
1842893Sdfr{
1852893Sdfr	u_long seconds;
18611921Sphk	u_long month;
18711921Sphk	u_long year;
1882893Sdfr	u_long days;
1892893Sdfr	u_short *months;
1902893Sdfr
19133548Sjkh	if (dd == 0) {
19233548Sjkh		/*
19333548Sjkh		 * Uninitialized field, return the epoch.
19433548Sjkh		 */
19533548Sjkh		tsp->tv_sec = 0;
19633548Sjkh		tsp->tv_nsec = 0;
19733548Sjkh		return;
19833548Sjkh	}
1995083Sbde	seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
2002893Sdfr	    + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
20133548Sjkh	    + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
20233548Sjkh	    + dh / 100;
2032893Sdfr	/*
2042893Sdfr	 * If the year, month, and day from the last conversion are the
2052893Sdfr	 * same then use the saved value.
2062893Sdfr	 */
2072893Sdfr	if (lastdosdate != dd) {
2082893Sdfr		lastdosdate = dd;
2092893Sdfr		days = 0;
2102893Sdfr		year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
2117465Sache		days = year * 365;
2127465Sache		days += year / 4 + 1;	/* add in leap days */
2137465Sache		if ((year & 0x03) == 0)
2147465Sache			days--;		/* if year is a leap year */
2152893Sdfr		months = year & 0x03 ? regyear : leapyear;
2162893Sdfr		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
2177465Sache		if (month < 1 || month > 12) {
21833548Sjkh			printf("dos2unixtime(): month value out of range (%ld)\n",
2193152Sphk			    month);
2202893Sdfr			month = 1;
2212893Sdfr		}
2227465Sache		if (month > 1)
2237465Sache			days += months[month - 2];
2242893Sdfr		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
2252893Sdfr		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
2262893Sdfr	}
22718397Snate	tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
22815055Sache	     + adjkerntz;
22915053Sache	     /* + daylight savings time correction */
23033548Sjkh	tsp->tv_nsec = (dh % 100) * 10000000;
2312893Sdfr}
2322893Sdfr
23333548Sjkhstatic u_char
23433548Sjkhunix2dos[256] = {
23533548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
23633548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
23733548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
23833548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
23933548Sjkh	0,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
24033548Sjkh	0x28, 0x29, 0,    0,    0,    0x2d, 0,    0,	/* 28-2f */
24133548Sjkh	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
24233548Sjkh	0x38, 0x39, 0,    0,    0,    0,    0,    0,	/* 38-3f */
24333548Sjkh	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
24433548Sjkh	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
24533548Sjkh	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
24633548Sjkh	0x58, 0x59, 0x5a, 0,    0,    0,    0x5e, 0x5f,	/* 58-5f */
24733548Sjkh	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
24833548Sjkh	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
24933548Sjkh	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
25033548Sjkh	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
25133548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
25233548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
25333548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
25433548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
25533548Sjkh	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
25633548Sjkh	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
25733548Sjkh	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
25833548Sjkh	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
25933548Sjkh	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
26033548Sjkh	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
26133548Sjkh	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
26233548Sjkh	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
26333548Sjkh	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
26433548Sjkh	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
26533548Sjkh	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
26633548Sjkh	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
26733548Sjkh};
2682893Sdfr
26933548Sjkhstatic u_char
27033548Sjkhdos2unix[256] = {
27133548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
27233548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
27333548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
27433548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
27533548Sjkh	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
27633548Sjkh	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
27733548Sjkh	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
27833548Sjkh	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
27933548Sjkh	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
28033548Sjkh	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
28133548Sjkh	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
28233548Sjkh	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
28333548Sjkh	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
28433548Sjkh	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
28533548Sjkh	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
28633548Sjkh	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
28733548Sjkh	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
28833548Sjkh	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
28933548Sjkh	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
29033548Sjkh	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
29133548Sjkh	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
29233548Sjkh	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
29333548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
29433548Sjkh	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
29533548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
29633548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
29733548Sjkh	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
29833548Sjkh	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
29933548Sjkh	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
30033548Sjkh	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
30133548Sjkh	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
30233548Sjkh	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
30333548Sjkh};
30433548Sjkh
30533548Sjkhstatic u_char
30633548Sjkhu2l[256] = {
30733548Sjkh	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
30833548Sjkh	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
30933548Sjkh	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
31033548Sjkh	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
31133548Sjkh	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
31233548Sjkh	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
31333548Sjkh	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
31433548Sjkh	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
31533548Sjkh	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
31633548Sjkh	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
31733548Sjkh	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
31833548Sjkh	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
31933548Sjkh	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
32033548Sjkh	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
32133548Sjkh	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
32233548Sjkh	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
32333548Sjkh	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
32433548Sjkh	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
32533548Sjkh	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
32633548Sjkh	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
32733548Sjkh	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
32833548Sjkh	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
32933548Sjkh	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
33033548Sjkh	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
33133548Sjkh	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
33233548Sjkh	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
33333548Sjkh	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
33433548Sjkh	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
33533548Sjkh	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
33633548Sjkh	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
33733548Sjkh	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
33833548Sjkh	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
33933548Sjkh};
34033548Sjkh
3412893Sdfr/*
3422893Sdfr * DOS filenames are made of 2 parts, the name part and the extension part.
3432893Sdfr * The name part is 8 characters long and the extension part is 3
3442893Sdfr * characters long.  They may contain trailing blanks if the name or
3452893Sdfr * extension are not long enough to fill their respective fields.
3462893Sdfr */
3472893Sdfr
3482893Sdfr/*
3492893Sdfr * Convert a DOS filename to a unix filename. And, return the number of
3502893Sdfr * characters in the resulting unix filename excluding the terminating
3512893Sdfr * null.
3522893Sdfr */
3532893Sdfrint
35433548Sjkhdos2unixfn(dn, un, lower)
3552893Sdfr	u_char dn[11];
3562893Sdfr	u_char *un;
35733548Sjkh	int lower;
3582893Sdfr{
3592893Sdfr	int i;
36033548Sjkh	int thislong = 1;
3612893Sdfr	u_char c;
3622893Sdfr
3632893Sdfr	/*
36433548Sjkh	 * If first char of the filename is SLOT_E5 (0x05), then the real
36533548Sjkh	 * first char of the filename should be 0xe5. But, they couldn't
36633548Sjkh	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
36733548Sjkh	 * directory slot. Another dos quirk.
3682893Sdfr	 */
36933548Sjkh	if (*dn == SLOT_E5)
37033548Sjkh		c = dos2unix[0xe5];
37133548Sjkh	else
37233548Sjkh		c = dos2unix[*dn];
37333548Sjkh	*un++ = lower ? u2l[c] : c;
37433548Sjkh	dn++;
3752893Sdfr
3762893Sdfr	/*
37733548Sjkh	 * Copy the name portion into the unix filename string.
3782893Sdfr	 */
37933548Sjkh	for (i = 1; i < 8 && *dn != ' '; i++) {
38033548Sjkh		c = dos2unix[*dn++];
38133548Sjkh		*un++ = lower ? u2l[c] : c;
3822893Sdfr		thislong++;
3832893Sdfr	}
38433548Sjkh	dn += 8 - i;
3852893Sdfr
3862893Sdfr	/*
3872893Sdfr	 * Now, if there is an extension then put in a period and copy in
3882893Sdfr	 * the extension.
3892893Sdfr	 */
39033548Sjkh	if (*dn != ' ') {
3912893Sdfr		*un++ = '.';
3922893Sdfr		thislong++;
39333548Sjkh		for (i = 0; i < 3 && *dn != ' '; i++) {
39433548Sjkh			c = dos2unix[*dn++];
39533548Sjkh			*un++ = lower ? u2l[c] : c;
3962893Sdfr			thislong++;
3972893Sdfr		}
3982893Sdfr	}
3992893Sdfr	*un++ = 0;
4002893Sdfr
40133548Sjkh	return (thislong);
4022893Sdfr}
4032893Sdfr
4042893Sdfr/*
40533548Sjkh * Convert a unix filename to a DOS filename according to Win95 rules.
40633548Sjkh * If applicable and gen is not 0, it is inserted into the converted
40733548Sjkh * filename as a generation number.
40833548Sjkh * Returns
40933548Sjkh *	0 if name couldn't be converted
41033548Sjkh *	1 if the converted name is the same as the original
41133548Sjkh *	  (no long filename entry necessary for Win95)
41233548Sjkh *	2 if conversion was successful
41333548Sjkh *	3 if conversion was successful and generation number was inserted
4142893Sdfr */
41533548Sjkhint
41633548Sjkhunix2dosfn(un, dn, unlen, gen)
41733548Sjkh	const u_char *un;
41833548Sjkh	u_char dn[12];
4192893Sdfr	int unlen;
42033548Sjkh	u_int gen;
4212893Sdfr{
42233548Sjkh	int i, j, l;
42333548Sjkh	int conv = 1;
42433548Sjkh	const u_char *cp, *dp, *dp1;
42533548Sjkh	u_char gentext[6], *wcp;
4262893Sdfr
4272893Sdfr	/*
4282893Sdfr	 * Fill the dos filename string with blanks. These are DOS's pad
4292893Sdfr	 * characters.
4302893Sdfr	 */
43133548Sjkh	for (i = 0; i < 11; i++)
4322893Sdfr		dn[i] = ' ';
43333548Sjkh	dn[11] = 0;
4342893Sdfr
4352893Sdfr	/*
4362893Sdfr	 * The filenames "." and ".." are handled specially, since they
4372893Sdfr	 * don't follow dos filename rules.
4382893Sdfr	 */
4392893Sdfr	if (un[0] == '.' && unlen == 1) {
4402893Sdfr		dn[0] = '.';
44133548Sjkh		return gen <= 1;
4422893Sdfr	}
4432893Sdfr	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
4442893Sdfr		dn[0] = '.';
4452893Sdfr		dn[1] = '.';
44633548Sjkh		return gen <= 1;
4472893Sdfr	}
4482893Sdfr
4492893Sdfr	/*
45033548Sjkh	 * Filenames with only blanks and dots are not allowed!
4512893Sdfr	 */
45233548Sjkh	for (cp = un, i = unlen; --i >= 0; cp++)
45333548Sjkh		if (*cp != ' ' && *cp != '.')
45433548Sjkh			break;
45533548Sjkh	if (i < 0)
45633548Sjkh		return 0;
45733548Sjkh
45833548Sjkh	/*
45933548Sjkh	 * Now find the extension
46033548Sjkh	 * Note: dot as first char doesn't start extension
46133548Sjkh	 *	 and trailing dots and blanks are ignored
46233548Sjkh	 */
46333548Sjkh	dp = dp1 = 0;
46433548Sjkh	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
46533548Sjkh		switch (*cp++) {
46633548Sjkh		case '.':
46733548Sjkh			if (!dp1)
46833548Sjkh				dp1 = cp;
46933548Sjkh			break;
47033548Sjkh		case ' ':
47133548Sjkh			break;
47233548Sjkh		default:
47333548Sjkh			if (dp1)
47433548Sjkh				dp = dp1;
47533548Sjkh			dp1 = 0;
47633548Sjkh			break;
47733548Sjkh		}
4782893Sdfr	}
4792893Sdfr
4802893Sdfr	/*
48133548Sjkh	 * Now convert it
4822893Sdfr	 */
48333548Sjkh	if (dp) {
48433548Sjkh		if (dp1)
48533548Sjkh			l = dp1 - dp;
48633548Sjkh		else
48733548Sjkh			l = unlen - (dp - un);
48833548Sjkh		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
48933548Sjkh			if (dp[i] != (dn[j] = unix2dos[dp[i]])
49033548Sjkh			    && conv != 3)
49133548Sjkh				conv = 2;
49233548Sjkh			if (!dn[j]) {
49333548Sjkh				conv = 3;
49433548Sjkh				dn[j--] = ' ';
49533548Sjkh			}
49633548Sjkh		}
49733548Sjkh		if (i < l)
49833548Sjkh			conv = 3;
49933548Sjkh		dp--;
50033548Sjkh	} else {
50133548Sjkh		for (dp = cp; *--dp == ' ' || *dp == '.';);
50233548Sjkh		dp++;
50333548Sjkh	}
50433548Sjkh
50533548Sjkh	/*
50633548Sjkh	 * Now convert the rest of the name
50733548Sjkh	 */
50833548Sjkh	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
50933548Sjkh		if (*un != (dn[j] = unix2dos[*un])
51033548Sjkh		    && conv != 3)
51133548Sjkh			conv = 2;
51233548Sjkh		if (!dn[j]) {
51333548Sjkh			conv = 3;
51433548Sjkh			dn[j--] = ' ';
51533548Sjkh		}
51633548Sjkh	}
51733548Sjkh	if (un < dp)
51833548Sjkh		conv = 3;
51933548Sjkh	/*
52033548Sjkh	 * If we didn't have any chars in filename,
52133548Sjkh	 * generate a default
52233548Sjkh	 */
52333548Sjkh	if (!j)
52433548Sjkh		dn[0] = '_';
52533548Sjkh
52633548Sjkh	/*
52733548Sjkh	 * The first character cannot be E5,
52833548Sjkh	 * because that means a deleted entry
52933548Sjkh	 */
53033548Sjkh	if (dn[0] == 0xe5)
5312893Sdfr		dn[0] = SLOT_E5;
5322893Sdfr
5332893Sdfr	/*
53433548Sjkh	 * If there wasn't any char dropped,
53533548Sjkh	 * there is no place for generation numbers
5362893Sdfr	 */
53733548Sjkh	if (conv != 3) {
53833548Sjkh		if (gen > 1)
53933548Sjkh			return 0;
54033548Sjkh		return conv;
5412893Sdfr	}
5422893Sdfr
5432893Sdfr	/*
54433548Sjkh	 * Now insert the generation number into the filename part
5452893Sdfr	 */
54633548Sjkh	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
54733548Sjkh		*--wcp = gen % 10 + '0';
54833548Sjkh	if (gen)
54933548Sjkh		return 0;
55033548Sjkh	for (i = 8; dn[--i] == ' ';);
55133548Sjkh	i++;
55233548Sjkh	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
55333548Sjkh		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
55433548Sjkh	dn[i++] = '~';
55533548Sjkh	while (wcp < gentext + sizeof(gentext))
55633548Sjkh		dn[i++] = *wcp++;
55733548Sjkh	return 3;
55833548Sjkh}
55933548Sjkh
56033548Sjkh/*
56133548Sjkh * Create a Win95 long name directory entry
56233548Sjkh * Note: assumes that the filename is valid,
56333548Sjkh *	 i.e. doesn't consist solely of blanks and dots
56433548Sjkh */
56533548Sjkhint
56633747Sacheunix2winfn(un, unlen, wep, cnt, chksum, table_loaded, u2w)
56733548Sjkh	const u_char *un;
56833548Sjkh	int unlen;
56933548Sjkh	struct winentry *wep;
57033548Sjkh	int cnt;
57133548Sjkh	int chksum;
57233747Sache	int table_loaded;
57333747Sache	u_int16_t *u2w;
57433548Sjkh{
57533548Sjkh	const u_int8_t *cp;
57633548Sjkh	u_int8_t *wcp;
57733548Sjkh	int i;
57833747Sache	u_int16_t code;
57933548Sjkh
58033548Sjkh	/*
58133548Sjkh	 * Drop trailing blanks and dots
58233548Sjkh	 */
58333548Sjkh	for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
58433548Sjkh
58533548Sjkh	un += (cnt - 1) * WIN_CHARS;
58633548Sjkh	unlen -= (cnt - 1) * WIN_CHARS;
58733548Sjkh
58833548Sjkh	/*
58933548Sjkh	 * Initialize winentry to some useful default
59033548Sjkh	 */
59133548Sjkh	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
59233548Sjkh	wep->weCnt = cnt;
59333548Sjkh	wep->weAttributes = ATTR_WIN95;
59433548Sjkh	wep->weReserved1 = 0;
59533548Sjkh	wep->weChksum = chksum;
59633548Sjkh	wep->weReserved2 = 0;
59733548Sjkh
59833548Sjkh	/*
59933548Sjkh	 * Now convert the filename parts
60033548Sjkh	 */
60133548Sjkh	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
60233548Sjkh		if (--unlen < 0)
60333548Sjkh			goto done;
60433747Sache		if (table_loaded && (*un & 0x80)) {
60533747Sache			code = u2w[*un++ & 0x7f];
60633747Sache			*wcp++ = code;
60733747Sache			*wcp++ = code >> 8;
60833747Sache		} else {
60933747Sache			*wcp++ = *un++;
61033747Sache			*wcp++ = 0;
61133747Sache		}
6122893Sdfr	}
61333548Sjkh	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
61433548Sjkh		if (--unlen < 0)
61533548Sjkh			goto done;
61633747Sache		if (table_loaded && (*un & 0x80)) {
61733747Sache			code = u2w[*un++ & 0x7f];
61833747Sache			*wcp++ = code;
61933747Sache			*wcp++ = code >> 8;
62033747Sache		} else {
62133747Sache			*wcp++ = *un++;
62233747Sache			*wcp++ = 0;
62333747Sache		}
62433548Sjkh	}
62533548Sjkh	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
62633548Sjkh		if (--unlen < 0)
62733548Sjkh			goto done;
62833747Sache		if (table_loaded && (*un & 0x80)) {
62933747Sache			code = u2w[*un++ & 0x7f];
63033747Sache			*wcp++ = code;
63133747Sache			*wcp++ = code >> 8;
63233747Sache		} else {
63333747Sache			*wcp++ = *un++;
63433747Sache			*wcp++ = 0;
63533747Sache		}
63633548Sjkh	}
63733548Sjkh	if (!unlen)
63833548Sjkh		wep->weCnt |= WIN_LAST;
63933548Sjkh	return unlen;
64033548Sjkh
64133548Sjkhdone:
64233548Sjkh	*wcp++ = 0;
64333548Sjkh	*wcp++ = 0;
64433548Sjkh	wep->weCnt |= WIN_LAST;
64533548Sjkh	return 0;
6462893Sdfr}
6472893Sdfr
64833750Sachestatic inline u_int8_t
64933750Sachefind_lcode(code, u2w)
65033750Sache	u_int16_t code;
65133750Sache	u_int16_t *u2w;
65233750Sache{
65333750Sache	int i;
65433750Sache
65533750Sache	for (i = 0; i < 128; i++)
65633750Sache		if (u2w[i] == code)
65733750Sache			return (i | 0x80);
65833750Sache	return '?';
65933750Sache}
66033750Sache
6612893Sdfr/*
66233548Sjkh * Compare our filename to the one in the Win95 entry
66333548Sjkh * Returns the checksum or -1 if no match
6642893Sdfr */
66533548Sjkhint
66633750SachewinChkName(un, unlen, wep, chksum, table_loaded, u2w)
66733548Sjkh	const u_char *un;
66833548Sjkh	int unlen;
66933548Sjkh	struct winentry *wep;
67033548Sjkh	int chksum;
67133750Sache	int table_loaded;
67233750Sache	u_int16_t *u2w;
67333548Sjkh{
67433548Sjkh	u_int8_t *cp;
67533548Sjkh	int i;
67633750Sache	u_int16_t code;
67733548Sjkh
67833548Sjkh	/*
67933548Sjkh	 * First compare checksums
68033548Sjkh	 */
68133548Sjkh	if (wep->weCnt&WIN_LAST)
68233548Sjkh		chksum = wep->weChksum;
68333548Sjkh	else if (chksum != wep->weChksum)
68433548Sjkh		chksum = -1;
68533548Sjkh	if (chksum == -1)
68633548Sjkh		return -1;
68733548Sjkh
68833548Sjkh	/*
68933548Sjkh	 * Offset of this entry
69033548Sjkh	 */
69133548Sjkh	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
69233548Sjkh	un += i;
69333548Sjkh	if ((unlen -= i) <= 0)
69433548Sjkh		return -1;
69533548Sjkh	if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
69633548Sjkh		return -1;
69733548Sjkh
69833548Sjkh	/*
69933548Sjkh	 * Compare the name parts
70033548Sjkh	 */
70133548Sjkh	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
70233548Sjkh		if (--unlen < 0) {
70333548Sjkh			if (!*cp++ && !*cp)
70433548Sjkh				return chksum;
70533548Sjkh			return -1;
70633548Sjkh		}
70733750Sache		code = (cp[1] << 8) | cp[0];
70833750Sache		if (code & 0xff80) {
70933750Sache			if (table_loaded)
71033750Sache				code = find_lcode(code, u2w);
71133750Sache			else if (code & 0xff00)
71233750Sache				code = '?';
71333750Sache		}
71433750Sache		if (u2l[code] != u2l[*un++])
71533548Sjkh			return -1;
71633750Sache		cp += 2;
71733548Sjkh	}
71833548Sjkh	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
71933548Sjkh		if (--unlen < 0) {
72033548Sjkh			if (!*cp++ && !*cp)
72133548Sjkh				return chksum;
72233548Sjkh			return -1;
72333548Sjkh		}
72433750Sache		code = (cp[1] << 8) | cp[0];
72533750Sache		if (code & 0xff80) {
72633750Sache			if (table_loaded)
72733750Sache				code = find_lcode(code, u2w);
72833750Sache			else if (code & 0xff00)
72933750Sache				code = '?';
73033750Sache		}
73133750Sache		if (u2l[code] != u2l[*un++])
73233548Sjkh			return -1;
73333750Sache		cp += 2;
73433548Sjkh	}
73533548Sjkh	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
73633548Sjkh		if (--unlen < 0) {
73733548Sjkh			if (!*cp++ && !*cp)
73833548Sjkh				return chksum;
73933548Sjkh			return -1;
74033548Sjkh		}
74133750Sache		code = (cp[1] << 8) | cp[0];
74233750Sache		if (code & 0xff80) {
74333750Sache			if (table_loaded)
74433750Sache				code = find_lcode(code, u2w);
74533750Sache			else if (code & 0xff00)
74633750Sache				code = '?';
74733750Sache		}
74833750Sache		if (u2l[code] != u2l[*un++])
74933548Sjkh			return -1;
75033750Sache		cp += 2;
75133548Sjkh	}
75233548Sjkh	return chksum;
75333548Sjkh}
75433548Sjkh
75533548Sjkh/*
75633548Sjkh * Convert Win95 filename to dirbuf.
75733548Sjkh * Returns the checksum or -1 if impossible
75833548Sjkh */
75933548Sjkhint
76033747Sachewin2unixfn(wep, dp, chksum, table_loaded, u2w)
76133548Sjkh	struct winentry *wep;
76233548Sjkh	struct dirent *dp;
76333548Sjkh	int chksum;
76433747Sache	int table_loaded;
76533747Sache	u_int16_t *u2w;
76633548Sjkh{
76733548Sjkh	u_int8_t *cp;
76833548Sjkh	u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
76933744Sache	u_int16_t code;
77033548Sjkh	int i;
77133548Sjkh
77233548Sjkh	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
77333548Sjkh	    || !(wep->weCnt&WIN_CNT))
77433548Sjkh		return -1;
77533548Sjkh
77633548Sjkh	/*
77733548Sjkh	 * First compare checksums
77833548Sjkh	 */
77933548Sjkh	if (wep->weCnt&WIN_LAST) {
78033548Sjkh		chksum = wep->weChksum;
78133548Sjkh		/*
78233548Sjkh		 * This works even though d_namlen is one byte!
78333548Sjkh		 */
78433548Sjkh		dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
78533548Sjkh	} else if (chksum != wep->weChksum)
78633548Sjkh		chksum = -1;
78733548Sjkh	if (chksum == -1)
78833548Sjkh		return -1;
78933548Sjkh
79033548Sjkh	/*
79133548Sjkh	 * Offset of this entry
79233548Sjkh	 */
79333548Sjkh	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
79433548Sjkh	np = (u_int8_t *)dp->d_name + i;
79533548Sjkh
79633548Sjkh	/*
79733548Sjkh	 * Convert the name parts
79833548Sjkh	 */
79933548Sjkh	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
80033744Sache		code = (cp[1] << 8) | cp[0];
80133744Sache		switch (code) {
80233548Sjkh		case 0:
80333744Sache			*np = '\0';
80433548Sjkh			dp->d_namlen -= sizeof(wep->wePart2)/2
80533548Sjkh			    + sizeof(wep->wePart3)/2 + i + 1;
80633548Sjkh			return chksum;
80733548Sjkh		case '/':
80833744Sache			*np = '\0';
80933548Sjkh			return -1;
81033744Sache		default:
81133747Sache			if (code & 0xff80) {
81233747Sache				if (table_loaded)
81333747Sache					code = find_lcode(code, u2w);
81433747Sache				else if (code & 0xff00)
81533745Sache					code = '?';
81633744Sache			}
81733744Sache			*np++ = code;
81833744Sache			break;
81933548Sjkh		}
82033548Sjkh		/*
82133548Sjkh		 * The size comparison should result in the compiler
82233548Sjkh		 * optimizing the whole if away
82333548Sjkh		 */
82433548Sjkh		if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
82533548Sjkh		    && np > ep) {
82633548Sjkh			np[-1] = 0;
82733548Sjkh			return -1;
82833548Sjkh		}
82933744Sache		cp += 2;
83033548Sjkh	}
83133548Sjkh	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
83233744Sache		code = (cp[1] << 8) | cp[0];
83333744Sache		switch (code) {
83433548Sjkh		case 0:
83533744Sache			*np = '\0';
83633548Sjkh			dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
83733548Sjkh			return chksum;
83833548Sjkh		case '/':
83933744Sache			*np = '\0';
84033548Sjkh			return -1;
84133744Sache		default:
84233747Sache			if (code & 0xff80) {
84333747Sache				if (table_loaded)
84433747Sache					code = find_lcode(code, u2w);
84533747Sache				else if (code & 0xff00)
84633745Sache					code = '?';
84733744Sache			}
84833744Sache			*np++ = code;
84933744Sache			break;
85033548Sjkh		}
85133548Sjkh		/*
85233548Sjkh		 * The size comparisons should be optimized away
85333548Sjkh		 */
85433548Sjkh		if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
85533548Sjkh		    && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
85633548Sjkh		    && np > ep) {
85733548Sjkh			np[-1] = 0;
85833548Sjkh			return -1;
85933548Sjkh		}
86033744Sache		cp += 2;
86133548Sjkh	}
86233548Sjkh	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
86333744Sache		code = (cp[1] << 8) | cp[0];
86433744Sache		switch (code) {
86533548Sjkh		case 0:
86633744Sache			*np = '\0';
86733548Sjkh			dp->d_namlen -= i + 1;
86833548Sjkh			return chksum;
86933548Sjkh		case '/':
87033744Sache			*np = '\0';
87133548Sjkh			return -1;
87233744Sache		default:
87333747Sache			if (code & 0xff80) {
87433747Sache				if (table_loaded)
87533747Sache					code = find_lcode(code, u2w);
87633747Sache				else if (code & 0xff00)
87733745Sache					code = '?';
87833744Sache			}
87933744Sache			*np++ = code;
88033744Sache			break;
88133548Sjkh		}
88233548Sjkh		/*
88333548Sjkh		 * See above
88433548Sjkh		 */
88533548Sjkh		if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
88633548Sjkh		    && np > ep) {
88733548Sjkh			np[-1] = 0;
88833548Sjkh			return -1;
88933548Sjkh		}
89033744Sache		cp += 2;
89133548Sjkh	}
89233548Sjkh	return chksum;
89333548Sjkh}
89433548Sjkh
89533548Sjkh/*
89633548Sjkh * Compute the checksum of a DOS filename for Win95 use
89733548Sjkh */
89833548Sjkhu_int8_t
89933548SjkhwinChksum(name)
90033548Sjkh	u_int8_t *name;
90133548Sjkh{
90233548Sjkh	int i;
90333548Sjkh	u_int8_t s;
90433548Sjkh
90533548Sjkh	for (s = 0, i = 11; --i >= 0; s += *name++)
90633548Sjkh		s = (s << 7)|(s >> 1);
90733548Sjkh	return s;
90833548Sjkh}
90933548Sjkh
91033548Sjkh/*
91133548Sjkh * Determine the number of slots necessary for Win95 names
91233548Sjkh */
91333548Sjkhint
91433548SjkhwinSlotCnt(un, unlen)
91533548Sjkh	const u_char *un;
91633548Sjkh	int unlen;
91733548Sjkh{
91833548Sjkh	for (un += unlen; unlen > 0; unlen--)
91933548Sjkh		if (*--un != ' ' && *un != '.')
92033548Sjkh			break;
92133548Sjkh	if (unlen > WIN_MAXLEN)
92233548Sjkh		return 0;
92333548Sjkh	return howmany(unlen, WIN_CHARS);
92433548Sjkh}
925