/
quintet_comp.py
143 lines (117 loc) · 4.35 KB
/
quintet_comp.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
#!/usr/bin/env python3
#
# Quintet Compressor
# Osteoclave
# 2012-02-04
#
# The compression format is described in the decompressor.
#
# This code uses python-bitstring:
# https://pypi.org/project/bitstring/
import bitstring
# Define some useful constants.
SEARCH_LOG2 = 8
SEARCH_SIZE = 2 ** SEARCH_LOG2
LOOKAHEAD_LOG2 = 4
PASTCOPY_MIN_LENGTH = 2
PASTCOPY_MAX_LENGTH = PASTCOPY_MIN_LENGTH + (2 ** LOOKAHEAD_LOG2) - 1
BIT_PASTCOPY = 0
BIT_LITERAL = 1
def compress(inBytes):
# Prepare the memory buffer.
inBuffer = bytearray(SEARCH_SIZE + len(inBytes))
inBuffer[:SEARCH_SIZE] = [0x20] * SEARCH_SIZE
inBuffer[SEARCH_SIZE:] = inBytes
# Prepare for compression.
output = bitstring.BitArray()
output += bitstring.pack("uintle:16", len(inBytes))
currentIndex = SEARCH_SIZE
# Main compression loop.
while currentIndex < len(inBuffer):
bestIndex = 0
bestLength = 0
# Don't compare past the end of the memory buffer.
# Don't compare past the end of the lookahead buffer.
compareLimit = min(
len(inBuffer) - currentIndex,
PASTCOPY_MAX_LENGTH,
)
# Look for a match in the search buffer. (Brute force)
for i in range(SEARCH_SIZE):
# Compare the search buffer to the lookahead buffer.
# Count how many sequential bytes match (possibly zero).
currentLength = 0
for j in range(compareLimit):
if inBuffer[currentIndex - SEARCH_SIZE + i + j] == inBuffer[currentIndex + j]:
currentLength += 1
else:
break
# Keep track of the largest match we've seen.
if currentLength > bestLength:
bestIndex = currentIndex - SEARCH_SIZE + i
bestLength = currentLength
# If we've found a maximum-possible-length match, break.
if bestLength == compareLimit:
break
# Write the next block of compressed output.
if bestLength >= PASTCOPY_MIN_LENGTH:
# For some reason, the decompressor expects the pastcopy
# source values to be offset by 0xEF. I have no idea why.
bestIndex = (bestIndex + 0xEF) & 0xFF
output += bitstring.pack("uint:1", BIT_PASTCOPY)
output += bitstring.pack(
"uint:n=v", n = SEARCH_LOG2, v = bestIndex
)
output += bitstring.pack(
"uint:n=v", n = LOOKAHEAD_LOG2, v = bestLength - PASTCOPY_MIN_LENGTH
)
currentIndex += bestLength
else:
output += bitstring.pack("uint:1", BIT_LITERAL)
output += bitstring.pack("uint:8", inBuffer[currentIndex])
currentIndex += 1
# Return the compressed data.
return output.tobytes()
if __name__ == "__main__":
import os
import sys
# Helper function for writing to the output file. Opens a file for
# reading and writing, and if the file doesn't exist, creates it.
def touchopen(filename, *args, **kwargs):
fd = os.open(filename, os.O_RDWR | os.O_CREAT)
return os.fdopen(fd, *args, **kwargs)
# Check for incorrect usage.
argc = len(sys.argv)
if argc < 2 or argc > 4:
print("Usage: {0:s} <inFile> [outFile] [outOffset]".format(
sys.argv[0]
))
sys.exit(1)
# Copy the arguments.
inFile = sys.argv[1]
outFile = None
if argc == 3 or argc == 4:
outFile = sys.argv[2]
outOffset = 0
if argc == 4:
outOffset = int(sys.argv[3], 16)
# Read the input file.
with open(inFile, "rb") as inStream:
inBytes = bytearray(inStream.read())
# Compress the data.
outBytes = compress(inBytes)
# Write the compressed output, if appropriate.
if outFile is not None:
with touchopen(outFile, "r+b") as outStream:
outStream.seek(outOffset)
outStream.write(outBytes)
lastOffset = outStream.tell()
print("Last offset written, inclusive: {0:X}".format(
lastOffset - 1
))
# Report statistics on the data.
print("Uncompressed size: 0x{0:X} ({0:d}) bytes".format(len(inBytes)))
print("Compressed size: 0x{0:X} ({0:d}) bytes".format(len(outBytes)))
print("Ratio: {0:f}".format(len(outBytes) / len(inBytes)))
# Exit.
sys.exit(0)