Compression.LZMADecompressor.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. unit Compression.LZMADecompressor;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Interface to the LZMA/LZMA2 SDK decompression OBJs in
  8. Compression.LZMADecompressor\Lzma2Decode, used by Setup.
  9. }
  10. interface
  11. uses
  12. Windows, SysUtils, Compression.Base;
  13. type
  14. TLZMACustomDecompressor = class(TCustomDecompressor)
  15. private
  16. FReachedEnd: Boolean;
  17. FHeaderProcessed: Boolean;
  18. FNextIn: PByte;
  19. FAvailIn: Cardinal;
  20. FBuffer: array[0..65535] of Byte;
  21. protected
  22. function DecodeToBuf(var Dest; var DestLen: Cardinal; const Src;
  23. var SrcLen: Cardinal; var Status: Integer): Integer; virtual; abstract;
  24. procedure FreeDecoder; virtual; abstract;
  25. procedure ProcessHeader; virtual; abstract;
  26. public
  27. destructor Destroy; override;
  28. procedure DecompressInto(var Buffer; Count: Cardinal); override;
  29. procedure Reset; override;
  30. end;
  31. const
  32. {$IFNDEF WIN64}
  33. TLZMA1StateSize = 100;
  34. TLZMA2StateSize = 116;
  35. {$ELSE}
  36. TLZMA1StateSize = 128;
  37. TLZMA2StateSize = 144;
  38. {$ENDIF}
  39. type
  40. { Internally-used records }
  41. TLZMA1InternalDecoderState = array[0..TLZMA1StateSize div SizeOf(UInt32) - 1] of UInt32;
  42. TLZMA2InternalDecoderState = array[0..TLZMA2StateSize div SizeOf(UInt32) - 1] of UInt32;
  43. TLZMA1Decompressor = class(TLZMACustomDecompressor)
  44. private
  45. FDecoderState: TLZMA1InternalDecoderState;
  46. protected
  47. function DecodeToBuf(var Dest; var DestLen: Cardinal; const Src;
  48. var SrcLen: Cardinal; var Status: Integer): Integer; override;
  49. procedure FreeDecoder; override;
  50. procedure ProcessHeader; override;
  51. end;
  52. TLZMA2Decompressor = class(TLZMACustomDecompressor)
  53. private
  54. FDecoderState: TLZMA2InternalDecoderState;
  55. protected
  56. function DecodeToBuf(var Dest; var DestLen: Cardinal; const Src;
  57. var SrcLen: Cardinal; var Status: Integer): Integer; override;
  58. procedure FreeDecoder; override;
  59. procedure ProcessHeader; override;
  60. end;
  61. implementation
  62. uses
  63. UnsignedFunc;
  64. type
  65. TLZMASRes = type Integer;
  66. PLZMAISzAlloc = ^TLZMAISzAlloc;
  67. TLZMAISzAlloc = record
  68. Alloc: function(p: PLZMAISzAlloc; size: NativeUInt): Pointer; cdecl;
  69. Free: procedure(p: PLZMAISzAlloc; address: Pointer); cdecl;
  70. end;
  71. const
  72. { SRes (TLZMASRes) }
  73. SZ_OK = 0;
  74. SZ_ERROR_DATA = 1;
  75. SZ_ERROR_MEM = 2;
  76. { ELzmaFinishMode }
  77. LZMA_FINISH_ANY = 0;
  78. LZMA_FINISH_END = 1;
  79. { ELzmaStatus }
  80. LZMA_STATUS_NOT_SPECIFIED = 0;
  81. LZMA_STATUS_FINISHED_WITH_MARK = 1;
  82. LZMA_STATUS_NOT_FINISHED = 2;
  83. LZMA_STATUS_NEEDS_MORE_INPUT = 3;
  84. LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK = 4;
  85. SLZMADecompDataError = 'lzmadecomp: Compressed data is corrupted (%d)';
  86. { To ensure we don't allocate inordinate amounts of memory in the event a
  87. stream's header is corrupted, we limit the dictionary size to the maximum
  88. size the compiler currently allows. }
  89. MaxDictionarySize = 1024 shl 20; { 1 GB - same as ssLZMADictionarySize allows in Compile.pas }
  90. { Compiled by Visual Studio 2022 using compile.bat }
  91. {$IFNDEF WIN64}
  92. { To enable source debugging recompile using compile-bcc32c.bat
  93. Note that in a speed test the code produced by bcc32c was about 33% slower }
  94. {$L Src\Compression.LZMADecompressor\Lzma2Decode\ISLzmaDec-x86.obj}
  95. {$ELSE}
  96. {$L Src\Compression.LZMADecompressor\Lzma2Decode\ISLzmaDec-x64.obj}
  97. {$ENDIF}
  98. function IS_LzmaDec_Init(var state: TLZMA1InternalDecoderState;
  99. stateSize: NativeUInt; const props; propsSize: Cardinal;
  100. const alloc: TLZMAISzAlloc): TLZMASRes; cdecl; external name {$IFNDEF WIN64} '_IS_LzmaDec_Init' {$ELSE} 'IS_LzmaDec_Init'{$ENDIF};
  101. function IS_LzmaDec_StateSize: NativeUInt; cdecl; external name {$IFNDEF WIN64} '_IS_LzmaDec_StateSize' {$ELSE} 'IS_LzmaDec_StateSize'{$ENDIF};
  102. function LzmaDec_DecodeToBuf(var state: TLZMA1InternalDecoderState; var dest;
  103. var destLen: NativeUInt; const src; var srcLen: NativeUInt; finishMode: Integer;
  104. var status: Integer): TLZMASRes; cdecl; external name {$IFNDEF WIN64} '_LzmaDec_DecodeToBuf' {$ELSE} 'LzmaDec_DecodeToBuf'{$ENDIF};
  105. procedure LzmaDec_Free(var state: TLZMA1InternalDecoderState;
  106. const alloc: TLZMAISzAlloc); cdecl; external name {$IFNDEF WIN64} '_LzmaDec_Free' {$ELSE} 'LzmaDec_Free'{$ENDIF};
  107. function IS_Lzma2Dec_Init(var state: TLZMA2InternalDecoderState;
  108. stateSize: NativeUInt; prop: Byte; const alloc: TLZMAISzAlloc): TLZMASRes; cdecl;
  109. external name {$IFNDEF WIN64} '_IS_Lzma2Dec_Init' {$ELSE} 'IS_Lzma2Dec_Init'{$ENDIF};
  110. function IS_Lzma2Dec_StateSize: NativeUInt; cdecl; external name {$IFNDEF WIN64} '_IS_Lzma2Dec_StateSize' {$ELSE} 'IS_Lzma2Dec_StateSize'{$ENDIF};
  111. function Lzma2Dec_DecodeToBuf(var state: TLZMA2InternalDecoderState; var dest;
  112. var destLen: NativeUInt; const src; var srcLen: NativeUInt; finishMode: Integer;
  113. var status: Integer): TLZMASRes; cdecl; external name {$IFNDEF WIN64} '_Lzma2Dec_DecodeToBuf' {$ELSE} 'Lzma2Dec_DecodeToBuf'{$ENDIF};
  114. procedure IS_Lzma2Dec_Free(var state: TLZMA2InternalDecoderState;
  115. const alloc: TLZMAISzAlloc); cdecl; external name {$IFNDEF WIN64} '_IS_Lzma2Dec_Free' {$ELSE} 'IS_Lzma2Dec_Free'{$ENDIF};
  116. function {$IFNDEF WIN64} _memcpy {$ELSE} memcpy {$ENDIF}(dest, src: Pointer; n: NativeUInt): Pointer; cdecl;
  117. begin
  118. UMove(src^, dest^, n);
  119. Result := dest;
  120. end;
  121. procedure LZMADecompInternalError(const Msg: String);
  122. begin
  123. raise ECompressInternalError.CreateFmt('lzmadecomp: %s', [Msg]);
  124. end;
  125. procedure LZMADecompDataError(const Id: Integer);
  126. begin
  127. raise ECompressDataError.CreateFmt(SLZMADecompDataError, [Id]);
  128. end;
  129. function LZMAAllocFunc(p: PLZMAISzAlloc; size: NativeUInt): Pointer; cdecl;
  130. begin
  131. if (size <> 0) and (size <= MaxDictionarySize) then
  132. Result := VirtualAlloc(nil, size, MEM_COMMIT, PAGE_READWRITE)
  133. else
  134. Result := nil;
  135. end;
  136. procedure LZMAFreeFunc(p: PLZMAISzAlloc; address: Pointer); cdecl;
  137. begin
  138. if Assigned(address) then
  139. VirtualFree(address, 0, MEM_RELEASE);
  140. end;
  141. const
  142. LZMAAlloc: TLZMAISzAlloc = (Alloc: LZMAAllocFunc; Free: LZMAFreeFunc);
  143. { TLZMACustomDecompressor }
  144. destructor TLZMACustomDecompressor.Destroy;
  145. begin
  146. FreeDecoder;
  147. inherited;
  148. end;
  149. procedure TLZMACustomDecompressor.DecompressInto(var Buffer; Count: Cardinal);
  150. var
  151. NextOut: PByte;
  152. OutBytes, InBytes: Cardinal;
  153. Code: TLZMASRes;
  154. DecodeStatus: Integer;
  155. begin
  156. if not FHeaderProcessed then begin
  157. { Reset these following a reset }
  158. FNextIn := PByte(@FBuffer);
  159. FAvailIn := 0;
  160. ProcessHeader;
  161. FHeaderProcessed := True;
  162. end;
  163. NextOut := @Buffer;
  164. var AvailOut := Count;
  165. while AvailOut > 0 do begin
  166. if (FAvailIn = 0) and not FReachedEnd then begin
  167. FNextIn := PByte(@FBuffer);
  168. FAvailIn := ReadProc(FBuffer, SizeOf(FBuffer));
  169. if FAvailIn = 0 then
  170. FReachedEnd := True; { not really necessary, but for consistency }
  171. end;
  172. OutBytes := AvailOut;
  173. InBytes := FAvailIn;
  174. Code := DecodeToBuf(NextOut^, OutBytes, FNextIn^, InBytes, DecodeStatus);
  175. case Code of
  176. SZ_OK: ;
  177. SZ_ERROR_DATA: LZMADecompDataError(3);
  178. else
  179. LZMADecompInternalError(Format('DecodeToBuf failed (%d)', [Code]));
  180. end;
  181. { No bytes in or out indicates corruption somewhere. Possibilities:
  182. - The caller is asking for too many bytes.
  183. - It needs more input but there isn't any left.
  184. - It encountered an "end mark" previously and is now refusing to
  185. process any further input. }
  186. if (InBytes = 0) and (OutBytes = 0) then
  187. LZMADecompDataError(4);
  188. Dec(AvailOut, OutBytes);
  189. Inc(NextOut, OutBytes);
  190. Dec(FAvailIn, InBytes);
  191. Inc(FNextIn, InBytes);
  192. end;
  193. end;
  194. procedure TLZMACustomDecompressor.Reset;
  195. begin
  196. FHeaderProcessed := False;
  197. FReachedEnd := False;
  198. end;
  199. { TLZMA1Decompressor }
  200. procedure TLZMA1Decompressor.FreeDecoder;
  201. begin
  202. LzmaDec_Free(FDecoderState, LZMAAlloc);
  203. end;
  204. procedure TLZMA1Decompressor.ProcessHeader;
  205. var
  206. Props: packed record { size = LZMA_PROPS_SIZE (5) }
  207. Misc: Byte;
  208. DictionarySize: LongWord; { little endian, unaligned }
  209. end;
  210. begin
  211. { Read header fields }
  212. if ReadProc(Props, SizeOf(Props)) <> SizeOf(Props) then
  213. LZMADecompDataError(1);
  214. if Props.DictionarySize > LongWord(MaxDictionarySize) then
  215. LZMADecompDataError(5);
  216. var StateSize := IS_LzmaDec_StateSize;
  217. var DecoderStateSize := SizeOf(FDecoderState);
  218. if StateSize <> DecoderStateSize then
  219. LZMADecompDataError(-Integer(StateSize));
  220. { Note: IS_LzmaDec_Init will re-use already-allocated memory if it can.
  221. FDecoderState is assumed to be initialized to zero on the first call. }
  222. case IS_LzmaDec_Init(FDecoderState, DecoderStateSize, Props,
  223. SizeOf(Props), LZMAAlloc) of
  224. SZ_OK: ;
  225. SZ_ERROR_MEM: OutOfMemoryError;
  226. else
  227. { SZ_ERROR_UNSUPPORTED and anything else }
  228. LZMADecompDataError(2);
  229. end;
  230. end;
  231. function TLZMA1Decompressor.DecodeToBuf(var Dest; var DestLen: Cardinal;
  232. const Src; var SrcLen: Cardinal; var Status: Integer): Integer;
  233. begin
  234. var NativeDestLen: NativeUInt := DestLen;
  235. var NativeSrcLen: NativeUInt := SrcLen;
  236. Result := LzmaDec_DecodeToBuf(FDecoderState, Dest, NativeDestLen, Src, NativeSrcLen,
  237. LZMA_FINISH_ANY, Status);
  238. DestLen := Cardinal(NativeDestLen);
  239. SrcLen := Cardinal(NativeSrcLen);
  240. end;
  241. { TLZMA2Decompressor }
  242. procedure TLZMA2Decompressor.FreeDecoder;
  243. begin
  244. IS_Lzma2Dec_Free(FDecoderState, LZMAAlloc);
  245. end;
  246. procedure TLZMA2Decompressor.ProcessHeader;
  247. { Macro from Lzma2Dec.c: }
  248. function LZMA2_DIC_SIZE_FROM_PROP(p: LongWord): LongWord;
  249. begin
  250. Result := (2 or (p and 1)) shl (p div 2 + 11);
  251. end;
  252. var
  253. Prop: Byte;
  254. begin
  255. { Read header fields }
  256. if ReadProc(Prop, SizeOf(Prop)) <> SizeOf(Prop) then
  257. LZMADecompDataError(1);
  258. if (Prop >= 40) or
  259. (LZMA2_DIC_SIZE_FROM_PROP(Prop) > LongWord(MaxDictionarySize)) then
  260. LZMADecompDataError(5);
  261. var StateSize := IS_Lzma2Dec_StateSize;
  262. var DecoderStateSize := SizeOf(FDecoderState);
  263. if StateSize <> DecoderStateSize then
  264. LZMADecompDataError(-Integer(StateSize));
  265. { Note: IS_Lzma2Dec_Init will re-use already-allocated memory if it can.
  266. FDecoderState is assumed to be initialized to zero on the first call. }
  267. case IS_Lzma2Dec_Init(FDecoderState, DecoderStateSize, Prop,
  268. LZMAAlloc) of
  269. SZ_OK: ;
  270. SZ_ERROR_MEM: OutOfMemoryError;
  271. else
  272. { SZ_ERROR_UNSUPPORTED and anything else }
  273. LZMADecompDataError(2);
  274. end;
  275. end;
  276. function TLZMA2Decompressor.DecodeToBuf(var Dest; var DestLen: Cardinal;
  277. const Src; var SrcLen: Cardinal; var Status: Integer): Integer;
  278. begin
  279. var NativeDestLen: NativeUInt := DestLen;
  280. var NativeSrcLen: NativeUInt := SrcLen;
  281. Result := Lzma2Dec_DecodeToBuf(FDecoderState, Dest, NativeDestLen, Src, NativeSrcLen,
  282. LZMA_FINISH_ANY, Status);
  283. DestLen := Cardinal(NativeDestLen);
  284. SrcLen := Cardinal(NativeSrcLen);
  285. end;
  286. end.