1 /**	$MirOS: src/sys/msdosfs/msdosfs_conv.c,v 1.2 2005/03/06 21:28:12 tg Exp $ */
2 /*	$OpenBSD: msdosfs_conv.c,v 1.13 2004/05/14 04:05:05 tedu Exp $	*/
3 /*	$NetBSD: msdosfs_conv.c,v 1.24 1997/10/17 11:23:54 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/systm.h>
57 #include <sys/time.h>
58 #include <sys/kernel.h>		/* defines tz */
59 #include <sys/dirent.h>
60 #include <sys/vnode.h>
61 
62 /*
63  * MSDOSFS include files.
64  */
65 #include <msdosfs/direntry.h>
66 #include <msdosfs/denode.h>
67 
68 /*
69  * Days in each month in a regular year.
70  */
71 const u_short regyear[] = {
72 	31, 28, 31, 30, 31, 30,
73 	31, 31, 30, 31, 30, 31
74 };
75 
76 /*
77  * Days in each month in a leap year.
78  */
79 const u_short leapyear[] = {
80 	31, 29, 31, 30, 31, 30,
81 	31, 31, 30, 31, 30, 31
82 };
83 
84 /*
85  * Variables used to remember parts of the last time conversion.  Maybe we
86  * can avoid a full conversion.
87  */
88 uint32_t lasttime;
89 uint32_t lastday;
90 u_short lastddate;
91 u_short lastdtime;
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  */
97 void
unix2dostime(tsp,ddp,dtp,dhp)98 unix2dostime(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 	uint32_t t;
105 	uint32_t days;
106 	uint32_t inc;
107 	uint32_t year;
108 	uint32_t month;
109 	const 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 	     /* +- daylight saving time correction */ ;
117 	t &= ~1;
118 	if (lasttime != t) {
119 		lasttime = t;
120 		lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
121 		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
122 		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
123 
124 		/*
125 		 * If the number of days since 1970 is the same as the last
126 		 * time we did the computation then skip all this leap year
127 		 * and month stuff.
128 		 */
129 		days = t / (24 * 60 * 60);
130 		if (days != lastday) {
131 			lastday = days;
132 			for (year = 1970;; year++) {
133 				inc = year & 0x03 ? 365 : 366;
134 				if (days < inc)
135 					break;
136 				days -= inc;
137 			}
138 			months = year & 0x03 ? regyear : leapyear;
139 			for (month = 0; month < 12; month++) {
140 				if (days < months[month])
141 					break;
142 				days -= months[month];
143 			}
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 
157 	if (dtp != NULL)
158 		*dtp = lastdtime;
159 	if (dhp != NULL)
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 u_short lastdosdate;
172 uint32_t 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
dos2unixtime(dd,dt,dh,tsp)180 dos2unixtime(dd, dt, dh, tsp)
181 	u_int dd;
182 	u_int dt;
183 	u_int dh;
184 	struct timespec *tsp;
185 {
186 	uint32_t seconds;
187 	uint32_t m, month;
188 	uint32_t y, year;
189 	uint32_t days;
190 	const 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) * 2
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 		for (y = 0; y < year; y++)
213 			days += y & 0x03 ? 365 : 366;
214 		months = year & 0x03 ? regyear : leapyear;
215 		/*
216 		 * Prevent going from 0 to 0xffffffff in the following
217 		 * loop.
218 		 */
219 		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
220 		if (month == 0) {
221 			printf("dos2unixtime(): month value out of range (%ld)\n",
222 			    (long)month);
223 			month = 1;
224 		}
225 		for (m = 0; m < month - 1; m++)
226 			days += months[m];
227 		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
228 		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
229 	}
230 	tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
231 	     /* -+ daylight saving time correction */ ;
232 	tsp->tv_nsec = (dh % 100) * 10000000;
233 }
234 
235 static const u_char
236 unix2dos[256] = {
237 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
238 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
239 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
240 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
241 	0,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
242 	0x28, 0x29, 0,    0,    0,    0x2d, 0,    0,	/* 28-2f */
243 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
244 	0x38, 0x39, 0,    0,    0,    0,    0,    0,	/* 38-3f */
245 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
246 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
247 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
248 	0x58, 0x59, 0x5a, 0,    0,    0,    0x5e, 0x5f,	/* 58-5f */
249 	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
250 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
251 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
252 	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
253 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
254 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
255 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
256 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
257 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
258 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
259 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
260 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
261 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
262 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
263 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
264 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
265 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
266 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
267 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
268 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
269 };
270 
271 static const u_char
272 dos2unix[256] = {
273 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
274 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
275 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
276 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
277 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
278 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
279 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
280 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
281 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
282 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
283 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
284 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
285 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
286 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
287 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
288 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
289 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
290 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
291 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
292 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
293 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
294 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
295 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
296 	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
297 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
298 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
299 	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
300 	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
301 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
302 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
303 	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
304 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
305 };
306 
307 static const u_char
308 u2l[256] = {
309 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
310 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
311 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
312 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
313 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
314 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
315 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
316 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
317 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
318 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
319 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
320 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
321 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
322 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
323 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
324 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
325 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
326 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
327 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
328 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
329 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
330 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
331 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
332 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
333 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
334 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
335 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
336 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
337 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
338 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
339 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
340 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
341 };
342 
343 /*
344  * DOS filenames are made of 2 parts, the name part and the extension part.
345  * The name part is 8 characters long and the extension part is 3
346  * characters long.  They may contain trailing blanks if the name or
347  * extension are not long enough to fill their respective fields.
348  */
349 
350 /*
351  * Convert a DOS filename to a unix filename. And, return the number of
352  * characters in the resulting unix filename excluding the terminating
353  * null.
354  */
355 int
dos2unixfn(dn,un,lower)356 dos2unixfn(dn, un, lower)
357 	u_char dn[11];
358 	u_char *un;
359 	int lower;
360 {
361 	int i;
362 	int thislong = 1;
363 	u_char c;
364 
365 	/*
366 	 * If first char of the filename is SLOT_E5 (0x05), then the real
367 	 * first char of the filename should be 0xe5. But, they couldn't
368 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
369 	 * directory slot. Another dos quirk.
370 	 */
371 	if (*dn == SLOT_E5)
372 		c = dos2unix[0xe5];
373 	else
374 		c = dos2unix[*dn];
375 	*un++ = lower ? u2l[c] : c;
376 	dn++;
377 
378 	/*
379 	 * Copy the name portion into the unix filename string.
380 	 */
381 	for (i = 1; i < 8 && *dn != ' '; i++) {
382 		c = dos2unix[*dn++];
383 		*un++ = lower ? u2l[c] : c;
384 		thislong++;
385 	}
386 	dn += 8 - i;
387 
388 	/*
389 	 * Now, if there is an extension then put in a period and copy in
390 	 * the extension.
391 	 */
392 	if (*dn != ' ') {
393 		*un++ = '.';
394 		thislong++;
395 		for (i = 0; i < 3 && *dn != ' '; i++) {
396 			c = dos2unix[*dn++];
397 			*un++ = lower ? u2l[c] : c;
398 			thislong++;
399 		}
400 	}
401 	*un++ = 0;
402 
403 	return (thislong);
404 }
405 
406 /*
407  * Convert a unix filename to a DOS filename according to Win95 rules.
408  * If applicable and gen is not 0, it is inserted into the converted
409  * filename as a generation number.
410  * Returns
411  *	0 if name couldn't be converted
412  *	1 if the converted name is the same as the original
413  *	  (no long filename entry necessary for Win95)
414  *	2 if conversion was successful
415  *	3 if conversion was successful and generation number was inserted
416  */
417 int
unix2dosfn(un,dn,unlen,gen)418 unix2dosfn(un, dn, unlen, gen)
419 	u_char *un;
420 	u_char dn[12];
421 	int unlen;
422 	u_int gen;
423 {
424 	int i, j, l;
425 	int conv = 1;
426 	u_char *cp, *dp, *dp1;
427 	u_char gentext[6];
428 
429 	/*
430 	 * Fill the dos filename string with blanks. These are DOS's pad
431 	 * characters.
432 	 */
433 	for (i = 0; i < 11; i++)
434 		dn[i] = ' ';
435 	dn[11] = 0;
436 
437 	/*
438 	 * The filenames "." and ".." are handled specially, since they
439 	 * don't follow dos filename rules.
440 	 */
441 	if (un[0] == '.' && unlen == 1) {
442 		dn[0] = '.';
443 		return gen <= 1;
444 	}
445 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
446 		dn[0] = '.';
447 		dn[1] = '.';
448 		return gen <= 1;
449 	}
450 
451 	/*
452 	 * Filenames with only blanks and dots are not allowed!
453 	 */
454 	for (cp = un, i = unlen; --i >= 0; cp++)
455 		if (*cp != ' ' && *cp != '.')
456 			break;
457 	if (i < 0)
458 		return 0;
459 
460 	/*
461 	 * Now find the extension
462 	 * Note: dot as first char doesn't start extension
463 	 *	 and trailing dots and blanks are ignored
464 	 */
465 	dp = dp1 = 0;
466 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
467 		switch (*cp++) {
468 		case '.':
469 			if (!dp1)
470 				dp1 = cp;
471 			break;
472 		case ' ':
473 			break;
474 		default:
475 			if (dp1)
476 				dp = dp1;
477 			dp1 = 0;
478 			break;
479 		}
480 	}
481 
482 	/*
483 	 * Now convert it
484 	 */
485 	if (dp) {
486 		if (dp1)
487 			l = dp1 - dp;
488 		else
489 			l = unlen - (dp - un);
490 		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
491 			if (dp[i] != (dn[j] = unix2dos[dp[i]])
492 			    && conv != 3)
493 				conv = 2;
494 			if (!dn[j]) {
495 				conv = 3;
496 				dn[j--] = ' ';
497 			}
498 		}
499 		if (i < l)
500 			conv = 3;
501 		dp--;
502 	} else {
503 		for (dp = cp; *--dp == ' ' || *dp == '.';);
504 		dp++;
505 	}
506 
507 	/*
508 	 * Now convert the rest of the name
509 	 */
510 	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
511 		if (*un != (dn[j] = unix2dos[*un])
512 		    && conv != 3)
513 			conv = 2;
514 		if (!dn[j]) {
515 			conv = 3;
516 			dn[j--] = ' ';
517 		}
518 	}
519 	if (un < dp)
520 		conv = 3;
521 	/*
522 	 * If we didn't have any chars in filename,
523 	 * generate a default
524 	 */
525 	if (!j)
526 		dn[0] = '_';
527 
528 	/*
529 	 * The first character cannot be E5,
530 	 * because that means a deleted entry
531 	 */
532 	if (dn[0] == 0xe5)
533 		dn[0] = SLOT_E5;
534 
535 	/*
536 	 * If there wasn't any char dropped,
537 	 * there is no place for generation numbers
538 	 */
539 	if (conv != 3) {
540 		if (gen > 1)
541 			return 0;
542 		return conv;
543 	}
544 
545 	/*
546 	 * Now insert the generation number into the filename part
547 	 */
548 	for (cp = gentext + sizeof(gentext); cp > gentext && gen; gen /= 10)
549 		*--cp = gen % 10 + '0';
550 	if (gen)
551 		return 0;
552 	for (i = 8; dn[--i] == ' ';);
553 	if (gentext + sizeof(gentext) - cp + 1 > 8 - i)
554 		i = 8 - (gentext + sizeof(gentext) - cp + 1);
555 	dn[i++] = '~';
556 	while (cp < gentext + sizeof(gentext))
557 		dn[i++] = *cp++;
558 	return 3;
559 }
560 
561 /*
562  * Create a Win95 long name directory entry
563  * Note: assumes that the filename is valid,
564  *	 i.e. doesn't consist solely of blanks and dots
565  */
566 int
unix2winfn(un,unlen,wep,cnt,chksum)567 unix2winfn(un, unlen, wep, cnt, chksum)
568 	u_char *un;
569 	int unlen;
570 	struct winentry *wep;
571 	int cnt;
572 	int chksum;
573 {
574 	u_int8_t *cp;
575 	int i;
576 
577 	/*
578 	 * Drop trailing blanks and dots
579 	 */
580 	for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
581 
582 	un += (cnt - 1) * WIN_CHARS;
583 	unlen -= (cnt - 1) * WIN_CHARS;
584 
585 	/*
586 	 * Initialize winentry to some useful default
587 	 */
588 	for (cp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *cp++ = 0xff);
589 	wep->weCnt = cnt;
590 	wep->weAttributes = ATTR_WIN95;
591 	wep->weReserved1 = 0;
592 	wep->weChksum = chksum;
593 	wep->weReserved2 = 0;
594 
595 	/*
596 	 * Now convert the filename parts
597 	 */
598 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
599 		if (--unlen < 0)
600 			goto done;
601 		*cp++ = *un++;
602 		*cp++ = 0;
603 	}
604 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
605 		if (--unlen < 0)
606 			goto done;
607 		*cp++ = *un++;
608 		*cp++ = 0;
609 	}
610 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
611 		if (--unlen < 0)
612 			goto done;
613 		*cp++ = *un++;
614 		*cp++ = 0;
615 	}
616 	if (!unlen)
617 		wep->weCnt |= WIN_LAST;
618 	return unlen;
619 
620 done:
621 	*cp++ = 0;
622 	*cp++ = 0;
623 	wep->weCnt |= WIN_LAST;
624 	return 0;
625 }
626 
627 /*
628  * Compare our filename to the one in the Win95 entry
629  * Returns the checksum or -1 if no match
630  */
631 int
winChkName(un,unlen,wep,chksum)632 winChkName(un, unlen, wep, chksum)
633 	u_char *un;
634 	int unlen;
635 	struct winentry *wep;
636 	int chksum;
637 {
638 	u_int8_t *cp;
639 	int i;
640 
641 	/*
642 	 * First compare checksums
643 	 */
644 	if (wep->weCnt&WIN_LAST)
645 		chksum = wep->weChksum;
646 	else if (chksum != wep->weChksum)
647 		chksum = -1;
648 	if (chksum == -1)
649 		return -1;
650 
651 	/*
652 	 * Offset of this entry
653 	 */
654 	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
655 	if ((unlen -= i) <= 0)
656 		return -1;
657 	un += i;
658 
659 	if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
660 		return -1;
661 
662 	/*
663 	 * Compare the name parts
664 	 */
665 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
666 		if (--unlen < 0) {
667 			if (!*cp++ && !*cp)
668 				return chksum;
669 			return -1;
670 		}
671 		if (u2l[*cp++] != u2l[*un++] || *cp++)
672 			return -1;
673 	}
674 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
675 		if (--unlen < 0) {
676 			if (!*cp++ && !*cp)
677 				return chksum;
678 			return -1;
679 		}
680 		if (u2l[*cp++] != u2l[*un++] || *cp++)
681 			return -1;
682 	}
683 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
684 		if (--unlen < 0) {
685 			if (!*cp++ && !*cp)
686 				return chksum;
687 			return -1;
688 		}
689 		if (u2l[*cp++] != u2l[*un++] || *cp++)
690 			return -1;
691 	}
692 	return chksum;
693 }
694 
695 /*
696  * Convert Win95 filename to dirbuf.
697  * Returns the checksum or -1 if impossible
698  */
699 int
win2unixfn(wep,dp,chksum)700 win2unixfn(wep, dp, chksum)
701 	struct winentry *wep;
702 	struct dirent *dp;
703 	int chksum;
704 {
705 	u_int8_t *cp;
706 	u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
707 	int i;
708 
709 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
710 	    || !(wep->weCnt&WIN_CNT))
711 		return -1;
712 
713 	/*
714 	 * First compare checksums
715 	 */
716 	if (wep->weCnt&WIN_LAST) {
717 		chksum = wep->weChksum;
718 		/*
719 		 * This works even though d_namlen is one byte!
720 		 */
721 		dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
722 	} else if (chksum != wep->weChksum)
723 		chksum = -1;
724 	if (chksum == -1)
725 		return -1;
726 
727 	/*
728 	 * Offset of this entry
729 	 */
730 	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
731 	np = (u_int8_t *)dp->d_name + i;
732 
733 	/*
734 	 * Convert the name parts
735 	 */
736 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
737 		switch (*np++ = *cp++) {
738 		case 0:
739 			dp->d_namlen -= sizeof(wep->wePart2)/2
740 			    + sizeof(wep->wePart3)/2 + i + 1;
741 			return chksum;
742 		case '/':
743 			np[-1] = 0;
744 			return -1;
745 		}
746 		/*
747 		 * The size comparison should result in the compiler
748 		 * optimizing the whole if away
749 		 */
750 		if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
751 		    && np > ep) {
752 			np[-1] = 0;
753 			return -1;
754 		}
755 		if (*cp++)
756 			return -1;
757 	}
758 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
759 		switch (*np++ = *cp++) {
760 		case 0:
761 			dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
762 			return chksum;
763 		case '/':
764 			np[-1] = 0;
765 			return -1;
766 		}
767 		/*
768 		 * The size comparisons should be optimized away
769 		 */
770 		if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
771 		    && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
772 		    && np > ep) {
773 			np[-1] = 0;
774 			return -1;
775 		}
776 		if (*cp++)
777 			return -1;
778 	}
779 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
780 		switch (*np++ = *cp++) {
781 		case 0:
782 			dp->d_namlen -= i + 1;
783 			return chksum;
784 		case '/':
785 			np[-1] = 0;
786 			return -1;
787 		}
788 		/*
789 		 * See above
790 		 */
791 		if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
792 		    && np > ep) {
793 			np[-1] = 0;
794 			return -1;
795 		}
796 		if (*cp++)
797 			return -1;
798 	}
799 	return chksum;
800 }
801 
802 /*
803  * Compute the checksum of a DOS filename for Win95 use
804  */
805 u_int8_t
winChksum(name)806 winChksum(name)
807 	u_int8_t *name;
808 {
809 	int i;
810 	u_int8_t s;
811 
812 	for (s = 0, i = 11; --i >= 0; s += *name++)
813 		s = (s << 7)|(s >> 1);
814 	return s;
815 }
816 
817 /*
818  * Determine the number of slots necessary for Win95 names
819  */
820 int
winSlotCnt(un,unlen)821 winSlotCnt(un, unlen)
822 	u_char *un;
823 	int unlen;
824 {
825 	for (un += unlen; unlen > 0; unlen--)
826 		if (*--un != ' ' && *un != '.')
827 			break;
828 	if (unlen > WIN_MAXLEN)
829 		return 0;
830 	return howmany(unlen, WIN_CHARS);
831 }
832