From 0ffbabc69c982ff2a77d88df7626ea4185d7d3f9 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Wed, 3 Jul 2019 16:00:26 -0700 Subject: [PATCH] Prep work for cleaning up linker heuristics (dotnet/corefx#39142) - Deleting dead or unrechable code - Adding ILLinkTrim.xml files for types and methods accessed via reflection Commit migrated from https://github.com/dotnet/corefx/commit/bc1f6ae4e757251c8691fe94e68617ce1516e684 --- .../VisualBasic/CompilerServices/NewLateBinding.vb | 50 --- .../VisualBasic/CompilerServices/Operators.vb | 14 - .../CompilerServices/Utils.LateBinder.vb | 14 - .../src/ILLinkTrim.xml | 16 + .../System.Data.Common/src/ILLinkTrim.xml | 36 ++ .../src/ILLinkTrim.xml | 12 +- .../src/System.IO.Compression.csproj | 6 - .../IO/Compression/DeflateManaged/CopyEncoder.cs | 81 ---- .../DeflateManaged/DeflateManagedStream.cs | 193 +-------- .../Compression/DeflateManaged/DeflaterManaged.cs | 276 ------------- .../IO/Compression/DeflateManaged/FastEncoder.cs | 175 -------- .../DeflateManaged/FastEncoderWindow.cs | 445 --------------------- .../IO/Compression/DeflateManaged/FileFormats.cs | 7 - .../System/IO/Compression/DeflateManaged/Match.cs | 17 - .../IO/Compression/DeflateManaged/OutputBuffer.cs | 123 ------ .../System.Linq.Expressions/src/ILLinkTrim.xml | 10 + .../System/Runtime/CompilerServices/CallSite.cs | 18 - .../System/Runtime/CompilerServices/RuleCache.cs | 18 - .../System/Net/Windows/WebSockets/WebSocketBase.cs | 17 - .../System.Net.Primitives/src/ILLinkTrim.xml | 9 + .../System.Private.Xml/src/ILLinkTrim.xml | 12 + .../src/System/Xml/Core/XmlReader.cs | 3 +- .../src/System/Xml/Schema/XmlValueConverter.cs | 92 ----- .../src/System/Xml/Xsl/XmlQueryType.cs | 6 - .../src/System/Xml/Xsl/XmlQueryTypeFactory.cs | 53 --- .../Serialization/Formatters/Binary/BinaryEnums.cs | 15 - .../Serialization/Formatters/Binary/Converter.cs | 58 --- .../src/System/Security/Principal/SID.cs | 15 - src/libraries/System.Text.Json/src/ILLinkTrim.xml | 20 + 29 files changed, 113 insertions(+), 1698 deletions(-) create mode 100644 src/libraries/System.Data.Common/src/ILLinkTrim.xml delete mode 100644 src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/CopyEncoder.cs delete mode 100644 src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflaterManaged.cs delete mode 100644 src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoder.cs delete mode 100644 src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoderWindow.cs delete mode 100644 src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/Match.cs delete mode 100644 src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/OutputBuffer.cs create mode 100644 src/libraries/System.Net.Primitives/src/ILLinkTrim.xml create mode 100644 src/libraries/System.Private.Xml/src/ILLinkTrim.xml create mode 100644 src/libraries/System.Text.Json/src/ILLinkTrim.xml diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/NewLateBinding.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/NewLateBinding.vb index 0596b38..4671f0a 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/NewLateBinding.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/NewLateBinding.vb @@ -31,56 +31,6 @@ Namespace Microsoft.VisualBasic.CompilerServices End Sub - Friend Shared Function LateCanEvaluate( - ByVal instance As Object, - ByVal type As System.Type, - ByVal memberName As String, - ByVal arguments As Object(), - ByVal allowFunctionEvaluation As Boolean, - ByVal allowPropertyEvaluation As Boolean) As Boolean - - Dim baseReference As Container - If type IsNot Nothing Then - baseReference = New Container(type) - Else - baseReference = New Container(instance) - End If - - Dim members As MemberInfo() = baseReference.GetMembers(memberName, False) - - If members.Length = 0 Then - Return True - End If - - ' This is a field access - If members(0).MemberType = MemberTypes.Field Then - If arguments.Length = 0 Then - Return True - Else - Dim fieldValue As Object = baseReference.GetFieldValue(DirectCast(members(0), FieldInfo)) - baseReference = New Container(fieldValue) - If baseReference.IsArray Then - Return True - End If - Return allowPropertyEvaluation - End If - End If - - ' This is a method invocation - If members(0).MemberType = MemberTypes.Method Then - Return allowFunctionEvaluation - End If - - ' This is a property access - If members(0).MemberType = MemberTypes.Property Then - Return allowPropertyEvaluation - End If - - Return True - End Function - - - Public Shared Function LateCall( ByVal Instance As Object, ByVal Type As System.Type, diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Operators.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Operators.vb index fa36302..3bd4b2f 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Operators.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Operators.vb @@ -353,20 +353,6 @@ Namespace Microsoft.VisualBasic.CompilerServices End Select End Function - Friend Shared Function CompareObject(ByVal left As Object, ByVal right As Object, ByVal textCompare As Boolean) As Integer - Dim comparison As CompareClass = CompareObject2(left, right, textCompare) - - Select Case comparison - Case CompareClass.Unordered - Return 0 - Case CompareClass.UserDefined, - CompareClass.Undefined - Throw GetNoValidOperatorException(UserDefinedOperator.IsTrue, left, right) - Case Else - Return comparison - End Select - End Function - Private Shared Function CompareObject2(ByVal left As Object, ByVal right As Object, ByVal textCompare As Boolean) As CompareClass Dim tc1 As TypeCode = GetTypeCode(left) diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb index 06fd03f..54b99ab 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb @@ -50,20 +50,6 @@ Namespace Microsoft.VisualBasic.CompilerServices Private Shared ReadOnly s_voidType As Type = System.Type.GetType("System.Void") Private Shared s_VBRuntimeAssembly As System.Reflection.Assembly - '============================================================================ - ' Shared Error functions - '============================================================================ - - Private Shared Function IntToHex(ByVal n As Integer) As Char - System.Diagnostics.Debug.Assert(n < &H10) - - If n <= 9 Then - Return ChrW(n + AscW("0"c)) - Else - Return ChrW(n - 10 + AscW("a"c)) - End If - End Function - '***************************************************************************** ';GetResourceString ' diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml b/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml index badebaa..140fc24 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml +++ b/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml @@ -5,6 +5,22 @@ + + + + + + + + + + + + + + + + diff --git a/src/libraries/System.Data.Common/src/ILLinkTrim.xml b/src/libraries/System.Data.Common/src/ILLinkTrim.xml new file mode 100644 index 0000000..0a68ae9 --- /dev/null +++ b/src/libraries/System.Data.Common/src/ILLinkTrim.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml b/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml index 42a497a..790c20b 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml @@ -1,13 +1,9 @@ - - - - - - - - + + + + \ No newline at end of file diff --git a/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj b/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj index ba5b514..f62d3f7 100644 --- a/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj +++ b/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj @@ -17,21 +17,15 @@ - - - - - - diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/CopyEncoder.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/CopyEncoder.cs deleted file mode 100644 index d020c0f..0000000 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/CopyEncoder.cs +++ /dev/null @@ -1,81 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Diagnostics; - -namespace System.IO.Compression -{ - internal sealed class CopyEncoder - { - /// - /// padding for copy encoder formatting - /// - 1 byte for header - /// - 4 bytes for len, nlen - /// - private const int PaddingSize = 5; - - private const int MaxUncompressedBlockSize = 65536; - - /// - /// null input means write an empty payload with formatting info. This is needed for the final block. - /// - public void GetBlock(DeflateInput input, OutputBuffer output, bool isFinal) - { - Debug.Assert(output != null); - Debug.Assert(output.FreeBytes >= PaddingSize); - - // determine number of bytes to write - int count = 0; - if (input != null) - { - // allow space for padding and bits not yet flushed to buffer - count = Math.Min(input.Count, output.FreeBytes - PaddingSize - output.BitsInBuffer); - - // we don't expect the output buffer to ever be this big (currently 4K), but we'll check this - // just in case that changes. - if (count > MaxUncompressedBlockSize - PaddingSize) - { - count = MaxUncompressedBlockSize - PaddingSize; - } - } - - // write header and flush bits - if (isFinal) - { - output.WriteBits( - FastEncoderStatics.BFinalNoCompressionHeaderBitCount, - FastEncoderStatics.BFinalNoCompressionHeader); - } - else - { - output.WriteBits( - FastEncoderStatics.NoCompressionHeaderBitCount, - FastEncoderStatics.NoCompressionHeader); - } - - // now we're aligned - output.FlushBits(); - - // write len, nlen - WriteLenNLen((ushort)count, output); - - // write uncompressed bytes - if (input != null && count > 0) - { - output.WriteBytes(input.Buffer, input.StartIndex, count); - input.ConsumeBytes(count); - } - } - - private void WriteLenNLen(ushort len, OutputBuffer output) - { - // len - output.WriteUInt16(len); - - // nlen - ushort onesComp = (ushort)(~(ushort)len); - output.WriteUInt16(onesComp); - } - } -} diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs index c7b9caa..dfadee5 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs @@ -9,23 +9,18 @@ using System.Threading.Tasks; namespace System.IO.Compression { + // DeflateManagedStream supports decompression of Deflate64 format only. internal sealed partial class DeflateManagedStream : Stream { internal const int DefaultBufferSize = 8192; private Stream _stream; - private CompressionMode _mode; private bool _leaveOpen; private InflaterManaged _inflater; - private DeflaterManaged _deflater; private byte[] _buffer; private int _asyncOperations; - private IFileFormatWriter _formatWriter; - private bool _wroteHeader; - private bool _wroteBytes; - // A specific constructor to allow decompression of Deflate64 internal DeflateManagedStream(Stream stream, ZipArchiveEntry.CompressionMethodValues method, long uncompressedSize) { @@ -43,26 +38,17 @@ namespace System.IO.Compression internal void InitializeInflater(Stream stream, bool leaveOpen, IFileFormatReader reader = null, ZipArchiveEntry.CompressionMethodValues method = ZipArchiveEntry.CompressionMethodValues.Deflate, long uncompressedSize = -1) { Debug.Assert(stream != null); - Debug.Assert(method == ZipArchiveEntry.CompressionMethodValues.Deflate || method == ZipArchiveEntry.CompressionMethodValues.Deflate64); + Debug.Assert(method == ZipArchiveEntry.CompressionMethodValues.Deflate64); if (!stream.CanRead) throw new ArgumentException(SR.NotSupported_UnreadableStream, nameof(stream)); _inflater = new InflaterManaged(reader, method == ZipArchiveEntry.CompressionMethodValues.Deflate64 ? true : false, uncompressedSize); _stream = stream; - _mode = CompressionMode.Decompress; _leaveOpen = leaveOpen; _buffer = new byte[DefaultBufferSize]; } - internal void SetFileFormatWriter(IFileFormatWriter writer) - { - if (writer != null) - { - _formatWriter = writer; - } - } - public override bool CanRead { get @@ -72,7 +58,7 @@ namespace System.IO.Compression return false; } - return (_mode == CompressionMode.Decompress && _stream.CanRead); + return _stream.CanRead; } } @@ -80,12 +66,7 @@ namespace System.IO.Compression { get { - if (_stream == null) - { - return false; - } - - return (_mode == CompressionMode.Compress && _stream.CanWrite); + return false; } } @@ -127,7 +108,6 @@ namespace System.IO.Compression public override int Read(byte[] array, int offset, int count) { - EnsureDecompressionMode(); ValidateParameters(array, offset, count); EnsureNotDisposed(); @@ -197,28 +177,6 @@ namespace System.IO.Compression throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed); } - private void EnsureDecompressionMode() - { - if (_mode != CompressionMode.Decompress) - ThrowCannotReadFromDeflateManagedStreamException(); - } - - private static void ThrowCannotReadFromDeflateManagedStreamException() - { - throw new InvalidOperationException(SR.CannotReadFromDeflateStream); - } - - private void EnsureCompressionMode() - { - if (_mode != CompressionMode.Compress) - ThrowCannotWriteToDeflateManagedStreamException(); - } - - private static void ThrowCannotWriteToDeflateManagedStreamException() - { - throw new InvalidOperationException(SR.CannotWriteToDeflateStream); - } - public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); @@ -227,8 +185,6 @@ namespace System.IO.Compression public override Task ReadAsync(byte[] array, int offset, int count, CancellationToken cancellationToken) { - EnsureDecompressionMode(); - // We use this checking order for compat to earlier versions: if (_asyncOperations != 0) throw new InvalidOperationException(SR.InvalidBeginCall); @@ -331,62 +287,7 @@ namespace System.IO.Compression public override void Write(byte[] array, int offset, int count) { - // Validate the state and the parameters - EnsureCompressionMode(); - ValidateParameters(array, offset, count); - EnsureNotDisposed(); - DoMaintenance(array, offset, count); - - // Write compressed the bytes we already passed to the deflater: - - WriteDeflaterOutput(); - - // Pass new bytes through deflater and write them too: - - _deflater.SetInput(array, offset, count); - WriteDeflaterOutput(); - } - - - private void WriteDeflaterOutput() - { - while (!_deflater.NeedsInput()) - { - int compressedBytes = _deflater.GetDeflateOutput(_buffer); - if (compressedBytes > 0) - { - _stream.Write(_buffer, 0, compressedBytes); - } - } - } - - /// - /// Perform deflate-mode maintenance required due to custom header and footer writers - /// (e.g. set by GZipStream). - /// - private void DoMaintenance(byte[] array, int offset, int count) - { - // If no bytes written, do nothing: - if (count <= 0) - return; - - // Note that stream contains more than zero data bytes: - _wroteBytes = true; - - // If no header/footer formatter present, nothing else to do: - if (_formatWriter == null) - return; - - // If formatter has not yet written a header, do it now: - if (!_wroteHeader) - { - byte[] b = _formatWriter.GetHeader(); - _stream.Write(b, 0, b.Length); - _wroteHeader = true; - } - - // Inform formatter of the data bytes written: - _formatWriter.UpdateWithBytesRead(array, offset, count); + throw new InvalidOperationException(SR.CannotWriteToDeflateStream); } // This is called by Dispose: @@ -399,52 +300,6 @@ namespace System.IO.Compression return; Flush(); - - if (_mode != CompressionMode.Compress) - return; - - // Some deflaters (e.g. ZLib) write more than zero bytes for zero byte inputs. - // This round-trips and we should be ok with this, but our legacy managed deflater - // always wrote zero output for zero input and upstack code (e.g. ZipArchiveEntry) - // took dependencies on it. Thus, make sure to only "flush" when we actually had - // some input: - if (_wroteBytes) - { - // Compress any bytes left - WriteDeflaterOutput(); - - // Pull out any bytes left inside deflater: - bool finished; - do - { - int compressedBytes; - finished = _deflater.Finish(_buffer, out compressedBytes); - - if (compressedBytes > 0) - _stream.Write(_buffer, 0, compressedBytes); - } while (!finished); - } - else - { - // In case of zero length buffer, we still need to clean up the native created stream before - // the object get disposed because eventually ZLibNative.ReleaseHandle will get called during - // the dispose operation and although it frees the stream but it return error code because the - // stream state was still marked as in use. The symptoms of this problem will not be seen except - // if running any diagnostic tools which check for disposing safe handle objects - bool finished; - do - { - int compressedBytes; - finished = _deflater.Finish(_buffer, out compressedBytes); - } while (!finished); - } - - // Write format footer: - if (_formatWriter != null && _wroteHeader) - { - byte[] b = _formatWriter.GetFooter(); - _stream.Write(b, 0, b.Length); - } } protected override void Dispose(bool disposing) @@ -469,53 +324,15 @@ namespace System.IO.Compression try { - _deflater?.Dispose(); _inflater?.Dispose(); } finally { - _deflater = null; _inflater = null; base.Dispose(disposing); } } } } - - public override Task WriteAsync(byte[] array, int offset, int count, CancellationToken cancellationToken) - { - EnsureCompressionMode(); - - // We use this checking order for compat to earlier versions: - if (_asyncOperations != 0) - throw new InvalidOperationException(SR.InvalidBeginCall); - - ValidateParameters(array, offset, count); - EnsureNotDisposed(); - - if (cancellationToken.IsCancellationRequested) - return Task.FromCanceled(cancellationToken); - - return WriteAsyncCore(array, offset, count, cancellationToken); - } - - private async Task WriteAsyncCore(byte[] array, int offset, int count, CancellationToken cancellationToken) - { - Interlocked.Increment(ref _asyncOperations); - try - { - await base.WriteAsync(array, offset, count, cancellationToken).ConfigureAwait(false); - } - finally - { - Interlocked.Decrement(ref _asyncOperations); - } - } - - public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); - - public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); } } diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflaterManaged.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflaterManaged.cs deleted file mode 100644 index abab78c..0000000 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflaterManaged.cs +++ /dev/null @@ -1,276 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -// Compression engine - -using System.Diagnostics; - -namespace System.IO.Compression -{ - internal sealed class DeflaterManaged : IDisposable - { - private const int MinBlockSize = 256; - private const int MaxHeaderFooterGoo = 120; - private const int CleanCopySize = DeflateManagedStream.DefaultBufferSize - MaxHeaderFooterGoo; - private const double BadCompressionThreshold = 1.0; - - private readonly FastEncoder _deflateEncoder; - private readonly CopyEncoder _copyEncoder; - - private readonly DeflateInput _input; - private readonly OutputBuffer _output; - private DeflaterState _processingState; - private DeflateInput _inputFromHistory; - - internal DeflaterManaged() - { - _deflateEncoder = new FastEncoder(); - _copyEncoder = new CopyEncoder(); - _input = new DeflateInput(); - _output = new OutputBuffer(); - - _processingState = DeflaterState.NotStarted; - } - - internal bool NeedsInput() => _input.Count == 0 && _deflateEncoder.BytesInHistory == 0; - - /// - /// Sets the input to compress. The only buffer copy occurs when the input is copied - /// to the FastEncoderWindow. - /// - internal void SetInput(byte[] inputBuffer, int startIndex, int count) - { - Debug.Assert(_input.Count == 0, "We have something left in previous input!"); - - _input.Buffer = inputBuffer; - _input.Count = count; - _input.StartIndex = startIndex; - - if (count > 0 && count < MinBlockSize) - { - // user is writing small buffers. If buffer size is below MinBlockSize, we - // need to switch to a small data mode, to avoid block headers and footers - // dominating the output. - switch (_processingState) - { - case DeflaterState.NotStarted: - case DeflaterState.CheckingForIncompressible: - // clean states, needs a block header first - _processingState = DeflaterState.StartingSmallData; - break; - - case DeflaterState.CompressThenCheck: - // already has correct block header - _processingState = DeflaterState.HandlingSmallData; - break; - } - } - } - - internal int GetDeflateOutput(byte[] outputBuffer) - { - Debug.Assert(outputBuffer != null, "Can't pass in a null output buffer!"); - Debug.Assert(!NeedsInput(), "GetDeflateOutput should only be called after providing input"); - - _output.UpdateBuffer(outputBuffer); - - switch (_processingState) - { - case DeflaterState.NotStarted: - { - // first call. Try to compress but if we get bad compression ratio, switch to uncompressed blocks. - Debug.Assert(_deflateEncoder.BytesInHistory == 0, "have leftover bytes in window"); - - // save these in case we need to switch to uncompressed format - DeflateInput.InputState initialInputState = _input.DumpState(); - OutputBuffer.BufferState initialOutputState = _output.DumpState(); - - _deflateEncoder.GetBlockHeader(_output); - _deflateEncoder.GetCompressedData(_input, _output); - - if (!UseCompressed(_deflateEncoder.LastCompressionRatio)) - { - // we're expanding; restore state and switch to uncompressed - _input.RestoreState(initialInputState); - _output.RestoreState(initialOutputState); - _copyEncoder.GetBlock(_input, _output, isFinal: false); - FlushInputWindows(); - _processingState = DeflaterState.CheckingForIncompressible; - } - else - { - _processingState = DeflaterState.CompressThenCheck; - } - - break; - } - case DeflaterState.CompressThenCheck: - { - // continue assuming data is compressible. If we reach data that indicates otherwise - // finish off remaining data in history and decide whether to compress on a - // block-by-block basis - _deflateEncoder.GetCompressedData(_input, _output); - - if (!UseCompressed(_deflateEncoder.LastCompressionRatio)) - { - _processingState = DeflaterState.SlowDownForIncompressible1; - _inputFromHistory = _deflateEncoder.UnprocessedInput; - } - break; - } - case DeflaterState.SlowDownForIncompressible1: - { - // finish off previous compressed block - _deflateEncoder.GetBlockFooter(_output); - - _processingState = DeflaterState.SlowDownForIncompressible2; - goto case DeflaterState.SlowDownForIncompressible2; // yeah I know, but there's no fallthrough - } - - case DeflaterState.SlowDownForIncompressible2: - { - // clear out data from history, but add them as uncompressed blocks - if (_inputFromHistory.Count > 0) - { - _copyEncoder.GetBlock(_inputFromHistory, _output, isFinal: false); - } - - if (_inputFromHistory.Count == 0) - { - // now we're clean - _deflateEncoder.FlushInput(); - _processingState = DeflaterState.CheckingForIncompressible; - } - break; - } - - case DeflaterState.CheckingForIncompressible: - { - // decide whether to compress on a block-by-block basis - Debug.Assert(_deflateEncoder.BytesInHistory == 0, "have leftover bytes in window"); - - // save these in case we need to store as uncompressed - DeflateInput.InputState initialInputState = _input.DumpState(); - OutputBuffer.BufferState initialOutputState = _output.DumpState(); - - // enforce max so we can ensure state between calls - _deflateEncoder.GetBlock(_input, _output, CleanCopySize); - - if (!UseCompressed(_deflateEncoder.LastCompressionRatio)) - { - // we're expanding; restore state and switch to uncompressed - _input.RestoreState(initialInputState); - _output.RestoreState(initialOutputState); - _copyEncoder.GetBlock(_input, _output, isFinal: false); - FlushInputWindows(); - } - - break; - } - - case DeflaterState.StartingSmallData: - { - // add compressed header and data, but not footer. Subsequent calls will keep - // adding compressed data (no header and no footer). We're doing this to - // avoid overhead of header and footer size relative to compressed payload. - _deflateEncoder.GetBlockHeader(_output); - - _processingState = DeflaterState.HandlingSmallData; - goto case DeflaterState.HandlingSmallData; // yeah I know, but there's no fallthrough - } - - case DeflaterState.HandlingSmallData: - { - // continue adding compressed data - _deflateEncoder.GetCompressedData(_input, _output); - break; - } - } - - return _output.BytesWritten; - } - - internal bool Finish(byte[] outputBuffer, out int bytesRead) - { - Debug.Assert(outputBuffer != null, "Can't pass in a null output buffer!"); - Debug.Assert( - _processingState == DeflaterState.NotStarted || - _processingState == DeflaterState.CheckingForIncompressible || - _processingState == DeflaterState.HandlingSmallData || - _processingState == DeflaterState.CompressThenCheck || - _processingState == DeflaterState.SlowDownForIncompressible1, - $"Got unexpected processing state = {_processingState}"); - - Debug.Assert(NeedsInput()); - - // no need to add end of block info if we didn't write anything - if (_processingState == DeflaterState.NotStarted) - { - bytesRead = 0; - return true; - } - - _output.UpdateBuffer(outputBuffer); - - if (_processingState == DeflaterState.CompressThenCheck || - _processingState == DeflaterState.HandlingSmallData || - _processingState == DeflaterState.SlowDownForIncompressible1) - { - // need to finish off block - _deflateEncoder.GetBlockFooter(_output); - } - - // write final block - WriteFinal(); - bytesRead = _output.BytesWritten; - return true; - } - - private bool UseCompressed(double ratio) => ratio <= BadCompressionThreshold; - - private void FlushInputWindows() => _deflateEncoder.FlushInput(); - - private void WriteFinal() => _copyEncoder.GetBlock(input: null, output: _output, isFinal: true); - - // These states allow us to assume that data is compressible and keep compression ratios at least - // as good as historical values, but switch to different handling if that approach may increase the - // data. If we detect we're getting a bad compression ratio, we switch to CheckingForIncompressible - // state and decide to compress on a block by block basis. - // - // If we're getting small data buffers, we want to avoid overhead of excessive header and footer - // info, so we add one header and keep adding blocks as compressed. This means that if the user uses - // small buffers, they won't get the "don't increase size" improvements. - // - // An earlier iteration of this fix handled that data separately by buffering this data until it - // reached a reasonable size, but given that Flush is not implemented on DeflateManagedStream, this meant - // data could be flushed only on Dispose. In the future, it would be reasonable to revisit this, in - // case this isn't breaking. - // - // NotStarted -> CheckingForIncompressible, CompressThenCheck, StartingSmallData - // CompressThenCheck -> SlowDownForIncompressible1 - // SlowDownForIncompressible1 -> SlowDownForIncompressible2 - // SlowDownForIncompressible2 -> CheckingForIncompressible - // StartingSmallData -> HandlingSmallData - private enum DeflaterState - { - // no bytes to write yet - NotStarted, - - // transient states - SlowDownForIncompressible1, - SlowDownForIncompressible2, - StartingSmallData, - - // stable state: may transition to CheckingForIncompressible (via transient states) if it - // appears we're expanding data - CompressThenCheck, - - // sink states - CheckingForIncompressible, - HandlingSmallData - } - - public void Dispose() { } - } -} diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoder.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoder.cs deleted file mode 100644 index b10668c..0000000 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoder.cs +++ /dev/null @@ -1,175 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Diagnostics; - -namespace System.IO.Compression -{ - internal sealed class FastEncoder - { - private readonly FastEncoderWindow _inputWindow; // input history window - private readonly Match _currentMatch; // current match in history window - private double _lastCompressionRatio; - - public FastEncoder() - { - _inputWindow = new FastEncoderWindow(); - _currentMatch = new Match(); - } - - internal int BytesInHistory => _inputWindow.BytesAvailable; - - internal DeflateInput UnprocessedInput => _inputWindow.UnprocessedInput; - - internal void FlushInput() => _inputWindow.FlushWindow(); - - internal double LastCompressionRatio => _lastCompressionRatio; - - // Copy the compressed bytes to output buffer as a block. maxBytesToCopy limits the number of - // bytes we can copy from input. Set to any value < 1 if no limit - internal void GetBlock(DeflateInput input, OutputBuffer output, int maxBytesToCopy) - { - Debug.Assert(InputAvailable(input), "call SetInput before trying to compress!"); - - WriteDeflatePreamble(output); - GetCompressedOutput(input, output, maxBytesToCopy); - WriteEndOfBlock(output); - } - - // Compress data but don't format as block (doesn't have header and footer) - internal void GetCompressedData(DeflateInput input, OutputBuffer output) => - GetCompressedOutput(input, output, maxBytesToCopy:- 1); - - internal void GetBlockHeader(OutputBuffer output) => WriteDeflatePreamble(output); - - internal void GetBlockFooter(OutputBuffer output) => WriteEndOfBlock(output); - - // maxBytesToCopy limits the number of bytes we can copy from input. Set to any value < 1 if no limit - private void GetCompressedOutput(DeflateInput input, OutputBuffer output, int maxBytesToCopy) - { - // snapshot for compression ratio stats - int bytesWrittenPre = output.BytesWritten; - int bytesConsumedFromInput = 0; - int inputBytesPre = BytesInHistory + input.Count; - - do - { - // read more input data into the window if there is space available - int bytesToCopy = (input.Count < _inputWindow.FreeWindowSpace) ? - input.Count : _inputWindow.FreeWindowSpace; - if (maxBytesToCopy >= 1) - { - bytesToCopy = Math.Min(bytesToCopy, maxBytesToCopy - bytesConsumedFromInput); - } - if (bytesToCopy > 0) - { - // copy data into history window - _inputWindow.CopyBytes(input.Buffer, input.StartIndex, bytesToCopy); - input.ConsumeBytes(bytesToCopy); - bytesConsumedFromInput += bytesToCopy; - } - - GetCompressedOutput(output); - } while (SafeToWriteTo(output) && InputAvailable(input) && (maxBytesToCopy < 1 || bytesConsumedFromInput < maxBytesToCopy)); - - // determine compression ratio, save - int bytesWrittenPost = output.BytesWritten; - int bytesWritten = bytesWrittenPost - bytesWrittenPre; - int inputBytesPost = BytesInHistory + input.Count; - int totalBytesConsumed = inputBytesPre - inputBytesPost; - if (bytesWritten != 0) - { - _lastCompressionRatio = (double)bytesWritten / (double)totalBytesConsumed; - } - } - - // compress the bytes in input history window - private void GetCompressedOutput(OutputBuffer output) - { - while (_inputWindow.BytesAvailable > 0 && SafeToWriteTo(output)) - { - // Find next match. A match can be a symbol, - // a distance/length pair, a symbol followed by a distance/Length pair - _inputWindow.GetNextSymbolOrMatch(_currentMatch); - - if (_currentMatch.State == MatchState.HasSymbol) - { - WriteChar(_currentMatch.Symbol, output); - } - else if (_currentMatch.State == MatchState.HasMatch) - { - WriteMatch(_currentMatch.Length, _currentMatch.Position, output); - } - else - { - WriteChar(_currentMatch.Symbol, output); - WriteMatch(_currentMatch.Length, _currentMatch.Position, output); - } - } - } - - private bool InputAvailable(DeflateInput input) => input.Count > 0 || BytesInHistory > 0; - - // Can we safely continue writing to output buffer - private bool SafeToWriteTo(OutputBuffer output) => output.FreeBytes > FastEncoderStatics.MaxCodeLen; - - private void WriteEndOfBlock(OutputBuffer output) - { - // The fast encoder outputs one long block, so it just needs to terminate this block - const int EndOfBlockCode = 256; - uint code_info = FastEncoderStatics.FastEncoderLiteralCodeInfo[EndOfBlockCode]; - int code_len = (int)(code_info & 31); - output.WriteBits(code_len, code_info >> 5); - } - - internal static void WriteMatch(int matchLen, int matchPos, OutputBuffer output) - { - Debug.Assert(matchLen >= FastEncoderWindow.MinMatch && matchLen <= FastEncoderWindow.MaxMatch, "Illegal currentMatch length!"); - - // Get the code information for a match code - uint codeInfo = FastEncoderStatics.FastEncoderLiteralCodeInfo[(FastEncoderStatics.NumChars + 1 - FastEncoderWindow.MinMatch) + matchLen]; - int codeLen = (int)codeInfo & 31; - Debug.Assert(codeLen != 0, "Invalid Match Length!"); - if (codeLen <= 16) - { - output.WriteBits(codeLen, codeInfo >> 5); - } - else - { - output.WriteBits(16, (codeInfo >> 5) & 65535); - output.WriteBits(codeLen - 16, codeInfo >> (5 + 16)); - } - - // Get the code information for a distance code - codeInfo = FastEncoderStatics.FastEncoderDistanceCodeInfo[FastEncoderStatics.GetSlot(matchPos)]; - output.WriteBits((int)(codeInfo & 15), codeInfo >> 8); - int extraBits = (int)(codeInfo >> 4) & 15; - if (extraBits != 0) - { - output.WriteBits(extraBits, (uint)matchPos & FastEncoderStatics.BitMask[extraBits]); - } - } - - internal static void WriteChar(byte b, OutputBuffer output) - { - uint code = FastEncoderStatics.FastEncoderLiteralCodeInfo[b]; - output.WriteBits((int)code & 31, code >> 5); - } - - // Output the block type and tree structure for our hard-coded trees. - // Contains following data: - // "final" block flag 1 bit - // BLOCKTYPE_DYNAMIC 2 bits - // FastEncoderLiteralTreeLength - // FastEncoderDistanceTreeLength - // - internal static void WriteDeflatePreamble(OutputBuffer output) - { - //Debug.Assert( bitCount == 0, "bitCount must be zero before writing tree bit!"); - - output.WriteBytes(FastEncoderStatics.FastEncoderTreeStructureData, 0, FastEncoderStatics.FastEncoderTreeStructureData.Length); - output.WriteBits(FastEncoderStatics.FastEncoderPostTreeBitCount, FastEncoderStatics.FastEncoderPostTreeBitBuf); - } - } -} diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoderWindow.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoderWindow.cs deleted file mode 100644 index 8c0ff14..0000000 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FastEncoderWindow.cs +++ /dev/null @@ -1,445 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Diagnostics; - -namespace System.IO.Compression -{ - internal sealed class FastEncoderWindow - { - private byte[] _window; // complete bytes window - private int _bufPos; // the start index of uncompressed bytes - private int _bufEnd; // the end index of uncompressed bytes - - // Be very careful about increasing the window size; the code tables will have to - // be updated, since they assume that extra_distance_bits is never larger than a - // certain size. - private const int FastEncoderHashShift = 4; - private const int FastEncoderHashtableSize = 2048; - private const int FastEncoderHashMask = FastEncoderHashtableSize - 1; - private const int FastEncoderWindowSize = 8192; - private const int FastEncoderWindowMask = FastEncoderWindowSize - 1; - private const int FastEncoderMatch3DistThreshold = 16384; - internal const int MaxMatch = 258; - internal const int MinMatch = 3; - - // Following constants affect the search, - // they should be modifiable if we support different compression levels in future. - private const int SearchDepth = 32; - private const int GoodLength = 4; - private const int NiceLength = 32; - private const int LazyMatchThreshold = 6; - - // Hashtable structure - private ushort[] _prev; // next most recent occurrence of chars with same hash value - private ushort[] _lookup; // hash table to find most recent occurrence of chars with same hash value - - public FastEncoderWindow() - { - ResetWindow(); - } - - /// Uncompressed bytes. - public int BytesAvailable - { - get - { - Debug.Assert(_bufEnd - _bufPos >= 0, "Ending pointer can't be in front of starting pointer!"); - return _bufEnd - _bufPos; - } - } - - public DeflateInput UnprocessedInput - { - get - { - DeflateInput input = new DeflateInput(); - input.Buffer = _window; - input.StartIndex = _bufPos; - input.Count = _bufEnd - _bufPos; - return input; - } - } - - public void FlushWindow() => ResetWindow(); - - private void ResetWindow() - { - _window = new byte[2 * FastEncoderWindowSize + MaxMatch + 4]; - _prev = new ushort[FastEncoderWindowSize + MaxMatch]; - _lookup = new ushort[FastEncoderHashtableSize]; - _bufPos = FastEncoderWindowSize; - _bufEnd = _bufPos; - } - - public int FreeWindowSpace => 2 * FastEncoderWindowSize - _bufEnd; - - /// Copy bytes from input buffer into window - public void CopyBytes(byte[] inputBuffer, int startIndex, int count) - { - Array.Copy(inputBuffer, startIndex, _window, _bufEnd, count); - _bufEnd += count; - } - - /// Slide the history window to the left by FastEncoderWindowSize bytes. - public void MoveWindows() - { - int i; - Debug.Assert(_bufPos == 2 * FastEncoderWindowSize, "only call this at the end of the window"); - - // verify that the hash table is correct - DebugAssertVerifyHashes(); - - Array.Copy(_window, _bufPos - FastEncoderWindowSize, _window, 0, FastEncoderWindowSize); - - // move all the hash pointers back - for (i = 0; i < FastEncoderHashtableSize; i++) - { - int val = ((int)_lookup[i]) - FastEncoderWindowSize; - - if (val <= 0) - { - // too far away now? then set to zero - _lookup[i] = 0; - } - else - { - _lookup[i] = (ushort)val; - } - } - - // prev[]'s are absolute pointers, not relative pointers, so we have to move them back too - // making prev[]'s into relative pointers poses problems of its own - for (i = 0; i < FastEncoderWindowSize; i++) - { - long val = ((long)_prev[i]) - FastEncoderWindowSize; - - if (val <= 0) - { - _prev[i] = 0; - } - else - { - _prev[i] = (ushort)val; - } - } - -#if DEBUG - // For debugging, wipe the window clean, so that if there is a bug in our hashing, - // the hash pointers will now point to locations which are not valid for the hash value - // (and will be caught by our ASSERTs). - Array.Clear(_window, FastEncoderWindowSize, _window.Length - FastEncoderWindowSize); -#endif - - DebugAssertVerifyHashes(); - - _bufPos = FastEncoderWindowSize; - _bufEnd = _bufPos; - } - - private uint HashValue(uint hash, byte b) => (hash << FastEncoderHashShift) ^ b; - - /// Insert string into hash table and return most recent location of same hash value. - private uint InsertString(ref uint hash) - { - // Note we only use the lowest 11 bits of the hash vallue (hash table size is 11). - // This enables fast calculation of hash value for the input string. - // If we want to get the next hash code starting at next position, - // we can just increment bufPos and call this function. - - hash = HashValue(hash, _window[_bufPos + 2]); - - // Need to assert the hash value - uint search = _lookup[hash & FastEncoderHashMask]; - _lookup[hash & FastEncoderHashMask] = (ushort)_bufPos; - _prev[_bufPos & FastEncoderWindowMask] = (ushort)search; - return search; - } - - /// Insert strings into hashtable. - /// initial hash value - /// 1 + number of strings we need to insert - private void InsertStrings(ref uint hash, int matchLen) - { - Debug.Assert(matchLen > 0, "Invalid match Len!"); - if (_bufEnd - _bufPos <= matchLen) - { - _bufPos += (matchLen - 1); - } - else - { - while (--matchLen > 0) - { - InsertString(ref hash); - _bufPos++; - } - } - } - - /// - /// Find out what we should generate next. It can be a symbol, a distance/length pair - /// or a symbol followed by distance/length pair - /// - internal bool GetNextSymbolOrMatch(Match match) - { - Debug.Assert(_bufPos >= FastEncoderWindowSize && _bufPos < (2 * FastEncoderWindowSize), "Invalid Buffer Position!"); - - // initialise the value of the hash, no problem if locations bufPos, bufPos+1 - // are invalid (not enough data), since we will never insert using that hash value - uint hash = HashValue(0, _window[_bufPos]); - hash = HashValue(hash, _window[_bufPos + 1]); - - int matchLen; - int matchPos = 0; - - DebugAssertVerifyHashes(); - if (_bufEnd - _bufPos <= 3) - { - // The hash value becomes corrupt when we get within 3 characters of the end of the - // input window, since the hash value is based on 3 characters. We just stop - // inserting into the hash table at this point, and allow no matches. - matchLen = 0; - } - else - { - // insert string into hash table and return most recent location of same hash value - int search = (int)InsertString(ref hash); - - // did we find a recent location of this hash value? - if (search != 0) - { - // yes, now find a match at what we'll call position X - matchLen = FindMatch(search, out matchPos, SearchDepth, NiceLength); - - // truncate match if we're too close to the end of the input window - if (_bufPos + matchLen > _bufEnd) - matchLen = _bufEnd - _bufPos; - } - else - { - // no most recent location found - matchLen = 0; - } - } - - if (matchLen < MinMatch) - { - // didn't find a match, so output unmatched char - match.State = MatchState.HasSymbol; - match.Symbol = _window[_bufPos]; - _bufPos++; - } - else - { - // bufPos now points to X+1 - _bufPos++; - - // is this match so good (long) that we should take it automatically without - // checking X+1 ? - if (matchLen <= LazyMatchThreshold) - { - int nextMatchLen; - int nextMatchPos = 0; - - // search at position X+1 - int search = (int)InsertString(ref hash); - - // no, so check for a better match at X+1 - if (search != 0) - { - nextMatchLen = FindMatch(search, out nextMatchPos, - matchLen < GoodLength ? SearchDepth : (SearchDepth >> 2), NiceLength); - - // truncate match if we're too close to the end of the window - // note: nextMatchLen could now be < MinMatch - if (_bufPos + nextMatchLen > _bufEnd) - { - nextMatchLen = _bufEnd - _bufPos; - } - } - else - { - nextMatchLen = 0; - } - - // right now X and X+1 are both inserted into the search tree - if (nextMatchLen > matchLen) - { - // since nextMatchLen > matchLen, it can't be < MinMatch here - - // match at X+1 is better, so output unmatched char at X - match.State = MatchState.HasSymbolAndMatch; - match.Symbol = _window[_bufPos - 1]; - match.Position = nextMatchPos; - match.Length = nextMatchLen; - - // insert remainder of second match into search tree - // example: (*=inserted already) - // - // X X+1 X+2 X+3 X+4 - // * * - // nextmatchlen=3 - // bufPos - // - // If nextMatchLen == 3, we want to perform 2 - // insertions (at X+2 and X+3). However, first we must - // inc bufPos. - // - _bufPos++; // now points to X+2 - matchLen = nextMatchLen; - InsertStrings(ref hash, matchLen); - } - else - { - // match at X is better, so take it - match.State = MatchState.HasMatch; - match.Position = matchPos; - match.Length = matchLen; - - // Insert remainder of first match into search tree, minus the first - // two locations, which were inserted by the FindMatch() calls. - // - // For example, if matchLen == 3, then we've inserted at X and X+1 - // already (and bufPos is now pointing at X+1), and now we need to insert - // only at X+2. - // - matchLen--; - _bufPos++; // now bufPos points to X+2 - InsertStrings(ref hash, matchLen); - } - } - else - { - // match_length >= good_match - // in assertion: bufPos points to X+1, location X inserted already - // first match is so good that we're not even going to check at X+1 - match.State = MatchState.HasMatch; - match.Position = matchPos; - match.Length = matchLen; - - // insert remainder of match at X into search tree - InsertStrings(ref hash, matchLen); - } - } - - if (_bufPos == 2 * FastEncoderWindowSize) - { - MoveWindows(); - } - return true; - } - - /// Find a match starting at specified position and return length of match. - /// where to start searching - /// return match position here - /// # links to traverse - /// stop immediately if we find a match >= NiceLength - /// - private int FindMatch(int search, out int matchPos, int searchDepth, int niceLength) - { - Debug.Assert(_bufPos >= 0 && _bufPos < 2 * FastEncoderWindowSize, "Invalid Buffer position!"); - Debug.Assert(search < _bufPos, "Invalid starting search point!"); - DebugAssertRecalculatedHashesAreEqual(search, _bufPos); - - int bestMatch = 0; // best match length found so far - int bestMatchPos = 0; // absolute match position of best match found - - // the earliest we can look - int earliest = _bufPos - FastEncoderWindowSize; - Debug.Assert(earliest >= 0, "bufPos is less than FastEncoderWindowSize!"); - - byte wantChar = _window[_bufPos]; - while (search > earliest) - { - // make sure all our hash links are valid - DebugAssertRecalculatedHashesAreEqual(search, _bufPos, "Corrupted hash link!"); - - // Start by checking the character that would allow us to increase the match - // length by one. This improves performance quite a bit. - if (_window[search + bestMatch] == wantChar) - { - int j; - - // Now make sure that all the other characters are correct - for (j = 0; j < MaxMatch; j++) - { - if (_window[_bufPos + j] != _window[search + j]) - break; - } - - if (j > bestMatch) - { - bestMatch = j; - bestMatchPos = search; // absolute position - if (j > NiceLength) break; - wantChar = _window[_bufPos + j]; - } - } - - if (--searchDepth == 0) - { - break; - } - - Debug.Assert(_prev[search & FastEncoderWindowMask] < search, "we should always go backwards!"); - - search = _prev[search & FastEncoderWindowMask]; - } - - // doesn't necessarily mean we found a match; bestMatch could be > 0 and < MinMatch - matchPos = _bufPos - bestMatchPos - 1; // convert absolute to relative position - - // don't allow match length 3's which are too far away to be worthwhile - if (bestMatch == 3 && matchPos >= FastEncoderMatch3DistThreshold) - { - return 0; - } - - Debug.Assert(bestMatch < MinMatch || matchPos < FastEncoderWindowSize, "Only find match inside FastEncoderWindowSize"); - return bestMatch; - } - - [Conditional("DEBUG")] - private void DebugAssertVerifyHashes() - { - // This function makes any execution take a *very* long time to complete. - // Disabling for now by using non-"DEBUG" compilation constant. -#if DEBUG && VERIFY_HASHES - for (int i = 0; i < FastEncoderHashtableSize; i++) - { - ushort where = _lookup[i]; - ushort nextWhere; - - while (where != 0 && _bufPos - where < FastEncoderWindowSize) - { - Debug.Assert(RecalculateHash(where) == i, "Incorrect Hashcode!"); - nextWhere = _prev[where & FastEncoderWindowMask]; - if (_bufPos - nextWhere >= FastEncoderWindowSize) - { - break; - } - - Debug.Assert(nextWhere < where, "pointer is messed up!"); - where = nextWhere; - } - } -#endif - } - - [Conditional("DEBUG")] - private void DebugAssertRecalculatedHashesAreEqual(int position1, int position2, string message = "") - { -#if DEBUG - Debug.Assert(RecalculateHash(position1) == RecalculateHash(position2), message); -#endif - } - -#if DEBUG - private uint RecalculateHash(int position) => - (uint)(((_window[position] << (2 * FastEncoderHashShift)) ^ - (_window[position + 1] << FastEncoderHashShift) ^ - (_window[position + 2])) & FastEncoderHashMask); -#endif - } -} diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FileFormats.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FileFormats.cs index f54ad5d..d042bef 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FileFormats.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/FileFormats.cs @@ -4,13 +4,6 @@ namespace System.IO.Compression { - internal interface IFileFormatWriter - { - byte[] GetHeader(); - void UpdateWithBytesRead(byte[] buffer, int offset, int bytesToCopy); - byte[] GetFooter(); - } - internal interface IFileFormatReader { bool ReadHeader(InputBuffer input); diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/Match.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/Match.cs deleted file mode 100644 index 4523edd..0000000 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/Match.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace System.IO.Compression -{ - /// - /// This class represents a match in the history window. - /// - internal sealed class Match - { - internal MatchState State { get; set; } - internal int Position { get; set; } - internal int Length { get; set; } - internal byte Symbol { get; set; } - } -} diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/OutputBuffer.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/OutputBuffer.cs deleted file mode 100644 index 7fbef8b..0000000 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/OutputBuffer.cs +++ /dev/null @@ -1,123 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Diagnostics; - -namespace System.IO.Compression -{ - internal sealed class OutputBuffer - { - private byte[] _byteBuffer; // buffer for storing bytes - private int _pos; // position - private uint _bitBuf; // store uncomplete bits - private int _bitCount; // number of bits in bitBuffer - - // set the output buffer we will be using - internal void UpdateBuffer(byte[] output) - { - _byteBuffer = output; - _pos = 0; - } - - internal int BytesWritten => _pos; - - internal int FreeBytes => _byteBuffer.Length - _pos; - - internal void WriteUInt16(ushort value) - { - Debug.Assert(FreeBytes >= 2, "No enough space in output buffer!"); - - _byteBuffer[_pos++] = (byte)value; - _byteBuffer[_pos++] = (byte)(value >> 8); - } - - internal void WriteBits(int n, uint bits) - { - Debug.Assert(n <= 16, "length must be larger than 16!"); - _bitBuf |= bits << _bitCount; - _bitCount += n; - if (_bitCount >= 16) - { - Debug.Assert(_byteBuffer.Length - _pos >= 2, "No enough space in output buffer!"); - _byteBuffer[_pos++] = unchecked((byte)_bitBuf); - _byteBuffer[_pos++] = unchecked((byte)(_bitBuf >> 8)); - _bitCount -= 16; - _bitBuf >>= 16; - } - } - - /// Write the bits left in the output as bytes. - internal void FlushBits() - { - // flush bits from bit buffer to output buffer - while (_bitCount >= 8) - { - _byteBuffer[_pos++] = unchecked((byte)_bitBuf); - _bitCount -= 8; - _bitBuf >>= 8; - } - - if (_bitCount > 0) - { - _byteBuffer[_pos++] = unchecked((byte)_bitBuf); - _bitBuf = 0; - _bitCount = 0; - } - } - - internal void WriteBytes(byte[] byteArray, int offset, int count) - { - Debug.Assert(FreeBytes >= count, "Not enough space in output buffer!"); - // faster - if (_bitCount == 0) - { - Array.Copy(byteArray, offset, _byteBuffer, _pos, count); - _pos += count; - } - else - { - WriteBytesUnaligned(byteArray, offset, count); - } - } - - private void WriteBytesUnaligned(byte[] byteArray, int offset, int count) - { - for (int i = 0; i < count; i++) - { - byte b = byteArray[offset + i]; - WriteByteUnaligned(b); - } - } - - private void WriteByteUnaligned(byte b) - { - WriteBits(8, b); - } - - internal int BitsInBuffer => (_bitCount / 8) + 1; - - internal BufferState DumpState() => new BufferState(_pos, _bitBuf, _bitCount); - - internal void RestoreState(BufferState state) - { - _pos = state._pos; - _bitBuf = state._bitBuf; - _bitCount = state._bitCount; - } - - internal readonly struct BufferState - { - internal readonly int _pos; // position - internal readonly uint _bitBuf; // store uncomplete bits - internal readonly int _bitCount; // number of bits in bitBuffer - - internal BufferState(int pos, uint bitBuf, int bitCount) - { - _pos = pos; - _bitBuf = bitBuf; - _bitCount = bitCount; - } - } - } -} diff --git a/src/libraries/System.Linq.Expressions/src/ILLinkTrim.xml b/src/libraries/System.Linq.Expressions/src/ILLinkTrim.xml index 4f96aaa..bf4e3f9 100644 --- a/src/libraries/System.Linq.Expressions/src/ILLinkTrim.xml +++ b/src/libraries/System.Linq.Expressions/src/ILLinkTrim.xml @@ -4,5 +4,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/CallSite.cs b/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/CallSite.cs index 5ad8d72..da4998c 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/CallSite.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/CallSite.cs @@ -236,24 +236,6 @@ namespace System.Runtime.CompilerServices return addr; } - /// - /// Clears the rule cache ... used by the call site tests. - /// - private void ClearRuleCache() - { - // make sure it initialized/atomized etc... - Binder.GetRuleCache(); - - Dictionary cache = Binder.Cache; - - if (cache != null) - { - lock (cache) - { - cache.Clear(); - } - } - } private const int MaxRules = 10; diff --git a/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/RuleCache.cs b/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/RuleCache.cs index 236b8af..d184444 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/RuleCache.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/RuleCache.cs @@ -74,24 +74,6 @@ namespace System.Runtime.CompilerServices } } - internal void ReplaceRule(T oldRule, T newRule) - { - // need a lock to make sure we are replacing the right rule - lock (_cacheLock) - { - int i = Array.IndexOf(_rules, oldRule); - if (i >= 0) - { - _rules[i] = newRule; - return; // DONE - } - - // could not find it. - _rules = AddOrInsert(_rules, newRule); - } - } - - // Adds to end or inserts items at InsertPosition private const int InsertPosition = MaxRules / 2; diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs index ad45b3c..1cfe0ad 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs @@ -118,14 +118,6 @@ namespace System.Net.WebSockets } } - internal static bool LoggingEnabled - { - get - { - return NetEventSource.IsEnabled; - } - } - public override WebSocketState State { get @@ -1196,15 +1188,6 @@ namespace System.Net.WebSockets _internalBuffer.ValidateNativeBuffers(action, bufferType, dataBuffers, dataBufferCount); } - internal void ThrowIfClosedOrAborted() - { - if (State == WebSocketState.Closed || State == WebSocketState.Aborted) - { - throw new WebSocketException(WebSocketError.InvalidState, - SR.Format(SR.net_WebSockets_InvalidState_ClosedOrAborted, GetType().FullName, State)); - } - } - private void ThrowIfAborted(bool aborted, Exception innerException) { if (aborted) diff --git a/src/libraries/System.Net.Primitives/src/ILLinkTrim.xml b/src/libraries/System.Net.Primitives/src/ILLinkTrim.xml new file mode 100644 index 0000000..16a2b6e --- /dev/null +++ b/src/libraries/System.Net.Primitives/src/ILLinkTrim.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/libraries/System.Private.Xml/src/ILLinkTrim.xml b/src/libraries/System.Private.Xml/src/ILLinkTrim.xml new file mode 100644 index 0000000..012ea05 --- /dev/null +++ b/src/libraries/System.Private.Xml/src/ILLinkTrim.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs index f36621e..abec82c 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs @@ -1846,8 +1846,7 @@ namespace System.Xml } // !!!!!! - // NOTE: This method is called via reflection from System.Data.dll and from Analysis Services in Yukon. - // Do not change its signature without notifying the appropriate teams! + // NOTE: This method is called via reflection from System.Data.Common.dll. // !!!!!! internal static XmlReader CreateSqlReader(Stream input, XmlReaderSettings settings, XmlParserContext inputContext) { diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlValueConverter.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlValueConverter.cs index 8c61eb7..45451ca 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlValueConverter.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlValueConverter.cs @@ -2510,98 +2510,6 @@ namespace System.Xml.Schema } } - internal class XmlNodeConverter : XmlBaseConverter - { - protected XmlNodeConverter() : base(XmlTypeCode.Node) - { - } - - public static readonly XmlValueConverter Node = new XmlNodeConverter(); - - #region AUTOGENERATED_XMLNODECONVERTER - - //----------------------------------------------- - // ToBoolean - //----------------------------------------------- - - // This converter does not support conversions to Boolean. - - - //----------------------------------------------- - // ToDateTime - //----------------------------------------------- - - // This converter does not support conversions to DateTime. - - - //----------------------------------------------- - // ToDecimal - //----------------------------------------------- - - // This converter does not support conversions to Decimal. - - - //----------------------------------------------- - // ToDouble - //----------------------------------------------- - - // This converter does not support conversions to Double. - - - //----------------------------------------------- - // ToInt32 - //----------------------------------------------- - - // This converter does not support conversions to Int32. - - - //----------------------------------------------- - // ToInt64 - //----------------------------------------------- - - // This converter does not support conversions to Int64. - - - //----------------------------------------------- - // ToSingle - //----------------------------------------------- - - // This converter does not support conversions to Single. - - - //----------------------------------------------- - // ToString - //----------------------------------------------- - - // This converter does not support conversions to String. - - - //----------------------------------------------- - // ChangeType - //----------------------------------------------- - - public override object ChangeType(object value, Type destinationType, IXmlNamespaceResolver nsResolver) - { - if (value == null) throw new ArgumentNullException(nameof(value)); - if (destinationType == null) throw new ArgumentNullException(nameof(destinationType)); - - Type sourceType = value.GetType(); - - if (destinationType == ObjectType) destinationType = DefaultClrType; - if (destinationType == XPathNavigatorType) - { - if (IsDerivedFrom(sourceType, XPathNavigatorType)) return ((XPathNavigator)value); - } - if (destinationType == XPathItemType) - { - if (IsDerivedFrom(sourceType, XPathNavigatorType)) return ((XPathItem)value); - } - - return ChangeListType(value, destinationType, nsResolver); - } - #endregion - } - internal class XmlAnyConverter : XmlBaseConverter { protected XmlAnyConverter(XmlTypeCode typeCode) : base(typeCode) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryType.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryType.cs index 87ffd51..6fc5f4e 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryType.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryType.cs @@ -112,12 +112,6 @@ namespace System.Xml.Xsl /// public abstract bool IsDod { get; } - /// - /// The XmlValueConverter maps each XmlQueryType to various Clr types which are capable of representing it. - /// - public abstract XmlValueConverter ClrMapping { get; } - - //----------------------------------------------- // Type Operations //----------------------------------------------- diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs index 522e378..f906e21 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XmlQueryTypeFactory.cs @@ -652,27 +652,6 @@ namespace System.Xml.Xsl get { return this; } } - /// - /// Return the item's converter. - /// - public override XmlValueConverter ClrMapping - { - get - { - // Return value converter from XmlSchemaType if type is atomic - if (IsAtomicValue) - return SchemaType.ValueConverter; - - // Return node converter if item must be a node - if (IsNode) - return XmlNodeConverter.Node; - - // Otherwise return item converter - return XmlAnyConverter.Item; - } - } - - //----------------------------------------------- // ListBase implementation //----------------------------------------------- @@ -940,23 +919,6 @@ namespace System.Xml.Xsl get { return this; } } - /// - /// Always return the item converter. - /// - public override XmlValueConverter ClrMapping - { - get - { - if (_code == XmlTypeCode.None || _code == XmlTypeCode.Item) - return XmlAnyConverter.Item; - - if (IsAtomicValue) - return SchemaType.ValueConverter; - - return XmlNodeConverter.Node; - } - } - //----------------------------------------------- // ListBase implementation //----------------------------------------------- @@ -1146,21 +1108,6 @@ namespace System.Xml.Xsl get { return _prime; } } - /// - /// Return the prime's converter wrapped in a list converter. - /// - public override XmlValueConverter ClrMapping - { - get - { - if (_converter == null) - _converter = XmlListConverter.Create(_prime.ClrMapping); - - return _converter; - } - } - - //----------------------------------------------- // ListBase implementation //----------------------------------------------- diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryEnums.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryEnums.cs index 87bf952..4a519a3 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryEnums.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryEnums.cs @@ -157,19 +157,4 @@ namespace System.Runtime.Serialization.Formatters.Binary Header = 2, Member = 3, } - - // name space - internal enum InternalNameSpaceE - { - None = 0, - Soap = 1, - XdrPrimitive = 2, - XdrString = 3, - UrtSystem = 4, - UrtUser = 5, - UserNameSpace = 6, - MemberName = 7, - Interop = 8, - CallElement = 9 - } } diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/Converter.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/Converter.cs index d3dcad0..1639862 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/Converter.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/Converter.cs @@ -120,64 +120,6 @@ namespace System.Runtime.Serialization.Formatters.Binary } } - internal static InternalNameSpaceE GetNameSpaceEnum(InternalPrimitiveTypeE code, Type type, WriteObjectInfo objectInfo, out string typeName) - { - InternalNameSpaceE nameSpaceEnum = InternalNameSpaceE.None; - typeName = null; - - if (code != InternalPrimitiveTypeE.Invalid) - { - switch (code) - { - case InternalPrimitiveTypeE.Boolean: - case InternalPrimitiveTypeE.Char: - case InternalPrimitiveTypeE.Byte: - case InternalPrimitiveTypeE.Double: - case InternalPrimitiveTypeE.Int16: - case InternalPrimitiveTypeE.Int32: - case InternalPrimitiveTypeE.Int64: - case InternalPrimitiveTypeE.SByte: - case InternalPrimitiveTypeE.Single: - case InternalPrimitiveTypeE.UInt16: - case InternalPrimitiveTypeE.UInt32: - case InternalPrimitiveTypeE.UInt64: - case InternalPrimitiveTypeE.DateTime: - case InternalPrimitiveTypeE.TimeSpan: - nameSpaceEnum = InternalNameSpaceE.XdrPrimitive; - typeName = "System." + ToComType(code); - break; - - case InternalPrimitiveTypeE.Decimal: - nameSpaceEnum = InternalNameSpaceE.UrtSystem; - typeName = "System." + ToComType(code); - break; - } - } - - if ((nameSpaceEnum == InternalNameSpaceE.None) && type != null) - { - if (ReferenceEquals(type, s_typeofString)) - { - nameSpaceEnum = InternalNameSpaceE.XdrString; - } - else - { - if (objectInfo == null) - { - typeName = type.FullName; - nameSpaceEnum = type.Assembly == s_urtAssembly ? InternalNameSpaceE.UrtSystem : InternalNameSpaceE.UrtUser; - } - else - { - typeName = objectInfo.GetTypeFullName(); - nameSpaceEnum = objectInfo.GetAssemblyString().Equals(s_urtAssemblyString) ? InternalNameSpaceE.UrtSystem : InternalNameSpaceE.UrtUser; - } - } - } - - return nameSpaceEnum; - } - internal static Type ToArrayType(InternalPrimitiveTypeE code) { if (s_arrayTypeA == null) diff --git a/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/SID.cs b/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/SID.cs index 32a4412..e739f40 100644 --- a/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/SID.cs +++ b/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/SID.cs @@ -665,21 +665,6 @@ nameof(binaryForm)); CreateFromBinaryForm(resultSid, 0); } - internal SecurityIdentifier(SecurityIdentifier domainSid, uint rid) - { - int i; - int[] SubAuthorities = new int[domainSid.SubAuthorityCount + 1]; - - for (i = 0; i < domainSid.SubAuthorityCount; i++) - { - SubAuthorities[i] = domainSid.GetSubAuthority(i); - } - - SubAuthorities[i] = (int)rid; - - CreateFromParts(domainSid.IdentifierAuthority, SubAuthorities); - } - internal SecurityIdentifier(IdentifierAuthority identifierAuthority, int[] subAuthorities) { CreateFromParts(identifierAuthority, subAuthorities); diff --git a/src/libraries/System.Text.Json/src/ILLinkTrim.xml b/src/libraries/System.Text.Json/src/ILLinkTrim.xml new file mode 100644 index 0000000..3ba61ee --- /dev/null +++ b/src/libraries/System.Text.Json/src/ILLinkTrim.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + -- 2.7.4