pytecode 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytecode/__init__.py +22 -0
- pytecode/analysis.py +2402 -0
- pytecode/attributes.py +868 -0
- pytecode/bytes_utils.py +208 -0
- pytecode/class_reader.py +810 -0
- pytecode/class_writer.py +630 -0
- pytecode/constant_pool.py +196 -0
- pytecode/constant_pool_builder.py +844 -0
- pytecode/constants.py +208 -0
- pytecode/debug_info.py +319 -0
- pytecode/descriptors.py +791 -0
- pytecode/hierarchy.py +561 -0
- pytecode/info.py +123 -0
- pytecode/instructions.py +495 -0
- pytecode/jar.py +271 -0
- pytecode/labels.py +1041 -0
- pytecode/model.py +929 -0
- pytecode/modified_utf8.py +145 -0
- pytecode/operands.py +683 -0
- pytecode/py.typed +0 -0
- pytecode/transforms.py +954 -0
- pytecode/verify.py +1386 -0
- pytecode-0.0.1.dist-info/METADATA +218 -0
- pytecode-0.0.1.dist-info/RECORD +27 -0
- pytecode-0.0.1.dist-info/WHEEL +5 -0
- pytecode-0.0.1.dist-info/licenses/LICENSE +21 -0
- pytecode-0.0.1.dist-info/top_level.txt +1 -0
pytecode/bytes_utils.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Buffer
|
|
4
|
+
from struct import Struct, pack, unpack_from
|
|
5
|
+
|
|
6
|
+
BE_U1 = Struct(">B")
|
|
7
|
+
BE_I1 = Struct(">b")
|
|
8
|
+
BE_U2 = Struct(">H")
|
|
9
|
+
BE_I2 = Struct(">h")
|
|
10
|
+
BE_U4 = Struct(">I")
|
|
11
|
+
BE_I4 = Struct(">i")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _read_u1(buffer: Buffer, offset: int = 0) -> int:
|
|
15
|
+
return BE_U1.unpack_from(buffer, offset)[0]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _read_i1(buffer: Buffer, offset: int = 0) -> int:
|
|
19
|
+
return BE_I1.unpack_from(buffer, offset)[0]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _read_u2(buffer: Buffer, offset: int = 0) -> int:
|
|
23
|
+
return BE_U2.unpack_from(buffer, offset)[0]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _read_i2(buffer: Buffer, offset: int = 0) -> int:
|
|
27
|
+
return BE_I2.unpack_from(buffer, offset)[0]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _read_u4(buffer: Buffer, offset: int = 0) -> int:
|
|
31
|
+
return BE_U4.unpack_from(buffer, offset)[0]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _read_i4(buffer: Buffer, offset: int = 0) -> int:
|
|
35
|
+
return BE_I4.unpack_from(buffer, offset)[0]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _read_bytes(buffer: Buffer, length: int, offset: int = 0) -> bytes:
|
|
39
|
+
return unpack_from(f">{length}s", buffer, offset)[0]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class BytesReader:
|
|
43
|
+
def __init__(self, bytes_or_bytearray: bytes | bytearray, offset: int = 0) -> None:
|
|
44
|
+
self.buffer: memoryview = memoryview(bytes_or_bytearray)
|
|
45
|
+
self.offset: int = offset
|
|
46
|
+
|
|
47
|
+
def rewind(self, distance: int | None = None) -> None:
|
|
48
|
+
if distance is None:
|
|
49
|
+
self.offset = 0
|
|
50
|
+
else:
|
|
51
|
+
self.offset = max(self.offset - distance, 0)
|
|
52
|
+
|
|
53
|
+
def read_u1(self) -> int:
|
|
54
|
+
res = _read_u1(self.buffer, self.offset)
|
|
55
|
+
self.offset += 1
|
|
56
|
+
return res
|
|
57
|
+
|
|
58
|
+
def read_i1(self) -> int:
|
|
59
|
+
res = _read_i1(self.buffer, self.offset)
|
|
60
|
+
self.offset += 1
|
|
61
|
+
return res
|
|
62
|
+
|
|
63
|
+
def read_u2(self) -> int:
|
|
64
|
+
res = _read_u2(self.buffer, self.offset)
|
|
65
|
+
self.offset += 2
|
|
66
|
+
return res
|
|
67
|
+
|
|
68
|
+
def read_i2(self) -> int:
|
|
69
|
+
res = _read_i2(self.buffer, self.offset)
|
|
70
|
+
self.offset += 2
|
|
71
|
+
return res
|
|
72
|
+
|
|
73
|
+
def read_u4(self) -> int:
|
|
74
|
+
res = _read_u4(self.buffer, self.offset)
|
|
75
|
+
self.offset += 4
|
|
76
|
+
return res
|
|
77
|
+
|
|
78
|
+
def read_i4(self) -> int:
|
|
79
|
+
res = _read_i4(self.buffer, self.offset)
|
|
80
|
+
self.offset += 4
|
|
81
|
+
return res
|
|
82
|
+
|
|
83
|
+
def read_bytes(self, size: int) -> bytes:
|
|
84
|
+
res = _read_bytes(self.buffer, size, self.offset)
|
|
85
|
+
self.offset += size
|
|
86
|
+
return res
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# ---------------------------------------------------------------------------
|
|
90
|
+
# Write side
|
|
91
|
+
# ---------------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _write_u1(value: int) -> bytes:
|
|
95
|
+
return BE_U1.pack(value)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _write_i1(value: int) -> bytes:
|
|
99
|
+
return BE_I1.pack(value)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _write_u2(value: int) -> bytes:
|
|
103
|
+
return BE_U2.pack(value)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _write_i2(value: int) -> bytes:
|
|
107
|
+
return BE_I2.pack(value)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _write_u4(value: int) -> bytes:
|
|
111
|
+
return BE_U4.pack(value)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _write_i4(value: int) -> bytes:
|
|
115
|
+
return BE_I4.pack(value)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _write_bytes(data: bytes | bytearray) -> bytes:
|
|
119
|
+
return pack(f">{len(data)}s", data)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class BytesWriter:
|
|
123
|
+
def __init__(self) -> None:
|
|
124
|
+
self._buf: bytearray = bytearray()
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def position(self) -> int:
|
|
128
|
+
return len(self._buf)
|
|
129
|
+
|
|
130
|
+
def __len__(self) -> int:
|
|
131
|
+
return len(self._buf)
|
|
132
|
+
|
|
133
|
+
def to_bytes(self) -> bytes:
|
|
134
|
+
return bytes(self._buf)
|
|
135
|
+
|
|
136
|
+
def write_u1(self, value: int) -> None:
|
|
137
|
+
self._buf += _write_u1(value)
|
|
138
|
+
|
|
139
|
+
def write_i1(self, value: int) -> None:
|
|
140
|
+
self._buf += _write_i1(value)
|
|
141
|
+
|
|
142
|
+
def write_u2(self, value: int) -> None:
|
|
143
|
+
self._buf += _write_u2(value)
|
|
144
|
+
|
|
145
|
+
def write_i2(self, value: int) -> None:
|
|
146
|
+
self._buf += _write_i2(value)
|
|
147
|
+
|
|
148
|
+
def write_u4(self, value: int) -> None:
|
|
149
|
+
self._buf += _write_u4(value)
|
|
150
|
+
|
|
151
|
+
def write_i4(self, value: int) -> None:
|
|
152
|
+
self._buf += _write_i4(value)
|
|
153
|
+
|
|
154
|
+
def write_bytes(self, data: bytes | bytearray) -> None:
|
|
155
|
+
self._buf += _write_bytes(data)
|
|
156
|
+
|
|
157
|
+
def align(self, alignment: int) -> None:
|
|
158
|
+
remainder = len(self._buf) % alignment
|
|
159
|
+
if remainder != 0:
|
|
160
|
+
self._buf += bytes(alignment - remainder)
|
|
161
|
+
|
|
162
|
+
def reserve_u1(self) -> int:
|
|
163
|
+
pos = len(self._buf)
|
|
164
|
+
self._buf += b"\x00"
|
|
165
|
+
return pos
|
|
166
|
+
|
|
167
|
+
def reserve_i1(self) -> int:
|
|
168
|
+
pos = len(self._buf)
|
|
169
|
+
self._buf += b"\x00"
|
|
170
|
+
return pos
|
|
171
|
+
|
|
172
|
+
def reserve_u2(self) -> int:
|
|
173
|
+
pos = len(self._buf)
|
|
174
|
+
self._buf += b"\x00\x00"
|
|
175
|
+
return pos
|
|
176
|
+
|
|
177
|
+
def reserve_i2(self) -> int:
|
|
178
|
+
pos = len(self._buf)
|
|
179
|
+
self._buf += b"\x00\x00"
|
|
180
|
+
return pos
|
|
181
|
+
|
|
182
|
+
def reserve_u4(self) -> int:
|
|
183
|
+
pos = len(self._buf)
|
|
184
|
+
self._buf += b"\x00\x00\x00\x00"
|
|
185
|
+
return pos
|
|
186
|
+
|
|
187
|
+
def reserve_i4(self) -> int:
|
|
188
|
+
pos = len(self._buf)
|
|
189
|
+
self._buf += b"\x00\x00\x00\x00"
|
|
190
|
+
return pos
|
|
191
|
+
|
|
192
|
+
def patch_u1(self, position: int, value: int) -> None:
|
|
193
|
+
self._buf[position : position + 1] = BE_U1.pack(value)
|
|
194
|
+
|
|
195
|
+
def patch_i1(self, position: int, value: int) -> None:
|
|
196
|
+
self._buf[position : position + 1] = BE_I1.pack(value)
|
|
197
|
+
|
|
198
|
+
def patch_u2(self, position: int, value: int) -> None:
|
|
199
|
+
self._buf[position : position + 2] = BE_U2.pack(value)
|
|
200
|
+
|
|
201
|
+
def patch_i2(self, position: int, value: int) -> None:
|
|
202
|
+
self._buf[position : position + 2] = BE_I2.pack(value)
|
|
203
|
+
|
|
204
|
+
def patch_u4(self, position: int, value: int) -> None:
|
|
205
|
+
self._buf[position : position + 4] = BE_U4.pack(value)
|
|
206
|
+
|
|
207
|
+
def patch_i4(self, position: int, value: int) -> None:
|
|
208
|
+
self._buf[position : position + 4] = BE_I4.pack(value)
|