Home | History | Annotate | Line # | Download | only in msdosfs
      1 /*	$NetBSD: msdosfs_conv.c,v 1.20 2023/06/02 08:51:47 andvar Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
      5  * Copyright (C) 1995, 1997 TooLs GmbH.
      6  * All rights reserved.
      7  * Original code by Paul Popelka (paulp (at) 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 (at) 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 
     51 #if HAVE_NBTOOL_CONFIG_H
     52 #include "nbtool_config.h"
     53 #endif
     54 
     55 #ifndef _KERNEL
     56 #include <assert.h>
     57 #define KASSERT(x)     assert(x)
     58 #endif
     59 
     60 #include <sys/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: msdosfs_conv.c,v 1.20 2023/06/02 08:51:47 andvar Exp $");
     62 
     63 /*
     64  * System include files.
     65  */
     66 #include <sys/param.h>
     67 #include <sys/time.h>
     68 #include <sys/endian.h>
     69 #ifdef _KERNEL
     70 #include <sys/dirent.h>
     71 #include <sys/systm.h>
     72 #include <sys/kernel.h>
     73 #include <sys/vnode.h>
     74 #else
     75 #include <stdio.h>
     76 #include <string.h>
     77 #include <dirent.h>
     78 #include <sys/queue.h>
     79 #endif
     80 #include <dev/clock_subr.h>
     81 
     82 /*
     83  * MSDOSFS include files.
     84  */
     85 #include <fs/msdosfs/direntry.h>
     86 #include <fs/msdosfs/denode.h>
     87 
     88 static int invalidname(const u_int16_t *, int);
     89 
     90 static int ucs2utf8(const u_int16_t *, u_int8_t *, int);
     91 static int utf8ucs2(const u_int8_t *, int, u_int16_t *);
     92 
     93 static int ucs2utf8str(const u_int16_t *, int, u_int8_t *, int);
     94 static int utf8ucs2str(const u_int8_t *, int, u_int16_t *, int);
     95 static int ucs2char8str(const u_int16_t *, int, u_int8_t *, int);
     96 static int char8ucs2str(const u_int8_t *, int, u_int16_t *, int);
     97 
     98 static void ucs2pad(u_int16_t *, int, int);
     99 
    100 static u_int16_t ucs2fold(u_int16_t);
    101 static int ucs2match(u_int16_t *, u_int16_t *, int n);
    102 static int char8match(u_int16_t *, u_int16_t *, int n);
    103 
    104 /*
    105  * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
    106  * interval there were 8 regular years and 2 leap years.
    107  */
    108 #define	DOSBIASYEAR	1980
    109 #define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
    110 /*
    111  * msdos fs can not store dates beyond the year 2234
    112  */
    113 #define DOSMAXYEAR	((DD_YEAR_MASK >> DD_YEAR_SHIFT) + DOSBIASYEAR)
    114 
    115 /*
    116  * Convert the unix version of time to dos's idea of time to be used in
    117  * file timestamps. The passed in unix time is assumed to be in GMT.
    118  */
    119 void
    120 msdosfs_unix2dostime(const struct timespec *tsp, int gmtoff,
    121     u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp)
    122 {
    123 	u_long t;
    124 	struct clock_ymdhms ymd;
    125 
    126 	t = tsp->tv_sec + gmtoff; /* time zone correction */
    127 
    128 	/*
    129 	 * DOS timestamps can not represent dates before 1980.
    130 	 */
    131 	if (t < SECONDSTO1980)
    132 		goto invalid_dos_date;
    133 
    134 	/*
    135 	 * DOS granularity is 2 seconds
    136 	 */
    137 	t &= ~1;
    138 
    139 	/*
    140 	 * Convert to year/month/day/.. format
    141 	 */
    142 	clock_secs_to_ymdhms(t, &ymd);
    143 	if (ymd.dt_year > DOSMAXYEAR)
    144 		goto invalid_dos_date;
    145 
    146 	/*
    147 	 * Now transform to DOS format
    148 	 */
    149 	*ddp = (ymd.dt_day << DD_DAY_SHIFT)
    150 	    + (ymd.dt_mon << DD_MONTH_SHIFT)
    151 	    + ((ymd.dt_year - DOSBIASYEAR) << DD_YEAR_SHIFT);
    152 	if (dhp)
    153 		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
    154 	if (dtp)
    155 		*dtp = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
    156 		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
    157 		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
    158 	return;
    159 
    160 invalid_dos_date:
    161 	*ddp = 0;
    162 	if (dtp)
    163 		*dtp = 0;
    164 	if (dhp)
    165 		*dhp = 0;
    166 }
    167 
    168 /*
    169  * Convert from dos' idea of time to unix'. This will probably only be
    170  * called from the stat(), and fstat() system calls and so probably need
    171  * not be too efficient.
    172  */
    173 void
    174 msdosfs_dos2unixtime(u_int dd, u_int dt, u_int dh, int gmtoff,
    175     struct timespec *tsp)
    176 {
    177 	time_t seconds;
    178 	struct clock_ymdhms ymd;
    179 
    180 	if (dd == 0) {
    181 		/*
    182 		 * Uninitialized field, return the epoch.
    183 		 */
    184 		tsp->tv_sec = 0;
    185 		tsp->tv_nsec = 0;
    186 		return;
    187 	}
    188 
    189 	memset(&ymd, 0, sizeof(ymd));
    190 	ymd.dt_year = ((dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT) + 1980 ;
    191 	ymd.dt_mon = ((dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT);
    192 	ymd.dt_day = ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT);
    193 	ymd.dt_hour = (dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT;
    194 	ymd.dt_min = (dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT;
    195 	ymd.dt_sec = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2;
    196 
    197 	seconds = clock_ymdhms_to_secs(&ymd);
    198 
    199 	tsp->tv_sec = seconds;
    200 	tsp->tv_sec -= gmtoff;	/* time zone correction */
    201 	tsp->tv_nsec = (dh % 100) * 10000000;
    202 }
    203 
    204 static const u_char
    205 unix2dos[256] = {
    206 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
    207 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
    208 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
    209 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
    210 	0,    '!',  0,    '#',  '$',  '%',  '&',  '\'',	/* 20-27 */
    211 	'(',  ')',  0,    '+',  0,    '-',  0,    0,	/* 28-2f */
    212 	'0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
    213 	'8',  '9',  0,    0,    0,    0,    0,    0,	/* 38-3f */
    214 	'@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
    215 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
    216 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
    217 	'X',  'Y',  'Z',  0,    0,    0,    '^',  '_',	/* 58-5f */
    218 	'`',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 60-67 */
    219 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 68-6f */
    220 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 70-77 */
    221 	'X',  'Y',  'Z',  '{',  0,    '}',  '~',  0,	/* 78-7f */
    222 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
    223 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
    224 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
    225 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
    226 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
    227 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
    228 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
    229 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
    230 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
    231 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
    232 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
    233 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
    234 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
    235 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
    236 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
    237 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
    238 };
    239 
    240 static const u_char
    241 dos2unix[256] = {
    242 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 00-07 */
    243 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 08-0f */
    244 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 10-17 */
    245 	 '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?',	/* 18-1f */
    246 	 ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'',	/* 20-27 */
    247 	 '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',	/* 28-2f */
    248 	 '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
    249 	 '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',	/* 38-3f */
    250 	 '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
    251 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
    252 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
    253 	 'X',  'Y',  'Z',  '[', '\\',  ']',  '^',  '_',	/* 58-5f */
    254 	 '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',	/* 60-67 */
    255 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',	/* 68-6f */
    256 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w',	/* 70-77 */
    257 	 'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f,	/* 78-7f */
    258 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
    259 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
    260 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
    261 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7,  '?',	/* 98-9f */
    262 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
    263 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
    264 	 '?',  '?',  '?',  '?',  '?', 0xc1, 0xc2, 0xc0,	/* b0-b7 */
    265 	0xa9,  '?',  '?',  '?',  '?', 0xa2, 0xa5,  '?',	/* b8-bf */
    266 	 '?',  '?',  '?',  '?',  '?',  '?', 0xe3, 0xc3,	/* c0-c7 */
    267 	 '?',  '?',  '?',  '?',  '?',  '?',  '?', 0xa4,	/* c8-cf */
    268 	0xf0, 0xd0, 0xca, 0xcb, 0xc8,  '?', 0xcd, 0xce,	/* d0-d7 */
    269 	0xcf,  '?',  '?',  '?',  '?', 0xa6, 0xcc,  '?',	/* d8-df */
    270 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
    271 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
    272 	0xad, 0xb1,  '?', 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
    273 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2,  '?',  '?',	/* f8-ff */
    274 };
    275 
    276 static const u_char
    277 u2l[256] = {
    278 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
    279 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
    280 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
    281 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
    282 	 ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'', /* 20-27 */
    283 	 '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/', /* 28-2f */
    284 	 '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7', /* 30-37 */
    285 	 '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?', /* 38-3f */
    286 	 '@',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 40-47 */
    287 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 48-4f */
    288 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 50-57 */
    289 	 'x',  'y',  'z',  '[', '\\',  ']',  '^',  '_', /* 58-5f */
    290 	 '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 60-67 */
    291 	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 68-6f */
    292 	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 70-77 */
    293 	 'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f, /* 78-7f */
    294 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
    295 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
    296 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
    297 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
    298 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
    299 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
    300 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
    301 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
    302 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
    303 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
    304 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
    305 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
    306 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
    307 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
    308 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
    309 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
    310 };
    311 
    312 /*
    313  * DOS filenames are made of 2 parts, the name part and the extension part.
    314  * The name part is 8 characters long and the extension part is 3
    315  * characters long.  They may contain trailing blanks if the name or
    316  * extension are not long enough to fill their respective fields.
    317  */
    318 
    319 /*
    320  * Convert a DOS filename to a unix filename. And, return the number of
    321  * characters in the resulting unix filename excluding the terminating
    322  * null.
    323  */
    324 int
    325 msdosfs_dos2unixfn(u_char dn[11], u_char *un, int lower)
    326 {
    327 	int i, j;
    328 	int thislong = 1;
    329 	u_char c;
    330 
    331 	/*
    332 	 * If first char of the filename is SLOT_E5 (0x05), then the real
    333 	 * first char of the filename should be 0xe5. But, they couldn't
    334 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
    335 	 * directory slot. Another dos quirk.
    336 	 */
    337 	if (*dn == SLOT_E5)
    338 		c = dos2unix[0xe5];
    339 	else
    340 		c = dos2unix[*dn];
    341 	*un++ = lower ? u2l[c] : c;
    342 
    343 	/*
    344 	 * Copy the rest into the unix filename string, ignoring
    345 	 * trailing blanks.
    346 	 */
    347 
    348 	for (j=7; (j >= 0) && (dn[j] == ' '); j--)
    349 		;
    350 
    351 	for (i = 1; i <= j; i++) {
    352 		c = dos2unix[dn[i]];
    353 		*un++ = lower ? u2l[c] : c;
    354 		thislong++;
    355 	}
    356 	dn += 8;
    357 
    358 	/*
    359 	 * Now, if there is an extension then put in a period and copy in
    360 	 * the extension.
    361 	 */
    362 	if (*dn != ' ') {
    363 		*un++ = '.';
    364 		thislong++;
    365 		for (i = 0; i < 3 && *dn != ' '; i++) {
    366 			c = dos2unix[*dn++];
    367 			*un++ = lower ? u2l[c] : c;
    368 			thislong++;
    369 		}
    370 	}
    371 	*un++ = 0;
    372 
    373 	return (thislong);
    374 }
    375 
    376 /*
    377  * Convert a unix filename to a DOS filename according to Win95 rules.
    378  * If applicable and gen is not 0, it is inserted into the converted
    379  * filename as a generation number.
    380  * Returns
    381  *	0 if name couldn't be converted
    382  *	1 if the converted name is the same as the original
    383  *	  (no long filename entry necessary for Win95)
    384  *	2 if conversion was successful
    385  *	3 if conversion was successful and generation number was inserted
    386  */
    387 int
    388 msdosfs_unix2dosfn(const u_char *un, u_char dn[12], int unlen, u_int gen)
    389 {
    390 	int i, j, l;
    391 	int conv = 1;
    392 	const u_char *cp, *dp, *dp1;
    393 	u_char gentext[6], *wcp;
    394 	int shortlen;
    395 
    396 	/*
    397 	 * Fill the dos filename string with blanks. These are DOS's pad
    398 	 * characters.
    399 	 */
    400 	for (i = 0; i < 11; i++)
    401 		dn[i] = ' ';
    402 	dn[11] = 0;
    403 
    404 	/*
    405 	 * The filenames "." and ".." are handled specially, since they
    406 	 * don't follow dos filename rules.
    407 	 */
    408 	if (un[0] == '.' && unlen == 1) {
    409 		dn[0] = '.';
    410 		return gen <= 1;
    411 	}
    412 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
    413 		dn[0] = '.';
    414 		dn[1] = '.';
    415 		return gen <= 1;
    416 	}
    417 
    418 	/*
    419 	 * Filenames with only blanks and dots are not allowed!
    420 	 */
    421 	for (cp = un, i = unlen; --i >= 0; cp++)
    422 		if (*cp != ' ' && *cp != '.')
    423 			break;
    424 	if (i < 0)
    425 		return 0;
    426 
    427 	/*
    428 	 * Now find the extension
    429 	 * Note: dot as first char doesn't start extension
    430 	 *	 and trailing dots and blanks are ignored
    431 	 */
    432 	dp = dp1 = 0;
    433 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
    434 		switch (*cp++) {
    435 		case '.':
    436 			if (!dp1)
    437 				dp1 = cp;
    438 			break;
    439 		case ' ':
    440 			break;
    441 		default:
    442 			if (dp1)
    443 				dp = dp1;
    444 			dp1 = 0;
    445 			break;
    446 		}
    447 	}
    448 
    449 	/*
    450 	 * Now convert it
    451 	 */
    452 	if (dp) {
    453 		if (dp1)
    454 			l = dp1 - dp;
    455 		else
    456 			l = unlen - (dp - un);
    457 		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
    458 			if (dp[i] != (dn[j] = unix2dos[dp[i]])
    459 			    && conv != 3)
    460 				conv = 2;
    461 			if (!dn[j]) {
    462 				conv = 3;
    463 				dn[j--] = ' ';
    464 			}
    465 		}
    466 		if (i < l)
    467 			conv = 3;
    468 		dp--;
    469 	} else {
    470 		for (dp = cp; *--dp == ' ' || *dp == '.';);
    471 		dp++;
    472 	}
    473 
    474 	shortlen = (dp - un) <= 8;
    475 
    476 	/*
    477 	 * Now convert the rest of the name
    478 	 */
    479 	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
    480 		if ((*un == ' ') && shortlen)
    481 			dn[j] = ' ';
    482 		else
    483 			dn[j] = unix2dos[*un];
    484 		if ((*un != dn[j])
    485 		    && conv != 3)
    486 			conv = 2;
    487 		if (!dn[j]) {
    488 			conv = 3;
    489 			dn[j--] = ' ';
    490 		}
    491 	}
    492 	if (un < dp)
    493 		conv = 3;
    494 	/*
    495 	 * If we didn't have any chars in filename,
    496 	 * generate a default
    497 	 */
    498 	if (!j)
    499 		dn[0] = '_';
    500 
    501 	/*
    502 	 * The first character cannot be E5,
    503 	 * because that means a deleted entry
    504 	 */
    505 	if (dn[0] == 0xe5)
    506 		dn[0] = SLOT_E5;
    507 
    508 	/*
    509 	 * If there wasn't any char dropped,
    510 	 * there is no place for generation numbers
    511 	 */
    512 	if (conv != 3) {
    513 		if (gen > 1)
    514 			return 0;
    515 		return conv;
    516 	}
    517 
    518 	/*
    519 	 * Now insert the generation number into the filename part
    520 	 */
    521 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
    522 		*--wcp = gen % 10 + '0';
    523 	if (gen)
    524 		return 0;
    525 	for (i = 8; dn[--i] == ' ';);
    526 	i++;
    527 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
    528 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
    529 	dn[i++] = '~';
    530 	while (wcp < gentext + sizeof(gentext))
    531 		dn[i++] = *wcp++;
    532 	return 3;
    533 }
    534 
    535 /*
    536  * Create a Win95 long name directory entry
    537  * Note: assumes that the filename is valid,
    538  *	 i.e. doesn't consist solely of blanks and dots
    539  */
    540 int
    541 msdosfs_unix2winfn(const u_char *un, int unlen, struct winentry *wep, int cnt,
    542     int chksum, int utf8)
    543 {
    544 	u_int16_t wn[WIN_MAXLEN], *p;
    545 	int i, len;
    546 	const u_char *cp;
    547 
    548 	/*
    549 	 * Drop trailing blanks and dots
    550 	 */
    551 	for (cp = un + unlen; unlen > 0; unlen--)
    552 		if (*--cp != ' ' && *cp != '.')
    553 			break;
    554 
    555 	/*
    556 	 * Offset of this entry
    557 	 */
    558 	i = (cnt - 1) * WIN_CHARS;
    559 
    560 	/*
    561 	 * Translate UNIX name to ucs-2
    562 	 */
    563 	len = utf8 ? utf8ucs2str(un, unlen, wn, WIN_MAXLEN) : char8ucs2str(un, unlen, wn, WIN_MAXLEN);
    564 	ucs2pad(wn, len, WIN_MAXLEN);
    565 
    566 	/*
    567 	 * Initialize winentry to some useful default
    568 	 */
    569 	memset(wep, 0xff, sizeof(*wep));
    570 	wep->weCnt = cnt;
    571 	wep->weAttributes = ATTR_WIN95;
    572 	wep->weReserved1 = 0;
    573 	wep->weChksum = chksum;
    574 	wep->weReserved2 = 0;
    575 
    576 	/*
    577 	 * Store name segment into directory entry
    578 	 */
    579 	p = &wn[i];
    580 	memcpy(wep->wePart1, p, sizeof(wep->wePart1));
    581 	p += sizeof(wep->wePart1) / sizeof(*p);
    582 	memcpy(wep->wePart2, p, sizeof(wep->wePart2));
    583 	p += sizeof(wep->wePart2) / sizeof(*p);
    584 	memcpy(wep->wePart3, p, sizeof(wep->wePart3));
    585 
    586 	if (len > i + WIN_CHARS)
    587 		return 1;
    588 
    589 	wep->weCnt |= WIN_LAST;
    590 	return 0;
    591 }
    592 
    593 /*
    594  * Compare our filename to the one in the Win95 entry
    595  * Returns the checksum or -1 if no match
    596  */
    597 int
    598 msdosfs_winChkName(const u_char *un, int unlen, struct winentry *wep,
    599     int chksum, int utf8)
    600 {
    601 	u_int16_t wn[WIN_MAXLEN], *p;
    602 	u_int16_t buf[WIN_CHARS];
    603 	int i, len;
    604 
    605 	/*
    606 	 * First compare checksums
    607 	 */
    608 	if (wep->weCnt & WIN_LAST)
    609 		chksum = wep->weChksum;
    610 	else if (chksum != wep->weChksum)
    611 		chksum = -1;
    612 	if (chksum == -1)
    613 		return -1;
    614 
    615 	/*
    616 	 * Offset of this entry
    617 	 */
    618 	i = ((wep->weCnt & WIN_CNT) - 1) * WIN_CHARS;
    619 
    620 	/*
    621 	 * Translate UNIX name to ucs-2
    622 	 */
    623 	len = utf8 ? utf8ucs2str(un, unlen, wn, WIN_MAXLEN) : char8ucs2str(un, unlen, wn, WIN_MAXLEN);
    624 	ucs2pad(wn, len, WIN_MAXLEN);
    625 
    626 	if (i >= len + 1)
    627 		return -1;
    628 	if ((wep->weCnt & WIN_LAST) && (len - i > WIN_CHARS))
    629 		return -1;
    630 
    631 	/*
    632 	 * Fetch name segment from directory entry
    633 	 */
    634 	p = &buf[0];
    635 	memcpy(p, wep->wePart1, sizeof(wep->wePart1));
    636 	p += sizeof(wep->wePart1) / sizeof(*p);
    637 	memcpy(p, wep->wePart2, sizeof(wep->wePart2));
    638 	p += sizeof(wep->wePart2) / sizeof(*p);
    639 	memcpy(p, wep->wePart3, sizeof(wep->wePart3));
    640 
    641 	/*
    642 	 * And compare name segment
    643 	 */
    644 	if (! (utf8 ? ucs2match(&wn[i], buf, WIN_CHARS) : char8match(&wn[i], buf, WIN_CHARS)))
    645 		return -1;
    646 
    647 	return chksum;
    648 }
    649 
    650 /*
    651  * Convert Win95 filename to dirbuf.
    652  * Returns the checksum or -1 if impossible
    653  */
    654 int
    655 msdosfs_win2unixfn(struct winentry *wep, struct dirent *dp, int chksum,
    656     uint16_t *namlen, int utf8)
    657 {
    658 	u_int16_t wn[WIN_CHARS], *p;
    659 	u_int8_t buf[WIN_CHARS*3];
    660 	int len;
    661 
    662 	if ((wep->weCnt & WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
    663 	    || !(wep->weCnt & WIN_CNT))
    664 		return -1;
    665 
    666 	/*
    667 	 * First compare checksums
    668 	 */
    669 	if (wep->weCnt & WIN_LAST) {
    670 		chksum = wep->weChksum;
    671 		*namlen = 0;
    672 	} else if (chksum != wep->weChksum)
    673 		chksum = -1;
    674 	if (chksum == -1)
    675 		return -1;
    676 
    677 	/*
    678 	 * Fetch name segment from directory entry
    679 	 */
    680 	p = &wn[0];
    681 	memcpy(p, wep->wePart1, sizeof(wep->wePart1));
    682 	p += sizeof(wep->wePart1) / sizeof(*p);
    683 	memcpy(p, wep->wePart2, sizeof(wep->wePart2));
    684 	p += sizeof(wep->wePart2) / sizeof(*p);
    685 	memcpy(p, wep->wePart3, sizeof(wep->wePart3));
    686 
    687 	/*
    688 	 * Don't allow slashes in UNIX names. Discard that entry.
    689 	 */
    690 	if (invalidname(wn, WIN_CHARS))
    691 		return -1;
    692 
    693 	/*
    694 	 * Translate ucs-2 to UNIX name
    695 	 */
    696 	len = utf8 ? ucs2utf8str(wn, WIN_CHARS, buf, sizeof(buf))
    697 	    : ucs2char8str(wn, WIN_CHARS, buf, sizeof(buf));
    698 
    699 	KASSERT(len >= 0);
    700 	KASSERT((size_t)len <= MIN(sizeof(buf), sizeof(dp->d_name)-1));
    701 
    702 	/*
    703 	 * Prepend name segment to directory entry
    704 	 *
    705 	 * This ignores the slot number from the windows entry but
    706 	 * assumes that segments are read in reverse order.
    707 	 *
    708 	 * The UCS-2 name (up to 255 chars) can overflow the UNIX
    709 	 * directory entry (up to 511 bytes). Trailing characters
    710 	 * are silently discarded. This could also end in multiple
    711 	 * files using the same (truncated) name.
    712 	 */
    713 	*namlen += len;
    714 	if (*namlen > sizeof(dp->d_name) - 1)
    715 		*namlen = sizeof(dp->d_name) - 1;
    716 
    717 	KASSERT(*namlen >= len);
    718 
    719 	memmove(&dp->d_name[len], &dp->d_name[0], *namlen - len);
    720 	memcpy(dp->d_name, buf, len);
    721 
    722 	return chksum;
    723 }
    724 
    725 /*
    726  * Compute the checksum of a DOS filename for Win95 use
    727  */
    728 u_int8_t
    729 msdosfs_winChksum(u_int8_t *name)
    730 {
    731 	int i;
    732 	u_int8_t s;
    733 
    734 	for (s = 0, i = 11; --i >= 0; s += *name++)
    735 		s = (s << 7) | (s >> 1);
    736 	return s;
    737 }
    738 
    739 /*
    740  * Determine the number of slots necessary for Win95 names
    741  */
    742 int
    743 msdosfs_winSlotCnt(const u_char *un, int unlen, int utf8)
    744 {
    745 	const u_char *cp;
    746 	int len;
    747 
    748 	/*
    749 	 * Drop trailing blanks and dots
    750 	 */
    751 	for (cp = un + unlen; unlen > 0; unlen--)
    752 		if (*--cp != ' ' && *cp != '.')
    753 			break;
    754 
    755 	len = utf8 ? utf8ucs2str(un, unlen, NULL, WIN_MAXLEN) : unlen;
    756 
    757 	return howmany(len, WIN_CHARS);
    758 }
    759 
    760 /*
    761  * Scan windows name for characters that must not
    762  * appear in a UNIX filename
    763  */
    764 static int
    765 invalidname(const u_int16_t *in, int n)
    766 {
    767 	while (n-- > 0) {
    768 		if (*in++ == '/')
    769 			return 1;
    770 	}
    771 
    772 	return 0;
    773 }
    774 
    775 /*
    776  * Convert UCS-2 character into UTF-8
    777  * return number of output bytes or 0 if output
    778  * buffer is too short
    779  */
    780 static int
    781 ucs2utf8(const u_int16_t *in, u_int8_t *out, int n)
    782 {
    783 	uint16_t inch = le16toh(in[0]);
    784 
    785 	if (inch <= 0x007f) {
    786 		if (n < 1) return 0;
    787 		if (out)
    788 			*out++ = inch;
    789 		return 1;
    790 	} else if (inch <= 0x07ff) {
    791 		if (n < 2) return 0;
    792 		if (out) {
    793 			*out++ = 0xc0 | (inch >> 6);
    794 			*out++ = 0x80 | (inch & 0x3f);
    795 		}
    796 		return 2;
    797 	} else {
    798 		if (n < 3) return 0;
    799 		if (out) {
    800 			*out++ = 0xe0 | (inch >> 12);
    801 			*out++ = 0x80 | ((inch >> 6) & 0x3f);
    802 			*out++ = 0x80 | (inch & 0x3f);
    803 		}
    804 		return 3;
    805 	}
    806 }
    807 
    808 
    809 /*
    810  * Convert UTF-8 bytes into UCS-2 character
    811  * return number of input bytes, 0 if input
    812  * is too short and -1 if input is invalid
    813  */
    814 static int
    815 utf8ucs2(const u_int8_t *in, int n, u_int16_t *out)
    816 {
    817 	uint16_t outch;
    818 
    819 	if (n < 1) return 0;
    820 
    821 	if (in[0] <= 0x7f) {
    822 		outch = in[0];
    823 		if (out)
    824 			*out = htole16(outch);
    825 		return 1;
    826 	} else if (in[0] <= 0xdf) {
    827 		if (n < 2) return 0;
    828 		outch = (in[0] & 0x1f) << 6 | (in[1] & 0x3f);
    829 		if (out)
    830 			*out = htole16(outch);
    831 		return 2;
    832 	} else if (in[0] <= 0xef) {
    833 		if (n < 3) return 0;
    834 		outch = (in[0] & 0x1f) << 12 | (in[1] & 0x3f) << 6 | (in[2] & 0x3f);
    835 		if (out)
    836 			*out = htole16(outch);
    837 		return 3;
    838 	}
    839 
    840 	return -1;
    841 }
    842 
    843 /*
    844  * Convert UCS-2 string into UTF-8 string
    845  * return total number of output bytes
    846  */
    847 static int
    848 ucs2utf8str(const u_int16_t *in, int n, u_int8_t *out, int m)
    849 {
    850 	u_int8_t *p;
    851 	int outlen;
    852 
    853 	p = out;
    854 	while (n > 0 && *in != 0) {
    855 		outlen = ucs2utf8(in, out ? p : out, m);
    856 		if (outlen == 0)
    857 			break;
    858 		p += outlen;
    859 		m -= outlen;
    860 		in += 1;
    861 		n -= 1;
    862 	}
    863 
    864 	return p - out;
    865 }
    866 
    867 /*
    868  * Convert UTF8 string into UCS-2 string
    869  * return total number of output characters
    870  */
    871 static int
    872 utf8ucs2str(const u_int8_t *in, int n, u_int16_t *out, int m)
    873 {
    874 	u_int16_t *p;
    875 	int inlen;
    876 
    877 	p = out;
    878 	while (n > 0 && *in != 0) {
    879 		if (m < 1)
    880 			break;
    881 		inlen = utf8ucs2(in, n, out ? p : out);
    882 		if (inlen <= 0)
    883 			break;
    884 		in += inlen;
    885 		n -= inlen;
    886 		p += 1;
    887 		m -= 1;
    888 	}
    889 
    890 	return p - out;
    891 }
    892 
    893 /*
    894  * Convert UCS-2 string into 8bit character string
    895  * return total number of output bytes
    896  */
    897 static int
    898 ucs2char8str(const u_int16_t *in, int n, u_int8_t *out, int m)
    899 {
    900 	u_int8_t *p;
    901 	u_int16_t inch;
    902 
    903 	p = out;
    904 	while (n > 0 && in[0] != 0) {
    905 		if (m < 1)
    906 			break;
    907 		inch = le16toh(in[0]);
    908 		if (inch > 255)
    909 			break;
    910 		if (p)
    911 			p[0] = inch;
    912 		p += 1;
    913 		m -= 1;
    914 		in += 1;
    915 		n -= 1;
    916 	}
    917 
    918 	return p - out;
    919 }
    920 
    921 /*
    922  * Convert 8bit character string into UCS-2 string
    923  * return total number of output characters
    924  */
    925 static int
    926 char8ucs2str(const u_int8_t *in, int n, u_int16_t *out, int m)
    927 {
    928 	u_int16_t *p;
    929 
    930 	p = out;
    931 	while (n > 0 && in[0] != 0) {
    932 		if (m < 1)
    933 			break;
    934 		if (p)
    935 			p[0] = htole16(in[0]);
    936 		p += 1;
    937 		m -= 1;
    938 		in += 1;
    939 		n -= 1;
    940 	}
    941 
    942 	return p - out;
    943 }
    944 
    945 static void
    946 ucs2pad(u_int16_t *buf, int len, int size)
    947 {
    948 
    949 	if (len < size-1)
    950 		buf[len++] = 0x0000;
    951 	while (len < size)
    952 		buf[len++] = 0xffff;
    953 }
    954 
    955 /*
    956  * Fold UCS-2 character to uppercase
    957  */
    958 static u_int16_t
    959 ucs2fold(u_int16_t w)
    960 {
    961 	int low,high,mid;
    962 	u_int16_t check;
    963 	extern const u_int16_t msdosfs_unicode_foldmap[];
    964 	extern size_t msdosfs_unicode_foldmap_entries;
    965 
    966 	w = le16toh(w);
    967 
    968 	low = 0;
    969 	high = msdosfs_unicode_foldmap_entries / 2;
    970 	while (low < high) {
    971 		mid = (low + high)/2;
    972 		check = msdosfs_unicode_foldmap[2*mid+0];
    973 
    974 		if (w == check) {
    975 			w = msdosfs_unicode_foldmap[2*mid+1];
    976 			break;
    977 		}
    978 
    979 		if (w < check)
    980 			high = mid;
    981 		else
    982 			low = mid+1;
    983 	}
    984 
    985 	w = le16toh(w);
    986 
    987 	return w;
    988 }
    989 
    990 /*
    991  * Compare two UCS-2 strings case-insensitive
    992  *
    993  * uses the Unicode case folding table
    994  */
    995 static int
    996 ucs2match(u_int16_t *w1, u_int16_t *w2, int n)
    997 {
    998 	u_int16_t u1, u2;
    999 
   1000 	while (n > 0) {
   1001 		if (*w1 == 0 || *w2 == 0)
   1002 			return *w1 == *w2;
   1003 		u1 = ucs2fold(*w1);
   1004 		u2 = ucs2fold(*w2);
   1005 		if (u1 != u2)
   1006 			return 0;
   1007 		++w1;
   1008 		++w2;
   1009 		--n;
   1010 	}
   1011 
   1012 	return 1;
   1013 }
   1014 
   1015 /*
   1016  * Compare two 8bit char conversions case-insensitive
   1017  *
   1018  * uses the DOS case folding table
   1019  */
   1020 static int
   1021 char8match(u_int16_t *w1, u_int16_t *w2, int n)
   1022 {
   1023 	u_int16_t u1, u2;
   1024 
   1025 	while (n > 0) {
   1026 		u1 = le16toh(*w1);
   1027 		u2 = le16toh(*w2);
   1028 		if (u1 == 0 || u2 == 0)
   1029 			return u1 == u2;
   1030 		if (u1 > 255 || u2 > 255)
   1031 			return 0;
   1032 		u1 = u2l[u1 & 0xff];
   1033 		u2 = u2l[u2 & 0xff];
   1034 		if (u1 != u2)
   1035 			return 0;
   1036 		++w1;
   1037 		++w2;
   1038 		--n;
   1039 	}
   1040 
   1041 	return 1;
   1042 }
   1043 
   1044