Mercurial > touhou
comparison pytouhou/utils/lzss.pyx @ 368:71cd4461bb7f
Minor optimizations
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Wed, 11 Jul 2012 15:19:44 +0200 |
parents | 2674c789e0c3 |
children | 1b532e7dd521 |
comparison
equal
deleted
inserted
replaced
367:2674c789e0c3 | 368:71cd4461bb7f |
---|---|
20 unsigned int dictionary_size=0x2000, | 20 unsigned int dictionary_size=0x2000, |
21 unsigned int offset_size=13, | 21 unsigned int offset_size=13, |
22 unsigned int length_size=4, | 22 unsigned int length_size=4, |
23 unsigned int minimum_match_length=3): | 23 unsigned int minimum_match_length=3): |
24 cdef unsigned int i, ptr, dictionary_head, offset, length | 24 cdef unsigned int i, ptr, dictionary_head, offset, length |
25 cdef unsigned char byte, *out_data, *dictionary | 25 cdef unsigned char byte |
26 cdef char *out_data, *dictionary | |
26 cdef bytes _out_data | 27 cdef bytes _out_data |
27 | 28 |
28 out_data = <unsigned char*> malloc(size) | 29 out_data = <char*> malloc(size) |
29 dictionary = <unsigned char*> calloc(dictionary_size, 1) | 30 dictionary = <char*> calloc(dictionary_size, 1) |
30 dictionary_head, ptr = 1, 0 | 31 dictionary_head, ptr = 1, 0 |
31 | 32 |
32 while ptr < size: | 33 while ptr < size: |
33 if bitstream.read_bit(): | 34 if bitstream.read_bit(): |
34 # The `flag` bit is set, indicating the upcoming chunk of data is a literal | 35 # The `flag` bit is set, indicating the upcoming chunk of data is a literal |
40 ptr += 1 | 41 ptr += 1 |
41 else: | 42 else: |
42 # The `flag` bit is not set, the upcoming chunk is a (offset, length) tuple | 43 # The `flag` bit is not set, the upcoming chunk is a (offset, length) tuple |
43 offset = bitstream.read(offset_size) | 44 offset = bitstream.read(offset_size) |
44 length = bitstream.read(length_size) + minimum_match_length | 45 length = bitstream.read(length_size) + minimum_match_length |
46 if ptr + length > size: | |
47 raise Exception | |
45 if offset == 0 and length == 0: | 48 if offset == 0 and length == 0: |
46 break | 49 break |
47 for i in range(offset, offset + length): | 50 for i in range(offset, offset + length): |
48 out_data[ptr % size] = dictionary[i % dictionary_size] | 51 out_data[ptr] = dictionary[i % dictionary_size] |
49 ptr += 1 | |
50 dictionary[dictionary_head] = dictionary[i % dictionary_size] | 52 dictionary[dictionary_head] = dictionary[i % dictionary_size] |
51 dictionary_head = (dictionary_head + 1) % dictionary_size | 53 dictionary_head = (dictionary_head + 1) % dictionary_size |
54 ptr += 1 | |
52 | 55 |
53 _out_data = out_data[:size] | 56 _out_data = out_data[:size] |
54 free(out_data) | 57 free(out_data) |
55 free(dictionary) | 58 free(dictionary) |
56 return _out_data | 59 return _out_data |