Compression.Zlib.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. unit Compression.Zlib;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2010 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Declarations for zlib functions & structures
  8. }
  9. interface
  10. uses
  11. Windows, SysUtils, Compression.Base;
  12. function ZlibInitCompressFunctions(Module: HMODULE): Boolean;
  13. function ZlibInitDecompressFunctions(Module: HMODULE): Boolean;
  14. type
  15. TZAlloc = function(AppData: Pointer; Items, Size: Cardinal): Pointer; stdcall;
  16. TZFree = procedure(AppData, Block: Pointer); stdcall;
  17. TZStreamRec = packed record
  18. next_in: Pointer; { next input byte }
  19. avail_in: Cardinal; { number of bytes available at next_in }
  20. total_in: Cardinal; { total nb of input bytes read so far }
  21. next_out: Pointer; { next output byte should be put here }
  22. avail_out: Cardinal; { remaining free space at next_out }
  23. total_out: Cardinal; { total nb of bytes output so far }
  24. msg: PAnsiChar; { last error message, NULL if no error }
  25. internal: Pointer; { not visible by applications }
  26. zalloc: TZAlloc; { used to allocate the internal state }
  27. zfree: TZFree; { used to free the internal state }
  28. AppData: Pointer; { private data object passed to zalloc and zfree }
  29. data_type: Integer; { best guess about the data type: ascii or binary }
  30. adler: Longint; { adler32 value of the uncompressed data }
  31. reserved: Longint; { reserved for future use }
  32. end;
  33. TZCompressor = class(TCustomCompressor)
  34. private
  35. FCompressionLevel: Integer;
  36. FInitialized: Boolean;
  37. FStrm: TZStreamRec;
  38. FBuffer: array[0..65535] of Byte;
  39. procedure EndCompress;
  40. procedure FlushBuffer;
  41. procedure InitCompress;
  42. protected
  43. procedure DoCompress(const Buffer; Count: Longint); override;
  44. procedure DoFinish; override;
  45. public
  46. constructor Create(AWriteProc: TCompressorWriteProc;
  47. AProgressProc: TCompressorProgressProc; CompressionLevel: Integer;
  48. ACompressorProps: TCompressorProps); override;
  49. destructor Destroy; override;
  50. end;
  51. TZDecompressor = class(TCustomDecompressor)
  52. private
  53. FInitialized: Boolean;
  54. FStrm: TZStreamRec;
  55. FReachedEnd: Boolean;
  56. FBuffer: array[0..65535] of Byte;
  57. public
  58. constructor Create(AReadProc: TDecompressorReadProc); override;
  59. destructor Destroy; override;
  60. procedure DecompressInto(var Buffer; Count: Longint); override;
  61. procedure Reset; override;
  62. end;
  63. implementation
  64. const
  65. SZlibDataError = 'zlib: Compressed data is corrupted';
  66. SZlibInternalError = 'zlib: Internal error. Code %d';
  67. ZLIB_VERSION = '1.2.1'; { Do not change this! }
  68. Z_NO_FLUSH = 0;
  69. Z_PARTIAL_FLUSH = 1;
  70. Z_SYNC_FLUSH = 2;
  71. Z_FULL_FLUSH = 3;
  72. Z_FINISH = 4;
  73. Z_OK = 0;
  74. Z_STREAM_END = 1;
  75. Z_NEED_DICT = 2;
  76. Z_ERRNO = -1;
  77. Z_STREAM_ERROR = -2;
  78. Z_DATA_ERROR = -3;
  79. Z_MEM_ERROR = -4;
  80. Z_BUF_ERROR = -5;
  81. Z_VERSION_ERROR = -6;
  82. var
  83. deflateInit_: function(var strm: TZStreamRec; level: Integer; version: PAnsiChar;
  84. stream_size: Integer): Integer; stdcall;
  85. deflate: function(var strm: TZStreamRec; flush: Integer): Integer; stdcall;
  86. deflateEnd: function(var strm: TZStreamRec): Integer; stdcall;
  87. inflateInit_: function(var strm: TZStreamRec; version: PAnsiChar;
  88. stream_size: Integer): Integer; stdcall;
  89. inflate: function(var strm: TZStreamRec; flush: Integer): Integer; stdcall;
  90. inflateEnd: function(var strm: TZStreamRec): Integer; stdcall;
  91. inflateReset: function(var strm: TZStreamRec): Integer; stdcall;
  92. function ZlibInitCompressFunctions(Module: HMODULE): Boolean;
  93. begin
  94. deflateInit_ := GetProcAddress(Module, 'deflateInit_');
  95. deflate := GetProcAddress(Module, 'deflate');
  96. deflateEnd := GetProcAddress(Module, 'deflateEnd');
  97. Result := Assigned(deflateInit_) and Assigned(deflate) and
  98. Assigned(deflateEnd);
  99. if not Result then begin
  100. deflateInit_ := nil;
  101. deflate := nil;
  102. deflateEnd := nil;
  103. end;
  104. end;
  105. function ZlibInitDecompressFunctions(Module: HMODULE): Boolean;
  106. begin
  107. inflateInit_ := GetProcAddress(Module, 'inflateInit_');
  108. inflate := GetProcAddress(Module, 'inflate');
  109. inflateEnd := GetProcAddress(Module, 'inflateEnd');
  110. inflateReset := GetProcAddress(Module, 'inflateReset');
  111. Result := Assigned(inflateInit_) and Assigned(inflate) and
  112. Assigned(inflateEnd) and Assigned(inflateReset);
  113. if not Result then begin
  114. inflateInit_ := nil;
  115. inflate := nil;
  116. inflateEnd := nil;
  117. inflateReset := nil;
  118. end;
  119. end;
  120. function zlibAllocMem(AppData: Pointer; Items, Size: Cardinal): Pointer; stdcall;
  121. begin
  122. try
  123. GetMem(Result, Items * Size);
  124. except
  125. { trap any exception, because zlib expects a NULL result if it's out
  126. of memory }
  127. Result := nil;
  128. end;
  129. end;
  130. procedure zlibFreeMem(AppData, Block: Pointer); stdcall;
  131. begin
  132. FreeMem(Block);
  133. end;
  134. function Check(const Code: Integer; const ValidCodes: array of Integer): Integer;
  135. var
  136. I: Integer;
  137. begin
  138. if Code = Z_MEM_ERROR then
  139. OutOfMemoryError;
  140. Result := Code;
  141. for I := Low(ValidCodes) to High(ValidCodes) do
  142. if ValidCodes[I] = Code then
  143. Exit;
  144. raise ECompressInternalError.CreateFmt(SZlibInternalError, [Code]);
  145. end;
  146. procedure InitStream(var strm: TZStreamRec);
  147. begin
  148. FillChar(strm, SizeOf(strm), 0);
  149. with strm do begin
  150. zalloc := zlibAllocMem;
  151. zfree := zlibFreeMem;
  152. end;
  153. end;
  154. { TZCompressor }
  155. constructor TZCompressor.Create(AWriteProc: TCompressorWriteProc;
  156. AProgressProc: TCompressorProgressProc; CompressionLevel: Integer;
  157. ACompressorProps: TCompressorProps);
  158. begin
  159. inherited;
  160. FCompressionLevel := CompressionLevel;
  161. InitCompress;
  162. end;
  163. destructor TZCompressor.Destroy;
  164. begin
  165. EndCompress;
  166. inherited;
  167. end;
  168. procedure TZCompressor.InitCompress;
  169. begin
  170. { Note: This really ought to use the more efficient deflateReset when
  171. starting a new stream, but our DLL doesn't currently export it. }
  172. if not FInitialized then begin
  173. InitStream(FStrm);
  174. FStrm.next_out := @FBuffer;
  175. FStrm.avail_out := SizeOf(FBuffer);
  176. Check(deflateInit_(FStrm, FCompressionLevel, zlib_version, SizeOf(FStrm)), [Z_OK]);
  177. FInitialized := True;
  178. end;
  179. end;
  180. procedure TZCompressor.EndCompress;
  181. begin
  182. if FInitialized then begin
  183. FInitialized := False;
  184. deflateEnd(FStrm);
  185. end;
  186. end;
  187. procedure TZCompressor.FlushBuffer;
  188. begin
  189. if FStrm.avail_out < SizeOf(FBuffer) then begin
  190. WriteProc(FBuffer, SizeOf(FBuffer) - FStrm.avail_out);
  191. FStrm.next_out := @FBuffer;
  192. FStrm.avail_out := SizeOf(FBuffer);
  193. end;
  194. end;
  195. procedure TZCompressor.DoCompress(const Buffer; Count: Longint);
  196. begin
  197. InitCompress;
  198. FStrm.next_in := @Buffer;
  199. FStrm.avail_in := Count;
  200. while FStrm.avail_in > 0 do begin
  201. Check(deflate(FStrm, Z_NO_FLUSH), [Z_OK]);
  202. if FStrm.avail_out = 0 then
  203. FlushBuffer;
  204. end;
  205. if Assigned(ProgressProc) then
  206. ProgressProc(Count);
  207. end;
  208. procedure TZCompressor.DoFinish;
  209. begin
  210. InitCompress;
  211. FStrm.next_in := nil;
  212. FStrm.avail_in := 0;
  213. { Note: This assumes FStrm.avail_out > 0. This shouldn't be a problem since
  214. Compress always flushes when FStrm.avail_out reaches 0. }
  215. while Check(deflate(FStrm, Z_FINISH), [Z_OK, Z_STREAM_END]) <> Z_STREAM_END do
  216. FlushBuffer;
  217. FlushBuffer;
  218. EndCompress;
  219. end;
  220. { TZDecompressor }
  221. constructor TZDecompressor.Create(AReadProc: TDecompressorReadProc);
  222. begin
  223. inherited Create(AReadProc);
  224. InitStream(FStrm);
  225. FStrm.next_in := @FBuffer;
  226. FStrm.avail_in := 0;
  227. Check(inflateInit_(FStrm, zlib_version, SizeOf(FStrm)), [Z_OK]);
  228. FInitialized := True;
  229. end;
  230. destructor TZDecompressor.Destroy;
  231. begin
  232. if FInitialized then
  233. inflateEnd(FStrm);
  234. inherited Destroy;
  235. end;
  236. procedure TZDecompressor.DecompressInto(var Buffer; Count: Longint);
  237. begin
  238. FStrm.next_out := @Buffer;
  239. FStrm.avail_out := Count;
  240. while FStrm.avail_out > 0 do begin
  241. if FReachedEnd then { unexpected EOF }
  242. raise ECompressDataError.Create(SZlibDataError);
  243. if FStrm.avail_in = 0 then begin
  244. FStrm.next_in := @FBuffer;
  245. FStrm.avail_in := ReadProc(FBuffer, SizeOf(FBuffer));
  246. { Note: If avail_in is zero while zlib still needs input, inflate() will
  247. return Z_BUF_ERROR. We interpret that as a data error (see below). }
  248. end;
  249. case Check(inflate(FStrm, Z_NO_FLUSH), [Z_OK, Z_STREAM_END, Z_DATA_ERROR, Z_BUF_ERROR]) of
  250. Z_STREAM_END: FReachedEnd := True;
  251. Z_DATA_ERROR, Z_BUF_ERROR: raise ECompressDataError.Create(SZlibDataError);
  252. end;
  253. end;
  254. end;
  255. procedure TZDecompressor.Reset;
  256. begin
  257. FStrm.next_in := @FBuffer;
  258. FStrm.avail_in := 0;
  259. Check(inflateReset(FStrm), [Z_OK]);
  260. FReachedEnd := False;
  261. end;
  262. end.