| 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 "pythoncapi_compat.h" |
| 30 |
#include <string.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <snappy-c.h> |
| 33 |
#include "crc32c.h" |
| 34 |
|
| 35 |
#define MODULE_VERSION "0.4.1" |
| 36 |
#define RESIZE_TOLERATION 0.75 |
| 37 |
|
| 38 |
struct module_state { |
| 39 |
PyObject *error; |
| 40 |
}; |
| 41 |
|
| 42 |
#if PY_MAJOR_VERSION >= 3 |
| 43 |
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) |
| 44 |
#else |
| 45 |
#define GETSTATE(m) (&_state) |
| 46 |
static struct module_state _state; |
| 47 |
#endif |
| 48 |
|
| 49 |
|
| 50 |
/* if support for Python 2.5 is dropped the bytesobject.h will do this for us */ |
| 51 |
#if PY_MAJOR_VERSION < 3 |
| 52 |
#define PyBytes_FromStringAndSize PyString_FromStringAndSize |
| 53 |
#define _PyBytes_Resize _PyString_Resize |
| 54 |
#define PyBytes_AS_STRING PyString_AS_STRING |
| 55 |
#endif |
| 56 |
|
| 57 |
static PyObject *SnappyCompressError, |
| 58 |
*SnappyUncompressError, |
| 59 |
*SnappyInvalidCompressedInputError, |
| 60 |
*SnappyCompressedLengthError; |
| 61 |
|
| 62 |
static inline PyObject * |
| 63 |
maybe_resize(PyObject *str, size_t expected_size, size_t actual_size) |
| 64 |
{ |
| 65 |
// Tolerate up to 25% slop, to reduce the likelihood of |
| 66 |
// reallocation and copying. |
| 67 |
if (actual_size != expected_size) { |
| 68 |
if (actual_size < expected_size * RESIZE_TOLERATION) { |
| 69 |
_PyBytes_Resize(&str, actual_size); |
| 70 |
return str; |
| 71 |
} |
| •72 |
Py_SET_SIZE(str, actual_size); |
| 73 |
} |
| 74 |
return str; |
| 75 |
} |
| 76 |
|
| 77 |
static const char * |
| 78 |
snappy_strerror(snappy_status status) |
| 79 |
{ |
| 80 |
switch (status) { |
| 81 |
case SNAPPY_OK: |
| 82 |
return "no error"; |
| 83 |
case SNAPPY_INVALID_INPUT: |
| 84 |
return "invalid input"; |
| 85 |
case SNAPPY_BUFFER_TOO_SMALL: |
| 86 |
return "buffer too small"; |
| 87 |
default: |
| 88 |
return "unknown error"; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
static PyObject * |
| 93 |
snappy__compress(PyObject *self, PyObject *args) |
| 94 |
{ |
| 95 |
Py_buffer input; |
| 96 |
size_t compressed_size, actual_size; |
| 97 |
PyObject * result; |
| 98 |
snappy_status status; |
| 99 |
|
| 100 |
#if PY_MAJOR_VERSION >= 3 |
| 101 |
if (!PyArg_ParseTuple(args, "y*", &input)) |
| 102 |
#else |
| 103 |
if (!PyArg_ParseTuple(args, "s*", &input)) |
| 104 |
#endif |
| 105 |
return NULL; |
| 106 |
|
| 107 |
// Ask for the max size of the compressed object. |
| 108 |
compressed_size = snappy_max_compressed_length(input.len); |
| 109 |
|
| 110 |
// Make snappy compression |
| 111 |
result = PyBytes_FromStringAndSize(NULL, compressed_size); |
| 112 |
if (result) { |
| 113 |
actual_size = compressed_size; |
| 114 |
Py_BEGIN_ALLOW_THREADS |
| 115 |
status = snappy_compress((const char *) input.buf, input.len, |
| 116 |
PyBytes_AS_STRING(result), &actual_size); |
| 117 |
Py_END_ALLOW_THREADS |
| 118 |
PyBuffer_Release(&input); |
| 119 |
if (status == SNAPPY_OK) { |
| 120 |
return maybe_resize(result, compressed_size, actual_size); |
| 121 |
} |
| 122 |
else { |
| 123 |
Py_DECREF(result); |
| 124 |
} |
| 125 |
PyErr_Format(SnappyCompressError, |
| 126 |
"Error while compressing: %s", snappy_strerror(status)); |
| 127 |
} |
| 128 |
else { |
| 129 |
PyBuffer_Release(&input); |
| 130 |
PyErr_Format(SnappyCompressError, |
| 131 |
"Error while compressing: unable to acquire output string"); |
| 132 |
} |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
static PyObject * |
| 137 |
snappy__uncompress(PyObject *self, PyObject *args) |
| 138 |
{ |
| 139 |
Py_buffer compressed; |
| 140 |
size_t uncomp_size, actual_size; |
| 141 |
PyObject * result; |
| 142 |
snappy_status status; |
| 143 |
|
| 144 |
#if PY_MAJOR_VERSION >=3 |
| 145 |
if (!PyArg_ParseTuple(args, "y*", &compressed)) |
| 146 |
#else |
| 147 |
if (!PyArg_ParseTuple(args, "s*", &compressed)) |
| 148 |
#endif |
| 149 |
return NULL; |
| 150 |
|
| 151 |
status = snappy_uncompressed_length((const char *) compressed.buf, compressed.len, |
| 152 |
&uncomp_size); |
| 153 |
if (status != SNAPPY_OK) { |
| 154 |
PyBuffer_Release(&compressed); |
| 155 |
PyErr_SetString(SnappyCompressedLengthError, |
| 156 |
"Can not calculate uncompressed length"); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
result = PyBytes_FromStringAndSize(NULL, uncomp_size); |
| 161 |
if (result) { |
| 162 |
actual_size = uncomp_size; |
| 163 |
Py_BEGIN_ALLOW_THREADS |
| 164 |
status = snappy_uncompress((const char *) compressed.buf, compressed.len, |
| 165 |
PyBytes_AS_STRING(result), &actual_size); |
| 166 |
Py_END_ALLOW_THREADS |
| 167 |
PyBuffer_Release(&compressed); |
| 168 |
if (SNAPPY_OK == status) { |
| 169 |
return maybe_resize(result, uncomp_size, actual_size); |
| 170 |
} |
| 171 |
else { |
| 172 |
Py_DECREF(result); |
| 173 |
PyErr_Format(SnappyUncompressError, |
| 174 |
"Error while decompressing: %s", snappy_strerror(status)); |
| 175 |
} |
| 176 |
} |
| 177 |
else { |
| 178 |
PyBuffer_Release(&compressed); |
| 179 |
} |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
static PyObject * |
| 185 |
snappy__is_valid_compressed_buffer(PyObject *self, PyObject *args) |
| 186 |
{ |
| 187 |
const char * compressed; |
| 188 |
int comp_size; |
| 189 |
snappy_status status; |
| 190 |
|
| 191 |
#if PY_MAJOR_VERSION >=3 |
| 192 |
if (!PyArg_ParseTuple(args, "y#", &compressed, &comp_size)) |
| 193 |
#else |
| 194 |
if (!PyArg_ParseTuple(args, "s#", &compressed, &comp_size)) |
| 195 |
#endif |
| 196 |
return NULL; |
| 197 |
|
| 198 |
status = snappy_validate_compressed_buffer(compressed, comp_size); |
| 199 |
if (status == SNAPPY_OK) |
| 200 |
Py_RETURN_TRUE; |
| 201 |
Py_RETURN_FALSE; |
| 202 |
} |
| 203 |
|
| 204 |
static PyObject * |
| 205 |
snappy__crc32c(PyObject *self, PyObject *args) |
| 206 |
{ |
| 207 |
const unsigned char * input; |
| 208 |
int input_size; |
| 209 |
|
| 210 |
#if PY_MAJOR_VERSION >= 3 |
| 211 |
if (!PyArg_ParseTuple(args, "y#", &input, &input_size)) |
| 212 |
#else |
| 213 |
if (!PyArg_ParseTuple(args, "s#", &input, &input_size)) |
| 214 |
#endif |
| 215 |
return NULL; |
| 216 |
|
| 217 |
return PyLong_FromUnsignedLong( |
| 218 |
crc_finalize(crc_update(crc_init(), input, input_size))); |
| 219 |
} |
| 220 |
|
| 221 |
static PyMethodDef snappy_methods[] = { |
| 222 |
{"compress", snappy__compress, METH_VARARGS, |
| 223 |
"Compress a string using the snappy library."}, |
| 224 |
{"uncompress", snappy__uncompress, METH_VARARGS, |
| 225 |
"Uncompress a string compressed with the snappy library."}, |
| 226 |
{"decompress", snappy__uncompress, METH_VARARGS, |
| 227 |
"Alias to Uncompress method, to be compatible with zlib."}, |
| 228 |
{"isValidCompressed", snappy__is_valid_compressed_buffer, METH_VARARGS, |
| 229 |
"Returns True if the compressed buffer is valid, False otherwise"}, |
| 230 |
{"_crc32c", snappy__crc32c, METH_VARARGS, |
| 231 |
"Generate an RFC3720, section 12.1 CRC-32C"}, |
| 232 |
{NULL, NULL, 0, NULL} /* Sentinel */ |
| 233 |
}; |
| 234 |
|
| 235 |
#if PY_MAJOR_VERSION >= 3 |
| 236 |
|
| 237 |
static int snappy_traverse(PyObject *m, visitproc visit, void *arg) { |
| 238 |
Py_VISIT(GETSTATE(m)->error); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
static int snappy_clear(PyObject *m) { |
| 243 |
Py_CLEAR(GETSTATE(m)->error); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static struct PyModuleDef moduledef = { |
| 249 |
PyModuleDef_HEAD_INIT, |
| 250 |
"_snappy", |
| 251 |
NULL, |
| 252 |
sizeof(struct module_state), |
| 253 |
snappy_methods, |
| 254 |
NULL, |
| 255 |
snappy_traverse, |
| 256 |
snappy_clear, |
| 257 |
NULL |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
#define INITERROR return NULL |
| 262 |
|
| 263 |
PyMODINIT_FUNC |
| 264 |
PyInit__snappy(void) |
| 265 |
|
| 266 |
#else |
| 267 |
#define INITERROR return |
| 268 |
|
| 269 |
PyMODINIT_FUNC |
| 270 |
init_snappy(void) |
| 271 |
#endif |
| 272 |
{ |
| 273 |
PyObject *m; |
| 274 |
|
| 275 |
#if PY_MAJOR_VERSION >= 3 |
| 276 |
m = PyModule_Create(&moduledef); |
| 277 |
#else |
| 278 |
m = Py_InitModule("_snappy", snappy_methods); |
| 279 |
#endif |
| 280 |
|
| 281 |
if (m == NULL) |
| 282 |
INITERROR; |
| 283 |
|
| 284 |
SnappyCompressError = PyErr_NewException((char*)"snappy.CompressError", |
| 285 |
NULL, NULL); |
| 286 |
SnappyUncompressError = PyErr_NewException((char*)"snappy.UncompressError", |
| 287 |
NULL, NULL); |
| 288 |
SnappyInvalidCompressedInputError = PyErr_NewException( |
| 289 |
(char*)"snappy.InvalidCompressedInputError", NULL, NULL); |
| 290 |
SnappyCompressedLengthError = PyErr_NewException( |
| 291 |
(char*)"snappy.CompressedLengthError", NULL, NULL); |
| 292 |
|
| 293 |
Py_INCREF(SnappyCompressError); |
| 294 |
Py_INCREF(SnappyUncompressError); |
| 295 |
Py_INCREF(SnappyInvalidCompressedInputError); |
| 296 |
Py_INCREF(SnappyCompressedLengthError); |
| 297 |
|
| 298 |
PyModule_AddObject(m, "CompressError", SnappyCompressError); |
| 299 |
PyModule_AddObject(m, "UncompressError", SnappyUncompressError); |
| 300 |
PyModule_AddObject(m, "InvalidCompressedInputError", |
| 301 |
SnappyInvalidCompressedInputError); |
| 302 |
PyModule_AddObject(m, "CompressedLengthError", SnappyCompressedLengthError); |
| 303 |
|
| 304 |
/* Version = MODULE_VERSION */ |
| 305 |
if (PyModule_AddStringConstant(m, "__version__", MODULE_VERSION)) |
| 306 |
INITERROR; |
| 307 |
|
| 308 |
#if PY_MAJOR_VERSION >= 3 |
| 309 |
return m; |
| 310 |
#endif |
| 311 |
} |