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