123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include <Python.h>
- #include <7zip/LzmaCompatDecode.h>
- #include "pylzma.h"
- #include "pylzma_decompress_compat.h"
- #include "pylzma_decompressobj_compat.h"
- static const char doc_decomp_decompress[] = \
- "decompress(data[, bufsize]) -- Returns a string containing the up to bufsize decompressed bytes of the data.\n" \
- "After calling, some of the input data may be available in internal buffers for later processing.";
- static PyObject *pylzma_decomp_decompress(CCompatDecompressionObject *self, PyObject *args)
- {
- PyObject *result=NULL;
- char *data;
- int length, old_length, start_total_out, res, max_length=BLOCK_SIZE;
-
- if (!PyArg_ParseTuple(args, "s#|l", &data, &length, &max_length))
- return NULL;
- if (max_length < 0)
- {
- PyErr_SetString(PyExc_ValueError, "bufsize must be greater than zero");
- return NULL;
- }
-
- start_total_out = self->stream.totalOut;
- if (self->unconsumed_length > 0) {
- self->unconsumed_tail = (char *)realloc(self->unconsumed_tail, self->unconsumed_length + length);
- self->stream.next_in = (Byte *)self->unconsumed_tail;
- memcpy(self->stream.next_in + self->unconsumed_length, data, length);
- } else
- self->stream.next_in = (Byte *)data;
-
- self->stream.avail_in = self->unconsumed_length + length;
-
- if (max_length && max_length < length)
- length = max_length;
-
- if (!(result = PyString_FromStringAndSize(NULL, length)))
- return NULL;
-
- self->stream.next_out = (unsigned char *)PyString_AS_STRING(result);
- self->stream.avail_out = length;
-
- Py_BEGIN_ALLOW_THREADS
- res = lzmaCompatDecode(&self->stream);
- Py_END_ALLOW_THREADS
-
- while (res == LZMA_OK && self->stream.avail_out == 0)
- {
- if (max_length && length >= max_length)
- break;
-
- old_length = length;
- length <<= 1;
- if (max_length && length > max_length)
- length = max_length;
-
- if (_PyString_Resize(&result, length) < 0)
- goto exit;
-
- self->stream.avail_out = length - old_length;
- self->stream.next_out = (Byte *)PyString_AS_STRING(result) + old_length;
-
- Py_BEGIN_ALLOW_THREADS
- res = lzmaCompatDecode(&self->stream);
- Py_END_ALLOW_THREADS
- }
-
- if (res == LZMA_NOT_ENOUGH_MEM) {
-
- PyErr_NoMemory();
- DEC_AND_NULL(result);
- goto exit;
- } else if (res == LZMA_DATA_ERROR) {
- PyErr_SetString(PyExc_ValueError, "data error during decompression");
- DEC_AND_NULL(result);
- goto exit;
- } else if (res != LZMA_OK && res != LZMA_STREAM_END) {
- PyErr_Format(PyExc_ValueError, "unknown return code from lzmaDecode: %d", res);
- DEC_AND_NULL(result);
- goto exit;
- }
-
- if (max_length != 0) {
- if (self->stream.avail_in > 0)
- {
- if (self->stream.avail_in != self->unconsumed_length)
- self->unconsumed_tail = (char *)realloc(self->unconsumed_tail, self->stream.avail_in);
-
- if (!self->unconsumed_tail) {
- PyErr_NoMemory();
- DEC_AND_NULL(result);
- goto exit;
- }
- memcpy(self->unconsumed_tail, self->stream.next_in, self->stream.avail_in);
- } else
- FREE_AND_NULL(self->unconsumed_tail);
- self->unconsumed_length = self->stream.avail_in;
- }
-
- if (res == LZMA_STREAM_END) {
- Py_XDECREF(self->unused_data);
- self->unused_data = PyString_FromStringAndSize((char *)self->stream.next_in, self->stream.avail_in);
- if (self->unused_data == NULL) {
- PyErr_NoMemory();
- DEC_AND_NULL(result);
- goto exit;
- }
- }
-
- _PyString_Resize(&result, self->stream.totalOut - start_total_out);
-
- exit:
- return result;
- }
- static const char doc_decomp_reset[] = \
- "reset() -- Resets the decompression object.";
- static PyObject *pylzma_decomp_reset(CCompatDecompressionObject *self, PyObject *args)
- {
- PyObject *result=NULL;
-
- if (!PyArg_ParseTuple(args, ""))
- return NULL;
-
- lzmaCompatInit(&self->stream);
- FREE_AND_NULL(self->unconsumed_tail);
- self->unconsumed_length = 0;
-
- Py_DECREF(self->unused_data);
- self->unused_data = PyString_FromString("");
- CHECK_NULL(self->unused_data);
-
- result = Py_None;
- Py_XINCREF(result);
-
- exit:
- return result;
- }
- PyMethodDef pylzma_decomp_compat_methods[] = {
- {"decompress", (PyCFunction)pylzma_decomp_decompress, METH_VARARGS, (char *)&doc_decomp_decompress},
- {"reset", (PyCFunction)pylzma_decomp_reset, METH_VARARGS, (char *)&doc_decomp_reset},
- {NULL, NULL},
- };
- static void pylzma_decomp_dealloc(CCompatDecompressionObject *self)
- {
- free_lzma_stream(&self->stream);
- FREE_AND_NULL(self->unconsumed_tail);
- DEC_AND_NULL(self->unused_data);
- PyObject_Del(self);
- }
- static PyObject *pylzma_decomp_getattr(CCompatDecompressionObject *self, char *attrname)
- {
- if (strcmp(attrname, "unused_data") == 0) {
- Py_INCREF(self->unused_data);
- return self->unused_data;
- } else
- return Py_FindMethod(pylzma_decomp_compat_methods, (PyObject *)self, attrname);
- }
- static int pylzma_decomp_setattr(CCompatDecompressionObject *self, char *attrname, PyObject *value)
- {
-
- PyErr_Format(PyExc_AttributeError, "no attribute named '%s'", attrname);
- return -1;
- }
- PyTypeObject CompatDecompressionObject_Type = {
-
- PyObject_HEAD_INIT(NULL)
- 0,
- "LZMACompatDecompress",
- sizeof(CCompatDecompressionObject),
- 0,
- (destructor)pylzma_decomp_dealloc,
- NULL,
- (getattrfunc)pylzma_decomp_getattr,
- (setattrfunc)pylzma_decomp_setattr,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- };
- const char doc_decompressobj_compat[] = \
- "decompressobj_compat() -- Returns object that can be used for decompression.";
- PyObject *pylzma_decompressobj_compat(PyObject *self, PyObject *args)
- {
- CCompatDecompressionObject *result=NULL;
-
- if (!PyArg_ParseTuple(args, ""))
- goto exit;
-
- result = PyObject_New(CCompatDecompressionObject, &CompatDecompressionObject_Type);
- CHECK_NULL(result);
-
- result->unconsumed_tail = NULL;
- result->unconsumed_length = 0;
- result->unused_data = PyString_FromString("");
- if (result->unused_data == NULL)
- {
- PyErr_NoMemory();
- PyObject_Del(result);
- result = NULL;
- goto exit;
- }
-
- memset(&result->stream, 0, sizeof(result->stream));
- lzmaCompatInit(&result->stream);
-
- exit:
-
- return (PyObject *)result;
- }
|