scrap-is-not-scrap/sins.py

39 lines
1022 B
Python
Executable File

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