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