Debian Codegraph @

snappy/snappymodule.cc

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 Copyright (c) 2011, Andres Moreira <[email protected]>
3 2011, Felipe Cruz <[email protected]>
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the authors nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ANDRES MOREIRA BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "Python.h"
29 #include <string.h>
30 #include <stdio.h>
31 #include <snappy-c.h>
32 #include "crc32c.h"
33
34 #define MODULE_VERSION "0.4.1"
35 #define RESIZE_TOLERATION 0.75
36
37 struct module_state {
38 PyObject *error;
39 };
40
41 #if PY_MAJOR_VERSION >= 3
42 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
43 #else
44 #define GETSTATE(m) (&_state)
45 static struct module_state _state;
46 #endif
47
48
49 /* if support for Python 2.5 is dropped the bytesobject.h will do this for us */
50 #if PY_MAJOR_VERSION < 3
51 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
52 #define _PyBytes_Resize _PyString_Resize
53 #define PyBytes_AS_STRING PyString_AS_STRING
54 #endif
55
56 static PyObject *SnappyCompressError,
57 *SnappyUncompressError,
58 *SnappyInvalidCompressedInputError,
59 *SnappyCompressedLengthError;
60
61 static inline PyObject *
62 maybe_resize(PyObject *str, size_t expected_size, size_t actual_size)
63 {
64 // Tolerate up to 25% slop, to reduce the likelihood of
65 // reallocation and copying.
66 if (actual_size != expected_size) {
67 if (actual_size < expected_size * RESIZE_TOLERATION) {
68 _PyBytes_Resize(&str, actual_size);
69 return str;
70 }
71 Py_SIZE(str) = actual_size;
72 }
73 return str;
74 }
75
76 static const char *
77 snappy_strerror(snappy_status status)
78 {
79 switch (status) {
80 case SNAPPY_OK:
81 return "no error";
82 case SNAPPY_INVALID_INPUT:
83 return "invalid input";
84 case SNAPPY_BUFFER_TOO_SMALL:
85 return "buffer too small";
86 default:
87 return "unknown error";
88 }
89 }
90
91 static PyObject *
92 snappy__compress(PyObject *self, PyObject *args)
93 {
94 Py_buffer input;
95 size_t compressed_size, actual_size;
96 PyObject * result;
97 snappy_status status;
98
99 #if PY_MAJOR_VERSION >= 3
100 if (!PyArg_ParseTuple(args, "y*", &input))
101 #else
102 if (!PyArg_ParseTuple(args, "s*", &input))
103 #endif
104 return NULL;
105
106 // Ask for the max size of the compressed object.
107 compressed_size = snappy_max_compressed_length(input.len);
108
109 // Make snappy compression
110 result = PyBytes_FromStringAndSize(NULL, compressed_size);
111 if (result) {
112 actual_size = compressed_size;
113 Py_BEGIN_ALLOW_THREADS
114 status = snappy_compress((const char *) input.buf, input.len,
115 PyBytes_AS_STRING(result), &actual_size);
116 Py_END_ALLOW_THREADS
117 PyBuffer_Release(&input);
118 if (status == SNAPPY_OK) {
119 return maybe_resize(result, compressed_size, actual_size);
120 }
121 else {
122 Py_DECREF(result);
123 }
124 PyErr_Format(SnappyCompressError,
125 "Error while compressing: %s", snappy_strerror(status));
126 }
127 else {
128 PyBuffer_Release(&input);
129 PyErr_Format(SnappyCompressError,
130 "Error while compressing: unable to acquire output string");
131 }
132 return NULL;
133 }
134
135 static PyObject *
136 snappy__uncompress(PyObject *self, PyObject *args)
137 {
138 Py_buffer compressed;
139 size_t uncomp_size, actual_size;
140 PyObject * result;
141 snappy_status status;
142
143 #if PY_MAJOR_VERSION >=3
144 if (!PyArg_ParseTuple(args, "y*", &compressed))
145 #else
146 if (!PyArg_ParseTuple(args, "s*", &compressed))
147 #endif
148 return NULL;
149
150 status = snappy_uncompressed_length((const char *) compressed.buf, compressed.len,
151 &uncomp_size);
152 if (status != SNAPPY_OK) {
153 PyBuffer_Release(&compressed);
154 PyErr_SetString(SnappyCompressedLengthError,
155 "Can not calculate uncompressed length");
156 return NULL;
157 }
158
159 result = PyBytes_FromStringAndSize(NULL, uncomp_size);
160 if (result) {
161 actual_size = uncomp_size;
162 Py_BEGIN_ALLOW_THREADS
163 status = snappy_uncompress((const char *) compressed.buf, compressed.len,
164 PyBytes_AS_STRING(result), &actual_size);
165 Py_END_ALLOW_THREADS
166 PyBuffer_Release(&compressed);
167 if (SNAPPY_OK == status) {
168 return maybe_resize(result, uncomp_size, actual_size);
169 }
170 else {
171 Py_DECREF(result);
172 PyErr_Format(SnappyUncompressError,
173 "Error while decompressing: %s", snappy_strerror(status));
174 }
175 }
176 else {
177 PyBuffer_Release(&compressed);
178 }
179 return NULL;
180 }
181
182
183 static PyObject *
184 snappy__is_valid_compressed_buffer(PyObject *self, PyObject *args)
185 {
186 const char * compressed;
187 int comp_size;
188 snappy_status status;
189
190 #if PY_MAJOR_VERSION >=3
191 if (!PyArg_ParseTuple(args, "y#", &compressed, &comp_size))
192 #else
193 if (!PyArg_ParseTuple(args, "s#", &compressed, &comp_size))
194 #endif
195 return NULL;
196
197 status = snappy_validate_compressed_buffer(compressed, comp_size);
198 if (status == SNAPPY_OK)
199 Py_RETURN_TRUE;
200 Py_RETURN_FALSE;
201 }
202
203 static PyObject *
204 snappy__crc32c(PyObject *self, PyObject *args)
205 {
206 const unsigned char * input;
207 int input_size;
208
209 #if PY_MAJOR_VERSION >= 3
210 if (!PyArg_ParseTuple(args, "y#", &input, &input_size))
211 #else
212 if (!PyArg_ParseTuple(args, "s#", &input, &input_size))
213 #endif
214 return NULL;
215
216 return PyLong_FromUnsignedLong(
217 crc_finalize(crc_update(crc_init(), input, input_size)));
218 }
219
220 static PyMethodDef snappy_methods[] = {
221 {"compress", snappy__compress, METH_VARARGS,
222 "Compress a string using the snappy library."},
223 {"uncompress", snappy__uncompress, METH_VARARGS,
224 "Uncompress a string compressed with the snappy library."},
225 {"decompress", snappy__uncompress, METH_VARARGS,
226 "Alias to Uncompress method, to be compatible with zlib."},
227 {"isValidCompressed", snappy__is_valid_compressed_buffer, METH_VARARGS,
228 "Returns True if the compressed buffer is valid, False otherwise"},
229 {"_crc32c", snappy__crc32c, METH_VARARGS,
230 "Generate an RFC3720, section 12.1 CRC-32C"},
231 {NULL, NULL, 0, NULL} /* Sentinel */
232 };
233
234 #if PY_MAJOR_VERSION >= 3
235
236 static int snappy_traverse(PyObject *m, visitproc visit, void *arg) {
237 Py_VISIT(GETSTATE(m)->error);
238 return 0;
239 }
240
241 static int snappy_clear(PyObject *m) {
242 Py_CLEAR(GETSTATE(m)->error);
243 return 0;
244 }
245
246
247 static struct PyModuleDef moduledef = {
248 PyModuleDef_HEAD_INIT,
249 "_snappy",
250 NULL,
251 sizeof(struct module_state),
252 snappy_methods,
253 NULL,
254 snappy_traverse,
255 snappy_clear,
256 NULL
257 };
258
259
260 #define INITERROR return NULL
261
262 PyMODINIT_FUNC
263 PyInit__snappy(void)
264
265 #else
266 #define INITERROR return
267
268 PyMODINIT_FUNC
269 init_snappy(void)
270 #endif
271 {
272 PyObject *m;
273
274 #if PY_MAJOR_VERSION >= 3
275 m = PyModule_Create(&moduledef);
276 #else
277 m = Py_InitModule("_snappy", snappy_methods);
278 #endif
279
280 if (m == NULL)
281 INITERROR;
282
283 SnappyCompressError = PyErr_NewException((char*)"snappy.CompressError",
284 NULL, NULL);
285 SnappyUncompressError = PyErr_NewException((char*)"snappy.UncompressError",
286 NULL, NULL);
287 SnappyInvalidCompressedInputError = PyErr_NewException(
288 (char*)"snappy.InvalidCompressedInputError", NULL, NULL);
289 SnappyCompressedLengthError = PyErr_NewException(
290 (char*)"snappy.CompressedLengthError", NULL, NULL);
291
292 Py_INCREF(SnappyCompressError);
293 Py_INCREF(SnappyUncompressError);
294 Py_INCREF(SnappyInvalidCompressedInputError);
295 Py_INCREF(SnappyCompressedLengthError);
296
297 PyModule_AddObject(m, "CompressError", SnappyCompressError);
298 PyModule_AddObject(m, "UncompressError", SnappyUncompressError);
299 PyModule_AddObject(m, "InvalidCompressedInputError",
300 SnappyInvalidCompressedInputError);
301 PyModule_AddObject(m, "CompressedLengthError", SnappyCompressedLengthError);
302
303 /* Version = MODULE_VERSION */
304 if (PyModule_AddStringConstant(m, "__version__", MODULE_VERSION))
305 INITERROR;
306
307 #if PY_MAJOR_VERSION >= 3
308 return m;
309 #endif
310 }