crc.pas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Free Pascal Development Team
  4. Routines to compute CRC values
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit CRC;
  19. Interface
  20. Function Crc32(Const HStr:String):longint;
  21. Function UpdateCrc32(InitCrc:longint;var InBuf;InLen:Longint):longint;
  22. Function UpdCrc32(InitCrc:longint;b:byte):longint;
  23. Implementation
  24. {*****************************************************************************
  25. Crc 32
  26. *****************************************************************************}
  27. var
  28. Crc32Tbl : array[0..255] of longint;
  29. procedure MakeCRC32Tbl;
  30. var
  31. crc : longint;
  32. i,n : byte;
  33. begin
  34. for i:=0 to 255 do
  35. begin
  36. crc:=i;
  37. for n:=1 to 8 do
  38. if odd(crc) then
  39. crc:=(crc shr 1) xor longint($edb88320)
  40. else
  41. crc:=crc shr 1;
  42. Crc32Tbl[i]:=crc;
  43. end;
  44. end;
  45. {$ifopt R+}
  46. {$define Range_check_on}
  47. {$endif opt R+}
  48. {$R- needed here }
  49. {CRC 32}
  50. Function Crc32(Const HStr:String):longint;
  51. var
  52. i,InitCrc : longint;
  53. begin
  54. if Crc32Tbl[1]=0 then
  55. MakeCrc32Tbl;
  56. InitCrc:=longint($ffffffff);
  57. for i:=1 to Length(Hstr) do
  58. InitCrc:=Crc32Tbl[byte(InitCrc) xor ord(Hstr[i])] xor (InitCrc shr 8);
  59. Crc32:=InitCrc;
  60. end;
  61. Function UpdateCrc32(InitCrc:longint;var InBuf;InLen:Longint):longint;
  62. var
  63. i : word;
  64. p : pchar;
  65. begin
  66. if Crc32Tbl[1]=0 then
  67. MakeCrc32Tbl;
  68. p:=@InBuf;
  69. for i:=1 to InLen do
  70. begin
  71. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  72. inc(longint(p));
  73. end;
  74. UpdateCrc32:=InitCrc;
  75. end;
  76. Function UpdCrc32(InitCrc:longint;b:byte):longint;
  77. begin
  78. if Crc32Tbl[1]=0 then
  79. MakeCrc32Tbl;
  80. UpdCrc32:=Crc32Tbl[byte(InitCrc) xor b] xor (InitCrc shr 8);
  81. end;
  82. {$ifdef Range_check_on}
  83. {$R+}
  84. {$undef Range_check_on}
  85. {$endif Range_check_on}
  86. end.
  87. {
  88. $Log$
  89. Revision 1.3 2000-08-13 13:04:38 peter
  90. * new ppu version
  91. Revision 1.2 2000/07/13 11:32:39 michael
  92. + removed logs
  93. }