summaryrefslogtreecommitdiff
path: root/usr.bin/file
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2015-10-05 20:05:53 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2015-10-05 20:05:53 +0000
commit775cd0daa9a5cd1526bb295c074248d0da5f1b97 (patch)
tree213403dd619bd4cf02693de097194833e74a1ce2 /usr.bin/file
parent6bbd2f17cd9183606bb244bbb39a3c31cca15fc6 (diff)
Add support for !:strength modifier to adjust strength of a test.
Diffstat (limited to 'usr.bin/file')
-rw-r--r--usr.bin/file/magic-load.c63
-rw-r--r--usr.bin/file/magic.h5
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 <nicm@openbsd.org>
@@ -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);
}
@@ -930,6 +948,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)
{
char *mimetype, *cp;
@@ -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 <nicm@openbsd.org>
@@ -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;