msdosfs_conv.c revision 33768
133768Sache/* $Id: msdosfs_conv.c,v 1.21 1998/02/23 09:39:24 ache Exp $ */ 233548Sjkh/* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */ 32893Sdfr 433548Sjkh/*- 533548Sjkh * Copyright (C) 1995, 1997 Wolfgang Solfrank. 633548Sjkh * Copyright (C) 1995, 1997 TooLs GmbH. 733548Sjkh * All rights reserved. 833548Sjkh * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 933548Sjkh * 1033548Sjkh * Redistribution and use in source and binary forms, with or without 1133548Sjkh * modification, are permitted provided that the following conditions 1233548Sjkh * are met: 1333548Sjkh * 1. Redistributions of source code must retain the above copyright 1433548Sjkh * notice, this list of conditions and the following disclaimer. 1533548Sjkh * 2. Redistributions in binary form must reproduce the above copyright 1633548Sjkh * notice, this list of conditions and the following disclaimer in the 1733548Sjkh * documentation and/or other materials provided with the distribution. 1833548Sjkh * 3. All advertising materials mentioning features or use of this software 1933548Sjkh * must display the following acknowledgement: 2033548Sjkh * This product includes software developed by TooLs GmbH. 2133548Sjkh * 4. The name of TooLs GmbH may not be used to endorse or promote products 2233548Sjkh * derived from this software without specific prior written permission. 2333548Sjkh * 2433548Sjkh * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2533548Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2633548Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2733548Sjkh * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2833548Sjkh * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2933548Sjkh * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3033548Sjkh * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3133548Sjkh * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3233548Sjkh * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3333548Sjkh * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3433548Sjkh */ 352893Sdfr/* 362893Sdfr * Written by Paul Popelka (paulp@uts.amdahl.com) 378876Srgrimes * 382893Sdfr * You can do anything you want with this software, just don't say you wrote 392893Sdfr * it, and don't remove this notice. 408876Srgrimes * 412893Sdfr * This software is provided "as is". 428876Srgrimes * 432893Sdfr * The author supplies this software to be publicly redistributed on the 442893Sdfr * understanding that the author is not responsible for the correct 452893Sdfr * functioning of this software in any circumstances and is not liable for 462893Sdfr * any damages caused by this software. 478876Srgrimes * 482893Sdfr * October 1992 492893Sdfr */ 502893Sdfr 512893Sdfr/* 522893Sdfr * System include files. 532893Sdfr */ 542893Sdfr#include <sys/param.h> 552893Sdfr#include <sys/time.h> 562893Sdfr#include <sys/kernel.h> /* defines tz */ 5733548Sjkh#include <sys/systm.h> 587465Sache#include <machine/clock.h> 5933548Sjkh#include <sys/dirent.h> 602893Sdfr 612893Sdfr/* 622893Sdfr * MSDOSFS include files. 632893Sdfr */ 642893Sdfr#include <msdosfs/direntry.h> 652893Sdfr 662893Sdfr/* 677465Sache * Total number of days that have passed for each month in a regular year. 682893Sdfr */ 6912144Sphkstatic u_short regyear[] = { 707465Sache 31, 59, 90, 120, 151, 181, 717465Sache 212, 243, 273, 304, 334, 365 722893Sdfr}; 732893Sdfr 742893Sdfr/* 757465Sache * Total number of days that have passed for each month in a leap year. 762893Sdfr */ 7712144Sphkstatic u_short leapyear[] = { 787465Sache 31, 60, 91, 121, 152, 182, 797465Sache 213, 244, 274, 305, 335, 366 802893Sdfr}; 812893Sdfr 822893Sdfr/* 832893Sdfr * Variables used to remember parts of the last time conversion. Maybe we 842893Sdfr * can avoid a full conversion. 852893Sdfr */ 8633181Seivindstatic u_long lasttime; 8733181Seivindstatic u_long lastday; 8833181Seivindstatic u_short lastddate; 8933181Seivindstatic u_short lastdtime; 902893Sdfr 9133747Sachestatic inline u_int8_t find_lcode __P((u_int16_t code, u_int16_t *u2w)); 9233747Sache 932893Sdfr/* 942893Sdfr * Convert the unix version of time to dos's idea of time to be used in 952893Sdfr * file timestamps. The passed in unix time is assumed to be in GMT. 962893Sdfr */ 972893Sdfrvoid 9833548Sjkhunix2dostime(tsp, ddp, dtp, dhp) 992893Sdfr struct timespec *tsp; 10033548Sjkh u_int16_t *ddp; 10133548Sjkh u_int16_t *dtp; 10233548Sjkh u_int8_t *dhp; 1032893Sdfr{ 1042893Sdfr u_long t; 1052893Sdfr u_long days; 1062893Sdfr u_long inc; 1072893Sdfr u_long year; 1082893Sdfr u_long month; 1092893Sdfr u_short *months; 1102893Sdfr 1112893Sdfr /* 1122893Sdfr * If the time from the last conversion is the same as now, then 1132893Sdfr * skip the computations and use the saved result. 1142893Sdfr */ 11518397Snate t = tsp->tv_sec - (tz.tz_minuteswest * 60) 11615055Sache - (wall_cmos_clock ? adjkerntz : 0); 11715053Sache /* - daylight savings time correction */ 11833548Sjkh t &= ~1; 1192893Sdfr if (lasttime != t) { 1202893Sdfr lasttime = t; 12133548Sjkh lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT) 1222893Sdfr + (((t / 60) % 60) << DT_MINUTES_SHIFT) 1232893Sdfr + (((t / 3600) % 24) << DT_HOURS_SHIFT); 1242893Sdfr 1252893Sdfr /* 1262893Sdfr * If the number of days since 1970 is the same as the last 1272893Sdfr * time we did the computation then skip all this leap year 1282893Sdfr * and month stuff. 1292893Sdfr */ 1302893Sdfr days = t / (24 * 60 * 60); 1312893Sdfr if (days != lastday) { 1322893Sdfr lastday = days; 1332893Sdfr for (year = 1970;; year++) { 1342893Sdfr inc = year & 0x03 ? 365 : 366; 1352893Sdfr if (days < inc) 1362893Sdfr break; 1372893Sdfr days -= inc; 1382893Sdfr } 1392893Sdfr months = year & 0x03 ? regyear : leapyear; 14020138Sbde for (month = 0; days >= months[month]; month++) 1417465Sache ; 1427465Sache if (month > 0) 1437465Sache days -= months[month - 1]; 1442893Sdfr lastddate = ((days + 1) << DD_DAY_SHIFT) 1452893Sdfr + ((month + 1) << DD_MONTH_SHIFT); 1462893Sdfr /* 1472893Sdfr * Remember dos's idea of time is relative to 1980. 1482893Sdfr * unix's is relative to 1970. If somehow we get a 1492893Sdfr * time before 1980 then don't give totally crazy 1502893Sdfr * results. 1512893Sdfr */ 1522893Sdfr if (year > 1980) 1532893Sdfr lastddate += (year - 1980) << DD_YEAR_SHIFT; 1542893Sdfr } 1552893Sdfr } 15633548Sjkh if (dtp) 15733548Sjkh *dtp = lastdtime; 15833548Sjkh if (dhp) 15933548Sjkh *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 16033548Sjkh 1612893Sdfr *ddp = lastddate; 1622893Sdfr} 1632893Sdfr 1642893Sdfr/* 1652893Sdfr * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that 1662893Sdfr * interval there were 8 regular years and 2 leap years. 1672893Sdfr */ 1682893Sdfr#define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) 1692893Sdfr 17033181Seivindstatic u_short lastdosdate; 17133181Seivindstatic u_long lastseconds; 1722893Sdfr 1732893Sdfr/* 1742893Sdfr * Convert from dos' idea of time to unix'. This will probably only be 1752893Sdfr * called from the stat(), and fstat() system calls and so probably need 1762893Sdfr * not be too efficient. 1772893Sdfr */ 1782893Sdfrvoid 17933548Sjkhdos2unixtime(dd, dt, dh, tsp) 18033548Sjkh u_int dd; 18133548Sjkh u_int dt; 18233548Sjkh u_int dh; 1832893Sdfr struct timespec *tsp; 1842893Sdfr{ 1852893Sdfr u_long seconds; 18611921Sphk u_long month; 18711921Sphk u_long year; 1882893Sdfr u_long days; 1892893Sdfr u_short *months; 1902893Sdfr 19133548Sjkh if (dd == 0) { 19233548Sjkh /* 19333548Sjkh * Uninitialized field, return the epoch. 19433548Sjkh */ 19533548Sjkh tsp->tv_sec = 0; 19633548Sjkh tsp->tv_nsec = 0; 19733548Sjkh return; 19833548Sjkh } 1995083Sbde seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1) 2002893Sdfr + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 20133548Sjkh + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 20233548Sjkh + dh / 100; 2032893Sdfr /* 2042893Sdfr * If the year, month, and day from the last conversion are the 2052893Sdfr * same then use the saved value. 2062893Sdfr */ 2072893Sdfr if (lastdosdate != dd) { 2082893Sdfr lastdosdate = dd; 2092893Sdfr days = 0; 2102893Sdfr year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 2117465Sache days = year * 365; 2127465Sache days += year / 4 + 1; /* add in leap days */ 2137465Sache if ((year & 0x03) == 0) 2147465Sache days--; /* if year is a leap year */ 2152893Sdfr months = year & 0x03 ? regyear : leapyear; 2162893Sdfr month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 2177465Sache if (month < 1 || month > 12) { 21833548Sjkh printf("dos2unixtime(): month value out of range (%ld)\n", 2193152Sphk month); 2202893Sdfr month = 1; 2212893Sdfr } 2227465Sache if (month > 1) 2237465Sache days += months[month - 2]; 2242893Sdfr days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 2252893Sdfr lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 2262893Sdfr } 22718397Snate tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60) 22815055Sache + adjkerntz; 22915053Sache /* + daylight savings time correction */ 23033548Sjkh tsp->tv_nsec = (dh % 100) * 10000000; 2312893Sdfr} 2322893Sdfr 23333548Sjkhstatic u_char 23433548Sjkhunix2dos[256] = { 23533548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 23633548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 23733548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 23833548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 23933548Sjkh 0, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 24033548Sjkh 0x28, 0x29, 0, 0, 0, 0x2d, 0, 0, /* 28-2f */ 24133548Sjkh 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 24233548Sjkh 0x38, 0x39, 0, 0, 0, 0, 0, 0, /* 38-3f */ 24333548Sjkh 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 24433548Sjkh 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 24533548Sjkh 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 24633548Sjkh 0x58, 0x59, 0x5a, 0, 0, 0, 0x5e, 0x5f, /* 58-5f */ 24733548Sjkh 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 24833548Sjkh 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 24933548Sjkh 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 25033548Sjkh 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 25133548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 25233548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 25333548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 25433548Sjkh 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 25533548Sjkh 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 25633548Sjkh 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 25733548Sjkh 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 25833548Sjkh 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 25933548Sjkh 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 26033548Sjkh 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 26133548Sjkh 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 26233548Sjkh 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 26333548Sjkh 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 26433548Sjkh 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 26533548Sjkh 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 26633548Sjkh 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 26733548Sjkh}; 2682893Sdfr 26933548Sjkhstatic u_char 27033548Sjkhdos2unix[256] = { 27133548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 27233548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 27333548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 27433548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 27533548Sjkh 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 27633548Sjkh 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 27733548Sjkh 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 27833548Sjkh 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 27933548Sjkh 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 28033548Sjkh 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 28133548Sjkh 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 28233548Sjkh 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 28333548Sjkh 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 28433548Sjkh 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 28533548Sjkh 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 28633548Sjkh 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 28733548Sjkh 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 28833548Sjkh 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 28933548Sjkh 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 29033548Sjkh 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 29133548Sjkh 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 29233548Sjkh 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 29333548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 29433548Sjkh 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 29533548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 29633548Sjkh 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 29733548Sjkh 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 29833548Sjkh 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 29933548Sjkh 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 30033548Sjkh 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 30133548Sjkh 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 30233548Sjkh 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 30333548Sjkh}; 30433548Sjkh 30533548Sjkhstatic u_char 30633548Sjkhu2l[256] = { 30733548Sjkh 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 30833548Sjkh 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 30933548Sjkh 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 31033548Sjkh 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 31133548Sjkh 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 31233548Sjkh 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 31333548Sjkh 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 31433548Sjkh 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 31533548Sjkh 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 31633548Sjkh 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 31733548Sjkh 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 31833548Sjkh 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 31933548Sjkh 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 32033548Sjkh 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 32133548Sjkh 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 32233548Sjkh 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 32333548Sjkh 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 32433548Sjkh 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 32533548Sjkh 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 32633548Sjkh 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 32733548Sjkh 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 32833548Sjkh 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 32933548Sjkh 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 33033548Sjkh 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 33133548Sjkh 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 33233548Sjkh 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 33333548Sjkh 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 33433548Sjkh 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 33533548Sjkh 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 33633548Sjkh 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 33733548Sjkh 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 33833548Sjkh 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 33933548Sjkh}; 34033548Sjkh 34133768Sachestatic u_char 34233768Sachel2u[256] = { 34333768Sache 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 34433768Sache 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 34533768Sache 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 34633768Sache 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 34733768Sache 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 34833768Sache 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 34933768Sache 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 35033768Sache 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 35133768Sache 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 35233768Sache 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 35333768Sache 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 35433768Sache 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 35533768Sache 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 35633768Sache 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 35733768Sache 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 35833768Sache 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 35933768Sache 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 36033768Sache 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 36133768Sache 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 36233768Sache 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 36333768Sache 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 36433768Sache 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 36533768Sache 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 36633768Sache 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 36733768Sache 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 36833768Sache 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 36933768Sache 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 37033768Sache 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 37133768Sache 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 37233768Sache 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 37333768Sache 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 37433768Sache 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 37533768Sache}; 37633768Sache 3772893Sdfr/* 3782893Sdfr * DOS filenames are made of 2 parts, the name part and the extension part. 3792893Sdfr * The name part is 8 characters long and the extension part is 3 3802893Sdfr * characters long. They may contain trailing blanks if the name or 3812893Sdfr * extension are not long enough to fill their respective fields. 3822893Sdfr */ 3832893Sdfr 3842893Sdfr/* 3852893Sdfr * Convert a DOS filename to a unix filename. And, return the number of 3862893Sdfr * characters in the resulting unix filename excluding the terminating 3872893Sdfr * null. 3882893Sdfr */ 3892893Sdfrint 39033768Sachedos2unixfn(dn, un, d2u_loaded, d2u, ul_loaded, ul) 3912893Sdfr u_char dn[11]; 3922893Sdfr u_char *un; 39333768Sache int d2u_loaded; 39433768Sache u_int8_t *d2u; 39533768Sache int ul_loaded; 39633760Sache u_int8_t *ul; 3972893Sdfr{ 3982893Sdfr int i; 39933548Sjkh int thislong = 1; 4002893Sdfr u_char c; 4012893Sdfr 4022893Sdfr /* 40333548Sjkh * If first char of the filename is SLOT_E5 (0x05), then the real 40433548Sjkh * first char of the filename should be 0xe5. But, they couldn't 40533548Sjkh * just have a 0xe5 mean 0xe5 because that is used to mean a freed 40633548Sjkh * directory slot. Another dos quirk. 4072893Sdfr */ 40833548Sjkh if (*dn == SLOT_E5) 40933768Sache c = d2u_loaded ? d2u[0xe5 & 0x7f] : dos2unix[0xe5]; 41033548Sjkh else 41133768Sache c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] : 41233768Sache dos2unix[*dn]; 41333768Sache *un++ = ul_loaded && (c & 0x80) ? 41433768Sache ul[c & 0x7f] : u2l[c]; 41533548Sjkh dn++; 4162893Sdfr 4172893Sdfr /* 41833548Sjkh * Copy the name portion into the unix filename string. 4192893Sdfr */ 42033548Sjkh for (i = 1; i < 8 && *dn != ' '; i++) { 42133768Sache c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] : 42233768Sache dos2unix[*dn]; 42333768Sache dn++; 42433768Sache *un++ = ul_loaded && (c & 0x80) ? 42533768Sache ul[c & 0x7f] : u2l[c]; 4262893Sdfr thislong++; 4272893Sdfr } 42833548Sjkh dn += 8 - i; 4292893Sdfr 4302893Sdfr /* 4312893Sdfr * Now, if there is an extension then put in a period and copy in 4322893Sdfr * the extension. 4332893Sdfr */ 43433548Sjkh if (*dn != ' ') { 4352893Sdfr *un++ = '.'; 4362893Sdfr thislong++; 43733548Sjkh for (i = 0; i < 3 && *dn != ' '; i++) { 43833768Sache c = d2u_loaded && (*dn & 0x80) ? d2u[*dn & 0x7f] : 43933768Sache dos2unix[*dn]; 44033768Sache dn++; 44133768Sache *un++ = ul_loaded && (c & 0x80) ? 44233768Sache ul[c & 0x7f] : u2l[c]; 4432893Sdfr thislong++; 4442893Sdfr } 4452893Sdfr } 4462893Sdfr *un++ = 0; 4472893Sdfr 44833548Sjkh return (thislong); 4492893Sdfr} 4502893Sdfr 4512893Sdfr/* 45233548Sjkh * Convert a unix filename to a DOS filename according to Win95 rules. 45333548Sjkh * If applicable and gen is not 0, it is inserted into the converted 45433548Sjkh * filename as a generation number. 45533548Sjkh * Returns 45633548Sjkh * 0 if name couldn't be converted 45733548Sjkh * 1 if the converted name is the same as the original 45833548Sjkh * (no long filename entry necessary for Win95) 45933548Sjkh * 2 if conversion was successful 46033548Sjkh * 3 if conversion was successful and generation number was inserted 4612893Sdfr */ 46233548Sjkhint 46333768Sacheunix2dosfn(un, dn, unlen, gen, u2d_loaded, u2d, lu_loaded, lu) 46433548Sjkh const u_char *un; 46533548Sjkh u_char dn[12]; 4662893Sdfr int unlen; 46733548Sjkh u_int gen; 46833768Sache int u2d_loaded; 46933768Sache u_int8_t *u2d; 47033768Sache int lu_loaded; 47133768Sache u_int8_t *lu; 4722893Sdfr{ 47333548Sjkh int i, j, l; 47433548Sjkh int conv = 1; 47533548Sjkh const u_char *cp, *dp, *dp1; 47633548Sjkh u_char gentext[6], *wcp; 47733768Sache u_int8_t c; 4782893Sdfr 4792893Sdfr /* 4802893Sdfr * Fill the dos filename string with blanks. These are DOS's pad 4812893Sdfr * characters. 4822893Sdfr */ 48333548Sjkh for (i = 0; i < 11; i++) 4842893Sdfr dn[i] = ' '; 48533548Sjkh dn[11] = 0; 4862893Sdfr 4872893Sdfr /* 4882893Sdfr * The filenames "." and ".." are handled specially, since they 4892893Sdfr * don't follow dos filename rules. 4902893Sdfr */ 4912893Sdfr if (un[0] == '.' && unlen == 1) { 4922893Sdfr dn[0] = '.'; 49333548Sjkh return gen <= 1; 4942893Sdfr } 4952893Sdfr if (un[0] == '.' && un[1] == '.' && unlen == 2) { 4962893Sdfr dn[0] = '.'; 4972893Sdfr dn[1] = '.'; 49833548Sjkh return gen <= 1; 4992893Sdfr } 5002893Sdfr 5012893Sdfr /* 50233548Sjkh * Filenames with only blanks and dots are not allowed! 5032893Sdfr */ 50433548Sjkh for (cp = un, i = unlen; --i >= 0; cp++) 50533548Sjkh if (*cp != ' ' && *cp != '.') 50633548Sjkh break; 50733548Sjkh if (i < 0) 50833548Sjkh return 0; 50933548Sjkh 51033548Sjkh /* 51133548Sjkh * Now find the extension 51233548Sjkh * Note: dot as first char doesn't start extension 51333548Sjkh * and trailing dots and blanks are ignored 51433548Sjkh */ 51533548Sjkh dp = dp1 = 0; 51633548Sjkh for (cp = un + 1, i = unlen - 1; --i >= 0;) { 51733548Sjkh switch (*cp++) { 51833548Sjkh case '.': 51933548Sjkh if (!dp1) 52033548Sjkh dp1 = cp; 52133548Sjkh break; 52233548Sjkh case ' ': 52333548Sjkh break; 52433548Sjkh default: 52533548Sjkh if (dp1) 52633548Sjkh dp = dp1; 52733548Sjkh dp1 = 0; 52833548Sjkh break; 52933548Sjkh } 5302893Sdfr } 5312893Sdfr 5322893Sdfr /* 53333548Sjkh * Now convert it 5342893Sdfr */ 53533548Sjkh if (dp) { 53633548Sjkh if (dp1) 53733548Sjkh l = dp1 - dp; 53833548Sjkh else 53933548Sjkh l = unlen - (dp - un); 54033548Sjkh for (i = 0, j = 8; i < l && j < 11; i++, j++) { 54133768Sache c = dp[i]; 54233768Sache c = lu_loaded && (c & 0x80) ? 54333768Sache lu[c & 0x7f] : l2u[c]; 54433768Sache c = u2d_loaded && (c & 0x80) ? 54533768Sache u2d[c & 0x7f] : unix2dos[c]; 54633768Sache if (dp[i] != (dn[j] = c) 54733548Sjkh && conv != 3) 54833548Sjkh conv = 2; 54933548Sjkh if (!dn[j]) { 55033548Sjkh conv = 3; 55133548Sjkh dn[j--] = ' '; 55233548Sjkh } 55333548Sjkh } 55433548Sjkh if (i < l) 55533548Sjkh conv = 3; 55633548Sjkh dp--; 55733548Sjkh } else { 55833548Sjkh for (dp = cp; *--dp == ' ' || *dp == '.';); 55933548Sjkh dp++; 56033548Sjkh } 56133548Sjkh 56233548Sjkh /* 56333548Sjkh * Now convert the rest of the name 56433548Sjkh */ 56533548Sjkh for (i = j = 0; un < dp && j < 8; i++, j++, un++) { 56633768Sache c = lu_loaded && (*un & 0x80) ? 56733768Sache lu[*un & 0x7f] : l2u[*un]; 56833768Sache c = u2d_loaded && (c & 0x80) ? 56933768Sache u2d[c & 0x7f] : unix2dos[c]; 57033768Sache if (*un != (dn[j] = c) 57133548Sjkh && conv != 3) 57233548Sjkh conv = 2; 57333548Sjkh if (!dn[j]) { 57433548Sjkh conv = 3; 57533548Sjkh dn[j--] = ' '; 57633548Sjkh } 57733548Sjkh } 57833548Sjkh if (un < dp) 57933548Sjkh conv = 3; 58033548Sjkh /* 58133548Sjkh * If we didn't have any chars in filename, 58233548Sjkh * generate a default 58333548Sjkh */ 58433548Sjkh if (!j) 58533548Sjkh dn[0] = '_'; 58633548Sjkh 58733548Sjkh /* 58833548Sjkh * The first character cannot be E5, 58933548Sjkh * because that means a deleted entry 59033548Sjkh */ 59133548Sjkh if (dn[0] == 0xe5) 5922893Sdfr dn[0] = SLOT_E5; 5932893Sdfr 5942893Sdfr /* 59533548Sjkh * If there wasn't any char dropped, 59633548Sjkh * there is no place for generation numbers 5972893Sdfr */ 59833548Sjkh if (conv != 3) { 59933548Sjkh if (gen > 1) 60033548Sjkh return 0; 60133548Sjkh return conv; 6022893Sdfr } 6032893Sdfr 6042893Sdfr /* 60533548Sjkh * Now insert the generation number into the filename part 6062893Sdfr */ 60733548Sjkh for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 60833548Sjkh *--wcp = gen % 10 + '0'; 60933548Sjkh if (gen) 61033548Sjkh return 0; 61133548Sjkh for (i = 8; dn[--i] == ' ';); 61233548Sjkh i++; 61333548Sjkh if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 61433548Sjkh i = 8 - (gentext + sizeof(gentext) - wcp + 1); 61533548Sjkh dn[i++] = '~'; 61633548Sjkh while (wcp < gentext + sizeof(gentext)) 61733548Sjkh dn[i++] = *wcp++; 61833548Sjkh return 3; 61933548Sjkh} 62033548Sjkh 62133548Sjkh/* 62233548Sjkh * Create a Win95 long name directory entry 62333548Sjkh * Note: assumes that the filename is valid, 62433548Sjkh * i.e. doesn't consist solely of blanks and dots 62533548Sjkh */ 62633548Sjkhint 62733768Sacheunix2winfn(un, unlen, wep, cnt, chksum, table_loaded, u2w) 62833548Sjkh const u_char *un; 62933548Sjkh int unlen; 63033548Sjkh struct winentry *wep; 63133548Sjkh int cnt; 63233548Sjkh int chksum; 63333768Sache int table_loaded; 63433747Sache u_int16_t *u2w; 63533548Sjkh{ 63633548Sjkh const u_int8_t *cp; 63733548Sjkh u_int8_t *wcp; 63833548Sjkh int i; 63933747Sache u_int16_t code; 64033548Sjkh 64133548Sjkh /* 64233548Sjkh * Drop trailing blanks and dots 64333548Sjkh */ 64433548Sjkh for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--); 64533548Sjkh 64633548Sjkh un += (cnt - 1) * WIN_CHARS; 64733548Sjkh unlen -= (cnt - 1) * WIN_CHARS; 64833548Sjkh 64933548Sjkh /* 65033548Sjkh * Initialize winentry to some useful default 65133548Sjkh */ 65233548Sjkh for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 65333548Sjkh wep->weCnt = cnt; 65433548Sjkh wep->weAttributes = ATTR_WIN95; 65533548Sjkh wep->weReserved1 = 0; 65633548Sjkh wep->weChksum = chksum; 65733548Sjkh wep->weReserved2 = 0; 65833548Sjkh 65933548Sjkh /* 66033548Sjkh * Now convert the filename parts 66133548Sjkh */ 66233548Sjkh for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 66333548Sjkh if (--unlen < 0) 66433548Sjkh goto done; 66533747Sache if (table_loaded && (*un & 0x80)) { 66633747Sache code = u2w[*un++ & 0x7f]; 66733747Sache *wcp++ = code; 66833747Sache *wcp++ = code >> 8; 66933747Sache } else { 67033747Sache *wcp++ = *un++; 67133747Sache *wcp++ = 0; 67233747Sache } 6732893Sdfr } 67433548Sjkh for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 67533548Sjkh if (--unlen < 0) 67633548Sjkh goto done; 67733747Sache if (table_loaded && (*un & 0x80)) { 67833747Sache code = u2w[*un++ & 0x7f]; 67933747Sache *wcp++ = code; 68033747Sache *wcp++ = code >> 8; 68133747Sache } else { 68233747Sache *wcp++ = *un++; 68333747Sache *wcp++ = 0; 68433747Sache } 68533548Sjkh } 68633548Sjkh for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 68733548Sjkh if (--unlen < 0) 68833548Sjkh goto done; 68933747Sache if (table_loaded && (*un & 0x80)) { 69033747Sache code = u2w[*un++ & 0x7f]; 69133747Sache *wcp++ = code; 69233747Sache *wcp++ = code >> 8; 69333747Sache } else { 69433747Sache *wcp++ = *un++; 69533747Sache *wcp++ = 0; 69633747Sache } 69733548Sjkh } 69833548Sjkh if (!unlen) 69933548Sjkh wep->weCnt |= WIN_LAST; 70033548Sjkh return unlen; 70133548Sjkh 70233548Sjkhdone: 70333548Sjkh *wcp++ = 0; 70433548Sjkh *wcp++ = 0; 70533548Sjkh wep->weCnt |= WIN_LAST; 70633548Sjkh return 0; 7072893Sdfr} 7082893Sdfr 70933750Sachestatic inline u_int8_t 71033750Sachefind_lcode(code, u2w) 71133750Sache u_int16_t code; 71233750Sache u_int16_t *u2w; 71333750Sache{ 71433750Sache int i; 71533750Sache 71633750Sache for (i = 0; i < 128; i++) 71733750Sache if (u2w[i] == code) 71833750Sache return (i | 0x80); 71933750Sache return '?'; 72033750Sache} 72133750Sache 7222893Sdfr/* 72333548Sjkh * Compare our filename to the one in the Win95 entry 72433548Sjkh * Returns the checksum or -1 if no match 7252893Sdfr */ 72633548Sjkhint 72733768SachewinChkName(un, unlen, wep, chksum, u2w_loaded, u2w, lu_loaded, lu) 72833548Sjkh const u_char *un; 72933548Sjkh int unlen; 73033548Sjkh struct winentry *wep; 73133548Sjkh int chksum; 73233768Sache int u2w_loaded; 73333750Sache u_int16_t *u2w; 73433768Sache int lu_loaded; 73533768Sache u_int8_t *lu; 73633548Sjkh{ 73733548Sjkh u_int8_t *cp; 73833548Sjkh int i; 73933750Sache u_int16_t code; 74033760Sache u_int8_t c1, c2; 74133548Sjkh 74233548Sjkh /* 74333548Sjkh * First compare checksums 74433548Sjkh */ 74533548Sjkh if (wep->weCnt&WIN_LAST) 74633548Sjkh chksum = wep->weChksum; 74733548Sjkh else if (chksum != wep->weChksum) 74833548Sjkh chksum = -1; 74933548Sjkh if (chksum == -1) 75033548Sjkh return -1; 75133548Sjkh 75233548Sjkh /* 75333548Sjkh * Offset of this entry 75433548Sjkh */ 75533548Sjkh i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 75633548Sjkh un += i; 75733548Sjkh if ((unlen -= i) <= 0) 75833548Sjkh return -1; 75933548Sjkh if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS) 76033548Sjkh return -1; 76133548Sjkh 76233548Sjkh /* 76333548Sjkh * Compare the name parts 76433548Sjkh */ 76533548Sjkh for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 76633548Sjkh if (--unlen < 0) { 76733548Sjkh if (!*cp++ && !*cp) 76833548Sjkh return chksum; 76933548Sjkh return -1; 77033548Sjkh } 77133750Sache code = (cp[1] << 8) | cp[0]; 77233750Sache if (code & 0xff80) { 77333760Sache if (u2w_loaded) 77433750Sache code = find_lcode(code, u2w); 77533750Sache else if (code & 0xff00) 77633750Sache code = '?'; 77733750Sache } 77833768Sache c1 = lu_loaded && (code & 0x80) ? 77933768Sache lu[code & 0x7f] : l2u[code]; 78033768Sache c2 = lu_loaded && (*un & 0x80) ? 78133768Sache lu[*un & 0x7f] : l2u[*un]; 78233760Sache if (c1 != c2) 78333548Sjkh return -1; 78433750Sache cp += 2; 78533760Sache un++; 78633548Sjkh } 78733548Sjkh for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 78833548Sjkh if (--unlen < 0) { 78933548Sjkh if (!*cp++ && !*cp) 79033548Sjkh return chksum; 79133548Sjkh return -1; 79233548Sjkh } 79333750Sache code = (cp[1] << 8) | cp[0]; 79433750Sache if (code & 0xff80) { 79533760Sache if (u2w_loaded) 79633750Sache code = find_lcode(code, u2w); 79733750Sache else if (code & 0xff00) 79833750Sache code = '?'; 79933750Sache } 80033768Sache c1 = lu_loaded && (code & 0x80) ? 80133768Sache lu[code & 0x7f] : l2u[code]; 80233768Sache c2 = lu_loaded && (*un & 0x80) ? 80333768Sache lu[*un & 0x7f] : l2u[*un]; 80433760Sache if (c1 != c2) 80533548Sjkh return -1; 80633750Sache cp += 2; 80733760Sache un++; 80833548Sjkh } 80933548Sjkh for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 81033548Sjkh if (--unlen < 0) { 81133548Sjkh if (!*cp++ && !*cp) 81233548Sjkh return chksum; 81333548Sjkh return -1; 81433548Sjkh } 81533750Sache code = (cp[1] << 8) | cp[0]; 81633750Sache if (code & 0xff80) { 81733760Sache if (u2w_loaded) 81833750Sache code = find_lcode(code, u2w); 81933750Sache else if (code & 0xff00) 82033750Sache code = '?'; 82133750Sache } 82233768Sache c1 = lu_loaded && (code & 0x80) ? 82333768Sache lu[code & 0x7f] : l2u[code]; 82433768Sache c2 = lu_loaded && (*un & 0x80) ? 82533768Sache lu[*un & 0x7f] : l2u[*un]; 82633760Sache if (c1 != c2) 82733548Sjkh return -1; 82833750Sache cp += 2; 82933760Sache un++; 83033548Sjkh } 83133548Sjkh return chksum; 83233548Sjkh} 83333548Sjkh 83433548Sjkh/* 83533548Sjkh * Convert Win95 filename to dirbuf. 83633548Sjkh * Returns the checksum or -1 if impossible 83733548Sjkh */ 83833548Sjkhint 83933768Sachewin2unixfn(wep, dp, chksum, table_loaded, u2w) 84033548Sjkh struct winentry *wep; 84133548Sjkh struct dirent *dp; 84233548Sjkh int chksum; 84333768Sache int table_loaded; 84433747Sache u_int16_t *u2w; 84533548Sjkh{ 84633548Sjkh u_int8_t *cp; 84733548Sjkh u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN; 84833744Sache u_int16_t code; 84933548Sjkh int i; 85033548Sjkh 85133548Sjkh if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 85233548Sjkh || !(wep->weCnt&WIN_CNT)) 85333548Sjkh return -1; 85433548Sjkh 85533548Sjkh /* 85633548Sjkh * First compare checksums 85733548Sjkh */ 85833548Sjkh if (wep->weCnt&WIN_LAST) { 85933548Sjkh chksum = wep->weChksum; 86033548Sjkh /* 86133548Sjkh * This works even though d_namlen is one byte! 86233548Sjkh */ 86333548Sjkh dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS; 86433548Sjkh } else if (chksum != wep->weChksum) 86533548Sjkh chksum = -1; 86633548Sjkh if (chksum == -1) 86733548Sjkh return -1; 86833548Sjkh 86933548Sjkh /* 87033548Sjkh * Offset of this entry 87133548Sjkh */ 87233548Sjkh i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 87333548Sjkh np = (u_int8_t *)dp->d_name + i; 87433548Sjkh 87533548Sjkh /* 87633548Sjkh * Convert the name parts 87733548Sjkh */ 87833548Sjkh for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 87933744Sache code = (cp[1] << 8) | cp[0]; 88033744Sache switch (code) { 88133548Sjkh case 0: 88233744Sache *np = '\0'; 88333548Sjkh dp->d_namlen -= sizeof(wep->wePart2)/2 88433548Sjkh + sizeof(wep->wePart3)/2 + i + 1; 88533548Sjkh return chksum; 88633548Sjkh case '/': 88733744Sache *np = '\0'; 88833548Sjkh return -1; 88933744Sache default: 89033747Sache if (code & 0xff80) { 89133747Sache if (table_loaded) 89233747Sache code = find_lcode(code, u2w); 89333747Sache else if (code & 0xff00) 89433745Sache code = '?'; 89533744Sache } 89633744Sache *np++ = code; 89733744Sache break; 89833548Sjkh } 89933548Sjkh /* 90033548Sjkh * The size comparison should result in the compiler 90133548Sjkh * optimizing the whole if away 90233548Sjkh */ 90333548Sjkh if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2 90433548Sjkh && np > ep) { 90533548Sjkh np[-1] = 0; 90633548Sjkh return -1; 90733548Sjkh } 90833744Sache cp += 2; 90933548Sjkh } 91033548Sjkh for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 91133744Sache code = (cp[1] << 8) | cp[0]; 91233744Sache switch (code) { 91333548Sjkh case 0: 91433744Sache *np = '\0'; 91533548Sjkh dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1; 91633548Sjkh return chksum; 91733548Sjkh case '/': 91833744Sache *np = '\0'; 91933548Sjkh return -1; 92033744Sache default: 92133747Sache if (code & 0xff80) { 92233747Sache if (table_loaded) 92333747Sache code = find_lcode(code, u2w); 92433747Sache else if (code & 0xff00) 92533745Sache code = '?'; 92633744Sache } 92733744Sache *np++ = code; 92833744Sache break; 92933548Sjkh } 93033548Sjkh /* 93133548Sjkh * The size comparisons should be optimized away 93233548Sjkh */ 93333548Sjkh if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2 93433548Sjkh && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 93533548Sjkh && np > ep) { 93633548Sjkh np[-1] = 0; 93733548Sjkh return -1; 93833548Sjkh } 93933744Sache cp += 2; 94033548Sjkh } 94133548Sjkh for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 94233744Sache code = (cp[1] << 8) | cp[0]; 94333744Sache switch (code) { 94433548Sjkh case 0: 94533744Sache *np = '\0'; 94633548Sjkh dp->d_namlen -= i + 1; 94733548Sjkh return chksum; 94833548Sjkh case '/': 94933744Sache *np = '\0'; 95033548Sjkh return -1; 95133744Sache default: 95233747Sache if (code & 0xff80) { 95333747Sache if (table_loaded) 95433747Sache code = find_lcode(code, u2w); 95533747Sache else if (code & 0xff00) 95633745Sache code = '?'; 95733744Sache } 95833744Sache *np++ = code; 95933744Sache break; 96033548Sjkh } 96133548Sjkh /* 96233548Sjkh * See above 96333548Sjkh */ 96433548Sjkh if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 96533548Sjkh && np > ep) { 96633548Sjkh np[-1] = 0; 96733548Sjkh return -1; 96833548Sjkh } 96933744Sache cp += 2; 97033548Sjkh } 97133548Sjkh return chksum; 97233548Sjkh} 97333548Sjkh 97433548Sjkh/* 97533548Sjkh * Compute the checksum of a DOS filename for Win95 use 97633548Sjkh */ 97733548Sjkhu_int8_t 97833548SjkhwinChksum(name) 97933548Sjkh u_int8_t *name; 98033548Sjkh{ 98133548Sjkh int i; 98233548Sjkh u_int8_t s; 98333548Sjkh 98433548Sjkh for (s = 0, i = 11; --i >= 0; s += *name++) 98533548Sjkh s = (s << 7)|(s >> 1); 98633548Sjkh return s; 98733548Sjkh} 98833548Sjkh 98933548Sjkh/* 99033548Sjkh * Determine the number of slots necessary for Win95 names 99133548Sjkh */ 99233548Sjkhint 99333548SjkhwinSlotCnt(un, unlen) 99433548Sjkh const u_char *un; 99533548Sjkh int unlen; 99633548Sjkh{ 99733548Sjkh for (un += unlen; unlen > 0; unlen--) 99833548Sjkh if (*--un != ' ' && *un != '.') 99933548Sjkh break; 100033548Sjkh if (unlen > WIN_MAXLEN) 100133548Sjkh return 0; 100233548Sjkh return howmany(unlen, WIN_CHARS); 100333548Sjkh} 1004