00001 <?php
00002
00014 class CdbFunctions {
00019 public static function unsignedMod( $a, $b ) {
00020 if ( $a & 0x80000000 ) {
00021 $m = ( $a & 0x7fffffff ) % $b + 2 * ( 0x40000000 % $b );
00022 return $m % $b;
00023 } else {
00024 return $a % $b;
00025 }
00026 }
00027
00031 public static function unsignedShiftRight( $a, $b ) {
00032 if ( $b == 0 ) {
00033 return $a;
00034 }
00035 if ( $a & 0x80000000 ) {
00036 return ( ( $a & 0x7fffffff ) >> $b ) | ( 0x40000000 >> ( $b - 1 ) );
00037 } else {
00038 return $a >> $b;
00039 }
00040 }
00041
00045 public static function hash( $s ) {
00046 $h = 5381;
00047 for ( $i = 0; $i < strlen( $s ); $i++ ) {
00048 $h5 = ($h << 5) & 0xffffffff;
00049
00050
00051 $sum = ($h & 0x3fffffff) + ($h5 & 0x3fffffff);
00052 $h =
00053 (
00054 ( $sum & 0x40000000 ? 1 : 0 )
00055 + ( $h & 0x80000000 ? 2 : 0 )
00056 + ( $h & 0x40000000 ? 1 : 0 )
00057 + ( $h5 & 0x80000000 ? 2 : 0 )
00058 + ( $h5 & 0x40000000 ? 1 : 0 )
00059 ) << 30
00060 | ( $sum & 0x3fffffff );
00061 $h ^= ord( $s[$i] );
00062 $h &= 0xffffffff;
00063 }
00064 return $h;
00065 }
00066 }
00067
00071 class CdbReader_PHP extends CdbReader {
00073 var $handle;
00074
00075
00076 var $loop;
00077
00078
00079 var $khash;
00080
00081
00082 var $kpos;
00083
00084
00085 var $hpos;
00086
00087
00088 var $hslots;
00089
00090
00091 var $dpos;
00092
00093
00094 var $dlen;
00095
00096 function __construct( $fileName ) {
00097 $this->handle = fopen( $fileName, 'rb' );
00098 if ( !$this->handle ) {
00099 throw new MWException( 'Unable to open DB file "' . $fileName . '"' );
00100 }
00101 $this->findStart();
00102 }
00103
00104 function close() {
00105 if( isset($this->handle) )
00106 fclose( $this->handle );
00107 unset( $this->handle );
00108 }
00109
00110 public function get( $key ) {
00111
00112 if ( $this->find( strval( $key ) ) ) {
00113 return $this->read( $this->dlen, $this->dpos );
00114 } else {
00115 return false;
00116 }
00117 }
00118
00119 protected function match( $key, $pos ) {
00120 $buf = $this->read( strlen( $key ), $pos );
00121 return $buf === $key;
00122 }
00123
00124 protected function findStart() {
00125 $this->loop = 0;
00126 }
00127
00128 protected function read( $length, $pos ) {
00129 if ( fseek( $this->handle, $pos ) == -1 ) {
00130
00131 throw new MWException( __METHOD__.': seek failed, file may be corrupted.' );
00132 }
00133
00134 if ( $length == 0 ) {
00135 return '';
00136 }
00137
00138 $buf = fread( $this->handle, $length );
00139 if ( $buf === false || strlen( $buf ) !== $length ) {
00140 throw new MWException( __METHOD__.': read from cdb file failed, file may be corrupted' );
00141 }
00142 return $buf;
00143 }
00144
00148 protected function unpack31( $s ) {
00149 $data = unpack( 'V', $s );
00150 if ( $data[1] > 0x7fffffff ) {
00151 throw new MWException( __METHOD__.': error in CDB file, integer too big' );
00152 }
00153 return $data[1];
00154 }
00155
00159 protected function unpackSigned( $s ) {
00160 $data = unpack( 'va/vb', $s );
00161 return $data['a'] | ( $data['b'] << 16 );
00162 }
00163
00164 protected function findNext( $key ) {
00165 if ( !$this->loop ) {
00166 $u = CdbFunctions::hash( $key );
00167 $buf = $this->read( 8, ( $u << 3 ) & 2047 );
00168 $this->hslots = $this->unpack31( substr( $buf, 4 ) );
00169 if ( !$this->hslots ) {
00170 return false;
00171 }
00172 $this->hpos = $this->unpack31( substr( $buf, 0, 4 ) );
00173 $this->khash = $u;
00174 $u = CdbFunctions::unsignedShiftRight( $u, 8 );
00175 $u = CdbFunctions::unsignedMod( $u, $this->hslots );
00176 $u <<= 3;
00177 $this->kpos = $this->hpos + $u;
00178 }
00179
00180 while ( $this->loop < $this->hslots ) {
00181 $buf = $this->read( 8, $this->kpos );
00182 $pos = $this->unpack31( substr( $buf, 4 ) );
00183 if ( !$pos ) {
00184 return false;
00185 }
00186 $this->loop += 1;
00187 $this->kpos += 8;
00188 if ( $this->kpos == $this->hpos + ( $this->hslots << 3 ) ) {
00189 $this->kpos = $this->hpos;
00190 }
00191 $u = $this->unpackSigned( substr( $buf, 0, 4 ) );
00192 if ( $u === $this->khash ) {
00193 $buf = $this->read( 8, $pos );
00194 $keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
00195 if ( $keyLen == strlen( $key ) && $this->match( $key, $pos + 8 ) ) {
00196
00197 $this->dlen = $this->unpack31( substr( $buf, 4 ) );
00198 $this->dpos = $pos + 8 + $keyLen;
00199 return true;
00200 }
00201 }
00202 }
00203 return false;
00204 }
00205
00206 protected function find( $key ) {
00207 $this->findStart();
00208 return $this->findNext( $key );
00209 }
00210 }
00211
00215 class CdbWriter_PHP extends CdbWriter {
00216 var $handle, $realFileName, $tmpFileName;
00217
00218 var $hplist;
00219 var $numEntries, $pos;
00220
00221 function __construct( $fileName ) {
00222 $this->realFileName = $fileName;
00223 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
00224 $this->handle = fopen( $this->tmpFileName, 'wb' );
00225 if ( !$this->handle ) {
00226 throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' );
00227 }
00228 $this->hplist = array();
00229 $this->numentries = 0;
00230 $this->pos = 2048;
00231 if ( fseek( $this->handle, $this->pos ) == -1 ) {
00232 throw new MWException( __METHOD__.': fseek failed' );
00233 }
00234 }
00235
00236 function __destruct() {
00237 if ( isset( $this->handle ) ) {
00238 $this->close();
00239 }
00240 }
00241
00242 public function set( $key, $value ) {
00243 if ( strval( $key ) === '' ) {
00244
00245 return;
00246 }
00247 $this->addbegin( strlen( $key ), strlen( $value ) );
00248 $this->write( $key );
00249 $this->write( $value );
00250 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions::hash( $key ) );
00251 }
00252
00253 public function close() {
00254 $this->finish();
00255 if( isset($this->handle) )
00256 fclose( $this->handle );
00257 if ( wfIsWindows() && file_exists($this->realFileName) ) {
00258 unlink( $this->realFileName );
00259 }
00260 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
00261 throw new MWException( 'Unable to move the new CDB file into place.' );
00262 }
00263 unset( $this->handle );
00264 }
00265
00266 protected function write( $buf ) {
00267 $len = fwrite( $this->handle, $buf );
00268 if ( $len !== strlen( $buf ) ) {
00269 throw new MWException( 'Error writing to CDB file.' );
00270 }
00271 }
00272
00273 protected function posplus( $len ) {
00274 $newpos = $this->pos + $len;
00275 if ( $newpos > 0x7fffffff ) {
00276 throw new MWException( 'A value in the CDB file is too large' );
00277 }
00278 $this->pos = $newpos;
00279 }
00280
00281 protected function addend( $keylen, $datalen, $h ) {
00282 $this->hplist[] = array(
00283 'h' => $h,
00284 'p' => $this->pos
00285 );
00286
00287 $this->numentries++;
00288 $this->posplus( 8 );
00289 $this->posplus( $keylen );
00290 $this->posplus( $datalen );
00291 }
00292
00293 protected function addbegin( $keylen, $datalen ) {
00294 if ( $keylen > 0x7fffffff ) {
00295 throw new MWException( __METHOD__.': key length too long' );
00296 }
00297 if ( $datalen > 0x7fffffff ) {
00298 throw new MWException( __METHOD__.': data length too long' );
00299 }
00300 $buf = pack( 'VV', $keylen, $datalen );
00301 $this->write( $buf );
00302 }
00303
00304 protected function finish() {
00305
00306 $this->hplist = array_reverse( $this->hplist );
00307
00308
00309 $counts = array_fill( 0, 256, 0 );
00310 foreach ( $this->hplist as $item ) {
00311 ++ $counts[ 255 & $item['h'] ];
00312 }
00313
00314
00315 $starts = array();
00316 $pos = 0;
00317 for ( $i = 0; $i < 256; ++$i ) {
00318 $pos += $counts[$i];
00319 $starts[$i] = $pos;
00320 }
00321
00322
00323
00324
00325 $packedTables = array_fill( 0, $this->numentries, false );
00326 foreach ( $this->hplist as $item ) {
00327 $packedTables[--$starts[255 & $item['h']]] = $item;
00328 }
00329
00330 $final = '';
00331 for ( $i = 0; $i < 256; ++$i ) {
00332 $count = $counts[$i];
00333
00334
00335
00336 $len = $count + $count;
00337 $final .= pack( 'VV', $this->pos, $len );
00338
00339 $hashtable = array();
00340 for ( $u = 0; $u < $len; ++$u ) {
00341 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
00342 }
00343
00344
00345
00346 for ( $u = 0; $u < $count; ++$u ) {
00347 $hp = $packedTables[$starts[$i] + $u];
00348 $where = CdbFunctions::unsignedMod(
00349 CdbFunctions::unsignedShiftRight( $hp['h'], 8 ), $len );
00350 while ( $hashtable[$where]['p'] )
00351 if ( ++$where == $len )
00352 $where = 0;
00353 $hashtable[$where] = $hp;
00354 }
00355
00356
00357 for ( $u = 0; $u < $len; ++$u ) {
00358 $buf = pack( 'vvV',
00359 $hashtable[$u]['h'] & 0xffff,
00360 CdbFunctions::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
00361 $hashtable[$u]['p'] );
00362 $this->write( $buf );
00363 $this->posplus( 8 );
00364 }
00365 }
00366
00367
00368 rewind( $this->handle );
00369 if ( ftell( $this->handle ) != 0 ) {
00370 throw new MWException( __METHOD__.': Error rewinding to start of file' );
00371 }
00372 $this->write( $final );
00373 }
00374 }