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