-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquicklzpy.c
executable file
·319 lines (275 loc) · 9.49 KB
/
quicklzpy.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
This is a python module to access to compression and decompression
in the QuickLZ library. This module only provides a few entry points from
Python:
The main functions:
qlz_compress()
qlz_decompress()
qlz_size_decompressed()
qlz_size_compressed()
The state object:
QLZStateCompress
**/
#include <Python.h>
#include "quicklz.h"
#if QLZ_STREAMING_BUFFER == 0
#error Define QLZ_STREAMING_BUFFER to a non-zero value for this module
#endif
PyTypeObject qlz_state_compress_Type;
PyTypeObject qlz_state_decompress_Type;
typedef struct {
PyObject_HEAD
qlz_state_compress* value;
} qlz_state_compress_;
typedef struct {
PyObject_HEAD
qlz_state_decompress* value;
} qlz_state_decompress_;
PyObject *
qlz_state_compress_NEW(void)
{
qlz_state_compress_ *object = PyObject_NEW(qlz_state_compress_, &qlz_state_compress_Type);
if (object != NULL) {
object->value = (qlz_state_compress *)malloc(sizeof(qlz_state_compress));
memset(object->value, 0, sizeof(qlz_state_compress));
}
return (PyObject *)object;
}
void
qlz_state_compress_dealloc(PyObject *self)
{
free(((qlz_state_compress_*)self)->value);
PyMem_DEL(self);
}
/* This is the Python constructor */
PyObject *
qlz_state_compress_new(PyObject * self, PyObject * args)
{
return qlz_state_compress_NEW();
}
PyObject *qlz_py_getattr(PyObject *self, char * attrname)
{
PyErr_SetString(PyExc_AttributeError, attrname);
return NULL;
}
PyObject *
qlz_state_decompress_NEW(void)
{
qlz_state_decompress_ *object = PyObject_NEW(qlz_state_decompress_, &qlz_state_decompress_Type);
if (object != NULL) {
object->value = (qlz_state_decompress *)malloc(sizeof(qlz_state_decompress));
memset(object->value, 0, sizeof(qlz_state_decompress));
}
return (PyObject *)object;
}
void
qlz_state_decompress_dealloc(PyObject *self)
{
free(((qlz_state_decompress_*)self)->value);
PyMem_DEL(self);
}
/* This is the Python constructor */
PyObject *
qlz_state_decompress_new(PyObject * self, PyObject * args)
{
return qlz_state_decompress_NEW();
}
PyObject *qlz_getattr(PyObject *self, char * attrname)
{
PyErr_SetString(PyExc_AttributeError, attrname);
return NULL;
}
int qlz_py_setattr(PyObject *self, char *attrname, PyObject *value)
{
PyErr_SetString(PyExc_AttributeError, attrname);
return -1;
}
int qlz_compare(PyObject *self, PyObject *other)
{
// 'is a' comparison
return self == other;
}
int qlz_c_print(PyObject *self, FILE *fp, int flags)
{
fprintf(fp, "QLZStateCompress(%ld)", (long)self);
return 0;
}
PyObject *qlz_c_str(PyObject *self)
{
char buf[100];
sprintf(buf, "QLZStateCompress(%ld)", (long)self);
return PyString_FromString(buf);
}
int qlz_d_print(PyObject *self, FILE *fp, int flags)
{
fprintf(fp, "QLZStateDecompress(%ld)", (long)self);
return 0;
}
PyObject *qlz_d_str(PyObject *self)
{
char buf[100];
sprintf(buf, "QLZStateDecompress(%ld)", (long)self);
return PyString_FromString(buf);
}
long
qlz_hash(PyObject *self)
{
return (long)self;
}
/**
* The qlz_state_compress python object
*/
PyTypeObject qlz_state_compress_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"QLZStateCompress", /* char *tp_name; */
sizeof(qlz_state_compress_),/* int tp_basicsize; */
0, /* int tp_itemsize; /* not used much */
qlz_state_compress_dealloc, /* destructor tp_dealloc; */
qlz_c_print, /* printfunc tp_print; */
qlz_py_getattr, /* getattrfunc tp_getattr; /* __getattr__ */
qlz_py_setattr, /* setattrfunc tp_setattr; /* __setattr__ */
qlz_compare, /* cmpfunc tp_compare; /* __cmp__ */
qlz_c_str, /* reprfunc tp_repr; /* __repr__ */
0, /* PyNumberMethods *tp_as_number; */
0, /* PySequenceMethods *tp_as_sequence; */
0, /* PyMappingMethods *tp_as_mapping; */
qlz_hash, /* hashfunc tp_hash; /* __hash__ */
0, /* ternaryfunc tp_call; /* __call__ */
qlz_c_str, /* reprfunc tp_str; /* __str__ */
};
/**
* The qlz_state_compress python object
*/
PyTypeObject qlz_state_decompress_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"QLZStateDecompress", /* char *tp_name; */
sizeof(qlz_state_decompress_),/* int tp_basicsize; */
0, /* int tp_itemsize; /* not used much */
qlz_state_decompress_dealloc,/* destructor tp_dealloc; */
qlz_d_print, /* printfunc tp_print; */
qlz_py_getattr, /* getattrfunc tp_getattr; /* __getattr__ */
qlz_py_setattr, /* setattrfunc tp_setattr; /* __setattr__ */
qlz_compare, /* cmpfunc tp_compare; /* __cmp__ */
qlz_d_str, /* reprfunc tp_repr; /* __repr__ */
0, /* PyNumberMethods *tp_as_number; */
0, /* PySequenceMethods *tp_as_sequence; */
0, /* PyMappingMethods *tp_as_mapping; */
qlz_hash, /* hashfunc tp_hash; /* __hash__ */
0, /* ternaryfunc tp_call; /* __call__ */
qlz_d_str, /* reprfunc tp_str; /* __str__ */
};
PyObject *qlz_size_decompressed_py(PyObject *self, PyObject *args)
{
PyObject *result = NULL;
char* buffer;
int buffer_length;
if (PyArg_ParseTuple(args, "s#", &buffer, &buffer_length)) {
result = Py_BuildValue("i", qlz_size_decompressed(buffer));
} /* otherwise there is an error,
* the exception already raised by PyArg_ParseTuple, and NULL is
* returned.
*/
return result;
}
PyObject *qlz_size_compressed_py(PyObject *self, PyObject *args)
{
PyObject *result = NULL;
char* buffer;
int buffer_length;
if (PyArg_ParseTuple(args, "s#", &buffer, &buffer_length)) {
result = Py_BuildValue("i", qlz_size_compressed(buffer));
} /* otherwise there is an error,
* the exception already raised by PyArg_ParseTuple, and NULL is
* returned.
*/
return result;
}
PyObject *qlz_compress_py(PyObject *self, PyObject *args)
{
PyObject *result = NULL;
PyObject *state = NULL;
char* buffer;
char* compressed_buffer;
int buffer_length;
int size_compressed;
if (PyArg_ParseTuple(args, "s#O!",
&buffer, &buffer_length,
&qlz_state_compress_Type, &state))
{
compressed_buffer = (char*)malloc(buffer_length+400);
size_compressed = qlz_compress(buffer, compressed_buffer,
buffer_length,
((qlz_state_compress_*)state)->value);
result = Py_BuildValue("s#", compressed_buffer, size_compressed);
free(compressed_buffer);
} /* otherwise there is an error,
* the exception already raised by PyArg_ParseTuple, and NULL is
* returned.
*/
return result;
}
PyObject *qlz_decompress_py(PyObject *self, PyObject *args)
{
PyObject *result = NULL;
PyObject *state = NULL;
char* buffer;
char* decompressed_buffer;
int buffer_length;
int size_decompressed;
if (PyArg_ParseTuple(args, "s#O!",
&buffer, &buffer_length,
&qlz_state_decompress_Type, &state))
{
size_decompressed = qlz_size_decompressed(buffer);
decompressed_buffer = (char*)malloc(size_decompressed);
qlz_decompress(buffer, decompressed_buffer,
((qlz_state_decompress_*)state)->value);
result = Py_BuildValue("s#", decompressed_buffer, size_decompressed);
free(decompressed_buffer);
} /* otherwise there is an error,
* the exception already raised by PyArg_ParseTuple, and NULL is
* returned.
*/
return result;
}
PyMethodDef methods[] = {
{"QLZStateCompress", qlz_state_compress_new, METH_VARARGS,
"An internal object for tracking streaming compression.\n"},
{"QLZStateDecompress", qlz_state_decompress_new, METH_VARARGS,
"An internal object for tracking streaming decompression.\n"},
{"qlz_size_decompressed", qlz_size_decompressed_py, METH_VARARGS,
"qlz_size_decompressed(compressed_data)\n"
"\n"
"How many bytes would the uncompressed data in this chunk be?\n"},
{"qlz_size_compressed", qlz_size_compressed_py, METH_VARARGS,
"qlz_size_compressed(compressed_data)\n"
"\n"
"How many bytes is the compressed data in this chunk?\n"},
{"qlz_compress", qlz_compress_py, METH_VARARGS,
"qlz_compress(raw_data, state)\n"
"Compress a chunk of data using QuickLZ.\n"
"\n"
"If the same state object is used to compress data sequentially,\n"
"then chunks must also be decompressed using a state object in the same\n"
"sequence.\n"
"\n"
"@param raw_data: string-like\n"
"@param state: a QLZStateCompress object.\n"},
{"qlz_decompress", qlz_decompress_py, METH_VARARGS,
"qlz_decompress(compressed_chunk, state)\n"
"Decompress a chunk of data using QuickLZ."
"\n"
"If the same state object is used to compress data sequentially,\n"
"then chunks must also be decompressed using a state object in the same\n"
"sequence.\n"
"\n"
"@param compressed_chunk: string-like\n"
"@param state: a QLZStateDecompress object.\n"},
{NULL, NULL},
};
void initquicklz(void)
{
(void)Py_InitModule("quicklz", methods);
}