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