Delphi CRC 算法函数

本文详细介绍了32位CRC算法的几种实现方式,包括函数CRC32Update用于更新现有CRC值,CRC32Full提供PKZip兼容的结果,以及通过TStream处理流和文件的CRC计算。还包含CRC32Test用于检验算法的正确性。
摘要由CSDN通过智能技术生成

转的很实用的crc函数:

unit _CRC;

interface

uses Types, SysUtils, Classes;

function CRC32Update(P: Pointer; ByteCount: LongInt; CurCrc: DWORD): DWORD;
function CRC32Full(P: Pointer; ByteCount: LongInt): DWORD;

function CRC32UpdateStream(Stream: TStream; CurCrc: DWORD): DWORD;
function CRC32FullStream(Stream: TStream): DWORD;

function CRC32File(FileName: string): DWORD;

function CRC32Test: Boolean;

implementation

const
  CRC32BASE: DWORD = $FFFFFFFF;

Var
  CRC32Table: array [0 .. 255] of DWORD;

procedure SZCRC32MakeTable;
// Making the 32-bit CRC table
var
  i, j: integer;
  r: DWORD;
begin
  for i := 0 to 255 do
  begin
    r := i;
    for j := 1 to 8 do
      if (r and 1) = 1 then
        r := (r shr 1) xor DWORD($EDB88320)
      else
        r := (r shr 1);

    CRC32Table[i] := r
  end;
end;

function CRC32Update(P: Pointer; ByteCount: LongInt; CurCrc: DWORD): DWORD;
// Updating existed 32-bit CRC with new calaculated
var
  CRCValue: DWORD;
  i: LongInt;
  b: ^Byte;
begin
  b := P;
  CRCValue := CurCrc;
  for i := 1 to ByteCount do
  begin
    CRCValue := (CRCValue shr 8) xor CRC32Table[b^ xor Byte(CRCValue and $FF)];
    inc(b);
  end;
  Result := CRCValue;
end;

function CRC32Full(P: Pointer; ByteCount: LongInt): DWORD;
// PKzip compatible - results with inverted bits
begin
  Result := not DWORD(CRC32Update(P, ByteCount, CRC32BASE));
end;

function CRC32UpdateStream(Stream: TStream; CurCrc: DWORD): DWORD;
// Calculates the 32-bit CRC of a stream
// For PKZip compatibility, result need to be inverted manually only for finally CRC value
const
  CRC32BUFSIZE = 2048;
var
  BufArray: array [0 .. (CRC32BUFSIZE - 1)] of Byte;
  Res: LongInt;
  CRC32: DWORD;
begin
  // Initialize 32-bit CRC
  CRC32 := CurCrc;
  repeat
    Res := Stream.Read(BufArray, CRC32BUFSIZE);

    CRC32 := CRC32Update(@BufArray, Res, CRC32);

  until (Res <> LongInt(CRC32BUFSIZE));
  Result := CRC32
end;

function CRC32FullStream(Stream: TStream): DWORD;
// Calculates the 32-bit CRC of a stream
// PKZip compatible, result is inverted
begin
  Result := not DWORD(CRC32UpdateStream(Stream, CRC32BASE));
end;

function CRC32File(FileName: string): DWORD;
// Calculates the 32-bit CRC of a file
// PKZip compatible
var
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
    Result := not DWORD(CRC32UpdateStream(FileStream, CRC32BASE));
  finally
    FileStream.Free;
  end;
end;

function CRC32Test: Boolean;
// Testing the 32-bit CRC algorithm
const
  CRC: DWORD = $29058C73;
var
  TestCRC: DWORD;
  TestData: Pointer;
  i: integer;
begin

  TestData := GetMemory(256);

  for i := 0 to 255 do
    pbyte(pchar(TestData) + i)^ := i;

  TestCRC := CRC32Full(TestData, 256);
  FreeMemory(TestData);

  if (TestCRC <> CRC) then
    Result := false
  else
    Result := true;
end;

initialization

// Making the 32-bit CRC table

SZCRC32MakeTable;

end.
 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值