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