64-bit host compatibility for hash function in pure-PHP implementation of CDB reader...
authorBrion Vibber <brion@users.mediawiki.org>
Fri, 24 Jul 2009 22:15:23 +0000 (22:15 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Fri, 24 Jul 2009 22:15:23 +0000 (22:15 +0000)
This fixes the problem where localization cache files generated under command-line and web were incompatible on Mac OS X when using $wgCacheDirectory instead of l10n_cache table in database.
(Apple ships a 64-bit Apache+mod_php but only a 32-bit CLI php binary, and neither has the cda extension present by default.)

Confirmed hash compatibility with native cda extension by running Tim's maintenance/cdb-test.php on 32-bit and 64-bit Ubuntu installs:

32 bit before patch:
brion@bribuntu:~/src/phase3$ php maintenance/cdb-test.php
Write test...
e5d9cbbcd0137b281da400bb213820fa  php.cdb
e5d9cbbcd0137b281da400bb213820fa  dba.cdb
Read test...
Done.

32-bit after patch:
brion@bribuntu:~/src/phase3$ php maintenance/cdb-test.php
Write test...
84108f6dab5c34823333169ca05eb5c3  php.cdb
84108f6dab5c34823333169ca05eb5c3  dba.cdb
Read test...
Done.
brion@bribuntu:~/src/phase3$

64-bit before patch:
brion@bribuntu64:~/src/phase3$ php maintenance/cdb-test.php
Write test...
188d06832d321b20e88e9d0200242efc  php.cdb
14e0225466464003d21496204f12d20c  dba.cdb <- note mismatch!
Read test...
Done.

64-bit after patch:
brion@bribuntu64:~/src/phase3$ php maintenance/cdb-test.php
Write test...
d244a877c9639e27c79aa1bdbcaee3c2  php.cdb
d244a877c9639e27c79aa1bdbcaee3c2  dba.cdb <- now matches!
Read test...
Done.

YESSSS

includes/Cdb_PHP.php

index b222909..8f1b30e 100644 (file)
@@ -17,7 +17,7 @@ class CdbFunctions {
         * $b must be less than 0x40000000 and greater than 0
         */
        public static function unsignedMod( $a, $b ) {
-               if ( $a 0 ) {
+               if ( $a & 0x80000000 ) {
                        $m = ( $a & 0x7fffffff ) % $b + 2 * ( 0x40000000 % $b );
                        return $m % $b;
                } else {
@@ -32,7 +32,7 @@ class CdbFunctions {
                if ( $b == 0 ) {
                        return $a;
                }
-               if ( $a 0 ) {
+               if ( $a & 0x80000000 ) {
                        return ( ( $a & 0x7fffffff ) >> $b ) | ( 0x40000000 >> ( $b - 1 ) );
                } else {
                        return $a >> $b;
@@ -45,20 +45,21 @@ class CdbFunctions {
        public static function hash( $s ) {
                $h = 5381;
                for ( $i = 0; $i < strlen( $s ); $i++ ) {
-                       $h5 = $h << 5;
+                       $h5 = ($h << 5) & 0xffffffff;
                        // Do a 32-bit sum
                        // Inlined here for speed
                        $sum = ($h & 0x3fffffff) + ($h5 & 0x3fffffff);
                        $h = 
                                (
                                        ( $sum & 0x40000000 ? 1 : 0 )
-                                       + ( $h 0 ? 2 : 0 )
+                                       + ( $h & 0x80000000 ? 2 : 0 )
                                        + ( $h & 0x40000000 ? 1 : 0 )
-                                       + ( $h5 0 ? 2 : 0 )
+                                       + ( $h5 & 0x80000000 ? 2 : 0 )
                                        + ( $h5 & 0x40000000 ? 1 : 0 )
                                ) << 30
                                | ( $sum & 0x3fffffff );
                        $h ^= ord( $s[$i] );
+                       $h &= 0xffffffff;
                }
                return $h;
        }