Initial Commit
This commit is contained in:
51
dependencies/amphp/byte-stream/lib/Base64/Base64DecodingInputStream.php
vendored
Normal file
51
dependencies/amphp/byte-stream/lib/Base64/Base64DecodingInputStream.php
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\StreamException;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class Base64DecodingInputStream implements InputStream
|
||||
{
|
||||
/** @var InputStream|null */
|
||||
private $source;
|
||||
/** @var string|null */
|
||||
private $buffer = '';
|
||||
public function __construct(InputStream $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
public function read() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
if ($this->source === null) {
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
$chunk = (yield $this->source->read());
|
||||
if ($chunk === null) {
|
||||
if ($this->buffer === null) {
|
||||
return null;
|
||||
}
|
||||
$chunk = \base64_decode($this->buffer, \true);
|
||||
if ($chunk === \false) {
|
||||
$this->source = null;
|
||||
$this->buffer = null;
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
$this->buffer = null;
|
||||
return $chunk;
|
||||
}
|
||||
$this->buffer .= $chunk;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_decode(\substr($this->buffer, 0, $length - $length % 4), \true);
|
||||
if ($chunk === \false) {
|
||||
$this->source = null;
|
||||
$this->buffer = null;
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 4);
|
||||
return $chunk;
|
||||
});
|
||||
}
|
||||
}
|
43
dependencies/amphp/byte-stream/lib/Base64/Base64DecodingOutputStream.php
vendored
Normal file
43
dependencies/amphp/byte-stream/lib/Base64/Base64DecodingOutputStream.php
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\OutputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\StreamException;
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
final class Base64DecodingOutputStream implements OutputStream
|
||||
{
|
||||
/** @var OutputStream */
|
||||
private $destination;
|
||||
/** @var string */
|
||||
private $buffer = '';
|
||||
/** @var int */
|
||||
private $offset = 0;
|
||||
public function __construct(OutputStream $destination)
|
||||
{
|
||||
$this->destination = $destination;
|
||||
}
|
||||
public function write(string $data) : Promise
|
||||
{
|
||||
$this->buffer .= $data;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_decode(\substr($this->buffer, 0, $length - $length % 4), \true);
|
||||
if ($chunk === \false) {
|
||||
return new Failure(new StreamException('Invalid base64 near offset ' . $this->offset));
|
||||
}
|
||||
$this->offset += $length - $length % 4;
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 4);
|
||||
return $this->destination->write($chunk);
|
||||
}
|
||||
public function end(string $finalData = "") : Promise
|
||||
{
|
||||
$this->offset += \strlen($this->buffer);
|
||||
$chunk = \base64_decode($this->buffer . $finalData, \true);
|
||||
if ($chunk === \false) {
|
||||
return new Failure(new StreamException('Invalid base64 near offset ' . $this->offset));
|
||||
}
|
||||
$this->buffer = '';
|
||||
return $this->destination->end($chunk);
|
||||
}
|
||||
}
|
37
dependencies/amphp/byte-stream/lib/Base64/Base64EncodingInputStream.php
vendored
Normal file
37
dependencies/amphp/byte-stream/lib/Base64/Base64EncodingInputStream.php
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class Base64EncodingInputStream implements InputStream
|
||||
{
|
||||
/** @var InputStream */
|
||||
private $source;
|
||||
/** @var string|null */
|
||||
private $buffer = '';
|
||||
public function __construct(InputStream $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
public function read() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
$chunk = (yield $this->source->read());
|
||||
if ($chunk === null) {
|
||||
if ($this->buffer === null) {
|
||||
return null;
|
||||
}
|
||||
$chunk = \base64_encode($this->buffer);
|
||||
$this->buffer = null;
|
||||
return $chunk;
|
||||
}
|
||||
$this->buffer .= $chunk;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_encode(\substr($this->buffer, 0, $length - $length % 3));
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 3);
|
||||
return $chunk;
|
||||
});
|
||||
}
|
||||
}
|
31
dependencies/amphp/byte-stream/lib/Base64/Base64EncodingOutputStream.php
vendored
Normal file
31
dependencies/amphp/byte-stream/lib/Base64/Base64EncodingOutputStream.php
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\OutputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
final class Base64EncodingOutputStream implements OutputStream
|
||||
{
|
||||
/** @var OutputStream */
|
||||
private $destination;
|
||||
/** @var string */
|
||||
private $buffer = '';
|
||||
public function __construct(OutputStream $destination)
|
||||
{
|
||||
$this->destination = $destination;
|
||||
}
|
||||
public function write(string $data) : Promise
|
||||
{
|
||||
$this->buffer .= $data;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_encode(\substr($this->buffer, 0, $length - $length % 3));
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 3);
|
||||
return $this->destination->write($chunk);
|
||||
}
|
||||
public function end(string $finalData = "") : Promise
|
||||
{
|
||||
$chunk = \base64_encode($this->buffer . $finalData);
|
||||
$this->buffer = '';
|
||||
return $this->destination->end($chunk);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user