1 /* $OpenBSD: msdosfs_conv.c,v 1.18 2014/12/16 18:30:04 tedu Exp $ */ 2 /* $NetBSD: msdosfs_conv.c,v 1.24 1997/10/17 11:23:54 ws Exp $ */ 3 4 /*- 5 * Copyright (C) 1995, 1997 Wolfgang Solfrank. 6 * Copyright (C) 1995, 1997 TooLs GmbH. 7 * All rights reserved. 8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 /* 36 * Written by Paul Popelka (paulp@uts.amdahl.com) 37 * 38 * You can do anything you want with this software, just don't say you wrote 39 * it, and don't remove this notice. 40 * 41 * This software is provided "as is". 42 * 43 * The author supplies this software to be publicly redistributed on the 44 * understanding that the author is not responsible for the correct 45 * functioning of this software in any circumstances and is not liable for 46 * any damages caused by this software. 47 * 48 * October 1992 49 */ 50 51 /* 52 * System include files. 53 */ 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/time.h> 57 #include <sys/kernel.h> /* defines tz */ 58 #include <sys/dirent.h> 59 #include <sys/vnode.h> 60 #include <sys/lock.h> 61 62 /* 63 * MSDOSFS include files. 64 */ 65 #include <msdosfs/direntry.h> 66 #include <msdosfs/denode.h> 67 68 /* 69 * Days in each month in a regular year. 70 */ 71 const u_short regyear[] = { 72 31, 28, 31, 30, 31, 30, 73 31, 31, 30, 31, 30, 31 74 }; 75 76 /* 77 * Days in each month in a leap year. 78 */ 79 const u_short leapyear[] = { 80 31, 29, 31, 30, 31, 30, 81 31, 31, 30, 31, 30, 31 82 }; 83 84 /* 85 * Variables used to remember parts of the last time conversion. Maybe we 86 * can avoid a full conversion. 87 */ 88 time_t lasttime; 89 uint32_t lastday; 90 u_short lastddate; 91 u_short lastdtime; 92 93 /* 94 * Convert the unix version of time to dos's idea of time to be used in 95 * file timestamps. The passed in unix time is assumed to be in GMT. 96 */ 97 void 98 unix2dostime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp) 99 { 100 time_t t; 101 uint32_t days; 102 uint32_t inc; 103 uint32_t year; 104 uint32_t month; 105 const u_short *months; 106 107 /* 108 * If the time from the last conversion is the same as now, then 109 * skip the computations and use the saved result. 110 */ 111 t = tsp->tv_sec - (tz.tz_minuteswest * 60) 112 /* +- daylight saving time correction */ ; 113 t &= ~1; 114 /* 115 * Before 1/1/1980 there is only a timeless void. After 12/31/2107 116 * there is only Cthulhu. 117 */ 118 #define DOSEPOCH 315532800LL 119 #define DOSENDTIME 4354775999LL 120 if (t < DOSEPOCH || t > DOSENDTIME) 121 t = DOSEPOCH; 122 123 if (lasttime != t) { 124 lasttime = t; 125 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT) 126 + (((t / 60) % 60) << DT_MINUTES_SHIFT) 127 + (((t / 3600) % 24) << DT_HOURS_SHIFT); 128 129 /* 130 * If the number of days since 1970 is the same as the last 131 * time we did the computation then skip all this leap year 132 * and month stuff. 133 */ 134 days = t / (24 * 60 * 60); 135 if (days != lastday) { 136 lastday = days; 137 for (year = 1970;; year++) { 138 inc = year & 0x03 ? 365 : 366; 139 if (days < inc) 140 break; 141 days -= inc; 142 } 143 months = year & 0x03 ? regyear : leapyear; 144 for (month = 0; month < 12; month++) { 145 if (days < months[month]) 146 break; 147 days -= months[month]; 148 } 149 lastddate = ((days + 1) << DD_DAY_SHIFT) 150 + ((month + 1) << DD_MONTH_SHIFT); 151 /* 152 * Remember dos's idea of time is relative to 1980. 153 * unix's is relative to 1970. If somehow we get a 154 * time before 1980 then don't give totally crazy 155 * results. 156 */ 157 if (year > 1980) 158 lastddate += (year - 1980) << DD_YEAR_SHIFT; 159 } 160 } 161 162 if (dtp != NULL) 163 *dtp = lastdtime; 164 if (dhp != NULL) 165 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 166 167 *ddp = lastddate; 168 } 169 170 /* 171 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that 172 * interval there were 8 regular years and 2 leap years. 173 */ 174 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) 175 176 u_short lastdosdate; 177 uint32_t lastseconds; 178 179 /* 180 * Convert from dos' idea of time to unix'. This will probably only be 181 * called from the stat(), and fstat() system calls and so probably need 182 * not be too efficient. 183 */ 184 void 185 dos2unixtime(u_int dd, u_int dt, u_int dh, struct timespec *tsp) 186 { 187 uint32_t seconds; 188 uint32_t m, month; 189 uint32_t y, year; 190 uint32_t days; 191 const u_short *months; 192 193 if (dd == 0) { 194 /* 195 * Uninitialized field, return the epoch. 196 */ 197 tsp->tv_sec = 0; 198 tsp->tv_nsec = 0; 199 return; 200 } 201 seconds = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2 202 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 203 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 204 + dh / 100; 205 /* 206 * If the year, month, and day from the last conversion are the 207 * same then use the saved value. 208 */ 209 if (lastdosdate != dd) { 210 lastdosdate = dd; 211 days = 0; 212 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 213 for (y = 0; y < year; y++) 214 days += y & 0x03 ? 365 : 366; 215 months = year & 0x03 ? regyear : leapyear; 216 /* 217 * Prevent going from 0 to 0xffffffff in the following 218 * loop. 219 */ 220 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 221 if (month == 0) { 222 printf("dos2unixtime(): month value out of range (%u)\n", 223 month); 224 month = 1; 225 } 226 for (m = 0; m < month - 1; m++) 227 days += months[m]; 228 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 229 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 230 } 231 tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60) 232 /* -+ daylight saving time correction */ ; 233 tsp->tv_nsec = (dh % 100) * 10000000; 234 } 235 236 static const u_char 237 unix2dos[256] = { 238 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 239 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 240 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 241 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 242 0, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 243 0x28, 0x29, 0, 0, 0, 0x2d, 0, 0, /* 28-2f */ 244 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 245 0x38, 0x39, 0, 0, 0, 0, 0, 0, /* 38-3f */ 246 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 247 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 248 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 249 0x58, 0x59, 0x5a, 0, 0, 0, 0x5e, 0x5f, /* 58-5f */ 250 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 251 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 252 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 253 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 254 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 255 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 256 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 257 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 258 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 259 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 260 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 261 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 262 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 263 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 264 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 265 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 266 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 267 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 268 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 269 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 270 }; 271 272 static const u_char 273 dos2unix[256] = { 274 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 275 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 276 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 277 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 278 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 279 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 280 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 281 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 282 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 283 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 284 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 285 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 286 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 287 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 288 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 289 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 290 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 291 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 292 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 293 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 294 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 295 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 296 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 297 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 298 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 299 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 300 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 301 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 302 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 303 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 304 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 305 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 306 }; 307 308 static const u_char 309 u2l[256] = { 310 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 311 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 312 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 313 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 314 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 315 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 316 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 317 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 318 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 319 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 320 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 321 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 322 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 323 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 324 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 325 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 326 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 327 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 328 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 329 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 330 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 331 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 332 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 333 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 334 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 335 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 336 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 337 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 338 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 339 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 340 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 341 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 342 }; 343 344 /* 345 * DOS filenames are made of 2 parts, the name part and the extension part. 346 * The name part is 8 characters long and the extension part is 3 347 * characters long. They may contain trailing blanks if the name or 348 * extension are not long enough to fill their respective fields. 349 */ 350 351 /* 352 * Convert a DOS filename to a unix filename. And, return the number of 353 * characters in the resulting unix filename excluding the terminating 354 * null. 355 */ 356 int 357 dos2unixfn(u_char dn[11], u_char *un, int lower) 358 { 359 int i; 360 int thislong = 1; 361 u_char c; 362 363 /* 364 * If first char of the filename is SLOT_E5 (0x05), then the real 365 * first char of the filename should be 0xe5. But, they couldn't 366 * just have a 0xe5 mean 0xe5 because that is used to mean a freed 367 * directory slot. Another dos quirk. 368 */ 369 if (*dn == SLOT_E5) 370 c = dos2unix[0xe5]; 371 else 372 c = dos2unix[*dn]; 373 *un++ = lower ? u2l[c] : c; 374 dn++; 375 376 /* 377 * Copy the name portion into the unix filename string. 378 */ 379 for (i = 1; i < 8 && *dn != ' '; i++) { 380 c = dos2unix[*dn++]; 381 *un++ = lower ? u2l[c] : c; 382 thislong++; 383 } 384 dn += 8 - i; 385 386 /* 387 * Now, if there is an extension then put in a period and copy in 388 * the extension. 389 */ 390 if (*dn != ' ') { 391 *un++ = '.'; 392 thislong++; 393 for (i = 0; i < 3 && *dn != ' '; i++) { 394 c = dos2unix[*dn++]; 395 *un++ = lower ? u2l[c] : c; 396 thislong++; 397 } 398 } 399 *un++ = 0; 400 401 return (thislong); 402 } 403 404 /* 405 * Convert a unix filename to a DOS filename according to Win95 rules. 406 * If applicable and gen is not 0, it is inserted into the converted 407 * filename as a generation number. 408 * Returns 409 * 0 if name couldn't be converted 410 * 1 if the converted name is the same as the original 411 * (no long filename entry necessary for Win95) 412 * 2 if conversion was successful 413 * 3 if conversion was successful and generation number was inserted 414 */ 415 int 416 unix2dosfn(u_char *un, u_char dn[11], int unlen, u_int gen) 417 { 418 int i, j, l; 419 int conv = 1; 420 u_char *cp, *dp, *dp1; 421 u_char gentext[6]; 422 423 /* 424 * Fill the dos filename string with blanks. These are DOS's pad 425 * characters. 426 */ 427 for (i = 0; i < 11; i++) 428 dn[i] = ' '; 429 430 /* 431 * The filenames "." and ".." are handled specially, since they 432 * don't follow dos filename rules. 433 */ 434 if (un[0] == '.' && unlen == 1) { 435 dn[0] = '.'; 436 return gen <= 1; 437 } 438 if (un[0] == '.' && un[1] == '.' && unlen == 2) { 439 dn[0] = '.'; 440 dn[1] = '.'; 441 return gen <= 1; 442 } 443 444 /* 445 * Filenames with only blanks and dots are not allowed! 446 */ 447 for (cp = un, i = unlen; --i >= 0; cp++) 448 if (*cp != ' ' && *cp != '.') 449 break; 450 if (i < 0) 451 return 0; 452 453 /* 454 * Now find the extension 455 * Note: dot as first char doesn't start extension 456 * and trailing dots and blanks are ignored 457 */ 458 dp = dp1 = 0; 459 for (cp = un + 1, i = unlen - 1; --i >= 0;) { 460 switch (*cp++) { 461 case '.': 462 if (!dp1) 463 dp1 = cp; 464 break; 465 case ' ': 466 break; 467 default: 468 if (dp1) 469 dp = dp1; 470 dp1 = 0; 471 break; 472 } 473 } 474 475 /* 476 * Now convert it 477 */ 478 if (dp) { 479 if (dp1) 480 l = dp1 - dp; 481 else 482 l = unlen - (dp - un); 483 for (i = 0, j = 8; i < l && j < 11; i++, j++) { 484 if (dp[i] != (dn[j] = unix2dos[dp[i]]) 485 && conv != 3) 486 conv = 2; 487 if (!dn[j]) { 488 conv = 3; 489 dn[j--] = ' '; 490 } 491 } 492 if (i < l) 493 conv = 3; 494 dp--; 495 } else { 496 for (dp = cp; *--dp == ' ' || *dp == '.';); 497 dp++; 498 } 499 500 /* 501 * Now convert the rest of the name 502 */ 503 for (i = j = 0; un < dp && j < 8; i++, j++, un++) { 504 if (*un != (dn[j] = unix2dos[*un]) 505 && conv != 3) 506 conv = 2; 507 if (!dn[j]) { 508 conv = 3; 509 dn[j--] = ' '; 510 } 511 } 512 if (un < dp) 513 conv = 3; 514 /* 515 * If we didn't have any chars in filename, 516 * generate a default 517 */ 518 if (!j) 519 dn[0] = '_'; 520 521 /* 522 * The first character cannot be E5, 523 * because that means a deleted entry 524 */ 525 if (dn[0] == 0xe5) 526 dn[0] = SLOT_E5; 527 528 /* 529 * If there wasn't any char dropped, 530 * there is no place for generation numbers 531 */ 532 if (conv != 3) { 533 if (gen > 1) 534 return 0; 535 return conv; 536 } 537 538 /* 539 * Now insert the generation number into the filename part 540 */ 541 for (cp = gentext + sizeof(gentext); cp > gentext && gen; gen /= 10) 542 *--cp = gen % 10 + '0'; 543 if (gen) 544 return 0; 545 for (i = 8; dn[--i] == ' ';); 546 if (gentext + sizeof(gentext) - cp + 1 > 8 - i) 547 i = 8 - (gentext + sizeof(gentext) - cp + 1); 548 dn[i++] = '~'; 549 while (cp < gentext + sizeof(gentext)) 550 dn[i++] = *cp++; 551 return 3; 552 } 553 554 /* 555 * Create a Win95 long name directory entry 556 * Note: assumes that the filename is valid, 557 * i.e. doesn't consist solely of blanks and dots 558 */ 559 int 560 unix2winfn(u_char *un, int unlen, struct winentry *wep, int cnt, int chksum) 561 { 562 u_int8_t *cp; 563 int i; 564 565 /* 566 * Drop trailing blanks and dots 567 */ 568 for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--); 569 570 un += (cnt - 1) * WIN_CHARS; 571 unlen -= (cnt - 1) * WIN_CHARS; 572 573 /* 574 * Initialize winentry to some useful default 575 */ 576 for (cp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *cp++ = 0xff); 577 wep->weCnt = cnt; 578 wep->weAttributes = ATTR_WIN95; 579 wep->weReserved1 = 0; 580 wep->weChksum = chksum; 581 wep->weReserved2 = 0; 582 583 /* 584 * Now convert the filename parts 585 */ 586 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 587 if (--unlen < 0) 588 goto done; 589 *cp++ = *un++; 590 *cp++ = 0; 591 } 592 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 593 if (--unlen < 0) 594 goto done; 595 *cp++ = *un++; 596 *cp++ = 0; 597 } 598 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 599 if (--unlen < 0) 600 goto done; 601 *cp++ = *un++; 602 *cp++ = 0; 603 } 604 if (!unlen) 605 wep->weCnt |= WIN_LAST; 606 return unlen; 607 608 done: 609 *cp++ = 0; 610 *cp++ = 0; 611 wep->weCnt |= WIN_LAST; 612 return 0; 613 } 614 615 /* 616 * Compare our filename to the one in the Win95 entry 617 * Returns the checksum or -1 if no match 618 */ 619 int 620 winChkName(u_char *un, int unlen, struct winentry *wep, int chksum) 621 { 622 u_int8_t *cp; 623 int i; 624 625 /* 626 * First compare checksums 627 */ 628 if (wep->weCnt&WIN_LAST) 629 chksum = wep->weChksum; 630 else if (chksum != wep->weChksum) 631 chksum = -1; 632 if (chksum == -1) 633 return -1; 634 635 /* 636 * Offset of this entry 637 */ 638 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 639 if ((unlen -= i) <= 0) 640 return -1; 641 un += i; 642 643 if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS) 644 return -1; 645 646 /* 647 * Compare the name parts 648 */ 649 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 650 if (--unlen < 0) { 651 if (!*cp++ && !*cp) 652 return chksum; 653 return -1; 654 } 655 if (u2l[*cp++] != u2l[*un++] || *cp++) 656 return -1; 657 } 658 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 659 if (--unlen < 0) { 660 if (!*cp++ && !*cp) 661 return chksum; 662 return -1; 663 } 664 if (u2l[*cp++] != u2l[*un++] || *cp++) 665 return -1; 666 } 667 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 668 if (--unlen < 0) { 669 if (!*cp++ && !*cp) 670 return chksum; 671 return -1; 672 } 673 if (u2l[*cp++] != u2l[*un++] || *cp++) 674 return -1; 675 } 676 return chksum; 677 } 678 679 /* 680 * Convert Win95 filename to dirbuf. 681 * Returns the checksum or -1 if impossible 682 */ 683 int 684 win2unixfn(struct winentry *wep, struct dirent *dp, int chksum) 685 { 686 u_int8_t *cp; 687 u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN; 688 int i; 689 690 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 691 || !(wep->weCnt&WIN_CNT)) 692 return -1; 693 694 /* 695 * First compare checksums 696 */ 697 if (wep->weCnt&WIN_LAST) { 698 chksum = wep->weChksum; 699 /* 700 * This works even though d_namlen is one byte! 701 */ 702 dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS; 703 } else if (chksum != wep->weChksum) 704 chksum = -1; 705 if (chksum == -1) 706 return -1; 707 708 /* 709 * Offset of this entry 710 */ 711 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 712 np = (u_int8_t *)dp->d_name + i; 713 714 /* 715 * Convert the name parts 716 */ 717 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 718 switch (*np++ = *cp++) { 719 case 0: 720 dp->d_namlen -= sizeof(wep->wePart2)/2 721 + sizeof(wep->wePart3)/2 + i + 1; 722 return chksum; 723 case '/': 724 np[-1] = 0; 725 return -1; 726 } 727 /* 728 * The size comparison should result in the compiler 729 * optimizing the whole if away 730 */ 731 if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2 732 && np > ep) { 733 np[-1] = 0; 734 return -1; 735 } 736 if (*cp++) 737 return -1; 738 } 739 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 740 switch (*np++ = *cp++) { 741 case 0: 742 dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1; 743 return chksum; 744 case '/': 745 np[-1] = 0; 746 return -1; 747 } 748 /* 749 * The size comparisons should be optimized away 750 */ 751 if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2 752 && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 753 && np > ep) { 754 np[-1] = 0; 755 return -1; 756 } 757 if (*cp++) 758 return -1; 759 } 760 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 761 switch (*np++ = *cp++) { 762 case 0: 763 dp->d_namlen -= i + 1; 764 return chksum; 765 case '/': 766 np[-1] = 0; 767 return -1; 768 } 769 /* 770 * See above 771 */ 772 if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 773 && np > ep) { 774 np[-1] = 0; 775 return -1; 776 } 777 if (*cp++) 778 return -1; 779 } 780 return chksum; 781 } 782 783 /* 784 * Compute the checksum of a DOS filename for Win95 use 785 */ 786 u_int8_t 787 winChksum(u_int8_t *name) 788 { 789 int i; 790 u_int8_t s; 791 792 for (s = 0, i = 11; --i >= 0; s += *name++) 793 s = (s << 7)|(s >> 1); 794 return s; 795 } 796 797 /* 798 * Determine the number of slots necessary for Win95 names 799 */ 800 int 801 winSlotCnt(u_char *un, int unlen) 802 { 803 for (un += unlen; unlen > 0; unlen--) 804 if (*--un != ' ' && *un != '.') 805 break; 806 if (unlen > WIN_MAXLEN) 807 return 0; 808 return howmany(unlen, WIN_CHARS); 809 } 810