ruby-lzma 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/README.markdown +15 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/ext/Alloc.cpp +118 -0
- data/ext/Alloc.h +29 -0
- data/ext/BinTree.h +55 -0
- data/ext/BinTree2.h +12 -0
- data/ext/BinTree3.h +16 -0
- data/ext/BinTree3Z.h +16 -0
- data/ext/BinTree4.h +18 -0
- data/ext/BinTree4b.h +20 -0
- data/ext/BinTreeMain.h +444 -0
- data/ext/BranchX86.c +101 -0
- data/ext/BranchX86.h +19 -0
- data/ext/CRC.cpp +61 -0
- data/ext/CRC.h +36 -0
- data/ext/C_FileIO.h +45 -0
- data/ext/CommandLineParser.h +82 -0
- data/ext/Defs.h +20 -0
- data/ext/FileStreams.h +98 -0
- data/ext/HC.h +55 -0
- data/ext/HC2.h +13 -0
- data/ext/HC3.h +17 -0
- data/ext/HC4.h +19 -0
- data/ext/HC4b.h +21 -0
- data/ext/HCMain.h +350 -0
- data/ext/ICoder.h +156 -0
- data/ext/IMatchFinder.h +63 -0
- data/ext/IStream.h +62 -0
- data/ext/InBuffer.cpp +80 -0
- data/ext/InBuffer.h +76 -0
- data/ext/LZInWindow.cpp +102 -0
- data/ext/LZInWindow.h +84 -0
- data/ext/LZMA.h +82 -0
- data/ext/LZMADecoder.h +248 -0
- data/ext/LZMAEncoder.cpp +1504 -0
- data/ext/LZMAEncoder.h +416 -0
- data/ext/LZOutWindow.cpp +17 -0
- data/ext/LZOutWindow.h +66 -0
- data/ext/LzmaBench.h +11 -0
- data/ext/LzmaDecode.c +588 -0
- data/ext/LzmaDecode.h +131 -0
- data/ext/LzmaRam.cpp +228 -0
- data/ext/LzmaRam.h +46 -0
- data/ext/LzmaRamDecode.c +79 -0
- data/ext/LzmaRamDecode.h +55 -0
- data/ext/MyCom.h +203 -0
- data/ext/MyGuidDef.h +54 -0
- data/ext/MyInitGuid.h +13 -0
- data/ext/MyString.h +631 -0
- data/ext/MyUnknown.h +24 -0
- data/ext/MyWindows.h +183 -0
- data/ext/OutBuffer.cpp +117 -0
- data/ext/OutBuffer.h +64 -0
- data/ext/Pat.h +318 -0
- data/ext/Pat2.h +22 -0
- data/ext/Pat2H.h +24 -0
- data/ext/Pat2R.h +20 -0
- data/ext/Pat3H.h +24 -0
- data/ext/Pat4H.h +24 -0
- data/ext/PatMain.h +989 -0
- data/ext/RangeCoder.h +205 -0
- data/ext/RangeCoderBit.cpp +80 -0
- data/ext/RangeCoderBit.h +120 -0
- data/ext/RangeCoderBitTree.h +161 -0
- data/ext/RangeCoderOpt.h +31 -0
- data/ext/StdAfx.h +8 -0
- data/ext/StreamUtils.cpp +44 -0
- data/ext/StreamUtils.h +11 -0
- data/ext/StringConvert.h +71 -0
- data/ext/StringToInt.h +17 -0
- data/ext/Types.h +19 -0
- data/ext/Vector.h +211 -0
- data/ext/extconf.rb +7 -0
- data/ext/lzma_ruby.cpp +51 -0
- data/ext/lzmalib.h +64 -0
- data/ext/mylib.cpp +81 -0
- data/java/SevenZip/CRC.java +52 -0
- data/java/SevenZip/Compression/LZ/BinTree.java +382 -0
- data/java/SevenZip/Compression/LZ/InWindow.java +131 -0
- data/java/SevenZip/Compression/LZ/OutWindow.java +85 -0
- data/java/SevenZip/Compression/LZMA/Base.java +88 -0
- data/java/SevenZip/Compression/LZMA/Decoder.java +329 -0
- data/java/SevenZip/Compression/LZMA/Encoder.java +1415 -0
- data/java/SevenZip/Compression/RangeCoder/BitTreeDecoder.java +55 -0
- data/java/SevenZip/Compression/RangeCoder/BitTreeEncoder.java +99 -0
- data/java/SevenZip/Compression/RangeCoder/Decoder.java +88 -0
- data/java/SevenZip/Compression/RangeCoder/Encoder.java +151 -0
- data/java/SevenZip/ICodeProgress.java +6 -0
- data/java/SevenZip/LzmaAlone.java +253 -0
- data/java/SevenZip/LzmaBench.java +391 -0
- data/java/com/ephemeronindustries/lzma/LZMA.java +104 -0
- data/lib/lzma.rb +32 -0
- data/ruby-lzma.gemspec +136 -0
- data/test/test_lzma.rb +42 -0
- metadata +157 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
package SevenZip.Compression.RangeCoder;
|
2
|
+
|
3
|
+
public class BitTreeDecoder
|
4
|
+
{
|
5
|
+
short[] Models;
|
6
|
+
int NumBitLevels;
|
7
|
+
|
8
|
+
public BitTreeDecoder(int numBitLevels)
|
9
|
+
{
|
10
|
+
NumBitLevels = numBitLevels;
|
11
|
+
Models = new short[1 << numBitLevels];
|
12
|
+
}
|
13
|
+
|
14
|
+
public void Init()
|
15
|
+
{
|
16
|
+
Decoder.InitBitModels(Models);
|
17
|
+
}
|
18
|
+
|
19
|
+
public int Decode(Decoder rangeDecoder) throws java.io.IOException
|
20
|
+
{
|
21
|
+
int m = 1;
|
22
|
+
for (int bitIndex = NumBitLevels; bitIndex != 0; bitIndex--)
|
23
|
+
m = (m << 1) + rangeDecoder.DecodeBit(Models, m);
|
24
|
+
return m - (1 << NumBitLevels);
|
25
|
+
}
|
26
|
+
|
27
|
+
public int ReverseDecode(Decoder rangeDecoder) throws java.io.IOException
|
28
|
+
{
|
29
|
+
int m = 1;
|
30
|
+
int symbol = 0;
|
31
|
+
for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
|
32
|
+
{
|
33
|
+
int bit = rangeDecoder.DecodeBit(Models, m);
|
34
|
+
m <<= 1;
|
35
|
+
m += bit;
|
36
|
+
symbol |= (bit << bitIndex);
|
37
|
+
}
|
38
|
+
return symbol;
|
39
|
+
}
|
40
|
+
|
41
|
+
public static int ReverseDecode(short[] Models, int startIndex,
|
42
|
+
Decoder rangeDecoder, int NumBitLevels) throws java.io.IOException
|
43
|
+
{
|
44
|
+
int m = 1;
|
45
|
+
int symbol = 0;
|
46
|
+
for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
|
47
|
+
{
|
48
|
+
int bit = rangeDecoder.DecodeBit(Models, startIndex + m);
|
49
|
+
m <<= 1;
|
50
|
+
m += bit;
|
51
|
+
symbol |= (bit << bitIndex);
|
52
|
+
}
|
53
|
+
return symbol;
|
54
|
+
}
|
55
|
+
}
|
@@ -0,0 +1,99 @@
|
|
1
|
+
package SevenZip.Compression.RangeCoder;
|
2
|
+
import java.io.IOException;
|
3
|
+
|
4
|
+
public class BitTreeEncoder
|
5
|
+
{
|
6
|
+
short[] Models;
|
7
|
+
int NumBitLevels;
|
8
|
+
|
9
|
+
public BitTreeEncoder(int numBitLevels)
|
10
|
+
{
|
11
|
+
NumBitLevels = numBitLevels;
|
12
|
+
Models = new short[1 << numBitLevels];
|
13
|
+
}
|
14
|
+
|
15
|
+
public void Init()
|
16
|
+
{
|
17
|
+
Decoder.InitBitModels(Models);
|
18
|
+
}
|
19
|
+
|
20
|
+
public void Encode(Encoder rangeEncoder, int symbol) throws IOException
|
21
|
+
{
|
22
|
+
int m = 1;
|
23
|
+
for (int bitIndex = NumBitLevels; bitIndex != 0; )
|
24
|
+
{
|
25
|
+
bitIndex--;
|
26
|
+
int bit = (symbol >>> bitIndex) & 1;
|
27
|
+
rangeEncoder.Encode(Models, m, bit);
|
28
|
+
m = (m << 1) | bit;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
public void ReverseEncode(Encoder rangeEncoder, int symbol) throws IOException
|
33
|
+
{
|
34
|
+
int m = 1;
|
35
|
+
for (int i = 0; i < NumBitLevels; i++)
|
36
|
+
{
|
37
|
+
int bit = symbol & 1;
|
38
|
+
rangeEncoder.Encode(Models, m, bit);
|
39
|
+
m = (m << 1) | bit;
|
40
|
+
symbol >>= 1;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
public int GetPrice(int symbol)
|
45
|
+
{
|
46
|
+
int price = 0;
|
47
|
+
int m = 1;
|
48
|
+
for (int bitIndex = NumBitLevels; bitIndex != 0; )
|
49
|
+
{
|
50
|
+
bitIndex--;
|
51
|
+
int bit = (symbol >>> bitIndex) & 1;
|
52
|
+
price += Encoder.GetPrice(Models[m], bit);
|
53
|
+
m = (m << 1) + bit;
|
54
|
+
}
|
55
|
+
return price;
|
56
|
+
}
|
57
|
+
|
58
|
+
public int ReverseGetPrice(int symbol)
|
59
|
+
{
|
60
|
+
int price = 0;
|
61
|
+
int m = 1;
|
62
|
+
for (int i = NumBitLevels; i != 0; i--)
|
63
|
+
{
|
64
|
+
int bit = symbol & 1;
|
65
|
+
symbol >>>= 1;
|
66
|
+
price += Encoder.GetPrice(Models[m], bit);
|
67
|
+
m = (m << 1) | bit;
|
68
|
+
}
|
69
|
+
return price;
|
70
|
+
}
|
71
|
+
|
72
|
+
public static int ReverseGetPrice(short[] Models, int startIndex,
|
73
|
+
int NumBitLevels, int symbol)
|
74
|
+
{
|
75
|
+
int price = 0;
|
76
|
+
int m = 1;
|
77
|
+
for (int i = NumBitLevels; i != 0; i--)
|
78
|
+
{
|
79
|
+
int bit = symbol & 1;
|
80
|
+
symbol >>>= 1;
|
81
|
+
price += Encoder.GetPrice(Models[startIndex + m], bit);
|
82
|
+
m = (m << 1) | bit;
|
83
|
+
}
|
84
|
+
return price;
|
85
|
+
}
|
86
|
+
|
87
|
+
public static void ReverseEncode(short[] Models, int startIndex,
|
88
|
+
Encoder rangeEncoder, int NumBitLevels, int symbol) throws IOException
|
89
|
+
{
|
90
|
+
int m = 1;
|
91
|
+
for (int i = 0; i < NumBitLevels; i++)
|
92
|
+
{
|
93
|
+
int bit = symbol & 1;
|
94
|
+
rangeEncoder.Encode(Models, startIndex + m, bit);
|
95
|
+
m = (m << 1) | bit;
|
96
|
+
symbol >>= 1;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
package SevenZip.Compression.RangeCoder;
|
2
|
+
import java.io.IOException;
|
3
|
+
|
4
|
+
public class Decoder
|
5
|
+
{
|
6
|
+
static final int kTopMask = ~((1 << 24) - 1);
|
7
|
+
|
8
|
+
static final int kNumBitModelTotalBits = 11;
|
9
|
+
static final int kBitModelTotal = (1 << kNumBitModelTotalBits);
|
10
|
+
static final int kNumMoveBits = 5;
|
11
|
+
|
12
|
+
int Range;
|
13
|
+
int Code;
|
14
|
+
|
15
|
+
java.io.InputStream Stream;
|
16
|
+
|
17
|
+
public final void SetStream(java.io.InputStream stream)
|
18
|
+
{
|
19
|
+
Stream = stream;
|
20
|
+
}
|
21
|
+
|
22
|
+
public final void ReleaseStream()
|
23
|
+
{
|
24
|
+
Stream = null;
|
25
|
+
}
|
26
|
+
|
27
|
+
public final void Init() throws IOException
|
28
|
+
{
|
29
|
+
Code = 0;
|
30
|
+
Range = -1;
|
31
|
+
for (int i = 0; i < 5; i++)
|
32
|
+
Code = (Code << 8) | Stream.read();
|
33
|
+
}
|
34
|
+
|
35
|
+
public final int DecodeDirectBits(int numTotalBits) throws IOException
|
36
|
+
{
|
37
|
+
int result = 0;
|
38
|
+
for (int i = numTotalBits; i != 0; i--)
|
39
|
+
{
|
40
|
+
Range >>>= 1;
|
41
|
+
int t = ((Code - Range) >>> 31);
|
42
|
+
Code -= Range & (t - 1);
|
43
|
+
result = (result << 1) | (1 - t);
|
44
|
+
|
45
|
+
if ((Range & kTopMask) == 0)
|
46
|
+
{
|
47
|
+
Code = (Code << 8) | Stream.read();
|
48
|
+
Range <<= 8;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
return result;
|
52
|
+
}
|
53
|
+
|
54
|
+
public int DecodeBit(short []probs, int index) throws IOException
|
55
|
+
{
|
56
|
+
int prob = probs[index];
|
57
|
+
int newBound = (Range >>> kNumBitModelTotalBits) * prob;
|
58
|
+
if ((Code ^ 0x80000000) < (newBound ^ 0x80000000))
|
59
|
+
{
|
60
|
+
Range = newBound;
|
61
|
+
probs[index] = (short)(prob + ((kBitModelTotal - prob) >>> kNumMoveBits));
|
62
|
+
if ((Range & kTopMask) == 0)
|
63
|
+
{
|
64
|
+
Code = (Code << 8) | Stream.read();
|
65
|
+
Range <<= 8;
|
66
|
+
}
|
67
|
+
return 0;
|
68
|
+
}
|
69
|
+
else
|
70
|
+
{
|
71
|
+
Range -= newBound;
|
72
|
+
Code -= newBound;
|
73
|
+
probs[index] = (short)(prob - ((prob) >>> kNumMoveBits));
|
74
|
+
if ((Range & kTopMask) == 0)
|
75
|
+
{
|
76
|
+
Code = (Code << 8) | Stream.read();
|
77
|
+
Range <<= 8;
|
78
|
+
}
|
79
|
+
return 1;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
public static void InitBitModels(short []probs)
|
84
|
+
{
|
85
|
+
for (int i = 0; i < probs.length; i++)
|
86
|
+
probs[i] = (kBitModelTotal >>> 1);
|
87
|
+
}
|
88
|
+
}
|
@@ -0,0 +1,151 @@
|
|
1
|
+
package SevenZip.Compression.RangeCoder;
|
2
|
+
import java.io.IOException;
|
3
|
+
|
4
|
+
public class Encoder
|
5
|
+
{
|
6
|
+
static final int kTopMask = ~((1 << 24) - 1);
|
7
|
+
|
8
|
+
static final int kNumBitModelTotalBits = 11;
|
9
|
+
static final int kBitModelTotal = (1 << kNumBitModelTotalBits);
|
10
|
+
static final int kNumMoveBits = 5;
|
11
|
+
|
12
|
+
java.io.OutputStream Stream;
|
13
|
+
|
14
|
+
long Low;
|
15
|
+
int Range;
|
16
|
+
int _cacheSize;
|
17
|
+
int _cache;
|
18
|
+
|
19
|
+
long _position;
|
20
|
+
|
21
|
+
public void SetStream(java.io.OutputStream stream)
|
22
|
+
{
|
23
|
+
Stream = stream;
|
24
|
+
}
|
25
|
+
|
26
|
+
public void ReleaseStream()
|
27
|
+
{
|
28
|
+
Stream = null;
|
29
|
+
}
|
30
|
+
|
31
|
+
public void Init()
|
32
|
+
{
|
33
|
+
_position = 0;
|
34
|
+
Low = 0;
|
35
|
+
Range = -1;
|
36
|
+
_cacheSize = 1;
|
37
|
+
_cache = 0;
|
38
|
+
}
|
39
|
+
|
40
|
+
public void FlushData() throws IOException
|
41
|
+
{
|
42
|
+
for (int i = 0; i < 5; i++)
|
43
|
+
ShiftLow();
|
44
|
+
}
|
45
|
+
|
46
|
+
public void FlushStream() throws IOException
|
47
|
+
{
|
48
|
+
Stream.flush();
|
49
|
+
}
|
50
|
+
|
51
|
+
public void ShiftLow() throws IOException
|
52
|
+
{
|
53
|
+
int LowHi = (int)(Low >>> 32);
|
54
|
+
if (LowHi != 0 || Low < 0xFF000000L)
|
55
|
+
{
|
56
|
+
_position += _cacheSize;
|
57
|
+
int temp = _cache;
|
58
|
+
do
|
59
|
+
{
|
60
|
+
Stream.write(temp + LowHi);
|
61
|
+
temp = 0xFF;
|
62
|
+
}
|
63
|
+
while(--_cacheSize != 0);
|
64
|
+
_cache = (((int)Low) >>> 24);
|
65
|
+
}
|
66
|
+
_cacheSize++;
|
67
|
+
Low = (Low & 0xFFFFFF) << 8;
|
68
|
+
}
|
69
|
+
|
70
|
+
public void EncodeDirectBits(int v, int numTotalBits) throws IOException
|
71
|
+
{
|
72
|
+
for (int i = numTotalBits - 1; i >= 0; i--)
|
73
|
+
{
|
74
|
+
Range >>>= 1;
|
75
|
+
if (((v >>> i) & 1) == 1)
|
76
|
+
Low += Range;
|
77
|
+
if ((Range & Encoder.kTopMask) == 0)
|
78
|
+
{
|
79
|
+
Range <<= 8;
|
80
|
+
ShiftLow();
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
public long GetProcessedSizeAdd()
|
87
|
+
{
|
88
|
+
return _cacheSize + _position + 4;
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
static final int kNumMoveReducingBits = 2;
|
94
|
+
public static final int kNumBitPriceShiftBits = 6;
|
95
|
+
|
96
|
+
public static void InitBitModels(short []probs)
|
97
|
+
{
|
98
|
+
for (int i = 0; i < probs.length; i++)
|
99
|
+
probs[i] = (kBitModelTotal >>> 1);
|
100
|
+
}
|
101
|
+
|
102
|
+
public void Encode(short []probs, int index, int symbol) throws IOException
|
103
|
+
{
|
104
|
+
int prob = probs[index];
|
105
|
+
int newBound = (Range >>> kNumBitModelTotalBits) * prob;
|
106
|
+
if (symbol == 0)
|
107
|
+
{
|
108
|
+
Range = newBound;
|
109
|
+
probs[index] = (short)(prob + ((kBitModelTotal - prob) >>> kNumMoveBits));
|
110
|
+
}
|
111
|
+
else
|
112
|
+
{
|
113
|
+
Low += (newBound & 0xFFFFFFFFL);
|
114
|
+
Range -= newBound;
|
115
|
+
probs[index] = (short)(prob - ((prob) >>> kNumMoveBits));
|
116
|
+
}
|
117
|
+
if ((Range & kTopMask) == 0)
|
118
|
+
{
|
119
|
+
Range <<= 8;
|
120
|
+
ShiftLow();
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
private static int[] ProbPrices = new int[kBitModelTotal >>> kNumMoveReducingBits];
|
125
|
+
|
126
|
+
static
|
127
|
+
{
|
128
|
+
int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
|
129
|
+
for (int i = kNumBits - 1; i >= 0; i--)
|
130
|
+
{
|
131
|
+
int start = 1 << (kNumBits - i - 1);
|
132
|
+
int end = 1 << (kNumBits - i);
|
133
|
+
for (int j = start; j < end; j++)
|
134
|
+
ProbPrices[j] = (i << kNumBitPriceShiftBits) +
|
135
|
+
(((end - j) << kNumBitPriceShiftBits) >>> (kNumBits - i - 1));
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
static public int GetPrice(int Prob, int symbol)
|
140
|
+
{
|
141
|
+
return ProbPrices[(((Prob - symbol) ^ ((-symbol))) & (kBitModelTotal - 1)) >>> kNumMoveReducingBits];
|
142
|
+
}
|
143
|
+
static public int GetPrice0(int Prob)
|
144
|
+
{
|
145
|
+
return ProbPrices[Prob >>> kNumMoveReducingBits];
|
146
|
+
}
|
147
|
+
static public int GetPrice1(int Prob)
|
148
|
+
{
|
149
|
+
return ProbPrices[(kBitModelTotal - Prob) >>> kNumMoveReducingBits];
|
150
|
+
}
|
151
|
+
}
|
@@ -0,0 +1,253 @@
|
|
1
|
+
package SevenZip;
|
2
|
+
|
3
|
+
public class LzmaAlone
|
4
|
+
{
|
5
|
+
static public class CommandLine
|
6
|
+
{
|
7
|
+
public static final int kEncode = 0;
|
8
|
+
public static final int kDecode = 1;
|
9
|
+
public static final int kBenchmak = 2;
|
10
|
+
|
11
|
+
public int Command = -1;
|
12
|
+
public int NumBenchmarkPasses = 10;
|
13
|
+
|
14
|
+
public int DictionarySize = 1 << 23;
|
15
|
+
public boolean DictionarySizeIsDefined = false;
|
16
|
+
|
17
|
+
public int Lc = 3;
|
18
|
+
public int Lp = 0;
|
19
|
+
public int Pb = 2;
|
20
|
+
|
21
|
+
public int Fb = 128;
|
22
|
+
public boolean FbIsDefined = false;
|
23
|
+
|
24
|
+
public boolean Eos = false;
|
25
|
+
|
26
|
+
public int Algorithm = 2;
|
27
|
+
public int MatchFinder = 1;
|
28
|
+
|
29
|
+
public String InFile;
|
30
|
+
public String OutFile;
|
31
|
+
|
32
|
+
boolean ParseSwitch(String s)
|
33
|
+
{
|
34
|
+
if (s.startsWith("d"))
|
35
|
+
{
|
36
|
+
DictionarySize = 1 << Integer.parseInt(s.substring(1));
|
37
|
+
DictionarySizeIsDefined = true;
|
38
|
+
}
|
39
|
+
else if (s.startsWith("fb"))
|
40
|
+
{
|
41
|
+
Fb = Integer.parseInt(s.substring(2));
|
42
|
+
FbIsDefined = true;
|
43
|
+
}
|
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"))
|
55
|
+
{
|
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;
|
65
|
+
}
|
66
|
+
else
|
67
|
+
return false;
|
68
|
+
return true;
|
69
|
+
}
|
70
|
+
|
71
|
+
public boolean Parse(String[] args) throws Exception
|
72
|
+
{
|
73
|
+
int pos = 0;
|
74
|
+
boolean switchMode = true;
|
75
|
+
for (int i = 0; i < args.length; i++)
|
76
|
+
{
|
77
|
+
String s = args[i];
|
78
|
+
if (s.length() == 0)
|
79
|
+
return false;
|
80
|
+
if (switchMode)
|
81
|
+
{
|
82
|
+
if (s.compareTo("--") == 0)
|
83
|
+
{
|
84
|
+
switchMode = false;
|
85
|
+
continue;
|
86
|
+
}
|
87
|
+
if (s.charAt(0) == '-')
|
88
|
+
{
|
89
|
+
String sw = s.substring(1).toLowerCase();
|
90
|
+
if (sw.length() == 0)
|
91
|
+
return false;
|
92
|
+
try
|
93
|
+
{
|
94
|
+
if (!ParseSwitch(sw))
|
95
|
+
return false;
|
96
|
+
}
|
97
|
+
catch (NumberFormatException e)
|
98
|
+
{
|
99
|
+
return false;
|
100
|
+
}
|
101
|
+
continue;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
if (pos == 0)
|
105
|
+
{
|
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;
|
114
|
+
}
|
115
|
+
else if(pos == 1)
|
116
|
+
{
|
117
|
+
if (Command == kBenchmak)
|
118
|
+
{
|
119
|
+
try
|
120
|
+
{
|
121
|
+
NumBenchmarkPasses = Integer.parseInt(s);
|
122
|
+
if (NumBenchmarkPasses < 1)
|
123
|
+
return false;
|
124
|
+
}
|
125
|
+
catch (NumberFormatException e)
|
126
|
+
{
|
127
|
+
return false;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
else
|
131
|
+
InFile = s;
|
132
|
+
}
|
133
|
+
else if(pos == 2)
|
134
|
+
OutFile = s;
|
135
|
+
else
|
136
|
+
return false;
|
137
|
+
pos++;
|
138
|
+
continue;
|
139
|
+
}
|
140
|
+
return true;
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
144
|
+
|
145
|
+
static void PrintHelp()
|
146
|
+
{
|
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, 1], default: 1 (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], default: bt4\n" +
|
160
|
+
" -eos: write End Of Stream marker\n"
|
161
|
+
);
|
162
|
+
}
|
163
|
+
|
164
|
+
public static void main(String[] args) throws Exception
|
165
|
+
{
|
166
|
+
System.out.println("\nLZMA (Java) 4.61 2008-11-23\n");
|
167
|
+
|
168
|
+
if (args.length < 1)
|
169
|
+
{
|
170
|
+
PrintHelp();
|
171
|
+
return;
|
172
|
+
}
|
173
|
+
|
174
|
+
CommandLine params = new CommandLine();
|
175
|
+
if (!params.Parse(args))
|
176
|
+
{
|
177
|
+
System.out.println("\nIncorrect command");
|
178
|
+
return;
|
179
|
+
}
|
180
|
+
|
181
|
+
if (params.Command == CommandLine.kBenchmak)
|
182
|
+
{
|
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);
|
189
|
+
}
|
190
|
+
else if (params.Command == CommandLine.kEncode || params.Command == CommandLine.kDecode)
|
191
|
+
{
|
192
|
+
java.io.File inFile = new java.io.File(params.InFile);
|
193
|
+
java.io.File outFile = new java.io.File(params.OutFile);
|
194
|
+
|
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));
|
197
|
+
|
198
|
+
boolean eos = false;
|
199
|
+
if (params.Eos)
|
200
|
+
eos = true;
|
201
|
+
if (params.Command == CommandLine.kEncode)
|
202
|
+
{
|
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.SetNumFastBytes(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);
|
224
|
+
}
|
225
|
+
else
|
226
|
+
{
|
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++)
|
236
|
+
{
|
237
|
+
int v = inStream.read();
|
238
|
+
if (v < 0)
|
239
|
+
throw new Exception("Can't read stream size");
|
240
|
+
outSize |= ((long)v) << (8 * i);
|
241
|
+
}
|
242
|
+
if (!decoder.Code(inStream, outStream, outSize))
|
243
|
+
throw new Exception("Error in data stream");
|
244
|
+
}
|
245
|
+
outStream.flush();
|
246
|
+
outStream.close();
|
247
|
+
inStream.close();
|
248
|
+
}
|
249
|
+
else
|
250
|
+
throw new Exception("Incorrect command");
|
251
|
+
return;
|
252
|
+
}
|
253
|
+
}
|