@@ -31,7 +31,8 @@ class ByteDecoder(object):
31
31
_contents = ""
32
32
33
33
def __init__ (self , contents , endian ):
34
- """ Accepts both strings and files implementing ``read()`` and
34
+ """
35
+ Accepts both strings and files implementing ``read()`` and
35
36
decodes them in the specified endian format.
36
37
"""
37
38
if hasattr (contents , "read" ):
@@ -66,52 +67,74 @@ def __init__(self, contents, endian):
66
67
self ._unpack_bytes = lambda bytes : bytes if self .endian == ">" else bytes [::- 1 ]
67
68
68
69
def done (self ):
69
- """ Returns true when all bytes have been decoded """
70
+ """
71
+ Returns true when all bytes have been decoded
72
+ """
70
73
return self .tell () == self .length
71
74
72
75
def read_range (self , start , end ):
73
- """ Returns the raw byte string from the indicated address range """
76
+ """
77
+ Returns the raw byte string from the indicated address range
78
+ """
74
79
return self ._contents [start :end ]
75
80
76
81
def peek (self , count ):
77
- """ Returns the raw byte string for the next ``count`` bytes """
82
+ """
83
+ Returns the raw byte string for the next ``count`` bytes
84
+ """
78
85
start = self .tell ()
79
86
return self ._contents [start : start + count ]
80
87
81
88
def read_uint8 (self ):
82
- """ Returns the next byte as an unsigned integer """
89
+ """
90
+ Returns the next byte as an unsigned integer
91
+ """
83
92
return ord (self .read (1 ))
84
93
85
94
def read_uint16 (self ):
86
- """ Returns the next two bytes as an unsigned integer """
95
+ """
96
+ Returns the next two bytes as an unsigned integer
97
+ """
87
98
return self ._unpack_short (self .read (2 ))[0 ]
88
99
89
100
def read_uint32 (self ):
90
- """ Returns the next four bytes as an unsigned integer """
101
+ """
102
+ Returns the next four bytes as an unsigned integer
103
+ """
91
104
return self ._unpack_int (self .read (4 ))[0 ]
92
105
93
106
def read_uint64 (self ):
94
- """ Returns the next eight bytes as an unsigned integer """
107
+ """
108
+ Returns the next eight bytes as an unsigned integer
109
+ """
95
110
return self ._unpack_longlong (self .read (8 ))[0 ]
96
111
97
112
def read_bytes (self , count ):
98
- """ Returns the next ``count`` bytes as a byte string """
113
+ """
114
+ Returns the next ``count`` bytes as a byte string
115
+ """
99
116
return self ._unpack_bytes (self .read (count ))
100
117
101
118
def read_uint (self , count ):
102
- """ Returns the next ``count`` bytes as an unsigned integer """
119
+ """
120
+ Returns the next ``count`` bytes as an unsigned integer
121
+ """
103
122
unpack = struct .Struct (str (self .endian + "B" * count )).unpack
104
123
uint = 0
105
124
for byte in unpack (self .read (count )):
106
125
uint = uint << 8 | byte
107
126
return uint
108
127
109
128
def read_string (self , count , encoding = "utf8" ):
110
- """ Read a string in given encoding (default utf8) that is ``count`` bytes long """
129
+ """
130
+ Read a string in given encoding (default utf8) that is ``count`` bytes long
131
+ """
111
132
return self .read_bytes (count ).decode (encoding )
112
133
113
134
def read_cstring (self , encoding = "utf8" ):
114
- """ Read a NULL byte terminated character string decoded with given encoding (default utf8). Ignores endian. """
135
+ """
136
+ Read a NULL byte terminated character string decoded with given encoding (default utf8). Ignores endian.
137
+ """
115
138
cstring = BytesIO ()
116
139
while True :
117
140
c = self .read (1 )
@@ -170,16 +193,22 @@ def __init__(self, contents):
170
193
self .read_bool = functools .partial (self .read_bits , 1 )
171
194
172
195
def done (self ):
173
- """ Returns true when all bytes in the buffer have been used"""
196
+ """
197
+ Returns true when all bytes in the buffer have been used
198
+ """
174
199
return self .tell () == self .length
175
200
176
201
def byte_align (self ):
177
- """ Moves cursor to the beginning of the next byte """
202
+ """
203
+ Moves cursor to the beginning of the next byte
204
+ """
178
205
self ._next_byte = None
179
206
self ._bit_shift = 0
180
207
181
208
def read_uint8 (self ):
182
- """ Returns the next 8 bits as an unsigned integer """
209
+ """
210
+ Returns the next 8 bits as an unsigned integer
211
+ """
183
212
data = ord (self ._buffer .read (1 ))
184
213
185
214
if self ._bit_shift != 0 :
@@ -192,7 +221,9 @@ def read_uint8(self):
192
221
return data
193
222
194
223
def read_uint16 (self ):
195
- """ Returns the next 16 bits as an unsigned integer """
224
+ """
225
+ Returns the next 16 bits as an unsigned integer
226
+ """
196
227
data = self ._buffer .read_uint16 ()
197
228
198
229
if self ._bit_shift != 0 :
@@ -206,7 +237,9 @@ def read_uint16(self):
206
237
return data
207
238
208
239
def read_uint32 (self ):
209
- """ Returns the next 32 bits as an unsigned integer """
240
+ """
241
+ Returns the next 32 bits as an unsigned integer
242
+ """
210
243
data = self ._buffer .read_uint32 ()
211
244
212
245
if self ._bit_shift != 0 :
@@ -220,7 +253,9 @@ def read_uint32(self):
220
253
return data
221
254
222
255
def read_uint64 (self ):
223
- """ Returns the next 64 bits as an unsigned integer """
256
+ """Returns
257
+ the next 64 bits as an unsigned integer
258
+ """
224
259
data = self ._buffer .read_uint64 ()
225
260
226
261
if self ._bit_shift != 0 :
@@ -234,7 +269,9 @@ def read_uint64(self):
234
269
return data
235
270
236
271
def read_vint (self ):
237
- """ Reads a signed integer of variable length """
272
+ """
273
+ Reads a signed integer of variable length
274
+ """
238
275
byte = ord (self ._buffer .read (1 ))
239
276
negative = byte & 0x01
240
277
result = (byte & 0x7F ) >> 1
@@ -246,17 +283,23 @@ def read_vint(self):
246
283
return - result if negative else result
247
284
248
285
def read_aligned_bytes (self , count ):
249
- """ Skips to the beginning of the next byte and returns the next ``count`` bytes as a byte string """
286
+ """
287
+ Skips to the beginning of the next byte and returns the next ``count`` bytes as a byte string
288
+ """
250
289
self .byte_align ()
251
290
return self ._buffer .read_bytes (count )
252
291
253
292
def read_aligned_string (self , count , encoding = "utf8" ):
254
- """ Skips to the beginning of the next byte and returns the next ``count`` bytes decoded with encoding (default utf8) """
293
+ """
294
+ Skips to the beginning of the next byte and returns the next ``count`` bytes decoded with encoding (default utf8)
295
+ """
255
296
self .byte_align ()
256
297
return self ._buffer .read_string (count , encoding )
257
298
258
299
def read_bytes (self , count ):
259
- """ Returns the next ``count*8`` bits as a byte string """
300
+ """
301
+ Returns the next ``count*8`` bits as a byte string
302
+ """
260
303
data = self ._buffer .read_bytes (count )
261
304
262
305
if self ._bit_shift != 0 :
@@ -276,7 +319,9 @@ def read_bytes(self, count):
276
319
return data
277
320
278
321
def read_bits (self , count ):
279
- """ Returns the next ``count`` bits as an unsigned integer """
322
+ """Returns
323
+ the next ``count`` bits as an unsigned integer
324
+ """
280
325
result = 0
281
326
bits = count
282
327
bit_shift = self ._bit_shift
@@ -325,7 +370,9 @@ def read_bits(self, count):
325
370
return result
326
371
327
372
def read_frames (self ):
328
- """ Reads a frame count as an unsigned integer """
373
+ """
374
+ Reads a frame count as an unsigned integer
375
+ """
329
376
byte = self .read_uint8 ()
330
377
time , additional_bytes = byte >> 2 , byte & 0x03
331
378
if additional_bytes == 0 :
@@ -338,8 +385,9 @@ def read_frames(self):
338
385
return time << 24 | self .read_uint16 () << 8 | self .read_uint8 ()
339
386
340
387
def read_struct (self , datatype = None ):
341
- """ Reads a nested data structure. If the type is not specified the
342
- first byte is used as the type identifier.
388
+ """
389
+ Reads a nested data structure. If the type is not specified
390
+ the first byte is used as the type identifier.
343
391
"""
344
392
self .byte_align ()
345
393
datatype = ord (self ._buffer .read (1 )) if datatype is None else datatype
0 commit comments