1baa3858d3f5d128a5c8466b700098109edcad5f2repo syncusing System;
2baa3858d3f5d128a5c8466b700098109edcad5f2repo syncusing System.IO;
3baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace SevenZip
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	using CommandLineParser;
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	public class CDoubleStream: Stream
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public System.IO.Stream s1;
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public System.IO.Stream s2;
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public int fileIndex;
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public long skipSize;
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override bool CanRead { get { return true; }}
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override bool CanWrite { get { return false; }}
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override bool CanSeek { get { return false; }}
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override long Length { get { return s1.Length + s2.Length - skipSize; } }
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override long Position
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			get { return 0;	}
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			set { }
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override void Flush() { }
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override int Read(byte[] buffer, int offset, int count)
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int numTotal = 0;
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			while (count > 0)
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (fileIndex == 0)
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					int num = s1.Read(buffer, offset, count);
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					offset += num;
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					count -= num;
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					numTotal += num;
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (num == 0)
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						fileIndex++;
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				}
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (fileIndex == 1)
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					numTotal += s2.Read(buffer, offset, count);
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					return numTotal;
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				}
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return numTotal;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override void Write(byte[] buffer, int offset, int count)
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			throw (new Exception("can't Write"));
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override long Seek(long offset, System.IO.SeekOrigin origin)
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			throw (new Exception("can't Seek"));
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public override void SetLength(long value)
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			throw (new Exception("can't SetLength"));
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	class LzmaAlone
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		enum Key
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Help1 = 0,
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Help2,
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Mode,
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Dictionary,
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			FastBytes,
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			LitContext,
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			LitPos,
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			PosBits,
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			MatchFinder,
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			EOS,
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			StdIn,
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			StdOut,
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Train
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		};
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		static void PrintHelp()
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			System.Console.WriteLine("\nUsage:  LZMA <e|d> [<switches>...] inputFile outputFile\n" +
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  e: encode file\n" +
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  d: decode file\n" +
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  b: Benchmark\n" +
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"<Switches>\n" +
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				// "  -a{N}:  set compression mode - [0, 1], default: 1 (max)\n" +
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  -d{N}:  set dictionary - [0, 29], default: 23 (8MB)\n" +
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  -fb{N}: set number of fast bytes - [5, 273], default: 128\n" +
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  -lc{N}: set number of literal context bits - [0, 8], default: 3\n" +
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  -lp{N}: set number of literal pos bits - [0, 4], default: 0\n" +
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  -pb{N}: set number of pos bits - [0, 4], default: 2\n" +
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n" +
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				"  -eos:   write End Of Stream marker\n"
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				// + "  -si:    read data from stdin\n"
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				// + "  -so:    write data to stdout\n"
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				);
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		static bool GetNumber(string s, out Int32 v)
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			v = 0;
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			for (int i = 0; i < s.Length; i++)
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				char c = s[i];
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (c < '0' || c > '9')
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					return false;
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				v *= 10;
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				v += (Int32)(c - '0');
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return true;
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		static int IncorrectCommand()
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			throw (new Exception("Command line error"));
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			// System.Console.WriteLine("\nCommand line error\n");
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			// return 1;
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		static int Main2(string[] args)
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			System.Console.WriteLine("\nLZMA# 4.61  2008-11-23\n");
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (args.Length == 0)
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				PrintHelp();
126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return 0;
127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			SwitchForm[] kSwitchForms = new SwitchForm[13];
130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int sw = 0;
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false);
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false);
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1);
134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1);
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1);
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1);
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1);
138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1);
139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1);
140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false);
141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false);
142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false);
143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			kSwitchForms[sw++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1);
144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Parser parser = new Parser(sw);
147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			try
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				parser.ParseStrings(kSwitchForms, args);
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			catch
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return IncorrectCommand();
154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs)
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				PrintHelp();
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return 0;
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			System.Collections.ArrayList nonSwitchStrings = parser.NonSwitchStrings;
163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int paramIndex = 0;
165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (paramIndex >= nonSwitchStrings.Count)
166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return IncorrectCommand();
167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			string command = (string)nonSwitchStrings[paramIndex++];
168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			command = command.ToLower();
169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			bool dictionaryIsDefined = false;
171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Int32 dictionary = 1 << 21;
172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (parser[(int)Key.Dictionary].ThereIs)
173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int32 dicLog;
175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (!GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog))
176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					IncorrectCommand();
177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				dictionary = (Int32)1 << dicLog;
178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				dictionaryIsDefined = true;
179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			string mf = "bt4";
181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (parser[(int)Key.MatchFinder].ThereIs)
182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				mf = (string)parser[(int)Key.MatchFinder].PostStrings[0];
183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			mf = mf.ToLower();
184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (command == "b")
186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				const Int32 kNumDefaultItereations = 10;
188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int32 numIterations = kNumDefaultItereations;
189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (paramIndex < nonSwitchStrings.Count)
190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (!GetNumber((string)nonSwitchStrings[paramIndex++], out numIterations))
191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						numIterations = kNumDefaultItereations;
192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary);
193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			string train = "";
196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (parser[(int)Key.Train].ThereIs)
197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				train = (string)parser[(int)Key.Train].PostStrings[0];
198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			bool encodeMode = false;
200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (command == "e")
201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				encodeMode = true;
202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			else if (command == "d")
203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				encodeMode = false;
204baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			else
205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				IncorrectCommand();
206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			bool stdInMode = parser[(int)Key.StdIn].ThereIs;
208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			bool stdOutMode = parser[(int)Key.StdOut].ThereIs;
209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Stream inStream = null;
211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (stdInMode)
212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
213baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				throw (new Exception("Not implemeted"));
214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			else
216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (paramIndex >= nonSwitchStrings.Count)
218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					IncorrectCommand();
219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				string inputName = (string)nonSwitchStrings[paramIndex++];
220baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read);
221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			FileStream outStream = null;
224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (stdOutMode)
225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				throw (new Exception("Not implemeted"));
227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			else
229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (paramIndex >= nonSwitchStrings.Count)
231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					IncorrectCommand();
232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				string outputName = (string)nonSwitchStrings[paramIndex++];
233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write);
234baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
235baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
236baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			FileStream trainStream = null;
237baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (train.Length != 0)
238baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				trainStream = new FileStream(train, FileMode.Open, FileAccess.Read);
239baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
240baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (encodeMode)
241baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (!dictionaryIsDefined)
243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					dictionary = 1 << 23;
244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int32 posStateBits = 2;
246baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int32 litContextBits = 3; // for normal files
247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				// UInt32 litContextBits = 0; // for 32-bit data
248baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int32 litPosBits = 0;
249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				// UInt32 litPosBits = 2; // for 32-bit data
250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int32 algorithm = 2;
251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int32 numFastBytes = 128;
252baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
253baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				bool eos = parser[(int)Key.EOS].ThereIs || stdInMode;
254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (parser[(int)Key.Mode].ThereIs)
256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (!GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm))
257baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						IncorrectCommand();
258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (parser[(int)Key.FastBytes].ThereIs)
260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (!GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes))
261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						IncorrectCommand();
262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (parser[(int)Key.LitContext].ThereIs)
263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (!GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits))
264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						IncorrectCommand();
265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (parser[(int)Key.LitPos].ThereIs)
266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (!GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits))
267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						IncorrectCommand();
268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (parser[(int)Key.PosBits].ThereIs)
269baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (!GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits))
270baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						IncorrectCommand();
271baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
272baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				CoderPropID[] propIDs =
273baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
274baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.DictionarySize,
275baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.PosStateBits,
276baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.LitContextBits,
277baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.LitPosBits,
278baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.Algorithm,
279baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.NumFastBytes,
280baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.MatchFinder,
281baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CoderPropID.EndMarker
282baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				};
283baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				object[] properties =
284baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
285baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					(Int32)(dictionary),
286baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					(Int32)(posStateBits),
287baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					(Int32)(litContextBits),
288baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					(Int32)(litPosBits),
289baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					(Int32)(algorithm),
290baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					(Int32)(numFastBytes),
291baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					mf,
292baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					eos
293baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				};
294baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
295baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
296baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				encoder.SetCoderProperties(propIDs, properties);
297baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				encoder.WriteCoderProperties(outStream);
298baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Int64 fileSize;
299baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (eos || stdInMode)
300baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					fileSize = -1;
301baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				else
302baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					fileSize = inStream.Length;
303baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				for (int i = 0; i < 8; i++)
304baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					outStream.WriteByte((Byte)(fileSize >> (8 * i)));
305baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (trainStream != null)
306baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
307baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					CDoubleStream doubleStream = new CDoubleStream();
308baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					doubleStream.s1 = trainStream;
309baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					doubleStream.s2 = inStream;
310baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					doubleStream.fileIndex = 0;
311baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					inStream = doubleStream;
312baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					long trainFileSize = trainStream.Length;
313baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					doubleStream.skipSize = 0;
314baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (trainFileSize > dictionary)
315baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						doubleStream.skipSize = trainFileSize - dictionary;
316baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					trainStream.Seek(doubleStream.skipSize, SeekOrigin.Begin);
317baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					encoder.SetTrainSize((uint)(trainFileSize - doubleStream.skipSize));
318baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				}
319baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				encoder.Code(inStream, outStream, -1, -1, null);
320baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
321baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			else if (command == "d")
322baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
323baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				byte[] properties = new byte[5];
324baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (inStream.Read(properties, 0, 5) != 5)
325baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					throw (new Exception("input .lzma is too short"));
326baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
327baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				decoder.SetDecoderProperties(properties);
328baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (trainStream != null)
329baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
330baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (!decoder.Train(trainStream))
331baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						throw (new Exception("can't train"));
332baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				}
333baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				long outSize = 0;
334baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				for (int i = 0; i < 8; i++)
335baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
336baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					int v = inStream.ReadByte();
337baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (v < 0)
338baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						throw (new Exception("Can't Read 1"));
339baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					outSize |= ((long)(byte)v) << (8 * i);
340baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				}
341baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				long compressedSize = inStream.Length - inStream.Position;
342baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				decoder.Code(inStream, outStream, compressedSize, outSize, null);
343baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
344baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			else
345baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				throw (new Exception("Command Error"));
346baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return 0;
347baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
348baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
349baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		[STAThread]
350baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		static int Main(string[] args)
351baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
352baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			try
353baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
354baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return Main2(args);
355baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
356baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			catch (Exception e)
357baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
358baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Console.WriteLine("{0} Caught exception #1.", e);
359baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				// throw e;
360baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return 1;
361baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
362baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
363baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
364baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
365