summaryrefslogtreecommitdiffstats
path: root/sys/fs/msdosfs/msdosfs_conv.c
diff options
context:
space:
mode:
authorjkh <jkh@FreeBSD.org>1998-02-18 09:28:47 +0000
committerjkh <jkh@FreeBSD.org>1998-02-18 09:28:47 +0000
commit90f90fc3470f6725b317b359c1b3149995b822b7 (patch)
treea41d30021bc38cb8b60af8e616a1a634111adecc /sys/fs/msdosfs/msdosfs_conv.c
parent4430df5b848751ab6f0d04b584f8c0921f47d494 (diff)
downloadFreeBSD-src-90f90fc3470f6725b317b359c1b3149995b822b7.zip
FreeBSD-src-90f90fc3470f6725b317b359c1b3149995b822b7.tar.gz
Update MSDOSFS code using NetBSD's msdosfs as a guide to support
FAT32 partitions. Unfortunately, we looked around here at Walnut Creek CDROM for any newer FAT32-supporting versions of Win95 and we were unsuccessful; only the older stuff here. So this is untested beyond simply making sure it compiles and someone with access to an actual FAT32 fs will have to let us know how well it actually works. Submitted by: Dmitrij Tejblum <dima@tejblum.dnttm.rssi.ru> Obtained from: NetBSD
Diffstat (limited to 'sys/fs/msdosfs/msdosfs_conv.c')
-rw-r--r--sys/fs/msdosfs/msdosfs_conv.c684
1 files changed, 577 insertions, 107 deletions
diff --git a/sys/fs/msdosfs/msdosfs_conv.c b/sys/fs/msdosfs/msdosfs_conv.c
index 59f4d2c..727cacd 100644
--- a/sys/fs/msdosfs/msdosfs_conv.c
+++ b/sys/fs/msdosfs/msdosfs_conv.c
@@ -1,6 +1,37 @@
-/* $Id: msdosfs_conv.c,v 1.13 1997/02/22 09:40:46 peter Exp $ */
-/* $NetBSD: msdosfs_conv.c,v 1.6.2.1 1994/08/30 02:27:57 cgd Exp $ */
+/* $Id: msdosfs_conv.c,v 1.14 1998/02/09 06:09:50 eivind Exp $ */
+/* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */
+/*-
+ * Copyright (C) 1995, 1997 Wolfgang Solfrank.
+ * Copyright (C) 1995, 1997 TooLs GmbH.
+ * All rights reserved.
+ * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by TooLs GmbH.
+ * 4. The name of TooLs GmbH may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
/*
* Written by Paul Popelka (paulp@uts.amdahl.com)
*
@@ -23,8 +54,9 @@
#include <sys/param.h>
#include <sys/time.h>
#include <sys/kernel.h> /* defines tz */
-#include <sys/systm.h> /* defines tz */
+#include <sys/systm.h>
#include <machine/clock.h>
+#include <sys/dirent.h>
/*
* MSDOSFS include files.
@@ -61,10 +93,11 @@ static u_short lastdtime;
* file timestamps. The passed in unix time is assumed to be in GMT.
*/
void
-unix2dostime(tsp, ddp, dtp)
+unix2dostime(tsp, ddp, dtp, dhp)
struct timespec *tsp;
- u_short *ddp;
- u_short *dtp;
+ u_int16_t *ddp;
+ u_int16_t *dtp;
+ u_int8_t *dhp;
{
u_long t;
u_long days;
@@ -80,9 +113,10 @@ unix2dostime(tsp, ddp, dtp)
t = tsp->tv_sec - (tz.tz_minuteswest * 60)
- (wall_cmos_clock ? adjkerntz : 0);
/* - daylight savings time correction */
+ t &= ~1;
if (lasttime != t) {
lasttime = t;
- lastdtime = (((t % 60) >> 1) << DT_2SECONDS_SHIFT)
+ lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
+ (((t / 60) % 60) << DT_MINUTES_SHIFT)
+ (((t / 3600) % 24) << DT_HOURS_SHIFT);
@@ -117,7 +151,11 @@ unix2dostime(tsp, ddp, dtp)
lastddate += (year - 1980) << DD_YEAR_SHIFT;
}
}
- *dtp = lastdtime;
+ if (dtp)
+ *dtp = lastdtime;
+ if (dhp)
+ *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
+
*ddp = lastddate;
}
@@ -136,9 +174,10 @@ static u_long lastseconds;
* not be too efficient.
*/
void
-dos2unixtime(dd, dt, tsp)
- u_short dd;
- u_short dt;
+dos2unixtime(dd, dt, dh, tsp)
+ u_int dd;
+ u_int dt;
+ u_int dh;
struct timespec *tsp;
{
u_long seconds;
@@ -147,9 +186,18 @@ dos2unixtime(dd, dt, tsp)
u_long days;
u_short *months;
+ if (dd == 0) {
+ /*
+ * Uninitialized field, return the epoch.
+ */
+ tsp->tv_sec = 0;
+ tsp->tv_nsec = 0;
+ return;
+ }
seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
+ ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
- + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600;
+ + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
+ + dh / 100;
/*
* If the year, month, and day from the last conversion are the
* same then use the saved value.
@@ -165,8 +213,7 @@ dos2unixtime(dd, dt, tsp)
months = year & 0x03 ? regyear : leapyear;
month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
if (month < 1 || month > 12) {
- printf(
- "dos2unixtime(): month value out of range (%ld)\n",
+ printf("dos2unixtime(): month value out of range (%ld)\n",
month);
month = 1;
}
@@ -178,17 +225,116 @@ dos2unixtime(dd, dt, tsp)
tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
+ adjkerntz;
/* + daylight savings time correction */
- tsp->tv_nsec = 0;
+ tsp->tv_nsec = (dh % 100) * 10000000;
}
-/*
- * Cheezy macros to do case detection and conversion for the ascii
- * character set. DOESN'T work for ebcdic.
- */
-#define isupper(c) (c >= 'A' && c <= 'Z')
-#define islower(c) (c >= 'a' && c <= 'z')
-#define toupper(c) (c & ~' ')
-#define tolower(c) (c | ' ')
+static u_char
+unix2dos[256] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
+ 0, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
+ 0x28, 0x29, 0, 0, 0, 0x2d, 0, 0, /* 28-2f */
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
+ 0x38, 0x39, 0, 0, 0, 0, 0, 0, /* 38-3f */
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
+ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
+ 0x58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+ 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
+};
+
+static u_char
+dos2unix[256] = {
+ 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
+ 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
+ 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
+ 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
+ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
+ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
+ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
+ 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
+ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
+ 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
+ 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
+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0x14, 0x15, 0x16, 0x17, /* 10-17 */
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
+ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
+ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
+ 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
+ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
+ 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
+ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
+ 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
+ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
+ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
+ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
+ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
+};
/*
* DOS filenames are made of 2 parts, the name part and the extension part.
@@ -203,90 +349,86 @@ dos2unixtime(dd, dt, tsp)
* null.
*/
int
-dos2unixfn(dn, un)
+dos2unixfn(dn, un, lower)
u_char dn[11];
u_char *un;
+ int lower;
{
int i;
- int ni;
- int ei;
- int thislong = 0;
+ int thislong = 1;
u_char c;
- u_char *origun = un;
-
- /*
- * Find the last character in the name portion of the dos filename.
- */
- for (ni = 7; ni >= 0; ni--)
- if (dn[ni] != ' ')
- break;
/*
- * Find the last character in the extension portion of the
- * filename.
+ * If first char of the filename is SLOT_E5 (0x05), then the real
+ * first char of the filename should be 0xe5. But, they couldn't
+ * just have a 0xe5 mean 0xe5 because that is used to mean a freed
+ * directory slot. Another dos quirk.
*/
- for (ei = 10; ei >= 8; ei--)
- if (dn[ei] != ' ')
- break;
+ if (*dn == SLOT_E5)
+ c = dos2unix[0xe5];
+ else
+ c = dos2unix[*dn];
+ *un++ = lower ? u2l[c] : c;
+ dn++;
/*
- * Copy the name portion into the unix filename string. NOTE: DOS
- * filenames are usually kept in upper case. To make it more unixy
- * we convert all DOS filenames to lower case. Some may like this,
- * some may not.
+ * Copy the name portion into the unix filename string.
*/
- for (i = 0; i <= ni; i++) {
- c = dn[i];
- *un++ = isupper(c) ? tolower(c) : c;
+ for (i = 1; i < 8 && *dn != ' '; i++) {
+ c = dos2unix[*dn++];
+ *un++ = lower ? u2l[c] : c;
thislong++;
}
+ dn += 8 - i;
/*
* Now, if there is an extension then put in a period and copy in
* the extension.
*/
- if (ei >= 8) {
+ if (*dn != ' ') {
*un++ = '.';
thislong++;
- for (i = 8; i <= ei; i++) {
- c = dn[i];
- *un++ = isupper(c) ? tolower(c) : c;
+ for (i = 0; i < 3 && *dn != ' '; i++) {
+ c = dos2unix[*dn++];
+ *un++ = lower ? u2l[c] : c;
thislong++;
}
}
*un++ = 0;
- /*
- * If first char of the filename is SLOT_E5 (0x05), then the real
- * first char of the filename should be 0xe5. But, they couldn't
- * just have a 0xe5 mean 0xe5 because that is used to mean a freed
- * directory slot. Another dos quirk.
- */
- if (*origun == SLOT_E5)
- *origun = 0xe5;
-
- return thislong;
+ return (thislong);
}
/*
- * Convert a unix filename to a DOS filename. This function does not ensure
- * that valid characters for a dos filename are supplied.
+ * Convert a unix filename to a DOS filename according to Win95 rules.
+ * If applicable and gen is not 0, it is inserted into the converted
+ * filename as a generation number.
+ * Returns
+ * 0 if name couldn't be converted
+ * 1 if the converted name is the same as the original
+ * (no long filename entry necessary for Win95)
+ * 2 if conversion was successful
+ * 3 if conversion was successful and generation number was inserted
*/
-void
-unix2dosfn(un, dn, unlen)
- u_char *un;
- u_char dn[11];
+int
+unix2dosfn(un, dn, unlen, gen)
+ const u_char *un;
+ u_char dn[12];
int unlen;
+ u_int gen;
{
- int i;
- u_char c;
+ int i, j, l;
+ int conv = 1;
+ const u_char *cp, *dp, *dp1;
+ u_char gentext[6], *wcp;
/*
* Fill the dos filename string with blanks. These are DOS's pad
* characters.
*/
- for (i = 0; i <= 10; i++)
+ for (i = 0; i < 11; i++)
dn[i] = ' ';
+ dn[11] = 0;
/*
* The filenames "." and ".." are handled specially, since they
@@ -294,65 +436,393 @@ unix2dosfn(un, dn, unlen)
*/
if (un[0] == '.' && unlen == 1) {
dn[0] = '.';
- return;
+ return gen <= 1;
}
if (un[0] == '.' && un[1] == '.' && unlen == 2) {
dn[0] = '.';
dn[1] = '.';
- return;
+ return gen <= 1;
+ }
+
+ /*
+ * Filenames with only blanks and dots are not allowed!
+ */
+ for (cp = un, i = unlen; --i >= 0; cp++)
+ if (*cp != ' ' && *cp != '.')
+ break;
+ if (i < 0)
+ return 0;
+
+ /*
+ * Now find the extension
+ * Note: dot as first char doesn't start extension
+ * and trailing dots and blanks are ignored
+ */
+ dp = dp1 = 0;
+ for (cp = un + 1, i = unlen - 1; --i >= 0;) {
+ switch (*cp++) {
+ case '.':
+ if (!dp1)
+ dp1 = cp;
+ break;
+ case ' ':
+ break;
+ default:
+ if (dp1)
+ dp = dp1;
+ dp1 = 0;
+ break;
+ }
+ }
+
+ /*
+ * Now convert it
+ */
+ if (dp) {
+ if (dp1)
+ l = dp1 - dp;
+ else
+ l = unlen - (dp - un);
+ for (i = 0, j = 8; i < l && j < 11; i++, j++) {
+ if (dp[i] != (dn[j] = unix2dos[dp[i]])
+ && conv != 3)
+ conv = 2;
+ if (!dn[j]) {
+ conv = 3;
+ dn[j--] = ' ';
+ }
+ }
+ if (i < l)
+ conv = 3;
+ dp--;
+ } else {
+ for (dp = cp; *--dp == ' ' || *dp == '.';);
+ dp++;
}
/*
- * Copy the unix filename into the dos filename string upto the end
- * of string, a '.', or 8 characters. Whichever happens first stops
- * us. This forms the name portion of the dos filename. Fold to
- * upper case.
+ * Now convert the rest of the name
*/
- for (i = 0; i <= 7 && unlen && (c = *un) && c != '.'; i++) {
- dn[i] = islower(c) ? toupper(c) : c;
- un++;
- unlen--;
+ for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
+ if (*un != (dn[j] = unix2dos[*un])
+ && conv != 3)
+ conv = 2;
+ if (!dn[j]) {
+ conv = 3;
+ dn[j--] = ' ';
+ }
}
+ if (un < dp)
+ conv = 3;
+ /*
+ * If we didn't have any chars in filename,
+ * generate a default
+ */
+ if (!j)
+ dn[0] = '_';
/*
- * If the first char of the filename is 0xe5, then translate it to
- * 0x05. This is because 0xe5 is the marker for a deleted
- * directory slot. I guess this means you can't have filenames
- * that start with 0x05. I suppose we should check for this and
- * doing something about it.
+ * The first character cannot be E5,
+ * because that means a deleted entry
*/
- if (dn[0] == SLOT_DELETED)
+ if (dn[0] == 0xe5)
dn[0] = SLOT_E5;
/*
- * Strip any further characters up to a '.' or the end of the
- * string.
+ * If there wasn't any char dropped,
+ * there is no place for generation numbers
*/
- while (unlen && (c = *un)) {
- un++;
- unlen--;
- /* Make sure we've skipped over the dot before stopping. */
- if (c == '.')
- break;
+ if (conv != 3) {
+ if (gen > 1)
+ return 0;
+ return conv;
}
/*
- * Copy in the extension part of the name, if any. Force to upper
- * case. Note that the extension is allowed to contain '.'s.
- * Filenames in this form are probably inaccessable under dos.
+ * Now insert the generation number into the filename part
+ */
+ for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
+ *--wcp = gen % 10 + '0';
+ if (gen)
+ return 0;
+ for (i = 8; dn[--i] == ' ';);
+ i++;
+ if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
+ i = 8 - (gentext + sizeof(gentext) - wcp + 1);
+ dn[i++] = '~';
+ while (wcp < gentext + sizeof(gentext))
+ dn[i++] = *wcp++;
+ return 3;
+}
+
+/*
+ * Create a Win95 long name directory entry
+ * Note: assumes that the filename is valid,
+ * i.e. doesn't consist solely of blanks and dots
+ */
+int
+unix2winfn(un, unlen, wep, cnt, chksum)
+ const u_char *un;
+ int unlen;
+ struct winentry *wep;
+ int cnt;
+ int chksum;
+{
+ const u_int8_t *cp;
+ u_int8_t *wcp;
+ int i;
+
+ /*
+ * Drop trailing blanks and dots
+ */
+ for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
+
+ un += (cnt - 1) * WIN_CHARS;
+ unlen -= (cnt - 1) * WIN_CHARS;
+
+ /*
+ * Initialize winentry to some useful default
*/
- for (i = 8; i <= 10 && unlen && (c = *un); i++) {
- dn[i] = islower(c) ? toupper(c) : c;
- un++;
- unlen--;
+ for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
+ wep->weCnt = cnt;
+ wep->weAttributes = ATTR_WIN95;
+ wep->weReserved1 = 0;
+ wep->weChksum = chksum;
+ wep->weReserved2 = 0;
+
+ /*
+ * Now convert the filename parts
+ */
+ for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
+ if (--unlen < 0)
+ goto done;
+ *wcp++ = *un++;
+ *wcp++ = 0;
+ }
+ for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
+ if (--unlen < 0)
+ goto done;
+ *wcp++ = *un++;
+ *wcp++ = 0;
+ }
+ for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
+ if (--unlen < 0)
+ goto done;
+ *wcp++ = *un++;
+ *wcp++ = 0;
+ }
+ if (!unlen)
+ wep->weCnt |= WIN_LAST;
+ return unlen;
+
+done:
+ *wcp++ = 0;
+ *wcp++ = 0;
+ wep->weCnt |= WIN_LAST;
+ return 0;
+}
+
+/*
+ * Compare our filename to the one in the Win95 entry
+ * Returns the checksum or -1 if no match
+ */
+int
+winChkName(un, unlen, wep, chksum)
+ const u_char *un;
+ int unlen;
+ struct winentry *wep;
+ int chksum;
+{
+ u_int8_t *cp;
+ int i;
+
+ /*
+ * First compare checksums
+ */
+ if (wep->weCnt&WIN_LAST)
+ chksum = wep->weChksum;
+ else if (chksum != wep->weChksum)
+ chksum = -1;
+ if (chksum == -1)
+ return -1;
+
+ /*
+ * Offset of this entry
+ */
+ i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
+ un += i;
+ if ((unlen -= i) <= 0)
+ return -1;
+ if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
+ return -1;
+
+ /*
+ * Compare the name parts
+ */
+ for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
+ if (--unlen < 0) {
+ if (!*cp++ && !*cp)
+ return chksum;
+ return -1;
+ }
+ if (u2l[*cp++] != u2l[*un++] || *cp++)
+ return -1;
+ }
+ for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
+ if (--unlen < 0) {
+ if (!*cp++ && !*cp)
+ return chksum;
+ return -1;
+ }
+ if (u2l[*cp++] != u2l[*un++] || *cp++)
+ return -1;
+ }
+ for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
+ if (--unlen < 0) {
+ if (!*cp++ && !*cp)
+ return chksum;
+ return -1;
+ }
+ if (u2l[*cp++] != u2l[*un++] || *cp++)
+ return -1;
}
+ return chksum;
}
/*
- * Get rid of these macros before someone discovers we are using such
- * hideous things.
+ * Convert Win95 filename to dirbuf.
+ * Returns the checksum or -1 if impossible
*/
-#undef isupper
-#undef islower
-#undef toupper
-#undef tolower
+int
+win2unixfn(wep, dp, chksum)
+ struct winentry *wep;
+ struct dirent *dp;
+ int chksum;
+{
+ u_int8_t *cp;
+ u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
+ int i;
+
+ if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
+ || !(wep->weCnt&WIN_CNT))
+ return -1;
+
+ /*
+ * First compare checksums
+ */
+ if (wep->weCnt&WIN_LAST) {
+ chksum = wep->weChksum;
+ /*
+ * This works even though d_namlen is one byte!
+ */
+ dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
+ } else if (chksum != wep->weChksum)
+ chksum = -1;
+ if (chksum == -1)
+ return -1;
+
+ /*
+ * Offset of this entry
+ */
+ i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
+ np = (u_int8_t *)dp->d_name + i;
+
+ /*
+ * Convert the name parts
+ */
+ for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
+ switch (*np++ = *cp++) {
+ case 0:
+ dp->d_namlen -= sizeof(wep->wePart2)/2
+ + sizeof(wep->wePart3)/2 + i + 1;
+ return chksum;
+ case '/':
+ np[-1] = 0;
+ return -1;
+ }
+ /*
+ * The size comparison should result in the compiler
+ * optimizing the whole if away
+ */
+ if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
+ && np > ep) {
+ np[-1] = 0;
+ return -1;
+ }
+ if (*cp++)
+ return -1;
+ }
+ for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
+ switch (*np++ = *cp++) {
+ case 0:
+ dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
+ return chksum;
+ case '/':
+ np[-1] = 0;
+ return -1;
+ }
+ /*
+ * The size comparisons should be optimized away
+ */
+ if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
+ && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
+ && np > ep) {
+ np[-1] = 0;
+ return -1;
+ }
+ if (*cp++)
+ return -1;
+ }
+ for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
+ switch (*np++ = *cp++) {
+ case 0:
+ dp->d_namlen -= i + 1;
+ return chksum;
+ case '/':
+ np[-1] = 0;
+ return -1;
+ }
+ /*
+ * See above
+ */
+ if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
+ && np > ep) {
+ np[-1] = 0;
+ return -1;
+ }
+ if (*cp++)
+ return -1;
+ }
+ return chksum;
+}
+
+/*
+ * Compute the checksum of a DOS filename for Win95 use
+ */
+u_int8_t
+winChksum(name)
+ u_int8_t *name;
+{
+ int i;
+ u_int8_t s;
+
+ for (s = 0, i = 11; --i >= 0; s += *name++)
+ s = (s << 7)|(s >> 1);
+ return s;
+}
+
+/*
+ * Determine the number of slots necessary for Win95 names
+ */
+int
+winSlotCnt(un, unlen)
+ const u_char *un;
+ int unlen;
+{
+ for (un += unlen; unlen > 0; unlen--)
+ if (*--un != ' ' && *un != '.')
+ break;
+ if (unlen > WIN_MAXLEN)
+ return 0;
+ return howmany(unlen, WIN_CHARS);
+}
OpenPOWER on IntegriCloud