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