-
Notifications
You must be signed in to change notification settings - Fork 1
/
compression.c
198 lines (166 loc) · 5.07 KB
/
compression.c
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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h>
#define BUFFER_SIZE 100000
int bitPtr = 0;
int bytePtr = 0;
int occurrence_length = 0;
void increment_bit_ptr() {
bitPtr++;
if (bitPtr == 8) {
bytePtr++;
bitPtr = 0;
}
}
char read_bit(char *buffer) {
char bit = buffer[bytePtr] >> (7 - bitPtr) & 0x01;
increment_bit_ptr();
return bit;
}
int read_bits(char *buffer, int bits) {
int value = 0;
for (int i = 0; i < bits; i++) {
value += read_bit(buffer) << (bits - i - 1);
}
return value;
}
void write_bit(char* buffer, int bit) {
buffer[bytePtr] += bit << (7 - bitPtr);
increment_bit_ptr();
}
void write_bits(char* buffer, int value, int bits) {
for (int i = 0; i < bits; i++) {
write_bit(buffer, (value >> (bits - 1 - i)) & 0x01);
}
}
static PyObject* fast_decompress(PyObject *self, PyObject *args) {
Py_buffer data;
char* buffer;
int bufferPtr = 0;
int offset_size, length_size;
PyObject *output;
int offset, length;
bitPtr = 0;
bytePtr = 0;
if (!PyArg_ParseTuple(args, "y*ii", &data, &offset_size, &length_size)) {
return NULL;
}
buffer = (char*) malloc(BUFFER_SIZE);
while (bufferPtr < BUFFER_SIZE) {
if (read_bit(data.buf) == 1) {
buffer[bufferPtr++] = (char) read_bits(data.buf, 8);
} else {
offset = read_bits(data.buf, offset_size);
length = read_bits(data.buf, length_size);
if (offset == 0 && length == 0) {
break;
}
length += 3;
for (int i = 0; i < length; i++) {
buffer[bufferPtr] = buffer[bufferPtr - offset];
bufferPtr++;
}
}
}
output = Py_BuildValue("y#", buffer, bufferPtr);
free(buffer);
return output;
}
static PyObject* fast_compression_length(PyObject *self, PyObject *args) {
Py_buffer data;
int offset_size, length_size;
bitPtr = 0;
bytePtr = 0;
int offset, length;
int bufferPtr = 0;
if (!PyArg_ParseTuple(args, "y*ii", &data, &offset_size, &length_size)) {
return NULL;
}
while (bufferPtr < BUFFER_SIZE) {
if (read_bit(data.buf) == 1) {
read_bits(data.buf, 8);
} else {
offset = read_bits(data.buf, offset_size);
length = read_bits(data.buf, length_size);
if (offset == 0 && length == 0) {
break;
}
}
}
return Py_BuildValue("i", bytePtr + (bitPtr > 0 ? 1 : 0));
}
int find_previous_occurrence(Py_buffer buffer, int ptr, int max_offset, int max_length) {
int found_offset = 0;
occurrence_length = 0;
for (int i = 1; i < min(buffer.len, max_offset); i++) {
int length = 0;
while(
(buffer.len > ptr + length) &&
(length + 1 < max_length) &&
(ptr + length - i >= 0) &&
(((char*)buffer.buf)[ptr + length] == ((char*)buffer.buf)[ptr + length - i])
) {
length++;
}
if (length >= 3 && length > occurrence_length) {
occurrence_length = length;
found_offset = i;
}
}
return found_offset;
}
static PyObject* fast_compress(PyObject *self, PyObject *args) {
Py_buffer data;
int offset_size, length_size;
bitPtr = 0;
bytePtr = 0;
int bufferPtr = 0;
char* buffer;
PyObject *output;
if (!PyArg_ParseTuple(args, "y*ii", &data, &offset_size, &length_size)) {
return NULL;
}
buffer = malloc(5120);
for (int i = 0; i < 5120; i++) {
buffer[i] = 0;
}
int max_offset = (1 << offset_size);
int max_length = (1 << length_size) + 3;
while (bufferPtr < data.len) {
int offset = find_previous_occurrence(data, bufferPtr, max_offset, max_length);
if (offset > 0) {
write_bit(buffer, 0);
write_bits(buffer, offset, offset_size);
write_bits(buffer, occurrence_length - 3, length_size);
bufferPtr += occurrence_length;
} else {
write_bit(buffer, 1);
int byte = ((char*)data.buf)[bufferPtr];
write_bits(buffer, byte, 8);
bufferPtr++;
}
}
write_bit(buffer, 0);
write_bits(buffer, 0, offset_size);
write_bits(buffer, 0, length_size);
output = Py_BuildValue("y#", buffer, bytePtr + min(bitPtr, 1));
free(buffer);
return output;
}
static PyMethodDef CompressionMethods[] = {
{"fast_decompress", fast_decompress, METH_VARARGS, "Decompress M.C. Kids data."},
{"fast_compression_length", fast_compression_length, METH_VARARGS, "Check compression length for M.C. Kids data."},
{"fast_compress", fast_compress, METH_VARARGS, "Compress M.C. Kids data."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef compression_ext = {
PyModuleDef_HEAD_INIT,
"fast_compression",
"M.C. Kids compression and decompression written in C.",
-1,
CompressionMethods
};
PyMODINIT_FUNC PyInit_fast_compression(void) {
Py_Initialize();
return PyModule_Create(&compression_ext);
}