crc.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. {$i defines.inc}
  20. Interface
  21. Function Crc32(Const HStr:String):longint;
  22. Function UpdateCrc32(InitCrc:longint;var InBuf;InLen:Longint):longint;
  23. Function UpdCrc32(InitCrc:longint;b:byte):longint;
  24. Implementation
  25. {*****************************************************************************
  26. Crc 32
  27. *****************************************************************************}
  28. var
  29. Crc32Tbl : array[0..255] of longint;
  30. procedure MakeCRC32Tbl;
  31. var
  32. crc : longint;
  33. i,n : byte;
  34. begin
  35. for i:=0 to 255 do
  36. begin
  37. crc:=i;
  38. for n:=1 to 8 do
  39. if odd(crc) then
  40. crc:=(crc shr 1) xor longint($edb88320)
  41. else
  42. crc:=crc shr 1;
  43. Crc32Tbl[i]:=crc;
  44. end;
  45. end;
  46. {$ifopt R+}
  47. {$define Range_check_on}
  48. {$endif opt R+}
  49. {$R- needed here }
  50. {CRC 32}
  51. Function Crc32(Const HStr:String):longint;
  52. var
  53. i,InitCrc : longint;
  54. begin
  55. if Crc32Tbl[1]=0 then
  56. MakeCrc32Tbl;
  57. InitCrc:=longint($ffffffff);
  58. for i:=1 to Length(Hstr) do
  59. InitCrc:=Crc32Tbl[byte(InitCrc) xor ord(Hstr[i])] xor (InitCrc shr 8);
  60. Crc32:=InitCrc;
  61. end;
  62. Function UpdateCrc32(InitCrc:longint;var InBuf;InLen:Longint):longint;
  63. var
  64. i : word;
  65. p : pchar;
  66. begin
  67. if Crc32Tbl[1]=0 then
  68. MakeCrc32Tbl;
  69. p:=@InBuf;
  70. for i:=1 to InLen do
  71. begin
  72. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  73. inc(longint(p));
  74. end;
  75. UpdateCrc32:=InitCrc;
  76. end;
  77. Function UpdCrc32(InitCrc:longint;b:byte):longint;
  78. begin
  79. if Crc32Tbl[1]=0 then
  80. MakeCrc32Tbl;
  81. UpdCrc32:=Crc32Tbl[byte(InitCrc) xor b] xor (InitCrc shr 8);
  82. end;
  83. {$ifdef Range_check_on}
  84. {$R+}
  85. {$undef Range_check_on}
  86. {$endif Range_check_on}
  87. end.
  88. {
  89. $Log$
  90. Revision 1.4 2000-09-24 15:06:14 peter
  91. * use defines.inc
  92. Revision 1.3 2000/08/13 13:04:38 peter
  93. * new ppu version
  94. Revision 1.2 2000/07/13 11:32:39 michael
  95. + removed logs
  96. }