| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
#include <zstd.h> |
| 23 |
|
| 24 |
typedef struct { |
| 25 |
PyObject_HEAD |
| 26 |
|
| 27 |
|
| 28 |
ZSTD_CCtx *cctx; |
| 29 |
|
| 30 |
|
| 31 |
PyObject *dict; |
| 32 |
|
| 33 |
|
| 34 |
int last_mode; |
| 35 |
|
| 36 |
|
| 37 |
int use_multithread; |
| 38 |
|
| 39 |
|
| 40 |
int compression_level; |
| 41 |
|
| 42 |
|
| 43 |
PyMutex lock; |
| 44 |
} ZstdCompressor; |
| 45 |
|
| 46 |
#define ZstdCompressor_CAST(op) ((ZstdCompressor *)op) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static int |
| 59 |
zstd_contentsize_converter(PyObject *size, unsigned long long *p) |
| 60 |
{ |
| 61 |
|
| 62 |
if (size == Py_None) { |
| 63 |
*p = ZSTD_CONTENTSIZE_UNKNOWN; |
| 64 |
} |
| 65 |
else { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
unsigned long long pledged_size = PyLong_AsUnsignedLongLong(size); |
| 70 |
|
| 71 |
|
| 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 |
|
| 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 |
|
| 110 |
self->compression_level = level; |
| 111 |
|
| 112 |
|
| 113 |
size_t zstd_ret = ZSTD_CCtx_setParameter( |
| 114 |
self->cctx, ZSTD_c_compressionLevel, level); |
| 115 |
|
| 116 |
|
| 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 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if (value_v != 0) { |
| 180 |
self->use_multithread = 1; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
size_t zstd_ret = ZSTD_CCtx_setParameter(self->cctx, key_v, value_v); |
| 186 |
|
| 187 |
|
| 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 |
|
| 214 |
level = PyLong_FromLong(compressionLevel); |
| 215 |
if (level == NULL) { |
| 216 |
goto error; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
ret = PyDict_GetItemRef(self->c_dicts, level, &capsule); |
| 221 |
if (ret < 0) { |
| 222 |
goto error; |
| 223 |
} |
| 224 |
if (capsule == NULL) { |
| 225 |
|
| 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 |
|
| 242 |
capsule = PyCapsule_New(cdict, NULL, capsule_free_cdict); |
| 243 |
if (capsule == NULL) { |
| 244 |
ZSTD_freeCDict(cdict); |
| 245 |
goto error; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
ret = PyDict_SetItem(self->c_dicts, level, capsule); |
| 250 |
if (ret < 0) { |
| 251 |
goto error; |
| 252 |
} |
| 253 |
} |
| 254 |
else { |
| 255 |
|
| 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 |
|
| 275 |
ZSTD_CDict *c_dict = _get_CDict(zd, self->compression_level); |
| 276 |
if (c_dict == NULL) { |
| 277 |
return -1; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
zstd_ret = ZSTD_CCtx_refCDict(self->cctx, c_dict); |
| 282 |
} |
| 283 |
else if (type == DICT_TYPE_UNDIGESTED) { |
| 284 |
|
| 285 |
|
| 286 |
zstd_ret = ZSTD_CCtx_loadDictionary(self->cctx, zd->dict_buffer, |
| 287 |
zd->dict_len); |
| 288 |
} |
| 289 |
else if (type == DICT_TYPE_PREFIX) { |
| 290 |
|
| 291 |
zstd_ret = ZSTD_CCtx_refPrefix(self->cctx, zd->dict_buffer, |
| 292 |
zd->dict_len); |
| 293 |
} |
| 294 |
else { |
| 295 |
Py_UNREACHABLE(); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 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 |
|
| 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 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
static PyObject * |
| 340 |
_zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level, |
| 341 |
PyObject *options, PyObject *zstd_dict) |
| 342 |
|
| 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->compression_level = ZSTD_CLEVEL_DEFAULT; |
| 351 |
self->dict = NULL; |
| 352 |
self->lock = (PyMutex){0}; |
| 353 |
|
| 354 |
|
| 355 |
self->cctx = ZSTD_createCCtx(); |
| 356 |
if (self->cctx == NULL) { |
| 357 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 358 |
if (mod_state != NULL) { |
| 359 |
PyErr_SetString(mod_state->ZstdError, |
| 360 |
"Unable to create ZSTD_CCtx instance."); |
| 361 |
} |
| 362 |
goto error; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
self->last_mode = ZSTD_e_end; |
| 367 |
|
| 368 |
if (level != Py_None && options != Py_None) { |
| 369 |
PyErr_SetString(PyExc_TypeError, |
| 370 |
"Only one of level or options should be used."); |
| 371 |
goto error; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
if (level != Py_None) { |
| 376 |
if (!PyLong_Check(level)) { |
| 377 |
PyErr_SetString(PyExc_TypeError, |
| 378 |
"invalid type for level, expected int"); |
| 379 |
goto error; |
| 380 |
} |
| 381 |
int level_v = PyLong_AsInt(level); |
| 382 |
if (level_v == -1 && PyErr_Occurred()) { |
| 383 |
if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 384 |
PyErr_Format(PyExc_ValueError, |
| 385 |
"illegal compression level; the valid range is [%d, %d]", |
| 386 |
ZSTD_minCLevel(), ZSTD_maxCLevel()); |
| 387 |
} |
| 388 |
goto error; |
| 389 |
} |
| 390 |
if (_zstd_set_c_level(self, level_v) < 0) { |
| 391 |
goto error; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
if (options != Py_None) { |
| 397 |
if (_zstd_set_c_parameters(self, options) < 0) { |
| 398 |
goto error; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
if (zstd_dict != Py_None) { |
| 404 |
if (_zstd_load_c_dict(self, zstd_dict) < 0) { |
| 405 |
goto error; |
| 406 |
} |
| 407 |
Py_INCREF(zstd_dict); |
| 408 |
self->dict = zstd_dict; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
PyObject_GC_Track(self); |
| 413 |
|
| 414 |
return (PyObject*)self; |
| 415 |
|
| 416 |
error: |
| 417 |
Py_XDECREF(self); |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
|
| 421 |
static void |
| 422 |
ZstdCompressor_dealloc(PyObject *ob) |
| 423 |
{ |
| 424 |
ZstdCompressor *self = ZstdCompressor_CAST(ob); |
| 425 |
|
| 426 |
PyObject_GC_UnTrack(self); |
| 427 |
|
| 428 |
|
| 429 |
if (self->cctx) { |
| 430 |
ZSTD_freeCCtx(self->cctx); |
| 431 |
} |
| 432 |
|
| 433 |
assert(!PyMutex_IsLocked(&self->lock)); |
| 434 |
|
| 435 |
|
| 436 |
Py_CLEAR(self->dict); |
| 437 |
|
| 438 |
PyTypeObject *tp = Py_TYPE(self); |
| 439 |
PyObject_GC_Del(ob); |
| 440 |
Py_DECREF(tp); |
| 441 |
} |
| 442 |
|
| 443 |
static PyObject * |
| 444 |
compress_lock_held(ZstdCompressor *self, Py_buffer *data, |
| 445 |
ZSTD_EndDirective end_directive) |
| 446 |
{ |
| 447 |
assert(PyMutex_IsLocked(&self->lock)); |
| 448 |
ZSTD_inBuffer in; |
| 449 |
ZSTD_outBuffer out; |
| 450 |
_BlocksOutputBuffer buffer = {.list = NULL}; |
| 451 |
size_t zstd_ret; |
| 452 |
PyObject *ret; |
| 453 |
|
| 454 |
|
| 455 |
if (data != NULL) { |
| 456 |
in.src = data->buf; |
| 457 |
in.size = data->len; |
| 458 |
in.pos = 0; |
| 459 |
} |
| 460 |
else { |
| 461 |
in.src = ∈ |
| 462 |
in.size = 0; |
| 463 |
in.pos = 0; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
size_t output_buffer_size = ZSTD_compressBound(in.size); |
| 468 |
if (output_buffer_size > (size_t) PY_SSIZE_T_MAX) { |
| 469 |
PyErr_NoMemory(); |
| 470 |
goto error; |
| 471 |
} |
| 472 |
|
| 473 |
if (_OutputBuffer_InitWithSize(&buffer, &out, -1, |
| 474 |
(Py_ssize_t) output_buffer_size) < 0) { |
| 475 |
goto error; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
while (1) { |
| 481 |
Py_BEGIN_ALLOW_THREADS |
| 482 |
zstd_ret = ZSTD_compressStream2(self->cctx, &out, &in, end_directive); |
| 483 |
Py_END_ALLOW_THREADS |
| 484 |
|
| 485 |
|
| 486 |
if (ZSTD_isError(zstd_ret)) { |
| 487 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 488 |
set_zstd_error(mod_state, ERR_COMPRESS, zstd_ret); |
| 489 |
goto error; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
if (zstd_ret == 0) { |
| 494 |
break; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
assert(out.pos == out.size); |
| 499 |
if (out.pos == out.size) { |
| 500 |
if (_OutputBuffer_Grow(&buffer, &out) < 0) { |
| 501 |
goto error; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
ret = _OutputBuffer_Finish(&buffer, &out); |
| 508 |
if (ret != NULL) { |
| 509 |
return ret; |
| 510 |
} |
| 511 |
|
| 512 |
error: |
| 513 |
_OutputBuffer_OnError(&buffer); |
| 514 |
return NULL; |
| 515 |
} |
| 516 |
|
| 517 |
#ifndef NDEBUG |
| 518 |
static inline int |
| 519 |
mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out) |
| 520 |
{ |
| 521 |
return in->size == in->pos && out->size != out->pos; |
| 522 |
} |
| 523 |
#endif |
| 524 |
|
| 525 |
static PyObject * |
| 526 |
compress_mt_continue_lock_held(ZstdCompressor *self, Py_buffer *data) |
| 527 |
{ |
| 528 |
assert(PyMutex_IsLocked(&self->lock)); |
| 529 |
ZSTD_inBuffer in; |
| 530 |
ZSTD_outBuffer out; |
| 531 |
_BlocksOutputBuffer buffer = {.list = NULL}; |
| 532 |
size_t zstd_ret; |
| 533 |
PyObject *ret; |
| 534 |
|
| 535 |
|
| 536 |
in.src = data->buf; |
| 537 |
in.size = data->len; |
| 538 |
in.pos = 0; |
| 539 |
|
| 540 |
if (_OutputBuffer_InitAndGrow(&buffer, &out, -1) < 0) { |
| 541 |
goto error; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
while (1) { |
| 546 |
Py_BEGIN_ALLOW_THREADS |
| 547 |
do { |
| 548 |
zstd_ret = ZSTD_compressStream2(self->cctx, &out, &in, |
| 549 |
ZSTD_e_continue); |
| 550 |
} while (out.pos != out.size |
| 551 |
&& in.pos != in.size |
| 552 |
&& !ZSTD_isError(zstd_ret)); |
| 553 |
Py_END_ALLOW_THREADS |
| 554 |
|
| 555 |
|
| 556 |
if (ZSTD_isError(zstd_ret)) { |
| 557 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 558 |
set_zstd_error(mod_state, ERR_COMPRESS, zstd_ret); |
| 559 |
goto error; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
if (out.pos == out.size) { |
| 564 |
if (_OutputBuffer_Grow(&buffer, &out) < 0) { |
| 565 |
goto error; |
| 566 |
} |
| 567 |
} |
| 568 |
else if (in.pos == in.size) { |
| 569 |
|
| 570 |
assert(mt_continue_should_break(&in, &out)); |
| 571 |
break; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
ret = _OutputBuffer_Finish(&buffer, &out); |
| 577 |
if (ret != NULL) { |
| 578 |
return ret; |
| 579 |
} |
| 580 |
|
| 581 |
error: |
| 582 |
_OutputBuffer_OnError(&buffer); |
| 583 |
return NULL; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
static PyObject * |
| 602 |
_zstd_ZstdCompressor_compress_impl(ZstdCompressor *self, Py_buffer *data, |
| 603 |
int mode) |
| 604 |
|
| 605 |
{ |
| 606 |
PyObject *ret; |
| 607 |
|
| 608 |
|
| 609 |
if (mode != ZSTD_e_continue && |
| 610 |
mode != ZSTD_e_flush && |
| 611 |
mode != ZSTD_e_end) |
| 612 |
{ |
| 613 |
PyErr_SetString(PyExc_ValueError, |
| 614 |
"mode argument wrong value, it should be one of " |
| 615 |
"ZstdCompressor.CONTINUE, ZstdCompressor.FLUSH_BLOCK, " |
| 616 |
"ZstdCompressor.FLUSH_FRAME."); |
| 617 |
return NULL; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
PyMutex_Lock(&self->lock); |
| 622 |
|
| 623 |
|
| 624 |
if (self->use_multithread && mode == ZSTD_e_continue) { |
| 625 |
ret = compress_mt_continue_lock_held(self, data); |
| 626 |
} |
| 627 |
else { |
| 628 |
ret = compress_lock_held(self, data, mode); |
| 629 |
} |
| 630 |
|
| 631 |
if (ret) { |
| 632 |
self->last_mode = mode; |
| 633 |
} |
| 634 |
else { |
| 635 |
self->last_mode = ZSTD_e_end; |
| 636 |
|
| 637 |
|
| 638 |
ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only); |
| 639 |
} |
| 640 |
PyMutex_Unlock(&self->lock); |
| 641 |
|
| 642 |
return ret; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
static PyObject * |
| 660 |
_zstd_ZstdCompressor_flush_impl(ZstdCompressor *self, int mode) |
| 661 |
|
| 662 |
{ |
| 663 |
PyObject *ret; |
| 664 |
|
| 665 |
|
| 666 |
if (mode != ZSTD_e_end && mode != ZSTD_e_flush) { |
| 667 |
PyErr_SetString(PyExc_ValueError, |
| 668 |
"mode argument wrong value, it should be " |
| 669 |
"ZstdCompressor.FLUSH_FRAME or " |
| 670 |
"ZstdCompressor.FLUSH_BLOCK."); |
| 671 |
return NULL; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
PyMutex_Lock(&self->lock); |
| 676 |
|
| 677 |
ret = compress_lock_held(self, NULL, mode); |
| 678 |
|
| 679 |
if (ret) { |
| 680 |
self->last_mode = mode; |
| 681 |
} |
| 682 |
else { |
| 683 |
self->last_mode = ZSTD_e_end; |
| 684 |
|
| 685 |
|
| 686 |
ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only); |
| 687 |
} |
| 688 |
PyMutex_Unlock(&self->lock); |
| 689 |
|
| 690 |
return ret; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
static PyObject * |
| 714 |
_zstd_ZstdCompressor_set_pledged_input_size_impl(ZstdCompressor *self, |
| 715 |
unsigned long long size) |
| 716 |
|
| 717 |
{ |
| 718 |
|
| 719 |
assert(size != ZSTD_CONTENTSIZE_ERROR); |
| 720 |
|
| 721 |
|
| 722 |
PyMutex_Lock(&self->lock); |
| 723 |
|
| 724 |
|
| 725 |
if (self->last_mode != ZSTD_e_end) { |
| 726 |
PyErr_SetString(PyExc_ValueError, |
| 727 |
"set_pledged_input_size() method must be called " |
| 728 |
"when last_mode == FLUSH_FRAME"); |
| 729 |
PyMutex_Unlock(&self->lock); |
| 730 |
return NULL; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
size_t zstd_ret = ZSTD_CCtx_setPledgedSrcSize(self->cctx, size); |
| 735 |
PyMutex_Unlock(&self->lock); |
| 736 |
if (ZSTD_isError(zstd_ret)) { |
| 737 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 738 |
set_zstd_error(mod_state, ERR_SET_PLEDGED_INPUT_SIZE, zstd_ret); |
| 739 |
return NULL; |
| 740 |
} |
| 741 |
|
| 742 |
Py_RETURN_NONE; |
| 743 |
} |
| 744 |
|
| 745 |
static PyMethodDef ZstdCompressor_methods[] = { |
| 746 |
_ZSTD_ZSTDCOMPRESSOR_COMPRESS_METHODDEF |
| 747 |
_ZSTD_ZSTDCOMPRESSOR_FLUSH_METHODDEF |
| 748 |
_ZSTD_ZSTDCOMPRESSOR_SET_PLEDGED_INPUT_SIZE_METHODDEF |
| 749 |
{NULL, NULL} |
| 750 |
}; |
| 751 |
|
| 752 |
PyDoc_STRVAR(ZstdCompressor_last_mode_doc, |
| 753 |
"The last mode used to this compressor object, its value can be .CONTINUE,\n" |
| 754 |
".FLUSH_BLOCK, .FLUSH_FRAME. Initialized to .FLUSH_FRAME.\n\n" |
| 755 |
"It can be used to get the current state of a compressor, such as, data\n" |
| 756 |
"flushed, or a frame ended."); |
| 757 |
|
| 758 |
static PyMemberDef ZstdCompressor_members[] = { |
| 759 |
{"last_mode", Py_T_INT, offsetof(ZstdCompressor, last_mode), |
| 760 |
Py_READONLY, ZstdCompressor_last_mode_doc}, |
| 761 |
{NULL} |
| 762 |
}; |
| 763 |
|
| 764 |
static int |
| 765 |
ZstdCompressor_traverse(PyObject *ob, visitproc visit, void *arg) |
| 766 |
{ |
| 767 |
ZstdCompressor *self = ZstdCompressor_CAST(ob); |
| 768 |
Py_VISIT(self->dict); |
| 769 |
return 0; |
| 770 |
} |
| 771 |
|
| 772 |
static int |
| 773 |
ZstdCompressor_clear(PyObject *ob) |
| 774 |
{ |
| 775 |
ZstdCompressor *self = ZstdCompressor_CAST(ob); |
| 776 |
Py_CLEAR(self->dict); |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
static PyType_Slot zstdcompressor_slots[] = { |
| 781 |
{Py_tp_new, _zstd_ZstdCompressor_new}, |
| 782 |
{Py_tp_dealloc, ZstdCompressor_dealloc}, |
| 783 |
{Py_tp_methods, ZstdCompressor_methods}, |
| 784 |
{Py_tp_members, ZstdCompressor_members}, |
| 785 |
{Py_tp_doc, (void *)_zstd_ZstdCompressor_new__doc__}, |
| 786 |
{Py_tp_traverse, ZstdCompressor_traverse}, |
| 787 |
{Py_tp_clear, ZstdCompressor_clear}, |
| 788 |
{0, 0} |
| 789 |
}; |
| 790 |
|
| 791 |
PyType_Spec zstd_compressor_type_spec = { |
| 792 |
.name = "compression.zstd.ZstdCompressor", |
| 793 |
.basicsize = sizeof(ZstdCompressor), |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 798 |
.slots = zstdcompressor_slots, |
| 799 |
}; |