| 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 |
#define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 12 |
|
| 13 |
extern PyObject *ZstdError; |
| 14 |
|
| 15 |
static void ZstdCompressorIterator_dealloc(ZstdCompressorIterator *self) { |
| 16 |
Py_XDECREF(self->readResult); |
| 17 |
Py_XDECREF(self->compressor); |
| 18 |
Py_XDECREF(self->reader); |
| 19 |
|
| 20 |
if (self->buffer.buf) { |
| 21 |
PyBuffer_Release(&self->buffer); |
| 22 |
memset(&self->buffer, 0, sizeof(self->buffer)); |
| 23 |
} |
| 24 |
|
| 25 |
if (self->output.dst) { |
| 26 |
PyMem_Free(self->output.dst); |
| 27 |
self->output.dst = NULL; |
| 28 |
} |
| 29 |
|
| 30 |
PyObject_Del(self); |
| 31 |
} |
| 32 |
|
| 33 |
static PyObject *ZstdCompressorIterator_iter(PyObject *self) { |
| 34 |
Py_INCREF(self); |
| 35 |
return self; |
| 36 |
} |
| 37 |
|
| 38 |
static PyObject *ZstdCompressorIterator_iternext(ZstdCompressorIterator *self) { |
| 39 |
size_t zresult; |
| 40 |
PyObject *readResult = NULL; |
| 41 |
PyObject *chunk; |
| 42 |
char *readBuffer; |
| 43 |
Py_ssize_t readSize = 0; |
| 44 |
Py_ssize_t bufferRemaining; |
| 45 |
|
| 46 |
if (self->finishedOutput) { |
| 47 |
PyErr_SetString(PyExc_StopIteration, "output flushed"); |
| 48 |
return NULL; |
| 49 |
} |
| 50 |
|
| 51 |
feedcompressor: |
| 52 |
|
| 53 |
/* If we have data left in the input, consume it. */ |
| 54 |
if (self->input.pos < self->input.size) { |
| 55 |
Py_BEGIN_ALLOW_THREADS zresult = |
| 56 |
ZSTD_compressStream2(self->compressor->cctx, &self->output, |
| 57 |
&self->input, ZSTD_e_continue); |
| 58 |
Py_END_ALLOW_THREADS |
| 59 |
|
| 60 |
/* Release the Python object holding the input buffer. */ |
| 61 |
if (self->input.pos == self->input.size) { |
| 62 |
self->input.src = NULL; |
| 63 |
self->input.pos = 0; |
| 64 |
self->input.size = 0; |
| 65 |
Py_DECREF(self->readResult); |
| 66 |
self->readResult = NULL; |
| 67 |
} |
| 68 |
|
| 69 |
if (ZSTD_isError(zresult)) { |
| 70 |
PyErr_Format(ZstdError, "zstd compress error: %s", |
| 71 |
ZSTD_getErrorName(zresult)); |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
/* If it produced output data, emit it. */ |
| 76 |
if (self->output.pos) { |
| 77 |
chunk = |
| 78 |
PyBytes_FromStringAndSize(self->output.dst, self->output.pos); |
| 79 |
self->output.pos = 0; |
| 80 |
return chunk; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/* We should never have output data sitting around after a previous call. */ |
| 85 |
assert(self->output.pos == 0); |
| 86 |
|
| 87 |
/* The code above should have either emitted a chunk and returned or |
| 88 |
consumed the entire input buffer. So the state of the input buffer is not |
| 89 |
relevant. */ |
| 90 |
if (!self->finishedInput) { |
| 91 |
if (self->reader) { |
| 92 |
readResult = |
| 93 |
PyObject_CallMethod(self->reader, "read", "I", self->inSize); |
| 94 |
if (!readResult) { |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
PyBytes_AsStringAndSize(readResult, &readBuffer, &readSize); |
| 99 |
} |
| 100 |
else { |
| 101 |
assert(self->buffer.buf); |
| 102 |
|
| 103 |
/* Only support contiguous C arrays. */ |
| 104 |
assert(self->buffer.strides == NULL && |
| 105 |
self->buffer.suboffsets == NULL); |
| 106 |
assert(self->buffer.itemsize == 1); |
| 107 |
|
| 108 |
readBuffer = (char *)self->buffer.buf + self->bufferOffset; |
| 109 |
bufferRemaining = self->buffer.len - self->bufferOffset; |
| 110 |
readSize = min(bufferRemaining, (Py_ssize_t)self->inSize); |
| 111 |
self->bufferOffset += readSize; |
| 112 |
} |
| 113 |
|
| 114 |
if (0 == readSize) { |
| 115 |
Py_XDECREF(readResult); |
| 116 |
self->finishedInput = 1; |
| 117 |
} |
| 118 |
else { |
| 119 |
self->readResult = readResult; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/* EOF */ |
| 124 |
if (0 == readSize) { |
| 125 |
self->input.src = NULL; |
| 126 |
self->input.size = 0; |
| 127 |
self->input.pos = 0; |
| 128 |
|
| 129 |
zresult = ZSTD_compressStream2(self->compressor->cctx, &self->output, |
| 130 |
&self->input, ZSTD_e_end); |
| 131 |
if (ZSTD_isError(zresult)) { |
| 132 |
PyErr_Format(ZstdError, "error ending compression stream: %s", |
| 133 |
ZSTD_getErrorName(zresult)); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
assert(self->output.pos); |
| 138 |
|
| 139 |
if (0 == zresult) { |
| 140 |
self->finishedOutput = 1; |
| 141 |
} |
| 142 |
|
| 143 |
chunk = PyBytes_FromStringAndSize(self->output.dst, self->output.pos); |
| 144 |
self->output.pos = 0; |
| 145 |
return chunk; |
| 146 |
} |
| 147 |
|
| 148 |
/* New data from reader. Feed into compressor. */ |
| 149 |
self->input.src = readBuffer; |
| 150 |
self->input.size = readSize; |
| 151 |
self->input.pos = 0; |
| 152 |
|
| 153 |
Py_BEGIN_ALLOW_THREADS zresult = ZSTD_compressStream2( |
| 154 |
self->compressor->cctx, &self->output, &self->input, ZSTD_e_continue); |
| 155 |
Py_END_ALLOW_THREADS |
| 156 |
|
| 157 |
/* The input buffer currently points to memory managed by Python |
| 158 |
(readBuffer). This object was allocated by this function. If it wasn't |
| 159 |
fully consumed, we need to release it in a subsequent function call. |
| 160 |
If it is fully consumed, do that now. |
| 161 |
*/ |
| 162 |
if (self->input.pos == self->input.size) { |
| 163 |
self->input.src = NULL; |
| 164 |
self->input.pos = 0; |
| 165 |
self->input.size = 0; |
| 166 |
Py_XDECREF(self->readResult); |
| 167 |
self->readResult = NULL; |
| 168 |
} |
| 169 |
|
| 170 |
if (ZSTD_isError(zresult)) { |
| 171 |
PyErr_Format(ZstdError, "zstd compress error: %s", |
| 172 |
ZSTD_getErrorName(zresult)); |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
|
| 176 |
assert(self->input.pos <= self->input.size); |
| 177 |
|
| 178 |
/* If we didn't write anything, start the process over. */ |
| 179 |
if (0 == self->output.pos) { |
| 180 |
goto feedcompressor; |
| 181 |
} |
| 182 |
|
| 183 |
chunk = PyBytes_FromStringAndSize(self->output.dst, self->output.pos); |
| 184 |
self->output.pos = 0; |
| 185 |
return chunk; |
| 186 |
} |
| 187 |
|
| 188 |
PyType_Slot ZstdCompressorIteratorSlots[] = { |
| 189 |
{Py_tp_dealloc, ZstdCompressorIterator_dealloc}, |
| 190 |
{Py_tp_iter, ZstdCompressorIterator_iter}, |
| 191 |
{Py_tp_iternext, ZstdCompressorIterator_iternext}, |
| 192 |
{Py_tp_new, PyType_GenericNew}, |
| 193 |
{0, NULL}, |
| 194 |
}; |
| 195 |
|
| 196 |
PyType_Spec ZstdCompressorIteratorSpec = { |
| 197 |
"zstandard.backend_c.ZstdCompressorIterator", |
| 198 |
sizeof(ZstdCompressorIterator), |
| 199 |
0, |
| 200 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 201 |
ZstdCompressorIteratorSlots, |
| 202 |
}; |
| 203 |
|
| 204 |
PyTypeObject *ZstdCompressorIteratorType; |
| 205 |
|
| 206 |
void compressoriterator_module_init(PyObject *mod) { |
| 207 |
ZstdCompressorIteratorType = |
| 208 |
(PyTypeObject *)PyType_FromSpec(&ZstdCompressorIteratorSpec); |
| 209 |
if (PyType_Ready(ZstdCompressorIteratorType) < 0) { |
| 210 |
return; |
| 211 |
} |
| 212 |
} |