Skip to content

Commit

Permalink
Undo commit: Fix for large base64 streams
Browse files Browse the repository at this point in the history
Fix involved filtering out non-b64 chars and reading to an offset,
this is expected to be done with a PregReplaceFilterStream, not
Base64Stream directly.
  • Loading branch information
zbateson committed Mar 21, 2019
1 parent 6bab95d commit e2bf1a5
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/Base64Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@
/**
* GuzzleHttp\Psr7 stream decoder extension for base64 streams.
*
* Note that it's expected the underlying stream will only contain valid base64
* characters (normally the stream should be wrapped in a
* PregReplaceFilterStream to filter out non-base64 characters for reading).
*
* ```
* $f = fopen(...);
* $stream = new Base64Stream(new PregReplaceFilterStream(
* Psr7\stream_for($f), '/[^a-zA-Z0-9\/\+=]/', ''
* ));
* //...
* ```
*
* For writing, a ChunkSplitStream could come in handy so the output is split
* into lines:
*
* ```
* $f = fopen(...);
* $stream = new Base64Stream(new ChunkSplitStream(new PregReplaceFilterStream(
* Psr7\stream_for($f), '/[^a-zA-Z0-9\/\+=]/', ''
* )));
* //...
* ```
*
* @author Zaahid Bateson
*/
class Base64Stream implements StreamInterface
Expand Down Expand Up @@ -99,31 +122,14 @@ public function eof()
return ($this->buffer->eof() && $this->stream->eof());
}

/**
* Checks if the passed bytes contain a multiple of four valid base64 bytes
* after stripping invalid characters, and reads as many additional bytes as
* needed until a multiple of four or the end of stream is reached.
*
* @param string $bytes
* @return string
*/
private function readToBase64Offset($bytes)
{
$raw = preg_replace('~[^A-Za-z0-9\+/\=]+~', '', $bytes);
while (strlen($raw) % 4 !== 0) {
$b = $this->stream->read(1);
if ($b === false || $b === '') {
return $raw;
}
$raw .= preg_replace('~[^A-Za-z0-9\+/\=]+~', '', $b);
}
return $raw;
}

/**
* Fills the internal byte buffer after reading and decoding data from the
* underlying stream.
*
* Note that it's expected the underlying stream will only contain valid
* base64 characters (normally the stream should be wrapped in a
* PregReplaceFilterStream to filter out non-base64 characters).
*
* @param int $length
*/
private function fillBuffer($length)
Expand All @@ -134,8 +140,7 @@ private function fillBuffer($length)
if ($read === false || $read === '') {
break;
}
$decoded = base64_decode($this->readToBase64Offset($read));
$this->buffer->write($decoded);
$this->buffer->write(base64_decode($read));
}
}

Expand Down

0 comments on commit e2bf1a5

Please sign in to comment.