dzlib.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. {*******************************************************}
  2. { }
  3. { Delphi Supplemental Components }
  4. { ZLIB Data Compression Interface Unit }
  5. { }
  6. { Copyright (c) 1997 Borland International }
  7. { Copyright (c) 1998 Jacques Nomssi Nzali }
  8. { }
  9. {*******************************************************}
  10. {
  11. Modified for
  12. Vampyre Imaging Library
  13. by Marek Mauder ([email protected])
  14. http://imaginglib.sourceforge.net
  15. You can choose which pascal zlib implementation will be
  16. used. IMPASZLIB and FPCPASZLIB are translations of zlib
  17. to pascal so they don't need any *.obj files.
  18. The others are interfaces to *.obj files (Windows) or
  19. *.so libraries (Linux).
  20. Default implementation is IMPASZLIB because it can be compiled
  21. by all supported compilers and works on all supported platforms.
  22. I usually use implementation with the fastest decompression
  23. when building release Win32 binaries.
  24. FPCPASZLIB is useful for Lazarus applications. FPC's zlib is linked
  25. to exe by default so there is no need to link additional (and almost identical)
  26. IMPASZLIB.
  27. There is a small speed comparison table of some of the
  28. supported implementations (TGA image 28 311 570 bytes, compression level = 6,
  29. Delphi 9, Win32, Athlon XP 1900).
  30. ZLib version Decompression Compression Comp. Size
  31. IMPASZLIB | 1.1.2 | 824 ms | 4 280 ms | 18 760 133 B
  32. ZLIBEX | 1.2.2 | 710 ms | 1 590 ms* | 19 056 621 B
  33. DELPHIZLIB | 1.0.4 | 976 ms | 9 190 ms | 18 365 562 B
  34. ZLIBPAS | 1.2.3 | 680 ms | 3 790 ms | 18 365 387 B
  35. * obj files are compiled with compression level hardcoded to 1 (fastest)
  36. }
  37. unit dzlib;
  38. interface
  39. {$DEFINE IMPASZLIB}
  40. { $DEFINE FPCPASZLIB}
  41. { $DEFINE ZLIBEX}
  42. { $DEFINE DELPHIZLIB}
  43. { $DEFINE ZLIBPAS}
  44. uses
  45. {$IFDEF IMPASZLIB}
  46. { use paszlib modified by me for Delphi and FPC }
  47. imzdeflate, imzinflate, impaszlib,
  48. {$ENDIF}
  49. {$IFDEF FPCPASZLIB}
  50. { use FPC's paszlib }
  51. zbase, paszlib,
  52. {$ENDIF}
  53. {$IFDEF ZLIBEX}
  54. ZLibEx,
  55. {$ENDIF}
  56. {$IFDEF DELPHIZLIB}
  57. { use ZLib unit shipped with Delphi }
  58. ZLib,
  59. {$ENDIF}
  60. {$IFDEF ZLIBPAS}
  61. { pascal interface to ZLib shipped with ZLib C source }
  62. zlibpas,
  63. {$ENDIF}
  64. SysUtils, Classes;
  65. {$IF Defined(IMPASZLIB) or Defined(FPCPASZLIB) or Defined(ZLIBPAS)}
  66. type
  67. TZStreamRec = z_stream;
  68. {$IFEND}
  69. {$IFDEF ZLIBEX}
  70. const
  71. Z_NO_FLUSH = 0;
  72. Z_PARTIAL_FLUSH = 1;
  73. Z_SYNC_FLUSH = 2;
  74. Z_FULL_FLUSH = 3;
  75. Z_FINISH = 4;
  76. Z_OK = 0;
  77. Z_STREAM_END = 1;
  78. Z_NEED_DICT = 2;
  79. Z_ERRNO = -1;
  80. Z_STREAM_ERROR = -2;
  81. Z_DATA_ERROR = -3;
  82. Z_MEM_ERROR = -4;
  83. Z_BUF_ERROR = -5;
  84. Z_VERSION_ERROR = -6;
  85. Z_NO_COMPRESSION = 0;
  86. Z_BEST_SPEED = 1;
  87. Z_BEST_COMPRESSION = 9;
  88. Z_DEFAULT_COMPRESSION = -1;
  89. Z_FILTERED = 1;
  90. Z_HUFFMAN_ONLY = 2;
  91. Z_RLE = 3;
  92. Z_DEFAULT_STRATEGY = 0;
  93. Z_BINARY = 0;
  94. Z_ASCII = 1;
  95. Z_UNKNOWN = 2;
  96. Z_DEFLATED = 8;
  97. {$ENDIF}
  98. type
  99. { Abstract ancestor class }
  100. TCustomZlibStream = class(TStream)
  101. private
  102. FStrm: TStream;
  103. FStrmPos: Integer;
  104. FOnProgress: TNotifyEvent;
  105. FZRec: TZStreamRec;
  106. FBuffer: array [Word] of Char;
  107. protected
  108. procedure Progress(Sender: TObject); dynamic;
  109. property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;
  110. constructor Create(Strm: TStream);
  111. end;
  112. { TCompressionStream compresses data on the fly as data is written to it, and
  113. stores the compressed data to another stream.
  114. TCompressionStream is write-only and strictly sequential. Reading from the
  115. stream will raise an exception. Using Seek to move the stream pointer
  116. will raise an exception.
  117. Output data is cached internally, written to the output stream only when
  118. the internal output buffer is full. All pending output data is flushed
  119. when the stream is destroyed.
  120. The Position property returns the number of uncompressed bytes of
  121. data that have been written to the stream so far.
  122. CompressionRate returns the on-the-fly percentage by which the original
  123. data has been compressed: (1 - (CompressedBytes / UncompressedBytes)) * 100
  124. If raw data size = 100 and compressed data size = 25, the CompressionRate
  125. is 75%
  126. The OnProgress event is called each time the output buffer is filled and
  127. written to the output stream. This is useful for updating a progress
  128. indicator when you are writing a large chunk of data to the compression
  129. stream in a single call.}
  130. TCompressionLevel = (clNone, clFastest, clDefault, clMax);
  131. TCompressionStream = class(TCustomZlibStream)
  132. private
  133. function GetCompressionRate: Single;
  134. public
  135. constructor Create(CompressionLevel: TCompressionLevel; Dest: TStream);
  136. destructor Destroy; override;
  137. function Read(var Buffer; Count: Longint): Longint; override;
  138. function Write(const Buffer; Count: Longint): Longint; override;
  139. function Seek(Offset: Longint; Origin: Word): Longint; override;
  140. property CompressionRate: Single read GetCompressionRate;
  141. property OnProgress;
  142. end;
  143. { TDecompressionStream decompresses data on the fly as data is read from it.
  144. Compressed data comes from a separate source stream. TDecompressionStream
  145. is read-only and unidirectional; you can seek forward in the stream, but not
  146. backwards. The special case of setting the stream position to zero is
  147. allowed. Seeking forward decompresses data until the requested position in
  148. the uncompressed data has been reached. Seeking backwards, seeking relative
  149. to the end of the stream, requesting the size of the stream, and writing to
  150. the stream will raise an exception.
  151. The Position property returns the number of bytes of uncompressed data that
  152. have been read from the stream so far.
  153. The OnProgress event is called each time the internal input buffer of
  154. compressed data is exhausted and the next block is read from the input stream.
  155. This is useful for updating a progress indicator when you are reading a
  156. large chunk of data from the decompression stream in a single call.}
  157. TDecompressionStream = class(TCustomZlibStream)
  158. public
  159. constructor Create(Source: TStream);
  160. destructor Destroy; override;
  161. function Read(var Buffer; Count: Longint): Longint; override;
  162. function Write(const Buffer; Count: Longint): Longint; override;
  163. function Seek(Offset: Longint; Origin: Word): Longint; override;
  164. property OnProgress;
  165. end;
  166. { CompressBuf compresses data, buffer to buffer, in one call.
  167. In: InBuf = ptr to compressed data
  168. InBytes = number of bytes in InBuf
  169. Out: OutBuf = ptr to newly allocated buffer containing decompressed data
  170. OutBytes = number of bytes in OutBuf }
  171. procedure CompressBuf(const InBuf: Pointer; InBytes: Integer;
  172. var OutBuf: Pointer; var OutBytes: Integer;
  173. CompressLevel: Integer = Z_DEFAULT_COMPRESSION);
  174. { DecompressBuf decompresses data, buffer to buffer, in one call.
  175. In: InBuf = ptr to compressed data
  176. InBytes = number of bytes in InBuf
  177. OutEstimate = zero, or est. size of the decompressed data
  178. Out: OutBuf = ptr to newly allocated buffer containing decompressed data
  179. OutBytes = number of bytes in OutBuf }
  180. procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer;
  181. OutEstimate: Integer; var OutBuf: Pointer; var OutBytes: Integer);
  182. type
  183. EZlibError = class(Exception);
  184. ECompressionError = class(EZlibError);
  185. EDecompressionError = class(EZlibError);
  186. implementation
  187. const
  188. ZErrorMessages: array[0..9] of PChar = (
  189. 'need dictionary', // Z_NEED_DICT (2)
  190. 'stream end', // Z_STREAM_END (1)
  191. '', // Z_OK (0)
  192. 'file error', // Z_ERRNO (-1)
  193. 'stream error', // Z_STREAM_ERROR (-2)
  194. 'data error', // Z_DATA_ERROR (-3)
  195. 'insufficient memory', // Z_MEM_ERROR (-4)
  196. 'buffer error', // Z_BUF_ERROR (-5)
  197. 'incompatible version', // Z_VERSION_ERROR (-6)
  198. '');
  199. function zlibAllocMem(AppData: Pointer; Items, Size: Cardinal): Pointer;
  200. begin
  201. GetMem(Result, Items*Size);
  202. end;
  203. procedure zlibFreeMem(AppData, Block: Pointer);
  204. begin
  205. FreeMem(Block);
  206. end;
  207. function CCheck(code: Integer): Integer;
  208. begin
  209. Result := code;
  210. if code < 0 then
  211. raise ECompressionError.Create('zlib: ' + ZErrorMessages[2 - code]);
  212. end;
  213. function DCheck(code: Integer): Integer;
  214. begin
  215. Result := code;
  216. if code < 0 then
  217. raise EDecompressionError.Create('zlib: ' + ZErrorMessages[2 - code]);
  218. end;
  219. procedure CompressBuf(const InBuf: Pointer; InBytes: Integer;
  220. var OutBuf: Pointer; var OutBytes: Integer;
  221. CompressLevel: Integer);
  222. var
  223. strm: TZStreamRec;
  224. P: Pointer;
  225. begin
  226. FillChar(strm, sizeof(strm), 0);
  227. strm.zalloc := @zlibAllocMem;
  228. strm.zfree := @zlibFreeMem;
  229. OutBytes := ((InBytes + (InBytes div 10) + 12) + 255) and not 255;
  230. GetMem(OutBuf, OutBytes);
  231. try
  232. strm.next_in := InBuf;
  233. strm.avail_in := InBytes;
  234. strm.next_out := OutBuf;
  235. strm.avail_out := OutBytes;
  236. CCheck(deflateInit_(strm, CompressLevel, zlib_version, sizeof(strm)));
  237. try
  238. while CCheck(deflate(strm, Z_FINISH)) <> Z_STREAM_END do
  239. begin
  240. P := OutBuf;
  241. Inc(OutBytes, 256);
  242. ReallocMem(OutBuf, OutBytes);
  243. strm.next_out := Pointer(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P)));
  244. strm.avail_out := 256;
  245. end;
  246. finally
  247. CCheck(deflateEnd(strm));
  248. end;
  249. ReallocMem(OutBuf, strm.total_out);
  250. OutBytes := strm.total_out;
  251. except
  252. zlibFreeMem(nil, OutBuf);
  253. raise
  254. end;
  255. end;
  256. procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer;
  257. OutEstimate: Integer; var OutBuf: Pointer; var OutBytes: Integer);
  258. var
  259. strm: TZStreamRec;
  260. P: Pointer;
  261. BufInc: Integer;
  262. begin
  263. FillChar(strm, sizeof(strm), 0);
  264. strm.zalloc := @zlibAllocMem;
  265. strm.zfree := @zlibFreeMem;
  266. BufInc := (InBytes + 255) and not 255;
  267. if OutEstimate = 0 then
  268. OutBytes := BufInc
  269. else
  270. OutBytes := OutEstimate;
  271. GetMem(OutBuf, OutBytes);
  272. try
  273. strm.next_in := InBuf;
  274. strm.avail_in := InBytes;
  275. strm.next_out := OutBuf;
  276. strm.avail_out := OutBytes;
  277. DCheck(inflateInit_(strm, zlib_version, sizeof(strm)));
  278. try
  279. while DCheck(inflate(strm, Z_NO_FLUSH)) <> Z_STREAM_END do
  280. begin
  281. P := OutBuf;
  282. Inc(OutBytes, BufInc);
  283. ReallocMem(OutBuf, OutBytes);
  284. strm.next_out := Pointer(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P)));
  285. strm.avail_out := BufInc;
  286. end;
  287. finally
  288. DCheck(inflateEnd(strm));
  289. end;
  290. ReallocMem(OutBuf, strm.total_out);
  291. OutBytes := strm.total_out;
  292. except
  293. zlibFreeMem(nil, OutBuf);
  294. raise
  295. end;
  296. end;
  297. { TCustomZlibStream }
  298. constructor TCustomZLibStream.Create(Strm: TStream);
  299. begin
  300. inherited Create;
  301. FStrm := Strm;
  302. FStrmPos := Strm.Position;
  303. FZRec.zalloc := @zlibAllocMem;
  304. FZRec.zfree := @zlibFreeMem;
  305. end;
  306. procedure TCustomZLibStream.Progress(Sender: TObject);
  307. begin
  308. if Assigned(FOnProgress) then FOnProgress(Sender);
  309. end;
  310. { TCompressionStream }
  311. constructor TCompressionStream.Create(CompressionLevel: TCompressionLevel;
  312. Dest: TStream);
  313. const
  314. Levels: array [TCompressionLevel] of ShortInt =
  315. (Z_NO_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION);
  316. begin
  317. inherited Create(Dest);
  318. FZRec.next_out := @FBuffer;
  319. FZRec.avail_out := sizeof(FBuffer);
  320. CCheck(deflateInit_(FZRec, Levels[CompressionLevel], zlib_version, sizeof(FZRec)));
  321. end;
  322. destructor TCompressionStream.Destroy;
  323. begin
  324. FZRec.next_in := nil;
  325. FZRec.avail_in := 0;
  326. try
  327. if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  328. while (CCheck(deflate(FZRec, Z_FINISH)) <> Z_STREAM_END)
  329. and (FZRec.avail_out = 0) do
  330. begin
  331. FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
  332. FZRec.next_out := @FBuffer;
  333. FZRec.avail_out := sizeof(FBuffer);
  334. end;
  335. if FZRec.avail_out < sizeof(FBuffer) then
  336. FStrm.WriteBuffer(FBuffer, sizeof(FBuffer) - FZRec.avail_out);
  337. finally
  338. deflateEnd(FZRec);
  339. end;
  340. inherited Destroy;
  341. end;
  342. function TCompressionStream.Read(var Buffer; Count: Longint): Longint;
  343. begin
  344. raise ECompressionError.Create('Invalid stream operation');
  345. end;
  346. function TCompressionStream.Write(const Buffer; Count: Longint): Longint;
  347. begin
  348. FZRec.next_in := @Buffer;
  349. FZRec.avail_in := Count;
  350. if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  351. while (FZRec.avail_in > 0) do
  352. begin
  353. CCheck(deflate(FZRec, 0));
  354. if FZRec.avail_out = 0 then
  355. begin
  356. FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
  357. FZRec.next_out := @FBuffer;
  358. FZRec.avail_out := sizeof(FBuffer);
  359. FStrmPos := FStrm.Position;
  360. Progress(Self);
  361. end;
  362. end;
  363. Result := Count;
  364. end;
  365. function TCompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
  366. begin
  367. if (Offset = 0) and (Origin = soFromCurrent) then
  368. Result := FZRec.total_in
  369. else
  370. raise ECompressionError.Create('Invalid stream operation');
  371. end;
  372. function TCompressionStream.GetCompressionRate: Single;
  373. begin
  374. if FZRec.total_in = 0 then
  375. Result := 0
  376. else
  377. Result := (1.0 - (FZRec.total_out / FZRec.total_in)) * 100.0;
  378. end;
  379. { TDecompressionStream }
  380. constructor TDecompressionStream.Create(Source: TStream);
  381. begin
  382. inherited Create(Source);
  383. FZRec.next_in := @FBuffer;
  384. FZRec.avail_in := 0;
  385. DCheck(inflateInit_(FZRec, zlib_version, sizeof(FZRec)));
  386. end;
  387. destructor TDecompressionStream.Destroy;
  388. begin
  389. inflateEnd(FZRec);
  390. inherited Destroy;
  391. end;
  392. function TDecompressionStream.Read(var Buffer; Count: Longint): Longint;
  393. begin
  394. FZRec.next_out := @Buffer;
  395. FZRec.avail_out := Count;
  396. if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  397. while (FZRec.avail_out > 0) do
  398. begin
  399. if FZRec.avail_in = 0 then
  400. begin
  401. FZRec.avail_in := FStrm.Read(FBuffer, sizeof(FBuffer));
  402. if FZRec.avail_in = 0 then
  403. begin
  404. Result := Count - Integer(FZRec.avail_out);
  405. Exit;
  406. end;
  407. FZRec.next_in := @FBuffer;
  408. FStrmPos := FStrm.Position;
  409. Progress(Self);
  410. end;
  411. CCheck(inflate(FZRec, 0));
  412. end;
  413. Result := Count;
  414. end;
  415. function TDecompressionStream.Write(const Buffer; Count: Longint): Longint;
  416. begin
  417. raise EDecompressionError.Create('Invalid stream operation');
  418. end;
  419. function TDecompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
  420. var
  421. I: Integer;
  422. Buf: array [0..4095] of Char;
  423. begin
  424. if (Offset = 0) and (Origin = soFromBeginning) then
  425. begin
  426. DCheck(inflateReset(FZRec));
  427. FZRec.next_in := @FBuffer;
  428. FZRec.avail_in := 0;
  429. FStrm.Position := 0;
  430. FStrmPos := 0;
  431. end
  432. else if ( (Offset >= 0) and (Origin = soFromCurrent)) or
  433. ( ((Offset - Integer(FZRec.total_out)) > 0) and (Origin = soFromBeginning)) then
  434. begin
  435. if Origin = soFromBeginning then Dec(Offset, FZRec.total_out);
  436. if Offset > 0 then
  437. begin
  438. for I := 1 to Offset div sizeof(Buf) do
  439. ReadBuffer(Buf, sizeof(Buf));
  440. ReadBuffer(Buf, Offset mod sizeof(Buf));
  441. end;
  442. end
  443. else
  444. raise EDecompressionError.Create('Invalid stream operation');
  445. Result := FZRec.total_out;
  446. end;
  447. end.