Decoder.php
10 months ago
InvalidDatabaseException.php
2 years ago
Metadata.php
10 months ago
Util.php
2 years ago
Util.php
34 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace MaxMind\Db\Reader; |
| 6 | |
| 7 | class Util |
| 8 | { |
| 9 | /** |
| 10 | * @param resource $stream |
| 11 | * @param int<0, max> $numberOfBytes |
| 12 | */ |
| 13 | public static function read($stream, int $offset, int $numberOfBytes): string |
| 14 | { |
| 15 | if ($numberOfBytes === 0) { |
| 16 | return ''; |
| 17 | } |
| 18 | if (fseek($stream, $offset) === 0) { |
| 19 | $value = fread($stream, $numberOfBytes); |
| 20 | |
| 21 | // We check that the number of bytes read is equal to the number |
| 22 | // asked for. We use ftell as getting the length of $value is |
| 23 | // much slower. |
| 24 | if ($value !== false && ftell($stream) - $offset === $numberOfBytes) { |
| 25 | return $value; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | throw new InvalidDatabaseException( |
| 30 | 'The MaxMind DB file contains bad data' |
| 31 | ); |
| 32 | } |
| 33 | } |
| 34 |