1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-12-11 17:39:37 +01:00
MadelineProto/tools/TL/Builder.php

560 lines
21 KiB
PHP
Raw Normal View History

2024-02-13 21:14:18 +01:00
<?php
declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\TL;
2024-03-08 13:19:15 +01:00
use AssertionError;
2024-03-10 16:07:35 +01:00
use danog\MadelineProto\Connection;
use danog\MadelineProto\Lang;
2024-03-08 13:19:15 +01:00
use danog\MadelineProto\MTProto;
use danog\MadelineProto\MTProtoTools\MinDatabase;
use danog\MadelineProto\MTProtoTools\PeerDatabase;
use danog\MadelineProto\MTProtoTools\ReferenceDatabase;
2024-03-10 16:07:35 +01:00
use danog\MadelineProto\SecurityException;
2024-02-13 21:14:18 +01:00
use danog\MadelineProto\Settings\TLSchema;
2024-03-10 15:48:55 +01:00
use ReflectionClass;
2024-03-08 13:19:15 +01:00
use ReflectionFunction;
use Webmozart\Assert\Assert;
2024-02-13 21:14:18 +01:00
/**
* @internal
*/
2024-03-10 15:48:55 +01:00
class Builder
2024-02-13 21:14:18 +01:00
{
/**
* TL instance.
*/
2024-03-10 15:48:55 +01:00
protected TL $TL;
protected readonly array $byType;
protected readonly array $idByPredicate;
protected readonly array $typeByPredicate;
protected readonly array $constructorByPredicate;
protected readonly array $methodVectorTypes;
protected readonly string $class;
protected $output;
2024-02-13 21:14:18 +01:00
public function __construct(
TLSchema $settings,
/**
* Output file.
*/
2024-03-02 13:03:09 +01:00
string $output,
2024-02-13 21:14:18 +01:00
/**
* Output namespace.
*/
2024-03-10 15:48:55 +01:00
protected string $namespace,
2024-02-13 21:14:18 +01:00
) {
2024-03-02 13:03:09 +01:00
$this->output = fopen($output, 'w');
2024-02-13 21:14:18 +01:00
$this->TL = new TL();
$this->TL->init($settings);
2024-03-10 15:48:55 +01:00
$this->class = basename($output, '.php');
$callbacks = [];
$callbacks []= (new ReflectionClass(MTProto::class))->newInstanceWithoutConstructor();
$callbacks []= (new ReflectionClass(ReferenceDatabase::class))->newInstanceWithoutConstructor();
$callbacks []= (new ReflectionClass(MinDatabase::class))->newInstanceWithoutConstructor();
$callbacks []= (new ReflectionClass(PeerDatabase::class))->newInstanceWithoutConstructor();
$this->TL->updateCallbacks($callbacks);
2024-02-13 21:14:18 +01:00
$byType = [];
$idByPredicate = ['vector' => var_export(hex2bin('1cb5c415'), true)];
2024-03-08 15:10:22 +01:00
$constructorByPredicate = [];
2024-02-13 21:14:18 +01:00
foreach ($this->TL->getConstructors()->by_id as $id => $constructor) {
2024-03-08 15:10:22 +01:00
foreach ($constructor['params'] as &$param) {
if ($constructor['predicate'] === 'photoStrippedSize'
&& $param['name'] === 'bytes'
) {
$param['name'] = 'inflated';
$param['type'] = 'inflated';
}
if ($param['name'] === 'random_bytes') {
$param['type'] = 'random_bytes';
}
2024-03-10 15:03:28 +01:00
if (isset($param['subtype'])) {
$param['type'] = match ($param['type']) {
'Vector t' => "Vector<{$param['subtype']}>",
'vector' => "vector<{$param['subtype']}>",
};
}
2024-03-08 15:10:22 +01:00
}
if (isset($constructor['layer'])) {
2024-03-10 15:48:55 +01:00
$constructor['predicate'] .= '_'.$constructor['layer'];
if (!$this instanceof SecretBuilder) {
continue;
}
2024-03-08 15:10:22 +01:00
}
$constructor['id'] = $id;
$constructorByPredicate[$constructor['predicate']] = $constructor;
2024-02-13 21:14:18 +01:00
$idByPredicate[$constructor['predicate']] = var_export($id, true);
2024-02-27 20:46:50 +01:00
$typeByPredicate[$constructor['predicate']] = $constructor['type'];
2024-03-08 15:10:22 +01:00
$byType[$constructor['type']][$id]= $constructor;
2024-02-13 21:14:18 +01:00
}
$this->idByPredicate = $idByPredicate;
2024-02-27 20:46:50 +01:00
$this->typeByPredicate = $typeByPredicate;
2024-03-08 15:10:22 +01:00
$this->constructorByPredicate = $constructorByPredicate;
2024-03-10 15:03:28 +01:00
$methodConstructors = [];
$methodVectorTypes = [];
foreach ($this->TL->getMethods()->by_id as $c) {
['type' => $type, 'method' => $name, 'encrypted' => $encrypted] = $c;
if (!$encrypted) {
continue;
}
// TODO
if ($type === 'X') {
continue;
}
if (isset($c['subtype'])) {
$methodVectorTypes[$name] = match ($c['type']) {
'Vector t' => "Vector<{$c['subtype']}>",
'vector' => "vector<{$c['subtype']}>",
};
continue;
}
$methodConstructors = array_merge($methodConstructors, $byType[$type]);
}
$byType['MethodResult'] = $methodConstructors;
$this->methodVectorTypes = $methodVectorTypes;
$this->byType = $byType;
2024-02-13 21:14:18 +01:00
}
2024-03-08 13:19:15 +01:00
2024-03-10 15:48:55 +01:00
protected static function escapeConstructorName(array $constructor): string
2024-02-13 21:14:18 +01:00
{
2024-03-10 15:03:28 +01:00
return str_replace(['.', ' '], '___', $constructor['predicate']);
2024-02-13 21:14:18 +01:00
}
2024-03-10 15:48:55 +01:00
protected static function escapeTypeName(string $name): string
2024-02-13 21:14:18 +01:00
{
return str_replace(['.', ' '], '___', $name);
}
2024-03-10 15:48:55 +01:00
protected function needFullConstructor(string $predicate): bool
2024-03-08 13:24:05 +01:00
{
2024-03-08 13:19:15 +01:00
if (isset($this->TL->beforeConstructorDeserialization[$predicate])
2024-03-08 13:24:05 +01:00
|| isset($this->TL->afterConstructorDeserialization[$predicate])) {
2024-03-08 13:19:15 +01:00
return true;
}
2024-03-08 15:10:22 +01:00
return false;
2024-03-08 13:19:15 +01:00
}
2024-03-10 15:48:55 +01:00
protected static function methodFromClosure(\Closure $closure): string
2024-03-08 13:24:05 +01:00
{
2024-03-08 13:19:15 +01:00
$refl = new ReflectionFunction($closure);
return match ($refl->getClosureThis()::class) {
PeerDatabase::class => '$this->peerDatabase',
MinDatabase::class => '$this->minDatabase?',
ReferenceDatabase::class => '$this->referenceDatabase?',
MTProto::class => '$this->API',
}."->".$refl->getName();
}
2024-03-10 15:48:55 +01:00
protected function buildTypes(array $constructors, string $type): string
2024-03-02 19:32:18 +01:00
{
2024-03-08 15:10:22 +01:00
$typeMethod = "_type_".self::escapeTypeName($type);
2024-03-08 13:19:15 +01:00
$result = "match (stream_get_contents(\$stream, 4)) {\n";
2024-03-10 15:48:55 +01:00
foreach ($constructors as ['predicate' => $predicate]) {
2024-03-08 13:19:15 +01:00
if ($predicate === 'gzip_packed') {
continue;
}
if ($predicate === 'jsonObjectValue') {
2024-03-08 15:10:22 +01:00
throw new AssertionError("Impossible!");
2024-03-08 13:19:15 +01:00
}
2024-03-10 15:48:55 +01:00
$result .= $this->idByPredicate[$predicate]." => ";
2024-03-08 13:19:15 +01:00
$result .= $this->buildConstructor($predicate);
$result .= ",\n";
}
2024-03-10 15:03:28 +01:00
if ($type === 'MethodResult') {
$result .= $this->idByPredicate['gzip_packed']." => ".$this->methodCall(
"deserialize$typeMethod",
'self::gzdecode($stream), $method'
).",\n";
$result .= $this->idByPredicate['vector']." => match (\$method) {\n";
foreach ($this->methodVectorTypes as $method => $type) {
$result .= var_export($method, true) . ' => '.$this->buildType($type).",\n";
}
$result .= "},\n";
} else {
$result .= $this->idByPredicate['gzip_packed']." => ".$this->methodCall(
"deserialize$typeMethod",
'self::gzdecode($stream)'
).",\n";
}
2024-03-08 13:19:15 +01:00
$result .= "default => self::err(\$stream)\n";
return $result."}\n";
2024-03-02 13:03:09 +01:00
}
2024-03-10 15:48:55 +01:00
protected array $createdConstructors = [];
2024-03-08 13:24:05 +01:00
public function buildConstructor(string $predicate): string
{
2024-03-08 15:10:22 +01:00
$constructor = $this->constructorByPredicate[$predicate];
2024-03-08 13:19:15 +01:00
Assert::notFalse($constructor, "Missing constructor $predicate");
[
'flags' => $flags,
'params' => $params,
] = $constructor;
2024-03-10 15:03:28 +01:00
if ($predicate === 'rpc_result') {
$result = "\$tmp = ['_' => '$predicate', 'req_msg_id' => \$id = {$this->buildType('long')}];\n";
$result .= '$message = $this->connection->outgoing_messages[$id];
$method = $message->constructor;
if (isset($this->beforeMethodResponseDeserialization[$method])) {
foreach ($this->beforeMethodResponseDeserialization[$method] as $callback) {
$callback($method);
}
}
$tmp["result"] = '.$this->buildType('MethodResult').';
if (isset($this->afterMethodResponseDeserialization[$method])) {
foreach ($this->afterMethodResponseDeserialization[$method] as $callback) {
$callback($tmp);
}
}
';
} elseif ($flags) {
2024-03-08 15:10:22 +01:00
$result = $this->buildConstructorFull($predicate, $params, $flags);
} else {
$result = $this->buildConstructorShort($predicate, $params, $flags);
if (!$this->needFullConstructor($predicate)) {
return $result;
}
2024-03-10 15:48:55 +01:00
$result = "\$tmp = $result;\n";
2024-03-08 15:10:22 +01:00
}
$pre = '';
2024-03-10 15:03:28 +01:00
foreach ($this->TL->beforeConstructorDeserialization[$predicate] ?? [] as $closure) {
2024-03-08 15:10:22 +01:00
$pre .= self::methodFromClosure($closure)."('$predicate');\n";
}
$result = $pre.$result;
2024-03-10 15:03:28 +01:00
foreach ($this->TL->afterConstructorDeserialization[$predicate] ?? [] as $closure) {
2024-03-08 15:10:22 +01:00
$result .= self::methodFromClosure($closure)."(\$tmp);\n";
2024-03-08 13:24:05 +01:00
}
2024-03-10 15:48:55 +01:00
$result .= "return \$tmp;\n";
2024-03-08 13:19:15 +01:00
$nameEscaped = self::escapeConstructorName($constructor);
if (!isset($this->createdConstructors[$predicate])) {
$this->createdConstructors[$predicate] = true;
2024-03-08 15:10:22 +01:00
$this->m("deserialize_$nameEscaped", $result);
2024-03-08 13:19:15 +01:00
}
2024-03-08 13:24:05 +01:00
2024-03-08 13:19:15 +01:00
return $this->methodCall("deserialize_$nameEscaped");
}
2024-03-10 15:48:55 +01:00
protected function buildConstructorFull(string $predicate, array $params, array $flags): string
2024-02-13 21:14:18 +01:00
{
2024-03-08 15:10:22 +01:00
$result = "\$tmp = ['_' => '$predicate'];\n";
2024-03-08 13:19:15 +01:00
$flagNames = [];
foreach ($flags as ['flag' => $flag]) {
$flagNames[$flag] = true;
}
foreach ($params as $param) {
$name = $param['name'];
if (!isset($param['pow'])) {
2024-03-10 15:03:28 +01:00
$code = $this->buildType($param['type']);
2024-03-08 13:19:15 +01:00
if (isset($flagNames[$name])) {
$result .= "\$$name = $code;\n";
} else {
$result .= "\$tmp['$name'] = $code;\n";
}
continue;
}
$flag = "(\${$param['flag']} & {$param['pow']}) !== 0";
if ($param['type'] === 'true') {
$result .= "\$tmp['$name'] = $flag;\n";
continue;
}
2024-03-10 15:03:28 +01:00
$code = $this->buildType($param['type']);
2024-03-08 13:19:15 +01:00
$result .= "if ($flag) \$tmp['$name'] = $code;\n";
}
2024-03-10 15:48:55 +01:00
return $result;
2024-02-13 21:14:18 +01:00
}
2024-03-08 13:19:15 +01:00
2024-03-10 15:48:55 +01:00
protected function buildConstructorShort(string $predicate, array $params = []): string
2024-02-13 21:14:18 +01:00
{
2024-02-27 20:25:09 +01:00
if ($predicate === 'dataJSON') {
2024-03-10 15:03:28 +01:00
return 'json_decode('.$this->buildType('string').', true, 512, \\JSON_THROW_ON_ERROR)';
2024-02-27 20:25:09 +01:00
}
2024-02-27 20:46:50 +01:00
if ($predicate === 'jsonNull') {
return 'null';
}
2024-02-27 21:01:28 +01:00
$superBare = $this->typeByPredicate[$predicate] === 'JSONValue'
|| $this->typeByPredicate[$predicate] === 'Peer';
2024-03-02 19:32:18 +01:00
2024-03-10 15:48:55 +01:00
if ($superBare) {
$result = $this->buildType(end($params)['type']);
} else {
$result = "[\n";
2024-02-27 20:46:50 +01:00
$result .= "'_' => '$predicate',\n";
2024-03-10 15:48:55 +01:00
foreach ($params as $param) {
$result .= var_export($param['name'], true).' => ';
$result .= $this->buildType($param['type']).",\n";
2024-02-27 20:46:50 +01:00
}
$result .= ']';
2024-02-13 21:14:18 +01:00
}
2024-03-10 15:48:55 +01:00
2024-02-27 21:01:28 +01:00
if ($predicate === 'peerChat') {
$result = "-$result";
} elseif ($predicate === 'peerChannel') {
$result = "-1000000000000 - $result";
}
2024-02-13 21:14:18 +01:00
return $result;
}
2024-03-08 13:19:15 +01:00
2024-03-10 15:48:55 +01:00
protected array $createdVectors = [];
protected function buildVector(string $type, bool $bare, ?string $payload = null): string
2024-02-13 21:14:18 +01:00
{
2024-03-08 13:19:15 +01:00
if (!isset($this->createdVectors[$type])) {
$this->createdVectors[$type] = true;
if ($type === 'JSONObjectValue') {
2024-03-10 15:03:28 +01:00
$payload = '$result['.$this->buildType('string').'] = '.$this->buildType('JSONValue');
2024-03-08 13:19:15 +01:00
} elseif (isset($this->byType[$type])) {
$payload = '$result []= '.$this->buildTypes($this->byType[$type], $type);
} elseif ($payload === null) {
if ($type === '%MTMessage') {
$type = 'MTmessage';
}
$payload = '$result []= '.$this->buildConstructor($type);
2024-02-13 21:14:18 +01:00
}
2024-03-08 13:19:15 +01:00
$this->m("deserialize_type_array_of_{$this->escapeTypeName($type)}", '
$result = [];
for ($x = unpack("V", stream_get_contents($stream, 4))[1]; $x > 0; --$x) {
'.$payload.';
}
return $result;
', 'array', static: $type === 'JSONValue');
2024-02-13 21:14:18 +01:00
}
2024-03-10 15:03:28 +01:00
return $this->methodCall(
"deserialize_type_array_of_{$this->escapeTypeName($type)}",
$bare ? '$stream' : 'match(stream_get_contents($stream, 4)) {
'.$this->idByPredicate['vector'].' => $stream,
'.$this->idByPredicate['gzip_packed'].' => self::gzdecode_vector($stream)
}'
);
2024-02-13 21:14:18 +01:00
}
2024-03-08 13:24:05 +01:00
2024-03-10 15:48:55 +01:00
protected array $createdTypes = ['Object' => true];
protected array $typeStack = [];
protected function buildType(string $type): string
2024-03-08 13:24:05 +01:00
{
2024-03-10 15:03:28 +01:00
if (str_starts_with($type, 'Vector<')) {
return $this->buildVector(str_replace(['Vector<', '>'], '', $type), false);
}
if (str_starts_with($type, 'vector<')) {
return $this->buildVector(str_replace(['vector<', '>'], '', $type), true);
}
$tmp = match ($type) {
'#' => "unpack('V', stream_get_contents(\$stream, 4))[1]",
'int' => "unpack('l', stream_get_contents(\$stream, 4))[1]",
'long' => "unpack('q', stream_get_contents(\$stream, 8))[1]",
'double' => "unpack('d', stream_get_contents(\$stream, 8))[1]",
'Bool' => 'match (stream_get_contents($stream, 4)) {'.
$this->idByPredicate['boolTrue'].' => true,'.
$this->idByPredicate['boolFalse'].' => false, default => '.$this->methodCall('err').' }',
'strlong' => 'stream_get_contents($stream, 8)',
'int128' => 'stream_get_contents($stream, 16)',
'int256' => 'stream_get_contents($stream, 32)',
'int512' => 'stream_get_contents($stream, 64)',
'string', 'bytes', 'waveform', 'random_bytes' =>
$this->methodCall("deserialize_$type"),
'inflated' =>
'new Types\Bytes(Tools::inflateStripped('.$this->methodCall("deserialize_string").'))',
default => null
};
if ($tmp !== null) {
return $tmp;
}
2024-03-08 13:19:15 +01:00
if (!isset($this->createdTypes[$type])) {
$this->createdTypes[$type] = true;
2024-03-10 15:03:28 +01:00
2024-03-08 13:19:15 +01:00
$this->m(
"deserialize_type_{$this->escapeTypeName($type)}",
"return {$this->buildTypes($this->byType[$type], $type)};",
static: $type === 'JSONValue'
);
2024-03-08 15:10:22 +01:00
}
2024-03-10 15:48:55 +01:00
$had = array_search($type, $this->typeStack, true) !== false;
2024-03-08 15:10:22 +01:00
$this->typeStack []= $type;
try {
2024-03-10 15:03:28 +01:00
if (!$had) {
2024-03-08 15:10:22 +01:00
return $this->buildTypes($this->byType[$type], $type);
}
2024-03-08 13:19:15 +01:00
return $this->methodCall("deserialize_type_{$this->escapeTypeName($type)}");
2024-03-08 15:10:22 +01:00
} finally {
array_pop($this->typeStack);
2024-03-08 13:19:15 +01:00
}
2024-03-02 13:03:09 +01:00
}
2024-03-08 13:19:15 +01:00
2024-03-10 15:48:55 +01:00
protected array $methodsCreated = [];
protected function methodCall(string $method, string $stream = '$stream'): string
2024-03-02 19:32:18 +01:00
{
2024-03-08 13:19:15 +01:00
return ($this->methodsCreated[$method] ?? true)
2024-03-08 15:10:22 +01:00
? "\$this->$method($stream)"
: "self::$method($stream)";
2024-03-02 13:03:09 +01:00
}
2024-03-08 13:19:15 +01:00
2024-03-10 15:03:28 +01:00
public function m(string $methodName, string $body, string $returnType = 'mixed', bool $public = false, bool $static = false, string $extraArg = ''): void
2024-03-02 19:32:18 +01:00
{
2024-03-08 13:19:15 +01:00
if (isset($this->methodsCreated[$methodName])) {
throw new AssertionError("Already created $methodName!");
}
2024-03-02 13:03:09 +01:00
$this->methodsCreated[$methodName] = $static;
$public = $public ? 'public' : 'private';
$static = $static ? 'static' : '';
2024-03-10 15:03:28 +01:00
$this->w(" $public $static function $methodName(mixed \$stream$extraArg): $returnType {\n{$body}\n }\n");
2024-02-13 21:14:18 +01:00
}
2024-03-10 15:48:55 +01:00
protected function w(string $data): void
2024-03-08 13:19:15 +01:00
{
fwrite($this->output, $data);
}
2024-02-13 21:14:18 +01:00
public function build(): void
{
2024-03-10 16:07:35 +01:00
$this->w("<?php namespace {$this->namespace};\n/** @internal Autogenerated using tools/TL/Builder.php */\n");
foreach ([
MTProto::class,
Connection::class,
PeerDatabase::class,
ReferenceDatabase::class,
MinDatabase::class,
SecurityException::class,
AssertionError::class,
2024-03-10 19:43:10 +01:00
Lang::class,
2024-03-10 16:07:35 +01:00
] as $clazz) {
$this->w("use {$clazz};\n");
}
$this->w("final class {$this->class} {\n");
$this->w('public function __construct(
private readonly MTProto $API,
private readonly Connection $connection,
private readonly PeerDatabase $peerDatabase,
private readonly ?ReferenceDatabase $referenceDatabase,
private readonly ?MinDatabase $minDatabase,
) {}
');
2024-02-13 21:14:18 +01:00
2024-03-02 13:03:09 +01:00
$this->m('err', '
2024-02-13 21:14:18 +01:00
fseek($stream, -4, SEEK_CUR);
throw new AssertionError("Unexpected ID ".bin2hex(fread($stream, 4)));
2024-03-02 13:03:09 +01:00
', 'never');
2024-02-13 21:14:18 +01:00
2024-03-02 13:03:09 +01:00
$this->m("gzdecode", "
2024-02-13 21:14:18 +01:00
\$res = fopen('php://memory', 'rw+b');
2024-03-02 13:03:09 +01:00
fwrite(\$res, gzdecode(self::deserialize_string(\$stream)));
2024-02-13 21:14:18 +01:00
rewind(\$res);
return \$res;
2024-03-02 13:03:09 +01:00
");
2024-02-13 21:14:18 +01:00
2024-03-02 13:03:09 +01:00
$this->m('gzdecode_vector', "
2024-02-13 21:14:18 +01:00
\$res = fopen('php://memory', 'rw+b');
2024-03-02 13:03:09 +01:00
fwrite(\$res, gzdecode(self::deserialize_string(\$stream)));
2024-02-13 21:14:18 +01:00
rewind(\$res);
return match (stream_get_contents(\$stream, 4)) {
{$this->idByPredicate['vector']} => \$stream,
default => self::err(\$stream)
};
2024-03-02 13:03:09 +01:00
");
2024-02-13 21:14:18 +01:00
$block_str = '
$l = \ord(stream_get_contents($stream, 1));
if ($l > 254) {
throw new Exception(Lang::$current_lang["length_too_big"]);
}
if ($l === 254) {
2024-02-27 20:25:09 +01:00
$l = unpack("V", stream_get_contents($stream, 3).\chr(0))[1];
$x = stream_get_contents($stream, $l);
$resto = (-$l) % 4;
2024-02-27 10:34:57 +01:00
$resto = $resto < 0 ? $resto + 4 : $resto;
2024-02-13 21:14:18 +01:00
if ($resto > 0) {
stream_get_contents($stream, $resto);
}
} else {
$x = $l ? stream_get_contents($stream, $l) : "";
2024-02-27 10:34:57 +01:00
$resto = (-$l+1) % 4;
$resto = $resto < 0 ? $resto + 4 : $resto;
2024-02-13 21:14:18 +01:00
if ($resto > 0) {
stream_get_contents($stream, $resto);
}
}'."\n";
2024-03-02 13:03:09 +01:00
$this->m("deserialize_bytes", "
2024-02-13 21:14:18 +01:00
$block_str
return new Types\Bytes(\$x);
2024-03-02 13:03:09 +01:00
");
$this->m("deserialize_string", "
2024-02-13 21:14:18 +01:00
$block_str
return \$x;
2024-03-02 13:03:09 +01:00
");
$this->m("deserialize_waveform", "
2024-02-13 21:14:18 +01:00
$block_str
return TL::extractWaveform(\$x);
2024-03-02 13:03:09 +01:00
");
$this->m('deserialize_random_bytes', '
2024-02-27 20:25:09 +01:00
$l = \ord(stream_get_contents($stream, 1));
if ($l > 254) {
throw new Exception(Lang::$current_lang["length_too_big"]);
}
if ($l === 254) {
$l = unpack("V", stream_get_contents($stream, 3).\chr(0))[1];
if ($l < 15) {
throw new SecurityException("Random_bytes is too small!");
}
} else {
if ($l < 15) {
throw new SecurityException("Random_bytes is too small!");
}
$l += 1;
}
$resto = (-$l) % 4;
$resto = $resto < 0 ? $resto + 4 : $resto;
if ($resto > 0) {
$l += $resto;
}
stream_get_contents($stream, $l);
2024-03-02 13:03:09 +01:00
', 'void');
2024-03-08 13:19:15 +01:00
foreach (['int', 'long', 'double', 'strlong', 'string', 'bytes'] as $type) {
2024-03-10 15:03:28 +01:00
$this->buildVector($type, false, '$result []= '.$this->buildType($type));
2024-03-08 15:10:22 +01:00
}
2024-03-10 15:48:55 +01:00
$this->buildMain();
$this->w("}\n");
}
protected function buildMain(): void
{
2024-02-27 21:21:50 +01:00
$initial_constructors = array_filter(
2024-03-08 15:10:22 +01:00
$this->constructorByPredicate,
2024-03-08 13:24:05 +01:00
static fn (array $arr) => (
$arr['type'] === 'Update'
2024-02-27 21:21:50 +01:00
|| $arr['predicate'] === 'rpc_result'
|| !$arr['encrypted']
2024-03-08 13:19:15 +01:00
) && (
2024-03-08 14:10:46 +01:00
$arr['predicate'] !== 'rpc_error'
2024-03-08 15:10:22 +01:00
&& $arr['predicate'] !== 'MTmessage'
2024-03-08 13:19:15 +01:00
)
2024-02-27 21:21:50 +01:00
);
2024-03-08 15:10:22 +01:00
$this->m("deserialize_type_Object", "return {$this->buildTypes($initial_constructors, 'Object')};", 'mixed', true, static: false);
2024-02-13 21:14:18 +01:00
}
}