Debian Codegraph @

c-ext/decompressoriterator.c

raw
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 ZstdDecompressorIterator_dealloc(ZstdDecompressorIterator *self) {
16 Py_XDECREF(self->decompressor);
17 Py_XDECREF(self->reader);
18
19 if (self->buffer.buf) {
20 PyBuffer_Release(&self->buffer);
21 memset(&self->buffer, 0, sizeof(self->buffer));
22 }
23
24 if (self->input.src) {
25 PyMem_Free((void *)self->input.src);
26 self->input.src = NULL;
27 }
28
29 PyObject_Del(self);
30 }
31
32 static PyObject *ZstdDecompressorIterator_iter(PyObject *self) {
33 Py_INCREF(self);
34 return self;
35 }
36
37 static DecompressorIteratorResult
38 read_decompressor_iterator(ZstdDecompressorIterator *self) {
39 size_t zresult;
40 PyObject *chunk;
41 DecompressorIteratorResult result;
42 size_t oldInputPos = self->input.pos;
43
44 result.chunk = NULL;
45
46 chunk = PyBytes_FromStringAndSize(NULL, self->outSize);
47 if (!chunk) {
48 result.errored = 1;
49 return result;
50 }
51
52 self->output.dst = PyBytes_AsString(chunk);
53 self->output.size = self->outSize;
54 self->output.pos = 0;
55
56 Py_BEGIN_ALLOW_THREADS zresult = ZSTD_decompressStream(
57 self->decompressor->dctx, &self->output, &self->input);
58 Py_END_ALLOW_THREADS
59
60 /* We're done with the pointer. Nullify to prevent anyone from getting a
61 handle on a Python object. */
62 self->output.dst = NULL;
63
64 if (ZSTD_isError(zresult)) {
65 Py_DECREF(chunk);
66 PyErr_Format(ZstdError, "zstd decompress error: %s",
67 ZSTD_getErrorName(zresult));
68 result.errored = 1;
69 return result;
70 }
71
72 self->readCount += self->input.pos - oldInputPos;
73
74 /* Frame is fully decoded. Input exhausted and output sitting in buffer. */
75 if (0 == zresult) {
76 self->finishedInput = 1;
77 self->finishedOutput = 1;
78 }
79
80 /* If it produced output data, return it. */
81 if (self->output.pos) {
82 if (self->output.pos < self->outSize) {
83 if (safe_pybytes_resize(&chunk, self->output.pos)) {
84 Py_XDECREF(chunk);
85 result.errored = 1;
86 return result;
87 }
88 }
89 }
90 else {
91 Py_DECREF(chunk);
92 chunk = NULL;
93 }
94
95 result.errored = 0;
96 result.chunk = chunk;
97
98 return result;
99 }
100
101 static PyObject *
102 ZstdDecompressorIterator_iternext(ZstdDecompressorIterator *self) {
103 PyObject *readResult = NULL;
104 char *readBuffer;
105 Py_ssize_t readSize;
106 Py_ssize_t bufferRemaining;
107 DecompressorIteratorResult result;
108
109 if (self->finishedOutput) {
110 PyErr_SetString(PyExc_StopIteration, "output flushed");
111 return NULL;
112 }
113
114 /* If we have data left in the input, consume it. */
115 if (self->input.pos < self->input.size) {
116 result = read_decompressor_iterator(self);
117 if (result.chunk || result.errored) {
118 return result.chunk;
119 }
120
121 /* Else fall through to get more data from input. */
122 }
123
124 read_from_source:
125
126 if (!self->finishedInput) {
127 if (self->reader) {
128 readResult =
129 PyObject_CallMethod(self->reader, "read", "I", self->inSize);
130 if (!readResult) {
131 return NULL;
132 }
133
134 PyBytes_AsStringAndSize(readResult, &readBuffer, &readSize);
135 }
136 else {
137 assert(self->buffer.buf);
138
139 /* Only support contiguous C arrays for now */
140 assert(self->buffer.strides == NULL &&
141 self->buffer.suboffsets == NULL);
142 assert(self->buffer.itemsize == 1);
143
144 /* TODO avoid memcpy() below */
145 readBuffer = (char *)self->buffer.buf + self->bufferOffset;
146 bufferRemaining = self->buffer.len - self->bufferOffset;
147 readSize = min(bufferRemaining, (Py_ssize_t)self->inSize);
148 self->bufferOffset += readSize;
149 }
150
151 if (readSize) {
152 if (!self->readCount && self->skipBytes) {
153 assert(self->skipBytes < self->inSize);
154 if ((Py_ssize_t)self->skipBytes >= readSize) {
155 PyErr_SetString(PyExc_ValueError,
156 "skip_bytes larger than first input chunk; "
157 "this scenario is currently unsupported");
158 Py_XDECREF(readResult);
159 return NULL;
160 }
161
162 readBuffer = readBuffer + self->skipBytes;
163 readSize -= self->skipBytes;
164 }
165
166 /* Copy input into previously allocated buffer because it can live
167 longer than a single function call and we don't want to keep a ref
168 to a Python object around. This could be changed... */
169 memcpy((void *)self->input.src, readBuffer, readSize);
170 self->input.size = readSize;
171 self->input.pos = 0;
172 }
173 /* No bytes on first read must mean an empty input stream. */
174 else if (!self->readCount) {
175 self->finishedInput = 1;
176 self->finishedOutput = 1;
177 Py_XDECREF(readResult);
178 PyErr_SetString(PyExc_StopIteration, "empty input");
179 return NULL;
180 }
181 else {
182 self->finishedInput = 1;
183 }
184
185 /* We've copied the data managed by memory. Discard the Python object.
186 */
187 Py_XDECREF(readResult);
188 }
189
190 result = read_decompressor_iterator(self);
191 if (result.errored || result.chunk) {
192 return result.chunk;
193 }
194
195 /* No new output data. Try again unless we know there is no more data. */
196 if (!self->finishedInput) {
197 goto read_from_source;
198 }
199
200 PyErr_SetString(PyExc_StopIteration, "input exhausted");
201 return NULL;
202 }
203
204 PyType_Slot ZstdDecompressorIteratorSlots[] = {
205 {Py_tp_dealloc, ZstdDecompressorIterator_dealloc},
206 {Py_tp_iter, ZstdDecompressorIterator_iter},
207 {Py_tp_iternext, ZstdDecompressorIterator_iternext},
208 {Py_tp_new, PyType_GenericNew},
209 {0, NULL},
210 };
211
212 PyType_Spec ZstdDecompressorIteratorSpec = {
213 "zstandard.backend_c.ZstdDecompressorIterator",
214 sizeof(ZstdDecompressorIterator),
215 0,
216 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
217 ZstdDecompressorIteratorSlots,
218 };
219
220 PyTypeObject *ZstdDecompressorIteratorType;
221
222 void decompressoriterator_module_init(PyObject *mod) {
223 ZstdDecompressorIteratorType =
224 (PyTypeObject *)PyType_FromSpec(&ZstdDecompressorIteratorSpec);
225 if (PyType_Ready(ZstdDecompressorIteratorType) < 0) {
226 return;
227 }
228 }