]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/msdosfs/msdosfs_conv.c
Fix some style bugs (some whitespace errors only).
[FreeBSD/FreeBSD.git] / sys / fs / msdosfs / msdosfs_conv.c
1 /* $FreeBSD$ */
2 /*      $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $     */
3
4 /*-
5  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*-
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/dirent.h>
54 #include <sys/iconv.h>
55 #include <sys/malloc.h>
56 #include <sys/mount.h>
57
58 #include <fs/msdosfs/bpb.h>
59 #include <fs/msdosfs/direntry.h>
60 #include <fs/msdosfs/msdosfsmount.h>
61
62 extern struct iconv_functions *msdosfs_iconv;
63
64 static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle);
65 static u_int16_t dos2unixchr(const u_char **, size_t *, int, struct msdosfsmount *);
66 static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
67 static u_int16_t win2unixchr(u_int16_t, struct msdosfsmount *);
68 static u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *);
69
70 static char     *nambuf_ptr;
71 static size_t   nambuf_len;
72 static int      nambuf_last_id;
73
74 /*
75  * 0 - character disallowed in long file name.
76  * 1 - character should be replaced by '_' in DOS file name, 
77  *     and generation number inserted.
78  * 2 - character ('.' and ' ') should be skipped in DOS file name,
79  *     and generation number inserted.
80  */
81 static u_char
82 unix2dos[256] = {
83 /* iso8859-1 -> cp850 */
84         0,    0,    0,    0,    0,    0,    0,    0,    /* 00-07 */
85         0,    0,    0,    0,    0,    0,    0,    0,    /* 08-0f */
86         0,    0,    0,    0,    0,    0,    0,    0,    /* 10-17 */
87         0,    0,    0,    0,    0,    0,    0,    0,    /* 18-1f */
88         2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
89         0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,    /* 28-2f */
90         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
91         0x38, 0x39, 0,    1,    0,    1,    0,    0,    /* 38-3f */
92         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
93         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
94         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
95         0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f, /* 58-5f */
96         0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */
97         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */
98         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */
99         0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,    /* 78-7f */
100         0,    0,    0,    0,    0,    0,    0,    0,    /* 80-87 */
101         0,    0,    0,    0,    0,    0,    0,    0,    /* 88-8f */
102         0,    0,    0,    0,    0,    0,    0,    0,    /* 90-97 */
103         0,    0,    0,    0,    0,    0,    0,    0,    /* 98-9f */
104         0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
105         0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
106         0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
107         0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
108         0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
109         0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
110         0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
111         0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
112         0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
113         0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
114         0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
115         0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
116 };
117
118 static u_char
119 dos2unix[256] = {
120 /* cp850 -> iso8859-1 */
121         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
122         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
123         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
124         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
125         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
126         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
127         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
128         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
129         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
130         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
131         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
132         0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
133         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
134         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
135         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
136         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
137         0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
138         0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
139         0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
140         0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */
141         0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
142         0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
143         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */
144         0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */
145         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */
146         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */
147         0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */
148         0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */
149         0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
150         0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
151         0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
152         0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */
153 };
154
155 static u_char
156 u2l[256] = {
157 /* tolower */
158         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
159         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
160         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
161         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
162         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
163         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
164         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
165         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
166         0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
167         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
168         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
169         0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
170         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
171         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
172         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
173         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
174         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
175         0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
176         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
177         0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
178         0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
179         0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
180         0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
181         0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
182         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
183         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
184         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
185         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
186         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
187         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
188         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
189         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
190 };
191
192 static u_char
193 l2u[256] = {
194 /* toupper */
195         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
196         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
197         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
198         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
199         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
200         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
201         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
202         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
203         0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
204         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
205         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
206         0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
207         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
208         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
209         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
210         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
211         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
212         0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
213         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
214         0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
215         0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
216         0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
217         0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
218         0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
219         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
220         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
221         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
222         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
223         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
224         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
225         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
226         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
227 };
228
229 /*
230  * DOS filenames are made of 2 parts, the name part and the extension part.
231  * The name part is 8 characters long and the extension part is 3
232  * characters long.  They may contain trailing blanks if the name or
233  * extension are not long enough to fill their respective fields.
234  */
235
236 /*
237  * Convert a DOS filename to a unix filename. And, return the number of
238  * characters in the resulting unix filename excluding the terminating
239  * null.
240  */
241 int
242 dos2unixfn(dn, un, lower, pmp)
243         u_char dn[11];
244         u_char *un;
245         int lower;
246         struct msdosfsmount *pmp;
247 {
248         size_t i;
249         int thislong = 0;
250         u_int16_t c;
251
252         /*
253          * If first char of the filename is SLOT_E5 (0x05), then the real
254          * first char of the filename should be 0xe5. But, they couldn't
255          * just have a 0xe5 mean 0xe5 because that is used to mean a freed
256          * directory slot. Another dos quirk.
257          */
258         if (*dn == SLOT_E5)
259                 *dn = 0xe5;
260
261         /*
262          * Copy the name portion into the unix filename string.
263          */
264         for (i = 8; i > 0 && *dn != ' ';) {
265                 c = dos2unixchr((const u_char **)&dn, &i, lower & LCASE_BASE,
266                     pmp);
267                 if (c & 0xff00) {
268                         *un++ = c >> 8;
269                         thislong++;
270                 }
271                 *un++ = c;
272                 thislong++;
273         }
274         dn += i;
275
276         /*
277          * Now, if there is an extension then put in a period and copy in
278          * the extension.
279          */
280         if (*dn != ' ') {
281                 *un++ = '.';
282                 thislong++;
283                 for (i = 3; i > 0 && *dn != ' ';) {
284                         c = dos2unixchr((const u_char **)&dn, &i,
285                             lower & LCASE_EXT, pmp);
286                         if (c & 0xff00) {
287                                 *un++ = c >> 8;
288                                 thislong++;
289                         }
290                         *un++ = c;
291                         thislong++;
292                 }
293         }
294         *un++ = 0;
295
296         return (thislong);
297 }
298
299 /*
300  * Convert a unix filename to a DOS filename according to Win95 rules.
301  * If applicable and gen is not 0, it is inserted into the converted
302  * filename as a generation number.
303  * Returns
304  *      0 if name couldn't be converted
305  *      1 if the converted name is the same as the original
306  *        (no long filename entry necessary for Win95)
307  *      2 if conversion was successful
308  *      3 if conversion was successful and generation number was inserted
309  */
310 int
311 unix2dosfn(un, dn, unlen, gen, pmp)
312         const u_char *un;
313         u_char dn[12];
314         size_t unlen;
315         u_int gen;
316         struct msdosfsmount *pmp;
317 {
318         ssize_t i, j;
319         int l;
320         int conv = 1;
321         const u_char *cp, *dp, *dp1;
322         u_char gentext[6], *wcp;
323         u_int16_t c;
324
325         /*
326          * Fill the dos filename string with blanks. These are DOS's pad
327          * characters.
328          */
329         for (i = 0; i < 11; i++)
330                 dn[i] = ' ';
331         dn[11] = 0;
332
333         /*
334          * The filenames "." and ".." are handled specially, since they
335          * don't follow dos filename rules.
336          */
337         if (un[0] == '.' && unlen == 1) {
338                 dn[0] = '.';
339                 return gen <= 1;
340         }
341         if (un[0] == '.' && un[1] == '.' && unlen == 2) {
342                 dn[0] = '.';
343                 dn[1] = '.';
344                 return gen <= 1;
345         }
346
347         /*
348          * Filenames with only blanks and dots are not allowed!
349          */
350         for (cp = un, i = unlen; --i >= 0; cp++)
351                 if (*cp != ' ' && *cp != '.')
352                         break;
353         if (i < 0)
354                 return 0;
355
356
357         /*
358          * Filenames with some characters are not allowed!
359          */
360         for (cp = un, i = unlen; i > 0;)
361                 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
362                         return 0;
363
364         /*
365          * Now find the extension
366          * Note: dot as first char doesn't start extension
367          *       and trailing dots and blanks are ignored
368          * Note(2003/7): It seems recent Windows has
369          *       defferent rule than this code, that Windows
370          *       ignores all dots before extension, and use all
371          *       chars as filename except for dots.
372          */
373         dp = dp1 = 0;
374         for (cp = un + 1, i = unlen - 1; --i >= 0;) {
375                 switch (*cp++) {
376                 case '.':
377                         if (!dp1)
378                                 dp1 = cp;
379                         break;
380                 case ' ':
381                         break;
382                 default:
383                         if (dp1)
384                                 dp = dp1;
385                         dp1 = 0;
386                         break;
387                 }
388         }
389
390         /*
391          * Now convert it (this part is for extension).
392          * As Windows XP do, if it's not ascii char,
393          * this function should return 2 or 3, so that checkng out Unicode name.
394          */
395         if (dp) {
396                 if (dp1)
397                         l = dp1 - dp;
398                 else
399                         l = unlen - (dp - un);
400                 for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
401                         c = unix2doschr(&cp, (size_t *)&i, pmp);
402                         if (c & 0xff00) {
403                                 dn[j] = c >> 8;
404                                 if (++j < 11) {
405                                         dn[j] = c;
406                                         if (conv != 3)
407                                                 conv = 2;
408                                         continue;
409                                 } else {
410                                         conv = 3;
411                                         dn[j-1] = ' ';
412                                         break;
413                                 }
414                         } else {
415                                 dn[j] = c;
416                         }
417                         if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
418                                 conv = 2;
419                         if (dn[j] == 1) {
420                                 conv = 3;
421                                 dn[j] = '_';
422                         }
423                         if (dn[j] == 2) {
424                                 conv = 3;
425                                 dn[j--] = ' ';
426                         }
427                 }
428                 if (i > 0)
429                         conv = 3;
430                 dp--;
431         } else {
432                 for (dp = cp; *--dp == ' ' || *dp == '.';);
433                 dp++;
434         }
435
436         /*
437          * Now convert the rest of the name
438          */
439         for (i = dp - un, j = 0; un < dp && j < 8; j++) {
440                 c = unix2doschr(&un, &i, pmp);
441                 if (c & 0xff00) {
442                         dn[j] = c >> 8;
443                         if (++j < 8) {
444                                 dn[j] = c;
445                                 if (conv != 3)
446                                         conv = 2;
447                                 continue;
448                         } else {
449                                 conv = 3;
450                                 dn[j-1] = ' ';
451                                 break;
452                         }
453                 } else {
454                         dn[j] = c;
455                 }
456                 if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
457                         conv = 2;
458                 if (dn[j] == 1) {
459                         conv = 3;
460                         dn[j] = '_';
461                 }
462                 if (dn[j] == 2) {
463                         conv = 3;
464                         dn[j--] = ' ';
465                 }
466         }
467         if (un < dp)
468                 conv = 3;
469         /*
470          * If we didn't have any chars in filename,
471          * generate a default
472          */
473         if (!j)
474                 dn[0] = '_';
475
476         /*
477          * If there wasn't any char dropped,
478          * there is no place for generation numbers
479          */
480         if (conv != 3) {
481                 if (gen > 1)
482                         conv = 0;
483                 goto done;
484         }
485
486         /*
487          * Now insert the generation number into the filename part
488          */
489         if (gen == 0)
490                 goto done;
491         for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
492                 *--wcp = gen % 10 + '0';
493         if (gen) {
494                 conv = 0;
495                 goto done;
496         }
497         for (i = 8; dn[--i] == ' ';);
498         i++;
499         if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
500                 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
501         /*
502          * Correct posision to where insert the generation number
503          */
504         cp = dn;
505         i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
506
507         dn[i++] = '~';
508         while (wcp < gentext + sizeof(gentext))
509                 dn[i++] = *wcp++;
510
511         /*
512          * Tail of the filename should be space
513          */
514         while (i < 8)
515                 dn[i++] = ' ';
516         conv = 3;
517
518 done:
519         /*
520          * The first character cannot be E5,
521          * because that means a deleted entry
522          */
523         if (dn[0] == 0xe5)
524                 dn[0] = SLOT_E5;
525
526         return conv;
527 }
528
529 /*
530  * Create a Win95 long name directory entry
531  * Note: assumes that the filename is valid,
532  *       i.e. doesn't consist solely of blanks and dots
533  */
534 int
535 unix2winfn(un, unlen, wep, cnt, chksum, pmp)
536         const u_char *un;
537         size_t unlen;
538         struct winentry *wep;
539         int cnt;
540         int chksum;
541         struct msdosfsmount *pmp;
542 {
543         u_int8_t *wcp;
544         int i, end;
545         u_int16_t code;
546
547         /*
548          * Drop trailing blanks and dots
549          */
550         unlen = winLenFixup(un, unlen);
551
552         /*
553          * Cut *un for this slot
554          */
555         unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
556                           pmp->pm_flags, pmp->pm_u2w);
557
558         /*
559          * Initialize winentry to some useful default
560          */
561         for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
562         wep->weCnt = cnt;
563         wep->weAttributes = ATTR_WIN95;
564         wep->weReserved1 = 0;
565         wep->weChksum = chksum;
566         wep->weReserved2 = 0;
567
568         /*
569          * Now convert the filename parts
570          */
571         end = 0;
572         for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
573                 code = unix2winchr(&un, &unlen, 0, pmp);
574                 *wcp++ = code;
575                 *wcp++ = code >> 8;
576                 if (!code)
577                         end = WIN_LAST;
578         }
579         for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
580                 code = unix2winchr(&un, &unlen, 0, pmp);
581                 *wcp++ = code;
582                 *wcp++ = code >> 8;
583                 if (!code)
584                         end = WIN_LAST;
585         }
586         for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
587                 code = unix2winchr(&un, &unlen, 0, pmp);
588                 *wcp++ = code;
589                 *wcp++ = code >> 8;
590                 if (!code)
591                         end = WIN_LAST;
592         }
593         if (*un == '\0')
594                 end = WIN_LAST;
595         wep->weCnt |= end;
596         return !end;
597 }
598
599 /*
600  * Compare our filename to the one in the Win95 entry
601  * Returns the checksum or -1 if no match
602  */
603 int
604 winChkName(un, unlen, chksum, pmp)
605         const u_char *un;
606         size_t unlen;
607         int chksum;
608         struct msdosfsmount *pmp;
609 {
610         size_t len;
611         u_int16_t c1, c2;
612         u_char *np;
613         struct dirent dirbuf;
614
615         /*
616          * We alread have winentry in mbnambuf
617          */
618         if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen)
619                 return -1;
620
621 #ifdef MSDOSFS_DEBUG
622         printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
623                                                         dirbuf.d_name,
624                                                         dirbuf.d_namlen);
625 #endif
626
627         /*
628          * Compare the name parts
629          */
630         len = dirbuf.d_namlen;
631         if (unlen != len)
632                 return -2;
633
634         for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
635                 /*
636                  * Comparison must be case insensitive, because FAT disallows
637                  * to look up or create files in case sensitive even when
638                  * it's a long file name.
639                  */
640                 c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
641                 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
642                 if (c1 != c2)
643                         return -2;
644         }
645         return chksum;
646 }
647
648 /*
649  * Convert Win95 filename to dirbuf.
650  * Returns the checksum or -1 if impossible
651  */
652 int
653 win2unixfn(wep, chksum, pmp)
654         struct winentry *wep;
655         int chksum;
656         struct msdosfsmount *pmp;
657 {
658         u_int8_t *cp;
659         u_int8_t *np, name[WIN_CHARS * 2 + 1];
660         u_int16_t code;
661         int i;
662
663         if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
664             || !(wep->weCnt&WIN_CNT))
665                 return -1;
666
667         /*
668          * First compare checksums
669          */
670         if (wep->weCnt&WIN_LAST) {
671                 chksum = wep->weChksum;
672         } else if (chksum != wep->weChksum)
673                 chksum = -1;
674         if (chksum == -1)
675                 return -1;
676
677         /*
678          * Convert the name parts
679          */
680         np = name;
681         for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
682                 code = (cp[1] << 8) | cp[0];
683                 switch (code) {
684                 case 0:
685                         *np = '\0';
686                         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
687                         return chksum;
688                 case '/':
689                         *np = '\0';
690                         return -1;
691                 default:
692                         code = win2unixchr(code, pmp);
693                         if (code & 0xff00)
694                                 *np++ = code >> 8;
695                         *np++ = code;
696                         break;
697                 }
698                 cp += 2;
699         }
700         for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
701                 code = (cp[1] << 8) | cp[0];
702                 switch (code) {
703                 case 0:
704                         *np = '\0';
705                         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
706                         return chksum;
707                 case '/':
708                         *np = '\0';
709                         return -1;
710                 default:
711                         code = win2unixchr(code, pmp);
712                         if (code & 0xff00)
713                                 *np++ = code >> 8;
714                         *np++ = code;
715                         break;
716                 }
717                 cp += 2;
718         }
719         for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
720                 code = (cp[1] << 8) | cp[0];
721                 switch (code) {
722                 case 0:
723                         *np = '\0';
724                         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
725                         return chksum;
726                 case '/':
727                         *np = '\0';
728                         return -1;
729                 default:
730                         code = win2unixchr(code, pmp);
731                         if (code & 0xff00)
732                                 *np++ = code >> 8;
733                         *np++ = code;
734                         break;
735                 }
736                 cp += 2;
737         }
738         *np = '\0';
739         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
740         return chksum;
741 }
742
743 /*
744  * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
745  */
746 u_int8_t
747 winChksum(struct direntry *dep)
748 {
749         u_int8_t s;
750
751         s = dep->deName[0];
752         s = ((s << 7) | (s >> 1)) + dep->deName[1];
753         s = ((s << 7) | (s >> 1)) + dep->deName[2];
754         s = ((s << 7) | (s >> 1)) + dep->deName[3];
755         s = ((s << 7) | (s >> 1)) + dep->deName[4];
756         s = ((s << 7) | (s >> 1)) + dep->deName[5];
757         s = ((s << 7) | (s >> 1)) + dep->deName[6];
758         s = ((s << 7) | (s >> 1)) + dep->deName[7];
759         s = ((s << 7) | (s >> 1)) + dep->deExtension[0];
760         s = ((s << 7) | (s >> 1)) + dep->deExtension[1];
761         s = ((s << 7) | (s >> 1)) + dep->deExtension[2];
762
763         return (s);
764 }
765
766 /*
767  * Determine the number of slots necessary for Win95 names
768  */
769 int
770 winSlotCnt(un, unlen, pmp)
771         const u_char *un;
772         size_t unlen;
773         struct msdosfsmount *pmp;
774 {
775         size_t wlen;
776         char wn[WIN_MAXLEN * 2 + 1], *wnp;
777
778         unlen = winLenFixup(un, unlen);
779
780         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
781                 wlen = WIN_MAXLEN * 2;
782                 wnp = wn;
783                 msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
784                 if (unlen > 0)
785                         return 0;
786                 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
787         }
788
789         if (unlen > WIN_MAXLEN)
790                 return 0;
791         return howmany(unlen, WIN_CHARS);
792 }
793
794 /*
795  * Determine the number of bytes neccesary for Win95 names
796  */
797 size_t
798 winLenFixup(un, unlen)
799         const u_char* un;
800         size_t unlen;
801 {
802         for (un += unlen; unlen > 0; unlen--)
803                 if (*--un != ' ' && *un != '.')
804                         break;
805         return unlen;
806 }
807
808 /*
809  * Store an area with multi byte string instr, and reterns left
810  * byte of instr and moves pointer forward. The area's size is
811  * inlen or outlen.
812  */
813 static int
814 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
815 {
816         char *outp, outstr[outlen * weight + 1];
817
818         if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
819                 outp = outstr;
820                 outlen *= weight;
821                 msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
822                 return (inlen);
823         }
824
825         (*instr) += min(inlen, outlen);
826         return (inlen - min(inlen, outlen));
827 }
828
829 /*
830  * Convert DOS char to Local char
831  */
832 static u_int16_t
833 dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
834 {
835         u_char c;
836         char *outp, outbuf[3];
837         u_int16_t wc;
838         size_t len, olen;
839
840         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
841                 olen = len = 2;
842                 outp = outbuf;
843
844                 if (lower & (LCASE_BASE | LCASE_EXT))
845                         msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
846                                                   ilen, &outp, &olen, KICONV_LOWER);
847                 else
848                         msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
849                                              ilen, &outp, &olen);
850                 len -= olen;
851
852                 /*
853                  * return '?' if failed to convert
854                  */
855                 if (len == 0) {
856                         (*ilen)--;
857                         (*instr)++;
858                         return ('?');
859                 }
860
861                 wc = 0;
862                 while(len--)
863                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
864                 return (wc);
865         }
866
867         (*ilen)--;
868         c = *(*instr)++;
869         c = dos2unix[c];
870         if (lower & (LCASE_BASE | LCASE_EXT))
871                 c = u2l[c];
872         return ((u_int16_t)c);
873 }
874
875 /*
876  * Convert Local char to DOS char
877  */
878 static u_int16_t
879 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
880 {
881         u_char c;
882         char *up, *outp, unicode[3], outbuf[3];
883         u_int16_t wc;
884         size_t len, ucslen, unixlen, olen;
885
886         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
887                 /*
888                  * to hide an invisible character, using a unicode filter
889                  */
890                 ucslen = 2;
891                 len = *ilen;
892                 up = unicode;
893                 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
894                                      ilen, &up, &ucslen);
895                 unixlen = len - *ilen;
896
897                 /*
898                  * cannot be converted
899                  */
900                 if (unixlen == 0) {
901                         (*ilen)--;
902                         (*instr)++;
903                         return (0);
904                 }
905
906                 /*
907                  * return magic number for ascii char
908                  */
909                 if (unixlen == 1) {
910                         c = *(*instr -1);
911                         if (! (c & 0x80)) {
912                                 c = unix2dos[c];
913                                 if (c <= 2)
914                                         return (c);
915                         }
916                 }
917
918                 /*
919                  * now convert using libiconv
920                  */
921                 *instr -= unixlen;
922                 *ilen = len;
923
924                 olen = len = 2;
925                 outp = outbuf;
926                 msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
927                                           ilen, &outp, &olen, KICONV_FROM_UPPER);
928                 len -= olen;
929
930                 /*
931                  * cannot be converted, but has unicode char, should return magic number
932                  */
933                 if (len == 0) {
934                         (*ilen) -= unixlen;
935                         (*instr) += unixlen;
936                         return (1);
937                 }
938
939                 wc = 0;
940                 while(len--)
941                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
942                 return (wc);
943         }
944
945         (*ilen)--;
946         c = *(*instr)++;
947         c = l2u[c];
948         c = unix2dos[c];
949         return ((u_int16_t)c);
950 }
951
952 /*
953  * Convert Windows char to Local char
954  */
955 static u_int16_t
956 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
957 {
958         u_char *inp, *outp, inbuf[3], outbuf[3];
959         size_t ilen, olen, len;
960
961         if (wc == 0)
962                 return (0);
963
964         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
965                 inbuf[0] = (u_char)(wc>>8);
966                 inbuf[1] = (u_char)wc;
967                 inbuf[2] = '\0';
968
969                 ilen = olen = len = 2;
970                 inp = inbuf;
971                 outp = outbuf;
972                 msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
973                                      (char **)&outp, &olen);
974                 len -= olen;
975
976                 /*
977                  * return '?' if failed to convert
978                  */
979                 if (len == 0) {
980                         wc = '?';
981                         return (wc);
982                 }
983
984                 wc = 0;
985                 while(len--)
986                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
987                 return (wc);
988         }
989
990         if (wc & 0xff00)
991                 wc = '?';
992
993         return (wc);
994 }
995
996 /*
997  * Convert Local char to Windows char
998  */
999 static u_int16_t
1000 unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
1001 {
1002         u_char *outp, outbuf[3];
1003         u_int16_t wc;
1004         size_t olen;
1005
1006         if (*ilen == 0)
1007                 return (0);
1008
1009         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
1010                 outp = outbuf;
1011                 olen = 2;
1012                 if (lower & (LCASE_BASE | LCASE_EXT))
1013                         msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
1014                                                   ilen, (char **)&outp, &olen,
1015                                                   KICONV_FROM_LOWER);
1016                 else
1017                         msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
1018                                              ilen, (char **)&outp, &olen);
1019
1020                 /*
1021                  * return '0' if end of filename
1022                  */
1023                 if (olen == 2)
1024                         return (0);
1025
1026                 wc = (outbuf[0]<<8) | outbuf[1];
1027
1028                 return (wc);
1029         }
1030
1031         (*ilen)--;
1032         wc = (*instr)[0];
1033         if (lower & (LCASE_BASE | LCASE_EXT))
1034                 wc = u2l[wc];
1035         (*instr)++;
1036         return (wc);
1037 }
1038
1039 /*
1040  * Initialize the temporary concatenation buffer (once) and mark it as
1041  * empty on subsequent calls.
1042  */
1043 void
1044 mbnambuf_init(void)
1045 {
1046
1047         if (nambuf_ptr == NULL) { 
1048                 nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK);
1049                 nambuf_ptr[MAXNAMLEN] = '\0';
1050         }
1051         nambuf_len = 0;
1052         nambuf_last_id = -1;
1053 }
1054
1055 /*
1056  * Fill out our concatenation buffer with the given substring, at the offset
1057  * specified by its id.  Since this function must be called with ids in
1058  * descending order, we take advantage of the fact that ASCII substrings are
1059  * exactly WIN_CHARS in length.  For non-ASCII substrings, we shift all
1060  * previous (i.e. higher id) substrings upwards to make room for this one.
1061  * This only penalizes portions of substrings that contain more than
1062  * WIN_CHARS bytes when they are first encountered.
1063  */
1064 void
1065 mbnambuf_write(char *name, int id)
1066 {
1067         size_t count;
1068         char *slot;
1069
1070         KASSERT(nambuf_len == 0 || id == nambuf_last_id - 1,
1071             ("non-decreasing id, id %d last id %d", id, nambuf_last_id));
1072
1073         /* Store this substring in a WIN_CHAR-aligned slot. */
1074         slot = nambuf_ptr + (id * WIN_CHARS);
1075         count = strlen(name);
1076         if (nambuf_len + count > MAXNAMLEN) {
1077                 printf("msdosfs: file name %zu too long\n", nambuf_len + count);
1078                 return;
1079         }
1080
1081         /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
1082         if (count > WIN_CHARS && nambuf_len != 0)
1083                 bcopy(slot + WIN_CHARS, slot + count, nambuf_len);
1084
1085         /* Copy in the substring to its slot and update length so far. */
1086         bcopy(name, slot, count);
1087         nambuf_len += count;
1088         nambuf_last_id = id;
1089 }
1090
1091 /*
1092  * Take the completed string and use it to setup the struct dirent.
1093  * Be sure to always nul-terminate the d_name and then copy the string
1094  * from our buffer.  Note that this function assumes the full string has
1095  * been reassembled in the buffer.  If it's called before all substrings
1096  * have been written via mbnambuf_write(), the result will be incorrect.
1097  */
1098 char *
1099 mbnambuf_flush(struct dirent *dp)
1100 {
1101
1102         if (nambuf_len > sizeof(dp->d_name) - 1) {
1103                 mbnambuf_init();
1104                 return (NULL);
1105         }
1106         bcopy(nambuf_ptr, dp->d_name, nambuf_len);
1107         dp->d_name[nambuf_len] = '\0';
1108         dp->d_namlen = nambuf_len;
1109
1110         mbnambuf_init();
1111         return (dp->d_name);
1112 }