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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / vendor / maxmind-db / reader / src / MaxMind / Db / Reader.php
woocommerce / vendor / maxmind-db / reader / src / MaxMind / Db Last commit date
Reader 10 months ago Reader.php 10 months ago
Reader.php
411 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace MaxMind\Db;
6
7 use MaxMind\Db\Reader\Decoder;
8 use MaxMind\Db\Reader\InvalidDatabaseException;
9 use MaxMind\Db\Reader\Metadata;
10 use MaxMind\Db\Reader\Util;
11
12 /**
13 * Instances of this class provide a reader for the MaxMind DB format. IP
14 * addresses can be looked up using the get method.
15 */
16 class Reader
17 {
18 /**
19 * @var int
20 */
21 private static $DATA_SECTION_SEPARATOR_SIZE = 16;
22
23 /**
24 * @var string
25 */
26 private static $METADATA_START_MARKER = "\xAB\xCD\xEFMaxMind.com";
27
28 /**
29 * @var int<0, max>
30 */
31 private static $METADATA_START_MARKER_LENGTH = 14;
32
33 /**
34 * @var int
35 */
36 private static $METADATA_MAX_SIZE = 131072; // 128 * 1024 = 128KiB
37
38 /**
39 * @var Decoder
40 */
41 private $decoder;
42
43 /**
44 * @var resource
45 */
46 private $fileHandle;
47
48 /**
49 * @var int
50 */
51 private $fileSize;
52
53 /**
54 * @var int
55 */
56 private $ipV4Start;
57
58 /**
59 * @var Metadata
60 */
61 private $metadata;
62
63 /**
64 * Constructs a Reader for the MaxMind DB format. The file passed to it must
65 * be a valid MaxMind DB file such as a GeoIp2 database file.
66 *
67 * @param string $database the MaxMind DB file to use
68 *
69 * @throws \InvalidArgumentException for invalid database path or unknown arguments
70 * @throws InvalidDatabaseException
71 * if the database is invalid or there is an error reading
72 * from it
73 */
74 public function __construct(string $database)
75 {
76 if (\func_num_args() !== 1) {
77 throw new \ArgumentCountError(
78 \sprintf('%s() expects exactly 1 parameter, %d given', __METHOD__, \func_num_args())
79 );
80 }
81
82 if (is_dir($database)) {
83 // This matches the error that the C extension throws.
84 throw new InvalidDatabaseException(
85 "Error opening database file ($database). Is this a valid MaxMind DB file?"
86 );
87 }
88
89 $fileHandle = @fopen($database, 'rb');
90 if ($fileHandle === false) {
91 throw new \InvalidArgumentException(
92 "The file \"$database\" does not exist or is not readable."
93 );
94 }
95 $this->fileHandle = $fileHandle;
96
97 $fileSize = @filesize($database);
98 if ($fileSize === false) {
99 throw new \UnexpectedValueException(
100 "Error determining the size of \"$database\"."
101 );
102 }
103 $this->fileSize = $fileSize;
104
105 $start = $this->findMetadataStart($database);
106 $metadataDecoder = new Decoder($this->fileHandle, $start);
107 [$metadataArray] = $metadataDecoder->decode($start);
108 $this->metadata = new Metadata($metadataArray);
109 $this->decoder = new Decoder(
110 $this->fileHandle,
111 $this->metadata->searchTreeSize + self::$DATA_SECTION_SEPARATOR_SIZE
112 );
113 $this->ipV4Start = $this->ipV4StartNode();
114 }
115
116 /**
117 * Retrieves the record for the IP address.
118 *
119 * @param string $ipAddress the IP address to look up
120 *
121 * @throws \BadMethodCallException if this method is called on a closed database
122 * @throws \InvalidArgumentException if something other than a single IP address is passed to the method
123 * @throws InvalidDatabaseException
124 * if the database is invalid or there is an error reading
125 * from it
126 *
127 * @return mixed the record for the IP address
128 */
129 public function get(string $ipAddress)
130 {
131 if (\func_num_args() !== 1) {
132 throw new \ArgumentCountError(
133 \sprintf('%s() expects exactly 1 parameter, %d given', __METHOD__, \func_num_args())
134 );
135 }
136 [$record] = $this->getWithPrefixLen($ipAddress);
137
138 return $record;
139 }
140
141 /**
142 * Retrieves the record for the IP address and its associated network prefix length.
143 *
144 * @param string $ipAddress the IP address to look up
145 *
146 * @throws \BadMethodCallException if this method is called on a closed database
147 * @throws \InvalidArgumentException if something other than a single IP address is passed to the method
148 * @throws InvalidDatabaseException
149 * if the database is invalid or there is an error reading
150 * from it
151 *
152 * @return array{0:mixed, 1:int} an array where the first element is the record and the
153 * second the network prefix length for the record
154 */
155 public function getWithPrefixLen(string $ipAddress): array
156 {
157 if (\func_num_args() !== 1) {
158 throw new \ArgumentCountError(
159 \sprintf('%s() expects exactly 1 parameter, %d given', __METHOD__, \func_num_args())
160 );
161 }
162
163 if (!\is_resource($this->fileHandle)) {
164 throw new \BadMethodCallException(
165 'Attempt to read from a closed MaxMind DB.'
166 );
167 }
168
169 [$pointer, $prefixLen] = $this->findAddressInTree($ipAddress);
170 if ($pointer === 0) {
171 return [null, $prefixLen];
172 }
173
174 return [$this->resolveDataPointer($pointer), $prefixLen];
175 }
176
177 /**
178 * @return array{0:int, 1:int}
179 */
180 private function findAddressInTree(string $ipAddress): array
181 {
182 $packedAddr = @inet_pton($ipAddress);
183 if ($packedAddr === false) {
184 throw new \InvalidArgumentException(
185 "The value \"$ipAddress\" is not a valid IP address."
186 );
187 }
188
189 $rawAddress = unpack('C*', $packedAddr);
190 if ($rawAddress === false) {
191 throw new InvalidDatabaseException(
192 'Could not unpack the unsigned char of the packed in_addr representation.'
193 );
194 }
195
196 $bitCount = \count($rawAddress) * 8;
197
198 // The first node of the tree is always node 0, at the beginning of the
199 // value
200 $node = 0;
201
202 $metadata = $this->metadata;
203
204 // Check if we are looking up an IPv4 address in an IPv6 tree. If this
205 // is the case, we can skip over the first 96 nodes.
206 if ($metadata->ipVersion === 6) {
207 if ($bitCount === 32) {
208 $node = $this->ipV4Start;
209 }
210 } elseif ($metadata->ipVersion === 4 && $bitCount === 128) {
211 throw new \InvalidArgumentException(
212 "Error looking up $ipAddress. You attempted to look up an"
213 . ' IPv6 address in an IPv4-only database.'
214 );
215 }
216
217 $nodeCount = $metadata->nodeCount;
218
219 for ($i = 0; $i < $bitCount && $node < $nodeCount; ++$i) {
220 $tempBit = 0xFF & $rawAddress[($i >> 3) + 1];
221 $bit = 1 & ($tempBit >> 7 - ($i % 8));
222
223 $node = $this->readNode($node, $bit);
224 }
225 if ($node === $nodeCount) {
226 // Record is empty
227 return [0, $i];
228 }
229 if ($node > $nodeCount) {
230 // Record is a data pointer
231 return [$node, $i];
232 }
233
234 throw new InvalidDatabaseException(
235 'Invalid or corrupt database. Maximum search depth reached without finding a leaf node'
236 );
237 }
238
239 private function ipV4StartNode(): int
240 {
241 // If we have an IPv4 database, the start node is the first node
242 if ($this->metadata->ipVersion === 4) {
243 return 0;
244 }
245
246 $node = 0;
247
248 for ($i = 0; $i < 96 && $node < $this->metadata->nodeCount; ++$i) {
249 $node = $this->readNode($node, 0);
250 }
251
252 return $node;
253 }
254
255 private function readNode(int $nodeNumber, int $index): int
256 {
257 $baseOffset = $nodeNumber * $this->metadata->nodeByteSize;
258
259 switch ($this->metadata->recordSize) {
260 case 24:
261 $bytes = Util::read($this->fileHandle, $baseOffset + $index * 3, 3);
262 $rc = unpack('N', "\x00" . $bytes);
263 if ($rc === false) {
264 throw new InvalidDatabaseException(
265 'Could not unpack the unsigned long of the node.'
266 );
267 }
268 [, $node] = $rc;
269
270 return $node;
271
272 case 28:
273 $bytes = Util::read($this->fileHandle, $baseOffset + 3 * $index, 4);
274 if ($index === 0) {
275 $middle = (0xF0 & \ord($bytes[3])) >> 4;
276 } else {
277 $middle = 0x0F & \ord($bytes[0]);
278 }
279 $rc = unpack('N', \chr($middle) . substr($bytes, $index, 3));
280 if ($rc === false) {
281 throw new InvalidDatabaseException(
282 'Could not unpack the unsigned long of the node.'
283 );
284 }
285 [, $node] = $rc;
286
287 return $node;
288
289 case 32:
290 $bytes = Util::read($this->fileHandle, $baseOffset + $index * 4, 4);
291 $rc = unpack('N', $bytes);
292 if ($rc === false) {
293 throw new InvalidDatabaseException(
294 'Could not unpack the unsigned long of the node.'
295 );
296 }
297 [, $node] = $rc;
298
299 return $node;
300
301 default:
302 throw new InvalidDatabaseException(
303 'Unknown record size: '
304 . $this->metadata->recordSize
305 );
306 }
307 }
308
309 /**
310 * @return mixed
311 */
312 private function resolveDataPointer(int $pointer)
313 {
314 $resolved = $pointer - $this->metadata->nodeCount
315 + $this->metadata->searchTreeSize;
316 if ($resolved >= $this->fileSize) {
317 throw new InvalidDatabaseException(
318 "The MaxMind DB file's search tree is corrupt"
319 );
320 }
321
322 [$data] = $this->decoder->decode($resolved);
323
324 return $data;
325 }
326
327 /*
328 * This is an extremely naive but reasonably readable implementation. There
329 * are much faster algorithms (e.g., Boyer-Moore) for this if speed is ever
330 * an issue, but I suspect it won't be.
331 */
332 private function findMetadataStart(string $filename): int
333 {
334 $handle = $this->fileHandle;
335 $fstat = fstat($handle);
336 if ($fstat === false) {
337 throw new InvalidDatabaseException(
338 "Error getting file information ($filename)."
339 );
340 }
341 $fileSize = $fstat['size'];
342 $marker = self::$METADATA_START_MARKER;
343 $markerLength = self::$METADATA_START_MARKER_LENGTH;
344
345 $minStart = $fileSize - min(self::$METADATA_MAX_SIZE, $fileSize);
346
347 for ($offset = $fileSize - $markerLength; $offset >= $minStart; --$offset) {
348 if (fseek($handle, $offset) !== 0) {
349 break;
350 }
351
352 $value = fread($handle, $markerLength);
353 if ($value === $marker) {
354 return $offset + $markerLength;
355 }
356 }
357
358 throw new InvalidDatabaseException(
359 "Error opening database file ($filename). "
360 . 'Is this a valid MaxMind DB file?'
361 );
362 }
363
364 /**
365 * @throws \InvalidArgumentException if arguments are passed to the method
366 * @throws \BadMethodCallException if the database has been closed
367 *
368 * @return Metadata object for the database
369 */
370 public function metadata(): Metadata
371 {
372 if (\func_num_args()) {
373 throw new \ArgumentCountError(
374 \sprintf('%s() expects exactly 0 parameters, %d given', __METHOD__, \func_num_args())
375 );
376 }
377
378 // Not technically required, but this makes it consistent with
379 // C extension and it allows us to change our implementation later.
380 if (!\is_resource($this->fileHandle)) {
381 throw new \BadMethodCallException(
382 'Attempt to read from a closed MaxMind DB.'
383 );
384 }
385
386 return clone $this->metadata;
387 }
388
389 /**
390 * Closes the MaxMind DB and returns resources to the system.
391 *
392 * @throws \Exception
393 * if an I/O error occurs
394 */
395 public function close(): void
396 {
397 if (\func_num_args()) {
398 throw new \ArgumentCountError(
399 \sprintf('%s() expects exactly 0 parameters, %d given', __METHOD__, \func_num_args())
400 );
401 }
402
403 if (!\is_resource($this->fileHandle)) {
404 throw new \BadMethodCallException(
405 'Attempt to close a closed MaxMind DB.'
406 );
407 }
408 fclose($this->fileHandle);
409 }
410 }
411