dzlib.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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
  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. FPCPASZLIB is useful for Lazarus applications. FPC's zlib is linked
  23. to exe by default so there is no need to link additional (and almost identical)
  24. IMPASZLIB.
  25. There is a small speed comparison table of some of the
  26. supported implementations (TGA image 28 311 570 bytes, compression level = 6,
  27. Delphi 9, Win32, Athlon XP 1900).
  28. ZLib version Decompression Compression Comp. Size
  29. IMPASZLIB | 1.1.2 | 824 ms | 4 280 ms | 18 760 133 B
  30. ZLIBEX | 1.2.2 | 710 ms | 1 590 ms* | 19 056 621 B
  31. DELPHIZLIB | 1.0.4 | 976 ms | 9 190 ms | 18 365 562 B
  32. ZLIBPAS | 1.2.3 | 680 ms | 3 790 ms | 18 365 387 B
  33. * obj files are compiled with compression level hardcoded to 1 (fastest)
  34. }
  35. unit dzlib;
  36. {$I ImagingOptions.inc}
  37. interface
  38. {$DEFINE IMPASZLIB}
  39. { $DEFINE ZLIBPAS}
  40. { $DEFINE FPCPASZLIB}
  41. { $DEFINE ZLIBEX}
  42. { $DEFINE DELPHIZLIB}
  43. { Automatically use FPC's PasZLib when compiling with FPC.}
  44. {$IFDEF FPC}
  45. {$UNDEF IMPASZLIB}
  46. {$DEFINE FPCPASZLIB}
  47. {$ENDIF}
  48. uses
  49. {$IF Defined(IMPASZLIB)}
  50. { Use paszlib modified by me for Delphi and FPC }
  51. imzdeflate, imzinflate, impaszlib,
  52. {$ELSEIF Defined(FPCPASZLIB)}
  53. { Use FPC's paszlib }
  54. zbase, paszlib,
  55. {$ELSEIF Defined(ZLIBPAS)}
  56. { Pascal interface to ZLib shipped with ZLib C source }
  57. zlibpas,
  58. {$ELSEIF Defined(ZLIBEX)}
  59. { Use ZlibEx unit }
  60. ZLibEx,
  61. {$ELSEIF Defined(DELPHIZLIB)}
  62. { Use ZLib unit shipped with Delphi }
  63. ZLib,
  64. {$IFEND}
  65. ImagingTypes, SysUtils, Classes;
  66. {$IF Defined(IMPASZLIB) or Defined(FPCPASZLIB) or Defined(ZLIBPAS)}
  67. type
  68. TZStreamRec = z_stream;
  69. {$IFEND}
  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. type
  98. { Abstract ancestor class }
  99. TCustomZlibStream = class(TStream)
  100. private
  101. FStrm: TStream;
  102. FStrmPos: Integer;
  103. FOnProgress: TNotifyEvent;
  104. FZRec: TZStreamRec;
  105. FBuffer: array [Word] of Byte;
  106. protected
  107. procedure Progress(Sender: TObject); dynamic;
  108. property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;
  109. constructor Create(Strm: TStream);
  110. end;
  111. { TCompressionStream compresses data on the fly as data is written to it, and
  112. stores the compressed data to another stream.
  113. TCompressionStream is write-only and strictly sequential. Reading from the
  114. stream will raise an exception. Using Seek to move the stream pointer
  115. will raise an exception.
  116. Output data is cached internally, written to the output stream only when
  117. the internal output buffer is full. All pending output data is flushed
  118. when the stream is destroyed.
  119. The Position property returns the number of uncompressed bytes of
  120. data that have been written to the stream so far.
  121. CompressionRate returns the on-the-fly percentage by which the original
  122. data has been compressed: (1 - (CompressedBytes / UncompressedBytes)) * 100
  123. If raw data size = 100 and compressed data size = 25, the CompressionRate
  124. is 75%
  125. The OnProgress event is called each time the output buffer is filled and
  126. written to the output stream. This is useful for updating a progress
  127. indicator when you are writing a large chunk of data to the compression
  128. stream in a single call.}
  129. TCompressionLevel = (clNone, clFastest, clDefault, clMax);
  130. TCompressionStream = class(TCustomZlibStream)
  131. private
  132. function GetCompressionRate: Single;
  133. public
  134. constructor Create(CompressionLevel: TCompressionLevel; Dest: TStream);
  135. destructor Destroy; override;
  136. function Read(var Buffer; Count: Longint): Longint; override;
  137. function Write(const Buffer; Count: Longint): Longint; override;
  138. function Seek(Offset: Longint; Origin: Word): Longint; override;
  139. property CompressionRate: Single read GetCompressionRate;
  140. property OnProgress;
  141. end;
  142. { TDecompressionStream decompresses data on the fly as data is read from it.
  143. Compressed data comes from a separate source stream. TDecompressionStream
  144. is read-only and unidirectional; you can seek forward in the stream, but not
  145. backwards. The special case of setting the stream position to zero is
  146. allowed. Seeking forward decompresses data until the requested position in
  147. the uncompressed data has been reached. Seeking backwards, seeking relative
  148. to the end of the stream, requesting the size of the stream, and writing to
  149. the stream will raise an exception.
  150. The Position property returns the number of bytes of uncompressed data that
  151. have been read from the stream so far.
  152. The OnProgress event is called each time the internal input buffer of
  153. compressed data is exhausted and the next block is read from the input stream.
  154. This is useful for updating a progress indicator when you are reading a
  155. large chunk of data from the decompression stream in a single call.}
  156. TDecompressionStream = class(TCustomZlibStream)
  157. public
  158. constructor Create(Source: TStream);
  159. destructor Destroy; override;
  160. function Read(var Buffer; Count: Longint): Longint; override;
  161. function Write(const Buffer; Count: Longint): Longint; override;
  162. function Seek(Offset: Longint; Origin: Word): Longint; override;
  163. property OnProgress;
  164. end;
  165. { CompressBuf compresses data, buffer to buffer, in one call.
  166. In: InBuf = ptr to compressed data
  167. InBytes = number of bytes in InBuf
  168. Out: OutBuf = ptr to newly allocated buffer containing decompressed data
  169. OutBytes = number of bytes in OutBuf }
  170. procedure CompressBuf(const InBuf: Pointer; InBytes: Integer;
  171. var OutBuf: Pointer; var OutBytes: Integer;
  172. CompressLevel: Integer = Z_DEFAULT_COMPRESSION;
  173. CompressStrategy: Integer = Z_DEFAULT_STRATEGY);
  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 PAnsiChar = (
  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, CompressStrategy: Integer);
  222. var
  223. strm: TZStreamRec;
  224. P: Pointer;
  225. begin
  226. FillChar(strm, sizeof(strm), 0);
  227. {$IFNDEF FPCPASZLIB}
  228. strm.zalloc := @zlibAllocMem;
  229. strm.zfree := @zlibFreeMem;
  230. {$ENDIF}
  231. OutBytes := ((InBytes + (InBytes div 10) + 12) + 255) and not 255;
  232. GetMem(OutBuf, OutBytes);
  233. try
  234. strm.next_in := InBuf;
  235. strm.avail_in := InBytes;
  236. strm.next_out := OutBuf;
  237. strm.avail_out := OutBytes;
  238. CCheck(deflateInit2(strm, CompressLevel, Z_DEFLATED, MAX_WBITS,
  239. DEF_MEM_LEVEL, CompressStrategy));
  240. try
  241. while CCheck(deflate(strm, Z_FINISH)) <> Z_STREAM_END do
  242. begin
  243. P := OutBuf;
  244. Inc(OutBytes, 256);
  245. ReallocMem(OutBuf, OutBytes);
  246. strm.next_out := Pointer(PtrUInt(OutBuf) + (PtrUInt(strm.next_out) - PtrUInt(P)));
  247. strm.avail_out := 256;
  248. end;
  249. finally
  250. CCheck(deflateEnd(strm));
  251. end;
  252. ReallocMem(OutBuf, strm.total_out);
  253. OutBytes := strm.total_out;
  254. except
  255. zlibFreeMem(nil, OutBuf);
  256. raise
  257. end;
  258. end;
  259. procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer;
  260. OutEstimate: Integer; var OutBuf: Pointer; var OutBytes: Integer);
  261. var
  262. strm: TZStreamRec;
  263. P: Pointer;
  264. BufInc: Integer;
  265. begin
  266. FillChar(strm, sizeof(strm), 0);
  267. {$IFNDEF FPCPASZLIB}
  268. strm.zalloc := @zlibAllocMem;
  269. strm.zfree := @zlibFreeMem;
  270. {$ENDIF}
  271. BufInc := (InBytes + 255) and not 255;
  272. if OutEstimate = 0 then
  273. OutBytes := BufInc
  274. else
  275. OutBytes := OutEstimate;
  276. GetMem(OutBuf, OutBytes);
  277. try
  278. strm.next_in := InBuf;
  279. strm.avail_in := InBytes;
  280. strm.next_out := OutBuf;
  281. strm.avail_out := OutBytes;
  282. DCheck(inflateInit_(strm, zlib_version, sizeof(strm)));
  283. try
  284. while DCheck(inflate(strm, Z_NO_FLUSH)) <> Z_STREAM_END do
  285. begin
  286. P := OutBuf;
  287. Inc(OutBytes, BufInc);
  288. ReallocMem(OutBuf, OutBytes);
  289. strm.next_out := Pointer(PtrUInt(OutBuf) + (PtrUInt(strm.next_out) - PtrUInt(P)));
  290. strm.avail_out := BufInc;
  291. end;
  292. finally
  293. DCheck(inflateEnd(strm));
  294. end;
  295. ReallocMem(OutBuf, strm.total_out);
  296. OutBytes := strm.total_out;
  297. except
  298. zlibFreeMem(nil, OutBuf);
  299. raise
  300. end;
  301. end;
  302. { TCustomZlibStream }
  303. constructor TCustomZLibStream.Create(Strm: TStream);
  304. begin
  305. inherited Create;
  306. FStrm := Strm;
  307. FStrmPos := Strm.Position;
  308. {$IFNDEF FPCPASZLIB}
  309. FZRec.zalloc := @zlibAllocMem;
  310. FZRec.zfree := @zlibFreeMem;
  311. {$ENDIF}
  312. end;
  313. procedure TCustomZLibStream.Progress(Sender: TObject);
  314. begin
  315. if Assigned(FOnProgress) then FOnProgress(Sender);
  316. end;
  317. { TCompressionStream }
  318. constructor TCompressionStream.Create(CompressionLevel: TCompressionLevel;
  319. Dest: TStream);
  320. const
  321. Levels: array [TCompressionLevel] of ShortInt =
  322. (Z_NO_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION);
  323. begin
  324. inherited Create(Dest);
  325. FZRec.next_out := @FBuffer;
  326. FZRec.avail_out := sizeof(FBuffer);
  327. CCheck(deflateInit_(FZRec, Levels[CompressionLevel], zlib_version, sizeof(FZRec)));
  328. end;
  329. destructor TCompressionStream.Destroy;
  330. begin
  331. FZRec.next_in := nil;
  332. FZRec.avail_in := 0;
  333. try
  334. if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  335. while (CCheck(deflate(FZRec, Z_FINISH)) <> Z_STREAM_END)
  336. and (FZRec.avail_out = 0) do
  337. begin
  338. FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
  339. FZRec.next_out := @FBuffer;
  340. FZRec.avail_out := sizeof(FBuffer);
  341. end;
  342. if FZRec.avail_out < sizeof(FBuffer) then
  343. FStrm.WriteBuffer(FBuffer, sizeof(FBuffer) - FZRec.avail_out);
  344. finally
  345. deflateEnd(FZRec);
  346. end;
  347. inherited Destroy;
  348. end;
  349. function TCompressionStream.Read(var Buffer; Count: Longint): Longint;
  350. begin
  351. Result := 0;
  352. raise ECompressionError.Create('Invalid stream operation');
  353. end;
  354. function TCompressionStream.Write(const Buffer; Count: Longint): Longint;
  355. begin
  356. FZRec.next_in := @Buffer;
  357. FZRec.avail_in := Count;
  358. if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  359. while (FZRec.avail_in > 0) do
  360. begin
  361. CCheck(deflate(FZRec, 0));
  362. if FZRec.avail_out = 0 then
  363. begin
  364. FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
  365. FZRec.next_out := @FBuffer;
  366. FZRec.avail_out := sizeof(FBuffer);
  367. FStrmPos := FStrm.Position;
  368. Progress(Self);
  369. end;
  370. end;
  371. Result := Count;
  372. end;
  373. function TCompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
  374. begin
  375. if (Offset = 0) and (Origin = soFromCurrent) then
  376. Result := FZRec.total_in
  377. else
  378. raise ECompressionError.Create('Invalid stream operation');
  379. end;
  380. function TCompressionStream.GetCompressionRate: Single;
  381. begin
  382. if FZRec.total_in = 0 then
  383. Result := 0
  384. else
  385. Result := (1.0 - (FZRec.total_out / FZRec.total_in)) * 100.0;
  386. end;
  387. { TDecompressionStream }
  388. constructor TDecompressionStream.Create(Source: TStream);
  389. begin
  390. inherited Create(Source);
  391. FZRec.next_in := @FBuffer;
  392. FZRec.avail_in := 0;
  393. DCheck(inflateInit_(FZRec, zlib_version, sizeof(FZRec)));
  394. end;
  395. destructor TDecompressionStream.Destroy;
  396. begin
  397. inflateEnd(FZRec);
  398. inherited Destroy;
  399. end;
  400. function TDecompressionStream.Read(var Buffer; Count: Longint): Longint;
  401. begin
  402. FZRec.next_out := @Buffer;
  403. FZRec.avail_out := Count;
  404. if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  405. while (FZRec.avail_out > 0) do
  406. begin
  407. if FZRec.avail_in = 0 then
  408. begin
  409. FZRec.avail_in := FStrm.Read(FBuffer, sizeof(FBuffer));
  410. if FZRec.avail_in = 0 then
  411. begin
  412. Result := Count - Integer(FZRec.avail_out);
  413. Exit;
  414. end;
  415. FZRec.next_in := @FBuffer;
  416. FStrmPos := FStrm.Position;
  417. Progress(Self);
  418. end;
  419. CCheck(inflate(FZRec, 0));
  420. end;
  421. Result := Count;
  422. end;
  423. function TDecompressionStream.Write(const Buffer; Count: Longint): Longint;
  424. begin
  425. Result := 0;
  426. raise EDecompressionError.Create('Invalid stream operation');
  427. end;
  428. function TDecompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
  429. var
  430. I: Integer;
  431. Buf: array [0..4095] of Byte;
  432. begin
  433. if (Offset = 0) and (Origin = soFromBeginning) then
  434. begin
  435. DCheck(inflateReset(FZRec));
  436. FZRec.next_in := @FBuffer;
  437. FZRec.avail_in := 0;
  438. FStrm.Position := 0;
  439. FStrmPos := 0;
  440. end
  441. else if ( (Offset >= 0) and (Origin = soFromCurrent)) or
  442. ( ((Offset - Integer(FZRec.total_out)) > 0) and (Origin = soFromBeginning)) then
  443. begin
  444. if Origin = soFromBeginning then Dec(Offset, FZRec.total_out);
  445. if Offset > 0 then
  446. begin
  447. for I := 1 to Offset div sizeof(Buf) do
  448. ReadBuffer(Buf, sizeof(Buf));
  449. ReadBuffer(Buf, Offset mod sizeof(Buf));
  450. end;
  451. end
  452. else
  453. raise EDecompressionError.Create('Invalid stream operation');
  454. Result := FZRec.total_out;
  455. end;
  456. end.