예제 #1
0
		static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize)
		{
			if (numIterations <= 0)
				return 0;
			if (dictionarySize < (1 << 18))
			{
				System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
				return 1;
			}
			System.Console.Write("\n       Compressing                Decompressing\n\n");

			Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
			Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();


			CoderPropID[] propIDs = 
			{ 
				CoderPropID.DictionarySize,
			};
			object[] properties = 
			{
				(Int32)(dictionarySize),
			};

			UInt32 kBufferSize = dictionarySize + kAdditionalSize;
			UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;

			encoder.SetCoderProperties(propIDs, properties);
			System.IO.MemoryStream propStream = new System.IO.MemoryStream();
			encoder.WriteCoderProperties(propStream);
			byte[] propArray = propStream.ToArray();

			CBenchRandomGenerator rg = new CBenchRandomGenerator();

			rg.Set(kBufferSize);
			rg.Generate();
			CRC crc = new CRC();
			crc.Init();
			crc.Update(rg.Buffer, 0, rg.BufferSize);

			CProgressInfo progressInfo = new CProgressInfo();
			progressInfo.ApprovedStart = dictionarySize;

			UInt64 totalBenchSize = 0;
			UInt64 totalEncodeTime = 0;
			UInt64 totalDecodeTime = 0;
			UInt64 totalCompressedSize = 0;

			MemoryStream inStream = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize);
			MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize);
			CrcOutStream crcOutStream = new CrcOutStream();
			for (Int32 i = 0; i < numIterations; i++)
			{
				progressInfo.Init();
				inStream.Seek(0, SeekOrigin.Begin);
				compressedStream.Seek(0, SeekOrigin.Begin);
				encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
				TimeSpan sp2 = DateTime.UtcNow - progressInfo.Time;
				UInt64 encodeTime = (UInt64)sp2.Ticks;

				long compressedSize = compressedStream.Position;
				if (progressInfo.InSize == 0)
					throw (new Exception("Internal ERROR 1282"));

				UInt64 decodeTime = 0;
				for (int j = 0; j < 2; j++)
				{
					compressedStream.Seek(0, SeekOrigin.Begin);
					crcOutStream.Init();

					decoder.SetDecoderProperties(propArray);
					UInt64 outSize = kBufferSize;
					System.DateTime startTime = DateTime.UtcNow;
					decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null);
					TimeSpan sp = (DateTime.UtcNow - startTime);
					decodeTime = (ulong)sp.Ticks;
					if (crcOutStream.GetDigest() != crc.GetDigest())
						throw (new Exception("CRC Error"));
				}
				UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize;
				PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
				System.Console.Write("     ");
				PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize);
				System.Console.WriteLine();

				totalBenchSize += benchSize;
				totalEncodeTime += encodeTime;
				totalDecodeTime += decodeTime;
				totalCompressedSize += (ulong)compressedSize;
			}
			System.Console.WriteLine("---------------------------------------------------");
			PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
			System.Console.Write("     ");
			PrintResults(dictionarySize, totalDecodeTime,
					kBufferSize * (UInt64)numIterations, true, totalCompressedSize);
			System.Console.WriteLine("    Average");
			return 0;
		}
예제 #2
0
파일: CRC.cs 프로젝트: EmilZhou/ConfuserEx
		private static uint CalculateDigest(byte[] data, uint offset, uint size) {
			var crc = new CRC();
			// crc.Init();
			crc.Update(data, offset, size);
			return crc.GetDigest();
		}
예제 #3
0
 public static uint Crc32(byte[] data, int offset, int len)
 {
     SevenZip.CRC crc = new SevenZip.CRC();
     crc.Update(data, (uint)offset, (uint)len);
     return(crc.GetDigest());
 }
예제 #4
0
        static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize)
        {
            if (numIterations <= 0)
            {
                return(0);
            }
            if (dictionarySize < (1 << 18))
            {
                System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
                return(1);
            }
            System.Console.Write("\n       Compressing                Decompressing\n\n");

            Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
            Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();


            CoderPropID[] propIDs =
            {
                CoderPropID.DictionarySize,
            };
            object[] properties =
            {
                (Int32)(dictionarySize),
            };

            UInt32 kBufferSize           = dictionarySize + kAdditionalSize;
            UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;

            encoder.SetCoderProperties(propIDs, properties);
            System.IO.MemoryStream propStream = new System.IO.MemoryStream();
            encoder.WriteCoderProperties(propStream);
            byte[] propArray = propStream.ToArray();

            CBenchRandomGenerator rg = new CBenchRandomGenerator();

            rg.Set(kBufferSize);
            rg.Generate();
            CRC crc = new CRC();

            crc.Init();
            crc.Update(rg.Buffer, 0, rg.BufferSize);

            CProgressInfo progressInfo = new CProgressInfo();

            progressInfo.ApprovedStart = dictionarySize;

            UInt64 totalBenchSize      = 0;
            UInt64 totalEncodeTime     = 0;
            UInt64 totalDecodeTime     = 0;
            UInt64 totalCompressedSize = 0;

            MemoryStream inStream         = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize);
            MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize);
            CrcOutStream crcOutStream     = new CrcOutStream();

            for (Int32 i = 0; i < numIterations; i++)
            {
                progressInfo.Init();
                inStream.Seek(0, SeekOrigin.Begin);
                compressedStream.Seek(0, SeekOrigin.Begin);
                encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
                TimeSpan sp2        = DateTime.UtcNow - progressInfo.Time;
                UInt64   encodeTime = (UInt64)sp2.Ticks;

                long compressedSize = compressedStream.Position;
                if (progressInfo.InSize == 0)
                {
                    throw (new Exception("Internal ERROR 1282"));
                }

                UInt64 decodeTime = 0;
                for (int j = 0; j < 2; j++)
                {
                    compressedStream.Seek(0, SeekOrigin.Begin);
                    crcOutStream.Init();

                    decoder.SetDecoderProperties(propArray);
                    UInt64          outSize   = kBufferSize;
                    System.DateTime startTime = DateTime.UtcNow;
                    decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null);
                    TimeSpan sp = (DateTime.UtcNow - startTime);
                    decodeTime = (ulong)sp.Ticks;
                    if (crcOutStream.GetDigest() != crc.GetDigest())
                    {
                        throw (new Exception("CRC Error"));
                    }
                }
                UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize;
                PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
                System.Console.Write("     ");
                PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize);
                System.Console.WriteLine();

                totalBenchSize      += benchSize;
                totalEncodeTime     += encodeTime;
                totalDecodeTime     += decodeTime;
                totalCompressedSize += (ulong)compressedSize;
            }
            System.Console.WriteLine("---------------------------------------------------");
            PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
            System.Console.Write("     ");
            PrintResults(dictionarySize, totalDecodeTime,
                         kBufferSize * (UInt64)numIterations, true, totalCompressedSize);
            System.Console.WriteLine("    Average");
            return(0);
        }
예제 #5
0
        public static int LzmaBenchmark(int numIterations, uint dictionarySize)
        {
            if (numIterations <= 0)
            {
                return(0);
            }
            if (dictionarySize < 262144U)
            {
                Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
                return(1);
            }
            Console.Write("\n       Compressing                Decompressing\n\n");
            Encoder encoder = new Encoder();
            Decoder decoder = new Decoder();

            CoderPropID[] propIDs = new CoderPropID[1]
            {
                CoderPropID.DictionarySize
            };
            object[] properties = new object[1]
            {
                (object)(int)dictionarySize
            };
            uint bufferSize = dictionarySize + 6291456U;
            int  capacity   = (int)(bufferSize / 2U) + 1024;

            encoder.SetCoderProperties(propIDs, properties);
            MemoryStream memoryStream1 = new MemoryStream();

            encoder.WriteCoderProperties((Stream)memoryStream1);
            byte[] array = memoryStream1.ToArray();
            LzmaBench.CBenchRandomGenerator cbenchRandomGenerator = new LzmaBench.CBenchRandomGenerator();
            cbenchRandomGenerator.Set(bufferSize);
            cbenchRandomGenerator.Generate();
            CRC crc = new CRC();

            crc.Init();
            crc.Update(cbenchRandomGenerator.Buffer, 0U, cbenchRandomGenerator.BufferSize);
            LzmaBench.CProgressInfo cprogressInfo = new LzmaBench.CProgressInfo();
            cprogressInfo.ApprovedStart = (long)dictionarySize;
            ulong        size1         = 0;
            ulong        elapsedTime1  = 0;
            ulong        elapsedTime2  = 0;
            ulong        secondSize    = 0;
            MemoryStream memoryStream2 = new MemoryStream(cbenchRandomGenerator.Buffer, 0, (int)cbenchRandomGenerator.BufferSize);
            MemoryStream memoryStream3 = new MemoryStream(capacity);

            LzmaBench.CrcOutStream crcOutStream = new LzmaBench.CrcOutStream();
            for (int index1 = 0; index1 < numIterations; ++index1)
            {
                cprogressInfo.Init();
                memoryStream2.Seek(0L, SeekOrigin.Begin);
                memoryStream3.Seek(0L, SeekOrigin.Begin);
                encoder.Code((Stream)memoryStream2, (Stream)memoryStream3, -1L, -1L, (ICodeProgress)cprogressInfo);
                ulong ticks    = (ulong)(DateTime.UtcNow - cprogressInfo.Time).Ticks;
                long  position = memoryStream3.Position;
                if (cprogressInfo.InSize == 0L)
                {
                    throw new Exception("Internal ERROR 1282");
                }
                ulong elapsedTime3 = 0;
                for (int index2 = 0; index2 < 2; ++index2)
                {
                    memoryStream3.Seek(0L, SeekOrigin.Begin);
                    crcOutStream.Init();
                    decoder.SetDecoderProperties(array);
                    ulong    num    = (ulong)bufferSize;
                    DateTime utcNow = DateTime.UtcNow;
                    decoder.Code((Stream)memoryStream3, (Stream)crcOutStream, 0L, (long)num, (ICodeProgress)null);
                    elapsedTime3 = (ulong)(DateTime.UtcNow - utcNow).Ticks;
                    if ((int)crcOutStream.GetDigest() != (int)crc.GetDigest())
                    {
                        throw new Exception("CRC Error");
                    }
                }
                ulong size2 = (ulong)bufferSize - (ulong)cprogressInfo.InSize;
                LzmaBench.PrintResults(dictionarySize, ticks, size2, false, 0UL);
                Console.Write("     ");
                LzmaBench.PrintResults(dictionarySize, elapsedTime3, (ulong)bufferSize, true, (ulong)position);
                Console.WriteLine();
                size1        += size2;
                elapsedTime1 += ticks;
                elapsedTime2 += elapsedTime3;
                secondSize   += (ulong)position;
            }
            Console.WriteLine("---------------------------------------------------");
            LzmaBench.PrintResults(dictionarySize, elapsedTime1, size1, false, 0UL);
            Console.Write("     ");
            LzmaBench.PrintResults(dictionarySize, elapsedTime2, (ulong)bufferSize * (ulong)numIterations, true, secondSize);
            Console.WriteLine("    Average");
            return(0);
        }
예제 #6
0
 private static bool VerifyDigest(uint digest, byte[] data, uint offset, uint size)
 {
     return((int)CRC.CalculateDigest(data, offset, size) == (int)digest);
 }
예제 #7
0
        public static int LzmaBenchmark(int numIterations, uint dictionarySize)
        {
            if (numIterations <= 0)
            {
                return(0);
            }
            if (dictionarySize < 262144)
            {
                Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
                return(1);
            }
            Console.Write("\n       Compressing                Decompressing\n\n");
            Encoder encoder = new Encoder();
            Decoder decoder = new Decoder();

            CoderPropID[] propIDs = new CoderPropID[1]
            {
                CoderPropID.DictionarySize
            };
            object[] properties = new object[1]
            {
                (int)dictionarySize
            };
            uint num      = dictionarySize + 6291456;
            uint capacity = num / 2u + 1024;

            encoder.SetCoderProperties(propIDs, properties);
            MemoryStream memoryStream = new MemoryStream();

            encoder.WriteCoderProperties(memoryStream);
            byte[] decoderProperties = memoryStream.ToArray();
            CBenchRandomGenerator cBenchRandomGenerator = new CBenchRandomGenerator();

            cBenchRandomGenerator.Set(num);
            cBenchRandomGenerator.Generate();
            CRC cRC = new CRC();

            cRC.Init();
            cRC.Update(cBenchRandomGenerator.Buffer, 0u, cBenchRandomGenerator.BufferSize);
            CProgressInfo cProgressInfo = new CProgressInfo();

            cProgressInfo.ApprovedStart = dictionarySize;
            ulong        num2          = 0uL;
            ulong        num3          = 0uL;
            ulong        num4          = 0uL;
            ulong        num5          = 0uL;
            MemoryStream memoryStream2 = new MemoryStream(cBenchRandomGenerator.Buffer, 0, (int)cBenchRandomGenerator.BufferSize);
            MemoryStream memoryStream3 = new MemoryStream((int)capacity);
            CrcOutStream crcOutStream  = new CrcOutStream();

            for (int i = 0; i < numIterations; i++)
            {
                cProgressInfo.Init();
                memoryStream2.Seek(0L, SeekOrigin.Begin);
                memoryStream3.Seek(0L, SeekOrigin.Begin);
                encoder.Code(memoryStream2, memoryStream3, -1L, -1L, cProgressInfo);
                ulong ticks    = (ulong)(DateTime.UtcNow - cProgressInfo.Time).Ticks;
                long  position = memoryStream3.Position;
                if (cProgressInfo.InSize == 0)
                {
                    throw new Exception("Internal ERROR 1282");
                }
                ulong num6 = 0uL;
                for (int j = 0; j < 2; j++)
                {
                    memoryStream3.Seek(0L, SeekOrigin.Begin);
                    crcOutStream.Init();
                    decoder.SetDecoderProperties(decoderProperties);
                    ulong    outSize = num;
                    DateTime utcNow  = DateTime.UtcNow;
                    decoder.Code(memoryStream3, crcOutStream, 0L, (long)outSize, null);
                    num6 = (ulong)(DateTime.UtcNow - utcNow).Ticks;
                    if (crcOutStream.GetDigest() != cRC.GetDigest())
                    {
                        throw new Exception("CRC Error");
                    }
                }
                ulong num7 = (ulong)(num - cProgressInfo.InSize);
                PrintResults(dictionarySize, ticks, num7, decompressMode: false, 0uL);
                Console.Write("     ");
                PrintResults(dictionarySize, num6, num, decompressMode: true, (ulong)position);
                Console.WriteLine();
                num2 += num7;
                num3 += ticks;
                num4 += num6;
                num5  = (ulong)((long)num5 + position);
            }
            Console.WriteLine("---------------------------------------------------");
            PrintResults(dictionarySize, num3, num2, decompressMode: false, 0uL);
            Console.Write("     ");
            PrintResults(dictionarySize, num4, (ulong)(num * numIterations), decompressMode: true, num5);
            Console.WriteLine("    Average");
            return(0);
        }