Fixing substr

This commit is contained in:
danogentili 2016-07-15 15:01:32 +02:00
parent 9e0ab1f9c5
commit 74ffc15157
4 changed files with 27 additions and 573 deletions

View File

@ -1,546 +0,0 @@
<?php
namespace danog\PHP;
/**
* PHPStruct
* PHP implementation of Python's struct module.
* This library was created to help me develop a [client for the mtproto protocol](https://github.com/danog/MadelineProto).
* The functions and the formats are exactly the ones used in python's struct (https://docs.python.org/3/library/struct.html)
* For now custom byte size may not work properly on certain machines for the i, I, f and d formats.
*
* @author Daniil Gentili <daniil@daniil.it>
* @license MIT license
*/
// Main class
class Struct
{
/**
* Constructor.
*
* Sets modifiers and gets endianness
*/
public function __construct()
{
$this->BIG_ENDIAN = (pack('L', 1) === pack('N', 1));
$this->IS64BIT = (PHP_INT_SIZE === 8);
$this->FORMATS = [
// These formats need to be modified after/before encoding/decoding.
'p' => 'p', // “Pascal string”, meaning a short variable-length string stored in a fixed number of bytes, given by the count. The first byte stored is the length of the string, or 255, whichever is smaller. The bytes of the string follow. If the string passed in to pack() is too long (longer than the count minus 1), only the leading count-1 bytes of the string are stored. If the string is shorter than count-1, it is padded with null bytes so that exactly count bytes in all are used. Note that for unpack(), the 'p' format character consumes count bytes, but that the string returned can never contain more than 255 characters.
// These formats have automatical byte size, this must be fixed.
'i' => 'i', // should be 4 (32 bit)
'I' => 'I', // should be 4 (32 bit)
'f' => 'f', // should be 4 (32 bit)
'd' => 'd', // should be 8 (64 bit)
// These formats should work exactly as in python's struct (same byte size, etc).
'c' => 'a',
'?' => 'c',
'x' => 'x',
'b' => 'c',
'B' => 'C',
'h' => 's',
'H' => 'S',
'l' => 'l',
'L' => 'L',
's' => 'a',
];
$this->NATIVE_FORMATS = array_merge([
// These formats need to be modified after/before encoding/decoding.
'P' => $this->IS64BIT ? 'Q' : 'L', // integer or long integer, depending on the size needed to hold a pointer when it has been cast to an integer type. A NULL pointer will always be returned as the Python integer 0. When packing pointer-sized values, Python integer or long integer objects may be used. For example, the Alpha and Merced processors use 64-bit pointer values, meaning a Python long integer will be used to hold the pointer; other platforms use 32-bit pointers and will use a Python integer.
'n' => $this->IS64BIT ? 'q' : 'l',
'N' => $this->IS64BIT ? 'Q' : 'L',
], $this->FORMATS);
$this->SIZE = [
'p' => 1,
'i' => 4,
'I' => 4,
'f' => 4,
'd' => 8,
'c' => 1,
'?' => 1,
'x' => 1,
'b' => 1,
'B' => 1,
'h' => 2,
'H' => 2,
'l' => 4,
'L' => 4,
's' => 1,
];
$this->NATIVE_SIZE = [
'p' => 1,
'P' => strlen(pack($this->NATIVE_FORMATS['P'], 2323)),
'i' => strlen(pack($this->NATIVE_FORMATS['i'], 1)),
'I' => strlen(pack($this->NATIVE_FORMATS['I'], 1)),
'f' => strlen(pack($this->NATIVE_FORMATS['f'], 2.0)),
'd' => strlen(pack($this->NATIVE_FORMATS['d'], 2.0)),
'c' => strlen(pack($this->NATIVE_FORMATS['c'], 'a')),
'?' => strlen(pack($this->NATIVE_FORMATS['?'], false)),
'x' => strlen(pack($this->NATIVE_FORMATS['x'])),
'b' => strlen(pack($this->NATIVE_FORMATS['b'], 'c')),
'B' => strlen(pack($this->NATIVE_FORMATS['B'], 'c')),
'h' => strlen(pack($this->NATIVE_FORMATS['h'], -700)),
'H' => strlen(pack($this->NATIVE_FORMATS['H'], 700)),
'l' => strlen(pack($this->NATIVE_FORMATS['l'], -70000000)),
'L' => strlen(pack($this->NATIVE_FORMATS['L'], 70000000)),
's' => strlen(pack($this->NATIVE_FORMATS['s'], 'c')),
'n' => strlen(pack($this->NATIVE_FORMATS['n'], 1)),
'N' => strlen(pack($this->NATIVE_FORMATS['N'], 1)),
];
$this->TYPE = [
'p' => 'string',
'i' => 'int',
'I' => 'int',
'f' => 'float',
'd' => 'float',
'c' => 'string',
'?' => 'bool',
'x' => 'unset',
'b' => 'int',
'B' => 'int',
'h' => 'int',
'H' => 'int',
'l' => 'int',
'L' => 'int',
's' => 'string',
];
$this->NATIVE_TYPE = array_merge([
'P' => 'int', // integer or long integer, depending on the size needed to hold a pointer when it has been cast to an integer type. A NULL pointer will always be returned as the Python integer 0. When packing pointer-sized values, Python integer or long integer objects may be used. For example, the Alpha and Merced processors use 64-bit pointer values, meaning a Python long integer will be used to hold the pointer; other platforms use 32-bit pointers and will use a Python integer.
'n' => 'int',
'N' => 'int',
], $this->TYPE);
if ($this->IS64BIT) {
$this->FORMATS['q'] = 'q';
$this->FORMATS['Q'] = 'Q';
$this->NATIVE_FORMATS['q'] = 'q';
$this->NATIVE_FORMATS['Q'] = 'Q';
$this->SIZE['q'] = 8;
$this->SIZE['Q'] = 8;
$this->NATIVE_SIZE['q'] = strlen(pack($this->NATIVE_FORMATS['q'], -70000000));
$this->NATIVE_SIZE['Q'] = strlen(pack($this->NATIVE_FORMATS['Q'], 70000000));
$this->TYPE['q'] = 'int';
$this->TYPE['Q'] = 'int';
$this->NATIVE_TYPE['q'] = 'int';
$this->NATIVE_TYPE['Q'] = 'int';
}
$this->MODIFIERS = [
'<' => ['BIG_ENDIAN' => false, 'SIZE' => $this->SIZE, 'FORMATS' => $this->FORMATS, 'TYPE' => $this->TYPE],
'>' => ['BIG_ENDIAN' => true, 'SIZE' => $this->SIZE, 'FORMATS' => $this->FORMATS, 'TYPE' => $this->TYPE],
'!' => ['BIG_ENDIAN' => true, 'SIZE' => $this->SIZE, 'FORMATS' => $this->FORMATS, 'TYPE' => $this->TYPE],
'=' => ['BIG_ENDIAN' => $this->BIG_ENDIAN, 'SIZE' => $this->SIZE, 'FORMATS' => $this->FORMATS, 'TYPE' => $this->TYPE],
'@' => ['BIG_ENDIAN' => $this->BIG_ENDIAN, 'SIZE' => $this->NATIVE_SIZE, 'FORMATS' => $this->NATIVE_FORMATS, 'TYPE' => $this->NATIVE_TYPE],
];
}
/**
* ExceptionErrorHandler.
*
* Error handler for pack and unpack
*/
public function ExceptionErrorHandler($errno = 0, $errstr = null, $errfile = null, $errline = null)
{
// If error is suppressed with @, don't throw an exception
if (error_reporting() === 0) {
return true; // return true to continue through the others error handlers
}
throw new StructException($errstr, $errno);
}
/**
* pack.
*
* Packs data into bytes
*
* @param $format Format string
* @param ...$data Parameters to encode
*
* @return Encoded data
*/
public function pack($format, ...$data)
{
if (!(isset($this) && get_class($this) == __CLASS__)) {
$struct = new \danog\PHP\Struct();
return $struct->pack($format, ...$data);
}
$result = null; // Data to return
$packcommand = $this->parseformat($format, $this->array_each_strlen($data)); // Get pack parameters
set_error_handler([$this, 'ExceptionErrorHandler']);
foreach ($packcommand as $key => $command) {
try {
switch ($command['modifiers']['TYPE']) {
case 'int':
if (!is_int($data[$command['datakey']])) {
throw new StructException('Required argument is not an integer.');
}
break;
case 'float':
if (!is_float($data[$command['datakey']])) {
throw new StructException('Required argument is not a float.');
}
break;
case 'string':
if (!is_string($data[$command['datakey']])) {
throw new StructException('Required argument is not a string.');
}
break;
case 'bool':
//if(!is_bool($data[$command["datakey"]])) throw new StructException("Required argument is not a bool."); // Ignore boolean type
break;
default:
break;
}
switch ($command['format']) {
case 'x':
$curresult = pack($command['phpformat'].$command['count']); // Pack current char
break;
case 'p':
$tempstring = pack('a'.($command['count'] - 1), $data[$command['datakey']]);
$curresult = pack('v', ($command['count'] - 1 > 255) ? 255 : $command['count'] - 1)[0].$tempstring;
break;
case '?':
$curresult = pack($command['phpformat'], (bool) $data[$command['datakey']]);
break;
default:
$curresult = pack($command['phpformat'].$command['count'], $data[$command['datakey']]); // Pack current char
break;
}
} catch (StructException $e) {
throw new StructException('An error occurred while packing data at offset '.$key.' ('.$e->getMessage().').');
}
if ($this->BIG_ENDIAN != $command['modifiers']['BIG_ENDIAN'] && !in_array($command['format'], ['x', 'c', 'b', 'B', '?', 's', 'p'])) {
$curresult = strrev($curresult);
} // Reverse if wrong endianness
if (strlen($curresult) > $command['modifiers']['SIZE'] * $command['count']) {
if ($command['modifiers']['BIG_ENDIAN']) {
$curresult = strrev($curresult);
}
$remains = array_slice(str_split($curresult), $command['modifiers']['SIZE'], strlen($curresult) - $command['modifiers']['SIZE']);
foreach ($remains as $rem) {
if ($rem != '') {
throw new StructException('Error while trimming result at offset '.$key.' (format char '.$command['format']."): data to trim isn't empty.");
}
}
$curresult = implode('', substr($curresult, 0, $command['modifiers']['SIZE']));
if ($command['modifiers']['BIG_ENDIAN']) {
$curresult = strrev($curresult);
}
}
$result .= $curresult;
}
restore_error_handler();
if (strlen($result) != $this->calcsize($format)) {
throw new StructException('Length of generated data ('.strlen($result).') is different from length calculated using format string ('.$this->calcsize($format).').');
}
return $result;
}
/**
* unpack.
*
* Unpacks data into an array
*
* @param $format Format string
* @param $data Data to decode
*
* @return Decoded data
*/
public function unpack($format, $data)
{
if (!(isset($this) && get_class($this) == __CLASS__)) {
$struct = new \danog\PHP\Struct();
return $struct->unpack($format, $data);
}
if (strlen($data) != $this->calcsize($format)) {
throw new StructException('Length of given data ('.strlen($data).') is different from length calculated using format string ('.$this->calcsize($format).').');
}
$dataarray = $this->data2array($format, $data);
if ($this->array_total_strlen($dataarray) != $this->calcsize($format)) {
throw new StructException('Length of given data array ('.$this->array_total_strlen($dataarray).') is different from length calculated using format string ('.$this->calcsize($format).').');
}
$result = []; // Data to return
$packcommand = $this->parseformat($format, $this->array_each_strlen($dataarray), true); // Get unpack parameters
set_error_handler([$this, 'ExceptionErrorHandler']);
$arraycount = 0;
foreach ($packcommand as $key => $command) {
if (isset($command['modifiers']['BIG_ENDIAN']) && $this->BIG_ENDIAN != $command['modifiers']['BIG_ENDIAN'] && !in_array($command['format'], ['x', 'c', 'b', 'B', '?', 's', 'p'])) {
$dataarray[$command['datakey']] = strrev($dataarray[$command['datakey']]);
} // Reverse if wrong endianness
try {
switch ($command['format']) {
case 'p':
$templength = unpack('s', $dataarray[$command['datakey']][0].pack('x'))[1];
$result[$arraycount] = implode('', unpack('a'.$templength, substr($dataarray[$command['datakey']], 1)));
break;
case '?':
if (implode('', unpack($command['phpformat'].$command['count'], $dataarray[$command['datakey']])) == 0) {
$result[$arraycount] = false;
} else {
$result[$arraycount] = true;
}
break;
default:
$result[$arraycount] = implode('', unpack($command['phpformat'].$command['count'], $dataarray[$command['datakey']])); // Unpack current char
break;
}
} catch (StructException $e) {
throw new StructException('An error occurred while unpacking data at offset '.$key.' ('.$e->getMessage().').');
}
switch ($command['modifiers']['TYPE']) {
case 'int':
$result[$arraycount] = (int) $result[$arraycount];
break;
case 'float':
$result[$arraycount] = (float) $result[$arraycount];
break;
case 'string':
$result[$arraycount] = (string) $result[$arraycount];
break;
case 'bool':
$result[$arraycount] = (bool) $result[$arraycount];
break;
case 'unset':
unset($result[$arraycount]);
$arraycount--;
break;
default:
$result[$arraycount] = (string) $result[$arraycount];
break;
}
$arraycount++;
}
restore_error_handler();
return $result;
}
/**
* calcsize.
*
* Return the size of the struct (and hence of the string) corresponding to the given format.
*
* @param $format Format string
*
* @return int with size of the struct.
*/
public function calcsize($format)
{
if (!(isset($this) && get_class($this) == __CLASS__)) {
$struct = new \danog\PHP\Struct();
return $struct->calcsize($format);
}
$size = 0;
$modifier = $this->MODIFIERS['@'];
$count = null;
foreach (str_split($format) as $offset => $currentformatchar) {
if (isset($this->MODIFIERS[$currentformatchar])) {
$modifier = $this->MODIFIERS[$currentformatchar]; // Set the modifiers for the current format char
} elseif (is_numeric($currentformatchar) && ((int) $currentformatchar > 0 || (int) $currentformatchar <= 9)) {
$count .= $currentformatchar; // Set the count for the current format char
} elseif (isset($modifier['SIZE'][$currentformatchar])) {
if (!isset($count) || $count == null) {
$count = 1; // Set count to 1 if something's wrong.
}
$size += $count * $modifier['SIZE'][$currentformatchar];
$count = null;
} else {
throw new StructException('Unkown format or modifier supplied ('.$currentformatchar.' at offset '.$offset.').');
}
}
return $size;
}
/**
* parseformat.
*
* Parses format string.
*
* @param $format Format string to parse
* @param $arraycount Array containing the number of chars contained in each element of the array to pack
*
* @throws StructException if format string is too long or there aren't enough parameters or if an unkown format or modifier is supplied.
*
* @return array with format and modifiers for each object to encode/decode
*/
public function parseformat($format, $arraycount, $unpack = false)
{
$datarraycount = 0; // Current element of the array to pack/unpack
$formatcharcount = 0; // Current element to pack/unpack (sometimes there isn't a correspondant element in the array)
$modifier = $this->MODIFIERS['@'];
$result = []; // Array with the results
$count = null;
$loopcount = 0;
foreach (str_split($format) as $offset => $currentformatchar) { // Current format char
if (!isset($result[$formatcharcount]) || !is_array($result[$formatcharcount])) {
$result[$formatcharcount] = []; // Create array for current element
}
if (isset($this->MODIFIERS[$currentformatchar])) { // If current format char is a modifier
$modifier = $this->MODIFIERS[$currentformatchar]; // Set the modifiers for the current format char
} elseif (is_numeric($currentformatchar) && ((int) $currentformatchar >= 0 || (int) $currentformatchar <= 9)) {
$count .= (int) $currentformatchar; // Set the count for the current format char
} elseif (isset($modifier['FORMATS'][$currentformatchar])) {
if (!isset($count) || $count == null) {
$count = 1; // Set count to 1 by default.
}
$count = (int) $count;
if ($currentformatchar == 's' || $currentformatchar == 'p') {
$loopcount = 1;
} else {
$loopcount = $count;
$count = 1;
}
for ($x = 0; $x < $loopcount; $x++) {
$result[$formatcharcount]['format'] = $currentformatchar; // Set format
$result[$formatcharcount]['phpformat'] = $modifier['FORMATS'][$currentformatchar]; // Set format
$result[$formatcharcount]['count'] = $count;
$result[$formatcharcount]['modifiers'] = ['BIG_ENDIAN' => $modifier['BIG_ENDIAN'], 'SIZE' => $modifier['SIZE'][$currentformatchar], 'TYPE' => $modifier['TYPE'][$currentformatchar]];
if ($unpack) {
if ($arraycount[$datarraycount] != $result[$formatcharcount]['count'] * $result[$formatcharcount]['modifiers']['SIZE']) {
throw new StructException('Length for format string '.$result[$formatcharcount]['format'].' at offset '.$offset.' ('.$result[$formatcharcount]['count'] * $result[$formatcharcount]['modifiers']['SIZE'].") isn't equal to the length of associated parameter (".$arraycount[$datarraycount].').');
}
$result[$formatcharcount]['datakey'] = $datarraycount;
$datarraycount++;
} else {
if ($currentformatchar != 'x') {
$result[$formatcharcount]['datakey'] = $datarraycount;
$datarraycount++;
}
}
if ($datarraycount > count($arraycount)) {
throw new StructException('Format string too long or not enough parameters at offset '.$offset.' ('.$currentformatchar.').');
}
$formatcharcount++; // Increase element count
}
$count = null;
} else {
throw new StructException('Unkown format or modifier supplied at offset '.$offset.' ('.$currentformatchar.').');
}
}
return $result;
}
public function data2array($format, $data)
{
$dataarray = [];
$dataarraykey = 0;
$datakey = 0;
$count = null;
$loopcount = 0;
$modifier = $this->MODIFIERS['@'];
foreach (str_split($format) as $offset => $currentformatchar) {
if (isset($this->MODIFIERS[$currentformatchar])) {
$modifier = $this->MODIFIERS[$currentformatchar]; // Set the modifiers for the current format char
} elseif (is_numeric($currentformatchar) && ((int) $currentformatchar > 0 || (int) $currentformatchar <= 9)) {
$count .= $currentformatchar; // Set the count for the current format char
} elseif (isset($modifier['SIZE'][$currentformatchar])) {
if (!isset($count) || $count == null) {
$count = 1; // Set count to 1 if something's wrong.
}
$count = (int) $count;
if ($currentformatchar == 's' || $currentformatchar == 'p') {
$loopcount = 1;
} else {
$loopcount = $count;
$count = 1;
}
for ($x = 0; $x < $loopcount; $x++) {
if (!isset($dataarray[$dataarraykey])) {
$dataarray[$dataarraykey] = null;
}
for ($a = 0; $a < $count * $modifier['SIZE'][$currentformatchar]; $a++) {
$dataarray[$dataarraykey] .= $data[$datakey];
$datakey++;
}
$dataarraykey++;
}
$count = null;
} else {
throw new StructException('Unkown format or modifier supplied ('.$currentformatchar.' at offset '.$offset.').');
}
}
return $dataarray;
}
/**
* array_each_strlen.
*
* Get length of each array element.
*
* @param $array Array to parse
*
* @return array with lengths
**/
public function array_each_strlen($array)
{
foreach ($array as &$value) {
$value = $this->count($value);
}
return $array;
}
/**
* array_total_strlen.
*
* Get total length of every array element.
*
* @param $array Array to parse
*
* @return int with the total length
**/
public function array_total_strlen($array)
{
$count = 0;
foreach ($array as $value) {
$count += $this->count($value);
}
return $count;
}
/**
* count.
*
* Get the length of a string or of an array
*
* @param $input String or array to parse
*
* @return int with the length
**/
public function count($input)
{
if (is_array($input)) {
return count($input);
}
return strlen($input);
}
}
/* Just an exception class */
class StructException extends \Exception
{
public function __construct($message, $code = 0, Exception $previous = null)
{
// some code
if (isset($GLOBALS['doingphptests']) && $GLOBALS['doingphptests']) {
var_dump($message);
}
// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}
}

14
TL.php
View File

@ -123,7 +123,7 @@ class TL
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, substr($this->struct->pack('<i', $l), 0, 3));
fwrite($bytes_io, $value);
fwrite($bytes_io, pack('@'.(-$l % 4)));
}
@ -133,7 +133,7 @@ class TL
/**
* :type bytes_io: io.BytesIO object.
*/
public function deserialize(&$bytes_io, $type_ = null, $subtype = null)
public function deserialize($bytes_io, $type_ = null, $subtype = null)
{
assert(get_resource_type($bytes_io) == 'file' || get_resource_type($bytes_io) == 'stream');
if (($type_ == 'int')) {
@ -149,7 +149,7 @@ class TL
} elseif (($type_ == 'int256')) {
$x = fread($bytes_io, 32);
} elseif (($type_ == 'string') || ($type_ == 'bytes')) {
$l = $this->struct->unpack('<C', fread($bytes_io, 1)) [0];
$l = $this->struct->unpack('<B', fread($bytes_io, 1)) [0];
assert($l <= 254);
if (($l == 254)) {
$long_len = $this->struct->unpack('<I', fread($bytes_io, 3).string2bin('\x00')) [0];
@ -162,10 +162,10 @@ class TL
assert(is_string($x));
} elseif (($type_ == 'vector')) {
assert($subtype != null);
$count = $this->struct->unpack('<l', substr($bytes_io, 4)) [0];
$count = $this->struct->unpack('<l', fread($bytes_io, 4)) [0];
$x = [];
foreach (pyjslib_range($count) as $i) {
$x[] = deserialize($bytes_io, $subtype);
$x[] = $this->deserialize($bytes_io, $subtype);
}
} else {
if (isset($this->constructor_type[$type_])) {
@ -181,11 +181,11 @@ class TL
$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);
$x = $this->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']);
$x[$arg['name']] = $this->deserialize($bytes_io, $arg['type'], $arg['subtype']);
}
}
}

View File

@ -6,7 +6,6 @@ require_once 'os_path.php';
require_once 'crypt.php';
require_once 'prime.php';
require_once 'TL.php';
require_once 'Struct.php';
require_once 'vendor/autoload.php';
/**
@ -22,9 +21,11 @@ function newcrc32($data)
* Function to dump the hex version of a string.
* :param what: What to dump.
*/
function hex_dump($what)
function hex_dump(...$what)
{
var_dump(bin2hex($what));
foreach($what as $w){
var_dump(bin2hex($w));
}
}
/**
* len.
@ -127,7 +128,7 @@ function long_to_bytes($n, $blocksize = 0)
break;
}
}
$s = array_slice($s, $i, null);
$s = array_slice($s, $i);
if (($blocksize > 0) && (strlen($s) % $blocksize)) {
$s = ((($blocksize - (strlen($s) % $blocksize)) * $b('')) + $s);
}
@ -160,7 +161,7 @@ class Session
$this->timedelta = 0;
$this->session_id = random_bytes(8);
$this->auth_key = $auth_key;
$this->auth_key_id = $this->auth_key ? substr(sha1($this->auth_key, true), -8, null) : null;
$this->auth_key_id = $this->auth_key ? substr(sha1($this->auth_key, true), -8) : null;
stream_set_timeout($this->sock, 5);
$this->MAX_RETRY = 5;
$this->AUTH_MAX_RETRY = 5;
@ -190,7 +191,7 @@ class Session
} else {
$encrypted_data =
$this->server_salt.$this->session_id.$message_id.$this->struct->pack('<II', $this->number, strlen($message_data)).$message_data;
$message_key = substr(sha1($encrypted_data, true), -16, null);
$message_key = substr(sha1($encrypted_data, true), -16);
$padding = random_bytes((-strlen($encrypted_data) % 16));
echo strlen($encrypted_data.$padding).PHP_EOL;
list($aes_key, $aes_iv) = $this->aes_calculate($message_key);
@ -211,15 +212,15 @@ class Session
if (len($packet_length_data) < 4) {
throw new Exception('Nothing in the socket!');
}
$packet_length = $this->struct->unpack('<I', $packet_length_data)[1];
$packet_length = $this->struct->unpack('<I', $packet_length_data)[0];
$packet = fread($this->sock, ($packet_length - 4));
if (!(newcrc32($packet_length_data.substr($packet, 0, -4 - 0)) == $this->struct->unpack('<I', substr($packet, -4, null))[1])) {
if (!(newcrc32($packet_length_data.substr($packet, 0, -4)) == $this->struct->unpack('<I', substr($packet, -4))[0])) {
throw new Exception('CRC32 was not correct!');
}
$x = $this->struct->unpack('<I', substr($packet, null, 4));
$auth_key_id = substr($packet, 4, 12 - 4);
$x = $this->struct->unpack('<I', substr($packet, 0, 4));
$auth_key_id = substr($packet, 4, 8);
if ($auth_key_id == string2bin('\x00\x00\x00\x00\x00\x00\x00\x00')) {
list($message_id, $message_length) = struct.unpack('<8sI', substr($packet, 12, 24));
list($message_id, $message_length) = $this->struct->unpack('<8sI', substr($packet, 12, 12));
$data = substr($packet, 24, (24 + $message_length) - 24);
} elseif ($auth_key_id == $this->auth_key_id) {
$message_key = substr($packet, 12, 28 - 12);
@ -251,7 +252,6 @@ class Session
pyjslib_printnl('Retry call method');
continue;
}
return $this->tl->deserialize(fopen_and_write('php://memory', 'rw+b', $server_answer));
}
}
@ -289,8 +289,8 @@ class Session
$tmp_aes_key = (sha1(($new_nonce + $server_nonce), true) + array_slice(sha1(($server_nonce + $new_nonce), true), 0, 12 - 0));
$tmp_aes_iv = ((array_slice(sha1(($server_nonce + $new_nonce), true), 12, 20 - 12) + sha1(($new_nonce + $new_nonce), true)) + array_slice($new_nonce, 0, 4 - 0));
$answer_with_hash = crypt::ige_decrypt($encrypted_answer, $tmp_aes_key, $tmp_aes_iv);
$answer_hash = array_slice($answer_with_hash, null, 20);
$answer = array_slice($answer_with_hash, 20, null);
$answer_hash = array_slice($answer_with_hash, 0, 20);
$answer = array_slice($answer_with_hash, 20);
$server_DH_inner_data = deserialize(io::BytesIO($answer));
assert(($nonce == $server_DH_inner_data['nonce']));
assert(($server_nonce == $server_DH_inner_data['server_nonce']));
@ -317,10 +317,10 @@ class Session
$auth_key = pow($g_a, $b, $dh_prime);
$auth_key_str = new long_to_bytes($auth_key);
$auth_key_sha = sha1($auth_key_str, true);
$auth_key_aux_hash = array_slice($auth_key_sha, null, 8);
$new_nonce_hash1 = array_slice(sha1($new_nonce.''.$auth_key_aux_hash, true), -16, null);
$new_nonce_hash2 = array_slice(sha1($new_nonce.''.$auth_key_aux_hash, true), -16, null);
$new_nonce_hash3 = array_slice(sha1($new_nonce.''.$auth_key_aux_hash, true), -16, null);
$auth_key_aux_hash = array_slice($auth_key_sha, 0, 8);
$new_nonce_hash1 = array_slice(sha1($new_nonce.''.$auth_key_aux_hash, true), -16);
$new_nonce_hash2 = array_slice(sha1($new_nonce.''.$auth_key_aux_hash, true), -16);
$new_nonce_hash3 = array_slice(sha1($new_nonce.''.$auth_key_aux_hash, true), -16);
assert(($Set_client_DH_params_answer['nonce'] == $nonce));
assert(($Set_client_DH_params_answer['server_nonce'] == $server_nonce));
if (($Set_client_DH_params_answer->name == 'dh_gen_ok')) {
@ -328,7 +328,7 @@ class Session
pyjslib_printnl('Diffie Hellman key exchange processed successfully');
$this->server_salt = new strxor(array_slice($new_nonce, 0, 8 - 0), array_slice($server_nonce, 0, 8 - 0));
$this->auth_key = $auth_key_str;
$this->auth_key_id = array_slice($auth_key_sha, -8, null);
$this->auth_key_id = array_slice($auth_key_sha, -8);
pyjslib_printnl('Auth key generated');
return 'Auth Ok';

View File

@ -109,7 +109,7 @@ class Session:
if auth_key_id == b'\x00\x00\x00\x00\x00\x00\x00\x00':
# No encryption - Plain text
(message_id, message_length) = struct.unpack("<8sI", packet[12:24])
print(len(packet[12:24]))
data = packet[24:24+message_length]
elif auth_key_id == self.auth_key_id:
message_key = packet[12:28]