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