Debian Codegraph @

ptrace/ctypes_tools.py

raw
1 from struct import pack, unpack
2 from ptrace.cpu_info import CPU_64BITS
3 from ctypes import cast, POINTER
4
5
6 def int2uint64(value):
7 """
8 Convert a signed 64 bits integer into an unsigned 64 bits integer.
9
10 >>> print(int2uint64(1))
11 1
12 >>> print(int2uint64(2**64 + 1)) # ignore bits larger than 64 bits
13 1
14 >>> print(int2uint64(-1))
15 18446744073709551615
16 >>> print(int2uint64(-2))
17 18446744073709551614
18 """
19 return (value & 0xffffffffffffffff)
20
21
22 def uint2int64(value):
23 """
24 Convert an unsigned 64 bits integer into a signed 64 bits integer.
25
26 >>> print(uint2int64(1))
27 1
28 >>> print(uint2int64(2**64 + 1)) # ignore bits larger than 64 bits
29 1
30 >>> print(uint2int64(18446744073709551615))
31 -1
32 >>> print(uint2int64(18446744073709551614))
33 -2
34 """
35 value = value & 0xffffffffffffffff
36 if value & 0x8000000000000000:
37 return value - 0x10000000000000000
38 else:
39 return value
40
41
42 def truncateWord32(value):
43 """
44 Truncate an unsigned integer to 32 bits.
45 """
46 return value & 0xFFFFFFFF
47
48
49 def truncateWord64(value):
50 """
51 Truncate an unsigned integer to 64 bits.
52 """
53 return value & 0xFFFFFFFFFFFFFFFF
54
55
56 def formatUintHex16(value):
57 """
58 Format an 16 bits unsigned integer.
59 """
60 return u"0x%04x" % value
61
62
63 def formatUintHex32(value):
64 """
65 Format an 32 bits unsigned integer.
66 """
67 return u"0x%08x" % value
68
69
70 def formatUintHex64(value):
71 """
72 Format an 64 bits unsigned integer.
73 """
74 return u"0x%016x" % value
75
76
77 def int2uint32(value):
78 """
79 Convert a signed 32 bits integer into an unsigned 32 bits integer.
80
81 >>> print(int2uint32(1))
82 1
83 >>> print(int2uint32(2**32 + 1)) # ignore bits larger than 32 bits
84 1
85 >>> print(int2uint32(-1))
86 4294967295
87 """
88 return value & 0xffffffff
89
90
91 def uint2int32(value):
92 """
93 Convert an unsigned 32 bits integer into a signed 32 bits integer.
94
95 >>> print(uint2int32(1))
96 1
97 >>> print(uint2int32(2**32 + 1)) # ignore bits larger than 32 bits
98 1
99 >>> print(uint2int32(4294967295))
100 -1
101 >>> print(uint2int32(4294967294))
102 -2
103 >>> print(uint2int32(18446744073709551615))
104 -1
105 """
106 value = value & 0xffffffff
107 if value & 0x80000000:
108 v = value - 0x100000000
109 else:
110 v = value
111 return v
112
113
114 uint2int = uint2int32
115 int2uint = int2uint32
116 if CPU_64BITS:
117 ulong2long = uint2int64
118 long2ulong = int2uint64
119 formatWordHex = formatUintHex64
120 truncateWord = truncateWord64
121 else:
122 ulong2long = uint2int32
123 long2ulong = int2uint32
124 formatWordHex = formatUintHex32
125 truncateWord = truncateWord32
126
127
128 def formatAddress(address):
129 """
130 Format an address to hexadecimal.
131 Return "NULL" for zero.
132 """
133 if address:
134 return formatWordHex(address)
135 else:
136 return u"NULL"
137
138
139 def formatAddressRange(start, end):
140 """
141 Format an address range, e.g. "0x80004000-0x8000ffff".
142 """
143 return u"%s-%s" % (formatWordHex(start), formatWordHex(end))
144
145
146 def ntoh_ushort(value):
147 """
148 Convert an unsigned short integer from network endian to host endian.
149 """
150 return unpack("<H", pack(">H", value))[0]
151
152
153 def ntoh_uint(value):
154 """
155 Convert an unsigned integer from network endian to host endian.
156 """
157 return unpack("<I", pack(">I", value))[0]
158
159
160 def word2bytes(word):
161 """
162 Convert an unsigned integer (a CPU word) to a bytes string.
163 """
164 return pack("L", word)
165
166
167 def bytes2word(bytes):
168 """
169 Convert a bytes string to an unsigned integer (a CPU word).
170 """
171 return unpack("L", bytes)[0]
172
173
174 def bytes2type(bytes, type):
175 """
176 Cast a bytes string to an object of the specified type.
177 """
178 return cast(bytes, POINTER(type))[0]
179
180
181 def bytes2array(bytes, basetype, size):
182 """
183 Cast a bytes string to an array of objects of the specified type
184 and size.
185 """
186 return bytes2type(bytes, basetype * size)