#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: ascmagic.c,v 1.103 2019/05/06 21:23:38 christos Exp $")
+FILE_RCSID("@(#)$File: ascmagic.c,v 1.104 2019/05/07 02:27:11 christos Exp $")
#endif /* lint */
#include "magic.h"
if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
== NULL)
goto done;
- buffer_init(&bb, b->fd, utf8_buf,
+ buffer_init(&bb, b->fd, &b->st, utf8_buf,
CAST(size_t, utf8_end - utf8_buf));
if ((rv = file_softmagic(ms, &bb, NULL, NULL,
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: buffer.c,v 1.5 2019/02/20 02:35:27 christos Exp $")
+FILE_RCSID("@(#)$File: buffer.c,v 1.6 2019/05/07 02:27:11 christos Exp $")
#endif /* lint */
#include "magic.h"
#include <sys/stat.h>
void
-buffer_init(struct buffer *b, int fd, const void *data, size_t len)
+buffer_init(struct buffer *b, int fd, const struct stat *st, const void *data,
+ size_t len)
{
b->fd = fd;
- if (b->fd == -1 || fstat(b->fd, &b->st) == -1)
+ if (st)
+ memcpy(&b->st, st, sizeof(b->st));
+ else if (b->fd == -1 || fstat(b->fd, &b->st) == -1)
memset(&b->st, 0, sizeof(b->st));
b->fbuf = data;
b->flen = len;
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: compress.c,v 1.120 2019/05/07 02:20:27 christos Exp $")
+FILE_RCSID("@(#)$File: compress.c,v 1.121 2019/05/07 02:27:11 christos Exp $")
#endif
#include "magic.h"
if (urv == ERRDATA)
prv = format_decompression_error(ms, i, newbuf);
else
- prv = file_buffer(ms, -1, name, newbuf, nsz);
+ prv = file_buffer(ms, -1, NULL, name, newbuf, nsz);
if (prv == -1)
goto error;
rv = 1;
* XXX: If file_buffer fails here, we overwrite
* the compressed text. FIXME.
*/
- if (file_buffer(ms, -1, NULL, buf, nbytes) == -1) {
+ if (file_buffer(ms, -1, NULL, NULL, buf, nbytes) == -1) {
if (file_pop_buffer(ms, pb) != NULL)
abort();
goto error;
*/
/*
* file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.205 2019/05/06 21:22:13 christos Exp $
+ * @(#)$File: file.h,v 1.206 2019/05/07 02:27:11 christos Exp $
*/
#ifndef __file_h__
protected struct magic_set *file_ms_alloc(int);
protected void file_ms_free(struct magic_set *);
protected int file_default(struct magic_set *, size_t);
-protected int file_buffer(struct magic_set *, int, const char *, const void *,
- size_t);
+protected int file_buffer(struct magic_set *, int, struct stat *, const char *,
+ const void *, size_t);
protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
protected int file_vprintf(struct magic_set *, const char *, va_list)
size_t);
#endif /* __EMX__ */
-protected void buffer_init(struct buffer *, int, const void *, size_t);
+protected void buffer_init(struct buffer *, int, const struct stat *,
+ const void *, size_t);
protected void buffer_fini(struct buffer *);
protected int buffer_fill(const struct buffer *);
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.103 2019/05/06 21:22:13 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.104 2019/05/07 02:27:11 christos Exp $")
#endif /* lint */
#include "magic.h"
*/
/*ARGSUSED*/
protected int
-file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)),
+file_buffer(struct magic_set *ms, int fd, struct stat *st,
+ const char *inname __attribute__ ((__unused__)),
const void *buf, size_t nb)
{
int m = 0, rv = 0, looks_text = 0;
char *rbuf = NULL;
struct buffer b;
- buffer_init(&b, fd, buf, nb);
+ buffer_init(&b, fd, st, buf, nb);
ms->mode = b.st.st_mode;
if (nb == 0) {
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: magic.c,v 1.110 2019/04/15 16:49:29 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.111 2019/05/07 02:27:11 christos Exp $")
#endif /* lint */
#include "magic.h"
struct stat sb;
ssize_t nbytes = 0; /* number of bytes read from a datafile */
int ispipe = 0;
+ int okstat = 0;
off_t pos = CAST(off_t, -1);
if (file_reset(ms, 1) == -1)
int flags = O_RDONLY|O_BINARY|O_NONBLOCK;
errno = 0;
if ((fd = open(inname, flags)) < 0) {
- int okstat = stat(inname, &sb) == 0;
+ okstat = stat(inname, &sb) == 0;
if (okstat && S_ISFIFO(sb.st_mode))
ispipe = 1;
#ifdef WIN32
}
if (fd != -1) {
- if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
+ if (!okstat)
+ okstat = fstat(fd, &sb) == 0;
+ if (okstat && S_ISFIFO(sb.st_mode))
ispipe = 1;
if (inname == NULL)
pos = lseek(fd, CAST(off_t, 0), SEEK_CUR);
}
(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
- if (file_buffer(ms, fd, inname, buf, CAST(size_t, nbytes)) == -1)
+ if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1)
goto done;
rv = 0;
done:
* The main work is done here!
* We have the file name and/or the data buffer to be identified.
*/
- if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
+ if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) {
return NULL;
}
return file_getbuffer(ms);
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: readelf.c,v 1.164 2019/04/15 16:49:53 christos Exp $")
+FILE_RCSID("@(#)$File: readelf.c,v 1.165 2019/05/07 02:27:11 christos Exp $")
#endif
#ifdef BUILTIN_ELF
int clazz;
int swap;
struct stat st;
+ const struct stat *stp;
off_t fsize;
int flags = 0;
Elf32_Ehdr elf32hdr;
&& (errno == ESPIPE))
fd = file_pipe2file(ms, fd, buf, nbytes);
- if (fd == -1 || fstat(fd, &st) == -1) {
- file_badread(ms);
+ if (fd == -1) {
+ file_badread(ms);
return -1;
}
- if (S_ISREG(st.st_mode) || st.st_size != 0)
- fsize = st.st_size;
+
+ stp = &b->st;
+ /*
+ * b->st.st_size != 0 if previous fstat() succeeded,
+ * which is likely, we can avoid extra stat() call.
+ */
+ if (b->st.st_size == 0) {
+ stp = &st;
+ if (fstat(fd, &st) == -1) {
+ file_badread(ms);
+ return -1;
+ }
+ }
+ if (S_ISREG(stp->st_mode) || stp->st_size != 0)
+ fsize = stp->st_size;
else
fsize = SIZE_UNKNOWN;
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.283 2019/05/06 21:22:40 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.284 2019/05/07 02:27:11 christos Exp $")
#endif /* lint */
#include "magic.h"
}
if (CAST(size_t, -m->offset) > b->elen)
return -1;
- buffer_init(bb, -1, b->ebuf, b->elen);
+ buffer_init(bb, -1, NULL, b->ebuf, b->elen);
ms->eoffset = ms->offset = CAST(int32_t, b->elen + m->offset);
} else {
if (cont_level == 0) {
normal:
// XXX: Pass real fd, then who frees bb?
- buffer_init(bb, -1, b->fbuf, b->flen);
+ buffer_init(bb, -1, NULL, b->fbuf, b->flen);
ms->offset = m->offset;
ms->eoffset = 0;
} else {