Commit fd2ebc3e authored by Manfred Kutas's avatar Manfred Kutas

ZP-1121 Output opaque data.

Released under the Affero GNU General Public License (AGPL) version 3.
parent a441242c
...@@ -162,20 +162,22 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -162,20 +162,22 @@ class WBXMLEncoder extends WBXMLDefs {
* *
* @param resource $stream * @param resource $stream
* @param boolean $asBase64 if true, the data will be encoded as base64, default: false * @param boolean $asBase64 if true, the data will be encoded as base64, default: false
* @param boolean $opaque if true, output the opaque data, default: false
* *
* @access public * @access public
* @return * @return
*/ */
public function contentStream($stream, $asBase64 = false) { public function contentStream($stream, $asBase64 = false, $opaque = false) {
if (!$asBase64) { // Do not append filters to opaque data as it might contain null char
if (!$asBase64 && !$opaque) {
stream_filter_register('replacenullchar', 'ReplaceNullcharFilter'); stream_filter_register('replacenullchar', 'ReplaceNullcharFilter');
$rnc_filter = stream_filter_append($stream, 'replacenullchar'); $rnc_filter = stream_filter_append($stream, 'replacenullchar');
} }
$this->_outputStack(); $this->_outputStack();
$this->_contentStream($stream, $asBase64); $this->_contentStream($stream, $asBase64, $opaque);
if (!$asBase64) { if (!$asBase64 && !$opaque) {
stream_filter_remove($rnc_filter); stream_filter_remove($rnc_filter);
} }
...@@ -286,9 +288,17 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -286,9 +288,17 @@ class WBXMLEncoder extends WBXMLDefs {
* @param boolean $asBase64 * @param boolean $asBase64
* @return * @return
*/ */
private function _contentStream($stream, $asBase64) { private function _contentStream($stream, $asBase64, $opaque) {
$stat = fstat($stream);
// write full stream, including the finalizing terminator to the output stream (stuff outTermStr() would do) // write full stream, including the finalizing terminator to the output stream (stuff outTermStr() would do)
if ($opaque) {
$this->outByte(self::WBXML_OPAQUE);
$this->outMBUInt($stat['size']);
}
else {
$this->outByte(self::WBXML_STR_I); $this->outByte(self::WBXML_STR_I);
}
if ($asBase64) { if ($asBase64) {
$out_filter = stream_filter_append($this->_out, 'convert.base64-encode'); $out_filter = stream_filter_append($this->_out, 'convert.base64-encode');
} }
...@@ -296,11 +306,12 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -296,11 +306,12 @@ class WBXMLEncoder extends WBXMLDefs {
if ($asBase64) { if ($asBase64) {
stream_filter_remove($out_filter); stream_filter_remove($out_filter);
} }
if (!$opaque) {
fwrite($this->_out, chr(0)); fwrite($this->_out, chr(0));
}
if ($this->log) { if ($this->log) {
// data is out, do some logging // data is out, do some logging
$stat = fstat($stream);
$this->logContent(sprintf("<<< written %d of %d bytes of %s data >>>", $written, $stat['size'], $asBase64 ? "base64 encoded":"plain")); $this->logContent(sprintf("<<< written %d of %d bytes of %s data >>>", $written, $stat['size'], $asBase64 ? "base64 encoded":"plain"));
} }
} }
...@@ -330,24 +341,39 @@ class WBXMLEncoder extends WBXMLDefs { ...@@ -330,24 +341,39 @@ class WBXMLEncoder extends WBXMLDefs {
} }
/** /**
* Outputs a string table * Output the multibyte integers to the stream.
*
* A multi-byte integer consists of a series of octets,
* where the most significant bit is the continuation flag
* and the remaining seven bits are a scalar value.
* The octets are arranged in a big-endian order,
* eg, the most significant seven bits are transmitted first.
*
* @see https://www.w3.org/1999/06/NOTE-wbxml-19990624/#_Toc443384895
* *
* @param $uint * @param int $uint
* *
* @access private * @access private
* @return * @return void
*/ */
private function outMBUInt($uint) { private function outMBUInt($uint) {
while(1) { if ($uint == 0x0) {
return $this->outByte($uint);
}
$out = '';
for ($i = 0; $uint != 0; $i++) {
$byte = $uint & 0x7f; $byte = $uint & 0x7f;
$uint = $uint >> 7; $uint = $uint >> 7;
if($uint == 0) { if ($i == 0) {
$this->outByte($byte); $out = chr($byte) . $out;
break; }
} else { else {
$this->outByte($byte | 0x80); $out = chr($byte | 0x80) . $out;
} }
} }
fwrite($this->_out, $out);
} }
/** /**
......
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