2 /* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */
5 * Copyright (C) 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
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.
52 * System include files.
54 #include <sys/param.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>
64 extern struct iconv_functions *msdosfs_iconv;
67 * MSDOSFS include files.
69 #include <fs/msdosfs/bpb.h>
70 #include <fs/msdosfs/msdosfsmount.h>
71 #include <fs/msdosfs/direntry.h>
74 * Total number of days that have passed for each month in a regular year.
76 static u_short regyear[] = {
77 31, 59, 90, 120, 151, 181,
78 212, 243, 273, 304, 334, 365
82 * Total number of days that have passed for each month in a leap year.
84 static u_short leapyear[] = {
85 31, 60, 91, 121, 152, 182,
86 213, 244, 274, 305, 335, 366
90 * Variables used to remember parts of the last time conversion. Maybe we
91 * can avoid a full conversion.
93 static u_long lasttime;
94 static u_long lastday;
95 static u_short lastddate;
96 static u_short lastdtime;
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 *);
104 static char *nambuf_ptr;
105 static size_t nambuf_len;
106 static int nambuf_max_id;
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.
113 unix2dostime(tsp, ddp, dtp, dhp)
114 struct timespec *tsp;
127 * If the time from the last conversion is the same as now, then
128 * skip the computations and use the saved result.
130 t = tsp->tv_sec - (tz_minuteswest * 60)
131 - (wall_cmos_clock ? adjkerntz : 0);
132 /* - daylight savings time correction */
136 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
137 + (((t / 60) % 60) << DT_MINUTES_SHIFT)
138 + (((t / 3600) % 24) << DT_HOURS_SHIFT);
141 * If the number of days since 1970 is the same as the last
142 * time we did the computation then skip all this leap year
145 days = t / (24 * 60 * 60);
146 if (days != lastday) {
148 for (year = 1970;; year++) {
149 inc = year & 0x03 ? 365 : 366;
154 months = year & 0x03 ? regyear : leapyear;
155 for (month = 0; days >= months[month]; month++)
158 days -= months[month - 1];
159 lastddate = ((days + 1) << DD_DAY_SHIFT)
160 + ((month + 1) << DD_MONTH_SHIFT);
162 * Remember dos's idea of time is relative to 1980.
163 * unix's is relative to 1970. If somehow we get a
164 * time before 1980 then don't give totally crazy
168 lastddate += (year - 1980) << DD_YEAR_SHIFT;
174 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
180 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
181 * interval there were 8 regular years and 2 leap years.
183 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
185 static u_short lastdosdate;
186 static u_long lastseconds;
189 * Convert from dos' idea of time to unix'. This will probably only be
190 * called from the stat(), and fstat() system calls and so probably need
191 * not be too efficient.
194 dos2unixtime(dd, dt, dh, tsp)
198 struct timespec *tsp;
208 * Uninitialized field, return the epoch.
214 seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
215 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
216 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
219 * If the year, month, and day from the last conversion are the
220 * same then use the saved value.
222 if (lastdosdate != dd) {
225 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
227 days += year / 4 + 1; /* add in leap days */
228 if ((year & 0x03) == 0)
229 days--; /* if year is a leap year */
230 months = year & 0x03 ? regyear : leapyear;
231 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
232 if (month < 1 || month > 12) {
233 printf("dos2unixtime(): month value out of range (%ld)\n",
238 days += months[month - 2];
239 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
240 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
242 tsp->tv_sec = seconds + lastseconds + (tz_minuteswest * 60)
244 /* + daylight savings time correction */
245 tsp->tv_nsec = (dh % 100) * 10000000;
249 * 0 - character disallowed in long file name.
250 * 1 - character should be replaced by '_' in DOS file name,
251 * and generation number inserted.
252 * 2 - character ('.' and ' ') should be skipped in DOS file name,
253 * and generation number inserted.
257 /* iso8859-1 -> cp850 */
258 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
259 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
260 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
261 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
262 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
263 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */
264 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
265 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */
266 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
267 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
268 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
269 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */
270 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */
271 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */
272 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */
273 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */
274 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
275 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
276 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
277 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
278 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
279 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
280 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
281 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
282 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
283 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
284 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
285 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
286 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
287 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
288 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
289 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
294 /* cp850 -> iso8859-1 */
295 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
296 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
297 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
298 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
299 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
300 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
301 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
302 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
303 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
304 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
305 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
306 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
307 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
308 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
309 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
310 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
311 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
312 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
313 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
314 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */
315 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
316 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
317 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */
318 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */
319 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */
320 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */
321 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */
322 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */
323 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
324 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
325 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
326 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */
332 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
333 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
334 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
335 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
336 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
337 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
338 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
339 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
340 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
341 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
342 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
343 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
344 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
345 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
346 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
347 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
348 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
349 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
350 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
351 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
352 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
353 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
354 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
355 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
356 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
357 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
358 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
359 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
360 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
361 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
362 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
363 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
369 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
370 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
371 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
372 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
373 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
374 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
375 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
376 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
377 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
378 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
379 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
380 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
381 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
382 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
383 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
384 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
385 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
386 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
387 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
388 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
389 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
390 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
391 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
392 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
393 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
394 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
395 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
396 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
397 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
398 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
399 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
400 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
404 * DOS filenames are made of 2 parts, the name part and the extension part.
405 * The name part is 8 characters long and the extension part is 3
406 * characters long. They may contain trailing blanks if the name or
407 * extension are not long enough to fill their respective fields.
411 * Convert a DOS filename to a unix filename. And, return the number of
412 * characters in the resulting unix filename excluding the terminating
416 dos2unixfn(dn, un, lower, pmp)
420 struct msdosfsmount *pmp;
427 * If first char of the filename is SLOT_E5 (0x05), then the real
428 * first char of the filename should be 0xe5. But, they couldn't
429 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
430 * directory slot. Another dos quirk.
436 * Copy the name portion into the unix filename string.
438 for (i = 8; i > 0 && *dn != ' ';) {
439 c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
450 * Now, if there is an extension then put in a period and copy in
456 for (i = 3; i > 0 && *dn != ' ';) {
457 c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
472 * Convert a unix filename to a DOS filename according to Win95 rules.
473 * If applicable and gen is not 0, it is inserted into the converted
474 * filename as a generation number.
476 * 0 if name couldn't be converted
477 * 1 if the converted name is the same as the original
478 * (no long filename entry necessary for Win95)
479 * 2 if conversion was successful
480 * 3 if conversion was successful and generation number was inserted
483 unix2dosfn(un, dn, unlen, gen, pmp)
488 struct msdosfsmount *pmp;
493 const u_char *cp, *dp, *dp1;
494 u_char gentext[6], *wcp;
498 * Fill the dos filename string with blanks. These are DOS's pad
501 for (i = 0; i < 11; i++)
506 * The filenames "." and ".." are handled specially, since they
507 * don't follow dos filename rules.
509 if (un[0] == '.' && unlen == 1) {
513 if (un[0] == '.' && un[1] == '.' && unlen == 2) {
520 * Filenames with only blanks and dots are not allowed!
522 for (cp = un, i = unlen; --i >= 0; cp++)
523 if (*cp != ' ' && *cp != '.')
530 * Filenames with some characters are not allowed!
532 for (cp = un, i = unlen; i > 0;)
533 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
537 * Now find the extension
538 * Note: dot as first char doesn't start extension
539 * and trailing dots and blanks are ignored
540 * Note(2003/7): It seems recent Windows has
541 * defferent rule than this code, that Windows
542 * ignores all dots before extension, and use all
543 * chars as filename except for dots.
546 for (cp = un + 1, i = unlen - 1; --i >= 0;) {
563 * Now convert it (this part is for extension)
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);
585 if (*(cp - 1) != dn[j] && conv != 3)
600 for (dp = cp; *--dp == ' ' || *dp == '.';);
605 * Now convert the rest of the name
607 for (i = dp - un, j = 0; un < dp && j < 8; j++) {
608 c = unix2doschr(&un, &i, pmp);
622 if (*(un - 1) != dn[j] && conv != 3)
636 * If we didn't have any chars in filename,
643 * If there wasn't any char dropped,
644 * there is no place for generation numbers
653 * Now insert the generation number into the filename part
657 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
658 *--wcp = gen % 10 + '0';
663 for (i = 8; dn[--i] == ' ';);
665 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
666 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
668 * Correct posision to where insert the generation number
671 i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
674 while (wcp < gentext + sizeof(gentext))
678 * Tail of the filename should be space
686 * The first character cannot be E5,
687 * because that means a deleted entry
696 * Create a Win95 long name directory entry
697 * Note: assumes that the filename is valid,
698 * i.e. doesn't consist solely of blanks and dots
701 unix2winfn(un, unlen, wep, cnt, chksum, pmp)
704 struct winentry *wep;
707 struct msdosfsmount *pmp;
714 * Drop trailing blanks and dots
716 unlen = winLenFixup(un, unlen);
719 * Cut *un for this slot
721 unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
722 pmp->pm_flags, pmp->pm_u2w);
725 * Initialize winentry to some useful default
727 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
729 wep->weAttributes = ATTR_WIN95;
730 wep->weReserved1 = 0;
731 wep->weChksum = chksum;
732 wep->weReserved2 = 0;
735 * Now convert the filename parts
738 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
739 code = unix2winchr(&un, &unlen, 0, pmp);
745 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
746 code = unix2winchr(&un, &unlen, 0, pmp);
752 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
753 code = unix2winchr(&un, &unlen, 0, pmp);
766 * Compare our filename to the one in the Win95 entry
767 * Returns the checksum or -1 if no match
770 winChkName(un, unlen, chksum, pmp)
774 struct msdosfsmount *pmp;
779 struct dirent dirbuf;
782 * We alread have winentry in mbnambuf
784 if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen)
788 printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
794 * Compare the name parts
796 len = dirbuf.d_namlen;
800 for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
802 * Comparison must be case insensitive, because FAT disallows
803 * to look up or create files in case sensitive even when
804 * it's a long file name.
806 c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
807 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
815 * Convert Win95 filename to dirbuf.
816 * Returns the checksum or -1 if impossible
819 win2unixfn(wep, chksum, pmp)
820 struct winentry *wep;
822 struct msdosfsmount *pmp;
825 u_int8_t *np, name[WIN_CHARS * 2 + 1];
829 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
830 || !(wep->weCnt&WIN_CNT))
834 * First compare checksums
836 if (wep->weCnt&WIN_LAST) {
837 chksum = wep->weChksum;
838 } else if (chksum != wep->weChksum)
844 * Convert the name parts
847 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
848 code = (cp[1] << 8) | cp[0];
852 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
858 code = win2unixchr(code, pmp);
866 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
867 code = (cp[1] << 8) | cp[0];
871 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
877 code = win2unixchr(code, pmp);
885 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
886 code = (cp[1] << 8) | cp[0];
890 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
896 code = win2unixchr(code, pmp);
905 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
910 * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
919 s = ((s << 7) | (s >> 1)) + name[1];
920 s = ((s << 7) | (s >> 1)) + name[2];
921 s = ((s << 7) | (s >> 1)) + name[3];
922 s = ((s << 7) | (s >> 1)) + name[4];
923 s = ((s << 7) | (s >> 1)) + name[5];
924 s = ((s << 7) | (s >> 1)) + name[6];
925 s = ((s << 7) | (s >> 1)) + name[7];
926 s = ((s << 7) | (s >> 1)) + name[8];
927 s = ((s << 7) | (s >> 1)) + name[9];
928 s = ((s << 7) | (s >> 1)) + name[10];
934 * Determine the number of slots necessary for Win95 names
937 winSlotCnt(un, unlen, pmp)
940 struct msdosfsmount *pmp;
943 char wn[WIN_MAXLEN * 2 + 1], *wnp;
945 unlen = winLenFixup(un, unlen);
947 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
948 wlen = WIN_MAXLEN * 2;
950 msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
953 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
956 if (unlen > WIN_MAXLEN)
958 return howmany(unlen, WIN_CHARS);
962 * Determine the number of bytes neccesary for Win95 names
965 winLenFixup(un, unlen)
969 for (un += unlen; unlen > 0; unlen--)
970 if (*--un != ' ' && *un != '.')
976 * Store an area with multi byte string instr, and reterns left
977 * byte of instr and moves pointer forward. The area's size is
981 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
983 char *outp, outstr[outlen * weight + 1];
985 if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
988 msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
992 (*instr) += min(inlen, outlen);
993 return (inlen - min(inlen, outlen));
997 * Convert DOS char to Local char
1000 dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1003 char *outp, outbuf[3];
1007 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1011 if (lower & (LCASE_BASE | LCASE_EXT))
1012 msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
1013 ilen, &outp, &olen, KICONV_LOWER);
1015 msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
1016 ilen, &outp, &olen);
1020 * return '?' if failed to convert
1030 wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1037 if (lower & (LCASE_BASE | LCASE_EXT))
1039 return ((u_int16_t)c);
1043 * Convert Local char to DOS char
1046 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
1049 char *up, *outp, unicode[3], outbuf[3];
1051 size_t len, ulen, olen;
1053 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1055 * to hide an invisible character, using a unicode filter
1060 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1064 * cannot be converted
1073 * return magic number for ascii char
1075 if ((len - *ilen) == 1) {
1085 * now convert using libiconv
1087 *instr -= len - *ilen;
1092 msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
1093 ilen, &outp, &olen, KICONV_FROM_UPPER);
1097 wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1105 return ((u_int16_t)c);
1109 * Convert Windows char to Local char
1112 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
1114 u_char *inp, *outp, inbuf[3], outbuf[3];
1115 size_t ilen, olen, len;
1120 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1121 inbuf[0] = (u_char)(wc>>8);
1122 inbuf[1] = (u_char)wc;
1125 ilen = olen = len = 2;
1128 msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
1129 (char **)&outp, &olen);
1133 * return '?' if failed to convert
1142 wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1153 * Convert Local char to Windows char
1156 unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1158 u_char *outp, outbuf[3];
1165 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1168 if (lower & (LCASE_BASE | LCASE_EXT))
1169 msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
1170 ilen, (char **)&outp, &olen,
1173 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1174 ilen, (char **)&outp, &olen);
1177 * return '0' if end of filename
1182 wc = (outbuf[0]<<8) | outbuf[1];
1189 if (lower & (LCASE_BASE | LCASE_EXT))
1196 * Initialize the temporary concatenation buffer (once) and mark it as
1197 * empty on subsequent calls.
1203 if (nambuf_ptr == NULL) {
1204 nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK);
1205 nambuf_ptr[MAXNAMLEN] = '\0';
1212 * Fill out our concatenation buffer with the given substring, at the
1213 * offset specified by its id. For the last substring (i.e., the one with
1214 * the maximum id), use it to calculate the length of the entire string.
1215 * Since this function is usually called with ids in descending order, this
1216 * reduces the number of times we need to call strlen() since we know that
1217 * all previous substrings are exactly WIN_CHARS in length.
1220 mbnambuf_write(char *name, int id)
1225 if (id > nambuf_max_id) {
1226 count = strlen(name);
1227 nambuf_len = id * WIN_CHARS + count;
1228 if (nambuf_len > MAXNAMLEN) {
1229 printf("msdosfs: file name %d too long\n", nambuf_len);
1234 memcpy(nambuf_ptr + (id * WIN_CHARS), name, count);
1238 * Take the completed string and use it to setup the struct dirent.
1239 * Be sure to always nul-terminate the d_name and then copy the string
1240 * from our buffer. Note that this function assumes the full string has
1241 * been reassembled in the buffer. If it's called before all substrings
1242 * have been written via mbnambuf_write(), the result will be incorrect.
1245 mbnambuf_flush(struct dirent *dp)
1248 if (nambuf_len > sizeof(dp->d_name) - 1) {
1252 nambuf_ptr[nambuf_len] = '\0';
1253 memcpy(dp->d_name, nambuf_ptr, nambuf_len);
1254 dp->d_namlen = nambuf_len;
1257 return (dp->d_name);