| 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 InputBuffer; |
| 7 |
|
| 8 |
{$mode ObjFPC}{$H+} |
| 9 |
|
| 10 |
interface |
| 11 |
|
| 12 |
uses |
| 13 |
Classes, SysUtils; |
| 14 |
|
| 15 |
type |
| 16 |
|
| 17 |
{ TInputBuffer } |
| 18 |
|
| 19 |
TInputBuffer = class |
| 20 |
private |
| 21 |
_buffer: PByte; // byte array to store input |
| 22 |
_start: Integer; // start poisition of the buffer |
| 23 |
_end: Integer; // end position of the buffer |
| 24 |
_bitBuffer: Cardinal; // store the bits here, we can quickly shift in this buffer |
| 25 |
_bitsInBuffer: Integer; // number of bits available in bitBuffer |
| 26 |
public |
| 27 |
// Total bytes available in the input buffer |
| 28 |
function AvailableBytes: Integer; |
| 29 |
// Ensure that count bits are in the bit buffer |
| 30 |
function EnsureBitsAvailable(count: Integer): Boolean; |
| 31 |
// This function will try to load 16 or more bits into bitBuffer |
| 32 |
function TryLoad16Bits: Cardinal; |
| 33 |
// Gets count bits from the input buffer. Returns -1 if not enough bits available |
| 34 |
function GetBits(count: Integer): Integer; |
| 35 |
// Copies length bytes from input buffer to output buffer starting at output[offset] |
| 36 |
function CopyTo(output: PByte; offset, length: Integer): Integer; |
| 37 |
// Return true is all input bytes are used |
| 38 |
function NeedsInput: Boolean; |
| 39 |
// Set the byte array to be processed |
| 40 |
procedure SetInput(buffer: PByte; offset, length: Integer); |
| 41 |
// Skip n bits in the buffer |
| 42 |
procedure SkipBits(n: Integer); |
| 43 |
/// Skips to the next byte boundary |
| 44 |
procedure SkipToByteBoundary; |
| 45 |
// Total bits available in the input buffer |
| 46 |
property AvailableBits: Integer read _bitsInBuffer; |
| 47 |
end; |
| 48 |
|
| 49 |
implementation |
| 50 |
|
| 51 |
{ TInputBuffer } |
| 52 |
|
| 53 |
function TInputBuffer.AvailableBytes: Integer; |
| 54 |
begin |
| 55 |
Result:= (_end - _start) + (_bitsInBuffer div 8); |
| 56 |
end; |
| 57 |
|
| 58 |
function TInputBuffer.EnsureBitsAvailable(count: Integer): Boolean; |
| 59 |
begin |
| 60 |
Assert((0 < count) and (count <= 16), 'count is invalid.'); |
| 61 |
|
| 62 |
// manual inlining to improve perf |
| 63 |
if (_bitsInBuffer < count) then |
| 64 |
begin |
| 65 |
if (NeedsInput()) then |
| 66 |
begin |
| 67 |
Exit(false); |
| 68 |
end; |
| 69 |
// insert a byte to bitbuffer |
| 70 |
_bitBuffer := _bitBuffer or Cardinal(_buffer[_start]) << _bitsInBuffer; |
| 71 |
_bitsInBuffer += 8; |
| 72 |
Inc(_start); |
| 73 |
|
| 74 |
if (_bitsInBuffer < count) then |
| 75 |
begin |
| 76 |
if (NeedsInput()) then |
| 77 |
begin |
| 78 |
Exit(false); |
| 79 |
end; |
| 80 |
// insert a byte to bitbuffer |
| 81 |
_bitBuffer := _bitBuffer or Cardinal(_buffer[_start]) << _bitsInBuffer; |
| 82 |
_bitsInBuffer += 8; |
| 83 |
Inc(_start); |
| 84 |
end; |
| 85 |
end; |
| 86 |
|
| 87 |
Result := true; |
| 88 |
end; |
| 89 |
|
| 90 |
function TInputBuffer.TryLoad16Bits: Cardinal; |
| 91 |
begin |
| 92 |
if (_bitsInBuffer < 8) then |
| 93 |
begin |
| 94 |
if (_start < _end) then |
| 95 |
begin |
| 96 |
_bitBuffer := _bitBuffer or Cardinal(_buffer[_start]) << _bitsInBuffer; |
| 97 |
_bitsInBuffer += 8; |
| 98 |
Inc(_start); |
| 99 |
end; |
| 100 |
|
| 101 |
if (_start < _end) then |
| 102 |
begin |
| 103 |
_bitBuffer := _bitBuffer or Cardinal(_buffer[_start]) << _bitsInBuffer; |
| 104 |
_bitsInBuffer += 8; |
| 105 |
Inc(_start); |
| 106 |
end; |
| 107 |
end |
| 108 |
else if (_bitsInBuffer < 16) then |
| 109 |
begin |
| 110 |
if (_start < _end) then |
| 111 |
begin |
| 112 |
_bitBuffer := _bitBuffer or Cardinal(_buffer[_start]) << _bitsInBuffer; |
| 113 |
_bitsInBuffer += 8; |
| 114 |
Inc(_start); |
| 115 |
end; |
| 116 |
end; |
| 117 |
|
| 118 |
Result := _bitBuffer; |
| 119 |
end; |
| 120 |
|
| 121 |
function TInputBuffer.GetBits(count: Integer): Integer; |
| 122 |
begin |
| 123 |
Assert((0 < count) and (count <= 16), 'count is invalid.'); |
| 124 |
|
| 125 |
if (not EnsureBitsAvailable(count)) then |
| 126 |
begin |
| 127 |
Exit(-1); |
| 128 |
end; |
| 129 |
|
| 130 |
result := Integer(_bitBuffer) and (((Cardinal(1) << count) - 1)); |
| 131 |
_bitBuffer := _bitBuffer >> count; |
| 132 |
_bitsInBuffer -= count; |
| 133 |
end; |
| 134 |
|
| 135 |
function TInputBuffer.CopyTo(output: PByte; offset, length: Integer): Integer; |
| 136 |
var |
| 137 |
avail: Integer; |
| 138 |
bytesFromBitBuffer: Integer = 0; |
| 139 |
begin |
| 140 |
Assert(output <> nil); |
| 141 |
Assert(offset >= 0); |
| 142 |
Assert(length >= 0); |
| 143 |
// Assert(offset <= System.Length(output) - length); |
| 144 |
Assert((_bitsInBuffer mod 8) = 0); |
| 145 |
|
| 146 |
// Copy the bytes in bitBuffer first. |
| 147 |
while (_bitsInBuffer > 0) and (length > 0) do |
| 148 |
begin |
| 149 |
output[offset] := Byte(_bitBuffer); |
| 150 |
_bitBuffer := _bitBuffer >> 8; |
| 151 |
_bitsInBuffer -= 8; |
| 152 |
Inc(offset); |
| 153 |
Dec(length); |
| 154 |
Inc(bytesFromBitBuffer); |
| 155 |
end; |
| 156 |
|
| 157 |
if (length = 0) then |
| 158 |
begin |
| 159 |
Exit(bytesFromBitBuffer); |
| 160 |
end; |
| 161 |
|
| 162 |
avail := _end - _start; |
| 163 |
if (length > avail) then |
| 164 |
begin |
| 165 |
length := avail; |
| 166 |
end; |
| 167 |
|
| 168 |
Move(_buffer[_start], output[offset], length); |
| 169 |
_start += length; |
| 170 |
Result := bytesFromBitBuffer + length; |
| 171 |
end; |
| 172 |
|
| 173 |
function TInputBuffer.NeedsInput(): Boolean; |
| 174 |
begin |
| 175 |
Result := (_start = _end); |
| 176 |
end; |
| 177 |
|
| 178 |
procedure TInputBuffer.SetInput(buffer: PByte; offset, length: Integer); |
| 179 |
begin |
| 180 |
Assert(buffer <> nil); |
| 181 |
Assert(offset >= 0); |
| 182 |
Assert(length >= 0); |
| 183 |
// Assert(offset <= System.Length(buffer) - length); |
| 184 |
Assert(_start = _end); |
| 185 |
|
| 186 |
_buffer := buffer; |
| 187 |
_start := offset; |
| 188 |
_end := offset + length; |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TInputBuffer.SkipBits(n: Integer); |
| 192 |
begin |
| 193 |
Assert( |
| 194 |
_bitsInBuffer >= n, |
| 195 |
'No enough bits in the buffer, Did you call EnsureBitsAvailable?' |
| 196 |
); |
| 197 |
_bitBuffer := _bitBuffer >> n; |
| 198 |
_bitsInBuffer -= n; |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TInputBuffer.SkipToByteBoundary; |
| 202 |
begin |
| 203 |
_bitBuffer := _bitBuffer >> (_bitsInBuffer mod 8); |
| 204 |
_bitsInBuffer -= (_bitsInBuffer mod 8); |
| 205 |
end; |
| 206 |
|
| 207 |
end. |
| 208 |
|