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