xref: /dragonfly/sys/vfs/msdosfs/msdosfs_conv.c (revision 3170ffd7)
1 /* $FreeBSD: src/sys/msdosfs/msdosfs_conv.c,v 1.29.2.1 2002/11/08 22:01:22 semenu 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 <sys/iconv.h>
59 #include <machine/clock.h>
60 #include <sys/dirent.h>
61 #include <sys/mount.h>
62 /*
63  * MSDOSFS include files.
64  */
65 #include "bpb.h"
66 #include "msdosfsmount.h"
67 #include "direntry.h"
68 
69 extern struct iconv_functions *msdos_iconv;
70 
71 /*
72  * Total number of days that have passed for each month in a regular year.
73  */
74 static u_short regyear[] = {
75 	31, 59, 90, 120, 151, 181,
76 	212, 243, 273, 304, 334, 365
77 };
78 
79 /*
80  * Total number of days that have passed for each month in a leap year.
81  */
82 static u_short leapyear[] = {
83 	31, 60, 91, 121, 152, 182,
84 	213, 244, 274, 305, 335, 366
85 };
86 
87 /*
88  * Variables used to remember parts of the last time conversion.  Maybe we
89  * can avoid a full conversion.
90  */
91 static u_long  lasttime;
92 static u_long  lastday;
93 static u_short lastddate;
94 static u_short lastdtime;
95 
96 static __inline u_int8_t find_lcode (u_int16_t code, u_int16_t *u2w);
97 
98 /*
99  * Convert the unix version of time to dos's idea of time to be used in
100  * file timestamps. The passed in unix time is assumed to be in GMT.
101  */
102 void
103 unix2dostime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, 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  * Initialize the temporary concatenation buffer.
176  */
177 void
178 mbnambuf_init(struct mbnambuf *nbp)
179 {
180         nbp->nb_len = 0;
181         nbp->nb_last_id = -1;
182         nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0';
183 }
184 /*
185  * Fill out our concatenation buffer with the given substring, at the offset
186  * specified by its id.  Since this function must be called with ids in
187  * descending order, we take advantage of the fact that ASCII substrings are
188  * exactly WIN_CHARS in length.  For non-ASCII substrings, we shift all
189  * previous (i.e. higher id) substrings upwards to make room for this one.
190  * This only penalizes portions of substrings that contain more than
191  * WIN_CHARS bytes when they are first encountered.
192  */
193 void
194 mbnambuf_write(struct mbnambuf *nbp, char *name, int id)
195 {
196         char *slot;
197         size_t count, newlen;
198         if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) {
199                 kprintf("msdosfs: non-decreasing id: id %d, last id %d\n",
200                     id, nbp->nb_last_id);
201                 return;
202         }
203         /* Will store this substring in a WIN_CHARS-aligned slot. */
204         slot = &nbp->nb_buf[id * WIN_CHARS];
205         count = strlen(name);
206         newlen = nbp->nb_len + count;
207         if (newlen > WIN_MAXLEN || newlen > 127) {
208                 kprintf("msdosfs: file name length %zu too large\n", newlen);
209                 return;
210         }
211         /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
212         if (count > WIN_CHARS && nbp->nb_len != 0)
213                 bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len);
214         /* Copy in the substring to its slot and update length so far. */
215         bcopy(name, slot, count);
216         nbp->nb_len = newlen;
217         nbp->nb_last_id = id;
218 }
219 /*
220  * Take the completed string and use it to setup the struct dirent.
221  * Be sure to always nul-terminate the d_name and then copy the string
222  * from our buffer.  Note that this function assumes the full string has
223  * been reassembled in the buffer.  If it's called before all substrings
224  * have been written via mbnambuf_write(), the result will be incorrect.
225  */
226 char *
227 mbnambuf_flush(struct mbnambuf *nbp, char *d_name, u_int16_t *d_namlen)
228 {
229 #if 0
230         if (nbp->nb_len > 127) {
231                 mbnambuf_init(nbp);
232                 return (NULL);
233         }
234 #endif
235         bcopy(&nbp->nb_buf[0], d_name, nbp->nb_len);
236 	d_name[nbp->nb_len] = '\0';
237         *d_namlen = nbp->nb_len;
238         mbnambuf_init(nbp);
239         return (d_name);
240 }
241 
242 /*
243  * Convert from dos' idea of time to unix'. This will probably only be
244  * called from the stat(), and fstat() system calls and so probably need
245  * not be too efficient.
246  */
247 void
248 dos2unixtime(u_int dd, u_int dt, u_int dh, struct timespec *tsp)
249 {
250 	u_long seconds;
251 	u_long month;
252 	u_long year;
253 	u_long days;
254 	u_short *months;
255 
256 	if (dd == 0) {
257 		/*
258 		 * Uninitialized field, return the epoch.
259 		 */
260 		tsp->tv_sec = 0;
261 		tsp->tv_nsec = 0;
262 		return;
263 	}
264 	seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
265 	    + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
266 	    + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
267 	    + dh / 100;
268 	/*
269 	 * If the year, month, and day from the last conversion are the
270 	 * same then use the saved value.
271 	 */
272 	if (lastdosdate != dd) {
273 		lastdosdate = dd;
274 		days = 0;
275 		year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
276 		days = year * 365;
277 		days += year / 4 + 1;	/* add in leap days */
278 		if ((year & 0x03) == 0)
279 			days--;		/* if year is a leap year */
280 		months = year & 0x03 ? regyear : leapyear;
281 		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
282 		if (month < 1 || month > 12) {
283 			kprintf("dos2unixtime(): month value out of range (%ld)\n",
284 			    month);
285 			month = 1;
286 		}
287 		if (month > 1)
288 			days += months[month - 2];
289 		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
290 		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
291 	}
292 	tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
293 	     + adjkerntz;
294 	     /* + daylight savings time correction */
295 	tsp->tv_nsec = (dh % 100) * 10000000;
296 }
297 
298 /*
299  * 0 - character disallowed in long file name.
300  * 1 - character should be replaced by '_' in DOS file name,
301  *     and generation number inserted.
302  * 2 - character ('.' and ' ') should be skipped in DOS file name,
303  *     and generation number inserted.
304  */
305 static u_char
306 unix2dos[256] = {
307 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
308 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
309 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
310 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
311 	2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
312 	0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,	/* 28-2f */
313 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
314 	0x38, 0x39, 0,    1,    0,    1,    0,    0,	/* 38-3f */
315 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
316 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
317 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
318 	0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f,	/* 58-5f */
319 	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
320 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
321 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
322 	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
323 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
324 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
325 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
326 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
327 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
328 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
329 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
330 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
331 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
332 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
333 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
334 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
335 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
336 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
337 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
338 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
339 };
340 
341 static u_char
342 dos2unix[256] = {
343 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
344 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
345 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
346 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 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, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
352 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
353 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
354 	0x58, 0x59, 0x5a, 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 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
360 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
361 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
362 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
363 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
364 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
365 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
366 	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
367 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
368 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
369 	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
370 	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
371 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
372 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
373 	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
374 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
375 };
376 
377 static u_char
378 u2l[256] = {
379 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
380 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
381 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
382 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
383 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
384 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
385 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
386 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
387 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
388 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
389 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
390 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
391 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
392 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
393 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
394 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
395 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
396 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
397 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
398 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
399 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
400 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
401 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
402 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
403 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
404 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
405 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
406 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
407 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
408 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
409 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
410 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
411 };
412 
413 static u_char
414 l2u[256] = {
415 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
416 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
417 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
418 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
419 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
420 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
421 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
422 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
423 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
424 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
425 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
426 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
427 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
428 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
429 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
430 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
431 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
432 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
433 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
434 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
435 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
436 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
437 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
438 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
439 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
440 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
441 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
442 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
443 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
444 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
445 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
446 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
447 };
448 
449 /*
450  * Convert DOS char to Local char
451  */
452 static u_int16_t
453 dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *
454 pmp)
455 {
456         u_char c;
457         char *outp, outbuf[3];
458         u_int16_t wc;
459         size_t len, olen;
460         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) {
461                 olen = len = 2;
462                 outp = outbuf;
463                 if (lower & (LCASE_BASE | LCASE_EXT))
464                         msdos_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
465                                                   ilen, &outp, &olen, KICONV_LOWER);
466                 else
467                         msdos_iconv->convchr(pmp->pm_d2u, (const char **)instr,
468                                              ilen, &outp, &olen);
469                 len -= olen;
470                 /*
471                  * return '?' if failed to convert
472 		 */
473                 if (len == 0) {
474                         (*ilen)--;
475                         (*instr)++;
476                         return ('?');
477                 }
478 		wc = 0;
479                 while(len--)
480                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
481                 return (wc);
482         }
483         (*ilen)--;
484         c = *(*instr)++;
485         c = dos2unix[c];
486         if (lower & (LCASE_BASE | LCASE_EXT))
487                 c = u2l[c];
488         return ((u_int16_t)c);
489 }
490 
491 /*
492  * DOS filenames are made of 2 parts, the name part and the extension part.
493  * The name part is 8 characters long and the extension part is 3
494  * characters long.  They may contain trailing blanks if the name or
495  * extension are not long enough to fill their respective fields.
496  */
497 
498 /*
499  * Convert a DOS filename to a unix filename. And, return the number of
500  * characters in the resulting unix filename excluding the terminating
501  * null.
502  */
503 int
504 dos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp)
505 {
506 	size_t i;
507 	int thislong = 1;
508 	u_char c;
509 
510 	/*
511 	 * If first char of the filename is SLOT_E5 (0x05), then the real
512 	 * first char of the filename should be 0xe5. But, they couldn't
513 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
514 	 * directory slot. Another dos quirk.
515 	 */
516 	if (*dn == SLOT_E5)
517                 *dn = 0xe5;
518 	/*
519 	 * Copy the name portion into the unix filename string.
520          */
521 	for(i  = 8; i > 0 && *dn != ' ';) {
522                 c = dos2unixchr((const u_char **)(void *)&dn,
523 		    &i, lower & LCASE_BASE, pmp);
524                 if (c & 0xff00) {
525                         *un++ = c >> 8;
526                         thislong++;
527                 }
528                 *un++ = c;
529                 thislong++;
530         }
531         dn += i;
532 
533 	/*
534 	 * Now, if there is an extension then put in a period and copy in
535 	 * the extension.
536 	 */
537 	 if (*dn != ' ') {
538                 *un++ = '.';
539                 thislong++;
540                 for (i = 3; i > 0 && *dn != ' ';) {
541                         c = dos2unixchr((const u_char **)(void *)&dn, &i,
542                             lower & LCASE_EXT, pmp);
543                         if (c & 0xff00) {
544                                 *un++ = c >> 8;
545                                 thislong++;
546                         }
547                         *un++ = c;
548                         thislong++;
549                 }
550         }
551 	*un++ = 0;
552 /*
553 	for(i = 0; un[i] != 0; i++)
554 		kprintf("0x%x", un[i]);
555 	kprintf(" ->%s\n", dn);
556 */
557 	return (thislong);
558 }
559 
560 /*
561  * Store an area with multi byte string instr, and reterns left
562  * byte of instr and moves pointer forward. The area's size is
563  * inlen or outlen.
564  */
565 static int
566 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag,
567  void *handle)
568 {
569         char *outp, outstr[outlen * weight + 1];
570 	bzero(outstr, outlen*weight+1);
571         if (flag & MSDOSFSMNT_KICONV && msdos_iconv) {
572                 outp = outstr;
573                 outlen *= weight;
574                 msdos_iconv->conv(handle, instr, &inlen, &outp, &outlen);
575                 return (inlen);
576         }
577         (*instr) += min(inlen, outlen);
578         return (inlen - min(inlen, outlen));
579 }
580 
581 /*
582  * Convert Local char to DOS char
583  */
584 static u_int16_t
585 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
586 {
587         u_char c;
588         char *up, *outp, unicode[3], outbuf[3];
589         u_int16_t wc;
590         size_t len, ucslen, unixlen, olen;
591         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) {
592                 /*
593                  * to hide an invisible character, using a unicode filter
594                  */
595                 ucslen = 2;
596                 len = *ilen;
597                 up = unicode;
598                 msdos_iconv->convchr(pmp->pm_u2w, (const char **)instr,
599                                      ilen, &up, &ucslen);
600                 unixlen = len - *ilen;
601 
602                 /*
603                  * cannot be converted
604                  */
605                 if (unixlen == 0) {
606                         (*ilen)--;
607                         (*instr)++;
608                         return (0);
609                 }
610                 /*
611                  * return magic number for ascii char
612                  */
613                 if (unixlen == 1) {
614                         c = *(*instr -1);
615                         if (! (c & 0x80)) {
616                                 c = unix2dos[c];
617                                 if (c <= 2)
618                                         return (c);
619                         }
620                 }
621                 /*
622                  * now convert using libiconv
623                  */
624                 *instr -= unixlen;
625                 *ilen = len;
626                 olen = len = 2;
627                 outp = outbuf;
628                 msdos_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
629                                           ilen, &outp, &olen, KICONV_FROM_UPPER);
630                 len -= olen;
631                 /*
632                  * cannot be converted, but has unicode char, should return magic number
633 		 */
634                 if (len == 0) {
635                         (*ilen) -= unixlen;
636                         (*instr) += unixlen;
637                         return (1);
638                 }
639                 wc = 0;
640                 while(len--)
641                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
642  return (wc);
643         }
644         (*ilen)--;
645         c = *(*instr)++;
646         c = l2u[c];
647         c = unix2dos[c];
648         return ((u_int16_t)c);
649 }
650 
651 /*
652  * Convert a unix filename to a DOS filename according to Win95 rules.
653  * If applicable and gen is not 0, it is inserted into the converted
654  * filename as a generation number.
655  * Returns
656  *	0 if name couldn't be converted
657  *	1 if the converted name is the same as the original
658  *	  (no long filename entry necessary for Win95)
659  *	2 if conversion was successful
660  *	3 if conversion was successful and generation number was inserted
661  */
662 
663 int
664 unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen, struct msdosfsmount *pmp)
665 {
666 	ssize_t i, j;
667         int l;
668         int conv = 1;
669         const u_char *cp, *dp, *dp1;
670         u_char gentext[6], *wcp;
671         u_int16_t c;
672         /*
673          * Fill the dos filename string with blanks. These are DOS's pad
674          * characters.
675          */
676         for (i = 0; i < 11; i++)
677                 dn[i] = ' ';
678         dn[11] = 0;
679         /*
680          * The filenames "." and ".." are handled specially, since they
681          * don't follow dos filename rules.
682          */
683         if (un[0] == '.' && unlen == 1) {
684                 dn[0] = '.';
685                 return gen <= 1;
686         }
687         if (un[0] == '.' && un[1] == '.' && unlen == 2) {
688                 dn[0] = '.';
689                 dn[1] = '.';
690                 return gen <= 1;
691         }
692         /*
693          * Filenames with only blanks and dots are not allowed!
694          */
695         for (cp = un, i = unlen; --i >= 0; cp++)
696                 if (*cp != ' ' && *cp != '.')
697                         break;
698         if (i < 0)
699                 return 0;
700         /*
701          * Filenames with some characters are not allowed!
702          */
703         for (cp = un, i = unlen; i > 0;)
704                 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
705                         return 0;
706         /*
707          * Now find the extension
708          * Note: dot as first char doesn't start extension
709          *       and trailing dots and blanks are ignored
710          * Note(2003/7): It seems recent Windows has
711          *       defferent rule than this code, that Windows
712          *       ignores all dots before extension, and use all
713          *       chars as filename except for dots.
714          */
715         dp = dp1 = NULL;
716         for (cp = un + 1, i = unlen - 1; --i >= 0;) {
717                 switch (*cp++) {
718                 case '.':
719                         if (!dp1)
720                                 dp1 = cp;
721                         break;
722                 case ' ':
723                         break;
724                 default:
725                         if (dp1)
726                                 dp = dp1;
727                         dp1 = NULL;
728                         break;
729                 }
730         }
731         /*
732          * Now convert it (this part is for extension).
733          * As Windows XP do, if it's not ascii char,
734          * this function should return 2 or 3, so that checkng out Unicode name.
735          */
736         if (dp) {
737                 if (dp1)
738                         l = dp1 - dp;
739                 else
740                         l = unlen - (dp - un);
741                 for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
742                         c = unix2doschr(&cp, (size_t *)&i, pmp);
743                         if (c & 0xff00) {
744                                 dn[j] = c >> 8;
745                                 if (++j < 11) {
746                                         dn[j] = c;
747                                         if (conv != 3)
748                                                 conv = 2;
749                                         continue;
750                                 } else {
751                                         conv = 3;
752                                         dn[j-1] = ' ';
753                                         break;
754                                 }
755                         } else {
756                                 dn[j] = c;
757                         }
758                         if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
759                                 conv = 2;
760                         if (dn[j] == 1) {
761                                 conv = 3;
762                                 dn[j] = '_';
763                         }
764                         if (dn[j] == 2) {
765                                 conv = 3;
766                                 dn[j--] = ' ';
767                         }
768                 }
769                 if (i > 0)
770                         conv = 3;
771                 dp--;
772         } else {
773                 for (dp = cp; *--dp == ' ' || *dp == '.';);
774                 dp++;
775         }
776         /*
777          * Now convert the rest of the name
778          */
779         for (i = dp - un, j = 0; un < dp && j < 8; j++) {
780                 c = unix2doschr(&un, &i, pmp);
781                 if (c & 0xff00) {
782                         dn[j] = c >> 8;
783                         if (++j < 8) {
784                                 dn[j] = c;
785                                 if (conv != 3)
786                                         conv = 2;
787                                 continue;
788                         } else {
789                                 conv = 3;
790                                 dn[j-1] = ' ';
791                                 break;
792                         }
793                 } else {
794                         dn[j] = c;
795                 }
796                 if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
797                         conv = 2;
798                 if (dn[j] == 1) {
799                         conv = 3;
800                         dn[j] = '_';
801                 }
802                 if (dn[j] == 2) {
803                         conv = 3;
804                         dn[j--] = ' ';
805                 }
806         }
807         if (un < dp)
808                 conv = 3;
809         /*
810          * If we didn't have any chars in filename,
811          * generate a default
812          */
813         if (!j)
814                 dn[0] = '_';
815         /*
816          * If there wasn't any char dropped,
817          * there is no place for generation numbers
818          */
819         if (conv != 3) {
820                 if (gen > 1)
821                         conv = 0;
822                 goto done;
823         }
824         /*
825          * Now insert the generation number into the filename part
826          */
827         if (gen == 0)
828                 goto done;
829         for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
830                 *--wcp = gen % 10 + '0';
831         if (gen) {
832                 conv = 0;
833                 goto done;
834         }
835         for (i = 8; dn[--i] == ' ';);
836         i++;
837         if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
838                 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
839         /*
840          * Correct posision to where insert the generation number
841          */
842         cp = dn;
843         i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
844         dn[i++] = '~';
845         while (wcp < gentext + sizeof(gentext))
846                 dn[i++] = *wcp++;
847         /*
848          * Tail of the filename should be space
849          */
850         while (i < 8)
851                 dn[i++] = ' ';
852         conv = 3;
853 done:
854         /*
855          * The first character cannot be E5,
856          * because that means a deleted entry
857          */
858         if (dn[0] == 0xe5)
859                 dn[0] = SLOT_E5;
860         return conv;
861 }
862 
863 /*
864  * Convert Local char to Windows char
865  */
866 static u_int16_t
867 unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *
868 pmp)
869 {
870         u_char *outp, outbuf[3];
871         u_int16_t wc;
872         size_t olen;
873         if (*ilen == 0)
874                 return (0);
875         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) {
876                 outp = outbuf;
877                 olen = 2;
878                 if (lower & (LCASE_BASE | LCASE_EXT))
879                         msdos_iconv->convchr_case(pmp->pm_u2w, (const char **)
880 instr,
881                                                   ilen, (char **)&outp, &olen,
882                                                   KICONV_FROM_LOWER);
883                 else
884                         msdos_iconv->convchr(pmp->pm_u2w, (const char **)instr,
885                                              ilen, (char **)&outp, &olen);
886                 /*
887                  * return '0' if end of filename
888                  */
889                 if (olen == 2)
890                         return (0);
891                 wc = (outbuf[0]<<8) | outbuf[1];
892                 return (wc);
893         }
894         (*ilen)--;
895         wc = (*instr)[0];
896         if (lower & (LCASE_BASE | LCASE_EXT))
897                 wc = u2l[wc];
898         (*instr)++;
899         return (wc);
900 }
901 
902 /*
903  * Create a Win95 long name directory entry
904  * Note: assumes that the filename is valid,
905  *	 i.e. doesn't consist solely of blanks and dots
906  */
907 int
908 unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt, int chksum, struct msdosfsmount *pmp)
909 {
910         u_int8_t *wcp;
911         int i, end;
912         u_int16_t code;
913 
914         /*
915          * Drop trailing blanks and dots
916          */
917         unlen = winLenFixup(un, unlen);
918 
919         /*
920          * Cut *un for this slot
921          */
922         unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
923                           pmp->pm_flags, pmp->pm_u2w);
924 
925         /*
926          * Initialize winentry to some useful default
927          */
928         for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
929         wep->weCnt = cnt;
930         wep->weAttributes = ATTR_WIN95;
931         wep->weReserved1 = 0;
932         wep->weChksum = chksum;
933 	wep->weReserved2 = 0;
934 
935         /*
936          * Now convert the filename parts
937          */
938         end = 0;
939         for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;)
940 	{
941                 code = unix2winchr(&un, &unlen, 0, pmp);
942                 *wcp++ = code;
943                 *wcp++ = code >> 8;
944                 if (!code)
945                         end = WIN_LAST;
946         }
947         for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;)
948 	{
949                 code = unix2winchr(&un, &unlen, 0, pmp);
950                 *wcp++ = code;
951                 *wcp++ = code >> 8;
952                 if (!code)
953                         end = WIN_LAST;
954         }
955         for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;)
956 	{
957                 code = unix2winchr(&un, &unlen, 0, pmp);
958 		*wcp++ = code;
959                 *wcp++ = code >> 8;
960                 if (!code)
961                         end = WIN_LAST;
962         }
963         if (*un == '\0')
964                 end = WIN_LAST;
965         wep->weCnt |= end;
966         return !end;
967 }
968 
969 static __inline u_int8_t
970 find_lcode(u_int16_t code, u_int16_t *u2w)
971 {
972 	int i;
973 
974 	for (i = 0; i < 128; i++)
975 		if (u2w[i] == code)
976 			return (i | 0x80);
977 	return '?';
978 }
979 
980 /*
981  * Compare our filename to the one in the Win95 entry
982  * Returns the checksum or -1 if no match
983  */
984 int
985 winChkName(struct mbnambuf *nbp, const u_char *un, size_t unlen, int chksum,
986     struct msdosfsmount *pmp)
987 {
988         size_t len;
989         u_int16_t c1, c2;
990         u_char *np;
991 	u_int16_t d_namlen;
992 	char d_name[127];
993 	bzero(d_name, 127);
994         /*
995          * We already have winentry in *nbp.
996          */
997         if (!mbnambuf_flush(nbp, d_name, &d_namlen) || d_namlen == 0)
998                 return -1;
999 #ifdef MSDOSFS_DEBUG
1000         kprintf("winChkName(): un=%s:%zd,d_name=%s:%d\n", un, unlen,
1001                                                         d_name,
1002                                                         d_namlen);
1003 #endif
1004         /*
1005          * Compare the name parts
1006          */
1007         len = d_namlen;
1008         if (unlen != len)
1009                 return -2;
1010         for (np = d_name; unlen > 0 && len > 0;) {
1011                 /*
1012                  * Comparison must be case insensitive, because FAT disallows
1013                  * to look up or create files in case sensitive even when
1014                  * it's a long file name.
1015                  */
1016                 c1 = unix2winchr((const u_char **)(void *)&np,
1017 		    &len, LCASE_BASE, pmp);
1018                 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
1019                 if (c1 != c2)
1020                         return -2;
1021         }
1022         return chksum;
1023 }
1024 
1025 /*
1026  * Convert Windows char to Local char
1027  */
1028 static u_int16_t
1029 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
1030 {
1031         u_char *inp, *outp, inbuf[3], outbuf[3];
1032         size_t ilen, olen, len;
1033         if (wc == 0)
1034                 return (0);
1035         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) {
1036                 inbuf[0] = (u_char)(wc>>8);
1037                 inbuf[1] = (u_char)wc;
1038                 inbuf[2] = '\0';
1039                 ilen = olen = len = 2;
1040                 inp = inbuf;
1041                 outp = outbuf;
1042                 msdos_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
1043                                      (char **)&outp, &olen);
1044                 len -= olen;
1045 
1046                 /*
1047                  * return '?' if failed to convert
1048                  */
1049                 if (len == 0) {
1050                         wc = '?';
1051                         return (wc);
1052                 }
1053                 wc = 0;
1054                 while(len--)
1055                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1056                 return (wc);
1057         }
1058         if (wc & 0xff00)
1059                 wc = '?';
1060         return (wc);
1061 }
1062 
1063 /*
1064  * Convert Win95 filename to dirbuf.
1065  * Returns the checksum or -1 if impossible
1066  */
1067 int
1068 win2unixfn(struct mbnambuf *nbp, struct winentry *wep, int chksum,
1069     struct msdosfsmount *pmp)
1070 {
1071         u_int8_t *cp;
1072         u_int8_t *np, name[WIN_CHARS * 2 + 1];
1073         u_int16_t code;
1074         int i;
1075         if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
1076             || !(wep->weCnt&WIN_CNT))
1077                 return -1;
1078         /*
1079          * First compare checksums
1080          */
1081         if (wep->weCnt&WIN_LAST) {
1082                 chksum = wep->weChksum;
1083         } else if (chksum != wep->weChksum)
1084                 chksum = -1;
1085         if (chksum == -1)
1086                 return -1;
1087         /*
1088          * Convert the name parts
1089          */
1090         np = name;
1091         for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
1092                 code = (cp[1] << 8) | cp[0];
1093                 switch (code) {
1094                 case 0:
1095                         *np = '\0';
1096                         mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
1097                         return chksum;
1098                 case '/':
1099                         *np = '\0';
1100                         return -1;
1101                 default:
1102                         code = win2unixchr(code, pmp);
1103                         if (code & 0xff00)
1104                                 *np++ = code >> 8;
1105                         *np++ = code;
1106                         break;
1107                 }
1108                 cp += 2;
1109         }
1110         for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
1111                 code = (cp[1] << 8) | cp[0];
1112                 switch (code) {
1113                 case 0:
1114                         *np = '\0';
1115                         mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
1116                         return chksum;
1117                 case '/':
1118                         *np = '\0';
1119                         return -1;
1120                 default:
1121                         code = win2unixchr(code, pmp);
1122                         if (code & 0xff00)
1123                                 *np++ = code >> 8;
1124                         *np++ = code;
1125                         break;
1126                 }
1127                 cp += 2;
1128         }
1129         for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
1130                 code = (cp[1] << 8) | cp[0];
1131                 switch (code) {
1132                 case 0:
1133                         *np = '\0';
1134                         mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
1135                         return chksum;
1136                 case '/':
1137                         *np = '\0';
1138                         return -1;
1139                 default:
1140                         code = win2unixchr(code, pmp);
1141                         if (code & 0xff00)
1142                                 *np++ = code >> 8;
1143                         *np++ = code;
1144                         break;
1145                 }
1146                 cp += 2;
1147         }
1148         *np = '\0';
1149         mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
1150         return chksum;
1151 }
1152 /*
1153  * Compute the checksum of a DOS filename for Win95 use
1154  */
1155 u_int8_t
1156 winChksum(u_int8_t *name)
1157 {
1158 	int i;
1159 	u_int8_t s;
1160 
1161 	for (s = 0, i = 11; --i >= 0; s += *name++)
1162 		s = (s << 7)|(s >> 1);
1163 	return s;
1164 }
1165 
1166 /*
1167  * Determine the number of slots necessary for Win95 names
1168  */
1169 int
1170 winSlotCnt(const u_char *un, size_t unlen, struct msdosfsmount *pmp)
1171 {
1172         size_t wlen;
1173         char wn[WIN_MAXLEN * 2 + 1], *wnp;
1174         unlen = winLenFixup(un, unlen);
1175         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) {
1176                 wlen = WIN_MAXLEN * 2;
1177                 wnp = wn;
1178                 msdos_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
1179                 if (unlen > 0)
1180                         return 0;
1181                 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
1182         }
1183         if (unlen > WIN_MAXLEN)
1184                 return 0;
1185 	 return howmany(unlen, WIN_CHARS);
1186 }
1187 
1188 /*
1189  * Determine the number of bytes neccesary for Win95 names
1190  */
1191 size_t
1192 winLenFixup(const u_char *un, size_t unlen)
1193 {
1194 	for (un += unlen; unlen > 0; unlen--)
1195 		if (*--un != ' ' && *un != '.')
1196 			break;
1197 	return unlen;
1198 }
1199