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>
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>
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_last_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 - utc_offset();
131 /* - daylight savings time correction */
135 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
136 + (((t / 60) % 60) << DT_MINUTES_SHIFT)
137 + (((t / 3600) % 24) << DT_HOURS_SHIFT);
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
144 days = t / (24 * 60 * 60);
145 if (days != lastday) {
147 for (year = 1970;; year++) {
148 inc = year & 0x03 ? 365 : 366;
153 months = year & 0x03 ? regyear : leapyear;
154 for (month = 0; days >= months[month]; month++)
157 days -= months[month - 1];
158 lastddate = ((days + 1) << DD_DAY_SHIFT)
159 + ((month + 1) << DD_MONTH_SHIFT);
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
167 lastddate += (year - 1980) << DD_YEAR_SHIFT;
173 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
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.
182 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
184 static u_short lastdosdate;
185 static u_long lastseconds;
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.
193 dos2unixtime(dd, dt, dh, tsp)
197 struct timespec *tsp;
207 * Uninitialized field, return the epoch.
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
218 * If the year, month, and day from the last conversion are the
219 * same then use the saved value.
221 if (lastdosdate != dd) {
224 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
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",
237 days += months[month - 2];
238 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
239 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
241 tsp->tv_sec = seconds + lastseconds + utc_offset();
242 /* + daylight savings time correction */
243 tsp->tv_nsec = (dh % 100) * 10000000;
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.
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 */
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 */
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 */
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 */
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.
409 * Convert a DOS filename to a unix filename. And, return the number of
410 * characters in the resulting unix filename excluding the terminating
414 dos2unixfn(dn, un, lower, pmp)
418 struct msdosfsmount *pmp;
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.
434 * Copy the name portion into the unix filename string.
436 for (i = 8; i > 0 && *dn != ' ';) {
437 c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
448 * Now, if there is an extension then put in a period and copy in
454 for (i = 3; i > 0 && *dn != ' ';) {
455 c = dos2unixchr((const u_char **)&dn, &i, lower, pmp);
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.
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
481 unix2dosfn(un, dn, unlen, gen, pmp)
486 struct msdosfsmount *pmp;
491 const u_char *cp, *dp, *dp1;
492 u_char gentext[6], *wcp;
496 * Fill the dos filename string with blanks. These are DOS's pad
499 for (i = 0; i < 11; i++)
504 * The filenames "." and ".." are handled specially, since they
505 * don't follow dos filename rules.
507 if (un[0] == '.' && unlen == 1) {
511 if (un[0] == '.' && un[1] == '.' && unlen == 2) {
518 * Filenames with only blanks and dots are not allowed!
520 for (cp = un, i = unlen; --i >= 0; cp++)
521 if (*cp != ' ' && *cp != '.')
528 * Filenames with some characters are not allowed!
530 for (cp = un, i = unlen; i > 0;)
531 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
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.
544 for (cp = un + 1, i = unlen - 1; --i >= 0;) {
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.
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);
587 if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
602 for (dp = cp; *--dp == ' ' || *dp == '.';);
607 * Now convert the rest of the name
609 for (i = dp - un, j = 0; un < dp && j < 8; j++) {
610 c = unix2doschr(&un, &i, pmp);
626 if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
640 * If we didn't have any chars in filename,
647 * If there wasn't any char dropped,
648 * there is no place for generation numbers
657 * Now insert the generation number into the filename part
661 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
662 *--wcp = gen % 10 + '0';
667 for (i = 8; dn[--i] == ' ';);
669 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
670 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
672 * Correct posision to where insert the generation number
675 i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
678 while (wcp < gentext + sizeof(gentext))
682 * Tail of the filename should be space
690 * The first character cannot be E5,
691 * because that means a deleted entry
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
705 unix2winfn(un, unlen, wep, cnt, chksum, pmp)
708 struct winentry *wep;
711 struct msdosfsmount *pmp;
718 * Drop trailing blanks and dots
720 unlen = winLenFixup(un, unlen);
723 * Cut *un for this slot
725 unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
726 pmp->pm_flags, pmp->pm_u2w);
729 * Initialize winentry to some useful default
731 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
733 wep->weAttributes = ATTR_WIN95;
734 wep->weReserved1 = 0;
735 wep->weChksum = chksum;
736 wep->weReserved2 = 0;
739 * Now convert the filename parts
742 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
743 code = unix2winchr(&un, &unlen, 0, pmp);
749 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
750 code = unix2winchr(&un, &unlen, 0, pmp);
756 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
757 code = unix2winchr(&un, &unlen, 0, pmp);
770 * Compare our filename to the one in the Win95 entry
771 * Returns the checksum or -1 if no match
774 winChkName(un, unlen, chksum, pmp)
778 struct msdosfsmount *pmp;
783 struct dirent dirbuf;
786 * We alread have winentry in mbnambuf
788 if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen)
792 printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
798 * Compare the name parts
800 len = dirbuf.d_namlen;
804 for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
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.
810 c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
811 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
819 * Convert Win95 filename to dirbuf.
820 * Returns the checksum or -1 if impossible
823 win2unixfn(wep, chksum, pmp)
824 struct winentry *wep;
826 struct msdosfsmount *pmp;
829 u_int8_t *np, name[WIN_CHARS * 2 + 1];
833 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
834 || !(wep->weCnt&WIN_CNT))
838 * First compare checksums
840 if (wep->weCnt&WIN_LAST) {
841 chksum = wep->weChksum;
842 } else if (chksum != wep->weChksum)
848 * Convert the name parts
851 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
852 code = (cp[1] << 8) | cp[0];
856 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
862 code = win2unixchr(code, pmp);
870 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
871 code = (cp[1] << 8) | cp[0];
875 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
881 code = win2unixchr(code, pmp);
889 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
890 code = (cp[1] << 8) | cp[0];
894 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
900 code = win2unixchr(code, pmp);
909 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
914 * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
917 winChksum(struct direntry *dep)
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];
937 * Determine the number of slots necessary for Win95 names
940 winSlotCnt(un, unlen, pmp)
943 struct msdosfsmount *pmp;
946 char wn[WIN_MAXLEN * 2 + 1], *wnp;
948 unlen = winLenFixup(un, unlen);
950 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
951 wlen = WIN_MAXLEN * 2;
953 msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
956 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
959 if (unlen > WIN_MAXLEN)
961 return howmany(unlen, WIN_CHARS);
965 * Determine the number of bytes neccesary for Win95 names
968 winLenFixup(un, unlen)
972 for (un += unlen; unlen > 0; unlen--)
973 if (*--un != ' ' && *un != '.')
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
984 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
986 char *outp, outstr[outlen * weight + 1];
988 if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
991 msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
995 (*instr) += min(inlen, outlen);
996 return (inlen - min(inlen, outlen));
1000 * Convert DOS char to Local char
1003 dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1006 char *outp, outbuf[3];
1010 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1014 if (lower & (LCASE_BASE | LCASE_EXT))
1015 msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
1016 ilen, &outp, &olen, KICONV_LOWER);
1018 msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
1019 ilen, &outp, &olen);
1023 * return '?' if failed to convert
1033 wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1040 if (lower & (LCASE_BASE | LCASE_EXT))
1042 return ((u_int16_t)c);
1046 * Convert Local char to DOS char
1049 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
1052 char *up, *outp, unicode[3], outbuf[3];
1054 size_t len, ucslen, unixlen, olen;
1056 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1058 * to hide an invisible character, using a unicode filter
1063 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1064 ilen, &up, &ucslen);
1065 unixlen = len - *ilen;
1068 * cannot be converted
1077 * return magic number for ascii char
1089 * now convert using libiconv
1096 msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
1097 ilen, &outp, &olen, KICONV_FROM_UPPER);
1101 * cannot be converted, but has unicode char, should return magic number
1105 (*instr) += unixlen;
1111 wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1119 return ((u_int16_t)c);
1123 * Convert Windows char to Local char
1126 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
1128 u_char *inp, *outp, inbuf[3], outbuf[3];
1129 size_t ilen, olen, len;
1134 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1135 inbuf[0] = (u_char)(wc>>8);
1136 inbuf[1] = (u_char)wc;
1139 ilen = olen = len = 2;
1142 msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
1143 (char **)&outp, &olen);
1147 * return '?' if failed to convert
1156 wc |= (*(outp - len - 1) & 0xff) << (len << 3);
1167 * Convert Local char to Windows char
1170 unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1172 u_char *outp, outbuf[3];
1179 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1182 if (lower & (LCASE_BASE | LCASE_EXT))
1183 msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
1184 ilen, (char **)&outp, &olen,
1187 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1188 ilen, (char **)&outp, &olen);
1191 * return '0' if end of filename
1196 wc = (outbuf[0]<<8) | outbuf[1];
1203 if (lower & (LCASE_BASE | LCASE_EXT))
1210 * Initialize the temporary concatenation buffer (once) and mark it as
1211 * empty on subsequent calls.
1217 if (nambuf_ptr == NULL) {
1218 nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK);
1219 nambuf_ptr[MAXNAMLEN] = '\0';
1222 nambuf_last_id = -1;
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.
1235 mbnambuf_write(char *name, int id)
1240 KASSERT(nambuf_len == 0 || id == nambuf_last_id - 1,
1241 ("non-decreasing id, id %d last id %d", id, nambuf_last_id));
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);
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);
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;
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.
1269 mbnambuf_flush(struct dirent *dp)
1272 if (nambuf_len > sizeof(dp->d_name) - 1) {
1276 bcopy(nambuf_ptr, dp->d_name, nambuf_len);
1277 dp->d_name[nambuf_len] = '\0';
1278 dp->d_namlen = nambuf_len;
1281 return (dp->d_name);