From cd410a1e370989d728a182ee677d7ba3ab6bf07c Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 5 Oct 2015 20:05:52 +0000 Subject: [PATCH] Add support for !:strength modifier to adjust strength of a test. --- usr.bin/file/magic-load.c | 63 +++++++++++++++++++++++++++++++++++++-- usr.bin/file/magic.h | 5 +++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/usr.bin/file/magic-load.c b/usr.bin/file/magic-load.c index c40cbbecc68..d45f30b6bd6 100644 --- a/usr.bin/file/magic-load.c +++ b/usr.bin/file/magic-load.c @@ -1,4 +1,4 @@ -/* $OpenBSD: magic-load.c,v 1.17 2015/08/11 23:17:17 nicm Exp $ */ +/* $OpenBSD: magic-load.c,v 1.18 2015/10/05 20:05:52 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott @@ -285,8 +285,10 @@ magic_get_strength(struct magic_line *ml) if (ml->type == MAGIC_TYPE_NONE) return (0); - if (ml->test_not || ml->test_operator == 'x') - return (1); + if (ml->test_not || ml->test_operator == 'x') { + n = 1; + goto skip; + } n = 2 * MAGIC_STRENGTH_MULTIPLIER; switch (ml->type) { @@ -385,6 +387,22 @@ magic_get_strength(struct magic_line *ml) n -= MAGIC_STRENGTH_MULTIPLIER; break; } + +skip: + switch (ml->strength_operator) { + case '+': + n += ml->strength_value; + break; + case '-': + n -= ml->strength_value; + break; + case '*': + n *= ml->strength_value; + break; + case '/': + n /= ml->strength_value; + break; + } return (n <= 0 ? 1 : n); } @@ -929,6 +947,41 @@ magic_compare(struct magic_line *ml1, struct magic_line *ml2) } RB_GENERATE(magic_tree, magic_line, node, magic_compare); +static void +magic_adjust_strength(struct magic *m, u_int at, struct magic_line *ml, + char *line) +{ + char *cp, *s; + int64_t value; + + cp = line + (sizeof "!:strength") - 1; + while (isspace((u_char)*cp)) + cp++; + s = cp; + + cp = strchr(s, '#'); + if (cp != NULL) + *cp = '\0'; + cp = s; + + if (strchr("+-*/", *s) == NULL) { + magic_warnm(m, at, "invalid strength operator: %s", s); + return; + } + ml->strength_operator = *cp++; + + while (isspace((u_char)*cp)) + cp++; + cp = magic_strtoll(cp, &value); + while (cp != NULL && isspace((u_char)*cp)) + cp++; + if (cp == NULL || *cp != '\0' || value < 0 || value > 255) { + magic_warnm(m, at, "invalid strength value: %s", s); + return; + } + ml->strength_value = value; +} + static void magic_set_mimetype(struct magic *m, u_int at, struct magic_line *ml, char *line) { @@ -1005,6 +1058,10 @@ magic_load(FILE *f, const char *path, int warnings) magic_set_mimetype(m, at, ml, line); continue; } + if (strncmp (line, "!:strength", 10) == 0) { + magic_adjust_strength(m, at, ml, line); + continue; + } if (strncmp (line, "!:", 2) == 0) { for (i = 0; i < 64 && line[i] != '\0'; i++) { if (isspace((u_char)line[i])) diff --git a/usr.bin/file/magic.h b/usr.bin/file/magic.h index 106e90a5745..e1c9fc1a8fd 100644 --- a/usr.bin/file/magic.h +++ b/usr.bin/file/magic.h @@ -1,4 +1,4 @@ -/* $OpenBSD: magic.h,v 1.10 2015/10/05 19:50:38 nicm Exp $ */ +/* $OpenBSD: magic.h,v 1.11 2015/10/05 20:05:52 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott @@ -106,6 +106,9 @@ struct magic_line { u_int strength; struct magic_line *parent; + char strength_operator; + u_int strength_value; + int text; int64_t offset; -- 2.20.1