2016-06-23 23:51:08 +02:00
|
|
|
<?php
|
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'libpy2php');
|
|
|
|
require_once ('libpy2php.php');
|
|
|
|
$__author__ = 'agrigoryev';
|
|
|
|
require_once ('os.php');
|
|
|
|
class TlConstructor {
|
|
|
|
function __construct($json_dict) {
|
2016-07-14 14:22:46 +02:00
|
|
|
$this->id = (int)$json_dict['id'];
|
2016-06-23 23:51:08 +02:00
|
|
|
$this->type = $json_dict['type'];
|
|
|
|
$this->predicate = $json_dict['predicate'];
|
|
|
|
$this->params = [];
|
|
|
|
foreach ($json_dict['params'] as $param) {
|
|
|
|
if (($param['type'] == 'Vector<long>')) {
|
|
|
|
$param['type'] = 'Vector t';
|
|
|
|
$param['subtype'] = 'long';
|
|
|
|
} else if (($param['type'] == 'vector<%Message>')) {
|
|
|
|
$param['type'] = 'vector';
|
|
|
|
$param['subtype'] = 'message';
|
|
|
|
} else if (($param['type'] == 'vector<future_salt>')) {
|
|
|
|
$param['type'] = 'vector';
|
|
|
|
$param['subtype'] = 'future_salt';
|
|
|
|
} else {
|
|
|
|
$param['subtype'] = null;
|
|
|
|
}
|
|
|
|
$this->params[] = $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class TlMethod {
|
|
|
|
function __construct($json_dict) {
|
2016-07-14 14:22:46 +02:00
|
|
|
$this->id = (int)$json_dict['id'];
|
2016-06-23 23:51:08 +02:00
|
|
|
$this->type = $json_dict['type'];
|
|
|
|
$this->method = $json_dict['method'];
|
|
|
|
$this->params = $json_dict['params'];
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 17:21:10 +02:00
|
|
|
class TLObject extends ArrayObject {
|
2016-06-23 23:51:08 +02:00
|
|
|
function __construct($tl_elem) {
|
2016-06-28 17:49:51 +02:00
|
|
|
parent::__construct();
|
2016-06-23 23:51:08 +02:00
|
|
|
$this->name = $tl_elem->predicate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class TL {
|
|
|
|
function __construct($filename) {
|
2016-06-26 14:53:16 +02:00
|
|
|
$TL_dict = json_decode(file_get_contents($filename), true);
|
2016-06-23 23:51:08 +02:00
|
|
|
$this->constructors = $TL_dict['constructors'];
|
|
|
|
$this->constructor_id = [];
|
|
|
|
$this->constructor_type = [];
|
|
|
|
foreach ($this->constructors as $elem) {
|
|
|
|
$z = new TlConstructor($elem);
|
|
|
|
$this->constructor_id[$z->id] = $z;
|
|
|
|
$this->constructor_type[$z->predicate] = $z;
|
|
|
|
}
|
|
|
|
$this->methods = $TL_dict['methods'];
|
|
|
|
$this->method_id = [];
|
|
|
|
$this->method_name = [];
|
|
|
|
foreach ($this->methods as $elem) {
|
|
|
|
$z = new TlMethod($elem);
|
|
|
|
$this->method_id[$z->id] = $z;
|
|
|
|
$this->method_name[$z->method] = $z;
|
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
$this->struct = new \danog\PHP\Struct();
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
function serialize_obj($type_, $kwargs) {
|
|
|
|
$bytes_io = fopen("php://memory", "rw+b");
|
|
|
|
if(isset($this->constructor_type[$type_])) {
|
|
|
|
$tl_constructor = $this->constructor_type[$type_];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception(sprintf('Could not extract type: %s', $type_));
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
fwrite($bytes_io, $this->struct->pack('<i', $tl_constructor->id));
|
|
|
|
foreach ($tl_constructor->params as $arg) {
|
|
|
|
$this->serialize_param($bytes_io, $arg['type'], $kwargs[$arg['name']]);
|
|
|
|
}
|
|
|
|
return fread_all($bytes_io);
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
function serialize_method($type_, $kwargs) {
|
|
|
|
$bytes_io = fopen("php://memory", "rw+b");
|
|
|
|
if(isset($this->method_name[$type_])) {
|
|
|
|
$tl_method = $this->method_name[$type_];
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception(sprintf('Could not extract type: %s', $type_));
|
2016-06-26 12:59:15 +02:00
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
fwrite($bytes_io, $this->struct->pack('<i', $tl_method->id));
|
|
|
|
foreach ($tl_method->params as $arg) {
|
|
|
|
$this->serialize_param($bytes_io, $arg['type'], $kwargs[$arg['name']]);
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
return fread_all($bytes_io);
|
|
|
|
}
|
|
|
|
function serialize_param($bytes_io, $type_, $value) {
|
|
|
|
if (($type_ == 'int')) {
|
|
|
|
assert(is_numeric($value));
|
|
|
|
assert(strlen(decbin($value)) <= 32);
|
|
|
|
fwrite($bytes_io, $this->struct->pack('<i', $value));
|
|
|
|
} else if (($type_ == 'long')) {
|
|
|
|
assert(is_numeric($value));
|
|
|
|
fwrite($bytes_io, $this->struct->pack('<q', $value));
|
|
|
|
} else if (in_array($type_, ['int128', 'int256'])) {
|
|
|
|
assert(is_string($value));
|
2016-07-14 14:25:00 +02:00
|
|
|
fwrite($bytes_io, $value);
|
2016-07-14 14:22:46 +02:00
|
|
|
} else if ($type_ == 'string' || $type_ == 'bytes') {
|
|
|
|
$l = len($value);
|
|
|
|
if (($l < 254)) {
|
|
|
|
fwrite($bytes_io, $this->struct->pack('<b', $l));
|
|
|
|
fwrite($bytes_io, $value);
|
|
|
|
fwrite($bytes_io, pack("@".((-$l - 1) % 4)));
|
|
|
|
} else {
|
|
|
|
fwrite($bytes_io, string2bin('\xfe'));
|
|
|
|
fwrite($bytes_io, substr($this->struct->pack('<i', $l), null, 3));
|
|
|
|
fwrite($bytes_io, $value);
|
|
|
|
fwrite($bytes_io, pack("@".(-$l % 4)));
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* :type bytes_io: io.BytesIO object
|
|
|
|
*/
|
|
|
|
function deserialize(&$bytes_io, $type_ = null, $subtype = null) {
|
|
|
|
assert(get_resource_type($bytes_io) == 'file' || get_resource_type($bytes_io) == 'stream');
|
|
|
|
if (($type_ == 'int')) {
|
|
|
|
$x = $this->struct->unpack('<i', fread($bytes_io, 4)) [0];
|
|
|
|
} else if (($type_ == '#')) {
|
|
|
|
$x = $this->struct->unpack('<I', fread($bytes_io, 4)) [0];
|
|
|
|
} else if (($type_ == 'long')) {
|
|
|
|
$x = $this->struct->unpack('<q', fread($bytes_io, 8)) [0];
|
|
|
|
} else if (($type_ == 'double')) {
|
|
|
|
$x = $this->struct->unpack('<d', fread($bytes_io, 8)) [0];
|
|
|
|
} else if (($type_ == 'int128')) {
|
|
|
|
$x = fread($bytes_io, 16);
|
|
|
|
} else if (($type_ == 'int256')) {
|
|
|
|
$x = fread($bytes_io, 32);
|
|
|
|
} else if (($type_ == 'string') || ($type_ == 'bytes')) {
|
|
|
|
$l = $this->struct->unpack('<C', fread($bytes_io, 1)) [0];
|
|
|
|
assert($l <= 254);
|
|
|
|
if (($l == 254)) {
|
|
|
|
$long_len = $this->struct->unpack('<I', fread($bytes_io, 3) . string2bin('\x00')) [0];
|
|
|
|
$x = fread($bytes_io, $long_len);
|
|
|
|
fread($bytes_io, (-$long_len % 4));
|
|
|
|
} else {
|
|
|
|
$x = fread($bytes_io, $l);
|
|
|
|
fread($bytes_io, (($l + 1) % 4));
|
|
|
|
}
|
|
|
|
assert(is_string($x));
|
|
|
|
} else if (($type_ == 'vector')) {
|
|
|
|
assert($subtype != null);
|
|
|
|
$count = $this->struct->unpack('<l', substr($bytes_io, 4)) [0];
|
|
|
|
$x = [];
|
|
|
|
foreach( pyjslib_range($count) as $i ) {
|
|
|
|
$x[] = deserialize($bytes_io, $subtype);
|
|
|
|
}
|
2016-06-23 23:51:08 +02:00
|
|
|
} else {
|
2016-07-14 14:22:46 +02:00
|
|
|
if(isset($this->constructor_type[$type_])) {
|
|
|
|
$tl_elem = $this->constructor_type[$type_];
|
|
|
|
} else {
|
|
|
|
$i = $this->struct->unpack('<i', fread($bytes_io, 4)) [0];
|
|
|
|
if(isset($this->constructor_id[$i])) {
|
|
|
|
$tl_elem = $this->constructor_id[$i];
|
|
|
|
} else {
|
|
|
|
throw new Exception(sprintf('Could not extract type: %s', $type_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$base_boxed_types = ['Vector t', 'Int', 'Long', 'Double', 'String', 'Int128', 'Int256'];
|
|
|
|
if (in_array($tl_elem->type, $base_boxed_types)) {
|
|
|
|
$x = deserialize($bytes_io, $tl_elem->predicate, $subtype);
|
|
|
|
} else {
|
|
|
|
$x = new TLObject($tl_elem);
|
|
|
|
foreach ($tl_elem->params as $arg) {
|
|
|
|
$x[$arg['name']] = deserialize($bytes_io, $arg['type'], $arg['subtype']);
|
|
|
|
}
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 14:22:46 +02:00
|
|
|
return $x;
|
2016-06-23 23:51:08 +02:00
|
|
|
}
|
|
|
|
}
|