Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 1 | """Simple class to read IFF chunks. |
| 2 | |
| 3 | An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File |
| 4 | Format)) has the following structure: |
| 5 | |
| 6 | +----------------+ |
| 7 | | ID (4 bytes) | |
| 8 | +----------------+ |
| 9 | | size (4 bytes) | |
| 10 | +----------------+ |
| 11 | | data | |
| 12 | | ... | |
| 13 | +----------------+ |
| 14 | |
| 15 | The ID is a 4-byte string which identifies the type of chunk. |
| 16 | |
| 17 | The size field (a 32-bit value, encoded using big-endian byte order) |
| 18 | gives the size of the whole chunk, including the 8-byte header. |
| 19 | |
Fred Drake | 624a191 | 1999-06-25 14:58:44 +0000 | [diff] [blame] | 20 | Usually an IFF-type file consists of one or more chunks. The proposed |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 21 | usage of the Chunk class defined here is to instantiate an instance at |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 22 | the start of each chunk and read from the instance until it reaches |
| 23 | the end, after which a new instance can be instantiated. At the end |
| 24 | of the file, creating a new instance will fail with a EOFError |
| 25 | exception. |
| 26 | |
| 27 | Usage: |
| 28 | while 1: |
| 29 | try: |
| 30 | chunk = Chunk(file) |
| 31 | except EOFError: |
| 32 | break |
| 33 | chunktype = chunk.getname() |
| 34 | while 1: |
| 35 | data = chunk.read(nbytes) |
| 36 | if not data: |
| 37 | pass |
| 38 | # do something with data |
| 39 | |
| 40 | The interface is file-like. The implemented methods are: |
| 41 | read, close, seek, tell, isatty. |
| 42 | Extra methods are: skip() (called by close, skips to the end of the chunk), |
| 43 | getname() (returns the name (ID) of the chunk) |
| 44 | |
| 45 | The __init__ method has one required argument, a file-like object |
| 46 | (including a chunk instance), and one optional argument, a flag which |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 47 | specifies whether or not chunks are aligned on 2-byte boundaries. The |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 48 | default is 1, i.e. aligned. |
| 49 | """ |
| 50 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 51 | __all__ = ["Chunk"] |
Tim Peters | dfc538a | 2001-01-21 04:49:16 +0000 | [diff] [blame^] | 52 | |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 53 | class Chunk: |
Guido van Rossum | 3601e88 | 1999-08-26 15:50:43 +0000 | [diff] [blame] | 54 | def __init__(self, file, align = 1, bigendian = 1, inclheader = 0): |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 55 | import struct |
| 56 | self.closed = 0 |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 57 | self.align = align # whether to align to word (2-byte) boundaries |
Guido van Rossum | 3601e88 | 1999-08-26 15:50:43 +0000 | [diff] [blame] | 58 | if bigendian: |
| 59 | strflag = '>' |
| 60 | else: |
| 61 | strflag = '<' |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 62 | self.file = file |
| 63 | self.chunkname = file.read(4) |
| 64 | if len(self.chunkname) < 4: |
| 65 | raise EOFError |
| 66 | try: |
Guido van Rossum | 3601e88 | 1999-08-26 15:50:43 +0000 | [diff] [blame] | 67 | self.chunksize = struct.unpack(strflag+'l', file.read(4))[0] |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 68 | except struct.error: |
| 69 | raise EOFError |
Guido van Rossum | 3601e88 | 1999-08-26 15:50:43 +0000 | [diff] [blame] | 70 | if inclheader: |
| 71 | self.chunksize = self.chunksize - 8 # subtract header |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 72 | self.size_read = 0 |
Guido van Rossum | 7bb11d6 | 1999-06-16 12:25:34 +0000 | [diff] [blame] | 73 | try: |
| 74 | self.offset = self.file.tell() |
| 75 | except: |
| 76 | self.seekable = 0 |
| 77 | else: |
| 78 | self.seekable = 1 |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 79 | |
| 80 | def getname(self): |
| 81 | """Return the name (ID) of the current chunk.""" |
| 82 | return self.chunkname |
| 83 | |
Guido van Rossum | 3601e88 | 1999-08-26 15:50:43 +0000 | [diff] [blame] | 84 | def getsize(self): |
| 85 | """Return the size of the current chunk.""" |
| 86 | return self.chunksize |
| 87 | |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 88 | def close(self): |
| 89 | if not self.closed: |
| 90 | self.skip() |
| 91 | self.closed = 1 |
| 92 | |
| 93 | def isatty(self): |
| 94 | if self.closed: |
| 95 | raise ValueError, "I/O operation on closed file" |
| 96 | return 0 |
| 97 | |
Fred Drake | 624a191 | 1999-06-25 14:58:44 +0000 | [diff] [blame] | 98 | def seek(self, pos, whence = 0): |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 99 | """Seek to specified position into the chunk. |
| 100 | Default position is 0 (start of chunk). |
| 101 | If the file is not seekable, this will result in an error. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 102 | """ |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 103 | |
| 104 | if self.closed: |
| 105 | raise ValueError, "I/O operation on closed file" |
Guido van Rossum | 7bb11d6 | 1999-06-16 12:25:34 +0000 | [diff] [blame] | 106 | if not self.seekable: |
| 107 | raise IOError, "cannot seek" |
Fred Drake | 624a191 | 1999-06-25 14:58:44 +0000 | [diff] [blame] | 108 | if whence == 1: |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 109 | pos = pos + self.size_read |
Fred Drake | 624a191 | 1999-06-25 14:58:44 +0000 | [diff] [blame] | 110 | elif whence == 2: |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 111 | pos = pos + self.chunk_size |
| 112 | if pos < 0 or pos > self.chunksize: |
| 113 | raise RuntimeError |
| 114 | self.file.seek(self.offset + pos, 0) |
| 115 | self.size_read = pos |
| 116 | |
| 117 | def tell(self): |
| 118 | if self.closed: |
| 119 | raise ValueError, "I/O operation on closed file" |
| 120 | return self.size_read |
| 121 | |
Fred Drake | 624a191 | 1999-06-25 14:58:44 +0000 | [diff] [blame] | 122 | def read(self, size = -1): |
| 123 | """Read at most size bytes from the chunk. |
| 124 | If size is omitted or negative, read until the end |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 125 | of the chunk. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 126 | """ |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 127 | |
| 128 | if self.closed: |
| 129 | raise ValueError, "I/O operation on closed file" |
| 130 | if self.size_read >= self.chunksize: |
| 131 | return '' |
Fred Drake | 624a191 | 1999-06-25 14:58:44 +0000 | [diff] [blame] | 132 | if size < 0: |
| 133 | size = self.chunksize - self.size_read |
| 134 | if size > self.chunksize - self.size_read: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 135 | size = self.chunksize - self.size_read |
Fred Drake | 624a191 | 1999-06-25 14:58:44 +0000 | [diff] [blame] | 136 | data = self.file.read(size) |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 137 | self.size_read = self.size_read + len(data) |
| 138 | if self.size_read == self.chunksize and \ |
| 139 | self.align and \ |
| 140 | (self.chunksize & 1): |
| 141 | dummy = self.file.read(1) |
| 142 | self.size_read = self.size_read + len(dummy) |
| 143 | return data |
| 144 | |
| 145 | def skip(self): |
| 146 | """Skip the rest of the chunk. |
| 147 | If you are not interested in the contents of the chunk, |
| 148 | this method should be called so that the file points to |
| 149 | the start of the next chunk. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 150 | """ |
Guido van Rossum | 8ea7bb8 | 1999-06-09 13:32:28 +0000 | [diff] [blame] | 151 | |
| 152 | if self.closed: |
| 153 | raise ValueError, "I/O operation on closed file" |
Guido van Rossum | 7bb11d6 | 1999-06-16 12:25:34 +0000 | [diff] [blame] | 154 | if self.seekable: |
| 155 | try: |
| 156 | n = self.chunksize - self.size_read |
| 157 | # maybe fix alignment |
| 158 | if self.align and (self.chunksize & 1): |
| 159 | n = n + 1 |
| 160 | self.file.seek(n, 1) |
| 161 | self.size_read = self.size_read + n |
| 162 | return |
| 163 | except: |
| 164 | pass |
| 165 | while self.size_read < self.chunksize: |
| 166 | n = min(8192, self.chunksize - self.size_read) |
| 167 | dummy = self.read(n) |
| 168 | if not dummy: |
| 169 | raise EOFError |