summaryrefslogtreecommitdiff
path: root/lib/compression/pycompression.c
blob: 3be3620b1cf145c4837abbd3b55a45719d1a4eb4 (plain)
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
/*
   Samba Unix SMB/CIFS implementation.

   Python bindings for compression functions.

   Copyright (C) Petr Viktorin 2015
   Copyright (C) Douglas Bagnall 2022

     ** NOTE! The following LGPL license applies to the talloc
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include <talloc.h>
#include <Python.h>
#include "lzxpress.h"
#include "lzxpress_huffman.h"

/* CompressionError is filled out in module init */
static PyObject *CompressionError = NULL;

static PyObject *plain_compress(PyObject *mod, PyObject *args)
{
	uint8_t *src = NULL;
	Py_ssize_t src_len;
	char *dest = NULL;
	Py_ssize_t dest_len;
	PyObject *dest_obj = NULL;
	size_t alloc_len;
	int ret;

	if (!PyArg_ParseTuple(args, "s#", &src, &src_len)) {
		return NULL;
	}

	/*
	 * 9/8 + 4 is the worst case growth, but we add room.
	 *
	 * alloc_len can't overflow as src_len is ssize_t while alloc_len is
	 * size_t.
	 */
	alloc_len = src_len + src_len / 8 + 500;

	dest_obj = PyBytes_FromStringAndSize(NULL, alloc_len);
	if (dest_obj == NULL) {
		return NULL;
	}
	dest = PyBytes_AS_STRING(dest_obj);

	dest_len = lzxpress_compress(src,
				     src_len,
				     (uint8_t *)dest,
				     alloc_len);
	if (dest_len < 0) {
		PyErr_SetString(CompressionError, "unable to compress data");
		Py_DECREF(dest_obj);
		return NULL;
	}

	ret = _PyBytes_Resize(&dest_obj, dest_len);
	if (ret != 0) {
		/*
		 * Don't try to free dest_obj, as we're in deep MemoryError
		 * territory here.
		 */
		return NULL;
	}
	return dest_obj;
}


static PyObject *plain_decompress(PyObject *mod, PyObject *args)
{
	uint8_t *src = NULL;
	Py_ssize_t src_len;
	char *dest = NULL;
	Py_ssize_t dest_len;
	PyObject *dest_obj = NULL;
	Py_ssize_t alloc_len = 0;
	Py_ssize_t given_len = 0;
	int ret;

	if (!PyArg_ParseTuple(args, "s#|n", &src, &src_len, &given_len)) {
		return NULL;
	}
	if (given_len != 0) {
		/*
		 * With plain decompression, we don't *need* the exact output
		 * size (as we do with LZ77+Huffman), but it certainly helps
		 * when guessing the size.
		 */
		alloc_len = given_len;
	} else if (src_len > UINT32_MAX) {
		/*
		 * The underlying decompress function will reject this, but by
		 * checking here we can give a better message and be clearer
		 * about overflow risks.
		 *
		 * Note, the limit is actually the smallest of UINT32_MAX and
		 * SSIZE_MAX, but src_len is ssize_t so it already can't
		 * exceed that.
		 */
		PyErr_Format(CompressionError,
			     "The maximum size for compressed data is 4GB "
			     "cannot decompress %zu bytes.", src_len);
	} else {
		/*
		 * The data can expand massively (though not beyond the
		 * 4GB limit) so we guess a big number for small inputs
		 * (we expect small inputs), and a relatively conservative
		 * number for big inputs.
		 */
		if (src_len <= 3333333) {
			alloc_len = 10000000;
		} else if (src_len > UINT32_MAX / 3) {
			alloc_len = UINT32_MAX;
		} else {
			alloc_len = src_len * 3;
		}
	}

	dest_obj = PyBytes_FromStringAndSize(NULL, alloc_len);
	if (dest_obj == NULL) {
		return NULL;
	}
	dest = PyBytes_AS_STRING(dest_obj);

	dest_len = lzxpress_decompress(src,
				       src_len,
				       (uint8_t *)dest,
				       alloc_len);
	if (dest_len < 0) {
		if (alloc_len == given_len) {
			PyErr_Format(CompressionError,
				     "unable to decompress data into a buffer "
				     "of %zd bytes.", alloc_len);
		} else {
			PyErr_Format(CompressionError,
				     "unable to decompress data into a buffer "
				     "of %zd bytes. If you know the length, "
				     "supply it as the second argument.",
				     alloc_len);
		}
		Py_DECREF(dest_obj);
		return NULL;
	}

	ret = _PyBytes_Resize(&dest_obj, dest_len);
	if (ret != 0) {
		/*
		 * Don't try to free dest_obj, as we're in deep MemoryError
		 * territory here.
		 */
		return NULL;
	}
	return dest_obj;
}



static PyObject *huffman_compress(PyObject *mod, PyObject *args)
{
	uint8_t *src = NULL;
	Py_ssize_t src_len;
	char *dest = NULL;
	Py_ssize_t dest_len;
	PyObject *dest_obj = NULL;
	size_t alloc_len;
	int ret;
	struct lzxhuff_compressor_mem cmp_mem;

	if (!PyArg_ParseTuple(args, "s#", &src, &src_len)) {
		return NULL;
	}
	/*
	 * worst case is roughly 256 per 64k or less.
	 *
	 * alloc_len won't overflow as src_len is ssize_t while alloc_len is
	 * size_t.
	 */
	alloc_len = src_len + src_len / 8 + 500;

	dest_obj = PyBytes_FromStringAndSize(NULL, alloc_len);
	if (dest_obj == NULL) {
		return NULL;
	}
	dest = PyBytes_AS_STRING(dest_obj);

	dest_len = lzxpress_huffman_compress(&cmp_mem,
					     src,
					     src_len,
					     (uint8_t *)dest,
					     alloc_len);
	if (dest_len < 0) {
		PyErr_SetString(CompressionError, "unable to compress data");
		Py_DECREF(dest_obj);
		return NULL;
	}

	ret = _PyBytes_Resize(&dest_obj, dest_len);
	if (ret != 0) {
		return NULL;
	}
	return dest_obj;
}


static PyObject *huffman_decompress(PyObject *mod, PyObject *args)
{
	uint8_t *src = NULL;
	Py_ssize_t src_len;
	char *dest = NULL;
	Py_ssize_t dest_len;
	PyObject *dest_obj = NULL;
	Py_ssize_t given_len = 0;
	/*
	 * Here it is always necessary to supply the exact length.
	 */

	if (!PyArg_ParseTuple(args, "s#n", &src, &src_len, &given_len)) {
		return NULL;
	}

	dest_obj = PyBytes_FromStringAndSize(NULL, given_len);
	if (dest_obj == NULL) {
		return NULL;
	}
	dest = PyBytes_AS_STRING(dest_obj);

	dest_len = lzxpress_huffman_decompress(src,
					       src_len,
					       (uint8_t *)dest,
					       given_len);
	if (dest_len != given_len) {
		PyErr_Format(CompressionError,
			     "unable to decompress data into a %zd bytes.",
			     given_len);
		Py_DECREF(dest_obj);
		return NULL;
	}
	/* no resize here */
	return dest_obj;
}


static PyMethodDef mod_methods[] = {
	{ "plain_compress", (PyCFunction)plain_compress, METH_VARARGS,
		"compress bytes using lzxpress plain compression"},
	{ "plain_decompress", (PyCFunction)plain_decompress, METH_VARARGS,
		"decompress lzxpress plain compressed bytes"},
	{ "huffman_compress", (PyCFunction)huffman_compress, METH_VARARGS,
		"compress bytes using lzxpress plain compression"},
	{ "huffman_decompress", (PyCFunction)huffman_decompress, METH_VARARGS,
		"decompress lzxpress plain compressed bytes"},
	{0}
};


#define MODULE_DOC PyDoc_STR("LZXpress compression/decompression bindings")

static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    .m_name = "compression",
    .m_doc = MODULE_DOC,
    .m_size = -1,
    .m_methods = mod_methods,
};


static PyObject *module_init(void)
{
	PyObject *m = PyModule_Create(&moduledef);
	if (m == NULL) {
		return NULL;
	}

	CompressionError = PyErr_NewException(
		"compression.CompressionError",
		PyExc_Exception,
		NULL);
	PyModule_AddObject(m, "CompressionError", CompressionError);

	return m;
}

PyMODINIT_FUNC PyInit_compression(void);
PyMODINIT_FUNC PyInit_compression(void)
{
	return module_init();
}