99_cffi_2.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import c
  2. assert c.void_p.from_hex('0x2568b60').hex() == '0x2568b60'
  3. # ------------------------------------------------
  4. class HexAddress:
  5. def __init__(self, address):
  6. if not address.startswith("0x"): # 确保地址以0x开头
  7. raise ValueError("Address should start with '0x'.")
  8. self.address = address[2:] # 去除0x前缀,并保存十六进制字符串
  9. def __str__(self):
  10. return "0x" + self.address
  11. def __add__(self, other):
  12. if isinstance(other, int):
  13. return HexAddress(hex(int(self.address, 16) + other)) # 将字符串地址转为整数进行运算
  14. elif isinstance(other, HexAddress):
  15. return HexAddress(hex(int(self.address, 16) + int(other.address, 16))) # 将字符串地址转为整数进行运算
  16. else:
  17. raise TypeError("Unsupported operand type for +: HexAddress and {}".format(type(other)))
  18. def __sub__(self, other):
  19. if isinstance(other, int):
  20. return HexAddress(hex(int(self.address, 16) - other)) # 将字符串地址转为整数进行运算
  21. elif isinstance(other, HexAddress):
  22. return HexAddress(hex(int(self.address, 16) - int(other.address, 16))) # 将字符串地址转为整数进行运算
  23. else:
  24. raise TypeError("Unsupported operand type for -: HexAddress and {}".format(type(other)))
  25. c_void_1 = c.malloc(8)
  26. assert (c_void_1 + 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) + 8)).hex()
  27. assert (c_void_1 - 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) - 8)).hex()
  28. # ------------------------------------------------
  29. # 此处测试并不完全
  30. c_void_1 = c.malloc(8)
  31. c_void_1.read_bytes(5)
  32. c_void_1.write_bytes(c_void_1.read_bytes(5))
  33. # ------------------------------------------------
  34. c_void_1 = c.malloc(32)
  35. my_struct2 = c_void_1.read_struct(32)
  36. assert my_struct2.size() == 32
  37. data_bytes = bytes([1,2,3])
  38. my_struct4 = c.struct(data_bytes)
  39. try:
  40. c.struct(True)
  41. raise Exception('c.struct 的构造方法未能触发 TypeError("expected int or bytes")')
  42. except TypeError:
  43. pass
  44. try:
  45. c.struct(1,2,3)
  46. raise Exception('c.struct 的构造方法未能触发 TypeError("expected 1 or 2 arguments")')
  47. except TypeError:
  48. pass
  49. # ------------------------------------------------
  50. my_struct1 = c.struct(16)
  51. assert my_struct1.size() == 16
  52. # 对 c.struct 的 copy 方法的测试不完全
  53. assert my_struct1.copy().size() == 16
  54. data_bytes = bytes([1,2,3])
  55. my_struct4 = c.struct(data_bytes)
  56. assert my_struct4.addr().read_bytes(
  57. my_struct4.size()
  58. ) == data_bytes
  59. # ------------------------------------------------
  60. # 此处测试并不完全
  61. c_void_1 = c.malloc(16)
  62. my_struct1 = c.struct(16)
  63. c_void_1.write_struct(my_struct1)
  64. assert c_void_1.read_struct(16) == my_struct1
  65. from c import array, int_
  66. a = array(10, item_size=4)
  67. assert a.item_count == 10
  68. assert a.item_size == 4
  69. _ = hash(a)
  70. a[4] = int_(123)
  71. assert a[4] == int_(123)