1 /* $NetBSD: msdosfs_conv.c,v 1.1 2002/12/26 12:31:34 jdolecek 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 #include <sys/cdefs.h> 51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_conv.c,v 1.1 2002/12/26 12:31:34 jdolecek Exp $"); 52 53 /* 54 * System include files. 55 */ 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/time.h> 59 #include <sys/kernel.h> 60 #include <sys/dirent.h> 61 #include <sys/vnode.h> 62 63 /* 64 * MSDOSFS include files. 65 */ 66 #include <fs/msdosfs/direntry.h> 67 #include <fs/msdosfs/denode.h> 68 69 /* 70 * Days in each month in a regular year. 71 */ 72 u_short const regyear[] = { 73 31, 28, 31, 30, 31, 30, 74 31, 31, 30, 31, 30, 31 75 }; 76 77 /* 78 * Days in each month in a leap year. 79 */ 80 u_short const leapyear[] = { 81 31, 29, 31, 30, 31, 30, 82 31, 31, 30, 31, 30, 31 83 }; 84 85 /* 86 * Variables used to remember parts of the last time conversion. Maybe we 87 * can avoid a full conversion. 88 */ 89 u_long lasttime; 90 u_long lastday; 91 u_short lastddate; 92 u_short lastdtime; 93 94 /* 95 * Convert the unix version of time to dos's idea of time to be used in 96 * file timestamps. The passed in unix time is assumed to be in GMT. 97 */ 98 void 99 unix2dostime(tsp, ddp, dtp, dhp) 100 struct timespec *tsp; 101 u_int16_t *ddp; 102 u_int16_t *dtp; 103 u_int8_t *dhp; 104 { 105 u_long t; 106 u_long days; 107 u_long inc; 108 u_long year; 109 u_long month; 110 const u_short *months; 111 112 /* 113 * If the time from the last conversion is the same as now, then 114 * skip the computations and use the saved result. 115 */ 116 t = tsp->tv_sec - (rtc_offset * 60) 117 /* +- daylight savings time correction */ ; 118 t &= ~1; 119 if (lasttime != t) { 120 lasttime = t; 121 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT) 122 + (((t / 60) % 60) << DT_MINUTES_SHIFT) 123 + (((t / 3600) % 24) << DT_HOURS_SHIFT); 124 125 /* 126 * If the number of days since 1970 is the same as the last 127 * time we did the computation then skip all this leap year 128 * and month stuff. 129 */ 130 days = t / (24 * 60 * 60); 131 if (days != lastday) { 132 lastday = days; 133 for (year = 1970;; year++) { 134 inc = year & 0x03 ? 365 : 366; 135 if (days < inc) 136 break; 137 days -= inc; 138 } 139 months = year & 0x03 ? regyear : leapyear; 140 for (month = 0; month < 12; month++) { 141 if (days < months[month]) 142 break; 143 days -= months[month]; 144 } 145 lastddate = ((days + 1) << DD_DAY_SHIFT) 146 + ((month + 1) << DD_MONTH_SHIFT); 147 /* 148 * Remember dos's idea of time is relative to 1980. 149 * unix's is relative to 1970. If somehow we get a 150 * time before 1980 then don't give totally crazy 151 * results. 152 */ 153 if (year > 1980) 154 lastddate += (year - 1980) << DD_YEAR_SHIFT; 155 } 156 } 157 if (dtp) 158 *dtp = lastdtime; 159 if (dhp) 160 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 161 162 *ddp = lastddate; 163 } 164 165 /* 166 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that 167 * interval there were 8 regular years and 2 leap years. 168 */ 169 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) 170 171 u_short lastdosdate; 172 u_long lastseconds; 173 174 /* 175 * Convert from dos' idea of time to unix'. This will probably only be 176 * called from the stat(), and fstat() system calls and so probably need 177 * not be too efficient. 178 */ 179 void 180 dos2unixtime(dd, dt, dh, tsp) 181 u_int dd; 182 u_int dt; 183 u_int dh; 184 struct timespec *tsp; 185 { 186 u_long seconds; 187 u_long m, month; 188 u_long y, year; 189 u_long days; 190 const u_short *months; 191 192 if (dd == 0) { 193 /* 194 * Uninitialized field, return the epoch. 195 */ 196 tsp->tv_sec = 0; 197 tsp->tv_nsec = 0; 198 return; 199 } 200 seconds = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2 201 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 202 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 203 + dh / 100; 204 /* 205 * If the year, month, and day from the last conversion are the 206 * same then use the saved value. 207 */ 208 if (lastdosdate != dd) { 209 lastdosdate = dd; 210 days = 0; 211 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 212 for (y = 0; y < year; y++) 213 days += y & 0x03 ? 365 : 366; 214 months = year & 0x03 ? regyear : leapyear; 215 /* 216 * Prevent going from 0 to 0xffffffff in the following 217 * loop. 218 */ 219 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 220 if (month == 0) { 221 printf("dos2unixtime(): month value out of range (%ld)\n", 222 month); 223 month = 1; 224 } 225 for (m = 0; m < month - 1; m++) 226 days += months[m]; 227 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 228 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 229 } 230 tsp->tv_sec = seconds + lastseconds + (rtc_offset * 60) 231 /* -+ daylight savings time correction */ ; 232 tsp->tv_nsec = (dh % 100) * 10000000; 233 } 234 235 static const u_char 236 unix2dos[256] = { 237 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 238 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 239 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 240 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 241 0, '!', 0, '#', '$', '%', '&', '\'', /* 20-27 */ 242 '(', ')', 0, '+', 0, '-', 0, 0, /* 28-2f */ 243 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */ 244 '8', '9', 0, 0, 0, 0, 0, 0, /* 38-3f */ 245 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */ 246 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */ 247 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */ 248 'X', 'Y', 'Z', 0, 0, 0, '^', '_', /* 58-5f */ 249 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 60-67 */ 250 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 68-6f */ 251 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 70-77 */ 252 'X', 'Y', 'Z', '{', 0, '}', '~', 0, /* 78-7f */ 253 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 254 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 255 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 256 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 257 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 258 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 259 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 260 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 261 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 262 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 263 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 264 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 265 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 266 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 267 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 268 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 269 }; 270 271 static const u_char 272 dos2unix[256] = { 273 '?', '?', '?', '?', '?', '?', '?', '?', /* 00-07 */ 274 '?', '?', '?', '?', '?', '?', '?', '?', /* 08-0f */ 275 '?', '?', '?', '?', '?', '?', '?', '?', /* 10-17 */ 276 '?', '?', '?', '?', '?', '?', '?', '?', /* 18-1f */ 277 ' ', '!', '"', '#', '$', '%', '&', '\'', /* 20-27 */ 278 '(', ')', '*', '+', ',', '-', '.', '/', /* 28-2f */ 279 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */ 280 '8', '9', ':', ';', '<', '=', '>', '?', /* 38-3f */ 281 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */ 282 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */ 283 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */ 284 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', /* 58-5f */ 285 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 60-67 */ 286 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 68-6f */ 287 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 70-77 */ 288 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, /* 78-7f */ 289 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 290 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 291 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 292 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, '?', /* 98-9f */ 293 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 294 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 295 '?', '?', '?', '?', '?', 0xc1, 0xc2, 0xc0, /* b0-b7 */ 296 0xa9, '?', '?', '?', '?', 0xa2, 0xa5, '?', /* b8-bf */ 297 '?', '?', '?', '?', '?', '?', 0xe3, 0xc3, /* c0-c7 */ 298 '?', '?', '?', '?', '?', '?', '?', 0xa4, /* c8-cf */ 299 0xf0, 0xd0, 0xca, 0xcb, 0xc8, '?', 0xcd, 0xce, /* d0-d7 */ 300 0xcf, '?', '?', '?', '?', 0xa6, 0xcc, '?', /* d8-df */ 301 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 302 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 303 0xad, 0xb1, '?', 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 304 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, '?', '?', /* f8-ff */ 305 }; 306 307 static const u_char 308 u2l[256] = { 309 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 310 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 311 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 312 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 313 ' ', '!', '"', '#', '$', '%', '&', '\'', /* 20-27 */ 314 '(', ')', '*', '+', ',', '-', '.', '/', /* 28-2f */ 315 '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */ 316 '8', '9', ':', ';', '<', '=', '>', '?', /* 38-3f */ 317 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 40-47 */ 318 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 48-4f */ 319 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 50-57 */ 320 'x', 'y', 'z', '[', '\\', ']', '^', '_', /* 58-5f */ 321 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 60-67 */ 322 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 68-6f */ 323 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 70-77 */ 324 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, /* 78-7f */ 325 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 326 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 327 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 328 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 329 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 330 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 331 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 332 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 333 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 334 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 335 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 336 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 337 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 338 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 339 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 340 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 341 }; 342 343 /* 344 * DOS filenames are made of 2 parts, the name part and the extension part. 345 * The name part is 8 characters long and the extension part is 3 346 * characters long. They may contain trailing blanks if the name or 347 * extension are not long enough to fill their respective fields. 348 */ 349 350 /* 351 * Convert a DOS filename to a unix filename. And, return the number of 352 * characters in the resulting unix filename excluding the terminating 353 * null. 354 */ 355 int 356 dos2unixfn(dn, un, lower) 357 u_char dn[11]; 358 u_char *un; 359 int lower; 360 { 361 int i, j; 362 int thislong = 1; 363 u_char c; 364 365 /* 366 * If first char of the filename is SLOT_E5 (0x05), then the real 367 * first char of the filename should be 0xe5. But, they couldn't 368 * just have a 0xe5 mean 0xe5 because that is used to mean a freed 369 * directory slot. Another dos quirk. 370 */ 371 if (*dn == SLOT_E5) 372 c = dos2unix[0xe5]; 373 else 374 c = dos2unix[*dn]; 375 *un++ = lower ? u2l[c] : c; 376 377 /* 378 * Copy the rest into the unix filename string, ignoring 379 * trailing blanks. 380 */ 381 382 for (j=7; (j >= 0) && (dn[j] == ' '); j--) 383 ; 384 385 for (i = 1; i <= j; i++) { 386 c = dos2unix[dn[i]]; 387 *un++ = lower ? u2l[c] : c; 388 thislong++; 389 } 390 dn += 8; 391 392 /* 393 * Now, if there is an extension then put in a period and copy in 394 * the extension. 395 */ 396 if (*dn != ' ') { 397 *un++ = '.'; 398 thislong++; 399 for (i = 0; i < 3 && *dn != ' '; i++) { 400 c = dos2unix[*dn++]; 401 *un++ = lower ? u2l[c] : c; 402 thislong++; 403 } 404 } 405 *un++ = 0; 406 407 return (thislong); 408 } 409 410 /* 411 * Convert a unix filename to a DOS filename according to Win95 rules. 412 * If applicable and gen is not 0, it is inserted into the converted 413 * filename as a generation number. 414 * Returns 415 * 0 if name couldn't be converted 416 * 1 if the converted name is the same as the original 417 * (no long filename entry necessary for Win95) 418 * 2 if conversion was successful 419 * 3 if conversion was successful and generation number was inserted 420 */ 421 int 422 unix2dosfn(un, dn, unlen, gen) 423 const u_char *un; 424 u_char dn[12]; 425 int unlen; 426 u_int gen; 427 { 428 int i, j, l; 429 int conv = 1; 430 const u_char *cp, *dp, *dp1; 431 u_char gentext[6], *wcp; 432 int shortlen; 433 434 /* 435 * Fill the dos filename string with blanks. These are DOS's pad 436 * characters. 437 */ 438 for (i = 0; i < 11; i++) 439 dn[i] = ' '; 440 dn[11] = 0; 441 442 /* 443 * The filenames "." and ".." are handled specially, since they 444 * don't follow dos filename rules. 445 */ 446 if (un[0] == '.' && unlen == 1) { 447 dn[0] = '.'; 448 return gen <= 1; 449 } 450 if (un[0] == '.' && un[1] == '.' && unlen == 2) { 451 dn[0] = '.'; 452 dn[1] = '.'; 453 return gen <= 1; 454 } 455 456 /* 457 * Filenames with only blanks and dots are not allowed! 458 */ 459 for (cp = un, i = unlen; --i >= 0; cp++) 460 if (*cp != ' ' && *cp != '.') 461 break; 462 if (i < 0) 463 return 0; 464 465 /* 466 * Now find the extension 467 * Note: dot as first char doesn't start extension 468 * and trailing dots and blanks are ignored 469 */ 470 dp = dp1 = 0; 471 for (cp = un + 1, i = unlen - 1; --i >= 0;) { 472 switch (*cp++) { 473 case '.': 474 if (!dp1) 475 dp1 = cp; 476 break; 477 case ' ': 478 break; 479 default: 480 if (dp1) 481 dp = dp1; 482 dp1 = 0; 483 break; 484 } 485 } 486 487 /* 488 * Now convert it 489 */ 490 if (dp) { 491 if (dp1) 492 l = dp1 - dp; 493 else 494 l = unlen - (dp - un); 495 for (i = 0, j = 8; i < l && j < 11; i++, j++) { 496 if (dp[i] != (dn[j] = unix2dos[dp[i]]) 497 && conv != 3) 498 conv = 2; 499 if (!dn[j]) { 500 conv = 3; 501 dn[j--] = ' '; 502 } 503 } 504 if (i < l) 505 conv = 3; 506 dp--; 507 } else { 508 for (dp = cp; *--dp == ' ' || *dp == '.';); 509 dp++; 510 } 511 512 shortlen = (dp - un) <= 8; 513 514 /* 515 * Now convert the rest of the name 516 */ 517 for (i = j = 0; un < dp && j < 8; i++, j++, un++) { 518 if ((*un == ' ') && shortlen) 519 dn[j] = ' '; 520 else 521 dn[j] = unix2dos[*un]; 522 if ((*un != dn[j]) 523 && conv != 3) 524 conv = 2; 525 if (!dn[j]) { 526 conv = 3; 527 dn[j--] = ' '; 528 } 529 } 530 if (un < dp) 531 conv = 3; 532 /* 533 * If we didn't have any chars in filename, 534 * generate a default 535 */ 536 if (!j) 537 dn[0] = '_'; 538 539 /* 540 * The first character cannot be E5, 541 * because that means a deleted entry 542 */ 543 if (dn[0] == 0xe5) 544 dn[0] = SLOT_E5; 545 546 /* 547 * If there wasn't any char dropped, 548 * there is no place for generation numbers 549 */ 550 if (conv != 3) { 551 if (gen > 1) 552 return 0; 553 return conv; 554 } 555 556 /* 557 * Now insert the generation number into the filename part 558 */ 559 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 560 *--wcp = gen % 10 + '0'; 561 if (gen) 562 return 0; 563 for (i = 8; dn[--i] == ' ';); 564 i++; 565 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 566 i = 8 - (gentext + sizeof(gentext) - wcp + 1); 567 dn[i++] = '~'; 568 while (wcp < gentext + sizeof(gentext)) 569 dn[i++] = *wcp++; 570 return 3; 571 } 572 573 /* 574 * Create a Win95 long name directory entry 575 * Note: assumes that the filename is valid, 576 * i.e. doesn't consist solely of blanks and dots 577 */ 578 int 579 unix2winfn(un, unlen, wep, cnt, chksum) 580 const u_char *un; 581 int unlen; 582 struct winentry *wep; 583 int cnt; 584 int chksum; 585 { 586 const u_int8_t *cp; 587 u_int8_t *wcp; 588 int i; 589 590 /* 591 * Drop trailing blanks and dots 592 */ 593 for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--); 594 595 un += (cnt - 1) * WIN_CHARS; 596 unlen -= (cnt - 1) * WIN_CHARS; 597 598 /* 599 * Initialize winentry to some useful default 600 */ 601 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 602 wep->weCnt = cnt; 603 wep->weAttributes = ATTR_WIN95; 604 wep->weReserved1 = 0; 605 wep->weChksum = chksum; 606 wep->weReserved2 = 0; 607 608 /* 609 * Now convert the filename parts 610 */ 611 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 612 if (--unlen < 0) 613 goto done; 614 *wcp++ = *un++; 615 *wcp++ = 0; 616 } 617 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 618 if (--unlen < 0) 619 goto done; 620 *wcp++ = *un++; 621 *wcp++ = 0; 622 } 623 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 624 if (--unlen < 0) 625 goto done; 626 *wcp++ = *un++; 627 *wcp++ = 0; 628 } 629 if (!unlen) 630 wep->weCnt |= WIN_LAST; 631 return unlen; 632 633 done: 634 *wcp++ = 0; 635 *wcp++ = 0; 636 wep->weCnt |= WIN_LAST; 637 return 0; 638 } 639 640 /* 641 * Compare our filename to the one in the Win95 entry 642 * Returns the checksum or -1 if no match 643 */ 644 int 645 winChkName(un, unlen, wep, chksum) 646 const u_char *un; 647 int unlen; 648 struct winentry *wep; 649 int chksum; 650 { 651 u_int8_t *cp; 652 int i; 653 654 /* 655 * First compare checksums 656 */ 657 if (wep->weCnt&WIN_LAST) 658 chksum = wep->weChksum; 659 else if (chksum != wep->weChksum) 660 chksum = -1; 661 if (chksum == -1) 662 return -1; 663 664 /* 665 * Offset of this entry 666 */ 667 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 668 un += i; 669 if ((unlen -= i) < 0) 670 return -1; 671 672 /* 673 * Ignore redundant winentries (those with only \0\0 on start in them). 674 * An appearance of such entry is a bug; unknown if in NetBSD msdosfs 675 * or MS Windows. 676 */ 677 if (unlen == 0) { 678 if (wep->wePart1[0] == '\0' && wep->wePart1[1] == '\0') 679 return chksum; 680 else 681 return -1; 682 } 683 684 if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS) 685 return -1; 686 687 /* 688 * Compare the name parts 689 */ 690 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 691 if (--unlen < 0) { 692 if (!*cp++ && !*cp) 693 return chksum; 694 return -1; 695 } 696 if (u2l[*cp++] != u2l[*un++] || *cp++) 697 return -1; 698 } 699 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 700 if (--unlen < 0) { 701 if (!*cp++ && !*cp) 702 return chksum; 703 return -1; 704 } 705 if (u2l[*cp++] != u2l[*un++] || *cp++) 706 return -1; 707 } 708 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 709 if (--unlen < 0) { 710 if (!*cp++ && !*cp) 711 return chksum; 712 return -1; 713 } 714 if (u2l[*cp++] != u2l[*un++] || *cp++) 715 return -1; 716 } 717 return chksum; 718 } 719 720 /* 721 * Convert Win95 filename to dirbuf. 722 * Returns the checksum or -1 if impossible 723 */ 724 int 725 win2unixfn(wep, dp, chksum) 726 struct winentry *wep; 727 struct dirent *dp; 728 int chksum; 729 { 730 u_int8_t *cp; 731 u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN; 732 int i; 733 734 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 735 || !(wep->weCnt&WIN_CNT)) 736 return -1; 737 738 /* 739 * First compare checksums 740 */ 741 if (wep->weCnt&WIN_LAST) { 742 chksum = wep->weChksum; 743 /* 744 * This works even though d_namlen is one byte! 745 */ 746 dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS; 747 } else if (chksum != wep->weChksum) 748 chksum = -1; 749 if (chksum == -1) 750 return -1; 751 752 /* 753 * Offset of this entry 754 */ 755 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 756 np = (u_int8_t *)dp->d_name + i; 757 758 /* 759 * Convert the name parts 760 */ 761 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 762 switch (*np++ = *cp++) { 763 case 0: 764 dp->d_namlen -= sizeof(wep->wePart2)/2 765 + sizeof(wep->wePart3)/2 + i + 1; 766 return chksum; 767 case '/': 768 np[-1] = 0; 769 return -1; 770 } 771 /* 772 * The size comparison should result in the compiler 773 * optimizing the whole if away 774 */ 775 if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2 776 && np > ep) { 777 np[-1] = 0; 778 return -1; 779 } 780 if (*cp++) 781 return -1; 782 } 783 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 784 switch (*np++ = *cp++) { 785 case 0: 786 dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1; 787 return chksum; 788 case '/': 789 np[-1] = 0; 790 return -1; 791 } 792 /* 793 * The size comparisons should be optimized away 794 */ 795 if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2 796 && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 797 && np > ep) { 798 np[-1] = 0; 799 return -1; 800 } 801 if (*cp++) 802 return -1; 803 } 804 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 805 switch (*np++ = *cp++) { 806 case 0: 807 dp->d_namlen -= i + 1; 808 return chksum; 809 case '/': 810 np[-1] = 0; 811 return -1; 812 } 813 /* 814 * See above 815 */ 816 if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 817 && np > ep) { 818 np[-1] = 0; 819 return -1; 820 } 821 if (*cp++) 822 return -1; 823 } 824 return chksum; 825 } 826 827 /* 828 * Compute the checksum of a DOS filename for Win95 use 829 */ 830 u_int8_t 831 winChksum(name) 832 u_int8_t *name; 833 { 834 int i; 835 u_int8_t s; 836 837 for (s = 0, i = 11; --i >= 0; s += *name++) 838 s = (s << 7)|(s >> 1); 839 return s; 840 } 841 842 /* 843 * Determine the number of slots necessary for Win95 names 844 */ 845 int 846 winSlotCnt(un, unlen) 847 const u_char *un; 848 int unlen; 849 { 850 for (un += unlen; unlen > 0; unlen--) 851 if (*--un != ' ' && *un != '.') 852 break; 853 if (unlen > WIN_MAXLEN) 854 return 0; 855 return howmany(unlen, WIN_CHARS); 856 } 857