r53729 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53728‎ | r53729 | r53730 >
Date:22:15, 24 July 2009
Author:brion
Status:ok
Tags:
Comment:
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:
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
Modified paths:
  • /trunk/phase3/includes/Cdb_PHP.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Cdb_PHP.php
@@ -17,7 +17,7 @@
1818 * $b must be less than 0x40000000 and greater than 0
1919 */
2020 public static function unsignedMod( $a, $b ) {
21 - if ( $a < 0 ) {
 21+ if ( $a & 0x80000000 ) {
2222 $m = ( $a & 0x7fffffff ) % $b + 2 * ( 0x40000000 % $b );
2323 return $m % $b;
2424 } else {
@@ -32,7 +32,7 @@
3333 if ( $b == 0 ) {
3434 return $a;
3535 }
36 - if ( $a < 0 ) {
 36+ if ( $a & 0x80000000 ) {
3737 return ( ( $a & 0x7fffffff ) >> $b ) | ( 0x40000000 >> ( $b - 1 ) );
3838 } else {
3939 return $a >> $b;
@@ -45,20 +45,21 @@
4646 public static function hash( $s ) {
4747 $h = 5381;
4848 for ( $i = 0; $i < strlen( $s ); $i++ ) {
49 - $h5 = $h << 5;
 49+ $h5 = ($h << 5) & 0xffffffff;
5050 // Do a 32-bit sum
5151 // Inlined here for speed
5252 $sum = ($h & 0x3fffffff) + ($h5 & 0x3fffffff);
5353 $h =
5454 (
5555 ( $sum & 0x40000000 ? 1 : 0 )
56 - + ( $h < 0 ? 2 : 0 )
 56+ + ( $h & 0x80000000 ? 2 : 0 )
5757 + ( $h & 0x40000000 ? 1 : 0 )
58 - + ( $h5 < 0 ? 2 : 0 )
 58+ + ( $h5 & 0x80000000 ? 2 : 0 )
5959 + ( $h5 & 0x40000000 ? 1 : 0 )
6060 ) << 30
6161 | ( $sum & 0x3fffffff );
6262 $h ^= ord( $s[$i] );
 63+ $h &= 0xffffffff;
6364 }
6465 return $h;
6566 }

Status & tagging log