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