xref: /netbsd-src/sys/fs/msdosfs/msdosfs_conv.c (revision cb40c69b164a69aec47d70a50d23979d0f1752cb)
1*cb40c69bSandvar /*	$NetBSD: msdosfs_conv.c,v 1.20 2023/06/02 08:51:47 andvar Exp $	*/
298d58548Sjdolecek 
398d58548Sjdolecek /*-
498d58548Sjdolecek  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
598d58548Sjdolecek  * Copyright (C) 1995, 1997 TooLs GmbH.
698d58548Sjdolecek  * All rights reserved.
798d58548Sjdolecek  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
898d58548Sjdolecek  *
998d58548Sjdolecek  * Redistribution and use in source and binary forms, with or without
1098d58548Sjdolecek  * modification, are permitted provided that the following conditions
1198d58548Sjdolecek  * are met:
1298d58548Sjdolecek  * 1. Redistributions of source code must retain the above copyright
1398d58548Sjdolecek  *    notice, this list of conditions and the following disclaimer.
1498d58548Sjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
1598d58548Sjdolecek  *    notice, this list of conditions and the following disclaimer in the
1698d58548Sjdolecek  *    documentation and/or other materials provided with the distribution.
1798d58548Sjdolecek  * 3. All advertising materials mentioning features or use of this software
1898d58548Sjdolecek  *    must display the following acknowledgement:
1998d58548Sjdolecek  *	This product includes software developed by TooLs GmbH.
2098d58548Sjdolecek  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2198d58548Sjdolecek  *    derived from this software without specific prior written permission.
2298d58548Sjdolecek  *
2398d58548Sjdolecek  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2498d58548Sjdolecek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2598d58548Sjdolecek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2698d58548Sjdolecek  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2798d58548Sjdolecek  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2898d58548Sjdolecek  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2998d58548Sjdolecek  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3098d58548Sjdolecek  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3198d58548Sjdolecek  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3298d58548Sjdolecek  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3398d58548Sjdolecek  */
3498d58548Sjdolecek /*
3598d58548Sjdolecek  * Written by Paul Popelka (paulp@uts.amdahl.com)
3698d58548Sjdolecek  *
3798d58548Sjdolecek  * You can do anything you want with this software, just don't say you wrote
3898d58548Sjdolecek  * it, and don't remove this notice.
3998d58548Sjdolecek  *
4098d58548Sjdolecek  * This software is provided "as is".
4198d58548Sjdolecek  *
4298d58548Sjdolecek  * The author supplies this software to be publicly redistributed on the
4398d58548Sjdolecek  * understanding that the author is not responsible for the correct
4498d58548Sjdolecek  * functioning of this software in any circumstances and is not liable for
4598d58548Sjdolecek  * any damages caused by this software.
4698d58548Sjdolecek  *
4798d58548Sjdolecek  * October 1992
48223c7df5Smlelstv  *
4998d58548Sjdolecek  */
5098d58548Sjdolecek 
51929f8943Schristos #if HAVE_NBTOOL_CONFIG_H
52929f8943Schristos #include "nbtool_config.h"
53929f8943Schristos #endif
54929f8943Schristos 
55200cdc61Smlelstv #ifndef _KERNEL
56200cdc61Smlelstv #include <assert.h>
57200cdc61Smlelstv #define KASSERT(x)     assert(x)
58200cdc61Smlelstv #endif
59200cdc61Smlelstv 
6098d58548Sjdolecek #include <sys/cdefs.h>
61*cb40c69bSandvar __KERNEL_RCSID(0, "$NetBSD: msdosfs_conv.c,v 1.20 2023/06/02 08:51:47 andvar Exp $");
6298d58548Sjdolecek 
6398d58548Sjdolecek /*
6498d58548Sjdolecek  * System include files.
6598d58548Sjdolecek  */
6698d58548Sjdolecek #include <sys/param.h>
6798d58548Sjdolecek #include <sys/time.h>
68223c7df5Smlelstv #include <sys/endian.h>
69691bcd5dSchristos #ifdef _KERNEL
7098d58548Sjdolecek #include <sys/dirent.h>
71691bcd5dSchristos #include <sys/systm.h>
72691bcd5dSchristos #include <sys/kernel.h>
7398d58548Sjdolecek #include <sys/vnode.h>
74691bcd5dSchristos #else
75929f8943Schristos #include <stdio.h>
763c93e210Smartin #include <string.h>
77691bcd5dSchristos #include <dirent.h>
78691bcd5dSchristos #include <sys/queue.h>
79929f8943Schristos #endif
803c93e210Smartin #include <dev/clock_subr.h>
8198d58548Sjdolecek 
8298d58548Sjdolecek /*
8398d58548Sjdolecek  * MSDOSFS include files.
8498d58548Sjdolecek  */
8598d58548Sjdolecek #include <fs/msdosfs/direntry.h>
8698d58548Sjdolecek #include <fs/msdosfs/denode.h>
8798d58548Sjdolecek 
88223c7df5Smlelstv static int invalidname(const u_int16_t *, int);
89223c7df5Smlelstv 
90223c7df5Smlelstv static int ucs2utf8(const u_int16_t *, u_int8_t *, int);
91223c7df5Smlelstv static int utf8ucs2(const u_int8_t *, int, u_int16_t *);
92223c7df5Smlelstv 
93223c7df5Smlelstv static int ucs2utf8str(const u_int16_t *, int, u_int8_t *, int);
94223c7df5Smlelstv static int utf8ucs2str(const u_int8_t *, int, u_int16_t *, int);
95223c7df5Smlelstv static int ucs2char8str(const u_int16_t *, int, u_int8_t *, int);
96223c7df5Smlelstv static int char8ucs2str(const u_int8_t *, int, u_int16_t *, int);
97223c7df5Smlelstv 
98223c7df5Smlelstv static void ucs2pad(u_int16_t *, int, int);
99223c7df5Smlelstv 
100223c7df5Smlelstv static u_int16_t ucs2fold(u_int16_t);
101223c7df5Smlelstv static int ucs2match(u_int16_t *, u_int16_t *, int n);
102223c7df5Smlelstv static int char8match(u_int16_t *, u_int16_t *, int n);
103223c7df5Smlelstv 
10498d58548Sjdolecek /*
1053c93e210Smartin  * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
1063c93e210Smartin  * interval there were 8 regular years and 2 leap years.
10798d58548Sjdolecek  */
1083c93e210Smartin #define	DOSBIASYEAR	1980
1093c93e210Smartin #define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
11098d58548Sjdolecek /*
111*cb40c69bSandvar  * msdos fs can not store dates beyond the year 2234
11298d58548Sjdolecek  */
1133c93e210Smartin #define DOSMAXYEAR	((DD_YEAR_MASK >> DD_YEAR_SHIFT) + DOSBIASYEAR)
11498d58548Sjdolecek 
11598d58548Sjdolecek /*
11698d58548Sjdolecek  * Convert the unix version of time to dos's idea of time to be used in
11798d58548Sjdolecek  * file timestamps. The passed in unix time is assumed to be in GMT.
11898d58548Sjdolecek  */
11998d58548Sjdolecek void
msdosfs_unix2dostime(const struct timespec * tsp,int gmtoff,u_int16_t * ddp,u_int16_t * dtp,u_int8_t * dhp)1208086f46eSthorpej msdosfs_unix2dostime(const struct timespec *tsp, int gmtoff,
1218086f46eSthorpej     u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp)
12298d58548Sjdolecek {
12398d58548Sjdolecek 	u_long t;
1243c93e210Smartin 	struct clock_ymdhms ymd;
12598d58548Sjdolecek 
126ce112dfcSitojun 	t = tsp->tv_sec + gmtoff; /* time zone correction */
12798d58548Sjdolecek 
12898d58548Sjdolecek 	/*
1293c93e210Smartin 	 * DOS timestamps can not represent dates before 1980.
13098d58548Sjdolecek 	 */
1313c93e210Smartin 	if (t < SECONDSTO1980)
1323c93e210Smartin 		goto invalid_dos_date;
1333c93e210Smartin 
13498d58548Sjdolecek 	/*
1353c93e210Smartin 	 * DOS granularity is 2 seconds
13698d58548Sjdolecek 	 */
1373c93e210Smartin 	t &= ~1;
1383c93e210Smartin 
1393c93e210Smartin 	/*
1403c93e210Smartin 	 * Convert to year/month/day/.. format
1413c93e210Smartin 	 */
1423c93e210Smartin 	clock_secs_to_ymdhms(t, &ymd);
1433c93e210Smartin 	if (ymd.dt_year > DOSMAXYEAR)
1443c93e210Smartin 		goto invalid_dos_date;
1453c93e210Smartin 
1463c93e210Smartin 	/*
1473c93e210Smartin 	 * Now transform to DOS format
1483c93e210Smartin 	 */
1493c93e210Smartin 	*ddp = (ymd.dt_day << DD_DAY_SHIFT)
1503c93e210Smartin 	    + (ymd.dt_mon << DD_MONTH_SHIFT)
1513c93e210Smartin 	    + ((ymd.dt_year - DOSBIASYEAR) << DD_YEAR_SHIFT);
15298d58548Sjdolecek 	if (dhp)
15398d58548Sjdolecek 		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
1543c93e210Smartin 	if (dtp)
1553c93e210Smartin 		*dtp = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
1563c93e210Smartin 		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
1573c93e210Smartin 		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
1583c93e210Smartin 	return;
15998d58548Sjdolecek 
1603c93e210Smartin invalid_dos_date:
1613c93e210Smartin 	*ddp = 0;
1623c93e210Smartin 	if (dtp)
1633c93e210Smartin 		*dtp = 0;
1643c93e210Smartin 	if (dhp)
1653c93e210Smartin 		*dhp = 0;
16698d58548Sjdolecek }
16798d58548Sjdolecek 
16898d58548Sjdolecek /*
16998d58548Sjdolecek  * Convert from dos' idea of time to unix'. This will probably only be
17098d58548Sjdolecek  * called from the stat(), and fstat() system calls and so probably need
17198d58548Sjdolecek  * not be too efficient.
17298d58548Sjdolecek  */
17398d58548Sjdolecek void
msdosfs_dos2unixtime(u_int dd,u_int dt,u_int dh,int gmtoff,struct timespec * tsp)1748086f46eSthorpej msdosfs_dos2unixtime(u_int dd, u_int dt, u_int dh, int gmtoff,
1758086f46eSthorpej     struct timespec *tsp)
17698d58548Sjdolecek {
1773c93e210Smartin 	time_t seconds;
1783c93e210Smartin 	struct clock_ymdhms ymd;
17998d58548Sjdolecek 
18098d58548Sjdolecek 	if (dd == 0) {
18198d58548Sjdolecek 		/*
18298d58548Sjdolecek 		 * Uninitialized field, return the epoch.
18398d58548Sjdolecek 		 */
18498d58548Sjdolecek 		tsp->tv_sec = 0;
18598d58548Sjdolecek 		tsp->tv_nsec = 0;
18698d58548Sjdolecek 		return;
18798d58548Sjdolecek 	}
1883c93e210Smartin 
1893c93e210Smartin 	memset(&ymd, 0, sizeof(ymd));
1903c93e210Smartin 	ymd.dt_year = ((dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT) + 1980 ;
1913c93e210Smartin 	ymd.dt_mon = ((dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT);
1923c93e210Smartin 	ymd.dt_day = ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT);
1933c93e210Smartin 	ymd.dt_hour = (dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT;
1943c93e210Smartin 	ymd.dt_min = (dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT;
1953c93e210Smartin 	ymd.dt_sec = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2;
1963c93e210Smartin 
1973c93e210Smartin 	seconds = clock_ymdhms_to_secs(&ymd);
1983c93e210Smartin 
1993c93e210Smartin 	tsp->tv_sec = seconds;
200ce112dfcSitojun 	tsp->tv_sec -= gmtoff;	/* time zone correction */
20198d58548Sjdolecek 	tsp->tv_nsec = (dh % 100) * 10000000;
20298d58548Sjdolecek }
20398d58548Sjdolecek 
20498d58548Sjdolecek static const u_char
20598d58548Sjdolecek unix2dos[256] = {
20698d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
20798d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
20898d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
20998d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
21098d58548Sjdolecek 	0,    '!',  0,    '#',  '$',  '%',  '&',  '\'',	/* 20-27 */
21198d58548Sjdolecek 	'(',  ')',  0,    '+',  0,    '-',  0,    0,	/* 28-2f */
21298d58548Sjdolecek 	'0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
21398d58548Sjdolecek 	'8',  '9',  0,    0,    0,    0,    0,    0,	/* 38-3f */
21498d58548Sjdolecek 	'@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
21598d58548Sjdolecek 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
21698d58548Sjdolecek 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
21798d58548Sjdolecek 	'X',  'Y',  'Z',  0,    0,    0,    '^',  '_',	/* 58-5f */
21898d58548Sjdolecek 	'`',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 60-67 */
21998d58548Sjdolecek 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 68-6f */
22098d58548Sjdolecek 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 70-77 */
22198d58548Sjdolecek 	'X',  'Y',  'Z',  '{',  0,    '}',  '~',  0,	/* 78-7f */
22298d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
22398d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
22498d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
22598d58548Sjdolecek 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
22698d58548Sjdolecek 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
22798d58548Sjdolecek 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
22898d58548Sjdolecek 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
22998d58548Sjdolecek 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
23098d58548Sjdolecek 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
23198d58548Sjdolecek 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
23298d58548Sjdolecek 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
23398d58548Sjdolecek 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
23498d58548Sjdolecek 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
23598d58548Sjdolecek 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
23698d58548Sjdolecek 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
23798d58548Sjdolecek 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
23898d58548Sjdolecek };
23998d58548Sjdolecek 
24098d58548Sjdolecek static const u_char
24198d58548Sjdolecek dos2unix[256] = {
24298d58548Sjdolecek 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 00-07 */
24398d58548Sjdolecek 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 08-0f */
24498d58548Sjdolecek 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 10-17 */
24598d58548Sjdolecek 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 18-1f */
24698d58548Sjdolecek 	 ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'',	/* 20-27 */
24798d58548Sjdolecek 	 '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',	/* 28-2f */
24898d58548Sjdolecek 	 '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
24998d58548Sjdolecek 	 '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',	/* 38-3f */
25098d58548Sjdolecek 	 '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
25198d58548Sjdolecek 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
25298d58548Sjdolecek 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
25398d58548Sjdolecek 	 'X',  'Y',  'Z',  '[', '\\',  ']',  '^',  '_',	/* 58-5f */
25498d58548Sjdolecek 	 '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',	/* 60-67 */
25598d58548Sjdolecek 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',	/* 68-6f */
25698d58548Sjdolecek 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w',	/* 70-77 */
25798d58548Sjdolecek 	 'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f,	/* 78-7f */
25898d58548Sjdolecek 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
25998d58548Sjdolecek 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
26098d58548Sjdolecek 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
26198d58548Sjdolecek 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7,  '?',	/* 98-9f */
26298d58548Sjdolecek 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
26398d58548Sjdolecek 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
26498d58548Sjdolecek 	 '?',  '?',  '?',  '?',  '?', 0xc1, 0xc2, 0xc0,	/* b0-b7 */
26598d58548Sjdolecek 	0xa9,  '?',  '?',  '?',  '?', 0xa2, 0xa5,  '?',	/* b8-bf */
26698d58548Sjdolecek 	 '?',  '?',  '?',  '?',  '?',  '?', 0xe3, 0xc3,	/* c0-c7 */
26798d58548Sjdolecek 	 '?',  '?',  '?',  '?',  '?',  '?',  '?', 0xa4,	/* c8-cf */
26898d58548Sjdolecek 	0xf0, 0xd0, 0xca, 0xcb, 0xc8,  '?', 0xcd, 0xce,	/* d0-d7 */
26998d58548Sjdolecek 	0xcf,  '?',  '?',  '?',  '?', 0xa6, 0xcc,  '?',	/* d8-df */
27098d58548Sjdolecek 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
27198d58548Sjdolecek 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
27298d58548Sjdolecek 	0xad, 0xb1,  '?', 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
27398d58548Sjdolecek 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2,  '?',  '?',	/* f8-ff */
27498d58548Sjdolecek };
27598d58548Sjdolecek 
27698d58548Sjdolecek static const u_char
27798d58548Sjdolecek u2l[256] = {
27898d58548Sjdolecek 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
27998d58548Sjdolecek 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
28098d58548Sjdolecek 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
28198d58548Sjdolecek 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
28298d58548Sjdolecek 	 ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'', /* 20-27 */
28398d58548Sjdolecek 	 '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/', /* 28-2f */
28498d58548Sjdolecek 	 '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7', /* 30-37 */
28598d58548Sjdolecek 	 '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?', /* 38-3f */
28698d58548Sjdolecek 	 '@',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 40-47 */
28798d58548Sjdolecek 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 48-4f */
28898d58548Sjdolecek 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 50-57 */
28998d58548Sjdolecek 	 'x',  'y',  'z',  '[', '\\',  ']',  '^',  '_', /* 58-5f */
29098d58548Sjdolecek 	 '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 60-67 */
29198d58548Sjdolecek 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 68-6f */
29298d58548Sjdolecek 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 70-77 */
29398d58548Sjdolecek 	 'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f, /* 78-7f */
29498d58548Sjdolecek 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
29598d58548Sjdolecek 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
29698d58548Sjdolecek 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
29798d58548Sjdolecek 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
29898d58548Sjdolecek 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
29998d58548Sjdolecek 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
30098d58548Sjdolecek 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
30198d58548Sjdolecek 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
30298d58548Sjdolecek 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
30398d58548Sjdolecek 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
30498d58548Sjdolecek 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
30598d58548Sjdolecek 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
30698d58548Sjdolecek 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
30798d58548Sjdolecek 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
30898d58548Sjdolecek 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
30998d58548Sjdolecek 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
31098d58548Sjdolecek };
31198d58548Sjdolecek 
31298d58548Sjdolecek /*
31398d58548Sjdolecek  * DOS filenames are made of 2 parts, the name part and the extension part.
31498d58548Sjdolecek  * The name part is 8 characters long and the extension part is 3
31598d58548Sjdolecek  * characters long.  They may contain trailing blanks if the name or
31698d58548Sjdolecek  * extension are not long enough to fill their respective fields.
31798d58548Sjdolecek  */
31898d58548Sjdolecek 
31998d58548Sjdolecek /*
32098d58548Sjdolecek  * Convert a DOS filename to a unix filename. And, return the number of
32198d58548Sjdolecek  * characters in the resulting unix filename excluding the terminating
32298d58548Sjdolecek  * null.
32398d58548Sjdolecek  */
32498d58548Sjdolecek int
msdosfs_dos2unixfn(u_char dn[11],u_char * un,int lower)3258086f46eSthorpej msdosfs_dos2unixfn(u_char dn[11], u_char *un, int lower)
32698d58548Sjdolecek {
32798d58548Sjdolecek 	int i, j;
32898d58548Sjdolecek 	int thislong = 1;
32998d58548Sjdolecek 	u_char c;
33098d58548Sjdolecek 
33198d58548Sjdolecek 	/*
33298d58548Sjdolecek 	 * If first char of the filename is SLOT_E5 (0x05), then the real
33398d58548Sjdolecek 	 * first char of the filename should be 0xe5. But, they couldn't
33498d58548Sjdolecek 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
33598d58548Sjdolecek 	 * directory slot. Another dos quirk.
33698d58548Sjdolecek 	 */
33798d58548Sjdolecek 	if (*dn == SLOT_E5)
33898d58548Sjdolecek 		c = dos2unix[0xe5];
33998d58548Sjdolecek 	else
34098d58548Sjdolecek 		c = dos2unix[*dn];
34198d58548Sjdolecek 	*un++ = lower ? u2l[c] : c;
34298d58548Sjdolecek 
34398d58548Sjdolecek 	/*
34498d58548Sjdolecek 	 * Copy the rest into the unix filename string, ignoring
34598d58548Sjdolecek 	 * trailing blanks.
34698d58548Sjdolecek 	 */
34798d58548Sjdolecek 
34898d58548Sjdolecek 	for (j=7; (j >= 0) && (dn[j] == ' '); j--)
34998d58548Sjdolecek 		;
35098d58548Sjdolecek 
35198d58548Sjdolecek 	for (i = 1; i <= j; i++) {
35298d58548Sjdolecek 		c = dos2unix[dn[i]];
35398d58548Sjdolecek 		*un++ = lower ? u2l[c] : c;
35498d58548Sjdolecek 		thislong++;
35598d58548Sjdolecek 	}
35698d58548Sjdolecek 	dn += 8;
35798d58548Sjdolecek 
35898d58548Sjdolecek 	/*
35998d58548Sjdolecek 	 * Now, if there is an extension then put in a period and copy in
36098d58548Sjdolecek 	 * the extension.
36198d58548Sjdolecek 	 */
36298d58548Sjdolecek 	if (*dn != ' ') {
36398d58548Sjdolecek 		*un++ = '.';
36498d58548Sjdolecek 		thislong++;
36598d58548Sjdolecek 		for (i = 0; i < 3 && *dn != ' '; i++) {
36698d58548Sjdolecek 			c = dos2unix[*dn++];
36798d58548Sjdolecek 			*un++ = lower ? u2l[c] : c;
36898d58548Sjdolecek 			thislong++;
36998d58548Sjdolecek 		}
37098d58548Sjdolecek 	}
37198d58548Sjdolecek 	*un++ = 0;
37298d58548Sjdolecek 
37398d58548Sjdolecek 	return (thislong);
37498d58548Sjdolecek }
37598d58548Sjdolecek 
37698d58548Sjdolecek /*
37798d58548Sjdolecek  * Convert a unix filename to a DOS filename according to Win95 rules.
37898d58548Sjdolecek  * If applicable and gen is not 0, it is inserted into the converted
37998d58548Sjdolecek  * filename as a generation number.
38098d58548Sjdolecek  * Returns
38198d58548Sjdolecek  *	0 if name couldn't be converted
38298d58548Sjdolecek  *	1 if the converted name is the same as the original
38398d58548Sjdolecek  *	  (no long filename entry necessary for Win95)
38498d58548Sjdolecek  *	2 if conversion was successful
38598d58548Sjdolecek  *	3 if conversion was successful and generation number was inserted
38698d58548Sjdolecek  */
38798d58548Sjdolecek int
msdosfs_unix2dosfn(const u_char * un,u_char dn[12],int unlen,u_int gen)3888086f46eSthorpej msdosfs_unix2dosfn(const u_char *un, u_char dn[12], int unlen, u_int gen)
38998d58548Sjdolecek {
39098d58548Sjdolecek 	int i, j, l;
39198d58548Sjdolecek 	int conv = 1;
39298d58548Sjdolecek 	const u_char *cp, *dp, *dp1;
39398d58548Sjdolecek 	u_char gentext[6], *wcp;
39498d58548Sjdolecek 	int shortlen;
39598d58548Sjdolecek 
39698d58548Sjdolecek 	/*
39798d58548Sjdolecek 	 * Fill the dos filename string with blanks. These are DOS's pad
39898d58548Sjdolecek 	 * characters.
39998d58548Sjdolecek 	 */
40098d58548Sjdolecek 	for (i = 0; i < 11; i++)
40198d58548Sjdolecek 		dn[i] = ' ';
40298d58548Sjdolecek 	dn[11] = 0;
40398d58548Sjdolecek 
40498d58548Sjdolecek 	/*
40598d58548Sjdolecek 	 * The filenames "." and ".." are handled specially, since they
40698d58548Sjdolecek 	 * don't follow dos filename rules.
40798d58548Sjdolecek 	 */
40898d58548Sjdolecek 	if (un[0] == '.' && unlen == 1) {
40998d58548Sjdolecek 		dn[0] = '.';
41098d58548Sjdolecek 		return gen <= 1;
41198d58548Sjdolecek 	}
41298d58548Sjdolecek 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
41398d58548Sjdolecek 		dn[0] = '.';
41498d58548Sjdolecek 		dn[1] = '.';
41598d58548Sjdolecek 		return gen <= 1;
41698d58548Sjdolecek 	}
41798d58548Sjdolecek 
41898d58548Sjdolecek 	/*
41998d58548Sjdolecek 	 * Filenames with only blanks and dots are not allowed!
42098d58548Sjdolecek 	 */
42198d58548Sjdolecek 	for (cp = un, i = unlen; --i >= 0; cp++)
42298d58548Sjdolecek 		if (*cp != ' ' && *cp != '.')
42398d58548Sjdolecek 			break;
42498d58548Sjdolecek 	if (i < 0)
42598d58548Sjdolecek 		return 0;
42698d58548Sjdolecek 
42798d58548Sjdolecek 	/*
42898d58548Sjdolecek 	 * Now find the extension
42998d58548Sjdolecek 	 * Note: dot as first char doesn't start extension
43098d58548Sjdolecek 	 *	 and trailing dots and blanks are ignored
43198d58548Sjdolecek 	 */
43298d58548Sjdolecek 	dp = dp1 = 0;
43398d58548Sjdolecek 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
43498d58548Sjdolecek 		switch (*cp++) {
43598d58548Sjdolecek 		case '.':
43698d58548Sjdolecek 			if (!dp1)
43798d58548Sjdolecek 				dp1 = cp;
43898d58548Sjdolecek 			break;
43998d58548Sjdolecek 		case ' ':
44098d58548Sjdolecek 			break;
44198d58548Sjdolecek 		default:
44298d58548Sjdolecek 			if (dp1)
44398d58548Sjdolecek 				dp = dp1;
44498d58548Sjdolecek 			dp1 = 0;
44598d58548Sjdolecek 			break;
44698d58548Sjdolecek 		}
44798d58548Sjdolecek 	}
44898d58548Sjdolecek 
44998d58548Sjdolecek 	/*
45098d58548Sjdolecek 	 * Now convert it
45198d58548Sjdolecek 	 */
45298d58548Sjdolecek 	if (dp) {
45398d58548Sjdolecek 		if (dp1)
45498d58548Sjdolecek 			l = dp1 - dp;
45598d58548Sjdolecek 		else
45698d58548Sjdolecek 			l = unlen - (dp - un);
45798d58548Sjdolecek 		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
45898d58548Sjdolecek 			if (dp[i] != (dn[j] = unix2dos[dp[i]])
45998d58548Sjdolecek 			    && conv != 3)
46098d58548Sjdolecek 				conv = 2;
46198d58548Sjdolecek 			if (!dn[j]) {
46298d58548Sjdolecek 				conv = 3;
46398d58548Sjdolecek 				dn[j--] = ' ';
46498d58548Sjdolecek 			}
46598d58548Sjdolecek 		}
46698d58548Sjdolecek 		if (i < l)
46798d58548Sjdolecek 			conv = 3;
46898d58548Sjdolecek 		dp--;
46998d58548Sjdolecek 	} else {
47098d58548Sjdolecek 		for (dp = cp; *--dp == ' ' || *dp == '.';);
47198d58548Sjdolecek 		dp++;
47298d58548Sjdolecek 	}
47398d58548Sjdolecek 
47498d58548Sjdolecek 	shortlen = (dp - un) <= 8;
47598d58548Sjdolecek 
47698d58548Sjdolecek 	/*
47798d58548Sjdolecek 	 * Now convert the rest of the name
47898d58548Sjdolecek 	 */
47998d58548Sjdolecek 	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
48098d58548Sjdolecek 		if ((*un == ' ') && shortlen)
48198d58548Sjdolecek 			dn[j] = ' ';
48298d58548Sjdolecek 		else
48398d58548Sjdolecek 			dn[j] = unix2dos[*un];
48498d58548Sjdolecek 		if ((*un != dn[j])
48598d58548Sjdolecek 		    && conv != 3)
48698d58548Sjdolecek 			conv = 2;
48798d58548Sjdolecek 		if (!dn[j]) {
48898d58548Sjdolecek 			conv = 3;
48998d58548Sjdolecek 			dn[j--] = ' ';
49098d58548Sjdolecek 		}
49198d58548Sjdolecek 	}
49298d58548Sjdolecek 	if (un < dp)
49398d58548Sjdolecek 		conv = 3;
49498d58548Sjdolecek 	/*
49598d58548Sjdolecek 	 * If we didn't have any chars in filename,
49698d58548Sjdolecek 	 * generate a default
49798d58548Sjdolecek 	 */
49898d58548Sjdolecek 	if (!j)
49998d58548Sjdolecek 		dn[0] = '_';
50098d58548Sjdolecek 
50198d58548Sjdolecek 	/*
50298d58548Sjdolecek 	 * The first character cannot be E5,
50398d58548Sjdolecek 	 * because that means a deleted entry
50498d58548Sjdolecek 	 */
50598d58548Sjdolecek 	if (dn[0] == 0xe5)
50698d58548Sjdolecek 		dn[0] = SLOT_E5;
50798d58548Sjdolecek 
50898d58548Sjdolecek 	/*
50998d58548Sjdolecek 	 * If there wasn't any char dropped,
51098d58548Sjdolecek 	 * there is no place for generation numbers
51198d58548Sjdolecek 	 */
51298d58548Sjdolecek 	if (conv != 3) {
51398d58548Sjdolecek 		if (gen > 1)
51498d58548Sjdolecek 			return 0;
51598d58548Sjdolecek 		return conv;
51698d58548Sjdolecek 	}
51798d58548Sjdolecek 
51898d58548Sjdolecek 	/*
51998d58548Sjdolecek 	 * Now insert the generation number into the filename part
52098d58548Sjdolecek 	 */
52198d58548Sjdolecek 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
52298d58548Sjdolecek 		*--wcp = gen % 10 + '0';
52398d58548Sjdolecek 	if (gen)
52498d58548Sjdolecek 		return 0;
52598d58548Sjdolecek 	for (i = 8; dn[--i] == ' ';);
52698d58548Sjdolecek 	i++;
52798d58548Sjdolecek 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
52898d58548Sjdolecek 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
52998d58548Sjdolecek 	dn[i++] = '~';
53098d58548Sjdolecek 	while (wcp < gentext + sizeof(gentext))
53198d58548Sjdolecek 		dn[i++] = *wcp++;
53298d58548Sjdolecek 	return 3;
53398d58548Sjdolecek }
53498d58548Sjdolecek 
53598d58548Sjdolecek /*
53698d58548Sjdolecek  * Create a Win95 long name directory entry
53798d58548Sjdolecek  * Note: assumes that the filename is valid,
53898d58548Sjdolecek  *	 i.e. doesn't consist solely of blanks and dots
53998d58548Sjdolecek  */
54098d58548Sjdolecek int
msdosfs_unix2winfn(const u_char * un,int unlen,struct winentry * wep,int cnt,int chksum,int utf8)5418086f46eSthorpej msdosfs_unix2winfn(const u_char *un, int unlen, struct winentry *wep, int cnt,
5428086f46eSthorpej     int chksum, int utf8)
54398d58548Sjdolecek {
544223c7df5Smlelstv 	u_int16_t wn[WIN_MAXLEN], *p;
545223c7df5Smlelstv 	int i, len;
546223c7df5Smlelstv 	const u_char *cp;
54798d58548Sjdolecek 
54898d58548Sjdolecek 	/*
54998d58548Sjdolecek 	 * Drop trailing blanks and dots
55098d58548Sjdolecek 	 */
551223c7df5Smlelstv 	for (cp = un + unlen; unlen > 0; unlen--)
552223c7df5Smlelstv 		if (*--cp != ' ' && *cp != '.')
553223c7df5Smlelstv 			break;
55498d58548Sjdolecek 
555223c7df5Smlelstv 	/*
556223c7df5Smlelstv 	 * Offset of this entry
557223c7df5Smlelstv 	 */
558223c7df5Smlelstv 	i = (cnt - 1) * WIN_CHARS;
559223c7df5Smlelstv 
560223c7df5Smlelstv 	/*
561223c7df5Smlelstv 	 * Translate UNIX name to ucs-2
562223c7df5Smlelstv 	 */
563223c7df5Smlelstv 	len = utf8 ? utf8ucs2str(un, unlen, wn, WIN_MAXLEN) : char8ucs2str(un, unlen, wn, WIN_MAXLEN);
564223c7df5Smlelstv 	ucs2pad(wn, len, WIN_MAXLEN);
56598d58548Sjdolecek 
56698d58548Sjdolecek 	/*
56798d58548Sjdolecek 	 * Initialize winentry to some useful default
56898d58548Sjdolecek 	 */
569223c7df5Smlelstv 	memset(wep, 0xff, sizeof(*wep));
57098d58548Sjdolecek 	wep->weCnt = cnt;
57198d58548Sjdolecek 	wep->weAttributes = ATTR_WIN95;
57298d58548Sjdolecek 	wep->weReserved1 = 0;
57398d58548Sjdolecek 	wep->weChksum = chksum;
57498d58548Sjdolecek 	wep->weReserved2 = 0;
57598d58548Sjdolecek 
57698d58548Sjdolecek 	/*
577223c7df5Smlelstv 	 * Store name segment into directory entry
57898d58548Sjdolecek 	 */
579223c7df5Smlelstv 	p = &wn[i];
580223c7df5Smlelstv 	memcpy(wep->wePart1, p, sizeof(wep->wePart1));
581223c7df5Smlelstv 	p += sizeof(wep->wePart1) / sizeof(*p);
582223c7df5Smlelstv 	memcpy(wep->wePart2, p, sizeof(wep->wePart2));
583223c7df5Smlelstv 	p += sizeof(wep->wePart2) / sizeof(*p);
584223c7df5Smlelstv 	memcpy(wep->wePart3, p, sizeof(wep->wePart3));
58598d58548Sjdolecek 
586223c7df5Smlelstv 	if (len > i + WIN_CHARS)
587223c7df5Smlelstv 		return 1;
588223c7df5Smlelstv 
58998d58548Sjdolecek 	wep->weCnt |= WIN_LAST;
59098d58548Sjdolecek 	return 0;
59198d58548Sjdolecek }
59298d58548Sjdolecek 
59398d58548Sjdolecek /*
59498d58548Sjdolecek  * Compare our filename to the one in the Win95 entry
59598d58548Sjdolecek  * Returns the checksum or -1 if no match
59698d58548Sjdolecek  */
59798d58548Sjdolecek int
msdosfs_winChkName(const u_char * un,int unlen,struct winentry * wep,int chksum,int utf8)5988086f46eSthorpej msdosfs_winChkName(const u_char *un, int unlen, struct winentry *wep,
5998086f46eSthorpej     int chksum, int utf8)
60098d58548Sjdolecek {
601223c7df5Smlelstv 	u_int16_t wn[WIN_MAXLEN], *p;
602223c7df5Smlelstv 	u_int16_t buf[WIN_CHARS];
603223c7df5Smlelstv 	int i, len;
60498d58548Sjdolecek 
60598d58548Sjdolecek 	/*
60698d58548Sjdolecek 	 * First compare checksums
60798d58548Sjdolecek 	 */
60898d58548Sjdolecek 	if (wep->weCnt & WIN_LAST)
60998d58548Sjdolecek 		chksum = wep->weChksum;
61098d58548Sjdolecek 	else if (chksum != wep->weChksum)
61198d58548Sjdolecek 		chksum = -1;
61298d58548Sjdolecek 	if (chksum == -1)
61398d58548Sjdolecek 		return -1;
61498d58548Sjdolecek 
61598d58548Sjdolecek 	/*
61698d58548Sjdolecek 	 * Offset of this entry
61798d58548Sjdolecek 	 */
61898d58548Sjdolecek 	i = ((wep->weCnt & WIN_CNT) - 1) * WIN_CHARS;
619223c7df5Smlelstv 
620223c7df5Smlelstv 	/*
621223c7df5Smlelstv 	 * Translate UNIX name to ucs-2
622223c7df5Smlelstv 	 */
623223c7df5Smlelstv 	len = utf8 ? utf8ucs2str(un, unlen, wn, WIN_MAXLEN) : char8ucs2str(un, unlen, wn, WIN_MAXLEN);
624223c7df5Smlelstv 	ucs2pad(wn, len, WIN_MAXLEN);
625223c7df5Smlelstv 
626223c7df5Smlelstv 	if (i >= len + 1)
62798d58548Sjdolecek 		return -1;
628eafa673dSnonaka 	if ((wep->weCnt & WIN_LAST) && (len - i > WIN_CHARS))
629eafa673dSnonaka 		return -1;
63098d58548Sjdolecek 
63198d58548Sjdolecek 	/*
632223c7df5Smlelstv 	 * Fetch name segment from directory entry
63398d58548Sjdolecek 	 */
634223c7df5Smlelstv 	p = &buf[0];
635223c7df5Smlelstv 	memcpy(p, wep->wePart1, sizeof(wep->wePart1));
636223c7df5Smlelstv 	p += sizeof(wep->wePart1) / sizeof(*p);
637223c7df5Smlelstv 	memcpy(p, wep->wePart2, sizeof(wep->wePart2));
638223c7df5Smlelstv 	p += sizeof(wep->wePart2) / sizeof(*p);
639223c7df5Smlelstv 	memcpy(p, wep->wePart3, sizeof(wep->wePart3));
64098d58548Sjdolecek 
64198d58548Sjdolecek 	/*
642223c7df5Smlelstv 	 * And compare name segment
64398d58548Sjdolecek 	 */
644223c7df5Smlelstv 	if (! (utf8 ? ucs2match(&wn[i], buf, WIN_CHARS) : char8match(&wn[i], buf, WIN_CHARS)))
64598d58548Sjdolecek 		return -1;
646223c7df5Smlelstv 
64798d58548Sjdolecek 	return chksum;
64898d58548Sjdolecek }
64998d58548Sjdolecek 
65098d58548Sjdolecek /*
65198d58548Sjdolecek  * Convert Win95 filename to dirbuf.
65298d58548Sjdolecek  * Returns the checksum or -1 if impossible
65398d58548Sjdolecek  */
65498d58548Sjdolecek int
msdosfs_win2unixfn(struct winentry * wep,struct dirent * dp,int chksum,uint16_t * namlen,int utf8)6558086f46eSthorpej msdosfs_win2unixfn(struct winentry *wep, struct dirent *dp, int chksum,
65676530fdbSchristos     uint16_t *namlen, int utf8)
65798d58548Sjdolecek {
658223c7df5Smlelstv 	u_int16_t wn[WIN_CHARS], *p;
659223c7df5Smlelstv 	u_int8_t buf[WIN_CHARS*3];
660223c7df5Smlelstv 	int len;
66198d58548Sjdolecek 
66298d58548Sjdolecek 	if ((wep->weCnt & WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
66398d58548Sjdolecek 	    || !(wep->weCnt & WIN_CNT))
66498d58548Sjdolecek 		return -1;
66598d58548Sjdolecek 
66698d58548Sjdolecek 	/*
66798d58548Sjdolecek 	 * First compare checksums
66898d58548Sjdolecek 	 */
66998d58548Sjdolecek 	if (wep->weCnt & WIN_LAST) {
67098d58548Sjdolecek 		chksum = wep->weChksum;
67176530fdbSchristos 		*namlen = 0;
67298d58548Sjdolecek 	} else if (chksum != wep->weChksum)
67398d58548Sjdolecek 		chksum = -1;
67498d58548Sjdolecek 	if (chksum == -1)
67598d58548Sjdolecek 		return -1;
67698d58548Sjdolecek 
67798d58548Sjdolecek 	/*
678223c7df5Smlelstv 	 * Fetch name segment from directory entry
67998d58548Sjdolecek 	 */
680223c7df5Smlelstv 	p = &wn[0];
681223c7df5Smlelstv 	memcpy(p, wep->wePart1, sizeof(wep->wePart1));
682223c7df5Smlelstv 	p += sizeof(wep->wePart1) / sizeof(*p);
683223c7df5Smlelstv 	memcpy(p, wep->wePart2, sizeof(wep->wePart2));
684223c7df5Smlelstv 	p += sizeof(wep->wePart2) / sizeof(*p);
685223c7df5Smlelstv 	memcpy(p, wep->wePart3, sizeof(wep->wePart3));
68698d58548Sjdolecek 
68798d58548Sjdolecek 	/*
688223c7df5Smlelstv 	 * Don't allow slashes in UNIX names. Discard that entry.
68998d58548Sjdolecek 	 */
690223c7df5Smlelstv 	if (invalidname(wn, WIN_CHARS))
69198d58548Sjdolecek 		return -1;
692223c7df5Smlelstv 
69398d58548Sjdolecek 	/*
694223c7df5Smlelstv 	 * Translate ucs-2 to UNIX name
69598d58548Sjdolecek 	 */
696ae157f38Schristos 	len = utf8 ? ucs2utf8str(wn, WIN_CHARS, buf, sizeof(buf))
697ae157f38Schristos 	    : ucs2char8str(wn, WIN_CHARS, buf, sizeof(buf));
698223c7df5Smlelstv 
699200cdc61Smlelstv 	KASSERT(len >= 0);
700200cdc61Smlelstv 	KASSERT((size_t)len <= MIN(sizeof(buf), sizeof(dp->d_name)-1));
70176530fdbSchristos 
70298d58548Sjdolecek 	/*
703223c7df5Smlelstv 	 * Prepend name segment to directory entry
704223c7df5Smlelstv 	 *
705223c7df5Smlelstv 	 * This ignores the slot number from the windows entry but
706223c7df5Smlelstv 	 * assumes that segments are read in reverse order.
707223c7df5Smlelstv 	 *
708223c7df5Smlelstv 	 * The UCS-2 name (up to 255 chars) can overflow the UNIX
709223c7df5Smlelstv 	 * directory entry (up to 511 bytes). Trailing characters
710223c7df5Smlelstv 	 * are silently discarded. This could also end in multiple
711223c7df5Smlelstv 	 * files using the same (truncated) name.
71298d58548Sjdolecek 	 */
71376530fdbSchristos 	*namlen += len;
71476530fdbSchristos 	if (*namlen > sizeof(dp->d_name) - 1)
71576530fdbSchristos 		*namlen = sizeof(dp->d_name) - 1;
716200cdc61Smlelstv 
717200cdc61Smlelstv 	KASSERT(*namlen >= len);
718200cdc61Smlelstv 
71976530fdbSchristos 	memmove(&dp->d_name[len], &dp->d_name[0], *namlen - len);
720223c7df5Smlelstv 	memcpy(dp->d_name, buf, len);
721223c7df5Smlelstv 
72298d58548Sjdolecek 	return chksum;
72398d58548Sjdolecek }
72498d58548Sjdolecek 
72598d58548Sjdolecek /*
72698d58548Sjdolecek  * Compute the checksum of a DOS filename for Win95 use
72798d58548Sjdolecek  */
72898d58548Sjdolecek u_int8_t
msdosfs_winChksum(u_int8_t * name)7298086f46eSthorpej msdosfs_winChksum(u_int8_t *name)
73098d58548Sjdolecek {
73198d58548Sjdolecek 	int i;
73298d58548Sjdolecek 	u_int8_t s;
73398d58548Sjdolecek 
73498d58548Sjdolecek 	for (s = 0, i = 11; --i >= 0; s += *name++)
73598d58548Sjdolecek 		s = (s << 7) | (s >> 1);
73698d58548Sjdolecek 	return s;
73798d58548Sjdolecek }
73898d58548Sjdolecek 
73998d58548Sjdolecek /*
74098d58548Sjdolecek  * Determine the number of slots necessary for Win95 names
74198d58548Sjdolecek  */
74298d58548Sjdolecek int
msdosfs_winSlotCnt(const u_char * un,int unlen,int utf8)7438086f46eSthorpej msdosfs_winSlotCnt(const u_char *un, int unlen, int utf8)
74498d58548Sjdolecek {
745223c7df5Smlelstv 	const u_char *cp;
746223c7df5Smlelstv 	int len;
747223c7df5Smlelstv 
748223c7df5Smlelstv 	/*
749223c7df5Smlelstv 	 * Drop trailing blanks and dots
750223c7df5Smlelstv 	 */
751223c7df5Smlelstv 	for (cp = un + unlen; unlen > 0; unlen--)
752223c7df5Smlelstv 		if (*--cp != ' ' && *cp != '.')
75398d58548Sjdolecek 			break;
754223c7df5Smlelstv 
755223c7df5Smlelstv 	len = utf8 ? utf8ucs2str(un, unlen, NULL, WIN_MAXLEN) : unlen;
756223c7df5Smlelstv 
757223c7df5Smlelstv 	return howmany(len, WIN_CHARS);
75898d58548Sjdolecek }
759223c7df5Smlelstv 
760223c7df5Smlelstv /*
761223c7df5Smlelstv  * Scan windows name for characters that must not
762223c7df5Smlelstv  * appear in a UNIX filename
763223c7df5Smlelstv  */
764223c7df5Smlelstv static int
invalidname(const u_int16_t * in,int n)765223c7df5Smlelstv invalidname(const u_int16_t *in, int n)
766223c7df5Smlelstv {
767223c7df5Smlelstv 	while (n-- > 0) {
768223c7df5Smlelstv 		if (*in++ == '/')
769223c7df5Smlelstv 			return 1;
770223c7df5Smlelstv 	}
771223c7df5Smlelstv 
772223c7df5Smlelstv 	return 0;
773223c7df5Smlelstv }
774223c7df5Smlelstv 
775223c7df5Smlelstv /*
776223c7df5Smlelstv  * Convert UCS-2 character into UTF-8
777223c7df5Smlelstv  * return number of output bytes or 0 if output
778223c7df5Smlelstv  * buffer is too short
779223c7df5Smlelstv  */
780223c7df5Smlelstv static int
ucs2utf8(const u_int16_t * in,u_int8_t * out,int n)781223c7df5Smlelstv ucs2utf8(const u_int16_t *in, u_int8_t *out, int n)
782223c7df5Smlelstv {
783223c7df5Smlelstv 	uint16_t inch = le16toh(in[0]);
784223c7df5Smlelstv 
785223c7df5Smlelstv 	if (inch <= 0x007f) {
786223c7df5Smlelstv 		if (n < 1) return 0;
787223c7df5Smlelstv 		if (out)
788223c7df5Smlelstv 			*out++ = inch;
789223c7df5Smlelstv 		return 1;
790223c7df5Smlelstv 	} else if (inch <= 0x07ff) {
791223c7df5Smlelstv 		if (n < 2) return 0;
792223c7df5Smlelstv 		if (out) {
793223c7df5Smlelstv 			*out++ = 0xc0 | (inch >> 6);
794223c7df5Smlelstv 			*out++ = 0x80 | (inch & 0x3f);
795223c7df5Smlelstv 		}
796223c7df5Smlelstv 		return 2;
797223c7df5Smlelstv 	} else {
798223c7df5Smlelstv 		if (n < 3) return 0;
799223c7df5Smlelstv 		if (out) {
800223c7df5Smlelstv 			*out++ = 0xe0 | (inch >> 12);
801223c7df5Smlelstv 			*out++ = 0x80 | ((inch >> 6) & 0x3f);
802223c7df5Smlelstv 			*out++ = 0x80 | (inch & 0x3f);
803223c7df5Smlelstv 		}
804223c7df5Smlelstv 		return 3;
805223c7df5Smlelstv 	}
806223c7df5Smlelstv }
807223c7df5Smlelstv 
808223c7df5Smlelstv 
809223c7df5Smlelstv /*
810223c7df5Smlelstv  * Convert UTF-8 bytes into UCS-2 character
811223c7df5Smlelstv  * return number of input bytes, 0 if input
812223c7df5Smlelstv  * is too short and -1 if input is invalid
813223c7df5Smlelstv  */
814223c7df5Smlelstv static int
utf8ucs2(const u_int8_t * in,int n,u_int16_t * out)815223c7df5Smlelstv utf8ucs2(const u_int8_t *in, int n, u_int16_t *out)
816223c7df5Smlelstv {
817223c7df5Smlelstv 	uint16_t outch;
818223c7df5Smlelstv 
819223c7df5Smlelstv 	if (n < 1) return 0;
820223c7df5Smlelstv 
821223c7df5Smlelstv 	if (in[0] <= 0x7f) {
822223c7df5Smlelstv 		outch = in[0];
823223c7df5Smlelstv 		if (out)
824223c7df5Smlelstv 			*out = htole16(outch);
825223c7df5Smlelstv 		return 1;
826223c7df5Smlelstv 	} else if (in[0] <= 0xdf) {
827223c7df5Smlelstv 		if (n < 2) return 0;
828223c7df5Smlelstv 		outch = (in[0] & 0x1f) << 6 | (in[1] & 0x3f);
829223c7df5Smlelstv 		if (out)
830223c7df5Smlelstv 			*out = htole16(outch);
831223c7df5Smlelstv 		return 2;
832223c7df5Smlelstv 	} else if (in[0] <= 0xef) {
833223c7df5Smlelstv 		if (n < 3) return 0;
834223c7df5Smlelstv 		outch = (in[0] & 0x1f) << 12 | (in[1] & 0x3f) << 6 | (in[2] & 0x3f);
835223c7df5Smlelstv 		if (out)
836223c7df5Smlelstv 			*out = htole16(outch);
837223c7df5Smlelstv 		return 3;
838223c7df5Smlelstv 	}
839223c7df5Smlelstv 
840223c7df5Smlelstv 	return -1;
841223c7df5Smlelstv }
842223c7df5Smlelstv 
843223c7df5Smlelstv /*
844223c7df5Smlelstv  * Convert UCS-2 string into UTF-8 string
845223c7df5Smlelstv  * return total number of output bytes
846223c7df5Smlelstv  */
847223c7df5Smlelstv static int
ucs2utf8str(const u_int16_t * in,int n,u_int8_t * out,int m)848223c7df5Smlelstv ucs2utf8str(const u_int16_t *in, int n, u_int8_t *out, int m)
849223c7df5Smlelstv {
850223c7df5Smlelstv 	u_int8_t *p;
851223c7df5Smlelstv 	int outlen;
852223c7df5Smlelstv 
853223c7df5Smlelstv 	p = out;
854223c7df5Smlelstv 	while (n > 0 && *in != 0) {
855223c7df5Smlelstv 		outlen = ucs2utf8(in, out ? p : out, m);
856223c7df5Smlelstv 		if (outlen == 0)
857223c7df5Smlelstv 			break;
858223c7df5Smlelstv 		p += outlen;
859223c7df5Smlelstv 		m -= outlen;
860223c7df5Smlelstv 		in += 1;
861223c7df5Smlelstv 		n -= 1;
862223c7df5Smlelstv 	}
863223c7df5Smlelstv 
864223c7df5Smlelstv 	return p - out;
865223c7df5Smlelstv }
866223c7df5Smlelstv 
867223c7df5Smlelstv /*
868223c7df5Smlelstv  * Convert UTF8 string into UCS-2 string
869d28dbf16Sandvar  * return total number of output characters
870223c7df5Smlelstv  */
871223c7df5Smlelstv static int
utf8ucs2str(const u_int8_t * in,int n,u_int16_t * out,int m)872223c7df5Smlelstv utf8ucs2str(const u_int8_t *in, int n, u_int16_t *out, int m)
873223c7df5Smlelstv {
874223c7df5Smlelstv 	u_int16_t *p;
875223c7df5Smlelstv 	int inlen;
876223c7df5Smlelstv 
877223c7df5Smlelstv 	p = out;
878223c7df5Smlelstv 	while (n > 0 && *in != 0) {
879223c7df5Smlelstv 		if (m < 1)
880223c7df5Smlelstv 			break;
881223c7df5Smlelstv 		inlen = utf8ucs2(in, n, out ? p : out);
882223c7df5Smlelstv 		if (inlen <= 0)
883223c7df5Smlelstv 			break;
884223c7df5Smlelstv 		in += inlen;
885223c7df5Smlelstv 		n -= inlen;
886223c7df5Smlelstv 		p += 1;
887223c7df5Smlelstv 		m -= 1;
888223c7df5Smlelstv 	}
889223c7df5Smlelstv 
890223c7df5Smlelstv 	return p - out;
891223c7df5Smlelstv }
892223c7df5Smlelstv 
893223c7df5Smlelstv /*
894223c7df5Smlelstv  * Convert UCS-2 string into 8bit character string
895223c7df5Smlelstv  * return total number of output bytes
896223c7df5Smlelstv  */
897223c7df5Smlelstv static int
ucs2char8str(const u_int16_t * in,int n,u_int8_t * out,int m)898223c7df5Smlelstv ucs2char8str(const u_int16_t *in, int n, u_int8_t *out, int m)
899223c7df5Smlelstv {
900223c7df5Smlelstv 	u_int8_t *p;
901223c7df5Smlelstv 	u_int16_t inch;
902223c7df5Smlelstv 
903223c7df5Smlelstv 	p = out;
904223c7df5Smlelstv 	while (n > 0 && in[0] != 0) {
905223c7df5Smlelstv 		if (m < 1)
906223c7df5Smlelstv 			break;
907223c7df5Smlelstv 		inch = le16toh(in[0]);
908223c7df5Smlelstv 		if (inch > 255)
909223c7df5Smlelstv 			break;
910223c7df5Smlelstv 		if (p)
911223c7df5Smlelstv 			p[0] = inch;
912223c7df5Smlelstv 		p += 1;
913223c7df5Smlelstv 		m -= 1;
914223c7df5Smlelstv 		in += 1;
915223c7df5Smlelstv 		n -= 1;
916223c7df5Smlelstv 	}
917223c7df5Smlelstv 
918223c7df5Smlelstv 	return p - out;
919223c7df5Smlelstv }
920223c7df5Smlelstv 
921223c7df5Smlelstv /*
922223c7df5Smlelstv  * Convert 8bit character string into UCS-2 string
923d28dbf16Sandvar  * return total number of output characters
924223c7df5Smlelstv  */
925223c7df5Smlelstv static int
char8ucs2str(const u_int8_t * in,int n,u_int16_t * out,int m)926223c7df5Smlelstv char8ucs2str(const u_int8_t *in, int n, u_int16_t *out, int m)
927223c7df5Smlelstv {
928223c7df5Smlelstv 	u_int16_t *p;
929223c7df5Smlelstv 
930223c7df5Smlelstv 	p = out;
931223c7df5Smlelstv 	while (n > 0 && in[0] != 0) {
932223c7df5Smlelstv 		if (m < 1)
933223c7df5Smlelstv 			break;
934223c7df5Smlelstv 		if (p)
935223c7df5Smlelstv 			p[0] = htole16(in[0]);
936223c7df5Smlelstv 		p += 1;
937223c7df5Smlelstv 		m -= 1;
938223c7df5Smlelstv 		in += 1;
939223c7df5Smlelstv 		n -= 1;
940223c7df5Smlelstv 	}
941223c7df5Smlelstv 
942223c7df5Smlelstv 	return p - out;
943223c7df5Smlelstv }
944223c7df5Smlelstv 
945223c7df5Smlelstv static void
ucs2pad(u_int16_t * buf,int len,int size)946223c7df5Smlelstv ucs2pad(u_int16_t *buf, int len, int size)
947223c7df5Smlelstv {
948223c7df5Smlelstv 
949223c7df5Smlelstv 	if (len < size-1)
950223c7df5Smlelstv 		buf[len++] = 0x0000;
951223c7df5Smlelstv 	while (len < size)
952223c7df5Smlelstv 		buf[len++] = 0xffff;
953223c7df5Smlelstv }
954223c7df5Smlelstv 
955223c7df5Smlelstv /*
956223c7df5Smlelstv  * Fold UCS-2 character to uppercase
957223c7df5Smlelstv  */
958223c7df5Smlelstv static u_int16_t
ucs2fold(u_int16_t w)959223c7df5Smlelstv ucs2fold(u_int16_t w)
960223c7df5Smlelstv {
961223c7df5Smlelstv 	int low,high,mid;
962223c7df5Smlelstv 	u_int16_t check;
963bbd8666dSmlelstv 	extern const u_int16_t msdosfs_unicode_foldmap[];
964bbd8666dSmlelstv 	extern size_t msdosfs_unicode_foldmap_entries;
965223c7df5Smlelstv 
966223c7df5Smlelstv 	w = le16toh(w);
967223c7df5Smlelstv 
968223c7df5Smlelstv 	low = 0;
969bbd8666dSmlelstv 	high = msdosfs_unicode_foldmap_entries / 2;
970223c7df5Smlelstv 	while (low < high) {
971223c7df5Smlelstv 		mid = (low + high)/2;
972bbd8666dSmlelstv 		check = msdosfs_unicode_foldmap[2*mid+0];
973223c7df5Smlelstv 
974223c7df5Smlelstv 		if (w == check) {
975bbd8666dSmlelstv 			w = msdosfs_unicode_foldmap[2*mid+1];
976223c7df5Smlelstv 			break;
977223c7df5Smlelstv 		}
978223c7df5Smlelstv 
979223c7df5Smlelstv 		if (w < check)
980223c7df5Smlelstv 			high = mid;
981223c7df5Smlelstv 		else
982223c7df5Smlelstv 			low = mid+1;
983223c7df5Smlelstv 	}
984223c7df5Smlelstv 
985223c7df5Smlelstv 	w = le16toh(w);
986223c7df5Smlelstv 
987223c7df5Smlelstv 	return w;
988223c7df5Smlelstv }
989223c7df5Smlelstv 
990223c7df5Smlelstv /*
991223c7df5Smlelstv  * Compare two UCS-2 strings case-insensitive
992223c7df5Smlelstv  *
993223c7df5Smlelstv  * uses the Unicode case folding table
994223c7df5Smlelstv  */
995223c7df5Smlelstv static int
ucs2match(u_int16_t * w1,u_int16_t * w2,int n)996223c7df5Smlelstv ucs2match(u_int16_t *w1, u_int16_t *w2, int n)
997223c7df5Smlelstv {
998223c7df5Smlelstv 	u_int16_t u1, u2;
999223c7df5Smlelstv 
1000223c7df5Smlelstv 	while (n > 0) {
1001223c7df5Smlelstv 		if (*w1 == 0 || *w2 == 0)
1002223c7df5Smlelstv 			return *w1 == *w2;
1003223c7df5Smlelstv 		u1 = ucs2fold(*w1);
1004223c7df5Smlelstv 		u2 = ucs2fold(*w2);
1005223c7df5Smlelstv 		if (u1 != u2)
1006223c7df5Smlelstv 			return 0;
1007223c7df5Smlelstv 		++w1;
1008223c7df5Smlelstv 		++w2;
1009223c7df5Smlelstv 		--n;
1010223c7df5Smlelstv 	}
1011223c7df5Smlelstv 
1012223c7df5Smlelstv 	return 1;
1013223c7df5Smlelstv }
1014223c7df5Smlelstv 
1015223c7df5Smlelstv /*
1016223c7df5Smlelstv  * Compare two 8bit char conversions case-insensitive
1017223c7df5Smlelstv  *
1018223c7df5Smlelstv  * uses the DOS case folding table
1019223c7df5Smlelstv  */
1020223c7df5Smlelstv static int
char8match(u_int16_t * w1,u_int16_t * w2,int n)1021223c7df5Smlelstv char8match(u_int16_t *w1, u_int16_t *w2, int n)
1022223c7df5Smlelstv {
1023223c7df5Smlelstv 	u_int16_t u1, u2;
1024223c7df5Smlelstv 
1025223c7df5Smlelstv 	while (n > 0) {
1026223c7df5Smlelstv 		u1 = le16toh(*w1);
1027223c7df5Smlelstv 		u2 = le16toh(*w2);
1028223c7df5Smlelstv 		if (u1 == 0 || u2 == 0)
1029223c7df5Smlelstv 			return u1 == u2;
1030223c7df5Smlelstv 		if (u1 > 255 || u2 > 255)
1031223c7df5Smlelstv 			return 0;
1032223c7df5Smlelstv 		u1 = u2l[u1 & 0xff];
1033223c7df5Smlelstv 		u2 = u2l[u2 & 0xff];
1034223c7df5Smlelstv 		if (u1 != u2)
1035223c7df5Smlelstv 			return 0;
1036223c7df5Smlelstv 		++w1;
1037223c7df5Smlelstv 		++w2;
1038223c7df5Smlelstv 		--n;
1039223c7df5Smlelstv 	}
1040223c7df5Smlelstv 
1041223c7df5Smlelstv 	return 1;
1042223c7df5Smlelstv }
1043223c7df5Smlelstv 
1044