vegaseat

read bytes little-endian

Mar 13th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. ''' class_ByteReader.py
  2. Little-endian systems store the least significant byte in the smallest address
  3.  
  4. tested with Python27/34
  5. '''
  6.  
  7. class ByteReader(object):
  8.     '''
  9.    reads bytes from a file
  10.    '''
  11.     def __init__(self, filename):
  12.         self._buffer = list(bytearray(open(filename, 'rb').read()))
  13.         self.pos = 0
  14.         self._eof = EOFError('unexpected end of file')
  15.  
  16.     def read_byte(self):
  17.         '''
  18.        read one byte
  19.        advances position
  20.        '''
  21.         try:
  22.             byte = self._buffer[self.pos]
  23.             self.pos += 1
  24.             return byte
  25.         except IndexError:
  26.             raise self._eof
  27.  
  28.     def peek_byte(self):
  29.         '''
  30.        return the next byte in the file (look-ahead)
  31.        '''
  32.         try:
  33.             return self._buffer[self.pos]
  34.         except IndexError:
  35.             raise self._eof
  36.  
  37.     def peek_list(self, n):
  38.         '''
  39.        return a list of the next n bytes
  40.        '''
  41.         return self._buffer[self.pos:self.pos+n]
  42.  
  43.     def read_short(self):
  44.         '''
  45.        read short (2 bytes little endian)
  46.        '''
  47.         a, b = self.read_list(2)
  48.         # same as a * 256 + b
  49.         return a << 8 | b
  50.  
  51.     def read_long(self):
  52.         '''
  53.        read long (4 bytes little endian)
  54.        '''
  55.         a, b, c, d = self.read_list(4)
  56.         return a << 24 | b << 16 | c << 8 | d
  57.  
  58.     def read_list(self, n):
  59.         '''
  60.        read n bytes and return as a list
  61.        advances position
  62.        '''
  63.         i = self.pos
  64.         ret = self._buffer[i:i + n]
  65.         if len(ret) < n:
  66.             raise self._eof
  67.         self.pos += n
  68.         return ret
  69.  
  70.     def __enter__(self):
  71.         return self
  72.  
  73.     def __exit__(self, type, value, traceback):
  74.         return False
  75.  
  76.  
  77. # testing the module ...
  78. if __name__ == '__main__':
  79.     # pick a file you have in the working directory or give full path
  80.     fname = "Farm.gif"
  81.     br = ByteReader(fname)
  82.     print("read first byte = {}".format(br.read_byte()))
  83.     print("peek next 10 bytes = {}".format(br.peek_list(10)))
  84.  
  85.     print("take 2 bytes:")
  86.     print(br.peek_list(2))
  87.     print("calculate a two byte value (little endian):")
  88.     two_bytes = br.peek_list(2)
  89.     print(two_bytes[0]*256 + two_bytes[1])
  90.     # same result
  91.     print(br.read_short())
  92.  
  93.     print("take 4 bytes:")
  94.     print(br.peek_list(4))
  95.     print("calculate value (4 bytes little endian):")
  96.     print(br.read_long())
  97.  
  98.     print("read next 8 bytes:")
  99.     print(br.read_list(8))
Advertisement
Add Comment
Please, Sign In to add comment