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