Debian Codegraph @

contrib/python-zstandard/c-ext/compressionwriter.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 extern PyObject *ZstdError;
12
13 static void ZstdCompressionWriter_dealloc(ZstdCompressionWriter *self) {
14 Py_XDECREF(self->compressor);
15 Py_XDECREF(self->writer);
16
17 PyMem_Free(self->output.dst);
18 self->output.dst = NULL;
19
20 PyObject_Del(self);
21 }
22
23 static PyObject *ZstdCompressionWriter_enter(ZstdCompressionWriter *self) {
24 if (self->closed) {
25 PyErr_SetString(PyExc_ValueError, "stream is closed");
26 return NULL;
27 }
28
29 if (self->entered) {
30 PyErr_SetString(ZstdError, "cannot __enter__ multiple times");
31 return NULL;
32 }
33
34 self->entered = 1;
35
36 Py_INCREF(self);
37 return (PyObject *)self;
38 }
39
40 static PyObject *ZstdCompressionWriter_exit(ZstdCompressionWriter *self,
41 PyObject *args) {
42 PyObject *exc_type;
43 PyObject *exc_value;
44 PyObject *exc_tb;
45 PyObject *result;
46
47 if (!PyArg_ParseTuple(args, "OOO:__exit__", &exc_type, &exc_value,
48 &exc_tb)) {
49 return NULL;
50 }
51
52 self->entered = 0;
53
54 result = PyObject_CallMethod((PyObject *)self, "close", NULL);
55
56 if (NULL == result) {
57 return NULL;
58 }
59
60 Py_RETURN_FALSE;
61 }
62
63 static PyObject *
64 ZstdCompressionWriter_memory_size(ZstdCompressionWriter *self) {
65 return PyLong_FromSize_t(ZSTD_sizeof_CCtx(self->compressor->cctx));
66 }
67
68 static PyObject *ZstdCompressionWriter_write(ZstdCompressionWriter *self,
69 PyObject *args, PyObject *kwargs) {
70 static char *kwlist[] = {"data", NULL};
71
72 PyObject *result = NULL;
73 Py_buffer source;
74 size_t zresult;
75 ZSTD_inBuffer input;
76 PyObject *res;
77 Py_ssize_t totalWrite = 0;
78
79 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:write", kwlist,
80 &source)) {
81 return NULL;
82 }
83
84 if (self->closed) {
85 PyErr_SetString(PyExc_ValueError, "stream is closed");
86 return NULL;
87 }
88
89 self->output.pos = 0;
90
91 input.src = source.buf;
92 input.size = source.len;
93 input.pos = 0;
94
95 while (input.pos < (size_t)source.len) {
96 Py_BEGIN_ALLOW_THREADS zresult = ZSTD_compressStream2(
97 self->compressor->cctx, &self->output, &input, ZSTD_e_continue);
98 Py_END_ALLOW_THREADS
99
100 if (ZSTD_isError(zresult)) {
101 PyErr_Format(ZstdError, "zstd compress error: %s",
102 ZSTD_getErrorName(zresult));
103 goto finally;
104 }
105
106 /* Copy data from output buffer to writer. */
107 if (self->output.pos) {
108 res = PyObject_CallMethod(self->writer, "write", "y#",
109 self->output.dst, self->output.pos);
110 if (NULL == res) {
111 goto finally;
112 }
113 Py_XDECREF(res);
114 totalWrite += self->output.pos;
115 self->bytesCompressed += self->output.pos;
116 }
117 self->output.pos = 0;
118 }
119
120 if (self->writeReturnRead) {
121 result = PyLong_FromSize_t(input.pos);
122 }
123 else {
124 result = PyLong_FromSsize_t(totalWrite);
125 }
126
127 finally:
128 PyBuffer_Release(&source);
129 return result;
130 }
131
132 static PyObject *ZstdCompressionWriter_flush(ZstdCompressionWriter *self,
133 PyObject *args, PyObject *kwargs) {
134 static char *kwlist[] = {"flush_mode", NULL};
135
136 size_t zresult;
137 ZSTD_inBuffer input;
138 PyObject *res;
139 Py_ssize_t totalWrite = 0;
140 unsigned flush_mode = 0;
141 ZSTD_EndDirective flush;
142
143 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|I:flush", kwlist,
144 &flush_mode)) {
145 return NULL;
146 }
147
148 switch (flush_mode) {
149 case 0:
150 flush = ZSTD_e_flush;
151 break;
152 case 1:
153 flush = ZSTD_e_end;
154 break;
155 default:
156 PyErr_Format(PyExc_ValueError, "unknown flush_mode: %d", flush_mode);
157 return NULL;
158 }
159
160 if (self->closed) {
161 PyErr_SetString(PyExc_ValueError, "stream is closed");
162 return NULL;
163 }
164
165 self->output.pos = 0;
166
167 input.src = NULL;
168 input.size = 0;
169 input.pos = 0;
170
171 while (1) {
172 Py_BEGIN_ALLOW_THREADS zresult = ZSTD_compressStream2(
173 self->compressor->cctx, &self->output, &input, flush);
174 Py_END_ALLOW_THREADS
175
176 if (ZSTD_isError(zresult)) {
177 PyErr_Format(ZstdError, "zstd compress error: %s",
178 ZSTD_getErrorName(zresult));
179 return NULL;
180 }
181
182 /* Copy data from output buffer to writer. */
183 if (self->output.pos) {
184 res = PyObject_CallMethod(self->writer, "write", "y#",
185 self->output.dst, self->output.pos);
186 if (NULL == res) {
187 return NULL;
188 }
189 Py_XDECREF(res);
190 totalWrite += self->output.pos;
191 self->bytesCompressed += self->output.pos;
192 }
193
194 self->output.pos = 0;
195
196 if (!zresult) {
197 break;
198 }
199 }
200
201 if (!self->closing && PyObject_HasAttrString(self->writer, "flush")) {
202 res = PyObject_CallMethod(self->writer, "flush", NULL);
203 if (NULL == res) {
204 return NULL;
205 }
206 Py_XDECREF(res);
207 }
208
209 return PyLong_FromSsize_t(totalWrite);
210 }
211
212 static PyObject *ZstdCompressionWriter_close(ZstdCompressionWriter *self) {
213 PyObject *result;
214
215 if (self->closed) {
216 Py_RETURN_NONE;
217 }
218
219 self->closing = 1;
220 result = PyObject_CallMethod((PyObject *)self, "flush", "I", 1);
221 self->closing = 0;
222 self->closed = 1;
223
224 if (NULL == result) {
225 return NULL;
226 }
227
228 /* Call close on underlying stream as well. */
229 if (self->closefd && PyObject_HasAttrString(self->writer, "close")) {
230 return PyObject_CallMethod(self->writer, "close", NULL);
231 }
232
233 Py_RETURN_NONE;
234 }
235
236 static PyObject *ZstdCompressionWriter_fileno(ZstdCompressionWriter *self) {
237 if (PyObject_HasAttrString(self->writer, "fileno")) {
238 return PyObject_CallMethod(self->writer, "fileno", NULL);
239 }
240 else {
241 PyErr_SetString(PyExc_OSError,
242 "fileno not available on underlying writer");
243 return NULL;
244 }
245 }
246
247 static PyObject *ZstdCompressionWriter_tell(ZstdCompressionWriter *self) {
248 return PyLong_FromUnsignedLongLong(self->bytesCompressed);
249 }
250
251 static PyObject *ZstdCompressionWriter_writelines(PyObject *self,
252 PyObject *args) {
253 PyErr_SetNone(PyExc_NotImplementedError);
254 return NULL;
255 }
256
257 static PyObject *ZstdCompressionWriter_iter(PyObject *self) {
258 set_io_unsupported_operation();
259 return NULL;
260 }
261
262 static PyObject *ZstdCompressionWriter_iternext(PyObject *self) {
263 set_io_unsupported_operation();
264 return NULL;
265 }
266
267 static PyObject *ZstdCompressionWriter_false(PyObject *self, PyObject *args) {
268 Py_RETURN_FALSE;
269 }
270
271 static PyObject *ZstdCompressionWriter_true(PyObject *self, PyObject *args) {
272 Py_RETURN_TRUE;
273 }
274
275 static PyObject *ZstdCompressionWriter_unsupported(PyObject *self,
276 PyObject *args,
277 PyObject *kwargs) {
278 set_io_unsupported_operation();
279 return NULL;
280 }
281
282 static PyMethodDef ZstdCompressionWriter_methods[] = {
283 {"__enter__", (PyCFunction)ZstdCompressionWriter_enter, METH_NOARGS,
284 PyDoc_STR("Enter a compression context.")},
285 {"__exit__", (PyCFunction)ZstdCompressionWriter_exit, METH_VARARGS,
286 PyDoc_STR("Exit a compression context.")},
287 {"close", (PyCFunction)ZstdCompressionWriter_close, METH_NOARGS, NULL},
288 {"fileno", (PyCFunction)ZstdCompressionWriter_fileno, METH_NOARGS, NULL},
289 {"isatty", (PyCFunction)ZstdCompressionWriter_false, METH_NOARGS, NULL},
290 {"readable", (PyCFunction)ZstdCompressionWriter_false, METH_NOARGS, NULL},
291 {"readline", (PyCFunction)ZstdCompressionWriter_unsupported,
292 METH_VARARGS | METH_KEYWORDS, NULL},
293 {"readlines", (PyCFunction)ZstdCompressionWriter_unsupported,
294 METH_VARARGS | METH_KEYWORDS, NULL},
295 {"seek", (PyCFunction)ZstdCompressionWriter_unsupported,
296 METH_VARARGS | METH_KEYWORDS, NULL},
297 {"seekable", ZstdCompressionWriter_false, METH_NOARGS, NULL},
298 {"truncate", (PyCFunction)ZstdCompressionWriter_unsupported,
299 METH_VARARGS | METH_KEYWORDS, NULL},
300 {"writable", ZstdCompressionWriter_true, METH_NOARGS, NULL},
301 {"writelines", ZstdCompressionWriter_writelines, METH_VARARGS, NULL},
302 {"read", (PyCFunction)ZstdCompressionWriter_unsupported,
303 METH_VARARGS | METH_KEYWORDS, NULL},
304 {"readall", (PyCFunction)ZstdCompressionWriter_unsupported,
305 METH_VARARGS | METH_KEYWORDS, NULL},
306 {"readinto", (PyCFunction)ZstdCompressionWriter_unsupported,
307 METH_VARARGS | METH_KEYWORDS, NULL},
308 {"memory_size", (PyCFunction)ZstdCompressionWriter_memory_size, METH_NOARGS,
309 PyDoc_STR("Obtain the memory size of the underlying compressor")},
310 {"write", (PyCFunction)ZstdCompressionWriter_write,
311 METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Compress data")},
312 {"flush", (PyCFunction)ZstdCompressionWriter_flush,
313 METH_VARARGS | METH_KEYWORDS,
314 PyDoc_STR("Flush data and finish a zstd frame")},
315 {"tell", (PyCFunction)ZstdCompressionWriter_tell, METH_NOARGS,
316 PyDoc_STR("Returns current number of bytes compressed")},
317 {NULL, NULL}};
318
319 static PyMemberDef ZstdCompressionWriter_members[] = {
320 {"closed", T_BOOL, offsetof(ZstdCompressionWriter, closed), READONLY, NULL},
321 {NULL}};
322
323 PyType_Slot ZstdCompressionWriterSlots[] = {
324 {Py_tp_dealloc, ZstdCompressionWriter_dealloc},
325 {Py_tp_iter, ZstdCompressionWriter_iter},
326 {Py_tp_iternext, ZstdCompressionWriter_iternext},
327 {Py_tp_methods, ZstdCompressionWriter_methods},
328 {Py_tp_members, ZstdCompressionWriter_members},
329 {Py_tp_new, PyType_GenericNew},
330 {0, NULL},
331 };
332
333 PyType_Spec ZstdCompressionWriterSpec = {
334 "zstd.ZstdCompressionWriter",
335 sizeof(ZstdCompressionWriter),
336 0,
337 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
338 ZstdCompressionWriterSlots,
339 };
340
341 PyTypeObject *ZstdCompressionWriterType;
342
343 void compressionwriter_module_init(PyObject *mod) {
344 ZstdCompressionWriterType =
345 (PyTypeObject *)PyType_FromSpec(&ZstdCompressionWriterSpec);
346 if (PyType_Ready(ZstdCompressionWriterType) < 0) {
347 return;
348 }
349
350 Py_INCREF((PyObject *)ZstdCompressionWriterType);
351 PyModule_AddObject(mod, "ZstdCompressionWriter",
352 (PyObject *)ZstdCompressionWriterType);
353 }