Mercurial > touhou
comparison pytouhou/utils/lzss.pyx @ 252:b5c7369abd7c
Improve data reading perfs
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 22 Jan 2012 15:54:51 +0100 |
parents | pytouhou/utils/lzss.py@ab826bc29aa2 |
children | 2674c789e0c3 |
comparison
equal
deleted
inserted
replaced
251:4b549894ef6b | 252:b5c7369abd7c |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 from libc.stdlib cimport calloc, malloc, free | |
16 | |
17 | |
18 cpdef bytes decompress(object bitstream, | |
19 Py_ssize_t size, | |
20 unsigned int dictionary_size=0x2000, | |
21 unsigned int offset_size=13, | |
22 unsigned int length_size=4, | |
23 unsigned int minimum_match_length=3): | |
24 cdef unsigned int i, ptr, dictionary_head, offset, length | |
25 cdef unsigned char flag, byte, *out_data, *dictionary | |
26 cdef bytes _out_data | |
27 | |
28 out_data = <unsigned char*> malloc(size) | |
29 dictionary = <unsigned char*> calloc(dictionary_size, 1) | |
30 dictionary_head, ptr = 1, 0 | |
31 | |
32 while ptr < size: | |
33 flag = bitstream.read_bit() | |
34 if flag: | |
35 # The `flag` bit is set, indicating the upcoming chunk of data is a literal | |
36 # Add it to the uncompressed file, and store it in the dictionary | |
37 byte = bitstream.read(8) | |
38 dictionary[dictionary_head] = byte | |
39 dictionary_head = (dictionary_head + 1) % dictionary_size | |
40 out_data[ptr] = byte | |
41 ptr += 1 | |
42 else: | |
43 # The `flag` bit is not set, the upcoming chunk is a (offset, length) tuple | |
44 offset = bitstream.read(offset_size) | |
45 length = bitstream.read(length_size) + minimum_match_length | |
46 if offset == 0 and length == 0: | |
47 break | |
48 for i in range(offset, offset + length): | |
49 out_data[ptr % size] = dictionary[i % dictionary_size] | |
50 ptr += 1 | |
51 dictionary[dictionary_head] = dictionary[i % dictionary_size] | |
52 dictionary_head = (dictionary_head + 1) % dictionary_size | |
53 | |
54 _out_data = out_data[:size] | |
55 free(out_data) | |
56 free(dictionary) | |
57 return _out_data | |
58 |