msdosfs_conv.c revision 33791
133791Sache/*	$Id: msdosfs_conv.c,v 1.22 1998/02/23 16:44:27 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
34133768Sachestatic u_char
34233768Sachel2u[256] = {
34333768Sache	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
34433768Sache	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
34533768Sache	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
34633768Sache	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
34733768Sache	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
34833768Sache	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
34933768Sache	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
35033768Sache	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
35133768Sache	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
35233768Sache	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
35333768Sache	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
35433768Sache	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
35533768Sache	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
35633768Sache	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
35733768Sache	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
35833768Sache	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
35933768Sache	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
36033768Sache	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
36133768Sache	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
36233768Sache	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
36333768Sache	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
36433768Sache	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
36533768Sache	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
36633768Sache	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
36733768Sache	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
36833768Sache	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
36933768Sache	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
37033768Sache	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
37133768Sache	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
37233768Sache	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
37333768Sache	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
37433768Sache	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
37533768Sache};
37633768Sache
3772893Sdfr/*
3782893Sdfr * DOS filenames are made of 2 parts, the name part and the extension part.
3792893Sdfr * The name part is 8 characters long and the extension part is 3
3802893Sdfr * characters long.  They may contain trailing blanks if the name or
3812893Sdfr * extension are not long enough to fill their respective fields.
3822893Sdfr */
3832893Sdfr
3842893Sdfr/*
3852893Sdfr * Convert a DOS filename to a unix filename. And, return the number of
3862893Sdfr * characters in the resulting unix filename excluding the terminating
3872893Sdfr * null.
3882893Sdfr */
3892893Sdfrint
39033791Sachedos2unixfn(dn, un, lower, d2u_loaded, d2u, ul_loaded, ul)
3912893Sdfr	u_char dn[11];
3922893Sdfr	u_char *un;
39333791Sache	int lower;
39433768Sache	int d2u_loaded;
39533768Sache	u_int8_t *d2u;
39633768Sache	int ul_loaded;
39733760Sache	u_int8_t *ul;
3982893Sdfr{
3992893Sdfr	int i;
40033548Sjkh	int thislong = 1;
4012893Sdfr	u_char c;
4022893Sdfr
4032893Sdfr	/*
40433548Sjkh	 * If first char of the filename is SLOT_E5 (0x05), then the real
40533548Sjkh	 * first char of the filename should be 0xe5. But, they couldn't
40633548Sjkh	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
40733548Sjkh	 * directory slot. Another dos quirk.
4082893Sdfr	 */
40933548Sjkh	if (*dn == SLOT_E5)
41033768Sache		c = d2u_loaded ? d2u[0xe5 & 0x7f] : dos2unix[0xe5];
41133548Sjkh	else
41233768Sache		c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
41333768Sache		    dos2unix[*dn];
41433791Sache	*un++ = lower ? (ul_loaded && (c & 0x80) ?
41533791Sache			 ul[c & 0x7f] : u2l[c]) : c;
41633548Sjkh	dn++;
4172893Sdfr
4182893Sdfr	/*
41933548Sjkh	 * Copy the name portion into the unix filename string.
4202893Sdfr	 */
42133548Sjkh	for (i = 1; i < 8 && *dn != ' '; i++) {
42233768Sache		c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
42333768Sache		    dos2unix[*dn];
42433768Sache		dn++;
42533791Sache		*un++ = lower ? (ul_loaded && (c & 0x80) ?
42633791Sache				 ul[c & 0x7f] : u2l[c]) : c;
4272893Sdfr		thislong++;
4282893Sdfr	}
42933548Sjkh	dn += 8 - i;
4302893Sdfr
4312893Sdfr	/*
4322893Sdfr	 * Now, if there is an extension then put in a period and copy in
4332893Sdfr	 * the extension.
4342893Sdfr	 */
43533548Sjkh	if (*dn != ' ') {
4362893Sdfr		*un++ = '.';
4372893Sdfr		thislong++;
43833548Sjkh		for (i = 0; i < 3 && *dn != ' '; i++) {
43933768Sache			c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
44033768Sache			    dos2unix[*dn];
44133768Sache			dn++;
44233791Sache			*un++ = lower ? (ul_loaded && (c & 0x80) ?
44333791Sache					 ul[c & 0x7f] : u2l[c]) : c;
4442893Sdfr			thislong++;
4452893Sdfr		}
4462893Sdfr	}
4472893Sdfr	*un++ = 0;
4482893Sdfr
44933548Sjkh	return (thislong);
4502893Sdfr}
4512893Sdfr
4522893Sdfr/*
45333548Sjkh * Convert a unix filename to a DOS filename according to Win95 rules.
45433548Sjkh * If applicable and gen is not 0, it is inserted into the converted
45533548Sjkh * filename as a generation number.
45633548Sjkh * Returns
45733548Sjkh *	0 if name couldn't be converted
45833548Sjkh *	1 if the converted name is the same as the original
45933548Sjkh *	  (no long filename entry necessary for Win95)
46033548Sjkh *	2 if conversion was successful
46133548Sjkh *	3 if conversion was successful and generation number was inserted
4622893Sdfr */
46333548Sjkhint
46433768Sacheunix2dosfn(un, dn, unlen, gen, u2d_loaded, u2d, lu_loaded, lu)
46533548Sjkh	const u_char *un;
46633548Sjkh	u_char dn[12];
4672893Sdfr	int unlen;
46833548Sjkh	u_int gen;
46933768Sache	int u2d_loaded;
47033768Sache	u_int8_t *u2d;
47133768Sache	int lu_loaded;
47233768Sache	u_int8_t *lu;
4732893Sdfr{
47433548Sjkh	int i, j, l;
47533548Sjkh	int conv = 1;
47633548Sjkh	const u_char *cp, *dp, *dp1;
47733548Sjkh	u_char gentext[6], *wcp;
47833768Sache	u_int8_t c;
4792893Sdfr
4802893Sdfr	/*
4812893Sdfr	 * Fill the dos filename string with blanks. These are DOS's pad
4822893Sdfr	 * characters.
4832893Sdfr	 */
48433548Sjkh	for (i = 0; i < 11; i++)
4852893Sdfr		dn[i] = ' ';
48633548Sjkh	dn[11] = 0;
4872893Sdfr
4882893Sdfr	/*
4892893Sdfr	 * The filenames "." and ".." are handled specially, since they
4902893Sdfr	 * don't follow dos filename rules.
4912893Sdfr	 */
4922893Sdfr	if (un[0] == '.' && unlen == 1) {
4932893Sdfr		dn[0] = '.';
49433548Sjkh		return gen <= 1;
4952893Sdfr	}
4962893Sdfr	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
4972893Sdfr		dn[0] = '.';
4982893Sdfr		dn[1] = '.';
49933548Sjkh		return gen <= 1;
5002893Sdfr	}
5012893Sdfr
5022893Sdfr	/*
50333548Sjkh	 * Filenames with only blanks and dots are not allowed!
5042893Sdfr	 */
50533548Sjkh	for (cp = un, i = unlen; --i >= 0; cp++)
50633548Sjkh		if (*cp != ' ' && *cp != '.')
50733548Sjkh			break;
50833548Sjkh	if (i < 0)
50933548Sjkh		return 0;
51033548Sjkh
51133548Sjkh	/*
51233548Sjkh	 * Now find the extension
51333548Sjkh	 * Note: dot as first char doesn't start extension
51433548Sjkh	 *	 and trailing dots and blanks are ignored
51533548Sjkh	 */
51633548Sjkh	dp = dp1 = 0;
51733548Sjkh	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
51833548Sjkh		switch (*cp++) {
51933548Sjkh		case '.':
52033548Sjkh			if (!dp1)
52133548Sjkh				dp1 = cp;
52233548Sjkh			break;
52333548Sjkh		case ' ':
52433548Sjkh			break;
52533548Sjkh		default:
52633548Sjkh			if (dp1)
52733548Sjkh				dp = dp1;
52833548Sjkh			dp1 = 0;
52933548Sjkh			break;
53033548Sjkh		}
5312893Sdfr	}
5322893Sdfr
5332893Sdfr	/*
53433548Sjkh	 * Now convert it
5352893Sdfr	 */
53633548Sjkh	if (dp) {
53733548Sjkh		if (dp1)
53833548Sjkh			l = dp1 - dp;
53933548Sjkh		else
54033548Sjkh			l = unlen - (dp - un);
54133548Sjkh		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
54233768Sache			c = dp[i];
54333768Sache			c = lu_loaded && (c & 0x80) ?
54433768Sache			    lu[c & 0x7f] : l2u[c];
54533768Sache			c = u2d_loaded && (c & 0x80) ?
54633768Sache			    u2d[c & 0x7f] : unix2dos[c];
54733768Sache			if (dp[i] != (dn[j] = c)
54833548Sjkh			    && conv != 3)
54933548Sjkh				conv = 2;
55033548Sjkh			if (!dn[j]) {
55133548Sjkh				conv = 3;
55233548Sjkh				dn[j--] = ' ';
55333548Sjkh			}
55433548Sjkh		}
55533548Sjkh		if (i < l)
55633548Sjkh			conv = 3;
55733548Sjkh		dp--;
55833548Sjkh	} else {
55933548Sjkh		for (dp = cp; *--dp == ' ' || *dp == '.';);
56033548Sjkh		dp++;
56133548Sjkh	}
56233548Sjkh
56333548Sjkh	/*
56433548Sjkh	 * Now convert the rest of the name
56533548Sjkh	 */
56633548Sjkh	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
56733768Sache		c = lu_loaded && (*un & 0x80) ?
56833768Sache		    lu[*un & 0x7f] : l2u[*un];
56933768Sache		c = u2d_loaded && (c & 0x80) ?
57033768Sache		    u2d[c & 0x7f] : unix2dos[c];
57133768Sache		if (*un != (dn[j] = c)
57233548Sjkh		    && conv != 3)
57333548Sjkh			conv = 2;
57433548Sjkh		if (!dn[j]) {
57533548Sjkh			conv = 3;
57633548Sjkh			dn[j--] = ' ';
57733548Sjkh		}
57833548Sjkh	}
57933548Sjkh	if (un < dp)
58033548Sjkh		conv = 3;
58133548Sjkh	/*
58233548Sjkh	 * If we didn't have any chars in filename,
58333548Sjkh	 * generate a default
58433548Sjkh	 */
58533548Sjkh	if (!j)
58633548Sjkh		dn[0] = '_';
58733548Sjkh
58833548Sjkh	/*
58933548Sjkh	 * The first character cannot be E5,
59033548Sjkh	 * because that means a deleted entry
59133548Sjkh	 */
59233548Sjkh	if (dn[0] == 0xe5)
5932893Sdfr		dn[0] = SLOT_E5;
5942893Sdfr
5952893Sdfr	/*
59633548Sjkh	 * If there wasn't any char dropped,
59733548Sjkh	 * there is no place for generation numbers
5982893Sdfr	 */
59933548Sjkh	if (conv != 3) {
60033548Sjkh		if (gen > 1)
60133548Sjkh			return 0;
60233548Sjkh		return conv;
6032893Sdfr	}
6042893Sdfr
6052893Sdfr	/*
60633548Sjkh	 * Now insert the generation number into the filename part
6072893Sdfr	 */
60833548Sjkh	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
60933548Sjkh		*--wcp = gen % 10 + '0';
61033548Sjkh	if (gen)
61133548Sjkh		return 0;
61233548Sjkh	for (i = 8; dn[--i] == ' ';);
61333548Sjkh	i++;
61433548Sjkh	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
61533548Sjkh		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
61633548Sjkh	dn[i++] = '~';
61733548Sjkh	while (wcp < gentext + sizeof(gentext))
61833548Sjkh		dn[i++] = *wcp++;
61933548Sjkh	return 3;
62033548Sjkh}
62133548Sjkh
62233548Sjkh/*
62333548Sjkh * Create a Win95 long name directory entry
62433548Sjkh * Note: assumes that the filename is valid,
62533548Sjkh *	 i.e. doesn't consist solely of blanks and dots
62633548Sjkh */
62733548Sjkhint
62833768Sacheunix2winfn(un, unlen, wep, cnt, chksum, table_loaded, u2w)
62933548Sjkh	const u_char *un;
63033548Sjkh	int unlen;
63133548Sjkh	struct winentry *wep;
63233548Sjkh	int cnt;
63333548Sjkh	int chksum;
63433768Sache	int table_loaded;
63533747Sache	u_int16_t *u2w;
63633548Sjkh{
63733548Sjkh	const u_int8_t *cp;
63833548Sjkh	u_int8_t *wcp;
63933548Sjkh	int i;
64033747Sache	u_int16_t code;
64133548Sjkh
64233548Sjkh	/*
64333548Sjkh	 * Drop trailing blanks and dots
64433548Sjkh	 */
64533548Sjkh	for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
64633548Sjkh
64733548Sjkh	un += (cnt - 1) * WIN_CHARS;
64833548Sjkh	unlen -= (cnt - 1) * WIN_CHARS;
64933548Sjkh
65033548Sjkh	/*
65133548Sjkh	 * Initialize winentry to some useful default
65233548Sjkh	 */
65333548Sjkh	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
65433548Sjkh	wep->weCnt = cnt;
65533548Sjkh	wep->weAttributes = ATTR_WIN95;
65633548Sjkh	wep->weReserved1 = 0;
65733548Sjkh	wep->weChksum = chksum;
65833548Sjkh	wep->weReserved2 = 0;
65933548Sjkh
66033548Sjkh	/*
66133548Sjkh	 * Now convert the filename parts
66233548Sjkh	 */
66333548Sjkh	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
66433548Sjkh		if (--unlen < 0)
66533548Sjkh			goto done;
66633747Sache		if (table_loaded && (*un & 0x80)) {
66733747Sache			code = u2w[*un++ & 0x7f];
66833747Sache			*wcp++ = code;
66933747Sache			*wcp++ = code >> 8;
67033747Sache		} else {
67133747Sache			*wcp++ = *un++;
67233747Sache			*wcp++ = 0;
67333747Sache		}
6742893Sdfr	}
67533548Sjkh	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
67633548Sjkh		if (--unlen < 0)
67733548Sjkh			goto done;
67833747Sache		if (table_loaded && (*un & 0x80)) {
67933747Sache			code = u2w[*un++ & 0x7f];
68033747Sache			*wcp++ = code;
68133747Sache			*wcp++ = code >> 8;
68233747Sache		} else {
68333747Sache			*wcp++ = *un++;
68433747Sache			*wcp++ = 0;
68533747Sache		}
68633548Sjkh	}
68733548Sjkh	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
68833548Sjkh		if (--unlen < 0)
68933548Sjkh			goto done;
69033747Sache		if (table_loaded && (*un & 0x80)) {
69133747Sache			code = u2w[*un++ & 0x7f];
69233747Sache			*wcp++ = code;
69333747Sache			*wcp++ = code >> 8;
69433747Sache		} else {
69533747Sache			*wcp++ = *un++;
69633747Sache			*wcp++ = 0;
69733747Sache		}
69833548Sjkh	}
69933548Sjkh	if (!unlen)
70033548Sjkh		wep->weCnt |= WIN_LAST;
70133548Sjkh	return unlen;
70233548Sjkh
70333548Sjkhdone:
70433548Sjkh	*wcp++ = 0;
70533548Sjkh	*wcp++ = 0;
70633548Sjkh	wep->weCnt |= WIN_LAST;
70733548Sjkh	return 0;
7082893Sdfr}
7092893Sdfr
71033750Sachestatic inline u_int8_t
71133750Sachefind_lcode(code, u2w)
71233750Sache	u_int16_t code;
71333750Sache	u_int16_t *u2w;
71433750Sache{
71533750Sache	int i;
71633750Sache
71733750Sache	for (i = 0; i < 128; i++)
71833750Sache		if (u2w[i] == code)
71933750Sache			return (i | 0x80);
72033750Sache	return '?';
72133750Sache}
72233750Sache
7232893Sdfr/*
72433548Sjkh * Compare our filename to the one in the Win95 entry
72533548Sjkh * Returns the checksum or -1 if no match
7262893Sdfr */
72733548Sjkhint
72833791SachewinChkName(un, unlen, wep, chksum, u2w_loaded, u2w, ul_loaded, ul)
72933548Sjkh	const u_char *un;
73033548Sjkh	int unlen;
73133548Sjkh	struct winentry *wep;
73233548Sjkh	int chksum;
73333768Sache	int u2w_loaded;
73433750Sache	u_int16_t *u2w;
73533791Sache	int ul_loaded;
73633791Sache	u_int8_t *ul;
73733548Sjkh{
73833548Sjkh	u_int8_t *cp;
73933548Sjkh	int i;
74033750Sache	u_int16_t code;
74133760Sache	u_int8_t c1, c2;
74233548Sjkh
74333548Sjkh	/*
74433548Sjkh	 * First compare checksums
74533548Sjkh	 */
74633548Sjkh	if (wep->weCnt&WIN_LAST)
74733548Sjkh		chksum = wep->weChksum;
74833548Sjkh	else if (chksum != wep->weChksum)
74933548Sjkh		chksum = -1;
75033548Sjkh	if (chksum == -1)
75133548Sjkh		return -1;
75233548Sjkh
75333548Sjkh	/*
75433548Sjkh	 * Offset of this entry
75533548Sjkh	 */
75633548Sjkh	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
75733548Sjkh	un += i;
75833548Sjkh	if ((unlen -= i) <= 0)
75933548Sjkh		return -1;
76033548Sjkh	if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
76133548Sjkh		return -1;
76233548Sjkh
76333548Sjkh	/*
76433548Sjkh	 * Compare the name parts
76533548Sjkh	 */
76633548Sjkh	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
76733548Sjkh		if (--unlen < 0) {
76833548Sjkh			if (!*cp++ && !*cp)
76933548Sjkh				return chksum;
77033548Sjkh			return -1;
77133548Sjkh		}
77233750Sache		code = (cp[1] << 8) | cp[0];
77333750Sache		if (code & 0xff80) {
77433760Sache			if (u2w_loaded)
77533750Sache				code = find_lcode(code, u2w);
77633750Sache			else if (code & 0xff00)
77733750Sache				code = '?';
77833750Sache		}
77933791Sache		c1 = ul_loaded && (code & 0x80) ?
78033791Sache		     ul[code & 0x7f] : u2l[code];
78133791Sache		c2 = ul_loaded && (*un & 0x80) ?
78233791Sache		     ul[*un & 0x7f] : u2l[*un];
78333760Sache		if (c1 != c2)
78433548Sjkh			return -1;
78533750Sache		cp += 2;
78633760Sache		un++;
78733548Sjkh	}
78833548Sjkh	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
78933548Sjkh		if (--unlen < 0) {
79033548Sjkh			if (!*cp++ && !*cp)
79133548Sjkh				return chksum;
79233548Sjkh			return -1;
79333548Sjkh		}
79433750Sache		code = (cp[1] << 8) | cp[0];
79533750Sache		if (code & 0xff80) {
79633760Sache			if (u2w_loaded)
79733750Sache				code = find_lcode(code, u2w);
79833750Sache			else if (code & 0xff00)
79933750Sache				code = '?';
80033750Sache		}
80133791Sache		c1 = ul_loaded && (code & 0x80) ?
80233791Sache		     ul[code & 0x7f] : u2l[code];
80333791Sache		c2 = ul_loaded && (*un & 0x80) ?
80433791Sache		     ul[*un & 0x7f] : u2l[*un];
80533760Sache		if (c1 != c2)
80633548Sjkh			return -1;
80733750Sache		cp += 2;
80833760Sache		un++;
80933548Sjkh	}
81033548Sjkh	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
81133548Sjkh		if (--unlen < 0) {
81233548Sjkh			if (!*cp++ && !*cp)
81333548Sjkh				return chksum;
81433548Sjkh			return -1;
81533548Sjkh		}
81633750Sache		code = (cp[1] << 8) | cp[0];
81733750Sache		if (code & 0xff80) {
81833760Sache			if (u2w_loaded)
81933750Sache				code = find_lcode(code, u2w);
82033750Sache			else if (code & 0xff00)
82133750Sache				code = '?';
82233750Sache		}
82333791Sache		c1 = ul_loaded && (code & 0x80) ?
82433791Sache		     ul[code & 0x7f] : u2l[code];
82533791Sache		c2 = ul_loaded && (*un & 0x80) ?
82633791Sache		     ul[*un & 0x7f] : u2l[*un];
82733760Sache		if (c1 != c2)
82833548Sjkh			return -1;
82933750Sache		cp += 2;
83033760Sache		un++;
83133548Sjkh	}
83233548Sjkh	return chksum;
83333548Sjkh}
83433548Sjkh
83533548Sjkh/*
83633548Sjkh * Convert Win95 filename to dirbuf.
83733548Sjkh * Returns the checksum or -1 if impossible
83833548Sjkh */
83933548Sjkhint
84033768Sachewin2unixfn(wep, dp, chksum, table_loaded, u2w)
84133548Sjkh	struct winentry *wep;
84233548Sjkh	struct dirent *dp;
84333548Sjkh	int chksum;
84433768Sache	int table_loaded;
84533747Sache	u_int16_t *u2w;
84633548Sjkh{
84733548Sjkh	u_int8_t *cp;
84833548Sjkh	u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
84933744Sache	u_int16_t code;
85033548Sjkh	int i;
85133548Sjkh
85233548Sjkh	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
85333548Sjkh	    || !(wep->weCnt&WIN_CNT))
85433548Sjkh		return -1;
85533548Sjkh
85633548Sjkh	/*
85733548Sjkh	 * First compare checksums
85833548Sjkh	 */
85933548Sjkh	if (wep->weCnt&WIN_LAST) {
86033548Sjkh		chksum = wep->weChksum;
86133548Sjkh		/*
86233548Sjkh		 * This works even though d_namlen is one byte!
86333548Sjkh		 */
86433548Sjkh		dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
86533548Sjkh	} else if (chksum != wep->weChksum)
86633548Sjkh		chksum = -1;
86733548Sjkh	if (chksum == -1)
86833548Sjkh		return -1;
86933548Sjkh
87033548Sjkh	/*
87133548Sjkh	 * Offset of this entry
87233548Sjkh	 */
87333548Sjkh	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
87433548Sjkh	np = (u_int8_t *)dp->d_name + i;
87533548Sjkh
87633548Sjkh	/*
87733548Sjkh	 * Convert the name parts
87833548Sjkh	 */
87933548Sjkh	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
88033744Sache		code = (cp[1] << 8) | cp[0];
88133744Sache		switch (code) {
88233548Sjkh		case 0:
88333744Sache			*np = '\0';
88433548Sjkh			dp->d_namlen -= sizeof(wep->wePart2)/2
88533548Sjkh			    + sizeof(wep->wePart3)/2 + i + 1;
88633548Sjkh			return chksum;
88733548Sjkh		case '/':
88833744Sache			*np = '\0';
88933548Sjkh			return -1;
89033744Sache		default:
89133747Sache			if (code & 0xff80) {
89233747Sache				if (table_loaded)
89333747Sache					code = find_lcode(code, u2w);
89433747Sache				else if (code & 0xff00)
89533745Sache					code = '?';
89633744Sache			}
89733744Sache			*np++ = code;
89833744Sache			break;
89933548Sjkh		}
90033548Sjkh		/*
90133548Sjkh		 * The size comparison should result in the compiler
90233548Sjkh		 * optimizing the whole if away
90333548Sjkh		 */
90433548Sjkh		if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
90533548Sjkh		    && np > ep) {
90633548Sjkh			np[-1] = 0;
90733548Sjkh			return -1;
90833548Sjkh		}
90933744Sache		cp += 2;
91033548Sjkh	}
91133548Sjkh	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
91233744Sache		code = (cp[1] << 8) | cp[0];
91333744Sache		switch (code) {
91433548Sjkh		case 0:
91533744Sache			*np = '\0';
91633548Sjkh			dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
91733548Sjkh			return chksum;
91833548Sjkh		case '/':
91933744Sache			*np = '\0';
92033548Sjkh			return -1;
92133744Sache		default:
92233747Sache			if (code & 0xff80) {
92333747Sache				if (table_loaded)
92433747Sache					code = find_lcode(code, u2w);
92533747Sache				else if (code & 0xff00)
92633745Sache					code = '?';
92733744Sache			}
92833744Sache			*np++ = code;
92933744Sache			break;
93033548Sjkh		}
93133548Sjkh		/*
93233548Sjkh		 * The size comparisons should be optimized away
93333548Sjkh		 */
93433548Sjkh		if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
93533548Sjkh		    && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
93633548Sjkh		    && np > ep) {
93733548Sjkh			np[-1] = 0;
93833548Sjkh			return -1;
93933548Sjkh		}
94033744Sache		cp += 2;
94133548Sjkh	}
94233548Sjkh	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
94333744Sache		code = (cp[1] << 8) | cp[0];
94433744Sache		switch (code) {
94533548Sjkh		case 0:
94633744Sache			*np = '\0';
94733548Sjkh			dp->d_namlen -= i + 1;
94833548Sjkh			return chksum;
94933548Sjkh		case '/':
95033744Sache			*np = '\0';
95133548Sjkh			return -1;
95233744Sache		default:
95333747Sache			if (code & 0xff80) {
95433747Sache				if (table_loaded)
95533747Sache					code = find_lcode(code, u2w);
95633747Sache				else if (code & 0xff00)
95733745Sache					code = '?';
95833744Sache			}
95933744Sache			*np++ = code;
96033744Sache			break;
96133548Sjkh		}
96233548Sjkh		/*
96333548Sjkh		 * See above
96433548Sjkh		 */
96533548Sjkh		if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
96633548Sjkh		    && np > ep) {
96733548Sjkh			np[-1] = 0;
96833548Sjkh			return -1;
96933548Sjkh		}
97033744Sache		cp += 2;
97133548Sjkh	}
97233548Sjkh	return chksum;
97333548Sjkh}
97433548Sjkh
97533548Sjkh/*
97633548Sjkh * Compute the checksum of a DOS filename for Win95 use
97733548Sjkh */
97833548Sjkhu_int8_t
97933548SjkhwinChksum(name)
98033548Sjkh	u_int8_t *name;
98133548Sjkh{
98233548Sjkh	int i;
98333548Sjkh	u_int8_t s;
98433548Sjkh
98533548Sjkh	for (s = 0, i = 11; --i >= 0; s += *name++)
98633548Sjkh		s = (s << 7)|(s >> 1);
98733548Sjkh	return s;
98833548Sjkh}
98933548Sjkh
99033548Sjkh/*
99133548Sjkh * Determine the number of slots necessary for Win95 names
99233548Sjkh */
99333548Sjkhint
99433548SjkhwinSlotCnt(un, unlen)
99533548Sjkh	const u_char *un;
99633548Sjkh	int unlen;
99733548Sjkh{
99833548Sjkh	for (un += unlen; unlen > 0; unlen--)
99933548Sjkh		if (*--un != ' ' && *un != '.')
100033548Sjkh			break;
100133548Sjkh	if (unlen > WIN_MAXLEN)
100233548Sjkh		return 0;
100333548Sjkh	return howmany(unlen, WIN_CHARS);
100433548Sjkh}
1005