Home | History | Annotate | Line # | Download | only in libsa
dosfs.c revision 1.11.20.1
      1 /*	$NetBSD: dosfs.c,v 1.11.20.1 2008/01/09 01:56:38 matt 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 /*
    154  * Mount DOS filesystem
    155  */
    156 static int
    157 dos_mount(DOS_FS *fs, struct open_file *fd)
    158 {
    159 	int     err;
    160 
    161 	bzero(fs, sizeof(DOS_FS));
    162 	fs->fd = fd;
    163 	if ((err = !(fs->buf = alloc(SECSIZ)) ? errno : 0) ||
    164 	    (err = ioget(fs->fd, 0, fs->buf, 1)) ||
    165 	    (err = parsebs(fs, (DOS_BS *)fs->buf))) {
    166 		(void) dosunmount(fs);
    167 		return err;
    168 	}
    169 	return 0;
    170 }
    171 
    172 #ifndef LIBSA_NO_FS_CLOSE
    173 /*
    174  * Unmount mounted filesystem
    175  */
    176 static int
    177 dos_unmount(DOS_FS *fs)
    178 {
    179 	int     err;
    180 
    181 	if (fs->links)
    182 		return EBUSY;
    183 	if ((err = dosunmount(fs)))
    184 		return err;
    185 	return 0;
    186 }
    187 #endif
    188 
    189 /*
    190  * Common code shared by dos_mount() and dos_unmount()
    191  */
    192 static int
    193 dosunmount(DOS_FS *fs)
    194 {
    195 	if (fs->buf)
    196 		dealloc(fs->buf, SECSIZ);
    197 	dealloc(fs, sizeof(DOS_FS));
    198 	return 0;
    199 }
    200 
    201 /*
    202  * Open DOS file
    203  */
    204 int
    205 dosfs_open(const char *path, struct open_file *fd)
    206 {
    207 	const struct direntry *de;
    208 	DOS_FILE *f;
    209 	DOS_FS *fs;
    210 	u_int   size, clus;
    211 	int     err = 0;
    212 
    213 	/* Allocate mount structure, associate with open */
    214 	fs = alloc(sizeof(DOS_FS));
    215 
    216 	if ((err = dos_mount(fs, fd)))
    217 		goto out;
    218 
    219 	if ((err = namede(fs, path, &de)))
    220 		goto out;
    221 
    222 	clus = stclus(fs->fatsz, de);
    223 	size = getulong(de->deFileSize);
    224 
    225 	if ((!(de->deAttributes & ATTR_DIRECTORY) && (!clus != !size)) ||
    226 	    ((de->deAttributes & ATTR_DIRECTORY) && size) ||
    227 	    (clus && !okclus(fs, clus))) {
    228 		err = EINVAL;
    229 		goto out;
    230 	}
    231 
    232 	f = alloc(sizeof(DOS_FILE));
    233 #ifdef BOOTXX
    234 	/* due to __internal_memset_ causing all sorts of register spillage
    235 	   (and being completely unoptimized for zeroing small amounts of
    236 	   memory), if we hand-initialize the remaining members of f to zero,
    237 	   the code size drops 68 bytes. This makes no sense, admittedly. */
    238 	f->offset = 0;
    239 	f->c = 0;
    240 #else
    241 	bzero(f, sizeof(DOS_FILE));
    242 #endif
    243 	f->fs = fs;
    244 	fs->links++;
    245 	f->de = *de;
    246 	fd->f_fsdata = (void *)f;
    247 
    248 out:
    249 	return err;
    250 }
    251 
    252 /*
    253  * Read from file
    254  */
    255 int
    256 dosfs_read(struct open_file *fd, void *vbuf, size_t nbyte, size_t *resid)
    257 {
    258 	off_t   size;
    259 	u_int8_t *buf = vbuf;
    260 	u_int   nb, off, clus, c, cnt, n;
    261 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    262 	int     err = 0;
    263 
    264 	nb = (u_int) nbyte;
    265 	if ((size = fsize(f->fs, &f->de)) == -1)
    266 		return EINVAL;
    267 	if (nb > (n = size - f->offset))
    268 		nb = n;
    269 	off = f->offset;
    270 	if ((clus = stclus(f->fs->fatsz, &f->de)))
    271 		off &= f->fs->bsize - 1;
    272 	c = f->c;
    273 	cnt = nb;
    274 	while (cnt) {
    275 		n = 0;
    276 		if (!c) {
    277 			if ((c = clus))
    278 				n = bytblk(f->fs, f->offset);
    279 		} else if (!off) {
    280 			n++;
    281 		}
    282 		while (n--) {
    283 			if ((err = fatget(f->fs, &c)))
    284 				goto out;
    285 			if (!okclus(f->fs, c)) {
    286 				err = EINVAL;
    287 				goto out;
    288 			}
    289 		}
    290 		if (!clus || (n = f->fs->bsize - off) > cnt)
    291 			n = cnt;
    292 		if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
    293 				secbyt(f->fs->lsndir)) + off,
    294 			    buf, n)))
    295 			goto out;
    296 		f->offset += n;
    297 		f->c = c;
    298 		off = 0;
    299 		buf += n;
    300 		cnt -= n;
    301 	}
    302 out:
    303 	if (resid)
    304 		*resid = nbyte - nb + cnt;
    305 	return err;
    306 }
    307 
    308 #ifndef LIBSA_NO_FS_WRITE
    309 /*
    310  * Not implemented.
    311  */
    312 int
    313 dosfs_write(struct open_file *fd, void *start, size_t size, size_t *resid)
    314 {
    315 
    316 	return EROFS;
    317 }
    318 #endif /* !LIBSA_NO_FS_WRITE */
    319 
    320 #ifndef LIBSA_NO_FS_SEEK
    321 /*
    322  * Reposition within file
    323  */
    324 off_t
    325 dosfs_seek(struct open_file *fd, off_t offset, int whence)
    326 {
    327 	off_t   off;
    328 	u_int   size;
    329 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    330 
    331 	size = getulong(f->de.deFileSize);
    332 	switch (whence) {
    333 	case SEEK_SET:
    334 		off = 0;
    335 		break;
    336 	case SEEK_CUR:
    337 		off = f->offset;
    338 		break;
    339 	case SEEK_END:
    340 		off = size;
    341 		break;
    342 	default:
    343 		return -1;
    344 	}
    345 	off += offset;
    346 	if (off < 0 || off > size)
    347 		return -1;
    348 	f->offset = (u_int) off;
    349 	f->c = 0;
    350 	return off;
    351 }
    352 #endif /* !LIBSA_NO_FS_SEEK */
    353 
    354 #ifndef LIBSA_NO_FS_CLOSE
    355 /*
    356  * Close open file
    357  */
    358 int
    359 dosfs_close(struct open_file *fd)
    360 {
    361 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    362 	DOS_FS *fs = f->fs;
    363 
    364 	f->fs->links--;
    365 	dealloc(f, sizeof(DOS_FILE));
    366 	dos_unmount(fs);
    367 	return 0;
    368 }
    369 #endif /* !LIBSA_NO_FS_CLOSE */
    370 
    371 /*
    372  * Return some stat information on a file.
    373  */
    374 int
    375 dosfs_stat(struct open_file *fd, struct stat *sb)
    376 {
    377 	DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
    378 
    379 	/* only important stuff */
    380 	sb->st_mode = (f->de.deAttributes & ATTR_DIRECTORY) ?
    381 	    (S_IFDIR | 0555) : (S_IFREG | 0444);
    382 	sb->st_nlink = 1;
    383 	sb->st_uid = 0;
    384 	sb->st_gid = 0;
    385 	if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
    386 		return EINVAL;
    387 	return 0;
    388 }
    389 
    390 /*
    391  * Parse DOS boot sector
    392  */
    393 static int
    394 parsebs(DOS_FS *fs, DOS_BS *bs)
    395 {
    396 	u_int   sc;
    397 
    398 	if ((bs->jmp[0] != 0x69 &&
    399 		bs->jmp[0] != 0xe9 &&
    400 		(bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
    401 	    bs->bpb.bpbMedia < 0xf0)
    402 		return EINVAL;
    403 	if (getushort(bs->bpb.bpbBytesPerSec) != SECSIZ)
    404 		return EINVAL;
    405 	if (!(fs->spc = bs->bpb.bpbSecPerClust) || fs->spc & (fs->spc - 1))
    406 		return EINVAL;
    407 	fs->bsize = secbyt(fs->spc);
    408 	fs->bshift = ffs(fs->bsize) - 1;
    409 	if ((fs->spf = getushort(bs->bpb.bpbFATsecs))) {
    410 		if (bs->bpb.bpbFATs != 2)
    411 			return EINVAL;
    412 		if (!(fs->dirents = getushort(bs->bpb.bpbRootDirEnts)))
    413 			return EINVAL;
    414 	} else {
    415 		if (!(fs->spf = getulong(bs->bpb.bpbBigFATsecs)))
    416 			return EINVAL;
    417 		if (!bs->bpb.bpbFATs || bs->bpb.bpbFATs > 16)
    418 			return EINVAL;
    419 		if ((fs->rdcl = getulong(bs->bpb.bpbRootClust)) < LOCLUS)
    420 			return EINVAL;
    421 	}
    422 	if (!(fs->lsnfat = getushort(bs->bpb.bpbResSectors)))
    423 		return EINVAL;
    424 	fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.bpbFATs;
    425 	fs->lsndta = fs->lsndir + entsec(fs->dirents);
    426 	if (!(sc = getushort(bs->bpb.bpbSectors)) &&
    427 	    !(sc = getulong(bs->bpb.bpbHugeSectors)))
    428 		return EINVAL;
    429 	if (fs->lsndta > sc)
    430 		return EINVAL;
    431 	if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
    432 		return EINVAL;
    433 	fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
    434 	sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
    435 	if (fs->xclus > sc)
    436 		fs->xclus = sc;
    437 	return 0;
    438 }
    439 
    440 /*
    441  * Return directory entry from path
    442  */
    443 static int
    444 namede(DOS_FS *fs, const char *path, const struct direntry **dep)
    445 {
    446 	char    name[256];
    447 	const struct direntry *de;
    448 	char   *s;
    449 	size_t  n;
    450 	int     err;
    451 
    452 	err = 0;
    453 	de = dot;
    454 	if (*path == '/')
    455 		path++;
    456 	while (*path) {
    457 		if (!(s = strchr(path, '/')))
    458 			s = strchr(path, 0);
    459 		if ((n = s - path) > 255)
    460 			return ENAMETOOLONG;
    461 		memcpy(name, path, n);
    462 		name[n] = 0;
    463 		path = s;
    464 		if (!(de->deAttributes & ATTR_DIRECTORY))
    465 			return ENOTDIR;
    466 		if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
    467 			return err;
    468 		if (*path == '/')
    469 			path++;
    470 	}
    471 	*dep = de;
    472 	return 0;
    473 }
    474 
    475 /*
    476  * Lookup path segment
    477  */
    478 static int
    479 lookup(DOS_FS *fs, u_int clus, const char *name, const struct direntry **dep)
    480 {
    481 	static DOS_DIR *dir = NULL;
    482 	u_char  lfn[261];
    483 	u_char  sfn[13];
    484 	u_int   nsec, lsec, xdn, chk, sec, ent, x;
    485 	int     err = 0, ok, i;
    486 
    487 	if (!clus)
    488 		for (ent = 0; ent < 2; ent++)
    489 			if (!strcasecmp(name, dotstr[ent])) {
    490 				*dep = dot + ent;
    491 				return 0;
    492 			}
    493 
    494 	if (dir == NULL) {
    495 		dir = alloc(sizeof(DOS_DIR) * DEPSEC);
    496 		if (dir == NULL)
    497 			return ENOMEM;
    498 	}
    499 
    500 	if (!clus && fs->fatsz == 32)
    501 		clus = fs->rdcl;
    502 	nsec = !clus ? entsec(fs->dirents) : fs->spc;
    503 	lsec = 0;
    504 	xdn = chk = 0;
    505 	for (;;) {
    506 		if (!clus && !lsec)
    507 			lsec = fs->lsndir;
    508 		else if (okclus(fs, clus))
    509 			lsec = blklsn(fs, clus);
    510 		else {
    511 			err = EINVAL;
    512 			goto out;
    513 		}
    514 		for (sec = 0; sec < nsec; sec++) {
    515 			if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
    516 				goto out;
    517 			for (ent = 0; ent < DEPSEC; ent++) {
    518 				if (!*dir[ent].de.deName) {
    519 					err = ENOENT;
    520 					goto out;
    521 				}
    522 				if (*dir[ent].de.deName != 0xe5) {
    523 					if (dir[ent].de.deAttributes ==
    524 					    ATTR_WIN95) {
    525 						x = dir[ent].xde.weCnt;
    526 						if (x & WIN_LAST ||
    527 						    (x + 1 == xdn &&
    528 						     dir[ent].xde.weChksum ==
    529 						     chk)) {
    530 							if (x & WIN_LAST) {
    531 								chk = dir[ent].xde.weChksum;
    532 								x &= WIN_CNT;
    533 							}
    534 							if (x >= 1 && x <= 20) {
    535 								cp_xdnm(lfn, &dir[ent].xde);
    536 								xdn = x;
    537 								continue;
    538 							}
    539 						}
    540 					} else if (!(dir[ent].de.deAttributes &
    541 						     ATTR_VOLUME)) {
    542 						if ((ok = xdn == 1)) {
    543 							for (x = 0, i = 0;
    544 							     i < 11; i++)
    545 								x = ((((x & 1) << 7) | (x >> 1)) +
    546 								    dir[ent].de.deName[i]) & 0xff;
    547 							ok = chk == x &&
    548 							    !strcasecmp(name, (const char *)lfn);
    549 						}
    550 						if (!ok) {
    551 							cp_sfn(sfn, &dir[ent].de);
    552 							ok = !strcasecmp(name, (const char *)sfn);
    553 						}
    554 						if (ok) {
    555 							*dep = &dir[ent].de;
    556 							goto out2;
    557 						}
    558 					}
    559 				}
    560 				xdn = 0;
    561 			}
    562 		}
    563 		if (!clus)
    564 			break;
    565 		if ((err = fatget(fs, &clus)))
    566 			goto out;
    567 		if (fatend(fs->fatsz, clus))
    568 			break;
    569 	}
    570 	err = ENOENT;
    571  out:
    572 	dealloc(dir, sizeof(DOS_DIR) * DEPSEC);
    573 	dir = NULL;
    574  out2:
    575 	return err;
    576 }
    577 
    578 /*
    579  * Copy name from extended directory entry
    580  */
    581 static void
    582 cp_xdnm(u_char *lfn, struct winentry *xde)
    583 {
    584 	static const struct {
    585 		u_int   off;
    586 		u_int   dim;
    587 	} ix[3] = {
    588 		{ offsetof(struct winentry, wePart1),
    589 		    sizeof(xde->wePart1) / 2 },
    590 		{ offsetof(struct winentry, wePart2),
    591 		    sizeof(xde->wePart2) / 2 },
    592 		{ offsetof(struct winentry, wePart3),
    593 		    sizeof(xde->wePart3) / 2 }
    594 	};
    595 	u_char *p;
    596 	u_int   n, x, c;
    597 
    598 	lfn += 13 * ((xde->weCnt & WIN_CNT) - 1);
    599 	for (n = 0; n < 3; n++)
    600 		for (p = (u_char *)xde + ix[n].off, x = ix[n].dim; x;
    601 		    p += 2, x--) {
    602 			if ((c = getushort(p)) && (c < 32 || c > 127))
    603 				c = '?';
    604 			if (!(*lfn++ = c))
    605 				return;
    606 		}
    607 	if (xde->weCnt & WIN_LAST)
    608 		*lfn = 0;
    609 }
    610 
    611 /*
    612  * Copy short filename
    613  */
    614 static void
    615 cp_sfn(u_char *sfn, struct direntry *de)
    616 {
    617 	u_char *p;
    618 	int     j, i;
    619 
    620 	p = sfn;
    621 	if (*de->deName != ' ') {
    622 		for (j = 7; de->deName[j] == ' '; j--);
    623 		for (i = 0; i <= j; i++)
    624 			*p++ = de->deName[i];
    625 		if (*de->deExtension != ' ') {
    626 			*p++ = '.';
    627 			for (j = 2; de->deExtension[j] == ' '; j--);
    628 			for (i = 0; i <= j; i++)
    629 				*p++ = de->deExtension[i];
    630 		}
    631 	}
    632 	*p = 0;
    633 	if (*sfn == 5)
    634 		*sfn = 0xe5;
    635 }
    636 
    637 /*
    638  * Return size of file in bytes
    639  */
    640 static  off_t
    641 fsize(DOS_FS *fs, struct direntry *de)
    642 {
    643 	u_long  size;
    644 	u_int   c;
    645 	int     n;
    646 
    647 	if (!(size = getulong(de->deFileSize)) &&
    648 	    de->deAttributes & ATTR_DIRECTORY) {
    649 		if (!(c = getushort(de->deStartCluster))) {
    650 			size = fs->dirents * sizeof(struct direntry);
    651 		} else {
    652 			if ((n = fatcnt(fs, c)) == -1)
    653 				return n;
    654 			size = blkbyt(fs, n);
    655 		}
    656 	}
    657 	return size;
    658 }
    659 
    660 /*
    661  * Count number of clusters in chain
    662  */
    663 static int
    664 fatcnt(DOS_FS *fs, u_int c)
    665 {
    666 	int     n;
    667 
    668 	for (n = 0; okclus(fs, c); n++)
    669 		if (fatget(fs, &c))
    670 			return -1;
    671 	return fatend(fs->fatsz, c) ? n : -1;
    672 }
    673 
    674 /*
    675  * Get next cluster in cluster chain
    676  */
    677 static int
    678 fatget(DOS_FS *fs, u_int *c)
    679 {
    680 	u_char  buf[4];
    681 	u_int   x;
    682 	int     err;
    683 
    684 	err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
    685 	    fs->fatsz != 32 ? 2 : 4);
    686 	if (err)
    687 		return err;
    688 	x = fs->fatsz != 32 ? getushort(buf) : getulong(buf);
    689 	*c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
    690 	return 0;
    691 }
    692 
    693 /*
    694  * Is cluster an end-of-chain marker?
    695  */
    696 static int
    697 fatend(u_int sz, u_int c)
    698 {
    699 	return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
    700 }
    701 
    702 /*
    703  * Offset-based I/O primitive
    704  */
    705 static int
    706 ioread(DOS_FS *fs, u_int offset, void *buf, u_int nbyte)
    707 {
    708 	char   *s;
    709 	u_int   off, n;
    710 	int     err;
    711 
    712 	s = buf;
    713 	if ((off = offset & (SECSIZ - 1))) {
    714 		offset -= off;
    715 		if ((err = iobuf(fs, bytsec(offset))))
    716 			return err;
    717 		offset += SECSIZ;
    718 		if ((n = SECSIZ - off) > nbyte)
    719 			n = nbyte;
    720 		memcpy(s, fs->buf + off, n);
    721 		s += n;
    722 		nbyte -= n;
    723 	}
    724 	n = nbyte & (SECSIZ - 1);
    725 	if (nbyte -= n) {
    726 		if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
    727 			return err;
    728 		offset += nbyte;
    729 		s += nbyte;
    730 	}
    731 	if (n) {
    732 		if ((err = iobuf(fs, bytsec(offset))))
    733 			return err;
    734 		memcpy(s, fs->buf, n);
    735 	}
    736 	return 0;
    737 }
    738 
    739 /*
    740  * Buffered sector-based I/O primitive
    741  */
    742 static int
    743 iobuf(DOS_FS *fs, u_int lsec)
    744 {
    745 	int     err;
    746 
    747 	if (fs->bufsec != lsec) {
    748 		if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
    749 			return err;
    750 		fs->bufsec = lsec;
    751 	}
    752 	return 0;
    753 }
    754 
    755 /*
    756  * Sector-based I/O primitive
    757  */
    758 static int
    759 ioget(struct open_file *fd, u_int lsec, void *buf, u_int nsec)
    760 {
    761 	size_t rsize;
    762 	int err;
    763 
    764 #ifndef LIBSA_NO_TWIDDLE
    765 	twiddle();
    766 #endif
    767 	err = DEV_STRATEGY(fd->f_dev)(fd->f_devdata, F_READ, lsec,
    768 	    secbyt(nsec), buf, &rsize);
    769 	return err;
    770 }
    771