File: ctypes_tools.py

package info (click to toggle)
python-ptrace 0.9.9-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 10,167; ansic: 263; makefile: 164
file content (186 lines) | stat: -rw-r--r-- 3,977 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from struct import pack, unpack
from ptrace.cpu_info import CPU_64BITS
from ctypes import cast, POINTER


def int2uint64(value):
    """
    Convert a signed 64 bits integer into an unsigned 64 bits integer.

    >>> print(int2uint64(1))
    1
    >>> print(int2uint64(2**64 + 1))  # ignore bits larger than 64 bits
    1
    >>> print(int2uint64(-1))
    18446744073709551615
    >>> print(int2uint64(-2))
    18446744073709551614
    """
    return (value & 0xffffffffffffffff)


def uint2int64(value):
    """
    Convert an unsigned 64 bits integer into a signed 64 bits integer.

    >>> print(uint2int64(1))
    1
    >>> print(uint2int64(2**64 + 1))  # ignore bits larger than 64 bits
    1
    >>> print(uint2int64(18446744073709551615))
    -1
    >>> print(uint2int64(18446744073709551614))
    -2
    """
    value = value & 0xffffffffffffffff
    if value & 0x8000000000000000:
        return value - 0x10000000000000000
    else:
        return value


def truncateWord32(value):
    """
    Truncate an unsigned integer to 32 bits.
    """
    return value & 0xFFFFFFFF


def truncateWord64(value):
    """
    Truncate an unsigned integer to 64 bits.
    """
    return value & 0xFFFFFFFFFFFFFFFF


def formatUintHex16(value):
    """
    Format an 16 bits unsigned integer.
    """
    return u"0x%04x" % value


def formatUintHex32(value):
    """
    Format an 32 bits unsigned integer.
    """
    return u"0x%08x" % value


def formatUintHex64(value):
    """
    Format an 64 bits unsigned integer.
    """
    return u"0x%016x" % value


def int2uint32(value):
    """
    Convert a signed 32 bits integer into an unsigned 32 bits integer.

    >>> print(int2uint32(1))
    1
    >>> print(int2uint32(2**32 + 1))  # ignore bits larger than 32 bits
    1
    >>> print(int2uint32(-1))
    4294967295
    """
    return value & 0xffffffff


def uint2int32(value):
    """
    Convert an unsigned 32 bits integer into a signed 32 bits integer.

    >>> print(uint2int32(1))
    1
    >>> print(uint2int32(2**32 + 1))  # ignore bits larger than 32 bits
    1
    >>> print(uint2int32(4294967295))
    -1
    >>> print(uint2int32(4294967294))
    -2
    >>> print(uint2int32(18446744073709551615))
    -1
    """
    value = value & 0xffffffff
    if value & 0x80000000:
        v = value - 0x100000000
    else:
        v = value
    return v


uint2int = uint2int32
int2uint = int2uint32
if CPU_64BITS:
    ulong2long = uint2int64
    long2ulong = int2uint64
    formatWordHex = formatUintHex64
    truncateWord = truncateWord64
else:
    ulong2long = uint2int32
    long2ulong = int2uint32
    formatWordHex = formatUintHex32
    truncateWord = truncateWord32


def formatAddress(address):
    """
    Format an address to hexadecimal.
    Return "NULL" for zero.
    """
    if address:
        return formatWordHex(address)
    else:
        return u"NULL"


def formatAddressRange(start, end):
    """
    Format an address range, e.g. "0x80004000-0x8000ffff".
    """
    return u"%s-%s" % (formatWordHex(start), formatWordHex(end))


def ntoh_ushort(value):
    """
    Convert an unsigned short integer from network endian to host endian.
    """
    return unpack("<H", pack(">H", value))[0]


def ntoh_uint(value):
    """
    Convert an unsigned integer from network endian to host endian.
    """
    return unpack("<I", pack(">I", value))[0]


def word2bytes(word):
    """
    Convert an unsigned integer (a CPU word) to a bytes string.
    """
    return pack("L", word)


def bytes2word(bytes):
    """
    Convert a bytes string to an unsigned integer (a CPU word).
    """
    return unpack("L", bytes)[0]


def bytes2type(bytes, type):
    """
    Cast a bytes string to an object of the specified type.
    """
    return cast(bytes, POINTER(type))[0]


def bytes2array(bytes, basetype, size):
    """
    Cast a bytes string to an array of objects of the specified type
    and size.
    """
    return bytes2type(bytes, basetype * size)