2
0

fpccrc.pas 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. {
  2. Copyright (c) 2000-2002 by Free Pascal Development Team
  3. Routines to compute CRC values
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. Unit fpccrc;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:integer):cardinal;
  21. Implementation
  22. {*****************************************************************************
  23. Crc 32
  24. *****************************************************************************}
  25. var
  26. Crc32Tbl : array[0..255] of cardinal;
  27. procedure MakeCRC32Tbl;
  28. var
  29. crc : cardinal;
  30. i,n : integer;
  31. begin
  32. for i:=0 to 255 do
  33. begin
  34. crc:=i;
  35. for n:=1 to 8 do
  36. if (crc and 1)<>0 then
  37. crc:=(crc shr 1) xor cardinal($edb88320)
  38. else
  39. crc:=crc shr 1;
  40. Crc32Tbl[i]:=crc;
  41. end;
  42. end;
  43. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:Integer):cardinal;
  44. var
  45. i : integer;
  46. p : pchar;
  47. begin
  48. if Crc32Tbl[1]=0 then
  49. MakeCrc32Tbl;
  50. p:=@InBuf;
  51. result:=not InitCrc;
  52. for i:=1 to InLen do
  53. begin
  54. result:=Crc32Tbl[byte(result) xor byte(p^)] xor (result shr 8);
  55. inc(p);
  56. end;
  57. result:=not result;
  58. end;
  59. end.