99_bufan.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # ------------------------------------------------
  2. '''source code in cffi.cpp :
  3. 57: 245: vm->bind_method<0>(type, "__repr__", [](VM* vm, ArgsView args){
  4. #####: 246: C99ReflType& self = _CAST(C99ReflType&, args[0]);
  5. #####: 247: return VAR("<ctype '" + Str(self.name) + "'>");
  6. #####: 248: });
  7. #####: 249:
  8. 57: 250: vm->bind_method<0>(type, "name", [](VM* vm, ArgsView args){
  9. #####: 251: C99ReflType& self = _CAST(C99ReflType&, args[0]);
  10. #####: 252: return VAR(self.name);
  11. #####: 253: });
  12. '''
  13. # test :
  14. import c
  15. c_int = c.refl("int")
  16. assert c_int.name() == "int"
  17. assert c_int.__repr__() == '<ctype \'int\'>'
  18. # ------------------------------------------------
  19. '''source code in cffi.cpp :
  20. 57: 180: vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  21. #####: 181: C99Struct& self = _CAST(C99Struct&, lhs);
  22. #####: 182: if(!is_non_tagged_type(rhs, C99Struct::_type(vm))) return vm->NotImplemented;
  23. #####: 183: C99Struct& other = _CAST(C99Struct&, rhs);
  24. #####: 184: bool ok = self.size == other.size && memcmp(self.p, other.p, self.size) == 0;
  25. #####: 185: return VAR(ok);
  26. #####: 186: });
  27. -: 187:
  28. '''
  29. # test :
  30. import c
  31. c_int_1 = c.refl("int")
  32. c_struct_1 = c_int_1()
  33. assert (c_int_1() == c_int_1())
  34. assert (c_struct_1 == c_struct_1) == True
  35. # ------------------------------------------------
  36. '''source code in cffi.cpp :
  37. 114: 8: vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){
  38. #####: 9: std::string s = CAST(Str&, args[0]).str();
  39. #####: 10: size_t size;
  40. #####: 11: intptr_t ptr = std::stoll(s, &size, 16);
  41. #####: 12: if(size != s.size()) vm->ValueError("invalid literal for void_p(): " + s);
  42. #####: 13: return VAR_T(VoidP, (void*)ptr);
  43. #####: 14: });
  44. 114: 15: vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
  45. #####: 16: VoidP& self = _CAST(VoidP&, args[0]);
  46. #####: 17: return VAR(self.hex());
  47. #####: 18: });
  48. -: 19:
  49. '''
  50. # test :
  51. import c
  52. assert c.void_p.from_hex('0x2568b60').hex() == '0x2568b60'
  53. # ------------------------------------------------
  54. '''source code in cffi.cpp :
  55. 114: 70: vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  56. #####: 71: VoidP& self = _CAST(VoidP&, lhs);
  57. #####: 72: i64 offset = CAST(i64, rhs);
  58. #####: 73: return VAR_T(VoidP, (char*)self.ptr + offset);
  59. -: 74: });
  60. -: 75:
  61. 114: 76: vm->bind__sub__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  62. #####: 77: VoidP& self = _CAST(VoidP&, lhs);
  63. #####: 78: i64 offset = CAST(i64, rhs);
  64. #####: 79: return VAR_T(VoidP, (char*)self.ptr - offset);
  65. -: 80: });
  66. -: 81:
  67. -: 19:
  68. '''
  69. # test :
  70. class HexAddress:
  71. def __init__(self, address):
  72. if not address.startswith("0x"): # 确保地址以0x开头
  73. raise ValueError("Address should start with '0x'.")
  74. self.address = address[2:] # 去除0x前缀,并保存十六进制字符串
  75. def __str__(self):
  76. return "0x" + self.address
  77. def __add__(self, other):
  78. if isinstance(other, int):
  79. return HexAddress(hex(int(self.address, 16) + other)) # 将字符串地址转为整数进行运算
  80. elif isinstance(other, HexAddress):
  81. return HexAddress(hex(int(self.address, 16) + int(other.address, 16))) # 将字符串地址转为整数进行运算
  82. else:
  83. raise TypeError("Unsupported operand type for +: HexAddress and {}".format(type(other)))
  84. def __sub__(self, other):
  85. if isinstance(other, int):
  86. return HexAddress(hex(int(self.address, 16) - other)) # 将字符串地址转为整数进行运算
  87. elif isinstance(other, HexAddress):
  88. return HexAddress(hex(int(self.address, 16) - int(other.address, 16))) # 将字符串地址转为整数进行运算
  89. else:
  90. raise TypeError("Unsupported operand type for -: HexAddress and {}".format(type(other)))
  91. import c
  92. c_void_1 = c.malloc(8)
  93. assert (c_void_1 + 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) + 8)).hex()
  94. assert (c_void_1 - 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) - 8)).hex()
  95. # ------------------------------------------------
  96. '''source code in cffi.cpp :
  97. -: 107:
  98. 114: 108: vm->bind_method<1>(type, "read_bytes", [](VM* vm, ArgsView args){
  99. #####: 109: VoidP& self = _CAST(VoidP&, args[0]);
  100. #####: 110: i64 size = CAST(i64, args[1]);
  101. #####: 111: std::vector<char> buffer(size);
  102. #####: 112: memcpy(buffer.data(), self.ptr, size);
  103. #####: 113: return VAR(Bytes(std::move(buffer)));
  104. #####: 114: });
  105. -: 115:
  106. 114: 116: vm->bind_method<1>(type, "write_bytes", [](VM* vm, ArgsView args){
  107. #####: 117: VoidP& self = _CAST(VoidP&, args[0]);
  108. #####: 118: Bytes& bytes = CAST(Bytes&, args[1]);
  109. #####: 119: memcpy(self.ptr, bytes.data(), bytes.size());
  110. #####: 120: return vm->None;
  111. -: 121: });
  112. -: 122:
  113. '''
  114. # test :
  115. import c
  116. # 此处测试并不完全
  117. c_void_1 = c.malloc(8)
  118. c_void_1.read_bytes(5)
  119. c_void_1.write_bytes(c_void_1.read_bytes(5))
  120. # ------------------------------------------------
  121. '''source code in cffi.cpp :
  122. 57: 126: void C99Struct::_register(VM* vm, PyObject* mod, PyObject* type){
  123. 114: 127: vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
  124. #####: 128: if(args.size() == 1+1){
  125. #####: 129: if(is_int(args[1])){
  126. #####: 130: int size = _CAST(int, args[1]);
  127. #####: 131: return VAR_T(C99Struct, size);
  128. #####: 132: }
  129. #####: 133: if(is_non_tagged_type(args[1], vm->tp_str)){
  130. #####: 134: const Str& s = _CAST(Str&, args[1]);
  131. #####: 135: return VAR_T(C99Struct, (void*)s.data, s.size);
  132. #####: 136: }
  133. #####: 137: if(is_non_tagged_type(args[1], vm->tp_bytes)){
  134. #####: 138: const Bytes& b = _CAST(Bytes&, args[1]);
  135. #####: 139: return VAR_T(C99Struct, (void*)b.data(), b.size());
  136. #####: 140: }
  137. #####: 141: vm->TypeError("expected int, str or bytes");
  138. #####: 142: return vm->None;
  139. -: 143: }
  140. #####: 144: if(args.size() == 1+2){
  141. #####: 145: void* p = CAST(void*, args[1]);
  142. #####: 146: int size = CAST(int, args[2]);
  143. #####: 147: return VAR_T(C99Struct, p, size);
  144. #####: 148: }
  145. #####: 149: vm->TypeError("expected 1 or 2 arguments");
  146. #####: 150: return vm->None;
  147. #####: 151: });
  148. '''
  149. # test :
  150. import c
  151. my_struct1 = c.struct(16)
  152. c_void_1 = c.malloc(8)
  153. my_struct2 = c.struct(c_void_1, 32)
  154. data_str = "Hello, World!"
  155. my_struct3 = c.struct(data_str)
  156. data_bytes = bytes([1,2,3])
  157. my_struct4 = c.struct(data_bytes)
  158. try:
  159. c.struct(True)
  160. raise Exception('c.struct 的构造方法未能触发 TypeError("expected int, str or bytes")')
  161. except TypeError:
  162. pass
  163. try:
  164. c.struct(1,2,3)
  165. raise Exception('c.struct 的构造方法未能触发 TypeError("expected 1 or 2 arguments")')
  166. except TypeError:
  167. pass
  168. # ------------------------------------------------
  169. '''source code in cffi.cpp :
  170. 114: 158: vm->bind_method<0>(type, "size", [](VM* vm, ArgsView args){
  171. #####: 159: C99Struct& self = _CAST(C99Struct&, args[0]);
  172. #####: 160: return VAR(self.size);
  173. -: 161: });
  174. -: 162:
  175. 114: 163: vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){
  176. #####: 164: const C99Struct& self = _CAST(C99Struct&, args[0]);
  177. #####: 165: return VAR_T(C99Struct, self);
  178. #####: 166: });
  179. -: 167:
  180. 114: 168: vm->bind_method<0>(type, "to_string", [](VM* vm, ArgsView args){
  181. #####: 169: C99Struct& self = _CAST(C99Struct&, args[0]);
  182. #####: 170: return VAR(Str(self.p, self.size));
  183. #####: 171: });
  184. -: 172:
  185. 114: 173: vm->bind_method<0>(type, "to_bytes", [](VM* vm, ArgsView args){
  186. #####: 174: C99Struct& self = _CAST(C99Struct&, args[0]);
  187. #####: 175: std::vector<char> buffer(self.size);
  188. #####: 176: memcpy(buffer.data(), self.p, self.size);
  189. #####: 177: return VAR(Bytes(std::move(buffer)));
  190. #####: 178: });
  191. '''
  192. # test :
  193. import c
  194. my_struct1 = c.struct(16)
  195. assert my_struct1.size() == 16
  196. # 对 c.struct 的 copy 方法的测试不完全
  197. assert my_struct1.copy().size() == 16
  198. data_str = "Hello, World!"
  199. my_struct3 = c.struct(data_str)
  200. assert my_struct3.to_string() == data_str
  201. data_bytes = bytes([1,2,3])
  202. my_struct4 = c.struct(data_bytes)
  203. assert my_struct4.to_bytes() == data_bytes
  204. # ------------------------------------------------
  205. '''source code in cffi.cpp :
  206. 114: 222: vm->bind_method<1>(type, "read_struct", [](VM* vm, ArgsView args){
  207. #####: 223: VoidP& self = _CAST(VoidP&, args[0]);
  208. #####: 224: const Str& type = CAST(Str&, args[1]);
  209. #####: 225: int size = c99_sizeof(vm, type);
  210. #####: 226: return VAR_T(C99Struct, self.ptr, size);
  211. #####: 227: });
  212. -: 228:
  213. 114: 229: vm->bind_method<1>(type, "write_struct", [](VM* vm, ArgsView args){
  214. #####: 230: VoidP& self = _CAST(VoidP&, args[0]);
  215. #####: 231: C99Struct& other = CAST(C99Struct&, args[1]);
  216. #####: 232: memcpy(self.ptr, other.p, other.size);
  217. #####: 233: return vm->None;
  218. -: 234: });
  219. 57: 235: }
  220. '''
  221. # test :
  222. import c
  223. # 此处测试并不完全
  224. c_void_1 = c.malloc(8)
  225. my_struct1 = c.struct(16)
  226. c_void_1.read_struct('long')
  227. c_void_1.write_struct(my_struct1)