public static int Main2(string[] args) { // System.Console.WriteLine("\nLZMA# 4.12 beta Copyright (c) 1999-2004 Igor Pavlov 2004-12-10\n"); // // if (args.Length == 0) // { // PrintHelp(); // return 0; // } SwitchForm[] kSwitchForms = new SwitchForm[12]; int sw = 0; kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false); Parser parser = new Parser(sw); try { parser.ParseStrings(kSwitchForms, args); } catch { return(IncorrectCommand()); } // if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs) // { // PrintHelp(); // return 0; // } System.Collections.ArrayList nonSwitchStrings = parser.NonSwitchStrings; int parametroIndex = 0; if (parametroIndex >= nonSwitchStrings.Count) { return(IncorrectCommand()); } string command = (string)nonSwitchStrings[parametroIndex++]; command = command.ToLower(); bool dictionaryIsDefined = false; Int32 dictionary = 1 << 21; // if (parser[(int)Key.Dictionary].ThereIs) // { // Int32 dicLog; // if (!GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog)) // IncorrectCommand(); // dictionary = (Int32)1 << dicLog; // dictionaryIsDefined = true; // } string mf = "bt4"; // if (parser[(int)Key.MatchFinder].ThereIs) // mf = (string)parser[(int)Key.MatchFinder].PostStrings[0]; mf = mf.ToLower(); // if (command == "b") // { // const Int32 kNumDefaultItereations = 10; // Int32 numIterations = kNumDefaultItereations; // if (parametroIndex < nonSwitchStrings.Count) // if (!GetNumber((string)nonSwitchStrings[parametroIndex++], out numIterations)) // numIterations = kNumDefaultItereations; // return LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary, mf == "bt4"); // } bool encodeMode = false; if (command == "e") { encodeMode = true; } else if (command == "d") { encodeMode = false; } // else // IncorrectCommand(); // bool stdInMode = parser[(int)Key.StdIn].ThereIs; // bool stdOutMode = parser[(int)Key.StdOut].ThereIs; FileStream inStream = null; // if (stdInMode) // { // throw (new Exception("Not implemeted")); // } // else // { parametroIndex = 1; // if (parametroIndex >= nonSwitchStrings.Count) // IncorrectCommand(); string inputName = (string)nonSwitchStrings[parametroIndex++]; inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read); System.Console.WriteLine("Estou vendo inStream: " + inStream); //} FileStream outStream = null; // if (stdOutMode) // { // throw (new Exception("Not implemeted")); // } // else // { // if (parametroIndex > nonSwitchStrings.Count) // IncorrectCommand(); string outputName = (string)nonSwitchStrings[parametroIndex++]; //[parametroIndex++]; //string outputName = "TestDesc.txt"; outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write); //} if (encodeMode) { if (!dictionaryIsDefined) { dictionary = 1 << 23; } 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 = 128; // bool eos = parser[(int)Key.EOS].ThereIs || stdInMode; // // if (parser[(int)Key.Mode].ThereIs) // if (!GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm)) // IncorrectCommand(); // // if (parser[(int)Key.FastBytes].ThereIs) // if (!GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes)) // IncorrectCommand(); // if (parser[(int)Key.LitContext].ThereIs) // if (!GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits)) // IncorrectCommand(); // if (parser[(int)Key.LitPos].ThereIs) // if (!GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits)) // IncorrectCommand(); // if (parser[(int)Key.PosBits].ThereIs) // if (!GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits)) // IncorrectCommand(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, //eos }; //Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); DataStructure.Encoder encoder = new LZMA.DataStructure.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; // if (eos || stdInMode) // fileSize = -1; // else fileSize = inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null); } else if (command == "d") { byte[] properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) { throw (new Exception("input .lzma is too short")); } //Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); DataStructure.Decoder decoder = new LZMA.DataStructure.Decoder(); decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = inStream.ReadByte(); if (v < 0) { throw (new Exception("Can't Read 1")); } outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize, null); } else { throw (new Exception("Command Error")); } return(0); }
static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize, bool isBT4) { if (numIterations <= 0) { return(0); } if (dictionarySize < (1 << 19) && isBT4 || dictionarySize < (1 << 15)) { 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(); DataStructure.Encoder encoder = new LZMA.DataStructure.Encoder(); //Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); DataStructure.Decoder decoder = new LZMA.DataStructure.Decoder(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.MatchFinder }; object[] properties = { (Int32)(dictionarySize), isBT4 ? bt4: bt2 }; 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.Init(); 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, isBT4, encodeTime, benchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, isBT4, decodeTime, kBufferSize, true, (ulong)compressedSize); System.Console.WriteLine(); totalBenchSize += benchSize; totalEncodeTime += encodeTime; totalDecodeTime += decodeTime; totalCompressedSize += (ulong)compressedSize; } System.Console.WriteLine("---------------------------------------------------"); PrintResults(dictionarySize, isBT4, totalEncodeTime, totalBenchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, isBT4, totalDecodeTime, kBufferSize * (UInt64)numIterations, true, totalCompressedSize); System.Console.WriteLine(" Average"); return(0); }