summaryrefslogtreecommitdiff
path: root/vendor/wikimedia/cdb/src/Reader/PHP.php
blob: 0f0d36ef2d95eee016efad1d2aceb76a6150cef7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php

namespace Cdb\Reader;

use Cdb\Exception;
use Cdb\Reader;
use Cdb\Util;

/**
 * This is a port of D.J. Bernstein's CDB to PHP. It's based on the copy that
 * appears in PHP 5.3.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

/**
 * CDB reader class
 */
class PHP extends Reader {

	/** @var string The file name of the CDB file. **/
	protected $fileName;

	/** @var string First 2048b of CDB file, containing the hash table. **/
	protected $hashTable;

	/** @var int Offset in file where value of found key starts. **/
	protected $dataPos;

	/** @var int Byte length of found key's value. **/
	protected $dataLen;

	/** @var int File position indicator when iterating over keys. **/
	protected $keyIterPos = 2048;

	/** @var string Read buffer for CDB file. **/
	protected $buf;

	/** @var int File offset where read buffer starts. **/
	protected $bufStart;

	/** @var int File handle position indicator **/
	protected $filePos = 2048;

	/**
	 * Constructor.
	 *
	 * @param string $fileName
	 * @throws Exception If CDB file cannot be opened or if it contains fewer
	 *   than 2048 bytes of data.
	 */
	public function __construct( $fileName ) {
		$this->fileName = $fileName;
		$this->handle = fopen( $fileName, 'rb' );
		if ( !$this->handle ) {
			throw new Exception( 'Unable to open CDB file "' . $this->fileName . '".' );
		}
		$this->hashTable = fread( $this->handle, 2048 );
		if ( strlen( $this->hashTable ) !== 2048 ) {
			throw new Exception( 'CDB file contains fewer than 2048 bytes of data.' );
		}
	}

	/**
	 * Close the handle on the CDB file.
	 */
	public function close() {
		if ( isset( $this->handle ) ) {
			fclose( $this->handle );
		}
		unset( $this->handle );
	}

	/**
	 * Get the value of a key.
	 *
	 * @param mixed $key
	 * @return bool|string The key's value or false if not found.
	 */
	public function get( $key ) {
		// strval is required
		if ( $this->find( strval( $key ) ) ) {
			return $this->read( $this->dataPos, $this->dataLen );
		}

		return false;
	}

	/**
	 * Read data from the CDB file.
	 *
	 * @throws Exception When attempting to read past the end of the file.
	 * @param int $start Start reading from this position.
	 * @param int $len Number of bytes to read.
	 * @return string Read data.
	 */
	protected function read( $start, $len ) {
		$end = $start + $len;

		// The first 2048 bytes are the lookup table, which is read into
		// memory on initialization.
		if ( $end <= 2048 ) {
			return substr( $this->hashTable, $start, $len );
		}

		// Read data from the internal buffer first.
		$bytes = '';
		if ( $this->buf && $start >= $this->bufStart ) {
			$bytes .= substr( $this->buf, $start - $this->bufStart, $len );
			$bytesRead = strlen( $bytes );
			$len -= $bytesRead;
			$start += $bytesRead;
		} else {
			$bytesRead = 0;
		}

		if ( !$len ) {
			return $bytes;
		}

		// Many reads are sequential, so the file position indicator may
		// already be in the right place, in which case we can avoid the
		// call to fseek().
		if ( $start !== $this->filePos ) {
			if ( fseek( $this->handle, $start ) === -1 ) {
				// This can easily happen if the internal pointers are incorrect
				throw new Exception(
					'Seek failed, file "' . $this->fileName . '" may be corrupted.' );
			}
		}

		$buf = fread( $this->handle, max( $len, 1024 ) );
		if ( $buf === false ) {
			$buf = '';
		}

		$bytes .= substr( $buf, 0, $len );
		if ( strlen( $bytes ) !== $len + $bytesRead ) {
			throw new Exception(
				'Read from CDB file failed, file "' . $this->fileName . '" may be corrupted.' );
		}

		$this->filePos = $end;
		$this->bufStart = $start;
		$this->buf = $buf;

		return $bytes;
	}

	/**
	 * Unpack an unsigned integer and throw an exception if it needs more than 31 bits.
	 *
	 * @param int $pos Position to read from.
	 * @throws Exception When the integer cannot be represented in 31 bits.
	 * @return int
	 */
	protected function readInt31( $pos = 0 ) {
		$uint31 = $this->readInt32( $pos );
		if ( $uint31 > 0x7fffffff ) {
			throw new Exception(
				'Error in CDB file "' . $this->fileName . '", integer too big.' );
		}

		return $uint31;
	}

	/**
	 * Unpack a 32-bit integer.
	 *
	 * @param int $pos
	 * @return int
	 */
	protected function readInt32( $pos = 0 ) {
		static $lookups;

		if ( !$lookups ) {
			$lookups = array();
			for ( $i = 1; $i < 256; $i++ ) {
				$lookups[ chr( $i ) ] = $i;
			}
		}

		$buf = $this->read( $pos, 4 );

		$rv = 0;

		if ( $buf[0] !== "\x0" ) {
			$rv = $lookups[ $buf[0] ];
		}
		if ( $buf[1] !== "\x0" ) {
			$rv |= ( $lookups[ $buf[1] ] << 8 );
		}
		if ( $buf[2] !== "\x0" ) {
			$rv |= ( $lookups[ $buf[2] ] << 16 );
		}
		if ( $buf[3] !== "\x0" ) {
			$rv |= ( $lookups[ $buf[3] ] << 24 );
		}

		return $rv;
	}

	/**
	 * Search the CDB file for a key.
	 *
	 * Sets `dataLen` and `dataPos` properties if successful.
	 *
	 * @param string $key
	 * @return bool Whether the key was found.
	 */
	protected function find( $key ) {
		$keyLen = strlen( $key );

		$u = Util::hash( $key );
		$upos = ( $u << 3 ) & 2047;
		$hashSlots = $this->readInt31( $upos + 4 );
		if ( !$hashSlots ) {
			return false;
		}
		$hashPos = $this->readInt31( $upos );
		$keyHash = $u;
		$u = Util::unsignedShiftRight( $u, 8 );
		$u = Util::unsignedMod( $u, $hashSlots );
		$u <<= 3;
		$keyPos = $hashPos + $u;

		for ( $i = 0; $i < $hashSlots; $i++ ) {
			$hash = $this->readInt32( $keyPos );
			$pos = $this->readInt31( $keyPos + 4 );
			if ( !$pos ) {
				return false;
			}
			$keyPos += 8;
			if ( $keyPos == $hashPos + ( $hashSlots << 3 ) ) {
				$keyPos = $hashPos;
			}
			if ( $hash === $keyHash ) {
				if ( $keyLen === $this->readInt31( $pos ) ) {
					$dataLen = $this->readInt31( $pos + 4 );
					$dataPos = $pos + 8 + $keyLen;
					$foundKey = $this->read( $pos + 8, $keyLen );
					if ( $foundKey === $key ) {
						// Found
						$this->dataLen = $dataLen;
						$this->dataPos = $dataPos;

						return true;
					}
				}
			}
		}

		return false;
	}

	/**
	 * Check if a key exists in the CDB file.
	 *
	 * @param string $key
	 * @return bool Whether the key exists.
	 */
	public function exists( $key ) {
		return $this->find( strval( $key ) );
	}

	/**
	 * Get the first key from the CDB file and reset the key iterator.
	 *
	 * @return string Key.
	 */
	public function firstkey() {
		$this->keyIterPos = 2048;
		return $this->nextkey();
	}

	/**
	 * Get the next key from the CDB file.
	 *
	 * @return string Key.
	 */
	public function nextkey() {
		$keyLen = $this->readInt31( $this->keyIterPos );
		$dataLen = $this->readInt31( $this->keyIterPos + 4 );
		$key = $this->read( $this->keyIterPos + 8, $keyLen );
		$this->keyIterPos += 8 + $keyLen + $dataLen;

		return $key;
	}
}