public static long CompressData(Stream inStream, Stream outStream, long dataSize, bool writeCompressedSize = false, ICodeProgress progressCallback = null)
        {
            var origPos = outStream.Position;
            var propIDs = new CoderPropID[]
            {
                CoderPropID.DictionarySize,
                CoderPropID.PosStateBits,
                CoderPropID.LitContextBits,
                CoderPropID.LitPosBits,
                CoderPropID.EndMarker
            };
            var propValues = new System.Object[]
            {
                (int)(1 << 23),  //(int)2048,
                (int)2,
                (int)3,
                (int)0,
                true
            };
            var encoder = new Encoder();

            encoder.SetCoderProperties(propIDs, propValues);
            encoder.WriteCoderProperties(outStream);
            //if (writeCompressedSize)
            //{
            //
            //}
            encoder.Code(inStream, outStream, dataSize, -1, progressCallback);
            return(outStream.Position - origPos);
        }
Esempio n. 2
0
        private static byte[] compress(byte[] decompressed)
        {
            byte[] retVal         = null;
            bool   eos            = true;
            Int32  dictionary     = 1 << 16;
            Int32  posStateBits   = 2;
            Int32  litContextBits = 3; // for normal files
                                       // UInt32 litContextBits = 0; // for 32-bit data
            Int32 litPosBits = 0;
            // UInt32 litPosBits = 2; // for 32-bit data
            Int32  algorithm    = 2;
            Int32  numFastBytes = 64;
            string mf           = "bt4";

            var propIDs = new CoderPropID[]
            {
                CoderPropID.DictionarySize,
                CoderPropID.PosStateBits,
                CoderPropID.LitContextBits,
                CoderPropID.LitPosBits,
                CoderPropID.Algorithm,
                CoderPropID.NumFastBytes,
                CoderPropID.MatchFinder,
                CoderPropID.EndMarker
            };
            var properties = new object[]
            {
                dictionary,
                posStateBits,
                litContextBits,
                litPosBits,
                algorithm,
                numFastBytes,
                mf,
                eos
            };

            SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
            using (Stream strmInStream = new MemoryStream(decompressed))
            {
                strmInStream.Seek(0, 0);
                using (MemoryStream strmOutStream = new MemoryStream())
                {
                    encoder.SetCoderProperties(propIDs, properties);
                    encoder.WriteCoderProperties(strmOutStream);
                    Int64 fileSize = strmInStream.Length;
                    for (int i = 0; i < 8; i++)
                    {
                        strmOutStream.WriteByte((Byte)(fileSize >> (8 * i)));
                    }
                    encoder.Code(strmInStream, strmOutStream, -1, -1, null);
                    retVal = strmOutStream.ToArray();
                }
            }
            return(retVal);
        }
Esempio n. 3
0
        public static byte[] Encode(CompressionConfig compressionConfig, byte[] data, long dataLength, out long encodedDataLength)
        {
            ArgumentChecker.NotNull(compressionConfig, "compressionConfig");
            ArgumentChecker.NotNull(data, "data");
            //
            using (MemoryStream outStream = new MemoryStream()) {
                using (Stream inStream = new MemoryStream(data)) {
                    const Int32  dictionary     = 1 << 21;
                    const string mf             = "bt4";
                    const bool   eos            = false;
                    const Int32  posStateBits   = 2;
                    const Int32  litContextBits = 3;
                    const Int32  litPosBits     = 0;
                    const Int32  algorithm      = 2;
                    const Int32  numFastBytes   = 128;

                    CoderPropID[] propIDs = new CoderPropID[] {
                        CoderPropID.DictionarySize,
                        CoderPropID.PosStateBits,
                        CoderPropID.LitContextBits,
                        CoderPropID.LitPosBits,
                        CoderPropID.Algorithm,
                        CoderPropID.NumFastBytes,
                        CoderPropID.MatchFinder,
                        CoderPropID.EndMarker
                    };
                    object[] properties = new object[] {
                        dictionary,
                        posStateBits,
                        litContextBits,
                        litPosBits,
                        algorithm,
                        numFastBytes,
                        mf,
                        eos
                    };
                    Encoder encoder = new Encoder();
                    encoder.SetCoderProperties(propIDs, properties);
                    encoder.WriteCoderProperties(outStream);
                    //
                    for (int i = 0; i < 8; i++)
                    {
                        outStream.WriteByte((Byte)(dataLength >> (8 * i)));
                    }
                    //

                    encoder.Code(inStream, outStream, dataLength, -1, null);
                }
                //
                encodedDataLength = outStream.Length;
                return(outStream.GetBuffer());
            }
        }
Esempio n. 4
0
    public static void Zip(Stream inStream, Stream outStream)
    {
        bool flag = false;
        int  num  = 2097152;

        if (!flag)
        {
            num = 8388608;
        }
        int    num2  = 2;
        int    num3  = 3;
        int    num4  = 0;
        int    num5  = 2;
        int    num6  = 128;
        string text  = "bt4";
        bool   flag2 = false;

        CoderPropID[] propIDs = new CoderPropID[]
        {
            CoderPropID.DictionarySize,
            CoderPropID.PosStateBits,
            CoderPropID.LitContextBits,
            CoderPropID.LitPosBits,
            CoderPropID.Algorithm,
            CoderPropID.NumFastBytes,
            CoderPropID.MatchFinder,
            CoderPropID.EndMarker
        };
        object[] properties = new object[]
        {
            num,
            num2,
            num3,
            num4,
            num5,
            num6,
            text,
            flag2
        };
        Encoder encoder = new Encoder();

        encoder.SetCoderProperties(propIDs, properties);
        encoder.WriteCoderProperties(outStream);
        long length = inStream.Length;

        for (int i = 0; i < 8; i++)
        {
            outStream.WriteByte((byte)(length >> 8 * i));
        }
        encoder.Code(inStream, outStream, -1L, -1L, null);
    }
Esempio n. 5
0
		public void SetCoderProperties(CoderPropID[] propIDs, object[] properties)
		{
			for (UInt32 i = 0; i < properties.Length; i++)
			{
				object prop = properties[i];
				switch (propIDs[i])
				{
					case CoderPropID.NumFastBytes:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 numFastBytes = (Int32)prop;
						if (numFastBytes < 5 || numFastBytes > Base.kMatchMaxLen)
							throw new InvalidParamException();
						_numFastBytes = (UInt32)numFastBytes;
						break;
					}
					case CoderPropID.Algorithm:
					{
						/*
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 maximize = (Int32)prop;
						_fastMode = (maximize == 0);
						_maxMode = (maximize >= 2);
						*/
						break;
					}
					case CoderPropID.MatchFinder:
					{
						if (!(prop is String))
							throw new InvalidParamException();
						EMatchFinderType matchFinderIndexPrev = _matchFinderType;
						int m = FindMatchFinder(((string)prop).ToUpper());
						if (m < 0)
							throw new InvalidParamException();
						_matchFinderType = (EMatchFinderType)m;
						if (_matchFinder != null && matchFinderIndexPrev != _matchFinderType)
							{
							_dictionarySizePrev = 0xFFFFFFFF;
							_matchFinder = null;
							}
						break;
					}
					case CoderPropID.DictionarySize:
					{
						const int kDicLogSizeMaxCompress = 30;
						if (!(prop is Int32))
							throw new InvalidParamException(); ;
						Int32 dictionarySize = (Int32)prop;
						if (dictionarySize < (UInt32)(1 << Base.kDicLogSizeMin) ||
							dictionarySize > (UInt32)(1 << kDicLogSizeMaxCompress))
							throw new InvalidParamException();
						_dictionarySize = (UInt32)dictionarySize;
						int dicLogSize;
						for (dicLogSize = 0; dicLogSize < (UInt32)kDicLogSizeMaxCompress; dicLogSize++)
							if (dictionarySize <= ((UInt32)(1) << dicLogSize))
								break;
						_distTableSize = (UInt32)dicLogSize * 2;
						break;
					}
					case CoderPropID.PosStateBits:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 v = (Int32)prop;
						if (v < 0 || v > (UInt32)Base.kNumPosStatesBitsEncodingMax)
							throw new InvalidParamException();
						_posStateBits = (int)v;
						_posStateMask = (((UInt32)1) << (int)_posStateBits) - 1;
						break;
					}
					case CoderPropID.LitPosBits:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 v = (Int32)prop;
						if (v < 0 || v > (UInt32)Base.kNumLitPosStatesBitsEncodingMax)
							throw new InvalidParamException();
						_numLiteralPosStateBits = (int)v;
						break;
					}
					case CoderPropID.LitContextBits:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 v = (Int32)prop;
						if (v < 0 || v > (UInt32)Base.kNumLitContextBitsMax)
							throw new InvalidParamException(); ;
						_numLiteralContextBits = (int)v;
						break;
					}
					case CoderPropID.EndMarker:
					{
						if (!(prop is Boolean))
							throw new InvalidParamException();
						SetWriteEndMarkerMode((Boolean)prop);
						break;
					}
					default:
						throw new InvalidParamException();
				}
			}
		}
Esempio n. 6
0
        private static int Main2(string[] args)
        {
            Console.WriteLine("\nLZMA# 4.61  2008-11-23\n");
            if (args.Length == 0)
            {
                LzmaAlone.PrintHelp();
                return(0);
            }
            SwitchForm[] switchForms = new SwitchForm[13];
            int          num1        = 0;

            SwitchForm[] switchFormArray1 = switchForms;
            int          index1           = num1;
            int          num2             = index1 + 1;
            SwitchForm   switchForm1      = new SwitchForm("?", SwitchType.Simple, false);

            switchFormArray1[index1] = switchForm1;
            SwitchForm[] switchFormArray2 = switchForms;
            int          index2           = num2;
            int          num3             = index2 + 1;
            SwitchForm   switchForm2      = new SwitchForm("H", SwitchType.Simple, false);

            switchFormArray2[index2] = switchForm2;
            SwitchForm[] switchFormArray3 = switchForms;
            int          index3           = num3;
            int          num4             = index3 + 1;
            SwitchForm   switchForm3      = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray3[index3] = switchForm3;
            SwitchForm[] switchFormArray4 = switchForms;
            int          index4           = num4;
            int          num5             = index4 + 1;
            SwitchForm   switchForm4      = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray4[index4] = switchForm4;
            SwitchForm[] switchFormArray5 = switchForms;
            int          index5           = num5;
            int          num6             = index5 + 1;
            SwitchForm   switchForm5      = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray5[index5] = switchForm5;
            SwitchForm[] switchFormArray6 = switchForms;
            int          index6           = num6;
            int          num7             = index6 + 1;
            SwitchForm   switchForm6      = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray6[index6] = switchForm6;
            SwitchForm[] switchFormArray7 = switchForms;
            int          index7           = num7;
            int          num8             = index7 + 1;
            SwitchForm   switchForm7      = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray7[index7] = switchForm7;
            SwitchForm[] switchFormArray8 = switchForms;
            int          index8           = num8;
            int          num9             = index8 + 1;
            SwitchForm   switchForm8      = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray8[index8] = switchForm8;
            SwitchForm[] switchFormArray9 = switchForms;
            int          index9           = num9;
            int          num10            = index9 + 1;
            SwitchForm   switchForm9      = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray9[index9] = switchForm9;
            SwitchForm[] switchFormArray10 = switchForms;
            int          index10           = num10;
            int          num11             = index10 + 1;
            SwitchForm   switchForm10      = new SwitchForm("EOS", SwitchType.Simple, false);

            switchFormArray10[index10] = switchForm10;
            SwitchForm[] switchFormArray11 = switchForms;
            int          index11           = num11;
            int          num12             = index11 + 1;
            SwitchForm   switchForm11      = new SwitchForm("SI", SwitchType.Simple, false);

            switchFormArray11[index11] = switchForm11;
            SwitchForm[] switchFormArray12 = switchForms;
            int          index12           = num12;
            int          num13             = index12 + 1;
            SwitchForm   switchForm12      = new SwitchForm("SO", SwitchType.Simple, false);

            switchFormArray12[index12] = switchForm12;
            SwitchForm[] switchFormArray13 = switchForms;
            int          index13           = num13;
            int          numSwitches       = index13 + 1;
            SwitchForm   switchForm13      = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1);

            switchFormArray13[index13] = switchForm13;
            Parser parser = new Parser(numSwitches);

            try
            {
                parser.ParseStrings(switchForms, args);
            }
            catch
            {
                return(LzmaAlone.IncorrectCommand());
            }
            if (parser[0].ThereIs || parser[1].ThereIs)
            {
                LzmaAlone.PrintHelp();
                return(0);
            }
            ArrayList nonSwitchStrings = parser.NonSwitchStrings;
            int       num14            = 0;

            if (num14 >= nonSwitchStrings.Count)
            {
                return(LzmaAlone.IncorrectCommand());
            }
            ArrayList arrayList1 = nonSwitchStrings;
            int       index14    = num14;
            int       num15      = index14 + 1;
            string    lower1     = ((string)arrayList1[index14]).ToLower();
            bool      flag1      = false;
            int       num16      = 2097152;

            if (parser[3].ThereIs)
            {
                int v;
                if (!LzmaAlone.GetNumber((string)parser[3].PostStrings[0], out v))
                {
                    LzmaAlone.IncorrectCommand();
                }
                num16 = 1 << v;
                flag1 = true;
            }
            string str = "bt4";

            if (parser[8].ThereIs)
            {
                str = (string)parser[8].PostStrings[0];
            }
            string lower2 = str.ToLower();
            int    num17;

            if (lower1 == "b")
            {
                int v = 10;
                if (num15 < nonSwitchStrings.Count)
                {
                    ArrayList arrayList2 = nonSwitchStrings;
                    int       index15    = num15;
                    num17 = index15 + 1;
                    if (!LzmaAlone.GetNumber((string)arrayList2[index15], out v))
                    {
                        v = 10;
                    }
                }
                return(LzmaBench.LzmaBenchmark(v, (uint)num16));
            }
            string path = "";

            if (parser[12].ThereIs)
            {
                path = (string)parser[12].PostStrings[0];
            }
            bool flag2 = false;

            if (lower1 == "e")
            {
                flag2 = true;
            }
            else if (lower1 == "d")
            {
                flag2 = false;
            }
            else
            {
                LzmaAlone.IncorrectCommand();
            }
            bool thereIs = parser[10].ThereIs;
            int  num18   = parser[11].ThereIs ? 1 : 0;

            if (thereIs)
            {
                throw new Exception("Not implemeted");
            }
            if (num15 >= nonSwitchStrings.Count)
            {
                LzmaAlone.IncorrectCommand();
            }
            ArrayList arrayList3 = nonSwitchStrings;
            int       index16    = num15;
            int       num19      = index16 + 1;
            Stream    inStream   = (Stream) new FileStream((string)arrayList3[index16], FileMode.Open, FileAccess.Read);

            if (num18 != 0)
            {
                throw new Exception("Not implemeted");
            }
            if (num19 >= nonSwitchStrings.Count)
            {
                LzmaAlone.IncorrectCommand();
            }
            ArrayList arrayList4 = nonSwitchStrings;
            int       index17    = num19;

            num17 = index17 + 1;
            FileStream fileStream1 = new FileStream((string)arrayList4[index17], FileMode.Create, FileAccess.Write);
            FileStream fileStream2 = (FileStream)null;

            if (path.Length != 0)
            {
                fileStream2 = new FileStream(path, FileMode.Open, FileAccess.Read);
            }
            if (flag2)
            {
                if (!flag1)
                {
                    num16 = 8388608;
                }
                int  v1    = 2;
                int  v2    = 3;
                int  v3    = 0;
                int  v4    = 2;
                int  v5    = 128;
                bool flag3 = parser[9].ThereIs | thereIs;
                if (parser[2].ThereIs && !LzmaAlone.GetNumber((string)parser[2].PostStrings[0], out v4))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[4].ThereIs && !LzmaAlone.GetNumber((string)parser[4].PostStrings[0], out v5))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[5].ThereIs && !LzmaAlone.GetNumber((string)parser[5].PostStrings[0], out v2))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[6].ThereIs && !LzmaAlone.GetNumber((string)parser[6].PostStrings[0], out v3))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[7].ThereIs && !LzmaAlone.GetNumber((string)parser[7].PostStrings[0], out v1))
                {
                    LzmaAlone.IncorrectCommand();
                }
                CoderPropID[] propIDs = new CoderPropID[8]
                {
                    CoderPropID.DictionarySize,
                    CoderPropID.PosStateBits,
                    CoderPropID.LitContextBits,
                    CoderPropID.LitPosBits,
                    CoderPropID.Algorithm,
                    CoderPropID.NumFastBytes,
                    CoderPropID.MatchFinder,
                    CoderPropID.EndMarker
                };
                object[] properties = new object[8]
                {
                    (object)num16,
                    (object)v1,
                    (object)v2,
                    (object)v3,
                    (object)v4,
                    (object)v5,
                    (object)lower2,
                    (object)flag3
                };
                Encoder encoder = new Encoder();
                encoder.SetCoderProperties(propIDs, properties);
                encoder.WriteCoderProperties((Stream)fileStream1);
                long num20 = !(flag3 | thereIs) ? inStream.Length : -1L;
                for (int index15 = 0; index15 < 8; ++index15)
                {
                    fileStream1.WriteByte((byte)(num20 >> 8 * index15));
                }
                if (fileStream2 != null)
                {
                    CDoubleStream cdoubleStream = new CDoubleStream();
                    cdoubleStream.s1        = (Stream)fileStream2;
                    cdoubleStream.s2        = inStream;
                    cdoubleStream.fileIndex = 0;
                    inStream = (Stream)cdoubleStream;
                    long length = fileStream2.Length;
                    cdoubleStream.skipSize = 0L;
                    if (length > (long)num16)
                    {
                        cdoubleStream.skipSize = length - (long)num16;
                    }
                    fileStream2.Seek(cdoubleStream.skipSize, SeekOrigin.Begin);
                    encoder.SetTrainSize((uint)(length - cdoubleStream.skipSize));
                }
                encoder.Code(inStream, (Stream)fileStream1, -1L, -1L, (ICodeProgress)null);
            }
            else
            {
                if (!(lower1 == "d"))
                {
                    throw new Exception("Command Error");
                }
                byte[] numArray = new byte[5];
                if (inStream.Read(numArray, 0, 5) != 5)
                {
                    throw new Exception("input .lzma is too short");
                }
                Decoder decoder = new Decoder();
                decoder.SetDecoderProperties(numArray);
                if (fileStream2 != null && !decoder.Train((Stream)fileStream2))
                {
                    throw new Exception("can't train");
                }
                long outSize = 0;
                for (int index15 = 0; index15 < 8; ++index15)
                {
                    int num20 = inStream.ReadByte();
                    if (num20 < 0)
                    {
                        throw new Exception("Can't Read 1");
                    }
                    outSize |= (long)(byte)num20 << 8 * index15;
                }
                long inSize = inStream.Length - inStream.Position;
                decoder.Code(inStream, (Stream)fileStream1, inSize, outSize, (ICodeProgress)null);
            }
            return(0);
        }
Esempio n. 7
0
 public abstract void SetCoderProperties(CoderPropID[] propIDs, object[] properties);
Esempio n. 8
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);
        }
Esempio n. 9
0
        public void SetCoderProperties(CoderPropID[] propIDs, object[] properties)
        {
            for (uint i = 0; i < properties.Length; i++)
            {
                EMatchFinderType type;
                int num4;
                int num6;
                object obj2 = properties[i];
                switch (propIDs[i])
                {
                    case CoderPropID.DictionarySize:
                        if (!(obj2 is int))
                        {
                            throw new InvalidParamException();
                        }
                        goto Label_013B;

                    case CoderPropID.PosStateBits:
                        if (!(obj2 is int))
                        {
                            throw new InvalidParamException();
                        }
                        goto Label_01C3;

                    case CoderPropID.LitContextBits:
                        if (!(obj2 is int))
                        {
                            throw new InvalidParamException();
                        }
                        goto Label_0267;

                    case CoderPropID.LitPosBits:
                        if (!(obj2 is int))
                        {
                            throw new InvalidParamException();
                        }
                        goto Label_0220;

                    case CoderPropID.NumFastBytes:
                        if (!(obj2 is int))
                        {
                            throw new InvalidParamException();
                        }
                        break;

                    case CoderPropID.MatchFinder:
                        if (!(obj2 is string))
                        {
                            throw new InvalidParamException();
                        }
                        goto Label_00C0;

                    case CoderPropID.Algorithm:
                    {
                        continue;
                    }
                    case CoderPropID.EndMarker:
                        if (!(obj2 is bool))
                        {
                            throw new InvalidParamException();
                        }
                        goto Label_02AE;

                    default:
                        throw new InvalidParamException();
                }
                int num2 = (int) obj2;
                if ((num2 < 5) || (num2 > 0x111L))
                {
                    throw new InvalidParamException();
                }
                this._numFastBytes = (uint) num2;
                continue;
            Label_00C0:
                type = this._matchFinderType;
                int num3 = FindMatchFinder(((string) obj2).ToUpper());
                if (num3 < 0)
                {
                    throw new InvalidParamException();
                }
                this._matchFinderType = (EMatchFinderType) num3;
                if ((this._matchFinder != null) && (type != this._matchFinderType))
                {
                    this._dictionarySizePrev = uint.MaxValue;
                    this._matchFinder = null;
                }
                continue;
            Label_013B:
                num4 = (int) obj2;
                if ((num4 < 1L) || (num4 > 0x40000000L))
                {
                    throw new InvalidParamException();
                }
                this._dictionarySize = (uint) num4;
                int num5 = 0;
                while (num5 < 30L)
                {
                    if (num4 <= (((uint) 1) << num5))
                    {
                        break;
                    }
                    num5++;
                }
                this._distTableSize = (uint) (num5 * 2);
                continue;
            Label_01C3:
                num6 = (int) obj2;
                if ((num6 < 0) || (num6 > 4L))
                {
                    throw new InvalidParamException();
                }
                this._posStateBits = num6;
                this._posStateMask = (uint) ((((int) 1) << this._posStateBits) - 1);
                continue;
            Label_0220:
                num6 = (int) obj2;
                if ((num6 < 0) || (num6 > 4L))
                {
                    throw new InvalidParamException();
                }
                this._numLiteralPosStateBits = num6;
                continue;
            Label_0267:
                num6 = (int) obj2;
                if ((num6 < 0) || (num6 > 8L))
                {
                    throw new InvalidParamException();
                }
                this._numLiteralContextBits = num6;
                continue;
            Label_02AE:
                this.SetWriteEndMarkerMode((bool) obj2);
            }
        }
Esempio n. 10
0
        public static void Encrypt()
        {
            Encoder encoder = new Encoder();

            string[] files  = Directory.GetFiles(@"In\Decrypted - SC", "*.sc");
            string   output = @"Out\Encrypted - SC";

            int filesAmount = 0;

            foreach (string file in files)
            {
                filesAmount++;
            }
            if (filesAmount == 0)
            {
                MessageBox.Show("No CSV files were found in the folder \"In\\Decrypted - SC.\""
                                + Environment.NewLine + "Place decrypted files in the folder and try again.",
                                "No Files Found",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return;
            }

            if (!Directory.Exists(output))
            {
                Directory.CreateDirectory(output);
            }

            try
            {
                foreach (string str in files)
                {
                    FileInfo fileInfo = new FileInfo(str);
                    using (FileStream fileStream1 = new FileStream(str, FileMode.Open))
                    {
                        using (FileStream fileStream2 = new FileStream(Path.Combine(output, Path.GetFileName(str)), FileMode.Create))
                        {
                            CoderPropID[] propIDs = new CoderPropID[8]
                            {
                                CoderPropID.DictionarySize,
                                CoderPropID.PosStateBits,
                                CoderPropID.LitContextBits,
                                CoderPropID.LitPosBits,
                                CoderPropID.Algorithm,
                                CoderPropID.NumFastBytes,
                                CoderPropID.MatchFinder,
                                CoderPropID.EndMarker
                            };
                            object[] properties = new object[8]
                            {
                                (object)262144,
                                (object)2,
                                (object)3,
                                (object)0,
                                (object)2,
                                (object)32,
                                (object)"bt4",
                                (object)false
                            };
                            encoder.SetCoderProperties(propIDs, properties);
                            encoder.WriteCoderProperties((Stream)fileStream2);
                            fileStream2.Write(BitConverter.GetBytes(fileStream1.Length), 0, 4);
                            encoder.Code((Stream)fileStream1, (Stream)fileStream2, fileStream1.Length, -1L, (ICodeProgress)null);
                            fileStream2.Flush();
                            fileStream2.Close();
                        }
                        fileStream1.Close();
                    }
                }
                var success = MessageBox.Show("Successfully Encrypted SC Files!" + Environment.NewLine +
                                              "Select OK to open the folder, else select cancel!",
                                              "Success",
                                              MessageBoxButtons.OKCancel,
                                              MessageBoxIcon.Information);
                if (success == DialogResult.OK)
                {
                    Process.Start(output);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("An error occured, please try again or contact someone on the Ultrapowa Network."
                                + Environment.NewLine + "Error: " + e,
                                "Error - Key:3",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
Esempio n. 11
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);
        }
Esempio n. 12
0
        private static int Main2(string[] args)
        {
            Console.WriteLine("\nLZMA# 4.61  2008-11-23\n");
            if (args.Length == 0)
            {
                LzmaAlone.PrintHelp();
                return(0);
            }
            SwitchForm[] array       = new SwitchForm[13];
            int          numSwitches = 0;

            array[numSwitches++] = new SwitchForm("?", SwitchType.Simple, false);
            array[numSwitches++] = new SwitchForm("H", SwitchType.Simple, false);
            array[numSwitches++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1);
            array[numSwitches++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1);
            array[numSwitches++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1);
            array[numSwitches++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1);
            array[numSwitches++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1);
            array[numSwitches++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1);
            array[numSwitches++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1);
            array[numSwitches++] = new SwitchForm("EOS", SwitchType.Simple, false);
            array[numSwitches++] = new SwitchForm("SI", SwitchType.Simple, false);
            array[numSwitches++] = new SwitchForm("SO", SwitchType.Simple, false);
            array[numSwitches++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1);
            Parser parser = new Parser(numSwitches);

            try
            {
                parser.ParseStrings(array, args);
            }
            catch
            {
                int result = LzmaAlone.IncorrectCommand();
                return(result);
            }
            if (parser[0].ThereIs || parser[1].ThereIs)
            {
                LzmaAlone.PrintHelp();
                return(0);
            }
            ArrayList nonSwitchStrings = parser.NonSwitchStrings;
            int       num = 0;

            if (num >= nonSwitchStrings.Count)
            {
                return(LzmaAlone.IncorrectCommand());
            }
            string text = (string)nonSwitchStrings[num++];

            text = text.ToLower();
            bool flag = false;
            int  num2 = 2097152;

            if (parser[3].ThereIs)
            {
                int num3;
                if (!LzmaAlone.GetNumber((string)parser[3].PostStrings[0], out num3))
                {
                    LzmaAlone.IncorrectCommand();
                }
                num2 = 1 << num3;
                flag = true;
            }
            string text2 = "bt4";

            if (parser[8].ThereIs)
            {
                text2 = (string)parser[8].PostStrings[0];
            }
            text2 = text2.ToLower();
            if (text == "b")
            {
                int numIterations = 10;
                if (num < nonSwitchStrings.Count && !LzmaAlone.GetNumber((string)nonSwitchStrings[num++], out numIterations))
                {
                    numIterations = 10;
                }
                return(LzmaBench.LzmaBenchmark(numIterations, (uint)num2));
            }
            string text3 = string.Empty;

            if (parser[12].ThereIs)
            {
                text3 = (string)parser[12].PostStrings[0];
            }
            bool flag2 = false;

            if (text == "e")
            {
                flag2 = true;
            }
            else if (text == "d")
            {
                flag2 = false;
            }
            else
            {
                LzmaAlone.IncorrectCommand();
            }
            bool thereIs  = parser[10].ThereIs;
            bool thereIs2 = parser[11].ThereIs;

            if (thereIs)
            {
                throw new Exception("Not implemeted");
            }
            if (num >= nonSwitchStrings.Count)
            {
                LzmaAlone.IncorrectCommand();
            }
            string path   = (string)nonSwitchStrings[num++];
            Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);

            if (thereIs2)
            {
                throw new Exception("Not implemeted");
            }
            if (num >= nonSwitchStrings.Count)
            {
                LzmaAlone.IncorrectCommand();
            }
            string     path2       = (string)nonSwitchStrings[num++];
            FileStream fileStream  = new FileStream(path2, FileMode.Create, FileAccess.Write);
            FileStream fileStream2 = null;

            if (text3.Length != 0)
            {
                fileStream2 = new FileStream(text3, FileMode.Open, FileAccess.Read);
            }
            if (flag2)
            {
                if (!flag)
                {
                    num2 = 8388608;
                }
                int  num4  = 2;
                int  num5  = 3;
                int  num6  = 0;
                int  num7  = 2;
                int  num8  = 128;
                bool flag3 = parser[9].ThereIs || thereIs;
                if (parser[2].ThereIs && !LzmaAlone.GetNumber((string)parser[2].PostStrings[0], out num7))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[4].ThereIs && !LzmaAlone.GetNumber((string)parser[4].PostStrings[0], out num8))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[5].ThereIs && !LzmaAlone.GetNumber((string)parser[5].PostStrings[0], out num5))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[6].ThereIs && !LzmaAlone.GetNumber((string)parser[6].PostStrings[0], out num6))
                {
                    LzmaAlone.IncorrectCommand();
                }
                if (parser[7].ThereIs && !LzmaAlone.GetNumber((string)parser[7].PostStrings[0], out num4))
                {
                    LzmaAlone.IncorrectCommand();
                }
                CoderPropID[] propIDs = new CoderPropID[]
                {
                    CoderPropID.DictionarySize,
                    CoderPropID.PosStateBits,
                    CoderPropID.LitContextBits,
                    CoderPropID.LitPosBits,
                    CoderPropID.Algorithm,
                    CoderPropID.NumFastBytes,
                    CoderPropID.MatchFinder,
                    CoderPropID.EndMarker
                };
                object[] properties = new object[]
                {
                    num2,
                    num4,
                    num5,
                    num6,
                    num7,
                    num8,
                    text2,
                    flag3
                };
                Encoder encoder = new Encoder();
                encoder.SetCoderProperties(propIDs, properties);
                encoder.WriteCoderProperties(fileStream);
                long num9;
                if (flag3 || thereIs)
                {
                    num9 = -1L;
                }
                else
                {
                    num9 = stream.Length;
                }
                for (int i = 0; i < 8; i++)
                {
                    fileStream.WriteByte((byte)(num9 >> 8 * i));
                }
                if (fileStream2 != null)
                {
                    CDoubleStream cDoubleStream = new CDoubleStream();
                    cDoubleStream.s1        = fileStream2;
                    cDoubleStream.s2        = stream;
                    cDoubleStream.fileIndex = 0;
                    stream = cDoubleStream;
                    long length = fileStream2.Length;
                    cDoubleStream.skipSize = 0L;
                    if (length > (long)num2)
                    {
                        cDoubleStream.skipSize = length - (long)num2;
                    }
                    fileStream2.Seek(cDoubleStream.skipSize, SeekOrigin.Begin);
                    encoder.SetTrainSize((uint)(length - cDoubleStream.skipSize));
                }
                encoder.Code(stream, fileStream, -1L, -1L, null);
            }
            else
            {
                if (!(text == "d"))
                {
                    throw new Exception("Command Error");
                }
                byte[] array2 = new byte[5];
                if (stream.Read(array2, 0, 5) != 5)
                {
                    throw new Exception("input .lzma is too short");
                }
                Decoder decoder = new Decoder();
                decoder.SetDecoderProperties(array2);
                if (fileStream2 != null && !decoder.Train(fileStream2))
                {
                    throw new Exception("can't train");
                }
                long num10 = 0L;
                for (int j = 0; j < 8; j++)
                {
                    int num11 = stream.ReadByte();
                    if (num11 < 0)
                    {
                        throw new Exception("Can't Read 1");
                    }
                    num10 |= (long)((byte)num11) << 8 * j;
                }
                long inSize = stream.Length - stream.Position;
                decoder.Code(stream, fileStream, inSize, num10, null);
            }
            return(0);
        }
Esempio n. 13
0
        /// <summary>
        /// Compresses this instance.
        /// </summary>
        internal void Process()
        {
            Console.WriteLine("[*] Processing " + this.Input.Name + "..");

            if (this.Input.Extension == ".csv" || this.Input.Extension == ".sc")
            {
                if (this.Input.Extension == ".sc")
                {
                    using (MemoryStream Stream = new MemoryStream(this.Buffer))
                    {
                        using (BinaryReader Reader = new BinaryReader(Stream))
                        {
                            char[] Prefix = Reader.ReadChars(2);

                            if (string.Join("", Prefix) == "SC")
                            {
                                Console.WriteLine("[*] Skipping compression of " + this.Output.Name + this.Output.Extension);
                                this.Output = this.Input.CopyTo(this.Output.FullName, true);
                                return;
                            }
                        }
                    }
                }

                Encoder Compresser = new Encoder();

                using (MemoryStream IStream = new MemoryStream(this.Buffer))
                {
                    using (FileStream OStream = this.Output.Create())
                    {
                        CoderPropID[] CoderPropIDs = new CoderPropID[8]
                        {
                            CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits,
                            CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker
                        };

                        object[] Properties = new object[8]
                        {
                            262144, 2, 3, 0, 2, 32, "bt4", false
                        };

                        Compresser.SetCoderProperties(CoderPropIDs, Properties);
                        Compresser.WriteCoderProperties(OStream);

                        OStream.Write(BitConverter.GetBytes(IStream.Length), 0, 4);

                        Compresser.Code(IStream, OStream, IStream.Length, -1L, null);
                    }

                    if (this.Input.Extension == ".sc")
                    {
                        int    Version = 1;
                        byte[] Header  = new byte[26];

                        using (BinaryWriter Writer = new BinaryWriter(new MemoryStream(Header)))
                        {
                            Writer.Write("SC".ToCharArray());
                            Writer.Write(BitConverter.GetBytes(Version).Reverse().ToArray());

                            using (var MD5Hash = MD5.Create())
                            {
                                byte[] MD5 = MD5Hash.ComputeHash(this.Buffer);

                                Writer.Write(BitConverter.GetBytes(MD5.Length).Reverse().ToArray());
                                Writer.Write(MD5);
                            }
                        }

                        byte[] OldData = File.ReadAllBytes(this.Output.FullName);
                        byte[] NewData = Header.Concat(OldData).ToArray();

                        this.Output.Delete();

                        using (FileStream OStream = this.Output.Create())
                        {
                            OStream.Write(NewData, 0, NewData.Length);
                        }
                    }
                }
            }
            else
            {
                try
                {
                    this.Output = this.Input.CopyTo(this.Output.FullName, true);
                }
                catch (Exception)
                {
                    Console.WriteLine("[*] Couldn't copy " + this.Output.Name + " to the patch folder, please do it manualy.");
                }
            }
        }