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