diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs index c4e6d108..47904984 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs @@ -1028,7 +1028,7 @@ internal sealed partial class Unpack : BitInput, IRarUnpack, IDisposable vmCode.Add((byte)(GetBits() >> 8)); AddBits(8); } - return (AddVMCode(FirstByte, vmCode, Length)); + return AddVMCode(FirstByte, vmCode); } private bool ReadVMCodePPM() @@ -1073,10 +1073,10 @@ internal sealed partial class Unpack : BitInput, IRarUnpack, IDisposable } vmCode.Add((byte)Ch); // VMCode[I]=Ch; } - return (AddVMCode(FirstByte, vmCode, Length)); + return AddVMCode(FirstByte, vmCode); } - private bool AddVMCode(int firstByte, List vmCode, int length) + private bool AddVMCode(int firstByte, List vmCode) { var Inp = new BitInput(); Inp.InitBitInput(); @@ -1199,19 +1199,28 @@ internal sealed partial class Unpack : BitInput, IRarUnpack, IDisposable { return (false); } - Span VMCode = stackalloc byte[VMCodeSize]; - for (var I = 0; I < VMCodeSize; I++) - { - if (Inp.Overflow(3)) - { - return (false); - } - VMCode[I] = (byte)(Inp.GetBits() >> 8); - Inp.AddBits(8); - } - // VM.Prepare(&VMCode[0],VMCodeSize,&Filter->Prg); - rarVM.prepare(VMCode, VMCodeSize, Filter.Program); + var VMCode = ArrayPool.Shared.Rent(VMCodeSize); + try + { + for (var I = 0; I < VMCodeSize; I++) + { + if (Inp.Overflow(3)) + { + return (false); + } + + VMCode[I] = (byte)(Inp.GetBits() >> 8); + Inp.AddBits(8); + } + + // VM.Prepare(&VMCode[0],VMCodeSize,&Filter->Prg); + rarVM.prepare(VMCode.AsSpan(0, VMCodeSize), Filter.Program); + } + finally + { + ArrayPool.Shared.Return(VMCode); + } } StackFilter.Program.AltCommands = Filter.Program.Commands; // StackFilter->Prg.AltCmd=&Filter->Prg.Cmd[0]; StackFilter.Program.CommandCount = Filter.Program.CommandCount; diff --git a/src/SharpCompress/Compressors/Rar/VM/RarVM.cs b/src/SharpCompress/Compressors/Rar/VM/RarVM.cs index f1ef3834..ce267bad 100644 --- a/src/SharpCompress/Compressors/Rar/VM/RarVM.cs +++ b/src/SharpCompress/Compressors/Rar/VM/RarVM.cs @@ -776,9 +776,10 @@ internal sealed class RarVM : BitInput } } - public void prepare(ReadOnlySpan code, int codeSize, VMPreparedProgram prg) + public void prepare(ReadOnlySpan code, VMPreparedProgram prg) { InitBitInput(); + var codeSize = code.Length; var cpLength = Math.Min(MAX_SIZE, codeSize); // memcpy(inBuf,Code,Min(CodeSize,BitInput::MAX_SIZE)); @@ -795,7 +796,7 @@ internal sealed class RarVM : BitInput prg.CommandCount = 0; if (xorSum == code[0]) { - var filterType = IsStandardFilter(code, codeSize); + var filterType = IsStandardFilter(code); if (filterType != VMStandardFilters.VMSF_NONE) { var curCmd = new VMPreparedCommand(); @@ -1105,7 +1106,7 @@ internal sealed class RarVM : BitInput } } - private VMStandardFilters IsStandardFilter(ReadOnlySpan code, int codeSize) + private VMStandardFilters IsStandardFilter(ReadOnlySpan code) { VMStandardFilterSignature[] stdList = {