msdosfs_conv.c revision 33768
1/*	$Id: msdosfs_conv.c,v 1.21 1998/02/23 09:39:24 ache Exp $ */
2/*	$NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $	*/
3
4/*-
5 * Copyright (C) 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 *    derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51/*
52 * System include files.
53 */
54#include <sys/param.h>
55#include <sys/time.h>
56#include <sys/kernel.h>		/* defines tz */
57#include <sys/systm.h>
58#include <machine/clock.h>
59#include <sys/dirent.h>
60
61/*
62 * MSDOSFS include files.
63 */
64#include <msdosfs/direntry.h>
65
66/*
67 * Total number of days that have passed for each month in a regular year.
68 */
69static u_short regyear[] = {
70	31, 59, 90, 120, 151, 181,
71	212, 243, 273, 304, 334, 365
72};
73
74/*
75 * Total number of days that have passed for each month in a leap year.
76 */
77static u_short leapyear[] = {
78	31, 60, 91, 121, 152, 182,
79	213, 244, 274, 305, 335, 366
80};
81
82/*
83 * Variables used to remember parts of the last time conversion.  Maybe we
84 * can avoid a full conversion.
85 */
86static u_long  lasttime;
87static u_long  lastday;
88static u_short lastddate;
89static u_short lastdtime;
90
91static inline u_int8_t find_lcode __P((u_int16_t code, u_int16_t *u2w));
92
93/*
94 * Convert the unix version of time to dos's idea of time to be used in
95 * file timestamps. The passed in unix time is assumed to be in GMT.
96 */
97void
98unix2dostime(tsp, ddp, dtp, dhp)
99	struct timespec *tsp;
100	u_int16_t *ddp;
101	u_int16_t *dtp;
102	u_int8_t *dhp;
103{
104	u_long t;
105	u_long days;
106	u_long inc;
107	u_long year;
108	u_long month;
109	u_short *months;
110
111	/*
112	 * If the time from the last conversion is the same as now, then
113	 * skip the computations and use the saved result.
114	 */
115	t = tsp->tv_sec - (tz.tz_minuteswest * 60)
116	    - (wall_cmos_clock ? adjkerntz : 0);
117	    /* - daylight savings time correction */
118	t &= ~1;
119	if (lasttime != t) {
120		lasttime = t;
121		lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
122		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
123		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
124
125		/*
126		 * If the number of days since 1970 is the same as the last
127		 * time we did the computation then skip all this leap year
128		 * and month stuff.
129		 */
130		days = t / (24 * 60 * 60);
131		if (days != lastday) {
132			lastday = days;
133			for (year = 1970;; year++) {
134				inc = year & 0x03 ? 365 : 366;
135				if (days < inc)
136					break;
137				days -= inc;
138			}
139			months = year & 0x03 ? regyear : leapyear;
140			for (month = 0; days >= months[month]; month++)
141				;
142			if (month > 0)
143				days -= months[month - 1];
144			lastddate = ((days + 1) << DD_DAY_SHIFT)
145			    + ((month + 1) << DD_MONTH_SHIFT);
146			/*
147			 * Remember dos's idea of time is relative to 1980.
148			 * unix's is relative to 1970.  If somehow we get a
149			 * time before 1980 then don't give totally crazy
150			 * results.
151			 */
152			if (year > 1980)
153				lastddate += (year - 1980) << DD_YEAR_SHIFT;
154		}
155	}
156	if (dtp)
157		*dtp = lastdtime;
158	if (dhp)
159		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
160
161	*ddp = lastddate;
162}
163
164/*
165 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
166 * interval there were 8 regular years and 2 leap years.
167 */
168#define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
169
170static u_short lastdosdate;
171static u_long  lastseconds;
172
173/*
174 * Convert from dos' idea of time to unix'. This will probably only be
175 * called from the stat(), and fstat() system calls and so probably need
176 * not be too efficient.
177 */
178void
179dos2unixtime(dd, dt, dh, tsp)
180	u_int dd;
181	u_int dt;
182	u_int dh;
183	struct timespec *tsp;
184{
185	u_long seconds;
186	u_long month;
187	u_long year;
188	u_long days;
189	u_short *months;
190
191	if (dd == 0) {
192		/*
193		 * Uninitialized field, return the epoch.
194		 */
195		tsp->tv_sec = 0;
196		tsp->tv_nsec = 0;
197		return;
198	}
199	seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
200	    + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
201	    + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
202	    + dh / 100;
203	/*
204	 * If the year, month, and day from the last conversion are the
205	 * same then use the saved value.
206	 */
207	if (lastdosdate != dd) {
208		lastdosdate = dd;
209		days = 0;
210		year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
211		days = year * 365;
212		days += year / 4 + 1;	/* add in leap days */
213		if ((year & 0x03) == 0)
214			days--;		/* if year is a leap year */
215		months = year & 0x03 ? regyear : leapyear;
216		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
217		if (month < 1 || month > 12) {
218			printf("dos2unixtime(): month value out of range (%ld)\n",
219			    month);
220			month = 1;
221		}
222		if (month > 1)
223			days += months[month - 2];
224		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
225		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
226	}
227	tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
228	     + adjkerntz;
229	     /* + daylight savings time correction */
230	tsp->tv_nsec = (dh % 100) * 10000000;
231}
232
233static u_char
234unix2dos[256] = {
235	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
236	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
237	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
238	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
239	0,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
240	0x28, 0x29, 0,    0,    0,    0x2d, 0,    0,	/* 28-2f */
241	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
242	0x38, 0x39, 0,    0,    0,    0,    0,    0,	/* 38-3f */
243	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
244	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
245	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
246	0x58, 0x59, 0x5a, 0,    0,    0,    0x5e, 0x5f,	/* 58-5f */
247	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
248	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
249	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
250	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
251	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
252	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
253	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
254	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
255	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
256	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
257	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
258	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
259	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
260	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
261	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
262	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
263	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
264	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
265	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
266	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
267};
268
269static u_char
270dos2unix[256] = {
271	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
272	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
273	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
274	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
275	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
276	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
277	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
278	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
279	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
280	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
281	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
282	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
283	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
284	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
285	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
286	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
287	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
288	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
289	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
290	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
291	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
292	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
293	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
294	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
295	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
296	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
297	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
298	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
299	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
300	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
301	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
302	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
303};
304
305static u_char
306u2l[256] = {
307	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
308	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
309	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
310	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
311	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
312	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
313	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
314	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
315	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
316	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
317	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
318	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
319	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
320	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
321	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
322	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
323	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
324	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
325	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
326	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
327	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
328	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
329	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
330	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
331	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
332	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
333	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
334	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
335	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
336	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
337	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
338	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
339};
340
341static u_char
342l2u[256] = {
343	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
344	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
345	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
346	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
347	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
348	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
349	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
350	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
351	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
352	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
353	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
354	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
355	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
356	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
357	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
358	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
359	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
360	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
361	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
362	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
363	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
364	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
365	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
366	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
367	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
368	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
369	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
370	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
371	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
372	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
373	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
374	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
375};
376
377/*
378 * DOS filenames are made of 2 parts, the name part and the extension part.
379 * The name part is 8 characters long and the extension part is 3
380 * characters long.  They may contain trailing blanks if the name or
381 * extension are not long enough to fill their respective fields.
382 */
383
384/*
385 * Convert a DOS filename to a unix filename. And, return the number of
386 * characters in the resulting unix filename excluding the terminating
387 * null.
388 */
389int
390dos2unixfn(dn, un, d2u_loaded, d2u, ul_loaded, ul)
391	u_char dn[11];
392	u_char *un;
393	int d2u_loaded;
394	u_int8_t *d2u;
395	int ul_loaded;
396	u_int8_t *ul;
397{
398	int i;
399	int thislong = 1;
400	u_char c;
401
402	/*
403	 * If first char of the filename is SLOT_E5 (0x05), then the real
404	 * first char of the filename should be 0xe5. But, they couldn't
405	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
406	 * directory slot. Another dos quirk.
407	 */
408	if (*dn == SLOT_E5)
409		c = d2u_loaded ? d2u[0xe5 & 0x7f] : dos2unix[0xe5];
410	else
411		c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
412		    dos2unix[*dn];
413	*un++ = ul_loaded && (c & 0x80) ?
414		ul[c & 0x7f] : u2l[c];
415	dn++;
416
417	/*
418	 * Copy the name portion into the unix filename string.
419	 */
420	for (i = 1; i < 8 && *dn != ' '; i++) {
421		c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
422		    dos2unix[*dn];
423		dn++;
424		*un++ = ul_loaded && (c & 0x80) ?
425			ul[c & 0x7f] : u2l[c];
426		thislong++;
427	}
428	dn += 8 - i;
429
430	/*
431	 * Now, if there is an extension then put in a period and copy in
432	 * the extension.
433	 */
434	if (*dn != ' ') {
435		*un++ = '.';
436		thislong++;
437		for (i = 0; i < 3 && *dn != ' '; i++) {
438			c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
439			    dos2unix[*dn];
440			dn++;
441			*un++ = ul_loaded && (c & 0x80) ?
442				ul[c & 0x7f] : u2l[c];
443			thislong++;
444		}
445	}
446	*un++ = 0;
447
448	return (thislong);
449}
450
451/*
452 * Convert a unix filename to a DOS filename according to Win95 rules.
453 * If applicable and gen is not 0, it is inserted into the converted
454 * filename as a generation number.
455 * Returns
456 *	0 if name couldn't be converted
457 *	1 if the converted name is the same as the original
458 *	  (no long filename entry necessary for Win95)
459 *	2 if conversion was successful
460 *	3 if conversion was successful and generation number was inserted
461 */
462int
463unix2dosfn(un, dn, unlen, gen, u2d_loaded, u2d, lu_loaded, lu)
464	const u_char *un;
465	u_char dn[12];
466	int unlen;
467	u_int gen;
468	int u2d_loaded;
469	u_int8_t *u2d;
470	int lu_loaded;
471	u_int8_t *lu;
472{
473	int i, j, l;
474	int conv = 1;
475	const u_char *cp, *dp, *dp1;
476	u_char gentext[6], *wcp;
477	u_int8_t c;
478
479	/*
480	 * Fill the dos filename string with blanks. These are DOS's pad
481	 * characters.
482	 */
483	for (i = 0; i < 11; i++)
484		dn[i] = ' ';
485	dn[11] = 0;
486
487	/*
488	 * The filenames "." and ".." are handled specially, since they
489	 * don't follow dos filename rules.
490	 */
491	if (un[0] == '.' && unlen == 1) {
492		dn[0] = '.';
493		return gen <= 1;
494	}
495	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
496		dn[0] = '.';
497		dn[1] = '.';
498		return gen <= 1;
499	}
500
501	/*
502	 * Filenames with only blanks and dots are not allowed!
503	 */
504	for (cp = un, i = unlen; --i >= 0; cp++)
505		if (*cp != ' ' && *cp != '.')
506			break;
507	if (i < 0)
508		return 0;
509
510	/*
511	 * Now find the extension
512	 * Note: dot as first char doesn't start extension
513	 *	 and trailing dots and blanks are ignored
514	 */
515	dp = dp1 = 0;
516	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
517		switch (*cp++) {
518		case '.':
519			if (!dp1)
520				dp1 = cp;
521			break;
522		case ' ':
523			break;
524		default:
525			if (dp1)
526				dp = dp1;
527			dp1 = 0;
528			break;
529		}
530	}
531
532	/*
533	 * Now convert it
534	 */
535	if (dp) {
536		if (dp1)
537			l = dp1 - dp;
538		else
539			l = unlen - (dp - un);
540		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
541			c = dp[i];
542			c = lu_loaded && (c & 0x80) ?
543			    lu[c & 0x7f] : l2u[c];
544			c = u2d_loaded && (c & 0x80) ?
545			    u2d[c & 0x7f] : unix2dos[c];
546			if (dp[i] != (dn[j] = c)
547			    && conv != 3)
548				conv = 2;
549			if (!dn[j]) {
550				conv = 3;
551				dn[j--] = ' ';
552			}
553		}
554		if (i < l)
555			conv = 3;
556		dp--;
557	} else {
558		for (dp = cp; *--dp == ' ' || *dp == '.';);
559		dp++;
560	}
561
562	/*
563	 * Now convert the rest of the name
564	 */
565	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
566		c = lu_loaded && (*un & 0x80) ?
567		    lu[*un & 0x7f] : l2u[*un];
568		c = u2d_loaded && (c & 0x80) ?
569		    u2d[c & 0x7f] : unix2dos[c];
570		if (*un != (dn[j] = c)
571		    && conv != 3)
572			conv = 2;
573		if (!dn[j]) {
574			conv = 3;
575			dn[j--] = ' ';
576		}
577	}
578	if (un < dp)
579		conv = 3;
580	/*
581	 * If we didn't have any chars in filename,
582	 * generate a default
583	 */
584	if (!j)
585		dn[0] = '_';
586
587	/*
588	 * The first character cannot be E5,
589	 * because that means a deleted entry
590	 */
591	if (dn[0] == 0xe5)
592		dn[0] = SLOT_E5;
593
594	/*
595	 * If there wasn't any char dropped,
596	 * there is no place for generation numbers
597	 */
598	if (conv != 3) {
599		if (gen > 1)
600			return 0;
601		return conv;
602	}
603
604	/*
605	 * Now insert the generation number into the filename part
606	 */
607	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
608		*--wcp = gen % 10 + '0';
609	if (gen)
610		return 0;
611	for (i = 8; dn[--i] == ' ';);
612	i++;
613	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
614		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
615	dn[i++] = '~';
616	while (wcp < gentext + sizeof(gentext))
617		dn[i++] = *wcp++;
618	return 3;
619}
620
621/*
622 * Create a Win95 long name directory entry
623 * Note: assumes that the filename is valid,
624 *	 i.e. doesn't consist solely of blanks and dots
625 */
626int
627unix2winfn(un, unlen, wep, cnt, chksum, table_loaded, u2w)
628	const u_char *un;
629	int unlen;
630	struct winentry *wep;
631	int cnt;
632	int chksum;
633	int table_loaded;
634	u_int16_t *u2w;
635{
636	const u_int8_t *cp;
637	u_int8_t *wcp;
638	int i;
639	u_int16_t code;
640
641	/*
642	 * Drop trailing blanks and dots
643	 */
644	for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
645
646	un += (cnt - 1) * WIN_CHARS;
647	unlen -= (cnt - 1) * WIN_CHARS;
648
649	/*
650	 * Initialize winentry to some useful default
651	 */
652	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
653	wep->weCnt = cnt;
654	wep->weAttributes = ATTR_WIN95;
655	wep->weReserved1 = 0;
656	wep->weChksum = chksum;
657	wep->weReserved2 = 0;
658
659	/*
660	 * Now convert the filename parts
661	 */
662	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
663		if (--unlen < 0)
664			goto done;
665		if (table_loaded && (*un & 0x80)) {
666			code = u2w[*un++ & 0x7f];
667			*wcp++ = code;
668			*wcp++ = code >> 8;
669		} else {
670			*wcp++ = *un++;
671			*wcp++ = 0;
672		}
673	}
674	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
675		if (--unlen < 0)
676			goto done;
677		if (table_loaded && (*un & 0x80)) {
678			code = u2w[*un++ & 0x7f];
679			*wcp++ = code;
680			*wcp++ = code >> 8;
681		} else {
682			*wcp++ = *un++;
683			*wcp++ = 0;
684		}
685	}
686	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
687		if (--unlen < 0)
688			goto done;
689		if (table_loaded && (*un & 0x80)) {
690			code = u2w[*un++ & 0x7f];
691			*wcp++ = code;
692			*wcp++ = code >> 8;
693		} else {
694			*wcp++ = *un++;
695			*wcp++ = 0;
696		}
697	}
698	if (!unlen)
699		wep->weCnt |= WIN_LAST;
700	return unlen;
701
702done:
703	*wcp++ = 0;
704	*wcp++ = 0;
705	wep->weCnt |= WIN_LAST;
706	return 0;
707}
708
709static inline u_int8_t
710find_lcode(code, u2w)
711	u_int16_t code;
712	u_int16_t *u2w;
713{
714	int i;
715
716	for (i = 0; i < 128; i++)
717		if (u2w[i] == code)
718			return (i | 0x80);
719	return '?';
720}
721
722/*
723 * Compare our filename to the one in the Win95 entry
724 * Returns the checksum or -1 if no match
725 */
726int
727winChkName(un, unlen, wep, chksum, u2w_loaded, u2w, lu_loaded, lu)
728	const u_char *un;
729	int unlen;
730	struct winentry *wep;
731	int chksum;
732	int u2w_loaded;
733	u_int16_t *u2w;
734	int lu_loaded;
735	u_int8_t *lu;
736{
737	u_int8_t *cp;
738	int i;
739	u_int16_t code;
740	u_int8_t c1, c2;
741
742	/*
743	 * First compare checksums
744	 */
745	if (wep->weCnt&WIN_LAST)
746		chksum = wep->weChksum;
747	else if (chksum != wep->weChksum)
748		chksum = -1;
749	if (chksum == -1)
750		return -1;
751
752	/*
753	 * Offset of this entry
754	 */
755	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
756	un += i;
757	if ((unlen -= i) <= 0)
758		return -1;
759	if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
760		return -1;
761
762	/*
763	 * Compare the name parts
764	 */
765	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
766		if (--unlen < 0) {
767			if (!*cp++ && !*cp)
768				return chksum;
769			return -1;
770		}
771		code = (cp[1] << 8) | cp[0];
772		if (code & 0xff80) {
773			if (u2w_loaded)
774				code = find_lcode(code, u2w);
775			else if (code & 0xff00)
776				code = '?';
777		}
778		c1 = lu_loaded && (code & 0x80) ?
779		     lu[code & 0x7f] : l2u[code];
780		c2 = lu_loaded && (*un & 0x80) ?
781		     lu[*un & 0x7f] : l2u[*un];
782		if (c1 != c2)
783			return -1;
784		cp += 2;
785		un++;
786	}
787	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
788		if (--unlen < 0) {
789			if (!*cp++ && !*cp)
790				return chksum;
791			return -1;
792		}
793		code = (cp[1] << 8) | cp[0];
794		if (code & 0xff80) {
795			if (u2w_loaded)
796				code = find_lcode(code, u2w);
797			else if (code & 0xff00)
798				code = '?';
799		}
800		c1 = lu_loaded && (code & 0x80) ?
801		     lu[code & 0x7f] : l2u[code];
802		c2 = lu_loaded && (*un & 0x80) ?
803		     lu[*un & 0x7f] : l2u[*un];
804		if (c1 != c2)
805			return -1;
806		cp += 2;
807		un++;
808	}
809	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
810		if (--unlen < 0) {
811			if (!*cp++ && !*cp)
812				return chksum;
813			return -1;
814		}
815		code = (cp[1] << 8) | cp[0];
816		if (code & 0xff80) {
817			if (u2w_loaded)
818				code = find_lcode(code, u2w);
819			else if (code & 0xff00)
820				code = '?';
821		}
822		c1 = lu_loaded && (code & 0x80) ?
823		     lu[code & 0x7f] : l2u[code];
824		c2 = lu_loaded && (*un & 0x80) ?
825		     lu[*un & 0x7f] : l2u[*un];
826		if (c1 != c2)
827			return -1;
828		cp += 2;
829		un++;
830	}
831	return chksum;
832}
833
834/*
835 * Convert Win95 filename to dirbuf.
836 * Returns the checksum or -1 if impossible
837 */
838int
839win2unixfn(wep, dp, chksum, table_loaded, u2w)
840	struct winentry *wep;
841	struct dirent *dp;
842	int chksum;
843	int table_loaded;
844	u_int16_t *u2w;
845{
846	u_int8_t *cp;
847	u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
848	u_int16_t code;
849	int i;
850
851	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
852	    || !(wep->weCnt&WIN_CNT))
853		return -1;
854
855	/*
856	 * First compare checksums
857	 */
858	if (wep->weCnt&WIN_LAST) {
859		chksum = wep->weChksum;
860		/*
861		 * This works even though d_namlen is one byte!
862		 */
863		dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
864	} else if (chksum != wep->weChksum)
865		chksum = -1;
866	if (chksum == -1)
867		return -1;
868
869	/*
870	 * Offset of this entry
871	 */
872	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
873	np = (u_int8_t *)dp->d_name + i;
874
875	/*
876	 * Convert the name parts
877	 */
878	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
879		code = (cp[1] << 8) | cp[0];
880		switch (code) {
881		case 0:
882			*np = '\0';
883			dp->d_namlen -= sizeof(wep->wePart2)/2
884			    + sizeof(wep->wePart3)/2 + i + 1;
885			return chksum;
886		case '/':
887			*np = '\0';
888			return -1;
889		default:
890			if (code & 0xff80) {
891				if (table_loaded)
892					code = find_lcode(code, u2w);
893				else if (code & 0xff00)
894					code = '?';
895			}
896			*np++ = code;
897			break;
898		}
899		/*
900		 * The size comparison should result in the compiler
901		 * optimizing the whole if away
902		 */
903		if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
904		    && np > ep) {
905			np[-1] = 0;
906			return -1;
907		}
908		cp += 2;
909	}
910	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
911		code = (cp[1] << 8) | cp[0];
912		switch (code) {
913		case 0:
914			*np = '\0';
915			dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
916			return chksum;
917		case '/':
918			*np = '\0';
919			return -1;
920		default:
921			if (code & 0xff80) {
922				if (table_loaded)
923					code = find_lcode(code, u2w);
924				else if (code & 0xff00)
925					code = '?';
926			}
927			*np++ = code;
928			break;
929		}
930		/*
931		 * The size comparisons should be optimized away
932		 */
933		if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
934		    && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
935		    && np > ep) {
936			np[-1] = 0;
937			return -1;
938		}
939		cp += 2;
940	}
941	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
942		code = (cp[1] << 8) | cp[0];
943		switch (code) {
944		case 0:
945			*np = '\0';
946			dp->d_namlen -= i + 1;
947			return chksum;
948		case '/':
949			*np = '\0';
950			return -1;
951		default:
952			if (code & 0xff80) {
953				if (table_loaded)
954					code = find_lcode(code, u2w);
955				else if (code & 0xff00)
956					code = '?';
957			}
958			*np++ = code;
959			break;
960		}
961		/*
962		 * See above
963		 */
964		if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
965		    && np > ep) {
966			np[-1] = 0;
967			return -1;
968		}
969		cp += 2;
970	}
971	return chksum;
972}
973
974/*
975 * Compute the checksum of a DOS filename for Win95 use
976 */
977u_int8_t
978winChksum(name)
979	u_int8_t *name;
980{
981	int i;
982	u_int8_t s;
983
984	for (s = 0, i = 11; --i >= 0; s += *name++)
985		s = (s << 7)|(s >> 1);
986	return s;
987}
988
989/*
990 * Determine the number of slots necessary for Win95 names
991 */
992int
993winSlotCnt(un, unlen)
994	const u_char *un;
995	int unlen;
996{
997	for (un += unlen; unlen > 0; unlen--)
998		if (*--un != ' ' && *un != '.')
999			break;
1000	if (unlen > WIN_MAXLEN)
1001		return 0;
1002	return howmany(unlen, WIN_CHARS);
1003}
1004