From 7528117e446b792a7c4e57a0eb79d2a52b1a4662 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 +++++++++++++++-------- 2 files changed, 17 insertions(+), 10 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..62184d32a94 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. If -1 then it will read until the end. + * @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 +}