1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <[email protected]>. |
7
|
|
|
Copyright (c) 2016 Michal Čihař <[email protected]> |
8
|
|
|
|
9
|
|
|
This file is part of MoTranslator. |
10
|
|
|
|
11
|
|
|
This program is free software; you can redistribute it and/or modify |
12
|
|
|
it under the terms of the GNU General Public License as published by |
13
|
|
|
the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
(at your option) any later version. |
15
|
|
|
|
16
|
|
|
This program is distributed in the hope that it will be useful, |
17
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
GNU General Public License for more details. |
20
|
|
|
|
21
|
|
|
You should have received a copy of the GNU General Public License along |
22
|
|
|
with this program; if not, write to the Free Software Foundation, Inc., |
23
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
24
|
|
|
*/ |
25
|
|
|
namespace PhpMyAdmin\MoTranslator; |
26
|
|
|
|
27
|
|
|
use const PHP_INT_MAX; |
28
|
|
|
use function file_get_contents; |
29
|
|
|
use function strlen; |
30
|
|
|
use function substr; |
31
|
|
|
use function unpack; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Simple wrapper around string buffer for |
35
|
|
|
* random access and values parsing. |
36
|
|
|
*/ |
37
|
|
|
class StringReader |
38
|
|
|
{ |
39
|
|
|
/** @var string */ |
40
|
|
|
private $string; |
41
|
|
|
/** @var int */ |
42
|
|
|
private $length; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $filename Name of file to load |
46
|
|
|
*/ |
47
|
156 |
|
public function __construct(string $filename) |
48
|
|
|
{ |
49
|
156 |
|
$this->string = (string) file_get_contents($filename); |
50
|
156 |
|
$this->length = strlen($this->string); |
51
|
156 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Read number of bytes from given offset. |
55
|
|
|
* |
56
|
|
|
* @param int $pos Offset |
57
|
|
|
* @param int $bytes Number of bytes to read |
58
|
|
|
*/ |
59
|
156 |
|
public function read(int $pos, int $bytes): string |
60
|
|
|
{ |
61
|
156 |
|
if ($pos + $bytes > $this->length) { |
62
|
20 |
|
throw new ReaderException('Not enough bytes!'); |
63
|
|
|
} |
64
|
|
|
|
65
|
152 |
|
return substr($this->string, $pos, $bytes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Reads a 32bit integer from the stream. |
70
|
|
|
* |
71
|
|
|
* @param string $unpack Unpack string |
72
|
|
|
* @param int $pos Position |
73
|
|
|
* |
74
|
|
|
* @return int Ingerer from the stream |
75
|
|
|
*/ |
76
|
148 |
|
public function readint(string $unpack, int $pos): int |
77
|
|
|
{ |
78
|
148 |
|
$data = unpack($unpack, $this->read($pos, 4)); |
79
|
148 |
|
$result = $data[1]; |
80
|
|
|
|
81
|
|
|
/* We're reading unsigned int, but PHP will happily |
82
|
|
|
* give us negative number on 32-bit platforms. |
83
|
|
|
* |
84
|
|
|
* See also documentation: |
85
|
|
|
* https://secure.php.net/manual/en/function.unpack.php#refsect1-function.unpack-notes |
86
|
|
|
*/ |
87
|
148 |
|
return $result < 0 ? PHP_INT_MAX : $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Reads an array of integers from the stream. |
92
|
|
|
* |
93
|
|
|
* @param string $unpack Unpack string |
94
|
|
|
* @param int $pos Position |
95
|
|
|
* @param int $count How many elements should be read |
96
|
|
|
* |
97
|
|
|
* @return array Array of Integers |
98
|
|
|
*/ |
99
|
136 |
|
public function readintarray(string $unpack, int $pos, int $count): array |
100
|
|
|
{ |
101
|
136 |
|
return unpack($unpack . $count, $this->read($pos, 4 * $count)); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|