1370b324cSopenharmony_cipackage SevenZip;
2370b324cSopenharmony_ci
3370b324cSopenharmony_cipublic class LzmaAlone
4370b324cSopenharmony_ci{
5370b324cSopenharmony_ci	static public class CommandLine
6370b324cSopenharmony_ci	{
7370b324cSopenharmony_ci		public static final int kEncode = 0;
8370b324cSopenharmony_ci		public static final int kDecode = 1;
9370b324cSopenharmony_ci		public static final int kBenchmak = 2;
10370b324cSopenharmony_ci
11370b324cSopenharmony_ci		public int Command = -1;
12370b324cSopenharmony_ci		public int NumBenchmarkPasses = 10;
13370b324cSopenharmony_ci
14370b324cSopenharmony_ci		public int DictionarySize = 1 << 23;
15370b324cSopenharmony_ci		public boolean DictionarySizeIsDefined = false;
16370b324cSopenharmony_ci
17370b324cSopenharmony_ci		public int Lc = 3;
18370b324cSopenharmony_ci		public int Lp = 0;
19370b324cSopenharmony_ci		public int Pb = 2;
20370b324cSopenharmony_ci
21370b324cSopenharmony_ci		public int Fb = 128;
22370b324cSopenharmony_ci		public boolean FbIsDefined = false;
23370b324cSopenharmony_ci
24370b324cSopenharmony_ci		public boolean Eos = false;
25370b324cSopenharmony_ci
26370b324cSopenharmony_ci		public int Algorithm = 2;
27370b324cSopenharmony_ci		public int MatchFinder = 1;
28370b324cSopenharmony_ci
29370b324cSopenharmony_ci		public String InFile;
30370b324cSopenharmony_ci		public String OutFile;
31370b324cSopenharmony_ci
32370b324cSopenharmony_ci		boolean ParseSwitch(String s)
33370b324cSopenharmony_ci		{
34370b324cSopenharmony_ci			if (s.startsWith("d"))
35370b324cSopenharmony_ci			{
36370b324cSopenharmony_ci				DictionarySize = 1 << Integer.parseInt(s.substring(1));
37370b324cSopenharmony_ci				DictionarySizeIsDefined = true;
38370b324cSopenharmony_ci			}
39370b324cSopenharmony_ci			else if (s.startsWith("fb"))
40370b324cSopenharmony_ci			{
41370b324cSopenharmony_ci				Fb = Integer.parseInt(s.substring(2));
42370b324cSopenharmony_ci				FbIsDefined = true;
43370b324cSopenharmony_ci			}
44370b324cSopenharmony_ci			else if (s.startsWith("a"))
45370b324cSopenharmony_ci				Algorithm = Integer.parseInt(s.substring(1));
46370b324cSopenharmony_ci			else if (s.startsWith("lc"))
47370b324cSopenharmony_ci				Lc = Integer.parseInt(s.substring(2));
48370b324cSopenharmony_ci			else if (s.startsWith("lp"))
49370b324cSopenharmony_ci				Lp = Integer.parseInt(s.substring(2));
50370b324cSopenharmony_ci			else if (s.startsWith("pb"))
51370b324cSopenharmony_ci				Pb = Integer.parseInt(s.substring(2));
52370b324cSopenharmony_ci			else if (s.startsWith("eos"))
53370b324cSopenharmony_ci				Eos = true;
54370b324cSopenharmony_ci			else if (s.startsWith("mf"))
55370b324cSopenharmony_ci			{
56370b324cSopenharmony_ci				String mfs = s.substring(2);
57370b324cSopenharmony_ci				if (mfs.equals("bt2"))
58370b324cSopenharmony_ci					MatchFinder = 0;
59370b324cSopenharmony_ci				else if (mfs.equals("bt4"))
60370b324cSopenharmony_ci					MatchFinder = 1;
61370b324cSopenharmony_ci				else if (mfs.equals("bt4b"))
62370b324cSopenharmony_ci					MatchFinder = 2;
63370b324cSopenharmony_ci				else
64370b324cSopenharmony_ci					return false;
65370b324cSopenharmony_ci			}
66370b324cSopenharmony_ci			else
67370b324cSopenharmony_ci				return false;
68370b324cSopenharmony_ci			return true;
69370b324cSopenharmony_ci		}
70370b324cSopenharmony_ci
71370b324cSopenharmony_ci		public boolean Parse(String[] args) throws Exception
72370b324cSopenharmony_ci		{
73370b324cSopenharmony_ci			int pos = 0;
74370b324cSopenharmony_ci			boolean switchMode = true;
75370b324cSopenharmony_ci			for (int i = 0; i < args.length; i++)
76370b324cSopenharmony_ci			{
77370b324cSopenharmony_ci				String s = args[i];
78370b324cSopenharmony_ci				if (s.length() == 0)
79370b324cSopenharmony_ci					return false;
80370b324cSopenharmony_ci				if (switchMode)
81370b324cSopenharmony_ci				{
82370b324cSopenharmony_ci					if (s.compareTo("--") == 0)
83370b324cSopenharmony_ci					{
84370b324cSopenharmony_ci						switchMode = false;
85370b324cSopenharmony_ci						continue;
86370b324cSopenharmony_ci					}
87370b324cSopenharmony_ci					if (s.charAt(0) == '-')
88370b324cSopenharmony_ci					{
89370b324cSopenharmony_ci						String sw = s.substring(1).toLowerCase();
90370b324cSopenharmony_ci						if (sw.length() == 0)
91370b324cSopenharmony_ci							return false;
92370b324cSopenharmony_ci						try
93370b324cSopenharmony_ci						{
94370b324cSopenharmony_ci							if (!ParseSwitch(sw))
95370b324cSopenharmony_ci								return false;
96370b324cSopenharmony_ci						}
97370b324cSopenharmony_ci						catch (NumberFormatException e)
98370b324cSopenharmony_ci						{
99370b324cSopenharmony_ci							return false;
100370b324cSopenharmony_ci						}
101370b324cSopenharmony_ci						continue;
102370b324cSopenharmony_ci					}
103370b324cSopenharmony_ci				}
104370b324cSopenharmony_ci				if (pos == 0)
105370b324cSopenharmony_ci				{
106370b324cSopenharmony_ci					if (s.equalsIgnoreCase("e"))
107370b324cSopenharmony_ci						Command = kEncode;
108370b324cSopenharmony_ci					else if (s.equalsIgnoreCase("d"))
109370b324cSopenharmony_ci						Command = kDecode;
110370b324cSopenharmony_ci					else if (s.equalsIgnoreCase("b"))
111370b324cSopenharmony_ci						Command = kBenchmak;
112370b324cSopenharmony_ci					else
113370b324cSopenharmony_ci						return false;
114370b324cSopenharmony_ci				}
115370b324cSopenharmony_ci				else if(pos == 1)
116370b324cSopenharmony_ci				{
117370b324cSopenharmony_ci					if (Command == kBenchmak)
118370b324cSopenharmony_ci					{
119370b324cSopenharmony_ci						try
120370b324cSopenharmony_ci						{
121370b324cSopenharmony_ci							NumBenchmarkPasses = Integer.parseInt(s);
122370b324cSopenharmony_ci							if (NumBenchmarkPasses < 1)
123370b324cSopenharmony_ci								return false;
124370b324cSopenharmony_ci						}
125370b324cSopenharmony_ci						catch (NumberFormatException e)
126370b324cSopenharmony_ci						{
127370b324cSopenharmony_ci							return false;
128370b324cSopenharmony_ci						}
129370b324cSopenharmony_ci					}
130370b324cSopenharmony_ci					else
131370b324cSopenharmony_ci						InFile = s;
132370b324cSopenharmony_ci				}
133370b324cSopenharmony_ci				else if(pos == 2)
134370b324cSopenharmony_ci					OutFile = s;
135370b324cSopenharmony_ci				else
136370b324cSopenharmony_ci					return false;
137370b324cSopenharmony_ci				pos++;
138370b324cSopenharmony_ci				continue;
139370b324cSopenharmony_ci			}
140370b324cSopenharmony_ci			return true;
141370b324cSopenharmony_ci		}
142370b324cSopenharmony_ci	}
143370b324cSopenharmony_ci
144370b324cSopenharmony_ci
145370b324cSopenharmony_ci	static void PrintHelp()
146370b324cSopenharmony_ci	{
147370b324cSopenharmony_ci		System.out.println(
148370b324cSopenharmony_ci				"\nUsage:  LZMA <e|d> [<switches>...] inputFile outputFile\n" +
149370b324cSopenharmony_ci				"  e: encode file\n" +
150370b324cSopenharmony_ci				"  d: decode file\n" +
151370b324cSopenharmony_ci				"  b: Benchmark\n" +
152370b324cSopenharmony_ci				"<Switches>\n" +
153370b324cSopenharmony_ci				// "  -a{N}:  set compression mode - [0, 1], default: 1 (max)\n" +
154370b324cSopenharmony_ci				"  -d{N}:  set dictionary - [0,28], default: 23 (8MB)\n" +
155370b324cSopenharmony_ci				"  -fb{N}: set number of fast bytes - [5, 273], default: 128\n" +
156370b324cSopenharmony_ci				"  -lc{N}: set number of literal context bits - [0, 8], default: 3\n" +
157370b324cSopenharmony_ci				"  -lp{N}: set number of literal pos bits - [0, 4], default: 0\n" +
158370b324cSopenharmony_ci				"  -pb{N}: set number of pos bits - [0, 4], default: 2\n" +
159370b324cSopenharmony_ci				"  -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n" +
160370b324cSopenharmony_ci				"  -eos:   write End Of Stream marker\n"
161370b324cSopenharmony_ci				);
162370b324cSopenharmony_ci	}
163370b324cSopenharmony_ci
164370b324cSopenharmony_ci	public static void main(String[] args) throws Exception
165370b324cSopenharmony_ci	{
166370b324cSopenharmony_ci		System.out.println("\nLZMA (Java) 4.61  2008-11-23\n");
167370b324cSopenharmony_ci
168370b324cSopenharmony_ci		if (args.length < 1)
169370b324cSopenharmony_ci		{
170370b324cSopenharmony_ci			PrintHelp();
171370b324cSopenharmony_ci			return;
172370b324cSopenharmony_ci		}
173370b324cSopenharmony_ci
174370b324cSopenharmony_ci		CommandLine params = new CommandLine();
175370b324cSopenharmony_ci		if (!params.Parse(args))
176370b324cSopenharmony_ci		{
177370b324cSopenharmony_ci			System.out.println("\nIncorrect command");
178370b324cSopenharmony_ci			return;
179370b324cSopenharmony_ci		}
180370b324cSopenharmony_ci
181370b324cSopenharmony_ci		if (params.Command == CommandLine.kBenchmak)
182370b324cSopenharmony_ci		{
183370b324cSopenharmony_ci			int dictionary = (1 << 21);
184370b324cSopenharmony_ci			if (params.DictionarySizeIsDefined)
185370b324cSopenharmony_ci				dictionary = params.DictionarySize;
186370b324cSopenharmony_ci			if (params.MatchFinder > 1)
187370b324cSopenharmony_ci				throw new Exception("Unsupported match finder");
188370b324cSopenharmony_ci			SevenZip.LzmaBench.LzmaBenchmark(params.NumBenchmarkPasses, dictionary);
189370b324cSopenharmony_ci		}
190370b324cSopenharmony_ci		else if (params.Command == CommandLine.kEncode || params.Command == CommandLine.kDecode)
191370b324cSopenharmony_ci		{
192370b324cSopenharmony_ci			java.io.File inFile = new java.io.File(params.InFile);
193370b324cSopenharmony_ci			java.io.File outFile = new java.io.File(params.OutFile);
194370b324cSopenharmony_ci
195370b324cSopenharmony_ci			java.io.BufferedInputStream inStream  = new java.io.BufferedInputStream(new java.io.FileInputStream(inFile));
196370b324cSopenharmony_ci			java.io.BufferedOutputStream outStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(outFile));
197370b324cSopenharmony_ci
198370b324cSopenharmony_ci			boolean eos = false;
199370b324cSopenharmony_ci			if (params.Eos)
200370b324cSopenharmony_ci				eos = true;
201370b324cSopenharmony_ci			if (params.Command == CommandLine.kEncode)
202370b324cSopenharmony_ci			{
203370b324cSopenharmony_ci				SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
204370b324cSopenharmony_ci				if (!encoder.SetAlgorithm(params.Algorithm))
205370b324cSopenharmony_ci					throw new Exception("Incorrect compression mode");
206370b324cSopenharmony_ci				if (!encoder.SetDictionarySize(params.DictionarySize))
207370b324cSopenharmony_ci					throw new Exception("Incorrect dictionary size");
208370b324cSopenharmony_ci				if (!encoder.SetNumFastBytes(params.Fb))
209370b324cSopenharmony_ci					throw new Exception("Incorrect -fb value");
210370b324cSopenharmony_ci				if (!encoder.SetMatchFinder(params.MatchFinder))
211370b324cSopenharmony_ci					throw new Exception("Incorrect -mf value");
212370b324cSopenharmony_ci				if (!encoder.SetLcLpPb(params.Lc, params.Lp, params.Pb))
213370b324cSopenharmony_ci					throw new Exception("Incorrect -lc or -lp or -pb value");
214370b324cSopenharmony_ci				encoder.SetEndMarkerMode(eos);
215370b324cSopenharmony_ci				encoder.WriteCoderProperties(outStream);
216370b324cSopenharmony_ci				long fileSize;
217370b324cSopenharmony_ci				if (eos)
218370b324cSopenharmony_ci					fileSize = -1;
219370b324cSopenharmony_ci				else
220370b324cSopenharmony_ci					fileSize = inFile.length();
221370b324cSopenharmony_ci				for (int i = 0; i < 8; i++)
222370b324cSopenharmony_ci					outStream.write((int)(fileSize >>> (8 * i)) & 0xFF);
223370b324cSopenharmony_ci				encoder.Code(inStream, outStream, -1, -1, null);
224370b324cSopenharmony_ci			}
225370b324cSopenharmony_ci			else
226370b324cSopenharmony_ci			{
227370b324cSopenharmony_ci				int propertiesSize = 5;
228370b324cSopenharmony_ci				byte[] properties = new byte[propertiesSize];
229370b324cSopenharmony_ci				if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
230370b324cSopenharmony_ci					throw new Exception("input .lzma file is too short");
231370b324cSopenharmony_ci				SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
232370b324cSopenharmony_ci				if (!decoder.SetDecoderProperties(properties))
233370b324cSopenharmony_ci					throw new Exception("Incorrect stream properties");
234370b324cSopenharmony_ci				long outSize = 0;
235370b324cSopenharmony_ci				for (int i = 0; i < 8; i++)
236370b324cSopenharmony_ci				{
237370b324cSopenharmony_ci					int v = inStream.read();
238370b324cSopenharmony_ci					if (v < 0)
239370b324cSopenharmony_ci						throw new Exception("Can't read stream size");
240370b324cSopenharmony_ci					outSize |= ((long)v) << (8 * i);
241370b324cSopenharmony_ci				}
242370b324cSopenharmony_ci				if (!decoder.Code(inStream, outStream, outSize))
243370b324cSopenharmony_ci					throw new Exception("Error in data stream");
244370b324cSopenharmony_ci			}
245370b324cSopenharmony_ci			outStream.flush();
246370b324cSopenharmony_ci			outStream.close();
247370b324cSopenharmony_ci			inStream.close();
248370b324cSopenharmony_ci		}
249370b324cSopenharmony_ci		else
250370b324cSopenharmony_ci			throw new Exception("Incorrect command");
251370b324cSopenharmony_ci		return;
252370b324cSopenharmony_ci	}
253370b324cSopenharmony_ci}
254