static void pylzma_compfile_dealloc(CCompressionFileObject *self) { DEC_AND_NULL(self->inFile); LzmaEnc_Destroy(self->encoder, &allocator, &allocator); if (self->outStream.data != NULL) { free(self->outStream.data); } self->ob_type->tp_free((PyObject*)self); }
static void pylzma_comp_dealloc(CCompressionObject *self) { LzmaEnc_Destroy(self->encoder, &allocator, &allocator); if (self->outStream.data != NULL) { free(self->outStream.data); } if (self->inStream.data != NULL) { free(self->inStream.data); } self->ob_type->tp_free((PyObject*)self); }
void CompressInc(std::vector<unsigned char> &outBuf, const std::vector<unsigned char> &inBuf) { CLzmaEncHandle enc = LzmaEnc_Create(&g_Alloc); assert(enc); CLzmaEncProps props; LzmaEncProps_Init(&props); //props.writeEndMark = 1; // 0 or 1 SRes res = LzmaEnc_SetProps(enc, &props); assert(res == SZ_OK); unsigned char tempprops[5]; unsigned propsSize = LZMA_PROPS_SIZE; //outBuf.resize(propsSize); //fill with zero, which cost me a week to check the mistake. res = LzmaEnc_WriteProperties(enc, tempprops, &propsSize); assert(res == SZ_OK && propsSize == LZMA_PROPS_SIZE); VectorInStream inStream = { &VectorInStream_Read, &inBuf, 0 }; VectorOutStream outStream = { &VectorOutStream_Write, &outBuf }; res = LzmaEnc_Encode(enc, (ISeqOutStream*)&outStream, (ISeqInStream*)&inStream, 0, &g_Alloc, &g_Alloc); assert(res == SZ_OK); UInt64 resLen = inBuf.size(); //printf("uint64 resLen : %" PRIu64 "\n", resLen); //print uint64 Byte header[8]; for (int i = 0; i < 8; i++) header[i] = (Byte)(resLen >> (8 * i)); FILE *file = fopen("data3.dat", "wb+"); fwrite(tempprops, 1, propsSize, file); fwrite(&header[0], 1, 8, file); fwrite(&outBuf[0], 1, outBuf.size(), file); fclose(file); LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc); }
SRes MyLzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) { CLzmaEncHandle *p = (CLzmaEncHandle *)LzmaEnc_Create(alloc); SRes res; if (p == 0) return SZ_ERROR_MEM; res = LzmaEnc_SetProps(p, props); if (res == SZ_OK) { res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); if (res == SZ_OK) res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, writeEndMark, progress, alloc, allocBig); } LzmaEnc_Destroy(p, alloc, allocBig); return res; }
STATIC void lzma_free_workspace(void) { LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc); }
CEncoder::~CEncoder() { if (_encoder != 0) LzmaEnc_Destroy(_encoder, &g_Alloc, &g_BigAlloc); }
inline ~TLzmaCompressBase() throw () { LzmaEnc_Destroy(H_, Alloc(), Alloc()); }
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; }
~CLZMA() { if (enc) LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc); }