msdosfs_conv.c revision 1.17 1 1.17 nonaka /* $NetBSD: msdosfs_conv.c,v 1.17 2016/06/30 09:34:01 nonaka Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*-
4 1.1 jdolecek * Copyright (C) 1995, 1997 Wolfgang Solfrank.
5 1.1 jdolecek * Copyright (C) 1995, 1997 TooLs GmbH.
6 1.1 jdolecek * All rights reserved.
7 1.1 jdolecek * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 1.1 jdolecek *
9 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
10 1.1 jdolecek * modification, are permitted provided that the following conditions
11 1.1 jdolecek * are met:
12 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
13 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
14 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
16 1.1 jdolecek * documentation and/or other materials provided with the distribution.
17 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
18 1.1 jdolecek * must display the following acknowledgement:
19 1.1 jdolecek * This product includes software developed by TooLs GmbH.
20 1.1 jdolecek * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.1 jdolecek * derived from this software without specific prior written permission.
22 1.1 jdolecek *
23 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.1 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 jdolecek * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.1 jdolecek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.1 jdolecek * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.1 jdolecek * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.1 jdolecek * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.1 jdolecek * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.1 jdolecek * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 jdolecek */
34 1.1 jdolecek /*
35 1.1 jdolecek * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 1.1 jdolecek *
37 1.1 jdolecek * You can do anything you want with this software, just don't say you wrote
38 1.1 jdolecek * it, and don't remove this notice.
39 1.1 jdolecek *
40 1.1 jdolecek * This software is provided "as is".
41 1.1 jdolecek *
42 1.1 jdolecek * The author supplies this software to be publicly redistributed on the
43 1.1 jdolecek * understanding that the author is not responsible for the correct
44 1.1 jdolecek * functioning of this software in any circumstances and is not liable for
45 1.1 jdolecek * any damages caused by this software.
46 1.1 jdolecek *
47 1.1 jdolecek * October 1992
48 1.11 mlelstv *
49 1.1 jdolecek */
50 1.1 jdolecek
51 1.8 christos #if HAVE_NBTOOL_CONFIG_H
52 1.8 christos #include "nbtool_config.h"
53 1.8 christos #endif
54 1.8 christos
55 1.16 mlelstv #ifndef _KERNEL
56 1.16 mlelstv #include <assert.h>
57 1.16 mlelstv #define KASSERT(x) assert(x)
58 1.16 mlelstv #endif
59 1.16 mlelstv
60 1.1 jdolecek #include <sys/cdefs.h>
61 1.17 nonaka __KERNEL_RCSID(0, "$NetBSD: msdosfs_conv.c,v 1.17 2016/06/30 09:34:01 nonaka Exp $");
62 1.1 jdolecek
63 1.1 jdolecek /*
64 1.1 jdolecek * System include files.
65 1.1 jdolecek */
66 1.1 jdolecek #include <sys/param.h>
67 1.9 christos #include <sys/time.h>
68 1.11 mlelstv #include <sys/endian.h>
69 1.9 christos #ifdef _KERNEL
70 1.9 christos #include <sys/dirent.h>
71 1.1 jdolecek #include <sys/systm.h>
72 1.1 jdolecek #include <sys/kernel.h>
73 1.1 jdolecek #include <sys/vnode.h>
74 1.9 christos #else
75 1.8 christos #include <stdio.h>
76 1.10 martin #include <string.h>
77 1.9 christos #include <dirent.h>
78 1.9 christos #include <sys/queue.h>
79 1.8 christos #endif
80 1.10 martin #include <dev/clock_subr.h>
81 1.1 jdolecek
82 1.1 jdolecek /*
83 1.1 jdolecek * MSDOSFS include files.
84 1.1 jdolecek */
85 1.1 jdolecek #include <fs/msdosfs/direntry.h>
86 1.1 jdolecek #include <fs/msdosfs/denode.h>
87 1.1 jdolecek
88 1.11 mlelstv static int invalidname(const u_int16_t *, int);
89 1.11 mlelstv
90 1.11 mlelstv static int ucs2utf8(const u_int16_t *, u_int8_t *, int);
91 1.11 mlelstv static int utf8ucs2(const u_int8_t *, int, u_int16_t *);
92 1.11 mlelstv
93 1.11 mlelstv static int ucs2utf8str(const u_int16_t *, int, u_int8_t *, int);
94 1.11 mlelstv static int utf8ucs2str(const u_int8_t *, int, u_int16_t *, int);
95 1.11 mlelstv static int ucs2char8str(const u_int16_t *, int, u_int8_t *, int);
96 1.11 mlelstv static int char8ucs2str(const u_int8_t *, int, u_int16_t *, int);
97 1.11 mlelstv
98 1.11 mlelstv static void ucs2pad(u_int16_t *, int, int);
99 1.11 mlelstv
100 1.11 mlelstv static u_int16_t ucs2fold(u_int16_t);
101 1.11 mlelstv static int ucs2match(u_int16_t *, u_int16_t *, int n);
102 1.11 mlelstv static int char8match(u_int16_t *, u_int16_t *, int n);
103 1.11 mlelstv
104 1.1 jdolecek /*
105 1.10 martin * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
106 1.10 martin * interval there were 8 regular years and 2 leap years.
107 1.1 jdolecek */
108 1.10 martin #define DOSBIASYEAR 1980
109 1.10 martin #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
110 1.1 jdolecek /*
111 1.10 martin * msdos fs can not store dates beyound the year 2234
112 1.1 jdolecek */
113 1.10 martin #define DOSMAXYEAR ((DD_YEAR_MASK >> DD_YEAR_SHIFT) + DOSBIASYEAR)
114 1.1 jdolecek
115 1.1 jdolecek /*
116 1.1 jdolecek * Convert the unix version of time to dos's idea of time to be used in
117 1.1 jdolecek * file timestamps. The passed in unix time is assumed to be in GMT.
118 1.1 jdolecek */
119 1.1 jdolecek void
120 1.6 dsl unix2dostime(const struct timespec *tsp, int gmtoff, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp)
121 1.1 jdolecek {
122 1.1 jdolecek u_long t;
123 1.10 martin struct clock_ymdhms ymd;
124 1.10 martin
125 1.10 martin t = tsp->tv_sec + gmtoff; /* time zone correction */
126 1.1 jdolecek
127 1.1 jdolecek /*
128 1.10 martin * DOS timestamps can not represent dates before 1980.
129 1.10 martin */
130 1.10 martin if (t < SECONDSTO1980)
131 1.10 martin goto invalid_dos_date;
132 1.10 martin
133 1.10 martin /*
134 1.10 martin * DOS granularity is 2 seconds
135 1.1 jdolecek */
136 1.1 jdolecek t &= ~1;
137 1.10 martin
138 1.10 martin /*
139 1.10 martin * Convert to year/month/day/.. format
140 1.10 martin */
141 1.10 martin clock_secs_to_ymdhms(t, &ymd);
142 1.10 martin if (ymd.dt_year > DOSMAXYEAR)
143 1.10 martin goto invalid_dos_date;
144 1.10 martin
145 1.10 martin /*
146 1.10 martin * Now transform to DOS format
147 1.10 martin */
148 1.10 martin *ddp = (ymd.dt_day << DD_DAY_SHIFT)
149 1.10 martin + (ymd.dt_mon << DD_MONTH_SHIFT)
150 1.10 martin + ((ymd.dt_year - DOSBIASYEAR) << DD_YEAR_SHIFT);
151 1.10 martin if (dhp)
152 1.10 martin *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
153 1.10 martin if (dtp)
154 1.10 martin *dtp = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
155 1.1 jdolecek + (((t / 60) % 60) << DT_MINUTES_SHIFT)
156 1.1 jdolecek + (((t / 3600) % 24) << DT_HOURS_SHIFT);
157 1.10 martin return;
158 1.1 jdolecek
159 1.10 martin invalid_dos_date:
160 1.10 martin *ddp = 0;
161 1.1 jdolecek if (dtp)
162 1.10 martin *dtp = 0;
163 1.1 jdolecek if (dhp)
164 1.10 martin *dhp = 0;
165 1.1 jdolecek }
166 1.1 jdolecek
167 1.1 jdolecek /*
168 1.1 jdolecek * Convert from dos' idea of time to unix'. This will probably only be
169 1.1 jdolecek * called from the stat(), and fstat() system calls and so probably need
170 1.1 jdolecek * not be too efficient.
171 1.1 jdolecek */
172 1.1 jdolecek void
173 1.6 dsl dos2unixtime(u_int dd, u_int dt, u_int dh, int gmtoff, struct timespec *tsp)
174 1.1 jdolecek {
175 1.10 martin time_t seconds;
176 1.10 martin struct clock_ymdhms ymd;
177 1.1 jdolecek
178 1.1 jdolecek if (dd == 0) {
179 1.1 jdolecek /*
180 1.1 jdolecek * Uninitialized field, return the epoch.
181 1.1 jdolecek */
182 1.1 jdolecek tsp->tv_sec = 0;
183 1.1 jdolecek tsp->tv_nsec = 0;
184 1.1 jdolecek return;
185 1.1 jdolecek }
186 1.10 martin
187 1.10 martin memset(&ymd, 0, sizeof(ymd));
188 1.10 martin ymd.dt_year = ((dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT) + 1980 ;
189 1.10 martin ymd.dt_mon = ((dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT);
190 1.10 martin ymd.dt_day = ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT);
191 1.10 martin ymd.dt_hour = (dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT;
192 1.10 martin ymd.dt_min = (dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT;
193 1.10 martin ymd.dt_sec = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2;
194 1.10 martin
195 1.10 martin seconds = clock_ymdhms_to_secs(&ymd);
196 1.10 martin
197 1.10 martin tsp->tv_sec = seconds;
198 1.2 itojun tsp->tv_sec -= gmtoff; /* time zone correction */
199 1.1 jdolecek tsp->tv_nsec = (dh % 100) * 10000000;
200 1.1 jdolecek }
201 1.1 jdolecek
202 1.1 jdolecek static const u_char
203 1.1 jdolecek unix2dos[256] = {
204 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
205 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
206 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
207 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
208 1.1 jdolecek 0, '!', 0, '#', '$', '%', '&', '\'', /* 20-27 */
209 1.1 jdolecek '(', ')', 0, '+', 0, '-', 0, 0, /* 28-2f */
210 1.1 jdolecek '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
211 1.1 jdolecek '8', '9', 0, 0, 0, 0, 0, 0, /* 38-3f */
212 1.1 jdolecek '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */
213 1.1 jdolecek 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */
214 1.1 jdolecek 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */
215 1.1 jdolecek 'X', 'Y', 'Z', 0, 0, 0, '^', '_', /* 58-5f */
216 1.1 jdolecek '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 60-67 */
217 1.1 jdolecek 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 68-6f */
218 1.1 jdolecek 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 70-77 */
219 1.1 jdolecek 'X', 'Y', 'Z', '{', 0, '}', '~', 0, /* 78-7f */
220 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
221 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
222 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
223 1.1 jdolecek 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
224 1.1 jdolecek 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
225 1.1 jdolecek 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
226 1.1 jdolecek 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
227 1.1 jdolecek 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
228 1.1 jdolecek 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
229 1.1 jdolecek 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
230 1.1 jdolecek 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
231 1.1 jdolecek 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
232 1.1 jdolecek 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
233 1.1 jdolecek 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
234 1.1 jdolecek 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
235 1.1 jdolecek 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
236 1.1 jdolecek };
237 1.1 jdolecek
238 1.1 jdolecek static const u_char
239 1.1 jdolecek dos2unix[256] = {
240 1.1 jdolecek '?', '?', '?', '?', '?', '?', '?', '?', /* 00-07 */
241 1.1 jdolecek '?', '?', '?', '?', '?', '?', '?', '?', /* 08-0f */
242 1.1 jdolecek '?', '?', '?', '?', '?', '?', '?', '?', /* 10-17 */
243 1.1 jdolecek '?', '?', '?', '?', '?', '?', '?', '?', /* 18-1f */
244 1.1 jdolecek ' ', '!', '"', '#', '$', '%', '&', '\'', /* 20-27 */
245 1.1 jdolecek '(', ')', '*', '+', ',', '-', '.', '/', /* 28-2f */
246 1.1 jdolecek '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
247 1.1 jdolecek '8', '9', ':', ';', '<', '=', '>', '?', /* 38-3f */
248 1.1 jdolecek '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */
249 1.1 jdolecek 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */
250 1.1 jdolecek 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */
251 1.1 jdolecek 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', /* 58-5f */
252 1.1 jdolecek '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 60-67 */
253 1.1 jdolecek 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 68-6f */
254 1.1 jdolecek 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 70-77 */
255 1.1 jdolecek 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, /* 78-7f */
256 1.1 jdolecek 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
257 1.1 jdolecek 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
258 1.1 jdolecek 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
259 1.1 jdolecek 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, '?', /* 98-9f */
260 1.1 jdolecek 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
261 1.1 jdolecek 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
262 1.1 jdolecek '?', '?', '?', '?', '?', 0xc1, 0xc2, 0xc0, /* b0-b7 */
263 1.1 jdolecek 0xa9, '?', '?', '?', '?', 0xa2, 0xa5, '?', /* b8-bf */
264 1.1 jdolecek '?', '?', '?', '?', '?', '?', 0xe3, 0xc3, /* c0-c7 */
265 1.1 jdolecek '?', '?', '?', '?', '?', '?', '?', 0xa4, /* c8-cf */
266 1.1 jdolecek 0xf0, 0xd0, 0xca, 0xcb, 0xc8, '?', 0xcd, 0xce, /* d0-d7 */
267 1.1 jdolecek 0xcf, '?', '?', '?', '?', 0xa6, 0xcc, '?', /* d8-df */
268 1.1 jdolecek 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
269 1.1 jdolecek 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
270 1.1 jdolecek 0xad, 0xb1, '?', 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
271 1.1 jdolecek 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, '?', '?', /* f8-ff */
272 1.1 jdolecek };
273 1.1 jdolecek
274 1.1 jdolecek static const u_char
275 1.1 jdolecek u2l[256] = {
276 1.1 jdolecek 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
277 1.1 jdolecek 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
278 1.1 jdolecek 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
279 1.1 jdolecek 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
280 1.1 jdolecek ' ', '!', '"', '#', '$', '%', '&', '\'', /* 20-27 */
281 1.1 jdolecek '(', ')', '*', '+', ',', '-', '.', '/', /* 28-2f */
282 1.1 jdolecek '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
283 1.1 jdolecek '8', '9', ':', ';', '<', '=', '>', '?', /* 38-3f */
284 1.1 jdolecek '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 40-47 */
285 1.1 jdolecek 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 48-4f */
286 1.1 jdolecek 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 50-57 */
287 1.1 jdolecek 'x', 'y', 'z', '[', '\\', ']', '^', '_', /* 58-5f */
288 1.1 jdolecek '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 60-67 */
289 1.1 jdolecek 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 68-6f */
290 1.1 jdolecek 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 70-77 */
291 1.1 jdolecek 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, /* 78-7f */
292 1.1 jdolecek 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
293 1.1 jdolecek 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
294 1.1 jdolecek 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
295 1.1 jdolecek 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
296 1.1 jdolecek 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
297 1.1 jdolecek 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
298 1.1 jdolecek 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
299 1.1 jdolecek 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
300 1.1 jdolecek 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
301 1.1 jdolecek 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
302 1.1 jdolecek 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
303 1.1 jdolecek 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
304 1.1 jdolecek 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
305 1.1 jdolecek 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
306 1.1 jdolecek 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
307 1.1 jdolecek 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
308 1.1 jdolecek };
309 1.1 jdolecek
310 1.1 jdolecek /*
311 1.1 jdolecek * DOS filenames are made of 2 parts, the name part and the extension part.
312 1.1 jdolecek * The name part is 8 characters long and the extension part is 3
313 1.1 jdolecek * characters long. They may contain trailing blanks if the name or
314 1.1 jdolecek * extension are not long enough to fill their respective fields.
315 1.1 jdolecek */
316 1.1 jdolecek
317 1.1 jdolecek /*
318 1.1 jdolecek * Convert a DOS filename to a unix filename. And, return the number of
319 1.1 jdolecek * characters in the resulting unix filename excluding the terminating
320 1.1 jdolecek * null.
321 1.1 jdolecek */
322 1.1 jdolecek int
323 1.7 cegger dos2unixfn(u_char dn[11], u_char *un, int lower)
324 1.1 jdolecek {
325 1.1 jdolecek int i, j;
326 1.1 jdolecek int thislong = 1;
327 1.1 jdolecek u_char c;
328 1.1 jdolecek
329 1.1 jdolecek /*
330 1.1 jdolecek * If first char of the filename is SLOT_E5 (0x05), then the real
331 1.1 jdolecek * first char of the filename should be 0xe5. But, they couldn't
332 1.1 jdolecek * just have a 0xe5 mean 0xe5 because that is used to mean a freed
333 1.1 jdolecek * directory slot. Another dos quirk.
334 1.1 jdolecek */
335 1.1 jdolecek if (*dn == SLOT_E5)
336 1.1 jdolecek c = dos2unix[0xe5];
337 1.1 jdolecek else
338 1.1 jdolecek c = dos2unix[*dn];
339 1.1 jdolecek *un++ = lower ? u2l[c] : c;
340 1.1 jdolecek
341 1.1 jdolecek /*
342 1.1 jdolecek * Copy the rest into the unix filename string, ignoring
343 1.1 jdolecek * trailing blanks.
344 1.1 jdolecek */
345 1.1 jdolecek
346 1.1 jdolecek for (j=7; (j >= 0) && (dn[j] == ' '); j--)
347 1.1 jdolecek ;
348 1.1 jdolecek
349 1.1 jdolecek for (i = 1; i <= j; i++) {
350 1.1 jdolecek c = dos2unix[dn[i]];
351 1.1 jdolecek *un++ = lower ? u2l[c] : c;
352 1.1 jdolecek thislong++;
353 1.1 jdolecek }
354 1.1 jdolecek dn += 8;
355 1.3 perry
356 1.1 jdolecek /*
357 1.1 jdolecek * Now, if there is an extension then put in a period and copy in
358 1.1 jdolecek * the extension.
359 1.1 jdolecek */
360 1.1 jdolecek if (*dn != ' ') {
361 1.1 jdolecek *un++ = '.';
362 1.1 jdolecek thislong++;
363 1.1 jdolecek for (i = 0; i < 3 && *dn != ' '; i++) {
364 1.1 jdolecek c = dos2unix[*dn++];
365 1.1 jdolecek *un++ = lower ? u2l[c] : c;
366 1.1 jdolecek thislong++;
367 1.1 jdolecek }
368 1.1 jdolecek }
369 1.1 jdolecek *un++ = 0;
370 1.1 jdolecek
371 1.1 jdolecek return (thislong);
372 1.1 jdolecek }
373 1.1 jdolecek
374 1.1 jdolecek /*
375 1.1 jdolecek * Convert a unix filename to a DOS filename according to Win95 rules.
376 1.1 jdolecek * If applicable and gen is not 0, it is inserted into the converted
377 1.1 jdolecek * filename as a generation number.
378 1.1 jdolecek * Returns
379 1.1 jdolecek * 0 if name couldn't be converted
380 1.1 jdolecek * 1 if the converted name is the same as the original
381 1.1 jdolecek * (no long filename entry necessary for Win95)
382 1.1 jdolecek * 2 if conversion was successful
383 1.1 jdolecek * 3 if conversion was successful and generation number was inserted
384 1.1 jdolecek */
385 1.1 jdolecek int
386 1.7 cegger unix2dosfn(const u_char *un, u_char dn[12], int unlen, u_int gen)
387 1.1 jdolecek {
388 1.1 jdolecek int i, j, l;
389 1.1 jdolecek int conv = 1;
390 1.1 jdolecek const u_char *cp, *dp, *dp1;
391 1.1 jdolecek u_char gentext[6], *wcp;
392 1.1 jdolecek int shortlen;
393 1.1 jdolecek
394 1.1 jdolecek /*
395 1.1 jdolecek * Fill the dos filename string with blanks. These are DOS's pad
396 1.1 jdolecek * characters.
397 1.1 jdolecek */
398 1.1 jdolecek for (i = 0; i < 11; i++)
399 1.1 jdolecek dn[i] = ' ';
400 1.1 jdolecek dn[11] = 0;
401 1.1 jdolecek
402 1.1 jdolecek /*
403 1.1 jdolecek * The filenames "." and ".." are handled specially, since they
404 1.1 jdolecek * don't follow dos filename rules.
405 1.1 jdolecek */
406 1.1 jdolecek if (un[0] == '.' && unlen == 1) {
407 1.1 jdolecek dn[0] = '.';
408 1.1 jdolecek return gen <= 1;
409 1.1 jdolecek }
410 1.1 jdolecek if (un[0] == '.' && un[1] == '.' && unlen == 2) {
411 1.1 jdolecek dn[0] = '.';
412 1.1 jdolecek dn[1] = '.';
413 1.1 jdolecek return gen <= 1;
414 1.1 jdolecek }
415 1.1 jdolecek
416 1.1 jdolecek /*
417 1.1 jdolecek * Filenames with only blanks and dots are not allowed!
418 1.1 jdolecek */
419 1.1 jdolecek for (cp = un, i = unlen; --i >= 0; cp++)
420 1.1 jdolecek if (*cp != ' ' && *cp != '.')
421 1.1 jdolecek break;
422 1.1 jdolecek if (i < 0)
423 1.1 jdolecek return 0;
424 1.1 jdolecek
425 1.1 jdolecek /*
426 1.1 jdolecek * Now find the extension
427 1.1 jdolecek * Note: dot as first char doesn't start extension
428 1.1 jdolecek * and trailing dots and blanks are ignored
429 1.1 jdolecek */
430 1.1 jdolecek dp = dp1 = 0;
431 1.1 jdolecek for (cp = un + 1, i = unlen - 1; --i >= 0;) {
432 1.1 jdolecek switch (*cp++) {
433 1.1 jdolecek case '.':
434 1.1 jdolecek if (!dp1)
435 1.1 jdolecek dp1 = cp;
436 1.1 jdolecek break;
437 1.1 jdolecek case ' ':
438 1.1 jdolecek break;
439 1.1 jdolecek default:
440 1.1 jdolecek if (dp1)
441 1.1 jdolecek dp = dp1;
442 1.1 jdolecek dp1 = 0;
443 1.1 jdolecek break;
444 1.1 jdolecek }
445 1.1 jdolecek }
446 1.1 jdolecek
447 1.1 jdolecek /*
448 1.1 jdolecek * Now convert it
449 1.1 jdolecek */
450 1.1 jdolecek if (dp) {
451 1.1 jdolecek if (dp1)
452 1.1 jdolecek l = dp1 - dp;
453 1.1 jdolecek else
454 1.1 jdolecek l = unlen - (dp - un);
455 1.1 jdolecek for (i = 0, j = 8; i < l && j < 11; i++, j++) {
456 1.1 jdolecek if (dp[i] != (dn[j] = unix2dos[dp[i]])
457 1.1 jdolecek && conv != 3)
458 1.1 jdolecek conv = 2;
459 1.1 jdolecek if (!dn[j]) {
460 1.1 jdolecek conv = 3;
461 1.1 jdolecek dn[j--] = ' ';
462 1.1 jdolecek }
463 1.1 jdolecek }
464 1.1 jdolecek if (i < l)
465 1.1 jdolecek conv = 3;
466 1.1 jdolecek dp--;
467 1.1 jdolecek } else {
468 1.1 jdolecek for (dp = cp; *--dp == ' ' || *dp == '.';);
469 1.1 jdolecek dp++;
470 1.1 jdolecek }
471 1.1 jdolecek
472 1.1 jdolecek shortlen = (dp - un) <= 8;
473 1.1 jdolecek
474 1.1 jdolecek /*
475 1.1 jdolecek * Now convert the rest of the name
476 1.1 jdolecek */
477 1.1 jdolecek for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
478 1.1 jdolecek if ((*un == ' ') && shortlen)
479 1.1 jdolecek dn[j] = ' ';
480 1.1 jdolecek else
481 1.1 jdolecek dn[j] = unix2dos[*un];
482 1.1 jdolecek if ((*un != dn[j])
483 1.1 jdolecek && conv != 3)
484 1.1 jdolecek conv = 2;
485 1.1 jdolecek if (!dn[j]) {
486 1.1 jdolecek conv = 3;
487 1.1 jdolecek dn[j--] = ' ';
488 1.1 jdolecek }
489 1.1 jdolecek }
490 1.1 jdolecek if (un < dp)
491 1.1 jdolecek conv = 3;
492 1.1 jdolecek /*
493 1.1 jdolecek * If we didn't have any chars in filename,
494 1.1 jdolecek * generate a default
495 1.1 jdolecek */
496 1.1 jdolecek if (!j)
497 1.1 jdolecek dn[0] = '_';
498 1.1 jdolecek
499 1.1 jdolecek /*
500 1.1 jdolecek * The first character cannot be E5,
501 1.1 jdolecek * because that means a deleted entry
502 1.1 jdolecek */
503 1.1 jdolecek if (dn[0] == 0xe5)
504 1.1 jdolecek dn[0] = SLOT_E5;
505 1.1 jdolecek
506 1.1 jdolecek /*
507 1.1 jdolecek * If there wasn't any char dropped,
508 1.1 jdolecek * there is no place for generation numbers
509 1.1 jdolecek */
510 1.1 jdolecek if (conv != 3) {
511 1.1 jdolecek if (gen > 1)
512 1.1 jdolecek return 0;
513 1.1 jdolecek return conv;
514 1.1 jdolecek }
515 1.1 jdolecek
516 1.1 jdolecek /*
517 1.1 jdolecek * Now insert the generation number into the filename part
518 1.1 jdolecek */
519 1.1 jdolecek for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
520 1.1 jdolecek *--wcp = gen % 10 + '0';
521 1.1 jdolecek if (gen)
522 1.1 jdolecek return 0;
523 1.1 jdolecek for (i = 8; dn[--i] == ' ';);
524 1.1 jdolecek i++;
525 1.1 jdolecek if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
526 1.1 jdolecek i = 8 - (gentext + sizeof(gentext) - wcp + 1);
527 1.1 jdolecek dn[i++] = '~';
528 1.1 jdolecek while (wcp < gentext + sizeof(gentext))
529 1.1 jdolecek dn[i++] = *wcp++;
530 1.1 jdolecek return 3;
531 1.1 jdolecek }
532 1.1 jdolecek
533 1.1 jdolecek /*
534 1.1 jdolecek * Create a Win95 long name directory entry
535 1.1 jdolecek * Note: assumes that the filename is valid,
536 1.1 jdolecek * i.e. doesn't consist solely of blanks and dots
537 1.1 jdolecek */
538 1.1 jdolecek int
539 1.11 mlelstv unix2winfn(const u_char *un, int unlen, struct winentry *wep, int cnt, int chksum, int utf8)
540 1.1 jdolecek {
541 1.11 mlelstv u_int16_t wn[WIN_MAXLEN], *p;
542 1.11 mlelstv int i, len;
543 1.11 mlelstv const u_char *cp;
544 1.1 jdolecek
545 1.1 jdolecek /*
546 1.1 jdolecek * Drop trailing blanks and dots
547 1.1 jdolecek */
548 1.11 mlelstv for (cp = un + unlen; unlen > 0; unlen--)
549 1.11 mlelstv if (*--cp != ' ' && *cp != '.')
550 1.11 mlelstv break;
551 1.1 jdolecek
552 1.11 mlelstv /*
553 1.11 mlelstv * Offset of this entry
554 1.11 mlelstv */
555 1.11 mlelstv i = (cnt - 1) * WIN_CHARS;
556 1.11 mlelstv
557 1.11 mlelstv /*
558 1.11 mlelstv * Translate UNIX name to ucs-2
559 1.11 mlelstv */
560 1.11 mlelstv len = utf8 ? utf8ucs2str(un, unlen, wn, WIN_MAXLEN) : char8ucs2str(un, unlen, wn, WIN_MAXLEN);
561 1.11 mlelstv ucs2pad(wn, len, WIN_MAXLEN);
562 1.1 jdolecek
563 1.1 jdolecek /*
564 1.1 jdolecek * Initialize winentry to some useful default
565 1.1 jdolecek */
566 1.11 mlelstv memset(wep, 0xff, sizeof(*wep));
567 1.1 jdolecek wep->weCnt = cnt;
568 1.1 jdolecek wep->weAttributes = ATTR_WIN95;
569 1.1 jdolecek wep->weReserved1 = 0;
570 1.1 jdolecek wep->weChksum = chksum;
571 1.1 jdolecek wep->weReserved2 = 0;
572 1.1 jdolecek
573 1.1 jdolecek /*
574 1.11 mlelstv * Store name segment into directory entry
575 1.1 jdolecek */
576 1.11 mlelstv p = &wn[i];
577 1.11 mlelstv memcpy(wep->wePart1, p, sizeof(wep->wePart1));
578 1.11 mlelstv p += sizeof(wep->wePart1) / sizeof(*p);
579 1.11 mlelstv memcpy(wep->wePart2, p, sizeof(wep->wePart2));
580 1.11 mlelstv p += sizeof(wep->wePart2) / sizeof(*p);
581 1.11 mlelstv memcpy(wep->wePart3, p, sizeof(wep->wePart3));
582 1.11 mlelstv
583 1.11 mlelstv if (len > i + WIN_CHARS)
584 1.11 mlelstv return 1;
585 1.11 mlelstv
586 1.1 jdolecek wep->weCnt |= WIN_LAST;
587 1.1 jdolecek return 0;
588 1.1 jdolecek }
589 1.1 jdolecek
590 1.1 jdolecek /*
591 1.1 jdolecek * Compare our filename to the one in the Win95 entry
592 1.1 jdolecek * Returns the checksum or -1 if no match
593 1.1 jdolecek */
594 1.1 jdolecek int
595 1.11 mlelstv winChkName(const u_char *un, int unlen, struct winentry *wep, int chksum, int utf8)
596 1.1 jdolecek {
597 1.11 mlelstv u_int16_t wn[WIN_MAXLEN], *p;
598 1.11 mlelstv u_int16_t buf[WIN_CHARS];
599 1.11 mlelstv int i, len;
600 1.1 jdolecek
601 1.1 jdolecek /*
602 1.1 jdolecek * First compare checksums
603 1.1 jdolecek */
604 1.11 mlelstv if (wep->weCnt & WIN_LAST)
605 1.1 jdolecek chksum = wep->weChksum;
606 1.1 jdolecek else if (chksum != wep->weChksum)
607 1.1 jdolecek chksum = -1;
608 1.1 jdolecek if (chksum == -1)
609 1.1 jdolecek return -1;
610 1.1 jdolecek
611 1.1 jdolecek /*
612 1.1 jdolecek * Offset of this entry
613 1.1 jdolecek */
614 1.11 mlelstv i = ((wep->weCnt & WIN_CNT) - 1) * WIN_CHARS;
615 1.1 jdolecek
616 1.1 jdolecek /*
617 1.11 mlelstv * Translate UNIX name to ucs-2
618 1.11 mlelstv */
619 1.11 mlelstv len = utf8 ? utf8ucs2str(un, unlen, wn, WIN_MAXLEN) : char8ucs2str(un, unlen, wn, WIN_MAXLEN);
620 1.11 mlelstv ucs2pad(wn, len, WIN_MAXLEN);
621 1.1 jdolecek
622 1.11 mlelstv if (i >= len + 1)
623 1.1 jdolecek return -1;
624 1.17 nonaka if ((wep->weCnt & WIN_LAST) && (len - i > WIN_CHARS))
625 1.17 nonaka return -1;
626 1.1 jdolecek
627 1.1 jdolecek /*
628 1.11 mlelstv * Fetch name segment from directory entry
629 1.11 mlelstv */
630 1.11 mlelstv p = &buf[0];
631 1.11 mlelstv memcpy(p, wep->wePart1, sizeof(wep->wePart1));
632 1.11 mlelstv p += sizeof(wep->wePart1) / sizeof(*p);
633 1.11 mlelstv memcpy(p, wep->wePart2, sizeof(wep->wePart2));
634 1.11 mlelstv p += sizeof(wep->wePart2) / sizeof(*p);
635 1.11 mlelstv memcpy(p, wep->wePart3, sizeof(wep->wePart3));
636 1.11 mlelstv
637 1.11 mlelstv /*
638 1.11 mlelstv * And compare name segment
639 1.1 jdolecek */
640 1.11 mlelstv if (! (utf8 ? ucs2match(&wn[i], buf, WIN_CHARS) : char8match(&wn[i], buf, WIN_CHARS)))
641 1.11 mlelstv return -1;
642 1.11 mlelstv
643 1.1 jdolecek return chksum;
644 1.1 jdolecek }
645 1.1 jdolecek
646 1.1 jdolecek /*
647 1.1 jdolecek * Convert Win95 filename to dirbuf.
648 1.1 jdolecek * Returns the checksum or -1 if impossible
649 1.1 jdolecek */
650 1.1 jdolecek int
651 1.12 christos win2unixfn(struct winentry *wep, struct dirent *dp, int chksum,
652 1.12 christos uint16_t *namlen, int utf8)
653 1.1 jdolecek {
654 1.11 mlelstv u_int16_t wn[WIN_CHARS], *p;
655 1.11 mlelstv u_int8_t buf[WIN_CHARS*3];
656 1.11 mlelstv int len;
657 1.1 jdolecek
658 1.11 mlelstv if ((wep->weCnt & WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
659 1.11 mlelstv || !(wep->weCnt & WIN_CNT))
660 1.1 jdolecek return -1;
661 1.1 jdolecek
662 1.1 jdolecek /*
663 1.1 jdolecek * First compare checksums
664 1.1 jdolecek */
665 1.11 mlelstv if (wep->weCnt & WIN_LAST) {
666 1.1 jdolecek chksum = wep->weChksum;
667 1.12 christos *namlen = 0;
668 1.1 jdolecek } else if (chksum != wep->weChksum)
669 1.1 jdolecek chksum = -1;
670 1.1 jdolecek if (chksum == -1)
671 1.1 jdolecek return -1;
672 1.1 jdolecek
673 1.1 jdolecek /*
674 1.11 mlelstv * Fetch name segment from directory entry
675 1.11 mlelstv */
676 1.11 mlelstv p = &wn[0];
677 1.11 mlelstv memcpy(p, wep->wePart1, sizeof(wep->wePart1));
678 1.11 mlelstv p += sizeof(wep->wePart1) / sizeof(*p);
679 1.11 mlelstv memcpy(p, wep->wePart2, sizeof(wep->wePart2));
680 1.11 mlelstv p += sizeof(wep->wePart2) / sizeof(*p);
681 1.11 mlelstv memcpy(p, wep->wePart3, sizeof(wep->wePart3));
682 1.11 mlelstv
683 1.11 mlelstv /*
684 1.11 mlelstv * Don't allow slashes in UNIX names. Discard that entry.
685 1.1 jdolecek */
686 1.11 mlelstv if (invalidname(wn, WIN_CHARS))
687 1.11 mlelstv return -1;
688 1.1 jdolecek
689 1.1 jdolecek /*
690 1.11 mlelstv * Translate ucs-2 to UNIX name
691 1.1 jdolecek */
692 1.14 christos len = utf8 ? ucs2utf8str(wn, WIN_CHARS, buf, sizeof(buf))
693 1.14 christos : ucs2char8str(wn, WIN_CHARS, buf, sizeof(buf));
694 1.11 mlelstv
695 1.16 mlelstv KASSERT(len >= 0);
696 1.16 mlelstv KASSERT((size_t)len <= MIN(sizeof(buf), sizeof(dp->d_name)-1));
697 1.12 christos
698 1.11 mlelstv /*
699 1.11 mlelstv * Prepend name segment to directory entry
700 1.11 mlelstv *
701 1.11 mlelstv * This ignores the slot number from the windows entry but
702 1.11 mlelstv * assumes that segments are read in reverse order.
703 1.11 mlelstv *
704 1.11 mlelstv * The UCS-2 name (up to 255 chars) can overflow the UNIX
705 1.11 mlelstv * directory entry (up to 511 bytes). Trailing characters
706 1.11 mlelstv * are silently discarded. This could also end in multiple
707 1.11 mlelstv * files using the same (truncated) name.
708 1.11 mlelstv */
709 1.12 christos *namlen += len;
710 1.12 christos if (*namlen > sizeof(dp->d_name) - 1)
711 1.12 christos *namlen = sizeof(dp->d_name) - 1;
712 1.16 mlelstv
713 1.16 mlelstv KASSERT(*namlen >= len);
714 1.16 mlelstv
715 1.12 christos memmove(&dp->d_name[len], &dp->d_name[0], *namlen - len);
716 1.11 mlelstv memcpy(dp->d_name, buf, len);
717 1.11 mlelstv
718 1.1 jdolecek return chksum;
719 1.1 jdolecek }
720 1.1 jdolecek
721 1.1 jdolecek /*
722 1.1 jdolecek * Compute the checksum of a DOS filename for Win95 use
723 1.1 jdolecek */
724 1.1 jdolecek u_int8_t
725 1.6 dsl winChksum(u_int8_t *name)
726 1.1 jdolecek {
727 1.1 jdolecek int i;
728 1.1 jdolecek u_int8_t s;
729 1.1 jdolecek
730 1.1 jdolecek for (s = 0, i = 11; --i >= 0; s += *name++)
731 1.11 mlelstv s = (s << 7) | (s >> 1);
732 1.1 jdolecek return s;
733 1.1 jdolecek }
734 1.1 jdolecek
735 1.1 jdolecek /*
736 1.1 jdolecek * Determine the number of slots necessary for Win95 names
737 1.1 jdolecek */
738 1.1 jdolecek int
739 1.11 mlelstv winSlotCnt(const u_char *un, int unlen, int utf8)
740 1.1 jdolecek {
741 1.11 mlelstv const u_char *cp;
742 1.11 mlelstv int len;
743 1.11 mlelstv
744 1.11 mlelstv /*
745 1.11 mlelstv * Drop trailing blanks and dots
746 1.11 mlelstv */
747 1.11 mlelstv for (cp = un + unlen; unlen > 0; unlen--)
748 1.11 mlelstv if (*--cp != ' ' && *cp != '.')
749 1.1 jdolecek break;
750 1.11 mlelstv
751 1.11 mlelstv len = utf8 ? utf8ucs2str(un, unlen, NULL, WIN_MAXLEN) : unlen;
752 1.11 mlelstv
753 1.11 mlelstv return howmany(len, WIN_CHARS);
754 1.11 mlelstv }
755 1.11 mlelstv
756 1.11 mlelstv /*
757 1.11 mlelstv * Scan windows name for characters that must not
758 1.11 mlelstv * appear in a UNIX filename
759 1.11 mlelstv */
760 1.11 mlelstv static int
761 1.11 mlelstv invalidname(const u_int16_t *in, int n)
762 1.11 mlelstv {
763 1.11 mlelstv while (n-- > 0) {
764 1.11 mlelstv if (*in++ == '/')
765 1.11 mlelstv return 1;
766 1.11 mlelstv }
767 1.11 mlelstv
768 1.11 mlelstv return 0;
769 1.11 mlelstv }
770 1.11 mlelstv
771 1.11 mlelstv /*
772 1.11 mlelstv * Convert UCS-2 character into UTF-8
773 1.11 mlelstv * return number of output bytes or 0 if output
774 1.11 mlelstv * buffer is too short
775 1.11 mlelstv */
776 1.11 mlelstv static int
777 1.11 mlelstv ucs2utf8(const u_int16_t *in, u_int8_t *out, int n)
778 1.11 mlelstv {
779 1.11 mlelstv uint16_t inch = le16toh(in[0]);
780 1.11 mlelstv
781 1.11 mlelstv if (inch <= 0x007f) {
782 1.11 mlelstv if (n < 1) return 0;
783 1.11 mlelstv if (out)
784 1.11 mlelstv *out++ = inch;
785 1.11 mlelstv return 1;
786 1.11 mlelstv } else if (inch <= 0x07ff) {
787 1.11 mlelstv if (n < 2) return 0;
788 1.11 mlelstv if (out) {
789 1.11 mlelstv *out++ = 0xc0 | (inch >> 6);
790 1.11 mlelstv *out++ = 0x80 | (inch & 0x3f);
791 1.11 mlelstv }
792 1.11 mlelstv return 2;
793 1.11 mlelstv } else {
794 1.11 mlelstv if (n < 3) return 0;
795 1.11 mlelstv if (out) {
796 1.11 mlelstv *out++ = 0xe0 | (inch >> 12);
797 1.11 mlelstv *out++ = 0x80 | ((inch >> 6) & 0x3f);
798 1.11 mlelstv *out++ = 0x80 | (inch & 0x3f);
799 1.11 mlelstv }
800 1.11 mlelstv return 3;
801 1.11 mlelstv }
802 1.11 mlelstv }
803 1.11 mlelstv
804 1.11 mlelstv
805 1.11 mlelstv /*
806 1.11 mlelstv * Convert UTF-8 bytes into UCS-2 character
807 1.11 mlelstv * return number of input bytes, 0 if input
808 1.11 mlelstv * is too short and -1 if input is invalid
809 1.11 mlelstv */
810 1.11 mlelstv static int
811 1.11 mlelstv utf8ucs2(const u_int8_t *in, int n, u_int16_t *out)
812 1.11 mlelstv {
813 1.11 mlelstv uint16_t outch;
814 1.11 mlelstv
815 1.11 mlelstv if (n < 1) return 0;
816 1.11 mlelstv
817 1.11 mlelstv if (in[0] <= 0x7f) {
818 1.11 mlelstv outch = in[0];
819 1.11 mlelstv if (out)
820 1.11 mlelstv *out = htole16(outch);
821 1.11 mlelstv return 1;
822 1.11 mlelstv } else if (in[0] <= 0xdf) {
823 1.11 mlelstv if (n < 2) return 0;
824 1.11 mlelstv outch = (in[0] & 0x1f) << 6 | (in[1] & 0x3f);
825 1.11 mlelstv if (out)
826 1.11 mlelstv *out = htole16(outch);
827 1.11 mlelstv return 2;
828 1.11 mlelstv } else if (in[0] <= 0xef) {
829 1.11 mlelstv if (n < 3) return 0;
830 1.11 mlelstv outch = (in[0] & 0x1f) << 12 | (in[1] & 0x3f) << 6 | (in[2] & 0x3f);
831 1.11 mlelstv if (out)
832 1.11 mlelstv *out = htole16(outch);
833 1.11 mlelstv return 3;
834 1.11 mlelstv }
835 1.11 mlelstv
836 1.11 mlelstv return -1;
837 1.11 mlelstv }
838 1.11 mlelstv
839 1.11 mlelstv /*
840 1.11 mlelstv * Convert UCS-2 string into UTF-8 string
841 1.11 mlelstv * return total number of output bytes
842 1.11 mlelstv */
843 1.11 mlelstv static int
844 1.11 mlelstv ucs2utf8str(const u_int16_t *in, int n, u_int8_t *out, int m)
845 1.11 mlelstv {
846 1.11 mlelstv u_int8_t *p;
847 1.11 mlelstv int outlen;
848 1.11 mlelstv
849 1.11 mlelstv p = out;
850 1.11 mlelstv while (n > 0 && *in != 0) {
851 1.11 mlelstv outlen = ucs2utf8(in, out ? p : out, m);
852 1.11 mlelstv if (outlen == 0)
853 1.11 mlelstv break;
854 1.11 mlelstv p += outlen;
855 1.11 mlelstv m -= outlen;
856 1.11 mlelstv in += 1;
857 1.11 mlelstv n -= 1;
858 1.11 mlelstv }
859 1.11 mlelstv
860 1.11 mlelstv return p - out;
861 1.11 mlelstv }
862 1.11 mlelstv
863 1.11 mlelstv /*
864 1.11 mlelstv * Convert UTF8 string into UCS-2 string
865 1.11 mlelstv * return total number of output chacters
866 1.11 mlelstv */
867 1.11 mlelstv static int
868 1.11 mlelstv utf8ucs2str(const u_int8_t *in, int n, u_int16_t *out, int m)
869 1.11 mlelstv {
870 1.11 mlelstv u_int16_t *p;
871 1.11 mlelstv int inlen;
872 1.11 mlelstv
873 1.11 mlelstv p = out;
874 1.11 mlelstv while (n > 0 && *in != 0) {
875 1.11 mlelstv if (m < 1)
876 1.11 mlelstv break;
877 1.11 mlelstv inlen = utf8ucs2(in, n, out ? p : out);
878 1.11 mlelstv if (inlen <= 0)
879 1.11 mlelstv break;
880 1.11 mlelstv in += inlen;
881 1.11 mlelstv n -= inlen;
882 1.11 mlelstv p += 1;
883 1.11 mlelstv m -= 1;
884 1.11 mlelstv }
885 1.11 mlelstv
886 1.11 mlelstv return p - out;
887 1.11 mlelstv }
888 1.11 mlelstv
889 1.11 mlelstv /*
890 1.11 mlelstv * Convert UCS-2 string into 8bit character string
891 1.11 mlelstv * return total number of output bytes
892 1.11 mlelstv */
893 1.11 mlelstv static int
894 1.11 mlelstv ucs2char8str(const u_int16_t *in, int n, u_int8_t *out, int m)
895 1.11 mlelstv {
896 1.11 mlelstv u_int8_t *p;
897 1.11 mlelstv u_int16_t inch;
898 1.11 mlelstv
899 1.11 mlelstv p = out;
900 1.11 mlelstv while (n > 0 && in[0] != 0) {
901 1.11 mlelstv if (m < 1)
902 1.11 mlelstv break;
903 1.11 mlelstv inch = le16toh(in[0]);
904 1.11 mlelstv if (inch > 255)
905 1.11 mlelstv break;
906 1.11 mlelstv if (p)
907 1.11 mlelstv p[0] = inch;
908 1.11 mlelstv p += 1;
909 1.11 mlelstv m -= 1;
910 1.11 mlelstv in += 1;
911 1.11 mlelstv n -= 1;
912 1.11 mlelstv }
913 1.11 mlelstv
914 1.11 mlelstv return p - out;
915 1.11 mlelstv }
916 1.11 mlelstv
917 1.11 mlelstv /*
918 1.11 mlelstv * Convert 8bit character string into UCS-2 string
919 1.11 mlelstv * return total number of output chacters
920 1.11 mlelstv */
921 1.11 mlelstv static int
922 1.11 mlelstv char8ucs2str(const u_int8_t *in, int n, u_int16_t *out, int m)
923 1.11 mlelstv {
924 1.11 mlelstv u_int16_t *p;
925 1.11 mlelstv
926 1.11 mlelstv p = out;
927 1.11 mlelstv while (n > 0 && in[0] != 0) {
928 1.11 mlelstv if (m < 1)
929 1.11 mlelstv break;
930 1.11 mlelstv if (p)
931 1.11 mlelstv p[0] = htole16(in[0]);
932 1.11 mlelstv p += 1;
933 1.11 mlelstv m -= 1;
934 1.11 mlelstv in += 1;
935 1.11 mlelstv n -= 1;
936 1.11 mlelstv }
937 1.11 mlelstv
938 1.11 mlelstv return p - out;
939 1.11 mlelstv }
940 1.11 mlelstv
941 1.11 mlelstv static void
942 1.11 mlelstv ucs2pad(u_int16_t *buf, int len, int size)
943 1.11 mlelstv {
944 1.11 mlelstv
945 1.11 mlelstv if (len < size-1)
946 1.11 mlelstv buf[len++] = 0x0000;
947 1.11 mlelstv while (len < size)
948 1.11 mlelstv buf[len++] = 0xffff;
949 1.11 mlelstv }
950 1.11 mlelstv
951 1.11 mlelstv /*
952 1.11 mlelstv * Fold UCS-2 character to uppercase
953 1.11 mlelstv */
954 1.11 mlelstv static u_int16_t
955 1.11 mlelstv ucs2fold(u_int16_t w)
956 1.11 mlelstv {
957 1.11 mlelstv int low,high,mid;
958 1.11 mlelstv u_int16_t check;
959 1.15 mlelstv extern const u_int16_t msdosfs_unicode_foldmap[];
960 1.15 mlelstv extern size_t msdosfs_unicode_foldmap_entries;
961 1.11 mlelstv
962 1.11 mlelstv w = le16toh(w);
963 1.11 mlelstv
964 1.11 mlelstv low = 0;
965 1.15 mlelstv high = msdosfs_unicode_foldmap_entries / 2;
966 1.11 mlelstv while (low < high) {
967 1.11 mlelstv mid = (low + high)/2;
968 1.15 mlelstv check = msdosfs_unicode_foldmap[2*mid+0];
969 1.11 mlelstv
970 1.11 mlelstv if (w == check) {
971 1.15 mlelstv w = msdosfs_unicode_foldmap[2*mid+1];
972 1.11 mlelstv break;
973 1.11 mlelstv }
974 1.11 mlelstv
975 1.11 mlelstv if (w < check)
976 1.11 mlelstv high = mid;
977 1.11 mlelstv else
978 1.11 mlelstv low = mid+1;
979 1.11 mlelstv }
980 1.11 mlelstv
981 1.11 mlelstv w = le16toh(w);
982 1.11 mlelstv
983 1.11 mlelstv return w;
984 1.11 mlelstv }
985 1.11 mlelstv
986 1.11 mlelstv /*
987 1.11 mlelstv * Compare two UCS-2 strings case-insensitive
988 1.11 mlelstv *
989 1.11 mlelstv * uses the Unicode case folding table
990 1.11 mlelstv */
991 1.11 mlelstv static int
992 1.11 mlelstv ucs2match(u_int16_t *w1, u_int16_t *w2, int n)
993 1.11 mlelstv {
994 1.11 mlelstv u_int16_t u1, u2;
995 1.11 mlelstv
996 1.11 mlelstv while (n > 0) {
997 1.11 mlelstv if (*w1 == 0 || *w2 == 0)
998 1.11 mlelstv return *w1 == *w2;
999 1.11 mlelstv u1 = ucs2fold(*w1);
1000 1.11 mlelstv u2 = ucs2fold(*w2);
1001 1.11 mlelstv if (u1 != u2)
1002 1.11 mlelstv return 0;
1003 1.11 mlelstv ++w1;
1004 1.11 mlelstv ++w2;
1005 1.11 mlelstv --n;
1006 1.11 mlelstv }
1007 1.11 mlelstv
1008 1.11 mlelstv return 1;
1009 1.11 mlelstv }
1010 1.11 mlelstv
1011 1.11 mlelstv /*
1012 1.11 mlelstv * Compare two 8bit char conversions case-insensitive
1013 1.11 mlelstv *
1014 1.11 mlelstv * uses the DOS case folding table
1015 1.11 mlelstv */
1016 1.11 mlelstv static int
1017 1.11 mlelstv char8match(u_int16_t *w1, u_int16_t *w2, int n)
1018 1.11 mlelstv {
1019 1.11 mlelstv u_int16_t u1, u2;
1020 1.11 mlelstv
1021 1.11 mlelstv while (n > 0) {
1022 1.11 mlelstv u1 = le16toh(*w1);
1023 1.11 mlelstv u2 = le16toh(*w2);
1024 1.11 mlelstv if (u1 == 0 || u2 == 0)
1025 1.11 mlelstv return u1 == u2;
1026 1.11 mlelstv if (u1 > 255 || u2 > 255)
1027 1.11 mlelstv return 0;
1028 1.11 mlelstv u1 = u2l[u1 & 0xff];
1029 1.11 mlelstv u2 = u2l[u2 & 0xff];
1030 1.11 mlelstv if (u1 != u2)
1031 1.11 mlelstv return 0;
1032 1.11 mlelstv ++w1;
1033 1.11 mlelstv ++w2;
1034 1.11 mlelstv --n;
1035 1.11 mlelstv }
1036 1.11 mlelstv
1037 1.11 mlelstv return 1;
1038 1.1 jdolecek }
1039 1.11 mlelstv
1040