Changes between Version 43 and Version 44 of HashSourceCodes


Ignore:
Timestamp:
Mar 17, 2013, 6:14:07 PM (11 years ago)
Author:
os
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v43 v44  
    459459}}}
    460460
     461alternate version by TRP
     462
     463{{{
     464unction CalcGabestHash(const Stream: TStream): Int64; overload;
     465const HashPartSize = 1 shl 16; // 64 KiB
     466
     467  procedure UpdateHashFromStream(const Stream: TStream; var Hash:
     468Int64); inline;
     469  var buffer: Array[0..HashPartSize div SizeOf(Int64) - 1] of Int64;
     470      i     : integer;
     471  begin
     472    Stream.ReadBuffer(buffer[0], SizeOf(buffer));
     473    for i := Low(buffer) to High(buffer) do
     474      Inc(Hash, buffer[i]);
     475  end;
     476
     477begin
     478  result:= Stream.Size;
     479
     480  if result < HashPartSize then
     481  begin
     482    // stream too small return invalid hash
     483    result:= 0;
     484    exit;
     485  end;
     486
     487  // first 64 KiB
     488  Stream.Position:= 0;
     489  UpdateHashFromStream(Stream, result);
     490
     491  // last 64 KiB
     492  Stream.Seek(-HashPartSize, soEnd);
     493  UpdateHashFromStream(Stream, result);
     494
     495  // use "IntToHex(result, 16);" to get a string and "StrToInt64('$' +
     496hash);" to get your Int64 back
     497end;
     498
     499function CalcGabestHash(const FileName: TFileName): Int64; overload;
     500var stream: TStream;
     501begin
     502  stream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
     503  try
     504    result:= CalcGabestHash(stream);
     505  finally
     506    stream.Free;
     507  end;
     508end;
     509}}}
     510
    461511
    462512== Lua ==