New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicated checksum code #13
Comments
How about a class, Something like: type
TCheckSum = class
private
fCheckSum: Longint;
public
constructor Create(Seed: Longint);
procedure Add(Value: Int8);
property CheckSum: Longint read fCheckSum;
end; Implemented as: procedure TCheckSum.Add(Value: Int8);
begin
fCheckSum := (fCheckSum shr 30) and 3) or (fCheckSum shl 2);
fCheckSum := fCheckSum xor Value;
end;
constructor TCheckSum.Create(Seed: Longint);
begin
inherited Create;
fCheckSum := Seed;
end; Our two pieces of code that calculate checksums then become: class function TPatcher.CheckSum(Data: PAnsiChar; DataSize: Cardinal;
const BFCheckSum: Integer): Longint;
begin
var CS := TCheckSum.Create(BFCheckSum);
try
while DataSize <> 0 do
begin
Dec(DataSize);
CS.Add(PShortInt(Data)^);
Inc(Data);
end;
Result := CS.CheckSum;
finally
CS.Free;
end;
end;
function TBinaryPatchWriter.CheckSum(Data: PCChar; Length: Cardinal): Longint;
begin
var CS := TCheckSum.Create(0);
try
while Length <> 0 do
begin
Dec(Length);
CS.Add(Ord(Data^));
Inc(Data);
end;
Result := CS.CheckSum;
finally
CS.Free;
end;
end; It may be possible to run the loop from within |
Extending type
TCheckSum = class
// ...
public
// ...
procedure AddBuffer(Data: PInt8; Length: Cardinal);
// ...
end;
// ...
procedure TCheckSum.AddBuffer(Data: PInt8; Length: Cardinal);
begin
while Length > 0 do
begin
Dec(Length);
Add(Data^);
Inc(Data);
end;
end; Now the calling code reduces to: class function TPatcher.CheckSum(Data: PAnsiChar; DataSize: Cardinal;
const BFCheckSum: Integer): Longint;
begin
var CS := TCheckSum.Create(BFCheckSum);
try
CS.AddBuffer(PInt8(Data), DataSize);
Result := CS.CheckSum;
finally
CS.Free;
end;
end;
function TBinaryPatchWriter.CheckSum(Data: PCChar; Length: Cardinal): Longint;
begin
var CS := TCheckSum.Create(0);
try
CS.AddBuffer(PInt8(Data), Length);
Result := CS.CheckSum;
finally
CS.Free;
end;
end; |
If this code is implemented before conversion to Delphi 11 (issue #12), then occurrences of inline variable declarations will need to be changed to use to separate the |
bdiff/Src/BDiff/UPatchWriters.pas
Line 114 in 464fe21
and
bdiff/Src/BPatch/UPatcher.pas
Line 19 in 464fe21
have similar, but not identical code:
For the avoidance of error could the calculation part of the loops be extracted into a utility function? Such a function would need to be passed a brought forward checksum and the two types of
Data
parameters would need to be cast toShortInt
(=Int8
).Could be something like:
or
The text was updated successfully, but these errors were encountered: