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