File: binary_file.py

package info (click to toggle)
python-bx 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: python: 17,136; ansic: 2,326; makefile: 24; sh: 8
file content (185 lines) | stat: -rw-r--r-- 5,064 bytes parent folder | download
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
"""
Wrappers for doing binary IO on file-like objects
"""

import struct
import sys

import numpy


def bytesify(s):
    if isinstance(s, bytes):
        return s
    else:
        return s.encode()


# Standard size:
# short is 8 bits
# int and long are 32 bits
# long long is 64 bits


class BadMagicNumber(IOError):
    pass


class BinaryFileReader:
    """
    Wrapper for doing binary reads on any file like object.

    Currently this is not heavily optimized (it uses the `struct` module to
    unpack)
    """

    def __init__(self, file, magic=None, is_little_endian=False):
        self.is_little_endian = is_little_endian
        self.file = file
        if magic is not None:
            # Attempt to read magic number and chuck endianess
            bytes = file.read(4)
            if struct.unpack(">I", bytes)[0] == magic:
                pass
            elif struct.unpack("<I", bytes)[0] == magic:
                self.is_little_endian = True
            else:
                raise BadMagicNumber(
                    "File does not have expected magic number: {:x} != {:x} or {:x}".format(
                        magic, struct.unpack(">I", bytes)[0], struct.unpack("<I", bytes)[0]
                    )
                )
        # Set endian code
        if self.is_little_endian:
            self.endian_code = "<"
            self.byteswap_needed = sys.byteorder != "little"
        else:
            self.endian_code = ">"
            self.byteswap_needed = sys.byteorder != "big"

    def unpack(self, format, buffer, byte_count=None):
        pattern = f"{self.endian_code}{format}"
        if byte_count is None:
            byte_count = struct.calcsize(pattern)
        return struct.unpack(pattern, buffer)

    def read_and_unpack(self, format, byte_count=None):
        """
        Read enough bytes to unpack according to `format` and return the
        tuple of unpacked values.
        """
        pattern = f"{self.endian_code}{format}"
        if byte_count is None:
            byte_count = struct.calcsize(pattern)
        return struct.unpack(pattern, self.file.read(byte_count))

    def read_c_string(self):
        """
        Read a zero terminated (C style) string
        """
        rval = []
        while True:
            ch = self.file.read(1)
            assert len(ch) == 1, "Unexpected end of file"
            if ch == b"\0":
                break
            rval.append(ch)
        return b"".join(rval)

    def read_raw_array(self, dtype, size):
        a = numpy.fromfile(self.file, dtype=dtype, count=size)
        if self.byteswap_needed:
            a.byteswap()
        return a

    def read(self, byte_count=1):
        return self.file.read(byte_count)

    def tell(self):
        return self.file.tell()

    def skip(self, count):
        self.file.seek(count, 1)

    def seek(self, pos, whence=0):
        return self.file.seek(pos, whence)

    def read_uint8(self):
        return self.read_and_unpack("B", 1)[0]

    def read_uint16(self):
        return self.read_and_unpack("H", 2)[0]

    def read_uint32(self):
        return self.read_and_unpack("L", 4)[0]

    def read_uint64(self):
        return self.read_and_unpack("Q", 8)[0]

    def read_float(self):
        return self.read_and_unpack("f", 4)[0]


class BinaryFileWriter:
    """
    Wrapper for doing binary writes on any file like object.

    Currently this is not heavily optimized (it uses the `struct` module to
    unpack)
    """

    def __init__(self, file, magic=None, is_little_endian=False):
        self.is_little_endian = is_little_endian
        if self.is_little_endian:
            self.endian_code = "<"
        else:
            self.endian_code = ">"
        self.file = file
        if magic is not None:
            self.write_uint32(magic)

    def pack(self, format, buffer):
        pattern = f"{self.endian_code}{format}"
        return struct.pack(pattern, buffer)

    def pack_and_write(self, format, value):
        """
        Read enough bytes to unpack according to `format` and return the
        tuple of unpacked values.
        """
        pattern = f"{self.endian_code}{format}"
        return self.file.write(struct.pack(pattern, value))

    def write_c_string(self, value):
        """
        Read a zero terminated (C style) string
        """
        self.file.write(value)
        self.file.write(b"\0")

    def write_raw_array(self, value):
        value.tofile(self.file)

    def write(self, value):
        return self.file.write(bytesify(value))

    def skip(self, count):
        self.file.seek(count, 1)

    def tell(self):
        return self.file.tell()

    def seek(self, pos, whence=0):
        return self.file.seek(pos, whence)

    def write_uint8(self, value):
        return self.pack_and_write("B", value)

    def write_uint16(self, value):
        return self.pack_and_write("H", value)

    def write_uint32(self, value):
        return self.pack_and_write("L", value)

    def write_uint64(self, value):
        return self.pack_and_write("Q", value)