Debian Codegraph @

src/c/compression_zstd/compressor.c

raw
1 /* Low level interface to the Zstandard algorithm & the zstd library. */
2
3 /* ZstdCompressor class definitions */
4
5 /*[clinic input]
6 module _zstd
7 class _zstd.ZstdCompressor "ZstdCompressor *" "&zstd_compressor_type_spec"
8 [clinic start generated code]*/
9 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7166021db1ef7df8]*/
10
11 #ifndef Py_BUILD_CORE_BUILTIN
12 # define Py_BUILD_CORE_MODULE 1
13 #endif
14
15 #include "backports_zstd_compat.h"
16 #include "backports_zstd_edits.h"
17
18 #include "Python.h"
19
20 #include "_zstdmodule.h"
21 #include "buffer.h"
22
23 #include <stddef.h> // offsetof()
24 #include <zstd.h> // ZSTD_*()
25
26 typedef struct {
27 PyObject_HEAD
28
29 /* Compression context */
30 ZSTD_CCtx *cctx;
31
32 /* ZstdDict object in use */
33 PyObject *dict;
34
35 /* Last mode, initialized to ZSTD_e_end */
36 int last_mode;
37
38 /* (nbWorker >= 1) ? 1 : 0 */
39 int use_multithread;
40
41 /* Compression level */
42 int compression_level;
43
44 /* Lock to protect the compression context */
45 BACKPORTSZSTD_LOCK lock;
46 } ZstdCompressor;
47
48 #define ZstdCompressor_CAST(op) ((ZstdCompressor *)op)
49
50 /*[python input]
51
52 class zstd_contentsize_converter(CConverter):
53 type = 'unsigned long long'
54 converter = 'zstd_contentsize_converter'
55
56 [python start generated code]*/
57 /*[python end generated code: output=da39a3ee5e6b4b0d input=0932c350d633c7de]*/
58
59
60 static int
61 zstd_contentsize_converter(PyObject *size, unsigned long long *p)
62 {
63 // None means the user indicates the size is unknown.
64 if (size == Py_None) {
65 *p = ZSTD_CONTENTSIZE_UNKNOWN;
66 }
67 else {
68 /* ZSTD_CONTENTSIZE_UNKNOWN is 0ULL - 1
69 ZSTD_CONTENTSIZE_ERROR is 0ULL - 2
70 Users should only pass values < ZSTD_CONTENTSIZE_ERROR */
71 unsigned long long pledged_size = PyLong_AsUnsignedLongLong(size);
72 /* Here we check for (unsigned long long)-1 as a sign of an error in
73 PyLong_AsUnsignedLongLong */
74 if (pledged_size == (unsigned long long)-1 && PyErr_Occurred()) {
75 *p = ZSTD_CONTENTSIZE_ERROR;
76 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
77 PyErr_Format(PyExc_ValueError,
78 "size argument should be a positive int less "
79 "than %llu", ZSTD_CONTENTSIZE_ERROR);
80 return 0;
81 }
82 return 0;
83 }
84 if (pledged_size >= ZSTD_CONTENTSIZE_ERROR) {
85 *p = ZSTD_CONTENTSIZE_ERROR;
86 PyErr_Format(PyExc_ValueError,
87 "size argument should be a positive int less "
88 "than %llu", ZSTD_CONTENTSIZE_ERROR);
89 return 0;
90 }
91 *p = pledged_size;
92 }
93 return 1;
94 }
95
96 #include "clinic/compressor.c.h"
97
98 static int
99 _zstd_set_c_level(ZstdCompressor *self, int level)
100 {
101 /* Set integer compression level */
102 int min_level = ZSTD_minCLevel();
103 int max_level = ZSTD_maxCLevel();
104 if (level < min_level || level > max_level) {
105 PyErr_Format(PyExc_ValueError,
106 "illegal compression level %d; the valid range is [%d, %d]",
107 level, min_level, max_level);
108 return -1;
109 }
110
111 /* Save for generating ZSTD_CDICT */
112 self->compression_level = level;
113
114 /* Set compressionLevel to compression context */
115 size_t zstd_ret = ZSTD_CCtx_setParameter(
116 self->cctx, ZSTD_c_compressionLevel, level);
117
118 /* Check error */
119 if (ZSTD_isError(zstd_ret)) {
120 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
121 set_zstd_error(mod_state, ERR_SET_C_LEVEL, zstd_ret);
122 return -1;
123 }
124 return 0;
125 }
126
127 static int
128 _zstd_set_c_parameters(ZstdCompressor *self, PyObject *options)
129 {
130 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
131 if (mod_state == NULL) {
132 return -1;
133 }
134
135 if (!PyDict_Check(options)) {
136 BACKPORTSZSTD_PyErr_Format_AppendPT(PyExc_TypeError,
137 "ZstdCompressor() argument 'options' must be dict, not ",
138 options);
139 return -1;
140 }
141
142 Py_ssize_t pos = 0;
143 PyObject *key, *value;
144 while (PyDict_Next(options, &pos, &key, &value)) {
145 /* Check key type */
146 if (Py_TYPE(key) == mod_state->DParameter_type) {
147 PyErr_SetString(PyExc_TypeError,
148 "compression options dictionary key must not be a "
149 "DecompressionParameter attribute");
150 return -1;
151 }
152
153 Py_INCREF(key);
154 Py_INCREF(value);
155 int key_v = PyLong_AsInt(key);
156 Py_DECREF(key);
157 if (key_v == -1 && PyErr_Occurred()) {
158 Py_DECREF(value);
159 return -1;
160 }
161
162 int value_v = PyLong_AsInt(value);
163 Py_DECREF(value);
164 if (value_v == -1 && PyErr_Occurred()) {
165 return -1;
166 }
167
168 if (key_v == ZSTD_c_compressionLevel) {
169 if (_zstd_set_c_level(self, value_v) < 0) {
170 return -1;
171 }
172 continue;
173 }
174 if (key_v == ZSTD_c_nbWorkers) {
175 /* From the zstd library docs:
176 1. When nbWorkers >= 1, triggers asynchronous mode when
177 used with ZSTD_compressStream2().
178 2, Default value is `0`, aka "single-threaded mode" : no
179 worker is spawned, compression is performed inside
180 caller's thread, all invocations are blocking. */
181 if (value_v != 0) {
182 self->use_multithread = 1;
183 }
184 }
185
186 /* Set parameter to compression context */
187 size_t zstd_ret = ZSTD_CCtx_setParameter(self->cctx, key_v, value_v);
188
189 /* Check error */
190 if (ZSTD_isError(zstd_ret)) {
191 set_parameter_error(1, key_v, value_v);
192 return -1;
193 }
194 }
195 return 0;
196 }
197
198 static void
199 capsule_free_cdict(PyObject *capsule)
200 {
201 ZSTD_CDict *cdict = PyCapsule_GetPointer(capsule, NULL);
202 ZSTD_freeCDict(cdict);
203 }
204
205 ZSTD_CDict *
206 _get_CDict(ZstdDict *self, int compressionLevel)
207 {
208 assert(BACKPORTSZSTD_LOCK_isLocked(&self->lock));
209 PyObject *level = NULL;
210 PyObject *capsule = NULL;
211 ZSTD_CDict *cdict;
212 int ret;
213
214
215 /* int level object */
216 level = PyLong_FromLong(compressionLevel);
217 if (level == NULL) {
218 goto error;
219 }
220
221 /* Get PyCapsule object from self->c_dicts */
222 ret = PyDict_GetItemRef(self->c_dicts, level, &capsule);
223 if (ret < 0) {
224 goto error;
225 }
226 if (capsule == NULL) {
227 /* Create ZSTD_CDict instance */
228 Py_BEGIN_ALLOW_THREADS
229 cdict = ZSTD_createCDict(self->dict_buffer, self->dict_len,
230 compressionLevel);
231 Py_END_ALLOW_THREADS
232
233 if (cdict == NULL) {
234 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
235 if (mod_state != NULL) {
236 PyErr_SetString(mod_state->ZstdError,
237 "Failed to create a ZSTD_CDict instance from "
238 "Zstandard dictionary content.");
239 }
240 goto error;
241 }
242
243 /* Put ZSTD_CDict instance into PyCapsule object */
244 capsule = PyCapsule_New(cdict, NULL, capsule_free_cdict);
245 if (capsule == NULL) {
246 ZSTD_freeCDict(cdict);
247 goto error;
248 }
249
250 /* Add PyCapsule object to self->c_dicts */
251 ret = PyDict_SetItem(self->c_dicts, level, capsule);
252 if (ret < 0) {
253 goto error;
254 }
255 }
256 else {
257 /* ZSTD_CDict instance already exists */
258 cdict = PyCapsule_GetPointer(capsule, NULL);
259 }
260 goto success;
261
262 error:
263 cdict = NULL;
264 success:
265 Py_XDECREF(level);
266 Py_XDECREF(capsule);
267 return cdict;
268 }
269
270 static int
271 _zstd_load_impl(ZstdCompressor *self, ZstdDict *zd,
272 _zstd_state *mod_state, int type)
273 {
274 size_t zstd_ret;
275 if (type == DICT_TYPE_DIGESTED) {
276 /* Get ZSTD_CDict */
277 ZSTD_CDict *c_dict = _get_CDict(zd, self->compression_level);
278 if (c_dict == NULL) {
279 return -1;
280 }
281 /* Reference a prepared dictionary.
282 It overrides some compression context's parameters. */
283 zstd_ret = ZSTD_CCtx_refCDict(self->cctx, c_dict);
284 }
285 else if (type == DICT_TYPE_UNDIGESTED) {
286 /* Load a dictionary.
287 It doesn't override compression context's parameters. */
288 zstd_ret = ZSTD_CCtx_loadDictionary(self->cctx, zd->dict_buffer,
289 zd->dict_len);
290 }
291 else if (type == DICT_TYPE_PREFIX) {
292 /* Load a prefix */
293 zstd_ret = ZSTD_CCtx_refPrefix(self->cctx, zd->dict_buffer,
294 zd->dict_len);
295 }
296 else {
297 Py_UNREACHABLE();
298 }
299
300 /* Check error */
301 if (ZSTD_isError(zstd_ret)) {
302 set_zstd_error(mod_state, ERR_LOAD_C_DICT, zstd_ret);
303 return -1;
304 }
305 return 0;
306 }
307
308 static int
309 _zstd_load_c_dict(ZstdCompressor *self, PyObject *dict)
310 {
311 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
312 /* When compressing, use undigested dictionary by default. */
313 int type = DICT_TYPE_UNDIGESTED;
314 ZstdDict *zd = _Py_parse_zstd_dict(mod_state, dict, &type);
315 if (zd == NULL) {
316 return -1;
317 }
318 int ret;
319 BACKPORTSZSTD_LOCK_lock(&zd->lock);
320 ret = _zstd_load_impl(self, zd, mod_state, type);
321 BACKPORTSZSTD_LOCK_unlock(&zd->lock);
322 return ret;
323 }
324
325 /*[clinic input]
326 @classmethod
327 _zstd.ZstdCompressor.__new__ as _zstd_ZstdCompressor_new
328 level: object = None
329 The compression level to use. Defaults to COMPRESSION_LEVEL_DEFAULT.
330 options: object = None
331 A dict object that contains advanced compression parameters.
332 zstd_dict: object = None
333 A ZstdDict object, a pre-trained Zstandard dictionary.
334
335 Create a compressor object for compressing data incrementally.
336
337 Thread-safe at method level. For one-shot compression, use the compress()
338 function instead.
339 [clinic start generated code]*/
340
341 static PyObject *
342 _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
343 PyObject *options, PyObject *zstd_dict)
344 /*[clinic end generated code: output=cdef61eafecac3d7 input=92de0211ae20ffdc]*/
345 {
346 ZstdCompressor* self = PyObject_GC_New(ZstdCompressor, type);
347 if (self == NULL) {
348 goto error;
349 }
350
351 self->use_multithread = 0;
352 self->dict = NULL;
353 self->lock = BACKPORTSZSTD_LOCK_allocate();
354 if(BACKPORTSZSTD_LOCK_isError(self->lock)) {
355 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
356 if (mod_state != NULL) {
357 PyErr_SetString(mod_state->ZstdError,
358 "Unable to allocate lock");
359 }
360 goto error;
361 }
362
363 /* Compression context */
364 self->cctx = ZSTD_createCCtx();
365 if (self->cctx == NULL) {
366 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
367 if (mod_state != NULL) {
368 PyErr_SetString(mod_state->ZstdError,
369 "Unable to create ZSTD_CCtx instance.");
370 }
371 goto error;
372 }
373
374 /* Last mode */
375 self->last_mode = ZSTD_e_end;
376
377 if (level != Py_None && options != Py_None) {
378 PyErr_SetString(PyExc_TypeError,
379 "Only one of level or options should be used.");
380 goto error;
381 }
382
383 /* Set compression level */
384 if (level != Py_None) {
385 if (!PyLong_Check(level)) {
386 PyErr_SetString(PyExc_TypeError,
387 "invalid type for level, expected int");
388 goto error;
389 }
390 int level_v = PyLong_AsInt(level);
391 if (level_v == -1 && PyErr_Occurred()) {
392 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
393 PyErr_Format(PyExc_ValueError,
394 "illegal compression level; the valid range is [%d, %d]",
395 ZSTD_minCLevel(), ZSTD_maxCLevel());
396 }
397 goto error;
398 }
399 if (_zstd_set_c_level(self, level_v) < 0) {
400 goto error;
401 }
402 }
403
404 /* Set options dictionary */
405 if (options != Py_None) {
406 if (_zstd_set_c_parameters(self, options) < 0) {
407 goto error;
408 }
409 }
410
411 /* Load Zstandard dictionary to compression context */
412 if (zstd_dict != Py_None) {
413 if (_zstd_load_c_dict(self, zstd_dict) < 0) {
414 goto error;
415 }
416 Py_INCREF(zstd_dict);
417 self->dict = zstd_dict;
418 }
419
420 // We can only start GC tracking once self->dict is set.
421 PyObject_GC_Track(self);
422
423 return (PyObject*)self;
424
425 error:
426 Py_XDECREF(self);
427 return NULL;
428 }
429
430 static void
431 ZstdCompressor_dealloc(PyObject *ob)
432 {
433 ZstdCompressor *self = ZstdCompressor_CAST(ob);
434
435 PyObject_GC_UnTrack(self);
436
437 /* Free compression context */
438 if (self->cctx) {
439 ZSTD_freeCCtx(self->cctx);
440 }
441
442 assert(!BACKPORTSZSTD_LOCK_isLocked(&self->lock));
443 BACKPORTSZSTD_LOCK_free(self->lock);
444
445 /* Py_XDECREF the dict after free the compression context */
446 Py_CLEAR(self->dict);
447
448 PyTypeObject *tp = Py_TYPE(self);
449 PyObject_GC_Del(ob);
450 Py_DECREF(tp);
451 }
452
453 static PyObject *
454 compress_lock_held(ZstdCompressor *self, Py_buffer *data,
455 ZSTD_EndDirective end_directive)
456 {
457 assert(BACKPORTSZSTD_LOCK_isLocked(&self->lock));
458 ZSTD_inBuffer in;
459 ZSTD_outBuffer out;
460 _BlocksOutputBuffer buffer = {.list = NULL};
461 size_t zstd_ret;
462 PyObject *ret;
463
464 /* Prepare input & output buffers */
465 if (data != NULL) {
466 in.src = data->buf;
467 in.size = data->len;
468 in.pos = 0;
469 }
470 else {
471 in.src = &in;
472 in.size = 0;
473 in.pos = 0;
474 }
475
476 /* Calculate output buffer's size */
477 size_t output_buffer_size = ZSTD_compressBound(in.size);
478 if (output_buffer_size > (size_t) PY_SSIZE_T_MAX) {
479 PyErr_NoMemory();
480 goto error;
481 }
482
483 if (_OutputBuffer_InitWithSize(&buffer, &out, -1,
484 (Py_ssize_t) output_buffer_size) < 0) {
485 goto error;
486 }
487
488
489 /* Zstandard stream compress */
490 while (1) {
491 Py_BEGIN_ALLOW_THREADS
492 zstd_ret = ZSTD_compressStream2(self->cctx, &out, &in, end_directive);
493 Py_END_ALLOW_THREADS
494
495 /* Check error */
496 if (ZSTD_isError(zstd_ret)) {
497 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
498 set_zstd_error(mod_state, ERR_COMPRESS, zstd_ret);
499 goto error;
500 }
501
502 /* Finished */
503 if (zstd_ret == 0) {
504 break;
505 }
506
507 /* Output buffer should be exhausted, grow the buffer. */
508 assert(out.pos == out.size);
509 if (out.pos == out.size) {
510 if (_OutputBuffer_Grow(&buffer, &out) < 0) {
511 goto error;
512 }
513 }
514 }
515
516 /* Return a bytes object */
517 ret = _OutputBuffer_Finish(&buffer, &out);
518 if (ret != NULL) {
519 return ret;
520 }
521
522 error:
523 _OutputBuffer_OnError(&buffer);
524 return NULL;
525 }
526
527 #ifndef NDEBUG
528 static inline int
529 mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out)
530 {
531 return in->size == in->pos && out->size != out->pos;
532 }
533 #endif
534
535 static PyObject *
536 compress_mt_continue_lock_held(ZstdCompressor *self, Py_buffer *data)
537 {
538 assert(BACKPORTSZSTD_LOCK_isLocked(&self->lock));
539 ZSTD_inBuffer in;
540 ZSTD_outBuffer out;
541 _BlocksOutputBuffer buffer = {.list = NULL};
542 size_t zstd_ret;
543 PyObject *ret;
544
545 /* Prepare input & output buffers */
546 in.src = data->buf;
547 in.size = data->len;
548 in.pos = 0;
549
550 if (_OutputBuffer_InitAndGrow(&buffer, &out, -1) < 0) {
551 goto error;
552 }
553
554 /* Zstandard stream compress */
555 while (1) {
556 Py_BEGIN_ALLOW_THREADS
557 do {
558 zstd_ret = ZSTD_compressStream2(self->cctx, &out, &in,
559 ZSTD_e_continue);
560 } while (out.pos != out.size
561 && in.pos != in.size
562 && !ZSTD_isError(zstd_ret));
563 Py_END_ALLOW_THREADS
564
565 /* Check error */
566 if (ZSTD_isError(zstd_ret)) {
567 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
568 set_zstd_error(mod_state, ERR_COMPRESS, zstd_ret);
569 goto error;
570 }
571
572 /* Like compress_lock_held(), output as much as possible. */
573 if (out.pos == out.size) {
574 if (_OutputBuffer_Grow(&buffer, &out) < 0) {
575 goto error;
576 }
577 }
578 else if (in.pos == in.size) {
579 /* Finished */
580 assert(mt_continue_should_break(&in, &out));
581 break;
582 }
583 }
584
585 /* Return a bytes object */
586 ret = _OutputBuffer_Finish(&buffer, &out);
587 if (ret != NULL) {
588 return ret;
589 }
590
591 error:
592 _OutputBuffer_OnError(&buffer);
593 return NULL;
594 }
595
596 /*[clinic input]
597 _zstd.ZstdCompressor.compress
598
599 data: Py_buffer
600 mode: int(c_default="ZSTD_e_continue") = ZstdCompressor.CONTINUE
601 Can be these 3 values ZstdCompressor.CONTINUE,
602 ZstdCompressor.FLUSH_BLOCK, ZstdCompressor.FLUSH_FRAME
603
604 Provide data to the compressor object.
605
606 Return a chunk of compressed data if possible, or b'' otherwise. When you have
607 finished providing data to the compressor, call the flush() method to finish
608 the compression process.
609 [clinic start generated code]*/
610
611 static PyObject *
612 _zstd_ZstdCompressor_compress_impl(ZstdCompressor *self, Py_buffer *data,
613 int mode)
614 /*[clinic end generated code: output=ed7982d1cf7b4f98 input=ac2c21d180f579ea]*/
615 {
616 PyObject *ret;
617
618 /* Check mode value */
619 if (mode != ZSTD_e_continue &&
620 mode != ZSTD_e_flush &&
621 mode != ZSTD_e_end)
622 {
623 PyErr_SetString(PyExc_ValueError,
624 "mode argument wrong value, it should be one of "
625 "ZstdCompressor.CONTINUE, ZstdCompressor.FLUSH_BLOCK, "
626 "ZstdCompressor.FLUSH_FRAME.");
627 return NULL;
628 }
629
630 /* Thread-safe code */
631 BACKPORTSZSTD_LOCK_lock(&self->lock);
632
633 /* Compress */
634 if (self->use_multithread && mode == ZSTD_e_continue) {
635 ret = compress_mt_continue_lock_held(self, data);
636 }
637 else {
638 ret = compress_lock_held(self, data, mode);
639 }
640
641 if (ret) {
642 self->last_mode = mode;
643 }
644 else {
645 self->last_mode = ZSTD_e_end;
646
647 /* Resetting cctx's session never fail */
648 ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only);
649 }
650 BACKPORTSZSTD_LOCK_unlock(&self->lock);
651
652 return ret;
653 }
654
655 /*[clinic input]
656 _zstd.ZstdCompressor.flush
657
658 mode: int(c_default="ZSTD_e_end") = ZstdCompressor.FLUSH_FRAME
659 Can be these 2 values ZstdCompressor.FLUSH_FRAME,
660 ZstdCompressor.FLUSH_BLOCK
661
662 Finish the compression process.
663
664 Flush any remaining data left in internal buffers. Since Zstandard data
665 consists of one or more independent frames, the compressor object can still
666 be used after this method is called.
667 [clinic start generated code]*/
668
669 static PyObject *
670 _zstd_ZstdCompressor_flush_impl(ZstdCompressor *self, int mode)
671 /*[clinic end generated code: output=b7cf2c8d64dcf2e3 input=0ab19627f323cdbc]*/
672 {
673 PyObject *ret;
674
675 /* Check mode value */
676 if (mode != ZSTD_e_end && mode != ZSTD_e_flush) {
677 PyErr_SetString(PyExc_ValueError,
678 "mode argument wrong value, it should be "
679 "ZstdCompressor.FLUSH_FRAME or "
680 "ZstdCompressor.FLUSH_BLOCK.");
681 return NULL;
682 }
683
684 /* Thread-safe code */
685 BACKPORTSZSTD_LOCK_lock(&self->lock);
686
687 ret = compress_lock_held(self, NULL, mode);
688
689 if (ret) {
690 self->last_mode = mode;
691 }
692 else {
693 self->last_mode = ZSTD_e_end;
694
695 /* Resetting cctx's session never fail */
696 ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only);
697 }
698 BACKPORTSZSTD_LOCK_unlock(&self->lock);
699
700 return ret;
701 }
702
703
704 /*[clinic input]
705 _zstd.ZstdCompressor.set_pledged_input_size
706
707 size: zstd_contentsize
708 The size of the uncompressed data to be provided to the compressor.
709 /
710
711 Set the uncompressed content size to be written into the frame header.
712
713 This method can be used to ensure the header of the frame about to be written
714 includes the size of the data, unless the CompressionParameter.content_size_flag
715 is set to False. If last_mode != FLUSH_FRAME, then a RuntimeError is raised.
716
717 It is important to ensure that the pledged data size matches the actual data
718 size. If they do not match the compressed output data may be corrupted and the
719 final chunk written may be lost.
720 [clinic start generated code]*/
721
722 static PyObject *
723 _zstd_ZstdCompressor_set_pledged_input_size_impl(ZstdCompressor *self,
724 unsigned long long size)
725 /*[clinic end generated code: output=3a09e55cc0e3b4f9 input=afd8a7d78cff2eb5]*/
726 {
727 // Error occured while converting argument, should be unreachable
728 assert(size != ZSTD_CONTENTSIZE_ERROR);
729
730 /* Thread-safe code */
731 BACKPORTSZSTD_LOCK_lock(&self->lock);
732
733 /* Check the current mode */
734 if (self->last_mode != ZSTD_e_end) {
735 PyErr_SetString(PyExc_ValueError,
736 "set_pledged_input_size() method must be called "
737 "when last_mode == FLUSH_FRAME");
738 BACKPORTSZSTD_LOCK_unlock(&self->lock);
739 return NULL;
740 }
741
742 /* Set pledged content size */
743 size_t zstd_ret = ZSTD_CCtx_setPledgedSrcSize(self->cctx, size);
744 BACKPORTSZSTD_LOCK_unlock(&self->lock);
745 if (ZSTD_isError(zstd_ret)) {
746 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
747 set_zstd_error(mod_state, ERR_SET_PLEDGED_INPUT_SIZE, zstd_ret);
748 return NULL;
749 }
750
751 Py_RETURN_NONE;
752 }
753
754 static PyMethodDef ZstdCompressor_methods[] = {
755 _ZSTD_ZSTDCOMPRESSOR_COMPRESS_METHODDEF
756 _ZSTD_ZSTDCOMPRESSOR_FLUSH_METHODDEF
757 _ZSTD_ZSTDCOMPRESSOR_SET_PLEDGED_INPUT_SIZE_METHODDEF
758 {NULL, NULL}
759 };
760
761 PyDoc_STRVAR(ZstdCompressor_last_mode_doc,
762 "The last mode used to this compressor object, its value can be .CONTINUE,\n"
763 ".FLUSH_BLOCK, .FLUSH_FRAME. Initialized to .FLUSH_FRAME.\n\n"
764 "It can be used to get the current state of a compressor, such as, data\n"
765 "flushed, or a frame ended.");
766
767 static PyMemberDef ZstdCompressor_members[] = {
768 {"last_mode", Py_T_INT, offsetof(ZstdCompressor, last_mode),
769 Py_READONLY, ZstdCompressor_last_mode_doc},
770 {NULL}
771 };
772
773 static int
774 ZstdCompressor_traverse(PyObject *ob, visitproc visit, void *arg)
775 {
776 ZstdCompressor *self = ZstdCompressor_CAST(ob);
777 Py_VISIT(self->dict);
778 return 0;
779 }
780
781 static int
782 ZstdCompressor_clear(PyObject *ob)
783 {
784 ZstdCompressor *self = ZstdCompressor_CAST(ob);
785 Py_CLEAR(self->dict);
786 return 0;
787 }
788
789 static PyType_Slot zstdcompressor_slots[] = {
790 {Py_tp_new, _zstd_ZstdCompressor_new},
791 {Py_tp_dealloc, ZstdCompressor_dealloc},
792 {Py_tp_methods, ZstdCompressor_methods},
793 {Py_tp_members, ZstdCompressor_members},
794 {Py_tp_doc, (void *)_zstd_ZstdCompressor_new__doc__},
795 {Py_tp_traverse, ZstdCompressor_traverse},
796 {Py_tp_clear, ZstdCompressor_clear},
797 {0, 0}
798 };
799
800 PyType_Spec zstd_compressor_type_spec = {
801 .name = "backports.zstd.ZstdCompressor",
802 .basicsize = sizeof(ZstdCompressor),
803 // Py_TPFLAGS_IMMUTABLETYPE is not used here as several
804 // associated constants need to be added to the type.
805 // PyType_Freeze is called later to set the flag.
806 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
807 .slots = zstdcompressor_slots,
808 };