diff --git a/lzma/Ppmd8.h b/lzma/Ppmd8.h index b8dd2ee..c58d73d 100755 --- a/lzma/Ppmd8.h +++ b/lzma/Ppmd8.h @@ -69,7 +69,7 @@ typedef struct UInt32 Code; UInt32 Low; Byte *buf; - UInt32 bufUsed, bufLen; + UInt32 bufUsed, bufLen, overflow; Byte Indx2Units[PPMD_NUM_INDEXES]; Byte Units2Indx[128]; diff --git a/lzma/Ppmd8Enc.c b/lzma/Ppmd8Enc.c index 2c334d6..f5bff65 100755 --- a/lzma/Ppmd8Enc.c +++ b/lzma/Ppmd8Enc.c @@ -13,8 +13,12 @@ void Ppmd8_RangeEnc_FlushData(CPpmd8 *p) { unsigned i; for (i = 0; i < 4; i++, p->Low <<= 8 ) { - p->buf[p->bufUsed] = (Byte)(p->Low >> 24); - p->bufUsed++; + if (p->bufUsed < p->bufLen) { + p->buf[p->bufUsed] = (Byte)(p->Low >> 24); + p->bufUsed++; + } else { + p->overflow = 1; + } } // p->Stream.Out->Write(p->Stream.Out, (Byte)(p->Low >> 24)); } @@ -24,9 +28,13 @@ static void RangeEnc_Normalize(CPpmd8 *p) while ((p->Low ^ (p->Low + p->Range)) < kTop || (p->Range < kBot && ((p->Range = (0 - p->Low) & (kBot - 1)), 1))) { - p->buf[p->bufUsed] = (Byte)(p->Low >> 24); - p->bufUsed++; - //p->Stream.Out->Write(p->Stream.Out, (Byte)(p->Low >> 24)); + if (p->bufUsed < p->bufLen) { + p->buf[p->bufUsed] = (Byte)(p->Low >> 24); + p->bufUsed++; + //p->Stream.Out->Write(p->Stream.Out, (Byte)(p->Low >> 24)); + } else { + p->overflow = 1; + } p->Range <<= 8; p->Low <<= 8; } diff --git a/main.c b/main.c index 601eb77..cd2e0e0 100644 --- a/main.c +++ b/main.c @@ -1171,7 +1171,6 @@ plain_compress: */ *(tdat->compressed_chunk) = type; -printf("type: %d\n", type); /* * If encrypting, compute HMAC for chunk header and trailer. */ @@ -2049,7 +2048,7 @@ main(int argc, char *argv[]) fprintf(stderr, "Encryption only makes sense when compressing!\n"); exit(1); - } else if (pipe_mode && !pwd_file) { + } else if (pipe_mode && encrypt_type && !pwd_file) { fprintf(stderr, "Pipe mode requires password to be provided in a file.\n"); exit(1); } diff --git a/ppmd_compress.c b/ppmd_compress.c index 1940b83..7a9552d 100644 --- a/ppmd_compress.c +++ b/ppmd_compress.c @@ -111,11 +111,13 @@ ppmd_compress(void *src, size_t srclen, void *dst, _ppmd->buf = (Byte *)dst; _ppmd->bufLen = *dstlen; _ppmd->bufUsed = 0; + _ppmd->overflow = 0; Ppmd8_EncodeBuffer(_ppmd, _src, srclen); Ppmd8_EncodeSymbol(_ppmd, -1); Ppmd8_RangeEnc_FlushData(_ppmd); + if (_ppmd->overflow) return (-1); *dstlen = _ppmd->bufUsed; return (0); } @@ -133,6 +135,7 @@ ppmd_decompress(void *src, size_t srclen, void *dst, _ppmd->buf = (Byte *)_src; _ppmd->bufLen = srclen; _ppmd->bufUsed = 0; + _ppmd->overflow = 0; Ppmd8_RangeDec_Init(_ppmd); Ppmd8_Init(_ppmd, _ppmd->Order, PPMD8_RESTORE_METHOD_RESTART); @@ -143,7 +146,7 @@ ppmd_decompress(void *src, size_t srclen, void *dst, i++; } - if (i < *dstlen) + if (i < *dstlen || _ppmd->overflow) return (-1); return (0); } diff --git a/test/t1.tst b/test/t1.tst index 2ee9c01..e12040f 100644 --- a/test/t1.tst +++ b/test/t1.tst @@ -34,8 +34,8 @@ do echo "${cmd} errored." exit 1 fi - diff ${tf} ${tf}.1 | grep -i differ - if [ $? -eq 0 ] + diff ${tf} ${tf}.1 > /dev/null + if [ $? -ne 0 ] then echo "${cmd}: Decompression was not correct" exit 1 diff --git a/test/t2.tst b/test/t2.tst index 32dccb0..8453f31 100644 --- a/test/t2.tst +++ b/test/t2.tst @@ -28,8 +28,8 @@ do exit 1 fi - diff ${tf} ${tf}.1 | grep -i differ - if [ $? -eq 0 ] + diff ${tf} ${tf}.1 > /dev/null + if [ $? -ne 0 ] then echo "${cmd}: Decompression was not correct" exit 1 diff --git a/test/t3.tst b/test/t3.tst index c40c553..f71c66a 100644 --- a/test/t3.tst +++ b/test/t3.tst @@ -3,7 +3,7 @@ # clean() { - for algo in lzfx lz4 zlib bzip2 lzma lzmaMt libbsc + for algo in lzfx lz4 zlib bzip2 lzma lzmaMt libbsc ppmd adapt adapt2 do for tf in bin.dat share.dat inc.dat do @@ -65,8 +65,8 @@ do echo "${cmd} errored." exit 1 fi - diff ${tf}.${algo} ${tf}.${algo}.1 | grep -i differ - if [ $? -eq 0 ] + diff ${tf}.${algo} ${tf}.${algo}.1 > /dev/null + if [ $? -ne 0 ] then echo "${cmd}: Decompression was not correct" exit 1 diff --git a/test/t4.tst b/test/t4.tst index 1109929..9751643 100644 --- a/test/t4.tst +++ b/test/t4.tst @@ -30,8 +30,8 @@ do exit 1 fi - diff ${tf} ${tf}.1 | grep -i differ - if [ $? -eq 0 ] + diff ${tf} ${tf}.1 > /dev/null + if [ $? -ne 0 ] then echo "${cmd}: Decompression was not correct" exit 1 diff --git a/test/t5.tst b/test/t5.tst index 5235715..8b5e69f 100644 --- a/test/t5.tst +++ b/test/t5.tst @@ -40,8 +40,8 @@ do exit 1 fi - diff ${tf} ${tf}.1 | grep -i differ - if [ $? -eq 0 ] + diff ${tf} ${tf}.1 > /dev/null + if [ $? -ne 0 ] then echo "${cmd}: Decompression was not correct" exit 1 diff --git a/test/t6.tst b/test/t6.tst new file mode 100644 index 0000000..3de85bc --- /dev/null +++ b/test/t6.tst @@ -0,0 +1,49 @@ +# +# Simple compress and decompress +# +echo "#################################################" +echo "# Simple pipe mode compress and decompress" +echo "#################################################" + +for algo in lzfx lz4 adapt +do + ../../pcompress 2>&1 | grep $algo > /dev/null + [ $? -ne 0 ] && continue + + for level in 1 3 + do + for tf in combined.dat + do + for seg in 1m 2m 3m + do + cmd="cat ${tf} | ../../pcompress -p -c ${algo} -l ${level} -s ${seg} > ${tf}.pz" + echo "Running $cmd" + eval $cmd + if [ $? -ne 0 ] + then + echo "${cmd} errored." + exit 1 + fi + cmd="../../pcompress -d ${tf}.pz ${tf}.1" + echo "Running $cmd" + eval $cmd + if [ $? -ne 0 ] + then + echo "${cmd} errored." + exit 1 + fi + diff ${tf} ${tf}.1 > /dev/null + if [ $? -ne 0 ] + then + echo "${cmd}: Decompression was not correct" + exit 1 + fi + rm -f ${tf}.pz ${tf}.1 + done + done + done +done + +echo "#################################################" +echo "" + diff --git a/test/t7.tst b/test/t7.tst new file mode 100644 index 0000000..3fdd1e0 --- /dev/null +++ b/test/t7.tst @@ -0,0 +1,66 @@ +# +# Test crypto +# +echo "#################################################" +echo "# Pipe mode Crypto tests" +echo "#################################################" + +for algo in lzfx adapt2 +do + for tf in comb_d.dat + do + for feat in "-e" "-e -L" "-D -e" "-D -EE -L -e" "-e -S CRC64" + do + for seg in 2m 5m + do + echo "sillypassword" > /tmp/pwf + cmd="cat ${tf} | ../../pcompress -c${algo} -p -l3 -s${seg} $feat -w /tmp/pwf > ${tf}.pz" + echo "Running $cmd" + eval $cmd + if [ $? -ne 0 ] + then + echo "${cmd} errored." + exit 1 + fi + + pw=`cat /tmp/pwf` + if [ "$pw" = "sillypassword" ] + then + echo "ERROR: Password file /tmp/pwf not zeroed!" + exit 1 + fi + + echo "sillypassword" > /tmp/pwf + cmd="../../pcompress -d -w /tmp/pwf ${tf}.pz ${tf}.1" + echo "Running $cmd" + eval $cmd + if [ $? -ne 0 ] + then + echo "${cmd} errored." + exit 1 + fi + + diff ${tf} ${tf}.1 > /dev/null + if [ $? -ne 0 ] + then + echo "${cmd}: Decompression was not correct" + exit 1 + fi + + pw=`cat /tmp/pwf` + if [ "$pw" = "sillypassword" ] + then + echo "ERROR: Password file /tmp/pwf not zeroed!" + exit 1 + fi + rm -f ${tf}.pz ${tf}.1 + done + done + done +done + +rm -f /tmp/pwf + +echo "#################################################" +echo "" +