| | | 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 | | // This class can be used to read bits from an byte array quickly. |
| | | 9 | | // Normally we get bits from 'bitBuffer' field and bitsInBuffer stores |
| | | 10 | | // the number of bits available in 'BitBuffer'. |
| | | 11 | | // When we used up the bits in bitBuffer, we will try to get byte from |
| | | 12 | | // the byte array and copy the byte to appropriate position in bitBuffer. |
| | | 13 | | // |
| | | 14 | | // The byte array is not reused. We will go from 'start' to 'end'. |
| | | 15 | | // When we reach the end, most read operations will return -1, |
| | | 16 | | // which means we are running out of input. |
| | | 17 | | |
| | | 18 | | internal sealed class InputBuffer |
| | | 19 | | { |
| | | 20 | | private Memory<byte> _buffer; // memory to store input |
| | | 21 | | private uint _bitBuffer; // store the bits here, we can quickly shift in this buffer |
| | | 22 | | private int _bitsInBuffer; // number of bits available in bitBuffer |
| | | 23 | | |
| | | 24 | | /// <summary>Total bits available in the input buffer.</summary> |
| | 0 | 25 | | public int AvailableBits => _bitsInBuffer; |
| | | 26 | | |
| | | 27 | | /// <summary>Total bytes available in the input buffer.</summary> |
| | 0 | 28 | | public int AvailableBytes => _buffer.Length + (_bitsInBuffer / 8); |
| | | 29 | | |
| | | 30 | | /// <summary>Ensure that count bits are in the bit buffer.</summary> |
| | | 31 | | /// <param name="count">Can be up to 16.</param> |
| | | 32 | | /// <returns>Returns false if input is not sufficient to make this true.</returns> |
| | | 33 | | public bool EnsureBitsAvailable(int count) |
| | 0 | 34 | | { |
| | 0 | 35 | | Debug.Assert(0 < count && count <= 16, "count is invalid."); |
| | | 36 | | |
| | | 37 | | // manual inlining to improve perf |
| | 0 | 38 | | if (_bitsInBuffer < count) |
| | 0 | 39 | | { |
| | 0 | 40 | | if (NeedsInput()) |
| | 0 | 41 | | { |
| | 0 | 42 | | return false; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | // insert a byte to bitbuffer |
| | 0 | 46 | | _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer; |
| | 0 | 47 | | _buffer = _buffer.Slice(1); |
| | 0 | 48 | | _bitsInBuffer += 8; |
| | | 49 | | |
| | 0 | 50 | | if (_bitsInBuffer < count) |
| | 0 | 51 | | { |
| | 0 | 52 | | if (NeedsInput()) |
| | 0 | 53 | | { |
| | 0 | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | // insert a byte to bitbuffer |
| | 0 | 57 | | _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer; |
| | 0 | 58 | | _buffer = _buffer.Slice(1); |
| | 0 | 59 | | _bitsInBuffer += 8; |
| | 0 | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | return true; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// This function will try to load 16 or more bits into bitBuffer. |
| | | 68 | | /// It returns whatever is contained in bitBuffer after loading. |
| | | 69 | | /// The main difference between this and GetBits is that this will |
| | | 70 | | /// never return -1. So the caller needs to check AvailableBits to |
| | | 71 | | /// see how many bits are available. |
| | | 72 | | /// </summary> |
| | | 73 | | public uint TryLoad16Bits() |
| | 0 | 74 | | { |
| | 0 | 75 | | if (_bitsInBuffer < 8) |
| | 0 | 76 | | { |
| | 0 | 77 | | if (_buffer.Length > 1) |
| | 0 | 78 | | { |
| | 0 | 79 | | Span<byte> span = _buffer.Span; |
| | 0 | 80 | | _bitBuffer |= (uint)span[0] << _bitsInBuffer; |
| | 0 | 81 | | _bitBuffer |= (uint)span[1] << (_bitsInBuffer + 8); |
| | 0 | 82 | | _buffer = _buffer.Slice(2); |
| | 0 | 83 | | _bitsInBuffer += 16; |
| | 0 | 84 | | } |
| | 0 | 85 | | else if (_buffer.Length != 0) |
| | 0 | 86 | | { |
| | 0 | 87 | | _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer; |
| | 0 | 88 | | _buffer = _buffer.Slice(1); |
| | 0 | 89 | | _bitsInBuffer += 8; |
| | 0 | 90 | | } |
| | 0 | 91 | | } |
| | 0 | 92 | | else if (_bitsInBuffer < 16) |
| | 0 | 93 | | { |
| | 0 | 94 | | if (!_buffer.IsEmpty) |
| | 0 | 95 | | { |
| | 0 | 96 | | _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer; |
| | 0 | 97 | | _buffer = _buffer.Slice(1); |
| | 0 | 98 | | _bitsInBuffer += 8; |
| | 0 | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | return _bitBuffer; |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | private static uint GetBitMask(int count) => ((uint)1 << count) - 1; |
| | | 106 | | |
| | | 107 | | /// <summary>Gets count bits from the input buffer. Returns -1 if not enough bits available.</summary> |
| | | 108 | | public int GetBits(int count) |
| | 0 | 109 | | { |
| | 0 | 110 | | Debug.Assert(0 < count && count <= 16, "count is invalid."); |
| | | 111 | | |
| | 0 | 112 | | if (!EnsureBitsAvailable(count)) |
| | 0 | 113 | | { |
| | 0 | 114 | | return -1; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | int result = (int)(_bitBuffer & GetBitMask(count)); |
| | 0 | 118 | | _bitBuffer >>= count; |
| | 0 | 119 | | _bitsInBuffer -= count; |
| | 0 | 120 | | return result; |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Copies bytes from input buffer to output buffer. |
| | | 125 | | /// You have to make sure, that the buffer is byte aligned. If not enough bytes are |
| | | 126 | | /// available, copies fewer bytes. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <returns>Returns the number of bytes copied, 0 if no byte is available.</returns> |
| | | 129 | | public int CopyTo(Memory<byte> output) |
| | 0 | 130 | | { |
| | 0 | 131 | | Debug.Assert(_bitsInBuffer % 8 == 0); |
| | | 132 | | |
| | | 133 | | // Copy the bytes in bitBuffer first. |
| | 0 | 134 | | int bytesFromBitBuffer = 0; |
| | 0 | 135 | | while (_bitsInBuffer > 0 && !output.IsEmpty) |
| | 0 | 136 | | { |
| | 0 | 137 | | output.Span[0] = (byte)_bitBuffer; |
| | 0 | 138 | | output = output.Slice(1); |
| | 0 | 139 | | _bitBuffer >>= 8; |
| | 0 | 140 | | _bitsInBuffer -= 8; |
| | 0 | 141 | | bytesFromBitBuffer++; |
| | 0 | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | if (output.IsEmpty) |
| | 0 | 145 | | { |
| | 0 | 146 | | return bytesFromBitBuffer; |
| | | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | int length = Math.Min(output.Length, _buffer.Length); |
| | 0 | 150 | | _buffer.Slice(0, length).CopyTo(output); |
| | 0 | 151 | | _buffer = _buffer.Slice(length); |
| | 0 | 152 | | return bytesFromBitBuffer + length; |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Copies length bytes from input buffer to output buffer starting at output[offset]. |
| | | 157 | | /// You have to make sure, that the buffer is byte aligned. If not enough bytes are |
| | | 158 | | /// available, copies fewer bytes. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <returns>Returns the number of bytes copied, 0 if no byte is available.</returns> |
| | | 161 | | public int CopyTo(byte[] output, int offset, int length) |
| | 0 | 162 | | { |
| | 0 | 163 | | Debug.Assert(output != null); |
| | 0 | 164 | | Debug.Assert(offset >= 0); |
| | 0 | 165 | | Debug.Assert(length >= 0); |
| | 0 | 166 | | Debug.Assert(offset <= output.Length - length); |
| | 0 | 167 | | Debug.Assert((_bitsInBuffer % 8) == 0); |
| | | 168 | | |
| | 0 | 169 | | return CopyTo(output.AsMemory(offset, length)); |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Return true is all input bytes are used. |
| | | 174 | | /// This means the caller can call SetInput to add more input. |
| | | 175 | | /// </summary> |
| | 0 | 176 | | public bool NeedsInput() => _buffer.IsEmpty; |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Set the byte buffer to be processed. |
| | | 180 | | /// All the bits remained in bitbuffer will be processed before the new bytes. |
| | | 181 | | /// We don't clone the byte buffer here since it is expensive. |
| | | 182 | | /// The caller should make sure after a buffer is passed in, that |
| | | 183 | | /// it will not be changed before calling this function again. |
| | | 184 | | /// </summary> |
| | | 185 | | public void SetInput(Memory<byte> buffer) |
| | 0 | 186 | | { |
| | 0 | 187 | | if (_buffer.IsEmpty) |
| | 0 | 188 | | { |
| | 0 | 189 | | _buffer = buffer; |
| | 0 | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Set the byte array to be processed. |
| | | 195 | | /// All the bits remained in bitBuffer will be processed before the new bytes. |
| | | 196 | | /// We don't clone the byte array here since it is expensive. |
| | | 197 | | /// The caller should make sure after a buffer is passed in. |
| | | 198 | | /// It will not be changed before calling this function again. |
| | | 199 | | /// </summary> |
| | | 200 | | public void SetInput(byte[] buffer, int offset, int length) |
| | 0 | 201 | | { |
| | 0 | 202 | | Debug.Assert(buffer != null); |
| | 0 | 203 | | Debug.Assert(offset >= 0); |
| | 0 | 204 | | Debug.Assert(length >= 0); |
| | 0 | 205 | | Debug.Assert(offset <= buffer.Length - length); |
| | | 206 | | |
| | 0 | 207 | | SetInput(buffer.AsMemory(offset, length)); |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary>Skip n bits in the buffer.</summary> |
| | | 211 | | public void SkipBits(int n) |
| | 0 | 212 | | { |
| | 0 | 213 | | Debug.Assert(_bitsInBuffer >= n, "No enough bits in the buffer, Did you call EnsureBitsAvailable?"); |
| | 0 | 214 | | _bitBuffer >>= n; |
| | 0 | 215 | | _bitsInBuffer -= n; |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary>Skips to the next byte boundary.</summary> |
| | | 219 | | public void SkipToByteBoundary() |
| | 0 | 220 | | { |
| | 0 | 221 | | _bitBuffer >>= (_bitsInBuffer % 8); |
| | 0 | 222 | | _bitsInBuffer -= (_bitsInBuffer % 8); |
| | 0 | 223 | | } |
| | | 224 | | } |
| | | 225 | | } |