Home | History | Annotate | Line # | Download | only in libsa
dosfs.c revision 1.16
      1 /*	$NetBSD: dosfs.c,v 1.16 2008/11/24 11:27:20 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998 Robert Nordier
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in
     14  *    the documentation and/or other materials provided with the
     15  *    distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
     32  * also supports VFAT.
     33  */
     34 
     35 /*
     36  * XXX DOES NOT SUPPORT:
     37  *
     38  *	LIBSA_FS_SINGLECOMPONENT
     39  */
     40 
     41 #include <sys/param.h>
     42 
     43 #include <fs/msdosfs/bpb.h>
     44 #include <fs/msdosfs/direntry.h>
     45 
     46 #ifdef _STANDALONE
     47 #include <lib/libkern/libkern.h>
     48 #else
     49 #include <string.h>
     50 #include <stddef.h>
     51 #endif
     52 
     53 #include "stand.h"
     54 #include "dosfs.h"
     55 
     56 #define SECSIZ  512		/* sector size */
     57 #define SSHIFT    9		/* SECSIZ shift */
     58 #define DEPSEC   16		/* directory entries per sector */
     59 #define DSHIFT    4		/* DEPSEC shift */
     60 #define LOCLUS    2		/* lowest cluster number */
     61 
     62 typedef union {
     63 	struct direntry de;	/* standard directory entry */
     64 	struct winentry xde;	/* extended directory entry */
     65 } DOS_DIR;
     66 
     67 typedef struct {
     68 	struct open_file *fd;	/* file descriptor */
     69 	u_char *buf;		/* buffer */
     70 	u_int   bufsec;		/* buffered sector */
     71 	u_int   links;		/* active links to structure */
     72 	u_int   spc;		/* sectors per cluster */
     73 	u_int   bsize;		/* cluster size in bytes */
     74 	u_int   bshift;		/* cluster conversion shift */
     75 	u_int   dirents;	/* root directory entries */
     76 	u_int   spf;		/* sectors per fat */
     77 	u_int   rdcl;		/* root directory start cluster */
     78 	u_int   lsnfat;		/* start of fat */
     79 	u_int   lsndir;		/* start of root dir */
     80 	u_int   lsndta;		/* start of data area */
     81 	u_int   fatsz;		/* FAT entry size */
     82 	u_int   xclus;		/* maximum cluster number */
     83 } DOS_FS;
     84 
     85 typedef struct {
     86 	DOS_FS *fs;		/* associated filesystem */
     87 	struct direntry de;	/* directory entry */
     88 	u_int   offset;		/* current offset */
     89 	u_int   c;		/* last cluster read */
     90 } DOS_FILE;
     91 
     92 /* Initial portion of DOS boot sector */
     93 typedef struct {
     94 	u_char  jmp[3];		/* usually 80x86 'jmp' opcode */
     95 	u_char  oem[8];		/* OEM name and version */
     96 	struct byte_bpb710 bpb;	/* BPB */
     97 } DOS_BS;
     98 
     99 /* Supply missing "." and ".." root directory entries */
    100 static const char *const dotstr[2] = {".", ".."};
    101 static const struct direntry dot[2] = {
    102 	{".       ", "   ", ATTR_DIRECTORY,
    103 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
    104 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
    105 
    106 	{"..      ", "   ", ATTR_DIRECTORY,
    107 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
    108 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
    109 };
    110 
    111 /* The usual conversion macros to avoid multiplication and division */
    112 #define bytsec(n)      ((n) >> SSHIFT)
    113 #define secbyt(s)      ((s) << SSHIFT)
    114 #define entsec(e)      ((e) >> DSHIFT)
    115 #define bytblk(fs, n)  ((n) >> (fs)->bshift)
    116 #define blkbyt(fs, b)  ((b) << (fs)->bshift)
    117 #define secblk(fs, s)  ((s) >> ((fs)->bshift - SSHIFT))
    118 #define blksec(fs, b)  ((b) << ((fs)->bshift - SSHIFT))
    119 
    120 /* Convert cluster number to offset within filesystem */
    121 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
    122 
    123 /* Convert cluster number to logical sector number */
    124 #define blklsn(fs, b)  ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
    125 
    126 /* Convert cluster number to offset within FAT */
    127 #define fatoff(sz, c)  ((sz) == 12 ? (c) + ((c) >> 1) :  \
    128                         (sz) == 16 ? (c) << 1 :          \
    129                         (c) << 2)
    130 
    131 /* Does cluster number reference a valid data cluster? */
    132 #define okclus(fs, c)  ((c) >= LOCLUS && (c) <= (fs)->xclus)
    133 
    134 /* Get start cluster from directory entry */
    135 #define stclus(sz, de)  ((sz) != 32 ? (u_int)getushort((de)->deStartCluster) : \
    136                          ((u_int)getushort((de)->deHighClust) << 16) |  \
    137                          (u_int)getushort((de)->deStartCluster))
    138 
    139 static int dosunmount(DOS_FS *);
    140 static int parsebs(DOS_FS *, DOS_BS *);
    141 static int namede(DOS_FS *, const char *, const struct direntry **);
    142 static int lookup(DOS_FS *, u_int, const char *, const struct direntry **);
    143 static void cp_xdnm(u_char *, struct winentry *);
    144 static void cp_sfn(u_char *, struct direntry *);
    145 static off_t fsize(DOS_FS *, struct direntry *);
    146 static int fatcnt(DOS_FS *, u_int);
    147 static int fatget(DOS_FS *, u_int *);
    148 static int fatend(u_int, u_int);
    149 static int ioread(DOS_FS *, u_int, void *, u_int);
    150 static int iobuf(DOS_FS *, u_int);
    151 static int ioget(struct open_file *, u_int, void *, u_int);
    152 
    153 #define strcasecmp(s1, s2) dos_strcasecmp(s1, s2)
    154 static int
    155 strcasecmp(const char *s1, const char *s2)
    156 {
    157 	char c1, c2;
    158 	#define TO_UPPER(c) ((c) >= 'a' && (c) <= 'z' ? (c) - ('a' - 'A') : (c))
    159 	for (;;) {
    160 		c1 = *s1++;
    161 		c2 = *s2++;
    162 		if (TO_UPPER(c1) != TO_UPPER(c2))
    163 			return 1;
    164 		if (c1 == 0)
    165 			return 0;
    166 	}
    167 	#undef TO_UPPER
    168 }
    169 
    170 /*
    171  * Mount DOS filesystem
    172  */
    173 static int
    174 dos_mount(DOS_FS *fs, struct open_file *fd)
    175 {
    176 	int     err;
    177 
    178 	(void)memset(fs, 0, sizeof(DOS_FS));
    179 	fs->fd = fd;
    180 	if ((err = !(fs->buf = alloc(SECSIZ)) ? errno : 0) ||
    181 	    (err = ioget(fs->fd, 0, fs->buf, 1)) ||
    182 	    (err = parsebs(fs, (DOS_BS *)fs->buf))) {
    183 		(void) dosunmount(fs);
    184 		return err;
    185 	}
    186 	return 0;
    187 }
    188 
    189 #ifndef LIBSA_NO_FS_CLOSE
    190 /*
    191  * Unmount mounted filesystem
    192  */
    193 static int
    194 dos_unmount(DOS_FS *fs)
    195 {
    196 	int     err;
    197 
    198 	if (fs->links)
    199 		return EBUSY;
    200 	if ((err = dosunmount(fs)))
    201 		return err;
    202 	return 0;
    203 }
    204 #endif
    205 
    206 /*
    207  * Common code shared by dos_mount() and dos_unmount()
    208  */
    209 static int
    210 dosunmount(DOS_FS *fs)
    211 {
    212 	if (fs->buf)
    213 		dealloc(fs->buf, SECSIZ);
    214 	dealloc(fs, sizeof(DOS_FS));
    215 	return 0;
    216 }
    217 
    218 /*
    219  * Open DOS file
    220  */
    221 int
    222 dosfs_open(const char *path, struct open_file *fd)
    223 {
    224 	const struct direntry *de;
    225 	DOS_FILE *f;
    226 	DOS_FS *fs;
    227 	u_int   size, clus;
    228 	int     err = 0;
    229 
    230 	/* Allocate mount structure, associate with open */
    231 	fs = alloc(sizeof(DOS_FS));
    232 
    233 	if ((err = dos_mount(fs, fd)))
    234 		goto out;
    235 
    236 	if ((err = namede(fs, path, &de)))
    237 		goto out;
    238 
    239 	clus = stclus(fs->fatsz, de);
    240 	size = getulong(de->deFileSize);
    241 
    242 	if ((!(de->deAttributes & ATTR_DIRECTORY) && (!clus != !size)) ||
    243 	    ((de->deAttributes & ATTR_DIRECTORY) && size) ||
    244 	    (clus && !okclus(fs, clus))) {
    245 		err = EINVAL;
    246 		goto out;
    247 	}
    248 
    249 	f = alloc(sizeof(DOS_FILE));
    250 #ifdef BOOTXX
    251 	/* due to __internal_memset_ causing all sorts of register spillage
    252 	   (and being completely unoptimized for zeroing small amounts of
    253 	   memory), if we hand-initialize the remaining members of f to zero,
    254 	   the code size drops 68 bytes. This makes no sense, admittedly. */
    255 	f->offset = 0;
    256 	f->c = 0;
    257 #else
    258 	(void)memset(f, 0, sizeof(DOS_FILE));
    259 #endif
    260 	f->fs = fs;
    261 	fs->links++;
    262 	f->de = *de;
    263 	fd->f_fsdata = (void *)f;
    264 	fsmod = "msdos";
    265 
    266 out:
    267 	return err;
    268 }
    269 
    270 /*
    271  * Read from file
    272  */
    273 int
    274 dosfs_read(struct open_file *fd, void *vbuf, size_t nbyte, size_t *resid)
    275 {
    276 	off_t   size;
    277 	u_int8_t *buf = vbuf;
    278 	u_int   nb, off, clus, c, cnt, n;
    279 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    280 	int     err = 0;
    281 
    282 	nb = (u_int) nbyte;
    283 	if ((size = fsize(f->fs, &f->de)) == -1)
    284 		return EINVAL;
    285 	if (nb > (n = size - f->offset))
    286 		nb = n;
    287 	off = f->offset;
    288 	if ((clus = stclus(f->fs->fatsz, &f->de)))
    289 		off &= f->fs->bsize - 1;
    290 	c = f->c;
    291 	cnt = nb;
    292 	while (cnt) {
    293 		n = 0;
    294 		if (!c) {
    295 			if ((c = clus))
    296 				n = bytblk(f->fs, f->offset);
    297 		} else if (!off) {
    298 			n++;
    299 		}
    300 		while (n--) {
    301 			if ((err = fatget(f->fs, &c)))
    302 				goto out;
    303 			if (!okclus(f->fs, c)) {
    304 				err = EINVAL;
    305 				goto out;
    306 			}
    307 		}
    308 		if (!clus || (n = f->fs->bsize - off) > cnt)
    309 			n = cnt;
    310 		if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
    311 				secbyt(f->fs->lsndir)) + off,
    312 			    buf, n)))
    313 			goto out;
    314 		f->offset += n;
    315 		f->c = c;
    316 		off = 0;
    317 		buf += n;
    318 		cnt -= n;
    319 	}
    320 out:
    321 	if (resid)
    322 		*resid = nbyte - nb + cnt;
    323 	return err;
    324 }
    325 
    326 #ifndef LIBSA_NO_FS_WRITE
    327 /*
    328  * Not implemented.
    329  */
    330 int
    331 dosfs_write(struct open_file *fd, void *start, size_t size, size_t *resid)
    332 {
    333 
    334 	return EROFS;
    335 }
    336 #endif /* !LIBSA_NO_FS_WRITE */
    337 
    338 #ifndef LIBSA_NO_FS_SEEK
    339 /*
    340  * Reposition within file
    341  */
    342 off_t
    343 dosfs_seek(struct open_file *fd, off_t offset, int whence)
    344 {
    345 	off_t   off;
    346 	u_int   size;
    347 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    348 
    349 	size = getulong(f->de.deFileSize);
    350 	switch (whence) {
    351 	case SEEK_SET:
    352 		off = 0;
    353 		break;
    354 	case SEEK_CUR:
    355 		off = f->offset;
    356 		break;
    357 	case SEEK_END:
    358 		off = size;
    359 		break;
    360 	default:
    361 		return -1;
    362 	}
    363 	off += offset;
    364 	if (off < 0 || off > size)
    365 		return -1;
    366 	f->offset = (u_int) off;
    367 	f->c = 0;
    368 	return off;
    369 }
    370 #endif /* !LIBSA_NO_FS_SEEK */
    371 
    372 #ifndef LIBSA_NO_FS_CLOSE
    373 /*
    374  * Close open file
    375  */
    376 int
    377 dosfs_close(struct open_file *fd)
    378 {
    379 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    380 	DOS_FS *fs = f->fs;
    381 
    382 	f->fs->links--;
    383 	dealloc(f, sizeof(DOS_FILE));
    384 	dos_unmount(fs);
    385 	return 0;
    386 }
    387 #endif /* !LIBSA_NO_FS_CLOSE */
    388 
    389 /*
    390  * Return some stat information on a file.
    391  */
    392 int
    393 dosfs_stat(struct open_file *fd, struct stat *sb)
    394 {
    395 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    396 
    397 	/* only important stuff */
    398 	sb->st_mode = (f->de.deAttributes & ATTR_DIRECTORY) ?
    399 	    (S_IFDIR | 0555) : (S_IFREG | 0444);
    400 	sb->st_nlink = 1;
    401 	sb->st_uid = 0;
    402 	sb->st_gid = 0;
    403 	if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
    404 		return EINVAL;
    405 	return 0;
    406 }
    407 
    408 /*
    409  * Parse DOS boot sector
    410  */
    411 static int
    412 parsebs(DOS_FS *fs, DOS_BS *bs)
    413 {
    414 	u_int   sc;
    415 
    416 	if ((bs->jmp[0] != 0x69 &&
    417 		bs->jmp[0] != 0xe9 &&
    418 		(bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
    419 	    bs->bpb.bpbMedia < 0xf0)
    420 		return EINVAL;
    421 	if (getushort(bs->bpb.bpbBytesPerSec) != SECSIZ)
    422 		return EINVAL;
    423 	if (!(fs->spc = bs->bpb.bpbSecPerClust) || fs->spc & (fs->spc - 1))
    424 		return EINVAL;
    425 	fs->bsize = secbyt(fs->spc);
    426 	fs->bshift = ffs(fs->bsize) - 1;
    427 	if ((fs->spf = getushort(bs->bpb.bpbFATsecs))) {
    428 		if (bs->bpb.bpbFATs != 2)
    429 			return EINVAL;
    430 		if (!(fs->dirents = getushort(bs->bpb.bpbRootDirEnts)))
    431 			return EINVAL;
    432 	} else {
    433 		if (!(fs->spf = getulong(bs->bpb.bpbBigFATsecs)))
    434 			return EINVAL;
    435 		if (!bs->bpb.bpbFATs || bs->bpb.bpbFATs > 16)
    436 			return EINVAL;
    437 		if ((fs->rdcl = getulong(bs->bpb.bpbRootClust)) < LOCLUS)
    438 			return EINVAL;
    439 	}
    440 	if (!(fs->lsnfat = getushort(bs->bpb.bpbResSectors)))
    441 		return EINVAL;
    442 	fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.bpbFATs;
    443 	fs->lsndta = fs->lsndir + entsec(fs->dirents);
    444 	if (!(sc = getushort(bs->bpb.bpbSectors)) &&
    445 	    !(sc = getulong(bs->bpb.bpbHugeSectors)))
    446 		return EINVAL;
    447 	if (fs->lsndta > sc)
    448 		return EINVAL;
    449 	if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
    450 		return EINVAL;
    451 	fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
    452 	sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
    453 	if (fs->xclus > sc)
    454 		fs->xclus = sc;
    455 	return 0;
    456 }
    457 
    458 /*
    459  * Return directory entry from path
    460  */
    461 static int
    462 namede(DOS_FS *fs, const char *path, const struct direntry **dep)
    463 {
    464 	char    name[256];
    465 	const struct direntry *de;
    466 	char   *s;
    467 	size_t  n;
    468 	int     err;
    469 
    470 	err = 0;
    471 	de = dot;
    472 	if (*path == '/')
    473 		path++;
    474 	while (*path) {
    475 		if (!(s = strchr(path, '/')))
    476 			s = strchr(path, 0);
    477 		if ((n = s - path) > 255)
    478 			return ENAMETOOLONG;
    479 		memcpy(name, path, n);
    480 		name[n] = 0;
    481 		path = s;
    482 		if (!(de->deAttributes & ATTR_DIRECTORY))
    483 			return ENOTDIR;
    484 		if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
    485 			return err;
    486 		if (*path == '/')
    487 			path++;
    488 	}
    489 	*dep = de;
    490 	return 0;
    491 }
    492 
    493 /*
    494  * Lookup path segment
    495  */
    496 static int
    497 lookup(DOS_FS *fs, u_int clus, const char *name, const struct direntry **dep)
    498 {
    499 	static DOS_DIR *dir = NULL;
    500 	u_char  lfn[261];
    501 	u_char  sfn[13];
    502 	u_int   nsec, lsec, xdn, chk, sec, ent, x;
    503 	int     err = 0, ok, i;
    504 
    505 	if (!clus)
    506 		for (ent = 0; ent < 2; ent++)
    507 			if (!strcasecmp(name, dotstr[ent])) {
    508 				*dep = dot + ent;
    509 				return 0;
    510 			}
    511 
    512 	if (dir == NULL) {
    513 		dir = alloc(sizeof(DOS_DIR) * DEPSEC);
    514 		if (dir == NULL)
    515 			return ENOMEM;
    516 	}
    517 
    518 	if (!clus && fs->fatsz == 32)
    519 		clus = fs->rdcl;
    520 	nsec = !clus ? entsec(fs->dirents) : fs->spc;
    521 	lsec = 0;
    522 	xdn = chk = 0;
    523 	for (;;) {
    524 		if (!clus && !lsec)
    525 			lsec = fs->lsndir;
    526 		else if (okclus(fs, clus))
    527 			lsec = blklsn(fs, clus);
    528 		else {
    529 			err = EINVAL;
    530 			goto out;
    531 		}
    532 		for (sec = 0; sec < nsec; sec++) {
    533 			if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
    534 				goto out;
    535 			for (ent = 0; ent < DEPSEC; ent++) {
    536 				if (!*dir[ent].de.deName) {
    537 					err = ENOENT;
    538 					goto out;
    539 				}
    540 				if (*dir[ent].de.deName != 0xe5) {
    541 					if (dir[ent].de.deAttributes ==
    542 					    ATTR_WIN95) {
    543 						x = dir[ent].xde.weCnt;
    544 						if (x & WIN_LAST ||
    545 						    (x + 1 == xdn &&
    546 						     dir[ent].xde.weChksum ==
    547 						     chk)) {
    548 							if (x & WIN_LAST) {
    549 								chk = dir[ent].xde.weChksum;
    550 								x &= WIN_CNT;
    551 							}
    552 							if (x >= 1 && x <= 20) {
    553 								cp_xdnm(lfn, &dir[ent].xde);
    554 								xdn = x;
    555 								continue;
    556 							}
    557 						}
    558 					} else if (!(dir[ent].de.deAttributes &
    559 						     ATTR_VOLUME)) {
    560 						if ((ok = xdn == 1)) {
    561 							for (x = 0, i = 0;
    562 							     i < 11; i++)
    563 								x = ((((x & 1) << 7) | (x >> 1)) +
    564 								    dir[ent].de.deName[i]) & 0xff;
    565 							ok = chk == x &&
    566 							    !strcasecmp(name, (const char *)lfn);
    567 						}
    568 						if (!ok) {
    569 							cp_sfn(sfn, &dir[ent].de);
    570 							ok = !strcasecmp(name, (const char *)sfn);
    571 						}
    572 						if (ok) {
    573 							*dep = &dir[ent].de;
    574 							goto out2;
    575 						}
    576 					}
    577 				}
    578 				xdn = 0;
    579 			}
    580 		}
    581 		if (!clus)
    582 			break;
    583 		if ((err = fatget(fs, &clus)))
    584 			goto out;
    585 		if (fatend(fs->fatsz, clus))
    586 			break;
    587 	}
    588 	err = ENOENT;
    589  out:
    590 	dealloc(dir, sizeof(DOS_DIR) * DEPSEC);
    591 	dir = NULL;
    592  out2:
    593 	return err;
    594 }
    595 
    596 /*
    597  * Copy name from extended directory entry
    598  */
    599 static void
    600 cp_xdnm(u_char *lfn, struct winentry *xde)
    601 {
    602 	static const struct {
    603 		u_int   off;
    604 		u_int   dim;
    605 	} ix[3] = {
    606 		{ offsetof(struct winentry, wePart1),
    607 		    sizeof(xde->wePart1) / 2 },
    608 		{ offsetof(struct winentry, wePart2),
    609 		    sizeof(xde->wePart2) / 2 },
    610 		{ offsetof(struct winentry, wePart3),
    611 		    sizeof(xde->wePart3) / 2 }
    612 	};
    613 	u_char *p;
    614 	u_int   n, x, c;
    615 
    616 	lfn += 13 * ((xde->weCnt & WIN_CNT) - 1);
    617 	for (n = 0; n < 3; n++)
    618 		for (p = (u_char *)xde + ix[n].off, x = ix[n].dim; x;
    619 		    p += 2, x--) {
    620 			if ((c = getushort(p)) && (c < 32 || c > 127))
    621 				c = '?';
    622 			if (!(*lfn++ = c))
    623 				return;
    624 		}
    625 	if (xde->weCnt & WIN_LAST)
    626 		*lfn = 0;
    627 }
    628 
    629 /*
    630  * Copy short filename
    631  */
    632 static void
    633 cp_sfn(u_char *sfn, struct direntry *de)
    634 {
    635 	u_char *p;
    636 	int     j, i;
    637 
    638 	p = sfn;
    639 	if (*de->deName != ' ') {
    640 		for (j = 7; de->deName[j] == ' '; j--);
    641 		for (i = 0; i <= j; i++)
    642 			*p++ = de->deName[i];
    643 		if (*de->deExtension != ' ') {
    644 			*p++ = '.';
    645 			for (j = 2; de->deExtension[j] == ' '; j--);
    646 			for (i = 0; i <= j; i++)
    647 				*p++ = de->deExtension[i];
    648 		}
    649 	}
    650 	*p = 0;
    651 	if (*sfn == 5)
    652 		*sfn = 0xe5;
    653 }
    654 
    655 /*
    656  * Return size of file in bytes
    657  */
    658 static  off_t
    659 fsize(DOS_FS *fs, struct direntry *de)
    660 {
    661 	u_long  size;
    662 	u_int   c;
    663 	int     n;
    664 
    665 	if (!(size = getulong(de->deFileSize)) &&
    666 	    de->deAttributes & ATTR_DIRECTORY) {
    667 		if (!(c = getushort(de->deStartCluster))) {
    668 			size = fs->dirents * sizeof(struct direntry);
    669 		} else {
    670 			if ((n = fatcnt(fs, c)) == -1)
    671 				return n;
    672 			size = blkbyt(fs, n);
    673 		}
    674 	}
    675 	return size;
    676 }
    677 
    678 /*
    679  * Count number of clusters in chain
    680  */
    681 static int
    682 fatcnt(DOS_FS *fs, u_int c)
    683 {
    684 	int     n;
    685 
    686 	for (n = 0; okclus(fs, c); n++)
    687 		if (fatget(fs, &c))
    688 			return -1;
    689 	return fatend(fs->fatsz, c) ? n : -1;
    690 }
    691 
    692 /*
    693  * Get next cluster in cluster chain
    694  */
    695 static int
    696 fatget(DOS_FS *fs, u_int *c)
    697 {
    698 	u_char  buf[4];
    699 	u_int   x;
    700 	int     err;
    701 
    702 	err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
    703 	    fs->fatsz != 32 ? 2 : 4);
    704 	if (err)
    705 		return err;
    706 	x = fs->fatsz != 32 ? getushort(buf) : getulong(buf);
    707 	*c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
    708 	return 0;
    709 }
    710 
    711 /*
    712  * Is cluster an end-of-chain marker?
    713  */
    714 static int
    715 fatend(u_int sz, u_int c)
    716 {
    717 	return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
    718 }
    719 
    720 /*
    721  * Offset-based I/O primitive
    722  */
    723 static int
    724 ioread(DOS_FS *fs, u_int offset, void *buf, u_int nbyte)
    725 {
    726 	char   *s;
    727 	u_int   off, n;
    728 	int     err;
    729 
    730 	s = buf;
    731 	if ((off = offset & (SECSIZ - 1))) {
    732 		offset -= off;
    733 		if ((err = iobuf(fs, bytsec(offset))))
    734 			return err;
    735 		offset += SECSIZ;
    736 		if ((n = SECSIZ - off) > nbyte)
    737 			n = nbyte;
    738 		memcpy(s, fs->buf + off, n);
    739 		s += n;
    740 		nbyte -= n;
    741 	}
    742 	n = nbyte & (SECSIZ - 1);
    743 	if (nbyte -= n) {
    744 		if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
    745 			return err;
    746 		offset += nbyte;
    747 		s += nbyte;
    748 	}
    749 	if (n) {
    750 		if ((err = iobuf(fs, bytsec(offset))))
    751 			return err;
    752 		memcpy(s, fs->buf, n);
    753 	}
    754 	return 0;
    755 }
    756 
    757 /*
    758  * Buffered sector-based I/O primitive
    759  */
    760 static int
    761 iobuf(DOS_FS *fs, u_int lsec)
    762 {
    763 	int     err;
    764 
    765 	if (fs->bufsec != lsec) {
    766 		if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
    767 			return err;
    768 		fs->bufsec = lsec;
    769 	}
    770 	return 0;
    771 }
    772 
    773 /*
    774  * Sector-based I/O primitive
    775  */
    776 static int
    777 ioget(struct open_file *fd, u_int lsec, void *buf, u_int nsec)
    778 {
    779 	size_t rsize;
    780 	int err;
    781 
    782 #ifndef LIBSA_NO_TWIDDLE
    783 	twiddle();
    784 #endif
    785 	err = DEV_STRATEGY(fd->f_dev)(fd->f_devdata, F_READ, lsec,
    786 	    secbyt(nsec), buf, &rsize);
    787 	return err;
    788 }
    789