fpchash.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 InitFnv64: uint64;
  28. function UpdateFnv64(const InitFnv: uint64; const InBuf; InLen: Integer): uint64;
  29. type
  30. Base64OfUint64String = string[11];
  31. function Base64Mangle(const x: uint64): Base64OfUint64String;
  32. Implementation
  33. {*****************************************************************************
  34. Crc 32
  35. *****************************************************************************}
  36. var
  37. Crc32Tbl : array[0..255] of cardinal;
  38. procedure MakeCRC32Tbl;
  39. var
  40. crc : cardinal;
  41. i,n : integer;
  42. begin
  43. for i:=0 to 255 do
  44. begin
  45. crc:=i;
  46. for n:=1 to 8 do
  47. if (crc and 1)<>0 then
  48. crc:=(crc shr 1) xor cardinal($edb88320)
  49. else
  50. crc:=crc shr 1;
  51. Crc32Tbl[i]:=crc;
  52. end;
  53. end;
  54. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:Integer):cardinal;
  55. var
  56. i : integer;
  57. p : pchar;
  58. begin
  59. if Crc32Tbl[1]=0 then
  60. MakeCrc32Tbl;
  61. p:=@InBuf;
  62. result:=not InitCrc;
  63. for i:=1 to InLen do
  64. begin
  65. result:=Crc32Tbl[byte(result) xor byte(p^)] xor (result shr 8);
  66. inc(p);
  67. end;
  68. result:=not result;
  69. end;
  70. function TrimStrCRC32(const s: ansistring; maxlen: longint): ansistring;
  71. var
  72. crc: DWord;
  73. len: longint;
  74. begin
  75. len:=length(s);
  76. if (len<=maxlen) or (len<12) then
  77. result:=s
  78. else
  79. begin
  80. dec(maxlen,11);
  81. crc:=0;
  82. crc:=UpdateCrc32(crc,s[maxlen+1],len-maxlen);
  83. result:=copy(s,1,maxlen)+'$CRC'+hexstr(crc,8);
  84. end;
  85. end;
  86. function InitFnv64: uint64;
  87. begin
  88. result:=uint64(14695981039346656037);
  89. end;
  90. { calculate string hash using FNV Hash:
  91. http://www.isthe.com/chongo/tech/comp/fnv/
  92. }
  93. {$push} {$rangechecks off} {$overflowchecks off}
  94. function UpdateFnv64(const InitFnv: uint64; const InBuf; InLen: Integer): uint64;
  95. const
  96. M = uint64(1099511628211);
  97. var
  98. pp: pByte;
  99. begin
  100. result := InitFnv;
  101. pp := @InBuf;
  102. while InLen >= 4 do
  103. begin
  104. result := ((((result xor pp[0]) * M xor pp[1]) * M xor pp[2]) * M xor pp[3]) * M;
  105. pp := pp + 4;
  106. InLen := InLen - 4;
  107. end;
  108. while InLen > 0 do
  109. begin
  110. result := (result xor 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.