public function setValue(Content $content) { $binaryData = $content->binaryData; $offsetIndex = 0; $contentLength = $this->contentLength->length; $isNegative = (ord($binaryData[$offsetIndex]) & 0x80) != 0x0; $number = gmp_init(ord($binaryData[$offsetIndex++]) & 0x7f, 10); for ($i = 0; $i < $contentLength - 1; $i++) { $number = gmp_or(gmp_mul($number, 0x100), ord($binaryData[$offsetIndex++])); } if ($isNegative) { $number = gmp_sub($number, gmp_pow(2, 8 * $contentLength - 1)); } $value = gmp_strval($number, 10); if (is_string($value)) { // remove gaps between hex digits $value = preg_replace('/\\s|0x/', '', $value); } elseif (is_numeric($value)) { $value = dechex($value); } else { throw new Exception('OctetString: unrecognized input type!'); } if (strlen($value) % 2 != 0) { // transform values like 1F2 to 01F2 $value = '0' . $value; } $this->value = $value; }
private function makeId32($timestamp, $machine, $sequence) { $timestamp = gmp_mul((string) $timestamp, gmp_pow(2, 22)); $machine = gmp_mul((string) $machine, gmp_pow(2, 12)); $sequence = gmp_init((string) $sequence, 10); $value = gmp_or(gmp_or($timestamp, $machine), $sequence); return gmp_strval($value, 10); }
/** * @param int $value * * @return string */ public static function encode($value) { $value = gmp_init($value, 10); $octets = chr(gmp_strval(gmp_and($value, 0x7f), 10)); $rightShift = function ($number, $positions) { return gmp_div($number, gmp_pow(2, $positions)); }; $value = $rightShift($value, 7); while (gmp_cmp($value, 0) > 0) { $octets .= chr(gmp_strval(gmp_or(0x80, gmp_and($value, 0x7f)), 10)); $value = $rightShift($value, 7); } return strrev($octets); }
public static function fromBinary(&$binaryData, &$offsetIndex = 0) { $parsedObject = new static(0); self::parseIdentifier($binaryData[$offsetIndex], $parsedObject->getType(), $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1); $isNegative = (ord($binaryData[$offsetIndex]) & 0x80) != 0x0; $number = gmp_init(ord($binaryData[$offsetIndex++]) & 0x7f, 10); for ($i = 0; $i < $contentLength - 1; $i++) { $number = gmp_or(gmp_mul($number, 0x100), ord($binaryData[$offsetIndex++])); } if ($isNegative) { $number = gmp_sub($number, gmp_pow(2, 8 * $contentLength - 1)); } $parsedObject = new static(gmp_strval($number, 10)); $parsedObject->setContentLength($contentLength); return $parsedObject; }
/** * @param $value * @param $mask * * @return string */ public static function add($value, $mask) { return gmp_strval(gmp_or($value, $mask)); }
// gmp_neg $neg1 = gmp_neg("1"); echo gmp_strval($neg1) . "\n"; $neg2 = gmp_neg("-1"); echo gmp_strval($neg2) . "\n"; // gmp_nextprime $prime1 = gmp_nextprime(10); // next prime number greater than 10 $prime2 = gmp_nextprime(-1000); // next prime number greater than -1000 echo gmp_strval($prime1) . "\n"; echo gmp_strval($prime2) . "\n"; // gmp_or $or1 = gmp_or("0xfffffff2", "4"); echo gmp_strval($or1, 16) . "\n"; $or2 = gmp_or("0xfffffff2", "2"); echo gmp_strval($or2, 16) . "\n"; // gmp_perfect_square var_dump(gmp_perfect_square("9")); // 3 * 3, perfect square var_dump(gmp_perfect_square("7")); // not a perfect square // 1234567890 * 1234567890, perfect square var_dump(gmp_perfect_square("1524157875019052100")); // gmp_popcount $pop1 = gmp_init("10000101", 2); // 3 1's echo gmp_popcount($pop1) . "\n"; $pop2 = gmp_init("11111110", 2); // 7 1's echo gmp_popcount($pop2) . "\n";
/** * Bitwise "or" (|) * * @param mixed $number * @access public * @return self */ public function bitOr($number) { $result = gmp_or($this->getRawValue(), static::upgradeParam($number)->getRawValue()); return static::factory($result); }
protected static function encodepoint($P) { list($x, $y) = $P; $t = gmp_or(gmp_and($y, gmp_sub(gmp_pow(2, 256), 1)), gmp_mul(gmp_and($x, 1), gmp_pow(2, 255))); $t = str_pad(gmp_strval($t, 16), 64, 0, STR_PAD_LEFT); $res = strrev(pack('H*', $t)); return $res; }
/** * @param int|string $int * @param int|string $otherInt * @return string */ public function bitwiseOr($int, $otherInt) { return gmp_strval(gmp_or(gmp_init($int, 10), gmp_init($otherInt, 10)), 10); }
/** * Logical Or * * @param Math_BigInteger $x * @access public * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat> * @return Math_BigInteger */ function bitwise_or($x) { switch (MATH_BIGINTEGER_MODE) { case MATH_BIGINTEGER_MODE_GMP: $temp = new Math_BigInteger(); $temp->value = gmp_or($this->value, $x->value); return $temp; case MATH_BIGINTEGER_MODE_BCMATH: return new Math_BigInteger($this->toBytes() | $x->toBytes(), 256); } $result = new Math_BigInteger(); $x_length = count($x->value); for ($i = 0; $i < $x_length; $i++) { $result->value[] = $this->value[$i] | $x->value[$i]; } return $result->_normalize(); }
<?php var_dump(gmp_strval(gmp_or("111111", "2222222"))); var_dump(gmp_strval(gmp_or(123123, 435234))); var_dump(gmp_strval(gmp_or(555, "2342341123"))); var_dump(gmp_strval(gmp_or(-1, 3333))); var_dump(gmp_strval(gmp_or(4545, -20))); var_dump(gmp_strval(gmp_or("test", "no test"))); $n = gmp_init("987657876543456"); var_dump(gmp_strval(gmp_or($n, "34332"))); $n1 = gmp_init("987657878765436543456"); var_dump(gmp_strval(gmp_or($n, $n1))); var_dump(gmp_or($n, $n1, 1)); var_dump(gmp_or(1)); var_dump(gmp_or(array(), 1)); var_dump(gmp_or(1, array())); var_dump(gmp_or(array(), array())); echo "Done\n";
/** * @param int[] $bytes array of ascii codes of bytes to decode * @return string represenation of decoded long. */ static function decode_long_from_array($bytes) { $b = array_shift($bytes); $g = gmp_init($b & 0x7f); $shift = 7; while (0 != ($b & 0x80)) { $b = array_shift($bytes); $g = gmp_or($g, self::shift_left($b & 0x7f, $shift)); $shift += 7; } $val = gmp_xor(self::shift_right($g, 1), gmp_neg(gmp_and($g, 1))); return gmp_strval($val); }
/** * Get server information * * @throws InvalidPacketException * @throws SocketException * * @return array Returns an array with information on success */ public function GetInfo() { if (!$this->Connected) { throw new SocketException('Not connected.', SocketException::NOT_CONNECTED); } $this->Socket->Write(self::A2S_INFO, "Source Engine Query"); $Buffer = $this->Socket->Read(); $Type = $Buffer->GetByte(); // Old GoldSource protocol, HLTV still uses it if ($Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE) { /** * If we try to read data again, and we get the result with type S2A_INFO (0x49) * That means this server is running dproto, * Because it sends answer for both protocols */ $Server['Address'] = $Buffer->GetString(); $Server['HostName'] = $Buffer->GetString(); $Server['Map'] = $Buffer->GetString(); $Server['ModDir'] = $Buffer->GetString(); $Server['ModDesc'] = $Buffer->GetString(); $Server['Players'] = $Buffer->GetByte(); $Server['MaxPlayers'] = $Buffer->GetByte(); $Server['Protocol'] = $Buffer->GetByte(); $Server['Dedicated'] = Chr($Buffer->GetByte()); $Server['Os'] = Chr($Buffer->GetByte()); $Server['Password'] = $Buffer->GetByte() === 1; $Server['IsMod'] = $Buffer->GetByte() === 1; if ($Server['IsMod']) { $Mod['Url'] = $Buffer->GetString(); $Mod['Download'] = $Buffer->GetString(); $Buffer->Get(1); // NULL byte $Mod['Version'] = $Buffer->GetLong(); $Mod['Size'] = $Buffer->GetLong(); $Mod['ServerSide'] = $Buffer->GetByte() === 1; $Mod['CustomDLL'] = $Buffer->GetByte() === 1; } $Server['Secure'] = $Buffer->GetByte() === 1; $Server['Bots'] = $Buffer->GetByte(); if (isset($Mod)) { $Server['Mod'] = $Mod; } return $Server; } if ($Type !== self::S2A_INFO) { throw new InvalidPacketException('GetInfo: Packet header mismatch. (0x' . DecHex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); } $Server['Protocol'] = $Buffer->GetByte(); $Server['HostName'] = $Buffer->GetString(); $Server['Map'] = $Buffer->GetString(); $Server['ModDir'] = $Buffer->GetString(); $Server['ModDesc'] = $Buffer->GetString(); $Server['AppID'] = $Buffer->GetShort(); $Server['Players'] = $Buffer->GetByte(); $Server['MaxPlayers'] = $Buffer->GetByte(); $Server['Bots'] = $Buffer->GetByte(); $Server['Dedicated'] = Chr($Buffer->GetByte()); $Server['Os'] = Chr($Buffer->GetByte()); $Server['Password'] = $Buffer->GetByte() === 1; $Server['Secure'] = $Buffer->GetByte() === 1; // The Ship (they violate query protocol spec by modifying the response) if ($Server['AppID'] === 2400) { $Server['GameMode'] = $Buffer->GetByte(); $Server['WitnessCount'] = $Buffer->GetByte(); $Server['WitnessTime'] = $Buffer->GetByte(); } $Server['Version'] = $Buffer->GetString(); // Extra Data Flags if ($Buffer->Remaining() > 0) { $Server['ExtraDataFlags'] = $Flags = $Buffer->GetByte(); // The server's game port if ($Flags & 0x80) { $Server['GamePort'] = $Buffer->GetShort(); } // The server's steamid // Want to play around with this? // You can use https://github.com/xPaw/SteamID.php if ($Flags & 0x10) { $SteamIDLower = $Buffer->GetUnsignedLong(); $SteamIDInstance = $Buffer->GetUnsignedLong(); // This gets shifted by 32 bits, which should be steamid instance $SteamID = 0; if (PHP_INT_SIZE === 4) { if (extension_loaded('gmp')) { $SteamIDLower = gmp_abs($SteamIDLower); $SteamIDInstance = gmp_abs($SteamIDInstance); $SteamID = gmp_strval(gmp_or($SteamIDLower, gmp_mul($SteamIDInstance, gmp_pow(2, 32)))); } else { throw new \RuntimeException('Either 64-bit PHP installation or "gmp" module is required to correctly parse server\'s steamid.'); } } else { $SteamID = $SteamIDLower | $SteamIDInstance << 32; } $Server['SteamID'] = $SteamID; unset($SteamIDLower, $SteamIDInstance, $SteamID); } // The spectator port and then the spectator server name if ($Flags & 0x40) { $Server['SpecPort'] = $Buffer->GetShort(); $Server['SpecName'] = $Buffer->GetString(); } // The game tag data string for the server if ($Flags & 0x20) { $Server['GameTags'] = $Buffer->GetString(); } // GameID -- alternative to AppID? if ($Flags & 0x1) { $Server['GameID'] = $Buffer->GetUnsignedLong() | $Buffer->GetUnsignedLong() << 32; } if ($Buffer->Remaining() > 0) { throw new InvalidPacketException('GetInfo: unread data? ' . $Buffer->Remaining() . ' bytes remaining in the buffer. Please report it to the library developer.', InvalidPacketException::BUFFER_NOT_EMPTY); } } return $Server; }
public function getShared($S, $P) { if (!is_string($S) || strlen($S) !== 32) { throw new \InvalidArgumentException(); } // Clamp the secret key. $n = gmp_init(bin2hex(strrev($S)), 16); $n = gmp_and(gmp_or($n, 64), gmp_init('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8', 16)); $P0 = $this->pubkeyToPoint(strrev($P)); $P1 = $this->scalarmult($P0[0], $n); $Q = $this->pointToPubkey($P1); return $Q; }
/** * Bitwise OR * * @param $value mixed anything that can be converted into an IP object * @return IP */ public function bit_or($value) { if (!$value instanceof self) { $value = new $this->class($value); } return new $this->class(gmp_or($this->ip, $value->ip)); }
public static function BitwiseOR($Number1, $Number2) { return gmp_or($Number1, $Number2); }
/** * @param int $BitOffset * @param int $ValueMask * @param int $Value * * @return void */ private function Set($BitOffset, $ValueMask, $Value) { $this->Data = gmp_or(gmp_and($this->Data, gmp_com(self::ShiftLeft($ValueMask, $BitOffset))), self::ShiftLeft(gmp_and($Value, $ValueMask), $BitOffset)); }
/** * Logical Or * * @param Math_BigInteger $x * @access public * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat> * @return Math_BigInteger */ function bitwise_or($x) { switch (MATH_BIGINTEGER_MODE) { case MATH_BIGINTEGER_MODE_GMP: $temp = new Math_BigInteger(); $temp->value = gmp_or($this->value, $x->value); return $this->_normalize($temp); case MATH_BIGINTEGER_MODE_BCMATH: $left = $this->toBytes(); $right = $x->toBytes(); $length = max(strlen($left), strlen($right)); $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); return $this->_normalize(new Math_BigInteger($left | $right, 256)); } $length = max(count($this->value), count($x->value)); $result = $this->copy(); $result->value = array_pad($result->value, $length, 0); $x->value = array_pad($x->value, $length, 0); for ($i = 0; $i < $length; ++$i) { $result->value[$i] |= $x->value[$i]; } return $this->_normalize($result); }
private function setIPv6( $addr, $mask ) { $this->net_addr = @inet_pton($addr); if( $this->net_addr == false ) { throw new Exception( "invalid ip address {$addr}" ); } $this->valid = true; $this->net_addr_long = $this->inet_ntogmp( $this->net_addr ); //$this->inet_gmpton( $this->net_addr_long ); // set the netmask if( preg_match( '/^[0-9]+$/', $mask )) { $this->net_mask_bits = intval( $mask ); if( $this->ipv4 && $this->net_mask_bits != 0 ){ $this->net_mask_bits += 96; } $this->net_mask_long = gmp_mul( gmp_sub( gmp_pow( 2, $this->net_mask_bits ), 1 ), gmp_pow( 2, 128-$this->net_mask_bits )); // echo gmp_strval( $this->net_mask_long, 2 )."<br />\n"; $this->net_mask = $this->inet_gmpton($this->net_mask_long); } else { $this->net_mask = inet_pton($mask); $this->net_mask_long = $this->inet_ntogmp($this->netmask); $this->net_mask_bits = gmp_scan0( $this->net_mask_long, 0 ); } // normalize it... $this->net_addr_long = gmp_and( $this->net_addr_long, $this->net_mask_long ); $this->net_addr = $this->inet_gmpton( $this->net_addr_long ); $this->net_broadcast_long = gmp_or( $this->net_addr_long, gmp_sub( gmp_pow( 2, 128-$this->net_mask_bits ), 1 )); $this->net_broadcast = $this->inet_gmpton($this->net_broadcast_long ); }