Skip to content

Commit 2edd10c

Browse files
add math.tonumber function to convert bool to number (VirusTotal#1450)
more data types can be supported if needed, for example string to number, etc.
1 parent 4dcf48f commit 2edd10c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

docs/modules/math.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,11 @@ file and create signatures based on those results.
110110
.. versionadded:: 3.8.0
111111

112112
Returns the minimum of two unsigned integer values.
113+
114+
.. c:function:: tonumber(bool)
115+
116+
.. versionadded:: 4.0.5
117+
118+
Returns 0 or 1, it's useful when writing a score based rule.
119+
120+
*Example: math.tonumber(SubRule1) \* 60 + math.tonumber(SubRule2) \* 20 + math.tonumber(SubRule3) \* 70 > 80*

libyara/modules/math/math.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ define_function(max)
605605
return_integer(i > j ? i : j);
606606
}
607607

608+
define_function(tonumber)
609+
{
610+
return_integer(integer_argument(1) ? 1 : 0);
611+
}
608612

609613
begin_declarations
610614
declare_float("MEAN_BYTES");
@@ -621,6 +625,7 @@ begin_declarations
621625
declare_function("entropy", "s", "f", string_entropy);
622626
declare_function("min", "ii", "i", min);
623627
declare_function("max", "ii", "i", max);
628+
declare_function("tonumber", "b", "i", tonumber);
624629
end_declarations
625630

626631

tests/test-math.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ int main(int argc, char** argv)
2828
}",
2929
"A");
3030

31+
assert_true_rule_blob(
32+
"import \"math\" \
33+
rule test { \
34+
condition: \
35+
math.tonumber(1 == 1) \
36+
}",
37+
"A");
38+
39+
assert_false_rule_blob(
40+
"import \"math\" \
41+
rule test { \
42+
condition: \
43+
math.tonumber(1 > 2) \
44+
}",
45+
"A");
46+
3147
yr_finalize();
3248

3349
YR_DEBUG_FPRINTF(

0 commit comments

Comments
 (0)