that the data in the file matches the type before we do any other tests.
This avoids having weak magic types such as msdos, match against text files.
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: ascmagic.c,v 1.80 2011/03/12 18:13:54 rrt Exp $")
+FILE_RCSID("@(#)$File: ascmagic.c,v 1.81 2011/03/15 22:16:29 christos Exp $")
#endif /* lint */
#include "magic.h"
}
protected int
-file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
+file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
+ int text)
{
unichar *ubuf = NULL;
size_t ulen;
}
rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
- type);
+ type, text);
done:
if (ubuf)
protected int
file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
- const char *type)
+ const char *type, int text)
{
unsigned char *utf8_buf = NULL, *utf8_end;
size_t mlen, i;
== NULL)
goto done;
if ((rv = file_softmagic(ms, utf8_buf,
- (size_t)(utf8_end - utf8_buf), TEXTTEST)) != 0)
+ (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) != 0)
goto subtype_identified;
else
rv = -1;
*/
/*
* file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.133 2011/05/13 22:15:40 christos Exp $
+ * @(#)$File: file.h,v 1.134 2011/09/16 21:23:59 christos Exp $
*/
#ifndef __file_h__
protected int file_zmagic(struct magic_set *, int, const char *,
const unsigned char *, size_t);
#endif
-protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
+protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t,
+ int);
protected int file_ascmagic_with_encoding(struct magic_set *,
const unsigned char *, size_t, unichar *, size_t, const char *,
- const char *);
+ const char *, int);
protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
unichar **, size_t *, const char **, const char **, const char **);
protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
- int);
+ int, int);
protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
protected uint64_t file_signextend(struct magic_set *, struct magic *,
uint64_t);
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.56 2011/02/03 01:43:33 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.57 2011/05/11 01:02:41 christos Exp $")
#endif /* lint */
#include "magic.h"
/* try soft magic tests */
if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
- if ((m = file_softmagic(ms, ubuf, nb, BINTEST)) != 0) {
+ if ((m = file_softmagic(ms, ubuf, nb, BINTEST,
+ looks_text)) != 0) {
if ((ms->flags & MAGIC_DEBUG) != 0)
(void)fprintf(stderr, "softmagic %d\n", m);
#ifdef BUILTIN_ELF
/* try text properties (and possibly text tokens) */
if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
- if ((m = file_ascmagic(ms, ubuf, nb)) != 0) {
+ if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 0) {
if ((ms->flags & MAGIC_DEBUG) != 0)
(void)fprintf(stderr, "ascmagic %d\n", m);
goto done;
if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
if (looks_text == 0)
if ((m = file_ascmagic_with_encoding( ms, ubuf,
- nb, u8buf, ulen, code, type)) != 0) {
+ nb, u8buf, ulen, code, type, looks_text))
+ != 0) {
if ((ms->flags & MAGIC_DEBUG) != 0)
(void)fprintf(stderr,
"ascmagic/enc %d\n", m);
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.144 2011/01/07 23:22:28 rrt Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.145 2011/05/13 22:15:40 christos Exp $")
#endif /* lint */
#include "magic.h"
private int match(struct magic_set *, struct magic *, uint32_t,
- const unsigned char *, size_t, int);
+ const unsigned char *, size_t, int, int);
private int mget(struct magic_set *, const unsigned char *,
- struct magic *, size_t, unsigned int);
+ struct magic *, size_t, unsigned int, int);
private int magiccheck(struct magic_set *, struct magic *);
private int32_t mprint(struct magic_set *, struct magic *);
private int32_t moffset(struct magic_set *, struct magic *);
*/
/*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
protected int
-file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, int mode)
+file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
+ int mode, int text)
{
struct mlist *ml;
int rv;
for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
- if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode)) != 0)
+ if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
+ text)) != 0)
return rv;
return 0;
*/
private int
match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
- const unsigned char *s, size_t nbytes, int mode)
+ const unsigned char *s, size_t nbytes, int mode, int text)
{
uint32_t magindex = 0;
unsigned int cont_level = 0;
int firstline = 1; /* a flag to print X\n X\n- X */
int printed_something = 0;
int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
+ int wrong_type = 0;
if (file_check_mem(ms, cont_level) == -1)
return -1;
int flush = 0;
struct magic *m = &magic[magindex];
- if ((m->flag & mode) != mode) {
+ if (IS_STRING(m->type)) {
+ if ((text && (m->str_flags & STRING_BINTEST)) ||
+ (!text && m->str_flags & STRING_TEXTTEST))
+ wrong_type = 1;
+ }
+
+ if (wrong_type || (m->flag & mode) != mode) {
/* Skip sub-tests */
while (magic[magindex + 1].cont_level != 0 &&
++magindex < nmagic)
ms->line = m->lineno;
/* if main entry matches, print it... */
- switch (mget(ms, s, m, nbytes, cont_level)) {
+ switch (mget(ms, s, m, nbytes, cont_level, text)) {
case -1:
return -1;
case 0:
continue;
}
#endif
- switch (mget(ms, s, m, nbytes, cont_level)) {
+ switch (mget(ms, s, m, nbytes, cont_level, text)) {
case -1:
return -1;
case 0:
private int
mget(struct magic_set *ms, const unsigned char *s,
- struct magic *m, size_t nbytes, unsigned int cont_level)
+ struct magic *m, size_t nbytes, unsigned int cont_level, int text)
{
uint32_t offset = ms->offset;
uint32_t count = m->str_range;
if (nbytes < offset)
return 0;
return file_softmagic(ms, s + offset, nbytes - offset,
- BINTEST);
+ BINTEST, text);
case FILE_DEFAULT: /* nothing to check */
default: