annotate pytouhou/utils/lzss.pyx @ 367:2674c789e0c3

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