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 <sys/iconv.h> 59 #include <machine/clock.h> 60 #include <sys/dirent.h> 61 #include <sys/mount.h> 62 /* 63 * MSDOSFS include files. 64 */ 65 #include "bpb.h" 66 #include "msdosfsmount.h" 67 #include "direntry.h" 68 69 extern struct iconv_functions *msdos_iconv; 70 71 /* 72 * Total number of days that have passed for each month in a regular year. 73 */ 74 static u_short regyear[] = { 75 31, 59, 90, 120, 151, 181, 76 212, 243, 273, 304, 334, 365 77 }; 78 79 /* 80 * Total number of days that have passed for each month in a leap year. 81 */ 82 static u_short leapyear[] = { 83 31, 60, 91, 121, 152, 182, 84 213, 244, 274, 305, 335, 366 85 }; 86 87 /* 88 * Variables used to remember parts of the last time conversion. Maybe we 89 * can avoid a full conversion. 90 */ 91 static u_long lasttime; 92 static u_long lastday; 93 static u_short lastddate; 94 static u_short lastdtime; 95 96 /* 97 * Convert the unix version of time to dos's idea of time to be used in 98 * file timestamps. The passed in unix time is assumed to be in GMT. 99 */ 100 void 101 unix2dostime(struct timespec *tsp, u_int16_t *ddp, 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 * Initialize the temporary concatenation buffer. 175 */ 176 void 177 mbnambuf_init(struct mbnambuf *nbp) 178 { 179 nbp->nb_len = 0; 180 nbp->nb_last_id = -1; 181 nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0'; 182 } 183 184 /* 185 * Fill out our concatenation buffer with the given substring, at the offset 186 * specified by its id. Since this function must be called with ids in 187 * descending order, we take advantage of the fact that ASCII substrings are 188 * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all 189 * previous (i.e. higher id) substrings upwards to make room for this one. 190 * This only penalizes portions of substrings that contain more than 191 * WIN_CHARS bytes when they are first encountered. 192 */ 193 void 194 mbnambuf_write(struct mbnambuf *nbp, char *name, int id) 195 { 196 char *slot; 197 size_t count, newlen; 198 199 if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) { 200 kprintf("msdosfs: non-decreasing id: id %d, last id %d\n", 201 id, nbp->nb_last_id); 202 return; 203 } 204 /* Will store this substring in a WIN_CHARS-aligned slot. */ 205 slot = &nbp->nb_buf[id * WIN_CHARS]; 206 count = strlen(name); 207 newlen = nbp->nb_len + count; 208 if (newlen > WIN_MAXLEN || newlen > 127) { 209 kprintf("msdosfs: file name length %zu too large\n", newlen); 210 return; 211 } 212 /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */ 213 if (count > WIN_CHARS && nbp->nb_len != 0) 214 bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len); 215 /* Copy in the substring to its slot and update length so far. */ 216 bcopy(name, slot, count); 217 nbp->nb_len = newlen; 218 nbp->nb_last_id = id; 219 } 220 221 /* 222 * Take the completed string and use it to setup the struct dirent. 223 * Be sure to always nul-terminate the d_name and then copy the string 224 * from our buffer. Note that this function assumes the full string has 225 * been reassembled in the buffer. If it's called before all substrings 226 * have been written via mbnambuf_write(), the result will be incorrect. 227 */ 228 char * 229 mbnambuf_flush(struct mbnambuf *nbp, char *d_name, u_int16_t *d_namlen) 230 { 231 #if 0 232 if (nbp->nb_len > 127) { 233 mbnambuf_init(nbp); 234 return (NULL); 235 } 236 #endif 237 bcopy(&nbp->nb_buf[0], d_name, nbp->nb_len); 238 d_name[nbp->nb_len] = '\0'; 239 *d_namlen = nbp->nb_len; 240 mbnambuf_init(nbp); 241 return (d_name); 242 } 243 244 /* 245 * Convert from dos' idea of time to unix'. This will probably only be 246 * called from the stat(), and fstat() system calls and so probably need 247 * not be too efficient. 248 */ 249 void 250 dos2unixtime(u_int dd, u_int dt, u_int dh, struct timespec *tsp) 251 { 252 u_long seconds; 253 u_long month; 254 u_long year; 255 u_long days; 256 u_short *months; 257 258 if (dd == 0) { 259 /* 260 * Uninitialized field, return the epoch. 261 */ 262 tsp->tv_sec = 0; 263 tsp->tv_nsec = 0; 264 return; 265 } 266 seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1) 267 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 268 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 269 + dh / 100; 270 /* 271 * If the year, month, and day from the last conversion are the 272 * same then use the saved value. 273 */ 274 if (lastdosdate != dd) { 275 lastdosdate = dd; 276 days = 0; 277 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 278 days = year * 365; 279 days += year / 4 + 1; /* add in leap days */ 280 if ((year & 0x03) == 0) 281 days--; /* if year is a leap year */ 282 months = year & 0x03 ? regyear : leapyear; 283 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 284 if (month < 1 || month > 12) { 285 kprintf("dos2unixtime(): month value out of range (%ld)\n", 286 month); 287 month = 1; 288 } 289 if (month > 1) 290 days += months[month - 2]; 291 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 292 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 293 } 294 tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60) 295 + adjkerntz; 296 /* + daylight savings time correction */ 297 tsp->tv_nsec = (dh % 100) * 10000000; 298 } 299 300 /* 301 * 0 - character disallowed in long file name. 302 * 1 - character should be replaced by '_' in DOS file name, 303 * and generation number inserted. 304 * 2 - character ('.' and ' ') should be skipped in DOS file name, 305 * and generation number inserted. 306 */ 307 static u_char 308 unix2dos[256] = { 309 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 310 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 311 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 312 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 313 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 314 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */ 315 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 316 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */ 317 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 318 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 319 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 320 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */ 321 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 322 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 323 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 324 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 325 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 326 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 327 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 328 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 329 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 330 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 331 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 332 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 333 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 334 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 335 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 336 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 337 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 338 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 339 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 340 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 341 }; 342 343 static u_char 344 dos2unix[256] = { 345 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 346 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 347 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 348 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 349 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 350 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 351 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 352 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 353 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 354 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 355 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 356 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 357 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 358 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 359 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 360 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 361 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 362 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 363 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 364 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 365 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 366 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 367 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 368 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 369 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 370 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 371 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 372 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 373 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 374 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 375 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 376 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 377 }; 378 379 static u_char 380 u2l[256] = { 381 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 382 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 383 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 384 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 385 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 386 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 387 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 388 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 389 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 390 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 391 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 392 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 393 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 394 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 395 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 396 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 397 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 398 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 399 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 400 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 401 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 402 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 403 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 404 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 405 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 406 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 407 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 408 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 409 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 410 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 411 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 412 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 413 }; 414 415 static u_char 416 l2u[256] = { 417 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 418 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 419 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 420 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 421 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 422 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 423 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 424 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 425 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 426 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 427 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 428 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 429 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 430 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 431 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 432 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 433 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 434 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 435 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 436 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 437 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 438 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 439 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 440 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 441 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 442 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 443 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 444 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 445 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 446 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 447 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 448 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 449 }; 450 451 /* 452 * Convert DOS char to Local char 453 */ 454 static u_int16_t 455 dos2unixchr(const u_char **instr, size_t *ilen, int lower, 456 struct msdosfsmount *pmp) 457 { 458 u_char c; 459 char *outp, outbuf[3]; 460 u_int16_t wc; 461 size_t len, olen; 462 463 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) { 464 olen = len = 2; 465 outp = outbuf; 466 if (lower & (LCASE_BASE | LCASE_EXT)) 467 msdos_iconv->convchr_case(pmp->pm_d2u, 468 (const char **)instr, ilen, 469 &outp, &olen, KICONV_LOWER); 470 else 471 msdos_iconv->convchr(pmp->pm_d2u, (const char **)instr, 472 ilen, &outp, &olen); 473 len -= olen; 474 /* 475 * return '?' if failed to convert 476 */ 477 if (len == 0) { 478 (*ilen)--; 479 (*instr)++; 480 return ('?'); 481 } 482 wc = 0; 483 while(len--) 484 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 485 return (wc); 486 } 487 (*ilen)--; 488 c = *(*instr)++; 489 c = dos2unix[c]; 490 if (lower & (LCASE_BASE | LCASE_EXT)) 491 c = u2l[c]; 492 return ((u_int16_t)c); 493 } 494 495 /* 496 * DOS filenames are made of 2 parts, the name part and the extension part. 497 * The name part is 8 characters long and the extension part is 3 498 * characters long. They may contain trailing blanks if the name or 499 * extension are not long enough to fill their respective fields. 500 */ 501 502 /* 503 * Convert a DOS filename to a unix filename. And, return the number of 504 * characters in the resulting unix filename excluding the terminating 505 * null. 506 */ 507 int 508 dos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp) 509 { 510 size_t i; 511 int thislong = 0; 512 u_char c; 513 514 /* 515 * If first char of the filename is SLOT_E5 (0x05), then the real 516 * first char of the filename should be 0xe5. But, they couldn't 517 * just have a 0xe5 mean 0xe5 because that is used to mean a freed 518 * directory slot. Another dos quirk. 519 */ 520 if (*dn == SLOT_E5) 521 *dn = 0xe5; 522 /* 523 * Copy the name portion into the unix filename string. 524 */ 525 for(i = 8; i > 0 && *dn != ' ';) { 526 c = dos2unixchr((const u_char **)(void *)&dn, 527 &i, lower & LCASE_BASE, pmp); 528 if (c & 0xff00) { 529 *un++ = c >> 8; 530 thislong++; 531 } 532 *un++ = c; 533 thislong++; 534 } 535 dn += i; 536 537 /* 538 * Now, if there is an extension then put in a period and copy in 539 * the extension. 540 */ 541 if (*dn != ' ') { 542 *un++ = '.'; 543 thislong++; 544 for (i = 3; i > 0 && *dn != ' ';) { 545 c = dos2unixchr((const u_char **)(void *)&dn, &i, 546 lower & LCASE_EXT, pmp); 547 if (c & 0xff00) { 548 *un++ = c >> 8; 549 thislong++; 550 } 551 *un++ = c; 552 thislong++; 553 } 554 } 555 *un++ = 0; 556 557 return (thislong); 558 } 559 560 /* 561 * Store an area with multi byte string instr, and reterns left 562 * byte of instr and moves pointer forward. The area's size is 563 * inlen or outlen. 564 */ 565 static int 566 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, 567 void *handle) 568 { 569 char *outp, outstr[outlen * weight + 1]; 570 571 bzero(outstr, outlen*weight+1); 572 if (flag & MSDOSFSMNT_KICONV && msdos_iconv) { 573 outp = outstr; 574 outlen *= weight; 575 msdos_iconv->conv(handle, instr, &inlen, &outp, &outlen); 576 return (inlen); 577 } 578 (*instr) += min(inlen, outlen); 579 return (inlen - min(inlen, outlen)); 580 } 581 582 /* 583 * Convert Local char to DOS char 584 */ 585 static u_int16_t 586 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp) 587 { 588 u_char c; 589 char *up, *outp, unicode[3], outbuf[3]; 590 u_int16_t wc; 591 size_t len, ucslen, unixlen, olen; 592 593 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) { 594 /* 595 * to hide an invisible character, using a unicode filter 596 */ 597 ucslen = 2; 598 len = *ilen; 599 up = unicode; 600 msdos_iconv->convchr(pmp->pm_u2w, (const char **)instr, 601 ilen, &up, &ucslen); 602 unixlen = len - *ilen; 603 604 /* 605 * cannot be converted 606 */ 607 if (unixlen == 0) { 608 (*ilen)--; 609 (*instr)++; 610 return (0); 611 } 612 /* 613 * return magic number for ascii char 614 */ 615 if (unixlen == 1) { 616 c = *(*instr -1); 617 if (! (c & 0x80)) { 618 c = unix2dos[c]; 619 if (c <= 2) 620 return (c); 621 } 622 } 623 /* 624 * now convert using libiconv 625 */ 626 *instr -= unixlen; 627 *ilen = len; 628 olen = len = 2; 629 outp = outbuf; 630 msdos_iconv->convchr_case(pmp->pm_u2d, (const char **)instr, 631 ilen, &outp, &olen, 632 KICONV_FROM_UPPER); 633 len -= olen; 634 /* 635 * cannot be converted, but has unicode char, should return magic number 636 */ 637 if (len == 0) { 638 (*ilen) -= unixlen; 639 (*instr) += unixlen; 640 return (1); 641 } 642 wc = 0; 643 while(len--) 644 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 645 return (wc); 646 } 647 (*ilen)--; 648 c = *(*instr)++; 649 c = l2u[c]; 650 c = unix2dos[c]; 651 return ((u_int16_t)c); 652 } 653 654 /* 655 * Convert a unix filename to a DOS filename according to Win95 rules. 656 * If applicable and gen is not 0, it is inserted into the converted 657 * filename as a generation number. 658 * Returns 659 * 0 if name couldn't be converted 660 * 1 if the converted name is the same as the original 661 * (no long filename entry necessary for Win95) 662 * 2 if conversion was successful 663 * 3 if conversion was successful and generation number was inserted 664 */ 665 666 int 667 unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen, 668 struct msdosfsmount *pmp) 669 { 670 ssize_t i, j; 671 int l; 672 int conv = 1; 673 const u_char *cp, *dp, *dp1; 674 u_char gentext[6], *wcp; 675 u_int16_t c; 676 677 /* 678 * Fill the dos filename string with blanks. These are DOS's pad 679 * characters. 680 */ 681 for (i = 0; i < 11; i++) 682 dn[i] = ' '; 683 dn[11] = 0; 684 /* 685 * The filenames "." and ".." are handled specially, since they 686 * don't follow dos filename rules. 687 */ 688 if (un[0] == '.' && unlen == 1) { 689 dn[0] = '.'; 690 return gen <= 1; 691 } 692 if (un[0] == '.' && un[1] == '.' && unlen == 2) { 693 dn[0] = '.'; 694 dn[1] = '.'; 695 return gen <= 1; 696 } 697 /* 698 * Filenames with only blanks and dots are not allowed! 699 */ 700 for (cp = un, i = unlen; --i >= 0; cp++) 701 if (*cp != ' ' && *cp != '.') 702 break; 703 if (i < 0) 704 return 0; 705 /* 706 * Filenames with some characters are not allowed! 707 */ 708 for (cp = un, i = unlen; i > 0;) 709 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0) 710 return 0; 711 /* 712 * Now find the extension 713 * Note: dot as first char doesn't start extension 714 * and trailing dots and blanks are ignored 715 * Note(2003/7): It seems recent Windows has 716 * defferent rule than this code, that Windows 717 * ignores all dots before extension, and use all 718 * chars as filename except for dots. 719 */ 720 dp = dp1 = NULL; 721 for (cp = un + 1, i = unlen - 1; --i >= 0;) { 722 switch (*cp++) { 723 case '.': 724 if (!dp1) 725 dp1 = cp; 726 break; 727 case ' ': 728 break; 729 default: 730 if (dp1) 731 dp = dp1; 732 dp1 = NULL; 733 break; 734 } 735 } 736 /* 737 * Now convert it (this part is for extension). 738 * As Windows XP do, if it's not ascii char, 739 * this function should return 2 or 3, so that checkng out Unicode name. 740 */ 741 if (dp) { 742 if (dp1) 743 l = dp1 - dp; 744 else 745 l = unlen - (dp - un); 746 for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) { 747 c = unix2doschr(&cp, (size_t *)&i, pmp); 748 if (c & 0xff00) { 749 dn[j] = c >> 8; 750 if (++j < 11) { 751 dn[j] = c; 752 if (conv != 3) 753 conv = 2; 754 continue; 755 } else { 756 conv = 3; 757 dn[j-1] = ' '; 758 break; 759 } 760 } else { 761 dn[j] = c; 762 } 763 if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3) 764 conv = 2; 765 if (dn[j] == 1) { 766 conv = 3; 767 dn[j] = '_'; 768 } 769 if (dn[j] == 2) { 770 conv = 3; 771 dn[j--] = ' '; 772 } 773 } 774 if (i > 0) 775 conv = 3; 776 dp--; 777 } else { 778 for (dp = cp; *--dp == ' ' || *dp == '.';); 779 dp++; 780 } 781 /* 782 * Now convert the rest of the name 783 */ 784 for (i = dp - un, j = 0; un < dp && j < 8; j++) { 785 c = unix2doschr(&un, &i, pmp); 786 if (c & 0xff00) { 787 dn[j] = c >> 8; 788 if (++j < 8) { 789 dn[j] = c; 790 if (conv != 3) 791 conv = 2; 792 continue; 793 } else { 794 conv = 3; 795 dn[j-1] = ' '; 796 break; 797 } 798 } else { 799 dn[j] = c; 800 } 801 if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3) 802 conv = 2; 803 if (dn[j] == 1) { 804 conv = 3; 805 dn[j] = '_'; 806 } 807 if (dn[j] == 2) { 808 conv = 3; 809 dn[j--] = ' '; 810 } 811 } 812 if (un < dp) 813 conv = 3; 814 /* 815 * If we didn't have any chars in filename, 816 * generate a default 817 */ 818 if (!j) 819 dn[0] = '_'; 820 /* 821 * If there wasn't any char dropped, 822 * there is no place for generation numbers 823 */ 824 if (conv != 3) { 825 if (gen > 1) 826 conv = 0; 827 goto done; 828 } 829 /* 830 * Now insert the generation number into the filename part 831 */ 832 if (gen == 0) 833 goto done; 834 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 835 *--wcp = gen % 10 + '0'; 836 if (gen) { 837 conv = 0; 838 goto done; 839 } 840 for (i = 8; dn[--i] == ' ';); 841 i++; 842 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 843 i = 8 - (gentext + sizeof(gentext) - wcp + 1); 844 /* 845 * Correct posision to where insert the generation number 846 */ 847 cp = dn; 848 i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, 849 pmp->pm_d2u); 850 dn[i++] = '~'; 851 while (wcp < gentext + sizeof(gentext)) 852 dn[i++] = *wcp++; 853 /* 854 * Tail of the filename should be space 855 */ 856 while (i < 8) 857 dn[i++] = ' '; 858 conv = 3; 859 done: 860 /* 861 * The first character cannot be E5, 862 * because that means a deleted entry 863 */ 864 if (dn[0] == 0xe5) 865 dn[0] = SLOT_E5; 866 return conv; 867 } 868 869 /* 870 * Convert Local char to Windows char 871 */ 872 static u_int16_t 873 unix2winchr(const u_char **instr, size_t *ilen, int lower, 874 struct msdosfsmount *pmp) 875 { 876 u_char *outp, outbuf[3]; 877 u_int16_t wc; 878 size_t olen; 879 880 if (*ilen == 0) 881 return (0); 882 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) { 883 outp = outbuf; 884 olen = 2; 885 if (lower & (LCASE_BASE | LCASE_EXT)) 886 msdos_iconv->convchr_case(pmp->pm_u2w, 887 (const char **)instr, 888 ilen, (char **)&outp, &olen, 889 KICONV_FROM_LOWER); 890 else 891 msdos_iconv->convchr(pmp->pm_u2w, (const char **)instr, 892 ilen, (char **)&outp, &olen); 893 /* 894 * return '0' if end of filename 895 */ 896 if (olen == 2) 897 return (0); 898 wc = (outbuf[0]<<8) | outbuf[1]; 899 return (wc); 900 } 901 (*ilen)--; 902 wc = (*instr)[0]; 903 if (lower & (LCASE_BASE | LCASE_EXT)) 904 wc = u2l[wc]; 905 (*instr)++; 906 return (wc); 907 } 908 909 /* 910 * Create a Win95 long name directory entry 911 * Note: assumes that the filename is valid, 912 * i.e. doesn't consist solely of blanks and dots 913 */ 914 int 915 unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt, 916 int chksum, struct msdosfsmount *pmp) 917 { 918 u_int8_t *wcp; 919 int i, end; 920 u_int16_t code; 921 922 /* 923 * Drop trailing blanks and dots 924 */ 925 unlen = winLenFixup(un, unlen); 926 927 /* 928 * Cut *un for this slot 929 */ 930 unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2, 931 pmp->pm_flags, pmp->pm_u2w); 932 933 /* 934 * Initialize winentry to some useful default 935 */ 936 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 937 wep->weCnt = cnt; 938 wep->weAttributes = ATTR_WIN95; 939 wep->weReserved1 = 0; 940 wep->weChksum = chksum; 941 wep->weReserved2 = 0; 942 943 /* 944 * Now convert the filename parts 945 */ 946 end = 0; 947 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) 948 { 949 code = unix2winchr(&un, &unlen, 0, pmp); 950 *wcp++ = code; 951 *wcp++ = code >> 8; 952 if (!code) 953 end = WIN_LAST; 954 } 955 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) 956 { 957 code = unix2winchr(&un, &unlen, 0, pmp); 958 *wcp++ = code; 959 *wcp++ = code >> 8; 960 if (!code) 961 end = WIN_LAST; 962 } 963 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) 964 { 965 code = unix2winchr(&un, &unlen, 0, pmp); 966 *wcp++ = code; 967 *wcp++ = code >> 8; 968 if (!code) 969 end = WIN_LAST; 970 } 971 if (*un == '\0') 972 end = WIN_LAST; 973 wep->weCnt |= end; 974 return !end; 975 } 976 977 static __inline u_int8_t 978 find_lcode(u_int16_t code, u_int16_t *u2w) 979 { 980 int i; 981 982 for (i = 0; i < 128; i++) 983 if (u2w[i] == code) 984 return (i | 0x80); 985 return '?'; 986 } 987 988 /* 989 * Compare our filename to the one in the Win95 entry 990 * Returns the checksum or -1 if no match 991 */ 992 int 993 winChkName(struct mbnambuf *nbp, const u_char *un, size_t unlen, int chksum, 994 struct msdosfsmount *pmp) 995 { 996 size_t len; 997 u_int16_t c1, c2; 998 u_char *np; 999 u_int16_t d_namlen; 1000 char d_name[127]; 1001 1002 bzero(d_name, 127); 1003 /* 1004 * We already have winentry in *nbp. 1005 */ 1006 if (!mbnambuf_flush(nbp, d_name, &d_namlen) || d_namlen == 0) 1007 return -1; 1008 mprintf("winChkName(): un=%s:%zd,d_name=%s:%d\n", 1009 un, unlen, d_name, d_namlen); 1010 /* 1011 * Compare the name parts 1012 */ 1013 len = d_namlen; 1014 if (unlen != len) 1015 return -2; 1016 for (np = d_name; unlen > 0 && len > 0;) { 1017 /* 1018 * Comparison must be case insensitive, because FAT disallows 1019 * to look up or create files in case sensitive even when 1020 * it's a long file name. 1021 */ 1022 c1 = unix2winchr((const u_char **)(void *)&np, 1023 &len, LCASE_BASE, pmp); 1024 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp); 1025 if (c1 != c2) 1026 return -2; 1027 } 1028 return chksum; 1029 } 1030 1031 /* 1032 * Convert Windows char to Local char 1033 */ 1034 static u_int16_t 1035 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp) 1036 { 1037 u_char *inp, *outp, inbuf[3], outbuf[3]; 1038 size_t ilen, olen, len; 1039 1040 if (wc == 0) 1041 return (0); 1042 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) { 1043 inbuf[0] = (u_char)(wc>>8); 1044 inbuf[1] = (u_char)wc; 1045 inbuf[2] = '\0'; 1046 ilen = olen = len = 2; 1047 inp = inbuf; 1048 outp = outbuf; 1049 msdos_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen, 1050 (char **)&outp, &olen); 1051 len -= olen; 1052 1053 /* 1054 * return '?' if failed to convert 1055 */ 1056 if (len == 0) { 1057 wc = '?'; 1058 return (wc); 1059 } 1060 wc = 0; 1061 while(len--) 1062 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 1063 return (wc); 1064 } 1065 if (wc & 0xff00) 1066 wc = '?'; 1067 return (wc); 1068 } 1069 1070 /* 1071 * Convert Win95 filename to dirbuf. 1072 * Returns the checksum or -1 if impossible 1073 */ 1074 int 1075 win2unixfn(struct mbnambuf *nbp, struct winentry *wep, int chksum, 1076 struct msdosfsmount *pmp) 1077 { 1078 u_int8_t *cp; 1079 u_int8_t *np, name[WIN_CHARS * 2 + 1]; 1080 u_int16_t code; 1081 int i; 1082 1083 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 1084 || !(wep->weCnt&WIN_CNT)) 1085 return -1; 1086 /* 1087 * First compare checksums 1088 */ 1089 if (wep->weCnt&WIN_LAST) { 1090 chksum = wep->weChksum; 1091 } else if (chksum != wep->weChksum) 1092 chksum = -1; 1093 if (chksum == -1) 1094 return -1; 1095 /* 1096 * Convert the name parts 1097 */ 1098 np = name; 1099 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 1100 code = (cp[1] << 8) | cp[0]; 1101 switch (code) { 1102 case 0: 1103 *np = '\0'; 1104 mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 1105 return chksum; 1106 case '/': 1107 *np = '\0'; 1108 return -1; 1109 default: 1110 code = win2unixchr(code, pmp); 1111 if (code & 0xff00) 1112 *np++ = code >> 8; 1113 *np++ = code; 1114 break; 1115 } 1116 cp += 2; 1117 } 1118 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 1119 code = (cp[1] << 8) | cp[0]; 1120 switch (code) { 1121 case 0: 1122 *np = '\0'; 1123 mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 1124 return chksum; 1125 case '/': 1126 *np = '\0'; 1127 return -1; 1128 default: 1129 code = win2unixchr(code, pmp); 1130 if (code & 0xff00) 1131 *np++ = code >> 8; 1132 *np++ = code; 1133 break; 1134 } 1135 cp += 2; 1136 } 1137 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 1138 code = (cp[1] << 8) | cp[0]; 1139 switch (code) { 1140 case 0: 1141 *np = '\0'; 1142 mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 1143 return chksum; 1144 case '/': 1145 *np = '\0'; 1146 return -1; 1147 default: 1148 code = win2unixchr(code, pmp); 1149 if (code & 0xff00) 1150 *np++ = code >> 8; 1151 *np++ = code; 1152 break; 1153 } 1154 cp += 2; 1155 } 1156 *np = '\0'; 1157 mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 1158 return chksum; 1159 } 1160 1161 /* 1162 * Compute the checksum of a DOS filename for Win95 use 1163 */ 1164 u_int8_t 1165 winChksum(u_int8_t *name) 1166 { 1167 int i; 1168 u_int8_t s; 1169 1170 for (s = 0, i = 11; --i >= 0; s += *name++) 1171 s = (s << 7)|(s >> 1); 1172 return s; 1173 } 1174 1175 /* 1176 * Determine the number of slots necessary for Win95 names 1177 */ 1178 int 1179 winSlotCnt(const u_char *un, size_t unlen, struct msdosfsmount *pmp) 1180 { 1181 size_t wlen; 1182 char wn[WIN_MAXLEN * 2 + 1], *wnp; 1183 1184 unlen = winLenFixup(un, unlen); 1185 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) { 1186 wlen = WIN_MAXLEN * 2; 1187 wnp = wn; 1188 msdos_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, 1189 &wlen); 1190 if (unlen > 0) 1191 return 0; 1192 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS); 1193 } 1194 if (unlen > WIN_MAXLEN) 1195 return 0; 1196 return howmany(unlen, WIN_CHARS); 1197 } 1198 1199 /* 1200 * Determine the number of bytes neccesary for Win95 names 1201 */ 1202 size_t 1203 winLenFixup(const u_char *un, size_t unlen) 1204 { 1205 for (un += unlen; unlen > 0; unlen--) 1206 if (*--un != ' ' && *un != '.') 1207 break; 1208 return unlen; 1209 } 1210