static PyObject * PyBlosc_decompress(PyObject *self, PyObject *args) { PyObject *result_str; void *input, *output; size_t nbytes, cbytes; if (!PyArg_ParseTuple(args, "s#:decompress", &input, &cbytes)) return NULL; /* fetch the uncompressed size into nbytes */ if (!get_nbytes(input, cbytes, &nbytes)) return NULL; /* Book memory for the result */ if (!(result_str = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes))) return NULL; output = PyBytes_AS_STRING(result_str); /* do decompression */ if (!decompress_helper(input, nbytes, output)){ Py_XDECREF(result_str); return NULL; } return result_str; }
static PyObject * PyBlosc_decompress_ptr(PyObject *self, PyObject *args) { PyObject * pointer, * return_int; void * input, * output; size_t cbytes, nbytes; /* require a compressed string and a pointer */ if (!PyArg_ParseTuple(args, "s#O:decompress", &input, &cbytes, &pointer)) return NULL; /* convert the int or long Python object to a void * */ output = PyLong_AsVoidPtr(pointer); if (output == NULL) return NULL; /* fetch the uncompressed size into nbytes */ if (!get_nbytes(input, cbytes, &nbytes)) return NULL; /* do decompression */ if (!decompress_helper(input, nbytes, output)) return NULL; /* Return nbytes as python integer. This is legitimate, because * decompress_helper above has checked that the number of bytes written * was indeed nbytes. * */ return_int = PyLong_FromSize_t(nbytes); Py_INCREF(return_int); return return_int; }
static PyObject * PyBlosc_decompress(PyObject *self, PyObject *args) { Py_buffer view; PyObject *result_str; void *input, *output; size_t nbytes, cbytes; char *format; /* Accept some kind of input */ #if PY_MAJOR_VERSION <= 2 /* s* : bytes like object including unicode and anything that supports * the buffer interface */ format = "s*:decompress"; #elif PY_MAJOR_VERSION >= 3 /* y* :bytes like object EXCLUDING unicode and anything that supports * the buffer interface. This is the recommended way to accept binary * data in Python 3. */ format = "y*:decompress"; #endif if (!PyArg_ParseTuple(args, format, &view)) return NULL; cbytes = view.len; input = view.buf; /* fetch the uncompressed size into nbytes */ if (!get_nbytes(input, cbytes, &nbytes)){ PyBuffer_Release(&view); return NULL; } /* Book memory for the result */ if (!(result_str = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes))){ PyBuffer_Release(&view); return NULL; } output = PyBytes_AS_STRING(result_str); /* do decompression */ if (!decompress_helper(input, nbytes, output)){ Py_XDECREF(result_str); PyBuffer_Release(&view); return NULL; } PyBuffer_Release(&view); return result_str; }
static PyObject * PyBlosc_decompress(PyObject *self, PyObject *args) { Py_buffer view; PyObject *result_str; void *input, *output; size_t nbytes, cbytes; int as_bytearray; /* Accept some kind of input */ #if PY_MAJOR_VERSION <= 2 PyObject *as_bytearray_obj = NULL; /* s* : bytes like object including unicode and anything that supports * the buffer interface. We cannot use p in python 2 so we will * create an object to hold the predicate. */ if (!PyArg_ParseTuple(args, "s*O:decompress", &view, &as_bytearray_obj)) return NULL; if ((as_bytearray = PyObject_IsTrue(as_bytearray_obj)) < 0) { /* failed to convert predicate to bool */ return NULL; } #elif PY_MAJOR_VERSION >= 3 /* y* :bytes like object EXCLUDING unicode and anything that supports * the buffer interface. This is the recommended way to accept binary * data in Python 3. */ if (!PyArg_ParseTuple(args, "y*p:decompress", &view, &as_bytearray)) return NULL; #endif cbytes = view.len; input = view.buf; /* fetch the uncompressed size into nbytes */ if (!get_nbytes(input, cbytes, &nbytes)){ PyBuffer_Release(&view); return NULL; } #define branch(from_string_and_size, as_string) \ /* Book memory for the result */ \ if (!(result_str = from_string_and_size(NULL, (Py_ssize_t)nbytes))){ \ PyBuffer_Release(&view); \ return NULL; \ } \ \ output = as_string(result_str); \ (void)NULL if (as_bytearray) { branch(PyByteArray_FromStringAndSize, PyByteArray_AS_STRING); } else { branch(PyBytes_FromStringAndSize, PyBytes_AS_STRING); } #undef branch /* do decompression */ if (!decompress_helper(input, nbytes, output)){ Py_XDECREF(result_str); PyBuffer_Release(&view); return NULL; } PyBuffer_Release(&view); return result_str; }