| 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 "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 <stdbool.h> |
| 24 |
#include <stddef.h> |
| 25 |
#include <zstd.h> |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
PyObject_HEAD |
| 29 |
|
| 30 |
|
| 31 |
ZSTD_DCtx *dctx; |
| 32 |
|
| 33 |
|
| 34 |
PyObject *dict; |
| 35 |
|
| 36 |
|
| 37 |
char *input_buffer; |
| 38 |
size_t input_buffer_size; |
| 39 |
size_t in_begin, in_end; |
| 40 |
|
| 41 |
|
| 42 |
PyObject *unused_data; |
| 43 |
|
| 44 |
|
| 45 |
bool needs_input; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
bool eof; |
| 50 |
|
| 51 |
|
| 52 |
BACKPORTSZSTD_LOCK lock; |
| 53 |
} ZstdDecompressor; |
| 54 |
|
| 55 |
#define ZstdDecompressor_CAST(op) ((ZstdDecompressor *)op) |
| 56 |
|
| 57 |
#include "clinic/decompressor.c.h" |
| 58 |
|
| 59 |
static inline ZSTD_DDict * |
| 60 |
_get_DDict(ZstdDict *self) |
| 61 |
{ |
| 62 |
assert(BACKPORTSZSTD_LOCK_isLocked(&self->lock)); |
| 63 |
ZSTD_DDict *ret; |
| 64 |
|
| 65 |
if (self->d_dict == NULL) { |
| 66 |
|
| 67 |
Py_BEGIN_ALLOW_THREADS |
| 68 |
ret = ZSTD_createDDict(self->dict_buffer, self->dict_len); |
| 69 |
Py_END_ALLOW_THREADS |
| 70 |
self->d_dict = ret; |
| 71 |
|
| 72 |
if (self->d_dict == NULL) { |
| 73 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 74 |
if (mod_state != NULL) { |
| 75 |
PyErr_SetString(mod_state->ZstdError, |
| 76 |
"Failed to create a ZSTD_DDict instance from " |
| 77 |
"Zstandard dictionary content."); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return self->d_dict; |
| 83 |
} |
| 84 |
|
| 85 |
static int |
| 86 |
_zstd_set_d_parameters(ZstdDecompressor *self, PyObject *options) |
| 87 |
{ |
| 88 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 89 |
if (mod_state == NULL) { |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
if (!PyDict_Check(options)) { |
| 94 |
BACKPORTSZSTD_PyErr_Format_AppendPT(PyExc_TypeError, |
| 95 |
"ZstdDecompressor() argument 'options' must be dict, not ", |
| 96 |
options); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
Py_ssize_t pos = 0; |
| 101 |
PyObject *key, *value; |
| 102 |
while (PyDict_Next(options, &pos, &key, &value)) { |
| 103 |
|
| 104 |
if (Py_TYPE(key) == mod_state->CParameter_type) { |
| 105 |
PyErr_SetString(PyExc_TypeError, |
| 106 |
"decompression options dictionary key must not be a " |
| 107 |
"CompressionParameter attribute"); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
Py_INCREF(key); |
| 112 |
Py_INCREF(value); |
| 113 |
int key_v = PyLong_AsInt(key); |
| 114 |
Py_DECREF(key); |
| 115 |
if (key_v == -1 && PyErr_Occurred()) { |
| 116 |
Py_DECREF(value); |
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
int value_v = PyLong_AsInt(value); |
| 121 |
Py_DECREF(value); |
| 122 |
if (value_v == -1 && PyErr_Occurred()) { |
| 123 |
return -1; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
size_t zstd_ret = ZSTD_DCtx_setParameter(self->dctx, key_v, value_v); |
| 128 |
|
| 129 |
|
| 130 |
if (ZSTD_isError(zstd_ret)) { |
| 131 |
set_parameter_error(0, key_v, value_v); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
} |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static int |
| 139 |
_zstd_load_impl(ZstdDecompressor *self, ZstdDict *zd, |
| 140 |
_zstd_state *mod_state, int type) |
| 141 |
{ |
| 142 |
size_t zstd_ret; |
| 143 |
if (type == DICT_TYPE_DIGESTED) { |
| 144 |
|
| 145 |
ZSTD_DDict *d_dict = _get_DDict(zd); |
| 146 |
if (d_dict == NULL) { |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
zstd_ret = ZSTD_DCtx_refDDict(self->dctx, d_dict); |
| 151 |
} |
| 152 |
else if (type == DICT_TYPE_UNDIGESTED) { |
| 153 |
|
| 154 |
zstd_ret = ZSTD_DCtx_loadDictionary(self->dctx, zd->dict_buffer, |
| 155 |
zd->dict_len); |
| 156 |
} |
| 157 |
else if (type == DICT_TYPE_PREFIX) { |
| 158 |
|
| 159 |
zstd_ret = ZSTD_DCtx_refPrefix(self->dctx, zd->dict_buffer, |
| 160 |
zd->dict_len); |
| 161 |
} |
| 162 |
else { |
| 163 |
Py_UNREACHABLE(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
if (ZSTD_isError(zstd_ret)) { |
| 168 |
set_zstd_error(mod_state, ERR_LOAD_D_DICT, zstd_ret); |
| 169 |
return -1; |
| 170 |
} |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static int |
| 176 |
_zstd_load_d_dict(ZstdDecompressor *self, PyObject *dict) |
| 177 |
{ |
| 178 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 179 |
|
| 180 |
int type = DICT_TYPE_DIGESTED; |
| 181 |
ZstdDict *zd = _Py_parse_zstd_dict(mod_state, dict, &type); |
| 182 |
if (zd == NULL) { |
| 183 |
return -1; |
| 184 |
} |
| 185 |
int ret; |
| 186 |
BACKPORTSZSTD_LOCK_lock(&zd->lock); |
| 187 |
ret = _zstd_load_impl(self, zd, mod_state, type); |
| 188 |
BACKPORTSZSTD_LOCK_unlock(&zd->lock); |
| 189 |
return ret; |
| 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 |
static PyObject * |
| 217 |
decompress_lock_held(ZstdDecompressor *self, ZSTD_inBuffer *in, |
| 218 |
Py_ssize_t max_length) |
| 219 |
{ |
| 220 |
size_t zstd_ret; |
| 221 |
ZSTD_outBuffer out; |
| 222 |
_BlocksOutputBuffer buffer = {.list = NULL}; |
| 223 |
PyObject *ret; |
| 224 |
|
| 225 |
|
| 226 |
if (_OutputBuffer_InitAndGrow(&buffer, &out, max_length) < 0) { |
| 227 |
goto error; |
| 228 |
} |
| 229 |
assert(out.pos == 0); |
| 230 |
|
| 231 |
while (1) { |
| 232 |
|
| 233 |
Py_BEGIN_ALLOW_THREADS |
| 234 |
zstd_ret = ZSTD_decompressStream(self->dctx, &out, in); |
| 235 |
Py_END_ALLOW_THREADS |
| 236 |
|
| 237 |
|
| 238 |
if (ZSTD_isError(zstd_ret)) { |
| 239 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 240 |
set_zstd_error(mod_state, ERR_DECOMPRESS, zstd_ret); |
| 241 |
goto error; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
if (zstd_ret == 0) { |
| 246 |
|
| 247 |
self->eof = 1; |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
if (out.pos == out.size) { |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
if (_OutputBuffer_ReachedMaxLength(&buffer, &out)) { |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
if (_OutputBuffer_Grow(&buffer, &out) < 0) { |
| 263 |
goto error; |
| 264 |
} |
| 265 |
assert(out.pos == 0); |
| 266 |
|
| 267 |
} |
| 268 |
else if (in->pos == in->size) { |
| 269 |
|
| 270 |
break; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
ret = _OutputBuffer_Finish(&buffer, &out); |
| 276 |
if (ret != NULL) { |
| 277 |
return ret; |
| 278 |
} |
| 279 |
|
| 280 |
error: |
| 281 |
_OutputBuffer_OnError(&buffer); |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
decompressor_reset_session_lock_held(ZstdDecompressor *self) |
| 287 |
{ |
| 288 |
assert(BACKPORTSZSTD_LOCK_isLocked(&self->lock)); |
| 289 |
|
| 290 |
|
| 291 |
self->in_begin = 0; |
| 292 |
self->in_end = 0; |
| 293 |
|
| 294 |
Py_CLEAR(self->unused_data); |
| 295 |
|
| 296 |
|
| 297 |
self->needs_input = 1; |
| 298 |
self->eof = 0; |
| 299 |
|
| 300 |
|
| 301 |
ZSTD_DCtx_reset(self->dctx, ZSTD_reset_session_only); |
| 302 |
} |
| 303 |
|
| 304 |
static PyObject * |
| 305 |
stream_decompress_lock_held(ZstdDecompressor *self, Py_buffer *data, |
| 306 |
Py_ssize_t max_length) |
| 307 |
{ |
| 308 |
assert(BACKPORTSZSTD_LOCK_isLocked(&self->lock)); |
| 309 |
ZSTD_inBuffer in; |
| 310 |
PyObject *ret = NULL; |
| 311 |
int use_input_buffer; |
| 312 |
|
| 313 |
|
| 314 |
if (self->eof) { |
| 315 |
PyErr_SetString(PyExc_EOFError, |
| 316 |
"Already at the end of a Zstandard frame."); |
| 317 |
assert(ret == NULL); |
| 318 |
return NULL; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
if (self->in_begin == self->in_end) { |
| 323 |
|
| 324 |
use_input_buffer = 0; |
| 325 |
|
| 326 |
in.src = data->buf; |
| 327 |
in.size = data->len; |
| 328 |
in.pos = 0; |
| 329 |
} |
| 330 |
else if (data->len == 0) { |
| 331 |
|
| 332 |
assert(self->in_begin < self->in_end); |
| 333 |
|
| 334 |
use_input_buffer = 1; |
| 335 |
|
| 336 |
in.src = self->input_buffer + self->in_begin; |
| 337 |
in.size = self->in_end - self->in_begin; |
| 338 |
in.pos = 0; |
| 339 |
} |
| 340 |
else { |
| 341 |
|
| 342 |
use_input_buffer = 1; |
| 343 |
|
| 344 |
|
| 345 |
size_t used_now = self->in_end - self->in_begin; |
| 346 |
assert(self->in_end > self->in_begin); |
| 347 |
|
| 348 |
|
| 349 |
size_t avail_now = self->input_buffer_size - self->in_end; |
| 350 |
assert(self->input_buffer_size >= self->in_end); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
size_t avail_total = self->input_buffer_size - used_now; |
| 355 |
assert(self->input_buffer_size >= used_now); |
| 356 |
|
| 357 |
if (avail_total < (size_t) data->len) { |
| 358 |
char *tmp; |
| 359 |
size_t new_size = used_now + data->len; |
| 360 |
|
| 361 |
|
| 362 |
tmp = PyMem_Malloc(new_size); |
| 363 |
if (tmp == NULL) { |
| 364 |
PyErr_NoMemory(); |
| 365 |
goto error; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
memcpy(tmp, |
| 370 |
self->input_buffer + self->in_begin, |
| 371 |
used_now); |
| 372 |
|
| 373 |
|
| 374 |
PyMem_Free(self->input_buffer); |
| 375 |
self->input_buffer = tmp; |
| 376 |
self->input_buffer_size = new_size; |
| 377 |
|
| 378 |
|
| 379 |
self->in_begin = 0; |
| 380 |
self->in_end = used_now; |
| 381 |
} |
| 382 |
else if (avail_now < (size_t) data->len) { |
| 383 |
|
| 384 |
|
| 385 |
memmove(self->input_buffer, |
| 386 |
self->input_buffer + self->in_begin, |
| 387 |
used_now); |
| 388 |
|
| 389 |
|
| 390 |
self->in_begin = 0; |
| 391 |
self->in_end = used_now; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
memcpy(self->input_buffer + self->in_end, data->buf, data->len); |
| 396 |
self->in_end += data->len; |
| 397 |
|
| 398 |
in.src = self->input_buffer + self->in_begin; |
| 399 |
in.size = used_now + data->len; |
| 400 |
in.pos = 0; |
| 401 |
} |
| 402 |
assert(in.pos == 0); |
| 403 |
|
| 404 |
|
| 405 |
ret = decompress_lock_held(self, &in, max_length); |
| 406 |
if (ret == NULL) { |
| 407 |
goto error; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
if (in.pos == in.size) { |
| 412 |
if (Py_SIZE(ret) == max_length || self->eof) { |
| 413 |
self->needs_input = 0; |
| 414 |
} |
| 415 |
else { |
| 416 |
self->needs_input = 1; |
| 417 |
} |
| 418 |
|
| 419 |
if (use_input_buffer) { |
| 420 |
|
| 421 |
self->in_begin = 0; |
| 422 |
self->in_end = 0; |
| 423 |
} |
| 424 |
} |
| 425 |
else { |
| 426 |
size_t data_size = in.size - in.pos; |
| 427 |
|
| 428 |
self->needs_input = 0; |
| 429 |
|
| 430 |
if (!use_input_buffer) { |
| 431 |
|
| 432 |
|
| 433 |
if (self->input_buffer != NULL |
| 434 |
&& self->input_buffer_size < data_size) |
| 435 |
{ |
| 436 |
PyMem_Free(self->input_buffer); |
| 437 |
self->input_buffer = NULL; |
| 438 |
self->input_buffer_size = 0; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
if (self->input_buffer == NULL) { |
| 443 |
self->input_buffer = PyMem_Malloc(data_size); |
| 444 |
if (self->input_buffer == NULL) { |
| 445 |
PyErr_NoMemory(); |
| 446 |
goto error; |
| 447 |
} |
| 448 |
self->input_buffer_size = data_size; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
memcpy(self->input_buffer, (char*)in.src + in.pos, data_size); |
| 453 |
self->in_begin = 0; |
| 454 |
self->in_end = data_size; |
| 455 |
} |
| 456 |
else { |
| 457 |
|
| 458 |
self->in_begin += in.pos; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
return ret; |
| 463 |
|
| 464 |
error: |
| 465 |
|
| 466 |
decompressor_reset_session_lock_held(self); |
| 467 |
|
| 468 |
Py_CLEAR(ret); |
| 469 |
return NULL; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
static PyObject * |
| 488 |
_zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict, |
| 489 |
PyObject *options) |
| 490 |
|
| 491 |
{ |
| 492 |
ZstdDecompressor* self = PyObject_GC_New(ZstdDecompressor, type); |
| 493 |
if (self == NULL) { |
| 494 |
goto error; |
| 495 |
} |
| 496 |
|
| 497 |
self->input_buffer = NULL; |
| 498 |
self->input_buffer_size = 0; |
| 499 |
self->in_begin = -1; |
| 500 |
self->in_end = -1; |
| 501 |
self->unused_data = NULL; |
| 502 |
self->eof = 0; |
| 503 |
self->dict = NULL; |
| 504 |
self->lock = BACKPORTSZSTD_LOCK_allocate(); |
| 505 |
if(BACKPORTSZSTD_LOCK_isError(self->lock)) { |
| 506 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 507 |
if (mod_state != NULL) { |
| 508 |
PyErr_SetString(mod_state->ZstdError, |
| 509 |
"Unable to allocate lock"); |
| 510 |
} |
| 511 |
goto error; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
self->needs_input = 1; |
| 516 |
|
| 517 |
|
| 518 |
self->dctx = ZSTD_createDCtx(); |
| 519 |
if (self->dctx == NULL) { |
| 520 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 521 |
if (mod_state != NULL) { |
| 522 |
PyErr_SetString(mod_state->ZstdError, |
| 523 |
"Unable to create ZSTD_DCtx instance."); |
| 524 |
} |
| 525 |
goto error; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
if (zstd_dict != Py_None) { |
| 530 |
if (_zstd_load_d_dict(self, zstd_dict) < 0) { |
| 531 |
goto error; |
| 532 |
} |
| 533 |
Py_INCREF(zstd_dict); |
| 534 |
self->dict = zstd_dict; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
if (options != Py_None) { |
| 539 |
if (_zstd_set_d_parameters(self, options) < 0) { |
| 540 |
goto error; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
PyObject_GC_Track(self); |
| 546 |
|
| 547 |
return (PyObject*)self; |
| 548 |
|
| 549 |
error: |
| 550 |
Py_XDECREF(self); |
| 551 |
return NULL; |
| 552 |
} |
| 553 |
|
| 554 |
static void |
| 555 |
ZstdDecompressor_dealloc(PyObject *ob) |
| 556 |
{ |
| 557 |
ZstdDecompressor *self = ZstdDecompressor_CAST(ob); |
| 558 |
|
| 559 |
PyObject_GC_UnTrack(self); |
| 560 |
|
| 561 |
|
| 562 |
if (self->dctx) { |
| 563 |
ZSTD_freeDCtx(self->dctx); |
| 564 |
} |
| 565 |
|
| 566 |
assert(!BACKPORTSZSTD_LOCK_isLocked(&self->lock)); |
| 567 |
BACKPORTSZSTD_LOCK_free(self->lock); |
| 568 |
|
| 569 |
|
| 570 |
Py_CLEAR(self->dict); |
| 571 |
|
| 572 |
|
| 573 |
PyMem_Free(self->input_buffer); |
| 574 |
|
| 575 |
|
| 576 |
Py_CLEAR(self->unused_data); |
| 577 |
|
| 578 |
PyTypeObject *tp = Py_TYPE(self); |
| 579 |
PyObject_GC_Del(ob); |
| 580 |
Py_DECREF(tp); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
static PyObject * |
| 595 |
_zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self) |
| 596 |
|
| 597 |
{ |
| 598 |
PyObject *ret; |
| 599 |
|
| 600 |
BACKPORTSZSTD_LOCK_lock(&self->lock); |
| 601 |
|
| 602 |
if (!self->eof) { |
| 603 |
BACKPORTSZSTD_LOCK_unlock(&self->lock); |
| 604 |
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); |
| 605 |
} |
| 606 |
else { |
| 607 |
if (self->unused_data == NULL) { |
| 608 |
if (self->input_buffer == NULL) { |
| 609 |
self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); |
| 610 |
} |
| 611 |
else { |
| 612 |
self->unused_data = PyBytes_FromStringAndSize( |
| 613 |
self->input_buffer + self->in_begin, |
| 614 |
self->in_end - self->in_begin); |
| 615 |
} |
| 616 |
ret = self->unused_data; |
| 617 |
Py_XINCREF(ret); |
| 618 |
} |
| 619 |
else { |
| 620 |
ret = self->unused_data; |
| 621 |
Py_INCREF(ret); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
BACKPORTSZSTD_LOCK_unlock(&self->lock); |
| 626 |
return ret; |
| 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 |
static PyObject * |
| 657 |
_zstd_ZstdDecompressor_decompress_impl(ZstdDecompressor *self, |
| 658 |
Py_buffer *data, |
| 659 |
Py_ssize_t max_length) |
| 660 |
|
| 661 |
{ |
| 662 |
PyObject *ret; |
| 663 |
|
| 664 |
BACKPORTSZSTD_LOCK_lock(&self->lock); |
| 665 |
ret = stream_decompress_lock_held(self, data, max_length); |
| 666 |
BACKPORTSZSTD_LOCK_unlock(&self->lock); |
| 667 |
return ret; |
| 668 |
} |
| 669 |
|
| 670 |
static PyMethodDef ZstdDecompressor_methods[] = { |
| 671 |
_ZSTD_ZSTDDECOMPRESSOR_DECOMPRESS_METHODDEF |
| 672 |
{NULL, NULL} |
| 673 |
}; |
| 674 |
|
| 675 |
PyDoc_STRVAR(ZstdDecompressor_eof_doc, |
| 676 |
"True means the end of the first frame has been reached. If decompress data\n" |
| 677 |
"after that, an EOFError exception will be raised."); |
| 678 |
|
| 679 |
PyDoc_STRVAR(ZstdDecompressor_needs_input_doc, |
| 680 |
"If the max_length output limit in .decompress() method has been reached,\n" |
| 681 |
"and the decompressor has (or may has) unconsumed input data, it will be set\n" |
| 682 |
"to False. In this case, passing b'' to the .decompress() method may output\n" |
| 683 |
"further data."); |
| 684 |
|
| 685 |
static PyMemberDef ZstdDecompressor_members[] = { |
| 686 |
{"eof", Py_T_BOOL, offsetof(ZstdDecompressor, eof), |
| 687 |
Py_READONLY, ZstdDecompressor_eof_doc}, |
| 688 |
{"needs_input", Py_T_BOOL, offsetof(ZstdDecompressor, needs_input), |
| 689 |
Py_READONLY, ZstdDecompressor_needs_input_doc}, |
| 690 |
{NULL} |
| 691 |
}; |
| 692 |
|
| 693 |
static PyGetSetDef ZstdDecompressor_getset[] = { |
| 694 |
_ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF |
| 695 |
{NULL} |
| 696 |
}; |
| 697 |
|
| 698 |
static int |
| 699 |
ZstdDecompressor_traverse(PyObject *ob, visitproc visit, void *arg) |
| 700 |
{ |
| 701 |
ZstdDecompressor *self = ZstdDecompressor_CAST(ob); |
| 702 |
Py_VISIT(self->dict); |
| 703 |
return 0; |
| 704 |
} |
| 705 |
|
| 706 |
static int |
| 707 |
ZstdDecompressor_clear(PyObject *ob) |
| 708 |
{ |
| 709 |
ZstdDecompressor *self = ZstdDecompressor_CAST(ob); |
| 710 |
Py_CLEAR(self->dict); |
| 711 |
Py_CLEAR(self->unused_data); |
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
static PyType_Slot ZstdDecompressor_slots[] = { |
| 716 |
{Py_tp_new, _zstd_ZstdDecompressor_new}, |
| 717 |
{Py_tp_dealloc, ZstdDecompressor_dealloc}, |
| 718 |
{Py_tp_methods, ZstdDecompressor_methods}, |
| 719 |
{Py_tp_members, ZstdDecompressor_members}, |
| 720 |
{Py_tp_getset, ZstdDecompressor_getset}, |
| 721 |
{Py_tp_doc, (void *)_zstd_ZstdDecompressor_new__doc__}, |
| 722 |
{Py_tp_traverse, ZstdDecompressor_traverse}, |
| 723 |
{Py_tp_clear, ZstdDecompressor_clear}, |
| 724 |
{0, 0} |
| 725 |
}; |
| 726 |
|
| 727 |
PyType_Spec zstd_decompressor_type_spec = { |
| 728 |
.name = "backports.zstd.ZstdDecompressor", |
| 729 |
.basicsize = sizeof(ZstdDecompressor), |
| 730 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
| 731 |
| Py_TPFLAGS_HAVE_GC, |
| 732 |
.slots = ZstdDecompressor_slots, |
| 733 |
}; |