406 lines
9.4 KiB
Python
406 lines
9.4 KiB
Python
from stdlib import malloc, free
|
||
import ctypes
|
||
from stdint import *
|
||
import string
|
||
import w32.win32console
|
||
from stdarg import va_start, va_end, va_arg, va_list
|
||
import t
|
||
import c
|
||
|
||
|
||
class Struct1:
|
||
a: UINT
|
||
b: UINT = 123
|
||
|
||
S1: list[Struct1, 5] = [
|
||
Struct1(1, 2),
|
||
Struct1(b=3, a=4),
|
||
Struct1(a=5, b=6),
|
||
Struct1(7, b=8),
|
||
Struct1(9)
|
||
]
|
||
S1_: Struct1 = Struct1(10)
|
||
S1_2 = Struct1(112)
|
||
|
||
|
||
def reptr(p: UINT | t.CPtr, v: UINT):
|
||
c.DerefAs(p, v)
|
||
|
||
GLOBAL_VAR: t.CInt = 0
|
||
|
||
@t.Object
|
||
class OOPTest2:
|
||
fb: t.CVoid | t.CPtr
|
||
def __init__(self):
|
||
self.fb = malloc(1024)
|
||
for i in range(0, 1024):
|
||
self.fb[i] = '1'
|
||
c.Set(t.CChar(self.fb[1023], t.CPtr), '\0')
|
||
def GetFB(self) -> t.CChar | t.CPtr:
|
||
return t.CChar(self.fb, t.CPtr)
|
||
def __del__(self):
|
||
print("OOPTest2 __del__")
|
||
free(self.fb)
|
||
|
||
@t.Object
|
||
class OOPTest:
|
||
a: UINT
|
||
b: UINT = 12345
|
||
c: UINT
|
||
oop2: OOPTest2 | t.CPtr
|
||
def __init__(self):
|
||
self.a = 451
|
||
self.oop2 = c.Addr(OOPTest2())
|
||
# c.Set(c.Deref(self.oop2.GetFB()[0]), 1)
|
||
self.oop2.GetFB()[0] = '1'
|
||
cp = self.oop2.fb # t.CChar(self.oop2.fb, t.CPtr)
|
||
cp[1] = '4'
|
||
cp[2] = '5'
|
||
cp[3] = '6'
|
||
cp[4] = '7'
|
||
cp[5] = '8'
|
||
cp[6] = '9'
|
||
cp[7] = '0'
|
||
cp[8] = '9'
|
||
|
||
print(t.CChar(cp[0], t.CPtr))
|
||
print("长度:", t.CChar(cp, t.CPtr).__sizeof__())
|
||
print("cp[0]:", cp[0])
|
||
cp += 1
|
||
print("cp[0]:", cp[0])
|
||
cp += 1
|
||
|
||
print("self.oop2.GetFB()[0]:", int(cp[0]))
|
||
#k = t.CChar(self.oop2.fb[1], t.CPtr)
|
||
#k[0] = "4"
|
||
|
||
cp -= 2
|
||
# print(t.CChar(cp, t.CPtr))
|
||
|
||
lambda_a = lambda: 1
|
||
print(cp[0], lambda_a())
|
||
|
||
print(type(cp))
|
||
print(dir(OOPTest2))
|
||
|
||
vkt: t.CVoid | t.CPtr = malloc(1024)
|
||
c.Set(c.Deref(vkt[0]), 8080)
|
||
vkt[0] = '2'
|
||
# vkt[0] = 20
|
||
print(t.CChar(vkt[0], t.CPtr)[0])
|
||
print(type(vkt[0]))
|
||
print(t.CChar(t.CInt("Hello"), t.CPtr))
|
||
print(str((c.Addr("你好"))))
|
||
# print(vkt)
|
||
free(vkt)
|
||
# print("vkt:", vkt)
|
||
|
||
vkt: t.CVoid | t.CPtr = malloc(1024)
|
||
vkt2: t.CChar | t.CPtr = t.CChar(vkt, t.CPtr)
|
||
vkt2[0] = '1'
|
||
vkt2[100] = '1'
|
||
print("首个参数:", vkt2[0])
|
||
print("第100参数:", vkt2[100])
|
||
#print("HEX", str_to_hex(vkt))
|
||
free(vkt)
|
||
print("HEX", str_to_hex("ABCDEFHIJKLMN"))
|
||
print("HEX", str_to_hex("1"))
|
||
|
||
del self.oop2
|
||
|
||
|
||
def str_to_hex(s: str) -> str:
|
||
buf: str = malloc(1024)
|
||
out: str = buf
|
||
hex: list[str, None] = "0123456789ABCDEF"
|
||
while c.Deref(s) != 0:
|
||
cr: t.CUnsignedChar = c.Deref(s)
|
||
c.Set(c.Deref(out), hex[(cr >> 4) & 0xF]) # 高 4 位
|
||
out += 1
|
||
c.Set(c.Deref(out), hex[cr & 0xF]) # 低 4 位
|
||
out += 1
|
||
s += 1 # 移动指针!
|
||
c.Set(c.Deref(out), 0) # 结尾
|
||
return buf
|
||
|
||
# 十进制 int → 16进制字符串(小写 0x 开头)
|
||
# 内部 malloc(128),外部必须 free()
|
||
def int_to_hex(num: int) -> str:
|
||
buf = t.CChar(malloc(128), t.CPtr)
|
||
if not buf: return None
|
||
|
||
hex_chars: list[t.CChar, None] = "0123456789ABCDEF"
|
||
i = 0
|
||
|
||
# 先加 0x 前缀
|
||
buf[i] = '0'
|
||
buf[i + 1] = 'x'
|
||
i += 2
|
||
|
||
# 处理 0
|
||
if num == 0:
|
||
buf[i] = '0'
|
||
i += 1
|
||
buf[i] = '0'
|
||
i += 1
|
||
buf[i] = '\0'
|
||
return buf
|
||
|
||
# 存储逆序的十六进制字符
|
||
temp: list[t.CChar, 32]
|
||
temp_idx = 0
|
||
|
||
# 转为无符号,避免负数问题
|
||
n: t.CUnsignedInt = t.CUnsignedInt(num)
|
||
|
||
while n > 0:
|
||
rem: t.CInt = n & 0xF; # 取低4位(等价%16)
|
||
temp[temp_idx] = hex_chars[rem]
|
||
temp_idx += 1
|
||
n = n >> 4; # 右移4位(等价/16)
|
||
|
||
# 反转写入最终缓冲区
|
||
while temp_idx > 0:
|
||
temp_idx -= 1
|
||
buf[i] = temp[temp_idx]
|
||
i += 1
|
||
|
||
buf[i] = '\0'
|
||
return buf
|
||
|
||
|
||
|
||
def int_to_str(num: int) -> str:
|
||
# 直接分配 128 字节(足够存任何 64 位整数)
|
||
buf: t.CChar | t.CPtr = t.CChar(malloc(128), t.CPtr)
|
||
if not buf: return None
|
||
is_negative = 0
|
||
i = 0
|
||
# 处理 0
|
||
if num == 0:
|
||
buf[i] = '0'
|
||
i += 1
|
||
buf[i] = '\0'
|
||
return buf
|
||
# 处理负数
|
||
if num < 0:
|
||
is_negative = 1
|
||
num = -num
|
||
# 生成逆序数字
|
||
while num > 0:
|
||
rem = num % 10
|
||
buf[i] = '0' + rem
|
||
i += 1
|
||
num = num / 10
|
||
# 负号
|
||
if is_negative:
|
||
buf[i] = '-'
|
||
i += 1
|
||
# 结束符
|
||
buf[i] = '\0'
|
||
# 反转字符串
|
||
start = 0
|
||
end = i - 1
|
||
while start < end:
|
||
tr: t.CChar = buf[start]
|
||
buf[start] = buf[end]
|
||
buf[end] = tr
|
||
start += 1
|
||
end -= 1
|
||
return buf
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# 一个元素:存 数据指针 + 数据大小
|
||
class Item:
|
||
data: t.CVoid | t.CPtr # i8* 数据指针
|
||
size: t.CSizeT # 存字节大小(用于拷贝、释放)
|
||
|
||
# 可变长列表
|
||
class List:
|
||
items: Item | t.CPtr
|
||
count: t.CSizeT = 0
|
||
capacity: t.CSizeT = 4
|
||
# 初始化列表
|
||
def __init__(self):
|
||
# l: List | t.CPtr = malloc(List.__sizeof__())
|
||
self.count = 0
|
||
self.capacity = 4
|
||
self.items = malloc(self.capacity * Item.__sizeof__())
|
||
|
||
# 自动扩容
|
||
def __grow(self):
|
||
if self.count >= self.capacity:
|
||
self.capacity *= 2
|
||
self.items = realloc(self.items, self.capacity * Item.__sizeof__())
|
||
|
||
# 添加任意类型数据(深拷贝)
|
||
def append(self, data: t.CVoid | t.CPtr, size: t.CSizeT):
|
||
self.__grow()
|
||
item: Item | t.CPtr = self.items[self.count]
|
||
item.size = size
|
||
item.data = malloc(size + 1)
|
||
memcpy(item.data, data, size)
|
||
c.Set(t.CUnsignedChar(item.data + size, t.CPtr), '\0')
|
||
self.count += 1
|
||
|
||
# 获取元素:返回 i8*(int8_t*),强转
|
||
def get(self, index: t.CSizeT) -> t.CInt8T | t.CPtr:
|
||
if index >= self.count: return None
|
||
return self.items[index].data
|
||
|
||
def __getitem__(self, index: t.CSizeT) -> t.CInt8T | t.CPtr:
|
||
return self.get(index)
|
||
|
||
# 销毁列表(所有内存全部释放)
|
||
def free(self):
|
||
i: t.CSizeT = 0
|
||
for i in range(self.count):
|
||
if self.items[i].data != 0:
|
||
free(self.items[i].data)
|
||
free(self.items)
|
||
# free(self)
|
||
|
||
|
||
|
||
|
||
|
||
|
||
DEFINE_T: t.CDefine = 2423432423
|
||
|
||
|
||
class EnumTest(t.CEnum):
|
||
A: t.State
|
||
B: t.State
|
||
C: t.State
|
||
Len: t.State
|
||
|
||
|
||
def main() -> t.CInt:
|
||
w32.win32console.SetConsoleOutputCP(65001)
|
||
w32.win32console.SetConsoleCP(65001)
|
||
|
||
d = OOPTest()
|
||
|
||
l = List()
|
||
nihao = "你好!世界,这是一个可变列表"
|
||
l.append(c.Addr(114514), t.CInt.__sizeof__())
|
||
l.append(nihao, len(nihao))
|
||
print("列表取值0:", c.Deref(t.CInt(l[0], t.CPtr)))
|
||
print("列表取值1:", l.get(1))
|
||
l.free()
|
||
# free(l)
|
||
print("结束")
|
||
|
||
# 未实现
|
||
#a = t.CInt32T
|
||
#print(a(-1))
|
||
#print(type(a(-1)))
|
||
|
||
E: EnumTest = EnumTest.B
|
||
print(E)
|
||
print(EnumTest.Len)
|
||
|
||
print(EnumTest(1))
|
||
print(type(EnumTest.B))
|
||
|
||
# c.Asm("mov eax, 1", op=[t.ASM_DESCR.CLOBBER_EAX])
|
||
|
||
u = Union()
|
||
u.A.a = 114514
|
||
u.A.b = 222222
|
||
print("u.A.a", u.A.a)
|
||
print("u.A.b", u.A.b)
|
||
print("u.A.c", u.B.c) # -16558
|
||
|
||
wb = WBIT()
|
||
wb.a = 1
|
||
wb.b = 1
|
||
wb.c = 1
|
||
wb.d = 12
|
||
print(wb.a)
|
||
print(wb.b)
|
||
print(wb.c)
|
||
print(wb.d)
|
||
wb.a = 2
|
||
print(wb.a) # 10 -> 0 -> 0
|
||
print(wb.__sizeof__())
|
||
print(type(wb))
|
||
|
||
ble = BLE()
|
||
ble.a = 0x114514
|
||
ble.b = 0x114514
|
||
print(ble.a)
|
||
print(ble.b)
|
||
print("a:", int_to_str(ble.a))
|
||
print("a_hex:", str_to_hex(int_to_str(ble.a)))
|
||
print(int_to_hex(ble.a[0])) # 0x00114514 00
|
||
print(int_to_hex(ble.a[1])) # 0x00114514 11
|
||
print(int_to_hex(ble.a[2])) # 0x00114514 45
|
||
print(int_to_hex(ble.a[3])) # 0x00114514 14
|
||
|
||
print(int_to_hex(ble.b[0])) # 0x00114514 14
|
||
print(int_to_hex(ble.b[1])) # 0x00114514 45
|
||
print(int_to_hex(ble.b[2])) # 0x00114514 11
|
||
print(int_to_hex(ble.b[3])) # 0x00114514 00
|
||
|
||
li = 0x0000000100000001
|
||
|
||
#bool_ = 0
|
||
#if bool_:
|
||
# tcls = t.CUInt32T
|
||
#else:
|
||
# tcls = LIWORK
|
||
#lx = tcls(li)
|
||
#print(type(lx))
|
||
#print(lx.b)
|
||
#print("END")
|
||
#未能完全实现
|
||
# x[-1]取值
|
||
|
||
print(DEFINE_T)
|
||
if c.CIf(DEFINE_T == 2423432423):
|
||
print("DEFINE_T == 2423432423")
|
||
elif c.CIf(DEFINE_T == 123):
|
||
print("DEFINE_T == 123")
|
||
else:
|
||
print("DEFINE_T != 2423432423 and DEFINE_T != 123")
|
||
#c.CIf
|
||
#c.CIfdef
|
||
#c.CIfndef
|
||
#c.CElif
|
||
#c.CElse
|
||
|
||
class LIWORK:
|
||
a: t.CUInt32T
|
||
b: t.CUInt32T
|
||
|
||
# class Union
|
||
class Union(t.CUnion):
|
||
class A:
|
||
a: t.CInt32T
|
||
b: t.CInt32T
|
||
class B:
|
||
c: t.CInt16T
|
||
d: t.CInt64T
|
||
|
||
class WBIT:
|
||
a: t.CInt | t.Bit(1)
|
||
b: t.CInt | t.Bit(1)
|
||
c: t.CInt | t.Bit(1)
|
||
d: t.CInt | t.Bit(5)
|
||
|
||
class BLE:
|
||
a: t.CInt | t.BigEndian
|
||
b: t.CInt | t.LittleEndian
|
||
|
||
|
||
|