2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 * Copyright (C) 1994, 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 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/endian.h>
60 #include <fs/msdosfs/bpb.h>
61 #include "msdos/direntry.h"
62 #include <fs/msdosfs/msdosfsmount.h>
67 static int char8ucs2str(const uint8_t *in, int n, uint16_t *out, int m);
68 static void ucs2pad(uint16_t *buf, int len, int size);
69 static int char8match(uint16_t *w1, uint16_t *w2, int n);
71 static const u_char unix2dos[256] = {
72 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
73 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
74 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
75 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
76 0, '!', 0, '#', '$', '%', '&', '\'', /* 20-27 */
77 '(', ')', 0, '+', 0, '-', 0, 0, /* 28-2f */
78 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
79 '8', '9', 0, 0, 0, 0, 0, 0, /* 38-3f */
80 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */
81 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */
82 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */
83 'X', 'Y', 'Z', 0, 0, 0, '^', '_', /* 58-5f */
84 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 60-67 */
85 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 68-6f */
86 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 70-77 */
87 'X', 'Y', 'Z', '{', 0, '}', '~', 0, /* 78-7f */
88 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
89 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
90 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
91 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
92 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
93 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
94 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
95 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
96 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
97 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
98 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
99 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
100 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
101 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
102 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
103 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
106 static const u_char u2l[256] = {
107 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
108 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
109 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
110 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
111 ' ', '!', '"', '#', '$', '%', '&', '\'', /* 20-27 */
112 '(', ')', '*', '+', ',', '-', '.', '/', /* 28-2f */
113 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
114 '8', '9', ':', ';', '<', '=', '>', '?', /* 38-3f */
115 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 40-47 */
116 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 48-4f */
117 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 50-57 */
118 'x', 'y', 'z', '[', '\\', ']', '^', '_', /* 58-5f */
119 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 60-67 */
120 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 68-6f */
121 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 70-77 */
122 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, /* 78-7f */
123 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
124 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
125 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
126 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
127 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
128 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
129 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
130 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
131 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
132 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
133 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
134 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
135 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
136 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
137 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
138 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
142 * Determine the number of slots necessary for Win95 names
145 winSlotCnt(const u_char *un, size_t unlen)
150 * Drop trailing blanks and dots
152 for (cp = un + unlen; unlen > 0; unlen--)
153 if (*--cp != ' ' && *cp != '.')
156 return howmany(unlen, WIN_CHARS);
160 * Compare our filename to the one in the Win95 entry
161 * Returns the checksum or -1 if no match
164 winChkName(const u_char *un, size_t unlen, struct winentry *wep, int chksum)
166 uint16_t wn[WIN_MAXLEN], *p;
167 uint16_t buf[WIN_CHARS];
171 * First compare checksums
173 if (wep->weCnt & WIN_LAST)
174 chksum = wep->weChksum;
175 else if (chksum != wep->weChksum)
181 * Offset of this entry
183 i = ((wep->weCnt & WIN_CNT) - 1) * WIN_CHARS;
186 * Translate UNIX name to ucs-2
188 len = char8ucs2str(un, unlen, wn, WIN_MAXLEN);
189 ucs2pad(wn, len, WIN_MAXLEN);
193 if ((wep->weCnt & WIN_LAST) && (len - i > WIN_CHARS))
197 * Fetch name segment from directory entry
200 memcpy(p, wep->wePart1, sizeof(wep->wePart1));
201 p += sizeof(wep->wePart1) / sizeof(*p);
202 memcpy(p, wep->wePart2, sizeof(wep->wePart2));
203 p += sizeof(wep->wePart2) / sizeof(*p);
204 memcpy(p, wep->wePart3, sizeof(wep->wePart3));
207 * And compare name segment
209 if (!(char8match(&wn[i], buf, WIN_CHARS)))
216 * Compute the checksum of a DOS filename for Win95 use
219 winChksum(uint8_t *name)
224 for (s = 0, i = 11; --i >= 0; s += *name++)
225 s = (s << 7) | (s >> 1);
230 * Create a Win95 long name directory entry
231 * Note: assumes that the filename is valid,
232 * i.e. doesn't consist solely of blanks and dots
235 unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt,
238 uint16_t wn[WIN_MAXLEN], *p;
243 * Drop trailing blanks and dots
245 for (cp = un + unlen; unlen > 0; unlen--)
246 if (*--cp != ' ' && *cp != '.')
250 * Offset of this entry
252 i = (cnt - 1) * WIN_CHARS;
255 * Translate UNIX name to ucs-2
257 len = char8ucs2str(un, unlen, wn, WIN_MAXLEN);
258 ucs2pad(wn, len, WIN_MAXLEN);
261 * Initialize winentry to some useful default
263 memset(wep, 0xff, sizeof(*wep));
265 wep->weAttributes = ATTR_WIN95;
266 wep->weReserved1 = 0;
267 wep->weChksum = chksum;
268 wep->weReserved2 = 0;
271 * Store name segment into directory entry
274 memcpy(wep->wePart1, p, sizeof(wep->wePart1));
275 p += sizeof(wep->wePart1) / sizeof(*p);
276 memcpy(wep->wePart2, p, sizeof(wep->wePart2));
277 p += sizeof(wep->wePart2) / sizeof(*p);
278 memcpy(wep->wePart3, p, sizeof(wep->wePart3));
280 if (len > i + WIN_CHARS)
283 wep->weCnt |= WIN_LAST;
288 * Convert a unix filename to a DOS filename according to Win95 rules.
289 * If applicable and gen is not 0, it is inserted into the converted
290 * filename as a generation number.
292 * 0 if name couldn't be converted
293 * 1 if the converted name is the same as the original
294 * (no long filename entry necessary for Win95)
295 * 2 if conversion was successful
296 * 3 if conversion was successful and generation number was inserted
299 unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen)
303 const u_char *cp, *dp, *dp1;
304 u_char gentext[6], *wcp;
308 * Fill the dos filename string with blanks. These are DOS's pad
311 for (i = 0; i < 11; i++)
316 * The filenames "." and ".." are handled specially, since they
317 * don't follow dos filename rules.
319 if (un[0] == '.' && unlen == 1) {
323 if (un[0] == '.' && un[1] == '.' && unlen == 2) {
330 * Filenames with only blanks and dots are not allowed!
332 for (cp = un, i = unlen; --i >= 0; cp++)
333 if (*cp != ' ' && *cp != '.')
339 * Now find the extension
340 * Note: dot as first char doesn't start extension
341 * and trailing dots and blanks are ignored
344 for (cp = un + 1, i = unlen - 1; --i >= 0;) {
367 l = unlen - (dp - un);
368 for (i = 0, j = 8; i < l && j < 11; i++, j++) {
369 if (dp[i] != (dn[j] = unix2dos[dp[i]])
381 for (dp = cp; *--dp == ' ' || *dp == '.';);
385 shortlen = (dp - un) <= 8;
388 * Now convert the rest of the name
390 for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
391 if ((*un == ' ') && shortlen)
394 dn[j] = unix2dos[*un];
406 * If we didn't have any chars in filename,
413 * The first character cannot be E5,
414 * because that means a deleted entry
420 * If there wasn't any char dropped,
421 * there is no place for generation numbers
430 * Now insert the generation number into the filename part
432 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
433 *--wcp = gen % 10 + '0';
436 for (i = 8; dn[--i] == ' ';);
438 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
439 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
441 while (wcp < gentext + sizeof(gentext))
447 * Convert 8bit character string into UCS-2 string
448 * return total number of output chacters
451 char8ucs2str(const uint8_t *in, int n, uint16_t *out, int m)
456 while (n > 0 && in[0] != 0) {
460 p[0] = htole16(in[0]);
471 ucs2pad(uint16_t *buf, int len, int size)
481 * Compare two 8bit char conversions case-insensitive
483 * uses the DOS case folding table
486 char8match(uint16_t *w1, uint16_t *w2, int n)
493 if (u1 == 0 || u2 == 0)
495 if (u1 > 255 || u2 > 255)