forked from agibli/sansapp
-
Notifications
You must be signed in to change notification settings - Fork 25
/
iff.py
164 lines (128 loc) · 5.14 KB
/
iff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import struct
from collections import namedtuple
from contextlib import contextmanager
from .common import align
IFF_NATIVE_ENDIAN = 0
IFF_BIG_ENDIAN = 1
IFF_LITTLE_ENDIAN = 2
IffFormat = namedtuple("IffFormat", ["endianness",
"typeid_bytes",
"size_bytes",
"header_alignment",
"chunk_alignment"])
IffChunk = namedtuple("IffChunk", ["typeid", "data_offset", "data_length"])
def _get_header_struct(format):
endian_formats = {IFF_NATIVE_ENDIAN: "=",
IFF_BIG_ENDIAN: ">",
IFF_LITTLE_ENDIAN: "<"}
byte_formats = {1: "B", 2: "H", 4: "L", 8: "Q"}
if format.endianness not in endian_formats:
raise ValueError("Iff: Invalid endianness")
if format.typeid_bytes not in byte_formats:
raise ValueError("Iff: Invalid typeid format")
if format.size_bytes not in byte_formats:
raise ValueError("Iff: Invalid size format")
typeid_padding = "x" * max(0, format.header_alignment - format.typeid_bytes)
size_padding = "x" * max(0, format.header_alignment - format.size_bytes)
fmt = (endian_formats[format.endianness] +
byte_formats[format.typeid_bytes] + typeid_padding +
byte_formats[format.size_bytes] + size_padding)
return struct.Struct(fmt)
class IffParser(object):
def __init__(self, stream, format):
self.__stream = stream
self.__format = format
self.__header_struct = _get_header_struct(format)
self.__current_chunk = None
self.__current_chunk_end = None
self.__chunk_handlers = {}
@property
def stream(self):
return self.__stream
@property
def chunk(self):
return self.__current_chunk
def reset(self):
self.__current_chunk = None
self.__current_chunk_end = None
self.__stream.seek(0)
def parse(self):
self.reset()
self._handle_all_chunks()
def on_iff_chunk(self, chunk):
pass
def _handle_all_chunks(self, types=None, alignment=None):
for chunk in self._iter_chunks(types=types):
self._get_chunk_handler(chunk.typeid)(chunk)
def _handle_next_chunk(self, alignment=None):
chunk = self._read_next_chunk()
if chunk:
with self._using_chunk(chunk, alignment=alignment):
self._get_chunk_handler(chunk.typeid)(chunk)
return True
return False
def _register_chunk_handler(self, typeid, callback):
self.__chunk_handlers[typeid] = callback
def _get_chunk_handler(self, typeid):
return self.__chunk_handlers.get(typeid, self.on_iff_chunk)
def _iter_chunks(self, types=None):
chunk = self._read_next_chunk()
while chunk:
with self._using_chunk(chunk):
if types is None or chunk.typeid in types:
yield chunk
chunk = self._read_next_chunk()
@contextmanager
def _using_chunk(self, chunk, alignment=None):
data_end = chunk.data_offset + chunk.data_length
chunk_end = chunk.data_offset + align(chunk.data_length, self.__format.chunk_alignment)
try:
old_chunk = self.__current_chunk
old_chunk_end = self.__current_chunk_end
self.__current_chunk = chunk
self.__current_chunk_end = chunk_end
self._set_offset(chunk.data_offset)
yield chunk
finally:
chunk_end = self.__current_chunk_end
self.__current_chunk = old_chunk
self.__current_chunk_end = old_chunk_end
self._set_offset(chunk_end)
def _realign(self):
chunk = self.__current_chunk
base_offset = self._get_offset()
base_delta = chunk.data_offset + chunk.data_length - base_offset
self.__current_chunk_end = base_offset + align(base_delta, self.__format.chunk_alignment)
def _read_chunk_data(self, chunk=None):
chunk = chunk or self.__current_chunk
if chunk:
self.__stream.seek(chunk.data_offset)
return self.__stream.read(chunk.data_length)
else:
return ""
def _read_next_chunk(self):
if self._is_past_the_end():
return None
header = self._read_next_chunk_header()
if not header:
return None
typeid, data_length = header
data_offset = self._get_offset()
return IffChunk(typeid=typeid,
data_offset=data_offset,
data_length=data_length)
def _read_next_chunk_header(self):
buf = self.__stream.read(self.__header_struct.size)
if len(buf) == self.__header_struct.size:
return self.__header_struct.unpack(buf)
else:
return None
def _is_past_the_end(self):
if self.__current_chunk_end:
return self._get_offset() >= self.__current_chunk_end
else:
return not self.__stream
def _get_offset(self):
return self.__stream.tell()
def _set_offset(self, offset):
self.__stream.seek(offset)