From 2c006544241ba90d205d0802eafbdcda7065ff69 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Wed, 7 May 2014 11:39:05 -0500 Subject: [PATCH] mail-parse: Use a bit less memory copying --- include/pear/Mail/mimeDecode.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/include/pear/Mail/mimeDecode.php b/include/pear/Mail/mimeDecode.php index f2a768005..931166957 100644 --- a/include/pear/Mail/mimeDecode.php +++ b/include/pear/Mail/mimeDecode.php @@ -404,10 +404,16 @@ class Mail_mimeDecode extends PEAR */ function _splitBodyHeader(&$input) { - // The headers should end within the first 500k of the email... - if (preg_match("/^.*?(\r?\n\r?\n)(.)/s", $input, $match, PREG_OFFSET_CAPTURE)) { - return array(substr($input, 0, $match[1][1]), - new StringView($input, $match[2][1])); + if ($input instanceof StringView) + $check = $input->substr(0, 64<<10); + else + $check = &$input; + if (preg_match("/^.*?(\r?\n\r?\n)(.)/s", $check, $match, PREG_OFFSET_CAPTURE)) { + $headers = ($input instanceof StringView) + ? (string) $input->substr(0, $match[1][1]) : substr($input, 0, $match[1][1]); + $body = ($input instanceof StringView) + ? $input->substr($match[2][1]) : new StringView($input, $match[2][1]); + return array($headers, $body); } $this->_error = 'Could not split header and body'; return false; @@ -887,6 +893,11 @@ class StringView { : substr($this->string, $this->start); } + function substr($start, $end=false) { + return new StringView($this->string, $this->start + $start, + $end ? min($this->start + $end, $this->end ?: PHP_INT_MAX) : $this->end); + } + function split($boundary) { $matches = array(); if (!preg_match_all('/^' . preg_quote($boundary) . '/m', $this->string, -- GitLab