Debian Codegraph @

c-ext/compressobj.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 ZstdCompressionObj_dealloc(ZstdCompressionObj *self) {
14 PyMem_Free(self->output.dst);
15 self->output.dst = NULL;
16
17 Py_XDECREF(self->compressor);
18
19 PyObject_Del(self);
20 }
21
22 static PyObject *ZstdCompressionObj_compress(ZstdCompressionObj *self,
23 PyObject *args, PyObject *kwargs) {
24 static char *kwlist[] = {"data", NULL};
25
26 Py_buffer source;
27 ZSTD_inBuffer input;
28 size_t zresult;
29 PyObject *result = NULL;
30 Py_ssize_t resultSize = 0;
31
32 if (self->finished) {
33 PyErr_SetString(ZstdError,
34 "cannot call compress() after compressor finished");
35 return NULL;
36 }
37
38 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:compress", kwlist,
39 &source)) {
40 return NULL;
41 }
42
43 input.src = source.buf;
44 input.size = source.len;
45 input.pos = 0;
46
47 while (input.pos < (size_t)source.len) {
48 Py_BEGIN_ALLOW_THREADS zresult = ZSTD_compressStream2(
49 self->compressor->cctx, &self->output, &input, ZSTD_e_continue);
50 Py_END_ALLOW_THREADS
51
52 if (ZSTD_isError(zresult)) {
53 PyErr_Format(ZstdError, "zstd compress error: %s",
54 ZSTD_getErrorName(zresult));
55 Py_CLEAR(result);
56 goto finally;
57 }
58
59 if (self->output.pos) {
60 if (result) {
61 resultSize = PyBytes_GET_SIZE(result);
62
63 if (safe_pybytes_resize(&result,
64 resultSize + self->output.pos)) {
65 Py_CLEAR(result);
66 goto finally;
67 }
68
69 memcpy(PyBytes_AS_STRING(result) + resultSize, self->output.dst,
70 self->output.pos);
71 }
72 else {
73 result = PyBytes_FromStringAndSize(self->output.dst,
74 self->output.pos);
75 if (!result) {
76 goto finally;
77 }
78 }
79
80 self->output.pos = 0;
81 }
82 }
83
84 if (NULL == result) {
85 result = PyBytes_FromString("");
86 }
87
88 finally:
89 PyBuffer_Release(&source);
90
91 return result;
92 }
93
94 static PyObject *ZstdCompressionObj_flush(ZstdCompressionObj *self,
95 PyObject *args, PyObject *kwargs) {
96 static char *kwlist[] = {"flush_mode", NULL};
97
98 int flushMode = compressorobj_flush_finish;
99 size_t zresult;
100 PyObject *result = NULL;
101 Py_ssize_t resultSize = 0;
102 ZSTD_inBuffer input;
103 ZSTD_EndDirective zFlushMode;
104
105 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:flush", kwlist,
106 &flushMode)) {
107 return NULL;
108 }
109
110 if (flushMode != compressorobj_flush_finish &&
111 flushMode != compressorobj_flush_block) {
112 PyErr_SetString(PyExc_ValueError, "flush mode not recognized");
113 return NULL;
114 }
115
116 if (self->finished) {
117 PyErr_SetString(ZstdError, "compressor object already finished");
118 return NULL;
119 }
120
121 switch (flushMode) {
122 case compressorobj_flush_block:
123 zFlushMode = ZSTD_e_flush;
124 break;
125
126 case compressorobj_flush_finish:
127 zFlushMode = ZSTD_e_end;
128 self->finished = 1;
129 break;
130
131 default:
132 PyErr_SetString(ZstdError, "unhandled flush mode");
133 return NULL;
134 }
135
136 assert(self->output.pos == 0);
137
138 input.src = NULL;
139 input.size = 0;
140 input.pos = 0;
141
142 while (1) {
143 Py_BEGIN_ALLOW_THREADS zresult = ZSTD_compressStream2(
144 self->compressor->cctx, &self->output, &input, zFlushMode);
145 Py_END_ALLOW_THREADS
146
147 if (ZSTD_isError(zresult)) {
148 PyErr_Format(ZstdError, "error ending compression stream: %s",
149 ZSTD_getErrorName(zresult));
150 return NULL;
151 }
152
153 if (self->output.pos) {
154 if (result) {
155 resultSize = PyBytes_GET_SIZE(result);
156
157 if (safe_pybytes_resize(&result,
158 resultSize + self->output.pos)) {
159 Py_XDECREF(result);
160 return NULL;
161 }
162
163 memcpy(PyBytes_AS_STRING(result) + resultSize, self->output.dst,
164 self->output.pos);
165 }
166 else {
167 result = PyBytes_FromStringAndSize(self->output.dst,
168 self->output.pos);
169 if (!result) {
170 return NULL;
171 }
172 }
173
174 self->output.pos = 0;
175 }
176
177 if (!zresult) {
178 break;
179 }
180 }
181
182 if (result) {
183 return result;
184 }
185 else {
186 return PyBytes_FromString("");
187 }
188 }
189
190 static PyMethodDef ZstdCompressionObj_methods[] = {
191 {"compress", (PyCFunction)ZstdCompressionObj_compress,
192 METH_VARARGS | METH_KEYWORDS, PyDoc_STR("compress data")},
193 {"flush", (PyCFunction)ZstdCompressionObj_flush,
194 METH_VARARGS | METH_KEYWORDS, PyDoc_STR("finish compression operation")},
195 {NULL, NULL}};
196
197 PyType_Slot ZstdCompressionObjSlots[] = {
198 {Py_tp_dealloc, ZstdCompressionObj_dealloc},
199 {Py_tp_methods, ZstdCompressionObj_methods},
200 {Py_tp_new, PyType_GenericNew},
201 {0, NULL},
202 };
203
204 PyType_Spec ZstdCompressionObjSpec = {
205 "zstandard.backend_c.ZstdCompressionObj",
206 sizeof(ZstdCompressionObj),
207 0,
208 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
209 ZstdCompressionObjSlots,
210 };
211
212 PyTypeObject *ZstdCompressionObjType;
213
214 void compressobj_module_init(PyObject *module) {
215 ZstdCompressionObjType =
216 (PyTypeObject *)PyType_FromSpec(&ZstdCompressionObjSpec);
217 if (PyType_Ready(ZstdCompressionObjType) < 0) {
218 return;
219 }
220 }