aboutsummaryrefslogtreecommitdiff
path: root/externals/libmpq/bindings/python/mpq.py
blob: cf6ecaae800f74d9280dd85f123d2acecf8cfe1f (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""wrapper for libmpq"""

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import ctypes
import ctypes.util
import os

libmpq = ctypes.CDLL(ctypes.util.find_library("mpq"))

class Error(Exception):
    pass

errors = {
    -1: (IOError, "open"),
    -2: (IOError, "close"),
    -3: (IOError, "seek"),
    -4: (IOError, "read"),
    -5: (IOError, "write"),
    -6: (MemoryError,),
    -7: (Error, "file is not an mpq or is corrupted"),
    -8: (AssertionError, "not initialized"),
    -9: (AssertionError, "buffer size too small"),
    -10: (IndexError, "file not in archive"),
    -11: (AssertionError, "decrypt"),
    -12: (AssertionError, "unpack"),
}

def check_error(result, func, arguments, errors=errors):
    try:
        error = errors[result]
    except KeyError:
        return result
    else:
        raise error[0](*error[1:])

libmpq.libmpq__version.restype = ctypes.c_char_p

libmpq.libmpq__archive_open.errcheck = check_error
libmpq.libmpq__archive_close.errcheck = check_error
libmpq.libmpq__archive_packed_size.errcheck = check_error
libmpq.libmpq__archive_unpacked_size.errcheck = check_error
libmpq.libmpq__archive_offset.errcheck = check_error
libmpq.libmpq__archive_version.errcheck = check_error
libmpq.libmpq__archive_files.errcheck = check_error

libmpq.libmpq__file_packed_size.errcheck = check_error
libmpq.libmpq__file_unpacked_size.errcheck = check_error
libmpq.libmpq__file_offset.errcheck = check_error
libmpq.libmpq__file_blocks.errcheck = check_error
libmpq.libmpq__file_encrypted.errcheck = check_error
libmpq.libmpq__file_compressed.errcheck = check_error
libmpq.libmpq__file_imploded.errcheck = check_error
libmpq.libmpq__file_number.errcheck = check_error
libmpq.libmpq__file_read.errcheck = check_error

libmpq.libmpq__block_open_offset.errcheck = check_error
libmpq.libmpq__block_close_offset.errcheck = check_error
libmpq.libmpq__block_unpacked_size.errcheck = check_error
libmpq.libmpq__block_read.errcheck = check_error

__version__ = libmpq.libmpq__version()


class Reader(object):
    def __init__(self, file, libmpq=libmpq):
        self._file = file
        self._pos = 0
        self._buf = []
        self._cur_block = 0
        libmpq.libmpq__block_open_offset(self._file._archive._mpq,
            self._file.number)
    
    def __iter__(self): 
        return self
    
    def __repr__(self):
        return "iter(%r)" % self._file
    
    def seek(self, offset, whence=os.SEEK_SET, os=os):
        if whence == os.SEEK_SET:
            pass
        elif whence == os.SEEK_CUR:
            offset += self._pos
        elif whence == os.SEEK_END:
            offset += self._file.unpacked_size
        else:
            raise ValueError, "invalid whence"
        
        if offset >= self._pos:
            self.read(offset - self._pos)
        else:
            self._pos = 0
            self._buf = []
            self._cur_block = 0
            self.read(offset)
    
    def tell(self):
        return self._pos
    
    def _read_block(self, ctypes=ctypes, libmpq=libmpq):
        block_size = ctypes.c_uint64()
        libmpq.libmpq__block_unpacked_size(self._file._archive._mpq,
            self._file.number, self._cur_block, ctypes.byref(block_size))
        block_data = ctypes.create_string_buffer(block_size.value)
        libmpq.libmpq__block_read(self._file._archive._mpq,
            self._file.number, self._cur_block,
            block_data, ctypes.c_uint64(len(block_data)), None)
        self._buf.append(block_data.raw)
        self._cur_block += 1
    
    def read(self, size=-1):
        while size < 0 or sum(map(len, self._buf)) < size:
            if self._cur_block == self._file.blocks:
                break
            self._read_block()
        buf = "".join(self._buf)
        if size < 0:
            ret = buf
            self._buf = []
        else:
            ret = buf[:size]
            self._buf = [buf[size:]]
        self._pos += len(ret)
        return ret
    
    def readline(self, os=os):
        line = []
        while True:
            char = self.read(1)
            if char == "":
                break
            if char not in '\r\n' and line and line[-1] in '\r\n':
                self.seek(-1, os.SEEK_CUR)
                break
            line.append(char)
        return ''.join(line)
    
    def next(self):
        line = self.readline()
        if not line:
            raise StopIteration
        return line
    
    def readlines(self, sizehint=-1):
        res = []
        while sizehint < 0 or sum(map(len, res)) < sizehint:
            line = self.readline()
            if not line:
                break
            res.append(line)
        return res
    
    xreadlines = __iter__
    
    def __del__(self, libmpq=libmpq):
        libmpq.libmpq__block_close_offset(self._file._archive._mpq,
            self._file.number)


class File(object):
    def __init__(self, archive, number, ctypes=ctypes, libmpq=libmpq):
        self._archive = archive
        self.number = number
        
        for name, atype in [
            ("packed_size", ctypes.c_uint64),
            ("unpacked_size", ctypes.c_uint64),
            ("offset", ctypes.c_uint64),
            ("blocks", ctypes.c_uint32),
            ("encrypted", ctypes.c_uint32),
            ("compressed", ctypes.c_uint32),
            ("imploded", ctypes.c_uint32),
        ]:
            data = atype()
            func = getattr(libmpq, "libmpq__file_"+name)
            func(self._archive._mpq, self.number, ctypes.byref(data))
            setattr(self, name, data.value)
    
    def __str__(self, ctypes=ctypes, libmpq=libmpq):
        data = ctypes.create_string_buffer(self.unpacked_size)
        libmpq.libmpq__file_read(self._archive._mpq, self.number,
            data, ctypes.c_uint64(len(data)), None)
        return data.raw
    
    def __repr__(self):
        return "%r[%i]" % (self._archive, self.number)
    
    def __iter__(self, Reader=Reader):
        return Reader(self)


class Archive(object):
    def __init__(self, source, ctypes=ctypes, File=File, libmpq=libmpq):
        self._source = source
        if isinstance(source, File):
            assert not source.encrypted
            assert not source.compressed
            assert not source.imploded
            self.filename = source._archive.filename
            offset = source._archive.offset + source.offset
        else:
            self.filename = source
            offset = -1
        
        self._mpq = ctypes.c_void_p()
        libmpq.libmpq__archive_open(ctypes.byref(self._mpq), self.filename,
            ctypes.c_uint64(offset))
        self._opened = True
        
        for field_name, field_type in [
            ("packed_size", ctypes.c_uint64),
            ("unpacked_size", ctypes.c_uint64),
            ("offset", ctypes.c_uint64),
            ("version", ctypes.c_uint32),
            ("files", ctypes.c_uint32),
        ]:
            func = getattr(libmpq, "libmpq__archive_" + field_name)
            data = field_type()
            func(self._mpq, ctypes.byref(data))
            setattr(self, field_name, data.value)
    
    def __del__(self, libmpq=libmpq):
        if getattr(self, "_opened", False):
            libmpq.libmpq__archive_close(self._mpq)
    
    def __len__(self):
        return self.files
    
    def __contains__(self, item, ctypes=ctypes, libmpq=libmpq):
        if isinstance(item, str):
            data = ctypes.c_uint32()
            try:
                libmpq.libmpq__file_number(self._mpq, ctypes.c_char_p(item),
                    ctypes.byref(data))
            except IndexError:
                return False
            return True
        return 0 <= item < self.files
    
    def __getitem__(self, item, ctypes=ctypes, File=File, libmpq=libmpq):
        if isinstance(item, str):
            data = ctypes.c_int()
            libmpq.libmpq__file_number(self._mpq, ctypes.c_char_p(item),
                ctypes.byref(data))
            item = data.value
        else:
            if not 0 <= item < self.files:
                raise IndexError, "file not in archive"
        return File(self, item)
    
    def __repr__(self):
        return "mpq.Archive(%r)" % self._source

# Remove clutter - everything except Error and Archive.
del os, check_error, ctypes, errors, File, libmpq, Reader

if __name__ == "__main__":
    import sys, random
    archive = Archive(sys.argv[1])
    print repr(archive)
    for k, v in archive.__dict__.iteritems():
        #if k[0] == '_': continue
        print " " * (4 - 1), k, v
    assert '(listfile)' in archive
    assert 0 in archive
    assert len(archive) == archive.files
    files = [x.strip() for x in archive['(listfile)']]
    files.extend(xrange(archive.files))
    for key in files: #sys.argv[2:] if sys.argv[2:] else xrange(archive.files):
        file = archive[key]
        print
        print " " * (4 - 1), repr(file)
        for k, v in file.__dict__.iteritems():
            #if k[0] == '_': continue
            print " " * (8 - 1), k, v
        
        a = str(file)
        
        b = iter(file).read()
        
        reader = iter(file)
        c = []
        while True:
            l = random.randrange(1, 10)
            d = reader.read(l)
            if not d: break
            assert len(d) <= l
            c.append(d)
        c = "".join(c)
        
        d = []
        reader.seek(0)
        for line in reader:
            d.append(line)
        d = "".join(d)
        
        assert a == b == c == d, map(hash, [a,b,c,d])
        assert len(a) == file.unpacked_size
        
        repr(iter(file))
        
        
        reader.seek(0)
        a = reader.readlines()
        
        reader.seek(0)
        b = list(reader)
        
        assert a == b