git: 6d4d2c4ebd04 - stable/12 - libsa: cstyle cleanup of dosfs.c
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 08 Oct 2021 16:09:19 UTC
The branch stable/12 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=6d4d2c4ebd04fb6debc930f058d6d4a625cb5582 commit 6d4d2c4ebd04fb6debc930f058d6d4a625cb5582 Author: Toomas Soome <tsoome@FreeBSD.org> AuthorDate: 2021-08-19 14:33:24 +0000 Commit: Kyle Evans <kevans@FreeBSD.org> CommitDate: 2021-10-08 08:16:35 +0000 libsa: cstyle cleanup of dosfs.c No functional changes intended. MFC after: 1 week (cherry picked from commit 3ec0714d6db0060113f6cadac6f4e44640603e8f) --- stand/libsa/dosfs.c | 1175 ++++++++++++++++++++++++++------------------------- 1 file changed, 595 insertions(+), 580 deletions(-) diff --git a/stand/libsa/dosfs.c b/stand/libsa/dosfs.c index 2d6029be837e..656af3642c88 100644 --- a/stand/libsa/dosfs.c +++ b/stand/libsa/dosfs.c @@ -69,40 +69,40 @@ struct fs_ops dosfs_fsops = { /* DOS "BIOS Parameter Block" */ typedef struct { - u_char secsiz[2]; /* sector size */ - u_char spc; /* sectors per cluster */ - u_char ressec[2]; /* reserved sectors */ - u_char fats; /* FATs */ - u_char dirents[2]; /* root directory entries */ - u_char secs[2]; /* total sectors */ - u_char media; /* media descriptor */ - u_char spf[2]; /* sectors per FAT */ - u_char spt[2]; /* sectors per track */ - u_char heads[2]; /* drive heads */ - u_char hidsec[4]; /* hidden sectors */ - u_char lsecs[4]; /* huge sectors */ - u_char lspf[4]; /* huge sectors per FAT */ - u_char xflg[2]; /* flags */ - u_char vers[2]; /* filesystem version */ - u_char rdcl[4]; /* root directory start cluster */ - u_char infs[2]; /* filesystem info sector */ - u_char bkbs[2]; /* backup boot sector */ + u_char secsiz[2]; /* sector size */ + u_char spc; /* sectors per cluster */ + u_char ressec[2]; /* reserved sectors */ + u_char fats; /* FATs */ + u_char dirents[2]; /* root directory entries */ + u_char secs[2]; /* total sectors */ + u_char media; /* media descriptor */ + u_char spf[2]; /* sectors per FAT */ + u_char spt[2]; /* sectors per track */ + u_char heads[2]; /* drive heads */ + u_char hidsec[4]; /* hidden sectors */ + u_char lsecs[4]; /* huge sectors */ + u_char lspf[4]; /* huge sectors per FAT */ + u_char xflg[2]; /* flags */ + u_char vers[2]; /* filesystem version */ + u_char rdcl[4]; /* root directory start cluster */ + u_char infs[2]; /* filesystem info sector */ + u_char bkbs[2]; /* backup boot sector */ } DOS_BPB; /* Initial portion of DOS boot sector */ typedef struct { - u_char jmp[3]; /* usually 80x86 'jmp' opcode */ - u_char oem[8]; /* OEM name and version */ - DOS_BPB bpb; /* BPB */ + u_char jmp[3]; /* usually 80x86 'jmp' opcode */ + u_char oem[8]; /* OEM name and version */ + DOS_BPB bpb; /* BPB */ } DOS_BS; /* Supply missing "." and ".." root directory entries */ static const char *const dotstr[2] = {".", ".."}; static DOS_DE dot[2] = { - {". ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, - {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}, - {".. ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, - {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}} + {". ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, + {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}, + {".. ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, + {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}} }; /* The usual conversion macros to avoid multiplication and division */ @@ -148,31 +148,31 @@ static int ioget(struct open_file *, daddr_t, void *, size_t); static int dos_read_fatblk(DOS_FS *fs, struct open_file *fd, u_int blknum) { - int err; - size_t io_size; - daddr_t offset_in_fat, max_offset_in_fat; - - offset_in_fat = ((daddr_t)blknum) * FATBLKSZ; - max_offset_in_fat = secbyt(fs->spf); - io_size = FATBLKSZ; - if (offset_in_fat > max_offset_in_fat) - offset_in_fat = max_offset_in_fat; - if (offset_in_fat + io_size > max_offset_in_fat) - io_size = ((size_t)(max_offset_in_fat - offset_in_fat)); - - if (io_size != 0) { - err = ioget(fd, fs->lsnfat + bytsec(offset_in_fat), - fs->fatbuf, io_size); - if (err != 0) { - fs->fatbuf_blknum = ((u_int)(-1)); - return (err); - } - } - if (io_size < FATBLKSZ) - memset(fs->fatbuf + io_size, 0, FATBLKSZ - io_size); - - fs->fatbuf_blknum = blknum; - return (0); + int err; + size_t io_size; + daddr_t offset_in_fat, max_offset_in_fat; + + offset_in_fat = ((daddr_t)blknum) * FATBLKSZ; + max_offset_in_fat = secbyt(fs->spf); + io_size = FATBLKSZ; + if (offset_in_fat > max_offset_in_fat) + offset_in_fat = max_offset_in_fat; + if (offset_in_fat + io_size > max_offset_in_fat) + io_size = ((size_t)(max_offset_in_fat - offset_in_fat)); + + if (io_size != 0) { + err = ioget(fd, fs->lsnfat + bytsec(offset_in_fat), + fs->fatbuf, io_size); + if (err != 0) { + fs->fatbuf_blknum = ((u_int)(-1)); + return (err); + } + } + if (io_size < FATBLKSZ) + memset(fs->fatbuf + io_size, 0, FATBLKSZ - io_size); + + fs->fatbuf_blknum = blknum; + return (0); } /* @@ -181,38 +181,38 @@ dos_read_fatblk(DOS_FS *fs, struct open_file *fd, u_int blknum) static int dos_mount(DOS_FS *fs, struct open_file *fd) { - int err; - u_char *buf; - - bzero(fs, sizeof(DOS_FS)); - fs->fd = fd; - - if ((buf = malloc(secbyt(1))) == NULL) - return (errno); - if ((err = ioget(fs->fd, 0, buf, secbyt(1))) || - (err = parsebs(fs, (DOS_BS *)buf))) { - free(buf); - return (err); - } - free(buf); - - if ((fs->fatbuf = malloc(FATBLKSZ)) == NULL) - return (errno); - err = dos_read_fatblk(fs, fd, 0); - if (err != 0) { - free(fs->fatbuf); - return (err); - } - - fs->root = dot[0]; - fs->root.name[0] = ' '; - if (fs->fatsz == 32) { - fs->root.clus[0] = fs->rdcl & 0xff; - fs->root.clus[1] = (fs->rdcl >> 8) & 0xff; - fs->root.dex.h_clus[0] = (fs->rdcl >> 16) & 0xff; - fs->root.dex.h_clus[1] = (fs->rdcl >> 24) & 0xff; - } - return (0); + int err; + u_char *buf; + + bzero(fs, sizeof(DOS_FS)); + fs->fd = fd; + + if ((buf = malloc(secbyt(1))) == NULL) + return (errno); + if ((err = ioget(fs->fd, 0, buf, secbyt(1))) || + (err = parsebs(fs, (DOS_BS *)buf))) { + free(buf); + return (err); + } + free(buf); + + if ((fs->fatbuf = malloc(FATBLKSZ)) == NULL) + return (errno); + err = dos_read_fatblk(fs, fd, 0); + if (err != 0) { + free(fs->fatbuf); + return (err); + } + + fs->root = dot[0]; + fs->root.name[0] = ' '; + if (fs->fatsz == 32) { + fs->root.clus[0] = fs->rdcl & 0xff; + fs->root.clus[1] = (fs->rdcl >> 8) & 0xff; + fs->root.dex.h_clus[0] = (fs->rdcl >> 16) & 0xff; + fs->root.dex.h_clus[1] = (fs->rdcl >> 24) & 0xff; + } + return (0); } /* @@ -221,11 +221,11 @@ dos_mount(DOS_FS *fs, struct open_file *fd) static int dos_unmount(DOS_FS *fs) { - if (fs->links) - return (EBUSY); - free(fs->fatbuf); - free(fs); - return (0); + if (fs->links) + return (EBUSY); + free(fs->fatbuf); + free(fs); + return (0); } /* @@ -234,45 +234,45 @@ dos_unmount(DOS_FS *fs) static int dos_open(const char *path, struct open_file *fd) { - DOS_DE *de; - DOS_FILE *f; - DOS_FS *fs; - u_int size, clus; - int err; - - /* Allocate mount structure, associate with open */ - if ((fs = malloc(sizeof(DOS_FS))) == NULL) - return (errno); - if ((err = dos_mount(fs, fd))) { - free(fs); - return (err); - } - - if ((err = namede(fs, path, &de))) { - dos_unmount(fs); - return (err); - } - - clus = stclus(fs->fatsz, de); - size = cv4(de->size); - - if ((!(de->attr & FA_DIR) && (!clus != !size)) || - ((de->attr & FA_DIR) && size) || - (clus && !okclus(fs, clus))) { - dos_unmount(fs); - return (EINVAL); - } - if ((f = malloc(sizeof(DOS_FILE))) == NULL) { - err = errno; - dos_unmount(fs); - return (err); - } - bzero(f, sizeof(DOS_FILE)); - f->fs = fs; - fs->links++; - f->de = *de; - fd->f_fsdata = (void *)f; - return (0); + DOS_DE *de; + DOS_FILE *f; + DOS_FS *fs; + u_int size, clus; + int err; + + /* Allocate mount structure, associate with open */ + if ((fs = malloc(sizeof(DOS_FS))) == NULL) + return (errno); + if ((err = dos_mount(fs, fd))) { + free(fs); + return (err); + } + + if ((err = namede(fs, path, &de))) { + dos_unmount(fs); + return (err); + } + + clus = stclus(fs->fatsz, de); + size = cv4(de->size); + + if ((!(de->attr & FA_DIR) && (!clus != !size)) || + ((de->attr & FA_DIR) && size) || + (clus && !okclus(fs, clus))) { + dos_unmount(fs); + return (EINVAL); + } + if ((f = malloc(sizeof(DOS_FILE))) == NULL) { + err = errno; + dos_unmount(fs); + return (err); + } + bzero(f, sizeof(DOS_FILE)); + f->fs = fs; + fs->links++; + f->de = *de; + fd->f_fsdata = (void *)f; + return (0); } /* @@ -281,57 +281,58 @@ dos_open(const char *path, struct open_file *fd) static int dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid) { - off_t size; - u_int nb, off, clus, c, cnt, n; - DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; - int err = 0; - - /* - * as ioget() can be called *a lot*, use twiddle here. - * also 4 seems to be good value not to slow loading down too much: - * with 270MB file (~540k ioget() calls, twiddle can easily waste 4-5sec. - */ - twiddle(4); - nb = (u_int)nbyte; - if ((size = fsize(f->fs, &f->de)) == -1) - return (EINVAL); - if (nb > (n = size - f->offset)) - nb = n; - off = f->offset; - if ((clus = stclus(f->fs->fatsz, &f->de))) - off &= f->fs->bsize - 1; - c = f->c; - cnt = nb; - while (cnt) { - n = 0; - if (!c) { - if ((c = clus)) - n = bytblk(f->fs, f->offset); - } else if (!off) - n++; - while (n--) { - if ((err = fatget(f->fs, &c))) - goto out; - if (!okclus(f->fs, c)) { - err = EINVAL; - goto out; - } + off_t size; + u_int nb, off, clus, c, cnt, n; + DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; + int err = 0; + + /* + * as ioget() can be called *a lot*, use twiddle here. + * also 4 seems to be good value not to slow loading down too much: + * with 270MB file (~540k ioget() calls, twiddle can easily waste + * 4-5 sec. + */ + twiddle(4); + nb = (u_int)nbyte; + if ((size = fsize(f->fs, &f->de)) == -1) + return (EINVAL); + if (nb > (n = size - f->offset)) + nb = n; + off = f->offset; + if ((clus = stclus(f->fs->fatsz, &f->de))) + off &= f->fs->bsize - 1; + c = f->c; + cnt = nb; + while (cnt) { + n = 0; + if (!c) { + if ((c = clus)) + n = bytblk(f->fs, f->offset); + } else if (!off) + n++; + while (n--) { + if ((err = fatget(f->fs, &c))) + goto out; + if (!okclus(f->fs, c)) { + err = EINVAL; + goto out; + } + } + if (!clus || (n = f->fs->bsize - off) > cnt) + n = cnt; + if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) : + secbyt(f->fs->lsndir)) + off, buf, n))) + goto out; + f->offset += n; + f->c = c; + off = 0; + buf = (char *)buf + n; + cnt -= n; } - if (!clus || (n = f->fs->bsize - off) > cnt) - n = cnt; - if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) : - secbyt(f->fs->lsndir)) + off, buf, n))) - goto out; - f->offset += n; - f->c = c; - off = 0; - buf = (char *)buf + n; - cnt -= n; - } - out: - if (resid) - *resid = nbyte - nb + cnt; - return (err); +out: + if (resid) + *resid = nbyte - nb + cnt; + return (err); } /* @@ -340,33 +341,33 @@ dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid) static off_t dos_seek(struct open_file *fd, off_t offset, int whence) { - off_t off; - u_int size; - DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; - - size = cv4(f->de.size); - switch (whence) { - case SEEK_SET: - off = 0; - break; - case SEEK_CUR: - off = f->offset; - break; - case SEEK_END: - off = size; - break; - default: - errno = EINVAL; - return (-1); - } - off += offset; - if (off < 0 || off > size) { - errno = EINVAL; - return (-1); - } - f->offset = (u_int)off; - f->c = 0; - return (off); + off_t off; + u_int size; + DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; + + size = cv4(f->de.size); + switch (whence) { + case SEEK_SET: + off = 0; + break; + case SEEK_CUR: + off = f->offset; + break; + case SEEK_END: + off = size; + break; + default: + errno = EINVAL; + return (-1); + } + off += offset; + if (off < 0 || off > size) { + errno = EINVAL; + return (-1); + } + f->offset = (u_int)off; + f->c = 0; + return (off); } /* @@ -375,13 +376,13 @@ dos_seek(struct open_file *fd, off_t offset, int whence) static int dos_close(struct open_file *fd) { - DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; - DOS_FS *fs = f->fs; + DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; + DOS_FS *fs = f->fs; - f->fs->links--; - free(f); - dos_unmount(fs); - return (0); + f->fs->links--; + free(f); + dos_unmount(fs); + return (0); } /* @@ -390,105 +391,106 @@ dos_close(struct open_file *fd) static int dos_stat(struct open_file *fd, struct stat *sb) { - DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; - - /* only important stuff */ - sb->st_mode = f->de.attr & FA_DIR ? S_IFDIR | 0555 : S_IFREG | 0444; - sb->st_nlink = 1; - sb->st_uid = 0; - sb->st_gid = 0; - if ((sb->st_size = fsize(f->fs, &f->de)) == -1) - return (EINVAL); - return (0); + DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; + + /* only important stuff */ + sb->st_mode = f->de.attr & FA_DIR ? S_IFDIR | 0555 : S_IFREG | 0444; + sb->st_nlink = 1; + sb->st_uid = 0; + sb->st_gid = 0; + if ((sb->st_size = fsize(f->fs, &f->de)) == -1) + return (EINVAL); + return (0); } static int dos_checksum(unsigned char *name, unsigned char *ext) { - int x, i; - char buf[11]; - - bcopy(name, buf, 8); - bcopy(ext, buf+8, 3); - x = 0; - for (i = 0; i < 11; i++) { - x = ((x & 1) << 7) | (x >> 1); - x += buf[i]; - x &= 0xff; - } - return (x); + int x, i; + char buf[11]; + + bcopy(name, buf, 8); + bcopy(ext, buf+8, 3); + x = 0; + for (i = 0; i < 11; i++) { + x = ((x & 1) << 7) | (x >> 1); + x += buf[i]; + x &= 0xff; + } + return (x); } static int dos_readdir(struct open_file *fd, struct dirent *d) { - /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */ - u_char fn[261]; - DOS_DIR dd; - size_t res; - u_int chk, x, xdn; - int err; - - x = chk = 0; - while (1) { - xdn = x; - x = 0; - err = dos_read(fd, &dd, sizeof(dd), &res); - if (err) - return (err); - if (res == sizeof(dd)) - return (ENOENT); - if (dd.de.name[0] == 0) - return (ENOENT); - - /* Skip deleted entries */ - if (dd.de.name[0] == 0xe5) - continue; - - /* Check if directory entry is volume label */ - if (dd.de.attr & FA_LABEL) { - /* - * If volume label set, check if the current entry is - * extended entry (FA_XDE) for long file names. - */ - if ((dd.de.attr & FA_MASK) == FA_XDE) { - /* - * Read through all following extended entries - * to get the long file name. 0x40 marks the - * last entry containing part of long file name. - */ - if (dd.xde.seq & 0x40) - chk = dd.xde.chk; - else if (dd.xde.seq != xdn - 1 || dd.xde.chk != chk) - continue; - x = dd.xde.seq & ~0x40; - if (x < 1 || x > 20) { - x = 0; - continue; + /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */ + u_char fn[261]; + DOS_DIR dd; + size_t res; + u_int chk, x, xdn; + int err; + + x = chk = 0; + for (;;) { + xdn = x; + x = 0; + err = dos_read(fd, &dd, sizeof(dd), &res); + if (err) + return (err); + if (res == sizeof(dd)) + return (ENOENT); + if (dd.de.name[0] == 0) + return (ENOENT); + + /* Skip deleted entries */ + if (dd.de.name[0] == 0xe5) + continue; + + /* Check if directory entry is volume label */ + if (dd.de.attr & FA_LABEL) { + /* + * If volume label set, check if the current entry is + * extended entry (FA_XDE) for long file names. + */ + if ((dd.de.attr & FA_MASK) == FA_XDE) { + /* + * Read through all following extended entries + * to get the long file name. 0x40 marks the + * last entry containing part of long file name. + */ + if (dd.xde.seq & 0x40) + chk = dd.xde.chk; + else if (dd.xde.seq != xdn - 1 || + dd.xde.chk != chk) + continue; + x = dd.xde.seq & ~0x40; + if (x < 1 || x > 20) { + x = 0; + continue; + } + cp_xdnm(fn, &dd.xde); + } else { + /* skip only volume label entries */ + continue; + } + } else { + if (xdn == 1) { + x = dos_checksum(dd.de.name, dd.de.ext); + if (x == chk) + break; + } else { + cp_sfn(fn, &dd.de); + break; + } + x = 0; } - cp_xdnm(fn, &dd.xde); - } else { - /* skip only volume label entries */ - continue; - } - } else { - if (xdn == 1) { - x = dos_checksum(dd.de.name, dd.de.ext); - if (x == chk) - break; - } else { - cp_sfn(fn, &dd.de); - break; - } - x = 0; } - } - d->d_fileno = (dd.de.clus[1] << 8) + dd.de.clus[0]; - d->d_reclen = sizeof(*d); - d->d_type = (dd.de.attr & FA_DIR) ? DT_DIR : DT_REG; - memcpy(d->d_name, fn, sizeof(d->d_name)); - return (0); + d->d_fileno = (dd.de.clus[1] << 8) + dd.de.clus[0]; + d->d_reclen = sizeof(*d); + d->d_type = (dd.de.attr & FA_DIR) ? DT_DIR : DT_REG; + memcpy(d->d_name, fn, sizeof(d->d_name)); + return (0); } /* @@ -497,47 +499,47 @@ dos_readdir(struct open_file *fd, struct dirent *d) static int parsebs(DOS_FS *fs, DOS_BS *bs) { - u_int sc; - - if ((bs->jmp[0] != 0x69 && - bs->jmp[0] != 0xe9 && - (bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) || - bs->bpb.media < 0xf0) - return (EINVAL); - if (cv2(bs->bpb.secsiz) != SECSIZ) - return (EINVAL); - if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1)) - return (EINVAL); - fs->bsize = secbyt(fs->spc); - fs->bshift = ffs(fs->bsize) - 1; - if ((fs->spf = cv2(bs->bpb.spf))) { - if (bs->bpb.fats != 2) - return (EINVAL); - if (!(fs->dirents = cv2(bs->bpb.dirents))) - return (EINVAL); - } else { - if (!(fs->spf = cv4(bs->bpb.lspf))) - return (EINVAL); - if (!bs->bpb.fats || bs->bpb.fats > 16) - return (EINVAL); - if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS) - return (EINVAL); - } - if (!(fs->lsnfat = cv2(bs->bpb.ressec))) - return (EINVAL); - fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.fats; - fs->lsndta = fs->lsndir + entsec(fs->dirents); - if (!(sc = cv2(bs->bpb.secs)) && !(sc = cv4(bs->bpb.lsecs))) - return (EINVAL); - if (fs->lsndta > sc) - return (EINVAL); - if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS) - return (EINVAL); - fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32; - sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1; - if (fs->xclus > sc) - fs->xclus = sc; - return (0); + u_int sc; + + if ((bs->jmp[0] != 0x69 && + bs->jmp[0] != 0xe9 && + (bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) || + bs->bpb.media < 0xf0) + return (EINVAL); + if (cv2(bs->bpb.secsiz) != SECSIZ) + return (EINVAL); + if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1)) + return (EINVAL); + fs->bsize = secbyt(fs->spc); + fs->bshift = ffs(fs->bsize) - 1; + if ((fs->spf = cv2(bs->bpb.spf))) { + if (bs->bpb.fats != 2) + return (EINVAL); + if (!(fs->dirents = cv2(bs->bpb.dirents))) + return (EINVAL); + } else { + if (!(fs->spf = cv4(bs->bpb.lspf))) + return (EINVAL); + if (!bs->bpb.fats || bs->bpb.fats > 16) + return (EINVAL); + if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS) + return (EINVAL); + } + if (!(fs->lsnfat = cv2(bs->bpb.ressec))) + return (EINVAL); + fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.fats; + fs->lsndta = fs->lsndir + entsec(fs->dirents); + if (!(sc = cv2(bs->bpb.secs)) && !(sc = cv4(bs->bpb.lsecs))) + return (EINVAL); + if (fs->lsndta > sc) + return (EINVAL); + if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS) + return (EINVAL); + fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32; + sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1; + if (fs->xclus > sc) + fs->xclus = sc; + return (0); } /* @@ -546,33 +548,33 @@ parsebs(DOS_FS *fs, DOS_BS *bs) static int namede(DOS_FS *fs, const char *path, DOS_DE **dep) { - char name[256]; - DOS_DE *de; - char *s; - size_t n; - int err; - - err = 0; - de = &fs->root; - while (*path) { - while (*path == '/') - path++; - if (*path == '\0') - break; - if (!(s = strchr(path, '/'))) - s = strchr(path, 0); - if ((n = s - path) > 255) - return (ENAMETOOLONG); - memcpy(name, path, n); - name[n] = 0; - path = s; - if (!(de->attr & FA_DIR)) - return (ENOTDIR); - if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de))) - return (err); - } - *dep = de; - return (0); + char name[256]; + DOS_DE *de; + char *s; + size_t n; + int err; + + err = 0; + de = &fs->root; + while (*path) { + while (*path == '/') + path++; + if (*path == '\0') + break; + if (!(s = strchr(path, '/'))) + s = strchr(path, 0); + if ((n = s - path) > 255) + return (ENAMETOOLONG); + memcpy(name, path, n); + name[n] = 0; + path = s; + if (!(de->attr & FA_DIR)) + return (ENOTDIR); + if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de))) + return (err); + } + *dep = de; + return (0); } /* @@ -581,78 +583,85 @@ namede(DOS_FS *fs, const char *path, DOS_DE **dep) static int lookup(DOS_FS *fs, u_int clus, const char *name, DOS_DE **dep) { - static DOS_DIR dir[DEPSEC]; - u_char lfn[261]; - u_char sfn[13]; - u_int nsec, lsec, xdn, chk, sec, ent, x; - int err, ok; - - if (!clus) - for (ent = 0; ent < 2; ent++) - if (!strcasecmp(name, dotstr[ent])) { - *dep = dot + ent; - return (0); - } - if (!clus && fs->fatsz == 32) - clus = fs->rdcl; - nsec = !clus ? entsec(fs->dirents) : fs->spc; - lsec = 0; - xdn = chk = 0; - for (;;) { - if (!clus && !lsec) - lsec = fs->lsndir; - else if (okclus(fs, clus)) - lsec = blklsn(fs, clus); - else - return (EINVAL); - for (sec = 0; sec < nsec; sec++) { - if ((err = ioget(fs->fd, lsec + sec, dir, secbyt(1)))) - return (err); - for (ent = 0; ent < DEPSEC; ent++) { - if (!*dir[ent].de.name) - return (ENOENT); - if (*dir[ent].de.name != 0xe5) { - if ((dir[ent].de.attr & FA_MASK) == FA_XDE) { - x = dir[ent].xde.seq; - if (x & 0x40 || (x + 1 == xdn && - dir[ent].xde.chk == chk)) { - if (x & 0x40) { - chk = dir[ent].xde.chk; - x &= ~0x40; - } - if (x >= 1 && x <= 20) { - cp_xdnm(lfn, &dir[ent].xde); - xdn = x; - continue; - } - } - } else if (!(dir[ent].de.attr & FA_LABEL)) { - if ((ok = xdn == 1)) { - x = dos_checksum(dir[ent].de.name, dir[ent].de.ext); - ok = chk == x && - !strcasecmp(name, (const char *)lfn); - } - if (!ok) { - cp_sfn(sfn, &dir[ent].de); - ok = !strcasecmp(name, (const char *)sfn); - } - if (ok) { - *dep = &dir[ent].de; - return (0); - } - } + static DOS_DIR dir[DEPSEC]; + u_char lfn[261]; + u_char sfn[13]; + u_int nsec, lsec, xdn, chk, sec, ent, x; + int err, ok; + + if (!clus) + for (ent = 0; ent < 2; ent++) + if (!strcasecmp(name, dotstr[ent])) { + *dep = dot + ent; + return (0); } - xdn = 0; - } - } - if (!clus) - break; - if ((err = fatget(fs, &clus))) - return (err); - if (fatend(fs->fatsz, clus)) - break; - } - return (ENOENT); + if (!clus && fs->fatsz == 32) + clus = fs->rdcl; + nsec = !clus ? entsec(fs->dirents) : fs->spc; + lsec = 0; + xdn = chk = 0; + for (;;) { + if (!clus && !lsec) + lsec = fs->lsndir; + else if (okclus(fs, clus)) + lsec = blklsn(fs, clus); + else + return (EINVAL); + for (sec = 0; sec < nsec; sec++) { + if ((err = ioget(fs->fd, lsec + sec, dir, secbyt(1)))) + return (err); + for (ent = 0; ent < DEPSEC; ent++) { + if (!*dir[ent].de.name) + return (ENOENT); + if (*dir[ent].de.name != 0xe5) { + if ((dir[ent].de.attr & FA_MASK) == + FA_XDE) { + x = dir[ent].xde.seq; + if (x & 0x40 || (x + 1 == xdn && + dir[ent].xde.chk == chk)) { + if (x & 0x40) { + chk = dir[ent].xde.chk; + x &= ~0x40; + } + if (x >= 1 && x <= 20) { + cp_xdnm(lfn, &dir[ent].xde); + xdn = x; + continue; + } + } + } else if (!(dir[ent].de.attr & + FA_LABEL)) { + if ((ok = xdn == 1)) { + x = dos_checksum( + dir[ent].de.name, + dir[ent].de.ext); + ok = chk == x && + !strcasecmp(name, + (const char *)lfn); + } + if (!ok) { + cp_sfn(sfn, *** 377 LINES SKIPPED ***