Compression.Zlib.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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: Cardinal); 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: Cardinal); 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. begin
  136. if Code = Z_MEM_ERROR then
  137. OutOfMemoryError;
  138. Result := Code;
  139. for var I := Low(ValidCodes) to High(ValidCodes) do
  140. if ValidCodes[I] = Code then
  141. Exit;
  142. raise ECompressInternalError.CreateFmt(SZlibInternalError, [Code]);
  143. end;
  144. procedure InitStream(var strm: TZStreamRec);
  145. begin
  146. FillChar(strm, SizeOf(strm), 0);
  147. with strm do begin
  148. zalloc := zlibAllocMem;
  149. zfree := zlibFreeMem;
  150. end;
  151. end;
  152. { TZCompressor }
  153. constructor TZCompressor.Create(AWriteProc: TCompressorWriteProc;
  154. AProgressProc: TCompressorProgressProc; CompressionLevel: Integer;
  155. ACompressorProps: TCompressorProps);
  156. begin
  157. inherited;
  158. FCompressionLevel := CompressionLevel;
  159. InitCompress;
  160. end;
  161. destructor TZCompressor.Destroy;
  162. begin
  163. EndCompress;
  164. inherited;
  165. end;
  166. procedure TZCompressor.InitCompress;
  167. begin
  168. { Note: This really ought to use the more efficient deflateReset when
  169. starting a new stream, but our DLL doesn't currently export it. }
  170. if not FInitialized then begin
  171. InitStream(FStrm);
  172. FStrm.next_out := @FBuffer;
  173. FStrm.avail_out := SizeOf(FBuffer);
  174. Check(deflateInit_(FStrm, FCompressionLevel, zlib_version, SizeOf(FStrm)), [Z_OK]);
  175. FInitialized := True;
  176. end;
  177. end;
  178. procedure TZCompressor.EndCompress;
  179. begin
  180. if FInitialized then begin
  181. FInitialized := False;
  182. deflateEnd(FStrm);
  183. end;
  184. end;
  185. procedure TZCompressor.FlushBuffer;
  186. begin
  187. if FStrm.avail_out < SizeOf(FBuffer) then begin
  188. WriteProc(FBuffer, SizeOf(FBuffer) - FStrm.avail_out);
  189. FStrm.next_out := @FBuffer;
  190. FStrm.avail_out := SizeOf(FBuffer);
  191. end;
  192. end;
  193. procedure TZCompressor.DoCompress(const Buffer; Count: Cardinal);
  194. begin
  195. InitCompress;
  196. FStrm.next_in := @Buffer;
  197. FStrm.avail_in := Count;
  198. while FStrm.avail_in > 0 do begin
  199. Check(deflate(FStrm, Z_NO_FLUSH), [Z_OK]);
  200. if FStrm.avail_out = 0 then
  201. FlushBuffer;
  202. end;
  203. if Assigned(ProgressProc) then
  204. ProgressProc(Count);
  205. end;
  206. procedure TZCompressor.DoFinish;
  207. begin
  208. InitCompress;
  209. FStrm.next_in := nil;
  210. FStrm.avail_in := 0;
  211. { Note: This assumes FStrm.avail_out > 0. This shouldn't be a problem since
  212. Compress always flushes when FStrm.avail_out reaches 0. }
  213. while Check(deflate(FStrm, Z_FINISH), [Z_OK, Z_STREAM_END]) <> Z_STREAM_END do
  214. FlushBuffer;
  215. FlushBuffer;
  216. EndCompress;
  217. end;
  218. { TZDecompressor }
  219. constructor TZDecompressor.Create(AReadProc: TDecompressorReadProc);
  220. begin
  221. inherited Create(AReadProc);
  222. InitStream(FStrm);
  223. FStrm.next_in := @FBuffer;
  224. FStrm.avail_in := 0;
  225. Check(inflateInit_(FStrm, zlib_version, SizeOf(FStrm)), [Z_OK]);
  226. FInitialized := True;
  227. end;
  228. destructor TZDecompressor.Destroy;
  229. begin
  230. if FInitialized then
  231. inflateEnd(FStrm);
  232. inherited Destroy;
  233. end;
  234. procedure TZDecompressor.DecompressInto(var Buffer; Count: Cardinal);
  235. begin
  236. FStrm.next_out := @Buffer;
  237. FStrm.avail_out := Count;
  238. while FStrm.avail_out > 0 do begin
  239. if FReachedEnd then { unexpected EOF }
  240. raise ECompressDataError.Create(SZlibDataError);
  241. if FStrm.avail_in = 0 then begin
  242. FStrm.next_in := @FBuffer;
  243. FStrm.avail_in := ReadProc(FBuffer, SizeOf(FBuffer));
  244. { Note: If avail_in is zero while zlib still needs input, inflate() will
  245. return Z_BUF_ERROR. We interpret that as a data error (see below). }
  246. end;
  247. case Check(inflate(FStrm, Z_NO_FLUSH), [Z_OK, Z_STREAM_END, Z_DATA_ERROR, Z_BUF_ERROR]) of
  248. Z_STREAM_END: FReachedEnd := True;
  249. Z_DATA_ERROR, Z_BUF_ERROR: raise ECompressDataError.Create(SZlibDataError);
  250. end;
  251. end;
  252. end;
  253. procedure TZDecompressor.Reset;
  254. begin
  255. FStrm.next_in := @FBuffer;
  256. FStrm.avail_in := 0;
  257. Check(inflateReset(FStrm), [Z_OK]);
  258. FReachedEnd := False;
  259. end;
  260. end.