| 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 ZstdDecompressionWriter_dealloc(ZstdDecompressionWriter *self) { |
| 14 |
Py_XDECREF(self->decompressor); |
| 15 |
Py_XDECREF(self->writer); |
| 16 |
|
| 17 |
PyObject_Del(self); |
| 18 |
} |
| 19 |
|
| 20 |
static PyObject *ZstdDecompressionWriter_enter(ZstdDecompressionWriter *self) { |
| 21 |
if (self->closed) { |
| 22 |
PyErr_SetString(PyExc_ValueError, "stream is closed"); |
| 23 |
return NULL; |
| 24 |
} |
| 25 |
|
| 26 |
if (self->entered) { |
| 27 |
PyErr_SetString(ZstdError, "cannot __enter__ multiple times"); |
| 28 |
return NULL; |
| 29 |
} |
| 30 |
|
| 31 |
self->entered = 1; |
| 32 |
|
| 33 |
Py_INCREF(self); |
| 34 |
return (PyObject *)self; |
| 35 |
} |
| 36 |
|
| 37 |
static PyObject *ZstdDecompressionWriter_exit(ZstdDecompressionWriter *self, |
| 38 |
PyObject *args) { |
| 39 |
self->entered = 0; |
| 40 |
|
| 41 |
if (NULL == PyObject_CallMethod((PyObject *)self, "close", NULL)) { |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
|
| 45 |
Py_RETURN_FALSE; |
| 46 |
} |
| 47 |
|
| 48 |
static PyObject * |
| 49 |
ZstdDecompressionWriter_memory_size(ZstdDecompressionWriter *self) { |
| 50 |
return PyLong_FromSize_t(ZSTD_sizeof_DCtx(self->decompressor->dctx)); |
| 51 |
} |
| 52 |
|
| 53 |
static PyObject *ZstdDecompressionWriter_write(ZstdDecompressionWriter *self, |
| 54 |
PyObject *args, |
| 55 |
PyObject *kwargs) { |
| 56 |
static char *kwlist[] = {"data", NULL}; |
| 57 |
|
| 58 |
PyObject *result = NULL; |
| 59 |
Py_buffer source; |
| 60 |
size_t zresult = 0; |
| 61 |
ZSTD_inBuffer input; |
| 62 |
ZSTD_outBuffer output; |
| 63 |
PyObject *res; |
| 64 |
Py_ssize_t totalWrite = 0; |
| 65 |
|
| 66 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:write", kwlist, |
| 67 |
&source)) { |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
if (self->closed) { |
| 72 |
PyErr_SetString(PyExc_ValueError, "stream is closed"); |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
output.dst = PyMem_Malloc(self->outSize); |
| 77 |
if (!output.dst) { |
| 78 |
PyErr_NoMemory(); |
| 79 |
goto finally; |
| 80 |
} |
| 81 |
output.size = self->outSize; |
| 82 |
output.pos = 0; |
| 83 |
|
| 84 |
input.src = source.buf; |
| 85 |
input.size = source.len; |
| 86 |
input.pos = 0; |
| 87 |
|
| 88 |
while (input.pos < (size_t)source.len) { |
| 89 |
Py_BEGIN_ALLOW_THREADS zresult = |
| 90 |
ZSTD_decompressStream(self->decompressor->dctx, &output, &input); |
| 91 |
Py_END_ALLOW_THREADS |
| 92 |
|
| 93 |
if (ZSTD_isError(zresult)) { |
| 94 |
PyMem_Free(output.dst); |
| 95 |
PyErr_Format(ZstdError, "zstd decompress error: %s", |
| 96 |
ZSTD_getErrorName(zresult)); |
| 97 |
goto finally; |
| 98 |
} |
| 99 |
|
| 100 |
if (output.pos) { |
| 101 |
res = PyObject_CallMethod(self->writer, "write", "y#", output.dst, |
| 102 |
output.pos); |
| 103 |
if (NULL == res) { |
| 104 |
goto finally; |
| 105 |
} |
| 106 |
Py_XDECREF(res); |
| 107 |
totalWrite += output.pos; |
| 108 |
output.pos = 0; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
PyMem_Free(output.dst); |
| 113 |
|
| 114 |
if (self->writeReturnRead) { |
| 115 |
result = PyLong_FromSize_t(input.pos); |
| 116 |
} |
| 117 |
else { |
| 118 |
result = PyLong_FromSsize_t(totalWrite); |
| 119 |
} |
| 120 |
|
| 121 |
finally: |
| 122 |
PyBuffer_Release(&source); |
| 123 |
return result; |
| 124 |
} |
| 125 |
|
| 126 |
static PyObject *ZstdDecompressionWriter_close(ZstdDecompressionWriter *self) { |
| 127 |
PyObject *result; |
| 128 |
|
| 129 |
if (self->closed) { |
| 130 |
Py_RETURN_NONE; |
| 131 |
} |
| 132 |
|
| 133 |
self->closing = 1; |
| 134 |
result = PyObject_CallMethod((PyObject *)self, "flush", NULL); |
| 135 |
self->closing = 0; |
| 136 |
self->closed = 1; |
| 137 |
|
| 138 |
if (NULL == result) { |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
/* Call close on underlying stream as well. */ |
| 143 |
if (self->closefd && PyObject_HasAttrString(self->writer, "close")) { |
| 144 |
return PyObject_CallMethod(self->writer, "close", NULL); |
| 145 |
} |
| 146 |
|
| 147 |
Py_RETURN_NONE; |
| 148 |
} |
| 149 |
|
| 150 |
static PyObject *ZstdDecompressionWriter_fileno(ZstdDecompressionWriter *self) { |
| 151 |
if (PyObject_HasAttrString(self->writer, "fileno")) { |
| 152 |
return PyObject_CallMethod(self->writer, "fileno", NULL); |
| 153 |
} |
| 154 |
else { |
| 155 |
PyErr_SetString(PyExc_OSError, |
| 156 |
"fileno not available on underlying writer"); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static PyObject *ZstdDecompressionWriter_flush(ZstdDecompressionWriter *self) { |
| 162 |
if (self->closed) { |
| 163 |
PyErr_SetString(PyExc_ValueError, "stream is closed"); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
if (!self->closing && PyObject_HasAttrString(self->writer, "flush")) { |
| 168 |
return PyObject_CallMethod(self->writer, "flush", NULL); |
| 169 |
} |
| 170 |
else { |
| 171 |
Py_RETURN_NONE; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
static PyObject *ZstdDecompressionWriter_iter(PyObject *self) { |
| 176 |
set_io_unsupported_operation(); |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
static PyObject *ZstdDecompressionWriter_iternext(PyObject *self) { |
| 181 |
set_io_unsupported_operation(); |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
static PyObject *ZstdDecompressionWriter_false(PyObject *self, PyObject *args) { |
| 186 |
Py_RETURN_FALSE; |
| 187 |
} |
| 188 |
|
| 189 |
static PyObject *ZstdDecompressionWriter_true(PyObject *self, PyObject *args) { |
| 190 |
Py_RETURN_TRUE; |
| 191 |
} |
| 192 |
|
| 193 |
static PyObject *ZstdDecompressionWriter_unsupported(PyObject *self, |
| 194 |
PyObject *args, |
| 195 |
PyObject *kwargs) { |
| 196 |
set_io_unsupported_operation(); |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
|
| 200 |
static PyMethodDef ZstdDecompressionWriter_methods[] = { |
| 201 |
{"__enter__", (PyCFunction)ZstdDecompressionWriter_enter, METH_NOARGS, |
| 202 |
PyDoc_STR("Enter a decompression context.")}, |
| 203 |
{"__exit__", (PyCFunction)ZstdDecompressionWriter_exit, METH_VARARGS, |
| 204 |
PyDoc_STR("Exit a decompression context.")}, |
| 205 |
{"memory_size", (PyCFunction)ZstdDecompressionWriter_memory_size, |
| 206 |
METH_NOARGS, |
| 207 |
PyDoc_STR( |
| 208 |
"Obtain the memory size in bytes of the underlying decompressor.")}, |
| 209 |
{"close", (PyCFunction)ZstdDecompressionWriter_close, METH_NOARGS, NULL}, |
| 210 |
{"fileno", (PyCFunction)ZstdDecompressionWriter_fileno, METH_NOARGS, NULL}, |
| 211 |
{"flush", (PyCFunction)ZstdDecompressionWriter_flush, METH_NOARGS, NULL}, |
| 212 |
{"isatty", ZstdDecompressionWriter_false, METH_NOARGS, NULL}, |
| 213 |
{"readable", ZstdDecompressionWriter_false, METH_NOARGS, NULL}, |
| 214 |
{"readline", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 215 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 216 |
{"readlines", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 217 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 218 |
{"seek", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 219 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 220 |
{"seekable", ZstdDecompressionWriter_false, METH_NOARGS, NULL}, |
| 221 |
{"tell", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 222 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 223 |
{"truncate", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 224 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 225 |
{"writable", ZstdDecompressionWriter_true, METH_NOARGS, NULL}, |
| 226 |
{"writelines", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 227 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 228 |
{"read", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 229 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 230 |
{"readall", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 231 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 232 |
{"readinto", (PyCFunction)ZstdDecompressionWriter_unsupported, |
| 233 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 234 |
{"write", (PyCFunction)ZstdDecompressionWriter_write, |
| 235 |
METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Compress data")}, |
| 236 |
{NULL, NULL}}; |
| 237 |
|
| 238 |
static PyMemberDef ZstdDecompressionWriter_members[] = { |
| 239 |
{"closed", T_BOOL, offsetof(ZstdDecompressionWriter, closed), READONLY, |
| 240 |
NULL}, |
| 241 |
{NULL}}; |
| 242 |
|
| 243 |
PyType_Slot ZstdDecompressionWriterSlots[] = { |
| 244 |
{Py_tp_dealloc, ZstdDecompressionWriter_dealloc}, |
| 245 |
{Py_tp_iter, ZstdDecompressionWriter_iter}, |
| 246 |
{Py_tp_iternext, ZstdDecompressionWriter_iternext}, |
| 247 |
{Py_tp_methods, ZstdDecompressionWriter_methods}, |
| 248 |
{Py_tp_members, ZstdDecompressionWriter_members}, |
| 249 |
{Py_tp_new, PyType_GenericNew}, |
| 250 |
{0, NULL}, |
| 251 |
}; |
| 252 |
|
| 253 |
PyType_Spec ZstdDecompressionWriterSpec = { |
| 254 |
"zstandard.backend_c.ZstdDecompressionWriter", |
| 255 |
sizeof(ZstdDecompressionWriter), |
| 256 |
0, |
| 257 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 258 |
ZstdDecompressionWriterSlots, |
| 259 |
}; |
| 260 |
|
| 261 |
PyTypeObject *ZstdDecompressionWriterType; |
| 262 |
|
| 263 |
void decompressionwriter_module_init(PyObject *mod) { |
| 264 |
ZstdDecompressionWriterType = |
| 265 |
(PyTypeObject *)PyType_FromSpec(&ZstdDecompressionWriterSpec); |
| 266 |
if (PyType_Ready(ZstdDecompressionWriterType) < 0) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
Py_INCREF((PyObject *)ZstdDecompressionWriterType); |
| 271 |
PyModule_AddObject(mod, "ZstdDecompressionWriter", |
| 272 |
(PyObject *)ZstdDecompressionWriterType); |
| 273 |
} |