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
|
|
|
|
26
|
|
|
namespace PhpMyAdmin\MoTranslator; |
27
|
|
|
|
28
|
|
|
use function file_get_contents; |
29
|
|
|
use function strlen; |
30
|
|
|
use function substr; |
31
|
|
|
use function unpack; |
32
|
|
|
|
33
|
|
|
use const PHP_INT_MAX; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Simple wrapper around string buffer for |
37
|
|
|
* random access and values parsing. |
38
|
|
|
*/ |
39
|
|
|
class StringReader |
40
|
|
|
{ |
41
|
|
|
private string $string; |
42
|
|
|
private int $length; |
43
|
|
|
|
44
|
|
|
/** @param string $filename Name of file to load */ |
45
|
|
|
public function __construct(string $filename) |
46
|
|
|
{ |
47
|
|
|
$this->string = (string) file_get_contents($filename); |
48
|
|
|
$this->length = strlen($this->string); |
49
|
588 |
|
} |
50
|
|
|
|
51
|
588 |
|
/** |
52
|
588 |
|
* Read number of bytes from given offset. |
53
|
84 |
|
* |
54
|
|
|
* @param int $pos Offset |
55
|
|
|
* @param int $bytes Number of bytes to read |
56
|
|
|
*/ |
57
|
|
|
public function read(int $pos, int $bytes): string |
58
|
|
|
{ |
59
|
|
|
if ($pos + $bytes > $this->length) { |
60
|
|
|
throw new ReaderException('Not enough bytes!'); |
61
|
588 |
|
} |
62
|
|
|
|
63
|
588 |
|
return substr($this->string, $pos, $bytes); |
64
|
70 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
574 |
|
* Reads a 32bit integer from the stream. |
68
|
|
|
* |
69
|
574 |
|
* @param string $unpack Unpack string |
70
|
|
|
* @param int $pos Position |
71
|
|
|
* |
72
|
|
|
* @return int Integer from the stream |
73
|
|
|
*/ |
74
|
|
|
public function readint(string $unpack, int $pos): int |
75
|
|
|
{ |
76
|
|
|
$data = unpack($unpack, $this->read($pos, 4)); |
77
|
|
|
if ($data === false) { |
78
|
|
|
return PHP_INT_MAX; |
79
|
|
|
} |
80
|
532 |
|
|
81
|
|
|
$result = $data[1]; |
82
|
532 |
|
|
83
|
532 |
|
/* We're reading unsigned int, but PHP will happily |
84
|
|
|
* give us negative number on 32-bit platforms. |
85
|
|
|
* |
86
|
|
|
* See also documentation: |
87
|
532 |
|
* https://secure.php.net/manual/en/function.unpack.php#refsect1-function.unpack-notes |
88
|
|
|
*/ |
89
|
|
|
return $result < 0 ? PHP_INT_MAX : $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Reads an array of integers from the stream. |
94
|
|
|
* |
95
|
532 |
|
* @param string $unpack Unpack string |
96
|
|
|
* @param int $pos Position |
97
|
|
|
* @param int $count How many elements should be read |
98
|
|
|
* |
99
|
|
|
* @return int[] Array of Integers |
100
|
|
|
*/ |
101
|
|
|
public function readintarray(string $unpack, int $pos, int $count): array |
102
|
|
|
{ |
103
|
|
|
$data = unpack($unpack . $count, $this->read($pos, 4 * $count)); |
104
|
|
|
if ($data === false) { |
105
|
|
|
return []; |
106
|
|
|
} |
107
|
504 |
|
|
108
|
|
|
return $data; |
109
|
504 |
|
} |
110
|
|
|
} |
111
|
|
|
|