| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.IO.Compression |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// This class maintains a window for decompressed output. |
| | | 10 | | /// We need to keep this because the decompressed information can be |
| | | 11 | | /// a literal or a length/distance pair. For length/distance pair, |
| | | 12 | | /// we need to look back in the output window and copy bytes from there. |
| | | 13 | | /// We use a byte array of WindowSize circularly. |
| | | 14 | | /// </summary> |
| | | 15 | | internal sealed class OutputWindow |
| | | 16 | | { |
| | | 17 | | // With Deflate64 we can have up to a 65538 length as well as up to a 65536 distance. This means we need a Windo |
| | | 18 | | // least 131074 bytes long so we have space to retrieve up to a full 64kb in lookback and place it in our buffer |
| | | 19 | | // overwriting existing data. OutputWindow requires that the WindowSize be an exponent of 2, so we round up to 2 |
| | | 20 | | private const int WindowSize = 262144; |
| | | 21 | | private const int WindowMask = 262143; |
| | | 22 | | |
| | 0 | 23 | | private readonly byte[] _window = new byte[WindowSize]; // The window is 2^18 bytes |
| | | 24 | | private int _end; // this is the position to where we should write next byte |
| | | 25 | | private int _bytesUsed; // The number of bytes in the output window which is not consumed. |
| | | 26 | | |
| | | 27 | | internal void ClearBytesUsed() |
| | 0 | 28 | | { |
| | 0 | 29 | | _bytesUsed = 0; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>Add a byte to output window.</summary> |
| | | 33 | | public void Write(byte b) |
| | 0 | 34 | | { |
| | 0 | 35 | | Debug.Assert(_bytesUsed < WindowSize, "Can't add byte when window is full!"); |
| | 0 | 36 | | _window[_end++] = b; |
| | 0 | 37 | | _end &= WindowMask; |
| | 0 | 38 | | ++_bytesUsed; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | public void WriteLengthDistance(int length, int distance) |
| | 0 | 42 | | { |
| | 0 | 43 | | Debug.Assert((_bytesUsed + length) <= WindowSize, "No Enough space"); |
| | | 44 | | |
| | | 45 | | // move backwards distance bytes in the output stream, |
| | | 46 | | // and copy length bytes from this position to the output stream. |
| | 0 | 47 | | _bytesUsed += length; |
| | 0 | 48 | | int copyStart = (_end - distance) & WindowMask; // start position for coping. |
| | | 49 | | |
| | 0 | 50 | | int border = WindowSize - length; |
| | 0 | 51 | | if (copyStart <= border && _end < border) |
| | 0 | 52 | | { |
| | 0 | 53 | | if (length <= distance) |
| | 0 | 54 | | { |
| | 0 | 55 | | Array.Copy(_window, copyStart, _window, _end, length); |
| | 0 | 56 | | _end += length; |
| | 0 | 57 | | } |
| | | 58 | | else |
| | 0 | 59 | | { |
| | | 60 | | // The referenced string may overlap the current |
| | | 61 | | // position; for example, if the last 2 bytes decoded have values |
| | | 62 | | // X and Y, a string reference with <length = 5, distance = 2> |
| | | 63 | | // adds X,Y,X,Y,X to the output stream. |
| | 0 | 64 | | while (length-- > 0) |
| | 0 | 65 | | { |
| | 0 | 66 | | _window[_end++] = _window[copyStart++]; |
| | 0 | 67 | | } |
| | 0 | 68 | | } |
| | 0 | 69 | | } |
| | | 70 | | else |
| | 0 | 71 | | { |
| | | 72 | | // copy byte by byte |
| | 0 | 73 | | while (length-- > 0) |
| | 0 | 74 | | { |
| | 0 | 75 | | _window[_end++] = _window[copyStart++]; |
| | 0 | 76 | | _end &= WindowMask; |
| | 0 | 77 | | copyStart &= WindowMask; |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Copy up to length of bytes from input directly. |
| | | 84 | | /// This is used for uncompressed block. |
| | | 85 | | /// </summary> |
| | | 86 | | public int CopyFrom(InputBuffer input, int length) |
| | 0 | 87 | | { |
| | 0 | 88 | | length = Math.Min(Math.Min(length, WindowSize - _bytesUsed), input.AvailableBytes); |
| | | 89 | | int copied; |
| | | 90 | | |
| | | 91 | | // We might need wrap around to copy all bytes. |
| | 0 | 92 | | int tailLen = WindowSize - _end; |
| | 0 | 93 | | if (length > tailLen) |
| | 0 | 94 | | { |
| | | 95 | | // copy the first part |
| | 0 | 96 | | copied = input.CopyTo(_window, _end, tailLen); |
| | 0 | 97 | | if (copied == tailLen) |
| | 0 | 98 | | { |
| | | 99 | | // only try to copy the second part if we have enough bytes in input |
| | 0 | 100 | | copied += input.CopyTo(_window, 0, length - tailLen); |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | else |
| | 0 | 104 | | { |
| | | 105 | | // only one copy is needed if there is no wrap around. |
| | 0 | 106 | | copied = input.CopyTo(_window, _end, length); |
| | 0 | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | _end = (_end + copied) & WindowMask; |
| | 0 | 110 | | _bytesUsed += copied; |
| | 0 | 111 | | return copied; |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary>Free space in output window.</summary> |
| | 0 | 115 | | public int FreeBytes => WindowSize - _bytesUsed; |
| | | 116 | | |
| | | 117 | | /// <summary>Bytes not consumed in output window.</summary> |
| | 0 | 118 | | public int AvailableBytes => _bytesUsed; |
| | | 119 | | |
| | | 120 | | /// <summary>Copy the decompressed bytes to output buffer.</summary> |
| | | 121 | | public int CopyTo(Span<byte> output) |
| | 0 | 122 | | { |
| | | 123 | | int copy_end; |
| | | 124 | | |
| | 0 | 125 | | if (output.Length > _bytesUsed) |
| | 0 | 126 | | { |
| | | 127 | | // we can copy all the decompressed bytes out |
| | 0 | 128 | | copy_end = _end; |
| | 0 | 129 | | output = output.Slice(0, _bytesUsed); |
| | 0 | 130 | | } |
| | | 131 | | else |
| | 0 | 132 | | { |
| | 0 | 133 | | copy_end = (_end - _bytesUsed + output.Length) & WindowMask; // copy length of bytes |
| | 0 | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | int copied = output.Length; |
| | | 137 | | |
| | 0 | 138 | | int tailLen = output.Length - copy_end; |
| | 0 | 139 | | if (tailLen > 0) |
| | 0 | 140 | | { |
| | | 141 | | // this means we need to copy two parts separately |
| | | 142 | | // copy the taillen bytes from the end of the output window |
| | 0 | 143 | | _window.AsSpan(WindowSize - tailLen, tailLen).CopyTo(output); |
| | 0 | 144 | | output = output.Slice(tailLen, copy_end); |
| | 0 | 145 | | } |
| | 0 | 146 | | _window.AsSpan(copy_end - output.Length, output.Length).CopyTo(output); |
| | 0 | 147 | | _bytesUsed -= copied; |
| | 0 | 148 | | Debug.Assert(_bytesUsed >= 0, "check this function and find why we copied more bytes than we have"); |
| | 0 | 149 | | return copied; |
| | 0 | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |