Exemple #1
0
    def test_array_infinite_nested_block():
        random.seed(0)

        class leaf(pint.uint32_t): pass
        class rootcontainer(parray.block):
            _object_ = leaf

        class acontainer(rootcontainer):
            blocksize = lambda x: 8

        class bcontainer(rootcontainer):
            _object_ = pint.uint16_t
            blocksize = lambda x: 8

        class ccontainer(rootcontainer):
            _object_ = pint.uint8_t
            blocksize = lambda x: 8

        class arr(parray.infinite):
            def randomcontainer(self):
                l = [ acontainer, bcontainer, ccontainer ]
                return random.sample(l, 1)[0]

            _object_ = randomcontainer

        iterable = (random.randint(*params) for params in [tuple(boundary for boundary in bytearray(b'AZ'))] * 0x100)
        string = bytes(bytearray(iterable))
        a = arr(source=provider.bytes(string))
        a=a.l
        if a.blocksize() == 0x108:
            raise Success
Exemple #2
0
 def test_str_string():
     x = pstr.string()
     string = b'helllo world ok, i\'m hungry for some sushi\0'
     x.length = len(string) // 2
     x.source = provider.bytes(string)
     x.load()
     if x.str() == string[:len(string) // 2].decode(x[0].encoding.name):
         raise Success
Exemple #3
0
 def test_int_littleendian_int32_unsigned_lowedge_load():
     pint.setbyteorder(config.byteorder.littleendian)
     s = b'\x00\x00\x00\x00'
     a = pint.int32_t(source=provider.bytes(s)).l
     b, = struct.unpack('i',s)
     if a.int() == b and a.serialize() == s:
         raise Success
     print(b, a, tohex(a.serialize()))
Exemple #4
0
    def test_str_struct_szstring():
        class IMAGE_IMPORT_HINT(pstruct.type):
            _fields_ = [(pint.uint16_t, 'Hint'), (pstr.szstring, 'String')]

        x = IMAGE_IMPORT_HINT(source=provider.bytes(
            b'AAHello world this is a zero0-terminated string\0this didnt work'
        )).l
        if x['String'].str(
        ) == 'Hello world this is a zero0-terminated string':
            raise Success
Exemple #5
0
    def test_str_szstring_customterm():
        class fuq(pstr.szstring):
            def isTerminator(self, value):
                return value.int() == 0x3f

        s = provider.bytes(b'hello world\x3f..................')
        a = fuq(source=s)
        a = a.l
        if a.size() == 12:
            raise Success
Exemple #6
0
    def test_array_terminated_string():
        class szstring(parray.terminated):
            _object_ = pint.uint8_t
            def isTerminator(self, value):
                return value.int() == 0

        data = provider.bytes(b'hello world\x00not included\x00')
        a = szstring(source=data).l
        if len(a) == len(b'hello world\x00'):
            raise Success
Exemple #7
0
    def test_array_block_uint8():
        class container(parray.block):
            _object_ = pint.uint8_t
            blocksize = lambda s:4

        block = bytes(bytearray(range(0x10)))

        a = container(source=provider.bytes(block)).l
        if len(a) == 4:
            raise Success
Exemple #8
0
    def test_array_infinite_struct_partial():
        class RecordContainer(parray.infinite):
            _object_ = RecordGeneral

        data = provider.bytes(b'AAAAA')
        z = RecordContainer(source=data).l
        s = RecordGeneral().a.blocksize()

        if z.blocksize() == len(z)*s and len(z) == 3 and z.size() == 5 and not z[-1].initializedQ():
            raise Success
Exemple #9
0
    def test_array_infinite_struct():
        class RecordContainer(parray.infinite):
            _object_ = RecordGeneral

        chars = b'\xdd\xdd'
        string = chars * 8
        string = string[:-1]

        z = RecordContainer(source=provider.bytes(string)).l
        if len(z)-1 == int(len(string)/2.0) and len(string)%2 == 1:
            raise Success
Exemple #10
0
    def test_array_terminated_uint8():
        class myarray(parray.terminated):
            _object_ = pint.uint8_t
            def isTerminator(self, v):
                if v.serialize() == b'H':
                    return True
                return False

        block = b'GFEDCBABCDHEFG'
        x = myarray(source=provider.bytes(block)).l
        if len(x) == 11:
            raise Success
Exemple #11
0
    def test_structure_fetch():
        class st(pstruct.type):
            _fields_ = [
                (uint8, 'a'),
                (uint16, 'b'),
                (uint8, 'c'),
            ]

        source = provider.bytes(b'ABCDEFG')
        x = st(source=source)
        x = x.l
        if x['b'].serialize() == b'BC':
            raise Success
Exemple #12
0
 def test_str_wstring():
     x = pstr.wstring()
     oldstring = b'ok, this is unicode'
     string = oldstring
     x.length = len(string) // 2
     string = bytes(
         bytearray(
             functools.reduce(operator.add,
                              ([c, 0] for c in bytearray(string)))))
     x.source = provider.bytes(string)
     x.load()
     if x.str() == oldstring[:len(oldstring) // 2].decode('ascii'):
         raise Success
Exemple #13
0
    def test_array_block_blocksize():
        class blocked(parray.block):
            _object_ = pint.uint32_t

            def blocksize(self):
                return 16

        data = b'\xAA\xAA\xAA\xAA'*4
        data+= b'\xBB'*4

        x = blocked(source=provider.bytes(data))
        x = x.l
        if len(x) == 4 and x.size() == 16:
            raise Success
Exemple #14
0
    def test_array_block_nested_terminated_string():
        class szstring(parray.terminated):
            _object_ = pint.uint16_t
            def isTerminator(self, value):
                return value.int() == 0

        class ninethousand(parray.block):
            _object_ = szstring
            blocksize = lambda x: 9000

        s = ((b'A'*498) + b'\x00\x00') + ((b'B'*498)+b'\x00\x00')
        a = ninethousand(source=provider.bytes(s*9000)).l
        if len(a) == 18 and a.size() == 9000:
            raise Success
Exemple #15
0
    def test_array_block_uint32():
        count = 8

        child_type = pint.uint32_t
        class container_type(parray.block):
            _object_ = child_type

        block_length = child_type.length * count
        block = b'\0'*block_length
        container_type.blocksize = lambda s: child_type.length * 4

        a = container_type(source=provider.bytes(block)).l
        if len(a) == 4:
            raise Success
Exemple #16
0
    def test_array_type_dword():
        class myarray(parray.type):
            length = 5
            _object_ = dword

        x = myarray()
#        print(x)
#        print(x.length,len(x), x.value)
        x.source = provider.bytes(b'AAAA'*15)
        x.l
#        print(x.length,len(x), x.value)
#        print("{!r}".format(x))
        if len(x) == 5 and x[4].serialize() == b'AAAA':
            raise Success
Exemple #17
0
    def test_str_array_szstring():
        data = b'here\0is\0my\0null-terminated\0strings\0eof\0stop here okay plz'

        class stringarray(parray.terminated):
            _object_ = pstr.szstring

            def isTerminator(self, value):
                if value.str() == 'eof':
                    return True
                return False

        x = stringarray(source=provider.bytes(data)).l
        if x[3].str() == 'null-terminated':
            raise Success
Exemple #18
0
    def test_structure_assign_same():
        class st(pstruct.type):
            _fields_ = [
                (uint8, 'a'),
                (uint32, 'b'),
                (uint8, 'c'),
            ]

        source = provider.bytes(b'ABCDEFG')
        v = uint32().set(b'XXXX')
        x = st(source=source)
        x = x.l
        x['b'] = v
        if x.serialize() == b'AXXXXF':
            raise Success
Exemple #19
0
    def test_array_infinite_type_partial():
        b = string.ascii_letters+string.digits

        count = 0x10

        child_type = pint.uint32_t
        class container_type(parray.infinite):
            _object_ = child_type

        block_length = child_type.length * count
        block = b'\0'*block_length

        n = container_type(source=provider.bytes(block)).l
        if len(n)-1 == count and not n[-1].initializedQ():
            raise Success
Exemple #20
0
    def test_array_nested_terminated_string():
        class szstring(parray.terminated):
            _object_ = pint.uint8_t
            def isTerminator(self, value):
                return value.int() == 0

        class argh(parray.terminated):
            _object_ = szstring
            def isTerminator(self, value):
                return value.serialize() == b'end\x00'

        data = provider.bytes(b'hello world\x00is included\x00end\x00not\x00')
        a = argh(source=data).l
        if len(a) == 3:
            raise Success
Exemple #21
0
    def test_structure_assign_diff():
        class st(pstruct.type):
            _fields_ = [
                (uint8, 'a'),
                (uint32, 'b'),
                (uint8, 'c'),
            ]

        source = provider.bytes(b'ABCDEFG')
        v = uint16().set(b'XX')
        x = st(source=source)
        x = x.l
        x['b'] = v
        x.setoffset(x.getoffset(), recurse=True)
        if x.serialize() == b'AXXF' and x['c'].getoffset() == 3:
            raise Success
Exemple #22
0
    def test_structure_assign_partial():
        class st(pstruct.type):
            _fields_ = [
                (uint32, 'a'),
                (uint32, 'b'),
                (uint32, 'c'),
            ]
        source = provider.bytes(b'AAAABBBBCCC')
        x = st(source=source)

        try:
            x = x.l
            raise Failure

        except ptypes.error.LoadError:
            pass

        if x.v is not None and not x.initializedQ() and x['b'].serialize() == b'BBBB' and x['c'].size() == 3:
            raise Success
Exemple #23
0
    def test_array_infinite_nested_array():
        class subarray(parray.type):
            length = 4
            _object_ = pint.uint8_t
            def int(self):
                return functools.reduce(lambda agg, item: 256 * agg + item.int(), self.value, 0)
            def repr(self, **options):
                if self.initializedQ():
                    return self.classname() + " {:x}".format(self.int())
                return self.classname() + ' ???'

        class extreme(parray.infinite):
            _object_ = subarray
            def isTerminator(self, v):
                return v.int() == 0x42424242

        a = extreme(source=provider.bytes(b'A'*0x100 + b'B'*0x100 + b'C'*0x100 + b'DDDD'))
        a=a.l
        if len(a) == (0x100 / subarray.length)+1:
            raise Success
Exemple #24
0
    def test_array_block_nested_terminated_block():
        class fiver(parray.block):
            _object_ = pint.uint8_t
            blocksize = lambda s: 5

        class feiverfrei(parray.terminated):
            _object_ = fiver
            def isTerminator(self, value):
                return value.serialize() == b'\x00\x00\x00\x00\x00'

        class dundundun(parray.block):
            _object_ = feiverfrei
            blocksize = lambda x: 50

        dat = b'A'*5
        end = b'\x00'*5
        s = (dat*4)+end + (dat*4)+end
        a = dundundun(source=provider.bytes(s*5)).l
        if len(a) == 2 and len(a[0]) == 5 and len(a[1]) == 5:
            raise Success
Exemple #25
0
    def test_str_szwstring_customchar():
        data = ' '.join(
            map(
                operator.methodcaller('strip'), '''
            00 57 00 65 00 6c 00 63 00 6f 00 6d 00 65 00 00
        '''.split('\n'))).strip()
        data = bytes(bytearray(int(item, 16) for item in data.split(' ')))

        def __ensure_binary__(s, encoding='utf-8', errors='strict'):
            '''ripped from six v1.12.0'''
            if isinstance(s, six.text_type):
                return s.encode(encoding, errors)
            elif isinstance(s, six.binary_type):
                return s
            raise TypeError("not expecting type '%s'" % type(s))

        class wbechar_t(pstr.wchar_t):
            def set(self, value):
                self.value = b'\0' + __ensure_binary__(value)
                return self

            def get(self):
                return __ensure_text__(self.value, 'utf-16-be').encode('utf-8')

        class unicodestring(pstr.szwstring):
            _object_ = wbechar_t

            def str(self):
                s = self.value.decode('utf-16-be')
                return utils.strdup(s)[:len(self)]

        class unicodespeech_packet(pstruct.type):
            _fields_ = [
                (unicodestring, 'msg'),
            ]

        a = unicodestring(source=provider.bytes(data)).l
        if a.l.str() == 'Welcome':
            raise Success
        raise Failure
Exemple #26
0
 def test_int_revert_littleendian_uint32_load():
     pint.setbyteorder(config.byteorder.littleendian)
     a = pint.uint32_t(source=provider.bytes(string2)).l
     if a.int() == 0x0abcdef0 and a.serialize() == string2:
         raise Success
     print(a, tohex(a.serialize()))
Exemple #27
0
 def test_int_littleendian_set():
     b = pint.littleendian(pint.uint32_t)(source=provider.bytes(string2)).l
     b.set(0x0abcdef0)
     if b.int() == 0x0abcdef0 and b.serialize() == string2:
         raise Success
     print(b, tohex(b.serialize()))
Exemple #28
0
 def test_int_bigendian_uint32_set():
     a = pint.bigendian(pint.uint32_t)(source=provider.bytes(string1)).l
     a.set(0x0abcdef0)
     if a.int() == 0x0abcdef0 and a.serialize() == string1:
         raise Success
     print(a, tohex(a.serialize()))
Exemple #29
0
 def test_str_szwstring():
     s = b'C\x00:\x00\\\x00P\x00y\x00t\x00h\x00o\x00n\x002\x006\x00\\\x00D\x00L\x00L\x00s\x00\\\x00_\x00c\x00t\x00y\x00p\x00e\x00s\x00.\x00p\x00y\x00d\x00\x00\x00'
     v = pstr.szwstring(source=provider.bytes(s)).l
     if v.str() == 'C:\Python26\DLLs\_ctypes.pyd':
         raise Success
Exemple #30
0
 def test_str_szstring():
     string = b'null-terminated\0ok'
     x = pstr.szstring(source=provider.bytes(string)).l
     if x.str() == 'null-terminated':
         raise Success