fpchash.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. {
  2. Copyright (c) 2000-2002 by Free Pascal Development Team
  3. Routines to compute hash 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 fpchash;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:integer):cardinal;
  21. { If needed trims the string to maxlen, adding at the end the CRC32 of discarded chars.
  22. The resulting string is guaranteed to be not longer than maxlen. }
  23. function TrimStrCRC32(const s: ansistring; maxlen: longint): ansistring;
  24. { calculate string hash using FNV Hash:
  25. http://www.isthe.com/chongo/tech/comp/fnv/
  26. }
  27. function UpdateFnv64(const InitFnv: uint64; const InBuf; InLen: Integer): uint64;
  28. type
  29. Base64OfUint64String = string[11];
  30. function Base64Mangle(const x: uint64): Base64OfUint64String;
  31. Implementation
  32. {*****************************************************************************
  33. Crc 32
  34. *****************************************************************************}
  35. var
  36. Crc32Tbl : array[0..255] of cardinal;
  37. procedure MakeCRC32Tbl;
  38. var
  39. crc : cardinal;
  40. i,n : integer;
  41. begin
  42. for i:=0 to 255 do
  43. begin
  44. crc:=i;
  45. for n:=1 to 8 do
  46. if (crc and 1)<>0 then
  47. crc:=(crc shr 1) xor cardinal($edb88320)
  48. else
  49. crc:=crc shr 1;
  50. Crc32Tbl[i]:=crc;
  51. end;
  52. end;
  53. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:Integer):cardinal;
  54. var
  55. i : integer;
  56. p : pchar;
  57. begin
  58. if Crc32Tbl[1]=0 then
  59. MakeCrc32Tbl;
  60. p:=@InBuf;
  61. result:=not InitCrc;
  62. for i:=1 to InLen do
  63. begin
  64. result:=Crc32Tbl[byte(result) xor byte(p^)] xor (result shr 8);
  65. inc(p);
  66. end;
  67. result:=not result;
  68. end;
  69. function TrimStrCRC32(const s: ansistring; maxlen: longint): ansistring;
  70. var
  71. crc: DWord;
  72. len: longint;
  73. begin
  74. len:=length(s);
  75. if (len<=maxlen) or (len<12) then
  76. result:=s
  77. else
  78. begin
  79. dec(maxlen,11);
  80. crc:=0;
  81. crc:=UpdateCrc32(crc,s[maxlen+1],len-maxlen);
  82. result:=copy(s,1,maxlen)+'$CRC'+hexstr(crc,8);
  83. end;
  84. end;
  85. { calculate string hash using FNV Hash:
  86. http://www.isthe.com/chongo/tech/comp/fnv/
  87. }
  88. {$push} {$rangechecks off} {$overflowchecks off}
  89. function UpdateFnv64(const InitFnv: uint64; const InBuf; InLen: Integer): uint64;
  90. const
  91. M = uint64(1099511628211);
  92. { Compiler yells at you for overflows in constants, even with disabled range checks,
  93. so there are precalculated values for unrolled loop: M^2, M^3, M^4. }
  94. Mp2 = uint64(956575116354345);
  95. Mp3 = uint64(624165263380053675);
  96. Mp4 = uint64(11527715348014283921);
  97. var
  98. pp: pByte;
  99. begin
  100. result := InitFnv;
  101. pp := @InBuf;
  102. while InLen >= 4 do
  103. begin
  104. result := (result + pp[0]) * Mp4 + pp[1] * Mp3 + pp[2] * Mp2 + pp[3] * M;
  105. pp := pp + 4;
  106. InLen := InLen - 4;
  107. end;
  108. while InLen > 0 do
  109. begin
  110. result := (result + pp^) * M;
  111. pp := pp + 1;
  112. InLen := InLen - 1;
  113. end;
  114. end;
  115. {$pop}
  116. const
  117. Base64Chars: array[0 .. 63] of char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$';
  118. function Base64Mangle(const x: uint64): Base64OfUint64String;
  119. var
  120. b64chars: pChar;
  121. begin
  122. b64chars := pChar(Base64Chars);
  123. result[0] := #11;
  124. result[1] := b64chars[x and $3f];
  125. result[2] := b64chars[uint32(x) shr 6 and $3f];
  126. result[3] := b64chars[uint32(x) shr 12 and $3f];
  127. result[4] := b64chars[uint32(x) shr 18 and $3f];
  128. result[5] := b64chars[uint32(x) shr 24 and $3f];
  129. result[6] := b64chars[x shr 30 and $3f];
  130. result[7] := b64chars[x shr 36 and $3f];
  131. result[8] := b64chars[x shr 42 and $3f];
  132. result[9] := b64chars[x shr 48 and $3f];
  133. result[10] := b64chars[x shr 54 and $3f];
  134. result[11] := b64chars[x shr 60];
  135. end;
  136. end.