Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Created December 25, 2017 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpluimers/b01aadb8049daada1d1caf5926eb1ad6 to your computer and use it in GitHub Desktop.
Save jpluimers/b01aadb8049daada1d1caf5926eb1ad6 to your computer and use it in GitHub Desktop.
Delphi has three different function ContainsPreamble implementations
Delphi has three different function ContainsPreamble implementations in these files:
...\source\internet\Web.HTTPProd.pas
...\source\vcl\Vcl.ComCtrls.pas
...\source\rtl\sys\System.SysUtils.pas
function ContainsPreamble(const Buffer, Signature: array of Byte): Boolean;
var
I: Integer;
begin
Result := True;
if Length(Buffer) >= Length(Signature) then
begin
for I := 1 to Length(Signature) do
if Buffer[I - 1] <> Signature [I - 1] then
begin
Result := False;
Break;
end;
end
else
Result := False;
end;
function ContainsPreamble(Stream: TStream; Signature: TBytes): Boolean;
var
Buffer: TBytes;
I, LBufLen, LSignatureLen, LPosition: Integer;
begin
Result := True;
LSignatureLen := Length(Signature);
LPosition := Stream.Position;
try
SetLength(Buffer, LSignatureLen);
{$IFDEF CLR}
LBufLen := Stream.Read(Buffer, LSignatureLen);
{$ELSE}
LBufLen := Stream.Read(Buffer[0], LSignatureLen);
{$ENDIF}
finally
Stream.Position := LPosition;
end;
if LBufLen = LSignatureLen then
begin
for I := 1 to LSignatureLen do
if Buffer[I - 1] <> Signature [I - 1] then
begin
Result := False;
Break;
end;
end
else
Result := False;
end;
function ContainsPreamble(AStream: TStream; Encoding: TEncoding; var ASignatureSize: Integer): Boolean;
var
I: Integer;
Signature: TBytes;
Bytes: TBytes;
begin
Result := False;
Signature := Encoding.GetPreamble;
if Signature <> nil then
begin
if AStream.Size >= Length(Signature) then
begin
SetLength(Bytes, Length(Signature));
AStream.Read(Bytes[0], Length(Bytes));
Result := True;
ASignatureSize := Length(Signature);
for I := 0 to Length(Signature)-1 do
if Bytes[I] <> Signature [I] then
begin
ASignatureSize := 0;
Result := False;
Break;
end;
end;
end
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment