| 1 |
(* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* This program, "bzip2", the associated library "libbzip2", and all |
| 3 |
* documentation, are copyright (C) 1996-2007 Julian R Seward. All |
| 4 |
* rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. The origin of this software must not be misrepresented; you must |
| 14 |
* not claim that you wrote the original software. If you use this |
| 15 |
* software in a product, an acknowledgment in the product |
| 16 |
* documentation would be appreciated but is not required. |
| 17 |
* |
| 18 |
* 3. Altered source versions must be plainly marked as such, and must |
| 19 |
* not be misrepresented as being the original software. |
| 20 |
* |
| 21 |
* 4. The name of the author may not be used to endorse or promote |
| 22 |
* products derived from this software without specific prior written |
| 23 |
* permission. |
| 24 |
* |
| 25 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 26 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 27 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 29 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 31 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 32 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 33 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 |
* |
| 37 |
* Julian Seward, [email protected] |
| 38 |
* bzip2/libbzip2 version 1.0.5 of 10 December 2007 |
| 39 |
* |
| 40 |
* Pascal wrapper created by Edison Mera, version 1.04 |
| 41 |
* http://edisonlife.homelinux.com/ |
| 42 |
* |
| 43 |
* Dynamic and runtime linking and Win64/OS X/Linux support by Craig Peterson |
| 44 |
* http://tpabbrevia.sourceforge.net/ |
| 45 |
* ***** END LICENSE BLOCK ***** *) |
| 46 |
|
| 47 |
unit AbBzip2; |
| 48 |
|
| 49 |
{$I AbDefine.inc} |
| 50 |
|
| 51 |
interface |
| 52 |
|
| 53 |
uses |
| 54 |
SysUtils, Classes; |
| 55 |
|
| 56 |
type |
| 57 |
TAlloc = function(opaque: Pointer; Items, Size: Integer): Pointer; cdecl; |
| 58 |
TFree = procedure(opaque, Block: Pointer); cdecl; |
| 59 |
|
| 60 |
// Internal structure. Ignore. |
| 61 |
TBZStreamRec = record |
| 62 |
next_in: PByte; // next input byte |
| 63 |
avail_in: Integer; // number of bytes available at next_in |
| 64 |
total_in_lo32: Integer; // total nb of input bytes read so far |
| 65 |
total_in_hi32: Integer; |
| 66 |
|
| 67 |
next_out: PByte; // next output byte should be put here |
| 68 |
avail_out: Integer; // remaining free space at next_out |
| 69 |
total_out_lo32: Integer; // total nb of bytes output so far |
| 70 |
total_out_hi32: Integer; |
| 71 |
|
| 72 |
state: Pointer; |
| 73 |
|
| 74 |
bzalloc: TAlloc; // used to allocate the internal state |
| 75 |
bzfree: TFree; // used to free the internal state |
| 76 |
opaque: Pointer; |
| 77 |
end; |
| 78 |
|
| 79 |
// Abstract ancestor class |
| 80 |
TCustomBZip2Stream = class(TStream) |
| 81 |
private |
| 82 |
FStrm: TStream; |
| 83 |
FStrmPos: Int64; |
| 84 |
FOnProgress: TNotifyEvent; |
| 85 |
FBZRec: TBZStreamRec; |
| 86 |
FBuffer: array[Word] of Byte; |
| 87 |
protected |
| 88 |
procedure Progress(Sender: TObject); dynamic; |
| 89 |
property OnProgress: TNotifyEvent read FOnProgress write FOnProgress; |
| 90 |
constructor Create(Strm: TStream); |
| 91 |
end; |
| 92 |
|
| 93 |
{ TBZCompressionStream compresses data on the fly as data is written to it, and |
| 94 |
stores the compressed data to another stream. |
| 95 |
|
| 96 |
TBZCompressionStream is write-only and strictly sequential. Reading from the |
| 97 |
stream will raise an exception. Using Seek to move the stream pointer |
| 98 |
will raise an exception. |
| 99 |
|
| 100 |
Output data is cached internally, written to the output stream only when |
| 101 |
the internal output buffer is full. All pending output data is flushed |
| 102 |
when the stream is destroyed. |
| 103 |
|
| 104 |
The Position property returns the number of uncompressed bytes of |
| 105 |
data that have been written to the stream so far. |
| 106 |
|
| 107 |
CompressionRate returns the on-the-fly percentage by which the original |
| 108 |
data has been compressed: (1 - (CompressedBytes / UncompressedBytes)) * 100 |
| 109 |
If raw data size = 100 and compressed data size = 25, the CompressionRate |
| 110 |
is 75% |
| 111 |
|
| 112 |
The OnProgress event is called each time the output buffer is filled and |
| 113 |
written to the output stream. This is useful for updating a progress |
| 114 |
indicator when you are writing a large chunk of data to the compression |
| 115 |
stream in a single call.} |
| 116 |
|
| 117 |
TBZCompressionStream = class(TCustomBZip2Stream) |
| 118 |
private |
| 119 |
function GetCompressionRate: Single; |
| 120 |
public |
| 121 |
constructor Create(Level: IntPtr; Dest: TStream); |
| 122 |
destructor Destroy; override; |
| 123 |
function Read(var Buffer; Count: Longint): Longint; override; |
| 124 |
function Write(const Buffer; Count: Longint): Longint; override; |
| 125 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
| 126 |
property CompressionRate: Single read GetCompressionRate; |
| 127 |
property OnProgress; |
| 128 |
end; |
| 129 |
|
| 130 |
{ TDecompressionStream decompresses data on the fly as data is read from it. |
| 131 |
|
| 132 |
Compressed data comes from a separate source stream. TDecompressionStream |
| 133 |
is read-only and unidirectional; you can seek forward in the stream, but not |
| 134 |
backwards. The special case of setting the stream position to zero is |
| 135 |
allowed. Seeking forward decompresses data until the requested position in |
| 136 |
the uncompressed data has been reached. Seeking backwards, seeking relative |
| 137 |
to the end of the stream, requesting the size of the stream, and writing to |
| 138 |
the stream will raise an exception. |
| 139 |
|
| 140 |
The Position property returns the number of bytes of uncompressed data that |
| 141 |
have been read from the stream so far. |
| 142 |
|
| 143 |
The OnProgress event is called each time the internal input buffer of |
| 144 |
compressed data is exhausted and the next block is read from the input stream. |
| 145 |
This is useful for updating a progress indicator when you are reading a |
| 146 |
large chunk of data from the decompression stream in a single call.} |
| 147 |
|
| 148 |
TBZDecompressionStream = class(TCustomBZip2Stream) |
| 149 |
private |
| 150 |
FReadState: LongInt; |
| 151 |
public |
| 152 |
constructor Create(Source: TStream); |
| 153 |
destructor Destroy; override; |
| 154 |
function Read(var Buffer; Count: Longint): Longint; override; |
| 155 |
function Write(const Buffer; Count: Longint): Longint; override; |
| 156 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
| 157 |
property OnProgress; |
| 158 |
end; |
| 159 |
|
| 160 |
{ CompressBuf compresses data, buffer to buffer, in one call. |
| 161 |
In: InBuf = ptr to compressed data |
| 162 |
InBytes = number of bytes in InBuf |
| 163 |
Out: OutBuf = ptr to newly allocated buffer containing decompressed data |
| 164 |
OutBytes = number of bytes in OutBuf } |
| 165 |
procedure BZCompressBuf(const InBuf: Pointer; InBytes: Integer; |
| 166 |
out OutBuf: Pointer; out OutBytes: Integer); |
| 167 |
|
| 168 |
|
| 169 |
{ DecompressBuf decompresses data, buffer to buffer, in one call. |
| 170 |
In: InBuf = ptr to compressed data |
| 171 |
InBytes = number of bytes in InBuf |
| 172 |
OutEstimate = zero, or est. size of the decompressed data |
| 173 |
Out: OutBuf = ptr to newly allocated buffer containing decompressed data |
| 174 |
OutBytes = number of bytes in OutBuf } |
| 175 |
procedure BZDecompressBuf(const InBuf: Pointer; InBytes: Integer; |
| 176 |
OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer); |
| 177 |
|
| 178 |
|
| 179 |
type |
| 180 |
EBZip2Error = class(Exception); |
| 181 |
EBZCompressionError = class(EBZip2Error); |
| 182 |
EBZDecompressionError = class(EBZip2Error); |
| 183 |
|
| 184 |
implementation |
| 185 |
|
| 186 |
// Compile for Win64 using MSVC |
| 187 |
// <Path To MSVC>\bin\x86_amd64\cl.exe -c -nologo -GS- -Z7 -wd4086 -Gs32768 |
| 188 |
// -DBZ_NO_STDIO blocksort.c huffman.c compress.c decompress.c bzlib.c |
| 189 |
|
| 190 |
uses |
| 191 |
{$IFDEF Bzip2Runtime} |
| 192 |
{$IF DEFINED(FPC)} |
| 193 |
dynlibs, |
| 194 |
{$ELSEIF DEFINED(MSWINDOWS)} |
| 195 |
Windows, |
| 196 |
{$IFEND} |
| 197 |
{$ENDIF} |
| 198 |
AbUtils; |
| 199 |
|
| 200 |
{$IFDEF Bzip2Static} |
| 201 |
{$IF DEFINED(WIN32)} |
| 202 |
{$L Win32\blocksort.obj} |
| 203 |
{$L Win32\huffman.obj} |
| 204 |
{$L Win32\compress.obj} |
| 205 |
{$L Win32\decompress.obj} |
| 206 |
{$L Win32\bzlib.obj} |
| 207 |
{$ELSEIF DEFINED(WIN64)} |
| 208 |
{$L Win64\blocksort.obj} |
| 209 |
{$L Win64\huffman.obj} |
| 210 |
{$L Win64\compress.obj} |
| 211 |
{$L Win64\decompress.obj} |
| 212 |
{$L Win64\bzlib.obj} |
| 213 |
{$IFEND} |
| 214 |
|
| 215 |
procedure BZ2_hbMakeCodeLengths; external; |
| 216 |
procedure BZ2_blockSort; external; |
| 217 |
procedure BZ2_hbCreateDecodeTables; external; |
| 218 |
procedure BZ2_hbAssignCodes; external; |
| 219 |
procedure BZ2_compressBlock; external; |
| 220 |
procedure BZ2_decompress; external; |
| 221 |
{$ENDIF} |
| 222 |
|
| 223 |
type |
| 224 |
TLargeInteger = record |
| 225 |
case Integer of |
| 226 |
0: ( |
| 227 |
LowPart: LongWord; |
| 228 |
HighPart: LongWord); |
| 229 |
1: ( |
| 230 |
QuadPart: Int64); |
| 231 |
end; |
| 232 |
|
| 233 |
const |
| 234 |
BZ_RUN = 0; |
| 235 |
BZ_FLUSH = 1; |
| 236 |
BZ_FINISH = 2; |
| 237 |
BZ_OK = 0; |
| 238 |
BZ_RUN_OK = 1; |
| 239 |
BZ_FLUSH_OK = 2; |
| 240 |
BZ_FINISH_OK = 3; |
| 241 |
BZ_STREAM_END = 4; |
| 242 |
BZ_SEQUENCE_ERROR = (-1); |
| 243 |
BZ_PARAM_ERROR = (-2); |
| 244 |
BZ_MEM_ERROR = (-3); |
| 245 |
BZ_DATA_ERROR = (-4); |
| 246 |
BZ_DATA_ERROR_MAGIC = (-5); |
| 247 |
BZ_IO_ERROR = (-6); |
| 248 |
BZ_UNEXPECTED_EOF = (-7); |
| 249 |
BZ_OUTBUFF_FULL = (-8); |
| 250 |
|
| 251 |
BZ_BLOCK_SIZE_100K = 9; |
| 252 |
|
| 253 |
{$IFDEF Bzip2Static} |
| 254 |
BZ2_rNums: array[0..511] of Longint = ( |
| 255 |
619, 720, 127, 481, 931, 816, 813, 233, 566, 247, |
| 256 |
985, 724, 205, 454, 863, 491, 741, 242, 949, 214, |
| 257 |
733, 859, 335, 708, 621, 574, 73, 654, 730, 472, |
| 258 |
419, 436, 278, 496, 867, 210, 399, 680, 480, 51, |
| 259 |
878, 465, 811, 169, 869, 675, 611, 697, 867, 561, |
| 260 |
862, 687, 507, 283, 482, 129, 807, 591, 733, 623, |
| 261 |
150, 238, 59, 379, 684, 877, 625, 169, 643, 105, |
| 262 |
170, 607, 520, 932, 727, 476, 693, 425, 174, 647, |
| 263 |
73, 122, 335, 530, 442, 853, 695, 249, 445, 515, |
| 264 |
909, 545, 703, 919, 874, 474, 882, 500, 594, 612, |
| 265 |
641, 801, 220, 162, 819, 984, 589, 513, 495, 799, |
| 266 |
161, 604, 958, 533, 221, 400, 386, 867, 600, 782, |
| 267 |
382, 596, 414, 171, 516, 375, 682, 485, 911, 276, |
| 268 |
98, 553, 163, 354, 666, 933, 424, 341, 533, 870, |
| 269 |
227, 730, 475, 186, 263, 647, 537, 686, 600, 224, |
| 270 |
469, 68, 770, 919, 190, 373, 294, 822, 808, 206, |
| 271 |
184, 943, 795, 384, 383, 461, 404, 758, 839, 887, |
| 272 |
715, 67, 618, 276, 204, 918, 873, 777, 604, 560, |
| 273 |
951, 160, 578, 722, 79, 804, 96, 409, 713, 940, |
| 274 |
652, 934, 970, 447, 318, 353, 859, 672, 112, 785, |
| 275 |
645, 863, 803, 350, 139, 93, 354, 99, 820, 908, |
| 276 |
609, 772, 154, 274, 580, 184, 79, 626, 630, 742, |
| 277 |
653, 282, 762, 623, 680, 81, 927, 626, 789, 125, |
| 278 |
411, 521, 938, 300, 821, 78, 343, 175, 128, 250, |
| 279 |
170, 774, 972, 275, 999, 639, 495, 78, 352, 126, |
| 280 |
857, 956, 358, 619, 580, 124, 737, 594, 701, 612, |
| 281 |
669, 112, 134, 694, 363, 992, 809, 743, 168, 974, |
| 282 |
944, 375, 748, 52, 600, 747, 642, 182, 862, 81, |
| 283 |
344, 805, 988, 739, 511, 655, 814, 334, 249, 515, |
| 284 |
897, 955, 664, 981, 649, 113, 974, 459, 893, 228, |
| 285 |
433, 837, 553, 268, 926, 240, 102, 654, 459, 51, |
| 286 |
686, 754, 806, 760, 493, 403, 415, 394, 687, 700, |
| 287 |
946, 670, 656, 610, 738, 392, 760, 799, 887, 653, |
| 288 |
978, 321, 576, 617, 626, 502, 894, 679, 243, 440, |
| 289 |
680, 879, 194, 572, 640, 724, 926, 56, 204, 700, |
| 290 |
707, 151, 457, 449, 797, 195, 791, 558, 945, 679, |
| 291 |
297, 59, 87, 824, 713, 663, 412, 693, 342, 606, |
| 292 |
134, 108, 571, 364, 631, 212, 174, 643, 304, 329, |
| 293 |
343, 97, 430, 751, 497, 314, 983, 374, 822, 928, |
| 294 |
140, 206, 73, 263, 980, 736, 876, 478, 430, 305, |
| 295 |
170, 514, 364, 692, 829, 82, 855, 953, 676, 246, |
| 296 |
369, 970, 294, 750, 807, 827, 150, 790, 288, 923, |
| 297 |
804, 378, 215, 828, 592, 281, 565, 555, 710, 82, |
| 298 |
896, 831, 547, 261, 524, 462, 293, 465, 502, 56, |
| 299 |
661, 821, 976, 991, 658, 869, 905, 758, 745, 193, |
| 300 |
768, 550, 608, 933, 378, 286, 215, 979, 792, 961, |
| 301 |
61, 688, 793, 644, 986, 403, 106, 366, 905, 644, |
| 302 |
372, 567, 466, 434, 645, 210, 389, 550, 919, 135, |
| 303 |
780, 773, 635, 389, 707, 100, 626, 958, 165, 504, |
| 304 |
920, 176, 193, 713, 857, 265, 203, 50, 668, 108, |
| 305 |
645, 990, 626, 197, 510, 357, 358, 850, 858, 364, |
| 306 |
936, 638 |
| 307 |
); |
| 308 |
|
| 309 |
BZ2_crc32Table: array[0..255] of Longint = ( |
| 310 |
$00000000, $04C11DB7, $09823B6E, $0D4326D9, |
| 311 |
$130476DC, $17C56B6B, $1A864DB2, $1E475005, |
| 312 |
$2608EDB8, $22C9F00F, $2F8AD6D6, $2B4BCB61, |
| 313 |
$350C9B64, $31CD86D3, $3C8EA00A, $384FBDBD, |
| 314 |
$4C11DB70, $48D0C6C7, $4593E01E, $4152FDA9, |
| 315 |
$5F15ADAC, $5BD4B01B, $569796C2, $52568B75, |
| 316 |
$6A1936C8, $6ED82B7F, $639B0DA6, $675A1011, |
| 317 |
$791D4014, $7DDC5DA3, $709F7B7A, $745E66CD, |
| 318 |
-$67DC4920, -$631D54A9, -$6E5E7272, -$6A9F6FC7, |
| 319 |
-$74D83FC4, -$70192275, -$7D5A04AE, -$799B191B, |
| 320 |
-$41D4A4A8, -$4515B911, -$48569FCA, -$4C97827F, |
| 321 |
-$52D0D27C, -$5611CFCD, -$5B52E916, -$5F93F4A3, |
| 322 |
-$2BCD9270, -$2F0C8FD9, -$224FA902, -$268EB4B7, |
| 323 |
-$38C9E4B4, -$3C08F905, -$314BDFDE, -$358AC26B, |
| 324 |
-$0DC57FD8, -$09046261, -$044744BA, -$0086590F, |
| 325 |
-$1EC1090C, -$1A0014BD, -$17433266, -$13822FD3, |
| 326 |
$34867077, $30476DC0, $3D044B19, $39C556AE, |
| 327 |
$278206AB, $23431B1C, $2E003DC5, $2AC12072, |
| 328 |
$128E9DCF, $164F8078, $1B0CA6A1, $1FCDBB16, |
| 329 |
$018AEB13, $054BF6A4, $0808D07D, $0CC9CDCA, |
| 330 |
$7897AB07, $7C56B6B0, $71159069, $75D48DDE, |
| 331 |
$6B93DDDB, $6F52C06C, $6211E6B5, $66D0FB02, |
| 332 |
$5E9F46BF, $5A5E5B08, $571D7DD1, $53DC6066, |
| 333 |
$4D9B3063, $495A2DD4, $44190B0D, $40D816BA, |
| 334 |
-$535A3969, -$579B24E0, -$5AD80207, -$5E191FB2, |
| 335 |
-$405E4FB5, -$449F5204, -$49DC74DB, -$4D1D696E, |
| 336 |
-$7552D4D1, -$7193C968, -$7CD0EFBF, -$7811F20A, |
| 337 |
-$6656A20D, -$6297BFBC, -$6FD49963, -$6B1584D6, |
| 338 |
-$1F4BE219, -$1B8AFFB0, -$16C9D977, -$1208C4C2, |
| 339 |
-$0C4F94C5, -$088E8974, -$05CDAFAB, -$010CB21E, |
| 340 |
-$39430FA1, -$3D821218, -$30C134CF, -$3400297A, |
| 341 |
-$2A47797D, -$2E8664CC, -$23C54213, -$27045FA6, |
| 342 |
$690CE0EE, $6DCDFD59, $608EDB80, $644FC637, |
| 343 |
$7A089632, $7EC98B85, $738AAD5C, $774BB0EB, |
| 344 |
$4F040D56, $4BC510E1, $46863638, $42472B8F, |
| 345 |
$5C007B8A, $58C1663D, $558240E4, $51435D53, |
| 346 |
$251D3B9E, $21DC2629, $2C9F00F0, $285E1D47, |
| 347 |
$36194D42, $32D850F5, $3F9B762C, $3B5A6B9B, |
| 348 |
$0315D626, $07D4CB91, $0A97ED48, $0E56F0FF, |
| 349 |
$1011A0FA, $14D0BD4D, $19939B94, $1D528623, |
| 350 |
-$0ED0A9F2, -$0A11B447, -$075292A0, -$03938F29, |
| 351 |
-$1DD4DF2E, -$1915C29B, -$1456E444, -$1097F9F5, |
| 352 |
-$28D8444A, -$2C1959FF, -$215A7F28, -$259B6291, |
| 353 |
-$3BDC3296, -$3F1D2F23, -$325E09FC, -$369F144D, |
| 354 |
-$42C17282, -$46006F37, -$4B4349F0, -$4F825459, |
| 355 |
-$51C5045E, -$550419EB, -$58473F34, -$5C862285, |
| 356 |
-$64C99F3A, -$6008828F, -$6D4BA458, -$698AB9E1, |
| 357 |
-$77CDE9E6, -$730CF453, -$7E4FD28C, -$7A8ECF3D, |
| 358 |
$5D8A9099, $594B8D2E, $5408ABF7, $50C9B640, |
| 359 |
$4E8EE645, $4A4FFBF2, $470CDD2B, $43CDC09C, |
| 360 |
$7B827D21, $7F436096, $7200464F, $76C15BF8, |
| 361 |
$68860BFD, $6C47164A, $61043093, $65C52D24, |
| 362 |
$119B4BE9, $155A565E, $18197087, $1CD86D30, |
| 363 |
$029F3D35, $065E2082, $0B1D065B, $0FDC1BEC, |
| 364 |
$3793A651, $3352BBE6, $3E119D3F, $3AD08088, |
| 365 |
$2497D08D, $2056CD3A, $2D15EBE3, $29D4F654, |
| 366 |
-$3A56D987, -$3E97C432, -$33D4E2E9, -$3715FF60, |
| 367 |
-$2952AF5B, -$2D93B2EE, -$20D09435, -$24118984, |
| 368 |
-$1C5E343F, -$189F298A, -$15DC0F51, -$111D12E8, |
| 369 |
-$0F5A42E3, -$0B9B5F56, -$06D8798D, -$0219643C, |
| 370 |
-$764702F7, -$72861F42, -$7FC53999, -$7B042430, |
| 371 |
-$6543742B, -$6182699E, -$6CC14F45, -$680052F4, |
| 372 |
-$504FEF4F, -$548EF2FA, -$59CDD421, -$5D0CC998, |
| 373 |
-$434B9993, -$478A8426, -$4AC9A2FD, -$4E08BF4C |
| 374 |
); |
| 375 |
|
| 376 |
procedure bz_internal_error(errcode: Integer); cdecl; |
| 377 |
begin |
| 378 |
raise EBZip2Error.CreateFmt('Compression Error %d', [errcode]); |
| 379 |
end; { _bz_internal_error } |
| 380 |
|
| 381 |
function malloc(size: Integer): Pointer; cdecl; |
| 382 |
begin |
| 383 |
GetMem(Result, Size); |
| 384 |
end; { _malloc } |
| 385 |
|
| 386 |
procedure free(block: Pointer); cdecl; |
| 387 |
begin |
| 388 |
FreeMem(block); |
| 389 |
end; { _free } |
| 390 |
{$ENDIF} |
| 391 |
|
| 392 |
const |
| 393 |
libbz2 = {$IF DEFINED(MSWINDOWS)}'bz2.dll' |
| 394 |
{$ELSEIF DEFINED(DARWIN)}'libbz2.dylib' |
| 395 |
{$ELSE}'libbz2.so.1'{$IFEND}; |
| 396 |
|
| 397 |
{$IFDEF Bzip2Runtime} |
| 398 |
var |
| 399 |
hBzip2: HMODULE; |
| 400 |
// deflate compresses data |
| 401 |
BZ2_bzCompressInit: function(var strm: TBZStreamRec; blockSize100k: Integer; |
| 402 |
verbosity: Integer; workFactor: Integer): Integer; |
| 403 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 404 |
BZ2_bzCompress: function(var strm: TBZStreamRec; action: Integer): Integer; |
| 405 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 406 |
BZ2_bzCompressEnd: function (var strm: TBZStreamRec): Integer; |
| 407 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 408 |
BZ2_bzBuffToBuffCompress: function(dest: Pointer; var destLen: Integer; |
| 409 |
source: Pointer; sourceLen, blockSize100k, verbosity, workFactor: Integer): Integer; |
| 410 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 411 |
// inflate decompresses data |
| 412 |
BZ2_bzDecompressInit: function(var strm: TBZStreamRec; verbosity: Integer; |
| 413 |
small: Integer): Integer; |
| 414 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 415 |
BZ2_bzDecompress: function(var strm: TBZStreamRec): Integer; |
| 416 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 417 |
BZ2_bzDecompressEnd: function(var strm: TBZStreamRec): Integer; |
| 418 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 419 |
BZ2_bzBuffToBuffDecompress: function(dest: Pointer; var destLen: Integer; |
| 420 |
source: Pointer; sourceLen, small, verbosity: Integer): Integer; |
| 421 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} |
| 422 |
{$ELSE} |
| 423 |
// deflate compresses data |
| 424 |
function BZ2_bzCompressInit(var strm: TBZStreamRec; blockSize100k: Integer; |
| 425 |
verbosity: Integer; workFactor: Integer): Integer; |
| 426 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 427 |
{$IFDEF DARWIN}name '_BZ2_bzCompressInit'{$ENDIF}; |
| 428 |
|
| 429 |
function BZ2_bzCompress(var strm: TBZStreamRec; action: Integer): Integer; |
| 430 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 431 |
{$IFDEF DARWIN}name '_BZ2_bzCompress'{$ENDIF}; |
| 432 |
|
| 433 |
function BZ2_bzCompressEnd(var strm: TBZStreamRec): Integer; |
| 434 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 435 |
{$IFDEF DARWIN}name '_BZ2_bzCompressEnd'{$ENDIF}; |
| 436 |
|
| 437 |
function BZ2_bzBuffToBuffCompress(dest: Pointer; var destLen: Integer; source: Pointer; |
| 438 |
sourceLen, blockSize100k, verbosity, workFactor: Integer): Integer; |
| 439 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 440 |
{$IFDEF DARWIN}name '_BZ2_bzBuffToBuffCompress'{$ENDIF}; |
| 441 |
|
| 442 |
// inflate decompresses data |
| 443 |
function BZ2_bzDecompressInit(var strm: TBZStreamRec; verbosity: Integer; |
| 444 |
small: Integer): Integer; |
| 445 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 446 |
{$IFDEF DARWIN}name '_BZ2_bzDecompressInit'{$ENDIF}; |
| 447 |
|
| 448 |
function BZ2_bzDecompress(var strm: TBZStreamRec): Integer; |
| 449 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 450 |
{$IFDEF DARWIN}name '_BZ2_bzDecompress'{$ENDIF}; |
| 451 |
|
| 452 |
function BZ2_bzDecompressEnd(var strm: TBZStreamRec): Integer; |
| 453 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 454 |
{$IFDEF DARWIN}name '_BZ2_bzDecompressEnd'{$ENDIF}; |
| 455 |
|
| 456 |
function BZ2_bzBuffToBuffDecompress(dest: Pointer; var destLen: Integer; source: Pointer; |
| 457 |
sourceLen, small, verbosity: Integer): Integer; |
| 458 |
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF} external {$IFDEF Bzip2Dynamic}libbz2{$ENDIF} |
| 459 |
{$IFDEF DARWIN}name '_BZ2_bzBuffToBuffDecompress'{$ENDIF}; |
| 460 |
{$ENDIF} |
| 461 |
|
| 462 |
procedure LoadBzip2DLL; |
| 463 |
begin |
| 464 |
{$IFDEF Bzip2Runtime} |
| 465 |
if hBzip2 <> 0 then |
| 466 |
Exit; |
| 467 |
hBzip2 := LoadLibrary(libbz2); |
| 468 |
if hBzip2 = 0 then |
| 469 |
raise EBZip2Error.Create('Bzip2 shared library not found'); |
| 470 |
@BZ2_bzCompressInit := GetProcAddress(hBzip2, 'BZ2_bzCompressInit'); |
| 471 |
@BZ2_bzCompress := GetProcAddress(hBzip2, 'BZ2_bzCompress'); |
| 472 |
@BZ2_bzCompressEnd := GetProcAddress(hBzip2, 'BZ2_bzCompressEnd'); |
| 473 |
@BZ2_bzBuffToBuffCompress := GetProcAddress(hBzip2, 'BZ2_bzBuffToBuffCompress'); |
| 474 |
@BZ2_bzDecompressInit := GetProcAddress(hBzip2, 'BZ2_bzDecompressInit'); |
| 475 |
@BZ2_bzDecompress := GetProcAddress(hBzip2, 'BZ2_bzDecompress'); |
| 476 |
@BZ2_bzDecompressEnd := GetProcAddress(hBzip2, 'BZ2_bzDecompressEnd'); |
| 477 |
@BZ2_bzBuffToBuffDecompress := GetProcAddress(hBzip2, 'BZ2_bzBuffToBuffDecompress'); |
| 478 |
{$ENDIF} |
| 479 |
end; |
| 480 |
|
| 481 |
function bzip2AllocMem(AppData: Pointer; Items, Size: Integer): Pointer; cdecl; |
| 482 |
begin |
| 483 |
GetMem(Result, Items * Size); |
| 484 |
end; { bzip2AllocMem } |
| 485 |
|
| 486 |
procedure bzip2FreeMem(AppData, Block: Pointer); cdecl; |
| 487 |
begin |
| 488 |
FreeMem(Block); |
| 489 |
end; { bzip2FreeMem } |
| 490 |
|
| 491 |
function CCheck(code: Integer): Integer; |
| 492 |
begin |
| 493 |
Result := code; |
| 494 |
if code < 0 then |
| 495 |
raise EBZCompressionError.CreateFmt('error %d', [code]); //!! |
| 496 |
end; { CCheck } |
| 497 |
|
| 498 |
function DCheck(code: Integer): Integer; |
| 499 |
begin |
| 500 |
Result := code; |
| 501 |
if code < 0 then |
| 502 |
raise EBZDecompressionError.CreateFmt('error %d', [code]); //!! |
| 503 |
end; { DCheck } |
| 504 |
|
| 505 |
procedure BZCompressBuf(const InBuf: Pointer; InBytes: Integer; |
| 506 |
out OutBuf: Pointer; out OutBytes: Integer); |
| 507 |
var |
| 508 |
strm: TBZStreamRec; |
| 509 |
P: Pointer; |
| 510 |
begin |
| 511 |
LoadBzip2DLL; |
| 512 |
FillChar(strm, sizeof(strm), 0); |
| 513 |
strm.bzalloc := bzip2AllocMem; |
| 514 |
strm.bzfree := bzip2FreeMem; |
| 515 |
OutBytes := ((InBytes + (InBytes div 10) + 12) + 255) and not 255; |
| 516 |
GetMem(OutBuf, OutBytes); |
| 517 |
try |
| 518 |
strm.next_in := InBuf; |
| 519 |
strm.avail_in := InBytes; |
| 520 |
strm.next_out := OutBuf; |
| 521 |
strm.avail_out := OutBytes; |
| 522 |
CCheck(BZ2_bzCompressInit(strm, 9, 0, 0)); |
| 523 |
try |
| 524 |
while CCheck(BZ2_bzCompress(strm, BZ_FINISH)) <> BZ_STREAM_END do |
| 525 |
begin |
| 526 |
P := OutBuf; |
| 527 |
Inc(OutBytes, 256); |
| 528 |
ReallocMem(OutBuf, OutBytes); |
| 529 |
strm.next_out := OutBuf + (strm.next_out - P); |
| 530 |
strm.avail_out := 256; |
| 531 |
end; |
| 532 |
finally |
| 533 |
CCheck(BZ2_bzCompressEnd(strm)); |
| 534 |
end; |
| 535 |
ReallocMem(OutBuf, strm.total_out_lo32); |
| 536 |
OutBytes := strm.total_out_lo32; |
| 537 |
except |
| 538 |
FreeMem(OutBuf); |
| 539 |
raise |
| 540 |
end; |
| 541 |
end; |
| 542 |
|
| 543 |
procedure BZDecompressBuf(const InBuf: Pointer; InBytes: Integer; |
| 544 |
OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer); |
| 545 |
var |
| 546 |
strm: TBZStreamRec; |
| 547 |
P: Pointer; |
| 548 |
BufInc: Integer; |
| 549 |
begin |
| 550 |
LoadBzip2DLL; |
| 551 |
FillChar(strm, sizeof(strm), 0); |
| 552 |
strm.bzalloc := bzip2AllocMem; |
| 553 |
strm.bzfree := bzip2FreeMem; |
| 554 |
BufInc := (InBytes + 255) and not 255; |
| 555 |
if OutEstimate = 0 then |
| 556 |
OutBytes := BufInc |
| 557 |
else |
| 558 |
OutBytes := OutEstimate; |
| 559 |
GetMem(OutBuf, OutBytes); |
| 560 |
try |
| 561 |
strm.next_in := InBuf; |
| 562 |
strm.avail_in := InBytes; |
| 563 |
strm.next_out := OutBuf; |
| 564 |
strm.avail_out := OutBytes; |
| 565 |
DCheck(BZ2_bzDecompressInit(strm, 0, 0)); |
| 566 |
try |
| 567 |
while DCheck(BZ2_bzDecompress(strm)) <> BZ_STREAM_END do |
| 568 |
begin |
| 569 |
P := OutBuf; |
| 570 |
Inc(OutBytes, BufInc); |
| 571 |
ReallocMem(OutBuf, OutBytes); |
| 572 |
strm.next_out := OutBuf + (strm.next_out - P); |
| 573 |
strm.avail_out := BufInc; |
| 574 |
end; |
| 575 |
finally |
| 576 |
DCheck(BZ2_bzDecompressEnd(strm)); |
| 577 |
end; |
| 578 |
ReallocMem(OutBuf, strm.total_out_lo32); |
| 579 |
OutBytes := strm.total_out_lo32; |
| 580 |
except |
| 581 |
FreeMem(OutBuf); |
| 582 |
raise |
| 583 |
end; |
| 584 |
end; |
| 585 |
|
| 586 |
// TCustomBZip2Stream |
| 587 |
|
| 588 |
constructor TCustomBZip2Stream.Create(Strm: TStream); |
| 589 |
begin |
| 590 |
inherited Create; |
| 591 |
FStrm := Strm; |
| 592 |
FStrmPos := Strm.Position; |
| 593 |
FBZRec.bzalloc := bzip2AllocMem; |
| 594 |
FBZRec.bzfree := bzip2FreeMem; |
| 595 |
end; |
| 596 |
|
| 597 |
procedure TCustomBZip2Stream.Progress(Sender: TObject); |
| 598 |
begin |
| 599 |
if Assigned(FOnProgress) then FOnProgress(Sender); |
| 600 |
end; { TCustomBZip2Stream } |
| 601 |
|
| 602 |
|
| 603 |
// TBZCompressionStream |
| 604 |
|
| 605 |
constructor TBZCompressionStream.Create(Level: IntPtr; Dest: TStream); |
| 606 |
begin |
| 607 |
inherited Create(Dest); |
| 608 |
LoadBzip2DLL; |
| 609 |
FBZRec.next_out := @FBuffer[0]; |
| 610 |
FBZRec.avail_out := sizeof(FBuffer); |
| 611 |
CCheck(BZ2_bzCompressInit(FBZRec, Level, 0, 0)); |
| 612 |
end; |
| 613 |
|
| 614 |
destructor TBZCompressionStream.Destroy; |
| 615 |
begin |
| 616 |
if FBZRec.state <> nil then begin |
| 617 |
FBZRec.next_in := nil; |
| 618 |
FBZRec.avail_in := 0; |
| 619 |
try |
| 620 |
if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos; |
| 621 |
while (CCheck(BZ2_bzCompress(FBZRec, BZ_FINISH)) <> BZ_STREAM_END) |
| 622 |
and (FBZRec.avail_out = 0) do |
| 623 |
begin |
| 624 |
FStrm.WriteBuffer(FBuffer, sizeof(FBuffer)); |
| 625 |
FBZRec.next_out := @FBuffer[0]; |
| 626 |
FBZRec.avail_out := sizeof(FBuffer); |
| 627 |
end; |
| 628 |
if FBZRec.avail_out < sizeof(FBuffer) then |
| 629 |
FStrm.WriteBuffer(FBuffer, sizeof(FBuffer) - FBZRec.avail_out); |
| 630 |
finally |
| 631 |
BZ2_bzCompressEnd(FBZRec); |
| 632 |
end; |
| 633 |
end; |
| 634 |
inherited Destroy; |
| 635 |
end; |
| 636 |
|
| 637 |
function TBZCompressionStream.Read(var Buffer; Count: Longint): Longint; |
| 638 |
begin |
| 639 |
raise EBZCompressionError.Create('Invalid stream operation'); |
| 640 |
end; { TBZCompressionStream } |
| 641 |
|
| 642 |
function TBZCompressionStream.Write(const Buffer; Count: Longint): Longint; |
| 643 |
begin |
| 644 |
FBZRec.next_in := @Buffer; |
| 645 |
FBZRec.avail_in := Count; |
| 646 |
if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos; |
| 647 |
while (FBZRec.avail_in > 0) do |
| 648 |
begin |
| 649 |
CCheck(BZ2_bzCompress(FBZRec, BZ_RUN)); |
| 650 |
if FBZRec.avail_out = 0 then |
| 651 |
begin |
| 652 |
FStrm.WriteBuffer(FBuffer, sizeof(FBuffer)); |
| 653 |
FBZRec.next_out := @FBuffer[0]; |
| 654 |
FBZRec.avail_out := sizeof(FBuffer); |
| 655 |
FStrmPos := FStrm.Position; |
| 656 |
end; |
| 657 |
Progress(Self); |
| 658 |
end; |
| 659 |
Result := Count; |
| 660 |
end; { TBZCompressionStream } |
| 661 |
|
| 662 |
function TBZCompressionStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 663 |
var |
| 664 |
conv64 : TLargeInteger; |
| 665 |
begin |
| 666 |
if (Offset = 0) and (Origin = soCurrent) then begin |
| 667 |
conv64.LowPart := FBZRec.total_in_lo32; |
| 668 |
conv64.HighPart := FBZRec.total_in_hi32; |
| 669 |
Result := conv64.QuadPart |
| 670 |
end |
| 671 |
else |
| 672 |
raise EBZCompressionError.Create('Invalid stream operation'); |
| 673 |
end; { TBZCompressionStream } |
| 674 |
|
| 675 |
function TBZCompressionStream.GetCompressionRate: Single; |
| 676 |
var |
| 677 |
conv64In : TLargeInteger; |
| 678 |
conv64Out: TLargeInteger; |
| 679 |
begin |
| 680 |
conv64In.LowPart := FBZRec.total_in_lo32; |
| 681 |
conv64In.HighPart := FBZRec.total_in_hi32; |
| 682 |
conv64Out.LowPart := FBZRec.total_out_lo32; |
| 683 |
conv64Out.HighPart := FBZRec.total_out_hi32; |
| 684 |
|
| 685 |
if conv64In.QuadPart = 0 then |
| 686 |
Result := 0 |
| 687 |
else |
| 688 |
Result := (1.0 - (conv64Out.QuadPart / conv64In.QuadPart)) * 100.0; |
| 689 |
end; { TBZCompressionStream } |
| 690 |
|
| 691 |
// TDecompressionStream |
| 692 |
|
| 693 |
constructor TBZDecompressionStream.Create(Source: TStream); |
| 694 |
begin |
| 695 |
inherited Create(Source); |
| 696 |
LoadBzip2DLL; |
| 697 |
FBZRec.next_in := @FBuffer[0]; |
| 698 |
FBZRec.avail_in := 0; |
| 699 |
DCheck(BZ2_bzDecompressInit(FBZRec, 0, 0)); |
| 700 |
end; |
| 701 |
|
| 702 |
destructor TBZDecompressionStream.Destroy; |
| 703 |
begin |
| 704 |
if FBZRec.state <> nil then |
| 705 |
BZ2_bzDecompressEnd(FBZRec); |
| 706 |
inherited Destroy; |
| 707 |
end; |
| 708 |
|
| 709 |
function TBZDecompressionStream.Read(var Buffer; Count: Longint): Longint; |
| 710 |
begin |
| 711 |
FBZRec.next_out := @Buffer; |
| 712 |
FBZRec.avail_out := Count; |
| 713 |
if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos; |
| 714 |
while (FBZRec.avail_out > 0) do |
| 715 |
begin |
| 716 |
if FReadState = BZ_STREAM_END then |
| 717 |
begin |
| 718 |
Result := Count - FBZRec.avail_out; |
| 719 |
Exit; |
| 720 |
end |
| 721 |
else if FBZRec.avail_in = 0 then |
| 722 |
begin |
| 723 |
FBZRec.avail_in := FStrm.Read(FBuffer, sizeof(FBuffer)); |
| 724 |
if FBZRec.avail_in = 0 then |
| 725 |
begin |
| 726 |
Result := Count - FBZRec.avail_out; |
| 727 |
Exit; |
| 728 |
end; |
| 729 |
FBZRec.next_in := @FBuffer[0]; |
| 730 |
FStrmPos := FStrm.Position; |
| 731 |
end; |
| 732 |
FReadState := DCheck(BZ2_bzDecompress(FBZRec)); |
| 733 |
Progress(Self); |
| 734 |
end; |
| 735 |
Result := Count; |
| 736 |
end; { TBZDecompressionStream } |
| 737 |
|
| 738 |
function TBZDecompressionStream.Write(const Buffer; Count: Longint): Longint; |
| 739 |
begin |
| 740 |
raise EBZDecompressionError.Create('Invalid stream operation'); |
| 741 |
end; { TBZDecompressionStream } |
| 742 |
|
| 743 |
function TBZDecompressionStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 744 |
var |
| 745 |
I : Integer; |
| 746 |
Buf : array[0..4095] of Char; |
| 747 |
conv64: TLargeInteger; |
| 748 |
NewOff: Int64; |
| 749 |
begin |
| 750 |
conv64.LowPart := FBZRec.total_out_lo32; |
| 751 |
conv64.HighPart := FBZRec.total_out_hi32; |
| 752 |
|
| 753 |
if (Offset = 0) and (Origin = soBeginning) then |
| 754 |
begin |
| 755 |
DCheck(BZ2_bzDecompressEnd(FBZRec)); |
| 756 |
DCheck(BZ2_bzDecompressInit(FBZRec, 0, 0)); |
| 757 |
FBZRec.next_in := @FBuffer[0]; |
| 758 |
FBZRec.avail_in := 0; |
| 759 |
FStrm.Position := 0; |
| 760 |
FStrmPos := 0; |
| 761 |
end |
| 762 |
else if ((Offset >= 0) and (Origin = soCurrent)) or |
| 763 |
(((Offset - conv64.QuadPart) > 0) and (Origin = soBeginning)) then |
| 764 |
begin |
| 765 |
NewOff := Offset; |
| 766 |
if Origin = soBeginning then Dec(NewOff, conv64.QuadPart); |
| 767 |
if NewOff > 0 then |
| 768 |
begin |
| 769 |
for I := 1 to NewOff div sizeof(Buf) do |
| 770 |
ReadBuffer(Buf, sizeof(Buf)); |
| 771 |
ReadBuffer(Buf, NewOff mod sizeof(Buf)); |
| 772 |
end; |
| 773 |
end |
| 774 |
else |
| 775 |
raise EBZDecompressionError.Create('Invalid stream operation'); |
| 776 |
Result := conv64.QuadPart; |
| 777 |
end; { TBZDecompressionStream } |
| 778 |
|
| 779 |
end. |