1 /* $NetBSD: msdosfs_conv.c,v 1.6 2009/03/14 15:36:21 dsl Exp $ */
4 * Copyright (C) 1995, 1997 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1997 TooLs GmbH.
7 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 * You can do anything you want with this software, just don't say you wrote
38 * it, and don't remove this notice.
40 * This software is provided "as is".
42 * The author supplies this software to be publicly redistributed on the
43 * understanding that the author is not responsible for the correct
44 * functioning of this software in any circumstances and is not liable for
45 * any damages caused by this software.
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_conv.c,v 1.6 2009/03/14 15:36:21 dsl Exp $");
54 * System include files.
56 #include <sys/param.h>
57 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/dirent.h>
61 #include <sys/vnode.h>
64 * MSDOSFS include files.
66 #include <fs/msdosfs/direntry.h>
67 #include <fs/msdosfs/denode.h>
70 * Days in each month in a regular year.
72 u_short
const regyear
[] = {
73 31, 28, 31, 30, 31, 30,
74 31, 31, 30, 31, 30, 31
78 * Days in each month in a leap year.
80 u_short
const leapyear
[] = {
81 31, 29, 31, 30, 31, 30,
82 31, 31, 30, 31, 30, 31
86 * Variables used to remember parts of the last time conversion. Maybe we
87 * can avoid a full conversion.
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.
99 unix2dostime(const struct timespec
*tsp
, int gmtoff
, u_int16_t
*ddp
, u_int16_t
*dtp
, u_int8_t
*dhp
)
106 const 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
+ gmtoff
; /* time zone correction */
116 lastdtime
= (((t
/ 2) % 30) << DT_2SECONDS_SHIFT
)
117 + (((t
/ 60) % 60) << DT_MINUTES_SHIFT
)
118 + (((t
/ 3600) % 24) << DT_HOURS_SHIFT
);
121 * If the number of days since 1970 is the same as the last
122 * time we did the computation then skip all this leap year
125 days
= t
/ (24 * 60 * 60);
126 if (days
!= lastday
) {
128 for (year
= 1970;; year
++) {
129 inc
= year
& 0x03 ? 365 : 366;
134 months
= year
& 0x03 ? regyear
: leapyear
;
135 for (month
= 0; month
< 12; month
++) {
136 if (days
< months
[month
])
138 days
-= months
[month
];
140 lastddate
= ((days
+ 1) << DD_DAY_SHIFT
)
141 + ((month
+ 1) << DD_MONTH_SHIFT
);
143 * Remember dos's idea of time is relative to 1980.
144 * unix's is relative to 1970. If somehow we get a
145 * time before 1980 then don't give totally crazy
149 lastddate
+= (year
- 1980) << DD_YEAR_SHIFT
;
155 *dhp
= (tsp
->tv_sec
& 1) * 100 + tsp
->tv_nsec
/ 10000000;
161 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
162 * interval there were 8 regular years and 2 leap years.
164 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
170 * Convert from dos' idea of time to unix'. This will probably only be
171 * called from the stat(), and fstat() system calls and so probably need
172 * not be too efficient.
175 dos2unixtime(u_int dd
, u_int dt
, u_int dh
, int gmtoff
, struct timespec
*tsp
)
181 const u_short
*months
;
185 * Uninitialized field, return the epoch.
191 seconds
= ((dt
& DT_2SECONDS_MASK
) >> DT_2SECONDS_SHIFT
) * 2
192 + ((dt
& DT_MINUTES_MASK
) >> DT_MINUTES_SHIFT
) * 60
193 + ((dt
& DT_HOURS_MASK
) >> DT_HOURS_SHIFT
) * 3600
196 * If the year, month, and day from the last conversion are the
197 * same then use the saved value.
199 if (lastdosdate
!= dd
) {
202 year
= (dd
& DD_YEAR_MASK
) >> DD_YEAR_SHIFT
;
203 for (y
= 0; y
< year
; y
++)
204 days
+= y
& 0x03 ? 365 : 366;
205 months
= year
& 0x03 ? regyear
: leapyear
;
207 * Prevent going from 0 to 0xffffffff in the following
210 month
= (dd
& DD_MONTH_MASK
) >> DD_MONTH_SHIFT
;
212 printf("dos2unixtime(): month value out of range (%ld)\n",
216 for (m
= 0; m
< month
- 1; m
++)
218 days
+= ((dd
& DD_DAY_MASK
) >> DD_DAY_SHIFT
) - 1;
219 lastseconds
= (days
* 24 * 60 * 60) + SECONDSTO1980
;
221 tsp
->tv_sec
= seconds
+ lastseconds
;
222 tsp
->tv_sec
-= gmtoff
; /* time zone correction */
223 tsp
->tv_nsec
= (dh
% 100) * 10000000;
228 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
229 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
230 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
231 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
232 0, '!', 0, '#', '$', '%', '&', '\'', /* 20-27 */
233 '(', ')', 0, '+', 0, '-', 0, 0, /* 28-2f */
234 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
235 '8', '9', 0, 0, 0, 0, 0, 0, /* 38-3f */
236 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */
237 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */
238 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */
239 'X', 'Y', 'Z', 0, 0, 0, '^', '_', /* 58-5f */
240 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 60-67 */
241 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 68-6f */
242 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 70-77 */
243 'X', 'Y', 'Z', '{', 0, '}', '~', 0, /* 78-7f */
244 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
245 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
246 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
247 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
248 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
249 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
250 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
251 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
252 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
253 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
254 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
255 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
256 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
257 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
258 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
259 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
264 '?', '?', '?', '?', '?', '?', '?', '?', /* 00-07 */
265 '?', '?', '?', '?', '?', '?', '?', '?', /* 08-0f */
266 '?', '?', '?', '?', '?', '?', '?', '?', /* 10-17 */
267 '?', '?', '?', '?', '?', '?', '?', '?', /* 18-1f */
268 ' ', '!', '"', '#', '$', '%', '&', '\'', /* 20-27 */
269 '(', ')', '*', '+', ',', '-', '.', '/', /* 28-2f */
270 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
271 '8', '9', ':', ';', '<', '=', '>', '?', /* 38-3f */
272 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */
273 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */
274 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */
275 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', /* 58-5f */
276 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 60-67 */
277 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 68-6f */
278 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 70-77 */
279 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, /* 78-7f */
280 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
281 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
282 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
283 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, '?', /* 98-9f */
284 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
285 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
286 '?', '?', '?', '?', '?', 0xc1, 0xc2, 0xc0, /* b0-b7 */
287 0xa9, '?', '?', '?', '?', 0xa2, 0xa5, '?', /* b8-bf */
288 '?', '?', '?', '?', '?', '?', 0xe3, 0xc3, /* c0-c7 */
289 '?', '?', '?', '?', '?', '?', '?', 0xa4, /* c8-cf */
290 0xf0, 0xd0, 0xca, 0xcb, 0xc8, '?', 0xcd, 0xce, /* d0-d7 */
291 0xcf, '?', '?', '?', '?', 0xa6, 0xcc, '?', /* d8-df */
292 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
293 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
294 0xad, 0xb1, '?', 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
295 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, '?', '?', /* f8-ff */
300 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
301 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
302 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
303 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
304 ' ', '!', '"', '#', '$', '%', '&', '\'', /* 20-27 */
305 '(', ')', '*', '+', ',', '-', '.', '/', /* 28-2f */
306 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
307 '8', '9', ':', ';', '<', '=', '>', '?', /* 38-3f */
308 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 40-47 */
309 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 48-4f */
310 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 50-57 */
311 'x', 'y', 'z', '[', '\\', ']', '^', '_', /* 58-5f */
312 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 60-67 */
313 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 68-6f */
314 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 70-77 */
315 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, /* 78-7f */
316 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
317 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
318 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
319 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
320 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
321 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
322 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
323 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
324 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
325 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
326 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
327 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
328 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
329 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
330 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
331 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
335 * DOS filenames are made of 2 parts, the name part and the extension part.
336 * The name part is 8 characters long and the extension part is 3
337 * characters long. They may contain trailing blanks if the name or
338 * extension are not long enough to fill their respective fields.
342 * Convert a DOS filename to a unix filename. And, return the number of
343 * characters in the resulting unix filename excluding the terminating
347 dos2unixfn(u_char dn
[11], u_char
*un
, int lower
)
354 * If first char of the filename is SLOT_E5 (0x05), then the real
355 * first char of the filename should be 0xe5. But, they couldn't
356 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
357 * directory slot. Another dos quirk.
363 *un
++ = lower
? u2l
[c
] : c
;
366 * Copy the rest into the unix filename string, ignoring
370 for (j
=7; (j
>= 0) && (dn
[j
] == ' '); j
--)
373 for (i
= 1; i
<= j
; i
++) {
375 *un
++ = lower
? u2l
[c
] : c
;
381 * Now, if there is an extension then put in a period and copy in
387 for (i
= 0; i
< 3 && *dn
!= ' '; i
++) {
389 *un
++ = lower
? u2l
[c
] : c
;
399 * Convert a unix filename to a DOS filename according to Win95 rules.
400 * If applicable and gen is not 0, it is inserted into the converted
401 * filename as a generation number.
403 * 0 if name couldn't be converted
404 * 1 if the converted name is the same as the original
405 * (no long filename entry necessary for Win95)
406 * 2 if conversion was successful
407 * 3 if conversion was successful and generation number was inserted
410 unix2dosfn(const u_char
*un
, u_char dn
[12], int unlen
, u_int gen
)
414 const u_char
*cp
, *dp
, *dp1
;
415 u_char gentext
[6], *wcp
;
419 * Fill the dos filename string with blanks. These are DOS's pad
422 for (i
= 0; i
< 11; i
++)
427 * The filenames "." and ".." are handled specially, since they
428 * don't follow dos filename rules.
430 if (un
[0] == '.' && unlen
== 1) {
434 if (un
[0] == '.' && un
[1] == '.' && unlen
== 2) {
441 * Filenames with only blanks and dots are not allowed!
443 for (cp
= un
, i
= unlen
; --i
>= 0; cp
++)
444 if (*cp
!= ' ' && *cp
!= '.')
450 * Now find the extension
451 * Note: dot as first char doesn't start extension
452 * and trailing dots and blanks are ignored
455 for (cp
= un
+ 1, i
= unlen
- 1; --i
>= 0;) {
478 l
= unlen
- (dp
- un
);
479 for (i
= 0, j
= 8; i
< l
&& j
< 11; i
++, j
++) {
480 if (dp
[i
] != (dn
[j
] = unix2dos
[dp
[i
]])
492 for (dp
= cp
; *--dp
== ' ' || *dp
== '.';);
496 shortlen
= (dp
- un
) <= 8;
499 * Now convert the rest of the name
501 for (i
= j
= 0; un
< dp
&& j
< 8; i
++, j
++, un
++) {
502 if ((*un
== ' ') && shortlen
)
505 dn
[j
] = unix2dos
[*un
];
517 * If we didn't have any chars in filename,
524 * The first character cannot be E5,
525 * because that means a deleted entry
531 * If there wasn't any char dropped,
532 * there is no place for generation numbers
541 * Now insert the generation number into the filename part
543 for (wcp
= gentext
+ sizeof(gentext
); wcp
> gentext
&& gen
; gen
/= 10)
544 *--wcp
= gen
% 10 + '0';
547 for (i
= 8; dn
[--i
] == ' ';);
549 if (gentext
+ sizeof(gentext
) - wcp
+ 1 > 8 - i
)
550 i
= 8 - (gentext
+ sizeof(gentext
) - wcp
+ 1);
552 while (wcp
< gentext
+ sizeof(gentext
))
558 * Create a Win95 long name directory entry
559 * Note: assumes that the filename is valid,
560 * i.e. doesn't consist solely of blanks and dots
563 unix2winfn(const u_char
*un
, int unlen
, struct winentry
*wep
, int cnt
, int chksum
)
570 * Drop trailing blanks and dots
572 for (cp
= un
+ unlen
; *--cp
== ' ' || *cp
== '.'; unlen
--);
574 un
+= (cnt
- 1) * WIN_CHARS
;
575 unlen
-= (cnt
- 1) * WIN_CHARS
;
578 * Initialize winentry to some useful default
580 for (wcp
= (u_int8_t
*)wep
, i
= sizeof(*wep
); --i
>= 0; *wcp
++ = 0xff);
582 wep
->weAttributes
= ATTR_WIN95
;
583 wep
->weReserved1
= 0;
584 wep
->weChksum
= chksum
;
585 wep
->weReserved2
= 0;
588 * Now convert the filename parts
590 for (wcp
= wep
->wePart1
, i
= sizeof(wep
->wePart1
)/2; --i
>= 0;) {
596 for (wcp
= wep
->wePart2
, i
= sizeof(wep
->wePart2
)/2; --i
>= 0;) {
602 for (wcp
= wep
->wePart3
, i
= sizeof(wep
->wePart3
)/2; --i
>= 0;) {
609 wep
->weCnt
|= WIN_LAST
;
615 wep
->weCnt
|= WIN_LAST
;
620 * Compare our filename to the one in the Win95 entry
621 * Returns the checksum or -1 if no match
624 winChkName(const u_char
*un
, int unlen
, struct winentry
*wep
, int chksum
)
630 * First compare checksums
632 if (wep
->weCnt
&WIN_LAST
)
633 chksum
= wep
->weChksum
;
634 else if (chksum
!= wep
->weChksum
)
640 * Offset of this entry
642 i
= ((wep
->weCnt
&WIN_CNT
) - 1) * WIN_CHARS
;
644 if ((unlen
-= i
) < 0)
648 * Ignore redundant winentries (those with only \0\0 on start in them).
649 * An appearance of such entry is a bug; unknown if in NetBSD msdosfs
653 if (wep
->wePart1
[0] == '\0' && wep
->wePart1
[1] == '\0')
659 if ((wep
->weCnt
&WIN_LAST
) && unlen
> WIN_CHARS
)
663 * Compare the name parts
665 for (cp
= wep
->wePart1
, i
= sizeof(wep
->wePart1
)/2; --i
>= 0;) {
671 if (u2l
[*cp
++] != u2l
[*un
++] || *cp
++)
674 for (cp
= wep
->wePart2
, i
= sizeof(wep
->wePart2
)/2; --i
>= 0;) {
680 if (u2l
[*cp
++] != u2l
[*un
++] || *cp
++)
683 for (cp
= wep
->wePart3
, i
= sizeof(wep
->wePart3
)/2; --i
>= 0;) {
689 if (u2l
[*cp
++] != u2l
[*un
++] || *cp
++)
696 * Convert Win95 filename to dirbuf.
697 * Returns the checksum or -1 if impossible
700 win2unixfn(struct winentry
*wep
, struct dirent
*dp
, int chksum
)
703 u_int8_t
*np
, *ep
= dp
->d_name
+ WIN_MAXLEN
;
706 if ((wep
->weCnt
&WIN_CNT
) > howmany(WIN_MAXLEN
, WIN_CHARS
)
707 || !(wep
->weCnt
&WIN_CNT
))
711 * First compare checksums
713 if (wep
->weCnt
&WIN_LAST
) {
714 chksum
= wep
->weChksum
;
716 * This works even though d_namlen is one byte!
718 dp
->d_namlen
= (wep
->weCnt
&WIN_CNT
) * WIN_CHARS
;
719 } else if (chksum
!= wep
->weChksum
)
725 * Offset of this entry
727 i
= ((wep
->weCnt
&WIN_CNT
) - 1) * WIN_CHARS
;
728 np
= (u_int8_t
*)dp
->d_name
+ i
;
731 * Convert the name parts
733 for (cp
= wep
->wePart1
, i
= sizeof(wep
->wePart1
)/2; --i
>= 0;) {
734 switch (*np
++ = *cp
++) {
736 dp
->d_namlen
-= sizeof(wep
->wePart2
)/2
737 + sizeof(wep
->wePart3
)/2 + i
+ 1;
744 * The size comparison should result in the compiler
745 * optimizing the whole if away
747 if (WIN_MAXLEN
% WIN_CHARS
< sizeof(wep
->wePart1
) / 2
755 for (cp
= wep
->wePart2
, i
= sizeof(wep
->wePart2
)/2; --i
>= 0;) {
756 switch (*np
++ = *cp
++) {
758 dp
->d_namlen
-= sizeof(wep
->wePart3
)/2 + i
+ 1;
765 * The size comparisons should be optimized away
767 if (WIN_MAXLEN
% WIN_CHARS
>= sizeof(wep
->wePart1
) / 2
768 && WIN_MAXLEN
% WIN_CHARS
< (sizeof(wep
->wePart1
) + sizeof(wep
->wePart2
)) / 2
776 for (cp
= wep
->wePart3
, i
= sizeof(wep
->wePart3
)/2; --i
>= 0;) {
777 switch (*np
++ = *cp
++) {
779 dp
->d_namlen
-= i
+ 1;
788 if (WIN_MAXLEN
% WIN_CHARS
>= (sizeof(wep
->wePart1
) + sizeof(wep
->wePart2
)) / 2
800 * Compute the checksum of a DOS filename for Win95 use
803 winChksum(u_int8_t
*name
)
808 for (s
= 0, i
= 11; --i
>= 0; s
+= *name
++)
809 s
= (s
<< 7)|(s
>> 1);
814 * Determine the number of slots necessary for Win95 names
817 winSlotCnt(const u_char
*un
, int unlen
)
819 for (un
+= unlen
; unlen
> 0; unlen
--)
820 if (*--un
!= ' ' && *un
!= '.')
822 if (unlen
> WIN_MAXLEN
)
824 return howmany(unlen
, WIN_CHARS
);