summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/file/magic-load.c41
-rw-r--r--usr.bin/file/magic-test.c36
-rw-r--r--usr.bin/file/magic.h3
3 files changed, 55 insertions, 25 deletions
diff --git a/usr.bin/file/magic-load.c b/usr.bin/file/magic-load.c
index e7b5a4823da..7a30563a033 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.10 2015/08/11 22:06:19 nicm Exp $ */
+/* $OpenBSD: magic-load.c,v 1.11 2015/08/11 22:12:48 nicm Exp $ */
/*
* Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
@@ -842,19 +842,34 @@ magic_parse_value(struct magic_line *ml, char **line)
*cp++ = *(*line)++;
*cp = '\0';
- if (*ml->type_string == 'u')
- endptr = magic_strtoull(copy, &ml->test_unsigned);
- else {
- endptr = magic_strtoll(copy, &ml->test_signed);
- if (endptr == NULL || *endptr != '\0') {
- /*
- * If we can't parse this as a signed number, try as
- * unsigned instead.
- */
- endptr = magic_strtoull(copy, &u);
- if (endptr != NULL && *endptr == '\0')
- ml->test_signed = (int64_t)u;
+ switch (ml->type) {
+ case MAGIC_TYPE_FLOAT:
+ case MAGIC_TYPE_DOUBLE:
+ case MAGIC_TYPE_BEFLOAT:
+ case MAGIC_TYPE_BEDOUBLE:
+ case MAGIC_TYPE_LEFLOAT:
+ case MAGIC_TYPE_LEDOUBLE:
+ errno = 0;
+ ml->test_double = strtod(copy, &endptr);
+ if (errno == ERANGE)
+ endptr = NULL;
+ break;
+ default:
+ if (*ml->type_string == 'u')
+ endptr = magic_strtoull(copy, &ml->test_unsigned);
+ else {
+ endptr = magic_strtoll(copy, &ml->test_signed);
+ if (endptr == NULL || *endptr != '\0') {
+ /*
+ * If we can't parse this as a signed number,
+ * try as unsigned instead.
+ */
+ endptr = magic_strtoull(copy, &u);
+ if (endptr != NULL && *endptr == '\0')
+ ml->test_signed = (int64_t)u;
+ }
}
+ break;
}
if (endptr == NULL || *endptr != '\0') {
magic_warn(ml, "can't parse number: %s", copy);
diff --git a/usr.bin/file/magic-test.c b/usr.bin/file/magic-test.c
index dbc9fd277ca..bd9b6031269 100644
--- a/usr.bin/file/magic-test.c
+++ b/usr.bin/file/magic-test.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: magic-test.c,v 1.6 2015/05/29 15:58:01 nicm Exp $ */
+/* $OpenBSD: magic-test.c,v 1.7 2015/08/11 22:12:48 nicm Exp $ */
/*
* Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
@@ -206,6 +206,18 @@ magic_test_unsigned(struct magic_line *ml, uint64_t value, uint64_t wanted)
}
static int
+magic_test_double(struct magic_line *ml, double value, double wanted)
+{
+ switch (ml->test_operator) {
+ case 'x':
+ return (1);
+ case '=':
+ return (value == wanted);
+ }
+ return (-1);
+}
+
+static int
magic_test_type_none(__unused struct magic_line *ml,
__unused struct magic_state *ms)
{
@@ -417,6 +429,7 @@ magic_test_type_float(struct magic_line *ml, struct magic_state *ms)
{
uint32_t value0;
double value;
+ int result;
if (magic_copy_from(ms, -1, &value0, sizeof value0) != 0)
return (0);
@@ -429,11 +442,11 @@ magic_test_type_float(struct magic_line *ml, struct magic_state *ms)
if (ml->type_operator != ' ')
return (-1);
- if (ml->test_operator != 'x')
- return (-1);
-
- magic_add_result(ms, ml, "%g", value);
- ms->offset += sizeof value0;
+ result = magic_test_double(ml, value, (float)ml->test_double);
+ if (result == !ml->test_not && ml->result != NULL) {
+ magic_add_result(ms, ml, "%g", value);
+ ms->offset += sizeof value0;
+ }
return (1);
}
@@ -442,6 +455,7 @@ magic_test_type_double(struct magic_line *ml, struct magic_state *ms)
{
uint64_t value0;
double value;
+ int result;
if (magic_copy_from(ms, -1, &value0, sizeof value0) != 0)
return (0);
@@ -454,11 +468,11 @@ magic_test_type_double(struct magic_line *ml, struct magic_state *ms)
if (ml->type_operator != ' ')
return (-1);
- if (ml->test_operator != 'x')
- return (-1);
-
- magic_add_result(ms, ml, "%g", value);
- ms->offset += sizeof value0;
+ result = magic_test_double(ml, value, (double)ml->test_double);
+ if (result == !ml->test_not && ml->result != NULL) {
+ magic_add_result(ms, ml, "%g", value);
+ ms->offset += sizeof value0;
+ }
return (1);
}
diff --git a/usr.bin/file/magic.h b/usr.bin/file/magic.h
index 4fde68874ce..dfbfed0c36e 100644
--- a/usr.bin/file/magic.h
+++ b/usr.bin/file/magic.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: magic.h,v 1.7 2015/08/11 21:42:16 nicm Exp $ */
+/* $OpenBSD: magic.h,v 1.8 2015/08/11 22:12:48 nicm Exp $ */
/*
* Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
@@ -129,6 +129,7 @@ struct magic_line {
size_t test_string_size;
uint64_t test_unsigned;
int64_t test_signed;
+ double test_double;
int stringify;
const char *result;