Debian Codegraph @

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