| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "Python.h" |
| 9 |
#include "pythread.h" |
| 10 |
#include "structmember.h" |
| 11 |
|
| 12 |
#include "inflate64.h" |
| 13 |
|
| 14 |
#if defined(_WIN32) && defined(timezone) |
| 15 |
#undef timezone |
| 16 |
#endif |
| 17 |
|
| 18 |
#ifndef Py_UNREACHABLE |
| 19 |
#define Py_UNREACHABLE() assert(0) |
| 20 |
#endif |
| 21 |
|
| 22 |
#define True 1 |
| 23 |
#define False 0 |
| 24 |
|
| 25 |
#include "_blocks_output_buffer.h" |
| 26 |
|
| 27 |
#if OUTPUT_BUFFER_MAX_BLOCK_SIZE > UINT32_MAX |
| 28 |
#error "The maximum block size accepted by zlib is UINT32_MAX." |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static inline Py_ssize_t |
| 34 |
OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length, |
| 35 |
Byte FAR **next_out, uint32_t *avail_out) |
| 36 |
{ |
| 37 |
Py_ssize_t allocated; |
| 38 |
|
| 39 |
allocated = _BlocksOutputBuffer_InitAndGrow( |
| 40 |
buffer, max_length, (void**) next_out); |
| 41 |
*avail_out = (uint32_t) allocated; |
| 42 |
return allocated; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static inline Py_ssize_t |
| 48 |
OutputBuffer_Grow(_BlocksOutputBuffer *buffer, |
| 49 |
Byte FAR **next_out, uint32_t *avail_out) |
| 50 |
{ |
| 51 |
Py_ssize_t allocated; |
| 52 |
|
| 53 |
allocated = _BlocksOutputBuffer_Grow( |
| 54 |
buffer, (void**) next_out, (Py_ssize_t) *avail_out); |
| 55 |
*avail_out = (uint32_t) allocated; |
| 56 |
return allocated; |
| 57 |
} |
| 58 |
|
| 59 |
static inline Py_ssize_t |
| 60 |
OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out) |
| 61 |
{ |
| 62 |
return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out); |
| 63 |
} |
| 64 |
|
| 65 |
static inline PyObject * |
| 66 |
OutputBuffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out) |
| 67 |
{ |
| 68 |
return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out); |
| 69 |
} |
| 70 |
|
| 71 |
static inline void |
| 72 |
OutputBuffer_OnError(_BlocksOutputBuffer *buffer) |
| 73 |
{ |
| 74 |
_BlocksOutputBuffer_OnError(buffer); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
typedef struct { |
| 93 |
Py_ssize_t left_bytes; |
| 94 |
Byte FAR *next_posi; |
| 95 |
} _Uint32Window; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static inline Py_ssize_t |
| 102 |
OutputBuffer_WindowInitWithSize(_BlocksOutputBuffer *buffer, _Uint32Window *window, |
| 103 |
Py_ssize_t init_size, |
| 104 |
Byte FAR **next_out, uint32_t *avail_out) |
| 105 |
{ |
| 106 |
Py_ssize_t allocated = _BlocksOutputBuffer_InitWithSize( |
| 107 |
buffer, init_size, (void**) next_out); |
| 108 |
|
| 109 |
if (allocated >= 0) { |
| 110 |
|
| 111 |
Py_ssize_t window_size = Py_MIN((size_t)allocated, UINT32_MAX); |
| 112 |
*avail_out = (uint32_t) window_size; |
| 113 |
|
| 114 |
window->left_bytes = allocated - window_size; |
| 115 |
window->next_posi = *next_out + window_size; |
| 116 |
} |
| 117 |
return allocated; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static inline Py_ssize_t |
| 125 |
OutputBuffer_WindowGrow(_BlocksOutputBuffer *buffer, _Uint32Window *window, |
| 126 |
Byte FAR **next_out, uint32_t *avail_out) |
| 127 |
{ |
| 128 |
Py_ssize_t allocated; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
if (*avail_out != 0) { |
| 133 |
PyErr_SetString(PyExc_SystemError, |
| 134 |
"*avail_out != 0 in OutputBuffer_WindowGrow()."); |
| 135 |
return -1; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if (window->left_bytes > 0) { |
| 140 |
Py_ssize_t window_size = Py_MIN((size_t)window->left_bytes, UINT32_MAX); |
| 141 |
|
| 142 |
*next_out = window->next_posi; |
| 143 |
*avail_out = (uint32_t) window_size; |
| 144 |
|
| 145 |
window->left_bytes -= window_size; |
| 146 |
window->next_posi += window_size; |
| 147 |
|
| 148 |
return window_size; |
| 149 |
} |
| 150 |
assert(window->left_bytes == 0); |
| 151 |
|
| 152 |
|
| 153 |
allocated = _BlocksOutputBuffer_Grow( |
| 154 |
buffer, (void**) next_out, (Py_ssize_t) *avail_out); |
| 155 |
*avail_out = (uint32_t) allocated; |
| 156 |
return allocated; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
static inline PyObject * |
| 164 |
OutputBuffer_WindowFinish(_BlocksOutputBuffer *buffer, _Uint32Window *window, |
| 165 |
uint32_t avail_out) |
| 166 |
{ |
| 167 |
Py_ssize_t real_avail_out = (Py_ssize_t) avail_out + window->left_bytes; |
| 168 |
return _BlocksOutputBuffer_Finish(buffer, real_avail_out); |
| 169 |
} |
| 170 |
|
| 171 |
static inline void |
| 172 |
OutputBuffer_WindowOnError(_BlocksOutputBuffer *buffer, _Uint32Window *window) |
| 173 |
{ |
| 174 |
_BlocksOutputBuffer_OnError(buffer); |
| 175 |
} |
| 176 |
|
| 177 |
typedef struct |
| 178 |
{ |
| 179 |
PyObject_HEAD |
| 180 |
z_stream zst; |
| 181 |
PyObject *unused_data; |
| 182 |
PyObject *unconsumed_tail; |
| 183 |
char eof; |
| 184 |
int is_initialised; |
| 185 |
PyObject *zdict; |
| 186 |
PyThread_type_lock lock; |
| 187 |
} compobject; |
| 188 |
|
| 189 |
typedef struct { |
| 190 |
PyTypeObject *Deflate64_type; |
| 191 |
PyTypeObject *Inflate64_type; |
| 192 |
PyObject *Inflate64Error; |
| 193 |
} _inflate64_state; |
| 194 |
|
| 195 |
static _inflate64_state static_state; |
| 196 |
|
| 197 |
static compobject * |
| 198 |
newcompobject(PyTypeObject *type) |
| 199 |
{ |
| 200 |
compobject *self; |
| 201 |
self = (compobject *)type->tp_alloc(type, 0); |
| 202 |
if (self == NULL) { |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
self->eof = 0; |
| 206 |
self->is_initialised = 0; |
| 207 |
self->zdict = NULL; |
| 208 |
self->unused_data = PyBytes_FromStringAndSize("", 0); |
| 209 |
if (self->unused_data == NULL) { |
| 210 |
Py_DECREF(self); |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
self->unconsumed_tail = PyBytes_FromStringAndSize("", 0); |
| 214 |
if (self->unconsumed_tail == NULL) { |
| 215 |
Py_DECREF(self); |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
self->lock = PyThread_allocate_lock(); |
| 219 |
if (self->lock == NULL) { |
| 220 |
Py_DECREF(self); |
| 221 |
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
return self; |
| 225 |
} |
| 226 |
|
| 227 |
#define ACQUIRE_LOCK(obj) do { \ |
| 228 |
if (!PyThread_acquire_lock((obj)->lock, 0)) { \ |
| 229 |
Py_BEGIN_ALLOW_THREADS \ |
| 230 |
PyThread_acquire_lock((obj)->lock, 1); \ |
| 231 |
Py_END_ALLOW_THREADS \ |
| 232 |
} } while (0) |
| 233 |
#define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock) |
| 234 |
|
| 235 |
static const char init_twice_msg[] = "__init__ method is called twice."; |
| 236 |
|
| 237 |
static voidpf zlib_alloc(voidpf opaque, uInt items, uInt size) { |
| 238 |
|
| 239 |
|
| 240 |
void* address = PyMem_RawCalloc(items, size); |
| 241 |
if (address == NULL) { |
| 242 |
|
| 243 |
return Z_NULL; |
| 244 |
} |
| 245 |
|
| 246 |
return address; |
| 247 |
} |
| 248 |
|
| 249 |
static void zlib_free(voidpf opaque, voidpf address) { |
| 250 |
PyMem_RawFree(address); |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
arrange_input_buffer(z_stream *zst, Py_ssize_t *remains) |
| 255 |
{ |
| 256 |
zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX); |
| 257 |
*remains -= zst->avail_in; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static int |
| 263 |
save_unconsumed_input(compobject *self, Py_buffer *data, int err) |
| 264 |
{ |
| 265 |
if (err == Z_STREAM_END) { |
| 266 |
|
| 267 |
|
| 268 |
if (self->zst.avail_in > 0) { |
| 269 |
Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data); |
| 270 |
Py_ssize_t new_size, left_size; |
| 271 |
PyObject *new_data; |
| 272 |
left_size = (Byte *)data->buf + data->len - self->zst.next_in; |
| 273 |
if (left_size > (PY_SSIZE_T_MAX - old_size)) { |
| 274 |
PyErr_NoMemory(); |
| 275 |
return -1; |
| 276 |
} |
| 277 |
new_size = old_size + left_size; |
| 278 |
new_data = PyBytes_FromStringAndSize(NULL, new_size); |
| 279 |
if (new_data == NULL) |
| 280 |
return -1; |
| 281 |
memcpy(PyBytes_AS_STRING(new_data), |
| 282 |
PyBytes_AS_STRING(self->unused_data), old_size); |
| 283 |
memcpy(PyBytes_AS_STRING(new_data) + old_size, |
| 284 |
self->zst.next_in, left_size); |
| 285 |
Py_SETREF(self->unused_data, new_data); |
| 286 |
self->zst.avail_in = 0; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) { |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in; |
| 295 |
PyObject *new_data = PyBytes_FromStringAndSize( |
| 296 |
(char *)self->zst.next_in, left_size); |
| 297 |
if (new_data == NULL) |
| 298 |
return -1; |
| 299 |
Py_SETREF(self->unconsumed_tail, new_data); |
| 300 |
} |
| 301 |
|
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
static PyObject * |
| 306 |
Deflater_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 307 |
{ |
| 308 |
compobject *self; |
| 309 |
self = newcompobject(type); |
| 310 |
return (PyObject*)self; |
| 311 |
} |
| 312 |
|
| 313 |
static void |
| 314 |
Deflater_dealloc(compobject *self) |
| 315 |
{ |
| 316 |
if (self->lock) { |
| 317 |
PyThread_free_lock(self->lock); |
| 318 |
} |
| 319 |
if (self->is_initialised) { |
| 320 |
int err = deflate9End(&self->zst); |
| 321 |
switch (err) { |
| 322 |
case Z_OK: |
| 323 |
break; |
| 324 |
case Z_DATA_ERROR: |
| 325 |
PyErr_SetString(PyExc_IOError, |
| 326 |
"The stream was freed prematurely (some input or output was discarded)."); |
| 327 |
break; |
| 328 |
case Z_STREAM_ERROR: |
| 329 |
PyErr_SetString(PyExc_IOError, "The stream state was inconsistent."); |
| 330 |
break; |
| 331 |
default: |
| 332 |
PyErr_BadInternalCall(); |
| 333 |
break; |
| 334 |
} |
| 335 |
} |
| 336 |
PyTypeObject *tp = Py_TYPE(self); |
| 337 |
tp->tp_free((PyObject*)self); |
| 338 |
Py_DECREF(tp); |
| 339 |
} |
| 340 |
|
| 341 |
PyDoc_STRVAR(Deflater_doc, "A Deflate64 deflater.\n\n" |
| 342 |
"Deflater.__init__(self)\n"); |
| 343 |
|
| 344 |
static int |
| 345 |
Deflater_init(compobject *self, PyObject *args, PyObject *kwargs) |
| 346 |
{ |
| 347 |
|
| 348 |
if (self->is_initialised) { |
| 349 |
PyErr_SetString(PyExc_RuntimeError, init_twice_msg); |
| 350 |
goto error; |
| 351 |
} |
| 352 |
|
| 353 |
self->zst.zalloc = zlib_alloc; |
| 354 |
self->zst.zfree = zlib_free; |
| 355 |
|
| 356 |
self->is_initialised = 1; |
| 357 |
|
| 358 |
int err = deflate9Init2(&self->zst); |
| 359 |
switch (err) { |
| 360 |
case Z_OK: |
| 361 |
goto success; |
| 362 |
case Z_MEM_ERROR: |
| 363 |
PyErr_NoMemory(); |
| 364 |
break; |
| 365 |
case Z_STREAM_ERROR: |
| 366 |
default: |
| 367 |
PyErr_BadInternalCall(); |
| 368 |
} |
| 369 |
|
| 370 |
error: |
| 371 |
return -1; |
| 372 |
|
| 373 |
success: |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
PyDoc_STRVAR(Deflater_deflate_doc, "deflate()\n" |
| 378 |
"----\n" |
| 379 |
"Deflate data with a Deflate64 compression."); |
| 380 |
|
| 381 |
static PyObject * |
| 382 |
Deflater_deflate(compobject *self, PyObject *args, PyObject *kwargs) { |
| 383 |
static char *kwlist[] = {"data", NULL}; |
| 384 |
Py_buffer data; |
| 385 |
PyObject *RetVal = NULL; |
| 386 |
int err; |
| 387 |
_BlocksOutputBuffer buffer = {.list = NULL}; |
| 388 |
|
| 389 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 390 |
"y*:Deflater.deflate", |
| 391 |
kwlist, &data)) { |
| 392 |
PyErr_Format(PyExc_ValueError, "Argument error"); |
| 393 |
return NULL; |
| 394 |
} |
| 395 |
|
| 396 |
ACQUIRE_LOCK(self); |
| 397 |
|
| 398 |
self->zst.next_in = data.buf; |
| 399 |
Py_ssize_t ibuflen = data.len; |
| 400 |
|
| 401 |
if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) { |
| 402 |
goto error; |
| 403 |
} |
| 404 |
|
| 405 |
do { |
| 406 |
arrange_input_buffer(&self->zst, &ibuflen); |
| 407 |
|
| 408 |
do { |
| 409 |
if (self->zst.avail_out == 0) { |
| 410 |
if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) { |
| 411 |
goto error; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
Py_BEGIN_ALLOW_THREADS |
| 416 |
err = deflate9(&self->zst, Z_NO_FLUSH); |
| 417 |
Py_END_ALLOW_THREADS |
| 418 |
|
| 419 |
if (err == Z_STREAM_ERROR) { |
| 420 |
goto error; |
| 421 |
} |
| 422 |
|
| 423 |
} while (self->zst.avail_out == 0); |
| 424 |
assert(self->zst.avail_in == 0); |
| 425 |
|
| 426 |
} while (ibuflen != 0); |
| 427 |
|
| 428 |
RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out); |
| 429 |
if (RetVal != NULL) { |
| 430 |
goto success; |
| 431 |
} |
| 432 |
|
| 433 |
error: |
| 434 |
OutputBuffer_OnError(&buffer); |
| 435 |
RetVal = NULL; |
| 436 |
success: |
| 437 |
RELEASE_LOCK(self); |
| 438 |
return RetVal; |
| 439 |
} |
| 440 |
|
| 441 |
PyDoc_STRVAR(Deflater_flush_doc, "flush()\n" |
| 442 |
"----\n" |
| 443 |
"Flush compressed data."); |
| 444 |
|
| 445 |
static PyObject * |
| 446 |
Deflater_flush(compobject *self, PyObject *args, PyObject *kwargs) { |
| 447 |
static char *kwlist[] = {"mode", NULL}; |
| 448 |
int err; |
| 449 |
int mode = Z_FINISH; |
| 450 |
PyObject *RetVal = NULL; |
| 451 |
_BlocksOutputBuffer buffer = {.list = NULL}; |
| 452 |
|
| 453 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 454 |
"|i:Deflater.flush", |
| 455 |
kwlist, &mode)) { |
| 456 |
PyErr_Format(PyExc_ValueError, "Argument error"); |
| 457 |
return NULL; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
if (mode == Z_NO_FLUSH) { |
| 465 |
return PyBytes_FromStringAndSize(NULL, 0); |
| 466 |
} |
| 467 |
|
| 468 |
ACQUIRE_LOCK(self); |
| 469 |
|
| 470 |
self->zst.next_in = NULL; |
| 471 |
self->zst.avail_in = 0; |
| 472 |
|
| 473 |
if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) { |
| 474 |
PyErr_NoMemory(); |
| 475 |
goto error; |
| 476 |
} |
| 477 |
|
| 478 |
do { |
| 479 |
if (self->zst.avail_out == 0) { |
| 480 |
if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) { |
| 481 |
PyErr_NoMemory(); |
| 482 |
goto error; |
| 483 |
} |
| 484 |
} |
| 485 |
Py_BEGIN_ALLOW_THREADS |
| 486 |
err = deflate9(&self->zst, mode); |
| 487 |
Py_END_ALLOW_THREADS |
| 488 |
|
| 489 |
if (err == Z_STREAM_ERROR) { |
| 490 |
PyErr_Format(PyExc_RuntimeError, "deflater9 return an unexpected return code %d\n", err); |
| 491 |
goto error; |
| 492 |
} |
| 493 |
} while (self->zst.avail_out == 0); |
| 494 |
|
| 495 |
if (err == Z_STREAM_END && mode == Z_FINISH) { |
| 496 |
err = deflate9End(&self->zst); |
| 497 |
self->eof = 1; |
| 498 |
if (err != Z_OK) { |
| 499 |
PyErr_Format(PyExc_RuntimeError, "deflater9End return an unexpected return code %d\n", err); |
| 500 |
goto error; |
| 501 |
} else { |
| 502 |
self->is_initialised = 0; |
| 503 |
} |
| 504 |
} else if (err != Z_OK && err != Z_BUF_ERROR){ |
| 505 |
PyErr_Format(PyExc_RuntimeError, "Deflater.flush got unexpected return code %d\n", err); |
| 506 |
goto error; |
| 507 |
} |
| 508 |
|
| 509 |
RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out); |
| 510 |
if (RetVal != NULL) { |
| 511 |
goto success; |
| 512 |
} |
| 513 |
|
| 514 |
error: |
| 515 |
OutputBuffer_OnError(&buffer); |
| 516 |
RetVal = NULL; |
| 517 |
|
| 518 |
success: |
| 519 |
RELEASE_LOCK(self); |
| 520 |
return RetVal; |
| 521 |
} |
| 522 |
|
| 523 |
static PyObject * |
| 524 |
Inflater_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 525 |
{ |
| 526 |
compobject *self; |
| 527 |
self = newcompobject(type); |
| 528 |
return (PyObject*)self; |
| 529 |
} |
| 530 |
|
| 531 |
static void |
| 532 |
Inflater_dealloc(compobject *self) { |
| 533 |
if (self->lock) { |
| 534 |
PyThread_free_lock(self->lock); |
| 535 |
} |
| 536 |
|
| 537 |
int err = inflate9End(&self->zst); |
| 538 |
switch (err) { |
| 539 |
case Z_OK: |
| 540 |
break; |
| 541 |
case Z_STREAM_ERROR: |
| 542 |
default: |
| 543 |
PyErr_BadInternalCall(); |
| 544 |
break; |
| 545 |
} |
| 546 |
|
| 547 |
PyTypeObject *tp = Py_TYPE(self); |
| 548 |
tp->tp_free((PyObject*)self); |
| 549 |
Py_DECREF(tp); |
| 550 |
} |
| 551 |
|
| 552 |
PyDoc_STRVAR(Inflater_doc, "A Deflate64 inflater.\n\n" |
| 553 |
"Inflater.__init__(self)\n" |
| 554 |
); |
| 555 |
|
| 556 |
static int |
| 557 |
Inflater_init(compobject *self, PyObject *args, PyObject *kwargs) |
| 558 |
{ |
| 559 |
|
| 560 |
if (self->is_initialised) { |
| 561 |
PyErr_SetString(PyExc_RuntimeError, init_twice_msg); |
| 562 |
goto error; |
| 563 |
} |
| 564 |
self->zst.opaque = NULL; |
| 565 |
self->zst.zalloc = zlib_alloc; |
| 566 |
self->zst.zfree = zlib_free; |
| 567 |
self->zst.next_in = NULL; |
| 568 |
self->zst.avail_in = 0; |
| 569 |
int err = inflate9Init2(&self->zst); |
| 570 |
switch (err) { |
| 571 |
case Z_OK: |
| 572 |
self->is_initialised = 1; |
| 573 |
goto success; |
| 574 |
case Z_STREAM_ERROR: |
| 575 |
case Z_MEM_ERROR: |
| 576 |
PyErr_NoMemory(); |
| 577 |
goto error; |
| 578 |
default: |
| 579 |
PyErr_BadInternalCall(); |
| 580 |
} |
| 581 |
|
| 582 |
error: |
| 583 |
return -1; |
| 584 |
|
| 585 |
success: |
| 586 |
return 0; |
| 587 |
} |
| 588 |
|
| 589 |
PyDoc_STRVAR(Inflater_inflate_doc, "inflate()\n" |
| 590 |
"----\n" |
| 591 |
"Inflate a Deflate64 compressed data."); |
| 592 |
|
| 593 |
static PyObject * |
| 594 |
Inflater_inflate(compobject *self, PyObject *args, PyObject *kwargs) { |
| 595 |
static char *kwlist[] = {"data", "max_length", NULL}; |
| 596 |
Py_buffer data; |
| 597 |
int max_length = 0; |
| 598 |
PyObject *RetVal = NULL; |
| 599 |
_BlocksOutputBuffer buffer = {.list = NULL}; |
| 600 |
Py_ssize_t ibuflen; |
| 601 |
int err = Z_OK; |
| 602 |
|
| 603 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 604 |
"y*|i:Inflater.inflate", kwlist, |
| 605 |
&data, &max_length)) { |
| 606 |
return NULL; |
| 607 |
} |
| 608 |
|
| 609 |
if (max_length < 0) { |
| 610 |
PyErr_SetString(PyExc_ValueError, "max_length must be non-negative"); |
| 611 |
return NULL; |
| 612 |
} else if (max_length == 0) { |
| 613 |
max_length = -1; |
| 614 |
} |
| 615 |
|
| 616 |
self->zst.next_in = data.buf; |
| 617 |
ibuflen = data.len; |
| 618 |
|
| 619 |
if (OutputBuffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) { |
| 620 |
goto abort; |
| 621 |
} |
| 622 |
|
| 623 |
ACQUIRE_LOCK(self); |
| 624 |
|
| 625 |
do { |
| 626 |
arrange_input_buffer(&self->zst, &ibuflen); |
| 627 |
|
| 628 |
do { |
| 629 |
if (self->zst.avail_out == 0) { |
| 630 |
if (OutputBuffer_GetDataSize(&buffer, self->zst.avail_out) == max_length) { |
| 631 |
goto save; |
| 632 |
} |
| 633 |
if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) { |
| 634 |
goto abort; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
Py_BEGIN_ALLOW_THREADS |
| 639 |
err = inflate9(&self->zst, Z_NO_FLUSH); |
| 640 |
Py_END_ALLOW_THREADS |
| 641 |
|
| 642 |
switch (err) { |
| 643 |
case Z_OK: |
| 644 |
case Z_BUF_ERROR: |
| 645 |
case Z_STREAM_END: |
| 646 |
break; |
| 647 |
default: |
| 648 |
goto save; |
| 649 |
} |
| 650 |
} while (self->zst.avail_out == 0); |
| 651 |
} while (err != Z_STREAM_END && ibuflen != 0); |
| 652 |
|
| 653 |
save: |
| 654 |
if (save_unconsumed_input(self, &data, err) < 0) |
| 655 |
goto abort; |
| 656 |
|
| 657 |
if (err == Z_STREAM_END) { |
| 658 |
self->eof = 1; |
| 659 |
} else if (err != Z_OK && err != Z_BUF_ERROR) { |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
PyErr_Format(PyExc_ValueError, "while decompressing data: error code is %d", err); |
| 664 |
goto abort; |
| 665 |
} |
| 666 |
RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out); |
| 667 |
if (RetVal != NULL) { |
| 668 |
goto success; |
| 669 |
} |
| 670 |
|
| 671 |
abort: |
| 672 |
OutputBuffer_OnError(&buffer); |
| 673 |
RetVal = NULL; |
| 674 |
success: |
| 675 |
RELEASE_LOCK(self); |
| 676 |
return RetVal; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
static PyMethodDef Deflater_methods[] = { |
| 681 |
{"deflate", (PyCFunction)Deflater_deflate, |
| 682 |
METH_VARARGS|METH_KEYWORDS, Deflater_deflate_doc}, |
| 683 |
{"flush", (PyCFunction)Deflater_flush, |
| 684 |
METH_VARARGS|METH_KEYWORDS, Deflater_flush_doc}, |
| 685 |
{NULL, NULL, 0, NULL} |
| 686 |
}; |
| 687 |
|
| 688 |
PyDoc_STRVAR(Deflater_eof__doc, "True if the end-of-stream marker has been reached."); |
| 689 |
|
| 690 |
static PyMemberDef Deflater_members[] = { |
| 691 |
{"eof", T_BOOL, offsetof(compobject, eof), |
| 692 |
READONLY, Deflater_eof__doc}, |
| 693 |
{NULL} |
| 694 |
}; |
| 695 |
|
| 696 |
static PyMethodDef Inflater_methods[] = { |
| 697 |
{"inflate", (PyCFunction)Inflater_inflate, |
| 698 |
METH_VARARGS|METH_KEYWORDS, Inflater_inflate_doc}, |
| 699 |
{NULL, NULL, 0, NULL} |
| 700 |
}; |
| 701 |
|
| 702 |
PyDoc_STRVAR(Inflater_eof__doc, "True if the end-of-stream marker has been reached."); |
| 703 |
|
| 704 |
static PyMemberDef Inflater_members[] = { |
| 705 |
{"eof", T_BOOL, offsetof(compobject, eof), |
| 706 |
READONLY, Inflater_eof__doc}, |
| 707 |
{NULL} |
| 708 |
}; |
| 709 |
|
| 710 |
|
| 711 |
static PyType_Slot Deflater_slots[] = { |
| 712 |
{Py_tp_new, Deflater_new}, |
| 713 |
{Py_tp_dealloc, Deflater_dealloc}, |
| 714 |
{Py_tp_init, Deflater_init}, |
| 715 |
{Py_tp_methods, Deflater_methods}, |
| 716 |
{Py_tp_members, Deflater_members}, |
| 717 |
{Py_tp_doc, (char *)Deflater_doc}, |
| 718 |
{0, 0} |
| 719 |
}; |
| 720 |
|
| 721 |
static PyType_Slot Inflater_slots[] = { |
| 722 |
{Py_tp_new, Inflater_new}, |
| 723 |
{Py_tp_dealloc, Inflater_dealloc}, |
| 724 |
{Py_tp_init, Inflater_init}, |
| 725 |
{Py_tp_methods, Inflater_methods}, |
| 726 |
{Py_tp_members, Inflater_members}, |
| 727 |
{Py_tp_doc, (char *)Inflater_doc}, |
| 728 |
{0, 0} |
| 729 |
}; |
| 730 |
|
| 731 |
static PyType_Spec Deflater_type_spec = { |
| 732 |
.name = "_inflate64.Deflater", |
| 733 |
.basicsize = sizeof(compobject), |
| 734 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 735 |
.slots = Deflater_slots, |
| 736 |
}; |
| 737 |
|
| 738 |
static PyType_Spec Inflater_type_spec = { |
| 739 |
.name = "_inflate64.Inflater", |
| 740 |
.basicsize = sizeof(compobject), |
| 741 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 742 |
.slots = Inflater_slots, |
| 743 |
}; |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
static int |
| 750 |
_inflate64_traverse(PyObject *module, visitproc visit, void *arg) |
| 751 |
{ |
| 752 |
Py_VISIT(static_state.Inflate64Error); |
| 753 |
Py_VISIT(static_state.Inflate64_type); |
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
static int |
| 758 |
_inflate64_clear(PyObject *module) |
| 759 |
{ |
| 760 |
Py_CLEAR(static_state.Inflate64Error); |
| 761 |
Py_CLEAR(static_state.Inflate64_type); |
| 762 |
return 0; |
| 763 |
} |
| 764 |
|
| 765 |
static void |
| 766 |
_inflate64_free(void *module) { |
| 767 |
_inflate64_clear((PyObject *)module); |
| 768 |
} |
| 769 |
|
| 770 |
static PyModuleDef _inflate64module = { |
| 771 |
PyModuleDef_HEAD_INIT, |
| 772 |
.m_name = "_inflate64", |
| 773 |
.m_size = -1, |
| 774 |
.m_traverse = _inflate64_traverse, |
| 775 |
.m_clear = _inflate64_clear, |
| 776 |
.m_free = _inflate64_free |
| 777 |
}; |
| 778 |
|
| 779 |
|
| 780 |
static inline int |
| 781 |
add_type_to_module(PyObject *module, const char *name, |
| 782 |
PyType_Spec *type_spec, PyTypeObject **dest) |
| 783 |
{ |
| 784 |
PyObject *temp; |
| 785 |
|
| 786 |
temp = PyType_FromSpec(type_spec); |
| 787 |
if (PyModule_AddObject(module, name, temp) < 0) { |
| 788 |
Py_XDECREF(temp); |
| 789 |
return -1; |
| 790 |
} |
| 791 |
|
| 792 |
Py_INCREF(temp); |
| 793 |
*dest = (PyTypeObject*) temp; |
| 794 |
|
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
PyMODINIT_FUNC |
| 799 |
PyInit__inflate64(void) { |
| 800 |
PyObject *module; |
| 801 |
|
| 802 |
module = PyModule_Create(&_inflate64module); |
| 803 |
if (!module) { |
| 804 |
goto error; |
| 805 |
} |
| 806 |
if (add_type_to_module(module, |
| 807 |
"Deflater", |
| 808 |
&Deflater_type_spec, |
| 809 |
&static_state.Inflate64_type) < 0) { |
| 810 |
goto error; |
| 811 |
} |
| 812 |
if (add_type_to_module(module, |
| 813 |
"Inflater", |
| 814 |
&Inflater_type_spec, |
| 815 |
&static_state.Inflate64_type) < 0) { |
| 816 |
goto error; |
| 817 |
} |
| 818 |
return module; |
| 819 |
|
| 820 |
error: |
| 821 |
_inflate64_clear(NULL); |
| 822 |
Py_XDECREF(module); |
| 823 |
|
| 824 |
return NULL; |
| 825 |
} |