HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_conv.c
blob72c9915d2ba53bb136886a880a9ba5603f11584b
1 /* $FreeBSD: src/sys/msdosfs/msdosfs_conv.c,v 1.29.2.1 2002/11/08 22:01:22 semenu Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_conv.c,v 1.7 2006/12/23 00:41:29 swildner Exp $ */
3 /* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */
5 /*-
6 * Copyright (C) 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1995, 1997 TooLs GmbH.
8 * All rights reserved.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
49 * October 1992
53 * System include files.
55 #include <sys/param.h>
56 #include <sys/time.h>
57 #include <sys/kernel.h> /* defines tz */
58 #include <sys/systm.h>
59 #include <machine/clock.h>
60 #include <sys/dirent.h>
63 * MSDOSFS include files.
65 #include "direntry.h"
68 * Total number of days that have passed for each month in a regular year.
70 static u_short regyear[] = {
71 31, 59, 90, 120, 151, 181,
72 212, 243, 273, 304, 334, 365
76 * Total number of days that have passed for each month in a leap year.
78 static u_short leapyear[] = {
79 31, 60, 91, 121, 152, 182,
80 213, 244, 274, 305, 335, 366
84 * Variables used to remember parts of the last time conversion. Maybe we
85 * can avoid a full conversion.
87 static u_long lasttime;
88 static u_long lastday;
89 static u_short lastddate;
90 static u_short lastdtime;
92 static __inline u_int8_t find_lcode (u_int16_t code, u_int16_t *u2w);
95 * Convert the unix version of time to dos's idea of time to be used in
96 * file timestamps. The passed in unix time is assumed to be in GMT.
98 void
99 unix2dostime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp)
101 u_long t;
102 u_long days;
103 u_long inc;
104 u_long year;
105 u_long month;
106 u_short *months;
109 * If the time from the last conversion is the same as now, then
110 * skip the computations and use the saved result.
112 t = tsp->tv_sec - (tz.tz_minuteswest * 60)
113 - (wall_cmos_clock ? adjkerntz : 0);
114 /* - daylight savings time correction */
115 t &= ~1;
116 if (lasttime != t) {
117 lasttime = t;
118 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
119 + (((t / 60) % 60) << DT_MINUTES_SHIFT)
120 + (((t / 3600) % 24) << DT_HOURS_SHIFT);
123 * If the number of days since 1970 is the same as the last
124 * time we did the computation then skip all this leap year
125 * and month stuff.
127 days = t / (24 * 60 * 60);
128 if (days != lastday) {
129 lastday = days;
130 for (year = 1970;; year++) {
131 inc = year & 0x03 ? 365 : 366;
132 if (days < inc)
133 break;
134 days -= inc;
136 months = year & 0x03 ? regyear : leapyear;
137 for (month = 0; days >= months[month]; month++)
139 if (month > 0)
140 days -= months[month - 1];
141 lastddate = ((days + 1) << DD_DAY_SHIFT)
142 + ((month + 1) << DD_MONTH_SHIFT);
144 * Remember dos's idea of time is relative to 1980.
145 * unix's is relative to 1970. If somehow we get a
146 * time before 1980 then don't give totally crazy
147 * results.
149 if (year > 1980)
150 lastddate += (year - 1980) << DD_YEAR_SHIFT;
153 if (dtp)
154 *dtp = lastdtime;
155 if (dhp)
156 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
158 *ddp = lastddate;
162 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
163 * interval there were 8 regular years and 2 leap years.
165 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
167 static u_short lastdosdate;
168 static u_long lastseconds;
171 * Convert from dos' idea of time to unix'. This will probably only be
172 * called from the stat(), and fstat() system calls and so probably need
173 * not be too efficient.
175 void
176 dos2unixtime(u_int dd, u_int dt, u_int dh, struct timespec *tsp)
178 u_long seconds;
179 u_long month;
180 u_long year;
181 u_long days;
182 u_short *months;
184 if (dd == 0) {
186 * Uninitialized field, return the epoch.
188 tsp->tv_sec = 0;
189 tsp->tv_nsec = 0;
190 return;
192 seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
193 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
194 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
195 + dh / 100;
197 * If the year, month, and day from the last conversion are the
198 * same then use the saved value.
200 if (lastdosdate != dd) {
201 lastdosdate = dd;
202 days = 0;
203 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
204 days = year * 365;
205 days += year / 4 + 1; /* add in leap days */
206 if ((year & 0x03) == 0)
207 days--; /* if year is a leap year */
208 months = year & 0x03 ? regyear : leapyear;
209 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
210 if (month < 1 || month > 12) {
211 kprintf("dos2unixtime(): month value out of range (%ld)\n",
212 month);
213 month = 1;
215 if (month > 1)
216 days += months[month - 2];
217 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
218 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
220 tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
221 + adjkerntz;
222 /* + daylight savings time correction */
223 tsp->tv_nsec = (dh % 100) * 10000000;
227 * 0 - character disallowed in long file name.
228 * 1 - character should be replaced by '_' in DOS file name,
229 * and generation number inserted.
230 * 2 - character ('.' and ' ') should be skipped in DOS file name,
231 * and generation number inserted.
233 static u_char
234 unix2dos[256] = {
235 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
236 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
237 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
238 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
239 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
240 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */
241 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
242 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */
243 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
244 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
245 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
246 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */
247 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */
248 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */
249 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */
250 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */
251 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
252 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
253 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
254 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
255 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
256 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
257 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
258 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
259 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
260 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
261 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
262 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
263 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
264 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
265 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
266 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
269 static u_char
270 dos2unix[256] = {
271 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
272 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
273 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
274 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
275 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
276 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
277 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
278 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
279 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
280 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
281 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
282 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
283 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
284 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
285 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
286 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
287 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
288 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
289 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
290 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */
291 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
292 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
293 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */
294 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */
295 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */
296 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */
297 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */
298 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */
299 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
300 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
301 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
302 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */
305 static u_char
306 u2l[256] = {
307 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
308 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
309 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
310 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
311 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
312 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
313 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
314 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
315 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
316 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
317 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
318 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
319 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
320 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
321 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
322 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
323 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
324 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
325 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
326 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
327 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
328 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
329 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
330 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
331 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
332 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
333 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
334 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
335 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
336 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
337 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
338 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
341 static u_char
342 l2u[256] = {
343 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
344 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
345 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
346 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
347 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
348 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
349 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
350 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
351 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
352 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
353 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
354 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
355 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
356 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
357 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
358 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
359 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
360 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
361 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
362 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
363 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
364 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
365 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
366 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
367 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
368 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
369 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
370 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
371 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
372 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
373 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
374 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
378 * DOS filenames are made of 2 parts, the name part and the extension part.
379 * The name part is 8 characters long and the extension part is 3
380 * characters long. They may contain trailing blanks if the name or
381 * extension are not long enough to fill their respective fields.
385 * Convert a DOS filename to a unix filename. And, return the number of
386 * characters in the resulting unix filename excluding the terminating
387 * null.
390 dos2unixfn(u_char dn[11], u_char *un, int lower, int d2u_loaded, u_int8_t *d2u,
391 int ul_loaded, u_int8_t *ul)
393 int i;
394 int thislong = 1;
395 u_char c;
398 * If first char of the filename is SLOT_E5 (0x05), then the real
399 * first char of the filename should be 0xe5. But, they couldn't
400 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
401 * directory slot. Another dos quirk.
403 if (*dn == SLOT_E5)
404 c = d2u_loaded ? d2u[0xe5 & 0x7f] : dos2unix[0xe5];
405 else
406 c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
407 dos2unix[*dn];
408 *un++ = (lower & LCASE_BASE) ? (ul_loaded && (c & 0x80) ?
409 ul[c & 0x7f] : u2l[c]) : c;
410 dn++;
413 * Copy the name portion into the unix filename string.
415 for (i = 1; i < 8 && *dn != ' '; i++) {
416 c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
417 dos2unix[*dn];
418 dn++;
419 *un++ = (lower & LCASE_BASE) ? (ul_loaded && (c & 0x80) ?
420 ul[c & 0x7f] : u2l[c]) : c;
421 thislong++;
423 dn += 8 - i;
426 * Now, if there is an extension then put in a period and copy in
427 * the extension.
429 if (*dn != ' ') {
430 *un++ = '.';
431 thislong++;
432 for (i = 0; i < 3 && *dn != ' '; i++) {
433 c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] :
434 dos2unix[*dn];
435 dn++;
436 *un++ = (lower & LCASE_EXT) ? (ul_loaded && (c & 0x80) ?
437 ul[c & 0x7f] : u2l[c]) : c;
438 thislong++;
441 *un++ = 0;
443 return (thislong);
447 * Convert a unix filename to a DOS filename according to Win95 rules.
448 * If applicable and gen is not 0, it is inserted into the converted
449 * filename as a generation number.
450 * Returns
451 * 0 if name couldn't be converted
452 * 1 if the converted name is the same as the original
453 * (no long filename entry necessary for Win95)
454 * 2 if conversion was successful
455 * 3 if conversion was successful and generation number was inserted
458 unix2dosfn(const u_char *un, u_char dn[12], int unlen, u_int gen, int u2d_loaded,
459 u_int8_t *u2d, int lu_loaded, u_int8_t *lu)
461 int i, j, l;
462 int conv = 1;
463 const u_char *cp, *dp, *dp1;
464 u_char gentext[6], *wcp;
465 u_int8_t c;
466 #define U2D(c) (u2d_loaded && ((c) & 0x80) ? u2d[(c) & 0x7f] : unix2dos[c])
469 * Fill the dos filename string with blanks. These are DOS's pad
470 * characters.
472 for (i = 0; i < 11; i++)
473 dn[i] = ' ';
474 dn[11] = 0;
477 * The filenames "." and ".." are handled specially, since they
478 * don't follow dos filename rules.
480 if (un[0] == '.' && unlen == 1) {
481 dn[0] = '.';
482 return gen <= 1;
484 if (un[0] == '.' && un[1] == '.' && unlen == 2) {
485 dn[0] = '.';
486 dn[1] = '.';
487 return gen <= 1;
491 * Filenames with only blanks and dots are not allowed!
493 for (cp = un, i = unlen; --i >= 0; cp++)
494 if (*cp != ' ' && *cp != '.')
495 break;
496 if (i < 0)
497 return 0;
501 * Filenames with some characters are not allowed!
503 for (cp = un, i = unlen; --i >= 0; cp++)
504 if (U2D(*cp) == 0)
505 return 0;
508 * Now find the extension
509 * Note: dot as first char doesn't start extension
510 * and trailing dots and blanks are ignored
512 dp = dp1 = 0;
513 for (cp = un + 1, i = unlen - 1; --i >= 0;) {
514 switch (*cp++) {
515 case '.':
516 if (!dp1)
517 dp1 = cp;
518 break;
519 case ' ':
520 break;
521 default:
522 if (dp1)
523 dp = dp1;
524 dp1 = 0;
525 break;
530 * Now convert it
532 if (dp) {
533 if (dp1)
534 l = dp1 - dp;
535 else
536 l = unlen - (dp - un);
537 for (i = 0, j = 8; i < l && j < 11; i++, j++) {
538 c = dp[i];
539 c = lu_loaded && (c & 0x80) ?
540 lu[c & 0x7f] : l2u[c];
541 c = U2D(c);
542 if (dp[i] != (dn[j] = c)
543 && conv != 3)
544 conv = 2;
545 if (dn[j] == 1) {
546 conv = 3;
547 dn[j] = '_';
549 if (dn[j] == 2) {
550 conv = 3;
551 dn[j--] = ' ';
554 if (i < l)
555 conv = 3;
556 dp--;
557 } else {
558 for (dp = cp; *--dp == ' ' || *dp == '.';);
559 dp++;
563 * Now convert the rest of the name
565 for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
566 c = lu_loaded && (*un & 0x80) ?
567 lu[*un & 0x7f] : l2u[*un];
568 c = U2D(c);
569 if (*un != (dn[j] = c)
570 && conv != 3)
571 conv = 2;
572 if (dn[j] == 1) {
573 conv = 3;
574 dn[j] = '_';
576 if (dn[j] == 2) {
577 conv = 3;
578 dn[j--] = ' ';
581 if (un < dp)
582 conv = 3;
584 * If we didn't have any chars in filename,
585 * generate a default
587 if (!j)
588 dn[0] = '_';
591 * The first character cannot be E5,
592 * because that means a deleted entry
594 if (dn[0] == 0xe5)
595 dn[0] = SLOT_E5;
598 * If there wasn't any char dropped,
599 * there is no place for generation numbers
601 if (conv != 3) {
602 if (gen > 1)
603 return 0;
604 return conv;
608 * Now insert the generation number into the filename part
610 if (gen == 0)
611 return conv;
612 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
613 *--wcp = gen % 10 + '0';
614 if (gen)
615 return 0;
616 for (i = 8; dn[--i] == ' ';);
617 i++;
618 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
619 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
620 dn[i++] = '~';
621 while (wcp < gentext + sizeof(gentext))
622 dn[i++] = *wcp++;
623 return 3;
624 #undef U2D
628 * Create a Win95 long name directory entry
629 * Note: assumes that the filename is valid,
630 * i.e. doesn't consist solely of blanks and dots
633 unix2winfn(const u_char *un, int unlen, struct winentry *wep, int cnt, int chksum,
634 int table_loaded, u_int16_t *u2w)
636 const u_int8_t *cp;
637 u_int8_t *wcp;
638 int i;
639 u_int16_t code;
642 * Drop trailing blanks and dots
644 for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
646 un += (cnt - 1) * WIN_CHARS;
647 unlen -= (cnt - 1) * WIN_CHARS;
650 * Initialize winentry to some useful default
652 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
653 wep->weCnt = cnt;
654 wep->weAttributes = ATTR_WIN95;
655 wep->weReserved1 = 0;
656 wep->weChksum = chksum;
657 wep->weReserved2 = 0;
660 * Now convert the filename parts
662 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
663 if (--unlen < 0)
664 goto done;
665 if (table_loaded && (*un & 0x80)) {
666 code = u2w[*un++ & 0x7f];
667 *wcp++ = code;
668 *wcp++ = code >> 8;
669 } else {
670 *wcp++ = *un++;
671 *wcp++ = 0;
674 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
675 if (--unlen < 0)
676 goto done;
677 if (table_loaded && (*un & 0x80)) {
678 code = u2w[*un++ & 0x7f];
679 *wcp++ = code;
680 *wcp++ = code >> 8;
681 } else {
682 *wcp++ = *un++;
683 *wcp++ = 0;
686 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
687 if (--unlen < 0)
688 goto done;
689 if (table_loaded && (*un & 0x80)) {
690 code = u2w[*un++ & 0x7f];
691 *wcp++ = code;
692 *wcp++ = code >> 8;
693 } else {
694 *wcp++ = *un++;
695 *wcp++ = 0;
698 if (!unlen)
699 wep->weCnt |= WIN_LAST;
700 return unlen;
702 done:
703 *wcp++ = 0;
704 *wcp++ = 0;
705 wep->weCnt |= WIN_LAST;
706 return 0;
709 static __inline u_int8_t
710 find_lcode(u_int16_t code, u_int16_t *u2w)
712 int i;
714 for (i = 0; i < 128; i++)
715 if (u2w[i] == code)
716 return (i | 0x80);
717 return '?';
721 * Compare our filename to the one in the Win95 entry
722 * Returns the checksum or -1 if no match
725 winChkName(const u_char *un, int unlen, struct winentry *wep, int chksum,
726 int u2w_loaded, u_int16_t *u2w, int ul_loaded, u_int8_t *ul)
728 u_int8_t *cp;
729 int i;
730 u_int16_t code;
731 u_int8_t c1, c2;
734 * First compare checksums
736 if (wep->weCnt&WIN_LAST)
737 chksum = wep->weChksum;
738 else if (chksum != wep->weChksum)
739 chksum = -1;
740 if (chksum == -1)
741 return -1;
744 * Offset of this entry
746 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
747 un += i;
748 unlen -= i;
751 * unlen being zero must not be treated as length missmatch. It is
752 * possible if the entry is WIN_LAST and contains nothing but the
753 * terminating 0.
755 if (unlen < 0)
756 return -1;
757 if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
758 return -1;
761 * Compare the name parts
763 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
764 if (--unlen < 0) {
765 if (!*cp++ && !*cp)
766 return chksum;
767 return -1;
769 code = (cp[1] << 8) | cp[0];
770 if (code & 0xff80) {
771 if (u2w_loaded)
772 code = find_lcode(code, u2w);
773 else if (code & 0xff00)
774 code = '?';
776 c1 = ul_loaded && (code & 0x80) ?
777 ul[code & 0x7f] : u2l[code];
778 c2 = ul_loaded && (*un & 0x80) ?
779 ul[*un & 0x7f] : u2l[*un];
780 if (c1 != c2)
781 return -1;
782 cp += 2;
783 un++;
785 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
786 if (--unlen < 0) {
787 if (!*cp++ && !*cp)
788 return chksum;
789 return -1;
791 code = (cp[1] << 8) | cp[0];
792 if (code & 0xff80) {
793 if (u2w_loaded)
794 code = find_lcode(code, u2w);
795 else if (code & 0xff00)
796 code = '?';
798 c1 = ul_loaded && (code & 0x80) ?
799 ul[code & 0x7f] : u2l[code];
800 c2 = ul_loaded && (*un & 0x80) ?
801 ul[*un & 0x7f] : u2l[*un];
802 if (c1 != c2)
803 return -1;
804 cp += 2;
805 un++;
807 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
808 if (--unlen < 0) {
809 if (!*cp++ && !*cp)
810 return chksum;
811 return -1;
813 code = (cp[1] << 8) | cp[0];
814 if (code & 0xff80) {
815 if (u2w_loaded)
816 code = find_lcode(code, u2w);
817 else if (code & 0xff00)
818 code = '?';
820 c1 = ul_loaded && (code & 0x80) ?
821 ul[code & 0x7f] : u2l[code];
822 c2 = ul_loaded && (*un & 0x80) ?
823 ul[*un & 0x7f] : u2l[*un];
824 if (c1 != c2)
825 return -1;
826 cp += 2;
827 un++;
829 return chksum;
833 * Convert Win95 filename to dirbuf.
834 * Returns the checksum or -1 if impossible
837 win2unixfn(struct winentry *wep, char *d_name, uint16_t *d_namlen, int chksum, int table_loaded,
838 u_int16_t *u2w)
840 u_int8_t *cp;
841 u_int8_t *np, *ep = d_name + WIN_MAXLEN;
842 u_int16_t code;
843 int i;
845 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
846 || !(wep->weCnt&WIN_CNT))
847 return -1;
850 * First compare checksums
852 if (wep->weCnt&WIN_LAST) {
853 chksum = wep->weChksum;
855 * This works even though d_namlen is one byte!
857 *d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
858 } else if (chksum != wep->weChksum)
859 chksum = -1;
860 if (chksum == -1)
861 return -1;
864 * Offset of this entry
866 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
867 np = (u_int8_t *)d_name + i;
870 * Convert the name parts
872 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
873 code = (cp[1] << 8) | cp[0];
874 switch (code) {
875 case 0:
876 *np = '\0';
877 *d_namlen -= sizeof(wep->wePart2)/2
878 + sizeof(wep->wePart3)/2 + i + 1;
879 return chksum;
880 case '/':
881 *np = '\0';
882 return -1;
883 default:
884 if (code & 0xff80) {
885 if (table_loaded)
886 code = find_lcode(code, u2w);
887 else if (code & 0xff00)
888 code = '?';
890 *np++ = code;
891 break;
894 * The size comparison should result in the compiler
895 * optimizing the whole if away
897 if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
898 && np > ep) {
899 np[-1] = 0;
900 return -1;
902 cp += 2;
904 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
905 code = (cp[1] << 8) | cp[0];
906 switch (code) {
907 case 0:
908 *np = '\0';
909 *d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
910 return chksum;
911 case '/':
912 *np = '\0';
913 return -1;
914 default:
915 if (code & 0xff80) {
916 if (table_loaded)
917 code = find_lcode(code, u2w);
918 else if (code & 0xff00)
919 code = '?';
921 *np++ = code;
922 break;
925 * The size comparisons should be optimized away
927 if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
928 && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
929 && np > ep) {
930 np[-1] = 0;
931 return -1;
933 cp += 2;
935 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
936 code = (cp[1] << 8) | cp[0];
937 switch (code) {
938 case 0:
939 *np = '\0';
940 *d_namlen -= i + 1;
941 return chksum;
942 case '/':
943 *np = '\0';
944 return -1;
945 default:
946 if (code & 0xff80) {
947 if (table_loaded)
948 code = find_lcode(code, u2w);
949 else if (code & 0xff00)
950 code = '?';
952 *np++ = code;
953 break;
956 * See above
958 if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
959 && np > ep) {
960 np[-1] = 0;
961 return -1;
963 cp += 2;
965 return chksum;
969 * Compute the checksum of a DOS filename for Win95 use
971 u_int8_t
972 winChksum(u_int8_t *name)
974 int i;
975 u_int8_t s;
977 for (s = 0, i = 11; --i >= 0; s += *name++)
978 s = (s << 7)|(s >> 1);
979 return s;
983 * Determine the number of slots necessary for Win95 names
986 winSlotCnt(const u_char *un, int unlen)
988 unlen = winLenFixup(un, unlen);
989 if (unlen > WIN_MAXLEN)
990 return 0;
991 return howmany(unlen, WIN_CHARS);
995 * Determine the number of bytes neccesary for Win95 names
998 winLenFixup(const u_char *un, int unlen)
1000 for (un += unlen; unlen > 0; unlen--)
1001 if (*--un != ' ' && *un != '.')
1002 break;
1003 return unlen;