diff --git a/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs b/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs index 55a52488f..359fcc64e 100644 --- a/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs +++ b/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs @@ -50,6 +50,7 @@ private static T[][] CreateTables() /// The block after the first run-length step (1 … 900,000 bytes). /// The block CRC of the bytes before that step. /// How often the Huffman tables are refined (1 or more). + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public void Encode(ReadOnlySpan block, uint crc, int passes, CancellationToken cancellationToken) { var n = block.Length; @@ -170,6 +171,7 @@ public void Encode(ReadOnlySpan block, uint crc, int passes, CancellationT /// bijective base 2 with RUNA (1) and RUNB (2), lowest digit first; position j is symbol j + 1. Ends with the /// end-of-block symbol. Returns the number of symbols. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private int MoveToFront(ReadOnlySpan bwt, ReadOnlySpan number, int inUse, int endOfBlock, Span frequencies) { Span list = stackalloc byte[256]; @@ -220,6 +222,7 @@ private int WriteRun(int run, int count, Span frequencies) } /// Picks the selectors and the tables' code lengths. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void ChooseTables(int count, int alphabet, int tables, int selectorCount, ReadOnlySpan frequencies, int passes) { // Start: table t is cheap for one slice of the symbols (by number), dear for the rest. @@ -308,6 +311,7 @@ internal static class Bzip2Huffman /// Code lengths (1 … ) for ; every symbol gets one, unused /// ones as if seen once. A tree deeper than the limit is built again from flattened frequencies. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static void Lengths(ReadOnlySpan frequencies, Span lengths, int maxLength) { var n = frequencies.Length; diff --git a/src/Filee.Engines/Archives/Bzip2Sort.cs b/src/Filee.Engines/Archives/Bzip2Sort.cs index 41672b7ee..a44bee8fa 100644 --- a/src/Filee.Engines/Archives/Bzip2Sort.cs +++ b/src/Filee.Engines/Archives/Bzip2Sort.cs @@ -6,16 +6,21 @@ // Equal rows (a periodic block) may come in any order among themselves; the transform is the same. using System.Buffers; +using System.Runtime.CompilerServices; namespace Filee.Engines.Archives; /// Sorts the rotations of a block and writes its Burrows-Wheeler transform. internal static class Bzip2Sort { + // The sort's methods are compiled fully optimised on first use (AggressiveOptimization): each runs for a whole + // block per call, so a job of a few dozen blocks never left their ReadyToRun code, which made it about half as long + // again. /// /// Writes the last column of the sorted rotations of to and returns /// the row of the unrotated block (bzip2's origin pointer). /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static int Transform(ReadOnlySpan block, Span output, CancellationToken cancellationToken) { var n = block.Length; @@ -70,6 +75,7 @@ public static int Transform(ReadOnlySpan block, Span output, Cancell /// smallest rotation is never dropped. A periodic text has two or more: the candidates end up on two of them and /// match all the way round. A text with one runs out of candidates before. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] internal static int SmallestRotation(ReadOnlySpan text, out bool periodic) { var n = text.Length; @@ -100,6 +106,7 @@ internal static int SmallestRotation(ReadOnlySpan text, out bool periodic) /// The length of the shortest piece is a whole number of copies of (its length when it is /// none), from the prefix function, kept in . /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private static int RootLength(ReadOnlySpan text, int[] scratch) { var n = text.Length; @@ -152,6 +159,7 @@ private readonly struct IntText(int[] text) : IText /// the suffix array. /// The string of names is sorted the same way when names repeat; its alphabet is at most half the size. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private static void SuffixArray(T text, int n, int alphabet, int[] sa, CancellationToken cancellationToken) where T : struct, IText { @@ -311,6 +319,7 @@ private static void SuffixArray(T text, int n, int alphabet, int[] sa, Cancel /// suffix whose predecessor is L-type has nothing to place in the S scan, one whose predecessor is S-type has. The /// S scan leaves every entry as the plain position. 0 is both a gap and suffix 0, which never places anything. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private static void Induce(T text, int n, int[] sa, int[] starts, int[] ends, int[] fill, int alphabet) where T : struct, IText { diff --git a/src/Filee.Engines/Archives/Bzip2Writer.cs b/src/Filee.Engines/Archives/Bzip2Writer.cs index baf75e8af..f6729e0a4 100644 --- a/src/Filee.Engines/Archives/Bzip2Writer.cs +++ b/src/Filee.Engines/Archives/Bzip2Writer.cs @@ -5,6 +5,7 @@ // stays bounded by the number of blocks in flight (about 20 MB each while sorting). It is a write-only stream, so a TAR // can be compressed while it is written (.tar.bz2) without a temporary file. +using System.Runtime.CompilerServices; using Filee.Core.Presets; namespace Filee.Engines.Archives; @@ -71,6 +72,7 @@ public Bzip2Writer(Stream output, ArchiveLevel level, CancellationToken cancella public override void Write(byte[] buffer, int offset, int count) => Write(buffer.AsSpan(offset, count)); + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public override void Write(ReadOnlySpan buffer) { ObjectDisposedException.ThrowIf(_finished, this); @@ -253,6 +255,7 @@ public void Clear() } /// Writes the low (0 … 32) bits of , which has no others set. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Write(int count, uint value) { _pending = (_pending << count) | value; diff --git a/src/Filee.Engines/Archives/Lzma/Lzma2Reader.cs b/src/Filee.Engines/Archives/Lzma/Lzma2Reader.cs index 9ef47baa0..5e0e3563c 100644 --- a/src/Filee.Engines/Archives/Lzma/Lzma2Reader.cs +++ b/src/Filee.Engines/Archives/Lzma/Lzma2Reader.cs @@ -7,6 +7,8 @@ // An LZMA chunk unpacks to at most 2 MiB from at most 64 KiB, with its own range coder, so each is read whole // before it is decoded and nothing in it can claim more than those limits. +using System.Buffers.Binary; + namespace Filee.Engines.Archives.Lzma; /// Buffered reading from a stream that counts the bytes it hands out. @@ -35,6 +37,21 @@ public void ReadExactly(Span target) throw new InvalidDataException("the data ends too early"); } + /// The next four bytes as a big-endian number; the stream ending first is damage. + public uint ReadUInt32BigEndian() + { + if (_end - _position >= 4) + { + var value = BinaryPrimitives.ReadUInt32BigEndian(_buffer.AsSpan(_position)); + _position += 4; + Consumed += 4; + return value; + } + Span bytes = stackalloc byte[4]; + ReadExactly(bytes); + return BinaryPrimitives.ReadUInt32BigEndian(bytes); + } + /// Reads as much of as the stream still has. public int ReadAtMost(Span target) { diff --git a/src/Filee.Engines/Archives/Lzma/Lzma2Writer.cs b/src/Filee.Engines/Archives/Lzma/Lzma2Writer.cs index 7cb81387f..d79290335 100644 --- a/src/Filee.Engines/Archives/Lzma/Lzma2Writer.cs +++ b/src/Filee.Engines/Archives/Lzma/Lzma2Writer.cs @@ -11,6 +11,7 @@ // threads of its own (MatchFinder.StartAhead, the trees split between them by hash), which gives the same bytes too. using System.Numerics; +using System.Runtime.CompilerServices; using Filee.Core.Presets; namespace Filee.Engines.Archives.Lzma; @@ -31,7 +32,8 @@ public static Lzma2Settings For(ArchiveLevel level, long expectedSize = -1) var settings = level switch { ArchiveLevel.Store => new Lzma2Settings(1 << 12, default, Store: true), - ArchiveLevel.Fast => new Lzma2Settings(1 << 18, new LzmaEncoderSettings(Depth: 8, Nice: 32, Optimal: false, Tree: false), false), + // Six chain candidates: against eight, about 4 % less work for 0.25 % larger output. + ArchiveLevel.Fast => new Lzma2Settings(1 << 18, new LzmaEncoderSettings(Depth: 6, Nice: 32, Optimal: false, Tree: false), false), ArchiveLevel.Maximum => new Lzma2Settings(1 << 26, new LzmaEncoderSettings(Depth: 64, Nice: 64, Optimal: true, Tree: true), false), _ => new Lzma2Settings(1 << 24, new LzmaEncoderSettings(Depth: 32, Nice: 32, Optimal: true, Tree: true), false), }; @@ -346,6 +348,12 @@ public void Encode(CancellationToken cancellationToken, int searchThreads = 0) } } + // Compiled fully optimised on first use, with the fast level's per-symbol code (LzmaEncoder.Encode and EncodeOne + // and the helpers marked AggressiveInlining) inlined, as the tiered compiler ends up doing; the methods it calls + // stay separate and are compiled the same way. A chunk then runs at full speed from the first one, where the + // ReadyToRun and instrumented code made a fast job of a few dozen megabytes about a third longer, more so with many + // threads. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void EncodeChunk(int end, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs b/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs index a8bfdc33f..87d72eb4e 100644 --- a/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs +++ b/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs @@ -317,10 +317,14 @@ public void AppendUncompressed(ReadOnlySpan data) } } + // The decoding loops are compiled fully optimised on first use (AggressiveOptimization) instead of starting from + // the ReadyToRun code and going through the instrumented tier: those ran the start of every job slower, and a job + // is often over before the tiered code would come. /// /// Decodes until is full or, with , the end marker /// comes. Returns the bytes written. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public int Decode(RangeDecoder rc, Span output, bool allowEndMarker) { // The literal coders come with the properties; without them the probabilities below would not be there. @@ -365,6 +369,7 @@ public int Decode(RangeDecoder rc, Span output, bool allowEndMarker) /// hand over when the IsMatch bit changes. Each keeps the range coder in registers for its whole run: one method /// for both had more live values than the CPU has registers, and the JIT spilled the coder in the literal loop. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private bool Run(RangeDecoder rc, int stop, bool allowEndMarker) where TInput : struct, IInputMode { var start = _pos; @@ -412,7 +417,7 @@ private int NextSymbol(RangeDecoder rc) where TInput : struct, IInputMod /// Decodes literals (the first one's IsMatch bit was read) until a match comes () or Run /// has to stop (). /// - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] private int LiteralRun(RangeDecoder rc) where TInput : struct, IInputMode { ref var win = ref MemoryMarshal.GetArrayDataReference(_window); @@ -502,7 +507,7 @@ private int LiteralRun(RangeDecoder rc) where TInput : struct, IInputMod /// (the rest stays pending), until a literal comes (), Run has to stop /// () or the end marker (). /// - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] private int MatchRun(RangeDecoder rc, bool allowEndMarker) where TInput : struct, IInputMode { var window = _window; @@ -677,6 +682,7 @@ private static unsafe void Prefetch(byte[] window, int source, int count) } /// Does the first copies MatchRun put off, in order. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void CopyDeferred(byte[] window, int count) { ref var deferred = ref MemoryMarshal.GetArrayDataReference(_deferred); diff --git a/src/Filee.Engines/Archives/Lzma/LzmaEncoder.cs b/src/Filee.Engines/Archives/Lzma/LzmaEncoder.cs index 3bec65072..0e8caec2a 100644 --- a/src/Filee.Engines/Archives/Lzma/LzmaEncoder.cs +++ b/src/Filee.Engines/Archives/Lzma/LzmaEncoder.cs @@ -77,6 +77,7 @@ public LzmaEncoder(MatchFinder mf, LzmaProperties properties, LzmaEncoderSetting /// Codes symbols from the cursor until (no match crosses it) or until the coder's output /// would pass bytes. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Encode(RangeEncoder rc, int end, long packedLimit) { while (_mf.Position < end && rc.FlushedSize < packedLimit) @@ -112,6 +113,7 @@ public void Shifted(int shift) _nextAt -= shift; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void EncodeOne(RangeEncoder rc, int end) { var buffer = _mf.Buffer; @@ -207,6 +209,7 @@ private void EncodeOne(RangeEncoder rc, int end) private static bool FarNearer(uint near, uint far) => far >> 7 > near; /// The longest repeat of one of the last four distances at (0 when none of 2 or more). + [MethodImpl(MethodImplOptions.AggressiveInlining)] private int LongestRep(byte[] buffer, int pos, int available, out int repIndex) { repIndex = 0; @@ -232,6 +235,7 @@ private int LongestRep(byte[] buffer, int pos, int available, out int repIndex) } /// True when one of the last four distances repeats at least bytes at (one byte past the cursor). + [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool RepReaches(byte[] buffer, int pos, int available, int length) { if (available < length) @@ -523,6 +527,7 @@ private void RefreshPrices() [MethodImpl(MethodImplOptions.AggressiveInlining)] private uint Rep(int index) => index switch { 0 => Rep0, 1 => Rep1, 2 => Rep2, _ => Rep3 }; + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void EmitLiteralOrShortRep(RangeEncoder rc) { var buffer = _mf.Buffer; @@ -543,6 +548,7 @@ private void EmitLiteralOrShortRep(RangeEncoder rc) EmitLiteral(rc); } + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void EmitShortRep(RangeEncoder rc) { var state2 = (State << 4) + ((int)_total & PbMask); @@ -554,6 +560,7 @@ private void EmitShortRep(RangeEncoder rc) Advance(1); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void EmitLiteral(RangeEncoder rc) { var buffer = _mf.Buffer; @@ -599,6 +606,7 @@ private int LiteralPrice(int pos, long total, int state, uint rep0) return price; } + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void EmitMatch(RangeEncoder rc, int length, uint distance) { var posState = (int)_total & PbMask; @@ -615,6 +623,7 @@ private void EmitMatch(RangeEncoder rc, int length, uint distance) Advance(length); } + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void EmitRep(RangeEncoder rc, int index, int length) { var posState = (int)_total & PbMask; diff --git a/src/Filee.Engines/Archives/Lzma/MatchFinder.cs b/src/Filee.Engines/Archives/Lzma/MatchFinder.cs index 0bcd78ea4..7deade184 100644 --- a/src/Filee.Engines/Archives/Lzma/MatchFinder.cs +++ b/src/Filee.Engines/Archives/Lzma/MatchFinder.cs @@ -338,7 +338,7 @@ private int Search(int pos, int maxLength, int[]? lengths, uint[]? distances, ou /// the same tables, candidates and lists. A candidate is only measured when its two bytes ending at the best length /// so far agree with the cursor's (a match longer than the best has them), which takes one load per candidate. /// - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] private int ChainSearch(int pos, int maxLength, int[] lengths, uint[] distances) { System.Diagnostics.Debug.Assert(pos >= _nextInsert, "a position is searched twice"); @@ -548,6 +548,7 @@ public void SkipTo(int pos) Insert(pos); } + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private void Insert(int pos) { var buffer = Buffer; diff --git a/src/Filee.Engines/Archives/Lzma/RangeEncoder.cs b/src/Filee.Engines/Archives/Lzma/RangeEncoder.cs index 2bc6612b5..19e5889c7 100644 --- a/src/Filee.Engines/Archives/Lzma/RangeEncoder.cs +++ b/src/Filee.Engines/Archives/Lzma/RangeEncoder.cs @@ -84,6 +84,7 @@ private void Bit(ref ushort probability, uint bit, ref ulong low, ref uint range } /// A literal (8 bits through the tree at , most significant first). + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void EncodeLiteral(ref ushort probabilities, uint symbol) { var low = _low; @@ -103,6 +104,7 @@ public void EncodeLiteral(ref ushort probabilities, uint symbol) /// A literal after a match: while its bits agree with 's, the probabilities picked by /// that byte's bit (0x100 or 0x200 on) are used, after the first difference the plain ones. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void EncodeMatchedLiteral(ref ushort probabilities, uint symbol, uint matchByte) { var low = _low; @@ -147,6 +149,7 @@ public void EncodeBit(ref ushort probability, int bit) } /// The low bits of at probability one half, most significant first. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public void EncodeDirectBits(uint value, int count) { var low = _low; @@ -166,6 +169,7 @@ public void EncodeDirectBits(uint value, int count) } /// A -bit symbol through a binary tree of probabilities, most significant bit first. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public void EncodeTree(Span probabilities, int bits, uint symbol) { if (probabilities.Length < 1 << bits) @@ -185,6 +189,7 @@ public void EncodeTree(Span probabilities, int bits, uint symbol) } /// Like , least significant bit first. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public void EncodeReverseTree(Span probabilities, int bits, uint symbol) { if (probabilities.Length < 1 << bits) diff --git a/src/Filee.Engines/Archives/SevenZipCoders.cs b/src/Filee.Engines/Archives/SevenZipCoders.cs index 3af63398a..98fd03350 100644 --- a/src/Filee.Engines/Archives/SevenZipCoders.cs +++ b/src/Filee.Engines/Archives/SevenZipCoders.cs @@ -9,6 +9,7 @@ using System.Buffers.Binary; using System.IO.Compression; +using System.Runtime.CompilerServices; using Filee.Engines.Archives.Lzma; using SharpCompress.Compressors.BZip2; using SharpCompressMode = SharpCompress.Compressors.CompressionMode; @@ -330,7 +331,10 @@ public override int Read(Span buffer) return count; } + // Compiled fully optimised on first use (AggressiveOptimization), as the BCJ2 loop below: each call converts a + // whole buffer, so the ReadyToRun code would run for most of a job. /// Converts what it can of (starting at ); returns how many bytes are done. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private int Convert(Span data) { if (data.Length < 5) @@ -341,10 +345,14 @@ private int Convert(Span data) var limit = data.Length - 4; while (true) { - while (at < limit && (data[at] & 0xFE) != 0xE8) - at++; - if (at >= limit) + // The next E8 or E9, found many bytes at a time. + var next = at < limit ? data[at..limit].IndexOfAny((byte)0xE8, (byte)0xE9) : -1; + if (next < 0) + { + at = Math.Max(at, limit); break; + } + at += next; var gap = at - lastOpcode; if (gap > 3) { @@ -414,7 +422,6 @@ internal sealed class Bcj2Stream : ReadOnlyDecoderStream private readonly ushort[] _probabilities = new ushort[256 + 2]; private readonly long _size; private readonly byte[] _pending = new byte[4]; - private readonly byte[] _target = new byte[4]; private int _pendingAt = 4; private long _done; private byte _previous; @@ -429,70 +436,78 @@ public Bcj2Stream(Stream main, Stream call, Stream jump, Stream rc, long size) _rc.Start(rc, new byte[1 << 12]); } + /// + /// The main stream's bytes are copied while they are scanned, one at a time, and everything the loop changes stays + /// in locals until the end: x86 code has an opcode to look at every dozen bytes or so, so the work per opcode (the + /// bit, the target) has to be small next to the copying. + /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] public override int Read(Span buffer) { + // At most what the stream still has; a target cut off by the end is not handed out. + var limit = (int)Math.Min(buffer.Length, _size - _done); var n = 0; - while (n < buffer.Length && _done < _size) + while (_pendingAt < 4 && n < limit) + buffer[n++] = _pending[_pendingAt++]; + var previous = _previous; + var main = _mainBuffer; + var at = _mainPosition; + var end = _mainEnd; + while (n < limit) { - if (_pendingAt < 4) - { - buffer[n++] = _pending[_pendingAt++]; - _done++; - continue; - } - if (_mainPosition == _mainEnd) + if (at == end) { - _mainPosition = 0; - _mainEnd = Math.Max(0, _main.Read(_mainBuffer, 0, _mainBuffer.Length)); - if (_mainEnd == 0) + at = 0; + end = Math.Max(0, _main.Read(main, 0, main.Length)); + if (end == 0) + { + _mainPosition = _mainEnd = 0; throw new InvalidDataException("the BCJ2 main stream ends too early"); + } } - // The main stream's bytes up to and with the next opcode that may have a target taken out, in one copy. - var run = _mainBuffer.AsSpan(_mainPosition, (int)Math.Min(Math.Min(_mainEnd - _mainPosition, buffer.Length - n), _size - _done)); - var previous = _previous; - var length = 0; + // The main stream's bytes up to and with the next opcode that may have a target taken out. + var stop = at + Math.Min(end - at, limit - n); + var before = previous; + var b = (byte)0; var jump = false; - while (length < run.Length) + while (at < stop) { - var c = run[length++]; - if (IsJump(previous, c)) + b = main[at++]; + buffer[n++] = b; + if ((b & 0xFE) == 0xE8 || previous == 0x0F && (b & 0xF0) == 0x80) { jump = true; - previous = c; + before = previous; + previous = b; break; } - previous = c; + previous = b; } - run[..length].CopyTo(buffer[n..]); - n += length; - _done += length; - _mainPosition += length; - var b = run[length - 1]; - var before = length >= 2 ? run[length - 2] : _previous; - if (!jump || _done == _size) - { - _previous = previous; + // The last byte of the stream has no bit, whatever it is. + if (!jump || _done + n == _size) continue; - } var index = b == 0xE8 ? before : b == 0xE9 ? 256 : 257; if (_rc.DecodeBit(ref _probabilities[index]) == 0) + continue; + var relative = (b == 0xE8 ? _call : _jump).ReadUInt32BigEndian() - (uint)(_done + n + 4); + previous = (byte)(relative >> 24); + if (limit - n >= 4) { - _previous = (byte)b; + BinaryPrimitives.WriteUInt32LittleEndian(buffer[n..], relative); + n += 4; continue; } - (b == 0xE8 ? _call : _jump).ReadExactly(_target); - var relative = BinaryPrimitives.ReadUInt32BigEndian(_target) - (uint)(_done + 4); - _pending[0] = (byte)relative; - _pending[1] = (byte)(relative >> 8); - _pending[2] = (byte)(relative >> 16); - _pending[3] = (byte)(relative >> 24); + BinaryPrimitives.WriteUInt32LittleEndian(_pending, relative); _pendingAt = 0; - _previous = _pending[3]; + while (_pendingAt < 4 && n < limit) + buffer[n++] = _pending[_pendingAt++]; } + _mainPosition = at; + _mainEnd = end; + _previous = previous; + _done += n; return n; } - - private static bool IsJump(byte previous, byte b) => (b & 0xFE) == 0xE8 || previous == 0x0F && (b & 0xF0) == 0x80; } /// diff --git a/tests/Filee.Engines.Tests/SevenZipReaderTests.cs b/tests/Filee.Engines.Tests/SevenZipReaderTests.cs index bfc06a70b..ae1ce11b9 100644 --- a/tests/Filee.Engines.Tests/SevenZipReaderTests.cs +++ b/tests/Filee.Engines.Tests/SevenZipReaderTests.cs @@ -8,6 +8,7 @@ using System.Text; using Filee.Core.Presets; using Filee.Engines.Archives; +using Filee.Engines.Archives.Lzma; using static Filee.Engines.Tests.ArchiveBuilders; using static Filee.Engines.Tests.ArchiveFormatBuilders; @@ -281,6 +282,100 @@ public async Task The_x86_filters_change_the_data_and_are_undone() } } + [Fact] + public void BCJ2_streams_decode_alike_in_reads_of_any_size() + { + // Lengths around the decoder's 64 KiB main buffer, and opcodes in the last bytes: a target cut by a read's end + // waits for the next read, one cut by the end of the data is dropped, and the last byte never has a bit. + foreach (var length in new[] { 1, 2, 5, 6, 9, 64, 65_535, 65_541, 200_003 }) + { + var data = X86Like(Math.Max(length, 16), length).AsSpan(0, length).ToArray(); + foreach (var back in new[] { 1, 3, 5, 6 }) + { + if (back <= length) + data[^back] = 0xE8; + } + var (main, call, jump, rc) = Bcj2Encode(data, length); + foreach (var seed in new[] { 1, 2, 3 }) + { + using var decoder = new Bcj2Stream(new MemoryStream(main), new MemoryStream(call), new MemoryStream(jump), + new MemoryStream(rc), data.Length); + Assert.Equal(data, ReadInPieces(decoder, seed)); + } + } + + // A main stream that ends early is damage. + var code = X86Like(50_000, 9); + var (m, c, j, r) = Bcj2Encode(code, 9); + using var cut = new Bcj2Stream(new MemoryStream(m[..^10]), new MemoryStream(c), new MemoryStream(j), new MemoryStream(r), + code.Length); + Assert.Throws(() => ReadInPieces(cut, 1)); + } + + /// + /// A BCJ2 encoder as the format describes it (Filee only decodes BCJ2): three of four opcodes that have four bytes + /// after them get their target taken out, made absolute and big-endian, to the call stream (E8) or the jump stream + /// (E9, 0F 8x); a range-coded bit per opcode (none for the last byte) says which. + /// + private static (byte[] Main, byte[] Call, byte[] Jump, byte[] Rc) Bcj2Encode(byte[] data, int seed) + { + var random = new Random(seed); + var probabilities = new ushort[256 + 2]; + RangeCoder.Reset(probabilities); + var rc = new RangeEncoder(data.Length + 1024); + using var main = new MemoryStream(); + using var call = new MemoryStream(); + using var jump = new MemoryStream(); + Span target = stackalloc byte[4]; + byte previous = 0; + for (var i = 0; i < data.Length; i++) + { + var b = data[i]; + main.WriteByte(b); + var opcode = (b & 0xFE) == 0xE8 || previous == 0x0F && (b & 0xF0) == 0x80; + if (!opcode || i == data.Length - 1) + { + previous = b; + continue; + } + var convert = i + 4 < data.Length && random.Next(4) != 0; + rc.EncodeBit(ref probabilities[b == 0xE8 ? previous : b == 0xE9 ? 256 : 257], convert ? 1 : 0); + if (!convert) + { + previous = b; + continue; + } + var absolute = BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(i + 1)) + (uint)(i + 5); + BinaryPrimitives.WriteUInt32BigEndian(target, absolute); + (b == 0xE8 ? call : jump).Write(target); + previous = data[i + 4]; + i += 4; + } + rc.Flush(); + return (main.ToArray(), call.ToArray(), jump.ToArray(), rc.Output.ToArray()); + } + + /// Reads to its end in pieces of random size (1 byte … 70 KB). + private static byte[] ReadInPieces(Stream stream, int seed) + { + var random = new Random(seed); + var buffer = new byte[70_000]; + using var output = new MemoryStream(); + while (true) + { + var want = random.Next(3) switch + { + 0 => random.Next(1, 6), + 1 => random.Next(1, 300), + _ => random.Next(1, buffer.Length), + }; + var read = stream.Read(buffer, 0, want); + if (read == 0) + return output.ToArray(); + output.Write(buffer, 0, read); + } + } + /// /// Bytes that look like code for to its branch converter: many call (and ARM64 ADRP) /// instructions with near targets between other words.