fpccrc.pas 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Crc32(Const HStr:String):cardinal;
  21. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:integer):cardinal;
  22. Function UpdCrc32(InitCrc:cardinal;b:byte):cardinal;
  23. Implementation
  24. {*****************************************************************************
  25. Crc 32
  26. *****************************************************************************}
  27. var
  28. Crc32Tbl : array[0..255] of cardinal;
  29. procedure MakeCRC32Tbl;
  30. var
  31. crc : cardinal;
  32. i,n : integer;
  33. begin
  34. for i:=0 to 255 do
  35. begin
  36. crc:=i;
  37. for n:=1 to 8 do
  38. if (crc and 1)<>0 then
  39. crc:=(crc shr 1) xor cardinal($edb88320)
  40. else
  41. crc:=crc shr 1;
  42. Crc32Tbl[i]:=crc;
  43. end;
  44. end;
  45. Function Crc32(Const HStr:String):cardinal;
  46. var
  47. i : integer;
  48. InitCrc : cardinal;
  49. begin
  50. if Crc32Tbl[1]=0 then
  51. MakeCrc32Tbl;
  52. InitCrc:=cardinal($ffffffff);
  53. for i:=1 to Length(Hstr) do
  54. InitCrc:=Crc32Tbl[byte(InitCrc) xor ord(Hstr[i])] xor (InitCrc shr 8);
  55. Crc32:=InitCrc;
  56. end;
  57. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:Integer):cardinal;
  58. var
  59. i : integer;
  60. p : pchar;
  61. begin
  62. if Crc32Tbl[1]=0 then
  63. MakeCrc32Tbl;
  64. p:=@InBuf;
  65. for i:=1 to InLen do
  66. begin
  67. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  68. inc(p);
  69. end;
  70. UpdateCrc32:=InitCrc;
  71. end;
  72. Function UpdCrc32(InitCrc:cardinal;b:byte):cardinal;
  73. begin
  74. if Crc32Tbl[1]=0 then
  75. MakeCrc32Tbl;
  76. UpdCrc32:=Crc32Tbl[byte(InitCrc) xor b] xor (InitCrc shr 8);
  77. end;
  78. end.