From 402623ce10e60cdb6266c6effe49d04c8f3981cb Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 11 Jan 2019 14:24:38 -0800 Subject: [PATCH] Fix reading of small SVG images to determine dimensions --- third_party/fasterimage/ImageParser.php | 4 ++-- third_party/fasterimage/Stream/Stream.php | 23 ++++++++++++------- .../Stream/StreamableInterface.php | 12 ++++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/third_party/fasterimage/ImageParser.php b/third_party/fasterimage/ImageParser.php index 33a526dbfaf..2901363721f 100644 --- a/third_party/fasterimage/ImageParser.php +++ b/third_party/fasterimage/ImageParser.php @@ -136,7 +136,7 @@ public function parseType() return $this->type = 'tiff'; default: $this->stream->resetPointer(); - $markup = $this->stream->read( 1024 ); + $markup = $this->stream->read( 1024, false ); if ( false !== strpos( $markup, 'type = 'svg'; } else { @@ -361,7 +361,7 @@ protected function parseSizeForWebp() protected function parseSizeForSvg() { $this->stream->resetPointer(); - $markup = $this->stream->read( 1024 ); + $markup = $this->stream->read( 1024, false ); if ( ! preg_match( '##s', $markup, $matches ) ) { return null; } diff --git a/third_party/fasterimage/Stream/Stream.php b/third_party/fasterimage/Stream/Stream.php index 69765efdfe1..13ad600b1ce 100644 --- a/third_party/fasterimage/Stream/Stream.php +++ b/third_party/fasterimage/Stream/Stream.php @@ -25,14 +25,15 @@ class Stream implements StreamableInterface /** * Get characters from the string but don't move the pointer * - * @param $characters + * @param int $characters Number of characters to read. + * @param bool $check_length Throw exception if there are not enough bytes left in the stream. * * @return string * @throws StreamBufferTooSmallException */ - public function peek($characters) + public function peek($characters, $check_length=true) { - if ( strlen($this->stream_string) < $this->strpos + $characters ) { + if ( $check_length && strlen($this->stream_string) < $this->strpos + $characters ) { throw new StreamBufferTooSmallException('Not enough of the stream available.'); } @@ -42,16 +43,22 @@ public function peek($characters) /** * Get Characters from the string * - * @param $characters + * @param int $characters Number of characters to read. + * @param bool $check_length Throw exception if there are not enough bytes left in the stream. * * @return string * @throws StreamBufferTooSmallException */ - public function read($characters) + public function read($characters, $check_length=true) { - $result = $this->peek($characters); + $size = strlen($this->stream_string); + $result = $this->peek($characters, $check_length); - $this->strpos += $characters; + if ( strlen( $result ) + $characters > $size ) { + $this->strpos = $size; + } else { + $this->strpos += $characters; + } return $result; } @@ -75,4 +82,4 @@ public function write($string) { $this->stream_string .= $string; } -} \ No newline at end of file +} diff --git a/third_party/fasterimage/Stream/StreamableInterface.php b/third_party/fasterimage/Stream/StreamableInterface.php index 0396cd377de..cf0bdb07db8 100644 --- a/third_party/fasterimage/Stream/StreamableInterface.php +++ b/third_party/fasterimage/Stream/StreamableInterface.php @@ -17,18 +17,20 @@ public function write($string); /** * Get Characters from the string * - * @param $characters + * @param int $characters Number of characters to read. + * @param bool $check_length Throw exception if there are not enough bytes left in the stream. */ - public function read($characters); + public function read($characters, $check_length=false); /** * Get characters from the string but don't move the pointer * - * @param $characters + * @param int $characters Number of characters to read. + * @param bool $check_length Throw exception if there are not enough bytes left in the stream. * * @return mixed */ - public function peek($characters); + public function peek($characters, $check_length=false); /** * Resets the pointer to the 0 position @@ -36,4 +38,4 @@ public function peek($characters); */ public function resetPointer(); -} \ No newline at end of file +}