xref: /freebsd/sys/fs/msdosfs/msdosfs_conv.c (revision 09bb4a314a1a5ed5024c3dd146eb484c467e431f)
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>
559b0802c9SBruce Evans #include <sys/mount.h>
569b0802c9SBruce Evans 
579b0802c9SBruce Evans #include <fs/msdosfs/bpb.h>
589b0802c9SBruce Evans #include <fs/msdosfs/direntry.h>
599b0802c9SBruce Evans #include <fs/msdosfs/msdosfsmount.h>
60c4f02a89SMax Khon 
61c4f02a89SMax Khon extern struct iconv_functions *msdosfs_iconv;
6227a0bc89SDoug Rabson 
63bddcdc51STim J. Robbins static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle);
64e2ee19e3SKevin Lo static u_char * dos2unixchr(u_char *, const u_char **, size_t *, int, struct msdosfsmount *);
65c4f02a89SMax Khon static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
66e2ee19e3SKevin Lo static u_char * win2unixchr(u_char *, u_int16_t, struct msdosfsmount *);
67c4f02a89SMax Khon static u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *);
68c4f02a89SMax Khon 
6927a0bc89SDoug Rabson /*
70cce0eefdSMike Smith  * 0 - character disallowed in long file name.
71cce0eefdSMike Smith  * 1 - character should be replaced by '_' in DOS file name,
72cce0eefdSMike Smith  *     and generation number inserted.
73cce0eefdSMike Smith  * 2 - character ('.' and ' ') should be skipped in DOS file name,
74cce0eefdSMike Smith  *     and generation number inserted.
75cce0eefdSMike Smith  */
76952a6212SJordan K. Hubbard static u_char
77952a6212SJordan K. Hubbard unix2dos[256] = {
78c4f02a89SMax Khon /* iso8859-1 -> cp850 */
79952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
80952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
81952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
82952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
83cce0eefdSMike Smith 	2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
84cce0eefdSMike Smith 	0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,	/* 28-2f */
85952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
86cce0eefdSMike Smith 	0x38, 0x39, 0,    1,    0,    1,    0,    0,	/* 38-3f */
87952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
88952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
89952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
90cce0eefdSMike Smith 	0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f,	/* 58-5f */
91952a6212SJordan K. Hubbard 	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
92952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
93952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
94952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
95952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
96952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
97952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
98952a6212SJordan K. Hubbard 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
99952a6212SJordan K. Hubbard 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
100952a6212SJordan K. Hubbard 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
101952a6212SJordan K. Hubbard 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
102952a6212SJordan K. Hubbard 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
103952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
104952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
105952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
106952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
107952a6212SJordan K. Hubbard 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
108952a6212SJordan K. Hubbard 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
109952a6212SJordan K. Hubbard 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
110952a6212SJordan K. Hubbard 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
111952a6212SJordan K. Hubbard };
112952a6212SJordan K. Hubbard 
113952a6212SJordan K. Hubbard static u_char
114952a6212SJordan K. Hubbard dos2unix[256] = {
115c4f02a89SMax Khon /* cp850 -> iso8859-1 */
116952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
117952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
118952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
119952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
120952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
121952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
122952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
123952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
124952a6212SJordan K. Hubbard 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
125952a6212SJordan K. Hubbard 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
126952a6212SJordan K. Hubbard 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
127952a6212SJordan K. Hubbard 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
128952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
129952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
130952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
131952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
132952a6212SJordan K. Hubbard 	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
133952a6212SJordan K. Hubbard 	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
134952a6212SJordan K. Hubbard 	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
135952a6212SJordan K. Hubbard 	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
136952a6212SJordan K. Hubbard 	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
137952a6212SJordan K. Hubbard 	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
138952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
139952a6212SJordan K. Hubbard 	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
140952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
141952a6212SJordan K. Hubbard 	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
142952a6212SJordan K. Hubbard 	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
143952a6212SJordan K. Hubbard 	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
144952a6212SJordan K. Hubbard 	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
145952a6212SJordan K. Hubbard 	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
146952a6212SJordan K. Hubbard 	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
147952a6212SJordan K. Hubbard 	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
148952a6212SJordan K. Hubbard };
149952a6212SJordan K. Hubbard 
150952a6212SJordan K. Hubbard static u_char
151952a6212SJordan K. Hubbard u2l[256] = {
152c4f02a89SMax Khon /* tolower */
153952a6212SJordan K. Hubbard 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
154952a6212SJordan K. Hubbard 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
155952a6212SJordan K. Hubbard 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
156952a6212SJordan K. Hubbard 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
157952a6212SJordan K. Hubbard 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
158952a6212SJordan K. Hubbard 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
159952a6212SJordan K. Hubbard 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
160952a6212SJordan K. Hubbard 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
161952a6212SJordan K. Hubbard 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
162952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
163952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
164952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
165952a6212SJordan K. Hubbard 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
166952a6212SJordan K. Hubbard 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
167952a6212SJordan K. Hubbard 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
168952a6212SJordan K. Hubbard 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
169952a6212SJordan K. Hubbard 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
170952a6212SJordan K. Hubbard 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
171952a6212SJordan K. Hubbard 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
172952a6212SJordan K. Hubbard 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
173952a6212SJordan K. Hubbard 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
174952a6212SJordan K. Hubbard 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
175952a6212SJordan K. Hubbard 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
176952a6212SJordan K. Hubbard 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
177952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
178952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
179952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
180952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
181952a6212SJordan K. Hubbard 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
182952a6212SJordan K. Hubbard 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
183952a6212SJordan K. Hubbard 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
184952a6212SJordan K. Hubbard 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
185952a6212SJordan K. Hubbard };
18627a0bc89SDoug Rabson 
1877391f611SAndrey A. Chernov static u_char
1887391f611SAndrey A. Chernov l2u[256] = {
189c4f02a89SMax Khon /* toupper */
1907391f611SAndrey A. Chernov 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
1917391f611SAndrey A. Chernov 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
1927391f611SAndrey A. Chernov 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
1937391f611SAndrey A. Chernov 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
1947391f611SAndrey A. Chernov 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
1957391f611SAndrey A. Chernov 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
1967391f611SAndrey A. Chernov 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
1977391f611SAndrey A. Chernov 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
1987391f611SAndrey A. Chernov 	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
1997391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
2007391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
2017391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
2027391f611SAndrey A. Chernov 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
2037391f611SAndrey A. Chernov 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
2047391f611SAndrey A. Chernov 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
2057391f611SAndrey A. Chernov 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
2067391f611SAndrey A. Chernov 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
2077391f611SAndrey A. Chernov 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
2087391f611SAndrey A. Chernov 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
2097391f611SAndrey A. Chernov 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
2107391f611SAndrey A. Chernov 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
2117391f611SAndrey A. Chernov 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
2127391f611SAndrey A. Chernov 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
2137391f611SAndrey A. Chernov 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
2147391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
2157391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
2167391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
2177391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
2187391f611SAndrey A. Chernov 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
2197391f611SAndrey A. Chernov 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
2207391f611SAndrey A. Chernov 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
2217391f611SAndrey A. Chernov 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
2227391f611SAndrey A. Chernov };
2237391f611SAndrey A. Chernov 
22427a0bc89SDoug Rabson /*
22527a0bc89SDoug Rabson  * DOS filenames are made of 2 parts, the name part and the extension part.
22627a0bc89SDoug Rabson  * The name part is 8 characters long and the extension part is 3
22727a0bc89SDoug Rabson  * characters long.  They may contain trailing blanks if the name or
22827a0bc89SDoug Rabson  * extension are not long enough to fill their respective fields.
22927a0bc89SDoug Rabson  */
23027a0bc89SDoug Rabson 
23127a0bc89SDoug Rabson /*
23227a0bc89SDoug Rabson  * Convert a DOS filename to a unix filename. And, return the number of
23327a0bc89SDoug Rabson  * characters in the resulting unix filename excluding the terminating
23427a0bc89SDoug Rabson  * null.
23527a0bc89SDoug Rabson  */
23627a0bc89SDoug Rabson int
23710c9700fSEd Maste dos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp)
23827a0bc89SDoug Rabson {
239bddcdc51STim J. Robbins 	size_t i;
240c4f02a89SMax Khon 	int thislong = 0;
241e2ee19e3SKevin Lo 	u_char *c, tmpbuf[5];
24227a0bc89SDoug Rabson 
24327a0bc89SDoug Rabson 	/*
24427a0bc89SDoug Rabson 	 * If first char of the filename is SLOT_E5 (0x05), then the real
24527a0bc89SDoug Rabson 	 * first char of the filename should be 0xe5. But, they couldn't
24627a0bc89SDoug Rabson 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
24727a0bc89SDoug Rabson 	 * directory slot. Another dos quirk.
24827a0bc89SDoug Rabson 	 */
249952a6212SJordan K. Hubbard 	if (*dn == SLOT_E5)
250c4f02a89SMax Khon 		*dn = 0xe5;
25127a0bc89SDoug Rabson 
252952a6212SJordan K. Hubbard 	/*
253952a6212SJordan K. Hubbard 	 * Copy the name portion into the unix filename string.
254952a6212SJordan K. Hubbard 	 */
255c4f02a89SMax Khon 	for (i = 8; i > 0 && *dn != ' ';) {
256*09bb4a31SDimitry Andric 		c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), &i,
257e2ee19e3SKevin Lo 		    lower & LCASE_BASE, pmp);
25841f1dcccSKevin Lo 		while (*c != '\0') {
25941f1dcccSKevin Lo 			*un++ = *c++;
260952a6212SJordan K. Hubbard 			thislong++;
261952a6212SJordan K. Hubbard 		}
262c4f02a89SMax Khon 	}
263c4f02a89SMax Khon 	dn += i;
264952a6212SJordan K. Hubbard 
265952a6212SJordan K. Hubbard 	/*
266952a6212SJordan K. Hubbard 	 * Now, if there is an extension then put in a period and copy in
267952a6212SJordan K. Hubbard 	 * the extension.
268952a6212SJordan K. Hubbard 	 */
269952a6212SJordan K. Hubbard 	if (*dn != ' ') {
270952a6212SJordan K. Hubbard 		*un++ = '.';
271952a6212SJordan K. Hubbard 		thislong++;
272c4f02a89SMax Khon 		for (i = 3; i > 0 && *dn != ' ';) {
273*09bb4a31SDimitry Andric 			c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn),
274*09bb4a31SDimitry Andric 			    &i, lower & LCASE_EXT, pmp);
27541f1dcccSKevin Lo 			while (*c != '\0') {
27641f1dcccSKevin Lo 				*un++ = *c++;
277c4f02a89SMax Khon 				thislong++;
278c4f02a89SMax Khon 			}
279952a6212SJordan K. Hubbard 		}
280952a6212SJordan K. Hubbard 	}
281952a6212SJordan K. Hubbard 	*un++ = 0;
282952a6212SJordan K. Hubbard 
283952a6212SJordan K. Hubbard 	return (thislong);
28427a0bc89SDoug Rabson }
28527a0bc89SDoug Rabson 
28627a0bc89SDoug Rabson /*
287952a6212SJordan K. Hubbard  * Convert a unix filename to a DOS filename according to Win95 rules.
288952a6212SJordan K. Hubbard  * If applicable and gen is not 0, it is inserted into the converted
289952a6212SJordan K. Hubbard  * filename as a generation number.
290952a6212SJordan K. Hubbard  * Returns
291952a6212SJordan K. Hubbard  *	0 if name couldn't be converted
292952a6212SJordan K. Hubbard  *	1 if the converted name is the same as the original
293952a6212SJordan K. Hubbard  *	  (no long filename entry necessary for Win95)
294952a6212SJordan K. Hubbard  *	2 if conversion was successful
295952a6212SJordan K. Hubbard  *	3 if conversion was successful and generation number was inserted
29627a0bc89SDoug Rabson  */
297952a6212SJordan K. Hubbard int
2984882501bSEd Maste unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen,
2994882501bSEd Maste     struct msdosfsmount *pmp)
30027a0bc89SDoug Rabson {
301bddcdc51STim J. Robbins 	ssize_t i, j;
302bddcdc51STim J. Robbins 	int l;
303952a6212SJordan K. Hubbard 	int conv = 1;
304952a6212SJordan K. Hubbard 	const u_char *cp, *dp, *dp1;
305952a6212SJordan K. Hubbard 	u_char gentext[6], *wcp;
306c4f02a89SMax Khon 	u_int16_t c;
30727a0bc89SDoug Rabson 
30827a0bc89SDoug Rabson 	/*
30927a0bc89SDoug Rabson 	 * Fill the dos filename string with blanks. These are DOS's pad
31027a0bc89SDoug Rabson 	 * characters.
31127a0bc89SDoug Rabson 	 */
312952a6212SJordan K. Hubbard 	for (i = 0; i < 11; i++)
31327a0bc89SDoug Rabson 		dn[i] = ' ';
314952a6212SJordan K. Hubbard 	dn[11] = 0;
31527a0bc89SDoug Rabson 
31627a0bc89SDoug Rabson 	/*
31727a0bc89SDoug Rabson 	 * The filenames "." and ".." are handled specially, since they
31827a0bc89SDoug Rabson 	 * don't follow dos filename rules.
31927a0bc89SDoug Rabson 	 */
32027a0bc89SDoug Rabson 	if (un[0] == '.' && unlen == 1) {
32127a0bc89SDoug Rabson 		dn[0] = '.';
322952a6212SJordan K. Hubbard 		return gen <= 1;
32327a0bc89SDoug Rabson 	}
32427a0bc89SDoug Rabson 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
32527a0bc89SDoug Rabson 		dn[0] = '.';
32627a0bc89SDoug Rabson 		dn[1] = '.';
327952a6212SJordan K. Hubbard 		return gen <= 1;
32827a0bc89SDoug Rabson 	}
32927a0bc89SDoug Rabson 
33027a0bc89SDoug Rabson 	/*
331952a6212SJordan K. Hubbard 	 * Filenames with only blanks and dots are not allowed!
33227a0bc89SDoug Rabson 	 */
333952a6212SJordan K. Hubbard 	for (cp = un, i = unlen; --i >= 0; cp++)
334952a6212SJordan K. Hubbard 		if (*cp != ' ' && *cp != '.')
335952a6212SJordan K. Hubbard 			break;
336952a6212SJordan K. Hubbard 	if (i < 0)
337952a6212SJordan K. Hubbard 		return 0;
338952a6212SJordan K. Hubbard 
339cce0eefdSMike Smith 
340cce0eefdSMike Smith 	/*
341cce0eefdSMike Smith 	 * Filenames with some characters are not allowed!
342cce0eefdSMike Smith 	 */
343c4f02a89SMax Khon 	for (cp = un, i = unlen; i > 0;)
344c4f02a89SMax Khon 		if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
345cce0eefdSMike Smith 			return 0;
346cce0eefdSMike Smith 
347952a6212SJordan K. Hubbard 	/*
348952a6212SJordan K. Hubbard 	 * Now find the extension
349952a6212SJordan K. Hubbard 	 * Note: dot as first char doesn't start extension
350952a6212SJordan K. Hubbard 	 *	 and trailing dots and blanks are ignored
351c4f02a89SMax Khon 	 * Note(2003/7): It seems recent Windows has
352c4f02a89SMax Khon 	 *	 defferent rule than this code, that Windows
353c4f02a89SMax Khon 	 *	 ignores all dots before extension, and use all
354c4f02a89SMax Khon 	 * 	 chars as filename except for dots.
355952a6212SJordan K. Hubbard 	 */
356952a6212SJordan K. Hubbard 	dp = dp1 = 0;
357952a6212SJordan K. Hubbard 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
358952a6212SJordan K. Hubbard 		switch (*cp++) {
359952a6212SJordan K. Hubbard 		case '.':
360952a6212SJordan K. Hubbard 			if (!dp1)
361952a6212SJordan K. Hubbard 				dp1 = cp;
362952a6212SJordan K. Hubbard 			break;
363952a6212SJordan K. Hubbard 		case ' ':
364952a6212SJordan K. Hubbard 			break;
365952a6212SJordan K. Hubbard 		default:
366952a6212SJordan K. Hubbard 			if (dp1)
367952a6212SJordan K. Hubbard 				dp = dp1;
368952a6212SJordan K. Hubbard 			dp1 = 0;
369952a6212SJordan K. Hubbard 			break;
370952a6212SJordan K. Hubbard 		}
37127a0bc89SDoug Rabson 	}
37227a0bc89SDoug Rabson 
37327a0bc89SDoug Rabson 	/*
374697ab829SR. Imura 	 * Now convert it (this part is for extension).
375697ab829SR. Imura 	 * As Windows XP do, if it's not ascii char,
376697ab829SR. Imura 	 * this function should return 2 or 3, so that checkng out Unicode name.
37727a0bc89SDoug Rabson 	 */
378952a6212SJordan K. Hubbard 	if (dp) {
379952a6212SJordan K. Hubbard 		if (dp1)
380952a6212SJordan K. Hubbard 			l = dp1 - dp;
381952a6212SJordan K. Hubbard 		else
382952a6212SJordan K. Hubbard 			l = unlen - (dp - un);
383c4f02a89SMax Khon 		for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
384c4f02a89SMax Khon 			c = unix2doschr(&cp, (size_t *)&i, pmp);
385c4f02a89SMax Khon 			if (c & 0xff00) {
386c4f02a89SMax Khon 				dn[j] = c >> 8;
387c4f02a89SMax Khon 				if (++j < 11) {
388c4f02a89SMax Khon 					dn[j] = c;
389697ab829SR. Imura 					if (conv != 3)
390697ab829SR. Imura 						conv = 2;
391c4f02a89SMax Khon 					continue;
392c4f02a89SMax Khon 				} else {
393c4f02a89SMax Khon 					conv = 3;
394c4f02a89SMax Khon 					dn[j-1] = ' ';
395c4f02a89SMax Khon 					break;
396c4f02a89SMax Khon 				}
397c4f02a89SMax Khon 			} else {
398c4f02a89SMax Khon 				dn[j] = c;
399c4f02a89SMax Khon 			}
400697ab829SR. Imura 			if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
401952a6212SJordan K. Hubbard 				conv = 2;
402cce0eefdSMike Smith 			if (dn[j] == 1) {
403cce0eefdSMike Smith 				conv = 3;
404cce0eefdSMike Smith 				dn[j] = '_';
405cce0eefdSMike Smith 			}
406cce0eefdSMike Smith 			if (dn[j] == 2) {
407952a6212SJordan K. Hubbard 				conv = 3;
408952a6212SJordan K. Hubbard 				dn[j--] = ' ';
409952a6212SJordan K. Hubbard 			}
410952a6212SJordan K. Hubbard 		}
411c4f02a89SMax Khon 		if (i > 0)
412952a6212SJordan K. Hubbard 			conv = 3;
413952a6212SJordan K. Hubbard 		dp--;
414952a6212SJordan K. Hubbard 	} else {
415952a6212SJordan K. Hubbard 		for (dp = cp; *--dp == ' ' || *dp == '.';);
416952a6212SJordan K. Hubbard 		dp++;
417952a6212SJordan K. Hubbard 	}
418952a6212SJordan K. Hubbard 
419952a6212SJordan K. Hubbard 	/*
420952a6212SJordan K. Hubbard 	 * Now convert the rest of the name
421952a6212SJordan K. Hubbard 	 */
422c4f02a89SMax Khon 	for (i = dp - un, j = 0; un < dp && j < 8; j++) {
423bddcdc51STim J. Robbins 		c = unix2doschr(&un, &i, pmp);
424c4f02a89SMax Khon 		if (c & 0xff00) {
425c4f02a89SMax Khon 			dn[j] = c >> 8;
426c4f02a89SMax Khon 			if (++j < 8) {
427c4f02a89SMax Khon 				dn[j] = c;
428697ab829SR. Imura 				if (conv != 3)
429697ab829SR. Imura 					conv = 2;
430c4f02a89SMax Khon 				continue;
431c4f02a89SMax Khon 			} else {
432c4f02a89SMax Khon 				conv = 3;
433c4f02a89SMax Khon 				dn[j-1] = ' ';
434c4f02a89SMax Khon 				break;
435c4f02a89SMax Khon 			}
436c4f02a89SMax Khon 		} else {
437c4f02a89SMax Khon 			dn[j] = c;
438c4f02a89SMax Khon 		}
439697ab829SR. Imura 		if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
440952a6212SJordan K. Hubbard 			conv = 2;
441cce0eefdSMike Smith 		if (dn[j] == 1) {
442cce0eefdSMike Smith 			conv = 3;
443cce0eefdSMike Smith 			dn[j] = '_';
444cce0eefdSMike Smith 		}
445cce0eefdSMike Smith 		if (dn[j] == 2) {
446952a6212SJordan K. Hubbard 			conv = 3;
447952a6212SJordan K. Hubbard 			dn[j--] = ' ';
448952a6212SJordan K. Hubbard 		}
449952a6212SJordan K. Hubbard 	}
450952a6212SJordan K. Hubbard 	if (un < dp)
451952a6212SJordan K. Hubbard 		conv = 3;
452952a6212SJordan K. Hubbard 	/*
453952a6212SJordan K. Hubbard 	 * If we didn't have any chars in filename,
454952a6212SJordan K. Hubbard 	 * generate a default
455952a6212SJordan K. Hubbard 	 */
456952a6212SJordan K. Hubbard 	if (!j)
457952a6212SJordan K. Hubbard 		dn[0] = '_';
458952a6212SJordan K. Hubbard 
459952a6212SJordan K. Hubbard 	/*
460952a6212SJordan K. Hubbard 	 * If there wasn't any char dropped,
461952a6212SJordan K. Hubbard 	 * there is no place for generation numbers
46227a0bc89SDoug Rabson 	 */
463952a6212SJordan K. Hubbard 	if (conv != 3) {
464952a6212SJordan K. Hubbard 		if (gen > 1)
465c4f02a89SMax Khon 			conv = 0;
466c4f02a89SMax Khon 		goto done;
467952a6212SJordan K. Hubbard 	}
468952a6212SJordan K. Hubbard 
469952a6212SJordan K. Hubbard 	/*
470952a6212SJordan K. Hubbard 	 * Now insert the generation number into the filename part
471952a6212SJordan K. Hubbard 	 */
472011cdb57SDmitrij Tejblum 	if (gen == 0)
473c4f02a89SMax Khon 		goto done;
474952a6212SJordan K. Hubbard 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
475952a6212SJordan K. Hubbard 		*--wcp = gen % 10 + '0';
476c4f02a89SMax Khon 	if (gen) {
477c4f02a89SMax Khon 		conv = 0;
478c4f02a89SMax Khon 		goto done;
479c4f02a89SMax Khon 	}
480952a6212SJordan K. Hubbard 	for (i = 8; dn[--i] == ' ';);
481952a6212SJordan K. Hubbard 	i++;
482952a6212SJordan K. Hubbard 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
483952a6212SJordan K. Hubbard 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
484c4f02a89SMax Khon 	/*
485c4f02a89SMax Khon 	 * Correct posision to where insert the generation number
486c4f02a89SMax Khon 	 */
487c4f02a89SMax Khon 	cp = dn;
488c4f02a89SMax Khon 	i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
489c4f02a89SMax Khon 
490952a6212SJordan K. Hubbard 	dn[i++] = '~';
491952a6212SJordan K. Hubbard 	while (wcp < gentext + sizeof(gentext))
492952a6212SJordan K. Hubbard 		dn[i++] = *wcp++;
493c4f02a89SMax Khon 
494c4f02a89SMax Khon 	/*
495c4f02a89SMax Khon 	 * Tail of the filename should be space
496c4f02a89SMax Khon 	 */
497c4f02a89SMax Khon 	while (i < 8)
498c4f02a89SMax Khon 		dn[i++] = ' ';
499c4f02a89SMax Khon 	conv = 3;
500c4f02a89SMax Khon 
501c4f02a89SMax Khon done:
502c4f02a89SMax Khon 	/*
503c4f02a89SMax Khon 	 * The first character cannot be E5,
504c4f02a89SMax Khon 	 * because that means a deleted entry
505c4f02a89SMax Khon 	 */
506c4f02a89SMax Khon 	if (dn[0] == 0xe5)
507c4f02a89SMax Khon 		dn[0] = SLOT_E5;
508c4f02a89SMax Khon 
509c4f02a89SMax Khon 	return conv;
510952a6212SJordan K. Hubbard }
511952a6212SJordan K. Hubbard 
512952a6212SJordan K. Hubbard /*
513952a6212SJordan K. Hubbard  * Create a Win95 long name directory entry
514952a6212SJordan K. Hubbard  * Note: assumes that the filename is valid,
515952a6212SJordan K. Hubbard  *	 i.e. doesn't consist solely of blanks and dots
516952a6212SJordan K. Hubbard  */
517952a6212SJordan K. Hubbard int
5184882501bSEd Maste unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt,
5194882501bSEd Maste     int chksum, struct msdosfsmount *pmp)
520952a6212SJordan K. Hubbard {
521952a6212SJordan K. Hubbard 	u_int8_t *wcp;
522c4f02a89SMax Khon 	int i, end;
52313df76f2SAndrey A. Chernov 	u_int16_t code;
524952a6212SJordan K. Hubbard 
525952a6212SJordan K. Hubbard 	/*
526952a6212SJordan K. Hubbard 	 * Drop trailing blanks and dots
527952a6212SJordan K. Hubbard 	 */
528c4f02a89SMax Khon 	unlen = winLenFixup(un, unlen);
529952a6212SJordan K. Hubbard 
530c4f02a89SMax Khon 	/*
531c4f02a89SMax Khon 	 * Cut *un for this slot
532c4f02a89SMax Khon 	 */
533c4f02a89SMax Khon 	unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
534c4f02a89SMax Khon 			  pmp->pm_flags, pmp->pm_u2w);
535952a6212SJordan K. Hubbard 
536952a6212SJordan K. Hubbard 	/*
537952a6212SJordan K. Hubbard 	 * Initialize winentry to some useful default
538952a6212SJordan K. Hubbard 	 */
539952a6212SJordan K. Hubbard 	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
540952a6212SJordan K. Hubbard 	wep->weCnt = cnt;
541952a6212SJordan K. Hubbard 	wep->weAttributes = ATTR_WIN95;
542952a6212SJordan K. Hubbard 	wep->weReserved1 = 0;
543952a6212SJordan K. Hubbard 	wep->weChksum = chksum;
544952a6212SJordan K. Hubbard 	wep->weReserved2 = 0;
545952a6212SJordan K. Hubbard 
546952a6212SJordan K. Hubbard 	/*
547952a6212SJordan K. Hubbard 	 * Now convert the filename parts
548952a6212SJordan K. Hubbard 	 */
549c4f02a89SMax Khon 	end = 0;
550c4f02a89SMax Khon 	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
551bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
55213df76f2SAndrey A. Chernov 		*wcp++ = code;
55313df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
554c4f02a89SMax Khon 		if (!code)
555c4f02a89SMax Khon 			end = WIN_LAST;
556952a6212SJordan K. Hubbard 	}
557c4f02a89SMax Khon 	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
558bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
55913df76f2SAndrey A. Chernov 		*wcp++ = code;
56013df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
561c4f02a89SMax Khon 		if (!code)
562c4f02a89SMax Khon 			end = WIN_LAST;
563952a6212SJordan K. Hubbard 	}
564c4f02a89SMax Khon 	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
565bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
56613df76f2SAndrey A. Chernov 		*wcp++ = code;
56713df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
568c4f02a89SMax Khon 		if (!code)
569c4f02a89SMax Khon 			end = WIN_LAST;
570952a6212SJordan K. Hubbard 	}
571c4f02a89SMax Khon 	if (*un == '\0')
572c4f02a89SMax Khon 		end = WIN_LAST;
573c4f02a89SMax Khon 	wep->weCnt |= end;
574c4f02a89SMax Khon 	return !end;
5754d63452fSAndrey A. Chernov }
5764d63452fSAndrey A. Chernov 
577952a6212SJordan K. Hubbard /*
578952a6212SJordan K. Hubbard  * Compare our filename to the one in the Win95 entry
579952a6212SJordan K. Hubbard  * Returns the checksum or -1 if no match
580952a6212SJordan K. Hubbard  */
581952a6212SJordan K. Hubbard int
5824882501bSEd Maste winChkName(struct mbnambuf *nbp, const u_char *un, size_t unlen, int chksum,
5834882501bSEd Maste     struct msdosfsmount *pmp)
584952a6212SJordan K. Hubbard {
585bddcdc51STim J. Robbins 	size_t len;
586c4f02a89SMax Khon 	u_int16_t c1, c2;
587c4f02a89SMax Khon 	u_char *np;
588c4f02a89SMax Khon 	struct dirent dirbuf;
589952a6212SJordan K. Hubbard 
590952a6212SJordan K. Hubbard 	/*
591c2819440SBruce Evans 	 * We already have winentry in *nbp.
592952a6212SJordan K. Hubbard 	 */
593c2819440SBruce Evans 	if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0)
594952a6212SJordan K. Hubbard 		return -1;
595952a6212SJordan K. Hubbard 
596c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG
597c4f02a89SMax Khon 	printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
598c4f02a89SMax Khon 							dirbuf.d_name,
599c4f02a89SMax Khon 							dirbuf.d_namlen);
600c4f02a89SMax Khon #endif
601952a6212SJordan K. Hubbard 
602952a6212SJordan K. Hubbard 	/*
603952a6212SJordan K. Hubbard 	 * Compare the name parts
604952a6212SJordan K. Hubbard 	 */
605c4f02a89SMax Khon 	len = dirbuf.d_namlen;
606c4f02a89SMax Khon 	if (unlen != len)
607c4f02a89SMax Khon 		return -2;
608c4f02a89SMax Khon 
609c4f02a89SMax Khon 	for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
610c4f02a89SMax Khon 		/*
6110f4e4130SMax Khon 		 * Comparison must be case insensitive, because FAT disallows
6120f4e4130SMax Khon 		 * to look up or create files in case sensitive even when
6130f4e4130SMax Khon 		 * it's a long file name.
614c4f02a89SMax Khon 		 */
615*09bb4a31SDimitry Andric 		c1 = unix2winchr(__DECONST(const u_char **, &np), &len,
616*09bb4a31SDimitry Andric 		    LCASE_BASE, pmp);
617bddcdc51STim J. Robbins 		c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
618b998efa9SAndrey A. Chernov 		if (c1 != c2)
619c4f02a89SMax Khon 			return -2;
620952a6212SJordan K. Hubbard 	}
621952a6212SJordan K. Hubbard 	return chksum;
622952a6212SJordan K. Hubbard }
623952a6212SJordan K. Hubbard 
624952a6212SJordan K. Hubbard /*
625952a6212SJordan K. Hubbard  * Convert Win95 filename to dirbuf.
626952a6212SJordan K. Hubbard  * Returns the checksum or -1 if impossible
627952a6212SJordan K. Hubbard  */
628952a6212SJordan K. Hubbard int
6294882501bSEd Maste win2unixfn(struct mbnambuf *nbp, struct winentry *wep, int chksum,
6304882501bSEd Maste     struct msdosfsmount *pmp)
631952a6212SJordan K. Hubbard {
632e2ee19e3SKevin Lo 	u_char *c, tmpbuf[5];
633952a6212SJordan K. Hubbard 	u_int8_t *cp;
63441f1dcccSKevin Lo 	u_int8_t *np, name[WIN_CHARS * 3 + 1];
6352b5b6623SAndrey A. Chernov 	u_int16_t code;
636952a6212SJordan K. Hubbard 	int i;
637952a6212SJordan K. Hubbard 
638952a6212SJordan K. Hubbard 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
639952a6212SJordan K. Hubbard 	    || !(wep->weCnt&WIN_CNT))
640952a6212SJordan K. Hubbard 		return -1;
641952a6212SJordan K. Hubbard 
642952a6212SJordan K. Hubbard 	/*
643952a6212SJordan K. Hubbard 	 * First compare checksums
644952a6212SJordan K. Hubbard 	 */
645952a6212SJordan K. Hubbard 	if (wep->weCnt&WIN_LAST) {
646952a6212SJordan K. Hubbard 		chksum = wep->weChksum;
647952a6212SJordan K. Hubbard 	} else if (chksum != wep->weChksum)
648952a6212SJordan K. Hubbard 		chksum = -1;
649952a6212SJordan K. Hubbard 	if (chksum == -1)
650952a6212SJordan K. Hubbard 		return -1;
651952a6212SJordan K. Hubbard 
652952a6212SJordan K. Hubbard 	/*
653952a6212SJordan K. Hubbard 	 * Convert the name parts
654952a6212SJordan K. Hubbard 	 */
655c4f02a89SMax Khon 	np = name;
656952a6212SJordan K. Hubbard 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
6572b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
6582b5b6623SAndrey A. Chernov 		switch (code) {
659952a6212SJordan K. Hubbard 		case 0:
6602b5b6623SAndrey A. Chernov 			*np = '\0';
661c2819440SBruce Evans 			mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
662952a6212SJordan K. Hubbard 			return chksum;
663952a6212SJordan K. Hubbard 		case '/':
6642b5b6623SAndrey A. Chernov 			*np = '\0';
665952a6212SJordan K. Hubbard 			return -1;
6662b5b6623SAndrey A. Chernov 		default:
667e2ee19e3SKevin Lo 			c = win2unixchr(tmpbuf, code, pmp);
66841f1dcccSKevin Lo 			while (*c != '\0')
66941f1dcccSKevin Lo 				*np++ = *c++;
6702b5b6623SAndrey A. Chernov 			break;
671952a6212SJordan K. Hubbard 		}
6722b5b6623SAndrey A. Chernov 		cp += 2;
673952a6212SJordan K. Hubbard 	}
674952a6212SJordan K. Hubbard 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
6752b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
6762b5b6623SAndrey A. Chernov 		switch (code) {
677952a6212SJordan K. Hubbard 		case 0:
6782b5b6623SAndrey A. Chernov 			*np = '\0';
679c2819440SBruce Evans 			mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
680952a6212SJordan K. Hubbard 			return chksum;
681952a6212SJordan K. Hubbard 		case '/':
6822b5b6623SAndrey A. Chernov 			*np = '\0';
683952a6212SJordan K. Hubbard 			return -1;
6842b5b6623SAndrey A. Chernov 		default:
685e2ee19e3SKevin Lo 			c = win2unixchr(tmpbuf, code, pmp);
68641f1dcccSKevin Lo 			while (*c != '\0')
68741f1dcccSKevin Lo 				*np++ = *c++;
6882b5b6623SAndrey A. Chernov 			break;
689952a6212SJordan K. Hubbard 		}
6902b5b6623SAndrey A. Chernov 		cp += 2;
691952a6212SJordan K. Hubbard 	}
692952a6212SJordan K. Hubbard 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
6932b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
6942b5b6623SAndrey A. Chernov 		switch (code) {
695952a6212SJordan K. Hubbard 		case 0:
6962b5b6623SAndrey A. Chernov 			*np = '\0';
697c2819440SBruce Evans 			mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
698952a6212SJordan K. Hubbard 			return chksum;
699952a6212SJordan K. Hubbard 		case '/':
7002b5b6623SAndrey A. Chernov 			*np = '\0';
701952a6212SJordan K. Hubbard 			return -1;
7022b5b6623SAndrey A. Chernov 		default:
703e2ee19e3SKevin Lo 			c = win2unixchr(tmpbuf, code, pmp);
70441f1dcccSKevin Lo 			while (*c != '\0')
70541f1dcccSKevin Lo 				*np++ = *c++;
7062b5b6623SAndrey A. Chernov 			break;
707952a6212SJordan K. Hubbard 		}
7082b5b6623SAndrey A. Chernov 		cp += 2;
709952a6212SJordan K. Hubbard 	}
710c4f02a89SMax Khon 	*np = '\0';
711c2819440SBruce Evans 	mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
712952a6212SJordan K. Hubbard 	return chksum;
713952a6212SJordan K. Hubbard }
714952a6212SJordan K. Hubbard 
715952a6212SJordan K. Hubbard /*
7162a05fbb9SNate Lawson  * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
717952a6212SJordan K. Hubbard  */
718952a6212SJordan K. Hubbard u_int8_t
71948d1bcf8SKonstantin Belousov winChksum(u_int8_t *name)
720952a6212SJordan K. Hubbard {
72148d1bcf8SKonstantin Belousov 	int i;
722952a6212SJordan K. Hubbard 	u_int8_t s;
723952a6212SJordan K. Hubbard 
72448d1bcf8SKonstantin Belousov 	for (s = 0, i = 11; --i >= 0; s += *name++)
72548d1bcf8SKonstantin Belousov 		s = (s << 7)|(s >> 1);
7262a05fbb9SNate Lawson 	return (s);
727952a6212SJordan K. Hubbard }
728952a6212SJordan K. Hubbard 
729952a6212SJordan K. Hubbard /*
730952a6212SJordan K. Hubbard  * Determine the number of slots necessary for Win95 names
731952a6212SJordan K. Hubbard  */
732952a6212SJordan K. Hubbard int
7334882501bSEd Maste winSlotCnt(const u_char *un, size_t unlen, struct msdosfsmount *pmp)
734952a6212SJordan K. Hubbard {
735c4f02a89SMax Khon 	size_t wlen;
736c4f02a89SMax Khon 	char wn[WIN_MAXLEN * 2 + 1], *wnp;
737c4f02a89SMax Khon 
73869a36f24SMike Smith 	unlen = winLenFixup(un, unlen);
739c4f02a89SMax Khon 
740c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
741c4f02a89SMax Khon 		wlen = WIN_MAXLEN * 2;
742c4f02a89SMax Khon 		wnp = wn;
743bddcdc51STim J. Robbins 		msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
744c4f02a89SMax Khon 		if (unlen > 0)
745c4f02a89SMax Khon 			return 0;
746c4f02a89SMax Khon 		return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
747c4f02a89SMax Khon 	}
748c4f02a89SMax Khon 
749952a6212SJordan K. Hubbard 	if (unlen > WIN_MAXLEN)
750952a6212SJordan K. Hubbard 		return 0;
751952a6212SJordan K. Hubbard 	return howmany(unlen, WIN_CHARS);
75227a0bc89SDoug Rabson }
75369a36f24SMike Smith 
75469a36f24SMike Smith /*
755a2098feaSGabor Kovesdan  * Determine the number of bytes neccessary for Win95 names
75669a36f24SMike Smith  */
757bddcdc51STim J. Robbins size_t
7584882501bSEd Maste winLenFixup(const u_char *un, size_t unlen)
75969a36f24SMike Smith {
76069a36f24SMike Smith 	for (un += unlen; unlen > 0; unlen--)
76169a36f24SMike Smith 		if (*--un != ' ' && *un != '.')
76269a36f24SMike Smith 			break;
76369a36f24SMike Smith 	return unlen;
76469a36f24SMike Smith }
765c4f02a89SMax Khon 
766c4f02a89SMax Khon /*
767a8b5c2a0SGabor Kovesdan  * Store an area with multi byte string instr, and returns left
768c4f02a89SMax Khon  * byte of instr and moves pointer forward. The area's size is
769c4f02a89SMax Khon  * inlen or outlen.
770c4f02a89SMax Khon  */
771c4f02a89SMax Khon static int
772bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
773c4f02a89SMax Khon {
774c4f02a89SMax Khon 	char *outp, outstr[outlen * weight + 1];
775c4f02a89SMax Khon 
776c4f02a89SMax Khon 	if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
777c4f02a89SMax Khon 		outp = outstr;
778c4f02a89SMax Khon 		outlen *= weight;
779bddcdc51STim J. Robbins 		msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
780c4f02a89SMax Khon 		return (inlen);
781c4f02a89SMax Khon 	}
782c4f02a89SMax Khon 
783c4f02a89SMax Khon 	(*instr) += min(inlen, outlen);
784c4f02a89SMax Khon 	return (inlen - min(inlen, outlen));
785c4f02a89SMax Khon }
786c4f02a89SMax Khon 
787c4f02a89SMax Khon /*
788c4f02a89SMax Khon  * Convert DOS char to Local char
789c4f02a89SMax Khon  */
79041f1dcccSKevin Lo static u_char *
791e2ee19e3SKevin Lo dos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
792c4f02a89SMax Khon {
793e2ee19e3SKevin Lo 	u_char c, *outp;
794c4f02a89SMax Khon 	size_t len, olen;
795c4f02a89SMax Khon 
796c4f02a89SMax Khon 	outp = outbuf;
79741f1dcccSKevin Lo 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
79841f1dcccSKevin Lo 		olen = len = 4;
799c4f02a89SMax Khon 
800c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
801c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
80241f1dcccSKevin Lo 						  ilen, (char **)&outp, &olen, KICONV_LOWER);
803c4f02a89SMax Khon 		else
804c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
80541f1dcccSKevin Lo 					     ilen, (char **)&outp, &olen);
806c4f02a89SMax Khon 		len -= olen;
807c4f02a89SMax Khon 
808c4f02a89SMax Khon 		/*
809c4f02a89SMax Khon 		 * return '?' if failed to convert
810c4f02a89SMax Khon 		 */
811c4f02a89SMax Khon 		if (len == 0) {
812c4f02a89SMax Khon 			(*ilen)--;
813c4f02a89SMax Khon 			(*instr)++;
81441f1dcccSKevin Lo 			*outp++ = '?';
815c4f02a89SMax Khon 		}
81641f1dcccSKevin Lo 	} else {
817c4f02a89SMax Khon 		(*ilen)--;
818c4f02a89SMax Khon 		c = *(*instr)++;
819c4f02a89SMax Khon 		c = dos2unix[c];
820c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
821c4f02a89SMax Khon 			c = u2l[c];
82241f1dcccSKevin Lo 		*outp++ = c;
82341f1dcccSKevin Lo 		outbuf[1] = '\0';
82441f1dcccSKevin Lo 	}
82541f1dcccSKevin Lo 
82641f1dcccSKevin Lo 	*outp = '\0';
82741f1dcccSKevin Lo 	outp = outbuf;
82841f1dcccSKevin Lo 	return (outp);
829c4f02a89SMax Khon }
830c4f02a89SMax Khon 
831c4f02a89SMax Khon /*
832c4f02a89SMax Khon  * Convert Local char to DOS char
833c4f02a89SMax Khon  */
834c4f02a89SMax Khon static u_int16_t
835c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
836c4f02a89SMax Khon {
837c4f02a89SMax Khon 	u_char c;
838c4f02a89SMax Khon 	char *up, *outp, unicode[3], outbuf[3];
839c4f02a89SMax Khon 	u_int16_t wc;
840697ab829SR. Imura 	size_t len, ucslen, unixlen, olen;
841c4f02a89SMax Khon 
842c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
843c4f02a89SMax Khon 		/*
844c4f02a89SMax Khon 		 * to hide an invisible character, using a unicode filter
845c4f02a89SMax Khon 		 */
846697ab829SR. Imura 		ucslen = 2;
847c4f02a89SMax Khon 		len = *ilen;
848c4f02a89SMax Khon 		up = unicode;
849c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
850697ab829SR. Imura 				     ilen, &up, &ucslen);
851697ab829SR. Imura 		unixlen = len - *ilen;
852c4f02a89SMax Khon 
853c4f02a89SMax Khon 		/*
854c4f02a89SMax Khon 		 * cannot be converted
855c4f02a89SMax Khon 		 */
856697ab829SR. Imura 		if (unixlen == 0) {
857c4f02a89SMax Khon 			(*ilen)--;
858c4f02a89SMax Khon 			(*instr)++;
859c4f02a89SMax Khon 			return (0);
860c4f02a89SMax Khon 		}
861c4f02a89SMax Khon 
862c4f02a89SMax Khon 		/*
863c4f02a89SMax Khon 		 * return magic number for ascii char
864c4f02a89SMax Khon 		 */
865697ab829SR. Imura 		if (unixlen == 1) {
866c4f02a89SMax Khon 			c = *(*instr -1);
867c4f02a89SMax Khon 			if (! (c & 0x80)) {
868c4f02a89SMax Khon 				c = unix2dos[c];
869c4f02a89SMax Khon 				if (c <= 2)
870c4f02a89SMax Khon 					return (c);
871c4f02a89SMax Khon 			}
872c4f02a89SMax Khon 		}
873c4f02a89SMax Khon 
874c4f02a89SMax Khon 		/*
875c4f02a89SMax Khon 		 * now convert using libiconv
876c4f02a89SMax Khon 		 */
877697ab829SR. Imura 		*instr -= unixlen;
878697ab829SR. Imura 		*ilen = len;
879c4f02a89SMax Khon 
880c4f02a89SMax Khon 		olen = len = 2;
881c4f02a89SMax Khon 		outp = outbuf;
882c4f02a89SMax Khon 		msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
883c4f02a89SMax Khon 					  ilen, &outp, &olen, KICONV_FROM_UPPER);
884c4f02a89SMax Khon 		len -= olen;
885697ab829SR. Imura 
886697ab829SR. Imura 		/*
887697ab829SR. Imura 		 * cannot be converted, but has unicode char, should return magic number
888697ab829SR. Imura 		 */
889697ab829SR. Imura 		if (len == 0) {
890697ab829SR. Imura 			(*ilen) -= unixlen;
891697ab829SR. Imura 			(*instr) += unixlen;
892697ab829SR. Imura 			return (1);
893697ab829SR. Imura 		}
894697ab829SR. Imura 
895c4f02a89SMax Khon 		wc = 0;
896c4f02a89SMax Khon 		while(len--)
897c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
898c4f02a89SMax Khon 		return (wc);
899c4f02a89SMax Khon 	}
900c4f02a89SMax Khon 
901c4f02a89SMax Khon 	(*ilen)--;
902c4f02a89SMax Khon 	c = *(*instr)++;
903c4f02a89SMax Khon 	c = l2u[c];
904c4f02a89SMax Khon 	c = unix2dos[c];
905c4f02a89SMax Khon 	return ((u_int16_t)c);
906c4f02a89SMax Khon }
907c4f02a89SMax Khon 
908c4f02a89SMax Khon /*
909c4f02a89SMax Khon  * Convert Windows char to Local char
910c4f02a89SMax Khon  */
91141f1dcccSKevin Lo static u_char *
912e2ee19e3SKevin Lo win2unixchr(u_char *outbuf, u_int16_t wc, struct msdosfsmount *pmp)
913c4f02a89SMax Khon {
914e2ee19e3SKevin Lo 	u_char *inp, *outp, inbuf[3];
915c4f02a89SMax Khon 	size_t ilen, olen, len;
916c4f02a89SMax Khon 
91741f1dcccSKevin Lo 	outp = outbuf;
918c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
919c4f02a89SMax Khon 		inbuf[0] = (u_char)(wc>>8);
920c4f02a89SMax Khon 		inbuf[1] = (u_char)wc;
921c4f02a89SMax Khon 		inbuf[2] = '\0';
922c4f02a89SMax Khon 
92341f1dcccSKevin Lo 		ilen = 2;
92441f1dcccSKevin Lo 		olen = len = 4;
925c4f02a89SMax Khon 		inp = inbuf;
926*09bb4a31SDimitry Andric 		msdosfs_iconv->convchr(pmp->pm_w2u, __DECONST(const char **,
927*09bb4a31SDimitry Andric 		    &inp), &ilen, (char **)&outp, &olen);
928c4f02a89SMax Khon 		len -= olen;
929c4f02a89SMax Khon 
930c4f02a89SMax Khon 		/*
931c4f02a89SMax Khon 		 * return '?' if failed to convert
932c4f02a89SMax Khon 		 */
93341f1dcccSKevin Lo 		if (len == 0)
93441f1dcccSKevin Lo 			*outp++ = '?';
93541f1dcccSKevin Lo 	} else {
93641f1dcccSKevin Lo 		*outp++ = (wc & 0xff00) ? '?' : (u_char)(wc & 0xff);
937c4f02a89SMax Khon 	}
938c4f02a89SMax Khon 
93941f1dcccSKevin Lo 	*outp = '\0';
94041f1dcccSKevin Lo 	outp = outbuf;
94141f1dcccSKevin Lo 	return (outp);
942c4f02a89SMax Khon }
943c4f02a89SMax Khon 
944c4f02a89SMax Khon /*
945c4f02a89SMax Khon  * Convert Local char to Windows char
946c4f02a89SMax Khon  */
947c4f02a89SMax Khon static u_int16_t
948c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
949c4f02a89SMax Khon {
950c4f02a89SMax Khon 	u_char *outp, outbuf[3];
951c4f02a89SMax Khon 	u_int16_t wc;
952c4f02a89SMax Khon 	size_t olen;
953c4f02a89SMax Khon 
954c4f02a89SMax Khon 	if (*ilen == 0)
955c4f02a89SMax Khon 		return (0);
956c4f02a89SMax Khon 
957c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
958c4f02a89SMax Khon 		outp = outbuf;
959c4f02a89SMax Khon 		olen = 2;
960c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
961c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
962c4f02a89SMax Khon 						  ilen, (char **)&outp, &olen,
963c4f02a89SMax Khon 						  KICONV_FROM_LOWER);
964c4f02a89SMax Khon 		else
965c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
966c4f02a89SMax Khon 					     ilen, (char **)&outp, &olen);
967c4f02a89SMax Khon 
968c4f02a89SMax Khon 		/*
969c4f02a89SMax Khon 		 * return '0' if end of filename
970c4f02a89SMax Khon 		 */
971c4f02a89SMax Khon 		if (olen == 2)
972c4f02a89SMax Khon 			return (0);
973c4f02a89SMax Khon 
974c4f02a89SMax Khon 		wc = (outbuf[0]<<8) | outbuf[1];
975c4f02a89SMax Khon 
976c4f02a89SMax Khon 		return (wc);
977c4f02a89SMax Khon 	}
978c4f02a89SMax Khon 
979c4f02a89SMax Khon 	(*ilen)--;
980c4f02a89SMax Khon 	wc = (*instr)[0];
981c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
982c4f02a89SMax Khon 		wc = u2l[wc];
983c4f02a89SMax Khon 	(*instr)++;
984c4f02a89SMax Khon 	return (wc);
985c4f02a89SMax Khon }
986c4f02a89SMax Khon 
987c4f02a89SMax Khon /*
988c2819440SBruce Evans  * Initialize the temporary concatenation buffer.
989c4f02a89SMax Khon  */
990c4f02a89SMax Khon void
991c2819440SBruce Evans mbnambuf_init(struct mbnambuf *nbp)
992c4f02a89SMax Khon {
993c4f02a89SMax Khon 
994c2819440SBruce Evans 	nbp->nb_len = 0;
995c2819440SBruce Evans 	nbp->nb_last_id = -1;
996c2819440SBruce Evans 	nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0';
997c4f02a89SMax Khon }
998c4f02a89SMax Khon 
999c4f02a89SMax Khon /*
100058ad326bSNate Lawson  * Fill out our concatenation buffer with the given substring, at the offset
100158ad326bSNate Lawson  * specified by its id.  Since this function must be called with ids in
100258ad326bSNate Lawson  * descending order, we take advantage of the fact that ASCII substrings are
100358ad326bSNate Lawson  * exactly WIN_CHARS in length.  For non-ASCII substrings, we shift all
100458ad326bSNate Lawson  * previous (i.e. higher id) substrings upwards to make room for this one.
100558ad326bSNate Lawson  * This only penalizes portions of substrings that contain more than
100658ad326bSNate Lawson  * WIN_CHARS bytes when they are first encountered.
1007c4f02a89SMax Khon  */
1008c4f02a89SMax Khon void
1009c2819440SBruce Evans mbnambuf_write(struct mbnambuf *nbp, char *name, int id)
1010c4f02a89SMax Khon {
101158ad326bSNate Lawson 	char *slot;
1012c2819440SBruce Evans 	size_t count, newlen;
10134cdb1483SNate Lawson 
1014abb0cbf9SEdward Tomasz Napierala 	if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) {
101554cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
1016abb0cbf9SEdward Tomasz Napierala 		printf("msdosfs: non-decreasing id: id %d, last id %d\n",
1017abb0cbf9SEdward Tomasz Napierala 		    id, nbp->nb_last_id);
101854cf9198SKonstantin Belousov #endif
1019abb0cbf9SEdward Tomasz Napierala 		return;
1020abb0cbf9SEdward Tomasz Napierala 	}
102158ad326bSNate Lawson 
1022c2819440SBruce Evans 	/* Will store this substring in a WIN_CHARS-aligned slot. */
1023c2819440SBruce Evans 	slot = &nbp->nb_buf[id * WIN_CHARS];
10244cdb1483SNate Lawson 	count = strlen(name);
1025c2819440SBruce Evans 	newlen = nbp->nb_len + count;
1026c2819440SBruce Evans 	if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) {
102754cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
1028c2819440SBruce Evans 		printf("msdosfs: file name length %zu too large\n", newlen);
102954cf9198SKonstantin Belousov #endif
10304cdb1483SNate Lawson 		return;
1031c4f02a89SMax Khon 	}
103258ad326bSNate Lawson 
103358ad326bSNate Lawson 	/* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
1034c2819440SBruce Evans 	if (count > WIN_CHARS && nbp->nb_len != 0)
1035c2819440SBruce Evans 		bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len);
103658ad326bSNate Lawson 
103758ad326bSNate Lawson 	/* Copy in the substring to its slot and update length so far. */
103858ad326bSNate Lawson 	bcopy(name, slot, count);
1039c2819440SBruce Evans 	nbp->nb_len = newlen;
1040c2819440SBruce Evans 	nbp->nb_last_id = id;
1041c4f02a89SMax Khon }
1042c4f02a89SMax Khon 
1043c4f02a89SMax Khon /*
10444cdb1483SNate Lawson  * Take the completed string and use it to setup the struct dirent.
10454cdb1483SNate Lawson  * Be sure to always nul-terminate the d_name and then copy the string
10464cdb1483SNate Lawson  * from our buffer.  Note that this function assumes the full string has
10474cdb1483SNate Lawson  * been reassembled in the buffer.  If it's called before all substrings
10484cdb1483SNate Lawson  * have been written via mbnambuf_write(), the result will be incorrect.
1049c4f02a89SMax Khon  */
1050c4f02a89SMax Khon char *
1051c2819440SBruce Evans mbnambuf_flush(struct mbnambuf *nbp, struct dirent *dp)
1052c4f02a89SMax Khon {
1053c4f02a89SMax Khon 
1054c2819440SBruce Evans 	if (nbp->nb_len > sizeof(dp->d_name) - 1) {
1055c2819440SBruce Evans 		mbnambuf_init(nbp);
1056c4f02a89SMax Khon 		return (NULL);
1057c4f02a89SMax Khon 	}
1058c2819440SBruce Evans 	bcopy(&nbp->nb_buf[0], dp->d_name, nbp->nb_len);
1059c2819440SBruce Evans 	dp->d_name[nbp->nb_len] = '\0';
1060c2819440SBruce Evans 	dp->d_namlen = nbp->nb_len;
10614cdb1483SNate Lawson 
1062c2819440SBruce Evans 	mbnambuf_init(nbp);
1063c4f02a89SMax Khon 	return (dp->d_name);
1064c4f02a89SMax Khon }
1065