[3] | 1 | """ |
---|
| 2 | Wrappers for doing binary IO on file-like objects |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import numpy |
---|
| 6 | import struct |
---|
| 7 | import sys |
---|
| 8 | |
---|
| 9 | ## Standard size: |
---|
| 10 | ## short is 8 bits |
---|
| 11 | ## int and long are 32 bits |
---|
| 12 | ## long long is 64 bits |
---|
| 13 | |
---|
| 14 | class BadMagicNumber( IOError ): |
---|
| 15 | pass |
---|
| 16 | |
---|
| 17 | class BinaryFileReader( object ): |
---|
| 18 | """ |
---|
| 19 | Wrapper for doing binary reads on any file like object. |
---|
| 20 | |
---|
| 21 | Currently this is not heavily optimized (it uses the `struct` module to |
---|
| 22 | unpack) |
---|
| 23 | """ |
---|
| 24 | def __init__( self, file, magic = None, is_little_endian = False ): |
---|
| 25 | self.is_little_endian = is_little_endian |
---|
| 26 | self.file = file |
---|
| 27 | if magic is not None: |
---|
| 28 | # Attempt to read magic number and chuck endianess |
---|
| 29 | bytes = file.read( 4 ) |
---|
| 30 | if struct.unpack( ">I", bytes )[0] == magic: |
---|
| 31 | pass |
---|
| 32 | elif struct.unpack( "<I", bytes )[0] == magic: |
---|
| 33 | self.is_little_endian = True |
---|
| 34 | else: |
---|
| 35 | raise BadMagic( "File does not have expected magic number" ) |
---|
| 36 | # Set endian code |
---|
| 37 | if self.is_little_endian: |
---|
| 38 | self.endian_code = "<" |
---|
| 39 | self.byteswap_needed = ( sys.byteorder != "little" ) |
---|
| 40 | else: |
---|
| 41 | self.endian_code = ">" |
---|
| 42 | self.byteswap_needed = ( sys.byteorder != "big" ) |
---|
| 43 | |
---|
| 44 | def unpack( self, format, buffer, byte_count=None ): |
---|
| 45 | pattern = "%s%s" % ( self.endian_code, format ) |
---|
| 46 | if byte_count is None: |
---|
| 47 | byte_count = struct.calcsize( pattern ) |
---|
| 48 | return struct.unpack( pattern, buffer ) |
---|
| 49 | |
---|
| 50 | def read_and_unpack( self, format, byte_count=None ): |
---|
| 51 | """ |
---|
| 52 | Read enough bytes to unpack according to `format` and return the |
---|
| 53 | tuple of unpacked values. |
---|
| 54 | """ |
---|
| 55 | pattern = "%s%s" % ( self.endian_code, format ) |
---|
| 56 | if byte_count is None: |
---|
| 57 | byte_count = struct.calcsize( pattern ) |
---|
| 58 | return struct.unpack( pattern, self.file.read( byte_count ) ) |
---|
| 59 | |
---|
| 60 | def read_c_string( self ): |
---|
| 61 | """ |
---|
| 62 | Read a zero terminated (C style) string |
---|
| 63 | """ |
---|
| 64 | rval = [] |
---|
| 65 | while 1: |
---|
| 66 | ch = self.file.read(1) |
---|
| 67 | assert len( ch ) == 1, "Unexpected end of file" |
---|
| 68 | if ch == '\0': |
---|
| 69 | break |
---|
| 70 | rval.append( ch ) |
---|
| 71 | return ''.join( rval ) |
---|
| 72 | |
---|
| 73 | def read_raw_array( self, dtype, size ): |
---|
| 74 | a = numpy.fromfile( self.file, dtype=dtype, count=size ) |
---|
| 75 | if self.byteswap_needed: |
---|
| 76 | a.byteswap() |
---|
| 77 | return a |
---|
| 78 | |
---|
| 79 | def read( self, byte_count=1 ): |
---|
| 80 | return self.file.read( byte_count ) |
---|
| 81 | |
---|
| 82 | def tell( self ): |
---|
| 83 | return self.file.tell() |
---|
| 84 | |
---|
| 85 | def skip( self, count ): |
---|
| 86 | self.file.seek( count, 1 ) |
---|
| 87 | |
---|
| 88 | def seek( self, pos, whence=0 ): |
---|
| 89 | return self.file.seek( pos, whence ) |
---|
| 90 | |
---|
| 91 | def read_uint8( self ): |
---|
| 92 | return self.read_and_unpack( "B", 1 )[0] |
---|
| 93 | |
---|
| 94 | def read_uint16( self ): |
---|
| 95 | return self.read_and_unpack( "H", 2 )[0] |
---|
| 96 | |
---|
| 97 | def read_uint32( self ): |
---|
| 98 | return self.read_and_unpack( "L", 4 )[0] |
---|
| 99 | |
---|
| 100 | def read_uint64( self ): |
---|
| 101 | return self.read_and_unpack( "Q", 8 )[0] |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | class BinaryFileWriter( object ): |
---|
| 105 | """ |
---|
| 106 | Wrapper for doing binary writes on any file like object. |
---|
| 107 | |
---|
| 108 | Currently this is not heavily optimized (it uses the `struct` module to |
---|
| 109 | unpack) |
---|
| 110 | """ |
---|
| 111 | def __init__( self, file, magic = None, is_little_endian = False ): |
---|
| 112 | self.is_little_endian = is_little_endian |
---|
| 113 | if self.is_little_endian: |
---|
| 114 | self.endian_code = "<" |
---|
| 115 | else: |
---|
| 116 | self.endian_code = ">" |
---|
| 117 | self.file = file |
---|
| 118 | if magic is not None: |
---|
| 119 | self.write_uint32( magic ) |
---|
| 120 | |
---|
| 121 | def pack( self, format, buffer ): |
---|
| 122 | pattern = "%s%s" % ( self.endian_code, format ) |
---|
| 123 | return struct.pack( pattern, buffer ) |
---|
| 124 | |
---|
| 125 | def pack_and_write( self, format, value ): |
---|
| 126 | """ |
---|
| 127 | Read enough bytes to unpack according to `format` and return the |
---|
| 128 | tuple of unpacked values. |
---|
| 129 | """ |
---|
| 130 | pattern = "%s%s" % ( self.endian_code, format ) |
---|
| 131 | return self.file.write( struct.pack( pattern, value ) ) |
---|
| 132 | |
---|
| 133 | def write_c_string( self, value ): |
---|
| 134 | """ |
---|
| 135 | Read a zero terminated (C style) string |
---|
| 136 | """ |
---|
| 137 | self.file.write( value ) |
---|
| 138 | self.file.write( '\0' ) |
---|
| 139 | |
---|
| 140 | def write_raw_array( self, value ): |
---|
| 141 | value.tofile( self.file ) |
---|
| 142 | |
---|
| 143 | def write( self, value ): |
---|
| 144 | return self.file.write( value ) |
---|
| 145 | |
---|
| 146 | def skip( self, count ): |
---|
| 147 | self.file.seek( count, 1 ) |
---|
| 148 | |
---|
| 149 | def tell( self ): |
---|
| 150 | return self.file.tell() |
---|
| 151 | |
---|
| 152 | def seek( self, pos, whence=0 ): |
---|
| 153 | return self.file.seek( pos, whence ) |
---|
| 154 | |
---|
| 155 | def write_uint8( self, value ): |
---|
| 156 | return self.pack_and_write( "B", value ) |
---|
| 157 | |
---|
| 158 | def write_uint16( self, value ): |
---|
| 159 | return self.pack_and_write( "H", value ) |
---|
| 160 | |
---|
| 161 | def write_uint32( self, value ): |
---|
| 162 | return self.pack_and_write( "L", value ) |
---|
| 163 | |
---|
| 164 | def write_uint64( self, value ): |
---|
| 165 | return self.pack_and_write( "Q", value ) |
---|