1 /* $FreeBSD: src/sys/msdosfs/msdosfs_conv.c,v 1.29.2.1 2002/11/08 22:01:22 semenu Exp $ */ 2 /* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 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/time.h> 56 #include <sys/kernel.h> /* defines tz */ 57 #include <sys/systm.h> 58 #include <machine/clock.h> 59 #include <sys/dirent.h> 60 61 /* 62 * MSDOSFS include files. 63 */ 64 #include <msdosfs/direntry.h> 65 66 /* 67 * Total number of days that have passed for each month in a regular year. 68 */ 69 static u_short regyear[] = { 70 31, 59, 90, 120, 151, 181, 71 212, 243, 273, 304, 334, 365 72 }; 73 74 /* 75 * Total number of days that have passed for each month in a leap year. 76 */ 77 static u_short leapyear[] = { 78 31, 60, 91, 121, 152, 182, 79 213, 244, 274, 305, 335, 366 80 }; 81 82 /* 83 * Variables used to remember parts of the last time conversion. Maybe we 84 * can avoid a full conversion. 85 */ 86 static u_long lasttime; 87 static u_long lastday; 88 static u_short lastddate; 89 static u_short lastdtime; 90 91 static __inline u_int8_t find_lcode __P((u_int16_t code, u_int16_t *u2w)); 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(tsp, ddp, dtp, dhp) 99 struct timespec *tsp; 100 u_int16_t *ddp; 101 u_int16_t *dtp; 102 u_int8_t *dhp; 103 { 104 u_long t; 105 u_long days; 106 u_long inc; 107 u_long year; 108 u_long month; 109 u_short *months; 110 111 /* 112 * If the time from the last conversion is the same as now, then 113 * skip the computations and use the saved result. 114 */ 115 t = tsp->tv_sec - (tz.tz_minuteswest * 60) 116 - (wall_cmos_clock ? adjkerntz : 0); 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; days >= months[month]; month++) 141 ; 142 if (month > 0) 143 days -= months[month - 1]; 144 lastddate = ((days + 1) << DD_DAY_SHIFT) 145 + ((month + 1) << DD_MONTH_SHIFT); 146 /* 147 * Remember dos's idea of time is relative to 1980. 148 * unix's is relative to 1970. If somehow we get a 149 * time before 1980 then don't give totally crazy 150 * results. 151 */ 152 if (year > 1980) 153 lastddate += (year - 1980) << DD_YEAR_SHIFT; 154 } 155 } 156 if (dtp) 157 *dtp = lastdtime; 158 if (dhp) 159 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 160 161 *ddp = lastddate; 162 } 163 164 /* 165 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that 166 * interval there were 8 regular years and 2 leap years. 167 */ 168 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) 169 170 static u_short lastdosdate; 171 static u_long lastseconds; 172 173 /* 174 * Convert from dos' idea of time to unix'. This will probably only be 175 * called from the stat(), and fstat() system calls and so probably need 176 * not be too efficient. 177 */ 178 void 179 dos2unixtime(dd, dt, dh, tsp) 180 u_int dd; 181 u_int dt; 182 u_int dh; 183 struct timespec *tsp; 184 { 185 u_long seconds; 186 u_long month; 187 u_long year; 188 u_long days; 189 u_short *months; 190 191 if (dd == 0) { 192 /* 193 * Uninitialized field, return the epoch. 194 */ 195 tsp->tv_sec = 0; 196 tsp->tv_nsec = 0; 197 return; 198 } 199 seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1) 200 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 201 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 202 + dh / 100; 203 /* 204 * If the year, month, and day from the last conversion are the 205 * same then use the saved value. 206 */ 207 if (lastdosdate != dd) { 208 lastdosdate = dd; 209 days = 0; 210 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 211 days = year * 365; 212 days += year / 4 + 1; /* add in leap days */ 213 if ((year & 0x03) == 0) 214 days--; /* if year is a leap year */ 215 months = year & 0x03 ? regyear : leapyear; 216 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 217 if (month < 1 || month > 12) { 218 printf("dos2unixtime(): month value out of range (%ld)\n", 219 month); 220 month = 1; 221 } 222 if (month > 1) 223 days += months[month - 2]; 224 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 225 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 226 } 227 tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60) 228 + adjkerntz; 229 /* + daylight savings time correction */ 230 tsp->tv_nsec = (dh % 100) * 10000000; 231 } 232 233 /* 234 * 0 - character disallowed in long file name. 235 * 1 - character should be replaced by '_' in DOS file name, 236 * and generation number inserted. 237 * 2 - character ('.' and ' ') should be skipped in DOS file name, 238 * and generation number inserted. 239 */ 240 static u_char 241 unix2dos[256] = { 242 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 243 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 244 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 245 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 246 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 247 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */ 248 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 249 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */ 250 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 251 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 252 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 253 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */ 254 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 255 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 256 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 257 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 258 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 259 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 260 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 261 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 262 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 263 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 264 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 265 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 266 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 267 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 268 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 269 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 270 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 271 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 272 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 273 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 274 }; 275 276 static u_char 277 dos2unix[256] = { 278 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 279 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 280 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 281 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 282 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 283 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 284 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 285 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 286 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 287 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 288 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 289 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 290 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 291 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 292 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 293 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 294 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 295 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 296 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 297 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 298 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 299 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 300 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 301 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 302 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 303 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 304 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 305 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 306 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 307 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 308 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 309 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 310 }; 311 312 static u_char 313 u2l[256] = { 314 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 315 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 316 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 317 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 318 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 319 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 320 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 321 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 322 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 323 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 324 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 325 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 326 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 327 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 328 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 329 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 330 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 331 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 332 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 333 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 334 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 335 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 336 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 337 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 338 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 339 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 340 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 341 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 342 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 343 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 344 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 345 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 346 }; 347 348 static u_char 349 l2u[256] = { 350 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 351 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 352 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 353 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 354 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 355 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 356 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 357 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 358 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 359 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 360 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 361 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 362 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 363 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 364 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 365 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 366 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 367 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 368 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 369 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 370 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 371 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 372 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 373 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 374 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 375 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 376 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 377 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 378 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 379 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 380 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 381 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 382 }; 383 384 /* 385 * DOS filenames are made of 2 parts, the name part and the extension part. 386 * The name part is 8 characters long and the extension part is 3 387 * characters long. They may contain trailing blanks if the name or 388 * extension are not long enough to fill their respective fields. 389 */ 390 391 /* 392 * Convert a DOS filename to a unix filename. And, return the number of 393 * characters in the resulting unix filename excluding the terminating 394 * null. 395 */ 396 int 397 dos2unixfn(dn, un, lower, d2u_loaded, d2u, ul_loaded, ul) 398 u_char dn[11]; 399 u_char *un; 400 int lower; 401 int d2u_loaded; 402 u_int8_t *d2u; 403 int ul_loaded; 404 u_int8_t *ul; 405 { 406 int i; 407 int thislong = 1; 408 u_char c; 409 410 /* 411 * If first char of the filename is SLOT_E5 (0x05), then the real 412 * first char of the filename should be 0xe5. But, they couldn't 413 * just have a 0xe5 mean 0xe5 because that is used to mean a freed 414 * directory slot. Another dos quirk. 415 */ 416 if (*dn == SLOT_E5) 417 c = d2u_loaded ? d2u[0xe5 & 0x7f] : dos2unix[0xe5]; 418 else 419 c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] : 420 dos2unix[*dn]; 421 *un++ = (lower & LCASE_BASE) ? (ul_loaded && (c & 0x80) ? 422 ul[c & 0x7f] : u2l[c]) : c; 423 dn++; 424 425 /* 426 * Copy the name portion into the unix filename string. 427 */ 428 for (i = 1; i < 8 && *dn != ' '; i++) { 429 c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] : 430 dos2unix[*dn]; 431 dn++; 432 *un++ = (lower & LCASE_BASE) ? (ul_loaded && (c & 0x80) ? 433 ul[c & 0x7f] : u2l[c]) : c; 434 thislong++; 435 } 436 dn += 8 - i; 437 438 /* 439 * Now, if there is an extension then put in a period and copy in 440 * the extension. 441 */ 442 if (*dn != ' ') { 443 *un++ = '.'; 444 thislong++; 445 for (i = 0; i < 3 && *dn != ' '; i++) { 446 c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] : 447 dos2unix[*dn]; 448 dn++; 449 *un++ = (lower & LCASE_EXT) ? (ul_loaded && (c & 0x80) ? 450 ul[c & 0x7f] : u2l[c]) : c; 451 thislong++; 452 } 453 } 454 *un++ = 0; 455 456 return (thislong); 457 } 458 459 /* 460 * Convert a unix filename to a DOS filename according to Win95 rules. 461 * If applicable and gen is not 0, it is inserted into the converted 462 * filename as a generation number. 463 * Returns 464 * 0 if name couldn't be converted 465 * 1 if the converted name is the same as the original 466 * (no long filename entry necessary for Win95) 467 * 2 if conversion was successful 468 * 3 if conversion was successful and generation number was inserted 469 */ 470 int 471 unix2dosfn(un, dn, unlen, gen, u2d_loaded, u2d, lu_loaded, lu) 472 const u_char *un; 473 u_char dn[12]; 474 int unlen; 475 u_int gen; 476 int u2d_loaded; 477 u_int8_t *u2d; 478 int lu_loaded; 479 u_int8_t *lu; 480 { 481 int i, j, l; 482 int conv = 1; 483 const u_char *cp, *dp, *dp1; 484 u_char gentext[6], *wcp; 485 u_int8_t c; 486 #define U2D(c) (u2d_loaded && ((c) & 0x80) ? u2d[(c) & 0x7f] : unix2dos[c]) 487 488 /* 489 * Fill the dos filename string with blanks. These are DOS's pad 490 * characters. 491 */ 492 for (i = 0; i < 11; i++) 493 dn[i] = ' '; 494 dn[11] = 0; 495 496 /* 497 * The filenames "." and ".." are handled specially, since they 498 * don't follow dos filename rules. 499 */ 500 if (un[0] == '.' && unlen == 1) { 501 dn[0] = '.'; 502 return gen <= 1; 503 } 504 if (un[0] == '.' && un[1] == '.' && unlen == 2) { 505 dn[0] = '.'; 506 dn[1] = '.'; 507 return gen <= 1; 508 } 509 510 /* 511 * Filenames with only blanks and dots are not allowed! 512 */ 513 for (cp = un, i = unlen; --i >= 0; cp++) 514 if (*cp != ' ' && *cp != '.') 515 break; 516 if (i < 0) 517 return 0; 518 519 520 /* 521 * Filenames with some characters are not allowed! 522 */ 523 for (cp = un, i = unlen; --i >= 0; cp++) 524 if (U2D(*cp) == 0) 525 return 0; 526 527 /* 528 * Now find the extension 529 * Note: dot as first char doesn't start extension 530 * and trailing dots and blanks are ignored 531 */ 532 dp = dp1 = 0; 533 for (cp = un + 1, i = unlen - 1; --i >= 0;) { 534 switch (*cp++) { 535 case '.': 536 if (!dp1) 537 dp1 = cp; 538 break; 539 case ' ': 540 break; 541 default: 542 if (dp1) 543 dp = dp1; 544 dp1 = 0; 545 break; 546 } 547 } 548 549 /* 550 * Now convert it 551 */ 552 if (dp) { 553 if (dp1) 554 l = dp1 - dp; 555 else 556 l = unlen - (dp - un); 557 for (i = 0, j = 8; i < l && j < 11; i++, j++) { 558 c = dp[i]; 559 c = lu_loaded && (c & 0x80) ? 560 lu[c & 0x7f] : l2u[c]; 561 c = U2D(c); 562 if (dp[i] != (dn[j] = c) 563 && conv != 3) 564 conv = 2; 565 if (dn[j] == 1) { 566 conv = 3; 567 dn[j] = '_'; 568 } 569 if (dn[j] == 2) { 570 conv = 3; 571 dn[j--] = ' '; 572 } 573 } 574 if (i < l) 575 conv = 3; 576 dp--; 577 } else { 578 for (dp = cp; *--dp == ' ' || *dp == '.';); 579 dp++; 580 } 581 582 /* 583 * Now convert the rest of the name 584 */ 585 for (i = j = 0; un < dp && j < 8; i++, j++, un++) { 586 c = lu_loaded && (*un & 0x80) ? 587 lu[*un & 0x7f] : l2u[*un]; 588 c = U2D(c); 589 if (*un != (dn[j] = c) 590 && conv != 3) 591 conv = 2; 592 if (dn[j] == 1) { 593 conv = 3; 594 dn[j] = '_'; 595 } 596 if (dn[j] == 2) { 597 conv = 3; 598 dn[j--] = ' '; 599 } 600 } 601 if (un < dp) 602 conv = 3; 603 /* 604 * If we didn't have any chars in filename, 605 * generate a default 606 */ 607 if (!j) 608 dn[0] = '_'; 609 610 /* 611 * The first character cannot be E5, 612 * because that means a deleted entry 613 */ 614 if (dn[0] == 0xe5) 615 dn[0] = SLOT_E5; 616 617 /* 618 * If there wasn't any char dropped, 619 * there is no place for generation numbers 620 */ 621 if (conv != 3) { 622 if (gen > 1) 623 return 0; 624 return conv; 625 } 626 627 /* 628 * Now insert the generation number into the filename part 629 */ 630 if (gen == 0) 631 return conv; 632 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 633 *--wcp = gen % 10 + '0'; 634 if (gen) 635 return 0; 636 for (i = 8; dn[--i] == ' ';); 637 i++; 638 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 639 i = 8 - (gentext + sizeof(gentext) - wcp + 1); 640 dn[i++] = '~'; 641 while (wcp < gentext + sizeof(gentext)) 642 dn[i++] = *wcp++; 643 return 3; 644 #undef U2D 645 } 646 647 /* 648 * Create a Win95 long name directory entry 649 * Note: assumes that the filename is valid, 650 * i.e. doesn't consist solely of blanks and dots 651 */ 652 int 653 unix2winfn(un, unlen, wep, cnt, chksum, table_loaded, u2w) 654 const u_char *un; 655 int unlen; 656 struct winentry *wep; 657 int cnt; 658 int chksum; 659 int table_loaded; 660 u_int16_t *u2w; 661 { 662 const u_int8_t *cp; 663 u_int8_t *wcp; 664 int i; 665 u_int16_t code; 666 667 /* 668 * Drop trailing blanks and dots 669 */ 670 for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--); 671 672 un += (cnt - 1) * WIN_CHARS; 673 unlen -= (cnt - 1) * WIN_CHARS; 674 675 /* 676 * Initialize winentry to some useful default 677 */ 678 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 679 wep->weCnt = cnt; 680 wep->weAttributes = ATTR_WIN95; 681 wep->weReserved1 = 0; 682 wep->weChksum = chksum; 683 wep->weReserved2 = 0; 684 685 /* 686 * Now convert the filename parts 687 */ 688 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 689 if (--unlen < 0) 690 goto done; 691 if (table_loaded && (*un & 0x80)) { 692 code = u2w[*un++ & 0x7f]; 693 *wcp++ = code; 694 *wcp++ = code >> 8; 695 } else { 696 *wcp++ = *un++; 697 *wcp++ = 0; 698 } 699 } 700 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 701 if (--unlen < 0) 702 goto done; 703 if (table_loaded && (*un & 0x80)) { 704 code = u2w[*un++ & 0x7f]; 705 *wcp++ = code; 706 *wcp++ = code >> 8; 707 } else { 708 *wcp++ = *un++; 709 *wcp++ = 0; 710 } 711 } 712 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 713 if (--unlen < 0) 714 goto done; 715 if (table_loaded && (*un & 0x80)) { 716 code = u2w[*un++ & 0x7f]; 717 *wcp++ = code; 718 *wcp++ = code >> 8; 719 } else { 720 *wcp++ = *un++; 721 *wcp++ = 0; 722 } 723 } 724 if (!unlen) 725 wep->weCnt |= WIN_LAST; 726 return unlen; 727 728 done: 729 *wcp++ = 0; 730 *wcp++ = 0; 731 wep->weCnt |= WIN_LAST; 732 return 0; 733 } 734 735 static __inline u_int8_t 736 find_lcode(code, u2w) 737 u_int16_t code; 738 u_int16_t *u2w; 739 { 740 int i; 741 742 for (i = 0; i < 128; i++) 743 if (u2w[i] == code) 744 return (i | 0x80); 745 return '?'; 746 } 747 748 /* 749 * Compare our filename to the one in the Win95 entry 750 * Returns the checksum or -1 if no match 751 */ 752 int 753 winChkName(un, unlen, wep, chksum, u2w_loaded, u2w, ul_loaded, ul) 754 const u_char *un; 755 int unlen; 756 struct winentry *wep; 757 int chksum; 758 int u2w_loaded; 759 u_int16_t *u2w; 760 int ul_loaded; 761 u_int8_t *ul; 762 { 763 u_int8_t *cp; 764 int i; 765 u_int16_t code; 766 u_int8_t c1, c2; 767 768 /* 769 * First compare checksums 770 */ 771 if (wep->weCnt&WIN_LAST) 772 chksum = wep->weChksum; 773 else if (chksum != wep->weChksum) 774 chksum = -1; 775 if (chksum == -1) 776 return -1; 777 778 /* 779 * Offset of this entry 780 */ 781 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 782 un += i; 783 unlen -= i; 784 785 /* 786 * unlen being zero must not be treated as length missmatch. It is 787 * possible if the entry is WIN_LAST and contains nothing but the 788 * terminating 0. 789 */ 790 if (unlen < 0) 791 return -1; 792 if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS) 793 return -1; 794 795 /* 796 * Compare the name parts 797 */ 798 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 799 if (--unlen < 0) { 800 if (!*cp++ && !*cp) 801 return chksum; 802 return -1; 803 } 804 code = (cp[1] << 8) | cp[0]; 805 if (code & 0xff80) { 806 if (u2w_loaded) 807 code = find_lcode(code, u2w); 808 else if (code & 0xff00) 809 code = '?'; 810 } 811 c1 = ul_loaded && (code & 0x80) ? 812 ul[code & 0x7f] : u2l[code]; 813 c2 = ul_loaded && (*un & 0x80) ? 814 ul[*un & 0x7f] : u2l[*un]; 815 if (c1 != c2) 816 return -1; 817 cp += 2; 818 un++; 819 } 820 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 821 if (--unlen < 0) { 822 if (!*cp++ && !*cp) 823 return chksum; 824 return -1; 825 } 826 code = (cp[1] << 8) | cp[0]; 827 if (code & 0xff80) { 828 if (u2w_loaded) 829 code = find_lcode(code, u2w); 830 else if (code & 0xff00) 831 code = '?'; 832 } 833 c1 = ul_loaded && (code & 0x80) ? 834 ul[code & 0x7f] : u2l[code]; 835 c2 = ul_loaded && (*un & 0x80) ? 836 ul[*un & 0x7f] : u2l[*un]; 837 if (c1 != c2) 838 return -1; 839 cp += 2; 840 un++; 841 } 842 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 843 if (--unlen < 0) { 844 if (!*cp++ && !*cp) 845 return chksum; 846 return -1; 847 } 848 code = (cp[1] << 8) | cp[0]; 849 if (code & 0xff80) { 850 if (u2w_loaded) 851 code = find_lcode(code, u2w); 852 else if (code & 0xff00) 853 code = '?'; 854 } 855 c1 = ul_loaded && (code & 0x80) ? 856 ul[code & 0x7f] : u2l[code]; 857 c2 = ul_loaded && (*un & 0x80) ? 858 ul[*un & 0x7f] : u2l[*un]; 859 if (c1 != c2) 860 return -1; 861 cp += 2; 862 un++; 863 } 864 return chksum; 865 } 866 867 /* 868 * Convert Win95 filename to dirbuf. 869 * Returns the checksum or -1 if impossible 870 */ 871 int 872 win2unixfn(wep, dp, chksum, table_loaded, u2w) 873 struct winentry *wep; 874 struct dirent *dp; 875 int chksum; 876 int table_loaded; 877 u_int16_t *u2w; 878 { 879 u_int8_t *cp; 880 u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN; 881 u_int16_t code; 882 int i; 883 884 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 885 || !(wep->weCnt&WIN_CNT)) 886 return -1; 887 888 /* 889 * First compare checksums 890 */ 891 if (wep->weCnt&WIN_LAST) { 892 chksum = wep->weChksum; 893 /* 894 * This works even though d_namlen is one byte! 895 */ 896 dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS; 897 } else if (chksum != wep->weChksum) 898 chksum = -1; 899 if (chksum == -1) 900 return -1; 901 902 /* 903 * Offset of this entry 904 */ 905 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 906 np = (u_int8_t *)dp->d_name + i; 907 908 /* 909 * Convert the name parts 910 */ 911 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 912 code = (cp[1] << 8) | cp[0]; 913 switch (code) { 914 case 0: 915 *np = '\0'; 916 dp->d_namlen -= sizeof(wep->wePart2)/2 917 + sizeof(wep->wePart3)/2 + i + 1; 918 return chksum; 919 case '/': 920 *np = '\0'; 921 return -1; 922 default: 923 if (code & 0xff80) { 924 if (table_loaded) 925 code = find_lcode(code, u2w); 926 else if (code & 0xff00) 927 code = '?'; 928 } 929 *np++ = code; 930 break; 931 } 932 /* 933 * The size comparison should result in the compiler 934 * optimizing the whole if away 935 */ 936 if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2 937 && np > ep) { 938 np[-1] = 0; 939 return -1; 940 } 941 cp += 2; 942 } 943 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 944 code = (cp[1] << 8) | cp[0]; 945 switch (code) { 946 case 0: 947 *np = '\0'; 948 dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1; 949 return chksum; 950 case '/': 951 *np = '\0'; 952 return -1; 953 default: 954 if (code & 0xff80) { 955 if (table_loaded) 956 code = find_lcode(code, u2w); 957 else if (code & 0xff00) 958 code = '?'; 959 } 960 *np++ = code; 961 break; 962 } 963 /* 964 * The size comparisons should be optimized away 965 */ 966 if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2 967 && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 968 && np > ep) { 969 np[-1] = 0; 970 return -1; 971 } 972 cp += 2; 973 } 974 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 975 code = (cp[1] << 8) | cp[0]; 976 switch (code) { 977 case 0: 978 *np = '\0'; 979 dp->d_namlen -= i + 1; 980 return chksum; 981 case '/': 982 *np = '\0'; 983 return -1; 984 default: 985 if (code & 0xff80) { 986 if (table_loaded) 987 code = find_lcode(code, u2w); 988 else if (code & 0xff00) 989 code = '?'; 990 } 991 *np++ = code; 992 break; 993 } 994 /* 995 * See above 996 */ 997 if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 998 && np > ep) { 999 np[-1] = 0; 1000 return -1; 1001 } 1002 cp += 2; 1003 } 1004 return chksum; 1005 } 1006 1007 /* 1008 * Compute the checksum of a DOS filename for Win95 use 1009 */ 1010 u_int8_t 1011 winChksum(name) 1012 u_int8_t *name; 1013 { 1014 int i; 1015 u_int8_t s; 1016 1017 for (s = 0, i = 11; --i >= 0; s += *name++) 1018 s = (s << 7)|(s >> 1); 1019 return s; 1020 } 1021 1022 /* 1023 * Determine the number of slots necessary for Win95 names 1024 */ 1025 int 1026 winSlotCnt(un, unlen) 1027 const u_char *un; 1028 int unlen; 1029 { 1030 unlen = winLenFixup(un, unlen); 1031 if (unlen > WIN_MAXLEN) 1032 return 0; 1033 return howmany(unlen, WIN_CHARS); 1034 } 1035 1036 /* 1037 * Determine the number of bytes neccesary for Win95 names 1038 */ 1039 int 1040 winLenFixup(un, unlen) 1041 const u_char* un; 1042 int unlen; 1043 { 1044 for (un += unlen; unlen > 0; unlen--) 1045 if (*--un != ' ' && *un != '.') 1046 break; 1047 return unlen; 1048 } 1049