Skip to content

Latest commit

 

History

History
364 lines (335 loc) · 10.1 KB

LzmaAlone.cs

File metadata and controls

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