From ad7b63425e27f18b424cac924a300c88f784e78b Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Thu, 13 Mar 2014 15:01:03 -0600 Subject: [PATCH] Switch to the faster FNV-1a hash for compose hashing --- imp/js/compose-dimp.js | 17 +++--- imp/js/compose.js | 12 ++-- imp/js/external/murmurhash3.js | 93 ------------------------------ imp/js/imp.js | 13 +++++ imp/lib/Basic/Compose.php | 2 +- imp/lib/Dynamic/Compose/Common.php | 2 +- imp/package.xml | 4 +- 7 files changed, 30 insertions(+), 113 deletions(-) delete mode 100644 imp/js/external/murmurhash3.js diff --git a/imp/js/compose-dimp.js b/imp/js/compose-dimp.js index 5ca2c60c049..bcb2b53aecb 100644 --- a/imp/js/compose-dimp.js +++ b/imp/js/compose-dimp.js @@ -28,7 +28,6 @@ var DimpCompose = { }) }), knl: {}, - seed: 3, getCacheElt: function() { @@ -702,11 +701,10 @@ var DimpCompose = { return; } - hdrs = murmurhash3( + hdrs = IMP_JS.fnv_1a( [$('to', 'cc', 'bcc', 'subject').compact().invoke('getValue'), $('attach_list').select('SPAN.attachName').pluck('textContent') - ].flatten().join('\0'), - this.seed + ].flatten().join('\0') ); if (Object.isUndefined(this.hash_hdrs)) { @@ -729,15 +727,16 @@ var DimpCompose = { msgHash: function() { - return murmurhash3(ImpComposeBase.editor_on ? this.rte.getData() : $F('composeMessage'), this.seed); + return IMP_JS.fnv_1a( + ImpComposeBase.editor_on ? this.rte.getData() : $F('composeMessage') + ); }, sigHash: function() { - if (!$('signature')) { - return 0; - } - return murmurhash3(ImpComposeBase.editor_on ? ImpComposeBase.rte.getData() : $F('signature'), this.seed); + return $('signature') + ? IMP_JS.fnv_1a(ImpComposeBase.editor_on ? ImpComposeBase.rte.getData() : $F('signature')) + : 0; }, updateSigHash: function() diff --git a/imp/js/compose.js b/imp/js/compose.js index 5ffd3cea07e..05846c76be1 100644 --- a/imp/js/compose.js +++ b/imp/js/compose.js @@ -13,7 +13,6 @@ var ImpCompose = { // sc_submit, sm_check, skip_spellcheck, spellcheck, text display_unload_warning: true, - seed: 3, confirmCancel: function(discard, e) { @@ -147,7 +146,9 @@ var ImpCompose = { CKEDITOR.instances.composeMessage.updateElement(); } - cur_msg = murmurhash3($('to', 'cc', 'bcc', 'subject').compact().invoke('getValue').join('\0') + $F('composeMessage'), this.seed); + cur_msg = IMP_JS.fnv_1a( + $('to', 'cc', 'bcc', 'subject').compact().invoke('getValue').join('\0') + $F('composeMessage') + ); if (this.last_msg && curr_hash != this.last_msg) { // Use an AJAX submit here so that the page doesn't reload. $('actionID').setValue(actionID); @@ -228,10 +229,9 @@ var ImpCompose = { sigHash: function() { - if (!$('signature')) { - return 0; - } - return murmurhash3(ImpComposeBase.editor_on ? ImpComposeBase.rte.getData() : $F('signature'), this.seed); + return $('signature') + ? IMP_JS.fnv_1a(ImpComposeBase.editor_on ? ImpComposeBase.rte.getData() : $F('signature')) + : 0; }, updateSigHash: function() diff --git a/imp/js/external/murmurhash3.js b/imp/js/external/murmurhash3.js deleted file mode 100644 index afd7a18d40f..00000000000 --- a/imp/js/external/murmurhash3.js +++ /dev/null @@ -1,93 +0,0 @@ -/** - * JS Implementation of MurmurHash3 - * - * Original version: - * https://github.com/kazuyukitanimura/murmurhash-js - * - * Additions by Michael Slusarz - * - * ----- - * - * Copyright (c) 2011 Gary Court - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - * @param {string} key ASCII only - * @param {number} seed Positive integer only - * @return {number} 32-bit positive integer hash - */ - -function murmurhash3(key, seed) -{ - var remainder = key.length & 3, // key.length % 4 - bytes = key.length - remainder, - h1 = seed, - c1 = 0xcc9e2d51, - c2 = 0x1b873593, - i = 0, - c1_l = c1 & 0xffff, - c1_h = c1 & 0xffff0000, - c2_l = c2 & 0xffff, - c2_h = c2 & 0xffff0000, - k1; - - while (i < bytes) { - k1 = ((key.charCodeAt(i++) & 0xff)) | - ((key.charCodeAt(i++) & 0xff) << 8) | - ((key.charCodeAt(i++) & 0xff) << 16) | - ((key.charCodeAt(i++) & 0xff) << 24); - - // note that javascript precision is 2^53 - k1 = (k1 * c1_l + (k1 & 0xffff) * c1_h) & 0xffffffff; - k1 = (k1 << 15) | (k1 >>> 17); - k1 = (k1 * c2_l + (k1 & 0xffff) * c2_h) & 0xffffffff; - - h1 ^= k1; - h1 = (h1 << 13) | (h1 >>> 19); - h1 = (h1 * 5 + 0xe6546b64) & 0xffffffff; - } - - k1 = 0; - - switch (remainder) { - case 3: - k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16; - - case 2: - k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8; - - case 1: - k1 ^= (key.charCodeAt(i) & 0xff); - - k1 = (k1 * c1_l + (k1 & 0xffff) * c1_h) & 0xffffffff; - k1 = (k1 << 16) | (k1 >>> 16); - k1 = (k1 * c2_l + (k1 & 0xffff) * c2_h) & 0xffffffff; - h1 ^= k1; - } - - h1 ^= key.length; - - h1 ^= h1 >>> 16; - h1 = (h1 * 0xca6b + (h1 & 0xffff) * 0x85eb0000) & 0xffffffff; - h1 ^= h1 >>> 13; - h1 = (h1 * 0xae35 + (h1 & 0xffff) * 0xc2b20000) & 0xffffffff; - h1 ^= h1 >>> 16; - - return h1 >>> 0; -} diff --git a/imp/js/imp.js b/imp/js/imp.js index f4d358a4902..c878392b321 100644 --- a/imp/js/imp.js +++ b/imp/js/imp.js @@ -152,6 +152,19 @@ var IMP_JS = { if (w || h) { win.resizeBy(w, h); } + }, + + fnv_1a: function(str) + { + var i, l, + hash = 0x811c9dc5; + + for (i = 0, l = str.length; i < l; ++i) { + hash ^= str.charCodeAt(i); + hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); + } + + return hash >>> 0; } }; diff --git a/imp/lib/Basic/Compose.php b/imp/lib/Basic/Compose.php index d2fdf2ce0d2..219329d537a 100644 --- a/imp/lib/Basic/Compose.php +++ b/imp/lib/Basic/Compose.php @@ -1058,7 +1058,7 @@ protected function _init() $page_output->addScriptPackage('IMP_Script_Package_ComposeBase'); $page_output->addScriptFile('compose.js'); - $page_output->addScriptFile('external/murmurhash3.js'); + $page_output->addScriptFile('imp.js'); $page_output->addInlineJsVars($js_vars); if (!$redirect) { $imp_ui->addIdentityJs(); diff --git a/imp/lib/Dynamic/Compose/Common.php b/imp/lib/Dynamic/Compose/Common.php index 6ffa5ef5b28..0407fd62e53 100644 --- a/imp/lib/Dynamic/Compose/Common.php +++ b/imp/lib/Dynamic/Compose/Common.php @@ -42,7 +42,7 @@ public function compose(IMP_Dynamic_Base $base, array $args = array()) $page_output->addScriptPackage('IMP_Script_Package_ComposeBase'); $page_output->addScriptFile('compose-dimp.js'); $page_output->addScriptFile('draghandler.js'); - $page_output->addScriptFile('external/murmurhash3.js'); + $page_output->addScriptFile('imp.js'); if (!$prefs->isLocked('default_encrypt') && ($prefs->getValue('use_pgp') || $prefs->getValue('use_smime'))) { diff --git a/imp/package.xml b/imp/package.xml index a94976e3ec6..fcc3b7b378f 100644 --- a/imp/package.xml +++ b/imp/package.xml @@ -22,7 +22,7 @@ chuck@horde.org yes - 2014-03-12 + 2014-03-13 6.2.0 6.2.0 @@ -109,7 +109,6 @@ - @@ -1579,7 +1578,6 @@ -