Index: lib/typedObjects/hash.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/typedObjects/hash.js |
@@ -0,0 +1,56 @@ |
+/* |
+ * This file is part of Adblock Plus <http://adblockplus.org/>, |
+ * Copyright (C) 2006-2014 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+"use strict"; |
+ |
+/* |
+ * Note: This implements the hash function by Paul Hsieh, see |
+ * http://www.azillionmonkeys.com/qed/hash.html. The code below is based on Paul |
+ * Hsieh's code which is licensed under LGPL 2.1. |
+ */ |
+ |
+exports.calculateHash = function calculateHash(/**string*/ str) |
+{ |
+ let length = str.length | 0; |
+ let hash = length << 1; |
+ |
+ for (let i = 0; i < length - 1; i += 2) |
+ { |
+ // Main loop, process two characters at a time |
+ hash += str.get(i); |
+ let tmp = (str.get(i + 1) << 11) ^ hash; |
+ hash = (hash << 16) ^ tmp; |
+ hash += hash >>> 11; |
+ } |
+ |
+ if (length & 1) |
+ { |
+ // Process remaining character |
+ hash += str.get(length - 1); |
+ hash ^= hash << 11; |
+ hash += hash >>> 17; |
+ } |
+ |
+ // "Avalanching" of final 127 bits |
+ hash ^= hash << 3; |
+ hash += hash >>> 5; |
+ hash ^= hash << 4; |
+ hash += hash >>> 17; |
+ hash ^= hash << 25; |
+ hash += hash >>> 6; |
+ return hash >>> 0; |
+} |