diff --git a/pfp/native/compat_string.py b/pfp/native/compat_string.py index 4eb658f..ebbf9e6 100644 --- a/pfp/native/compat_string.py +++ b/pfp/native/compat_string.py @@ -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 ) diff --git a/tests/test_compat.py b/tests/test_compat.py index b02221f..d6b6245 100644 --- a/tests/test_compat.py +++ b/tests/test_compat.py @@ -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):