| 1 |
unit ULZMAAlone; |
| 2 |
|
| 3 |
{$IFDEF FPC} |
| 4 |
{$MODE Delphi} |
| 5 |
{$ENDIF} |
| 6 |
|
| 7 |
interface |
| 8 |
|
| 9 |
uses ULZMABench,ULZMAEncoder,ULZMADecoder,UBufferedFS,ULZMACommon,Classes; |
| 10 |
|
| 11 |
const kEncode=0; |
| 12 |
kDecode=1; |
| 13 |
kBenchmark=2; |
| 14 |
|
| 15 |
type TCommandLine=class |
| 16 |
public |
| 17 |
command:integer; |
| 18 |
NumBenchMarkPasses:integer; |
| 19 |
DictionarySize:integer; |
| 20 |
DictionarySizeIsDefined:boolean; |
| 21 |
Lc:integer; |
| 22 |
Lp:integer; |
| 23 |
Pb:integer; |
| 24 |
|
| 25 |
Fb:integer; |
| 26 |
FbIsDefined:boolean; |
| 27 |
|
| 28 |
Eos:boolean; |
| 29 |
|
| 30 |
Algorithm:integer; |
| 31 |
MatchFinder:integer; |
| 32 |
|
| 33 |
InFile:string; |
| 34 |
OutFile:string; |
| 35 |
|
| 36 |
constructor Create; |
| 37 |
function ParseSwitch(const s:string):boolean; |
| 38 |
function Parse:boolean; |
| 39 |
end; |
| 40 |
|
| 41 |
TLZMAAlone=class |
| 42 |
public |
| 43 |
procedure PrintHelp; |
| 44 |
procedure Main; |
| 45 |
end; |
| 46 |
|
| 47 |
implementation |
| 48 |
|
| 49 |
uses SysUtils; |
| 50 |
|
| 51 |
constructor TCommandLine.Create; |
| 52 |
begin |
| 53 |
Command:=-1; |
| 54 |
NumBenchMarkPasses:=10; |
| 55 |
DictionarySize:=1 shl 23; |
| 56 |
DictionarySizeIsDefined:= false; |
| 57 |
Lc:= 3; |
| 58 |
Lp:= 0; |
| 59 |
Pb:= 2; |
| 60 |
|
| 61 |
Fb:= 128; |
| 62 |
FbIsDefined:= false; |
| 63 |
|
| 64 |
Eos:= false; |
| 65 |
|
| 66 |
Algorithm:= 2; |
| 67 |
MatchFinder:= 1; |
| 68 |
end; |
| 69 |
|
| 70 |
function GetStr(const str:string;const offset:integer):string; |
| 71 |
var i:integer; |
| 72 |
begin |
| 73 |
result:=''; |
| 74 |
for i:=offset to length(str) do |
| 75 |
result:=result+str[i]; |
| 76 |
end; |
| 77 |
|
| 78 |
function GetInt(const str:string;const offset:integer):integer; |
| 79 |
var s:string; |
| 80 |
begin |
| 81 |
s:=GetStr(s,offset); |
| 82 |
result:=strtoint(s); |
| 83 |
end; |
| 84 |
|
| 85 |
function TCommandLine.ParseSwitch(const s:string):boolean; |
| 86 |
var l:integer; |
| 87 |
mfs:string; |
| 88 |
begin |
| 89 |
result:=false; |
| 90 |
l:=length(s); |
| 91 |
if l=0 then exit; |
| 92 |
case s[1] of |
| 93 |
'd': begin |
| 94 |
DictionarySize := 1 shl GetInt(s,2); |
| 95 |
DictionarySizeIsDefined := true; |
| 96 |
result:=true; |
| 97 |
end; |
| 98 |
'f': begin |
| 99 |
if (l>=2)and(s[2]='b') then begin |
| 100 |
fb:=GetInt(s,3); |
| 101 |
FbIsDefined := true; |
| 102 |
result:=true; |
| 103 |
end; |
| 104 |
end; |
| 105 |
'a': begin |
| 106 |
Algorithm := GetInt(s,2); |
| 107 |
result:=true; |
| 108 |
end; |
| 109 |
'l': begin |
| 110 |
if (l>=2) then begin |
| 111 |
if s[2]='c' then begin |
| 112 |
Lc:=GetInt(s,3); |
| 113 |
result:=true; |
| 114 |
end; |
| 115 |
if s[2]='p' then begin |
| 116 |
Lp:=GetInt(s,3); |
| 117 |
result:=true; |
| 118 |
end; |
| 119 |
end; |
| 120 |
end; |
| 121 |
'p': begin |
| 122 |
if (l>=2)and(s[2]='b') then begin |
| 123 |
Pb:=GetInt(s,3); |
| 124 |
result:=true; |
| 125 |
end; |
| 126 |
end; |
| 127 |
'e': begin |
| 128 |
if (l>=3)and(s[2]='o')and(s[3]='s') then begin |
| 129 |
eos:=true; |
| 130 |
result:=true; |
| 131 |
end; |
| 132 |
end; |
| 133 |
'm': begin |
| 134 |
if (l>=2)and(s[2]='f') then begin |
| 135 |
mfs:=GetStr(s,3); |
| 136 |
if mfs='bt2' then |
| 137 |
MatchFinder:=0 |
| 138 |
else if mfs='bt4' then |
| 139 |
MatchFinder:=1 |
| 140 |
else if mfs='bt4b' then |
| 141 |
MatchFinder:=2 |
| 142 |
else begin |
| 143 |
result:=false; |
| 144 |
exit; |
| 145 |
end; |
| 146 |
end; |
| 147 |
end; |
| 148 |
else |
| 149 |
result:=false; |
| 150 |
end; |
| 151 |
end; |
| 152 |
|
| 153 |
function TCommandLine.Parse:boolean; |
| 154 |
var pos:integer; |
| 155 |
switchMode:boolean; |
| 156 |
i,l:integer; |
| 157 |
s,sw:string; |
| 158 |
begin |
| 159 |
pos := 1; |
| 160 |
switchMode := true; |
| 161 |
l:=ParamCount; |
| 162 |
for i := 1 to l do begin |
| 163 |
s := ParamStr(i); |
| 164 |
if length(s) = 0 then begin |
| 165 |
result:=false; |
| 166 |
exit; |
| 167 |
end; |
| 168 |
if switchMode then begin |
| 169 |
if comparestr(s,'--')= 0 then begin |
| 170 |
switchMode := false; |
| 171 |
continue; |
| 172 |
end; |
| 173 |
if s[1]='-' then begin |
| 174 |
sw := AnsiLowerCase(GetStr(s,2)); |
| 175 |
if length(sw) = 0 then begin |
| 176 |
result:=false; |
| 177 |
exit; |
| 178 |
end; |
| 179 |
try |
| 180 |
if not ParseSwitch(sw) then begin |
| 181 |
result:=false; |
| 182 |
exit; |
| 183 |
end; |
| 184 |
except on e:EConvertError do begin |
| 185 |
result:=false; |
| 186 |
exit; |
| 187 |
end; |
| 188 |
end; |
| 189 |
continue; |
| 190 |
end; |
| 191 |
end; |
| 192 |
if pos = 1 then begin |
| 193 |
if comparetext(s,'e')=0 then |
| 194 |
Command := kEncode |
| 195 |
else if comparetext(s,'d')=0 then |
| 196 |
Command := kDecode |
| 197 |
else if comparetext(s,'b')=0 then |
| 198 |
Command := kBenchmark |
| 199 |
else begin |
| 200 |
result:=false; |
| 201 |
exit; |
| 202 |
end; |
| 203 |
end |
| 204 |
else if pos = 2 then begin |
| 205 |
if Command = kBenchmark then begin |
| 206 |
try |
| 207 |
NumBenchmarkPasses := strtoint(s); |
| 208 |
if NumBenchmarkPasses < 1 then begin |
| 209 |
result:=false; |
| 210 |
exit; |
| 211 |
end; |
| 212 |
except on e:EConvertError do begin |
| 213 |
result:=false; |
| 214 |
exit; |
| 215 |
end; |
| 216 |
end; |
| 217 |
end |
| 218 |
else InFile := s; |
| 219 |
end |
| 220 |
else if pos = 3 then |
| 221 |
OutFile := s |
| 222 |
else begin |
| 223 |
result:=false; |
| 224 |
exit; |
| 225 |
end; |
| 226 |
inc(pos); |
| 227 |
continue; |
| 228 |
end; |
| 229 |
result:=true; |
| 230 |
exit; |
| 231 |
end; |
| 232 |
|
| 233 |
procedure TLZMAAlone.PrintHelp; |
| 234 |
begin |
| 235 |
writeln( |
| 236 |
#10'Usage: LZMA <e|d> [<switches>...] inputFile outputFile'#10 + |
| 237 |
' e: encode file'#10 + |
| 238 |
' d: decode file'#10 + |
| 239 |
' b: Benchmark'#10 + |
| 240 |
'<Switches>'#10 + |
| 241 |
// ' -a{N}: set compression mode - [0, 1], default: 1 (max)\n' + |
| 242 |
' -d{N}: set dictionary - [0,28], default: 23 (8MB)'#10 + |
| 243 |
' -fb{N}: set number of fast bytes - [5, 273], default: 128'#10 + |
| 244 |
' -lc{N}: set number of literal context bits - [0, 8], default: 3'#10 + |
| 245 |
' -lp{N}: set number of literal pos bits - [0, 4], default: 0'#10 + |
| 246 |
' -pb{N}: set number of pos bits - [0, 4], default: 2'#10 + |
| 247 |
' -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4'#10 + |
| 248 |
' -eos: write End Of Stream marker'#10 |
| 249 |
); |
| 250 |
end; |
| 251 |
|
| 252 |
procedure TLZMAAlone.Main; |
| 253 |
var params:TCommandLine; |
| 254 |
dictionary:integer; |
| 255 |
lzmaBench:tlzmabench; |
| 256 |
inStream:TBufferedFS; |
| 257 |
outStream:TBufferedFS; |
| 258 |
eos:boolean; |
| 259 |
encoder:TLZMAEncoder; |
| 260 |
filesize:int64; |
| 261 |
i:integer; |
| 262 |
properties:array[0..4] of byte; |
| 263 |
decoder:TLZMADecoder; |
| 264 |
outSize:int64; |
| 265 |
v:byte; |
| 266 |
const propertiessize=5; |
| 267 |
begin |
| 268 |
writeln(#10'LZMA (Pascal) 4.42 Copyright (c) 1999-2006 Igor Pavlov 2006-05-15'#10); |
| 269 |
if paramcount<1 then begin |
| 270 |
PrintHelp; |
| 271 |
exit; |
| 272 |
end; |
| 273 |
params:=TCommandLine.Create; |
| 274 |
if not params.Parse then begin |
| 275 |
writeln(#10'Incorrect command'); |
| 276 |
exit; |
| 277 |
end; |
| 278 |
if params.command=kBenchmark then begin |
| 279 |
dictionary:=1 shl 21; |
| 280 |
if params.DictionarySizeIsDefined then |
| 281 |
dictionary:=params.DictionarySize; |
| 282 |
if params.MatchFinder>1 then |
| 283 |
raise Exception.Create('Unsupported match finder'); |
| 284 |
lzmaBench:=TLZMABench.Create; |
| 285 |
lzmaBench.LzmaBenchmark(params.NumBenchMarkPasses,dictionary); |
| 286 |
lzmaBench.Free; |
| 287 |
end |
| 288 |
else if (params.command=kEncode)or(params.command=kDecode) then begin |
| 289 |
inStream:=TBufferedFS.Create(params.InFile,fmOpenRead or fmsharedenynone); |
| 290 |
outStream:=TBufferedFS.Create(params.OutFile,fmcreate); |
| 291 |
|
| 292 |
eos := false; |
| 293 |
if params.Eos then |
| 294 |
eos := true; |
| 295 |
if params.Command = kEncode then begin |
| 296 |
encoder:=TLZMAEncoder.Create; |
| 297 |
if not encoder.SetAlgorithm(params.Algorithm) then |
| 298 |
raise Exception.Create('Incorrect compression mode'); |
| 299 |
if not encoder.SetDictionarySize(params.DictionarySize) then |
| 300 |
raise Exception.Create('Incorrect dictionary size'); |
| 301 |
if not encoder.SeNumFastBytes(params.Fb) then |
| 302 |
raise Exception.Create('Incorrect -fb value'); |
| 303 |
if not encoder.SetMatchFinder(params.MatchFinder) then |
| 304 |
raise Exception.Create('Incorrect -mf value'); |
| 305 |
if not encoder.SetLcLpPb(params.Lc, params.Lp, params.Pb) then |
| 306 |
raise Exception.Create('Incorrect -lc or -lp or -pb value'); |
| 307 |
encoder.SetEndMarkerMode(eos); |
| 308 |
encoder.WriteCoderProperties(outStream); |
| 309 |
if eos then fileSize := -1 |
| 310 |
else fileSize := inStream.Size; |
| 311 |
for i := 0 to 7 do |
| 312 |
WriteByte(outStream,(fileSize shr (8 * i)) and $FF); |
| 313 |
encoder.Code(inStream, outStream, -1, -1); |
| 314 |
encoder.free; |
| 315 |
end |
| 316 |
else begin |
| 317 |
if inStream.read(properties, propertiesSize) <> propertiesSize then |
| 318 |
raise Exception.Create('input .lzma file is too short'); |
| 319 |
decoder := TLZMADecoder.Create; |
| 320 |
if not decoder.SetDecoderProperties(properties) then |
| 321 |
raise Exception.Create('Incorrect stream properties'); |
| 322 |
outSize := 0; |
| 323 |
for i := 0 to 7 do begin |
| 324 |
v := {shortint}(ReadByte(inStream)); |
| 325 |
if v < 0 then |
| 326 |
raise Exception.Create('Can''t read stream size'); |
| 327 |
outSize := outSize or v shl (8 * i); |
| 328 |
end; |
| 329 |
if not decoder.Code(inStream, outStream, outSize) then |
| 330 |
raise Exception.Create('Error in data stream'); |
| 331 |
decoder.Free; |
| 332 |
end; |
| 333 |
outStream.Free; |
| 334 |
inStream.Free; |
| 335 |
end |
| 336 |
else raise Exception.Create('Incorrect command'); |
| 337 |
params.Free; |
| 338 |
end; |
| 339 |
|
| 340 |
end. |