123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System;
- using System.Diagnostics;
- namespace SharpCompress.Compressors.Deflate64
- {
-
-
-
-
-
-
-
- internal sealed class OutputWindow
- {
-
-
-
- private const int WINDOW_SIZE = 262144;
- private const int WINDOW_MASK = 262143;
- private readonly byte[] _window = new byte[WINDOW_SIZE];
- private int _end;
- private int _bytesUsed;
-
- public void Write(byte b)
- {
- Debug.Assert(_bytesUsed < WINDOW_SIZE, "Can't add byte when window is full!");
- _window[_end++] = b;
- _end &= WINDOW_MASK;
- ++_bytesUsed;
- }
- public void WriteLengthDistance(int length, int distance)
- {
- Debug.Assert((_bytesUsed + length) <= WINDOW_SIZE, "No Enough space");
-
-
-
- _bytesUsed += length;
- int copyStart = (_end - distance) & WINDOW_MASK;
- int border = WINDOW_SIZE - length;
- if (copyStart <= border && _end < border)
- {
- if (length <= distance)
- {
- Array.Copy(_window, copyStart, _window, _end, length);
- _end += length;
- }
- else
- {
-
-
-
-
- while (length-- > 0)
- {
- _window[_end++] = _window[copyStart++];
- }
- }
- }
- else
- {
-
- while (length-- > 0)
- {
- _window[_end++] = _window[copyStart++];
- _end &= WINDOW_MASK;
- copyStart &= WINDOW_MASK;
- }
- }
- }
-
-
-
-
- public int CopyFrom(InputBuffer input, int length)
- {
- length = Math.Min(Math.Min(length, WINDOW_SIZE - _bytesUsed), input.AvailableBytes);
- int copied;
-
- int tailLen = WINDOW_SIZE - _end;
- if (length > tailLen)
- {
-
- copied = input.CopyTo(_window, _end, tailLen);
- if (copied == tailLen)
- {
-
- copied += input.CopyTo(_window, 0, length - tailLen);
- }
- }
- else
- {
-
- copied = input.CopyTo(_window, _end, length);
- }
- _end = (_end + copied) & WINDOW_MASK;
- _bytesUsed += copied;
- return copied;
- }
-
- public int FreeBytes => WINDOW_SIZE - _bytesUsed;
-
- public int AvailableBytes => _bytesUsed;
-
- public int CopyTo(byte[] output, int offset, int length)
- {
- int copyEnd;
- if (length > _bytesUsed)
- {
-
- copyEnd = _end;
- length = _bytesUsed;
- }
- else
- {
- copyEnd = (_end - _bytesUsed + length) & WINDOW_MASK;
- }
- int copied = length;
- int tailLen = length - copyEnd;
- if (tailLen > 0)
- {
-
-
- Array.Copy(_window, WINDOW_SIZE - tailLen,
- output, offset, tailLen);
- offset += tailLen;
- length = copyEnd;
- }
- Array.Copy(_window, copyEnd - length, output, offset, length);
- _bytesUsed -= copied;
- Debug.Assert(_bytesUsed >= 0, "check this function and find why we copied more bytes than we have");
- return copied;
- }
- }
- }
|