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