1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6  * All rights reserved.
7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
22  *
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.
33  */
34 /*-
35  * Written by Paul Popelka (paulp@uts.amdahl.com)
36  *
37  * You can do anything you want with this software, just don't say you wrote
38  * it, and don't remove this notice.
39  *
40  * This software is provided "as is".
41  *
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.
46  *
47  * October 1992
48  */
49 
50 #include <sys/param.h>
51 #include <sys/endian.h>
52 
53 #include <dirent.h>
54 #include <stdio.h>
55 #include <string.h>
56 
57 #include <fs/msdosfs/bpb.h>
58 #include "msdos/direntry.h"
59 #include <fs/msdosfs/msdosfsmount.h>
60 
61 #include "makefs.h"
62 #include "msdos.h"
63 
64 static int char8ucs2str(const uint8_t *in, int n, uint16_t *out, int m);
65 static void ucs2pad(uint16_t *buf, int len, int size);
66 static int char8match(uint16_t *w1, uint16_t *w2, int n);
67 
68 static const u_char unix2dos[256] = {
69 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
70 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
71 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
72 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
73 	0,    '!',  0,    '#',  '$',  '%',  '&',  '\'',	/* 20-27 */
74 	'(',  ')',  0,    '+',  0,    '-',  0,    0,	/* 28-2f */
75 	'0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
76 	'8',  '9',  0,    0,    0,    0,    0,    0,	/* 38-3f */
77 	'@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
78 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
79 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
80 	'X',  'Y',  'Z',  0,    0,    0,    '^',  '_',	/* 58-5f */
81 	'`',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 60-67 */
82 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 68-6f */
83 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 70-77 */
84 	'X',  'Y',  'Z',  '{',  0,    '}',  '~',  0,	/* 78-7f */
85 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
86 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
87 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
88 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
89 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
90 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
91 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
92 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
93 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
94 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
95 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
96 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
97 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
98 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
99 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
100 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
101 };
102 
103 static const u_char u2l[256] = {
104 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
105 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
106 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
107 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
108 	' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'', /* 20-27 */
109 	'(',  ')',  '*',  '+',  ',',  '-',  '.',  '/', /* 28-2f */
110 	'0',  '1',  '2',  '3',  '4',  '5',  '6',  '7', /* 30-37 */
111 	'8',  '9',  ':',  ';',  '<',  '=',  '>',  '?', /* 38-3f */
112 	'@',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 40-47 */
113 	'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 48-4f */
114 	'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 50-57 */
115 	'x',  'y',  'z',  '[', '\\',  ']',  '^',  '_', /* 58-5f */
116 	'`',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 60-67 */
117 	'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 68-6f */
118 	'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 70-77 */
119 	'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f, /* 78-7f */
120 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
121 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
122 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
123 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
124 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
125 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
126 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
127 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
128 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
129 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
130 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
131 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
132 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
133 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
134 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
135 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
136 };
137 
138 /*
139  * Determine the number of slots necessary for Win95 names
140  */
141 int
142 winSlotCnt(const u_char *un, size_t unlen)
143 {
144 	const u_char *cp;
145 
146 	/*
147 	 * Drop trailing blanks and dots
148 	 */
149 	for (cp = un + unlen; unlen > 0; unlen--)
150 		if (*--cp != ' ' && *cp != '.')
151 			break;
152 
153 	return howmany(unlen, WIN_CHARS);
154 }
155 
156 /*
157  * Compare our filename to the one in the Win95 entry
158  * Returns the checksum or -1 if no match
159  */
160 int
161 winChkName(const u_char *un, size_t unlen, struct winentry *wep, int chksum)
162 {
163 	uint16_t wn[WIN_MAXLEN], *p;
164 	uint16_t buf[WIN_CHARS];
165 	int i, len;
166 
167 	/*
168 	 * First compare checksums
169 	 */
170 	if (wep->weCnt & WIN_LAST)
171 		chksum = wep->weChksum;
172 	else if (chksum != wep->weChksum)
173 		chksum = -1;
174 	if (chksum == -1)
175 		return -1;
176 
177 	/*
178 	 * Offset of this entry
179 	 */
180 	i = ((wep->weCnt & WIN_CNT) - 1) * WIN_CHARS;
181 
182 	/*
183 	 * Translate UNIX name to ucs-2
184 	 */
185 	len = char8ucs2str(un, unlen, wn, WIN_MAXLEN);
186 	ucs2pad(wn, len, WIN_MAXLEN);
187 
188 	if (i >= len + 1)
189 		return -1;
190 	if ((wep->weCnt & WIN_LAST) && (len - i > WIN_CHARS))
191 		return -1;
192 
193 	/*
194 	 * Fetch name segment from directory entry
195 	 */
196 	p = &buf[0];
197 	memcpy(p, wep->wePart1, sizeof(wep->wePart1));
198 	p += sizeof(wep->wePart1) / sizeof(*p);
199 	memcpy(p, wep->wePart2, sizeof(wep->wePart2));
200 	p += sizeof(wep->wePart2) / sizeof(*p);
201 	memcpy(p, wep->wePart3, sizeof(wep->wePart3));
202 
203 	/*
204 	 * And compare name segment
205 	 */
206 	if (!(char8match(&wn[i], buf, WIN_CHARS)))
207 		return -1;
208 
209 	return chksum;
210 }
211 
212 /*
213  * Compute the checksum of a DOS filename for Win95 use
214  */
215 uint8_t
216 winChksum(uint8_t *name)
217 {
218 	int i;
219 	uint8_t s;
220 
221 	for (s = 0, i = 11; --i >= 0; s += *name++)
222 		s = (s << 7) | (s >> 1);
223 	return s;
224 }
225 
226 /*
227  * Create a Win95 long name directory entry
228  * Note: assumes that the filename is valid,
229  *	 i.e. doesn't consist solely of blanks and dots
230  */
231 int
232 unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt,
233     int chksum)
234 {
235 	uint16_t wn[WIN_MAXLEN], *p;
236 	int i, len;
237 	const u_char *cp;
238 
239 	/*
240 	 * Drop trailing blanks and dots
241 	 */
242 	for (cp = un + unlen; unlen > 0; unlen--)
243 		if (*--cp != ' ' && *cp != '.')
244 			break;
245 
246 	/*
247 	 * Offset of this entry
248 	 */
249 	i = (cnt - 1) * WIN_CHARS;
250 
251 	/*
252 	 * Translate UNIX name to ucs-2
253 	 */
254 	len = char8ucs2str(un, unlen, wn, WIN_MAXLEN);
255 	ucs2pad(wn, len, WIN_MAXLEN);
256 
257 	/*
258 	 * Initialize winentry to some useful default
259 	 */
260 	memset(wep, 0xff, sizeof(*wep));
261 	wep->weCnt = cnt;
262 	wep->weAttributes = ATTR_WIN95;
263 	wep->weReserved1 = 0;
264 	wep->weChksum = chksum;
265 	wep->weReserved2 = 0;
266 
267 	/*
268 	 * Store name segment into directory entry
269 	 */
270 	p = &wn[i];
271 	memcpy(wep->wePart1, p, sizeof(wep->wePart1));
272 	p += sizeof(wep->wePart1) / sizeof(*p);
273 	memcpy(wep->wePart2, p, sizeof(wep->wePart2));
274 	p += sizeof(wep->wePart2) / sizeof(*p);
275 	memcpy(wep->wePart3, p, sizeof(wep->wePart3));
276 
277 	if (len > i + WIN_CHARS)
278 		return 1;
279 
280 	wep->weCnt |= WIN_LAST;
281 	return 0;
282 }
283 
284 /*
285  * Convert a unix filename to a DOS filename according to Win95 rules.
286  * If applicable and gen is not 0, it is inserted into the converted
287  * filename as a generation number.
288  * Returns
289  *	0 if name couldn't be converted
290  *	1 if the converted name is the same as the original
291  *	  (no long filename entry necessary for Win95)
292  *	2 if conversion was successful
293  *	3 if conversion was successful and generation number was inserted
294  */
295 int
296 unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen)
297 {
298 	int i, j, l;
299 	int conv = 1;
300 	const u_char *cp, *dp, *dp1;
301 	u_char gentext[6], *wcp;
302 	int shortlen;
303 
304 	/*
305 	 * Fill the dos filename string with blanks. These are DOS's pad
306 	 * characters.
307 	 */
308 	for (i = 0; i < 11; i++)
309 		dn[i] = ' ';
310 	dn[11] = 0;
311 
312 	/*
313 	 * The filenames "." and ".." are handled specially, since they
314 	 * don't follow dos filename rules.
315 	 */
316 	if (un[0] == '.' && unlen == 1) {
317 		dn[0] = '.';
318 		return gen <= 1;
319 	}
320 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
321 		dn[0] = '.';
322 		dn[1] = '.';
323 		return gen <= 1;
324 	}
325 
326 	/*
327 	 * Filenames with only blanks and dots are not allowed!
328 	 */
329 	for (cp = un, i = unlen; --i >= 0; cp++)
330 		if (*cp != ' ' && *cp != '.')
331 			break;
332 	if (i < 0)
333 		return 0;
334 
335 	/*
336 	 * Now find the extension
337 	 * Note: dot as first char doesn't start extension
338 	 *	 and trailing dots and blanks are ignored
339 	 */
340 	dp = dp1 = 0;
341 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
342 		switch (*cp++) {
343 		case '.':
344 			if (!dp1)
345 				dp1 = cp;
346 			break;
347 		case ' ':
348 			break;
349 		default:
350 			if (dp1)
351 				dp = dp1;
352 			dp1 = 0;
353 			break;
354 		}
355 	}
356 
357 	/*
358 	 * Now convert it
359 	 */
360 	if (dp) {
361 		if (dp1)
362 			l = dp1 - dp;
363 		else
364 			l = unlen - (dp - un);
365 		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
366 			if (dp[i] != (dn[j] = unix2dos[dp[i]])
367 			    && conv != 3)
368 				conv = 2;
369 			if (!dn[j]) {
370 				conv = 3;
371 				dn[j--] = ' ';
372 			}
373 		}
374 		if (i < l)
375 			conv = 3;
376 		dp--;
377 	} else {
378 		for (dp = cp; *--dp == ' ' || *dp == '.';);
379 		dp++;
380 	}
381 
382 	shortlen = (dp - un) <= 8;
383 
384 	/*
385 	 * Now convert the rest of the name
386 	 */
387 	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
388 		if ((*un == ' ') && shortlen)
389 			dn[j] = ' ';
390 		else
391 			dn[j] = unix2dos[*un];
392 		if ((*un != dn[j])
393 		    && conv != 3)
394 			conv = 2;
395 		if (!dn[j]) {
396 			conv = 3;
397 			dn[j--] = ' ';
398 		}
399 	}
400 	if (un < dp)
401 		conv = 3;
402 	/*
403 	 * If we didn't have any chars in filename,
404 	 * generate a default
405 	 */
406 	if (!j)
407 		dn[0] = '_';
408 
409 	/*
410 	 * The first character cannot be E5,
411 	 * because that means a deleted entry
412 	 */
413 	if (dn[0] == 0xe5)
414 		dn[0] = SLOT_E5;
415 
416 	/*
417 	 * If there wasn't any char dropped,
418 	 * there is no place for generation numbers
419 	 */
420 	if (conv != 3) {
421 		if (gen > 1)
422 			return 0;
423 		return conv;
424 	}
425 
426 	/*
427 	 * Now insert the generation number into the filename part
428 	 */
429 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
430 		*--wcp = gen % 10 + '0';
431 	if (gen)
432 		return 0;
433 	for (i = 8; dn[--i] == ' ';);
434 	i++;
435 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
436 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
437 	dn[i++] = '~';
438 	while (wcp < gentext + sizeof(gentext))
439 		dn[i++] = *wcp++;
440 	return 3;
441 }
442 
443 /*
444  * Convert 8bit character string into UCS-2 string
445  * return total number of output chacters
446  */
447 static int
448 char8ucs2str(const uint8_t *in, int n, uint16_t *out, int m)
449 {
450 	uint16_t *p;
451 
452 	p = out;
453 	while (n > 0 && in[0] != 0) {
454 		if (m < 1)
455 			break;
456 		if (p)
457 			p[0] = htole16(in[0]);
458 		p += 1;
459 		m -= 1;
460 		in += 1;
461 		n -= 1;
462 	}
463 
464 	return p - out;
465 }
466 
467 static void
468 ucs2pad(uint16_t *buf, int len, int size)
469 {
470 
471 	if (len < size-1)
472 		buf[len++] = 0x0000;
473 	while (len < size)
474 		buf[len++] = 0xffff;
475 }
476 
477 /*
478  * Compare two 8bit char conversions case-insensitive
479  *
480  * uses the DOS case folding table
481  */
482 static int
483 char8match(uint16_t *w1, uint16_t *w2, int n)
484 {
485 	uint16_t u1, u2;
486 
487 	while (n > 0) {
488 		u1 = le16toh(*w1);
489 		u2 = le16toh(*w2);
490 		if (u1 == 0 || u2 == 0)
491 			return u1 == u2;
492 		if (u1 > 255 || u2 > 255)
493 			return 0;
494 		u1 = u2l[u1 & 0xff];
495 		u2 = u2l[u2 & 0xff];
496 		if (u1 != u2)
497 			return 0;
498 		++w1;
499 		++w2;
500 		--n;
501 	}
502 
503 	return 1;
504 }
505