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