1/* 2 * Copyright (c) 2000-2008,2010-2012 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23/* $FreeBSD: src/sys/msdosfs/msdosfs_conv.c,v 1.29 1999/08/28 00:48:08 peter Exp $ */ 24/* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */ 25 26/*- 27 * Copyright (C) 1995, 1997 Wolfgang Solfrank. 28 * Copyright (C) 1995, 1997 TooLs GmbH. 29 * All rights reserved. 30 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 3. All advertising materials mentioning features or use of this software 41 * must display the following acknowledgement: 42 * This product includes software developed by TooLs GmbH. 43 * 4. The name of TooLs GmbH may not be used to endorse or promote products 44 * derived from this software without specific prior written permission. 45 * 46 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 52 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 54 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 55 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57/* 58 * Written by Paul Popelka (paulp@uts.amdahl.com) 59 * 60 * You can do anything you want with this software, just don't say you wrote 61 * it, and don't remove this notice. 62 * 63 * This software is provided "as is". 64 * 65 * The author supplies this software to be publicly redistributed on the 66 * understanding that the author is not responsible for the correct 67 * functioning of this software in any circumstances and is not liable for 68 * any damages caused by this software. 69 * 70 * October 1992 71 */ 72 73/* 74 * System include files. 75 */ 76#include <sys/param.h> 77#include <sys/time.h> 78#include <sys/systm.h> 79#include <sys/dirent.h> 80 81/* 82 * MSDOSFS include files. 83 */ 84#include "direntry.h" 85 86/* 87 * Total number of days that have passed for each month in a regular year. 88 */ 89static uint16_t regyear[] = { 90 31, 59, 90, 120, 151, 181, 91 212, 243, 273, 304, 334, 365 92}; 93 94/* 95 * Total number of days that have passed for each month in a leap year. 96 */ 97static uint16_t leapyear[] = { 98 31, 60, 91, 121, 152, 182, 99 213, 244, 274, 305, 335, 366 100}; 101 102/* 103 * Variables used to remember parts of the last time conversion. Maybe we 104 * can avoid a full conversion. 105 */ 106static uint32_t lasttime; 107static uint32_t lastday; 108static uint16_t lastddate; 109static uint16_t lastdtime; 110 111/* 112 * This variable contains the number of seconds that local time is west of GMT 113 * It is updated every time an msdosfs volume is mounted. This value is 114 * essentially tz_minuteswest * 60 - (tz_dsttime ? 3600 : 0) based on the 115 * timezone returned by gettimeofday() in the mount_msdosfs tool. 116 * 117 * This has a problem with daylight savings time. If the current daylight 118 * savings time setting does not match the date being converted, then it 119 * will be off by one hour. The only way to properly fix this is to know 120 * (inside the kernel) what dates should be adjusted for daylight savings, 121 * and which should not. That would let us return correct GMT for dates 122 * throughout the year. Of course, the GUI would have to do the opposite 123 * conversion when converting to local time for display to the user. 124 */ 125__private_extern__ int32_t msdos_secondsWest = 0; 126 127/* 128 * Convert the unix version of time to dos's idea of time to be used in 129 * file timestamps. The passed in unix time is assumed to be in GMT. 130 */ 131void msdosfs_unix2dostime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp) 132{ 133 uint32_t t; 134 uint32_t days; 135 uint32_t inc; 136 uint32_t year; 137 uint32_t month; 138 uint16_t *months; 139 140 /* 141 * If the time from the last conversion is the same as now, then 142 * skip the computations and use the saved result. 143 */ 144 t = (uint32_t)(tsp->tv_sec - msdos_secondsWest); 145 t &= ~1; /* Round down to multiple of 2 seconds */ 146 if (lasttime != t) { 147 lasttime = t; 148 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT) 149 + (((t / 60) % 60) << DT_MINUTES_SHIFT) 150 + (((t / 3600) % 24) << DT_HOURS_SHIFT); 151 152 /* 153 * If the number of days since 1970 is the same as the last 154 * time we did the computation then skip all this leap year 155 * and month stuff. 156 */ 157 days = t / (24 * 60 * 60); 158 if (days != lastday) { 159 lastday = days; 160 for (year = 1970;; year++) { 161 inc = year & 0x03 ? 365 : 366; 162 if (days < inc) 163 break; 164 days -= inc; 165 } 166 months = year & 0x03 ? regyear : leapyear; 167 for (month = 0; days >= months[month]; month++) 168 ; 169 if (month > 0) 170 days -= months[month - 1]; 171 lastddate = ((days + 1) << DD_DAY_SHIFT) 172 + ((month + 1) << DD_MONTH_SHIFT); 173 /* 174 * Remember dos's idea of time is relative to 1980. 175 * unix's is relative to 1970. If somehow we get a 176 * time before 1980 then don't give totally crazy 177 * results. 178 */ 179 if (year > 1980) 180 lastddate += (year - 1980) << DD_YEAR_SHIFT; 181 } 182 } 183 if (dtp) 184 *dtp = lastdtime; 185 if (dhp) 186 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 187 188 *ddp = lastddate; 189} 190 191/* 192 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that 193 * interval there were 8 regular years and 2 leap years. 194 */ 195#define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) 196 197static uint16_t lastdosdate; 198static uint32_t lastseconds; 199 200/* 201 * Convert from dos' idea of time to unix'. This will probably only be 202 * called from the stat(), and fstat() system calls and so probably need 203 * not be too efficient. 204 */ 205void msdosfs_dos2unixtime(u_int dd, u_int dt, u_int dh, struct timespec *tsp) 206{ 207 uint32_t seconds; 208 uint32_t month; 209 uint32_t year; 210 uint32_t days; 211 uint16_t *months; 212 213 if (dd == 0) { 214 /* 215 * Uninitialized field, return the epoch. 216 */ 217 tsp->tv_sec = 0; 218 tsp->tv_nsec = 0; 219 return; 220 } 221 seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1) 222 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 223 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 224 + dh / 100; 225 /* 226 * If the year, month, and day from the last conversion are the 227 * same then use the saved value. 228 */ 229 if (lastdosdate != dd) { 230 lastdosdate = dd; 231 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 232 days = year * 365; 233 days += year / 4 + 1; /* add in leap days */ 234 if ((year & 0x03) == 0) 235 days--; /* if year is a leap year */ 236 months = year & 0x03 ? regyear : leapyear; 237 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 238 if (month < 1 || month > 12) { 239 month = 1; 240 } 241 if (month > 1) 242 days += months[month - 2]; 243 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 244 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 245 } 246 tsp->tv_sec = seconds + lastseconds + msdos_secondsWest; 247 tsp->tv_nsec = (dh % 100) * 10000000; 248} 249 250/* 251 * Unicode (LSB) to Win Latin1 (ANSI CodePage 1252). 252 * Also converts ASCII to upper case. 253 * 254 * 0 - character disallowed in long file name. 255 * 1 - character should be replaced by '_' in DOS file name, 256 * and generation number inserted. 257 * 2 - character ('.' and ' ') should be skipped in DOS file name, 258 * and generation number inserted. 259 */ 260static u_char 261unilsb2dos[256] = { 262 0, 1, 1, 1, 1, 1, 1, 1, /* 00-07 */ 263 1, 1, 1, 1, 1, 1, 1, 1, /* 08-0f */ 264 1, 1, 1, 1, 1, 1, 1, 1, /* 10-17 */ 265 1, 1, 1, 1, 1, 1, 1, 1, /* 18-1f */ 266 2, 0x21, 1, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 267 0x28, 0x29, 1, 1, 1, 0x2d, 2, 0, /* 28-2f */ 268 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 269 0x38, 0x39, 1, 1, 1, 1, 1, 1, /* 38-3f */ 270 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 271 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 272 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 273 0x58, 0x59, 0x5a, 1, 1, 1, 0x5e, 0x5f, /* 58-5f */ 274 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 275 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 276 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 277 0x58, 0x59, 0x5a, 0x7b, 1, 0x7d, 0x7e, 0x7f, /* 78-7f */ 278 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 279 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 280 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 281 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 282 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 283 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 284 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 285 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 286 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* c0-c7 */ 287 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* c8-cf */ 288 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, /* d0-d7 */ 289 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, /* d8-df */ 290 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 291 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 292 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 293 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 294}; 295 296/* Unicode punctuation marks to Win Latin1 (ANSI CodePage 1252) */ 297static u_char 298unipunct2dos[48] = { 299 1, 1, 1, 0x96, 0x97, 1, 1, 1, /* 2010-2017 */ 300 0x91, 0x92, 0x82, 1, 0x93, 0x94, 0x84, 1, /* 2018-201F */ 301 0x86, 0x87, 0x95, 1, 1, 1, 0x85, 1, /* 2020-2027 */ 302 1, 1, 1, 1, 1, 1, 1, 1, /* 2028-202F */ 303 0x89, 1, 1, 1, 1, 1, 1, 1, /* 2030-2037 */ 304 1, 0x8B, 0x9B, 1, 1, 1, 1, 1 /* 2038-203F */ 305}; 306 307/* Win Latin1 (ANSI CodePage 1252) to Unicode */ 308__private_extern__ u_int16_t 309dos2unicode[32] = { 310 0x20AC, 0x003f, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, /* 80-87 */ 311 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x003f, 0x017D, 0x003f, /* 88-8F */ 312 0x003f, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, /* 90-97 */ 313 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x003f, 0x017E, 0x0178, /* 98-9F */ 314}; 315 316 317/* 318 * Case folding table for Latin-1. 319 * 320 * Maps 'A'..'Z' to 'a'..'z' and 0xc0..0xd6 to 0xe0..0xf6 and 321 * 0xd8..0xde to 0xf8..0xfe. 322 */ 323__private_extern__ u_char 324l2u[256] = { 325 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 326 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 327 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 328 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 329 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 330 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 331 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 332 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 333 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 334 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 335 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 336 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 337 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 338 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 339 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 340 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 341 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 342 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 343 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 344 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 345 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 346 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 347 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 348 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 349 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 350 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 351 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 352 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 353 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 354 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 355 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 356 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 357}; 358 359/* 360 * Look-up table to determine whether a given ASCII character is 361 * considered upper case, lower case, or neither. Also indicates 362 * which characters should require generation of a long name. 363 * 364 * Values are bit masks composed of the following constants: 365 */ 366enum { 367 CASE_LOWER = 1, 368 CASE_UPPER = 2, 369 CASE_LONG = 4, /* A long name should be generated */ 370}; 371 372static u_char 373ascii_case[128] = { 374 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 00-07 */ 375 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 08-0F */ 376 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 10-17 */ 377 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 18-1F */ 378 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, /* 20-27 */ 379 0x00, 0x00, 0x04, 0x04, 0x04, 0x00, 0x04, 0x04, /* 28-2F */ 380 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30-37 */ 381 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 38-3F */ 382 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 40-47 */ 383 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48-4F */ 384 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50-57 */ 385 0x02, 0x02, 0x02, 0x04, 0x04, 0x04, 0x00, 0x00, /* 58-5F */ 386 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* 60-67 */ 387 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* 68-6F */ 388 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* 70-77 */ 389 0x01, 0x01, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, /* 78-7F */ 390}; 391 392#if 0 393/* 394 * Macintosh Unicode (LSB) to Microsoft Services for Macintosh (SFM) Unicode 395 */ 396static u_int16_t 397mac2sfm[128] = { 398 0x0, 0xf001, 0xf002, 0xf003, 0xf004, 0xf005, 0xf006, 0xf007, /* 00-07 */ 399 0xf008, 0xf009, 0xf00a, 0xf00b, 0xf00c, 0xf00d, 0xf00e, 0xf00f, /* 08-0f */ 400 0xf010, 0xf011, 0xf012, 0xf013, 0xf014, 0xf015, 0xf016, 0xf017, /* 10-17 */ 401 0xf018, 0xf019, 0xf01a, 0xf01b, 0xf01c, 0xf01d, 0xf01e, 0xf01f, /* 18-1f */ 402 0x20, 0x21, 0xf020, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 403 0x28, 0x29, 0xf021, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 404 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 405 0x38, 0x39, 0xf022, 0x3b, 0xf023, 0x3d, 0xf024, 0xf025, /* 38-3f */ 406 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 407 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 408 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 409 0x58, 0x59, 0x5a, 0x5b, 0xf026, 0x5d, 0x5e, 0x5f, /* 58-5f */ 410 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 411 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 412 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 413 0x78, 0x79, 0x7a, 0x7b, 0xf027, 0x7d, 0x7e, 0x7f, /* 78-7f */ 414}; 415 416#define MAX_MAC2SFM 0x80 417#define MAX_SFM2MAC 0x29 418#define SFMCODE_PREFIX_MASK 0xf000 419/* 420 * SFM Unicode (LSB) to Macintosh Unicode (LSB) 421 */ 422static u_char 423sfm2mac[42] = { 424 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 425 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0F */ 426 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 427 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1F */ 428 0x22, 0x2a, 0x3a, 0x3c, 0x3e, 0x3f, 0x5c, 0x7c, /* 20-27 */ 429 0x20, 0x2e, /* 28-29 */ 430}; 431#endif 432 433/* map a Unicode char into a DOS char */ 434u_char msdosfs_unicode2dos(u_int16_t uc) 435{ 436 if (uc < 0x100) 437 return (unilsb2dos[uc]); 438 439 if (uc > 0x2122) 440 return (1); 441 442 if (uc >= 0x2010 && uc <= 0x203F) 443 return (unipunct2dos[uc - 0x2010]); 444 445 if (uc >= 0x0152 && uc <= 0x02DC) 446 switch (uc) { 447 case 0x0152: 448 return (0x8C); /* LATIN CAPITAL LIGATURE OE */ 449 case 0x0153: 450 return (0x9C); /* LATIN SMALL LIGATURE OE */ 451 case 0x0160: 452 return (0x8A); /* CAPITAL LETTER S WITH CARON */ 453 case 0x0161: 454 return (0x9A); /* SMALL LETTER S WITH CARON */ 455 case 0x0178: 456 return (0x9F); /* CAPITAL LETTER Y WITH DIAERESIS */ 457 case 0x017D: 458 return (0x8E); /* CAPITAL LETTER Z WITH CARON */ 459 case 0x017E: 460 return (0x9E); /* SMALL LETTER Z WITH CARON */ 461 case 0x0192: 462 return (0x83); /* SMALL LETTER F WITH HOOK */ 463 case 0x02C6: 464 return (0x88); /* MODIFIER LETTER CIRCUMFLEX ACCENT */ 465 case 0x02DC: 466 return (0x98); /* SMALL TILDE */ 467 default: 468 return (1); 469 } 470 471 if (uc == 0x20AC) 472 return (0x80); /* EURO SIGN */ 473 if (uc == 0x2122) 474 return (0x99); /* TRADE MARK SIGN */ 475 476 return (1); 477} 478 479/* 480 * DOS filenames are made of 2 parts, the name part and the extension part. 481 * The name part is 8 characters long and the extension part is 3 482 * characters long. They may contain trailing blanks if the name or 483 * extension are not long enough to fill their respective fields. 484 */ 485 486/* 487 * Convert a DOS filename to a Unicode filename. And, return the number of 488 * characters in the resulting unix filename. 489 */ 490size_t msdosfs_dos2unicodefn(u_char dn[SHORT_NAME_LEN], u_int16_t *un, int lower) 491{ 492 int i; 493 u_char dc; 494 int unichars = 0; 495 496 /* 497 * Copy the base name portion into the Unicode string. 498 */ 499 for (i = 0; i < 8; i++) { 500 dc = *dn++; 501 502 /* 503 * If first char of the filename is SLOT_E5 (0x05), then 504 * the real first char of the filename should be 0xe5. 505 * But, they couldn't just have a 0xe5 mean 0xe5 because 506 * that is used to mean a freed directory slot. 507 */ 508 if (i == 0 && dc == SLOT_E5) 509 dc = 0xe5; 510 511 /* 512 * If the base name was supposed to be lower case, 513 * then convert it. 514 */ 515 if (lower & LCASE_BASE) 516 dc = l2u[dc]; /* Map to lower case equivalent */ 517 518 un[unichars++] = (dc < 0x80 || dc > 0x9F ? (u_int16_t)dc : dos2unicode[dc - 0x80]); 519 } 520 521 /* 522 * Get rid of trailing space padding in the base name. 523 */ 524 while (unichars > 0 && un[unichars-1]==' ') 525 { 526 -- unichars; 527 } 528 529 /* 530 * Copy the extension portion (with dot) into the Unicode string. 531 */ 532 un[unichars++] = '.'; 533 for (i = 0; i < 3; i++) { 534 dc = *dn++; 535 536 /* 537 * If the extension was supposed to be all lower case, 538 * then convert it. 539 */ 540 if (lower & LCASE_EXT) 541 dc = l2u[dc]; /* Map to lower case equivalent */ 542 543 un[unichars++] = (dc < 0x80 || dc > 0x9F ? (u_int16_t)dc : dos2unicode[dc - 0x80]); 544 } 545 546 /* 547 * Get rid of trailing space padding in the extension (and the dot 548 * if there was no extension). 549 */ 550 for (i=0; i<3 && un[unichars-1]==' '; ++i) 551 { 552 --unichars; 553 } 554 if (i==3) /* Was the extension entirely spaces? */ 555 --unichars; /* Yes, so remove the dot, too. */ 556 557 return unichars; 558} 559 560 561/* 562 * Convert a Unicode filename to an 8.3-style short name. 563 * 564 * This function implements the Basis-Name Generation Algorithm 565 * from the FAT32 specification. It does NOT implement the numeric 566 * tail generation; that part is implemented elsewhere. 567 * 568 * In order to support setting the LCASE_BASE and LCASE_EXT flags, this 569 * routine returns the corresponding value for those flags when the name can 570 * be converted (when the function result is 1). 571 * 572 * The test is whether the name (base or extension) contains at least one 573 * lower case letter, no upper case, and zero or more case-neutral characters 574 * allowed in an 8.3 name (such as digits, dollar sign, ampersand, etc.). 575 * 576 * By experimentation with Windows XP, it seems to always create long names 577 * for names that contain extended characters, such as letter e with acute 578 * (regardless of upper/lower case). This is true even on a U.S. system 579 * with characters that are representable in code page 437 (which is in 580 * fact what you see in the short name). I'm guessing it uses pure short 581 * names only if all characters are pure ASCII. 582 * 583 * Inputs: 584 * unicode Points to a buffer of UTF-16 (native endian) 585 * containing the original name. 586 * unicode_length The length of the Unicode name in UTF-16 code 587 * points. 588 * 589 * Outputs: 590 * short_name The corresponding 8.3-style short name is returned 591 * in this buffer. The short name is upper case. 592 * lower_case A pointer to returned lower case flags. The pointer 593 * must not be NULL. 594 * 595 * Returns: 596 * 0 if name couldn't be converted (has disallowed characters). 597 * 1 if the converted name is the same as the original 598 * (no long filename entry necessary for Win95). 599 * 2 if conversion was successful, and long name entries should be created, 600 * but a generation number is not necessary (for example, the name had a 601 * mix of upper and lower case, contained non-ASCII characters, contained 602 * an embedded space, or contained a dot in the base name). 603 * 3 if conversion was successful, long name entries should be created, and 604 * one or more characters of the original name were removed (thus, a 605 * generation number should also be added). 606 */ 607int msdosfs_unicode_to_dos_name(const uint16_t *unicode, 608 size_t unicode_length, 609 uint8_t short_name[SHORT_NAME_LEN], 610 u_int8_t *lower_case) 611{ 612 int i; /* An index into the Unicode name. */ 613 int j; /* An index into the short name. */ 614 int conv = 1; /* The function's result. */ 615 const uint16_t *cp; /* A pointer for looping over the Unicode name. */ 616 const uint16_t *dp; /* A pointer to the first char of extension (or NULL). */ 617 const uint16_t *dp1; /* A pointer after last seen dot character. */ 618 uint16_t c; /* The current character being examined. */ 619 int case_flags; /* Accumulates upper/low/neither case information */ 620 621 *lower_case = 0; /* default to all upper case, and clear undefined bits */ 622 623 /* 624 * Fill the dos filename string with blanks. These are DOS's pad 625 * characters. 626 */ 627 memset(short_name, ' ', SHORT_NAME_LEN); 628 629 /* 630 * The filenames "." and ".." are handled specially, since they 631 * don't follow dos filename rules. 632 */ 633 if (unicode_length == 1 && unicode[0] == '.') { 634 short_name[0] = '.'; 635 return 1; 636 } 637 if (unicode_length == 2 && unicode[0] == '.' && unicode[1] == '.') { 638 short_name[0] = '.'; 639 short_name[1] = '.'; 640 return 1; 641 } 642 643 /* 644 * Filenames with some characters are not allowed! 645 * 646 * NOTE: For a name passed into the POSIX APIs, most of the disallowed 647 * characters will have already been replaced with alternate characters 648 * according to the Services For Macintosh conversions. 649 */ 650 for (cp = unicode, i = (int)unicode_length; --i >= 0; cp++) 651 if (msdosfs_unicode2dos(*cp) == 0) 652 return 0; 653 654 /* 655 * Find the extension (if any). 656 * 657 * Note: A dot as the first or last character do not count as 658 * an extension. Trailing blanks are supposed to be ignored, 659 * but we don't (and didn't previously) do that. 660 */ 661 dp = dp1 = NULL; 662 for (cp = unicode + 1, i = (int)unicode_length - 1; --i >= 0;) { 663 switch (*cp++) { 664 case '.': 665 if (!dp1) 666 dp1 = cp; 667 break; 668 default: 669 if (dp1) 670 dp = dp1; 671 dp1 = NULL; 672 break; 673 } 674 } 675 676 /* 677 * Convert the extension (if any). 678 */ 679 if (dp) { 680 int l; /* The length of the extension (in UTF-16 code points). */ 681 682 if (dp1) 683 l = (int)(dp1 - dp); /* Ignore trailing dots. */ 684 else 685 l = (int)(unicode_length - (dp - unicode)); 686 687 /* 688 * Convert up to 3 characters of the extension. 689 */ 690 for (case_flags = i = 0, j = 8; i < l && j < SHORT_NAME_LEN; i++, j++) { 691 c = dp[i]; 692 if (c < 0x80) 693 case_flags |= ascii_case[c]; 694 else 695 case_flags |= CASE_LONG; /* Non-ASCII always requires a long name */ 696 if (c < 0x100) 697 c = l2u[c]; 698 c = msdosfs_unicode2dos(c); 699 short_name[j] = c; 700 if (c == 1) { 701 conv = 3; /* Character is not allowed in short names */ 702 short_name[j] = '_'; /* and must be replaced with underscore */ 703 } 704 if (c == 2) { 705 conv = 3; /* Character is not allowed in short names */ 706 short_name[j--] = ' '; /* and is not substituted */ 707 } 708 } 709 710 /* 711 * Check for other conditions which might require a long name. 712 */ 713 if ((case_flags & CASE_LONG) != 0 && conv != 3) 714 conv = 2; /* Force a long name for things like embedded spaces or non-ASCII */ 715 if (conv == 1) { 716 if ((case_flags & (CASE_LOWER | CASE_UPPER)) == (CASE_LOWER | CASE_UPPER)) 717 conv = 2; /* Force a long name for names with mixed case */ 718 else if (case_flags & CASE_LOWER) 719 *lower_case |= LCASE_EXT; /* Extension has lower case */ 720 } 721 if (i < l) 722 conv = 3; /* Extension was longer than 3 characters */ 723 724 dp--; /* dp points at the dot at the start of the extension. */ 725 } else { 726 dp = cp; /* dp points past the end of the Unicode name. */ 727 } 728 729 /* 730 * Now convert the base name 731 * 732 * When we get here, dp points just past the last character of the base name. 733 */ 734 for (case_flags = i = j = 0; unicode < dp && j < 8; i++, j++, unicode++) { 735 c = *unicode; 736 if (c < 0x80) 737 case_flags |= ascii_case[c]; 738 else 739 case_flags |= CASE_LONG; /* Non-ASCII always requires a long name */ 740 if (c < 0x100) 741 c = l2u[c]; 742 c = msdosfs_unicode2dos(c); 743 short_name[j] = c; 744 if (c == 1) { 745 conv = 3; /* Character is not allowed in short names */ 746 short_name[j] = '_'; /* and must be replaced with underscore */ 747 } 748 if (c == 2) { 749 conv = 3; /* Character is not allowed in short names */ 750 short_name[j--] = ' '; /* and is not substituted */ 751 } 752 } 753 if ((case_flags & CASE_LONG) != 0 && conv != 3) 754 conv = 2; /* Force a long name for things like embedded spaces */ 755 if (conv == 1) { 756 if ((case_flags & (CASE_LOWER | CASE_UPPER)) == (CASE_LOWER | CASE_UPPER)) 757 conv = 2; /* Force a long name for names with mixed case */ 758 else if (case_flags & CASE_LOWER) 759 *lower_case |= LCASE_BASE; /* Base name has lower case */ 760 } 761 762 if (unicode < dp) 763 conv = 3; /* Base name was longer than 8 characters */ 764 765 /* 766 * If the resulting base name was empty, generate a default 767 */ 768 if (!j) 769 short_name[0] = '_'; 770 771 /* 772 * The first character cannot be E5, because that means a deleted entry 773 */ 774 if (short_name[0] == 0xe5) 775 short_name[0] = SLOT_E5; 776 777 /* 778 * If the name couldn't be represented as a short name, make sure the 779 * lower case flags are clear (in case the base or extension was all 780 * lower case, but the other was not, in which case we left one of the 781 * bits set above). 782 * 783 * That is, the lower case flags are only set if the name is a valid 784 * 8.3-style short name, where any letters in the base and extension 785 * are all upper or all lower case. (The case of the base may differ 786 * from the case of the extension.) Non-letter ASCII characters do 787 * not affect the lower case flags. 788 */ 789 if (conv != 1) 790 *lower_case = 0; 791 792 return conv; 793} 794 795 796/* 797 * Add or update a generation number, and preceding tilde (~), to a short name. 798 * 799 * The base name is up to 8 characters. We want to keep at least the first 800 * character. We need to insert a tilde before the generation number. That 801 * leaves us with room for up to 6 characters for the generation number. 802 * 803 * Parameters: 804 * short_name Modified in place. On input, a short name with no 805 * generation number, or a generation number less than 806 * or equal to the "generation" parameter. On output, 807 * the short name with generation number. 808 * generation The generation number to add or update. 809 * 810 * Result: 811 * 0 The generation number was successfully added. 812 * ENAMETOOLONG The generation number was too large to fit in the short name. 813 */ 814int msdosfs_apply_generation_to_short_name(uint8_t short_name[SHORT_NAME_LEN], 815 unsigned generation) 816{ 817 uint8_t generation_text[6]; /* Note: this is stored in reverse order. */ 818 unsigned generation_text_length = 0; 819 unsigned base_name_index; /* Index into base name where generation string 820 should be stored. */ 821 /* Convert the generation number to ASCII. */ 822 do { 823 generation_text[generation_text_length++] = '0' + (generation % 10); 824 generation /= 10; 825 } while (generation > 0 && generation_text_length < 6); 826 827 /* Check for a too-large generation number. */ 828 if (generation != 0) 829 return ENAMETOOLONG; 830 831 /* 832 * Add or replace the generation string. 833 * 834 * We start by assuming the base name is the maximum 8 characters, then scan left 835 * to find a non-space character. (FAT uses trailing space padding in short names. 836 * This also handles the case where the short name had an embedded space, and we'd 837 * like to avoid putting the generation string after an embedded space.) 838 */ 839 base_name_index = 8 - (generation_text_length + 1); /* +1 is for the tilde. */ 840 while (short_name[base_name_index] == ' ' && base_name_index > 0) 841 { 842 --base_name_index; /* Skip over trailing or embedded spaces. */ 843 } 844 short_name[base_name_index++] = '~'; 845 while (generation_text_length > 0) 846 { 847 short_name[base_name_index++] = generation_text[--generation_text_length]; 848 } 849 850 return 0; 851} 852 853 854/* 855 * Create a Win95 long name directory entry 856 * Note: assumes that the filename is valid, 857 * i.e. doesn't consist solely of blanks and dots 858 */ 859int msdosfs_unicode2winfn(const u_int16_t *un, int unlen, struct winentry *wep, int cnt, int chksum) 860{ 861 u_int8_t *wcp; 862 int i; 863 u_int16_t code; 864 865 un += (cnt - 1) * WIN_CHARS; 866 unlen -= (cnt - 1) * WIN_CHARS; 867 868 /* 869 * Initialize winentry to some useful default 870 */ 871 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 872 wep->weCnt = cnt; 873 wep->weAttributes = ATTR_WIN95; 874 wep->weReserved1 = 0; 875 wep->weChksum = chksum; 876 wep->weReserved2 = 0; 877 878 /* 879 * Now convert the filename parts 880 */ 881 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 882 if (--unlen < 0) 883 goto done; 884 code = *un++; 885 *wcp++ = code & 0x00ff; 886 *wcp++ = code >> 8; 887 } 888 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 889 if (--unlen < 0) 890 goto done; 891 code = *un++; 892 *wcp++ = code & 0x00ff; 893 *wcp++ = code >> 8; 894 } 895 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 896 if (--unlen < 0) 897 goto done; 898 code = *un++; 899 *wcp++ = code & 0x00ff; 900 *wcp++ = code >> 8; 901 } 902 if (!unlen) 903 wep->weCnt |= WIN_LAST; 904 return unlen; 905 906done: 907 *wcp++ = 0; 908 *wcp = 0; 909 wep->weCnt |= WIN_LAST; 910 return 0; 911} 912 913 914 915/* 916 * Convert a Unicode character to a single known case. Upper and lower case 917 * variants of the same character produce the same result. 918 * 919 * As an implementation detail, we convert the character to lower case. 920 * 921 * Note: this currently only handles case folding of ASCII characters. The 922 * Unicode standard defines case equivalence for other characters (such as 923 * precomposed characters), but I don't know whether Windows considers them 924 * case equivalents. 925 */ 926static inline u_int16_t case_fold(u_int16_t ch) 927{ 928 if (ch < 0x100) 929 return l2u[ch]; 930 else 931 return ch; 932} 933 934 935/* 936 * Compare a single long name entry to the corresponding portion of a 937 * filename. If the name matches, we return the on-disk name and a 938 * flag indicating whether the name needed to be case folded. 939 * 940 * We also make sure all long name entries have the same checksum 941 * value; if not, the long name is invalid and is not a match. 942 * 943 * Inputs: 944 * un[] The name being looked up (the search name). 945 * ucslen The number of code points in the name (length of un[] in elements). 946 * wep The current long name entry being compared. 947 * chksum The checksum field from previous long name entries. 948 * 949 * Outputs: 950 * found_name The actual on-disk name that matched. OPTIONAL. MAY BE NULL. 951 * case_folded Set to true if the on-disk name is a case variant of un[] (that is 952 * NOT a case-sensitive match), or if the on-disk name is not a match 953 * to un[]. Unchanged if the on-disk name is exactly equal to un[]. 954 * 955 * Function result: 956 * -1 The name or checksums did not match 957 * <checksum> The name matched. <checksum> is the checksum stored in all 958 * of the long name entries. 959 */ 960int msdosfs_winChkName(const u_int16_t *un, int ucslen, struct winentry *wep, int chksum, 961 u_int16_t *found_name, boolean_t *case_folded) 962{ 963 u_int8_t *cp; 964 int i; 965 u_int16_t code; 966 967 /* 968 * First compare checksums 969 */ 970 if (wep->weCnt&WIN_LAST) 971 chksum = wep->weChksum; 972 else if (chksum != wep->weChksum) 973 chksum = -1; 974 if (chksum == -1) 975 return -1; 976 977 /* 978 * Offset of this entry 979 */ 980 i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 981 un += i; 982 if (found_name) 983 found_name += i; 984 985 if ((ucslen -= i) < 0) /* Was "<=". See below. */ 986 return -1; /* More long name entries than the name would need */ 987 if ((wep->weCnt&WIN_LAST) && ucslen > WIN_CHARS) 988 return -1; /* Too few long name entries to hold the name */ 989 990 /* 991 * [2865792] Some FAT implementations have a bug when the long 992 * is an exact multiple of WIN_CHARS long. They make an extra 993 * long name entry containing only a terminating 0x0000 and 994 * the 0xFFFF pad characters. While this is out-of-spec 995 * (i.e. corrupt), we can be graceful and handle it anyway, 996 * like Windows does. 997 * 998 * We handle this case by falling through here with ucslen == 0. 999 * We then expect to return during the first iteration of the 1000 * following for() loop where --ucslen goes negative, and 1001 * "cp" points to two zero bytes. 1002 */ 1003 1004 /* 1005 * Compare the name parts 1006 */ 1007 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 1008 code = (cp[1] << 8) | cp[0]; 1009 if (--ucslen < 0) { 1010 /* Got to end of input name. Are we also at end of on-disk name? */ 1011 if (code == 0) 1012 return chksum; 1013 else 1014 return -1; 1015 } 1016 if (found_name) 1017 *found_name++ = code; 1018 if (code != *un) 1019 { 1020 /* Not an exact match. Try case-insensitive match. */ 1021 *case_folded = TRUE; 1022 if (case_fold(code) != case_fold(*un)) 1023 return (-1); 1024 } 1025 cp += 2; 1026 un++; 1027 } 1028 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 1029 code = (cp[1] << 8) | cp[0]; 1030 if (--ucslen < 0) { 1031 /* Got to end of input name. Are we also at end of on-disk name? */ 1032 if (code == 0) 1033 return chksum; 1034 else 1035 return -1; 1036 } 1037 if (found_name) 1038 *found_name++ = code; 1039 if (code != *un) 1040 { 1041 /* Not an exact match. Try case-insensitive match. */ 1042 *case_folded = TRUE; 1043 if (case_fold(code) != case_fold(*un)) 1044 return (-1); 1045 } 1046 cp += 2; 1047 un++; 1048 } 1049 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 1050 code = (cp[1] << 8) | cp[0]; 1051 if (--ucslen < 0) { 1052 /* Got to end of input name. Are we also at end of on-disk name? */ 1053 if (code == 0) 1054 return chksum; 1055 else 1056 return -1; 1057 } 1058 if (found_name) 1059 *found_name++ = code; 1060 if (code != *un) 1061 { 1062 /* Not an exact match. Try case-insensitive match. */ 1063 *case_folded = TRUE; 1064 if (case_fold(code) != case_fold(*un)) 1065 return (-1); 1066 } 1067 cp += 2; 1068 un++; 1069 } 1070 return chksum; 1071} 1072 1073 1074/* 1075 * Collect Win95 filename Unicode chars into ucfn. Stores the number 1076 * of characters in *unichars. Returns the checksum, or -1 if impossible. 1077 */ 1078int msdosfs_getunicodefn(struct winentry *wep, u_int16_t ucfn[WIN_MAXLEN], u_int16_t *unichars, int chksum) 1079{ 1080 u_int8_t *cp; 1081 u_int16_t *np; 1082 u_int16_t *ep = &ucfn[WIN_MAXLEN]; 1083 u_int16_t code; 1084 int i; 1085 1086 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 1087 || !(wep->weCnt&WIN_CNT)) 1088 { 1089 return -1; 1090 } 1091 1092 /* 1093 * First compare checksums 1094 */ 1095 if (wep->weCnt&WIN_LAST) 1096 { 1097 /* 1098 * The "last" entry is the one we encounter first in the directory, 1099 * so save off the checksum to compare against the other entries. 1100 * 1101 * The length we return here assumes the name is an exact multiple 1102 * of WIN_CHARS long, and therefore has no terminator in the "last" 1103 * entry. If we in fact find a terminator, we'll adjust the length. 1104 */ 1105 chksum = wep->weChksum; 1106 *unichars = (wep->weCnt&WIN_CNT) * WIN_CHARS; 1107 } 1108 else if (chksum != wep->weChksum) 1109 { 1110 chksum = -1; 1111 } 1112 if (chksum == -1) 1113 return -1; 1114 1115 /* 1116 * Find the offset within ucfn where the first character of this 1117 * long name entry should get stored. 1118 */ 1119 np = &ucfn[((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS]; 1120 1121 /* 1122 * Extract the characters from the three discontiguous parts. 1123 * 1124 * A maximum name (255 characters) will occupy 19 full entries 1125 * with 13 characters per entry, and a partial (20th) entry 1126 * containing 8 characters. In that case, we'd better see the 1127 * terminating 0-character in wePart2. We only need to check 1128 * for maximum name length while checking wePart2. 1129 */ 1130 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) 1131 { 1132 code = (cp[1] << 8) | cp[0]; 1133 switch (code) { 1134 case 0: 1135 *unichars = np - ucfn; 1136 return chksum; 1137 case '/': 1138 return -1; 1139 default: 1140 *np++ = code; 1141 break; 1142 } 1143 cp += 2; 1144 } 1145 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) 1146 { 1147 code = (cp[1] << 8) | cp[0]; 1148 switch (code) { 1149 case 0: 1150 *unichars = np - ucfn; 1151 return chksum; 1152 case '/': 1153 return -1; 1154 default: 1155 if (np >= ep) 1156 { 1157 /* The name is too long. Return error. */ 1158 return -1; 1159 } 1160 *np++ = code; 1161 break; 1162 } 1163 cp += 2; 1164 } 1165 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) 1166 { 1167 code = (cp[1] << 8) | cp[0]; 1168 switch (code) { 1169 case 0: 1170 *unichars = np - ucfn; 1171 return chksum; 1172 case '/': 1173 return -1; 1174 default: 1175 *np++ = code; 1176 break; 1177 } 1178 cp += 2; 1179 } 1180 return chksum; 1181} 1182 1183 1184/* 1185 * Compute the checksum of a DOS filename for Win95 use 1186 */ 1187u_int8_t msdosfs_winChksum(u_int8_t *name) 1188{ 1189 int i; 1190 u_int8_t s; 1191 1192 for (s = 0, i = SHORT_NAME_LEN; --i >= 0; s += *name++) 1193 s = (s << 7)|(s >> 1); 1194 return s; 1195} 1196 1197/* 1198 * Determine the number of slots necessary for Win95 names 1199 */ 1200int msdosfs_winSlotCnt(const u_int16_t *un, int unlen) 1201{ 1202#pragma unused (un) 1203 if (unlen > WIN_MAXLEN) 1204 return 0; 1205 return howmany(unlen, WIN_CHARS); 1206} 1207