| 1 |
/** |
| 2 |
* Copyright (c) 2016-present, Gregory Szorc |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This software may be modified and distributed under the terms |
| 6 |
* of the BSD license. See the LICENSE file for details. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "python-zstandard.h" |
| 10 |
|
| 11 |
extern PyObject *ZstdError; |
| 12 |
|
| 13 |
static void DecompressionObj_dealloc(ZstdDecompressionObj *self) { |
| 14 |
Py_XDECREF(self->decompressor); |
| 15 |
Py_CLEAR(self->unused_data); |
| 16 |
|
| 17 |
PyObject_Del(self); |
| 18 |
} |
| 19 |
|
| 20 |
static PyObject *DecompressionObj_decompress(ZstdDecompressionObj *self, |
| 21 |
PyObject *args, PyObject *kwargs) { |
| 22 |
static char *kwlist[] = {"data", NULL}; |
| 23 |
|
| 24 |
Py_buffer source; |
| 25 |
size_t zresult; |
| 26 |
ZSTD_inBuffer input; |
| 27 |
ZSTD_outBuffer output; |
| 28 |
PyObject *result = NULL; |
| 29 |
Py_ssize_t resultSize = 0; |
| 30 |
|
| 31 |
output.dst = NULL; |
| 32 |
|
| 33 |
if (self->finished) { |
| 34 |
PyErr_SetString(ZstdError, "cannot use a decompressobj multiple times"); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:decompress", kwlist, |
| 39 |
&source)) { |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
/* Special case of empty input. Output will always be empty. */ |
| 44 |
if (source.len == 0) { |
| 45 |
result = PyBytes_FromString(""); |
| 46 |
goto finally; |
| 47 |
} |
| 48 |
|
| 49 |
input.src = source.buf; |
| 50 |
input.size = source.len; |
| 51 |
input.pos = 0; |
| 52 |
|
| 53 |
output.dst = PyMem_Malloc(self->outSize); |
| 54 |
if (!output.dst) { |
| 55 |
PyErr_NoMemory(); |
| 56 |
goto except; |
| 57 |
} |
| 58 |
output.size = self->outSize; |
| 59 |
output.pos = 0; |
| 60 |
|
| 61 |
while (1) { |
| 62 |
Py_BEGIN_ALLOW_THREADS zresult = |
| 63 |
ZSTD_decompressStream(self->decompressor->dctx, &output, &input); |
| 64 |
Py_END_ALLOW_THREADS |
| 65 |
|
| 66 |
if (ZSTD_isError(zresult)) { |
| 67 |
PyErr_Format(ZstdError, "zstd decompressor error: %s", |
| 68 |
ZSTD_getErrorName(zresult)); |
| 69 |
goto except; |
| 70 |
} |
| 71 |
|
| 72 |
if (output.pos) { |
| 73 |
if (result) { |
| 74 |
resultSize = PyBytes_GET_SIZE(result); |
| 75 |
if (-1 == |
| 76 |
safe_pybytes_resize(&result, resultSize + output.pos)) { |
| 77 |
Py_XDECREF(result); |
| 78 |
goto except; |
| 79 |
} |
| 80 |
|
| 81 |
memcpy(PyBytes_AS_STRING(result) + resultSize, output.dst, |
| 82 |
output.pos); |
| 83 |
} |
| 84 |
else { |
| 85 |
result = PyBytes_FromStringAndSize(output.dst, output.pos); |
| 86 |
if (!result) { |
| 87 |
goto except; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (0 == zresult && !self->readAcrossFrames) { |
| 93 |
self->finished = 1; |
| 94 |
|
| 95 |
/* We should only get here at most once. */ |
| 96 |
assert(!self->unused_data); |
| 97 |
self->unused_data = PyBytes_FromStringAndSize((char *)(input.src) + input.pos, input.size - input.pos); |
| 98 |
|
| 99 |
break; |
| 100 |
} |
| 101 |
else if (0 == zresult && self->readAcrossFrames) { |
| 102 |
if (input.pos == input.size) { |
| 103 |
break; |
| 104 |
} else { |
| 105 |
output.pos = 0; |
| 106 |
} |
| 107 |
} |
| 108 |
else if (input.pos == input.size && output.pos == 0) { |
| 109 |
break; |
| 110 |
} |
| 111 |
else { |
| 112 |
output.pos = 0; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if (!result) { |
| 117 |
result = PyBytes_FromString(""); |
| 118 |
} |
| 119 |
|
| 120 |
goto finally; |
| 121 |
|
| 122 |
except: |
| 123 |
Py_CLEAR(result); |
| 124 |
|
| 125 |
finally: |
| 126 |
PyMem_Free(output.dst); |
| 127 |
PyBuffer_Release(&source); |
| 128 |
|
| 129 |
return result; |
| 130 |
} |
| 131 |
|
| 132 |
static PyObject *DecompressionObj_flush(ZstdDecompressionObj *self, |
| 133 |
PyObject *args, PyObject *kwargs) { |
| 134 |
static char *kwlist[] = {"length", NULL}; |
| 135 |
|
| 136 |
PyObject *length = NULL; |
| 137 |
|
| 138 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:flush", kwlist, |
| 139 |
&length)) { |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
return PyBytes_FromString(""); |
| 144 |
} |
| 145 |
|
| 146 |
static PyObject *DecompressionObj_unused_data(PyObject *self, void *unused) { |
| 147 |
ZstdDecompressionObj *slf = (ZstdDecompressionObj *)(self); |
| 148 |
|
| 149 |
if (slf->unused_data) { |
| 150 |
Py_INCREF(slf->unused_data); |
| 151 |
return slf->unused_data; |
| 152 |
} else { |
| 153 |
return PyBytes_FromString(""); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static PyObject *DecompressionObj_unconsumed_tail(PyObject *self, void *unused) { |
| 158 |
return PyBytes_FromString(""); |
| 159 |
} |
| 160 |
|
| 161 |
static PyObject *DecompressionObj_eof(PyObject *self, void *unused) { |
| 162 |
ZstdDecompressionObj *slf = (ZstdDecompressionObj*)(self); |
| 163 |
|
| 164 |
if (slf->finished) { |
| 165 |
Py_RETURN_TRUE; |
| 166 |
} else { |
| 167 |
Py_RETURN_FALSE; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static PyMethodDef DecompressionObj_methods[] = { |
| 172 |
{"decompress", (PyCFunction)DecompressionObj_decompress, |
| 173 |
METH_VARARGS | METH_KEYWORDS, PyDoc_STR("decompress data")}, |
| 174 |
{"flush", (PyCFunction)DecompressionObj_flush, METH_VARARGS | METH_KEYWORDS, |
| 175 |
PyDoc_STR("no-op")}, |
| 176 |
{NULL, NULL}}; |
| 177 |
|
| 178 |
static PyGetSetDef DecompressionObj_getset[] = { |
| 179 |
{"unused_data", DecompressionObj_unused_data, NULL, NULL, NULL }, |
| 180 |
{"unconsumed_tail", DecompressionObj_unconsumed_tail, NULL, NULL, NULL }, |
| 181 |
{"eof", DecompressionObj_eof, NULL, NULL, NULL }, |
| 182 |
{NULL}}; |
| 183 |
|
| 184 |
PyType_Slot ZstdDecompressionObjSlots[] = { |
| 185 |
{Py_tp_dealloc, DecompressionObj_dealloc}, |
| 186 |
{Py_tp_methods, DecompressionObj_methods}, |
| 187 |
{Py_tp_getset, DecompressionObj_getset}, |
| 188 |
{Py_tp_new, PyType_GenericNew}, |
| 189 |
{0, NULL}, |
| 190 |
}; |
| 191 |
|
| 192 |
PyType_Spec ZstdDecompressionObjSpec = { |
| 193 |
"zstandard.backend_c.ZstdDecompressionObj", |
| 194 |
sizeof(ZstdDecompressionObj), |
| 195 |
0, |
| 196 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 197 |
ZstdDecompressionObjSlots, |
| 198 |
}; |
| 199 |
|
| 200 |
PyTypeObject *ZstdDecompressionObjType; |
| 201 |
|
| 202 |
void decompressobj_module_init(PyObject *module) { |
| 203 |
ZstdDecompressionObjType = |
| 204 |
(PyTypeObject *)PyType_FromSpec(&ZstdDecompressionObjSpec); |
| 205 |
if (PyType_Ready(ZstdDecompressionObjType) < 0) { |
| 206 |
return; |
| 207 |
} |
| 208 |
} |