forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
130 lines (104 loc) · 3.94 KB
/
data.py
File metadata and controls
130 lines (104 loc) · 3.94 KB
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
import struct
import sys
from typing import IO, Optional, Union
class PycData:
"""Abstract base class for reading Python bytecode data."""
def is_open(self) -> bool:
raise NotImplementedError
def at_eof(self) -> bool:
raise NotImplementedError
def get_byte(self) -> int:
raise NotImplementedError
def get_buffer(self, bytes_count: int) -> bytes:
raise NotImplementedError
def get16(self) -> int:
"""Reads a 16-bit little-endian unsigned integer."""
b1 = self.get_byte()
b2 = self.get_byte()
if b1 == -1 or b2 == -1:
# Handle EOF or error appropriately, C++ implementation often assumes valid input or returns partial
# In C++: result |= (getByte() & 0xFF) << 8; [cite: 374]
pass
return (b1 & 0xFF) | ((b2 & 0xFF) << 8)
def get32(self) -> int:
"""Reads a 32-bit little-endian unsigned integer."""
# [cite: 375-376]
b1 = self.get_byte()
b2 = self.get_byte()
b3 = self.get_byte()
b4 = self.get_byte()
return (b1 & 0xFF) | ((b2 & 0xFF) << 8) | ((b3 & 0xFF) << 16) | ((b4 & 0xFF) << 24)
def get64(self) -> int:
"""Reads a 64-bit little-endian unsigned integer."""
# [cite: 377-380]
lo = self.get32()
hi = self.get32()
return (lo & 0xFFFFFFFF) | ((hi & 0xFFFFFFFF) << 32)
class PycFile(PycData):
"""Implementation of PycData for reading from a file."""
def __init__(self, filename: str):
try:
self.m_stream: Optional[IO[bytes]] = open(filename, "rb")
except OSError:
self.m_stream = None
def __del__(self):
if self.m_stream:
self.m_stream.close()
def is_open(self) -> bool:
return self.m_stream is not None
def at_eof(self) -> bool:
if not self.m_stream:
return True
# Check for EOF by peeking
current_pos = self.m_stream.tell()
b = self.m_stream.read(1)
if not b:
return True
self.m_stream.seek(current_pos)
return False
def get_byte(self) -> int:
if not self.m_stream:
return -1
b = self.m_stream.read(1)
if not b:
return -1 # EOF
return b[0]
def get_buffer(self, bytes_count: int) -> bytes:
if not self.m_stream:
return b""
return self.m_stream.read(bytes_count)
class PycBuffer(PycData):
"""Implementation of PycData for reading from a memory buffer."""
def __init__(self, buffer: bytes):
self.m_buffer = buffer
self.m_size = len(buffer)
self.m_pos = 0
def is_open(self) -> bool:
return self.m_buffer is not None
def at_eof(self) -> bool:
return self.m_pos >= self.m_size
def get_byte(self) -> int:
if self.at_eof():
return -1
ch = self.m_buffer[self.m_pos]
self.m_pos += 1
return ch
def get_buffer(self, bytes_count: int) -> bytes:
if self.m_pos + bytes_count > self.m_size:
bytes_count = self.m_size - self.m_pos
if bytes_count <= 0:
return b""
result = self.m_buffer[self.m_pos : self.m_pos + bytes_count]
self.m_pos += bytes_count
return result
# Formatted print helper functions usually map to print() or file.write() in Python
def formatted_print(stream: IO[str], format_str: str, *args):
# [cite: 388-392]
# Python's string formatting is powerful enough to handle most C-style formats
# usually via % operator or f-strings.
# Note: C++ uses vsnprintf style formatting.
try:
stream.write(format_str % args)
except TypeError:
# Fallback if format string doesn't match args directly
stream.write(format_str + " " + str(args))