2 /* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (C) 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1995, 1997 TooLs GmbH.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
43 * This software is provided "as is".
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.
53 #include <sys/param.h>
54 #include <sys/kernel.h> /* defines tz */
55 #include <sys/systm.h>
56 #include <machine/clock.h>
57 #include <sys/dirent.h>
58 #include <sys/iconv.h>
59 #include <sys/mount.h>
61 #include <vfs/msdosfs/bpb.h>
62 #include <vfs/msdosfs/direntry.h>
63 #include <vfs/msdosfs/msdosfsmount.h>
65 extern struct iconv_functions
*msdos_iconv
;
67 static int mbsadjpos(const char **, size_t, size_t, int, int, void *);
68 static uint16_t dos2unixchr(u_char
*, const u_char
**, size_t *, int,
69 struct msdosfsmount
*);
70 static uint16_t unix2doschr(const u_char
**, size_t *, struct msdosfsmount
*);
71 static uint16_t win2unixchr(u_char
*, uint16_t, struct msdosfsmount
*);
72 static uint16_t unix2winchr(const u_char
**, size_t *, int,
73 struct msdosfsmount
*);
76 * 0 - character disallowed in long file name.
77 * 1 - character should be replaced by '_' in DOS file name,
78 * and generation number inserted.
79 * 2 - character ('.' and ' ') should be skipped in DOS file name,
80 * and generation number inserted.
84 /* iso8859-1 -> cp850 */
85 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
86 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
87 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
88 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
89 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
90 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */
91 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
92 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */
93 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
94 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
95 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
96 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */
97 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */
98 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */
99 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */
100 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */
101 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
102 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
103 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
104 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
105 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
106 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
107 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
108 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
109 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
110 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
111 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
112 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
113 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
114 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
115 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
116 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
121 /* cp850 -> iso8859-1 */
122 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
123 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
124 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
125 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
126 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
127 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
128 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
129 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
130 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
131 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
132 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
133 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
134 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
135 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
136 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
137 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
138 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
139 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
140 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
141 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */
142 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
143 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
144 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */
145 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */
146 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */
147 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */
148 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */
149 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */
150 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
151 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
152 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
153 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */
159 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
160 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
161 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
162 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
163 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
164 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
165 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
166 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
167 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
168 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
169 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
170 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
171 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
172 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
173 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
174 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
175 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
176 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
177 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
178 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
179 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
180 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
181 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
182 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
183 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
184 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
185 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
186 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
187 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
188 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
189 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
190 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
196 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
197 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
198 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
199 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
200 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
201 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
202 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
203 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
204 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
205 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
206 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
207 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
208 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
209 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
210 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
211 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
212 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
213 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
214 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
215 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
216 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
217 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
218 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
219 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
220 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
221 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
222 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
223 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
224 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
225 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
226 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
227 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
231 * DOS filenames are made of 2 parts, the name part and the extension part.
232 * The name part is 8 characters long and the extension part is 3
233 * characters long. They may contain trailing blanks if the name or
234 * extension are not long enough to fill their respective fields.
238 * Convert a DOS filename to a unix filename. And, return the number of
239 * characters in the resulting unix filename excluding the terminating
243 dos2unixfn(u_char dn
[11], u_char
*un
, int lower
, struct msdosfsmount
*pmp
)
250 * If first char of the filename is SLOT_E5 (0x05), then the real
251 * first char of the filename should be 0xe5. But, they couldn't
252 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
253 * directory slot. Another dos quirk.
259 * Copy the name portion into the unix filename string.
261 for (i
= 8; i
> 0 && *dn
!= ' ';) {
262 c
= dos2unixchr(tmpbuf
, __DECONST(const u_char
**, &dn
), &i
,
263 lower
& LCASE_BASE
, pmp
);
274 * Now, if there is an extension then put in a period and copy in
280 for (i
= 3; i
> 0 && *dn
!= ' ';) {
281 c
= dos2unixchr(tmpbuf
, __DECONST(const u_char
**, &dn
),
282 &i
, lower
& LCASE_EXT
, pmp
);
297 * Convert a unix filename to a DOS filename according to Win95 rules.
298 * If applicable and gen is not 0, it is inserted into the converted
299 * filename as a generation number.
301 * 0 if name couldn't be converted
302 * 1 if the converted name is the same as the original
303 * (no long filename entry necessary for Win95)
304 * 2 if conversion was successful
305 * 3 if conversion was successful and generation number was inserted
308 unix2dosfn(const u_char
*un
, u_char dn
[12], size_t unlen
, u_int gen
,
309 struct msdosfsmount
*pmp
)
314 const u_char
*cp
, *dp
, *dp1
;
315 u_char gentext
[6], *wcp
;
319 * Fill the dos filename string with blanks. These are DOS's pad
322 for (i
= 0; i
< 11; i
++)
327 * The filenames "." and ".." are handled specially, since they
328 * don't follow dos filename rules.
330 if (un
[0] == '.' && unlen
== 1) {
334 if (un
[0] == '.' && un
[1] == '.' && unlen
== 2) {
341 * Filenames with only blanks and dots are not allowed!
343 for (cp
= un
, i
= unlen
; --i
>= 0; cp
++)
344 if (*cp
!= ' ' && *cp
!= '.')
350 * Filenames with some characters are not allowed!
352 for (cp
= un
, i
= unlen
; i
> 0;)
353 if (unix2doschr(&cp
, (size_t *)&i
, pmp
) == 0)
357 * Now find the extension
358 * Note: dot as first char doesn't start extension
359 * and trailing dots and blanks are ignored
360 * Note(2003/7): It seems recent Windows has
361 * defferent rule than this code, that Windows
362 * ignores all dots before extension, and use all
363 * chars as filename except for dots.
366 for (cp
= un
+ 1, i
= unlen
- 1; --i
>= 0;) {
383 * Now convert it (this part is for extension).
384 * As Windows XP do, if it's not ascii char,
385 * this function should return 2 or 3, so that checkng out Unicode name.
391 l
= unlen
- (dp
- un
);
392 for (cp
= dp
, i
= l
, j
= 8; i
> 0 && j
< 11; j
++) {
393 c
= unix2doschr(&cp
, (size_t *)&i
, pmp
);
409 if (((dn
[j
] & 0x80) || *(cp
- 1) != dn
[j
]) && conv
!= 3)
424 for (dp
= cp
; *--dp
== ' ' || *dp
== '.';);
429 * Now convert the rest of the name
431 for (i
= dp
- un
, j
= 0; un
< dp
&& j
< 8; j
++) {
432 c
= unix2doschr(&un
, &i
, pmp
);
448 if (((dn
[j
] & 0x80) || *(un
- 1) != dn
[j
]) && conv
!= 3)
462 * If we didn't have any chars in filename,
469 * If there wasn't any char dropped,
470 * there is no place for generation numbers
479 * Now insert the generation number into the filename part
483 for (wcp
= gentext
+ sizeof(gentext
); wcp
> gentext
&& gen
; gen
/= 10)
484 *--wcp
= gen
% 10 + '0';
489 for (i
= 8; dn
[--i
] == ' ';);
491 if (gentext
+ sizeof(gentext
) - wcp
+ 1 > 8 - i
)
492 i
= 8 - (gentext
+ sizeof(gentext
) - wcp
+ 1);
494 * Correct posision to where insert the generation number
497 i
-= mbsadjpos((const char**)&cp
, i
, unlen
, 1, pmp
->pm_flags
,
500 while (wcp
< gentext
+ sizeof(gentext
))
504 * Tail of the filename should be space
512 * The first character cannot be E5,
513 * because that means a deleted entry
522 * Create a Win95 long name directory entry
523 * Note: assumes that the filename is valid,
524 * i.e. doesn't consist solely of blanks and dots
527 unix2winfn(const u_char
*un
, size_t unlen
, struct winentry
*wep
, int cnt
,
528 int chksum
, struct msdosfsmount
*pmp
)
535 * Drop trailing blanks and dots
537 unlen
= winLenFixup(un
, unlen
);
540 * Cut *un for this slot
542 unlen
= mbsadjpos((const char **)&un
, unlen
, (cnt
- 1) * WIN_CHARS
, 2,
543 pmp
->pm_flags
, pmp
->pm_u2w
);
546 * Initialize winentry to some useful default
548 memset(wep
, 0xff, sizeof(*wep
));
550 wep
->weAttributes
= ATTR_WIN95
;
551 wep
->weReserved1
= 0;
552 wep
->weChksum
= chksum
;
553 wep
->weReserved2
= 0;
556 * Now convert the filename parts
559 for (wcp
= wep
->wePart1
, i
= sizeof(wep
->wePart1
)/2; --i
>= 0 && !end
;) {
560 code
= unix2winchr(&un
, &unlen
, 0, pmp
);
566 for (wcp
= wep
->wePart2
, i
= sizeof(wep
->wePart2
)/2; --i
>= 0 && !end
;) {
567 code
= unix2winchr(&un
, &unlen
, 0, pmp
);
573 for (wcp
= wep
->wePart3
, i
= sizeof(wep
->wePart3
)/2; --i
>= 0 && !end
;) {
574 code
= unix2winchr(&un
, &unlen
, 0, pmp
);
587 * Compare our filename to the one in the Win95 entry
588 * Returns the checksum or -1 if no match
591 winChkName(struct mbnambuf
*nbp
, const u_char
*un
, size_t unlen
, int chksum
,
592 struct msdosfsmount
*pmp
)
597 struct dirent dirbuf
;
600 * We already have winentry in *nbp.
602 if (!mbnambuf_flush(nbp
, &dirbuf
) || dirbuf
.d_namlen
== 0)
604 mprintf("winChkName(): un=%s:%zu,d_name=%s:%d\n",
605 un
, unlen
, dirbuf
.d_name
, dirbuf
.d_namlen
);
607 * Compare the name parts
609 len
= dirbuf
.d_namlen
;
613 for (np
= dirbuf
.d_name
; unlen
> 0 && len
> 0;) {
615 * Comparison must be case insensitive, because FAT disallows
616 * to look up or create files in case sensitive even when
617 * it's a long file name.
619 c1
= unix2winchr(__DECONST(const u_char
**, &np
), &len
,
621 c2
= unix2winchr(&un
, &unlen
, LCASE_BASE
, pmp
);
629 * Convert Win95 filename to dirbuf.
630 * Returns the checksum or -1 if impossible
633 win2unixfn(struct mbnambuf
*nbp
, struct winentry
*wep
, int chksum
,
634 struct msdosfsmount
*pmp
)
638 uint8_t *np
, name
[WIN_CHARS
* 2 + 1];
642 if ((wep
->weCnt
&WIN_CNT
) > howmany(WIN_MAXLEN
, WIN_CHARS
)
643 || !(wep
->weCnt
&WIN_CNT
))
647 * First compare checksums
649 if (wep
->weCnt
&WIN_LAST
) {
650 chksum
= wep
->weChksum
;
651 } else if (chksum
!= wep
->weChksum
)
657 * Convert the name parts
660 for (cp
= wep
->wePart1
, i
= sizeof(wep
->wePart1
)/2; --i
>= 0;) {
661 code
= (cp
[1] << 8) | cp
[0];
665 if (mbnambuf_write(nbp
, name
,
666 (wep
->weCnt
& WIN_CNT
) - 1) != 0)
673 code
= win2unixchr(tmpbuf
, code
, pmp
);
681 for (cp
= wep
->wePart2
, i
= sizeof(wep
->wePart2
)/2; --i
>= 0;) {
682 code
= (cp
[1] << 8) | cp
[0];
686 if (mbnambuf_write(nbp
, name
,
687 (wep
->weCnt
& WIN_CNT
) - 1) != 0)
694 code
= win2unixchr(tmpbuf
, code
, pmp
);
702 for (cp
= wep
->wePart3
, i
= sizeof(wep
->wePart3
)/2; --i
>= 0;) {
703 code
= (cp
[1] << 8) | cp
[0];
707 if (mbnambuf_write(nbp
, name
,
708 (wep
->weCnt
& WIN_CNT
) - 1) != 0)
715 code
= win2unixchr(tmpbuf
, code
, pmp
);
724 if (mbnambuf_write(nbp
, name
, (wep
->weCnt
& WIN_CNT
) - 1) != 0)
730 * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
733 winChksum(uint8_t *name
)
738 for (s
= 0, i
= 11; --i
>= 0; s
+= *name
++)
739 s
= (s
<< 7)|(s
>> 1);
744 * Determine the number of slots necessary for Win95 names
747 winSlotCnt(const u_char
*un
, size_t unlen
, struct msdosfsmount
*pmp
)
750 char wn
[WIN_MAXLEN
* 2 + 1], *wnp
;
752 unlen
= winLenFixup(un
, unlen
);
754 if (pmp
->pm_flags
& MSDOSFSMNT_KICONV
&& msdosfs_iconv
) {
755 wlen
= WIN_MAXLEN
* 2;
757 msdosfs_iconv
->conv(pmp
->pm_u2w
, (const char **)&un
, &unlen
,
761 return howmany(WIN_MAXLEN
- wlen
/2, WIN_CHARS
);
764 if (unlen
> WIN_MAXLEN
)
766 return howmany(unlen
, WIN_CHARS
);
770 * Determine the number of bytes necessary for Win95 names
773 winLenFixup(const u_char
*un
, size_t unlen
)
775 for (un
+= unlen
; unlen
> 0; unlen
--)
776 if (*--un
!= ' ' && *un
!= '.')
782 * Store an area with multi byte string instr, and returns left
783 * byte of instr and moves pointer forward. The area's size is
787 mbsadjpos(const char **instr
, size_t inlen
, size_t outlen
, int weight
, int flag
,
790 char *outp
, outstr
[outlen
* weight
+ 1];
792 if (flag
& MSDOSFSMNT_KICONV
&& msdosfs_iconv
) {
795 msdosfs_iconv
->conv(handle
, instr
, &inlen
, &outp
, &outlen
);
799 (*instr
) += min(inlen
, outlen
);
800 return (inlen
- min(inlen
, outlen
));
804 * Convert DOS char to Local char
807 dos2unixchr(u_char
*outbuf
, const u_char
**instr
, size_t *ilen
, int lower
,
808 struct msdosfsmount
*pmp
)
815 if (pmp
->pm_flags
& MSDOSFSMNT_KICONV
&& msdosfs_iconv
) {
819 if (lower
& (LCASE_BASE
| LCASE_EXT
))
820 msdosfs_iconv
->convchr_case(pmp
->pm_d2u
,
821 (const char **)instr
, ilen
,
822 &outp
, &olen
, KICONV_LOWER
);
824 msdosfs_iconv
->convchr(pmp
->pm_d2u
,
825 (const char **)instr
,
830 * return '?' if failed to convert
839 wc
|= (*(outp
- len
- 1) & 0xff) << (len
<< 3);
845 if (lower
& (LCASE_BASE
| LCASE_EXT
))
847 return ((uint16_t)c
);
851 * Convert Local char to DOS char
854 unix2doschr(const u_char
**instr
, size_t *ilen
, struct msdosfsmount
*pmp
)
857 char *up
, *outp
, unicode
[3], outbuf
[3];
859 size_t len
, ucslen
, unixlen
, olen
;
861 if (pmp
->pm_flags
& MSDOSFSMNT_KICONV
&& msdosfs_iconv
) {
863 * to hide an invisible character, using a unicode filter
868 msdosfs_iconv
->convchr(pmp
->pm_u2w
, (const char **)instr
,
870 unixlen
= len
- *ilen
;
873 * cannot be converted
882 * return magic number for ascii char
894 * now convert using libiconv
901 msdosfs_iconv
->convchr_case(pmp
->pm_u2d
, (const char **)instr
,
907 * cannot be converted, but has unicode char, should return magic number
917 wc
|= (*(outp
- len
- 1) & 0xff) << (len
<< 3);
925 return ((uint16_t)c
);
929 * Convert Windows char to Local char
932 win2unixchr(u_char
*outbuf
, uint16_t wc
, struct msdosfsmount
*pmp
)
934 u_char
*inp
, *outp
, inbuf
[3];
935 size_t ilen
, olen
, len
;
939 if (pmp
->pm_flags
& MSDOSFSMNT_KICONV
&& msdosfs_iconv
) {
940 inbuf
[0] = (u_char
)(wc
>>8);
941 inbuf
[1] = (u_char
)wc
;
943 ilen
= olen
= len
= 2;
946 msdosfs_iconv
->convchr(pmp
->pm_w2u
, __DECONST(const char **,
947 &inp
), &ilen
, (char **)&outp
, &olen
);
951 * return '?' if failed to convert
959 wc
|= (*(outp
- len
- 1) & 0xff) << (len
<< 3);
968 * Convert Local char to Windows char
971 unix2winchr(const u_char
**instr
, size_t *ilen
, int lower
,
972 struct msdosfsmount
*pmp
)
974 u_char
*outp
, outbuf
[3];
981 if (pmp
->pm_flags
& MSDOSFSMNT_KICONV
&& msdosfs_iconv
) {
984 if (lower
& (LCASE_BASE
| LCASE_EXT
))
985 msdosfs_iconv
->convchr_case(pmp
->pm_u2w
,
986 (const char **)instr
,
987 ilen
, (char **)&outp
, &olen
,
990 msdosfs_iconv
->convchr(pmp
->pm_u2w
,
991 (const char **)instr
,
992 ilen
, (char **)&outp
, &olen
);
994 * return '0' if end of filename
999 wc
= (outbuf
[0]<<8) | outbuf
[1];
1005 if (lower
& (LCASE_BASE
| LCASE_EXT
))
1012 * Initialize the temporary concatenation buffer.
1015 mbnambuf_init(struct mbnambuf
*nbp
)
1018 nbp
->nb_last_id
= -1;
1019 nbp
->nb_buf
[sizeof(nbp
->nb_buf
) - 1] = '\0';
1023 * Fill out our concatenation buffer with the given substring, at the offset
1024 * specified by its id. Since this function must be called with ids in
1025 * descending order, we take advantage of the fact that ASCII substrings are
1026 * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all
1027 * previous (i.e. higher id) substrings upwards to make room for this one.
1028 * This only penalizes portions of substrings that contain more than
1029 * WIN_CHARS bytes when they are first encountered.
1032 mbnambuf_write(struct mbnambuf
*nbp
, char *name
, int id
)
1035 size_t count
, newlen
;
1037 if (nbp
->nb_len
!= 0 && id
!= nbp
->nb_last_id
- 1) {
1038 kprintf("msdosfs: non-decreasing id: id %d, last id %d\n",
1039 id
, nbp
->nb_last_id
);
1043 /* Will store this substring in a WIN_CHARS-aligned slot. */
1044 slot
= &nbp
->nb_buf
[id
* WIN_CHARS
];
1045 count
= strlen(name
);
1046 newlen
= nbp
->nb_len
+ count
;
1047 if (newlen
> WIN_MAXLEN
|| newlen
> 127) {
1048 kprintf("msdosfs: file name length %zu too large\n", newlen
);
1049 return (ENAMETOOLONG
);
1052 /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
1053 if (count
> WIN_CHARS
&& nbp
->nb_len
!= 0) {
1054 if ((id
* WIN_CHARS
+ count
+ nbp
->nb_len
) >
1055 sizeof(nbp
->nb_buf
))
1056 return (ENAMETOOLONG
);
1058 memmove(slot
+ count
, slot
+ WIN_CHARS
, nbp
->nb_len
);
1061 /* Copy in the substring to its slot and update length so far. */
1062 memcpy(slot
, name
, count
);
1063 nbp
->nb_len
= newlen
;
1064 nbp
->nb_last_id
= id
;
1070 * Take the completed string and use it to setup the struct dirent.
1071 * Be sure to always nul-terminate the d_name and then copy the string
1072 * from our buffer. Note that this function assumes the full string has
1073 * been reassembled in the buffer. If it's called before all substrings
1074 * have been written via mbnambuf_write(), the result will be incorrect.
1077 mbnambuf_flush(struct mbnambuf
*nbp
, struct dirent
*dp
)
1079 if (nbp
->nb_len
> sizeof(dp
->d_name
) - 1) {
1083 memcpy(dp
->d_name
, &nbp
->nb_buf
[0], nbp
->nb_len
);
1084 dp
->d_name
[nbp
->nb_len
] = '\0';
1085 dp
->d_namlen
= nbp
->nb_len
;
1088 return (dp
->d_name
);