Skip to content

Commit

Permalink
Implement octal number literals
Browse files Browse the repository at this point in the history
  • Loading branch information
hillu authored and plusvic committed Jun 29, 2017
1 parent d0f4e0a commit 872a9be
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions libyara/include/yara/strutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ uint64_t xtoi(
const char* hexstr);


uint64_t otoi(
const char* octstr);


#if !HAVE_STRLCPY && !defined(strlcpy)
size_t strlcpy(
char *dst,
Expand Down
7 changes: 6 additions & 1 deletion libyara/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ with noyywrap then we can remove this pragma.
digit [0-9]
letter [a-zA-Z]
hexdigit [a-fA-F0-9]
octdigit [0-7]

%%

Expand Down Expand Up @@ -472,11 +473,15 @@ u?int(8|16|32)(be)? {
}

0x{hexdigit}+ {

yylval->integer = xtoi(yytext + 2);
return _NUMBER_;
}

0o{octdigit}+ {
yylval->integer = otoi(yytext + 2);
return _NUMBER_;
}


<str>\" { /* saw closing quote - all done */

Expand Down
30 changes: 30 additions & 0 deletions libyara/strutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ uint64_t xtoi(
return r;
}

uint64_t otoi(
const char* octstr)
{
size_t i;
size_t l = strlen(octstr);

uint64_t r = 0;

for (i = 0; i < l; i++)
{
switch (octstr[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
r |= ((uint64_t)(octstr[i] - '0')) << ((l - i - 1) * 3);
break;
default:
i = l; // force loop exit
}
}

return r;
}

/*
strlcpy and strlcat are defined in FreeBSD and OpenBSD,
Expand Down
9 changes: 9 additions & 0 deletions tests/test-rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ static void test_arithmetic_operators()
assert_true_rule(
"rule test { condition: -0x01 == -1}", NULL);

assert_true_rule(
"rule test { condition: 0o10 == 8 }", NULL);

assert_true_rule(
"rule test { condition: 0o100 == 64 }", NULL);

assert_true_rule(
"rule test { condition: 0o755 == 493 }", NULL);

}


Expand Down

0 comments on commit 872a9be

Please sign in to comment.