You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
898 B
PHTML
48 lines
898 B
PHTML
5 years ago
|
<?php
|
||
|
|
||
|
|
||
|
namespace Cijber\OpenSSL\C;
|
||
|
|
||
|
|
||
|
use Cijber\OpenSSL;
|
||
|
use FFI;
|
||
|
use FFI\CData;
|
||
|
|
||
|
class Memory extends CBackedObject
|
||
|
{
|
||
|
private int $size;
|
||
|
|
||
|
public function __construct(int $size)
|
||
|
{
|
||
|
$this->size = $size;
|
||
|
parent::__construct(OpenSSL::malloc($size));
|
||
|
}
|
||
|
|
||
|
public static function new(int $chunkSize): Memory
|
||
|
{
|
||
|
return new Memory($chunkSize);
|
||
|
}
|
||
|
|
||
|
public function get(): CData
|
||
|
{
|
||
|
return $this->cObj;
|
||
|
}
|
||
|
|
||
|
public function string(int $length, int $offset = 0)
|
||
|
{
|
||
|
return substr(FFI::string($this->cObj, $length + $offset), $offset);
|
||
|
}
|
||
|
|
||
|
public static function buffer(string $data): Memory
|
||
|
{
|
||
|
$len = strlen($data);
|
||
|
$mem = new Memory($len);
|
||
|
FFI::memcpy($mem->cObj, $data, $len);
|
||
|
return $mem;
|
||
|
}
|
||
|
|
||
|
public function pointer(): CData
|
||
|
{
|
||
|
return FFI::addr($this->cObj);
|
||
|
}
|
||
|
}
|