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