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