5 using CommandLineParser;
7 public class CDoubleStream: Stream
9 public System.IO.Stream s1;
10 public System.IO.Stream s2;
14 public override bool CanRead { get { return true; }}
15 public override bool CanWrite { get { return false; }}
16 public override bool CanSeek { get { return false; }}
17 public override long Length { get { return s1.Length + s2.Length - skipSize; } }
18 public override long Position
23 public override void Flush() { }
24 public override int Read(byte[] buffer, int offset, int count)
31 int num = s1.Read(buffer, offset, count);
40 numTotal += s2.Read(buffer, offset, count);
46 public override void Write(byte[] buffer, int offset, int count)
48 throw (new Exception("can't Write"));
50 public override long Seek(long offset, System.IO.SeekOrigin origin)
52 throw (new Exception("can't Seek"));
54 public override void SetLength(long value)
56 throw (new Exception("can't SetLength"));
79 static void PrintHelp()
81 System.Console.WriteLine("\nUsage: LZMA <e|d> [<switches>...] inputFile outputFile\n" +
86 // " -a{N}: set compression mode - [0, 1], default: 1 (max)\n" +
87 " -d{N}: set dictionary - [0, 29], default: 23 (8MB)\n" +
88 " -fb{N}: set number of fast bytes - [5, 273], default: 128\n" +
89 " -lc{N}: set number of literal context bits - [0, 8], default: 3\n" +
90 " -lp{N}: set number of literal pos bits - [0, 4], default: 0\n" +
91 " -pb{N}: set number of pos bits - [0, 4], default: 2\n" +
92 " -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n" +
93 " -eos: write End Of Stream marker\n"
94 // + " -si: read data from stdin\n"
95 // + " -so: write data to stdout\n"
99 static bool GetNumber(string s, out Int32 v)
102 for (int i = 0; i < s.Length; i++)
105 if (c < '0' || c > '9')
108 v += (Int32)(c - '0');
113 static int IncorrectCommand()
115 throw (new Exception("Command line error"));
116 // System.Console.WriteLine("\nCommand line error\n");
119 static int Main2(string[] args)
121 System.Console.WriteLine("\nLZMA# 4.49 Copyright (c) 1999-2007 Igor Pavlov 2006-07-05\n");
123 if (args.Length == 0)
129 SwitchForm[] kSwitchForms = new SwitchForm[13];
131 kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false);
132 kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false);
133 kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1);
134 kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1);
135 kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1);
136 kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1);
137 kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1);
138 kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1);
139 kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1);
140 kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false);
141 kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false);
142 kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false);
143 kSwitchForms[sw++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1);
146 Parser parser = new Parser(sw);
149 parser.ParseStrings(kSwitchForms, args);
153 return IncorrectCommand();
156 if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs)
162 System.Collections.ArrayList nonSwitchStrings = parser.NonSwitchStrings;
165 if (paramIndex >= nonSwitchStrings.Count)
166 return IncorrectCommand();
167 string command = (string)nonSwitchStrings[paramIndex++];
168 command = command.ToLower();
170 bool dictionaryIsDefined = false;
171 Int32 dictionary = 1 << 21;
172 if (parser[(int)Key.Dictionary].ThereIs)
175 if (!GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog))
177 dictionary = (Int32)1 << dicLog;
178 dictionaryIsDefined = true;
181 if (parser[(int)Key.MatchFinder].ThereIs)
182 mf = (string)parser[(int)Key.MatchFinder].PostStrings[0];
187 const Int32 kNumDefaultItereations = 10;
188 Int32 numIterations = kNumDefaultItereations;
189 if (paramIndex < nonSwitchStrings.Count)
190 if (!GetNumber((string)nonSwitchStrings[paramIndex++], out numIterations))
191 numIterations = kNumDefaultItereations;
192 return LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary);
196 if (parser[(int)Key.Train].ThereIs)
197 train = (string)parser[(int)Key.Train].PostStrings[0];
199 bool encodeMode = false;
202 else if (command == "d")
207 bool stdInMode = parser[(int)Key.StdIn].ThereIs;
208 bool stdOutMode = parser[(int)Key.StdOut].ThereIs;
210 Stream inStream = null;
213 throw (new Exception("Not implemeted"));
217 if (paramIndex >= nonSwitchStrings.Count)
219 string inputName = (string)nonSwitchStrings[paramIndex++];
220 inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read);
223 FileStream outStream = null;
226 throw (new Exception("Not implemeted"));
230 if (paramIndex >= nonSwitchStrings.Count)
232 string outputName = (string)nonSwitchStrings[paramIndex++];
233 outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write);
236 FileStream trainStream = null;
237 if (train.Length != 0)
238 trainStream = new FileStream(train, FileMode.Open, FileAccess.Read);
242 if (!dictionaryIsDefined)
243 dictionary = 1 << 23;
245 Int32 posStateBits = 2;
246 Int32 litContextBits = 3; // for normal files
247 // UInt32 litContextBits = 0; // for 32-bit data
248 Int32 litPosBits = 0;
249 // UInt32 litPosBits = 2; // for 32-bit data
251 Int32 numFastBytes = 128;
253 bool eos = parser[(int)Key.EOS].ThereIs || stdInMode;
255 if (parser[(int)Key.Mode].ThereIs)
256 if (!GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm))
259 if (parser[(int)Key.FastBytes].ThereIs)
260 if (!GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes))
262 if (parser[(int)Key.LitContext].ThereIs)
263 if (!GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits))
265 if (parser[(int)Key.LitPos].ThereIs)
266 if (!GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits))
268 if (parser[(int)Key.PosBits].ThereIs)
269 if (!GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits))
272 CoderPropID[] propIDs =
274 CoderPropID.DictionarySize,
275 CoderPropID.PosStateBits,
276 CoderPropID.LitContextBits,
277 CoderPropID.LitPosBits,
278 CoderPropID.Algorithm,
279 CoderPropID.NumFastBytes,
280 CoderPropID.MatchFinder,
281 CoderPropID.EndMarker
283 object[] properties =
286 (Int32)(posStateBits),
287 (Int32)(litContextBits),
290 (Int32)(numFastBytes),
295 Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
296 encoder.SetCoderProperties(propIDs, properties);
297 encoder.WriteCoderProperties(outStream);
299 if (eos || stdInMode)
302 fileSize = inStream.Length;
303 for (int i = 0; i < 8; i++)
304 outStream.WriteByte((Byte)(fileSize >> (8 * i)));
305 if (trainStream != null)
307 CDoubleStream doubleStream = new CDoubleStream();
308 doubleStream.s1 = trainStream;
309 doubleStream.s2 = inStream;
310 doubleStream.fileIndex = 0;
311 inStream = doubleStream;
312 long trainFileSize = trainStream.Length;
313 doubleStream.skipSize = 0;
314 if (trainFileSize > dictionary)
315 doubleStream.skipSize = trainFileSize - dictionary;
316 trainStream.Seek(doubleStream.skipSize, SeekOrigin.Begin);
317 encoder.SetTrainSize((uint)(trainFileSize - doubleStream.skipSize));
319 encoder.Code(inStream, outStream, -1, -1, null);
321 else if (command == "d")
323 byte[] properties = new byte[5];
324 if (inStream.Read(properties, 0, 5) != 5)
325 throw (new Exception("input .lzma is too short"));
326 Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
327 decoder.SetDecoderProperties(properties);
328 if (trainStream != null)
330 if (!decoder.Train(trainStream))
331 throw (new Exception("can't train"));
334 for (int i = 0; i < 8; i++)
336 int v = inStream.ReadByte();
338 throw (new Exception("Can't Read 1"));
339 outSize |= ((long)(byte)v) << (8 * i);
341 long compressedSize = inStream.Length - inStream.Position;
342 decoder.Code(inStream, outStream, compressedSize, outSize, null);
345 throw (new Exception("Command Error"));
350 static int Main(string[] args)
358 Console.WriteLine("{0} Caught exception #1.", e);