@@ -31,7 +31,8 @@ class ByteDecoder(object):
3131 _contents = ""
3232
3333 def __init__ (self , contents , endian ):
34- """ Accepts both strings and files implementing ``read()`` and
34+ """
35+ Accepts both strings and files implementing ``read()`` and
3536 decodes them in the specified endian format.
3637 """
3738 if hasattr (contents , "read" ):
@@ -66,52 +67,74 @@ def __init__(self, contents, endian):
6667 self ._unpack_bytes = lambda bytes : bytes if self .endian == ">" else bytes [::- 1 ]
6768
6869 def done (self ):
69- """ Returns true when all bytes have been decoded """
70+ """
71+ Returns true when all bytes have been decoded
72+ """
7073 return self .tell () == self .length
7174
7275 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+ """
7479 return self ._contents [start :end ]
7580
7681 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+ """
7885 start = self .tell ()
7986 return self ._contents [start : start + count ]
8087
8188 def read_uint8 (self ):
82- """ Returns the next byte as an unsigned integer """
89+ """
90+ Returns the next byte as an unsigned integer
91+ """
8392 return ord (self .read (1 ))
8493
8594 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+ """
8798 return self ._unpack_short (self .read (2 ))[0 ]
8899
89100 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+ """
91104 return self ._unpack_int (self .read (4 ))[0 ]
92105
93106 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+ """
95110 return self ._unpack_longlong (self .read (8 ))[0 ]
96111
97112 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+ """
99116 return self ._unpack_bytes (self .read (count ))
100117
101118 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+ """
103122 unpack = struct .Struct (str (self .endian + "B" * count )).unpack
104123 uint = 0
105124 for byte in unpack (self .read (count )):
106125 uint = uint << 8 | byte
107126 return uint
108127
109128 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+ """
111132 return self .read_bytes (count ).decode (encoding )
112133
113134 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+ """
115138 cstring = BytesIO ()
116139 while True :
117140 c = self .read (1 )
@@ -170,16 +193,22 @@ def __init__(self, contents):
170193 self .read_bool = functools .partial (self .read_bits , 1 )
171194
172195 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+ """
174199 return self .tell () == self .length
175200
176201 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+ """
178205 self ._next_byte = None
179206 self ._bit_shift = 0
180207
181208 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+ """
183212 data = ord (self ._buffer .read (1 ))
184213
185214 if self ._bit_shift != 0 :
@@ -192,7 +221,9 @@ def read_uint8(self):
192221 return data
193222
194223 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+ """
196227 data = self ._buffer .read_uint16 ()
197228
198229 if self ._bit_shift != 0 :
@@ -206,7 +237,9 @@ def read_uint16(self):
206237 return data
207238
208239 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+ """
210243 data = self ._buffer .read_uint32 ()
211244
212245 if self ._bit_shift != 0 :
@@ -220,7 +253,9 @@ def read_uint32(self):
220253 return data
221254
222255 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+ """
224259 data = self ._buffer .read_uint64 ()
225260
226261 if self ._bit_shift != 0 :
@@ -234,7 +269,9 @@ def read_uint64(self):
234269 return data
235270
236271 def read_vint (self ):
237- """ Reads a signed integer of variable length """
272+ """
273+ Reads a signed integer of variable length
274+ """
238275 byte = ord (self ._buffer .read (1 ))
239276 negative = byte & 0x01
240277 result = (byte & 0x7F ) >> 1
@@ -246,17 +283,23 @@ def read_vint(self):
246283 return - result if negative else result
247284
248285 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+ """
250289 self .byte_align ()
251290 return self ._buffer .read_bytes (count )
252291
253292 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+ """
255296 self .byte_align ()
256297 return self ._buffer .read_string (count , encoding )
257298
258299 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+ """
260303 data = self ._buffer .read_bytes (count )
261304
262305 if self ._bit_shift != 0 :
@@ -276,7 +319,9 @@ def read_bytes(self, count):
276319 return data
277320
278321 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+ """
280325 result = 0
281326 bits = count
282327 bit_shift = self ._bit_shift
@@ -325,7 +370,9 @@ def read_bits(self, count):
325370 return result
326371
327372 def read_frames (self ):
328- """ Reads a frame count as an unsigned integer """
373+ """
374+ Reads a frame count as an unsigned integer
375+ """
329376 byte = self .read_uint8 ()
330377 time , additional_bytes = byte >> 2 , byte & 0x03
331378 if additional_bytes == 0 :
@@ -338,8 +385,9 @@ def read_frames(self):
338385 return time << 24 | self .read_uint16 () << 8 | self .read_uint8 ()
339386
340387 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.
343391 """
344392 self .byte_align ()
345393 datatype = ord (self ._buffer .read (1 )) if datatype is None else datatype
0 commit comments