| 1 |
/** |
| 2 |
* Copyright (c) 2018-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 |
| 14 |
ZstdCompressionChunkerIterator_dealloc(ZstdCompressionChunkerIterator *self) { |
| 15 |
Py_XDECREF(self->chunker); |
| 16 |
|
| 17 |
PyObject_Del(self); |
| 18 |
} |
| 19 |
|
| 20 |
static PyObject *ZstdCompressionChunkerIterator_iter(PyObject *self) { |
| 21 |
Py_INCREF(self); |
| 22 |
return self; |
| 23 |
} |
| 24 |
|
| 25 |
static PyObject * |
| 26 |
ZstdCompressionChunkerIterator_iternext(ZstdCompressionChunkerIterator *self) { |
| 27 |
size_t zresult; |
| 28 |
PyObject *chunk; |
| 29 |
ZstdCompressionChunker *chunker = self->chunker; |
| 30 |
ZSTD_EndDirective zFlushMode; |
| 31 |
|
| 32 |
if (self->mode != compressionchunker_mode_normal && |
| 33 |
chunker->input.pos != chunker->input.size) { |
| 34 |
PyErr_SetString(ZstdError, "input should have been fully consumed " |
| 35 |
"before calling flush() or finish()"); |
| 36 |
return NULL; |
| 37 |
} |
| 38 |
|
| 39 |
if (chunker->finished) { |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
/* If we have data left in the input, consume it. */ |
| 44 |
while (chunker->input.pos < chunker->input.size) { |
| 45 |
Py_BEGIN_ALLOW_THREADS zresult = |
| 46 |
ZSTD_compressStream2(chunker->compressor->cctx, &chunker->output, |
| 47 |
&chunker->input, ZSTD_e_continue); |
| 48 |
Py_END_ALLOW_THREADS |
| 49 |
|
| 50 |
/* Input is fully consumed. */ |
| 51 |
if (chunker->input.pos == chunker->input.size) { |
| 52 |
chunker->input.src = NULL; |
| 53 |
chunker->input.pos = 0; |
| 54 |
chunker->input.size = 0; |
| 55 |
PyBuffer_Release(&chunker->inBuffer); |
| 56 |
} |
| 57 |
|
| 58 |
if (ZSTD_isError(zresult)) { |
| 59 |
PyErr_Format(ZstdError, "zstd compress error: %s", |
| 60 |
ZSTD_getErrorName(zresult)); |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
|
| 64 |
/* If it produced a full output chunk, emit it. */ |
| 65 |
if (chunker->output.pos == chunker->output.size) { |
| 66 |
chunk = PyBytes_FromStringAndSize(chunker->output.dst, |
| 67 |
chunker->output.pos); |
| 68 |
if (!chunk) { |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
chunker->output.pos = 0; |
| 73 |
|
| 74 |
return chunk; |
| 75 |
} |
| 76 |
|
| 77 |
/* Else continue to compress available input data. */ |
| 78 |
} |
| 79 |
|
| 80 |
/* We also need this here for the special case of an empty input buffer. */ |
| 81 |
if (chunker->input.pos == chunker->input.size) { |
| 82 |
chunker->input.src = NULL; |
| 83 |
chunker->input.pos = 0; |
| 84 |
chunker->input.size = 0; |
| 85 |
PyBuffer_Release(&chunker->inBuffer); |
| 86 |
} |
| 87 |
|
| 88 |
/* No more input data. A partial chunk may be in chunker->output. |
| 89 |
* If we're in normal compression mode, we're done. Otherwise if we're in |
| 90 |
* flush or finish mode, we need to emit what data remains. |
| 91 |
*/ |
| 92 |
if (self->mode == compressionchunker_mode_normal) { |
| 93 |
/* We don't need to set StopIteration. */ |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
if (self->mode == compressionchunker_mode_flush) { |
| 98 |
zFlushMode = ZSTD_e_flush; |
| 99 |
} |
| 100 |
else if (self->mode == compressionchunker_mode_finish) { |
| 101 |
zFlushMode = ZSTD_e_end; |
| 102 |
} |
| 103 |
else { |
| 104 |
PyErr_SetString(ZstdError, |
| 105 |
"unhandled compression mode; this should never happen"); |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
Py_BEGIN_ALLOW_THREADS zresult = |
| 110 |
ZSTD_compressStream2(chunker->compressor->cctx, &chunker->output, |
| 111 |
&chunker->input, zFlushMode); |
| 112 |
Py_END_ALLOW_THREADS |
| 113 |
|
| 114 |
if (ZSTD_isError(zresult)) { |
| 115 |
PyErr_Format(ZstdError, "zstd compress error: %s", |
| 116 |
ZSTD_getErrorName(zresult)); |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
if (!zresult && chunker->output.pos == 0) { |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
chunk = PyBytes_FromStringAndSize(chunker->output.dst, chunker->output.pos); |
| 125 |
if (!chunk) { |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
|
| 129 |
chunker->output.pos = 0; |
| 130 |
|
| 131 |
if (!zresult && self->mode == compressionchunker_mode_finish) { |
| 132 |
chunker->finished = 1; |
| 133 |
} |
| 134 |
|
| 135 |
return chunk; |
| 136 |
} |
| 137 |
|
| 138 |
PyType_Slot ZstdCompressionChunkerIteratorSlots[] = { |
| 139 |
{Py_tp_dealloc, ZstdCompressionChunkerIterator_dealloc}, |
| 140 |
{Py_tp_iter, ZstdCompressionChunkerIterator_iter}, |
| 141 |
{Py_tp_iternext, ZstdCompressionChunkerIterator_iternext}, |
| 142 |
{Py_tp_new, PyType_GenericNew}, |
| 143 |
{0, NULL}, |
| 144 |
}; |
| 145 |
|
| 146 |
PyType_Spec ZstdCompressionChunkerIteratorSpec = { |
| 147 |
"zstandard.backend_c.ZstdCompressionChunkerIterator", |
| 148 |
sizeof(ZstdCompressionChunkerIterator), |
| 149 |
0, |
| 150 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 151 |
ZstdCompressionChunkerIteratorSlots, |
| 152 |
}; |
| 153 |
|
| 154 |
PyTypeObject *ZstdCompressionChunkerIteratorType; |
| 155 |
|
| 156 |
static void ZstdCompressionChunker_dealloc(ZstdCompressionChunker *self) { |
| 157 |
PyBuffer_Release(&self->inBuffer); |
| 158 |
self->input.src = NULL; |
| 159 |
|
| 160 |
PyMem_Free(self->output.dst); |
| 161 |
self->output.dst = NULL; |
| 162 |
|
| 163 |
Py_XDECREF(self->compressor); |
| 164 |
|
| 165 |
PyObject_Del(self); |
| 166 |
} |
| 167 |
|
| 168 |
static ZstdCompressionChunkerIterator * |
| 169 |
ZstdCompressionChunker_compress(ZstdCompressionChunker *self, PyObject *args, |
| 170 |
PyObject *kwargs) { |
| 171 |
static char *kwlist[] = {"data", NULL}; |
| 172 |
|
| 173 |
ZstdCompressionChunkerIterator *result; |
| 174 |
|
| 175 |
if (self->finished) { |
| 176 |
PyErr_SetString(ZstdError, |
| 177 |
"cannot call compress() after compression finished"); |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
if (self->inBuffer.obj) { |
| 182 |
PyErr_SetString(ZstdError, "cannot perform operation before consuming " |
| 183 |
"output from previous operation"); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:compress", kwlist, |
| 188 |
&self->inBuffer)) { |
| 189 |
return NULL; |
| 190 |
} |
| 191 |
|
| 192 |
result = (ZstdCompressionChunkerIterator *)PyObject_CallObject( |
| 193 |
(PyObject *)ZstdCompressionChunkerIteratorType, NULL); |
| 194 |
if (!result) { |
| 195 |
PyBuffer_Release(&self->inBuffer); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
self->input.src = self->inBuffer.buf; |
| 200 |
self->input.size = self->inBuffer.len; |
| 201 |
self->input.pos = 0; |
| 202 |
|
| 203 |
result->chunker = self; |
| 204 |
Py_INCREF(result->chunker); |
| 205 |
|
| 206 |
result->mode = compressionchunker_mode_normal; |
| 207 |
|
| 208 |
return result; |
| 209 |
} |
| 210 |
|
| 211 |
static ZstdCompressionChunkerIterator * |
| 212 |
ZstdCompressionChunker_finish(ZstdCompressionChunker *self) { |
| 213 |
ZstdCompressionChunkerIterator *result; |
| 214 |
|
| 215 |
if (self->finished) { |
| 216 |
PyErr_SetString(ZstdError, |
| 217 |
"cannot call finish() after compression finished"); |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
|
| 221 |
if (self->inBuffer.obj) { |
| 222 |
PyErr_SetString(ZstdError, "cannot call finish() before consuming " |
| 223 |
"output from previous operation"); |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
result = (ZstdCompressionChunkerIterator *)PyObject_CallObject( |
| 228 |
(PyObject *)ZstdCompressionChunkerIteratorType, NULL); |
| 229 |
if (!result) { |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
|
| 233 |
result->chunker = self; |
| 234 |
Py_INCREF(result->chunker); |
| 235 |
|
| 236 |
result->mode = compressionchunker_mode_finish; |
| 237 |
|
| 238 |
return result; |
| 239 |
} |
| 240 |
|
| 241 |
static ZstdCompressionChunkerIterator * |
| 242 |
ZstdCompressionChunker_flush(ZstdCompressionChunker *self, PyObject *args, |
| 243 |
PyObject *kwargs) { |
| 244 |
ZstdCompressionChunkerIterator *result; |
| 245 |
|
| 246 |
if (self->finished) { |
| 247 |
PyErr_SetString(ZstdError, |
| 248 |
"cannot call flush() after compression finished"); |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
|
| 252 |
if (self->inBuffer.obj) { |
| 253 |
PyErr_SetString(ZstdError, "cannot call flush() before consuming " |
| 254 |
"output from previous operation"); |
| 255 |
return NULL; |
| 256 |
} |
| 257 |
|
| 258 |
result = (ZstdCompressionChunkerIterator *)PyObject_CallObject( |
| 259 |
(PyObject *)ZstdCompressionChunkerIteratorType, NULL); |
| 260 |
if (!result) { |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
result->chunker = self; |
| 265 |
Py_INCREF(result->chunker); |
| 266 |
|
| 267 |
result->mode = compressionchunker_mode_flush; |
| 268 |
|
| 269 |
return result; |
| 270 |
} |
| 271 |
|
| 272 |
static PyMethodDef ZstdCompressionChunker_methods[] = { |
| 273 |
{"compress", (PyCFunction)ZstdCompressionChunker_compress, |
| 274 |
METH_VARARGS | METH_KEYWORDS, PyDoc_STR("compress data")}, |
| 275 |
{"finish", (PyCFunction)ZstdCompressionChunker_finish, METH_NOARGS, |
| 276 |
PyDoc_STR("finish compression operation")}, |
| 277 |
{"flush", (PyCFunction)ZstdCompressionChunker_flush, |
| 278 |
METH_VARARGS | METH_KEYWORDS, PyDoc_STR("finish compression operation")}, |
| 279 |
{NULL, NULL}}; |
| 280 |
|
| 281 |
PyType_Slot ZstdCompressionChunkerSlots[] = { |
| 282 |
{Py_tp_dealloc, ZstdCompressionChunker_dealloc}, |
| 283 |
{Py_tp_methods, ZstdCompressionChunker_methods}, |
| 284 |
{Py_tp_new, PyType_GenericNew}, |
| 285 |
{0, NULL}, |
| 286 |
}; |
| 287 |
|
| 288 |
PyType_Spec ZstdCompressionChunkerSpec = { |
| 289 |
"zstandard.backend_c.ZstdCompressionChunkerType", |
| 290 |
sizeof(ZstdCompressionChunker), |
| 291 |
0, |
| 292 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 293 |
ZstdCompressionChunkerSlots, |
| 294 |
}; |
| 295 |
|
| 296 |
PyTypeObject *ZstdCompressionChunkerType; |
| 297 |
|
| 298 |
void compressionchunker_module_init(PyObject *module) { |
| 299 |
ZstdCompressionChunkerIteratorType = |
| 300 |
(PyTypeObject *)PyType_FromSpec(&ZstdCompressionChunkerIteratorSpec); |
| 301 |
if (PyType_Ready(ZstdCompressionChunkerIteratorType) < 0) { |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
ZstdCompressionChunkerType = |
| 306 |
(PyTypeObject *)PyType_FromSpec(&ZstdCompressionChunkerSpec); |
| 307 |
if (PyType_Ready(ZstdCompressionChunkerType) < 0) { |
| 308 |
return; |
| 309 |
} |
| 310 |
} |