TL now uses an istantiated version of PHPStruct, TLConstructor and TLMethod now decode vectors and optional params

This commit is contained in:
Daniil Gentili 2016-10-18 13:42:14 +00:00
parent 737c4e286b
commit 6c94e2426a
7 changed files with 91 additions and 61 deletions

23
composer.lock generated
View File

@ -128,22 +128,23 @@
}, },
{ {
"name": "paragonie/constant_time_encoding", "name": "paragonie/constant_time_encoding",
"version": "v2.0.3", "version": "v1.0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/paragonie/constant_time_encoding.git", "url": "https://github.com/paragonie/constant_time_encoding.git",
"reference": "e085e08c939de49707dbf64315d178d90fbc708d" "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/e085e08c939de49707dbf64315d178d90fbc708d", "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/d96e63b79a7135a65659ba5b1cb02826172bfedd",
"reference": "e085e08c939de49707dbf64315d178d90fbc708d", "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": "^7" "php": "^5.3|^7"
}, },
"require-dev": { "require-dev": {
"paragonie/random_compat": "^1.4|^2.0",
"phpunit/phpunit": "4.*|5.*" "phpunit/phpunit": "4.*|5.*"
}, },
"type": "library", "type": "library",
@ -185,20 +186,20 @@
"hex2bin", "hex2bin",
"rfc4648" "rfc4648"
], ],
"time": "2016-07-11 20:32:06" "time": "2016-06-13 01:00:24"
}, },
{ {
"name": "paragonie/random_compat", "name": "paragonie/random_compat",
"version": "v2.0.2", "version": "v2.0.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/paragonie/random_compat.git", "url": "https://github.com/paragonie/random_compat.git",
"reference": "088c04e2f261c33bed6ca5245491cfca69195ccf" "reference": "c0125896dbb151380ab47e96c621741e79623beb"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/paragonie/random_compat/zipball/088c04e2f261c33bed6ca5245491cfca69195ccf", "url": "https://api.github.com/repos/paragonie/random_compat/zipball/c0125896dbb151380ab47e96c621741e79623beb",
"reference": "088c04e2f261c33bed6ca5245491cfca69195ccf", "reference": "c0125896dbb151380ab47e96c621741e79623beb",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -233,7 +234,7 @@
"pseudorandom", "pseudorandom",
"random" "random"
], ],
"time": "2016-04-03 06:00:07" "time": "2016-10-17 15:23:22"
}, },
{ {
"name": "phpseclib/phpseclib", "name": "phpseclib/phpseclib",

View File

@ -106,8 +106,11 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
} }
} }
$this->settings = $settings; $this->settings = $settings;
// Istantiate logging class
$this->log = new Logging($this->settings['logging']['logging'], $this->settings['logging']['logging_param']);
// Connect to servers // Connect to servers
$this->log->log('Connecting to server...');
$this->connection = new DataCenter($this->settings['connection'], $this->settings['connection_settings']); $this->connection = new DataCenter($this->settings['connection'], $this->settings['connection_settings']);
$this->connection->dc_connect(2); $this->connection->dc_connect(2);
@ -116,9 +119,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
// Istantiate struct class // Istantiate struct class
$this->struct = new \danog\PHP\StructTools(); $this->struct = new \danog\PHP\StructTools();
// Istantiate TL class // Istantiate TL class
$this->log->log('Translating tl schemas...');
$this->tl = new TL\TL($this->settings['tl_schema']['src']); $this->tl = new TL\TL($this->settings['tl_schema']['src']);
// Istantiate logging class
$this->log = new Logging($this->settings['logging']['logging'], $this->settings['logging']['logging_param']);
$this->seq_no = 0; $this->seq_no = 0;
$this->timedelta = 0; // time delta $this->timedelta = 0; // time delta

View File

@ -75,8 +75,7 @@ class PrimeModule extends Tools
return ($b == 0) ? $a : $b; return ($b == 0) ? $a : $b;
} }
public function PrimeFactors($pq)
public function PrimeFactors($pq, $sort = false)
{ {
$pqstr = (string) $pq; $pqstr = (string) $pq;

View File

@ -26,7 +26,7 @@ class RSA extends TL\TL
$this->key->loadKey($key); $this->key->loadKey($key);
$this->n = $this->key->modulus; $this->n = $this->key->modulus;
$this->e = $this->key->exponent; $this->e = $this->key->exponent;
$this->struct = new \danog\PHP\StructTools();
$this->fp_bytes = substr(sha1($this->serialize_param('bytes', null, $this->n->toBytes()).$this->serialize_param('bytes', null, $this->e->toBytes()), true), -8); $this->fp_bytes = substr(sha1($this->serialize_param('bytes', null, $this->n->toBytes()).$this->serialize_param('bytes', null, $this->e->toBytes()), true), -8);
$this->fp = new \phpseclib\Math\BigInteger(strrev($this->fp_bytes), -256); $this->fp = new \phpseclib\Math\BigInteger(strrev($this->fp_bytes), -256);
} }

View File

@ -12,10 +12,11 @@ If not, see <http://www.gnu.org/licenses/>.
namespace danog\MadelineProto\TL; namespace danog\MadelineProto\TL;
class TL class TL extends \danog\MadelineProto\Tools
{ {
public function __construct($filename) public function __construct($filename)
{ {
$this->struct = new \danog\PHP\StructTools();
if (is_array($filename)) { if (is_array($filename)) {
$TL_dict = ['constructors' => [], 'methods' => []]; $TL_dict = ['constructors' => [], 'methods' => []];
foreach ($filename as $file) { foreach ($filename as $file) {
@ -51,7 +52,7 @@ class TL
} else { } else {
throw new Exception('Could not extract type: '.$type_); throw new Exception('Could not extract type: '.$type_);
} }
$bytes_io .= \danog\PHP\Struct::pack('<i', $tl_constructor->id); $bytes_io .= $this->struct->pack('<i', $tl_constructor->id);
foreach ($tl_constructor->params as $arg) { foreach ($tl_constructor->params as $arg) {
$bytes_io .= $this->serialize_param($arg['type'], $arg['subtype'], $kwargs[$arg['name']]); $bytes_io .= $this->serialize_param($arg['type'], $arg['subtype'], $kwargs[$arg['name']]);
} }
@ -87,10 +88,13 @@ class TL
} else { } else {
throw new Exception('Could not extract type: '.$type_); throw new Exception('Could not extract type: '.$type_);
} }
$bytes_io .= \danog\PHP\Struct::pack('<i', $tl_method->id); $bytes_io .= $this->struct->pack('<i', $tl_method->id);
foreach ($tl_method->params as $arg) { foreach ($tl_method->params as $arg) {
if (!isset($arg['subtype'])) { if (!isset($kwargs[$arg['name']])) {
$arg['subtype'] = null; if ($arg['opt']) {
continue;
}
throw new Exception('Missing required parameter ('.$arg['name'].')');
} }
$bytes_io .= $this->serialize_param($arg['type'], $arg['subtype'], $kwargs[$arg['name']]); $bytes_io .= $this->serialize_param($arg['type'], $arg['subtype'], $kwargs[$arg['name']]);
} }
@ -109,7 +113,7 @@ class TL
throw new Exception('Given value is too long.'); throw new Exception('Given value is too long.');
} }
return \danog\PHP\Struct::pack('<i', $value); return $this->struct->pack('<i', $value);
break; break;
case '#': case '#':
if (!is_numeric($value)) { if (!is_numeric($value)) {
@ -119,14 +123,14 @@ class TL
throw new Exception('Given value is too long.'); throw new Exception('Given value is too long.');
} }
return \danog\PHP\Struct::pack('<I', $value); return $this->struct->pack('<I', $value);
break; break;
case 'long': case 'long':
if (!is_numeric($value)) { if (!is_numeric($value)) {
throw new Exception("serialize_param: given value isn't numeric"); throw new Exception("serialize_param: given value isn't numeric");
} }
return \danog\PHP\Struct::pack('<q', $value); return $this->struct->pack('<q', $value);
break; break;
case 'int128': case 'int128':
case 'int256': case 'int256':
@ -137,21 +141,21 @@ class TL
return $value; return $value;
break; break;
case 'double': case 'double':
return \danog\PHP\Struct::pack('<d', $value); return $this->struct->pack('<d', $value);
break; break;
case 'string': case 'string':
case 'bytes': case 'bytes':
$l = strlen($value); $l = strlen($value);
$concat = ''; $concat = '';
if ($l <= 253) { if ($l <= 253) {
$concat .= \danog\PHP\Struct::pack('<b', $l); $concat .= $this->struct->pack('<b', $l);
$concat .= $value; $concat .= $value;
$concat .= pack('@'.\danog\MadelineProto\Tools::posmod((-$l - 1), 4)); $concat .= pack('@'.$this->posmod((-$l - 1), 4));
} else { } else {
$concat .= \danog\MadelineProto\Tools::string2bin('\xfe'); $concat .= $this->string2bin('\xfe');
$concat .= substr(\danog\PHP\Struct::pack('<i', $l), 0, 3); $concat .= substr($this->struct->pack('<i', $l), 0, 3);
$concat .= $value; $concat .= $value;
$concat .= pack('@'.\danog\MadelineProto\Tools::posmod(-$l, 4)); $concat .= pack('@'.$this->posmod(-$l, 4));
} }
return $concat; return $concat;
@ -159,7 +163,7 @@ class TL
case '!X': case '!X':
return $value; return $value;
case 'Vector t': case 'Vector t':
$concat = \danog\PHP\Struct::pack('<i', $this->constructor_type['vector']->id); $concat = $this->struct->pack('<i', $this->constructor_type['vector']->id);
foreach ($value as $curv) { foreach ($value as $curv) {
$concat .= $this->serialize_param($subtype, null, $curv); $concat .= $this->serialize_param($subtype, null, $curv);
} }
@ -188,16 +192,16 @@ class TL
} }
switch ($type_) { switch ($type_) {
case 'int': case 'int':
$x = \danog\PHP\Struct::unpack('<i', fread($bytes_io, 4)) [0]; $x = $this->struct->unpack('<i', fread($bytes_io, 4)) [0];
break; break;
case '#': case '#':
$x = \danog\PHP\Struct::unpack('<I', fread($bytes_io, 4)) [0]; $x = $this->struct->unpack('<I', fread($bytes_io, 4)) [0];
break; break;
case 'long': case 'long':
$x = \danog\PHP\Struct::unpack('<q', fread($bytes_io, 8)) [0]; $x = $this->struct->unpack('<q', fread($bytes_io, 8)) [0];
break; break;
case 'double': case 'double':
$x = \danog\PHP\Struct::unpack('<d', fread($bytes_io, 8)) [0]; $x = $this->struct->unpack('<d', fread($bytes_io, 8)) [0];
break; break;
case 'int128': case 'int128':
$x = fread($bytes_io, 16); $x = fread($bytes_io, 16);
@ -207,20 +211,20 @@ class TL
break; break;
case 'string': case 'string':
case 'bytes': case 'bytes':
$l = \danog\PHP\Struct::unpack('<B', fread($bytes_io, 1)) [0]; $l = $this->struct->unpack('<B', fread($bytes_io, 1)) [0];
if ($l > 254) { if ($l > 254) {
throw new Exception('Length is too big'); throw new Exception('Length is too big');
} }
if ($l == 254) { if ($l == 254) {
$long_len = \danog\PHP\Struct::unpack('<I', fread($bytes_io, 3).\danog\MadelineProto\Tools::string2bin('\x00')) [0]; $long_len = $this->struct->unpack('<I', fread($bytes_io, 3).$this->string2bin('\x00')) [0];
$x = fread($bytes_io, $long_len); $x = fread($bytes_io, $long_len);
$resto = \danog\MadelineProto\Tools::posmod(-$long_len, 4); $resto = $this->posmod(-$long_len, 4);
if ($resto > 0) { if ($resto > 0) {
fread($bytes_io, $resto); fread($bytes_io, $resto);
} }
} else { } else {
$x = fread($bytes_io, $l); $x = fread($bytes_io, $l);
$resto = \danog\MadelineProto\Tools::posmod(-($l + 1), 4); $resto = $this->posmod(-($l + 1), 4);
if ($resto > 0) { if ($resto > 0) {
fread($bytes_io, $resto); fread($bytes_io, $resto);
} }
@ -233,9 +237,9 @@ class TL
if ($subtype == null) { if ($subtype == null) {
throw new Exception("deserialize: subtype isn't null"); throw new Exception("deserialize: subtype isn't null");
} }
$count = \danog\PHP\Struct::unpack('<l', fread($bytes_io, 4)) [0]; $count = $this->struct->unpack('<l', fread($bytes_io, 4)) [0];
$x = []; $x = [];
foreach (\danog\MadelineProto\Tools::range($count) as $i) { foreach ($this->range($count) as $i) {
$x[] = $this->deserialize($bytes_io, $subtype); $x[] = $this->deserialize($bytes_io, $subtype);
} }
break; break;
@ -244,7 +248,7 @@ class TL
$tl_elem = $this->constructor_type[$type_]; $tl_elem = $this->constructor_type[$type_];
} else { } else {
$Idata = fread($bytes_io, 4); $Idata = fread($bytes_io, 4);
$i = \danog\PHP\Struct::unpack('<i', $Idata) [0]; $i = $this->struct->unpack('<i', $Idata) [0];
if (isset($this->constructor_id[$i])) { if (isset($this->constructor_id[$i])) {
$tl_elem = $this->constructor_id[$i]; $tl_elem = $this->constructor_id[$i];
} else { } else {

View File

@ -19,26 +19,28 @@ class TLConstructor
$this->id = (int) $json_dict['id']; $this->id = (int) $json_dict['id'];
$this->predicate = $json_dict['predicate']; $this->predicate = $json_dict['predicate'];
$this->type = $json_dict['type']; $this->type = $json_dict['type'];
$this->params = []; $this->params = $json_dict['params'];
foreach ($json_dict['params'] as $param) { foreach ($this->params as &$param) {
switch ($param['type']) { $param['opt'] = false;
case 'Vector<long>': $param['subtype'] = '';
$param['type'] = 'Vector t'; if (preg_match('/^flags\.\d\?/', $param['type'])) {
$param['subtype'] = 'long'; $param['opt'] = true;
break; $param['flag'] = preg_replace(['/^flags\./', '/\?.*/'], '', $param['type']);
case 'vector<%Message>': $param['type'] = preg_replace('/^flags\.\d\?/', '', $param['type']);
$param['type'] = 'vector'; }
$param['subtype'] = 'message'; if (preg_match('/vector<.*>/i', $param['type'])) {
break; if (preg_match('/vector/', $param['type'])) {
case 'vector<future_salt>': $param['subtype'] = preg_replace(['/.*</', '/>$/'], '', $param['type']);
$param['type'] = 'vector'; $param['type'] = 'vector';
$param['subtype'] = 'future_salt'; }
break; if (preg_match('/Vector/', $param['type'])) {
default: $param['subtype'] = preg_replace(['/.*</', '/>$/'], '', $param['type']);
$param['subtype'] = null; $param['type'] = 'Vector t';
break; }
if (preg_match('/^\%/', $param['subtype'])) {
$param['subtype'] = lcfirst(preg_replace('/^\%/', '', $param['subtype']));
}
} }
$this->params[] = $param;
} }
} }
} }

View File

@ -20,5 +20,27 @@ class TLMethod
$this->type = $json_dict['type']; $this->type = $json_dict['type'];
$this->method = $json_dict['method']; $this->method = $json_dict['method'];
$this->params = $json_dict['params']; $this->params = $json_dict['params'];
foreach ($this->params as &$param) {
$param['opt'] = false;
$param['subtype'] = '';
if (preg_match('/^flags\.\d\?/', $param['type'])) {
$param['opt'] = true;
$param['flag'] = preg_replace(['/^flags\./', '/\?.*/'], '', $param['type']);
$param['type'] = preg_replace('/^flags\.\d\?/', '', $param['type']);
}
if (preg_match('/vector<.*>/i', $param['type'])) {
if (preg_match('/vector/', $param['type'])) {
$param['subtype'] = preg_replace(['/.*</', '/>$/'], '', $param['type']);
$param['type'] = 'vector';
}
if (preg_match('/Vector/', $param['type'])) {
$param['subtype'] = preg_replace(['/.*</', '/>$/'], '', $param['type']);
$param['type'] = 'Vector t';
}
if (preg_match('/^\%/', $param['subtype'])) {
$param['subtype'] = lcfirst(preg_replace('/^\%/', '', $param['subtype']));
}
}
}
} }
} }