def test_array_infinite_nested_block(): import random from ptypes import parray,dynamic,ptype,pint,provider 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 string = ''.join([ chr(random.randint(ord('A'),ord('Z'))) for x in range(0x100) ]) a = arr(source=provider.string(string)) a=a.l if a.blocksize() == 0x108: raise Success
def test_str_szwstring_customchar(): data = ' '.join(map(lambda x:x.strip(),''' 00 57 00 65 00 6c 00 63 00 6f 00 6d 00 65 00 00 '''.split('\n'))).strip() data = map(lambda x: chr(int(x,16)), data.split(' ')) data = ''.join(data) import pstruct,pstr,provider,utils class wbechar_t(pstr.wchar_t): def set(self, value): self.value = '\x00' + value return self def get(self): return unicode(self.value, 'utf-16-be').encode('utf-8') class unicodestring(pstr.szwstring): _object_ = wbechar_t def str(self): s = __builtin__.unicode(self.value, 'utf-16-be').encode('utf-8') return utils.strdup(s)[:len(self)] class unicodespeech_packet(pstruct.type): _fields_ = [ (unicodestring, 'msg'), ] a = unicodestring(source=provider.string(data)).l if a.l.str() == 'Welcome': raise Success raise Failure
def test_str_string(): x = pstr.string() string = "helllo world ok, i'm hungry for some sushi\x00" x.length = len(string)/2 x.source = provider.string(string) x.load() if x.str() == string[:len(string)/2]: raise Success
def test_int_littleendian_int32_unsigned_lowedge_load(): pint.setbyteorder(config.byteorder.littleendian) s = '\x00\x00\x00\x00' a = pint.int32_t(source=provider.string(s)).l b, = struct.unpack('l',s) if a.int() == b and a.serialize() == s: raise Success print b,a, repr(a.serialize())
def test_array_infinite_struct_partial(): class RecordContainer(parray.infinite): _object_ = RecordGeneral data = provider.string('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].initialized: raise Success
def test_str_wstring(): x = pstr.wstring() oldstring = "ok, this is unicode" string = oldstring x.length = len(string)/2 string = ''.join([c+'\x00' for c in string]) x.source = provider.string(string) x.load() if x.str() == oldstring[:len(oldstring)/2]: raise Success
def test_str_szstring_customterm(): class fuq(pstr.szstring): def isTerminator(self, value): return value.num() == 0x3f s = provider.string('hello world\x3f..................') a = fuq(source=s) a = a.l if a.size() == 12: raise Success
def test_array_terminated_string(): class szstring(parray.terminated): _object_ = pint.uint8_t def isTerminator(self, value): return value.int() == 0 data = provider.string("hello world\x00not included\x00") a = szstring(source=data).l if len(a) == len('hello world\x00'): raise Success
def test_array_infinite_struct_partial(): class RecordContainer(parray.infinite): _object_ = RecordGeneral data = provider.string('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].initialized: raise Success
def test_array_infinite_struct(): class RecordContainer(parray.infinite): _object_ = RecordGeneral chars = '\xdd\xdd' string = chars * 8 string = string[:-1] z = RecordContainer(source=provider.string(string)).l if len(z)-1 == int(len(string)/2.0) and len(string)%2 == 1: raise Success
def test_array_block_uint8(): import pint class container(parray.block): _object_ = pint.uint8_t blocksize = lambda s:4 block = ''.join(map(chr,range(0x10))) a = container(source=provider.string(block)).l if len(a) == 4: raise Success
def test_array_infinite_struct(): class RecordContainer(parray.infinite): _object_ = RecordGeneral chars = '\xdd\xdd' string = chars * 8 string = string[:-1] z = RecordContainer(source=provider.string(string)).l if len(z) - 1 == int(len(string) / 2.0) and len(string) % 2 == 1: raise Success
def test_str_struct_szstring(): import pstruct,pint,pstr class IMAGE_IMPORT_HINT(pstruct.type): _fields_ = [ ( pint.uint16_t, 'Hint' ), ( pstr.szstring, 'String' ) ] x = IMAGE_IMPORT_HINT(source=provider.string('AAHello world this is a zero0-terminated string\x00this didnt work')).l if x['String'].str() == 'Hello world this is a zero0-terminated string': raise Success
def test_structure_assign_partial(): class st(pstruct.type): _fields_ = [ (uint32, 'a'), (uint32, 'b'), (uint32, 'c'), ] source = provider.string('AAAABBBBCCC') x = st(source=source) x = x.l if x.v is not None and not x.initialized and x['b'].serialize() == 'BBBB' and x['c'].size() == 3: raise Success
def test_array_block_uint8(): import pint class container(parray.block): _object_ = pint.uint8_t blocksize = lambda s: 4 block = ''.join(map(chr, range(0x10))) a = container(source=provider.string(block)).l if len(a) == 4: raise Success
def test_structure_fetch(): class st(pstruct.type): _fields_ = [ (uint8, 'a'), (uint16, 'b'), (uint8, 'c'), ] source = provider.string('ABCDEFG') x = st(source=source) x = x.l if x['b'].serialize() == 'BC': raise Success
def test_array_terminated_uint8(): import pint class myarray(parray.terminated): _object_ = pint.uint8_t def isTerminator(self, v): if v.serialize() == 'H': return True return False block = 'GFEDCBABCDHEFG' x = myarray(source=provider.string(block)).l if len(x) == 11: raise Success
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 = '\x00'*block_length container_type.blocksize = lambda s: child_type.length * 4 a = container_type(source=provider.string(block)).l if len(a) == 4: raise Success
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.string('AAAA' * 15) x.l # print x.length,len(x), x.value # print repr(x) if len(x) == 5 and x[4].serialize() == 'AAAA': raise Success
def test_array_block_blocksize(): class blocked(parray.block): _object_ = pint.uint32_t def blocksize(self): return 16 data = '\xAA\xAA\xAA\xAA'*4 data+= '\xBB'*4 x = blocked(source=provider.string(data)) x = x.l if len(x) == 4 and x.size() == 16: raise Success
def test_structure_serialize(): class st(pstruct.type): _fields_ = [ (uint8, 'a'), (uint8, 'b'), (uint8, 'c'), ] global x source = provider.string('ABCDEFG') x = st(source=source) x = x.l if x.serialize() == 'ABC': raise Success
def test_array_block_blocksize(): class blocked(parray.block): _object_ = pint.uint32_t def blocksize(self): return 16 data = '\xAA\xAA\xAA\xAA' * 4 data += '\xBB' * 4 x = blocked(source=provider.string(data)) x = x.l if len(x) == 4 and x.size() == 16: raise Success
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 = (('A'*498) + '\x00\x00') + (('B'*498)+'\x00\x00') a = ninethousand(source=provider.string(s*9000)).l if len(a) == 18 and a.size() == 9000: raise Success
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.string('AAAA'*15) x.l # print x.length,len(x), x.value # print repr(x) if len(x) == 5 and x[4].serialize() == 'AAAA': raise Success
def test_structure_assign_partial(): class st(pstruct.type): _fields_ = [ (uint32, 'a'), (uint32, 'b'), (uint32, 'c'), ] source = provider.string('AAAABBBBCCC') x = st(source=source) x = x.l if x.v is not None and not x.initialized and x['b'].serialize( ) == 'BBBB' and x['c'].size() == 3: raise Success
def test_structure_assign_same(): class st(pstruct.type): _fields_ = [ (uint8, 'a'), (uint32, 'b'), (uint8, 'c'), ] source = provider.string('ABCDEFG') v = uint32().set('XXXX') x = st(source=source) x = x.l x['b'] = v if x.serialize() == 'AXXXXF': raise Success
def test_str_array_szstring(): import parray data = 'here\x00is\x00my\x00null-terminated\x00strings\x00eof\x00stop 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.string(data)).l if x[3].str() == 'null-terminated': raise Success
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 = '\x00' * block_length container_type.blocksize = lambda s: child_type.length * 4 a = container_type(source=provider.string(block)).l if len(a) == 4: raise Success
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 = (('A' * 498) + '\x00\x00') + (('B' * 498) + '\x00\x00') a = ninethousand(source=provider.string(s * 9000)).l if len(a) == 18 and a.size() == 9000: raise Success
def test_array_infinite_type_partial(): b = ''.join(map(chr,range(ord('a'), ord('z')) + range(ord('A'), ord('Z')) + range(ord('0'), ord('9')))) count = 0x10 child_type = pint.uint32_t class container_type(parray.infinite): _object_ = child_type block_length = child_type.length * count block = '\x00'*block_length n = container_type(source=provider.string(block)).l if len(n)-1 == count and not n[-1].initialized: raise Success
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() == 'end\x00' data = provider.string("hello world\x00is included\x00end\x00not\x00") a = argh(source=data).l if len(a) == 3: raise Success
def test_structure_assign_diff(): class st(pstruct.type): _fields_ = [ (uint8, 'a'), (uint32, 'b'), (uint8, 'c'), ] source = provider.string('ABCDEFG') v = uint16().set('XX') x = st(source=source) x = x.l x['b'] = v x.setoffset(x.getoffset(), recurse=True) if x.serialize() == 'AXXF' and x['c'].getoffset() == 3: raise Success
def test_structure_assign_diff(): class st(pstruct.type): _fields_ = [ (uint8, 'a'), (uint32, 'b'), (uint8, 'c'), ] source = provider.string('ABCDEFG') v = uint16().set('XX') x = st(source=source) x = x.l x['b'] = v x.setoffset(x.getoffset(),recurse=True) if x.serialize() == 'AXXF' and x['c'].getoffset() == 3: raise Success
def test_array_infinite_type_partial(): b = ''.join( map( chr, range(ord('a'), ord('z')) + range(ord('A'), ord('Z')) + range(ord('0'), ord('9')))) count = 0x10 child_type = pint.uint32_t class container_type(parray.infinite): _object_ = child_type block_length = child_type.length * count block = '\x00' * block_length n = container_type(source=provider.string(block)).l if len(n) - 1 == count and not n[-1].initialized: raise Success
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() == '\x00\x00\x00\x00\x00' class dundundun(parray.block): _object_ = feiverfrei blocksize = lambda x: 50 dat = 'A'*5 end = '\x00'*5 s = (dat*4)+end + (dat*4)+end a = dundundun(source=provider.string(s*5)).l if len(a) == 2 and len(a[0]) == 5 and len(a[1]) == 5: raise Success
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() == '\x00\x00\x00\x00\x00' class dundundun(parray.block): _object_ = feiverfrei blocksize = lambda x: 50 dat = 'A' * 5 end = '\x00' * 5 s = (dat * 4) + end + (dat * 4) + end a = dundundun(source=provider.string(s * 5)).l if len(a) == 2 and len(a[0]) == 5 and len(a[1]) == 5: raise Success
def test_array_infinite_nested_array(): class subarray(parray.type): length = 4 _object_ = pint.uint8_t def int(self): return reduce(lambda x,y:x*256+int(y), self.v, 0) def repr(self, **options): if self.initialized: return self.classname() + ' %x'% self.int() return self.classname() + ' ???' class extreme(parray.infinite): _object_ = subarray def isTerminator(self, v): return v.int() == 0x42424242 a = extreme(source=provider.string('A'*0x100 + 'B'*0x100 + 'C'*0x100 + 'DDDD')) a=a.l if len(a) == (0x100 / subarray.length)+1: raise Success
def test_array_infinite_nested_array(): class subarray(parray.type): length = 4 _object_ = pint.uint8_t def int(self): return reduce(lambda x, y: x * 256 + int(y), self.v, 0) def repr(self, **options): if self.initialized: return self.classname() + ' %x' % self.int() return self.classname() + ' ???' class extreme(parray.infinite): _object_ = subarray def isTerminator(self, v): return v.int() == 0x42424242 a = extreme(source=provider.string('A' * 0x100 + 'B' * 0x100 + 'C' * 0x100 + 'DDDD')) a = a.l if len(a) == (0x100 / subarray.length) + 1: raise Success
def test_array_infinite_nested_block(): import random from ptypes import parray, dynamic, ptype, pint, provider 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 string = ''.join( [chr(random.randint(ord('A'), ord('Z'))) for x in range(0x100)]) a = arr(source=provider.string(string)) a = a.l if a.blocksize() == 0x108: raise Success
def test_int_revert_littleendian_uint32_load(): pint.setbyteorder(config.byteorder.littleendian) a = pint.uint32_t(source=provider.string(string2)).l if a.int() == 0x0abcdef0 and a.serialize() == string2: raise Success print a, repr(a.serialize())
def test_str_szstring(): string = 'null-terminated\x00ok' x = pstr.szstring(source=provider.string(string)).l if x.str() == 'null-terminated': raise Success
def test_str_szwstring(): s = '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.string(s)).l if v.str() == 'C:\Python26\DLLs\_ctypes.pyd': raise Success
def test_str_char(): x = pstr.char_t(source=provider.string('hello')).l if x.get() == 'h': raise Success
def test_int_bigendian_uint32_set(): a = pint.bigendian(pint.uint32_t)(source=provider.string(string1)).l a.set(0x0abcdef0) if a.int() == 0x0abcdef0 and a.serialize() == string1: raise Success print b, repr(b.serialize())
def test_str_wchar(): x = pstr.wchar_t(source=provider.string('\x43\x00')).l if x.get() == '\x43': raise Success
def test_int_littleendian_set(): b = pint.littleendian(pint.uint32_t)(source=provider.string(string2)).l b.set(0x0abcdef0) if b.int() == 0x0abcdef0 and b.serialize() == string2: raise Success print b, repr(b.serialize())