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

View File

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

View File

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

View File

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

View File

@ -19,26 +19,28 @@ class TLConstructor
$this->id = (int) $json_dict['id'];
$this->predicate = $json_dict['predicate'];
$this->type = $json_dict['type'];
$this->params = [];
foreach ($json_dict['params'] as $param) {
switch ($param['type']) {
case 'Vector<long>':
$param['type'] = 'Vector t';
$param['subtype'] = 'long';
break;
case 'vector<%Message>':
$param['type'] = 'vector';
$param['subtype'] = 'message';
break;
case 'vector<future_salt>':
$param['type'] = 'vector';
$param['subtype'] = 'future_salt';
break;
default:
$param['subtype'] = null;
break;
$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']));
}
}
$this->params[] = $param;
}
}
}

View File

@ -20,5 +20,27 @@ class TLMethod
$this->type = $json_dict['type'];
$this->method = $json_dict['method'];
$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']));
}
}
}
}
}