Revision: 53729
Author:   brion
Date:     2009-07-24 22:15:23 +0000 (Fri, 24 Jul 2009)

Log Message:
-----------
64-bit host compatibility for hash function in pure-PHP implementation of CDB 
reader/writer.
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:
br...@bribuntu:~/src/phase3$ php maintenance/cdb-test.php 
Write test...
e5d9cbbcd0137b281da400bb213820fa  php.cdb
e5d9cbbcd0137b281da400bb213820fa  dba.cdb
Read test...
Done.


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


64-bit before patch:
br...@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:
br...@bribuntu64:~/src/phase3$ php maintenance/cdb-test.php 
Write test...
d244a877c9639e27c79aa1bdbcaee3c2  php.cdb
d244a877c9639e27c79aa1bdbcaee3c2  dba.cdb <- now matches!
Read test...
Done.

YESSSS

Modified Paths:
--------------
    trunk/phase3/includes/Cdb_PHP.php

Modified: trunk/phase3/includes/Cdb_PHP.php
===================================================================
--- trunk/phase3/includes/Cdb_PHP.php   2009-07-24 21:56:48 UTC (rev 53728)
+++ trunk/phase3/includes/Cdb_PHP.php   2009-07-24 22:15:23 UTC (rev 53729)
@@ -17,7 +17,7 @@
         * $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 @@
                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 @@
        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;
        }



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to