static int pylzma_compfile_init(CCompressionFileObject *self, PyObject *args, PyObject *kwargs) { PyObject *inFile; CLzmaEncProps props; Byte header[LZMA_PROPS_SIZE]; size_t headerSize = LZMA_PROPS_SIZE; int result = -1; // possible keywords for this function static char *kwlist[] = {"infile", "dictionary", "fastBytes", "literalContextBits", "literalPosBits", "posBits", "algorithm", "eos", "multithreading", "matchfinder", NULL}; int dictionary = 23; // [0,28], default 23 (8MB) int fastBytes = 128; // [5,255], default 128 int literalContextBits = 3; // [0,8], default 3 int literalPosBits = 0; // [0,4], default 0 int posBits = 2; // [0,4], default 2 int eos = 1; // write "end of stream" marker? int multithreading = 1; // use multithreading if available? char *matchfinder = NULL; // matchfinder algorithm int algorithm = 2; int res; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iiiiiiiis", kwlist, &inFile, &dictionary, &fastBytes, &literalContextBits, &literalPosBits, &posBits, &algorithm, &eos, &multithreading, &matchfinder)) return -1; CHECK_RANGE(dictionary, 0, 28, "dictionary must be between 0 and 28"); CHECK_RANGE(fastBytes, 5, 255, "fastBytes must be between 5 and 255"); CHECK_RANGE(literalContextBits, 0, 8, "literalContextBits must be between 0 and 8"); CHECK_RANGE(literalPosBits, 0, 4, "literalPosBits must be between 0 and 4"); CHECK_RANGE(posBits, 0, 4, "posBits must be between 0 and 4"); CHECK_RANGE(algorithm, 0, 2, "algorithm must be between 0 and 2"); if (matchfinder != NULL) { #if (PY_VERSION_HEX >= 0x02050000) PyErr_WarnEx(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored", 1); #else PyErr_Warn(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored"); #endif } if (PyString_Check(inFile)) { // create new cStringIO object from string inFile = PycStringIO->NewInput(inFile); if (inFile == NULL) { PyErr_NoMemory(); return -1; } } else if (!PyObject_HasAttrString(inFile, "read")) { PyErr_SetString(PyExc_ValueError, "first parameter must be a file-like object"); return -1; } else { // protect object from being refcounted out... Py_INCREF(inFile); } self->encoder = LzmaEnc_Create(&allocator); if (self->encoder == NULL) { Py_DECREF(inFile); PyErr_NoMemory(); return -1; } LzmaEncProps_Init(&props); props.dictSize = 1 << dictionary; props.lc = literalContextBits; props.lp = literalPosBits; props.pb = posBits; props.algo = algorithm; props.fb = fastBytes; // props.btMode = 1; // props.numHashBytes = 4; // props.mc = 32; props.writeEndMark = eos ? 1 : 0; props.numThreads = multithreading ? 2 : 1; LzmaEncProps_Normalize(&props); res = LzmaEnc_SetProps(self->encoder, &props); if (res != SZ_OK) { Py_DECREF(inFile); PyErr_Format(PyExc_TypeError, "could not set encoder properties: %d", res); return -1; } self->inFile = inFile; CreatePythonInStream(&self->inStream, inFile); CreateMemoryOutStream(&self->outStream); LzmaEnc_WriteProperties(self->encoder, header, &headerSize); if (self->outStream.s.Write(&self->outStream, header, headerSize) != headerSize) { PyErr_SetString(PyExc_TypeError, "could not generate stream header"); goto exit; } LzmaEnc_Prepare(self->encoder, &self->inStream.s, &self->outStream.s, &allocator, &allocator); result = 0; exit: return result; }
PyObject * pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *result = NULL; CLzmaEncProps props; CLzmaEncHandle encoder=NULL; CMemoryOutStream outStream; CMemoryInStream inStream; Byte header[LZMA_PROPS_SIZE]; size_t headerSize = LZMA_PROPS_SIZE; int res; // possible keywords for this function static char *kwlist[] = {"data", "dictionary", "fastBytes", "literalContextBits", "literalPosBits", "posBits", "algorithm", "eos", "multithreading", "matchfinder", NULL}; int dictionary = 23; // [0,27], default 23 (8MB) int fastBytes = 128; // [5,273], default 128 int literalContextBits = 3; // [0,8], default 3 int literalPosBits = 0; // [0,4], default 0 int posBits = 2; // [0,4], default 2 int eos = 1; // write "end of stream" marker? int multithreading = 1; // use multithreading if available? char *matchfinder = NULL; // matchfinder algorithm int algorithm = 2; char *data; int length; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|iiiiiiiis", kwlist, &data, &length, &dictionary, &fastBytes, &literalContextBits, &literalPosBits, &posBits, &algorithm, &eos, &multithreading, &matchfinder)) return NULL; outStream.data = NULL; CHECK_RANGE(dictionary, 0, 27, "dictionary must be between 0 and 27"); CHECK_RANGE(fastBytes, 5, 273, "fastBytes must be between 5 and 273"); CHECK_RANGE(literalContextBits, 0, 8, "literalContextBits must be between 0 and 8"); CHECK_RANGE(literalPosBits, 0, 4, "literalPosBits must be between 0 and 4"); CHECK_RANGE(posBits, 0, 4, "posBits must be between 0 and 4"); CHECK_RANGE(algorithm, 0, 2, "algorithm must be between 0 and 2"); if (matchfinder != NULL) { #if (PY_VERSION_HEX >= 0x02050000) PyErr_WarnEx(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored", 1); #else PyErr_Warn(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored"); #endif } encoder = LzmaEnc_Create(&allocator); if (encoder == NULL) return PyErr_NoMemory(); CreateMemoryInStream(&inStream, (Byte *) data, length); CreateMemoryOutStream(&outStream); LzmaEncProps_Init(&props); props.dictSize = 1 << dictionary; props.lc = literalContextBits; props.lp = literalPosBits; props.pb = posBits; props.algo = algorithm; props.fb = fastBytes; // props.btMode = 1; // props.numHashBytes = 4; // props.mc = 32; props.writeEndMark = eos ? 1 : 0; props.numThreads = multithreading ? 2 : 1; LzmaEncProps_Normalize(&props); res = LzmaEnc_SetProps(encoder, &props); if (res != SZ_OK) { PyErr_Format(PyExc_TypeError, "could not set encoder properties: %d", res); goto exit; } Py_BEGIN_ALLOW_THREADS LzmaEnc_WriteProperties(encoder, header, &headerSize); if (outStream.s.Write(&outStream, header, headerSize) != headerSize) { res = SZ_ERROR_WRITE; } else { res = LzmaEnc_Encode(encoder, &outStream.s, &inStream.s, NULL, &allocator, &allocator); } Py_END_ALLOW_THREADS if (res != SZ_OK) { PyErr_Format(PyExc_TypeError, "Error during compressing: %d", res); goto exit; } result = PyBytes_FromStringAndSize((const char *) outStream.data, outStream.size); exit: if (encoder != NULL) { LzmaEnc_Destroy(encoder, &allocator, &allocator); } if (outStream.data != NULL) { free(outStream.data); } return result; }