-
Notifications
You must be signed in to change notification settings - Fork 4
/
bitreader.py
227 lines (198 loc) · 7.13 KB
/
bitreader.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of pt4utils.
#
# Copyright (C) 2013, Marcelo Martins.
#
# pt4utils was developed in affiliation with Brown University,
# Department of Computer Science. For more information about the
# department, see <http://www.cs.brown.edu/>.
#
# pt4utils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pt4utils is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pt4utils. If not, see <http://www.gnu.org/licenses/>.
#
import os
import struct
import sys
class BitReader(object):
""" Interpreter for binary stream """
def __init__(self, filename):
try:
self.filesize = os.stat(filename).st_size
self.fin = (open(filename, "rb"))
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
def close(self):
""" Close binary stream """
try:
self.fin.close()
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
def skipBytes(self, nbytes):
""" Forward and ignore nbytes from current reading position
"""
try:
self.fin.seek(nbytes, os.SEEK_CUR)
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
def readUInt8(self):
""" Read and return unsigned 8-bit integer from input
"""
try:
# B = unsigned char
val = struct.unpack('B', self.fin.read(1))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readInt8(self):
""" Read and return signed 8-bit integer from input
"""
try:
# b = signed char
val = struct.unpack('b', self.fin.read(1))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readFloat32(self):
""" Read and return 32-bit floating-point number from input
"""
try:
# f = floating point (4 bytes)
val = struct.unpack('f', self.fin.read(4))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readUInt16(self):
""" Read and return unsigned 16-bit integer from input
"""
try:
# H = unsigned short (2 bytes)
val = struct.unpack('H', self.fin.read(2))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readInt16(self):
""" Read and return signed 16-bit integer from input
"""
try:
# h = short (2 bytes)
val = struct.unpack('h', self.fin.read(2))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readUInt32(self):
""" Read and return unsigned 32-bit integer from input
"""
try:
# I = unsigned int (4 bytes)
val = struct.unpack('I', self.fin.read(4))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readInt32(self):
""" Read and return signed 32-bit integer from input
"""
try:
# i = int (4 bytes)
val = struct.unpack('i', self.fin.read(4))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readUInt64(self):
""" Read and return unsigned 64-bit integer from input
"""
try:
# Q = unsigned long long (8 bytes)
val = struct.unpack('Q', self.fin.read(8))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readInt64(self):
""" Read and return signed 64-bit integer from input
"""
try:
# q = long long (8 bytes)
val = struct.unpack('q', self.fin.read(8))[0]
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return val
def readString(self, length):
""" Read and return length-byte string from input
"""
assert length >= 0
try:
s = self.fin.read(length)
return s.strip()
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
except:
sys.stderr.write("Unexpected error: %s\n" % sys.exc_info()[0])
raise
return None
def isFinished(self):
""" Return whether finished to read input
"""
try:
return self.fin.tell() == self.filesize
except IOError, (errno, strerror):
sys.stderr.write("I/O error(%s): %s\n" % (errno, strerror))
self.fin.close()
return True