Skip to content

Commit

Permalink
Merge pull request #109 from d0c-s4vage/hotfix/100-implement_memcmp
Browse files Browse the repository at this point in the history
Added Memcmp implementation and tests
  • Loading branch information
d0c-s4vage authored Jan 6, 2020
2 parents 150222e + 6acf322 commit 28ac356
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pfp/native/compat_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,25 @@ def IntToBinaryStr(params, ctxt, scope, stream, coord):
# int Memcmp( const uchar s1[], const uchar s2[], int n )
@native(name="Memcmp", ret=pfp.fields.Int)
def Memcmp(params, ctxt, scope, stream, coord):
raise NotImplementedError()
"""
int Memcmp( const uchar s1[], const uchar s2[], int n )
Compares the first n bytes of s1 and s2. Returns a value less than zero if
s1 is less than s2, zero if they are equal, or a value greater than zero if
s1 is greater than s2.
"""
if len(params) < 3:
raise errors.InvalidArguments(
coord, "{} args".format(len(params)), "3 arguments",
)
s1 = PYSTR(params[0])
s2 = PYSTR(params[1])
n = PYVAL(params[2])

s1_sub = s1[:n]
s2_sub = s2[:n]

return _cmp(s1_sub, s2_sub)


# void Memcpy( uchar dest[], const uchar src[], int n, int destOffset=0, int srcOffset=0 )
Expand Down
17 changes: 17 additions & 0 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,23 @@ def test_strstr2(self):
stdout="-1",
)

def test_memcmp(self):
dom = self._test_parse_build(
"",
r"""
local string a = "hellothere";
local string b = "helloblah";
Printf("%d\n", Memcmp(a, b, 1));
Printf("%d\n", Memcmp(a, b, 2));
Printf("%d\n", Memcmp(a, b, 3));
Printf("%d\n", Memcmp(a, b, 4));
Printf("%d\n", Memcmp(a, b, 5));
Printf("%d\n", Memcmp(a, b, 6));
Printf("%d\n", Memcmp(b, a, 6));
""",
stdout="0\n0\n0\n0\n0\n1\n-1\n",
)


class TestCompatTools(utils.PfpTestCase):
def setUp(self):
Expand Down

0 comments on commit 28ac356

Please sign in to comment.