BCM WL 6.30.102.9 (r366174)
[tomato.git] / release / src-rt / linux / linux-2.6 / scripts / squashfs / lzma / Java / SevenZip / LzmaAlone.java
blob5f4067fabb58f035fc20a36fb9d5d29e8fb3e22c
1 package SevenZip;
3 public class LzmaAlone
5 static public class CommandLine
7 public static final int kEncode = 0;
8 public static final int kDecode = 1;
9 public static final int kBenchmak = 2;
11 public int Command = -1;
12 public int NumBenchmarkPasses = 10;
14 public int DictionarySize = 1 << 23;
15 public boolean DictionarySizeIsDefined = false;
17 public int Lc = 3;
18 public int Lp = 0;
19 public int Pb = 2;
21 public int Fb = 128;
22 public boolean FbIsDefined = false;
24 public boolean Eos = false;
26 public int Algorithm = 2;
27 public int MatchFinder = 1;
29 public String InFile;
30 public String OutFile;
32 boolean ParseSwitch(String s)
34 if (s.startsWith("d"))
36 DictionarySize = 1 << Integer.parseInt(s.substring(1));
37 DictionarySizeIsDefined = true;
39 else if (s.startsWith("fb"))
41 Fb = Integer.parseInt(s.substring(2));
42 FbIsDefined = true;
44 else if (s.startsWith("a"))
45 Algorithm = Integer.parseInt(s.substring(1));
46 else if (s.startsWith("lc"))
47 Lc = Integer.parseInt(s.substring(2));
48 else if (s.startsWith("lp"))
49 Lp = Integer.parseInt(s.substring(2));
50 else if (s.startsWith("pb"))
51 Pb = Integer.parseInt(s.substring(2));
52 else if (s.startsWith("eos"))
53 Eos = true;
54 else if (s.startsWith("mf"))
56 String mfs = s.substring(2);
57 if (mfs.equals("bt2"))
58 MatchFinder = 0;
59 else if (mfs.equals("bt4"))
60 MatchFinder = 1;
61 else if (mfs.equals("bt4b"))
62 MatchFinder = 2;
63 else
64 return false;
66 else
67 return false;
68 return true;
71 public boolean Parse(String[] args) throws Exception
73 int pos = 0;
74 boolean switchMode = true;
75 for (int i = 0; i < args.length; i++)
77 String s = args[i];
78 if (s.length() == 0)
79 return false;
80 if (switchMode)
82 if (s.compareTo("--") == 0)
84 switchMode = false;
85 continue;
87 if (s.charAt(0) == '-')
89 String sw = s.substring(1).toLowerCase();
90 if (sw.length() == 0)
91 return false;
92 try
94 if (!ParseSwitch(sw))
95 return false;
97 catch (NumberFormatException e)
99 return false;
101 continue;
104 if (pos == 0)
106 if (s.equalsIgnoreCase("e"))
107 Command = kEncode;
108 else if (s.equalsIgnoreCase("d"))
109 Command = kDecode;
110 else if (s.equalsIgnoreCase("b"))
111 Command = kBenchmak;
112 else
113 return false;
115 else if(pos == 1)
117 if (Command == kBenchmak)
121 NumBenchmarkPasses = Integer.parseInt(s);
122 if (NumBenchmarkPasses < 1)
123 return false;
125 catch (NumberFormatException e)
127 return false;
130 else
131 InFile = s;
133 else if(pos == 2)
134 OutFile = s;
135 else
136 return false;
137 pos++;
138 continue;
140 return true;
145 static void PrintHelp()
147 System.out.println(
148 "\nUsage: LZMA <e|d> [<switches>...] inputFile outputFile\n" +
149 " e: encode file\n" +
150 " d: decode file\n" +
151 " b: Benchmark\n" +
152 "<Switches>\n" +
153 " -a{N}: set compression mode - [0, 2], default: 2 (max)\n" +
154 " -d{N}: set dictionary - [0,28], default: 23 (8MB)\n" +
155 " -fb{N}: set number of fast bytes - [5, 273], default: 128\n" +
156 " -lc{N}: set number of literal context bits - [0, 8], default: 3\n" +
157 " -lp{N}: set number of literal pos bits - [0, 4], default: 0\n" +
158 " -pb{N}: set number of pos bits - [0, 4], default: 2\n" +
159 " -mf{MF_ID}: set Match Finder: [bt2, bt4, bt4b], default: bt4\n" +
160 " -eos: write End Of Stream marker\n"
164 public static void main(String[] args) throws Exception
166 System.out.println("\nLZMA (Java) 4.32 Copyright (c) 1999-2005 Igor Pavlov 2005-12-09\n");
168 if (args.length < 1)
170 PrintHelp();
171 return;
174 CommandLine params = new CommandLine();
175 if (!params.Parse(args))
177 System.out.println("\nIncorrect command");
178 return;
181 if (params.Command == CommandLine.kBenchmak)
183 int dictionary = (1 << 21);
184 if (params.DictionarySizeIsDefined)
185 dictionary = params.DictionarySize;
186 if (params.MatchFinder > 1)
187 throw new Exception("Unsupported match finder");
188 SevenZip.LzmaBench.LzmaBenchmark(params.NumBenchmarkPasses, dictionary, params.MatchFinder >= 1);
190 else if (params.Command == CommandLine.kEncode || params.Command == CommandLine.kDecode)
192 java.io.File inFile = new java.io.File(params.InFile);
193 java.io.File outFile = new java.io.File(params.OutFile);
195 java.io.BufferedInputStream inStream = new java.io.BufferedInputStream(new java.io.FileInputStream(inFile));
196 java.io.BufferedOutputStream outStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(outFile));
198 boolean eos = false;
199 if (params.Eos)
200 eos = true;
201 if (params.Command == CommandLine.kEncode)
203 SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
204 if (!encoder.SetAlgorithm(params.Algorithm))
205 throw new Exception("Incorrect compression mode");
206 if (!encoder.SetDictionarySize(params.DictionarySize))
207 throw new Exception("Incorrect dictionary size");
208 if (!encoder.SeNumFastBytes(params.Fb))
209 throw new Exception("Incorrect -fb value");
210 if (!encoder.SetMatchFinder(params.MatchFinder))
211 throw new Exception("Incorrect -mf value");
212 if (!encoder.SetLcLpPb(params.Lc, params.Lp, params.Pb))
213 throw new Exception("Incorrect -lc or -lp or -pb value");
214 encoder.SetEndMarkerMode(eos);
215 encoder.WriteCoderProperties(outStream);
216 long fileSize;
217 if (eos)
218 fileSize = -1;
219 else
220 fileSize = inFile.length();
221 for (int i = 0; i < 8; i++)
222 outStream.write((int)(fileSize >>> (8 * i)) & 0xFF);
223 encoder.Code(inStream, outStream, -1, -1, null);
225 else
227 int propertiesSize = 5;
228 byte[] properties = new byte[propertiesSize];
229 if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
230 throw new Exception("input .lzma file is too short");
231 SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
232 if (!decoder.SetDecoderProperties(properties))
233 throw new Exception("Incorrect stream properties");
234 long outSize = 0;
235 for (int i = 0; i < 8; i++)
237 int v = inStream.read();
238 if (v < 0)
239 throw new Exception("Can't read stream size");
240 outSize |= ((long)v) << (8 * i);
242 if (!decoder.Code(inStream, outStream, outSize))
243 throw new Exception("Error in data stream");
245 outStream.flush();
246 outStream.close();
247 inStream.close();
249 else
250 throw new Exception("Incorrect command");
251 return;