xref: /dragonfly/sys/vfs/msdosfs/msdosfs_conv.c (revision 49837aef)
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/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>
60 
61 #include <vfs/msdosfs/bpb.h>
62 #include <vfs/msdosfs/direntry.h>
63 #include <vfs/msdosfs/msdosfsmount.h>
64 
65 extern struct iconv_functions *msdos_iconv;
66 
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 *);
74 
75 /*
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.
81  */
82 static const u_char
83 unix2dos[256] = {
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 */
117 };
118 
119 static const u_char
120 dos2unix[256] = {
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 */
154 };
155 
156 static const u_char
157 u2l[256] = {
158 /* tolower */
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 */
191 };
192 
193 static const u_char
194 l2u[256] = {
195 /* toupper */
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 */
228 };
229 
230 /*
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.
235  */
236 
237 /*
238  * Convert a DOS filename to a unix filename. And, return the number of
239  * characters in the resulting unix filename excluding the terminating
240  * null.
241  */
242 int
243 dos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp)
244 {
245 	size_t i;
246 	int thislong = 0;
247 	u_char c, tmpbuf[5];
248 
249 	/*
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.
254 	 */
255 	if (*dn == SLOT_E5)
256 		*dn = 0xe5;
257 
258 	/*
259 	 * Copy the name portion into the unix filename string.
260 	 */
261 	for (i = 8; i > 0 && *dn != ' ';) {
262 		c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), &i,
263 		    lower & LCASE_BASE, pmp);
264 		if (c & 0xff00) {
265 			*un++ = c >> 8;
266 			thislong++;
267 		}
268 		*un++ = c;
269 		thislong++;
270 	}
271 	dn += i;
272 
273 	/*
274 	 * Now, if there is an extension then put in a period and copy in
275 	 * the extension.
276 	 */
277 	if (*dn != ' ') {
278 		*un++ = '.';
279 		thislong++;
280 		for (i = 3; i > 0 && *dn != ' ';) {
281 			c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn),
282 			    &i, lower & LCASE_EXT, pmp);
283 			if (c & 0xff00) {
284 				*un++ = c >> 8;
285 				thislong++;
286 			}
287 			*un++ = c;
288 			thislong++;
289 		}
290 	}
291 	*un++ = 0;
292 
293 	return (thislong);
294 }
295 
296 /*
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.
300  * Returns
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
306  */
307 int
308 unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen,
309     struct msdosfsmount *pmp)
310 {
311 	ssize_t i, j;
312 	int l;
313 	int conv = 1;
314 	const u_char *cp, *dp, *dp1;
315 	u_char gentext[6], *wcp;
316 	uint16_t c;
317 
318 	/*
319 	 * Fill the dos filename string with blanks. These are DOS's pad
320 	 * characters.
321 	 */
322 	for (i = 0; i < 11; i++)
323 		dn[i] = ' ';
324 	dn[11] = 0;
325 
326 	/*
327 	 * The filenames "." and ".." are handled specially, since they
328 	 * don't follow dos filename rules.
329 	 */
330 	if (un[0] == '.' && unlen == 1) {
331 		dn[0] = '.';
332 		return gen <= 1;
333 	}
334 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
335 		dn[0] = '.';
336 		dn[1] = '.';
337 		return gen <= 1;
338 	}
339 
340 	/*
341 	 * Filenames with only blanks and dots are not allowed!
342 	 */
343 	for (cp = un, i = unlen; --i >= 0; cp++)
344 		if (*cp != ' ' && *cp != '.')
345 			break;
346 	if (i < 0)
347 		return 0;
348 
349 	/*
350 	 * Filenames with some characters are not allowed!
351 	 */
352 	for (cp = un, i = unlen; i > 0;)
353 		if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
354 			return 0;
355 
356 	/*
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.
364 	 */
365 	dp = dp1 = NULL;
366 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
367 		switch (*cp++) {
368 		case '.':
369 			if (!dp1)
370 				dp1 = cp;
371 			break;
372 		case ' ':
373 			break;
374 		default:
375 			if (dp1)
376 				dp = dp1;
377 			dp1 = NULL;
378 			break;
379 		}
380 	}
381 
382 	/*
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.
386 	 */
387 	if (dp) {
388 		if (dp1)
389 			l = dp1 - dp;
390 		else
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);
394 			if (c & 0xff00) {
395 				dn[j] = c >> 8;
396 				if (++j < 11) {
397 					dn[j] = c;
398 					if (conv != 3)
399 						conv = 2;
400 					continue;
401 				} else {
402 					conv = 3;
403 					dn[j-1] = ' ';
404 					break;
405 				}
406 			} else {
407 				dn[j] = c;
408 			}
409 			if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
410 				conv = 2;
411 			if (dn[j] == 1) {
412 				conv = 3;
413 				dn[j] = '_';
414 			}
415 			if (dn[j] == 2) {
416 				conv = 3;
417 				dn[j--] = ' ';
418 			}
419 		}
420 		if (i > 0)
421 			conv = 3;
422 		dp--;
423 	} else {
424 		for (dp = cp; *--dp == ' ' || *dp == '.';);
425 		dp++;
426 	}
427 
428 	/*
429 	 * Now convert the rest of the name
430 	 */
431 	for (i = dp - un, j = 0; un < dp && j < 8; j++) {
432 		c = unix2doschr(&un, &i, pmp);
433 		if (c & 0xff00) {
434 			dn[j] = c >> 8;
435 			if (++j < 8) {
436 				dn[j] = c;
437 				if (conv != 3)
438 					conv = 2;
439 				continue;
440 			} else {
441 				conv = 3;
442 				dn[j-1] = ' ';
443 				break;
444 			}
445 		} else {
446 			dn[j] = c;
447 		}
448 		if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
449 			conv = 2;
450 		if (dn[j] == 1) {
451 			conv = 3;
452 			dn[j] = '_';
453 		}
454 		if (dn[j] == 2) {
455 			conv = 3;
456 			dn[j--] = ' ';
457 		}
458 	}
459 	if (un < dp)
460 		conv = 3;
461 	/*
462 	 * If we didn't have any chars in filename,
463 	 * generate a default
464 	 */
465 	if (!j)
466 		dn[0] = '_';
467 
468 	/*
469 	 * If there wasn't any char dropped,
470 	 * there is no place for generation numbers
471 	 */
472 	if (conv != 3) {
473 		if (gen > 1)
474 			conv = 0;
475 		goto done;
476 	}
477 
478 	/*
479 	 * Now insert the generation number into the filename part
480 	 */
481 	if (gen == 0)
482 		goto done;
483 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
484 		*--wcp = gen % 10 + '0';
485 	if (gen) {
486 		conv = 0;
487 		goto done;
488 	}
489 	for (i = 8; dn[--i] == ' ';);
490 	i++;
491 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
492 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
493 	/*
494 	 * Correct posision to where insert the generation number
495 	 */
496 	cp = dn;
497 	i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags,
498 		       pmp->pm_d2u);
499 	dn[i++] = '~';
500 	while (wcp < gentext + sizeof(gentext))
501 		dn[i++] = *wcp++;
502 
503 	/*
504 	 * Tail of the filename should be space
505 	 */
506 	while (i < 8)
507 		dn[i++] = ' ';
508 	conv = 3;
509 
510 done:
511 	/*
512 	 * The first character cannot be E5,
513 	 * because that means a deleted entry
514 	 */
515 	if (dn[0] == 0xe5)
516 		dn[0] = SLOT_E5;
517 
518 	return conv;
519 }
520 
521 /*
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
525  */
526 int
527 unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt,
528     int chksum, struct msdosfsmount *pmp)
529 {
530 	uint8_t *wcp;
531 	int i, end;
532 	uint16_t code;
533 
534 	/*
535 	 * Drop trailing blanks and dots
536 	 */
537 	unlen = winLenFixup(un, unlen);
538 
539 	/*
540 	 * Cut *un for this slot
541 	 */
542 	unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
543 			  pmp->pm_flags, pmp->pm_u2w);
544 
545 	/*
546 	 * Initialize winentry to some useful default
547 	 */
548 	memset(wep, 0xff, sizeof(*wep));
549 	wep->weCnt = cnt;
550 	wep->weAttributes = ATTR_WIN95;
551 	wep->weReserved1 = 0;
552 	wep->weChksum = chksum;
553 	wep->weReserved2 = 0;
554 
555 	/*
556 	 * Now convert the filename parts
557 	 */
558 	end = 0;
559 	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
560 		code = unix2winchr(&un, &unlen, 0, pmp);
561 		*wcp++ = code;
562 		*wcp++ = code >> 8;
563 		if (!code)
564 			end = WIN_LAST;
565 	}
566 	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
567 		code = unix2winchr(&un, &unlen, 0, pmp);
568 		*wcp++ = code;
569 		*wcp++ = code >> 8;
570 		if (!code)
571 			end = WIN_LAST;
572 	}
573 	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
574 		code = unix2winchr(&un, &unlen, 0, pmp);
575 		*wcp++ = code;
576 		*wcp++ = code >> 8;
577 		if (!code)
578 			end = WIN_LAST;
579 	}
580 	if (!unlen)
581 		end = WIN_LAST;
582 	wep->weCnt |= end;
583 	return !end;
584 }
585 
586 /*
587  * Compare our filename to the one in the Win95 entry
588  * Returns the checksum or -1 if no match
589  */
590 int
591 winChkName(struct mbnambuf *nbp, const u_char *un, size_t unlen, int chksum,
592     struct msdosfsmount *pmp)
593 {
594 	size_t len;
595 	uint16_t c1, c2;
596 	u_char *np;
597 	struct dirent dirbuf;
598 
599 	/*
600 	 * We already have winentry in *nbp.
601 	 */
602 	if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0)
603 		return -1;
604 	mprintf("winChkName(): un=%s:%zu,d_name=%s:%d\n",
605 		un, unlen, dirbuf.d_name, dirbuf.d_namlen);
606 	/*
607 	 * Compare the name parts
608 	 */
609 	len = dirbuf.d_namlen;
610 	if (unlen != len)
611 		return -2;
612 
613 	for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
614 		/*
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.
618 		 */
619 		c1 = unix2winchr(__DECONST(const u_char **, &np), &len,
620 		    LCASE_BASE, pmp);
621 		c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
622 		if (c1 != c2)
623 			return -2;
624 	}
625 	return chksum;
626 }
627 
628 /*
629  * Convert Win95 filename to dirbuf.
630  * Returns the checksum or -1 if impossible
631  */
632 int
633 win2unixfn(struct mbnambuf *nbp, struct winentry *wep, int chksum,
634     struct msdosfsmount *pmp)
635 {
636 	u_char tmpbuf[5];
637 	uint8_t *cp;
638 	uint8_t *np, name[WIN_CHARS * 2 + 1];
639 	uint16_t code;
640 	int i;
641 
642 	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
643 	    || !(wep->weCnt&WIN_CNT))
644 		return -1;
645 
646 	/*
647 	 * First compare checksums
648 	 */
649 	if (wep->weCnt&WIN_LAST) {
650 		chksum = wep->weChksum;
651 	} else if (chksum != wep->weChksum)
652 		chksum = -1;
653 	if (chksum == -1)
654 		return -1;
655 
656 	/*
657 	 * Convert the name parts
658 	 */
659 	np = name;
660 	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
661 		code = (cp[1] << 8) | cp[0];
662 		switch (code) {
663 		case 0:
664 			*np = '\0';
665 			if (mbnambuf_write(nbp, name,
666 			    (wep->weCnt & WIN_CNT) - 1) != 0)
667 				return -1;
668 			return chksum;
669 		case '/':
670 			*np = '\0';
671 			return -1;
672 		default:
673 			code = win2unixchr(tmpbuf, code, pmp);
674 			if (code & 0xff00)
675 				*np++ = code >> 8;
676 			*np++ = code;
677 			break;
678 		}
679 		cp += 2;
680 	}
681 	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
682 		code = (cp[1] << 8) | cp[0];
683 		switch (code) {
684 		case 0:
685 			*np = '\0';
686 			if (mbnambuf_write(nbp, name,
687 			    (wep->weCnt & WIN_CNT) - 1) != 0)
688 				return -1;
689 			return chksum;
690 		case '/':
691 			*np = '\0';
692 			return -1;
693 		default:
694 			code = win2unixchr(tmpbuf, code, pmp);
695 			if (code & 0xff00)
696 				*np++ = code >> 8;
697 			*np++ = code;
698 			break;
699 		}
700 		cp += 2;
701 	}
702 	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
703 		code = (cp[1] << 8) | cp[0];
704 		switch (code) {
705 		case 0:
706 			*np = '\0';
707 			if (mbnambuf_write(nbp, name,
708 			    (wep->weCnt & WIN_CNT) - 1) != 0)
709 				return -1;
710 			return chksum;
711 		case '/':
712 			*np = '\0';
713 			return -1;
714 		default:
715 			code = win2unixchr(tmpbuf, code, pmp);
716 			if (code & 0xff00)
717 				*np++ = code >> 8;
718 			*np++ = code;
719 			break;
720 		}
721 		cp += 2;
722 	}
723 	*np = '\0';
724 	if (mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1) != 0)
725 		return -1;
726 	return chksum;
727 }
728 
729 /*
730  * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
731  */
732 uint8_t
733 winChksum(uint8_t *name)
734 {
735 	int i;
736 	uint8_t s;
737 
738 	for (s = 0, i = 11; --i >= 0; s += *name++)
739 		s = (s << 7)|(s >> 1);
740 	return (s);
741 }
742 
743 /*
744  * Determine the number of slots necessary for Win95 names
745  */
746 int
747 winSlotCnt(const u_char *un, size_t unlen, struct msdosfsmount *pmp)
748 {
749 	size_t wlen;
750 	char wn[WIN_MAXLEN * 2 + 1], *wnp;
751 
752 	unlen = winLenFixup(un, unlen);
753 
754 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
755 		wlen = WIN_MAXLEN * 2;
756 		wnp = wn;
757 		msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen,
758 				    &wnp, &wlen);
759 		if (unlen > 0)
760 			return 0;
761 		return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
762 	}
763 
764 	if (unlen > WIN_MAXLEN)
765 		return 0;
766 	return howmany(unlen, WIN_CHARS);
767 }
768 
769 /*
770  * Determine the number of bytes necessary for Win95 names
771  */
772 size_t
773 winLenFixup(const u_char *un, size_t unlen)
774 {
775 	for (un += unlen; unlen > 0; unlen--)
776 		if (*--un != ' ' && *un != '.')
777 			break;
778 	return unlen;
779 }
780 
781 /*
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
784  * inlen or outlen.
785  */
786 static int
787 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag,
788     void *handle)
789 {
790 	char *outp, outstr[outlen * weight + 1];
791 
792 	if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
793 		outp = outstr;
794 		outlen *= weight;
795 		msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
796 		return (inlen);
797 	}
798 
799 	(*instr) += min(inlen, outlen);
800 	return (inlen - min(inlen, outlen));
801 }
802 
803 /*
804  * Convert DOS char to Local char
805  */
806 static uint16_t
807 dos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower,
808     struct msdosfsmount *pmp)
809 {
810 	u_char c;
811 	char *outp;
812 	uint16_t wc;
813 	size_t len, olen;
814 
815 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
816 		olen = len = 2;
817 		outp = outbuf;
818 
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);
823 		else
824 			msdosfs_iconv->convchr(pmp->pm_d2u,
825 					       (const char **)instr,
826 					       ilen, &outp, &olen);
827 		len -= olen;
828 
829 		/*
830 		 * return '?' if failed to convert
831 		 */
832 		if (len == 0) {
833 			(*ilen)--;
834 			(*instr)++;
835 			return ('?');
836 		}
837 		wc = 0;
838 		while(len--)
839 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
840 		return (wc);
841 	}
842 	(*ilen)--;
843 	c = *(*instr)++;
844 	c = dos2unix[c];
845 	if (lower & (LCASE_BASE | LCASE_EXT))
846 		c = u2l[c];
847 	return ((uint16_t)c);
848 }
849 
850 /*
851  * Convert Local char to DOS char
852  */
853 static uint16_t
854 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
855 {
856 	u_char c;
857 	char *up, *outp, unicode[3], outbuf[3];
858 	uint16_t wc;
859 	size_t len, ucslen, unixlen, olen;
860 
861 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
862 		/*
863 		 * to hide an invisible character, using a unicode filter
864 		 */
865 		ucslen = 2;
866 		len = *ilen;
867 		up = unicode;
868 		msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
869 				       ilen, &up, &ucslen);
870 		unixlen = len - *ilen;
871 
872 		/*
873 		 * cannot be converted
874 		 */
875 		if (unixlen == 0) {
876 			(*ilen)--;
877 			(*instr)++;
878 			return (0);
879 		}
880 
881 		/*
882 		 * return magic number for ascii char
883 		 */
884 		if (unixlen == 1) {
885 			c = *(*instr -1);
886 			if (! (c & 0x80)) {
887 				c = unix2dos[c];
888 				if (c <= 2)
889 					return (c);
890 			}
891 		}
892 
893 		/*
894 		 * now convert using libiconv
895 		 */
896 		*instr -= unixlen;
897 		*ilen = len;
898 
899 		olen = len = 2;
900 		outp = outbuf;
901 		msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
902 					    ilen, &outp, &olen,
903 					    KICONV_FROM_UPPER);
904 		len -= olen;
905 
906 		/*
907 		 * cannot be converted, but has unicode char, should return magic number
908 		 */
909 		if (len == 0) {
910 			(*ilen) -= unixlen;
911 			(*instr) += unixlen;
912 			return (1);
913 		}
914 
915 		wc = 0;
916 		while(len--)
917 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
918 		return (wc);
919 	}
920 
921 	(*ilen)--;
922 	c = *(*instr)++;
923 	c = l2u[c];
924 	c = unix2dos[c];
925 	return ((uint16_t)c);
926 }
927 
928 /*
929  * Convert Windows char to Local char
930  */
931 static uint16_t
932 win2unixchr(u_char *outbuf, uint16_t wc, struct msdosfsmount *pmp)
933 {
934 	u_char *inp, *outp, inbuf[3];
935 	size_t ilen, olen, len;
936 
937 	if (wc == 0)
938 		return (0);
939 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
940 		inbuf[0] = (u_char)(wc>>8);
941 		inbuf[1] = (u_char)wc;
942 		inbuf[2] = '\0';
943 		ilen = olen = len = 2;
944 		inp = inbuf;
945 		outp = outbuf;
946 		msdosfs_iconv->convchr(pmp->pm_w2u, __DECONST(const char **,
947 		    &inp), &ilen, (char **)&outp, &olen);
948 		len -= olen;
949 
950 		/*
951 		 * return '?' if failed to convert
952 		 */
953 		if (len == 0) {
954 			wc = '?';
955 			return (wc);
956 		}
957 		wc = 0;
958 		while(len--)
959 			wc |= (*(outp - len - 1) & 0xff) << (len << 3);
960 		return (wc);
961 	}
962 	if (wc & 0xff00)
963 		wc = '?';
964 	return (wc);
965 }
966 
967 /*
968  * Convert Local char to Windows char
969  */
970 static uint16_t
971 unix2winchr(const u_char **instr, size_t *ilen, int lower,
972     struct msdosfsmount *pmp)
973 {
974 	u_char *outp, outbuf[3];
975 	uint16_t wc;
976 	size_t olen;
977 
978 	if (*ilen == 0)
979 		return (0);
980 
981 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
982 		outp = outbuf;
983 		olen = 2;
984 		if (lower & (LCASE_BASE | LCASE_EXT))
985 			msdosfs_iconv->convchr_case(pmp->pm_u2w,
986 						    (const char **)instr,
987 						    ilen, (char **)&outp, &olen,
988 						    KICONV_FROM_LOWER);
989 		else
990 			msdosfs_iconv->convchr(pmp->pm_u2w,
991 					       (const char **)instr,
992 					       ilen, (char **)&outp, &olen);
993 		/*
994 		 * return '0' if end of filename
995 		 */
996 		if (olen == 2)
997 			return (0);
998 
999 		wc = (outbuf[0]<<8) | outbuf[1];
1000 		return (wc);
1001 	}
1002 
1003 	(*ilen)--;
1004 	wc = (*instr)[0];
1005 	if (lower & (LCASE_BASE | LCASE_EXT))
1006 		wc = u2l[wc];
1007 	(*instr)++;
1008 	return (wc);
1009 }
1010 
1011 /*
1012  * Initialize the temporary concatenation buffer.
1013  */
1014 void
1015 mbnambuf_init(struct mbnambuf *nbp)
1016 {
1017 	nbp->nb_len = 0;
1018 	nbp->nb_last_id = -1;
1019 	nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0';
1020 }
1021 
1022 /*
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.
1030  */
1031 int
1032 mbnambuf_write(struct mbnambuf *nbp, char *name, int id)
1033 {
1034 	char *slot;
1035 	size_t count, newlen;
1036 
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);
1040 		return (EINVAL);
1041 	}
1042 
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);
1050 	}
1051 
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);
1057 
1058 		memmove(slot + count, slot + WIN_CHARS, nbp->nb_len);
1059 	}
1060 
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;
1065 
1066 	return (0);
1067 }
1068 
1069 /*
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.
1075  */
1076 char *
1077 mbnambuf_flush(struct mbnambuf *nbp, struct dirent *dp)
1078 {
1079 	if (nbp->nb_len > sizeof(dp->d_name) - 1) {
1080 		mbnambuf_init(nbp);
1081 		return (NULL);
1082 	}
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;
1086 
1087 	mbnambuf_init(nbp);
1088 	return (dp->d_name);
1089 }
1090