scrap-is-not-scrap/sins.py

40 lines
994 B
Python
Raw Normal View History

2016-04-20 01:45:44 +00:00
#! /usr/bin/env python
# encoding: utf-8
import struct
import mmap
import ctypes
class Sins:
def __init__(self):
self.seed = open('./build/scrap.asm.2.o', 'rb').read()
2016-04-20 02:10:33 +00:00
self.pic = self.pic_load(self.seed)
self.callback = ctypes.PYFUNCTYPE(None, ctypes.py_object)(self.callme)
2016-04-20 01:45:44 +00:00
2016-04-20 02:10:33 +00:00
def pic_load(self, seedling):
size = len(seedling)
2016-04-20 01:45:44 +00:00
prot = (mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)
2016-04-20 02:10:33 +00:00
page = mmap.mmap(-1, size, prot=prot)
page.write(seedling)
addr = ctypes.addressof((ctypes.c_char * size).from_buffer(page))
2016-04-20 01:45:44 +00:00
func = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint)(addr)
func.page = page
2016-04-20 02:10:33 +00:00
func.addr = addr
func.size = size
return func
2016-04-20 01:45:44 +00:00
2016-04-20 02:10:33 +00:00
def callme(self):
print('So Happy.')
2016-04-20 01:45:44 +00:00
if __name__ == '__main__':
import pprint
pp = pprint.PrettyPrinter()
sins = Sins()
2016-04-20 02:10:33 +00:00
2016-04-20 01:45:44 +00:00
print('pic_load')
2016-04-20 02:10:33 +00:00
pp.pprint(sins.pic(sins.callback, sins.pic.size))