1*febe0fa6Sclaudio /* $OpenBSD: msdosfs_conv.c,v 1.22 2024/09/12 09:07:28 claudio Exp $ */ 2b099d67bSprovos /* $NetBSD: msdosfs_conv.c,v 1.24 1997/10/17 11:23:54 ws Exp $ */ 3df930be7Sderaadt 416bf7bd1Sderaadt /*- 5b099d67bSprovos * Copyright (C) 1995, 1997 Wolfgang Solfrank. 6b099d67bSprovos * Copyright (C) 1995, 1997 TooLs GmbH. 716bf7bd1Sderaadt * All rights reserved. 816bf7bd1Sderaadt * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 916bf7bd1Sderaadt * 1016bf7bd1Sderaadt * Redistribution and use in source and binary forms, with or without 1116bf7bd1Sderaadt * modification, are permitted provided that the following conditions 1216bf7bd1Sderaadt * are met: 1316bf7bd1Sderaadt * 1. Redistributions of source code must retain the above copyright 1416bf7bd1Sderaadt * notice, this list of conditions and the following disclaimer. 1516bf7bd1Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 1616bf7bd1Sderaadt * notice, this list of conditions and the following disclaimer in the 1716bf7bd1Sderaadt * documentation and/or other materials provided with the distribution. 1816bf7bd1Sderaadt * 3. All advertising materials mentioning features or use of this software 1916bf7bd1Sderaadt * must display the following acknowledgement: 2016bf7bd1Sderaadt * This product includes software developed by TooLs GmbH. 2116bf7bd1Sderaadt * 4. The name of TooLs GmbH may not be used to endorse or promote products 2216bf7bd1Sderaadt * derived from this software without specific prior written permission. 2316bf7bd1Sderaadt * 2416bf7bd1Sderaadt * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2516bf7bd1Sderaadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2616bf7bd1Sderaadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2716bf7bd1Sderaadt * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2816bf7bd1Sderaadt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2916bf7bd1Sderaadt * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3016bf7bd1Sderaadt * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3116bf7bd1Sderaadt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3216bf7bd1Sderaadt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3316bf7bd1Sderaadt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3416bf7bd1Sderaadt */ 35df930be7Sderaadt /* 36df930be7Sderaadt * Written by Paul Popelka (paulp@uts.amdahl.com) 37df930be7Sderaadt * 38df930be7Sderaadt * You can do anything you want with this software, just don't say you wrote 39df930be7Sderaadt * it, and don't remove this notice. 40df930be7Sderaadt * 41df930be7Sderaadt * This software is provided "as is". 42df930be7Sderaadt * 43df930be7Sderaadt * The author supplies this software to be publicly redistributed on the 44df930be7Sderaadt * understanding that the author is not responsible for the correct 45df930be7Sderaadt * functioning of this software in any circumstances and is not liable for 46df930be7Sderaadt * any damages caused by this software. 47df930be7Sderaadt * 48df930be7Sderaadt * October 1992 49df930be7Sderaadt */ 50df930be7Sderaadt 51df930be7Sderaadt /* 52df930be7Sderaadt * System include files. 53df930be7Sderaadt */ 54df930be7Sderaadt #include <sys/param.h> 55879b3eabSniklas #include <sys/systm.h> 56df930be7Sderaadt #include <sys/time.h> 57df930be7Sderaadt #include <sys/kernel.h> /* defines tz */ 5816bf7bd1Sderaadt #include <sys/dirent.h> 5916bf7bd1Sderaadt #include <sys/vnode.h> 60fde894e5Stedu #include <sys/lock.h> 61df930be7Sderaadt 62df930be7Sderaadt /* 63df930be7Sderaadt * MSDOSFS include files. 64df930be7Sderaadt */ 65df930be7Sderaadt #include <msdosfs/direntry.h> 6616bf7bd1Sderaadt #include <msdosfs/denode.h> 67df930be7Sderaadt 68df930be7Sderaadt /* 69df930be7Sderaadt * Days in each month in a regular year. 70df930be7Sderaadt */ 71359756ecSmickey const u_short regyear[] = { 72df930be7Sderaadt 31, 28, 31, 30, 31, 30, 73df930be7Sderaadt 31, 31, 30, 31, 30, 31 74df930be7Sderaadt }; 75df930be7Sderaadt 76df930be7Sderaadt /* 77df930be7Sderaadt * Days in each month in a leap year. 78df930be7Sderaadt */ 79359756ecSmickey const u_short leapyear[] = { 80df930be7Sderaadt 31, 29, 31, 30, 31, 30, 81df930be7Sderaadt 31, 31, 30, 31, 30, 31 82df930be7Sderaadt }; 83df930be7Sderaadt 84df930be7Sderaadt /* 85df930be7Sderaadt * Variables used to remember parts of the last time conversion. Maybe we 86df930be7Sderaadt * can avoid a full conversion. 87df930be7Sderaadt */ 8835cd4f41Skrw time_t lasttime; 8982fa9538Stedu uint32_t lastday; 90df930be7Sderaadt u_short lastddate; 91df930be7Sderaadt u_short lastdtime; 92df930be7Sderaadt 93df930be7Sderaadt /* 94df930be7Sderaadt * Convert the unix version of time to dos's idea of time to be used in 95df930be7Sderaadt * file timestamps. The passed in unix time is assumed to be in GMT. 96df930be7Sderaadt */ 97df930be7Sderaadt void 987d80fe84Sjasper unix2dostime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp) 99df930be7Sderaadt { 10035cd4f41Skrw time_t t; 10182fa9538Stedu uint32_t days; 10282fa9538Stedu uint32_t inc; 10382fa9538Stedu uint32_t year; 10482fa9538Stedu uint32_t month; 105359756ecSmickey const u_short *months; 106df930be7Sderaadt 107df930be7Sderaadt /* 108df930be7Sderaadt * If the time from the last conversion is the same as now, then 109df930be7Sderaadt * skip the computations and use the saved result. 110df930be7Sderaadt */ 111612d413bScheloha t = tsp->tv_sec; 112b099d67bSprovos t &= ~1; 11335cd4f41Skrw /* 11435cd4f41Skrw * Before 1/1/1980 there is only a timeless void. After 12/31/2107 11535cd4f41Skrw * there is only Cthulhu. 11635cd4f41Skrw */ 11735cd4f41Skrw #define DOSEPOCH 315532800LL 11835cd4f41Skrw #define DOSENDTIME 4354775999LL 11935cd4f41Skrw if (t < DOSEPOCH || t > DOSENDTIME) 12035cd4f41Skrw t = DOSEPOCH; 12135cd4f41Skrw 122df930be7Sderaadt if (lasttime != t) { 123df930be7Sderaadt lasttime = t; 124879b3eabSniklas lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT) 125df930be7Sderaadt + (((t / 60) % 60) << DT_MINUTES_SHIFT) 126df930be7Sderaadt + (((t / 3600) % 24) << DT_HOURS_SHIFT); 127df930be7Sderaadt 128df930be7Sderaadt /* 129df930be7Sderaadt * If the number of days since 1970 is the same as the last 130df930be7Sderaadt * time we did the computation then skip all this leap year 131df930be7Sderaadt * and month stuff. 132df930be7Sderaadt */ 133df930be7Sderaadt days = t / (24 * 60 * 60); 134df930be7Sderaadt if (days != lastday) { 135df930be7Sderaadt lastday = days; 136df930be7Sderaadt for (year = 1970;; year++) { 137df930be7Sderaadt inc = year & 0x03 ? 365 : 366; 138df930be7Sderaadt if (days < inc) 139df930be7Sderaadt break; 140df930be7Sderaadt days -= inc; 141df930be7Sderaadt } 142df930be7Sderaadt months = year & 0x03 ? regyear : leapyear; 143df930be7Sderaadt for (month = 0; month < 12; month++) { 144df930be7Sderaadt if (days < months[month]) 145df930be7Sderaadt break; 146df930be7Sderaadt days -= months[month]; 147df930be7Sderaadt } 148df930be7Sderaadt lastddate = ((days + 1) << DD_DAY_SHIFT) 149df930be7Sderaadt + ((month + 1) << DD_MONTH_SHIFT); 150df930be7Sderaadt /* 151df930be7Sderaadt * Remember dos's idea of time is relative to 1980. 152df930be7Sderaadt * unix's is relative to 1970. If somehow we get a 153df930be7Sderaadt * time before 1980 then don't give totally crazy 154df930be7Sderaadt * results. 155df930be7Sderaadt */ 156df930be7Sderaadt if (year > 1980) 157df930be7Sderaadt lastddate += (year - 1980) << DD_YEAR_SHIFT; 158df930be7Sderaadt } 159df930be7Sderaadt } 160bae0976aSderaadt 161bae0976aSderaadt if (dtp != NULL) 162df930be7Sderaadt *dtp = lastdtime; 163b099d67bSprovos if (dhp != NULL) 164b099d67bSprovos *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 165b099d67bSprovos 166df930be7Sderaadt *ddp = lastddate; 167df930be7Sderaadt } 168df930be7Sderaadt 169df930be7Sderaadt /* 170df930be7Sderaadt * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that 171df930be7Sderaadt * interval there were 8 regular years and 2 leap years. 172df930be7Sderaadt */ 173df930be7Sderaadt #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) 174df930be7Sderaadt 175df930be7Sderaadt u_short lastdosdate; 17682fa9538Stedu uint32_t lastseconds; 177df930be7Sderaadt 178df930be7Sderaadt /* 179df930be7Sderaadt * Convert from dos' idea of time to unix'. This will probably only be 180df930be7Sderaadt * called from the stat(), and fstat() system calls and so probably need 181df930be7Sderaadt * not be too efficient. 182df930be7Sderaadt */ 183df930be7Sderaadt void 1847d80fe84Sjasper dos2unixtime(u_int dd, u_int dt, u_int dh, struct timespec *tsp) 185df930be7Sderaadt { 18682fa9538Stedu uint32_t seconds; 18782fa9538Stedu uint32_t m, month; 18882fa9538Stedu uint32_t y, year; 18982fa9538Stedu uint32_t days; 190359756ecSmickey const u_short *months; 191df930be7Sderaadt 192879b3eabSniklas if (dd == 0) { 193879b3eabSniklas /* 194879b3eabSniklas * Uninitialized field, return the epoch. 195879b3eabSniklas */ 196879b3eabSniklas tsp->tv_sec = 0; 197879b3eabSniklas tsp->tv_nsec = 0; 198879b3eabSniklas return; 199879b3eabSniklas } 200879b3eabSniklas seconds = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2 201df930be7Sderaadt + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 202b099d67bSprovos + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 203b099d67bSprovos + dh / 100; 204df930be7Sderaadt /* 205df930be7Sderaadt * If the year, month, and day from the last conversion are the 206df930be7Sderaadt * same then use the saved value. 207df930be7Sderaadt */ 208df930be7Sderaadt if (lastdosdate != dd) { 209df930be7Sderaadt lastdosdate = dd; 210df930be7Sderaadt days = 0; 211df930be7Sderaadt year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 212df930be7Sderaadt for (y = 0; y < year; y++) 213df930be7Sderaadt days += y & 0x03 ? 365 : 366; 214df930be7Sderaadt months = year & 0x03 ? regyear : leapyear; 215df930be7Sderaadt /* 216df930be7Sderaadt * Prevent going from 0 to 0xffffffff in the following 217df930be7Sderaadt * loop. 218df930be7Sderaadt */ 219df930be7Sderaadt month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 220df930be7Sderaadt if (month == 0) { 2217e0e4efdSsf printf("dos2unixtime(): month value out of range (%u)\n", 222df930be7Sderaadt month); 223df930be7Sderaadt month = 1; 224df930be7Sderaadt } 225df930be7Sderaadt for (m = 0; m < month - 1; m++) 226df930be7Sderaadt days += months[m]; 227df930be7Sderaadt days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 228df930be7Sderaadt lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 229df930be7Sderaadt } 230612d413bScheloha tsp->tv_sec = seconds + lastseconds; 231b099d67bSprovos tsp->tv_nsec = (dh % 100) * 10000000; 232df930be7Sderaadt } 233df930be7Sderaadt 234359756ecSmickey static const u_char 23516bf7bd1Sderaadt unix2dos[256] = { 23616bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 23716bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 23816bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 23916bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 24078b4a1c4Smillert 0, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 24178b4a1c4Smillert 0x28, 0x29, 0, 0, 0, 0x2d, 0, 0, /* 28-2f */ 24216bf7bd1Sderaadt 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 24378b4a1c4Smillert 0x38, 0x39, 0, 0, 0, 0, 0, 0, /* 38-3f */ 24416bf7bd1Sderaadt 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 24516bf7bd1Sderaadt 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 24616bf7bd1Sderaadt 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 24778b4a1c4Smillert 0x58, 0x59, 0x5a, 0, 0, 0, 0x5e, 0x5f, /* 58-5f */ 24816bf7bd1Sderaadt 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 24916bf7bd1Sderaadt 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 25016bf7bd1Sderaadt 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 25116bf7bd1Sderaadt 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 25216bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 25316bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 25416bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 25516bf7bd1Sderaadt 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 25616bf7bd1Sderaadt 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 25716bf7bd1Sderaadt 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 25816bf7bd1Sderaadt 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 25916bf7bd1Sderaadt 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 26016bf7bd1Sderaadt 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 26116bf7bd1Sderaadt 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 26216bf7bd1Sderaadt 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 26316bf7bd1Sderaadt 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 26416bf7bd1Sderaadt 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 26516bf7bd1Sderaadt 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 26616bf7bd1Sderaadt 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 26716bf7bd1Sderaadt 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 26816bf7bd1Sderaadt }; 26916bf7bd1Sderaadt 270359756ecSmickey static const u_char 27116bf7bd1Sderaadt dos2unix[256] = { 27216bf7bd1Sderaadt 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 27316bf7bd1Sderaadt 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 27416bf7bd1Sderaadt 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 27516bf7bd1Sderaadt 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 27616bf7bd1Sderaadt 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 277*febe0fa6Sclaudio 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x3f, /* 28-2f */ 27816bf7bd1Sderaadt 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 27916bf7bd1Sderaadt 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 28016bf7bd1Sderaadt 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 28116bf7bd1Sderaadt 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 28216bf7bd1Sderaadt 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 28316bf7bd1Sderaadt 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 28416bf7bd1Sderaadt 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 28516bf7bd1Sderaadt 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 28616bf7bd1Sderaadt 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 28716bf7bd1Sderaadt 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 28816bf7bd1Sderaadt 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 28916bf7bd1Sderaadt 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 29016bf7bd1Sderaadt 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 29116bf7bd1Sderaadt 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 29216bf7bd1Sderaadt 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 29316bf7bd1Sderaadt 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 29416bf7bd1Sderaadt 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 29516bf7bd1Sderaadt 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 29616bf7bd1Sderaadt 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 29716bf7bd1Sderaadt 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 29816bf7bd1Sderaadt 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 29916bf7bd1Sderaadt 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 30016bf7bd1Sderaadt 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 30116bf7bd1Sderaadt 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 30216bf7bd1Sderaadt 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 30316bf7bd1Sderaadt 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 30416bf7bd1Sderaadt }; 30516bf7bd1Sderaadt 306359756ecSmickey static const u_char 30716bf7bd1Sderaadt u2l[256] = { 30816bf7bd1Sderaadt 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 30916bf7bd1Sderaadt 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 31016bf7bd1Sderaadt 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 31116bf7bd1Sderaadt 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 31216bf7bd1Sderaadt 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 313*febe0fa6Sclaudio 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x3f, /* 28-2f */ 31416bf7bd1Sderaadt 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 31516bf7bd1Sderaadt 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 31616bf7bd1Sderaadt 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 31716bf7bd1Sderaadt 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 31816bf7bd1Sderaadt 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 31916bf7bd1Sderaadt 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 32016bf7bd1Sderaadt 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 32116bf7bd1Sderaadt 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 32216bf7bd1Sderaadt 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 32316bf7bd1Sderaadt 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 32416bf7bd1Sderaadt 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 32516bf7bd1Sderaadt 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 32616bf7bd1Sderaadt 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 32716bf7bd1Sderaadt 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 32816bf7bd1Sderaadt 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 32916bf7bd1Sderaadt 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 33016bf7bd1Sderaadt 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 33116bf7bd1Sderaadt 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 33216bf7bd1Sderaadt 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 33316bf7bd1Sderaadt 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 33416bf7bd1Sderaadt 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 33516bf7bd1Sderaadt 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 33616bf7bd1Sderaadt 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 33716bf7bd1Sderaadt 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 33816bf7bd1Sderaadt 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 33916bf7bd1Sderaadt 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 34016bf7bd1Sderaadt }; 341df930be7Sderaadt 342df930be7Sderaadt /* 343df930be7Sderaadt * DOS filenames are made of 2 parts, the name part and the extension part. 344df930be7Sderaadt * The name part is 8 characters long and the extension part is 3 345df930be7Sderaadt * characters long. They may contain trailing blanks if the name or 346df930be7Sderaadt * extension are not long enough to fill their respective fields. 347df930be7Sderaadt */ 348df930be7Sderaadt 349df930be7Sderaadt /* 350df930be7Sderaadt * Convert a DOS filename to a unix filename. And, return the number of 351df930be7Sderaadt * characters in the resulting unix filename excluding the terminating 352df930be7Sderaadt * null. 353df930be7Sderaadt */ 354df930be7Sderaadt int 3557d80fe84Sjasper dos2unixfn(u_char dn[11], u_char *un, int lower) 356df930be7Sderaadt { 357df930be7Sderaadt int i; 35816bf7bd1Sderaadt int thislong = 1; 359df930be7Sderaadt u_char c; 360df930be7Sderaadt 361df930be7Sderaadt /* 362df930be7Sderaadt * If first char of the filename is SLOT_E5 (0x05), then the real 363df930be7Sderaadt * first char of the filename should be 0xe5. But, they couldn't 364df930be7Sderaadt * just have a 0xe5 mean 0xe5 because that is used to mean a freed 365df930be7Sderaadt * directory slot. Another dos quirk. 366df930be7Sderaadt */ 36716bf7bd1Sderaadt if (*dn == SLOT_E5) 36816bf7bd1Sderaadt c = dos2unix[0xe5]; 36916bf7bd1Sderaadt else 37016bf7bd1Sderaadt c = dos2unix[*dn]; 37116bf7bd1Sderaadt *un++ = lower ? u2l[c] : c; 37216bf7bd1Sderaadt dn++; 37316bf7bd1Sderaadt 37416bf7bd1Sderaadt /* 37516bf7bd1Sderaadt * Copy the name portion into the unix filename string. 37616bf7bd1Sderaadt */ 37716bf7bd1Sderaadt for (i = 1; i < 8 && *dn != ' '; i++) { 37816bf7bd1Sderaadt c = dos2unix[*dn++]; 37916bf7bd1Sderaadt *un++ = lower ? u2l[c] : c; 38016bf7bd1Sderaadt thislong++; 38116bf7bd1Sderaadt } 38216bf7bd1Sderaadt dn += 8 - i; 38316bf7bd1Sderaadt 38416bf7bd1Sderaadt /* 38516bf7bd1Sderaadt * Now, if there is an extension then put in a period and copy in 38616bf7bd1Sderaadt * the extension. 38716bf7bd1Sderaadt */ 38816bf7bd1Sderaadt if (*dn != ' ') { 38916bf7bd1Sderaadt *un++ = '.'; 39016bf7bd1Sderaadt thislong++; 39116bf7bd1Sderaadt for (i = 0; i < 3 && *dn != ' '; i++) { 39216bf7bd1Sderaadt c = dos2unix[*dn++]; 39316bf7bd1Sderaadt *un++ = lower ? u2l[c] : c; 39416bf7bd1Sderaadt thislong++; 39516bf7bd1Sderaadt } 39616bf7bd1Sderaadt } 39716bf7bd1Sderaadt *un++ = 0; 398df930be7Sderaadt 399df930be7Sderaadt return (thislong); 400df930be7Sderaadt } 401df930be7Sderaadt 402df930be7Sderaadt /* 40316bf7bd1Sderaadt * Convert a unix filename to a DOS filename according to Win95 rules. 40416bf7bd1Sderaadt * If applicable and gen is not 0, it is inserted into the converted 40516bf7bd1Sderaadt * filename as a generation number. 40616bf7bd1Sderaadt * Returns 40716bf7bd1Sderaadt * 0 if name couldn't be converted 40816bf7bd1Sderaadt * 1 if the converted name is the same as the original 40916bf7bd1Sderaadt * (no long filename entry necessary for Win95) 41016bf7bd1Sderaadt * 2 if conversion was successful 41116bf7bd1Sderaadt * 3 if conversion was successful and generation number was inserted 412df930be7Sderaadt */ 41316bf7bd1Sderaadt int 414e6f855f7Skrw unix2dosfn(u_char *un, u_char dn[11], int unlen, u_int gen) 415df930be7Sderaadt { 41616bf7bd1Sderaadt int i, j, l; 41716bf7bd1Sderaadt int conv = 1; 41816bf7bd1Sderaadt u_char *cp, *dp, *dp1; 41916bf7bd1Sderaadt u_char gentext[6]; 420df930be7Sderaadt 421df930be7Sderaadt /* 422df930be7Sderaadt * Fill the dos filename string with blanks. These are DOS's pad 423df930be7Sderaadt * characters. 424df930be7Sderaadt */ 42516bf7bd1Sderaadt for (i = 0; i < 11; i++) 426df930be7Sderaadt dn[i] = ' '; 427df930be7Sderaadt 428df930be7Sderaadt /* 429df930be7Sderaadt * The filenames "." and ".." are handled specially, since they 430df930be7Sderaadt * don't follow dos filename rules. 431df930be7Sderaadt */ 432df930be7Sderaadt if (un[0] == '.' && unlen == 1) { 433df930be7Sderaadt dn[0] = '.'; 43416bf7bd1Sderaadt return gen <= 1; 435df930be7Sderaadt } 436df930be7Sderaadt if (un[0] == '.' && un[1] == '.' && unlen == 2) { 437df930be7Sderaadt dn[0] = '.'; 438df930be7Sderaadt dn[1] = '.'; 43916bf7bd1Sderaadt return gen <= 1; 440df930be7Sderaadt } 441df930be7Sderaadt 442df930be7Sderaadt /* 44316bf7bd1Sderaadt * Filenames with only blanks and dots are not allowed! 444df930be7Sderaadt */ 44516bf7bd1Sderaadt for (cp = un, i = unlen; --i >= 0; cp++) 44616bf7bd1Sderaadt if (*cp != ' ' && *cp != '.') 44716bf7bd1Sderaadt break; 44816bf7bd1Sderaadt if (i < 0) 44916bf7bd1Sderaadt return 0; 45016bf7bd1Sderaadt 45116bf7bd1Sderaadt /* 45216bf7bd1Sderaadt * Now find the extension 45316bf7bd1Sderaadt * Note: dot as first char doesn't start extension 45416bf7bd1Sderaadt * and trailing dots and blanks are ignored 45516bf7bd1Sderaadt */ 45616bf7bd1Sderaadt dp = dp1 = 0; 45716bf7bd1Sderaadt for (cp = un + 1, i = unlen - 1; --i >= 0;) { 45816bf7bd1Sderaadt switch (*cp++) { 45916bf7bd1Sderaadt case '.': 46016bf7bd1Sderaadt if (!dp1) 46116bf7bd1Sderaadt dp1 = cp; 46216bf7bd1Sderaadt break; 46316bf7bd1Sderaadt case ' ': 46416bf7bd1Sderaadt break; 46516bf7bd1Sderaadt default: 46616bf7bd1Sderaadt if (dp1) 46716bf7bd1Sderaadt dp = dp1; 46816bf7bd1Sderaadt dp1 = 0; 46916bf7bd1Sderaadt break; 47016bf7bd1Sderaadt } 471df930be7Sderaadt } 472df930be7Sderaadt 473df930be7Sderaadt /* 47416bf7bd1Sderaadt * Now convert it 475df930be7Sderaadt */ 47616bf7bd1Sderaadt if (dp) { 47716bf7bd1Sderaadt if (dp1) 47816bf7bd1Sderaadt l = dp1 - dp; 47916bf7bd1Sderaadt else 48016bf7bd1Sderaadt l = unlen - (dp - un); 48116bf7bd1Sderaadt for (i = 0, j = 8; i < l && j < 11; i++, j++) { 48216bf7bd1Sderaadt if (dp[i] != (dn[j] = unix2dos[dp[i]]) 48316bf7bd1Sderaadt && conv != 3) 48416bf7bd1Sderaadt conv = 2; 48516bf7bd1Sderaadt if (!dn[j]) { 48616bf7bd1Sderaadt conv = 3; 48716bf7bd1Sderaadt dn[j--] = ' '; 48816bf7bd1Sderaadt } 48916bf7bd1Sderaadt } 49016bf7bd1Sderaadt if (i < l) 49116bf7bd1Sderaadt conv = 3; 49216bf7bd1Sderaadt dp--; 49316bf7bd1Sderaadt } else { 49436dba039Sjsg for (dp = cp; *--dp == ' ' || *dp == '.';) 49536dba039Sjsg ; 49616bf7bd1Sderaadt dp++; 49716bf7bd1Sderaadt } 49816bf7bd1Sderaadt 49916bf7bd1Sderaadt /* 50016bf7bd1Sderaadt * Now convert the rest of the name 50116bf7bd1Sderaadt */ 50216bf7bd1Sderaadt for (i = j = 0; un < dp && j < 8; i++, j++, un++) { 50316bf7bd1Sderaadt if (*un != (dn[j] = unix2dos[*un]) 50416bf7bd1Sderaadt && conv != 3) 50516bf7bd1Sderaadt conv = 2; 50616bf7bd1Sderaadt if (!dn[j]) { 50716bf7bd1Sderaadt conv = 3; 50816bf7bd1Sderaadt dn[j--] = ' '; 50916bf7bd1Sderaadt } 51016bf7bd1Sderaadt } 51116bf7bd1Sderaadt if (un < dp) 51216bf7bd1Sderaadt conv = 3; 51316bf7bd1Sderaadt /* 51416bf7bd1Sderaadt * If we didn't have any chars in filename, 51516bf7bd1Sderaadt * generate a default 51616bf7bd1Sderaadt */ 51716bf7bd1Sderaadt if (!j) 51816bf7bd1Sderaadt dn[0] = '_'; 51916bf7bd1Sderaadt 52016bf7bd1Sderaadt /* 52116bf7bd1Sderaadt * The first character cannot be E5, 52216bf7bd1Sderaadt * because that means a deleted entry 52316bf7bd1Sderaadt */ 52416bf7bd1Sderaadt if (dn[0] == 0xe5) 525df930be7Sderaadt dn[0] = SLOT_E5; 526df930be7Sderaadt 527df930be7Sderaadt /* 52816bf7bd1Sderaadt * If there wasn't any char dropped, 52916bf7bd1Sderaadt * there is no place for generation numbers 530df930be7Sderaadt */ 53116bf7bd1Sderaadt if (conv != 3) { 53216bf7bd1Sderaadt if (gen > 1) 53316bf7bd1Sderaadt return 0; 53416bf7bd1Sderaadt return conv; 535df930be7Sderaadt } 536df930be7Sderaadt 537df930be7Sderaadt /* 53816bf7bd1Sderaadt * Now insert the generation number into the filename part 539df930be7Sderaadt */ 54016bf7bd1Sderaadt for (cp = gentext + sizeof(gentext); cp > gentext && gen; gen /= 10) 54116bf7bd1Sderaadt *--cp = gen % 10 + '0'; 54216bf7bd1Sderaadt if (gen) 54316bf7bd1Sderaadt return 0; 54436dba039Sjsg for (i = 8; dn[--i] == ' ';) 54536dba039Sjsg ; 54616bf7bd1Sderaadt if (gentext + sizeof(gentext) - cp + 1 > 8 - i) 54716bf7bd1Sderaadt i = 8 - (gentext + sizeof(gentext) - cp + 1); 54816bf7bd1Sderaadt dn[i++] = '~'; 54916bf7bd1Sderaadt while (cp < gentext + sizeof(gentext)) 550b099d67bSprovos dn[i++] = *cp++; 55116bf7bd1Sderaadt return 3; 552df930be7Sderaadt } 55316bf7bd1Sderaadt 55416bf7bd1Sderaadt /* 55516bf7bd1Sderaadt * Create a Win95 long name directory entry 55616bf7bd1Sderaadt * Note: assumes that the filename is valid, 55716bf7bd1Sderaadt * i.e. doesn't consist solely of blanks and dots 55816bf7bd1Sderaadt */ 55916bf7bd1Sderaadt int 5607d80fe84Sjasper unix2winfn(u_char *un, int unlen, struct winentry *wep, int cnt, int chksum) 56116bf7bd1Sderaadt { 56216bf7bd1Sderaadt u_int8_t *cp; 56316bf7bd1Sderaadt int i; 56416bf7bd1Sderaadt 56516bf7bd1Sderaadt /* 56616bf7bd1Sderaadt * Drop trailing blanks and dots 56716bf7bd1Sderaadt */ 56836dba039Sjsg for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--) 56936dba039Sjsg ; 57016bf7bd1Sderaadt 57116bf7bd1Sderaadt un += (cnt - 1) * WIN_CHARS; 57216bf7bd1Sderaadt unlen -= (cnt - 1) * WIN_CHARS; 57316bf7bd1Sderaadt 57416bf7bd1Sderaadt /* 57516bf7bd1Sderaadt * Initialize winentry to some useful default 57616bf7bd1Sderaadt */ 57736dba039Sjsg for (cp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *cp++ = 0xff) 57836dba039Sjsg ; 57916bf7bd1Sderaadt wep->weCnt = cnt; 58016bf7bd1Sderaadt wep->weAttributes = ATTR_WIN95; 58116bf7bd1Sderaadt wep->weReserved1 = 0; 58216bf7bd1Sderaadt wep->weChksum = chksum; 58316bf7bd1Sderaadt wep->weReserved2 = 0; 58416bf7bd1Sderaadt 58516bf7bd1Sderaadt /* 58616bf7bd1Sderaadt * Now convert the filename parts 58716bf7bd1Sderaadt */ 58816bf7bd1Sderaadt for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 58916bf7bd1Sderaadt if (--unlen < 0) 59016bf7bd1Sderaadt goto done; 59116bf7bd1Sderaadt *cp++ = *un++; 59216bf7bd1Sderaadt *cp++ = 0; 59316bf7bd1Sderaadt } 59416bf7bd1Sderaadt for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 59516bf7bd1Sderaadt if (--unlen < 0) 59616bf7bd1Sderaadt goto done; 59716bf7bd1Sderaadt *cp++ = *un++; 59816bf7bd1Sderaadt *cp++ = 0; 59916bf7bd1Sderaadt } 60016bf7bd1Sderaadt for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 60116bf7bd1Sderaadt if (--unlen < 0) 60216bf7bd1Sderaadt goto done; 60316bf7bd1Sderaadt *cp++ = *un++; 60416bf7bd1Sderaadt *cp++ = 0; 60516bf7bd1Sderaadt } 60616bf7bd1Sderaadt if (!unlen) 60716bf7bd1Sderaadt wep->weCnt |= WIN_LAST; 60816bf7bd1Sderaadt return unlen; 60916bf7bd1Sderaadt 61016bf7bd1Sderaadt done: 61116bf7bd1Sderaadt *cp++ = 0; 61216bf7bd1Sderaadt *cp++ = 0; 61316bf7bd1Sderaadt wep->weCnt |= WIN_LAST; 61416bf7bd1Sderaadt return 0; 61516bf7bd1Sderaadt } 61616bf7bd1Sderaadt 61716bf7bd1Sderaadt /* 61816bf7bd1Sderaadt * Compare our filename to the one in the Win95 entry 61916bf7bd1Sderaadt * Returns the checksum or -1 if no match 62016bf7bd1Sderaadt */ 62116bf7bd1Sderaadt int 6227d80fe84Sjasper winChkName(u_char *un, int unlen, struct winentry *wep, int chksum) 62316bf7bd1Sderaadt { 62416bf7bd1Sderaadt u_int8_t *cp; 62516bf7bd1Sderaadt int i; 62616bf7bd1Sderaadt 62716bf7bd1Sderaadt /* 62816bf7bd1Sderaadt * First compare checksums 62916bf7bd1Sderaadt */ 63016bf7bd1Sderaadt if (wep->weCnt&WIN_LAST) 63116bf7bd1Sderaadt chksum = wep->weChksum; 63216bf7bd1Sderaadt else if (chksum != wep->weChksum) 63316bf7bd1Sderaadt chksum = -1; 63416bf7bd1Sderaadt if (chksum == -1) 63516bf7bd1Sderaadt return -1; 63616bf7bd1Sderaadt 63716bf7bd1Sderaadt /* 63816bf7bd1Sderaadt * Offset of this entry 63916bf7bd1Sderaadt */ 64016bf7bd1Sderaadt i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 64116bf7bd1Sderaadt if ((unlen -= i) <= 0) 64216bf7bd1Sderaadt return -1; 64316bf7bd1Sderaadt un += i; 64416bf7bd1Sderaadt 64559af49d1Smillert if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS) 64659af49d1Smillert return -1; 64759af49d1Smillert 64816bf7bd1Sderaadt /* 64916bf7bd1Sderaadt * Compare the name parts 65016bf7bd1Sderaadt */ 65116bf7bd1Sderaadt for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 65216bf7bd1Sderaadt if (--unlen < 0) { 65316bf7bd1Sderaadt if (!*cp++ && !*cp) 65416bf7bd1Sderaadt return chksum; 65516bf7bd1Sderaadt return -1; 65616bf7bd1Sderaadt } 65716bf7bd1Sderaadt if (u2l[*cp++] != u2l[*un++] || *cp++) 65816bf7bd1Sderaadt return -1; 65916bf7bd1Sderaadt } 66016bf7bd1Sderaadt for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 66116bf7bd1Sderaadt if (--unlen < 0) { 66216bf7bd1Sderaadt if (!*cp++ && !*cp) 66316bf7bd1Sderaadt return chksum; 66416bf7bd1Sderaadt return -1; 66516bf7bd1Sderaadt } 66616bf7bd1Sderaadt if (u2l[*cp++] != u2l[*un++] || *cp++) 66716bf7bd1Sderaadt return -1; 66816bf7bd1Sderaadt } 66916bf7bd1Sderaadt for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 67016bf7bd1Sderaadt if (--unlen < 0) { 67116bf7bd1Sderaadt if (!*cp++ && !*cp) 67216bf7bd1Sderaadt return chksum; 67316bf7bd1Sderaadt return -1; 67416bf7bd1Sderaadt } 67516bf7bd1Sderaadt if (u2l[*cp++] != u2l[*un++] || *cp++) 67616bf7bd1Sderaadt return -1; 67716bf7bd1Sderaadt } 67816bf7bd1Sderaadt return chksum; 67916bf7bd1Sderaadt } 68016bf7bd1Sderaadt 68116bf7bd1Sderaadt /* 68216bf7bd1Sderaadt * Convert Win95 filename to dirbuf. 68316bf7bd1Sderaadt * Returns the checksum or -1 if impossible 68416bf7bd1Sderaadt */ 68516bf7bd1Sderaadt int 6867d80fe84Sjasper win2unixfn(struct winentry *wep, struct dirent *dp, int chksum) 68716bf7bd1Sderaadt { 68816bf7bd1Sderaadt u_int8_t *cp; 68916bf7bd1Sderaadt u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN; 69016bf7bd1Sderaadt int i; 69116bf7bd1Sderaadt 69216bf7bd1Sderaadt if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 69316bf7bd1Sderaadt || !(wep->weCnt&WIN_CNT)) 69416bf7bd1Sderaadt return -1; 69516bf7bd1Sderaadt 69616bf7bd1Sderaadt /* 69716bf7bd1Sderaadt * First compare checksums 69816bf7bd1Sderaadt */ 69916bf7bd1Sderaadt if (wep->weCnt&WIN_LAST) { 70016bf7bd1Sderaadt chksum = wep->weChksum; 70116bf7bd1Sderaadt /* 70216bf7bd1Sderaadt * This works even though d_namlen is one byte! 70316bf7bd1Sderaadt */ 70416bf7bd1Sderaadt dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS; 70516bf7bd1Sderaadt } else if (chksum != wep->weChksum) 70616bf7bd1Sderaadt chksum = -1; 70716bf7bd1Sderaadt if (chksum == -1) 70816bf7bd1Sderaadt return -1; 70916bf7bd1Sderaadt 71016bf7bd1Sderaadt /* 71116bf7bd1Sderaadt * Offset of this entry 71216bf7bd1Sderaadt */ 71316bf7bd1Sderaadt i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS; 71416bf7bd1Sderaadt np = (u_int8_t *)dp->d_name + i; 71516bf7bd1Sderaadt 71616bf7bd1Sderaadt /* 71716bf7bd1Sderaadt * Convert the name parts 71816bf7bd1Sderaadt */ 71916bf7bd1Sderaadt for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 72016bf7bd1Sderaadt switch (*np++ = *cp++) { 72116bf7bd1Sderaadt case 0: 72216bf7bd1Sderaadt dp->d_namlen -= sizeof(wep->wePart2)/2 72316bf7bd1Sderaadt + sizeof(wep->wePart3)/2 + i + 1; 72416bf7bd1Sderaadt return chksum; 72516bf7bd1Sderaadt case '/': 72616bf7bd1Sderaadt np[-1] = 0; 72716bf7bd1Sderaadt return -1; 72816bf7bd1Sderaadt } 72916bf7bd1Sderaadt /* 73016bf7bd1Sderaadt * The size comparison should result in the compiler 73116bf7bd1Sderaadt * optimizing the whole if away 73216bf7bd1Sderaadt */ 73316bf7bd1Sderaadt if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2 73416bf7bd1Sderaadt && np > ep) { 73516bf7bd1Sderaadt np[-1] = 0; 73616bf7bd1Sderaadt return -1; 73716bf7bd1Sderaadt } 73816bf7bd1Sderaadt if (*cp++) 73916bf7bd1Sderaadt return -1; 74016bf7bd1Sderaadt } 74116bf7bd1Sderaadt for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 74216bf7bd1Sderaadt switch (*np++ = *cp++) { 74316bf7bd1Sderaadt case 0: 74416bf7bd1Sderaadt dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1; 74516bf7bd1Sderaadt return chksum; 74616bf7bd1Sderaadt case '/': 74716bf7bd1Sderaadt np[-1] = 0; 74816bf7bd1Sderaadt return -1; 74916bf7bd1Sderaadt } 75016bf7bd1Sderaadt /* 75116bf7bd1Sderaadt * The size comparisons should be optimized away 75216bf7bd1Sderaadt */ 75316bf7bd1Sderaadt if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2 75416bf7bd1Sderaadt && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 75516bf7bd1Sderaadt && np > ep) { 75616bf7bd1Sderaadt np[-1] = 0; 75716bf7bd1Sderaadt return -1; 75816bf7bd1Sderaadt } 75916bf7bd1Sderaadt if (*cp++) 76016bf7bd1Sderaadt return -1; 76116bf7bd1Sderaadt } 76216bf7bd1Sderaadt for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 76316bf7bd1Sderaadt switch (*np++ = *cp++) { 76416bf7bd1Sderaadt case 0: 76516bf7bd1Sderaadt dp->d_namlen -= i + 1; 76616bf7bd1Sderaadt return chksum; 76716bf7bd1Sderaadt case '/': 76816bf7bd1Sderaadt np[-1] = 0; 76916bf7bd1Sderaadt return -1; 77016bf7bd1Sderaadt } 77116bf7bd1Sderaadt /* 77216bf7bd1Sderaadt * See above 77316bf7bd1Sderaadt */ 77416bf7bd1Sderaadt if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2 77516bf7bd1Sderaadt && np > ep) { 77616bf7bd1Sderaadt np[-1] = 0; 77716bf7bd1Sderaadt return -1; 77816bf7bd1Sderaadt } 77916bf7bd1Sderaadt if (*cp++) 78016bf7bd1Sderaadt return -1; 78116bf7bd1Sderaadt } 78216bf7bd1Sderaadt return chksum; 78316bf7bd1Sderaadt } 78416bf7bd1Sderaadt 78516bf7bd1Sderaadt /* 78616bf7bd1Sderaadt * Compute the checksum of a DOS filename for Win95 use 78716bf7bd1Sderaadt */ 78816bf7bd1Sderaadt u_int8_t 7897d80fe84Sjasper winChksum(u_int8_t *name) 79016bf7bd1Sderaadt { 79116bf7bd1Sderaadt int i; 79216bf7bd1Sderaadt u_int8_t s; 79316bf7bd1Sderaadt 79416bf7bd1Sderaadt for (s = 0, i = 11; --i >= 0; s += *name++) 79516bf7bd1Sderaadt s = (s << 7)|(s >> 1); 79616bf7bd1Sderaadt return s; 79716bf7bd1Sderaadt } 79816bf7bd1Sderaadt 79916bf7bd1Sderaadt /* 80016bf7bd1Sderaadt * Determine the number of slots necessary for Win95 names 80116bf7bd1Sderaadt */ 80216bf7bd1Sderaadt int 8037d80fe84Sjasper winSlotCnt(u_char *un, int unlen) 80416bf7bd1Sderaadt { 80516bf7bd1Sderaadt for (un += unlen; unlen > 0; unlen--) 80616bf7bd1Sderaadt if (*--un != ' ' && *un != '.') 80716bf7bd1Sderaadt break; 80816bf7bd1Sderaadt if (unlen > WIN_MAXLEN) 80916bf7bd1Sderaadt return 0; 81016bf7bd1Sderaadt return howmany(unlen, WIN_CHARS); 811df930be7Sderaadt } 812