Created
December 25, 2017 12:07
-
-
Save jpluimers/b01aadb8049daada1d1caf5926eb1ad6 to your computer and use it in GitHub Desktop.
Delphi has three different function ContainsPreamble implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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