-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTools.php
More file actions
206 lines (192 loc) · 4.74 KB
/
Tools.php
File metadata and controls
206 lines (192 loc) · 4.74 KB
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
<?php declare(strict_types=1);
namespace danog\Decoder;
\define('BIG_ENDIAN', \pack('L', 1) === \pack('N', 1));
/** @internal */
final class Tools
{
public const WEB_LOCATION_FLAG = 1 << 24;
public const FILE_REFERENCE_FLAG = 1 << 25;
/**
* Unpack long properly, returns an actual number in any case.
*
* @internal
*
* @param string $field Field to unpack
*/
public static function unpackLong(string $field): int
{
/** @var int */
return \unpack('q', BIG_ENDIAN ? \strrev($field) : $field)[1];
}
/**
* Unpack integer.
* @internal
*
* @param string $field Field to unpack
*/
public static function unpackInt(string $field): int
{
/** @var int */
return \unpack('l', $field)[1];
}
/**
* Pack string long.
*
* @internal
*
*/
public static function packLong(int $field): string
{
$res = \pack('q', $field);
return BIG_ENDIAN ? \strrev($res) : $res;
}
/**
* Base64URL decode.
*
* @param string $data Data to decode
*
* @internal
*
*/
public static function base64urlDecode(string $data): string
{
return \base64_decode(\str_pad(\strtr($data, '-_', '+/'), \strlen($data) % 4, '=', STR_PAD_RIGHT));
}
/**
* Base64URL encode.
*
* @param string $data Data to encode
*
* @internal
*
*/
public static function base64urlEncode(string $data): string
{
return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '=');
}
/**
* Null-byte RLE decode.
*
* @param string $string Data to decode
*
* @internal
*
*/
public static function rleDecode(string $string): string
{
$new = '';
$last = '';
$null = "\0";
foreach (\str_split($string) as $cur) {
if ($last === $null) {
$new .= \str_repeat($last, \ord($cur));
$last = '';
} else {
$new .= $last;
$last = $cur;
}
}
$string = $new.$last;
return $string;
}
/**
* Null-byte RLE encode.
*
* @param string $string Data to encode
*
* @internal
*
*/
public static function rleEncode(string $string): string
{
$new = '';
$count = 0;
$null = "\0";
foreach (\str_split($string) as $cur) {
if ($cur === $null) {
++$count;
} else {
if ($count > 0) {
$new .= $null.\chr($count);
$count = 0;
}
$new .= $cur;
}
}
if ($count > 0) {
$new .= $null.\chr($count);
}
return $new;
}
/**
* Positive modulo
* Works just like the % (modulus) operator, only returns always a postive number.
*
* @param int $a A
* @param int $b B
*
* @internal
*
* @return int Modulo
*/
public static function posmod(int $a, int $b): int
{
$resto = $a % $b;
return $resto < 0 ? $resto + \abs($b) : $resto;
}
/**
* Read TL string.
*
* @param resource $stream Byte stream
*
* @internal
*
*/
public static function readTLString(mixed $stream): string
{
$l = \ord(\stream_get_contents($stream, 1));
if ($l > 254) {
throw new \InvalidArgumentException("Length too big!");
}
if ($l === 254) {
/** @var int */
$long_len = \unpack('V', \stream_get_contents($stream, 3).\chr(0))[1];
$x = \stream_get_contents($stream, $long_len);
$resto = self::posmod(-$long_len, 4);
if ($resto > 0) {
\fseek($stream, $resto, SEEK_CUR);
}
} else {
$x = $l ? \stream_get_contents($stream, $l) : '';
$resto = self::posmod(-($l + 1), 4);
if ($resto > 0) {
\fseek($stream, $resto, SEEK_CUR);
}
}
\assert($x !== false);
return $x;
}
/**
* Pack TL string.
*
* @param string $string String
*
* @internal
*/
public static function packTLString(string $string): string
{
$l = \strlen($string);
$concat = '';
if ($l <= 253) {
$concat .= \chr($l);
$concat .= $string;
$concat .= \pack('@'.self::posmod(-$l - 1, 4));
} else {
$concat .= \chr(254);
$concat .= \substr(\pack('V', $l), 0, 3);
$concat .= $string;
$concat .= \pack('@'.self::posmod(-$l, 4));
}
return $concat;
}
}