xref: /freebsd/sys/fs/msdosfs/msdosfs_conv.c (revision abb0cbf9c9c16a927d844d16cc203b84232c6ed1)
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);
64c4f02a89SMax Khon static u_int16_t dos2unixchr(const u_char **, size_t *, int, struct msdosfsmount *);
65c4f02a89SMax Khon static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
66c4f02a89SMax Khon static u_int16_t win2unixchr(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
237c4f02a89SMax Khon dos2unixfn(dn, un, lower, pmp)
23827a0bc89SDoug Rabson 	u_char dn[11];
23927a0bc89SDoug Rabson 	u_char *un;
2405114f1d7SAndrey A. Chernov 	int lower;
241c4f02a89SMax Khon 	struct msdosfsmount *pmp;
24227a0bc89SDoug Rabson {
243bddcdc51STim J. Robbins 	size_t i;
244c4f02a89SMax Khon 	int thislong = 0;
245c4f02a89SMax Khon 	u_int16_t c;
24627a0bc89SDoug Rabson 
24727a0bc89SDoug Rabson 	/*
24827a0bc89SDoug Rabson 	 * If first char of the filename is SLOT_E5 (0x05), then the real
24927a0bc89SDoug Rabson 	 * first char of the filename should be 0xe5. But, they couldn't
25027a0bc89SDoug Rabson 	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
25127a0bc89SDoug Rabson 	 * directory slot. Another dos quirk.
25227a0bc89SDoug Rabson 	 */
253952a6212SJordan K. Hubbard 	if (*dn == SLOT_E5)
254c4f02a89SMax Khon 		*dn = 0xe5;
25527a0bc89SDoug Rabson 
256952a6212SJordan K. Hubbard 	/*
257952a6212SJordan K. Hubbard 	 * Copy the name portion into the unix filename string.
258952a6212SJordan K. Hubbard 	 */
259c4f02a89SMax Khon 	for (i = 8; i > 0 && *dn != ' ';) {
260cc005bb9SMaxim Konovalov 		c = dos2unixchr((const u_char **)&dn, &i, lower & LCASE_BASE,
261cc005bb9SMaxim Konovalov 		    pmp);
262c4f02a89SMax Khon 		if (c & 0xff00) {
263c4f02a89SMax Khon 			*un++ = c >> 8;
264952a6212SJordan K. Hubbard 			thislong++;
265952a6212SJordan K. Hubbard 		}
266c4f02a89SMax Khon 		*un++ = c;
267c4f02a89SMax Khon 		thislong++;
268c4f02a89SMax Khon 	}
269c4f02a89SMax Khon 	dn += i;
270952a6212SJordan K. Hubbard 
271952a6212SJordan K. Hubbard 	/*
272952a6212SJordan K. Hubbard 	 * Now, if there is an extension then put in a period and copy in
273952a6212SJordan K. Hubbard 	 * the extension.
274952a6212SJordan K. Hubbard 	 */
275952a6212SJordan K. Hubbard 	if (*dn != ' ') {
276952a6212SJordan K. Hubbard 		*un++ = '.';
277952a6212SJordan K. Hubbard 		thislong++;
278c4f02a89SMax Khon 		for (i = 3; i > 0 && *dn != ' ';) {
279cc005bb9SMaxim Konovalov 			c = dos2unixchr((const u_char **)&dn, &i,
280cc005bb9SMaxim Konovalov 			    lower & LCASE_EXT, pmp);
281c4f02a89SMax Khon 			if (c & 0xff00) {
282c4f02a89SMax Khon 				*un++ = c >> 8;
283c4f02a89SMax Khon 				thislong++;
284c4f02a89SMax Khon 			}
285c4f02a89SMax Khon 			*un++ = c;
286952a6212SJordan K. Hubbard 			thislong++;
287952a6212SJordan K. Hubbard 		}
288952a6212SJordan K. Hubbard 	}
289952a6212SJordan K. Hubbard 	*un++ = 0;
290952a6212SJordan K. Hubbard 
291952a6212SJordan K. Hubbard 	return (thislong);
29227a0bc89SDoug Rabson }
29327a0bc89SDoug Rabson 
29427a0bc89SDoug Rabson /*
295952a6212SJordan K. Hubbard  * Convert a unix filename to a DOS filename according to Win95 rules.
296952a6212SJordan K. Hubbard  * If applicable and gen is not 0, it is inserted into the converted
297952a6212SJordan K. Hubbard  * filename as a generation number.
298952a6212SJordan K. Hubbard  * Returns
299952a6212SJordan K. Hubbard  *	0 if name couldn't be converted
300952a6212SJordan K. Hubbard  *	1 if the converted name is the same as the original
301952a6212SJordan K. Hubbard  *	  (no long filename entry necessary for Win95)
302952a6212SJordan K. Hubbard  *	2 if conversion was successful
303952a6212SJordan K. Hubbard  *	3 if conversion was successful and generation number was inserted
30427a0bc89SDoug Rabson  */
305952a6212SJordan K. Hubbard int
306c4f02a89SMax Khon unix2dosfn(un, dn, unlen, gen, pmp)
307952a6212SJordan K. Hubbard 	const u_char *un;
308952a6212SJordan K. Hubbard 	u_char dn[12];
309bddcdc51STim J. Robbins 	size_t unlen;
310952a6212SJordan K. Hubbard 	u_int gen;
311c4f02a89SMax Khon 	struct msdosfsmount *pmp;
31227a0bc89SDoug Rabson {
313bddcdc51STim J. Robbins 	ssize_t i, j;
314bddcdc51STim J. Robbins 	int l;
315952a6212SJordan K. Hubbard 	int conv = 1;
316952a6212SJordan K. Hubbard 	const u_char *cp, *dp, *dp1;
317952a6212SJordan K. Hubbard 	u_char gentext[6], *wcp;
318c4f02a89SMax Khon 	u_int16_t c;
31927a0bc89SDoug Rabson 
32027a0bc89SDoug Rabson 	/*
32127a0bc89SDoug Rabson 	 * Fill the dos filename string with blanks. These are DOS's pad
32227a0bc89SDoug Rabson 	 * characters.
32327a0bc89SDoug Rabson 	 */
324952a6212SJordan K. Hubbard 	for (i = 0; i < 11; i++)
32527a0bc89SDoug Rabson 		dn[i] = ' ';
326952a6212SJordan K. Hubbard 	dn[11] = 0;
32727a0bc89SDoug Rabson 
32827a0bc89SDoug Rabson 	/*
32927a0bc89SDoug Rabson 	 * The filenames "." and ".." are handled specially, since they
33027a0bc89SDoug Rabson 	 * don't follow dos filename rules.
33127a0bc89SDoug Rabson 	 */
33227a0bc89SDoug Rabson 	if (un[0] == '.' && unlen == 1) {
33327a0bc89SDoug Rabson 		dn[0] = '.';
334952a6212SJordan K. Hubbard 		return gen <= 1;
33527a0bc89SDoug Rabson 	}
33627a0bc89SDoug Rabson 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
33727a0bc89SDoug Rabson 		dn[0] = '.';
33827a0bc89SDoug Rabson 		dn[1] = '.';
339952a6212SJordan K. Hubbard 		return gen <= 1;
34027a0bc89SDoug Rabson 	}
34127a0bc89SDoug Rabson 
34227a0bc89SDoug Rabson 	/*
343952a6212SJordan K. Hubbard 	 * Filenames with only blanks and dots are not allowed!
34427a0bc89SDoug Rabson 	 */
345952a6212SJordan K. Hubbard 	for (cp = un, i = unlen; --i >= 0; cp++)
346952a6212SJordan K. Hubbard 		if (*cp != ' ' && *cp != '.')
347952a6212SJordan K. Hubbard 			break;
348952a6212SJordan K. Hubbard 	if (i < 0)
349952a6212SJordan K. Hubbard 		return 0;
350952a6212SJordan K. Hubbard 
351cce0eefdSMike Smith 
352cce0eefdSMike Smith 	/*
353cce0eefdSMike Smith 	 * Filenames with some characters are not allowed!
354cce0eefdSMike Smith 	 */
355c4f02a89SMax Khon 	for (cp = un, i = unlen; i > 0;)
356c4f02a89SMax Khon 		if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
357cce0eefdSMike Smith 			return 0;
358cce0eefdSMike Smith 
359952a6212SJordan K. Hubbard 	/*
360952a6212SJordan K. Hubbard 	 * Now find the extension
361952a6212SJordan K. Hubbard 	 * Note: dot as first char doesn't start extension
362952a6212SJordan K. Hubbard 	 *	 and trailing dots and blanks are ignored
363c4f02a89SMax Khon 	 * Note(2003/7): It seems recent Windows has
364c4f02a89SMax Khon 	 *	 defferent rule than this code, that Windows
365c4f02a89SMax Khon 	 *	 ignores all dots before extension, and use all
366c4f02a89SMax Khon 	 * 	 chars as filename except for dots.
367952a6212SJordan K. Hubbard 	 */
368952a6212SJordan K. Hubbard 	dp = dp1 = 0;
369952a6212SJordan K. Hubbard 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
370952a6212SJordan K. Hubbard 		switch (*cp++) {
371952a6212SJordan K. Hubbard 		case '.':
372952a6212SJordan K. Hubbard 			if (!dp1)
373952a6212SJordan K. Hubbard 				dp1 = cp;
374952a6212SJordan K. Hubbard 			break;
375952a6212SJordan K. Hubbard 		case ' ':
376952a6212SJordan K. Hubbard 			break;
377952a6212SJordan K. Hubbard 		default:
378952a6212SJordan K. Hubbard 			if (dp1)
379952a6212SJordan K. Hubbard 				dp = dp1;
380952a6212SJordan K. Hubbard 			dp1 = 0;
381952a6212SJordan K. Hubbard 			break;
382952a6212SJordan K. Hubbard 		}
38327a0bc89SDoug Rabson 	}
38427a0bc89SDoug Rabson 
38527a0bc89SDoug Rabson 	/*
386697ab829SR. Imura 	 * Now convert it (this part is for extension).
387697ab829SR. Imura 	 * As Windows XP do, if it's not ascii char,
388697ab829SR. Imura 	 * this function should return 2 or 3, so that checkng out Unicode name.
38927a0bc89SDoug Rabson 	 */
390952a6212SJordan K. Hubbard 	if (dp) {
391952a6212SJordan K. Hubbard 		if (dp1)
392952a6212SJordan K. Hubbard 			l = dp1 - dp;
393952a6212SJordan K. Hubbard 		else
394952a6212SJordan K. Hubbard 			l = unlen - (dp - un);
395c4f02a89SMax Khon 		for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
396c4f02a89SMax Khon 			c = unix2doschr(&cp, (size_t *)&i, pmp);
397c4f02a89SMax Khon 			if (c & 0xff00) {
398c4f02a89SMax Khon 				dn[j] = c >> 8;
399c4f02a89SMax Khon 				if (++j < 11) {
400c4f02a89SMax Khon 					dn[j] = c;
401697ab829SR. Imura 					if (conv != 3)
402697ab829SR. Imura 						conv = 2;
403c4f02a89SMax Khon 					continue;
404c4f02a89SMax Khon 				} else {
405c4f02a89SMax Khon 					conv = 3;
406c4f02a89SMax Khon 					dn[j-1] = ' ';
407c4f02a89SMax Khon 					break;
408c4f02a89SMax Khon 				}
409c4f02a89SMax Khon 			} else {
410c4f02a89SMax Khon 				dn[j] = c;
411c4f02a89SMax Khon 			}
412697ab829SR. Imura 			if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
413952a6212SJordan K. Hubbard 				conv = 2;
414cce0eefdSMike Smith 			if (dn[j] == 1) {
415cce0eefdSMike Smith 				conv = 3;
416cce0eefdSMike Smith 				dn[j] = '_';
417cce0eefdSMike Smith 			}
418cce0eefdSMike Smith 			if (dn[j] == 2) {
419952a6212SJordan K. Hubbard 				conv = 3;
420952a6212SJordan K. Hubbard 				dn[j--] = ' ';
421952a6212SJordan K. Hubbard 			}
422952a6212SJordan K. Hubbard 		}
423c4f02a89SMax Khon 		if (i > 0)
424952a6212SJordan K. Hubbard 			conv = 3;
425952a6212SJordan K. Hubbard 		dp--;
426952a6212SJordan K. Hubbard 	} else {
427952a6212SJordan K. Hubbard 		for (dp = cp; *--dp == ' ' || *dp == '.';);
428952a6212SJordan K. Hubbard 		dp++;
429952a6212SJordan K. Hubbard 	}
430952a6212SJordan K. Hubbard 
431952a6212SJordan K. Hubbard 	/*
432952a6212SJordan K. Hubbard 	 * Now convert the rest of the name
433952a6212SJordan K. Hubbard 	 */
434c4f02a89SMax Khon 	for (i = dp - un, j = 0; un < dp && j < 8; j++) {
435bddcdc51STim J. Robbins 		c = unix2doschr(&un, &i, pmp);
436c4f02a89SMax Khon 		if (c & 0xff00) {
437c4f02a89SMax Khon 			dn[j] = c >> 8;
438c4f02a89SMax Khon 			if (++j < 8) {
439c4f02a89SMax Khon 				dn[j] = c;
440697ab829SR. Imura 				if (conv != 3)
441697ab829SR. Imura 					conv = 2;
442c4f02a89SMax Khon 				continue;
443c4f02a89SMax Khon 			} else {
444c4f02a89SMax Khon 				conv = 3;
445c4f02a89SMax Khon 				dn[j-1] = ' ';
446c4f02a89SMax Khon 				break;
447c4f02a89SMax Khon 			}
448c4f02a89SMax Khon 		} else {
449c4f02a89SMax Khon 			dn[j] = c;
450c4f02a89SMax Khon 		}
451697ab829SR. Imura 		if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
452952a6212SJordan K. Hubbard 			conv = 2;
453cce0eefdSMike Smith 		if (dn[j] == 1) {
454cce0eefdSMike Smith 			conv = 3;
455cce0eefdSMike Smith 			dn[j] = '_';
456cce0eefdSMike Smith 		}
457cce0eefdSMike Smith 		if (dn[j] == 2) {
458952a6212SJordan K. Hubbard 			conv = 3;
459952a6212SJordan K. Hubbard 			dn[j--] = ' ';
460952a6212SJordan K. Hubbard 		}
461952a6212SJordan K. Hubbard 	}
462952a6212SJordan K. Hubbard 	if (un < dp)
463952a6212SJordan K. Hubbard 		conv = 3;
464952a6212SJordan K. Hubbard 	/*
465952a6212SJordan K. Hubbard 	 * If we didn't have any chars in filename,
466952a6212SJordan K. Hubbard 	 * generate a default
467952a6212SJordan K. Hubbard 	 */
468952a6212SJordan K. Hubbard 	if (!j)
469952a6212SJordan K. Hubbard 		dn[0] = '_';
470952a6212SJordan K. Hubbard 
471952a6212SJordan K. Hubbard 	/*
472952a6212SJordan K. Hubbard 	 * If there wasn't any char dropped,
473952a6212SJordan K. Hubbard 	 * there is no place for generation numbers
47427a0bc89SDoug Rabson 	 */
475952a6212SJordan K. Hubbard 	if (conv != 3) {
476952a6212SJordan K. Hubbard 		if (gen > 1)
477c4f02a89SMax Khon 			conv = 0;
478c4f02a89SMax Khon 		goto done;
479952a6212SJordan K. Hubbard 	}
480952a6212SJordan K. Hubbard 
481952a6212SJordan K. Hubbard 	/*
482952a6212SJordan K. Hubbard 	 * Now insert the generation number into the filename part
483952a6212SJordan K. Hubbard 	 */
484011cdb57SDmitrij Tejblum 	if (gen == 0)
485c4f02a89SMax Khon 		goto done;
486952a6212SJordan K. Hubbard 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
487952a6212SJordan K. Hubbard 		*--wcp = gen % 10 + '0';
488c4f02a89SMax Khon 	if (gen) {
489c4f02a89SMax Khon 		conv = 0;
490c4f02a89SMax Khon 		goto done;
491c4f02a89SMax Khon 	}
492952a6212SJordan K. Hubbard 	for (i = 8; dn[--i] == ' ';);
493952a6212SJordan K. Hubbard 	i++;
494952a6212SJordan K. Hubbard 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
495952a6212SJordan K. Hubbard 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
496c4f02a89SMax Khon 	/*
497c4f02a89SMax Khon 	 * Correct posision to where insert the generation number
498c4f02a89SMax Khon 	 */
499c4f02a89SMax Khon 	cp = dn;
500c4f02a89SMax Khon 	i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
501c4f02a89SMax Khon 
502952a6212SJordan K. Hubbard 	dn[i++] = '~';
503952a6212SJordan K. Hubbard 	while (wcp < gentext + sizeof(gentext))
504952a6212SJordan K. Hubbard 		dn[i++] = *wcp++;
505c4f02a89SMax Khon 
506c4f02a89SMax Khon 	/*
507c4f02a89SMax Khon 	 * Tail of the filename should be space
508c4f02a89SMax Khon 	 */
509c4f02a89SMax Khon 	while (i < 8)
510c4f02a89SMax Khon 		dn[i++] = ' ';
511c4f02a89SMax Khon 	conv = 3;
512c4f02a89SMax Khon 
513c4f02a89SMax Khon done:
514c4f02a89SMax Khon 	/*
515c4f02a89SMax Khon 	 * The first character cannot be E5,
516c4f02a89SMax Khon 	 * because that means a deleted entry
517c4f02a89SMax Khon 	 */
518c4f02a89SMax Khon 	if (dn[0] == 0xe5)
519c4f02a89SMax Khon 		dn[0] = SLOT_E5;
520c4f02a89SMax Khon 
521c4f02a89SMax Khon 	return conv;
522952a6212SJordan K. Hubbard }
523952a6212SJordan K. Hubbard 
524952a6212SJordan K. Hubbard /*
525952a6212SJordan K. Hubbard  * Create a Win95 long name directory entry
526952a6212SJordan K. Hubbard  * Note: assumes that the filename is valid,
527952a6212SJordan K. Hubbard  *	 i.e. doesn't consist solely of blanks and dots
528952a6212SJordan K. Hubbard  */
529952a6212SJordan K. Hubbard int
530c4f02a89SMax Khon unix2winfn(un, unlen, wep, cnt, chksum, pmp)
531952a6212SJordan K. Hubbard 	const u_char *un;
532bddcdc51STim J. Robbins 	size_t unlen;
533952a6212SJordan K. Hubbard 	struct winentry *wep;
534952a6212SJordan K. Hubbard 	int cnt;
535952a6212SJordan K. Hubbard 	int chksum;
536c4f02a89SMax Khon 	struct msdosfsmount *pmp;
537952a6212SJordan K. Hubbard {
538952a6212SJordan K. Hubbard 	u_int8_t *wcp;
539c4f02a89SMax Khon 	int i, end;
54013df76f2SAndrey A. Chernov 	u_int16_t code;
541952a6212SJordan K. Hubbard 
542952a6212SJordan K. Hubbard 	/*
543952a6212SJordan K. Hubbard 	 * Drop trailing blanks and dots
544952a6212SJordan K. Hubbard 	 */
545c4f02a89SMax Khon 	unlen = winLenFixup(un, unlen);
546952a6212SJordan K. Hubbard 
547c4f02a89SMax Khon 	/*
548c4f02a89SMax Khon 	 * Cut *un for this slot
549c4f02a89SMax Khon 	 */
550c4f02a89SMax Khon 	unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
551c4f02a89SMax Khon 			  pmp->pm_flags, pmp->pm_u2w);
552952a6212SJordan K. Hubbard 
553952a6212SJordan K. Hubbard 	/*
554952a6212SJordan K. Hubbard 	 * Initialize winentry to some useful default
555952a6212SJordan K. Hubbard 	 */
556952a6212SJordan K. Hubbard 	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
557952a6212SJordan K. Hubbard 	wep->weCnt = cnt;
558952a6212SJordan K. Hubbard 	wep->weAttributes = ATTR_WIN95;
559952a6212SJordan K. Hubbard 	wep->weReserved1 = 0;
560952a6212SJordan K. Hubbard 	wep->weChksum = chksum;
561952a6212SJordan K. Hubbard 	wep->weReserved2 = 0;
562952a6212SJordan K. Hubbard 
563952a6212SJordan K. Hubbard 	/*
564952a6212SJordan K. Hubbard 	 * Now convert the filename parts
565952a6212SJordan K. Hubbard 	 */
566c4f02a89SMax Khon 	end = 0;
567c4f02a89SMax Khon 	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
568bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
56913df76f2SAndrey A. Chernov 		*wcp++ = code;
57013df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
571c4f02a89SMax Khon 		if (!code)
572c4f02a89SMax Khon 			end = WIN_LAST;
573952a6212SJordan K. Hubbard 	}
574c4f02a89SMax Khon 	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
575bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
57613df76f2SAndrey A. Chernov 		*wcp++ = code;
57713df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
578c4f02a89SMax Khon 		if (!code)
579c4f02a89SMax Khon 			end = WIN_LAST;
580952a6212SJordan K. Hubbard 	}
581c4f02a89SMax Khon 	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
582bddcdc51STim J. Robbins 		code = unix2winchr(&un, &unlen, 0, pmp);
58313df76f2SAndrey A. Chernov 		*wcp++ = code;
58413df76f2SAndrey A. Chernov 		*wcp++ = code >> 8;
585c4f02a89SMax Khon 		if (!code)
586c4f02a89SMax Khon 			end = WIN_LAST;
587952a6212SJordan K. Hubbard 	}
588c4f02a89SMax Khon 	if (*un == '\0')
589c4f02a89SMax Khon 		end = WIN_LAST;
590c4f02a89SMax Khon 	wep->weCnt |= end;
591c4f02a89SMax Khon 	return !end;
5924d63452fSAndrey A. Chernov }
5934d63452fSAndrey A. Chernov 
594952a6212SJordan K. Hubbard /*
595952a6212SJordan K. Hubbard  * Compare our filename to the one in the Win95 entry
596952a6212SJordan K. Hubbard  * Returns the checksum or -1 if no match
597952a6212SJordan K. Hubbard  */
598952a6212SJordan K. Hubbard int
599c2819440SBruce Evans winChkName(nbp, un, unlen, chksum, pmp)
600c2819440SBruce Evans 	struct mbnambuf *nbp;
601952a6212SJordan K. Hubbard 	const u_char *un;
602bddcdc51STim J. Robbins 	size_t unlen;
603952a6212SJordan K. Hubbard 	int chksum;
604c4f02a89SMax Khon 	struct msdosfsmount *pmp;
605952a6212SJordan K. Hubbard {
606bddcdc51STim J. Robbins 	size_t len;
607c4f02a89SMax Khon 	u_int16_t c1, c2;
608c4f02a89SMax Khon 	u_char *np;
609c4f02a89SMax Khon 	struct dirent dirbuf;
610952a6212SJordan K. Hubbard 
611952a6212SJordan K. Hubbard 	/*
612c2819440SBruce Evans 	 * We already have winentry in *nbp.
613952a6212SJordan K. Hubbard 	 */
614c2819440SBruce Evans 	if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0)
615952a6212SJordan K. Hubbard 		return -1;
616952a6212SJordan K. Hubbard 
617c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG
618c4f02a89SMax Khon 	printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
619c4f02a89SMax Khon 							dirbuf.d_name,
620c4f02a89SMax Khon 							dirbuf.d_namlen);
621c4f02a89SMax Khon #endif
622952a6212SJordan K. Hubbard 
623952a6212SJordan K. Hubbard 	/*
624952a6212SJordan K. Hubbard 	 * Compare the name parts
625952a6212SJordan K. Hubbard 	 */
626c4f02a89SMax Khon 	len = dirbuf.d_namlen;
627c4f02a89SMax Khon 	if (unlen != len)
628c4f02a89SMax Khon 		return -2;
629c4f02a89SMax Khon 
630c4f02a89SMax Khon 	for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
631c4f02a89SMax Khon 		/*
6320f4e4130SMax Khon 		 * Comparison must be case insensitive, because FAT disallows
6330f4e4130SMax Khon 		 * to look up or create files in case sensitive even when
6340f4e4130SMax Khon 		 * it's a long file name.
635c4f02a89SMax Khon 		 */
636bddcdc51STim J. Robbins 		c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
637bddcdc51STim J. Robbins 		c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
638b998efa9SAndrey A. Chernov 		if (c1 != c2)
639c4f02a89SMax Khon 			return -2;
640952a6212SJordan K. Hubbard 	}
641952a6212SJordan K. Hubbard 	return chksum;
642952a6212SJordan K. Hubbard }
643952a6212SJordan K. Hubbard 
644952a6212SJordan K. Hubbard /*
645952a6212SJordan K. Hubbard  * Convert Win95 filename to dirbuf.
646952a6212SJordan K. Hubbard  * Returns the checksum or -1 if impossible
647952a6212SJordan K. Hubbard  */
648952a6212SJordan K. Hubbard int
649c2819440SBruce Evans win2unixfn(nbp, wep, chksum, pmp)
650c2819440SBruce Evans 	struct mbnambuf *nbp;
651952a6212SJordan K. Hubbard 	struct winentry *wep;
652952a6212SJordan K. Hubbard 	int chksum;
653c4f02a89SMax Khon 	struct msdosfsmount *pmp;
654952a6212SJordan K. Hubbard {
655952a6212SJordan K. Hubbard 	u_int8_t *cp;
656c4f02a89SMax Khon 	u_int8_t *np, name[WIN_CHARS * 2 + 1];
6572b5b6623SAndrey A. Chernov 	u_int16_t code;
658952a6212SJordan K. Hubbard 	int i;
659952a6212SJordan K. Hubbard 
660952a6212SJordan K. Hubbard 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
661952a6212SJordan K. Hubbard 	    || !(wep->weCnt&WIN_CNT))
662952a6212SJordan K. Hubbard 		return -1;
663952a6212SJordan K. Hubbard 
664952a6212SJordan K. Hubbard 	/*
665952a6212SJordan K. Hubbard 	 * First compare checksums
666952a6212SJordan K. Hubbard 	 */
667952a6212SJordan K. Hubbard 	if (wep->weCnt&WIN_LAST) {
668952a6212SJordan K. Hubbard 		chksum = wep->weChksum;
669952a6212SJordan K. Hubbard 	} else if (chksum != wep->weChksum)
670952a6212SJordan K. Hubbard 		chksum = -1;
671952a6212SJordan K. Hubbard 	if (chksum == -1)
672952a6212SJordan K. Hubbard 		return -1;
673952a6212SJordan K. Hubbard 
674952a6212SJordan K. Hubbard 	/*
675952a6212SJordan K. Hubbard 	 * Convert the name parts
676952a6212SJordan K. Hubbard 	 */
677c4f02a89SMax Khon 	np = name;
678952a6212SJordan K. Hubbard 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
6792b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
6802b5b6623SAndrey A. Chernov 		switch (code) {
681952a6212SJordan K. Hubbard 		case 0:
6822b5b6623SAndrey A. Chernov 			*np = '\0';
683c2819440SBruce Evans 			mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
684952a6212SJordan K. Hubbard 			return chksum;
685952a6212SJordan K. Hubbard 		case '/':
6862b5b6623SAndrey A. Chernov 			*np = '\0';
687952a6212SJordan K. Hubbard 			return -1;
6882b5b6623SAndrey A. Chernov 		default:
689c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
690c4f02a89SMax Khon 			if (code & 0xff00)
691c4f02a89SMax Khon 				*np++ = code >> 8;
6922b5b6623SAndrey A. Chernov 			*np++ = code;
6932b5b6623SAndrey A. Chernov 			break;
694952a6212SJordan K. Hubbard 		}
6952b5b6623SAndrey A. Chernov 		cp += 2;
696952a6212SJordan K. Hubbard 	}
697952a6212SJordan K. Hubbard 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
6982b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
6992b5b6623SAndrey A. Chernov 		switch (code) {
700952a6212SJordan K. Hubbard 		case 0:
7012b5b6623SAndrey A. Chernov 			*np = '\0';
702c2819440SBruce Evans 			mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
703952a6212SJordan K. Hubbard 			return chksum;
704952a6212SJordan K. Hubbard 		case '/':
7052b5b6623SAndrey A. Chernov 			*np = '\0';
706952a6212SJordan K. Hubbard 			return -1;
7072b5b6623SAndrey A. Chernov 		default:
708c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
709c4f02a89SMax Khon 			if (code & 0xff00)
710c4f02a89SMax Khon 				*np++ = code >> 8;
7112b5b6623SAndrey A. Chernov 			*np++ = code;
7122b5b6623SAndrey A. Chernov 			break;
713952a6212SJordan K. Hubbard 		}
7142b5b6623SAndrey A. Chernov 		cp += 2;
715952a6212SJordan K. Hubbard 	}
716952a6212SJordan K. Hubbard 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
7172b5b6623SAndrey A. Chernov 		code = (cp[1] << 8) | cp[0];
7182b5b6623SAndrey A. Chernov 		switch (code) {
719952a6212SJordan K. Hubbard 		case 0:
7202b5b6623SAndrey A. Chernov 			*np = '\0';
721c2819440SBruce Evans 			mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
722952a6212SJordan K. Hubbard 			return chksum;
723952a6212SJordan K. Hubbard 		case '/':
7242b5b6623SAndrey A. Chernov 			*np = '\0';
725952a6212SJordan K. Hubbard 			return -1;
7262b5b6623SAndrey A. Chernov 		default:
727c4f02a89SMax Khon 			code = win2unixchr(code, pmp);
728c4f02a89SMax Khon 			if (code & 0xff00)
729c4f02a89SMax Khon 				*np++ = code >> 8;
7302b5b6623SAndrey A. Chernov 			*np++ = code;
7312b5b6623SAndrey A. Chernov 			break;
732952a6212SJordan K. Hubbard 		}
7332b5b6623SAndrey A. Chernov 		cp += 2;
734952a6212SJordan K. Hubbard 	}
735c4f02a89SMax Khon 	*np = '\0';
736c2819440SBruce Evans 	mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1);
737952a6212SJordan K. Hubbard 	return chksum;
738952a6212SJordan K. Hubbard }
739952a6212SJordan K. Hubbard 
740952a6212SJordan K. Hubbard /*
7412a05fbb9SNate Lawson  * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
742952a6212SJordan K. Hubbard  */
743952a6212SJordan K. Hubbard u_int8_t
744710a9accSMax Khon winChksum(struct direntry *dep)
745952a6212SJordan K. Hubbard {
746952a6212SJordan K. Hubbard 	u_int8_t s;
747952a6212SJordan K. Hubbard 
748710a9accSMax Khon 	s = dep->deName[0];
749710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[1];
750710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[2];
751710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[3];
752710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[4];
753710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[5];
754710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[6];
755710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deName[7];
756710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deExtension[0];
757710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deExtension[1];
758710a9accSMax Khon 	s = ((s << 7) | (s >> 1)) + dep->deExtension[2];
7592a05fbb9SNate Lawson 
7602a05fbb9SNate Lawson 	return (s);
761952a6212SJordan K. Hubbard }
762952a6212SJordan K. Hubbard 
763952a6212SJordan K. Hubbard /*
764952a6212SJordan K. Hubbard  * Determine the number of slots necessary for Win95 names
765952a6212SJordan K. Hubbard  */
766952a6212SJordan K. Hubbard int
767c4f02a89SMax Khon winSlotCnt(un, unlen, pmp)
768952a6212SJordan K. Hubbard 	const u_char *un;
769bddcdc51STim J. Robbins 	size_t unlen;
770c4f02a89SMax Khon 	struct msdosfsmount *pmp;
771952a6212SJordan K. Hubbard {
772c4f02a89SMax Khon 	size_t wlen;
773c4f02a89SMax Khon 	char wn[WIN_MAXLEN * 2 + 1], *wnp;
774c4f02a89SMax Khon 
77569a36f24SMike Smith 	unlen = winLenFixup(un, unlen);
776c4f02a89SMax Khon 
777c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
778c4f02a89SMax Khon 		wlen = WIN_MAXLEN * 2;
779c4f02a89SMax Khon 		wnp = wn;
780bddcdc51STim J. Robbins 		msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
781c4f02a89SMax Khon 		if (unlen > 0)
782c4f02a89SMax Khon 			return 0;
783c4f02a89SMax Khon 		return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
784c4f02a89SMax Khon 	}
785c4f02a89SMax Khon 
786952a6212SJordan K. Hubbard 	if (unlen > WIN_MAXLEN)
787952a6212SJordan K. Hubbard 		return 0;
788952a6212SJordan K. Hubbard 	return howmany(unlen, WIN_CHARS);
78927a0bc89SDoug Rabson }
79069a36f24SMike Smith 
79169a36f24SMike Smith /*
79269a36f24SMike Smith  * Determine the number of bytes neccesary for Win95 names
79369a36f24SMike Smith  */
794bddcdc51STim J. Robbins size_t
79569a36f24SMike Smith winLenFixup(un, unlen)
79669a36f24SMike Smith 	const u_char* un;
797bddcdc51STim J. Robbins 	size_t unlen;
79869a36f24SMike Smith {
79969a36f24SMike Smith 	for (un += unlen; unlen > 0; unlen--)
80069a36f24SMike Smith 		if (*--un != ' ' && *un != '.')
80169a36f24SMike Smith 			break;
80269a36f24SMike Smith 	return unlen;
80369a36f24SMike Smith }
804c4f02a89SMax Khon 
805c4f02a89SMax Khon /*
806c4f02a89SMax Khon  * Store an area with multi byte string instr, and reterns left
807c4f02a89SMax Khon  * byte of instr and moves pointer forward. The area's size is
808c4f02a89SMax Khon  * inlen or outlen.
809c4f02a89SMax Khon  */
810c4f02a89SMax Khon static int
811bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
812c4f02a89SMax Khon {
813c4f02a89SMax Khon 	char *outp, outstr[outlen * weight + 1];
814c4f02a89SMax Khon 
815c4f02a89SMax Khon 	if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
816c4f02a89SMax Khon 		outp = outstr;
817c4f02a89SMax Khon 		outlen *= weight;
818bddcdc51STim J. Robbins 		msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
819c4f02a89SMax Khon 		return (inlen);
820c4f02a89SMax Khon 	}
821c4f02a89SMax Khon 
822c4f02a89SMax Khon 	(*instr) += min(inlen, outlen);
823c4f02a89SMax Khon 	return (inlen - min(inlen, outlen));
824c4f02a89SMax Khon }
825c4f02a89SMax Khon 
826c4f02a89SMax Khon /*
827c4f02a89SMax Khon  * Convert DOS char to Local char
828c4f02a89SMax Khon  */
829c4f02a89SMax Khon static u_int16_t
830c4f02a89SMax Khon dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
831c4f02a89SMax Khon {
832c4f02a89SMax Khon 	u_char c;
833c4f02a89SMax Khon 	char *outp, outbuf[3];
834c4f02a89SMax Khon 	u_int16_t wc;
835c4f02a89SMax Khon 	size_t len, olen;
836c4f02a89SMax Khon 
837c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
838c4f02a89SMax Khon 		olen = len = 2;
839c4f02a89SMax Khon 		outp = outbuf;
840c4f02a89SMax Khon 
841c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
842c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
843c4f02a89SMax Khon 						  ilen, &outp, &olen, KICONV_LOWER);
844c4f02a89SMax Khon 		else
845c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
846c4f02a89SMax Khon 					     ilen, &outp, &olen);
847c4f02a89SMax Khon 		len -= olen;
848c4f02a89SMax Khon 
849c4f02a89SMax Khon 		/*
850c4f02a89SMax Khon 		 * return '?' if failed to convert
851c4f02a89SMax Khon 		 */
852c4f02a89SMax Khon 		if (len == 0) {
853c4f02a89SMax Khon 			(*ilen)--;
854c4f02a89SMax Khon 			(*instr)++;
855c4f02a89SMax Khon 			return ('?');
856c4f02a89SMax Khon 		}
857c4f02a89SMax Khon 
858c4f02a89SMax Khon 		wc = 0;
859c4f02a89SMax Khon 		while(len--)
860c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
861c4f02a89SMax Khon 		return (wc);
862c4f02a89SMax Khon 	}
863c4f02a89SMax Khon 
864c4f02a89SMax Khon 	(*ilen)--;
865c4f02a89SMax Khon 	c = *(*instr)++;
866c4f02a89SMax Khon 	c = dos2unix[c];
867c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
868c4f02a89SMax Khon 		c = u2l[c];
869c4f02a89SMax Khon 	return ((u_int16_t)c);
870c4f02a89SMax Khon }
871c4f02a89SMax Khon 
872c4f02a89SMax Khon /*
873c4f02a89SMax Khon  * Convert Local char to DOS char
874c4f02a89SMax Khon  */
875c4f02a89SMax Khon static u_int16_t
876c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
877c4f02a89SMax Khon {
878c4f02a89SMax Khon 	u_char c;
879c4f02a89SMax Khon 	char *up, *outp, unicode[3], outbuf[3];
880c4f02a89SMax Khon 	u_int16_t wc;
881697ab829SR. Imura 	size_t len, ucslen, unixlen, olen;
882c4f02a89SMax Khon 
883c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
884c4f02a89SMax Khon 		/*
885c4f02a89SMax Khon 		 * to hide an invisible character, using a unicode filter
886c4f02a89SMax Khon 		 */
887697ab829SR. Imura 		ucslen = 2;
888c4f02a89SMax Khon 		len = *ilen;
889c4f02a89SMax Khon 		up = unicode;
890c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
891697ab829SR. Imura 				     ilen, &up, &ucslen);
892697ab829SR. Imura 		unixlen = len - *ilen;
893c4f02a89SMax Khon 
894c4f02a89SMax Khon 		/*
895c4f02a89SMax Khon 		 * cannot be converted
896c4f02a89SMax Khon 		 */
897697ab829SR. Imura 		if (unixlen == 0) {
898c4f02a89SMax Khon 			(*ilen)--;
899c4f02a89SMax Khon 			(*instr)++;
900c4f02a89SMax Khon 			return (0);
901c4f02a89SMax Khon 		}
902c4f02a89SMax Khon 
903c4f02a89SMax Khon 		/*
904c4f02a89SMax Khon 		 * return magic number for ascii char
905c4f02a89SMax Khon 		 */
906697ab829SR. Imura 		if (unixlen == 1) {
907c4f02a89SMax Khon 			c = *(*instr -1);
908c4f02a89SMax Khon 			if (! (c & 0x80)) {
909c4f02a89SMax Khon 				c = unix2dos[c];
910c4f02a89SMax Khon 				if (c <= 2)
911c4f02a89SMax Khon 					return (c);
912c4f02a89SMax Khon 			}
913c4f02a89SMax Khon 		}
914c4f02a89SMax Khon 
915c4f02a89SMax Khon 		/*
916c4f02a89SMax Khon 		 * now convert using libiconv
917c4f02a89SMax Khon 		 */
918697ab829SR. Imura 		*instr -= unixlen;
919697ab829SR. Imura 		*ilen = len;
920c4f02a89SMax Khon 
921c4f02a89SMax Khon 		olen = len = 2;
922c4f02a89SMax Khon 		outp = outbuf;
923c4f02a89SMax Khon 		msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
924c4f02a89SMax Khon 					  ilen, &outp, &olen, KICONV_FROM_UPPER);
925c4f02a89SMax Khon 		len -= olen;
926697ab829SR. Imura 
927697ab829SR. Imura 		/*
928697ab829SR. Imura 		 * cannot be converted, but has unicode char, should return magic number
929697ab829SR. Imura 		 */
930697ab829SR. Imura 		if (len == 0) {
931697ab829SR. Imura 			(*ilen) -= unixlen;
932697ab829SR. Imura 			(*instr) += unixlen;
933697ab829SR. Imura 			return (1);
934697ab829SR. Imura 		}
935697ab829SR. Imura 
936c4f02a89SMax Khon 		wc = 0;
937c4f02a89SMax Khon 		while(len--)
938c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
939c4f02a89SMax Khon 		return (wc);
940c4f02a89SMax Khon 	}
941c4f02a89SMax Khon 
942c4f02a89SMax Khon 	(*ilen)--;
943c4f02a89SMax Khon 	c = *(*instr)++;
944c4f02a89SMax Khon 	c = l2u[c];
945c4f02a89SMax Khon 	c = unix2dos[c];
946c4f02a89SMax Khon 	return ((u_int16_t)c);
947c4f02a89SMax Khon }
948c4f02a89SMax Khon 
949c4f02a89SMax Khon /*
950c4f02a89SMax Khon  * Convert Windows char to Local char
951c4f02a89SMax Khon  */
952c4f02a89SMax Khon static u_int16_t
953c4f02a89SMax Khon win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
954c4f02a89SMax Khon {
955c4f02a89SMax Khon 	u_char *inp, *outp, inbuf[3], outbuf[3];
956c4f02a89SMax Khon 	size_t ilen, olen, len;
957c4f02a89SMax Khon 
958c4f02a89SMax Khon 	if (wc == 0)
959c4f02a89SMax Khon 		return (0);
960c4f02a89SMax Khon 
961c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
962c4f02a89SMax Khon 		inbuf[0] = (u_char)(wc>>8);
963c4f02a89SMax Khon 		inbuf[1] = (u_char)wc;
964c4f02a89SMax Khon 		inbuf[2] = '\0';
965c4f02a89SMax Khon 
966c4f02a89SMax Khon 		ilen = olen = len = 2;
967c4f02a89SMax Khon 		inp = inbuf;
968c4f02a89SMax Khon 		outp = outbuf;
969c4f02a89SMax Khon 		msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
970c4f02a89SMax Khon 				     (char **)&outp, &olen);
971c4f02a89SMax Khon 		len -= olen;
972c4f02a89SMax Khon 
973c4f02a89SMax Khon 		/*
974c4f02a89SMax Khon 		 * return '?' if failed to convert
975c4f02a89SMax Khon 		 */
976c4f02a89SMax Khon 		if (len == 0) {
977c4f02a89SMax Khon 			wc = '?';
978c4f02a89SMax Khon 			return (wc);
979c4f02a89SMax Khon 		}
980c4f02a89SMax Khon 
981c4f02a89SMax Khon 		wc = 0;
982c4f02a89SMax Khon 		while(len--)
983c4f02a89SMax Khon 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
984c4f02a89SMax Khon 		return (wc);
985c4f02a89SMax Khon 	}
986c4f02a89SMax Khon 
987c4f02a89SMax Khon 	if (wc & 0xff00)
988c4f02a89SMax Khon 		wc = '?';
989c4f02a89SMax Khon 
990c4f02a89SMax Khon 	return (wc);
991c4f02a89SMax Khon }
992c4f02a89SMax Khon 
993c4f02a89SMax Khon /*
994c4f02a89SMax Khon  * Convert Local char to Windows char
995c4f02a89SMax Khon  */
996c4f02a89SMax Khon static u_int16_t
997c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
998c4f02a89SMax Khon {
999c4f02a89SMax Khon 	u_char *outp, outbuf[3];
1000c4f02a89SMax Khon 	u_int16_t wc;
1001c4f02a89SMax Khon 	size_t olen;
1002c4f02a89SMax Khon 
1003c4f02a89SMax Khon 	if (*ilen == 0)
1004c4f02a89SMax Khon 		return (0);
1005c4f02a89SMax Khon 
1006c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1007c4f02a89SMax Khon 		outp = outbuf;
1008c4f02a89SMax Khon 		olen = 2;
1009c4f02a89SMax Khon 		if (lower & (LCASE_BASE | LCASE_EXT))
1010c4f02a89SMax Khon 			msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
1011c4f02a89SMax Khon 						  ilen, (char **)&outp, &olen,
1012c4f02a89SMax Khon 						  KICONV_FROM_LOWER);
1013c4f02a89SMax Khon 		else
1014c4f02a89SMax Khon 			msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1015c4f02a89SMax Khon 					     ilen, (char **)&outp, &olen);
1016c4f02a89SMax Khon 
1017c4f02a89SMax Khon 		/*
1018c4f02a89SMax Khon 		 * return '0' if end of filename
1019c4f02a89SMax Khon 		 */
1020c4f02a89SMax Khon 		if (olen == 2)
1021c4f02a89SMax Khon 			return (0);
1022c4f02a89SMax Khon 
1023c4f02a89SMax Khon 		wc = (outbuf[0]<<8) | outbuf[1];
1024c4f02a89SMax Khon 
1025c4f02a89SMax Khon 		return (wc);
1026c4f02a89SMax Khon 	}
1027c4f02a89SMax Khon 
1028c4f02a89SMax Khon 	(*ilen)--;
1029c4f02a89SMax Khon 	wc = (*instr)[0];
1030c4f02a89SMax Khon 	if (lower & (LCASE_BASE | LCASE_EXT))
1031c4f02a89SMax Khon 		wc = u2l[wc];
1032c4f02a89SMax Khon 	(*instr)++;
1033c4f02a89SMax Khon 	return (wc);
1034c4f02a89SMax Khon }
1035c4f02a89SMax Khon 
1036c4f02a89SMax Khon /*
1037c2819440SBruce Evans  * Initialize the temporary concatenation buffer.
1038c4f02a89SMax Khon  */
1039c4f02a89SMax Khon void
1040c2819440SBruce Evans mbnambuf_init(struct mbnambuf *nbp)
1041c4f02a89SMax Khon {
1042c4f02a89SMax Khon 
1043c2819440SBruce Evans 	nbp->nb_len = 0;
1044c2819440SBruce Evans 	nbp->nb_last_id = -1;
1045c2819440SBruce Evans 	nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0';
1046c4f02a89SMax Khon }
1047c4f02a89SMax Khon 
1048c4f02a89SMax Khon /*
104958ad326bSNate Lawson  * Fill out our concatenation buffer with the given substring, at the offset
105058ad326bSNate Lawson  * specified by its id.  Since this function must be called with ids in
105158ad326bSNate Lawson  * descending order, we take advantage of the fact that ASCII substrings are
105258ad326bSNate Lawson  * exactly WIN_CHARS in length.  For non-ASCII substrings, we shift all
105358ad326bSNate Lawson  * previous (i.e. higher id) substrings upwards to make room for this one.
105458ad326bSNate Lawson  * This only penalizes portions of substrings that contain more than
105558ad326bSNate Lawson  * WIN_CHARS bytes when they are first encountered.
1056c4f02a89SMax Khon  */
1057c4f02a89SMax Khon void
1058c2819440SBruce Evans mbnambuf_write(struct mbnambuf *nbp, char *name, int id)
1059c4f02a89SMax Khon {
106058ad326bSNate Lawson 	char *slot;
1061c2819440SBruce Evans 	size_t count, newlen;
10624cdb1483SNate Lawson 
1063abb0cbf9SEdward Tomasz Napierala 	if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) {
1064abb0cbf9SEdward Tomasz Napierala 		printf("msdosfs: non-decreasing id: id %d, last id %d\n",
1065abb0cbf9SEdward Tomasz Napierala 		    id, nbp->nb_last_id);
1066abb0cbf9SEdward Tomasz Napierala 		return;
1067abb0cbf9SEdward Tomasz Napierala 	}
106858ad326bSNate Lawson 
1069c2819440SBruce Evans 	/* Will store this substring in a WIN_CHARS-aligned slot. */
1070c2819440SBruce Evans 	slot = &nbp->nb_buf[id * WIN_CHARS];
10714cdb1483SNate Lawson 	count = strlen(name);
1072c2819440SBruce Evans 	newlen = nbp->nb_len + count;
1073c2819440SBruce Evans 	if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) {
1074c2819440SBruce Evans 		printf("msdosfs: file name length %zu too large\n", newlen);
10754cdb1483SNate Lawson 		return;
1076c4f02a89SMax Khon 	}
107758ad326bSNate Lawson 
107858ad326bSNate Lawson 	/* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
1079c2819440SBruce Evans 	if (count > WIN_CHARS && nbp->nb_len != 0)
1080c2819440SBruce Evans 		bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len);
108158ad326bSNate Lawson 
108258ad326bSNate Lawson 	/* Copy in the substring to its slot and update length so far. */
108358ad326bSNate Lawson 	bcopy(name, slot, count);
1084c2819440SBruce Evans 	nbp->nb_len = newlen;
1085c2819440SBruce Evans 	nbp->nb_last_id = id;
1086c4f02a89SMax Khon }
1087c4f02a89SMax Khon 
1088c4f02a89SMax Khon /*
10894cdb1483SNate Lawson  * Take the completed string and use it to setup the struct dirent.
10904cdb1483SNate Lawson  * Be sure to always nul-terminate the d_name and then copy the string
10914cdb1483SNate Lawson  * from our buffer.  Note that this function assumes the full string has
10924cdb1483SNate Lawson  * been reassembled in the buffer.  If it's called before all substrings
10934cdb1483SNate Lawson  * have been written via mbnambuf_write(), the result will be incorrect.
1094c4f02a89SMax Khon  */
1095c4f02a89SMax Khon char *
1096c2819440SBruce Evans mbnambuf_flush(struct mbnambuf *nbp, struct dirent *dp)
1097c4f02a89SMax Khon {
1098c4f02a89SMax Khon 
1099c2819440SBruce Evans 	if (nbp->nb_len > sizeof(dp->d_name) - 1) {
1100c2819440SBruce Evans 		mbnambuf_init(nbp);
1101c4f02a89SMax Khon 		return (NULL);
1102c4f02a89SMax Khon 	}
1103c2819440SBruce Evans 	bcopy(&nbp->nb_buf[0], dp->d_name, nbp->nb_len);
1104c2819440SBruce Evans 	dp->d_name[nbp->nb_len] = '\0';
1105c2819440SBruce Evans 	dp->d_namlen = nbp->nb_len;
11064cdb1483SNate Lawson 
1107c2819440SBruce Evans 	mbnambuf_init(nbp);
1108c4f02a89SMax Khon 	return (dp->d_name);
1109c4f02a89SMax Khon }
1110