You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

802 lines
24 KiB

  1. /* _bz2 - Low-level Python interface to libbzip2. */
  2. #define PY_SSIZE_T_CLEAN
  3. #include "Python.h"
  4. #include "structmember.h"
  5. #ifdef WITH_THREAD
  6. #include "pythread.h"
  7. #endif
  8. #include <bzlib.h>
  9. #include <stdio.h>
  10. #ifndef BZ_CONFIG_ERROR
  11. #define BZ2_bzCompress bzCompress
  12. #define BZ2_bzCompressInit bzCompressInit
  13. #define BZ2_bzCompressEnd bzCompressEnd
  14. #define BZ2_bzDecompress bzDecompress
  15. #define BZ2_bzDecompressInit bzDecompressInit
  16. #define BZ2_bzDecompressEnd bzDecompressEnd
  17. #endif /* ! BZ_CONFIG_ERROR */
  18. #ifdef WITH_THREAD
  19. #define ACQUIRE_LOCK(obj) do { \
  20. if (!PyThread_acquire_lock((obj)->lock, 0)) { \
  21. Py_BEGIN_ALLOW_THREADS \
  22. PyThread_acquire_lock((obj)->lock, 1); \
  23. Py_END_ALLOW_THREADS \
  24. } } while (0)
  25. #define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock)
  26. #else
  27. #define ACQUIRE_LOCK(obj)
  28. #define RELEASE_LOCK(obj)
  29. #endif
  30. typedef struct {
  31. PyObject_HEAD
  32. bz_stream bzs;
  33. int flushed;
  34. #ifdef WITH_THREAD
  35. PyThread_type_lock lock;
  36. #endif
  37. } BZ2Compressor;
  38. typedef struct {
  39. PyObject_HEAD
  40. bz_stream bzs;
  41. char eof; /* T_BOOL expects a char */
  42. PyObject *unused_data;
  43. char needs_input;
  44. char *input_buffer;
  45. size_t input_buffer_size;
  46. /* bzs->avail_in is only 32 bit, so we store the true length
  47. separately. Conversion and looping is encapsulated in
  48. decompress_buf() */
  49. size_t bzs_avail_in_real;
  50. #ifdef WITH_THREAD
  51. PyThread_type_lock lock;
  52. #endif
  53. } BZ2Decompressor;
  54. static PyTypeObject BZ2Compressor_Type;
  55. static PyTypeObject BZ2Decompressor_Type;
  56. /* Helper functions. */
  57. static int
  58. catch_bz2_error(int bzerror)
  59. {
  60. switch(bzerror) {
  61. case BZ_OK:
  62. case BZ_RUN_OK:
  63. case BZ_FLUSH_OK:
  64. case BZ_FINISH_OK:
  65. case BZ_STREAM_END:
  66. return 0;
  67. #ifdef BZ_CONFIG_ERROR
  68. case BZ_CONFIG_ERROR:
  69. PyErr_SetString(PyExc_SystemError,
  70. "libbzip2 was not compiled correctly");
  71. return 1;
  72. #endif
  73. case BZ_PARAM_ERROR:
  74. PyErr_SetString(PyExc_ValueError,
  75. "Internal error - "
  76. "invalid parameters passed to libbzip2");
  77. return 1;
  78. case BZ_MEM_ERROR:
  79. PyErr_NoMemory();
  80. return 1;
  81. case BZ_DATA_ERROR:
  82. case BZ_DATA_ERROR_MAGIC:
  83. PyErr_SetString(PyExc_IOError, "Invalid data stream");
  84. return 1;
  85. case BZ_IO_ERROR:
  86. PyErr_SetString(PyExc_IOError, "Unknown I/O error");
  87. return 1;
  88. case BZ_UNEXPECTED_EOF:
  89. PyErr_SetString(PyExc_EOFError,
  90. "Compressed file ended before the logical "
  91. "end-of-stream was detected");
  92. return 1;
  93. case BZ_SEQUENCE_ERROR:
  94. PyErr_SetString(PyExc_RuntimeError,
  95. "Internal error - "
  96. "Invalid sequence of commands sent to libbzip2");
  97. return 1;
  98. default:
  99. PyErr_Format(PyExc_IOError,
  100. "Unrecognized error from libbzip2: %d", bzerror);
  101. return 1;
  102. }
  103. }
  104. #if BUFSIZ < 8192
  105. #define INITIAL_BUFFER_SIZE 8192
  106. #else
  107. #define INITIAL_BUFFER_SIZE BUFSIZ
  108. #endif
  109. static int
  110. grow_buffer(PyObject **buf, Py_ssize_t max_length)
  111. {
  112. /* Expand the buffer by an amount proportional to the current size,
  113. giving us amortized linear-time behavior. Use a less-than-double
  114. growth factor to avoid excessive allocation. */
  115. size_t size = PyBytes_GET_SIZE(*buf);
  116. size_t new_size = size + (size >> 3) + 6;
  117. if (max_length > 0 && new_size > (size_t) max_length)
  118. new_size = (size_t) max_length;
  119. if (new_size > size) {
  120. return _PyBytes_Resize(buf, new_size);
  121. } else { /* overflow */
  122. PyErr_SetString(PyExc_OverflowError,
  123. "Unable to allocate buffer - output too large");
  124. return -1;
  125. }
  126. }
  127. /* BZ2Compressor class. */
  128. static PyObject *
  129. compress(BZ2Compressor *c, char *data, size_t len, int action)
  130. {
  131. size_t data_size = 0;
  132. PyObject *result;
  133. result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
  134. if (result == NULL)
  135. return NULL;
  136. c->bzs.next_in = data;
  137. c->bzs.avail_in = 0;
  138. c->bzs.next_out = PyBytes_AS_STRING(result);
  139. c->bzs.avail_out = INITIAL_BUFFER_SIZE;
  140. for (;;) {
  141. char *this_out;
  142. int bzerror;
  143. /* On a 64-bit system, len might not fit in avail_in (an unsigned int).
  144. Do compression in chunks of no more than UINT_MAX bytes each. */
  145. if (c->bzs.avail_in == 0 && len > 0) {
  146. c->bzs.avail_in = (unsigned int)Py_MIN(len, UINT_MAX);
  147. len -= c->bzs.avail_in;
  148. }
  149. /* In regular compression mode, stop when input data is exhausted. */
  150. if (action == BZ_RUN && c->bzs.avail_in == 0)
  151. break;
  152. if (c->bzs.avail_out == 0) {
  153. size_t buffer_left = PyBytes_GET_SIZE(result) - data_size;
  154. if (buffer_left == 0) {
  155. if (grow_buffer(&result, -1) < 0)
  156. goto error;
  157. c->bzs.next_out = PyBytes_AS_STRING(result) + data_size;
  158. buffer_left = PyBytes_GET_SIZE(result) - data_size;
  159. }
  160. c->bzs.avail_out = (unsigned int)Py_MIN(buffer_left, UINT_MAX);
  161. }
  162. Py_BEGIN_ALLOW_THREADS
  163. this_out = c->bzs.next_out;
  164. bzerror = BZ2_bzCompress(&c->bzs, action);
  165. data_size += c->bzs.next_out - this_out;
  166. Py_END_ALLOW_THREADS
  167. if (catch_bz2_error(bzerror))
  168. goto error;
  169. /* In flushing mode, stop when all buffered data has been flushed. */
  170. if (action == BZ_FINISH && bzerror == BZ_STREAM_END)
  171. break;
  172. }
  173. if (data_size != (size_t)PyBytes_GET_SIZE(result))
  174. if (_PyBytes_Resize(&result, data_size) < 0)
  175. goto error;
  176. return result;
  177. error:
  178. Py_XDECREF(result);
  179. return NULL;
  180. }
  181. /*[clinic input]
  182. module _bz2
  183. class _bz2.BZ2Compressor "BZ2Compressor *" "&BZ2Compressor_Type"
  184. class _bz2.BZ2Decompressor "BZ2Decompressor *" "&BZ2Decompressor_Type"
  185. [clinic start generated code]*/
  186. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=dc7d7992a79f9cb7]*/
  187. #include "clinic/_bz2module.c.h"
  188. /*[clinic input]
  189. _bz2.BZ2Compressor.compress
  190. data: Py_buffer
  191. /
  192. Provide data to the compressor object.
  193. Returns a chunk of compressed data if possible, or b'' otherwise.
  194. When you have finished providing data to the compressor, call the
  195. flush() method to finish the compression process.
  196. [clinic start generated code]*/
  197. static PyObject *
  198. _bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data)
  199. /*[clinic end generated code: output=59365426e941fbcc input=85c963218070fc4c]*/
  200. {
  201. PyObject *result = NULL;
  202. ACQUIRE_LOCK(self);
  203. if (self->flushed)
  204. PyErr_SetString(PyExc_ValueError, "Compressor has been flushed");
  205. else
  206. result = compress(self, data->buf, data->len, BZ_RUN);
  207. RELEASE_LOCK(self);
  208. return result;
  209. }
  210. /*[clinic input]
  211. _bz2.BZ2Compressor.flush
  212. Finish the compression process.
  213. Returns the compressed data left in internal buffers.
  214. The compressor object may not be used after this method is called.
  215. [clinic start generated code]*/
  216. static PyObject *
  217. _bz2_BZ2Compressor_flush_impl(BZ2Compressor *self)
  218. /*[clinic end generated code: output=3ef03fc1b092a701 input=d64405d3c6f76691]*/
  219. {
  220. PyObject *result = NULL;
  221. ACQUIRE_LOCK(self);
  222. if (self->flushed)
  223. PyErr_SetString(PyExc_ValueError, "Repeated call to flush()");
  224. else {
  225. self->flushed = 1;
  226. result = compress(self, NULL, 0, BZ_FINISH);
  227. }
  228. RELEASE_LOCK(self);
  229. return result;
  230. }
  231. static PyObject *
  232. BZ2Compressor_getstate(BZ2Compressor *self, PyObject *noargs)
  233. {
  234. PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
  235. Py_TYPE(self)->tp_name);
  236. return NULL;
  237. }
  238. static void*
  239. BZ2_Malloc(void* ctx, int items, int size)
  240. {
  241. if (items < 0 || size < 0)
  242. return NULL;
  243. if ((size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size)
  244. return NULL;
  245. /* PyMem_Malloc() cannot be used: compress() and decompress()
  246. release the GIL */
  247. return PyMem_RawMalloc(items * size);
  248. }
  249. static void
  250. BZ2_Free(void* ctx, void *ptr)
  251. {
  252. PyMem_RawFree(ptr);
  253. }
  254. /*[clinic input]
  255. _bz2.BZ2Compressor.__init__
  256. compresslevel: int = 9
  257. Compression level, as a number between 1 and 9.
  258. /
  259. Create a compressor object for compressing data incrementally.
  260. For one-shot compression, use the compress() function instead.
  261. [clinic start generated code]*/
  262. static int
  263. _bz2_BZ2Compressor___init___impl(BZ2Compressor *self, int compresslevel)
  264. /*[clinic end generated code: output=c4e6adfd02963827 input=4e1ff7b8394b6e9a]*/
  265. {
  266. int bzerror;
  267. if (!(1 <= compresslevel && compresslevel <= 9)) {
  268. PyErr_SetString(PyExc_ValueError,
  269. "compresslevel must be between 1 and 9");
  270. return -1;
  271. }
  272. #ifdef WITH_THREAD
  273. self->lock = PyThread_allocate_lock();
  274. if (self->lock == NULL) {
  275. PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
  276. return -1;
  277. }
  278. #endif
  279. self->bzs.opaque = NULL;
  280. self->bzs.bzalloc = BZ2_Malloc;
  281. self->bzs.bzfree = BZ2_Free;
  282. bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0);
  283. if (catch_bz2_error(bzerror))
  284. goto error;
  285. return 0;
  286. error:
  287. #ifdef WITH_THREAD
  288. PyThread_free_lock(self->lock);
  289. self->lock = NULL;
  290. #endif
  291. return -1;
  292. }
  293. static void
  294. BZ2Compressor_dealloc(BZ2Compressor *self)
  295. {
  296. BZ2_bzCompressEnd(&self->bzs);
  297. #ifdef WITH_THREAD
  298. if (self->lock != NULL)
  299. PyThread_free_lock(self->lock);
  300. #endif
  301. Py_TYPE(self)->tp_free((PyObject *)self);
  302. }
  303. static PyMethodDef BZ2Compressor_methods[] = {
  304. _BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF
  305. _BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF
  306. {"__getstate__", (PyCFunction)BZ2Compressor_getstate, METH_NOARGS},
  307. {NULL}
  308. };
  309. static PyTypeObject BZ2Compressor_Type = {
  310. PyVarObject_HEAD_INIT(NULL, 0)
  311. "_bz2.BZ2Compressor", /* tp_name */
  312. sizeof(BZ2Compressor), /* tp_basicsize */
  313. 0, /* tp_itemsize */
  314. (destructor)BZ2Compressor_dealloc, /* tp_dealloc */
  315. 0, /* tp_print */
  316. 0, /* tp_getattr */
  317. 0, /* tp_setattr */
  318. 0, /* tp_reserved */
  319. 0, /* tp_repr */
  320. 0, /* tp_as_number */
  321. 0, /* tp_as_sequence */
  322. 0, /* tp_as_mapping */
  323. 0, /* tp_hash */
  324. 0, /* tp_call */
  325. 0, /* tp_str */
  326. 0, /* tp_getattro */
  327. 0, /* tp_setattro */
  328. 0, /* tp_as_buffer */
  329. Py_TPFLAGS_DEFAULT, /* tp_flags */
  330. _bz2_BZ2Compressor___init____doc__, /* tp_doc */
  331. 0, /* tp_traverse */
  332. 0, /* tp_clear */
  333. 0, /* tp_richcompare */
  334. 0, /* tp_weaklistoffset */
  335. 0, /* tp_iter */
  336. 0, /* tp_iternext */
  337. BZ2Compressor_methods, /* tp_methods */
  338. 0, /* tp_members */
  339. 0, /* tp_getset */
  340. 0, /* tp_base */
  341. 0, /* tp_dict */
  342. 0, /* tp_descr_get */
  343. 0, /* tp_descr_set */
  344. 0, /* tp_dictoffset */
  345. _bz2_BZ2Compressor___init__, /* tp_init */
  346. 0, /* tp_alloc */
  347. PyType_GenericNew, /* tp_new */
  348. };
  349. /* BZ2Decompressor class. */
  350. /* Decompress data of length d->bzs_avail_in_real in d->bzs.next_in. The output
  351. buffer is allocated dynamically and returned. At most max_length bytes are
  352. returned, so some of the input may not be consumed. d->bzs.next_in and
  353. d->bzs_avail_in_real are updated to reflect the consumed input. */
  354. static PyObject*
  355. decompress_buf(BZ2Decompressor *d, Py_ssize_t max_length)
  356. {
  357. /* data_size is strictly positive, but because we repeatedly have to
  358. compare against max_length and PyBytes_GET_SIZE we declare it as
  359. signed */
  360. Py_ssize_t data_size = 0;
  361. PyObject *result;
  362. bz_stream *bzs = &d->bzs;
  363. if (max_length < 0 || max_length >= INITIAL_BUFFER_SIZE)
  364. result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
  365. else
  366. result = PyBytes_FromStringAndSize(NULL, max_length);
  367. if (result == NULL)
  368. return NULL;
  369. bzs->next_out = PyBytes_AS_STRING(result);
  370. for (;;) {
  371. int bzret;
  372. size_t avail;
  373. /* On a 64-bit system, buffer length might not fit in avail_out, so we
  374. do decompression in chunks of no more than UINT_MAX bytes
  375. each. Note that the expression for `avail` is guaranteed to be
  376. positive, so the cast is safe. */
  377. avail = (size_t) (PyBytes_GET_SIZE(result) - data_size);
  378. bzs->avail_out = (unsigned int)Py_MIN(avail, UINT_MAX);
  379. bzs->avail_in = (unsigned int)Py_MIN(d->bzs_avail_in_real, UINT_MAX);
  380. d->bzs_avail_in_real -= bzs->avail_in;
  381. Py_BEGIN_ALLOW_THREADS
  382. bzret = BZ2_bzDecompress(bzs);
  383. data_size = bzs->next_out - PyBytes_AS_STRING(result);
  384. d->bzs_avail_in_real += bzs->avail_in;
  385. Py_END_ALLOW_THREADS
  386. if (catch_bz2_error(bzret))
  387. goto error;
  388. if (bzret == BZ_STREAM_END) {
  389. d->eof = 1;
  390. break;
  391. } else if (d->bzs_avail_in_real == 0) {
  392. break;
  393. } else if (bzs->avail_out == 0) {
  394. if (data_size == max_length)
  395. break;
  396. if (data_size == PyBytes_GET_SIZE(result) &&
  397. grow_buffer(&result, max_length) == -1)
  398. goto error;
  399. bzs->next_out = PyBytes_AS_STRING(result) + data_size;
  400. }
  401. }
  402. if (data_size != PyBytes_GET_SIZE(result))
  403. if (_PyBytes_Resize(&result, data_size) == -1)
  404. goto error;
  405. return result;
  406. error:
  407. Py_XDECREF(result);
  408. return NULL;
  409. }
  410. static PyObject *
  411. decompress(BZ2Decompressor *d, char *data, size_t len, Py_ssize_t max_length)
  412. {
  413. char input_buffer_in_use;
  414. PyObject *result;
  415. bz_stream *bzs = &d->bzs;
  416. /* Prepend unconsumed input if necessary */
  417. if (bzs->next_in != NULL) {
  418. size_t avail_now, avail_total;
  419. /* Number of bytes we can append to input buffer */
  420. avail_now = (d->input_buffer + d->input_buffer_size)
  421. - (bzs->next_in + d->bzs_avail_in_real);
  422. /* Number of bytes we can append if we move existing
  423. contents to beginning of buffer (overwriting
  424. consumed input) */
  425. avail_total = d->input_buffer_size - d->bzs_avail_in_real;
  426. if (avail_total < len) {
  427. size_t offset = bzs->next_in - d->input_buffer;
  428. char *tmp;
  429. size_t new_size = d->input_buffer_size + len - avail_now;
  430. /* Assign to temporary variable first, so we don't
  431. lose address of allocated buffer if realloc fails */
  432. tmp = PyMem_Realloc(d->input_buffer, new_size);
  433. if (tmp == NULL) {
  434. PyErr_SetNone(PyExc_MemoryError);
  435. return NULL;
  436. }
  437. d->input_buffer = tmp;
  438. d->input_buffer_size = new_size;
  439. bzs->next_in = d->input_buffer + offset;
  440. }
  441. else if (avail_now < len) {
  442. memmove(d->input_buffer, bzs->next_in,
  443. d->bzs_avail_in_real);
  444. bzs->next_in = d->input_buffer;
  445. }
  446. memcpy((void*)(bzs->next_in + d->bzs_avail_in_real), data, len);
  447. d->bzs_avail_in_real += len;
  448. input_buffer_in_use = 1;
  449. }
  450. else {
  451. bzs->next_in = data;
  452. d->bzs_avail_in_real = len;
  453. input_buffer_in_use = 0;
  454. }
  455. result = decompress_buf(d, max_length);
  456. if(result == NULL)
  457. return NULL;
  458. if (d->eof) {
  459. d->needs_input = 0;
  460. if (d->bzs_avail_in_real > 0) {
  461. Py_CLEAR(d->unused_data);
  462. d->unused_data = PyBytes_FromStringAndSize(
  463. bzs->next_in, d->bzs_avail_in_real);
  464. if (d->unused_data == NULL)
  465. goto error;
  466. }
  467. }
  468. else if (d->bzs_avail_in_real == 0) {
  469. bzs->next_in = NULL;
  470. d->needs_input = 1;
  471. }
  472. else {
  473. d->needs_input = 0;
  474. /* If we did not use the input buffer, we now have
  475. to copy the tail from the caller's buffer into the
  476. input buffer */
  477. if (!input_buffer_in_use) {
  478. /* Discard buffer if it's too small
  479. (resizing it may needlessly copy the current contents) */
  480. if (d->input_buffer != NULL &&
  481. d->input_buffer_size < d->bzs_avail_in_real) {
  482. PyMem_Free(d->input_buffer);
  483. d->input_buffer = NULL;
  484. }
  485. /* Allocate if necessary */
  486. if (d->input_buffer == NULL) {
  487. d->input_buffer = PyMem_Malloc(d->bzs_avail_in_real);
  488. if (d->input_buffer == NULL) {
  489. PyErr_SetNone(PyExc_MemoryError);
  490. goto error;
  491. }
  492. d->input_buffer_size = d->bzs_avail_in_real;
  493. }
  494. /* Copy tail */
  495. memcpy(d->input_buffer, bzs->next_in, d->bzs_avail_in_real);
  496. bzs->next_in = d->input_buffer;
  497. }
  498. }
  499. return result;
  500. error:
  501. Py_XDECREF(result);
  502. return NULL;
  503. }
  504. /*[clinic input]
  505. _bz2.BZ2Decompressor.decompress
  506. self: self(type="BZ2Decompressor *")
  507. data: Py_buffer
  508. max_length: Py_ssize_t=-1
  509. Decompress *data*, returning uncompressed data as bytes.
  510. If *max_length* is nonnegative, returns at most *max_length* bytes of
  511. decompressed data. If this limit is reached and further output can be
  512. produced, *self.needs_input* will be set to ``False``. In this case, the next
  513. call to *decompress()* may provide *data* as b'' to obtain more of the output.
  514. If all of the input data was decompressed and returned (either because this
  515. was less than *max_length* bytes, or because *max_length* was negative),
  516. *self.needs_input* will be set to True.
  517. Attempting to decompress data after the end of stream is reached raises an
  518. EOFError. Any data found after the end of the stream is ignored and saved in
  519. the unused_data attribute.
  520. [clinic start generated code]*/
  521. static PyObject *
  522. _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
  523. Py_ssize_t max_length)
  524. /*[clinic end generated code: output=23e41045deb240a3 input=9558b424c8b00516]*/
  525. {
  526. PyObject *result = NULL;
  527. ACQUIRE_LOCK(self);
  528. if (self->eof)
  529. PyErr_SetString(PyExc_EOFError, "End of stream already reached");
  530. else
  531. result = decompress(self, data->buf, data->len, max_length);
  532. RELEASE_LOCK(self);
  533. return result;
  534. }
  535. static PyObject *
  536. BZ2Decompressor_getstate(BZ2Decompressor *self, PyObject *noargs)
  537. {
  538. PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
  539. Py_TYPE(self)->tp_name);
  540. return NULL;
  541. }
  542. /*[clinic input]
  543. _bz2.BZ2Decompressor.__init__
  544. Create a decompressor object for decompressing data incrementally.
  545. For one-shot decompression, use the decompress() function instead.
  546. [clinic start generated code]*/
  547. static int
  548. _bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self)
  549. /*[clinic end generated code: output=e4d2b9bb866ab8f1 input=95f6500dcda60088]*/
  550. {
  551. int bzerror;
  552. #ifdef WITH_THREAD
  553. self->lock = PyThread_allocate_lock();
  554. if (self->lock == NULL) {
  555. PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
  556. return -1;
  557. }
  558. #endif
  559. self->needs_input = 1;
  560. self->bzs_avail_in_real = 0;
  561. self->input_buffer = NULL;
  562. self->input_buffer_size = 0;
  563. self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
  564. if (self->unused_data == NULL)
  565. goto error;
  566. bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
  567. if (catch_bz2_error(bzerror))
  568. goto error;
  569. return 0;
  570. error:
  571. Py_CLEAR(self->unused_data);
  572. #ifdef WITH_THREAD
  573. PyThread_free_lock(self->lock);
  574. self->lock = NULL;
  575. #endif
  576. return -1;
  577. }
  578. static void
  579. BZ2Decompressor_dealloc(BZ2Decompressor *self)
  580. {
  581. if(self->input_buffer != NULL)
  582. PyMem_Free(self->input_buffer);
  583. BZ2_bzDecompressEnd(&self->bzs);
  584. Py_CLEAR(self->unused_data);
  585. #ifdef WITH_THREAD
  586. if (self->lock != NULL)
  587. PyThread_free_lock(self->lock);
  588. #endif
  589. Py_TYPE(self)->tp_free((PyObject *)self);
  590. }
  591. static PyMethodDef BZ2Decompressor_methods[] = {
  592. _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF
  593. {"__getstate__", (PyCFunction)BZ2Decompressor_getstate, METH_NOARGS},
  594. {NULL}
  595. };
  596. PyDoc_STRVAR(BZ2Decompressor_eof__doc__,
  597. "True if the end-of-stream marker has been reached.");
  598. PyDoc_STRVAR(BZ2Decompressor_unused_data__doc__,
  599. "Data found after the end of the compressed stream.");
  600. PyDoc_STRVAR(BZ2Decompressor_needs_input_doc,
  601. "True if more input is needed before more decompressed data can be produced.");
  602. static PyMemberDef BZ2Decompressor_members[] = {
  603. {"eof", T_BOOL, offsetof(BZ2Decompressor, eof),
  604. READONLY, BZ2Decompressor_eof__doc__},
  605. {"unused_data", T_OBJECT_EX, offsetof(BZ2Decompressor, unused_data),
  606. READONLY, BZ2Decompressor_unused_data__doc__},
  607. {"needs_input", T_BOOL, offsetof(BZ2Decompressor, needs_input), READONLY,
  608. BZ2Decompressor_needs_input_doc},
  609. {NULL}
  610. };
  611. static PyTypeObject BZ2Decompressor_Type = {
  612. PyVarObject_HEAD_INIT(NULL, 0)
  613. "_bz2.BZ2Decompressor", /* tp_name */
  614. sizeof(BZ2Decompressor), /* tp_basicsize */
  615. 0, /* tp_itemsize */
  616. (destructor)BZ2Decompressor_dealloc,/* tp_dealloc */
  617. 0, /* tp_print */
  618. 0, /* tp_getattr */
  619. 0, /* tp_setattr */
  620. 0, /* tp_reserved */
  621. 0, /* tp_repr */
  622. 0, /* tp_as_number */
  623. 0, /* tp_as_sequence */
  624. 0, /* tp_as_mapping */
  625. 0, /* tp_hash */
  626. 0, /* tp_call */
  627. 0, /* tp_str */
  628. 0, /* tp_getattro */
  629. 0, /* tp_setattro */
  630. 0, /* tp_as_buffer */
  631. Py_TPFLAGS_DEFAULT, /* tp_flags */
  632. _bz2_BZ2Decompressor___init____doc__, /* tp_doc */
  633. 0, /* tp_traverse */
  634. 0, /* tp_clear */
  635. 0, /* tp_richcompare */
  636. 0, /* tp_weaklistoffset */
  637. 0, /* tp_iter */
  638. 0, /* tp_iternext */
  639. BZ2Decompressor_methods, /* tp_methods */
  640. BZ2Decompressor_members, /* tp_members */
  641. 0, /* tp_getset */
  642. 0, /* tp_base */
  643. 0, /* tp_dict */
  644. 0, /* tp_descr_get */
  645. 0, /* tp_descr_set */
  646. 0, /* tp_dictoffset */
  647. _bz2_BZ2Decompressor___init__, /* tp_init */
  648. 0, /* tp_alloc */
  649. PyType_GenericNew, /* tp_new */
  650. };
  651. /* Module initialization. */
  652. static struct PyModuleDef _bz2module = {
  653. PyModuleDef_HEAD_INIT,
  654. "_bz2",
  655. NULL,
  656. -1,
  657. NULL,
  658. NULL,
  659. NULL,
  660. NULL,
  661. NULL
  662. };
  663. PyMODINIT_FUNC
  664. PyInit__bz2(void)
  665. {
  666. PyObject *m;
  667. if (PyType_Ready(&BZ2Compressor_Type) < 0)
  668. return NULL;
  669. if (PyType_Ready(&BZ2Decompressor_Type) < 0)
  670. return NULL;
  671. m = PyModule_Create(&_bz2module);
  672. if (m == NULL)
  673. return NULL;
  674. Py_INCREF(&BZ2Compressor_Type);
  675. PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Compressor_Type);
  676. Py_INCREF(&BZ2Decompressor_Type);
  677. PyModule_AddObject(m, "BZ2Decompressor",
  678. (PyObject *)&BZ2Decompressor_Type);
  679. return m;
  680. }