diff --git a/setup.py b/setup.py index 10f898d..4cc4d10 100644 --- a/setup.py +++ b/setup.py @@ -129,12 +129,18 @@ def build_extension(self, ext): if isinstance(self.compiler, MSVCCompiler) or getattr(self.compiler, 'compiler_type', '') == 'msvc': # set flags only available when using MSVC ext.extra_link_args.append('/MANIFEST') + # conversion from '__int64' to 'Py_ssize_t', possible loss of data + ext.extra_compile_args.append('/we4244') + # conversion from 'size_t' to 'int', possible loss of data + ext.extra_compile_args.append('/we4267') if COMPILE_DEBUG: ext.extra_compile_args.append('/Zi') ext.extra_compile_args.append('/MTd') ext.extra_link_args.append('/DEBUG') else: ext.extra_compile_args.append('/MT') + else: + ext.extra_compile_args.append('-Werror=sign-compare') _build_ext.build_extension(self, ext) diff --git a/src/compat/LzmaCompatDecode.c b/src/compat/LzmaCompatDecode.c index 413b858..7f4aa37 100644 --- a/src/compat/LzmaCompatDecode.c +++ b/src/compat/LzmaCompatDecode.c @@ -140,7 +140,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s) { UInt32 bound; UInt32 pos; - + /* restore decoder state */ lzma_stream _s = *s; @@ -240,7 +240,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s) while (numProbs--) p[numProbs] = kBitModelTotal >> 1; - + //for (i = 0, newDictionarySize = 0; i < 4; i++) for (i = 0, _s.temp3 = 0; i < 4; i++) { @@ -306,7 +306,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s) } } while (symbol < 0x100); - previousByte = symbol; + previousByte = (Byte)symbol; } isPreviousMatch = 0; } @@ -319,7 +319,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s) RC_GET_BIT(LZMA_C_LITD, prob, symbol) } while (symbol < 0x100); - previousByte = symbol; + previousByte = (Byte)symbol; } NEED_OUT(LZMA_C_OUTPUT_1); PUT_BYTE(previousByte); diff --git a/src/pylzma/pylzma.c b/src/pylzma/pylzma.c index 4229fde..0a00c55 100644 --- a/src/pylzma/pylzma.c +++ b/src/pylzma/pylzma.c @@ -63,7 +63,7 @@ static PyObject * pylzma_calculate_key(PyObject *self, PyObject *args, PyObject *kwargs) { char *password; - PARSE_LENGTH_TYPE pwlen; + Py_ssize_t pwlen; int cycles; PyObject *pysalt=NULL; char *salt; @@ -136,7 +136,7 @@ static PyObject * pylzma_bcj_x86_convert(PyObject *self, PyObject *args) { char *data; - PARSE_LENGTH_TYPE length; + Py_ssize_t length; int encoding=0; PyObject *result; @@ -169,7 +169,7 @@ static PyObject * \ pylzma_bcj_##id##_convert(PyObject *self, PyObject *args) \ { \ char *data; \ - PARSE_LENGTH_TYPE length; \ + Py_ssize_t length; \ int encoding=0; \ PyObject *result; \ \ @@ -205,8 +205,8 @@ static PyObject * pylzma_bcj2_decode(PyObject *self, PyObject *args) { char *main_data, *call_data, *jump_data, *rc_data; - PARSE_LENGTH_TYPE main_length, call_length, jump_length, rc_length; - PARSE_LENGTH_TYPE dest_len = -1; + Py_ssize_t main_length, call_length, jump_length, rc_length; + Py_ssize_t dest_len = -1; CBcj2Dec dec; SRes res; PyObject *result; @@ -280,7 +280,7 @@ static PyObject * pylzma_delta_decode(PyObject *self, PyObject *args) { char *data; - PARSE_LENGTH_TYPE length; + Py_ssize_t length; unsigned int delta; Byte state[DELTA_STATE_SIZE]; Byte *tmp; @@ -320,7 +320,7 @@ static PyObject * pylzma_delta_encode(PyObject *self, PyObject *args) { char *data; - PARSE_LENGTH_TYPE length; + Py_ssize_t length; unsigned int delta; Byte state[DELTA_STATE_SIZE]; Byte *tmp; @@ -395,9 +395,9 @@ static PyObject * pylzma_ppmd_decompress(PyObject *self, PyObject *args) { char *data; - PARSE_LENGTH_TYPE length; + Py_ssize_t length; char *props; - PARSE_LENGTH_TYPE propssize; + Py_ssize_t propssize; unsigned int outsize; PyObject *result; Byte *tmp; @@ -469,7 +469,7 @@ pylzma_ppmd_decompress(PyObject *self, PyObject *args) } if (i != outsize) { res = (s.res != SZ_OK ? s.res : SZ_ERROR_DATA); - } else if (s.processed + (s.cur - s.begin) != length || !Ppmd7z_RangeDec_IsFinishedOK(&rc)) { + } else if (s.processed + (s.cur - s.begin) != (UInt64)length || !Ppmd7z_RangeDec_IsFinishedOK(&rc)) { res = SZ_ERROR_DATA; } } diff --git a/src/pylzma/pylzma.h b/src/pylzma/pylzma.h index 47ac21b..c26bff2 100644 --- a/src/pylzma/pylzma.h +++ b/src/pylzma/pylzma.h @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -96,14 +96,6 @@ PyInterpreterState* _pylzma_interpreterState; 0, #endif -#if defined(PY_SSIZE_T_CLEAN) && (PY_VERSION_HEX >= 0x02040000) -#define PARSE_LENGTH_TYPE Py_ssize_t -#define PARSE_LENGTH_FORMAT "%zd" -#else -#define PARSE_LENGTH_TYPE int -#define PARSE_LENGTH_FORMAT "%d" -#endif - // Handle tp_print -> tp_vectorcall_offset change in Python 3.8+ #if PY_VERSION_HEX >= 0x03080000 #define PYLZMA_TP_PRINT 0 diff --git a/src/pylzma/pylzma_aes.c b/src/pylzma/pylzma_aes.c index bb58d70..9c562be 100644 --- a/src/pylzma/pylzma_aes.c +++ b/src/pylzma/pylzma_aes.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -43,16 +43,16 @@ int aesdecrypt_init(CAESDecryptObject *self, PyObject *args, PyObject *kwargs) { char *key=NULL; - PARSE_LENGTH_TYPE keylength=0; + Py_ssize_t keylength=0; char *iv=NULL; - PARSE_LENGTH_TYPE ivlength=0; + Py_ssize_t ivlength=0; int offset; - + // possible keywords for this function static char *kwlist[] = {"key", "iv", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s#s#", kwlist, &key, &keylength, &iv, &ivlength)) return -1; - + memset(&self->aesBuf, 0, sizeof(self->aesBuf)); self->aes = (UInt32 *) self->aesBuf; // AES code expects aligned memory @@ -64,15 +64,15 @@ aesdecrypt_init(CAESDecryptObject *self, PyObject *args, PyObject *kwargs) if (keylength > 0) { if (keylength != 16 && keylength != 24 && keylength != 32) { - PyErr_Format(PyExc_TypeError, "key must be 16, 24 or 32 bytes, got " PARSE_LENGTH_FORMAT, keylength); + PyErr_Format(PyExc_TypeError, "key must be 16, 24 or 32 bytes, got %zd", keylength); return -1; } - Aes_SetKey_Dec(self->aes + 4, (Byte *) key, keylength); + Aes_SetKey_Dec(self->aes + 4, (Byte *) key, (unsigned)keylength); } if (ivlength > 0) { if (ivlength != AES_BLOCK_SIZE) { - PyErr_Format(PyExc_TypeError, "iv must be %d bytes, got " PARSE_LENGTH_FORMAT, AES_BLOCK_SIZE, ivlength); + PyErr_Format(PyExc_TypeError, "iv must be %d bytes, got %zd", AES_BLOCK_SIZE, ivlength); return -1; } @@ -81,7 +81,7 @@ aesdecrypt_init(CAESDecryptObject *self, PyObject *args, PyObject *kwargs) return 0; } -static char +static char doc_aesdecrypt_decrypt[] = \ "decrypt(data) -- Decrypt given data."; @@ -89,20 +89,20 @@ static PyObject * aesdecrypt_decrypt(CAESDecryptObject *self, PyObject *args) { char *data; - PARSE_LENGTH_TYPE length; + Py_ssize_t length; PyObject *result; char *out; - PARSE_LENGTH_TYPE outlength; + Py_ssize_t outlength; char *tmpdata = NULL; - + if (!PyArg_ParseTuple(args, "s#", &data, &length)) return NULL; - + if (length % AES_BLOCK_SIZE) { - PyErr_Format(PyExc_TypeError, "data must be a multiple of %d bytes, got " PARSE_LENGTH_FORMAT, AES_BLOCK_SIZE, length); + PyErr_Format(PyExc_TypeError, "data must be a multiple of %d bytes, got %zd", AES_BLOCK_SIZE, length); return NULL; } - + result = PyBytes_FromStringAndSize(NULL, length); if (result == NULL) { return NULL; diff --git a/src/pylzma/pylzma_compress.c b/src/pylzma/pylzma_compress.c index 118b3f6..f75a7e5 100644 --- a/src/pylzma/pylzma_compress.c +++ b/src/pylzma/pylzma_compress.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -44,7 +44,7 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs) CMemoryInStream inStream; Byte header[LZMA_PROPS_SIZE]; size_t headerSize = LZMA_PROPS_SIZE; - int res; + int res; // possible keywords for this function static char *kwlist[] = {"data", "dictionary", "fastBytes", "literalContextBits", "literalPosBits", "posBits", "algorithm", "eos", "multithreading", "matchfinder", NULL}; @@ -58,12 +58,12 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs) char *matchfinder = NULL; // matchfinder algorithm int algorithm = 2; char *data; - PARSE_LENGTH_TYPE length; + Py_ssize_t 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"); @@ -71,7 +71,7 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs) 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); @@ -79,16 +79,16 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs) 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; @@ -119,9 +119,9 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs) 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); @@ -129,6 +129,6 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs) if (outStream.data != NULL) { free(outStream.data); } - + return result; } diff --git a/src/pylzma/pylzma_compressfile.c b/src/pylzma/pylzma_compressfile.c index a001f1c..62f1604 100644 --- a/src/pylzma/pylzma_compressfile.c +++ b/src/pylzma/pylzma_compressfile.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -60,8 +60,9 @@ static PyObject * pylzma_compfile_read(CCompressionFileObject *self, PyObject *args) { PyObject *result = NULL; - int length, bufsize=0, res=SZ_OK; - + int bufsize=0, res=SZ_OK; + size_t length; + if (!PyArg_ParseTuple(args, "|i", &bufsize)) return NULL; @@ -74,22 +75,22 @@ pylzma_compfile_read(CCompressionFileObject *self, PyObject *args) break; } } - + if (LzmaEnc_IsFinished(self->encoder)) { LzmaEnc_Finish(self->encoder); } - + if (bufsize) length = min((size_t) bufsize, self->outStream.size); else length = self->outStream.size; - - result = PyBytes_FromStringAndSize((const char *)self->outStream.data, length); + + result = PyBytes_FromStringAndSize((const char *)self->outStream.data, (Py_ssize_t)length); if (result == NULL) { PyErr_NoMemory(); goto exit; } - + MemoryOutStreamDiscard(&self->outStream, length); exit: @@ -124,7 +125,7 @@ pylzma_compfile_init(CCompressionFileObject *self, PyObject *args, PyObject *kwa 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}; @@ -138,18 +139,18 @@ pylzma_compfile_init(CCompressionFileObject *self, PyObject *args, PyObject *kwa 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); @@ -157,7 +158,7 @@ pylzma_compfile_init(CCompressionFileObject *self, PyObject *args, PyObject *kwa PyErr_Warn(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored"); #endif } - + if (PyBytes_Check(inFile)) { #if PY_MAJOR_VERSION >= 3 PyErr_SetString(PyExc_TypeError, "first parameter must be a file-like object"); @@ -178,16 +179,16 @@ pylzma_compfile_init(CCompressionFileObject *self, PyObject *args, PyObject *kwa // 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; @@ -216,10 +217,10 @@ pylzma_compfile_init(CCompressionFileObject *self, PyObject *args, PyObject *kwa PyErr_SetString(PyExc_TypeError, "could not generate stream header"); goto exit; } - + LzmaEnc_Prepare(self->encoder, &self->outStream.s, &self->inStream.s, &allocator, &allocator); result = 0; - + exit: return result; } diff --git a/src/pylzma/pylzma_compressobj.c b/src/pylzma/pylzma_compressobj.c index 80a159b..449772f 100644 --- a/src/pylzma/pylzma_compressobj.c +++ b/src/pylzma/pylzma_compressobj.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -47,17 +47,17 @@ pylzma_comp_compress(CCompressionObject *self, PyObject *args) PyObject *result = NULL; char *data; PARSE_LENGTH_TYPE length; - PY_LONG_LONG bufsize=BLOCK_SIZE; + Py_ssize_t bufsize=BLOCK_SIZE; int res; size_t before; - - if (!PyArg_ParseTuple(args, "s#|L", &data, &length, &bufsize)) + + if (!PyArg_ParseTuple(args, "s#|n", &data, &length, &bufsize)) return NULL; - + if (!MemoryInOutStreamAppend(&self->inStream, (Byte *) data, length)) { return PyErr_NoMemory(); } - + Py_BEGIN_ALLOW_THREADS while (1) { before = self->inStream.avail; @@ -71,17 +71,17 @@ pylzma_comp_compress(CCompressionObject *self, PyObject *args) PyErr_Format(PyExc_TypeError, "Error during compressing: %d", res); return NULL; } - - length = min(self->outStream.size, bufsize); + + length = min(self->outStream.size, (size_t)bufsize); result = PyString_FromStringAndSize((const char *)self->outStream.data, length); if (result != NULL) { MemoryOutStreamDiscard(&self->outStream, length); } - + return result; } -static char +static char doc_comp_flush[] = \ "flush() -- Finishes the compression and returns any remaining compressed data."; @@ -90,7 +90,7 @@ pylzma_comp_flush(CCompressionObject *self, PyObject *args) { PyObject *result; int res; - + Py_BEGIN_ALLOW_THREADS while (1) { res = LzmaEnc_CodeOneBlock(self->encoder, False, 0, 0); @@ -103,13 +103,13 @@ pylzma_comp_flush(CCompressionObject *self, PyObject *args) PyErr_Format(PyExc_TypeError, "Error during compressing: %d", res); return NULL; } - + LzmaEnc_Finish(self->encoder); result = PyString_FromStringAndSize((const char *) self->outStream.data, self->outStream.size); if (result != NULL) { MemoryOutStreamDiscard(&self->outStream, self->outStream.size); } - + return result; } @@ -140,7 +140,7 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs) Byte header[LZMA_PROPS_SIZE]; size_t headerSize = LZMA_PROPS_SIZE; int result=-1; - + // possible keywords for this function static char *kwlist[] = {"dictionary", "fastBytes", "literalContextBits", "literalPosBits", "posBits", "algorithm", "eos", "multithreading", "matchfinder", NULL}; @@ -154,11 +154,11 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs) char *matchfinder = NULL; // matchfinder algorithm int algorithm = 2; int res; - + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiiiis", kwlist, &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"); @@ -172,15 +172,15 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs) PyErr_Warn(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored"); #endif } - + self->encoder = LzmaEnc_Create(&allocator); if (self->encoder == NULL) { PyErr_NoMemory(); return -1; } - + LzmaEncProps_Init(&props); - + props.dictSize = 1 << dictionary; props.lc = literalContextBits; props.lp = literalPosBits; @@ -207,10 +207,10 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs) 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; } diff --git a/src/pylzma/pylzma_decompress.c b/src/pylzma/pylzma_decompress.c index 1045a98..992d0fe 100644 --- a/src/pylzma/pylzma_decompress.c +++ b/src/pylzma/pylzma_decompress.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -43,11 +43,11 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) { unsigned char *data; Byte *tmp; - PARSE_LENGTH_TYPE length; + Py_ssize_t length; int bufsize=BLOCK_SIZE; - PY_LONG_LONG totallength=-1; + Py_ssize_t totallength=-1; int lzma2 = 0; - PARSE_LENGTH_TYPE avail; + Py_ssize_t avail; PyObject *result=NULL; union { CLzmaDec lzma; @@ -60,8 +60,8 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) int propertiesLength; // possible keywords for this function static char *kwlist[] = {"data", "bufsize", "maxlength", "lzma2", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|iLi", kwlist, &data, &length, &bufsize, &totallength, &lzma2)) + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|ini", kwlist, &data, &length, &bufsize, &totallength, &lzma2)) return NULL; propertiesLength = lzma2 ? 1 : LZMA_PROPS_SIZE; @@ -72,10 +72,10 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) if (result == NULL) { return NULL; } - + tmp = (Byte *) PyBytes_AS_STRING(result); srcLen = length - propertiesLength; - destLen = totallength; + destLen = (size_t)totallength; Py_BEGIN_ALLOW_THREADS if (lzma2) { res = Lzma2Decode(tmp, &destLen, (Byte *) (data + propertiesLength), &srcLen, data[0], LZMA_FINISH_ANY, &status, &allocator); @@ -92,7 +92,7 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) } return result; } - + CreateMemoryOutStream(&outStream); tmp = (Byte *) malloc(bufsize); if (tmp == NULL) { @@ -126,7 +126,7 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) for (;;) { srcLen = avail; destLen = bufsize; - + if (lzma2) { res = Lzma2Dec_DecodeToBuf(&state.lzma2, tmp, &destLen, data, &srcLen, LZMA_FINISH_ANY, &status); } else { @@ -140,10 +140,10 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) if (res != SZ_OK || status == LZMA_STATUS_FINISHED_WITH_MARK || status == LZMA_STATUS_NEEDS_MORE_INPUT) { break; } - + } Py_END_ALLOW_THREADS - + if (status == LZMA_STATUS_NEEDS_MORE_INPUT) { PyErr_SetString(PyExc_ValueError, "data error during decompression"); } else if (res != SZ_OK) { @@ -151,7 +151,7 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) } else { result = PyBytes_FromStringAndSize((const char *) outStream.data, outStream.size); } - + exit: if (outStream.data != NULL) { free(outStream.data); @@ -162,6 +162,6 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs) LzmaDec_Free(&state.lzma, &allocator); } free(tmp); - + return result; } diff --git a/src/pylzma/pylzma_decompress_compat.c b/src/pylzma/pylzma_decompress_compat.c index 071745a..102be74 100644 --- a/src/pylzma/pylzma_decompress_compat.c +++ b/src/pylzma/pylzma_decompress_compat.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -33,7 +33,7 @@ void free_lzma_stream(lzma_stream *stream) if (stream->dynamicData) lzmafree(stream->dynamicData); stream->dynamicData = NULL; - + if (stream->dictionary) lzmafree(stream->dictionary); stream->dictionary = NULL; @@ -46,36 +46,41 @@ const char doc_decompress_compat[] = \ PyObject *pylzma_decompress_compat(PyObject *self, PyObject *args) { char *data; - PARSE_LENGTH_TYPE length; - PY_LONG_LONG blocksize=BLOCK_SIZE; + Py_ssize_t length; + Py_ssize_t blocksize=BLOCK_SIZE; PyObject *result = NULL; lzma_stream stream; int res; char *output; - - if (!PyArg_ParseTuple(args, "s#|L", &data, &length, &blocksize)) + + if (!PyArg_ParseTuple(args, "s#|n", &data, &length, &blocksize)) + return NULL; + + if (blocksize <= 0) { + PyErr_SetString(PyExc_ValueError, "bufsize must be greater than zero"); return NULL; - + } + memset(&stream, 0, sizeof(stream)); if (!(output = (char *)malloc(blocksize))) { PyErr_NoMemory(); goto exit; } - + lzmaCompatInit(&stream); stream.next_in = (Byte *)data; - stream.avail_in = length; + stream.avail_in = (UInt32)length; stream.next_out = (Byte *)output; - stream.avail_out = blocksize; - + stream.avail_out = (UInt32)blocksize; + // decompress data while (1) { Py_BEGIN_ALLOW_THREADS res = lzmaCompatDecode(&stream); Py_END_ALLOW_THREADS - + if (res == LZMA_STREAM_END) { break; } else if (res == LZMA_NOT_ENOUGH_MEM) { @@ -98,7 +103,7 @@ PyObject *pylzma_decompress_compat(PyObject *self, PyObject *args) PyErr_Format(PyExc_ValueError, "unknown return code from lzmaDecode: %d", res); goto exit; } - + // if we exit here, decompression finished without returning LZMA_STREAM_END // XXX: why is this sometimes? if (stream.avail_in == 0) @@ -106,11 +111,11 @@ PyObject *pylzma_decompress_compat(PyObject *self, PyObject *args) } result = PyBytes_FromStringAndSize(output, stream.totalOut); - + exit: free_lzma_stream(&stream); if (output != NULL) free(output); - + return result; } diff --git a/src/pylzma/pylzma_decompressobj.c b/src/pylzma/pylzma_decompressobj.c index aacea85..8a1bbf8 100644 --- a/src/pylzma/pylzma_decompressobj.c +++ b/src/pylzma/pylzma_decompressobj.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -33,18 +33,18 @@ pylzma_decomp_init(CDecompressionObject *self, PyObject *args, PyObject *kwargs) { PY_LONG_LONG max_length = -1; int lzma2 = 0; - + // possible keywords for this function static char *kwlist[] = {"maxlength", "lzma2", NULL}; - + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Li", kwlist, &max_length, &lzma2)) return -1; - + if (max_length == 0 || max_length < -1) { PyErr_SetString(PyExc_ValueError, "the decompressed size must be greater than zero"); return -1; } - + self->unconsumed_tail = NULL; self->unconsumed_length = 0; self->need_properties = 1; @@ -70,14 +70,14 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) PyObject *result=NULL; unsigned char *data; Byte *next_in, *next_out; - PARSE_LENGTH_TYPE length; + Py_ssize_t length; int res; - PY_LONG_LONG bufsize=BLOCK_SIZE; + Py_ssize_t bufsize=BLOCK_SIZE; SizeT avail_in; SizeT inProcessed, outProcessed; ELzmaStatus status; - - if (!PyArg_ParseTuple(args, "s#|L", &data, &length, &bufsize)){ + + if (!PyArg_ParseTuple(args, "s#|n", &data, &length, &bufsize)){ return NULL; } @@ -85,7 +85,7 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) PyErr_SetString(PyExc_ValueError, "bufsize must be greater than zero"); return NULL; } - + if (self->unconsumed_length > 0) { self->unconsumed_tail = (unsigned char *) realloc(self->unconsumed_tail, self->unconsumed_length + length); next_in = (unsigned char *) self->unconsumed_tail; @@ -93,9 +93,9 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) } else { next_in = data; } - + if (self->need_properties) { - int propertiesLength = self->lzma2 ? 1 : LZMA_PROPS_SIZE; + SizeT propertiesLength = self->lzma2 ? 1 : LZMA_PROPS_SIZE; if ((self->unconsumed_length + length) < propertiesLength) { // we need enough bytes to read the properties self->unconsumed_tail = (unsigned char *) realloc(self->unconsumed_tail, self->unconsumed_length + length); @@ -112,13 +112,13 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) if (self->lzma2) { res = Lzma2Dec_Allocate(&self->state.lzma2, next_in[0], &allocator); } else { - res = LzmaDec_Allocate(&self->state.lzma, next_in, propertiesLength, &allocator); + res = LzmaDec_Allocate(&self->state.lzma, next_in, (unsigned)propertiesLength, &allocator); } if (res != SZ_OK) { PyErr_SetString(PyExc_TypeError, "Incorrect stream properties"); return NULL; } - + next_in += propertiesLength; self->unconsumed_length -= propertiesLength; if (self->unconsumed_length > 0) { @@ -143,7 +143,7 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) } else { FREE_AND_NULL(self->unconsumed_tail); } - + self->need_properties = 0; if (self->lzma2) { Lzma2Dec_Init(&self->state.lzma2); @@ -158,13 +158,13 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) // no more bytes to decompress return PyBytes_FromString(""); } - + result = PyBytes_FromStringAndSize(NULL, bufsize); if (result == NULL) { PyErr_NoMemory(); goto exit; } - + next_out = (unsigned char *) PyBytes_AS_STRING(result); Py_BEGIN_ALLOW_THREADS // Decompress until EOS marker is reached @@ -181,7 +181,7 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) self->total_out += outProcessed; next_in += inProcessed; avail_in -= inProcessed; - + if (res != SZ_OK) { DEC_AND_NULL(result); PyErr_SetString(PyExc_ValueError, "data error during decompression"); @@ -207,10 +207,10 @@ pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args) } else { FREE_AND_NULL(self->unconsumed_tail); } - + self->unconsumed_length = avail_in; _PyBytes_Resize(&result, outProcessed); - + exit: return result; } @@ -224,27 +224,34 @@ pylzma_decomp_flush(CDecompressionObject *self, PyObject *args) { PyObject *result=NULL; int res; - SizeT avail_out, outsize; + SizeT avail_out; + Py_ssize_t outsize; unsigned char *tmp; SizeT inProcessed, outProcessed; ELzmaStatus status; - + if (self->max_length != -1) { - avail_out = self->max_length - self->total_out; + PY_LONG_LONG available = self->max_length - self->total_out; +#if LONG_MAX > PY_SIZE_MAX + if (available > (PY_LONG_LONG)PY_SIZE_MAX) { + available = PY_SIZE_MAX; + } +#endif + avail_out = (SizeT)available; } else { avail_out = BLOCK_SIZE; } - + if (avail_out == 0) { // no more remaining data return PyBytes_FromString(""); } - - result = PyBytes_FromStringAndSize(NULL, avail_out); + + result = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)avail_out); if (result == NULL) { return NULL; } - + tmp = (unsigned char *) PyBytes_AS_STRING(result); outsize = 0; while (1) { @@ -278,46 +285,46 @@ pylzma_decomp_flush(CDecompressionObject *self, PyObject *args) FREE_AND_NULL(self->unconsumed_tail); } Py_END_ALLOW_THREADS - + if (res != SZ_OK) { PyErr_SetString(PyExc_ValueError, "data error during decompression"); DEC_AND_NULL(result); goto exit; } - + if (!outProcessed && self->max_length != -1 && self->total_out < self->max_length) { PyErr_SetString(PyExc_ValueError, "data error during decompression"); DEC_AND_NULL(result); goto exit; } - + self->total_out += outProcessed; outsize += outProcessed; if (outProcessed < avail_out || (outProcessed == avail_out && self->max_length != -1)) { break; } - + if (self->max_length != -1) { PyErr_SetString(PyExc_ValueError, "not enough input data for decompression"); DEC_AND_NULL(result); goto exit; } - + avail_out -= outProcessed; - + // Output buffer is full, might be more data for decompression if (_PyBytes_Resize(&result, outsize+BLOCK_SIZE) != 0) { goto exit; } - + avail_out += BLOCK_SIZE; tmp = (unsigned char *) PyBytes_AS_STRING(result) + outsize; } - + if (outsize != PyBytes_GET_SIZE(result)) { _PyBytes_Resize(&result, outsize); } - + exit: return result; } @@ -330,13 +337,13 @@ static PyObject * pylzma_decomp_reset(CDecompressionObject *self, PyObject *args, PyObject *kwargs) { PY_LONG_LONG max_length = -1; - + // possible keywords for this function static char *kwlist[] = {"maxlength", NULL}; - + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|L", kwlist, &max_length)) return NULL; - + if (self->lzma2) { Lzma2Dec_Free(&self->state.lzma2, &allocator); Lzma2Dec_Construct(&self->state.lzma2); @@ -349,7 +356,7 @@ pylzma_decomp_reset(CDecompressionObject *self, PyObject *args, PyObject *kwargs self->need_properties = 1; self->total_out = 0; self->max_length = max_length; - + Py_INCREF(Py_None); return Py_None; } diff --git a/src/pylzma/pylzma_decompressobj.h b/src/pylzma/pylzma_decompressobj.h index 7d55db8..bad33cb 100644 --- a/src/pylzma/pylzma_decompressobj.h +++ b/src/pylzma/pylzma_decompressobj.h @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -39,8 +39,8 @@ typedef struct { CLzma2Dec lzma2; } state; ELzmaStatus status; - SizeT max_length; - SizeT total_out; + PY_LONG_LONG max_length; + PY_LONG_LONG total_out; unsigned char *unconsumed_tail; SizeT unconsumed_length; int need_properties; diff --git a/src/pylzma/pylzma_decompressobj_compat.c b/src/pylzma/pylzma_decompressobj_compat.c index f60e960..8e291c7 100644 --- a/src/pylzma/pylzma_decompressobj_compat.c +++ b/src/pylzma/pylzma_decompressobj_compat.c @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -38,12 +38,12 @@ static PyObject *pylzma_decomp_decompress(CCompatDecompressionObject *self, PyOb { PyObject *result=NULL; char *data; - PARSE_LENGTH_TYPE length, old_length; - PY_LONG_LONG start_total_out; + Py_ssize_t length, old_length; + UInt32 start_total_out; int res; - PY_LONG_LONG max_length=BLOCK_SIZE; - - if (!PyArg_ParseTuple(args, "s#|L", &data, &length, &max_length)) + Py_ssize_t max_length=BLOCK_SIZE; + + if (!PyArg_ParseTuple(args, "s#|n", &data, &length, &max_length)) return NULL; if (max_length < 0) @@ -51,7 +51,7 @@ static PyObject *pylzma_decomp_decompress(CCompatDecompressionObject *self, PyOb 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); @@ -59,43 +59,43 @@ static PyObject *pylzma_decomp_decompress(CCompatDecompressionObject *self, PyOb 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; - + + self->stream.avail_in = (UInt32)(self->unconsumed_length + length); + if (max_length && max_length < length) length = max_length; - + if (!(result = PyBytes_FromStringAndSize(NULL, length))) return NULL; - - self->stream.next_out = (unsigned char *) PyBytes_AS_STRING(result); - self->stream.avail_out = length; - + + self->stream.next_out = (Byte *) PyBytes_AS_STRING(result); + self->stream.avail_out = (UInt32)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 (_PyBytes_Resize(&result, length) < 0) goto exit; - - self->stream.avail_out = length - old_length; + + self->stream.avail_out = (UInt32)(length - old_length); self->stream.next_out = (Byte *) PyBytes_AS_STRING(result) + old_length; - + Py_BEGIN_ALLOW_THREADS res = lzmaCompatDecode(&self->stream); Py_END_ALLOW_THREADS } - + if (res == LZMA_NOT_ENOUGH_MEM) { // out of memory during decompression PyErr_NoMemory(); @@ -118,7 +118,7 @@ static PyObject *pylzma_decomp_decompress(CCompatDecompressionObject *self, PyOb { 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); @@ -145,11 +145,11 @@ static PyObject *pylzma_decomp_decompress(CCompatDecompressionObject *self, PyOb goto exit; } } - + _PyBytes_Resize(&result, self->stream.totalOut - start_total_out); - + exit: - return result; + return result; } static const char doc_decomp_reset[] = \ @@ -158,21 +158,21 @@ static const char doc_decomp_reset[] = \ 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 = PyBytes_FromString(""); CHECK_NULL(self->unused_data); - + result = Py_None; Py_XINCREF(result); - + exit: return result; } @@ -243,13 +243,13 @@ const char doc_decompressobj_compat[] = \ 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; @@ -260,12 +260,12 @@ PyObject *pylzma_decompressobj_compat(PyObject *self, PyObject *args) PyObject_Del(result); result = NULL; goto exit; - } - + } + memset(&result->stream, 0, sizeof(result->stream)); lzmaCompatInit(&result->stream); - + exit: - + return (PyObject *)result; } diff --git a/src/pylzma/pylzma_decompressobj_compat.h b/src/pylzma/pylzma_decompressobj_compat.h index 8ef3f34..5cdfa4f 100644 --- a/src/pylzma/pylzma_decompressobj_compat.h +++ b/src/pylzma/pylzma_decompressobj_compat.h @@ -9,16 +9,16 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * * $Id$ * */ @@ -34,7 +34,7 @@ typedef struct { PyObject_HEAD lzma_stream stream; char *unconsumed_tail; - int unconsumed_length; + UInt32 unconsumed_length; PyObject *unused_data; } CCompatDecompressionObject;