xref: /freebsd/sys/fs/msdosfs/msdosfs_conv.c (revision 9b0802c90b408f0acea146ee275d0de0a40d0783)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
2952a6212SJordan K. Hubbard /*	$NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $	*/
327a0bc89SDoug Rabson 
4952a6212SJordan K. Hubbard /*-
5952a6212SJordan K. Hubbard  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
6952a6212SJordan K. Hubbard  * Copyright (C) 1995, 1997 TooLs GmbH.
7952a6212SJordan K. Hubbard  * All rights reserved.
8952a6212SJordan K. Hubbard  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9952a6212SJordan K. Hubbard  *
10952a6212SJordan K. Hubbard  * Redistribution and use in source and binary forms, with or without
11952a6212SJordan K. Hubbard  * modification, are permitted provided that the following conditions
12952a6212SJordan K. Hubbard  * are met:
13952a6212SJordan K. Hubbard  * 1. Redistributions of source code must retain the above copyright
14952a6212SJordan K. Hubbard  *    notice, this list of conditions and the following disclaimer.
15952a6212SJordan K. Hubbard  * 2. Redistributions in binary form must reproduce the above copyright
16952a6212SJordan K. Hubbard  *    notice, this list of conditions and the following disclaimer in the
17952a6212SJordan K. Hubbard  *    documentation and/or other materials provided with the distribution.
18952a6212SJordan K. Hubbard  * 3. All advertising materials mentioning features or use of this software
19952a6212SJordan K. Hubbard  *    must display the following acknowledgement:
20952a6212SJordan K. Hubbard  *	This product includes software developed by TooLs GmbH.
21952a6212SJordan K. Hubbard  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22952a6212SJordan K. Hubbard  *    derived from this software without specific prior written permission.
23952a6212SJordan K. Hubbard  *
24952a6212SJordan K. Hubbard  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25952a6212SJordan K. Hubbard  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26952a6212SJordan K. Hubbard  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27952a6212SJordan K. Hubbard  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28952a6212SJordan K. Hubbard  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29952a6212SJordan K. Hubbard  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30952a6212SJordan K. Hubbard  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31952a6212SJordan K. Hubbard  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32952a6212SJordan K. Hubbard  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33952a6212SJordan K. Hubbard  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34952a6212SJordan K. Hubbard  */
35d167cf6fSWarner Losh /*-
3627a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3727a0bc89SDoug Rabson  *
3827a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
3927a0bc89SDoug Rabson  * it, and don't remove this notice.
4027a0bc89SDoug Rabson  *
4127a0bc89SDoug Rabson  * This software is provided "as is".
4227a0bc89SDoug Rabson  *
4327a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4427a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4527a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4627a0bc89SDoug Rabson  * any damages caused by this software.
4727a0bc89SDoug Rabson  *
4827a0bc89SDoug Rabson  * October 1992
4927a0bc89SDoug Rabson  */
5027a0bc89SDoug Rabson 
5127a0bc89SDoug Rabson #include <sys/param.h>
52952a6212SJordan K. Hubbard #include <sys/systm.h>
53952a6212SJordan K. Hubbard #include <sys/dirent.h>
54c4f02a89SMax Khon #include <sys/iconv.h>
55c4f02a89SMax Khon #include <sys/malloc.h>
569b0802c9SBruce Evans #include <sys/mount.h>
579b0802c9SBruce Evans 
589b0802c9SBruce Evans #include <fs/msdosfs/bpb.h>
599b0802c9SBruce Evans #include <fs/msdosfs/direntry.h>
609b0802c9SBruce Evans #include <fs/msdosfs/msdosfsmount.h>
61c4f02a89SMax Khon 
62c4f02a89SMax Khon extern struct iconv_functions *msdosfs_iconv;
6327a0bc89SDoug Rabson 
64bddcdc51STim J. Robbins static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle);
65c4f02a89SMax Khon static u_int16_t dos2unixchr(const u_char **, size_t *, int, struct msdosfsmount *);
66c4f02a89SMax Khon static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
67c4f02a89SMax Khon static u_int16_t win2unixchr(u_int16_t, struct msdosfsmount *);
68c4f02a89SMax Khon static u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *);
69c4f02a89SMax Khon 
704cdb1483SNate Lawson static char	*nambuf_ptr;
714cdb1483SNate Lawson static size_t	nambuf_len;
7258ad326bSNate Lawson static int	nambuf_last_id;
7313df76f2SAndrey A. Chernov 
7427a0bc89SDoug Rabson /*
75cce0eefdSMike Smith  * 0 - character disallowed in long file name.
76cce0eefdSMike Smith  * 1 - character should be replaced by '_' in DOS file name,
77cce0eefdSMike Smith  *     and generation number inserted.
78cce0eefdSMike Smith  * 2 - character ('.' and ' ') should be skipped in DOS file name,
79cce0eefdSMike Smith  *     and generation number inserted.
80cce0eefdSMike Smith  */
81952a6212SJordan K. Hubbard static u_char
82952a6212SJordan K. Hubbard unix2dos[256] = {
83c4f02a89SMax Khon /* iso8859-1 -> cp850 */
84952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
85952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
86952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
87952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
88cce0eefdSMike Smith 	2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
89cce0eefdSMike Smith 	0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,	/* 28-2f */
90952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
91cce0eefdSMike Smith 	0x38, 0x39, 0,    1,    0,    1,    0,    0,	/* 38-3f */
92952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
93952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
94952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
95cce0eefdSMike Smith 	0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f,	/* 58-5f */
96952a6212SJordan K. Hubbard 	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
97952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
98952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
99952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
100952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
101952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
102952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
103952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
104952a6212SJordan K. Hubbard 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
105952a6212SJordan K. Hubbard 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
106952a6212SJordan K. Hubbard 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
107952a6212SJordan K. Hubbard 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
108952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
109952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
110952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
111952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
112952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
113952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
114952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
115952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
116952a6212SJordan K. Hubbard };
117952a6212SJordan K. Hubbard 
118952a6212SJordan K. Hubbard static u_char
119952a6212SJordan K. Hubbard dos2unix[256] = {
120c4f02a89SMax Khon /* cp850 -> iso8859-1 */
121952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
122952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
123952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
124952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
125952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
126952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
127952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
128952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
129952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
130952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
131952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
132952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
133952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
134952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
135952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
136952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
137952a6212SJordan K. Hubbard 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
138952a6212SJordan K. Hubbard 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
139952a6212SJordan K. Hubbard 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
140952a6212SJordan K. Hubbard 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
141952a6212SJordan K. Hubbard 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
142952a6212SJordan K. Hubbard 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
143952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
144952a6212SJordan K. Hubbard 	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
145952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
146952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
147952a6212SJordan K. Hubbard 	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
148952a6212SJordan K. Hubbard 	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
149952a6212SJordan K. Hubbard 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
150952a6212SJordan K. Hubbard 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
151952a6212SJordan K. Hubbard 	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
152952a6212SJordan K. Hubbard 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
153952a6212SJordan K. Hubbard };
154952a6212SJordan K. Hubbard 
155952a6212SJordan K. Hubbard static u_char
156952a6212SJordan K. Hubbard u2l[256] = {
157c4f02a89SMax Khon /* tolower */
158952a6212SJordan K. Hubbard 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
159952a6212SJordan K. Hubbard 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
160952a6212SJordan K. Hubbard 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
161952a6212SJordan K. Hubbard 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
162952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
163952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
164952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
165952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
166952a6212SJordan K. Hubbard 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
167952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
168952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
169952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
170952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
171952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
172952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
173952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
174952a6212SJordan K. Hubbard 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
175952a6212SJordan K. Hubbard 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
176952a6212SJordan K. Hubbard 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
177952a6212SJordan K. Hubbard 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
178952a6212SJordan K. Hubbard 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
179952a6212SJordan K. Hubbard 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
180952a6212SJordan K. Hubbard 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
181952a6212SJordan K. Hubbard 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
182952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
183952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
184952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
185952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
186952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
187952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
188952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
189952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
190952a6212SJordan K. Hubbard };
19127a0bc89SDoug Rabson 
1927391f611SAndrey A. Chernov static u_char
1937391f611SAndrey A. Chernov l2u[256] = {
194c4f02a89SMax Khon /* toupper */
1957391f611SAndrey A. Chernov 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
1967391f611SAndrey A. Chernov 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
1977391f611SAndrey A. Chernov 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
1987391f611SAndrey A. Chernov 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
1997391f611SAndrey A. Chernov 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
2007391f611SAndrey A. Chernov 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
2017391f611SAndrey A. Chernov 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
2027391f611SAndrey A. Chernov 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
2037391f611SAndrey A. Chernov 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
2047391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
2057391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
2067391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
2077391f611SAndrey A. Chernov 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
2087391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
2097391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
2107391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
2117391f611SAndrey A. Chernov 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
2127391f611SAndrey A. Chernov 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
2137391f611SAndrey A. Chernov 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
2147391f611SAndrey A. Chernov 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
2157391f611SAndrey A. Chernov 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
2167391f611SAndrey A. Chernov 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
2177391f611SAndrey A. Chernov 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
2187391f611SAndrey A. Chernov 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
2197391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
2207391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
2217391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
2227391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
2237391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
2247391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
2257391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
2267391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
2277391f611SAndrey A. Chernov };
2287391f611SAndrey A. Chernov 
22927a0bc89SDoug Rabson /*
23027a0bc89SDoug Rabson  * DOS filenames are made of 2 parts, the name part and the extension part.
23127a0bc89SDoug Rabson  * The name part is 8 characters long and the extension part is 3
23227a0bc89SDoug Rabson  * characters long.  They may contain trailing blanks if the name or
23327a0bc89SDoug Rabson  * extension are not long enough to fill their respective fields.
23427a0bc89SDoug Rabson  */
23527a0bc89SDoug Rabson 
23627a0bc89SDoug Rabson /*
23727a0bc89SDoug Rabson  * Convert a DOS filename to a unix filename. And, return the number of
23827a0bc89SDoug Rabson  * characters in the resulting unix filename excluding the terminating
23927a0bc89SDoug Rabson  * null.
24027a0bc89SDoug Rabson  */
24127a0bc89SDoug Rabson int
242c4f02a89SMax Khon dos2unixfn(dn, un, lower, pmp)
24327a0bc89SDoug Rabson 	u_char dn[11];
24427a0bc89SDoug Rabson 	u_char *un;
2455114f1d7SAndrey A. Chernov 	int lower;
246c4f02a89SMax Khon 	struct msdosfsmount *pmp;
24727a0bc89SDoug Rabson {
248bddcdc51STim J. Robbins 	size_t i;
249c4f02a89SMax Khon 	int thislong = 0;
250c4f02a89SMax Khon 	u_int16_t c;
25127a0bc89SDoug Rabson 
25227a0bc89SDoug Rabson 	/*
25327a0bc89SDoug Rabson 	 * If first char of the filename is SLOT_E5 (0x05), then the real
25427a0bc89SDoug Rabson 	 * first char of the filename should be 0xe5. But, they couldn't
25527a0bc89SDoug Rabson 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
25627a0bc89SDoug Rabson 	 * directory slot. Another dos quirk.
25727a0bc89SDoug Rabson 	 */
258952a6212SJordan K. Hubbard 	if (*dn == SLOT_E5)
259c4f02a89SMax Khon 		*dn = 0xe5;
26027a0bc89SDoug Rabson 
261952a6212SJordan K. Hubbard 	/*
262952a6212SJordan K. Hubbard 	 * Copy the name portion into the unix filename string.
263952a6212SJordan K. Hubbard 	 */
264c4f02a89SMax Khon 	for (i = 8; i > 0 && *dn != ' ';) {
265cc005bb9SMaxim Konovalov 		c = dos2unixchr((const u_char **)&dn, &i, lower & LCASE_BASE,
266cc005bb9SMaxim Konovalov 		    pmp);
267c4f02a89SMax Khon 		if (c & 0xff00) {
268c4f02a89SMax Khon 			*un++ = c >> 8;
269952a6212SJordan K. Hubbard 			thislong++;
270952a6212SJordan K. Hubbard 		}
271c4f02a89SMax Khon 		*un++ = c;
272c4f02a89SMax Khon 		thislong++;
273c4f02a89SMax Khon 	}
274c4f02a89SMax Khon 	dn += i;
275952a6212SJordan K. Hubbard 
276952a6212SJordan K. Hubbard 	/*
277952a6212SJordan K. Hubbard 	 * Now, if there is an extension then put in a period and copy in
278952a6212SJordan K. Hubbard 	 * the extension.
279952a6212SJordan K. Hubbard 	 */
280952a6212SJordan K. Hubbard 	if (*dn != ' ') {
281952a6212SJordan K. Hubbard 		*un++ = '.';
282952a6212SJordan K. Hubbard 		thislong++;
283c4f02a89SMax Khon 		for (i = 3; i > 0 && *dn != ' ';) {
284cc005bb9SMaxim Konovalov 			c = dos2unixchr((const u_char **)&dn, &i,
285cc005bb9SMaxim Konovalov 			    lower & LCASE_EXT, pmp);
286c4f02a89SMax Khon 			if (c & 0xff00) {
287c4f02a89SMax Khon 				*un++ = c >> 8;
288c4f02a89SMax Khon 				thislong++;
289c4f02a89SMax Khon 			}
290c4f02a89SMax Khon 			*un++ = c;
291952a6212SJordan K. Hubbard 			thislong++;
292952a6212SJordan K. Hubbard 		}
293952a6212SJordan K. Hubbard 	}
294952a6212SJordan K. Hubbard 	*un++ = 0;
295952a6212SJordan K. Hubbard 
296952a6212SJordan K. Hubbard 	return (thislong);
29727a0bc89SDoug Rabson }
29827a0bc89SDoug Rabson 
29927a0bc89SDoug Rabson /*
300952a6212SJordan K. Hubbard  * Convert a unix filename to a DOS filename according to Win95 rules.
301952a6212SJordan K. Hubbard  * If applicable and gen is not 0, it is inserted into the converted
302952a6212SJordan K. Hubbard  * filename as a generation number.
303952a6212SJordan K. Hubbard  * Returns
304952a6212SJordan K. Hubbard  *	0 if name couldn't be converted
305952a6212SJordan K. Hubbard  *	1 if the converted name is the same as the original
306952a6212SJordan K. Hubbard  *	  (no long filename entry necessary for Win95)
307952a6212SJordan K. Hubbard  *	2 if conversion was successful
308952a6212SJordan K. Hubbard  *	3 if conversion was successful and generation number was inserted
30927a0bc89SDoug Rabson  */
310952a6212SJordan K. Hubbard int
311c4f02a89SMax Khon unix2dosfn(un, dn, unlen, gen, pmp)
312952a6212SJordan K. Hubbard 	const u_char *un;
313952a6212SJordan K. Hubbard 	u_char dn[12];
314bddcdc51STim J. Robbins 	size_t unlen;
315952a6212SJordan K. Hubbard 	u_int gen;
316c4f02a89SMax Khon 	struct msdosfsmount *pmp;
31727a0bc89SDoug Rabson {
318bddcdc51STim J. Robbins 	ssize_t i, j;
319bddcdc51STim J. Robbins 	int l;
320952a6212SJordan K. Hubbard 	int conv = 1;
321952a6212SJordan K. Hubbard 	const u_char *cp, *dp, *dp1;
322952a6212SJordan K. Hubbard 	u_char gentext[6], *wcp;
323c4f02a89SMax Khon 	u_int16_t c;
32427a0bc89SDoug Rabson 
32527a0bc89SDoug Rabson 	/*
32627a0bc89SDoug Rabson 	 * Fill the dos filename string with blanks. These are DOS's pad
32727a0bc89SDoug Rabson 	 * characters.
32827a0bc89SDoug Rabson 	 */
329952a6212SJordan K. Hubbard 	for (i = 0; i < 11; i++)
33027a0bc89SDoug Rabson 		dn[i] = ' ';
331952a6212SJordan K. Hubbard 	dn[11] = 0;
33227a0bc89SDoug Rabson 
33327a0bc89SDoug Rabson 	/*
33427a0bc89SDoug Rabson 	 * The filenames "." and ".." are handled specially, since they
33527a0bc89SDoug Rabson 	 * don't follow dos filename rules.
33627a0bc89SDoug Rabson 	 */
33727a0bc89SDoug Rabson 	if (un[0] == '.' && unlen == 1) {
33827a0bc89SDoug Rabson 		dn[0] = '.';
339952a6212SJordan K. Hubbard 		return gen <= 1;
34027a0bc89SDoug Rabson 	}
34127a0bc89SDoug Rabson 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
34227a0bc89SDoug Rabson 		dn[0] = '.';
34327a0bc89SDoug Rabson 		dn[1] = '.';
344952a6212SJordan K. Hubbard 		return gen <= 1;
34527a0bc89SDoug Rabson 	}
34627a0bc89SDoug Rabson 
34727a0bc89SDoug Rabson 	/*
348952a6212SJordan K. Hubbard 	 * Filenames with only blanks and dots are not allowed!
34927a0bc89SDoug Rabson 	 */
350952a6212SJordan K. Hubbard 	for (cp = un, i = unlen; --i >= 0; cp++)
351952a6212SJordan K. Hubbard 		if (*cp != ' ' && *cp != '.')
352952a6212SJordan K. Hubbard 			break;
353952a6212SJordan K. Hubbard 	if (i < 0)
354952a6212SJordan K. Hubbard 		return 0;
355952a6212SJordan K. Hubbard 
356cce0eefdSMike Smith 
357cce0eefdSMike Smith 	/*
358cce0eefdSMike Smith 	 * Filenames with some characters are not allowed!
359cce0eefdSMike Smith 	 */
360c4f02a89SMax Khon 	for (cp = un, i = unlen; i > 0;)
361c4f02a89SMax Khon 		if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
362cce0eefdSMike Smith 			return 0;
363cce0eefdSMike Smith 
364952a6212SJordan K. Hubbard 	/*
365952a6212SJordan K. Hubbard 	 * Now find the extension
366952a6212SJordan K. Hubbard 	 * Note: dot as first char doesn't start extension
367952a6212SJordan K. Hubbard 	 *	 and trailing dots and blanks are ignored
368c4f02a89SMax Khon 	 * Note(2003/7): It seems recent Windows has
369c4f02a89SMax Khon 	 *	 defferent rule than this code, that Windows
370c4f02a89SMax Khon 	 *	 ignores all dots before extension, and use all
371c4f02a89SMax Khon 	 * 	 chars as filename except for dots.
372952a6212SJordan K. Hubbard 	 */
373952a6212SJordan K. Hubbard 	dp = dp1 = 0;
374952a6212SJordan K. Hubbard 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
375952a6212SJordan K. Hubbard 		switch (*cp++) {
376952a6212SJordan K. Hubbard 		case '.':
377952a6212SJordan K. Hubbard 			if (!dp1)
378952a6212SJordan K. Hubbard 				dp1 = cp;
379952a6212SJordan K. Hubbard 			break;
380952a6212SJordan K. Hubbard 		case ' ':
381952a6212SJordan K. Hubbard 			break;
382952a6212SJordan K. Hubbard 		default:
383952a6212SJordan K. Hubbard 			if (dp1)
384952a6212SJordan K. Hubbard 				dp = dp1;
385952a6212SJordan K. Hubbard 			dp1 = 0;
386952a6212SJordan K. Hubbard 			break;
387952a6212SJordan K. Hubbard 		}
38827a0bc89SDoug Rabson 	}
38927a0bc89SDoug Rabson 
39027a0bc89SDoug Rabson 	/*
391697ab829SR. Imura 	 * Now convert it (this part is for extension).
392697ab829SR. Imura 	 * As Windows XP do, if it's not ascii char,
393697ab829SR. Imura 	 * this function should return 2 or 3, so that checkng out Unicode name.
39427a0bc89SDoug Rabson 	 */
395952a6212SJordan K. Hubbard 	if (dp) {
396952a6212SJordan K. Hubbard 		if (dp1)
397952a6212SJordan K. Hubbard 			l = dp1 - dp;
398952a6212SJordan K. Hubbard 		else
399952a6212SJordan K. Hubbard 			l = unlen - (dp - un);
400c4f02a89SMax Khon 		for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
401c4f02a89SMax Khon 			c = unix2doschr(&cp, (size_t *)&i, pmp);
402c4f02a89SMax Khon 			if (c & 0xff00) {
403c4f02a89SMax Khon 				dn[j] = c >> 8;
404c4f02a89SMax Khon 				if (++j < 11) {
405c4f02a89SMax Khon 					dn[j] = c;
406697ab829SR. Imura 					if (conv != 3)
407697ab829SR. Imura 						conv = 2;
408c4f02a89SMax Khon 					continue;
409c4f02a89SMax Khon 				} else {
410c4f02a89SMax Khon 					conv = 3;
411c4f02a89SMax Khon 					dn[j-1] = ' ';
412c4f02a89SMax Khon 					break;
413c4f02a89SMax Khon 				}
414c4f02a89SMax Khon 			} else {
415c4f02a89SMax Khon 				dn[j] = c;
416c4f02a89SMax Khon 			}
417697ab829SR. Imura 			if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
418952a6212SJordan K. Hubbard 				conv = 2;
419cce0eefdSMike Smith 			if (dn[j] == 1) {
420cce0eefdSMike Smith 				conv = 3;
421cce0eefdSMike Smith 				dn[j] = '_';
422cce0eefdSMike Smith 			}
423cce0eefdSMike Smith 			if (dn[j] == 2) {
424952a6212SJordan K. Hubbard 				conv = 3;
425952a6212SJordan K. Hubbard 				dn[j--] = ' ';
426952a6212SJordan K. Hubbard 			}
427952a6212SJordan K. Hubbard 		}
428c4f02a89SMax Khon 		if (i > 0)
429952a6212SJordan K. Hubbard 			conv = 3;
430952a6212SJordan K. Hubbard 		dp--;
431952a6212SJordan K. Hubbard 	} else {
432952a6212SJordan K. Hubbard 		for (dp = cp; *--dp == ' ' || *dp == '.';);
433952a6212SJordan K. Hubbard 		dp++;
434952a6212SJordan K. Hubbard 	}
435952a6212SJordan K. Hubbard 
436952a6212SJordan K. Hubbard 	/*
437952a6212SJordan K. Hubbard 	 * Now convert the rest of the name
438952a6212SJordan K. Hubbard 	 */
439c4f02a89SMax Khon 	for (i = dp - un, j = 0; un < dp && j < 8; j++) {
440bddcdc51STim J. Robbins 		c = unix2doschr(&un, &i, pmp);
441c4f02a89SMax Khon 		if (c & 0xff00) {
442c4f02a89SMax Khon 			dn[j] = c >> 8;
443c4f02a89SMax Khon 			if (++j < 8) {
444c4f02a89SMax Khon 				dn[j] = c;
445697ab829SR. Imura 				if (conv != 3)
446697ab829SR. Imura 					conv = 2;
447c4f02a89SMax Khon 				continue;
448c4f02a89SMax Khon 			} else {
449c4f02a89SMax Khon 				conv = 3;
450c4f02a89SMax Khon 				dn[j-1] = ' ';
451c4f02a89SMax Khon 				break;
452c4f02a89SMax Khon 			}
453c4f02a89SMax Khon 		} else {
454c4f02a89SMax Khon 			dn[j] = c;
455c4f02a89SMax Khon 		}
456697ab829SR. Imura 		if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
457952a6212SJordan K. Hubbard 			conv = 2;
458cce0eefdSMike Smith 		if (dn[j] == 1) {
459cce0eefdSMike Smith 			conv = 3;
460cce0eefdSMike Smith 			dn[j] = '_';
461cce0eefdSMike Smith 		}
462cce0eefdSMike Smith 		if (dn[j] == 2) {
463952a6212SJordan K. Hubbard 			conv = 3;
464952a6212SJordan K. Hubbard 			dn[j--] = ' ';
465952a6212SJordan K. Hubbard 		}
466952a6212SJordan K. Hubbard 	}
467952a6212SJordan K. Hubbard 	if (un < dp)
468952a6212SJordan K. Hubbard 		conv = 3;
469952a6212SJordan K. Hubbard 	/*
470952a6212SJordan K. Hubbard 	 * If we didn't have any chars in filename,
471952a6212SJordan K. Hubbard 	 * generate a default
472952a6212SJordan K. Hubbard 	 */
473952a6212SJordan K. Hubbard 	if (!j)
474952a6212SJordan K. Hubbard 		dn[0] = '_';
475952a6212SJordan K. Hubbard 
476952a6212SJordan K. Hubbard 	/*
477952a6212SJordan K. Hubbard 	 * If there wasn't any char dropped,
478952a6212SJordan K. Hubbard 	 * there is no place for generation numbers
47927a0bc89SDoug Rabson 	 */
480952a6212SJordan K. Hubbard 	if (conv != 3) {
481952a6212SJordan K. Hubbard 		if (gen > 1)
482c4f02a89SMax Khon 			conv = 0;
483c4f02a89SMax Khon 		goto done;
484952a6212SJordan K. Hubbard 	}
485952a6212SJordan K. Hubbard 
486952a6212SJordan K. Hubbard 	/*
487952a6212SJordan K. Hubbard 	 * Now insert the generation number into the filename part
488952a6212SJordan K. Hubbard 	 */
489011cdb57SDmitrij Tejblum 	if (gen == 0)
490c4f02a89SMax Khon 		goto done;
491952a6212SJordan K. Hubbard 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
492952a6212SJordan K. Hubbard 		*--wcp = gen % 10 + '0';
493c4f02a89SMax Khon 	if (gen) {
494c4f02a89SMax Khon 		conv = 0;
495c4f02a89SMax Khon 		goto done;
496c4f02a89SMax Khon 	}
497952a6212SJordan K. Hubbard 	for (i = 8; dn[--i] == ' ';);
498952a6212SJordan K. Hubbard 	i++;
499952a6212SJordan K. Hubbard 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
500952a6212SJordan K. Hubbard 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
501c4f02a89SMax Khon 	/*
502c4f02a89SMax Khon 	 * Correct posision to where insert the generation number
503c4f02a89SMax Khon 	 */
504c4f02a89SMax Khon 	cp = dn;
505c4f02a89SMax Khon 	i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
506c4f02a89SMax Khon 
507952a6212SJordan K. Hubbard 	dn[i++] = '~';
508952a6212SJordan K. Hubbard 	while (wcp < gentext + sizeof(gentext))
509952a6212SJordan K. Hubbard 		dn[i++] = *wcp++;
510c4f02a89SMax Khon 
511c4f02a89SMax Khon 	/*
512c4f02a89SMax Khon 	 * Tail of the filename should be space
513c4f02a89SMax Khon 	 */
514c4f02a89SMax Khon 	while (i < 8)
515c4f02a89SMax Khon 		dn[i++] = ' ';
516c4f02a89SMax Khon 	conv = 3;
517c4f02a89SMax Khon 
518c4f02a89SMax Khon done:
519c4f02a89SMax Khon 	/*
520c4f02a89SMax Khon 	 * The first character cannot be E5,
521c4f02a89SMax Khon 	 * because that means a deleted entry
522c4f02a89SMax Khon 	 */
523c4f02a89SMax Khon 	if (dn[0] == 0xe5)
524c4f02a89SMax Khon 		dn[0] = SLOT_E5;
525c4f02a89SMax Khon 
526c4f02a89SMax Khon 	return conv;
527952a6212SJordan K. Hubbard }
528952a6212SJordan K. Hubbard 
529952a6212SJordan K. Hubbard /*
530952a6212SJordan K. Hubbard  * Create a Win95 long name directory entry
531952a6212SJordan K. Hubbard  * Note: assumes that the filename is valid,
532952a6212SJordan K. Hubbard  *	 i.e. doesn't consist solely of blanks and dots
533952a6212SJordan K. Hubbard  */
534952a6212SJordan K. Hubbard int
535c4f02a89SMax Khon unix2winfn(un, unlen, wep, cnt, chksum, pmp)
536952a6212SJordan K. Hubbard 	const u_char *un;
537bddcdc51STim J. Robbins 	size_t unlen;
538952a6212SJordan K. Hubbard 	struct winentry *wep;
539952a6212SJordan K. Hubbard 	int cnt;
540952a6212SJordan K. Hubbard 	int chksum;
541c4f02a89SMax Khon 	struct msdosfsmount *pmp;
542952a6212SJordan K. Hubbard {
543952a6212SJordan K. Hubbard 	u_int8_t *wcp;
544c4f02a89SMax Khon 	int i, end;
54513df76f2SAndrey A. Chernov 	u_int16_t code;
546952a6212SJordan K. Hubbard 
547952a6212SJordan K. Hubbard 	/*
548952a6212SJordan K. Hubbard 	 * Drop trailing blanks and dots
549952a6212SJordan K. Hubbard 	 */
550c4f02a89SMax Khon 	unlen = winLenFixup(un, unlen);
551952a6212SJordan K. Hubbard 
552c4f02a89SMax Khon 	/*
553c4f02a89SMax Khon 	 * Cut *un for this slot
554c4f02a89SMax Khon 	 */
555c4f02a89SMax Khon 	unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
556c4f02a89SMax Khon 			  pmp->pm_flags, pmp->pm_u2w);
557952a6212SJordan K. Hubbard 
558952a6212SJordan K. Hubbard 	/*
559952a6212SJordan K. Hubbard 	 * Initialize winentry to some useful default
560952a6212SJordan K. Hubbard 	 */
561952a6212SJordan K. Hubbard 	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
562952a6212SJordan K. Hubbard 	wep->weCnt = cnt;
563952a6212SJordan K. Hubbard 	wep->weAttributes = ATTR_WIN95;
564952a6212SJordan K. Hubbard 	wep->weReserved1 = 0;
565952a6212SJordan K. Hubbard 	wep->weChksum = chksum;
566952a6212SJordan K. Hubbard 	wep->weReserved2 = 0;
567952a6212SJordan K. Hubbard 
568952a6212SJordan K. Hubbard 	/*
569952a6212SJordan K. Hubbard 	 * Now convert the filename parts
570952a6212SJordan K. Hubbard 	 */
571c4f02a89SMax Khon 	end = 0;
572c4f02a89SMax Khon 	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
573bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
57413df76f2SAndrey A. Chernov 		*wcp++ = code;
57513df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
576c4f02a89SMax Khon 		if (!code)
577c4f02a89SMax Khon 			end = WIN_LAST;
578952a6212SJordan K. Hubbard 	}
579c4f02a89SMax Khon 	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
580bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
58113df76f2SAndrey A. Chernov 		*wcp++ = code;
58213df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
583c4f02a89SMax Khon 		if (!code)
584c4f02a89SMax Khon 			end = WIN_LAST;
585952a6212SJordan K. Hubbard 	}
586c4f02a89SMax Khon 	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
587bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
58813df76f2SAndrey A. Chernov 		*wcp++ = code;
58913df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
590c4f02a89SMax Khon 		if (!code)
591c4f02a89SMax Khon 			end = WIN_LAST;
592952a6212SJordan K. Hubbard 	}
593c4f02a89SMax Khon 	if (*un == '\0')
594c4f02a89SMax Khon 		end = WIN_LAST;
595c4f02a89SMax Khon 	wep->weCnt |= end;
596c4f02a89SMax Khon 	return !end;
5974d63452fSAndrey A. Chernov }
5984d63452fSAndrey A. Chernov 
599952a6212SJordan K. Hubbard /*
600952a6212SJordan K. Hubbard  * Compare our filename to the one in the Win95 entry
601952a6212SJordan K. Hubbard  * Returns the checksum or -1 if no match
602952a6212SJordan K. Hubbard  */
603952a6212SJordan K. Hubbard int
604c4f02a89SMax Khon winChkName(un, unlen, chksum, pmp)
605952a6212SJordan K. Hubbard 	const u_char *un;
606bddcdc51STim J. Robbins 	size_t unlen;
607952a6212SJordan K. Hubbard 	int chksum;
608c4f02a89SMax Khon 	struct msdosfsmount *pmp;
609952a6212SJordan K. Hubbard {
610bddcdc51STim J. Robbins 	size_t len;
611c4f02a89SMax Khon 	u_int16_t c1, c2;
612c4f02a89SMax Khon 	u_char *np;
613c4f02a89SMax Khon 	struct dirent dirbuf;
614952a6212SJordan K. Hubbard 
615952a6212SJordan K. Hubbard 	/*
616c4f02a89SMax Khon 	 * We alread have winentry in mbnambuf
617952a6212SJordan K. Hubbard 	 */
618c4f02a89SMax Khon 	if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen)
619952a6212SJordan K. Hubbard 		return -1;
620952a6212SJordan K. Hubbard 
621c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG
622c4f02a89SMax Khon 	printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
623c4f02a89SMax Khon 							dirbuf.d_name,
624c4f02a89SMax Khon 							dirbuf.d_namlen);
625c4f02a89SMax Khon #endif
626952a6212SJordan K. Hubbard 
627952a6212SJordan K. Hubbard 	/*
628952a6212SJordan K. Hubbard 	 * Compare the name parts
629952a6212SJordan K. Hubbard 	 */
630c4f02a89SMax Khon 	len = dirbuf.d_namlen;
631c4f02a89SMax Khon 	if (unlen != len)
632c4f02a89SMax Khon 		return -2;
633c4f02a89SMax Khon 
634c4f02a89SMax Khon 	for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
635c4f02a89SMax Khon 		/*
6360f4e4130SMax Khon 		 * Comparison must be case insensitive, because FAT disallows
6370f4e4130SMax Khon 		 * to look up or create files in case sensitive even when
6380f4e4130SMax Khon 		 * it's a long file name.
639c4f02a89SMax Khon 		 */
640bddcdc51STim J. Robbins 		c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
641bddcdc51STim J. Robbins 		c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
642b998efa9SAndrey A. Chernov 		if (c1 != c2)
643c4f02a89SMax Khon 			return -2;
644952a6212SJordan K. Hubbard 	}
645952a6212SJordan K. Hubbard 	return chksum;
646952a6212SJordan K. Hubbard }
647952a6212SJordan K. Hubbard 
648952a6212SJordan K. Hubbard /*
649952a6212SJordan K. Hubbard  * Convert Win95 filename to dirbuf.
650952a6212SJordan K. Hubbard  * Returns the checksum or -1 if impossible
651952a6212SJordan K. Hubbard  */
652952a6212SJordan K. Hubbard int
653c4f02a89SMax Khon win2unixfn(wep, chksum, pmp)
654952a6212SJordan K. Hubbard 	struct winentry *wep;
655952a6212SJordan K. Hubbard 	int chksum;
656c4f02a89SMax Khon 	struct msdosfsmount *pmp;
657952a6212SJordan K. Hubbard {
658952a6212SJordan K. Hubbard 	u_int8_t *cp;
659c4f02a89SMax Khon 	u_int8_t *np, name[WIN_CHARS * 2 + 1];
6602b5b6623SAndrey A. Chernov 	u_int16_t code;
661952a6212SJordan K. Hubbard 	int i;
662952a6212SJordan K. Hubbard 
663952a6212SJordan K. Hubbard 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
664952a6212SJordan K. Hubbard 	    || !(wep->weCnt&WIN_CNT))
665952a6212SJordan K. Hubbard 		return -1;
666952a6212SJordan K. Hubbard 
667952a6212SJordan K. Hubbard 	/*
668952a6212SJordan K. Hubbard 	 * First compare checksums
669952a6212SJordan K. Hubbard 	 */
670952a6212SJordan K. Hubbard 	if (wep->weCnt&WIN_LAST) {
671952a6212SJordan K. Hubbard 		chksum = wep->weChksum;
672952a6212SJordan K. Hubbard 	} else if (chksum != wep->weChksum)
673952a6212SJordan K. Hubbard 		chksum = -1;
674952a6212SJordan K. Hubbard 	if (chksum == -1)
675952a6212SJordan K. Hubbard 		return -1;
676952a6212SJordan K. Hubbard 
677952a6212SJordan K. Hubbard 	/*
678952a6212SJordan K. Hubbard 	 * Convert the name parts
679952a6212SJordan K. Hubbard 	 */
680c4f02a89SMax Khon 	np = name;
681952a6212SJordan K. Hubbard 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
6822b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
6832b5b6623SAndrey A. Chernov 		switch (code) {
684952a6212SJordan K. Hubbard 		case 0:
6852b5b6623SAndrey A. Chernov 			*np = '\0';
686c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
687952a6212SJordan K. Hubbard 			return chksum;
688952a6212SJordan K. Hubbard 		case '/':
6892b5b6623SAndrey A. Chernov 			*np = '\0';
690952a6212SJordan K. Hubbard 			return -1;
6912b5b6623SAndrey A. Chernov 		default:
692c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
693c4f02a89SMax Khon 			if (code & 0xff00)
694c4f02a89SMax Khon 				*np++ = code >> 8;
6952b5b6623SAndrey A. Chernov 			*np++ = code;
6962b5b6623SAndrey A. Chernov 			break;
697952a6212SJordan K. Hubbard 		}
6982b5b6623SAndrey A. Chernov 		cp += 2;
699952a6212SJordan K. Hubbard 	}
700952a6212SJordan K. Hubbard 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
7012b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
7022b5b6623SAndrey A. Chernov 		switch (code) {
703952a6212SJordan K. Hubbard 		case 0:
7042b5b6623SAndrey A. Chernov 			*np = '\0';
705c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
706952a6212SJordan K. Hubbard 			return chksum;
707952a6212SJordan K. Hubbard 		case '/':
7082b5b6623SAndrey A. Chernov 			*np = '\0';
709952a6212SJordan K. Hubbard 			return -1;
7102b5b6623SAndrey A. Chernov 		default:
711c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
712c4f02a89SMax Khon 			if (code & 0xff00)
713c4f02a89SMax Khon 				*np++ = code >> 8;
7142b5b6623SAndrey A. Chernov 			*np++ = code;
7152b5b6623SAndrey A. Chernov 			break;
716952a6212SJordan K. Hubbard 		}
7172b5b6623SAndrey A. Chernov 		cp += 2;
718952a6212SJordan K. Hubbard 	}
719952a6212SJordan K. Hubbard 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
7202b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
7212b5b6623SAndrey A. Chernov 		switch (code) {
722952a6212SJordan K. Hubbard 		case 0:
7232b5b6623SAndrey A. Chernov 			*np = '\0';
724c4f02a89SMax Khon 			mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
725952a6212SJordan K. Hubbard 			return chksum;
726952a6212SJordan K. Hubbard 		case '/':
7272b5b6623SAndrey A. Chernov 			*np = '\0';
728952a6212SJordan K. Hubbard 			return -1;
7292b5b6623SAndrey A. Chernov 		default:
730c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
731c4f02a89SMax Khon 			if (code & 0xff00)
732c4f02a89SMax Khon 				*np++ = code >> 8;
7332b5b6623SAndrey A. Chernov 			*np++ = code;
7342b5b6623SAndrey A. Chernov 			break;
735952a6212SJordan K. Hubbard 		}
7362b5b6623SAndrey A. Chernov 		cp += 2;
737952a6212SJordan K. Hubbard 	}
738c4f02a89SMax Khon 	*np = '\0';
739c4f02a89SMax Khon 	mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
740952a6212SJordan K. Hubbard 	return chksum;
741952a6212SJordan K. Hubbard }
742952a6212SJordan K. Hubbard 
743952a6212SJordan K. Hubbard /*
7442a05fbb9SNate Lawson  * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
745952a6212SJordan K. Hubbard  */
746952a6212SJordan K. Hubbard u_int8_t
747710a9accSMax Khon winChksum(struct direntry *dep)
748952a6212SJordan K. Hubbard {
749952a6212SJordan K. Hubbard 	u_int8_t s;
750952a6212SJordan K. Hubbard 
751710a9accSMax Khon 	s = dep->deName[0];
752710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[1];
753710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[2];
754710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[3];
755710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[4];
756710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[5];
757710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[6];
758710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[7];
759710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deExtension[0];
760710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deExtension[1];
761710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deExtension[2];
7622a05fbb9SNate Lawson 
7632a05fbb9SNate Lawson 	return (s);
764952a6212SJordan K. Hubbard }
765952a6212SJordan K. Hubbard 
766952a6212SJordan K. Hubbard /*
767952a6212SJordan K. Hubbard  * Determine the number of slots necessary for Win95 names
768952a6212SJordan K. Hubbard  */
769952a6212SJordan K. Hubbard int
770c4f02a89SMax Khon winSlotCnt(un, unlen, pmp)
771952a6212SJordan K. Hubbard 	const u_char *un;
772bddcdc51STim J. Robbins 	size_t unlen;
773c4f02a89SMax Khon 	struct msdosfsmount *pmp;
774952a6212SJordan K. Hubbard {
775c4f02a89SMax Khon 	size_t wlen;
776c4f02a89SMax Khon 	char wn[WIN_MAXLEN * 2 + 1], *wnp;
777c4f02a89SMax Khon 
77869a36f24SMike Smith 	unlen = winLenFixup(un, unlen);
779c4f02a89SMax Khon 
780c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
781c4f02a89SMax Khon 		wlen = WIN_MAXLEN * 2;
782c4f02a89SMax Khon 		wnp = wn;
783bddcdc51STim J. Robbins 		msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
784c4f02a89SMax Khon 		if (unlen > 0)
785c4f02a89SMax Khon 			return 0;
786c4f02a89SMax Khon 		return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
787c4f02a89SMax Khon 	}
788c4f02a89SMax Khon 
789952a6212SJordan K. Hubbard 	if (unlen > WIN_MAXLEN)
790952a6212SJordan K. Hubbard 		return 0;
791952a6212SJordan K. Hubbard 	return howmany(unlen, WIN_CHARS);
79227a0bc89SDoug Rabson }
79369a36f24SMike Smith 
79469a36f24SMike Smith /*
79569a36f24SMike Smith  * Determine the number of bytes neccesary for Win95 names
79669a36f24SMike Smith  */
797bddcdc51STim J. Robbins size_t
79869a36f24SMike Smith winLenFixup(un, unlen)
79969a36f24SMike Smith 	const u_char* un;
800bddcdc51STim J. Robbins 	size_t unlen;
80169a36f24SMike Smith {
80269a36f24SMike Smith 	for (un += unlen; unlen > 0; unlen--)
80369a36f24SMike Smith 		if (*--un != ' ' && *un != '.')
80469a36f24SMike Smith 			break;
80569a36f24SMike Smith 	return unlen;
80669a36f24SMike Smith }
807c4f02a89SMax Khon 
808c4f02a89SMax Khon /*
809c4f02a89SMax Khon  * Store an area with multi byte string instr, and reterns left
810c4f02a89SMax Khon  * byte of instr and moves pointer forward. The area's size is
811c4f02a89SMax Khon  * inlen or outlen.
812c4f02a89SMax Khon  */
813c4f02a89SMax Khon static int
814bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
815c4f02a89SMax Khon {
816c4f02a89SMax Khon 	char *outp, outstr[outlen * weight + 1];
817c4f02a89SMax Khon 
818c4f02a89SMax Khon 	if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
819c4f02a89SMax Khon 		outp = outstr;
820c4f02a89SMax Khon 		outlen *= weight;
821bddcdc51STim J. Robbins 		msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
822c4f02a89SMax Khon 		return (inlen);
823c4f02a89SMax Khon 	}
824c4f02a89SMax Khon 
825c4f02a89SMax Khon 	(*instr) += min(inlen, outlen);
826c4f02a89SMax Khon 	return (inlen - min(inlen, outlen));
827c4f02a89SMax Khon }
828c4f02a89SMax Khon 
829c4f02a89SMax Khon /*
830c4f02a89SMax Khon  * Convert DOS char to Local char
831c4f02a89SMax Khon  */
832c4f02a89SMax Khon static u_int16_t
833c4f02a89SMax Khon dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
834c4f02a89SMax Khon {
835c4f02a89SMax Khon 	u_char c;
836c4f02a89SMax Khon 	char *outp, outbuf[3];
837c4f02a89SMax Khon 	u_int16_t wc;
838c4f02a89SMax Khon 	size_t len, olen;
839c4f02a89SMax Khon 
840c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
841c4f02a89SMax Khon 		olen = len = 2;
842c4f02a89SMax Khon 		outp = outbuf;
843c4f02a89SMax Khon 
844c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
845c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
846c4f02a89SMax Khon 						  ilen, &outp, &olen, KICONV_LOWER);
847c4f02a89SMax Khon 		else
848c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
849c4f02a89SMax Khon 					     ilen, &outp, &olen);
850c4f02a89SMax Khon 		len -= olen;
851c4f02a89SMax Khon 
852c4f02a89SMax Khon 		/*
853c4f02a89SMax Khon 		 * return '?' if failed to convert
854c4f02a89SMax Khon 		 */
855c4f02a89SMax Khon 		if (len == 0) {
856c4f02a89SMax Khon 			(*ilen)--;
857c4f02a89SMax Khon 			(*instr)++;
858c4f02a89SMax Khon 			return ('?');
859c4f02a89SMax Khon 		}
860c4f02a89SMax Khon 
861c4f02a89SMax Khon 		wc = 0;
862c4f02a89SMax Khon 		while(len--)
863c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
864c4f02a89SMax Khon 		return (wc);
865c4f02a89SMax Khon 	}
866c4f02a89SMax Khon 
867c4f02a89SMax Khon 	(*ilen)--;
868c4f02a89SMax Khon 	c = *(*instr)++;
869c4f02a89SMax Khon 	c = dos2unix[c];
870c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
871c4f02a89SMax Khon 		c = u2l[c];
872c4f02a89SMax Khon 	return ((u_int16_t)c);
873c4f02a89SMax Khon }
874c4f02a89SMax Khon 
875c4f02a89SMax Khon /*
876c4f02a89SMax Khon  * Convert Local char to DOS char
877c4f02a89SMax Khon  */
878c4f02a89SMax Khon static u_int16_t
879c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
880c4f02a89SMax Khon {
881c4f02a89SMax Khon 	u_char c;
882c4f02a89SMax Khon 	char *up, *outp, unicode[3], outbuf[3];
883c4f02a89SMax Khon 	u_int16_t wc;
884697ab829SR. Imura 	size_t len, ucslen, unixlen, olen;
885c4f02a89SMax Khon 
886c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
887c4f02a89SMax Khon 		/*
888c4f02a89SMax Khon 		 * to hide an invisible character, using a unicode filter
889c4f02a89SMax Khon 		 */
890697ab829SR. Imura 		ucslen = 2;
891c4f02a89SMax Khon 		len = *ilen;
892c4f02a89SMax Khon 		up = unicode;
893c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
894697ab829SR. Imura 				     ilen, &up, &ucslen);
895697ab829SR. Imura 		unixlen = len - *ilen;
896c4f02a89SMax Khon 
897c4f02a89SMax Khon 		/*
898c4f02a89SMax Khon 		 * cannot be converted
899c4f02a89SMax Khon 		 */
900697ab829SR. Imura 		if (unixlen == 0) {
901c4f02a89SMax Khon 			(*ilen)--;
902c4f02a89SMax Khon 			(*instr)++;
903c4f02a89SMax Khon 			return (0);
904c4f02a89SMax Khon 		}
905c4f02a89SMax Khon 
906c4f02a89SMax Khon 		/*
907c4f02a89SMax Khon 		 * return magic number for ascii char
908c4f02a89SMax Khon 		 */
909697ab829SR. Imura 		if (unixlen == 1) {
910c4f02a89SMax Khon 			c = *(*instr -1);
911c4f02a89SMax Khon 			if (! (c & 0x80)) {
912c4f02a89SMax Khon 				c = unix2dos[c];
913c4f02a89SMax Khon 				if (c <= 2)
914c4f02a89SMax Khon 					return (c);
915c4f02a89SMax Khon 			}
916c4f02a89SMax Khon 		}
917c4f02a89SMax Khon 
918c4f02a89SMax Khon 		/*
919c4f02a89SMax Khon 		 * now convert using libiconv
920c4f02a89SMax Khon 		 */
921697ab829SR. Imura 		*instr -= unixlen;
922697ab829SR. Imura 		*ilen = len;
923c4f02a89SMax Khon 
924c4f02a89SMax Khon 		olen = len = 2;
925c4f02a89SMax Khon 		outp = outbuf;
926c4f02a89SMax Khon 		msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
927c4f02a89SMax Khon 					  ilen, &outp, &olen, KICONV_FROM_UPPER);
928c4f02a89SMax Khon 		len -= olen;
929697ab829SR. Imura 
930697ab829SR. Imura 		/*
931697ab829SR. Imura 		 * cannot be converted, but has unicode char, should return magic number
932697ab829SR. Imura 		 */
933697ab829SR. Imura 		if (len == 0) {
934697ab829SR. Imura 			(*ilen) -= unixlen;
935697ab829SR. Imura 			(*instr) += unixlen;
936697ab829SR. Imura 			return (1);
937697ab829SR. Imura 		}
938697ab829SR. Imura 
939c4f02a89SMax Khon 		wc = 0;
940c4f02a89SMax Khon 		while(len--)
941c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
942c4f02a89SMax Khon 		return (wc);
943c4f02a89SMax Khon 	}
944c4f02a89SMax Khon 
945c4f02a89SMax Khon 	(*ilen)--;
946c4f02a89SMax Khon 	c = *(*instr)++;
947c4f02a89SMax Khon 	c = l2u[c];
948c4f02a89SMax Khon 	c = unix2dos[c];
949c4f02a89SMax Khon 	return ((u_int16_t)c);
950c4f02a89SMax Khon }
951c4f02a89SMax Khon 
952c4f02a89SMax Khon /*
953c4f02a89SMax Khon  * Convert Windows char to Local char
954c4f02a89SMax Khon  */
955c4f02a89SMax Khon static u_int16_t
956c4f02a89SMax Khon win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
957c4f02a89SMax Khon {
958c4f02a89SMax Khon 	u_char *inp, *outp, inbuf[3], outbuf[3];
959c4f02a89SMax Khon 	size_t ilen, olen, len;
960c4f02a89SMax Khon 
961c4f02a89SMax Khon 	if (wc == 0)
962c4f02a89SMax Khon 		return (0);
963c4f02a89SMax Khon 
964c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
965c4f02a89SMax Khon 		inbuf[0] = (u_char)(wc>>8);
966c4f02a89SMax Khon 		inbuf[1] = (u_char)wc;
967c4f02a89SMax Khon 		inbuf[2] = '\0';
968c4f02a89SMax Khon 
969c4f02a89SMax Khon 		ilen = olen = len = 2;
970c4f02a89SMax Khon 		inp = inbuf;
971c4f02a89SMax Khon 		outp = outbuf;
972c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
973c4f02a89SMax Khon 				     (char **)&outp, &olen);
974c4f02a89SMax Khon 		len -= olen;
975c4f02a89SMax Khon 
976c4f02a89SMax Khon 		/*
977c4f02a89SMax Khon 		 * return '?' if failed to convert
978c4f02a89SMax Khon 		 */
979c4f02a89SMax Khon 		if (len == 0) {
980c4f02a89SMax Khon 			wc = '?';
981c4f02a89SMax Khon 			return (wc);
982c4f02a89SMax Khon 		}
983c4f02a89SMax Khon 
984c4f02a89SMax Khon 		wc = 0;
985c4f02a89SMax Khon 		while(len--)
986c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
987c4f02a89SMax Khon 		return (wc);
988c4f02a89SMax Khon 	}
989c4f02a89SMax Khon 
990c4f02a89SMax Khon 	if (wc & 0xff00)
991c4f02a89SMax Khon 		wc = '?';
992c4f02a89SMax Khon 
993c4f02a89SMax Khon 	return (wc);
994c4f02a89SMax Khon }
995c4f02a89SMax Khon 
996c4f02a89SMax Khon /*
997c4f02a89SMax Khon  * Convert Local char to Windows char
998c4f02a89SMax Khon  */
999c4f02a89SMax Khon static u_int16_t
1000c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1001c4f02a89SMax Khon {
1002c4f02a89SMax Khon 	u_char *outp, outbuf[3];
1003c4f02a89SMax Khon 	u_int16_t wc;
1004c4f02a89SMax Khon 	size_t olen;
1005c4f02a89SMax Khon 
1006c4f02a89SMax Khon 	if (*ilen == 0)
1007c4f02a89SMax Khon 		return (0);
1008c4f02a89SMax Khon 
1009c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1010c4f02a89SMax Khon 		outp = outbuf;
1011c4f02a89SMax Khon 		olen = 2;
1012c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
1013c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
1014c4f02a89SMax Khon 						  ilen, (char **)&outp, &olen,
1015c4f02a89SMax Khon 						  KICONV_FROM_LOWER);
1016c4f02a89SMax Khon 		else
1017c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1018c4f02a89SMax Khon 					     ilen, (char **)&outp, &olen);
1019c4f02a89SMax Khon 
1020c4f02a89SMax Khon 		/*
1021c4f02a89SMax Khon 		 * return '0' if end of filename
1022c4f02a89SMax Khon 		 */
1023c4f02a89SMax Khon 		if (olen == 2)
1024c4f02a89SMax Khon 			return (0);
1025c4f02a89SMax Khon 
1026c4f02a89SMax Khon 		wc = (outbuf[0]<<8) | outbuf[1];
1027c4f02a89SMax Khon 
1028c4f02a89SMax Khon 		return (wc);
1029c4f02a89SMax Khon 	}
1030c4f02a89SMax Khon 
1031c4f02a89SMax Khon 	(*ilen)--;
1032c4f02a89SMax Khon 	wc = (*instr)[0];
1033c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
1034c4f02a89SMax Khon 		wc = u2l[wc];
1035c4f02a89SMax Khon 	(*instr)++;
1036c4f02a89SMax Khon 	return (wc);
1037c4f02a89SMax Khon }
1038c4f02a89SMax Khon 
1039c4f02a89SMax Khon /*
10404cdb1483SNate Lawson  * Initialize the temporary concatenation buffer (once) and mark it as
10414cdb1483SNate Lawson  * empty on subsequent calls.
1042c4f02a89SMax Khon  */
1043c4f02a89SMax Khon void
1044c4f02a89SMax Khon mbnambuf_init(void)
1045c4f02a89SMax Khon {
1046c4f02a89SMax Khon 
10474cdb1483SNate Lawson         if (nambuf_ptr == NULL) {
10484cdb1483SNate Lawson 		nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK);
10494cdb1483SNate Lawson 		nambuf_ptr[MAXNAMLEN] = '\0';
1050c4f02a89SMax Khon 	}
10514cdb1483SNate Lawson 	nambuf_len = 0;
105258ad326bSNate Lawson 	nambuf_last_id = -1;
1053c4f02a89SMax Khon }
1054c4f02a89SMax Khon 
1055c4f02a89SMax Khon /*
105658ad326bSNate Lawson  * Fill out our concatenation buffer with the given substring, at the offset
105758ad326bSNate Lawson  * specified by its id.  Since this function must be called with ids in
105858ad326bSNate Lawson  * descending order, we take advantage of the fact that ASCII substrings are
105958ad326bSNate Lawson  * exactly WIN_CHARS in length.  For non-ASCII substrings, we shift all
106058ad326bSNate Lawson  * previous (i.e. higher id) substrings upwards to make room for this one.
106158ad326bSNate Lawson  * This only penalizes portions of substrings that contain more than
106258ad326bSNate Lawson  * WIN_CHARS bytes when they are first encountered.
1063c4f02a89SMax Khon  */
1064c4f02a89SMax Khon void
1065c4f02a89SMax Khon mbnambuf_write(char *name, int id)
1066c4f02a89SMax Khon {
10674cdb1483SNate Lawson 	size_t count;
106858ad326bSNate Lawson 	char *slot;
10694cdb1483SNate Lawson 
107058ad326bSNate Lawson 	KASSERT(nambuf_len == 0 || id == nambuf_last_id - 1,
107158ad326bSNate Lawson 	    ("non-decreasing id, id %d last id %d", id, nambuf_last_id));
107258ad326bSNate Lawson 
107358ad326bSNate Lawson 	/* Store this substring in a WIN_CHAR-aligned slot. */
107458ad326bSNate Lawson 	slot = nambuf_ptr + (id * WIN_CHARS);
10754cdb1483SNate Lawson 	count = strlen(name);
107658ad326bSNate Lawson 	if (nambuf_len + count > MAXNAMLEN) {
107758ad326bSNate Lawson 		printf("msdosfs: file name %zu too long\n", nambuf_len + count);
10784cdb1483SNate Lawson 		return;
1079c4f02a89SMax Khon 	}
108058ad326bSNate Lawson 
108158ad326bSNate Lawson 	/* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
108258ad326bSNate Lawson 	if (count > WIN_CHARS && nambuf_len != 0)
108358ad326bSNate Lawson 		bcopy(slot + WIN_CHARS, slot + count, nambuf_len);
108458ad326bSNate Lawson 
108558ad326bSNate Lawson 	/* Copy in the substring to its slot and update length so far. */
108658ad326bSNate Lawson 	bcopy(name, slot, count);
108758ad326bSNate Lawson 	nambuf_len += count;
108858ad326bSNate Lawson 	nambuf_last_id = id;
1089c4f02a89SMax Khon }
1090c4f02a89SMax Khon 
1091c4f02a89SMax Khon /*
10924cdb1483SNate Lawson  * Take the completed string and use it to setup the struct dirent.
10934cdb1483SNate Lawson  * Be sure to always nul-terminate the d_name and then copy the string
10944cdb1483SNate Lawson  * from our buffer.  Note that this function assumes the full string has
10954cdb1483SNate Lawson  * been reassembled in the buffer.  If it's called before all substrings
10964cdb1483SNate Lawson  * have been written via mbnambuf_write(), the result will be incorrect.
1097c4f02a89SMax Khon  */
1098c4f02a89SMax Khon char *
1099c4f02a89SMax Khon mbnambuf_flush(struct dirent *dp)
1100c4f02a89SMax Khon {
1101c4f02a89SMax Khon 
11024cdb1483SNate Lawson 	if (nambuf_len > sizeof(dp->d_name) - 1) {
1103c4f02a89SMax Khon 		mbnambuf_init();
1104c4f02a89SMax Khon 		return (NULL);
1105c4f02a89SMax Khon 	}
110658ad326bSNate Lawson 	bcopy(nambuf_ptr, dp->d_name, nambuf_len);
1107d81812beSNate Lawson 	dp->d_name[nambuf_len] = '\0';
11084cdb1483SNate Lawson 	dp->d_namlen = nambuf_len;
11094cdb1483SNate Lawson 
1110c4f02a89SMax Khon 	mbnambuf_init();
1111c4f02a89SMax Khon 	return (dp->d_name);
1112c4f02a89SMax Khon }
1113