msdosfs_conv.c revision 36133
136133Sdt/*	$Id: msdosfs_conv.c,v 1.26 1998/04/15 17:46:37 bde 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
9135210Sbdestatic __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
23333872Smsmith/*
23433872Smsmith * 0 - character disallowed in long file name.
23533872Smsmith * 1 - character should be replaced by '_' in DOS file name,
23633872Smsmith *     and generation number inserted.
23733872Smsmith * 2 - character ('.' and ' ') should be skipped in DOS file name,
23833872Smsmith *     and generation number inserted.
23933872Smsmith */
24033548Sjkhstatic u_char
24133548Sjkhunix2dos[256] = {
24233548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
24333548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
24433548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
24533548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
24633872Smsmith	2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
24733872Smsmith	0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,	/* 28-2f */
24833548Sjkh	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
24933872Smsmith	0x38, 0x39, 0,    1,    0,    1,    0,    0,	/* 38-3f */
25033548Sjkh	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
25133548Sjkh	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
25233548Sjkh	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
25333872Smsmith	0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f,	/* 58-5f */
25433548Sjkh	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
25533548Sjkh	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
25633548Sjkh	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
25733548Sjkh	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
25833548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
25933548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
26033548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
26133548Sjkh	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
26233548Sjkh	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
26333548Sjkh	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
26433548Sjkh	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
26533548Sjkh	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
26633548Sjkh	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
26733548Sjkh	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
26833548Sjkh	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
26933548Sjkh	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
27033548Sjkh	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
27133548Sjkh	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
27233548Sjkh	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
27333548Sjkh	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
27433548Sjkh};
2752893Sdfr
27633548Sjkhstatic u_char
27733548Sjkhdos2unix[256] = {
27833548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
27933548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
28033548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
28133548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
28233548Sjkh	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
28333548Sjkh	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
28433548Sjkh	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
28533548Sjkh	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
28633548Sjkh	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
28733548Sjkh	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
28833548Sjkh	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
28933548Sjkh	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
29033548Sjkh	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
29133548Sjkh	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
29233548Sjkh	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
29333548Sjkh	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
29433548Sjkh	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
29533548Sjkh	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
29633548Sjkh	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
29733548Sjkh	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
29833548Sjkh	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
29933548Sjkh	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
30033548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
30133548Sjkh	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
30233548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
30333548Sjkh	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
30433548Sjkh	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
30533548Sjkh	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
30633548Sjkh	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
30733548Sjkh	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
30833548Sjkh	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
30933548Sjkh	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
31033548Sjkh};
31133548Sjkh
31233548Sjkhstatic u_char
31333548Sjkhu2l[256] = {
31433548Sjkh	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
31533548Sjkh	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
31633548Sjkh	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
31733548Sjkh	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
31833548Sjkh	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
31933548Sjkh	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
32033548Sjkh	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
32133548Sjkh	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
32233548Sjkh	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
32333548Sjkh	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
32433548Sjkh	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
32533548Sjkh	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
32633548Sjkh	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
32733548Sjkh	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
32833548Sjkh	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
32933548Sjkh	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
33033548Sjkh	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
33133548Sjkh	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
33233548Sjkh	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
33333548Sjkh	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
33433548Sjkh	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
33533548Sjkh	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
33633548Sjkh	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
33733548Sjkh	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
33833548Sjkh	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
33933548Sjkh	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
34033548Sjkh	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
34133548Sjkh	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
34233548Sjkh	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
34333548Sjkh	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
34433548Sjkh	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
34533548Sjkh	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
34633548Sjkh};
34733548Sjkh
34833768Sachestatic u_char
34933768Sachel2u[256] = {
35033768Sache	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
35133768Sache	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
35233768Sache	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
35333768Sache	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
35433768Sache	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
35533768Sache	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
35633768Sache	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
35733768Sache	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
35833768Sache	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
35933768Sache	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
36033768Sache	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
36133768Sache	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
36233768Sache	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
36333768Sache	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
36433768Sache	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
36533768Sache	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
36633768Sache	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
36733768Sache	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
36833768Sache	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
36933768Sache	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
37033768Sache	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
37133768Sache	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
37233768Sache	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
37333768Sache	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
37433768Sache	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
37533768Sache	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
37633768Sache	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
37733768Sache	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
37833768Sache	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
37933768Sache	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
38033768Sache	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
38133768Sache	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
38233768Sache};
38333768Sache
3842893Sdfr/*
3852893Sdfr * DOS filenames are made of 2 parts, the name part and the extension part.
3862893Sdfr * The name part is 8 characters long and the extension part is 3
3872893Sdfr * characters long.  They may contain trailing blanks if the name or
3882893Sdfr * extension are not long enough to fill their respective fields.
3892893Sdfr */
3902893Sdfr
3912893Sdfr/*
3922893Sdfr * Convert a DOS filename to a unix filename. And, return the number of
3932893Sdfr * characters in the resulting unix filename excluding the terminating
3942893Sdfr * null.
3952893Sdfr */
3962893Sdfrint
39733791Sachedos2unixfn(dn, un, lower, d2u_loaded, d2u, ul_loaded, ul)
3982893Sdfr	u_char dn[11];
3992893Sdfr	u_char *un;
40033791Sache	int lower;
40133768Sache	int d2u_loaded;
40233768Sache	u_int8_t *d2u;
40333768Sache	int ul_loaded;
40433760Sache	u_int8_t *ul;
4052893Sdfr{
4062893Sdfr	int i;
40733548Sjkh	int thislong = 1;
4082893Sdfr	u_char c;
4092893Sdfr
4102893Sdfr	/*
41133548Sjkh	 * If first char of the filename is SLOT_E5 (0x05), then the real
41233548Sjkh	 * first char of the filename should be 0xe5. But, they couldn't
41333548Sjkh	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
41433548Sjkh	 * directory slot. Another dos quirk.
4152893Sdfr	 */
41633548Sjkh	if (*dn == SLOT_E5)
41733768Sache		c = d2u_loaded ? d2u[0xe5 & 0x7f] : dos2unix[0xe5];
41833548Sjkh	else
41933768Sache		c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
42033768Sache		    dos2unix[*dn];
42133791Sache	*un++ = lower ? (ul_loaded && (c & 0x80) ?
42233791Sache			 ul[c & 0x7f] : u2l[c]) : c;
42333548Sjkh	dn++;
4242893Sdfr
4252893Sdfr	/*
42633548Sjkh	 * Copy the name portion into the unix filename string.
4272893Sdfr	 */
42833548Sjkh	for (i = 1; i < 8 && *dn != ' '; i++) {
42933768Sache		c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
43033768Sache		    dos2unix[*dn];
43133768Sache		dn++;
43233791Sache		*un++ = lower ? (ul_loaded && (c & 0x80) ?
43333791Sache				 ul[c & 0x7f] : u2l[c]) : c;
4342893Sdfr		thislong++;
4352893Sdfr	}
43633548Sjkh	dn += 8 - i;
4372893Sdfr
4382893Sdfr	/*
4392893Sdfr	 * Now, if there is an extension then put in a period and copy in
4402893Sdfr	 * the extension.
4412893Sdfr	 */
44233548Sjkh	if (*dn != ' ') {
4432893Sdfr		*un++ = '.';
4442893Sdfr		thislong++;
44533548Sjkh		for (i = 0; i < 3 && *dn != ' '; i++) {
44633768Sache			c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
44733768Sache			    dos2unix[*dn];
44833768Sache			dn++;
44933791Sache			*un++ = lower ? (ul_loaded && (c & 0x80) ?
45033791Sache					 ul[c & 0x7f] : u2l[c]) : c;
4512893Sdfr			thislong++;
4522893Sdfr		}
4532893Sdfr	}
4542893Sdfr	*un++ = 0;
4552893Sdfr
45633548Sjkh	return (thislong);
4572893Sdfr}
4582893Sdfr
4592893Sdfr/*
46033548Sjkh * Convert a unix filename to a DOS filename according to Win95 rules.
46133548Sjkh * If applicable and gen is not 0, it is inserted into the converted
46233548Sjkh * filename as a generation number.
46333548Sjkh * Returns
46433548Sjkh *	0 if name couldn't be converted
46533548Sjkh *	1 if the converted name is the same as the original
46633548Sjkh *	  (no long filename entry necessary for Win95)
46733548Sjkh *	2 if conversion was successful
46833548Sjkh *	3 if conversion was successful and generation number was inserted
4692893Sdfr */
47033548Sjkhint
47133768Sacheunix2dosfn(un, dn, unlen, gen, u2d_loaded, u2d, lu_loaded, lu)
47233548Sjkh	const u_char *un;
47333548Sjkh	u_char dn[12];
4742893Sdfr	int unlen;
47533548Sjkh	u_int gen;
47633768Sache	int u2d_loaded;
47733768Sache	u_int8_t *u2d;
47833768Sache	int lu_loaded;
47933768Sache	u_int8_t *lu;
4802893Sdfr{
48133548Sjkh	int i, j, l;
48233548Sjkh	int conv = 1;
48333548Sjkh	const u_char *cp, *dp, *dp1;
48433548Sjkh	u_char gentext[6], *wcp;
48533768Sache	u_int8_t c;
48633872Smsmith#define U2D(c) (u2d_loaded && ((c) & 0x80) ? u2d[(c) & 0x7f] : unix2dos[c])
4872893Sdfr
4882893Sdfr	/*
4892893Sdfr	 * Fill the dos filename string with blanks. These are DOS's pad
4902893Sdfr	 * characters.
4912893Sdfr	 */
49233548Sjkh	for (i = 0; i < 11; i++)
4932893Sdfr		dn[i] = ' ';
49433548Sjkh	dn[11] = 0;
4952893Sdfr
4962893Sdfr	/*
4972893Sdfr	 * The filenames "." and ".." are handled specially, since they
4982893Sdfr	 * don't follow dos filename rules.
4992893Sdfr	 */
5002893Sdfr	if (un[0] == '.' && unlen == 1) {
5012893Sdfr		dn[0] = '.';
50233548Sjkh		return gen <= 1;
5032893Sdfr	}
5042893Sdfr	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
5052893Sdfr		dn[0] = '.';
5062893Sdfr		dn[1] = '.';
50733548Sjkh		return gen <= 1;
5082893Sdfr	}
5092893Sdfr
5102893Sdfr	/*
51133548Sjkh	 * Filenames with only blanks and dots are not allowed!
5122893Sdfr	 */
51333548Sjkh	for (cp = un, i = unlen; --i >= 0; cp++)
51433548Sjkh		if (*cp != ' ' && *cp != '.')
51533548Sjkh			break;
51633548Sjkh	if (i < 0)
51733548Sjkh		return 0;
51833548Sjkh
51933872Smsmith
52033548Sjkh	/*
52133872Smsmith	 * Filenames with some characters are not allowed!
52233872Smsmith	 */
52333872Smsmith	for (cp = un, i = unlen; --i >= 0; cp++)
52433872Smsmith		if (U2D(*cp) == 0)
52533872Smsmith			return 0;
52633872Smsmith
52733872Smsmith	/*
52833548Sjkh	 * Now find the extension
52933548Sjkh	 * Note: dot as first char doesn't start extension
53033548Sjkh	 *	 and trailing dots and blanks are ignored
53133548Sjkh	 */
53233548Sjkh	dp = dp1 = 0;
53333548Sjkh	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
53433548Sjkh		switch (*cp++) {
53533548Sjkh		case '.':
53633548Sjkh			if (!dp1)
53733548Sjkh				dp1 = cp;
53833548Sjkh			break;
53933548Sjkh		case ' ':
54033548Sjkh			break;
54133548Sjkh		default:
54233548Sjkh			if (dp1)
54333548Sjkh				dp = dp1;
54433548Sjkh			dp1 = 0;
54533548Sjkh			break;
54633548Sjkh		}
5472893Sdfr	}
5482893Sdfr
5492893Sdfr	/*
55033548Sjkh	 * Now convert it
5512893Sdfr	 */
55233548Sjkh	if (dp) {
55333548Sjkh		if (dp1)
55433548Sjkh			l = dp1 - dp;
55533548Sjkh		else
55633548Sjkh			l = unlen - (dp - un);
55733548Sjkh		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
55833768Sache			c = dp[i];
55933768Sache			c = lu_loaded && (c & 0x80) ?
56033768Sache			    lu[c & 0x7f] : l2u[c];
56133872Smsmith			c = U2D(c);
56233768Sache			if (dp[i] != (dn[j] = c)
56333548Sjkh			    && conv != 3)
56433548Sjkh				conv = 2;
56533872Smsmith			if (dn[j] == 1) {
56633548Sjkh				conv = 3;
56733872Smsmith				dn[j] = '_';
56833872Smsmith			}
56933872Smsmith			if (dn[j] == 2) {
57033872Smsmith				conv = 3;
57133548Sjkh				dn[j--] = ' ';
57233548Sjkh			}
57333548Sjkh		}
57433548Sjkh		if (i < l)
57533548Sjkh			conv = 3;
57633548Sjkh		dp--;
57733548Sjkh	} else {
57833548Sjkh		for (dp = cp; *--dp == ' ' || *dp == '.';);
57933548Sjkh		dp++;
58033548Sjkh	}
58133548Sjkh
58233548Sjkh	/*
58333548Sjkh	 * Now convert the rest of the name
58433548Sjkh	 */
58533548Sjkh	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
58633768Sache		c = lu_loaded && (*un & 0x80) ?
58733768Sache		    lu[*un & 0x7f] : l2u[*un];
58833872Smsmith		c = U2D(c);
58933768Sache		if (*un != (dn[j] = c)
59033548Sjkh		    && conv != 3)
59133548Sjkh			conv = 2;
59233872Smsmith		if (dn[j] == 1) {
59333548Sjkh			conv = 3;
59433872Smsmith			dn[j] = '_';
59533872Smsmith		}
59633872Smsmith		if (dn[j] == 2) {
59733872Smsmith			conv = 3;
59833548Sjkh			dn[j--] = ' ';
59933548Sjkh		}
60033548Sjkh	}
60133548Sjkh	if (un < dp)
60233548Sjkh		conv = 3;
60333548Sjkh	/*
60433548Sjkh	 * If we didn't have any chars in filename,
60533548Sjkh	 * generate a default
60633548Sjkh	 */
60733548Sjkh	if (!j)
60833548Sjkh		dn[0] = '_';
60933548Sjkh
61033548Sjkh	/*
61133548Sjkh	 * The first character cannot be E5,
61233548Sjkh	 * because that means a deleted entry
61333548Sjkh	 */
61433548Sjkh	if (dn[0] == 0xe5)
6152893Sdfr		dn[0] = SLOT_E5;
6162893Sdfr
6172893Sdfr	/*
61833548Sjkh	 * If there wasn't any char dropped,
61933548Sjkh	 * there is no place for generation numbers
6202893Sdfr	 */
62133548Sjkh	if (conv != 3) {
62233548Sjkh		if (gen > 1)
62333548Sjkh			return 0;
62433548Sjkh		return conv;
6252893Sdfr	}
6262893Sdfr
6272893Sdfr	/*
62833548Sjkh	 * Now insert the generation number into the filename part
6292893Sdfr	 */
63036133Sdt	if (gen == 0)
63136133Sdt		return conv;
63233548Sjkh	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
63333548Sjkh		*--wcp = gen % 10 + '0';
63433548Sjkh	if (gen)
63533548Sjkh		return 0;
63633548Sjkh	for (i = 8; dn[--i] == ' ';);
63733548Sjkh	i++;
63833548Sjkh	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
63933548Sjkh		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
64033548Sjkh	dn[i++] = '~';
64133548Sjkh	while (wcp < gentext + sizeof(gentext))
64233548Sjkh		dn[i++] = *wcp++;
64333548Sjkh	return 3;
64433872Smsmith#undef U2D
64533548Sjkh}
64633548Sjkh
64733548Sjkh/*
64833548Sjkh * Create a Win95 long name directory entry
64933548Sjkh * Note: assumes that the filename is valid,
65033548Sjkh *	 i.e. doesn't consist solely of blanks and dots
65133548Sjkh */
65233548Sjkhint
65333768Sacheunix2winfn(un, unlen, wep, cnt, chksum, table_loaded, u2w)
65433548Sjkh	const u_char *un;
65533548Sjkh	int unlen;
65633548Sjkh	struct winentry *wep;
65733548Sjkh	int cnt;
65833548Sjkh	int chksum;
65933768Sache	int table_loaded;
66033747Sache	u_int16_t *u2w;
66133548Sjkh{
66233548Sjkh	const u_int8_t *cp;
66333548Sjkh	u_int8_t *wcp;
66433548Sjkh	int i;
66533747Sache	u_int16_t code;
66633548Sjkh
66733548Sjkh	/*
66833548Sjkh	 * Drop trailing blanks and dots
66933548Sjkh	 */
67033548Sjkh	for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
67133548Sjkh
67233548Sjkh	un += (cnt - 1) * WIN_CHARS;
67333548Sjkh	unlen -= (cnt - 1) * WIN_CHARS;
67433548Sjkh
67533548Sjkh	/*
67633548Sjkh	 * Initialize winentry to some useful default
67733548Sjkh	 */
67833548Sjkh	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
67933548Sjkh	wep->weCnt = cnt;
68033548Sjkh	wep->weAttributes = ATTR_WIN95;
68133548Sjkh	wep->weReserved1 = 0;
68233548Sjkh	wep->weChksum = chksum;
68333548Sjkh	wep->weReserved2 = 0;
68433548Sjkh
68533548Sjkh	/*
68633548Sjkh	 * Now convert the filename parts
68733548Sjkh	 */
68833548Sjkh	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
68933548Sjkh		if (--unlen < 0)
69033548Sjkh			goto done;
69133747Sache		if (table_loaded && (*un & 0x80)) {
69233747Sache			code = u2w[*un++ & 0x7f];
69333747Sache			*wcp++ = code;
69433747Sache			*wcp++ = code >> 8;
69533747Sache		} else {
69633747Sache			*wcp++ = *un++;
69733747Sache			*wcp++ = 0;
69833747Sache		}
6992893Sdfr	}
70033548Sjkh	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
70133548Sjkh		if (--unlen < 0)
70233548Sjkh			goto done;
70333747Sache		if (table_loaded && (*un & 0x80)) {
70433747Sache			code = u2w[*un++ & 0x7f];
70533747Sache			*wcp++ = code;
70633747Sache			*wcp++ = code >> 8;
70733747Sache		} else {
70833747Sache			*wcp++ = *un++;
70933747Sache			*wcp++ = 0;
71033747Sache		}
71133548Sjkh	}
71233548Sjkh	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
71333548Sjkh		if (--unlen < 0)
71433548Sjkh			goto done;
71533747Sache		if (table_loaded && (*un & 0x80)) {
71633747Sache			code = u2w[*un++ & 0x7f];
71733747Sache			*wcp++ = code;
71833747Sache			*wcp++ = code >> 8;
71933747Sache		} else {
72033747Sache			*wcp++ = *un++;
72133747Sache			*wcp++ = 0;
72233747Sache		}
72333548Sjkh	}
72433548Sjkh	if (!unlen)
72533548Sjkh		wep->weCnt |= WIN_LAST;
72633548Sjkh	return unlen;
72733548Sjkh
72833548Sjkhdone:
72933548Sjkh	*wcp++ = 0;
73033548Sjkh	*wcp++ = 0;
73133548Sjkh	wep->weCnt |= WIN_LAST;
73233548Sjkh	return 0;
7332893Sdfr}
7342893Sdfr
73535210Sbdestatic __inline u_int8_t
73633750Sachefind_lcode(code, u2w)
73733750Sache	u_int16_t code;
73833750Sache	u_int16_t *u2w;
73933750Sache{
74033750Sache	int i;
74133750Sache
74233750Sache	for (i = 0; i < 128; i++)
74333750Sache		if (u2w[i] == code)
74433750Sache			return (i | 0x80);
74533750Sache	return '?';
74633750Sache}
74733750Sache
7482893Sdfr/*
74933548Sjkh * Compare our filename to the one in the Win95 entry
75033548Sjkh * Returns the checksum or -1 if no match
7512893Sdfr */
75233548Sjkhint
75333791SachewinChkName(un, unlen, wep, chksum, u2w_loaded, u2w, ul_loaded, ul)
75433548Sjkh	const u_char *un;
75533548Sjkh	int unlen;
75633548Sjkh	struct winentry *wep;
75733548Sjkh	int chksum;
75833768Sache	int u2w_loaded;
75933750Sache	u_int16_t *u2w;
76033791Sache	int ul_loaded;
76133791Sache	u_int8_t *ul;
76233548Sjkh{
76333548Sjkh	u_int8_t *cp;
76433548Sjkh	int i;
76533750Sache	u_int16_t code;
76633760Sache	u_int8_t c1, c2;
76733548Sjkh
76833548Sjkh	/*
76933548Sjkh	 * First compare checksums
77033548Sjkh	 */
77133548Sjkh	if (wep->weCnt&WIN_LAST)
77233548Sjkh		chksum = wep->weChksum;
77333548Sjkh	else if (chksum != wep->weChksum)
77433548Sjkh		chksum = -1;
77533548Sjkh	if (chksum == -1)
77633548Sjkh		return -1;
77733548Sjkh
77833548Sjkh	/*
77933548Sjkh	 * Offset of this entry
78033548Sjkh	 */
78133548Sjkh	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
78233548Sjkh	un += i;
78333548Sjkh	if ((unlen -= i) <= 0)
78433548Sjkh		return -1;
78533548Sjkh	if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
78633548Sjkh		return -1;
78733548Sjkh
78833548Sjkh	/*
78933548Sjkh	 * Compare the name parts
79033548Sjkh	 */
79133548Sjkh	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
79233548Sjkh		if (--unlen < 0) {
79333548Sjkh			if (!*cp++ && !*cp)
79433548Sjkh				return chksum;
79533548Sjkh			return -1;
79633548Sjkh		}
79733750Sache		code = (cp[1] << 8) | cp[0];
79833750Sache		if (code & 0xff80) {
79933760Sache			if (u2w_loaded)
80033750Sache				code = find_lcode(code, u2w);
80133750Sache			else if (code & 0xff00)
80233750Sache				code = '?';
80333750Sache		}
80433791Sache		c1 = ul_loaded && (code & 0x80) ?
80533791Sache		     ul[code & 0x7f] : u2l[code];
80633791Sache		c2 = ul_loaded && (*un & 0x80) ?
80733791Sache		     ul[*un & 0x7f] : u2l[*un];
80833760Sache		if (c1 != c2)
80933548Sjkh			return -1;
81033750Sache		cp += 2;
81133760Sache		un++;
81233548Sjkh	}
81333548Sjkh	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
81433548Sjkh		if (--unlen < 0) {
81533548Sjkh			if (!*cp++ && !*cp)
81633548Sjkh				return chksum;
81733548Sjkh			return -1;
81833548Sjkh		}
81933750Sache		code = (cp[1] << 8) | cp[0];
82033750Sache		if (code & 0xff80) {
82133760Sache			if (u2w_loaded)
82233750Sache				code = find_lcode(code, u2w);
82333750Sache			else if (code & 0xff00)
82433750Sache				code = '?';
82533750Sache		}
82633791Sache		c1 = ul_loaded && (code & 0x80) ?
82733791Sache		     ul[code & 0x7f] : u2l[code];
82833791Sache		c2 = ul_loaded && (*un & 0x80) ?
82933791Sache		     ul[*un & 0x7f] : u2l[*un];
83033760Sache		if (c1 != c2)
83133548Sjkh			return -1;
83233750Sache		cp += 2;
83333760Sache		un++;
83433548Sjkh	}
83533548Sjkh	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
83633548Sjkh		if (--unlen < 0) {
83733548Sjkh			if (!*cp++ && !*cp)
83833548Sjkh				return chksum;
83933548Sjkh			return -1;
84033548Sjkh		}
84133750Sache		code = (cp[1] << 8) | cp[0];
84233750Sache		if (code & 0xff80) {
84333760Sache			if (u2w_loaded)
84433750Sache				code = find_lcode(code, u2w);
84533750Sache			else if (code & 0xff00)
84633750Sache				code = '?';
84733750Sache		}
84833791Sache		c1 = ul_loaded && (code & 0x80) ?
84933791Sache		     ul[code & 0x7f] : u2l[code];
85033791Sache		c2 = ul_loaded && (*un & 0x80) ?
85133791Sache		     ul[*un & 0x7f] : u2l[*un];
85233760Sache		if (c1 != c2)
85333548Sjkh			return -1;
85433750Sache		cp += 2;
85533760Sache		un++;
85633548Sjkh	}
85733548Sjkh	return chksum;
85833548Sjkh}
85933548Sjkh
86033548Sjkh/*
86133548Sjkh * Convert Win95 filename to dirbuf.
86233548Sjkh * Returns the checksum or -1 if impossible
86333548Sjkh */
86433548Sjkhint
86533768Sachewin2unixfn(wep, dp, chksum, table_loaded, u2w)
86633548Sjkh	struct winentry *wep;
86733548Sjkh	struct dirent *dp;
86833548Sjkh	int chksum;
86933768Sache	int table_loaded;
87033747Sache	u_int16_t *u2w;
87133548Sjkh{
87233548Sjkh	u_int8_t *cp;
87333548Sjkh	u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
87433744Sache	u_int16_t code;
87533548Sjkh	int i;
87633548Sjkh
87733548Sjkh	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
87833548Sjkh	    || !(wep->weCnt&WIN_CNT))
87933548Sjkh		return -1;
88033548Sjkh
88133548Sjkh	/*
88233548Sjkh	 * First compare checksums
88333548Sjkh	 */
88433548Sjkh	if (wep->weCnt&WIN_LAST) {
88533548Sjkh		chksum = wep->weChksum;
88633548Sjkh		/*
88733548Sjkh		 * This works even though d_namlen is one byte!
88833548Sjkh		 */
88933548Sjkh		dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
89033548Sjkh	} else if (chksum != wep->weChksum)
89133548Sjkh		chksum = -1;
89233548Sjkh	if (chksum == -1)
89333548Sjkh		return -1;
89433548Sjkh
89533548Sjkh	/*
89633548Sjkh	 * Offset of this entry
89733548Sjkh	 */
89833548Sjkh	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
89933548Sjkh	np = (u_int8_t *)dp->d_name + i;
90033548Sjkh
90133548Sjkh	/*
90233548Sjkh	 * Convert the name parts
90333548Sjkh	 */
90433548Sjkh	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
90533744Sache		code = (cp[1] << 8) | cp[0];
90633744Sache		switch (code) {
90733548Sjkh		case 0:
90833744Sache			*np = '\0';
90933548Sjkh			dp->d_namlen -= sizeof(wep->wePart2)/2
91033548Sjkh			    + sizeof(wep->wePart3)/2 + i + 1;
91133548Sjkh			return chksum;
91233548Sjkh		case '/':
91333744Sache			*np = '\0';
91433548Sjkh			return -1;
91533744Sache		default:
91633747Sache			if (code & 0xff80) {
91733747Sache				if (table_loaded)
91833747Sache					code = find_lcode(code, u2w);
91933747Sache				else if (code & 0xff00)
92033745Sache					code = '?';
92133744Sache			}
92233744Sache			*np++ = code;
92333744Sache			break;
92433548Sjkh		}
92533548Sjkh		/*
92633548Sjkh		 * The size comparison should result in the compiler
92733548Sjkh		 * optimizing the whole if away
92833548Sjkh		 */
92933548Sjkh		if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
93033548Sjkh		    && np > ep) {
93133548Sjkh			np[-1] = 0;
93233548Sjkh			return -1;
93333548Sjkh		}
93433744Sache		cp += 2;
93533548Sjkh	}
93633548Sjkh	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
93733744Sache		code = (cp[1] << 8) | cp[0];
93833744Sache		switch (code) {
93933548Sjkh		case 0:
94033744Sache			*np = '\0';
94133548Sjkh			dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
94233548Sjkh			return chksum;
94333548Sjkh		case '/':
94433744Sache			*np = '\0';
94533548Sjkh			return -1;
94633744Sache		default:
94733747Sache			if (code & 0xff80) {
94833747Sache				if (table_loaded)
94933747Sache					code = find_lcode(code, u2w);
95033747Sache				else if (code & 0xff00)
95133745Sache					code = '?';
95233744Sache			}
95333744Sache			*np++ = code;
95433744Sache			break;
95533548Sjkh		}
95633548Sjkh		/*
95733548Sjkh		 * The size comparisons should be optimized away
95833548Sjkh		 */
95933548Sjkh		if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
96033548Sjkh		    && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
96133548Sjkh		    && np > ep) {
96233548Sjkh			np[-1] = 0;
96333548Sjkh			return -1;
96433548Sjkh		}
96533744Sache		cp += 2;
96633548Sjkh	}
96733548Sjkh	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
96833744Sache		code = (cp[1] << 8) | cp[0];
96933744Sache		switch (code) {
97033548Sjkh		case 0:
97133744Sache			*np = '\0';
97233548Sjkh			dp->d_namlen -= i + 1;
97333548Sjkh			return chksum;
97433548Sjkh		case '/':
97533744Sache			*np = '\0';
97633548Sjkh			return -1;
97733744Sache		default:
97833747Sache			if (code & 0xff80) {
97933747Sache				if (table_loaded)
98033747Sache					code = find_lcode(code, u2w);
98133747Sache				else if (code & 0xff00)
98233745Sache					code = '?';
98333744Sache			}
98433744Sache			*np++ = code;
98533744Sache			break;
98633548Sjkh		}
98733548Sjkh		/*
98833548Sjkh		 * See above
98933548Sjkh		 */
99033548Sjkh		if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
99133548Sjkh		    && np > ep) {
99233548Sjkh			np[-1] = 0;
99333548Sjkh			return -1;
99433548Sjkh		}
99533744Sache		cp += 2;
99633548Sjkh	}
99733548Sjkh	return chksum;
99833548Sjkh}
99933548Sjkh
100033548Sjkh/*
100133548Sjkh * Compute the checksum of a DOS filename for Win95 use
100233548Sjkh */
100333548Sjkhu_int8_t
100433548SjkhwinChksum(name)
100533548Sjkh	u_int8_t *name;
100633548Sjkh{
100733548Sjkh	int i;
100833548Sjkh	u_int8_t s;
100933548Sjkh
101033548Sjkh	for (s = 0, i = 11; --i >= 0; s += *name++)
101133548Sjkh		s = (s << 7)|(s >> 1);
101233548Sjkh	return s;
101333548Sjkh}
101433548Sjkh
101533548Sjkh/*
101633548Sjkh * Determine the number of slots necessary for Win95 names
101733548Sjkh */
101833548Sjkhint
101933548SjkhwinSlotCnt(un, unlen)
102033548Sjkh	const u_char *un;
102133548Sjkh	int unlen;
102233548Sjkh{
102333848Smsmith	unlen = winLenFixup(un, unlen);
102433548Sjkh	if (unlen > WIN_MAXLEN)
102533548Sjkh		return 0;
102633548Sjkh	return howmany(unlen, WIN_CHARS);
102733548Sjkh}
102833848Smsmith
102933848Smsmith/*
103033848Smsmith * Determine the number of bytes neccesary for Win95 names
103133848Smsmith */
103233848Smsmithint
103333848SmsmithwinLenFixup(un, unlen)
103433848Smsmith	const u_char* un;
103533848Smsmith	int unlen;
103633848Smsmith{
103733848Smsmith	for (un += unlen; unlen > 0; unlen--)
103833848Smsmith		if (*--un != ' ' && *un != '.')
103933848Smsmith			break;
104033848Smsmith	return unlen;
104133848Smsmith}
1042