forked from synopse/mORMot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynBz.pas
649 lines (570 loc) · 19.2 KB
/
SynBz.pas
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/// fast BZ2 compression/decompression
// - licensed under a MPL/GPL/LGPL tri-license; version 1.18
unit SynBz;
{
This file is part of Synopse BZ2 Compression.
Synopse Synopse BZ2 Compression. Copyright (C) 2017 Arnaud Bouchez
Synopse Informatique - https://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
The Original Code is Synopse BZ2 Compression.
The Initial Developer of the Original Code is Arnaud Bouchez.
Portions created by the Initial Developer are Copyright (C) 2017
the Initial Developer. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****
Cross-platform BZLib implementation
===================================
decompression:
- can use inline assembler version (bunzipasm.inc)
- more than 2 times faster than standard C version
- no external .obj necessary
- 100% API compatible
- does CRC Bz2Check
- fully tested under Delphi, should work with (Cross)Kylix
asm translation extracted from isbunzip.dll and optimized for Delphi 7
compression:
- use fast bzip.dll (faster than standard C version)
Cross-Platform:
- under Win32, use fast bzip.dll (provided in bzasm.zip) + assembler
- under Linux, use default libbz2.so
(Linux can use faster assembler version - must be checked if this
is really useful)
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Initial Developer of the Original Code is Arnaud Bouchez.
This work is Copyright (C) 2008 Arnaud Bouchez - http://bouchez.info
All Rights Reserved.
Version 1.18
- fixed ticket [2783e5f81b] about UNICODE compilation issue
}
interface
{$define USEBZAASM}
// use ASM version of bunzip.dll (ok as far as tested)
uses
SysUtils, Types, Classes,
{$ifdef WIN32}
Windows;
{$else}
LibC;
{$endif}
type
TBZAlloc = function(AppData: Pointer; Items, Size: Cardinal): Pointer; stdcall;
TBZFree = procedure(AppData, Block: Pointer); stdcall;
TBZStreamRec = object
next_in: PAnsiChar;
avail_in: Integer;
total_in: Int64;
next_out: PAnsiChar;
avail_out: Integer;
total_out: Int64;
State: Pointer;
zalloc: TBZAlloc;
zfree: TBZFree;
AppData: Pointer;
procedure Init;
end;
TBZBuffer = array[word] of byte;
TBZCompressor = class(TStream)
private
FInitialized: Boolean;
FStrm: TBZStreamRec;
FDestStream: TStream;
SrcLen, DestLen: Cardinal;
FBufferIn: TBZBuffer;
BufferOut: ^TBZBuffer; // via Getmem() for AES 16-bytes alignment
procedure FlushBufferOut;
protected
// override for crc or encryption, e.g.
procedure WriteDest(const Buffer; Count: Longint); virtual;
public
constructor Create(outStream: TStream; CompressionLevel: Integer=6);
destructor Destroy; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
procedure Finish;
property FullSize: cardinal read SrcLen;
property CompressedSize: cardinal read DestLen;
end;
TBZDecompressor = class(TStream)
private
FReachedEnd,
FInitialized: Boolean;
FStrm: TBZStreamRec;
FSrcStream: TStream;
DestLen: Cardinal;
FBufferIn: array[word] of byte; // one 64kb buffers
public
constructor Create(inStream: TStream);
destructor Destroy; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
property FullSize: cardinal read DestLen;
end;
/// result=nil if error decompressing
function UnCompressBzDll(Source: PAnsiChar;
SourceSize, DestSize: integer): TMemoryStream; overload;
/// Dest must already be allocated with enough place (DestSize); return true if ok
function UnCompressBzDll(Source: PAnsiChar; SourceSize: integer;
Dest: PAnsiChar; Destsize: integer): boolean; overload;
// raw BZ2 functions:
{$ifdef USEBZAASM}
function BZ2_bzDecompressInit(var strm: TBZStreamRec;
verbosity, small: Integer): Integer; cdecl;
function BZ2_bzDecompress(var strm: TBZStreamRec): Integer; cdecl;
function BZ2_bzDecompressEnd(var strm: TBZStreamRec): Integer; cdecl;
{$endif}
{$ifdef Win32}
function BZCompressorInit: boolean;
{$ifdef USEBZAASM}
const BZDeCompressorInit = true;
{$else}
function BZDeCompressorInit: boolean;
{$endif}
{$endif}
var
{$ifdef Win32}
BZ2_bzCompressInit: function(var strm: TBZStreamRec;
blockSize100k, verbosity, workFactor: Integer): Integer; stdcall = nil;
BZ2_bzCompress: function(var strm: TBZStreamRec;
action: Integer): Integer; stdcall = nil;
BZ2_bzCompressEnd: function(var strm: TBZStreamRec): Integer; stdcall = nil;
{$ifndef USEBZAASM}
BZ2_bzDecompressInit: function(var strm: TBZStreamRec;
verbosity, small: Integer): Integer; stdcall = nil;
BZ2_bzDecompress: function(var strm: TBZStreamRec): Integer; stdcall = nil;
BZ2_bzDecompressEnd: function(var strm: TBZStreamRec): Integer; stdcall = nil;
{$endif}
{$else}
const
bzlib='libbz2.so';
function BZ2_bzCompressInit(var strm: TBZStreamRec;
blockSize100k, verbosity, workFactor: Integer): Integer; cdecl;
external bzlib name 'BZ2_bzCompressInit';
function BZ2_bzCompress(var strm: TBZStreamRec;
action: Integer): Integer; cdecl;
external bzlib name 'BZ2_bzCompress';
function BZ2_bzCompressEnd(var strm: TBZStreamRec): Integer; cdecl;
external bzlib name 'BZ2_bzCompressEnd';
{$ifndef USEBZAASM}
function BZ2_bzDecompressInit(var strm: TBZStreamRec;
verbosity, small: Integer): Integer; cdecl;
external bzlib name 'BZ2_bzDecompressInit';
function BZ2_bzDecompress(var strm: TBZStreamRec): Integer; cdecl;
external bzlib name 'BZ2_bzDecompress';
function BZ2_bzDecompressEnd(var strm: TBZStreamRec): Integer; cdecl;
external bzlib name 'BZ2_bzDecompressEnd';
{$endif}
{$endif}
const
BZ_RUN = 0;
BZ_FLUSH = 1;
BZ_FINISH = 2;
BZ_OK = 0;
BZ_RUN_OK = 1;
BZ_FLUSH_OK = 2;
BZ_FINISH_OK = 3;
BZ_STREAM_END = 4;
BZ_SEQUENCE_ERROR = (-1);
BZ_PARAM_ERROR = (-2);
BZ_MEM_ERROR = (-3);
BZ_DATA_ERROR = (-4);
BZ_DATA_ERROR_MAGIC = (-5);
BZ_IO_ERROR = (-6);
BZ_UNEXPECTED_EOF = (-7);
BZ_OUTBUFF_FULL = (-8);
BZ_CONFIG_ERROR = (-9);
function Bz2Check(const Code: Integer; const ValidCodes: array of Integer): integer;
implementation
const
SBzlibDataError = 'bzlib: Compressed data is corrupted';
SBzlibInternalErrorN = 'bzlib: Internal error. Code %d';
{$ifdef USEBZAASM}
{$I bunzipasm.inc} // all asm code extracted from bunzip.dll :)
{$endif}
{$ifdef Win32}
var BZCompressModule: HMODULE = 0;
BZDecompressModule: HMODULE = 0;
function BZInitCompressFunctions(Module: HMODULE): Boolean;
begin
BZCompressModule := Module;
if Module=0 then
Result := false else begin
BZ2_bzCompressInit := GetProcAddress(Module, 'BZ2_bzCompressInit');
BZ2_bzCompress := GetProcAddress(Module, 'BZ2_bzCompress');
BZ2_bzCompressEnd := GetProcAddress(Module, 'BZ2_bzCompressEnd');
Result := Assigned(BZ2_bzCompressInit) and Assigned(BZ2_bzCompress) and
Assigned(BZ2_bzCompressEnd);
end;
if not Result then begin
BZ2_bzCompressInit := nil;
BZ2_bzCompress := nil;
BZ2_bzCompressEnd := nil;
end;
end;
{$ifndef USEBZAASM}
function BZInitDecompressFunctions: Boolean;
var F: string;
begin
result := false;
F := ExtractFilePath(paramstr(0))+'bunzip.dll';
if FileExists(F) then
BZDeCompressModule:= LoadLibrary(PChar(F)) else
exit;
if BZDeCompressModule=0 then
exit;
BZ2_bzDecompressInit := GetProcAddress(BZDeCompressModule, 'BZ2_bzDecompressInit');
BZ2_bzDecompress := GetProcAddress(BZDeCompressModule, 'BZ2_bzDecompress');
BZ2_bzDecompressEnd := GetProcAddress(BZDeCompressModule, 'BZ2_bzDecompressEnd');
result := Assigned(BZ2_bzDecompressInit) and Assigned(BZ2_bzDecompress) and
Assigned(BZ2_bzDecompressEnd);
if not result then begin
BZ2_bzDecompressInit := nil;
BZ2_bzDecompress := nil;
BZ2_bzDecompressEnd := nil;
end;
end;
{$endif}
{$endif}
function BZAllocMem(AppData: Pointer; Items, Size: Cardinal): Pointer; stdcall;
begin
GetMem(Result, Items * Size);
end;
procedure BZFreeMem(AppData, Block: Pointer); stdcall;
begin
FreeMem(Block);
end;
function Bz2Check(const Code: Integer; const ValidCodes: array of Integer): integer;
var I: Integer;
begin
if Code = BZ_MEM_ERROR then
OutOfMemoryError;
Result := Code;
for I := Low(ValidCodes) to High(ValidCodes) do
if ValidCodes[I] = Code then
Exit;
raise Exception.CreateFmt(SBzlibInternalErrorN, [Code]);
end;
{$ifdef Win32}
function BZCompressorInit: boolean;
var F: string;
begin
result := true;
if @BZ2_bzCompressInit=nil then begin
F := extractfilepath(paramstr(0))+'bzip.dll';
if FileExists(F) then
result := BZInitCompressFunctions(LoadLibrary(PChar(F))) else
result := false;
end;
end;
{$ifndef USEBZAASM}
function BZDeCompressorInit: boolean;
begin
if (@BZ2_bzDecompressInit=nil) then
result := BZInitDecompressFunctions else
result := true;
end;
{$endif}
{$endif}
{ TBZCompressor }
constructor TBZCompressor.Create(outStream: TStream; CompressionLevel: Integer);
begin
{$ifdef Win32}
BZCompressorInit;
if @BZ2_bzCompressInit=nil then
CompressionLevel := -1; // <0 -> direct copy (no compression)
{$endif}
FDestStream := outStream;
FStrm.Init;
New(BufferOut);
with FStrm do begin
next_out := PAnsiChar(BufferOut);
avail_out := SizeOf(BufferOut^);
next_in := @FBufferIn;
end;
if CompressionLevel>=0 then // FInitialized=false -> direct copy
FInitialized := Bz2Check(BZ2_bzCompressInit(FStrm, CompressionLevel, 0, 0),
[BZ_OK])=BZ_OK;
end;
destructor TBZCompressor.Destroy;
begin
if FInitialized then begin
FStrm.next_out := nil;
FStrm.avail_out := 0;
BZ2_bzCompressEnd(FStrm);
FreeMem(BufferOut);
end;
inherited;
end;
procedure TBZCompressor.Finish;
begin
if self=nil then
exit;
if FInitialized then begin
while FStrm.avail_in > 0 do begin
Bz2Check(BZ2_bzCompress(FStrm, BZ_RUN), [BZ_RUN_OK]);
if FStrm.avail_out = 0 then FlushBufferOut;
end;
FStrm.next_in := nil;
FStrm.avail_in := 0;
while Bz2Check(BZ2_bzCompress(FStrm, BZ_FINISH), [BZ_FINISH_OK, BZ_STREAM_END])<>BZ_STREAM_END do
FlushBufferOut;
end;
FlushBufferOut;
end;
procedure TBZCompressor.FlushBufferOut;
var Count: integer;
begin
// if not FInitialized then exit;
Count := SizeOf(BufferOut^) - FStrm.avail_out;
if Count=0 then exit;
WriteDest(BufferOut^, Count);
FStrm.next_out := PAnsiChar(BufferOut);
FStrm.avail_out := SizeOf(BufferOut^);
end;
function TBZCompressor.Read(var Buffer; Count: Integer): Longint;
begin
assert(false);
result := 0;
end;
function TBZCompressor.Seek(Offset: Integer; Origin: Word): Longint;
begin
if not FInitialized then // CompressionLevel<0: direct copy to
result := 0 else
if (Offset = 0) and (Origin = soFromCurrent) then // for TStream.Position
Result := FStrm.total_out else begin
Result := 0;
assert((Offset = 0) and (Origin = soFromBeginning) and (FStrm.total_out = 0));
end;
end;
function TBZCompressor.Write(const Buffer; Count: Integer): Longint;
var i: integer;
p: PAnsiChar;
begin
if (self<>nil) and (Count>0) then begin
result := Count;
inc(SrcLen,Count);
if FInitialized then begin
if Count+FStrm.avail_in>sizeof(fBufferIn)-1 then begin
while FStrm.avail_in > 0 do begin
Bz2Check(BZ2_bzCompress(FStrm, BZ_RUN), [BZ_RUN_OK]);
if FStrm.avail_out = 0 then
FlushBufferOut;
end;
FStrm.avail_in := 0;
FStrm.next_in := @fBufferIn;
end;
if Count<sizeof(fBufferIn) then begin
move(Buffer,fBufferIn[FStrm.avail_in],Count);
inc(FStrm.avail_in,Count);
end else begin
FStrm.avail_in := Count;
FStrm.next_in := @Buffer;
while FStrm.avail_in > 0 do begin
Bz2Check(BZ2_bzCompress(FStrm, BZ_RUN), [BZ_RUN_OK]);
if FStrm.avail_out = 0 then
FlushBufferOut;
end;
FStrm.avail_in := 0;
FStrm.next_in := @fBufferIn;
end;
end else begin // if not FIinitialized: CompressionLevel<0: direct copy to
p := @Buffer;
while Count>=FStrm.avail_out do begin
FlushBufferOut;
if Count<sizeof(BufferOut^) then
i := Count else
i := sizeof(BufferOut^);
move(p^,BufferOut^,i);
inc(p,i);
dec(Count,i);
dec(FStrm.avail_out,i);
end;
if Count>0 then begin
move(p^,BufferOut^[sizeof(BufferOut^)-FStrm.avail_out],Count);
dec(FStrm.avail_out,Count);
end;
end;
end else
result := 0; // self=nil
end;
procedure TBZCompressor.WriteDest(const Buffer; Count: Integer);
begin
inc(DestLen,Count);
FDestStream.Write(Buffer, Count);
end;
{ TBZDecompressor }
constructor TBZDecompressor.Create(inStream: TStream);
begin
FSrcStream := inStream;
{$ifndef USEBZAASM}
if (@BZ2_bzDecompressInit=nil) and not BZInitDecompressFunctions then exit;
{$endif}
FStrm.Init;
FStrm.next_in := @FBufferIn;
FInitialized := Bz2Check(BZ2_bzDecompressInit(FStrm, 0, 0), [BZ_OK])=BZ_OK;
end;
destructor TBZDecompressor.Destroy;
begin
if FInitialized then begin
FStrm.next_out := nil;
FStrm.avail_out := 0;
BZ2_bzDecompressEnd(FStrm);
end;
inherited;
end;
function TBZDecompressor.Read(var Buffer; Count: Integer): Longint;
begin
if not FInitialized then begin // no decompression: direct copy from source
Result := FSrcStream.Read(Buffer,Count);
inc(DestLen,Result);
exit;
end;
Result := 0;
if FReachedEnd then { unexpected EOF }
exit;
FStrm.next_out := @Buffer;
FStrm.avail_out := Count;
while FStrm.avail_out > 0 do begin
if FStrm.avail_in = 0 then begin
FStrm.next_in := @FBufferIn;
FStrm.avail_in := FSrcStream.Read(FBufferIn, SizeOf(FBufferIn));
{ Unlike zlib, bzlib does not return an error when avail_in is zero and
it still needs input. To avoid an infinite loop, check for this. }
if FStrm.avail_in=0 then
exit; // leave result=0 to mark error
end;
case Bz2Check(BZ2_bzDecompress(FStrm),
[BZ_OK, BZ_STREAM_END, BZ_DATA_ERROR, BZ_DATA_ERROR_MAGIC]) of
BZ_STREAM_END: begin
FReachedEnd := True;
break;
end;
BZ_DATA_ERROR, BZ_DATA_ERROR_MAGIC:
raise Exception.Create(SBzlibDataError);
end;
end;
Result := Count-FStrm.avail_out;
inc(DestLen,Result);
end;
function TBZDecompressor.Seek(Offset: Integer; Origin: Word): Longint;
begin
if not FInitialized then // CompressionLevel<0: direct copy to
result := DestLen else
result := FStrm.total_out;
if (Offset<>0) or (Origin<>soFromCurrent) then begin // for TStream.Position
assert((Offset = 0) and (Origin = soFromBeginning) and (result = 0));
Result := 0;
end;
end;
function TBZDecompressor.Write(const Buffer; Count: Integer): Longint;
begin
assert(false);
result := 0;
end;
function UnCompressBzDll(Source: PAnsiChar; SourceSize, DestSize: integer): TMemoryStream; overload;
// result=nil if error decompressing
var strm: TBZStreamRec;
res: integer;
begin
result := nil;
{$ifndef USEBZAASM}
if (@BZ2_bzDecompressInit=nil) and not BZInitDecompressFunctions then exit;
{$endif}
strm.Init;
if BZ2_bzDecompressInit(strm, 0,0)<>BZ_OK then
exit;
strm.next_in := Source;
strm.avail_in := SourceSize;
result := TMemoryStream.Create;
result.Size := DestSize;
strm.next_out := result.Memory;
strm.avail_out := DestSize;
{$ifdef USEBZAASM}
try
res := BZ2_bzDecompress(strm);
except
on E: Exception do
res := BZ_DATA_ERROR;
end;
{$else}
res := BZ2_bzDecompress(strm);
{$endif}
if res<>BZ_STREAM_END then
FreeAndNil(result);
BZ2_bzDecompressEnd(strm);
end;
function UnCompressBzDll(Source: PAnsiChar; SourceSize: integer;
Dest: PAnsiChar; Destsize: integer): boolean; overload;
// Dest must already be allocated with DestSize = enough place
var strm: TBZStreamRec;
res: integer;
begin
result := false;
{$ifndef USEBZAASM}
if (@BZ2_bzDecompressInit=nil) and not BZInitDecompressFunctions then exit;
{$endif}
strm.Init;
if BZ2_bzDecompressInit(strm, 0,0)<>BZ_OK then
exit;
strm.next_in := Source;
strm.avail_in := SourceSize;
strm.next_out := Dest;
strm.avail_out := DestSize;
{$ifdef USEBZAASM}
try
res := BZ2_bzDecompress(strm);
except
on E: Exception do
res := BZ_DATA_ERROR;
end;
{$else}
res := BZ2_bzDecompress(strm);
{$endif}
result := (res=BZ_STREAM_END);
BZ2_bzDecompressEnd(strm);
end;
{ TBZStreamRec }
procedure TBZStreamRec.Init;
begin
FillChar(self, SizeOf(self), 0);
{$ifdef Win32} // use LibC Alloc/Free on Linux
zalloc := BZAllocMem;
zfree := BZFreeMem;
{$endif}
end;
{$ifdef Win32}
initialization
finalization
if BZCompressModule<>0 then
FreeLibrary(BZCompressModule);
if BZDeCompressModule<>0 then
FreeLibrary(BZDeCompressModule);
{$endif}
end.