introduce a new setting that allows extension authors to whitelist deprecated funtion...
[lhc/web/wiklou.git] / includes / Cdb_PHP.php
1 <?php
2 /**
3 * This is a port of D.J. Bernstein's CDB to PHP. It's based on the copy that
4 * appears in PHP 5.3. Changes are:
5 * * Error returns replaced with exceptions
6 * * Exception thrown if sizes or offsets are between 2GB and 4GB
7 * * Some variables renamed
8 *
9 * @file
10 */
11
12 /**
13 * Common functions for readers and writers
14 */
15 class CdbFunctions {
16 /**
17 * Take a modulo of a signed integer as if it were an unsigned integer.
18 * $b must be less than 0x40000000 and greater than 0
19 *
20 * @param $a
21 * @param $b
22 *
23 * @return int
24 */
25 public static function unsignedMod( $a, $b ) {
26 if ( $a & 0x80000000 ) {
27 $m = ( $a & 0x7fffffff ) % $b + 2 * ( 0x40000000 % $b );
28 return $m % $b;
29 } else {
30 return $a % $b;
31 }
32 }
33
34 /**
35 * Shift a signed integer right as if it were unsigned
36 * @param $a
37 * @param $b
38 * @return int
39 */
40 public static function unsignedShiftRight( $a, $b ) {
41 if ( $b == 0 ) {
42 return $a;
43 }
44 if ( $a & 0x80000000 ) {
45 return ( ( $a & 0x7fffffff ) >> $b ) | ( 0x40000000 >> ( $b - 1 ) );
46 } else {
47 return $a >> $b;
48 }
49 }
50
51 /**
52 * The CDB hash function.
53 *
54 * @param $s
55 *
56 * @return
57 */
58 public static function hash( $s ) {
59 $h = 5381;
60 for ( $i = 0; $i < strlen( $s ); $i++ ) {
61 $h5 = ($h << 5) & 0xffffffff;
62 // Do a 32-bit sum
63 // Inlined here for speed
64 $sum = ($h & 0x3fffffff) + ($h5 & 0x3fffffff);
65 $h =
66 (
67 ( $sum & 0x40000000 ? 1 : 0 )
68 + ( $h & 0x80000000 ? 2 : 0 )
69 + ( $h & 0x40000000 ? 1 : 0 )
70 + ( $h5 & 0x80000000 ? 2 : 0 )
71 + ( $h5 & 0x40000000 ? 1 : 0 )
72 ) << 30
73 | ( $sum & 0x3fffffff );
74 $h ^= ord( $s[$i] );
75 $h &= 0xffffffff;
76 }
77 return $h;
78 }
79 }
80
81 /**
82 * CDB reader class
83 */
84 class CdbReader_PHP extends CdbReader {
85 /** The file handle */
86 var $handle;
87
88 /* number of hash slots searched under this key */
89 var $loop;
90
91 /* initialized if loop is nonzero */
92 var $khash;
93
94 /* initialized if loop is nonzero */
95 var $kpos;
96
97 /* initialized if loop is nonzero */
98 var $hpos;
99
100 /* initialized if loop is nonzero */
101 var $hslots;
102
103 /* initialized if findNext() returns true */
104 var $dpos;
105
106 /* initialized if cdb_findnext() returns 1 */
107 var $dlen;
108
109 /**
110 * @param $fileName string
111 */
112 function __construct( $fileName ) {
113 $this->handle = fopen( $fileName, 'rb' );
114 if ( !$this->handle ) {
115 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
116 }
117 $this->findStart();
118 }
119
120 function close() {
121 if( isset( $this->handle ) ) {
122 fclose( $this->handle );
123 }
124 unset( $this->handle );
125 }
126
127 /**
128 * @param $key
129 * @return bool|string
130 */
131 public function get( $key ) {
132 // strval is required
133 if ( $this->find( strval( $key ) ) ) {
134 return $this->read( $this->dlen, $this->dpos );
135 } else {
136 return false;
137 }
138 }
139
140 /**
141 * @param $key
142 * @param $pos
143 * @return bool
144 */
145 protected function match( $key, $pos ) {
146 $buf = $this->read( strlen( $key ), $pos );
147 return $buf === $key;
148 }
149
150 protected function findStart() {
151 $this->loop = 0;
152 }
153
154 /**
155 * @throws MWException
156 * @param $length
157 * @param $pos
158 * @return string
159 */
160 protected function read( $length, $pos ) {
161 if ( fseek( $this->handle, $pos ) == -1 ) {
162 // This can easily happen if the internal pointers are incorrect
163 throw new MWException( __METHOD__.': seek failed, file may be corrupted.' );
164 }
165
166 if ( $length == 0 ) {
167 return '';
168 }
169
170 $buf = fread( $this->handle, $length );
171 if ( $buf === false || strlen( $buf ) !== $length ) {
172 throw new MWException( __METHOD__.': read from CDB file failed, file may be corrupted' );
173 }
174 return $buf;
175 }
176
177 /**
178 * Unpack an unsigned integer and throw an exception if it needs more than 31 bits
179 * @param $s
180 * @return
181 */
182 protected function unpack31( $s ) {
183 $data = unpack( 'V', $s );
184 if ( $data[1] > 0x7fffffff ) {
185 throw new MWException( __METHOD__.': error in CDB file, integer too big' );
186 }
187 return $data[1];
188 }
189
190 /**
191 * Unpack a 32-bit signed integer
192 * @param $s
193 * @return int
194 */
195 protected function unpackSigned( $s ) {
196 $data = unpack( 'va/vb', $s );
197 return $data['a'] | ( $data['b'] << 16 );
198 }
199
200 /**
201 * @param $key
202 * @return bool
203 */
204 protected function findNext( $key ) {
205 if ( !$this->loop ) {
206 $u = CdbFunctions::hash( $key );
207 $buf = $this->read( 8, ( $u << 3 ) & 2047 );
208 $this->hslots = $this->unpack31( substr( $buf, 4 ) );
209 if ( !$this->hslots ) {
210 return false;
211 }
212 $this->hpos = $this->unpack31( substr( $buf, 0, 4 ) );
213 $this->khash = $u;
214 $u = CdbFunctions::unsignedShiftRight( $u, 8 );
215 $u = CdbFunctions::unsignedMod( $u, $this->hslots );
216 $u <<= 3;
217 $this->kpos = $this->hpos + $u;
218 }
219
220 while ( $this->loop < $this->hslots ) {
221 $buf = $this->read( 8, $this->kpos );
222 $pos = $this->unpack31( substr( $buf, 4 ) );
223 if ( !$pos ) {
224 return false;
225 }
226 $this->loop += 1;
227 $this->kpos += 8;
228 if ( $this->kpos == $this->hpos + ( $this->hslots << 3 ) ) {
229 $this->kpos = $this->hpos;
230 }
231 $u = $this->unpackSigned( substr( $buf, 0, 4 ) );
232 if ( $u === $this->khash ) {
233 $buf = $this->read( 8, $pos );
234 $keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
235 if ( $keyLen == strlen( $key ) && $this->match( $key, $pos + 8 ) ) {
236 // Found
237 $this->dlen = $this->unpack31( substr( $buf, 4 ) );
238 $this->dpos = $pos + 8 + $keyLen;
239 return true;
240 }
241 }
242 }
243 return false;
244 }
245
246 /**
247 * @param $key
248 * @return bool
249 */
250 protected function find( $key ) {
251 $this->findStart();
252 return $this->findNext( $key );
253 }
254 }
255
256 /**
257 * CDB writer class
258 */
259 class CdbWriter_PHP extends CdbWriter {
260 var $handle, $realFileName, $tmpFileName;
261
262 var $hplist;
263 var $numentries, $pos;
264
265 /**
266 * @param $fileName string
267 */
268 function __construct( $fileName ) {
269 $this->realFileName = $fileName;
270 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
271 $this->handle = fopen( $this->tmpFileName, 'wb' );
272 if ( !$this->handle ) {
273 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
274 }
275 $this->hplist = array();
276 $this->numentries = 0;
277 $this->pos = 2048; // leaving space for the pointer array, 256 * 8
278 if ( fseek( $this->handle, $this->pos ) == -1 ) {
279 throw new MWException( __METHOD__.': fseek failed' );
280 }
281 }
282
283 function __destruct() {
284 if ( isset( $this->handle ) ) {
285 $this->close();
286 }
287 }
288
289 /**
290 * @param $key
291 * @param $value
292 * @return
293 */
294 public function set( $key, $value ) {
295 if ( strval( $key ) === '' ) {
296 // DBA cross-check hack
297 return;
298 }
299 $this->addbegin( strlen( $key ), strlen( $value ) );
300 $this->write( $key );
301 $this->write( $value );
302 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions::hash( $key ) );
303 }
304
305 /**
306 * @throws MWException
307 */
308 public function close() {
309 $this->finish();
310 if( isset($this->handle) ) {
311 fclose( $this->handle );
312 }
313 if ( wfIsWindows() && file_exists($this->realFileName) ) {
314 unlink( $this->realFileName );
315 }
316 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
317 throw new MWException( 'Unable to move the new CDB file into place.' );
318 }
319 unset( $this->handle );
320 }
321
322 /**
323 * @throws MWException
324 * @param $buf
325 */
326 protected function write( $buf ) {
327 $len = fwrite( $this->handle, $buf );
328 if ( $len !== strlen( $buf ) ) {
329 throw new MWException( 'Error writing to CDB file.' );
330 }
331 }
332
333 /**
334 * @throws MWException
335 * @param $len
336 */
337 protected function posplus( $len ) {
338 $newpos = $this->pos + $len;
339 if ( $newpos > 0x7fffffff ) {
340 throw new MWException( 'A value in the CDB file is too large' );
341 }
342 $this->pos = $newpos;
343 }
344
345 /**
346 * @param $keylen
347 * @param $datalen
348 * @param $h
349 */
350 protected function addend( $keylen, $datalen, $h ) {
351 $this->hplist[] = array(
352 'h' => $h,
353 'p' => $this->pos
354 );
355
356 $this->numentries++;
357 $this->posplus( 8 );
358 $this->posplus( $keylen );
359 $this->posplus( $datalen );
360 }
361
362 /**
363 * @throws MWException
364 * @param $keylen
365 * @param $datalen
366 */
367 protected function addbegin( $keylen, $datalen ) {
368 if ( $keylen > 0x7fffffff ) {
369 throw new MWException( __METHOD__.': key length too long' );
370 }
371 if ( $datalen > 0x7fffffff ) {
372 throw new MWException( __METHOD__.': data length too long' );
373 }
374 $buf = pack( 'VV', $keylen, $datalen );
375 $this->write( $buf );
376 }
377
378 /**
379 * @throws MWException
380 */
381 protected function finish() {
382 // Hack for DBA cross-check
383 $this->hplist = array_reverse( $this->hplist );
384
385 // Calculate the number of items that will be in each hashtable
386 $counts = array_fill( 0, 256, 0 );
387 foreach ( $this->hplist as $item ) {
388 ++ $counts[ 255 & $item['h'] ];
389 }
390
391 // Fill in $starts with the *end* indexes
392 $starts = array();
393 $pos = 0;
394 for ( $i = 0; $i < 256; ++$i ) {
395 $pos += $counts[$i];
396 $starts[$i] = $pos;
397 }
398
399 // Excessively clever and indulgent code to simultaneously fill $packedTables
400 // with the packed hashtables, and adjust the elements of $starts
401 // to actually point to the starts instead of the ends.
402 $packedTables = array_fill( 0, $this->numentries, false );
403 foreach ( $this->hplist as $item ) {
404 $packedTables[--$starts[255 & $item['h']]] = $item;
405 }
406
407 $final = '';
408 for ( $i = 0; $i < 256; ++$i ) {
409 $count = $counts[$i];
410
411 // The size of the hashtable will be double the item count.
412 // The rest of the slots will be empty.
413 $len = $count + $count;
414 $final .= pack( 'VV', $this->pos, $len );
415
416 $hashtable = array();
417 for ( $u = 0; $u < $len; ++$u ) {
418 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
419 }
420
421 // Fill the hashtable, using the next empty slot if the hashed slot
422 // is taken.
423 for ( $u = 0; $u < $count; ++$u ) {
424 $hp = $packedTables[$starts[$i] + $u];
425 $where = CdbFunctions::unsignedMod(
426 CdbFunctions::unsignedShiftRight( $hp['h'], 8 ), $len );
427 while ( $hashtable[$where]['p'] )
428 if ( ++$where == $len )
429 $where = 0;
430 $hashtable[$where] = $hp;
431 }
432
433 // Write the hashtable
434 for ( $u = 0; $u < $len; ++$u ) {
435 $buf = pack( 'vvV',
436 $hashtable[$u]['h'] & 0xffff,
437 CdbFunctions::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
438 $hashtable[$u]['p'] );
439 $this->write( $buf );
440 $this->posplus( 8 );
441 }
442 }
443
444 // Write the pointer array at the start of the file
445 rewind( $this->handle );
446 if ( ftell( $this->handle ) != 0 ) {
447 throw new MWException( __METHOD__.': Error rewinding to start of file' );
448 }
449 $this->write( $final );
450 }
451 }