| 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 |
// See the LICENSE.txt file in the directory for more information. |
| 4 |
// The Pascal translation by Alexander Koblov. |
| 5 |
|
| 6 |
unit OutputWindow; |
| 7 |
|
| 8 |
{$mode ObjFPC}{$H+} |
| 9 |
|
| 10 |
interface |
| 11 |
|
| 12 |
uses |
| 13 |
Classes, SysUtils, InputBuffer; |
| 14 |
|
| 15 |
type |
| 16 |
|
| 17 |
{ TOutputWindow } |
| 18 |
|
| 19 |
TOutputWindow = class |
| 20 |
private |
| 21 |
// With Deflate64 we can have up to a 65536 length as well as up to a 65538 distance. This means we need a Window that is at |
| 22 |
// least 131074 bytes long so we have space to retrieve up to a full 64kb in lookback and place it in our buffer without |
| 23 |
// overwriting existing data. OutputWindow requires that the WindowSize be an exponent of 2, so we round up to 2^18. |
| 24 |
const WINDOW_SIZE = Integer(262144); |
| 25 |
const WINDOW_MASK = Integer(262143); |
| 26 |
private |
| 27 |
_window: array [0..Pred(WINDOW_SIZE)] of Byte; // The window is 2^18 bytes |
| 28 |
_end: Integer; // this is the position to where we should write next byte |
| 29 |
_bytesUsed: Integer; // The number of bytes in the output window which is not consumed. |
| 30 |
public |
| 31 |
// Add a byte to output window |
| 32 |
procedure Write(b: Byte); |
| 33 |
procedure WriteLengthDistance(length, distance: Integer); |
| 34 |
// Copy up to length of bytes from input directly |
| 35 |
function CopyFrom(input: TInputBuffer; length: Integer): Integer; |
| 36 |
// Free space in output window |
| 37 |
function FreeBytes: Integer; |
| 38 |
// Copy the decompressed bytes to output array |
| 39 |
function CopyTo(output: PByte; offset, length: Integer): Integer; |
| 40 |
// Bytes not consumed in output window |
| 41 |
property AvailableBytes: Integer read _bytesUsed; |
| 42 |
end; |
| 43 |
|
| 44 |
implementation |
| 45 |
|
| 46 |
uses |
| 47 |
Math; |
| 48 |
|
| 49 |
{ TOutputWindow } |
| 50 |
|
| 51 |
procedure TOutputWindow.Write(b: Byte); |
| 52 |
begin |
| 53 |
Assert(_bytesUsed < WINDOW_SIZE, 'Can''t add byte when window is full!'); |
| 54 |
_window[_end] := b; |
| 55 |
Inc(_end); |
| 56 |
_end := _end and WINDOW_MASK; |
| 57 |
Inc(_bytesUsed); |
| 58 |
end; |
| 59 |
|
| 60 |
procedure TOutputWindow.WriteLengthDistance(length, distance: Integer); |
| 61 |
var |
| 62 |
copyStart, border: Integer; |
| 63 |
begin |
| 64 |
Assert((_bytesUsed + length) <= WINDOW_SIZE, 'No Enough space'); |
| 65 |
|
| 66 |
// move backwards distance bytes in the output stream, |
| 67 |
// and copy length bytes from this position to the output stream. |
| 68 |
_bytesUsed += length; |
| 69 |
copyStart := (_end - distance) and WINDOW_MASK; // start position for coping. |
| 70 |
|
| 71 |
border := WINDOW_SIZE - length; |
| 72 |
if (copyStart <= border) and (_end < border) then |
| 73 |
begin |
| 74 |
if (length <= distance) then |
| 75 |
begin |
| 76 |
Move(_window[copyStart], _window[_end], length); |
| 77 |
_end += length; |
| 78 |
end |
| 79 |
else |
| 80 |
begin |
| 81 |
// The referenced string may overlap the current |
| 82 |
// position; for example, if the last 2 bytes decoded have values |
| 83 |
// X and Y, a string reference with <length = 5, distance = 2> |
| 84 |
// adds X,Y,X,Y,X to the output stream. |
| 85 |
while (length > 0) do |
| 86 |
begin |
| 87 |
_window[_end] := _window[copyStart]; |
| 88 |
Inc(_end); |
| 89 |
Dec(length); |
| 90 |
Inc(copyStart); |
| 91 |
end; |
| 92 |
end; |
| 93 |
end |
| 94 |
else |
| 95 |
begin |
| 96 |
// copy byte by byte |
| 97 |
while (length > 0) do |
| 98 |
begin |
| 99 |
_window[_end] := _window[copyStart]; |
| 100 |
Inc(_end); |
| 101 |
Dec(length); |
| 102 |
Inc(copyStart); |
| 103 |
_end := _end and WINDOW_MASK; |
| 104 |
copyStart := copyStart and WINDOW_MASK; |
| 105 |
end; |
| 106 |
end; |
| 107 |
end; |
| 108 |
|
| 109 |
function TOutputWindow.CopyFrom(input: TInputBuffer; length: Integer): Integer; |
| 110 |
var |
| 111 |
copied, tailLen: Integer; |
| 112 |
begin |
| 113 |
length := Math.Min(Math.Min(length, WINDOW_SIZE - _bytesUsed), input.AvailableBytes); |
| 114 |
// We might need wrap around to copy all bytes. |
| 115 |
tailLen := WINDOW_SIZE - _end; |
| 116 |
if (length > tailLen) then |
| 117 |
begin |
| 118 |
// copy the first part |
| 119 |
copied := input.CopyTo(_window, _end, tailLen); |
| 120 |
if (copied = tailLen) then |
| 121 |
begin |
| 122 |
// only try to copy the second part if we have enough bytes in input |
| 123 |
copied += input.CopyTo(_window, 0, length - tailLen); |
| 124 |
end; |
| 125 |
end |
| 126 |
else |
| 127 |
begin |
| 128 |
// only one copy is needed if there is no wrap around. |
| 129 |
copied := input.CopyTo(_window, _end, length); |
| 130 |
end; |
| 131 |
|
| 132 |
_end := (_end + copied) and WINDOW_MASK; |
| 133 |
_bytesUsed += copied; |
| 134 |
Result := copied; |
| 135 |
end; |
| 136 |
|
| 137 |
function TOutputWindow.FreeBytes: Integer; |
| 138 |
begin |
| 139 |
Result := WINDOW_SIZE - _bytesUsed; |
| 140 |
end; |
| 141 |
|
| 142 |
function TOutputWindow.CopyTo(output: PByte; offset, length: Integer): Integer; |
| 143 |
var |
| 144 |
copyEnd, copied, tailLen: Integer; |
| 145 |
begin |
| 146 |
if (length > _bytesUsed) then |
| 147 |
begin |
| 148 |
// we can copy all the decompressed bytes out |
| 149 |
copyEnd := _end; |
| 150 |
length := _bytesUsed; |
| 151 |
end |
| 152 |
else |
| 153 |
begin |
| 154 |
copyEnd := (_end - _bytesUsed + length) and WINDOW_MASK; // copy length of bytes |
| 155 |
end; |
| 156 |
|
| 157 |
copied := length; |
| 158 |
|
| 159 |
tailLen := length - copyEnd; |
| 160 |
if (tailLen > 0) then |
| 161 |
begin |
| 162 |
// this means we need to copy two parts separately |
| 163 |
// copy tailLen bytes from the end of output window |
| 164 |
Move(_window[WINDOW_SIZE - tailLen], output[offset], tailLen); |
| 165 |
offset += tailLen; |
| 166 |
length := copyEnd; |
| 167 |
end; |
| 168 |
if (length > 0) then |
| 169 |
begin |
| 170 |
Move(_window[copyEnd - length], output[offset], length); |
| 171 |
end; |
| 172 |
_bytesUsed -= copied; |
| 173 |
Assert( |
| 174 |
_bytesUsed >= 0, |
| 175 |
'check this function and find why we copied more bytes than we have' |
| 176 |
); |
| 177 |
Result := copied; |
| 178 |
end; |
| 179 |
|
| 180 |
end. |
| 181 |
|