Commit 745be2a2 authored by Ralf Becker's avatar Ralf Becker

ZP-702 Attachments are 1 or 2 bytes short if size is not 3 byte aligned....

ZP-702 Attachments are 1 or 2 bytes short if size is not 3 byte aligned. Released under the Affero GNU General Public License (AGPL) version 3.
parent 6cfaf59b
...@@ -319,22 +319,7 @@ class Streamer implements Serializable { ...@@ -319,22 +319,7 @@ class Streamer implements Serializable {
$encoder->content(strtoupper(bin2hex($this->$map[self::STREAMER_VAR]))); $encoder->content(strtoupper(bin2hex($this->$map[self::STREAMER_VAR])));
} }
else if(isset($map[self::STREAMER_TYPE]) && $map[self::STREAMER_TYPE] == self::STREAMER_TYPE_STREAM) { else if(isset($map[self::STREAMER_TYPE]) && $map[self::STREAMER_TYPE] == self::STREAMER_TYPE_STREAM) {
//encode stream with base64 $encoder->content($this->$map[self::STREAMER_VAR]);
$stream = $this->$map[self::STREAMER_VAR];
$stat = fstat($stream);
// the padding size muss be calculated for the entire stream,
// the base64 filter seems to process 8192 byte chunks correctly itself
$padding = (isset($stat['size']) && $stat['size'] > 8192) ? ($stat['size'] % 3) : 0;
$paddingfilter = stream_filter_append($stream, 'padding.'.$padding);
$base64filter = stream_filter_append($stream, 'convert.base64-encode');
$d = "";
while (!feof($stream)) {
$d .= fgets($stream, 4096);
}
$encoder->content($d);
stream_filter_remove($base64filter);
stream_filter_remove($paddingfilter);
} }
// implode comma or semicolon arrays into a string // implode comma or semicolon arrays into a string
else if(isset($map[self::STREAMER_TYPE]) && is_array($this->$map[self::STREAMER_VAR]) && else if(isset($map[self::STREAMER_TYPE]) && is_array($this->$map[self::STREAMER_VAR]) &&
......
...@@ -167,18 +167,27 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -167,18 +167,27 @@ class WBXMLEncoder extends WBXMLDefs {
/** /**
* Puts content on the output stack * Puts content on the output stack
* *
* @param $content * @param string|resource $content
* *
* @access public * @access public
* @return string * @return string
*/ */
public function content($content) { public function content($content) {
// We need to filter out any \0 chars because it's the string terminator in WBXML. We currently if (is_resource($content)) {
// cannot send \0 characters within the XML content anywhere. if (($stat = fstat($content)) && isset($stat['size']) && !$stat['size']) {
$content = str_replace("\0","",$content); ZLog::Write(LOGLEVEL_DEBUG, "Ommit writing 0 size stream to WBXML");
return;
}
fseek($content, 0, SEEK_SET); // fstat seeks to end of file to determine size!
}
else {
// We need to filter out any \0 chars because it's the string terminator in WBXML. We currently
// cannot send \0 characters within the XML content anywhere.
$content = str_replace("\0","",$content);
if("x" . $content == "x") if("x" . $content == "x")
return; return;
}
$this->_outputStack(); $this->_outputStack();
$this->_content($content); $this->_content($content);
} }
...@@ -272,12 +281,16 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -272,12 +281,16 @@ class WBXMLEncoder extends WBXMLDefs {
* Outputs actual data * Outputs actual data
* *
* @access private * @access private
* @param string|resource $content
* @return * @return
*/ */
private function _content($content) { private function _content($content) {
$this->logContent($content); $this->logContent($content);
$this->outByte(WBXML_STR_I); $this->outByte(WBXML_STR_I);
$this->outTermStr($content); if (is_resource($content))
$this->outTermStream ($content);
else
$this->outTermStr($content);
} }
/** /**
...@@ -346,6 +359,28 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -346,6 +359,28 @@ class WBXMLEncoder extends WBXMLDefs {
fwrite($this->_outLog, chr(0)); fwrite($this->_outLog, chr(0));
} }
/**
* Outputs stream content base64 encoded with string terminator
*
* @param resource $stream
*
* @access private
* @return
*/
private function outTermStream($stream) {
$out_filter = stream_filter_append($this->_out, 'convert.base64-encode');
stream_copy_to_stream($stream, $this->_out);
stream_filter_remove($out_filter);
fwrite($this->_out, chr(0));
fseek($stream, 0, SEEK_SET);
$outLog_filter = stream_filter_append($this->_outLog, 'convert.base64-encode');
stream_copy_to_stream($stream, $this->_outLog);
stream_filter_remove($outLog_filter);
fwrite($this->_outLog, chr(0));
fseek($stream, 0, SEEK_SET);
}
/** /**
* Output attributes * Output attributes
* We don't actually support this, because to do so, we would have * We don't actually support this, because to do so, we would have
...@@ -470,7 +505,7 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -470,7 +505,7 @@ class WBXMLEncoder extends WBXMLDefs {
/** /**
* Logs content to ZLog * Logs content to ZLog
* *
* @param $content * @param string|resource $content
* *
* @access private * @access private
* @return * @return
...@@ -480,7 +515,14 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -480,7 +515,14 @@ class WBXMLEncoder extends WBXMLDefs {
return; return;
$spaces = str_repeat(" ", count($this->logStack)); $spaces = str_repeat(" ", count($this->logStack));
ZLog::Write(LOGLEVEL_WBXML,"O " . $spaces . $content); if (is_resource($content)) {
$stat = fstat($content);
fseek($content, 0, SEEK_SET); // fstat seeks to end of file to determine size!
ZLog::Write(LOGLEVEL_WBXML,"O " . $spaces . " <<< ".$stat['size']." bytes of base64 encoded data >>>");
}
else {
ZLog::Write(LOGLEVEL_WBXML,"O " . $spaces . $content);
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment