forked from jaredsuttles/python-fastlz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastlz.c
162 lines (128 loc) · 3.92 KB
/
fastlz.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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "fastlz/fastlz.h"
#ifdef _MSC_VER
typedef unsigned __int32 uint32_t;
#else
#include <stdint.h>
#endif
PyObject* FastlzError;
static PyObject *
compress(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *result;
const char *input;
char *output;
Py_ssize_t level = 0, input_len;
uint32_t output_len;
static char *arglist[] = {"string", "level", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i", arglist, &input,
&input_len, &level))
return NULL;
if (level == 0) {
if (input_len < 65536)
level = 1;
else
level = 2;
} else if (level != 1 && level != 2) {
PyErr_SetString(PyExc_ValueError, "level must be either 1 or 2");
return NULL;
}
output_len = sizeof(uint32_t) + (uint32_t)((input_len * 1.05) + 2);
output = (char *) malloc(output_len);
if (output == NULL)
return PyErr_NoMemory();
memcpy(output, &input_len, sizeof(uint32_t));
output_len = fastlz_compress_level(level, input, input_len,
output + sizeof(uint32_t));
if (output_len == 0 && input_len != 0) {
free(output);
PyErr_SetString(FastlzError, "could not compress");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
result = Py_BuildValue("y#", output, output_len + sizeof(uint32_t));
#else
result = Py_BuildValue("s#", output, output_len + sizeof(uint32_t));
#endif
free(output);
return result;
}
static PyObject *
decompress(PyObject *self, PyObject *args)
{
PyObject *result;
const char *input;
Py_ssize_t input_len;
char *output;
uint32_t output_len, decompressed_len;
if (!PyArg_ParseTuple(args, "s#", &input, &input_len))
return NULL;
if (input_len < (Py_ssize_t)sizeof(uint32_t)) {
PyErr_SetString(FastlzError, "invalid input");
return NULL;
}
memcpy(&output_len, input, sizeof(uint32_t));
if (output_len / 256.0 > input_len) {
PyErr_SetString(FastlzError, "invalid input");
return NULL;
}
output = (char *) malloc(output_len + 1);
if (output == NULL)
return PyErr_NoMemory();
input_len -= sizeof(uint32_t);
input += sizeof(uint32_t);
decompressed_len = (uint32_t)fastlz_decompress(input, input_len, output,
output_len);
if (decompressed_len != output_len) {
free(output);
PyErr_SetString(FastlzError, "could not decompress");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
result = Py_BuildValue("y#", output, output_len);
#else
result = Py_BuildValue("s#", output, output_len);
#endif
free(output);
return result;
}
static PyMethodDef module_methods[] = {
{"compress", (PyCFunction)compress, METH_VARARGS|METH_KEYWORDS,
PyDoc_STR("Compress a string. Optionally provide a compression level.")},
{"decompress", (PyCFunction)decompress, METH_VARARGS,
PyDoc_STR("Decompress a string.")},
{NULL, NULL, 0, NULL}
};
PyDoc_STRVAR(module_doc,
"Python wrapper for FastLZ, a lightning-fast lossless compression library.");
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"fastlz",
module_doc,
-1,
module_methods
};
PyMODINIT_FUNC
PyInit_fastlz(void)
#else
PyMODINIT_FUNC
initfastlz(void)
#endif
{
PyObject *m, *d;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&module_def);
#else
m = Py_InitModule3("fastlz", module_methods, module_doc);
#endif
PyModule_AddObject(m, "__version__", Py_BuildValue("s", "0.0.2"));
PyModule_AddObject(m, "__author__", Py_BuildValue("s", "Jared Suttles"));
d = PyModule_GetDict(m);
FastlzError = PyErr_NewException("fastlz.FastlzError", NULL, NULL);
PyDict_SetItemString(d, "FastlzError", FastlzError);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}