Decorates a stream with a sha256 linear hashing stream.

@param Stream $stream Stream to decorate.
@param array  $data   Part data to augment with the hash result.

@return Stream
507138
private function decorateWithHashes(Stream $stream, array &$data)
    {
        // Decorate source with a hashing stream
        $hash = new PhpHash('sha256');
        return new HashingStream($stream, $hash, function ($result) use (&$data) {
            $data['ContentSHA256'] = bin2hex($result);
        });
    }
15983
public function stream_write($data)
    {
        if ( ! $this->stream instanceof WritableStream) {
            return 0;
        }

        try {
            return $this->stream->writeBytes($data);
        } catch (Exception $e) {
            trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
            return false;
        }
    }
229263
public function readByteStream(Stream $stream)
    {
        $length = $this->readVarint($stream);
        $value  = $stream->readStream($length);

        return $value;
    }
1191500
public function makeHashable(Data $data): ?HashableFormat
    {
        if (!$data->isHashable()) {
            return null;
        }

        return HashableFormat::make($data);
    }
17239
public function createStream(string $content = ''): StreamInterface
    {
        $resource = fopen('php://temp', 'rw+');
        $stream = new Stream($resource);
        $stream->write($content);

        return $stream;
    }
503142
public function setStream($stream = null)
    {
        // String passed in? Try converting it to a stream
        if (is_string($stream)) {
            $stream = @fopen($stream, 'a');
        }

        // Make sure the stream is a resource
        if (!is_resource($stream)) {
            throw new \InvalidArgumentException('Invalid Stream');
        }

        // Store it
        $this->stream = $stream;

        // Afford chaining
        return $this;
    }
381679
private function parseHashList(Stream $stream)
    {
        $hash = [];

        do {
            $key = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();

            $stream->expects(Token::TYPE_SYMBOL, ':');

            $hash[$key] = $this->parseValue($stream);
        } while ($stream->current()->equals(Token::TYPE_SYMBOL, ',') && $stream->expects(Token::TYPE_SYMBOL, ','));

        return $hash;
    }
522867
public function setStream(StreamInterface $stream = null)
    {
        $this->stream = $stream;
        $this->decodedStream = null;
        $this->charsetStream = null;
    }
531343
private function parseValue(Stream $stream)
    {
        if ($stream->current()->is(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)) {
            return new ValueNode($stream->expects(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)->getValue());
        }

        if ($stream->current()->is(Token::TYPE_NUMBER)) {
            $number = $stream->expects(Token::TYPE_NUMBER)->getValue();

            return new ValueNode(strpos($number, '.') === false ? intval($number) : floatval($number));
        }

        if ($stream->current()->is(Token::TYPE_CONSTANT)) {
            $constant = $stream->expects(Token::TYPE_CONSTANT)->getValue();

            switch ($constant) {
                case 'true':
                    return new ValueNode(true);

                case 'false':
                    return new ValueNode(false);

                case 'null':
                    return new ValueNode(null);

                default:
                    throw new ParserException(sprintf('Unexpected constant "%s" found', $constant));
            }
        }

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '[')) {
            $stream->expects(Token::TYPE_SYMBOL, '[');

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, ']')) {
                $array = $this->parseList($stream);
            } else {
                $array = [];
            }

            $stream->expects(Token::TYPE_SYMBOL, ']');

            return new ValueNode($array);
        }

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '{')) {
            $stream->expects(Token::TYPE_SYMBOL, '{');

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, '}')) {
                $hash = $this->parseHashList($stream);
            } else {
                $hash = [];
            }

            $stream->expects(Token::TYPE_SYMBOL, '}');

            return new ValueNode($hash);
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'new')) {
            return $this->parseClass($stream);
        }

        throw new ParserException(sprintf('Can\'t parse the stream "%s".', $stream->humanize()));
    }
501098
private function parseTypeAlias(Stream $stream)
    {
        $alias = $this->parseTypeIdentifier($stream);

        while ($stream->current()->equals(Token::TYPE_SYMBOL, '[')) {
            $stream->expects(Token::TYPE_SYMBOL, '[');
            $stream->expects(Token::TYPE_SYMBOL, ']');

            $alias = sprintf('%s[]', $alias);
        }

        return $alias;
    }
632214
public function postStream($url, $data, $files = [], $header = [])
    {
        if (!is_array($files)) {
            $file = $files;
            $files = ['file' => $file];
        }
        foreach ($files as $key => $item) {
            if ($streamData = FileUtil::readStream($item)) {
                $data["{$key}\";filename=\"{$key}"] = $streamData;
            }
        }
        return $this->exec($url, $data, 'post', $header);
    }
1014470
private function parseClass(Stream $stream)
    {
        $stream->expects(Token::TYPE_KEYWORD, 'new');

        $class = [$stream->expects(Token::TYPE_IDENTIFIER)->getValue()];

        while ($stream->current()->equals(Token::TYPE_SYMBOL, '\\')) {
            $stream->expects(Token::TYPE_SYMBOL, '\\');

            $class[] = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
        }

        $class = implode('\\', $class);

        $arguments = [];

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '(')) {
            $stream->expects(Token::TYPE_SYMBOL, '(');

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, ')')) {
                $arguments = $this->parseList($stream);
            }

            $stream->expects(Token::TYPE_SYMBOL, ')');
        }

        return new ClassNode($class, $arguments);
    }
674508
public function parse($stream)
    {
        if ($this->format->isEachLine()) {
            $iterator = new LineStreamIterator(
                $stream,
                [
                    LineStreamIterator::OPTION_ENDING         => "\n",
                    LineStreamIterator::OPTION_IGNORE_BLANK   => $this->format->isIgnoreBlankLines(),
                    LineStreamIterator::OPTION_INCLUDE_ENDING => false,
                ]
            );
            $iterator->setIgnoreBlank($this->format->isIgnoreBlankLines());
            return new MapIterator($iterator, [$this, 'decodeJson']);
        } else {
            $json = $this->decodeJson(stream_get_contents($stream));
            if (is_array($json)) {
                return new ArrayIterator($json);
            } else {
                throw new RuntimeException("Expecting a json array to parse, unknown format detected");
            }
        }
    }
889067
public function toStream($stream)
    {
        $bytesWritten = 0;
        foreach ($this->getChunkCursor() as $chunk) {
            $bytesWritten += fwrite($stream, $chunk['data']->getData());
        }
        return $bytesWritten;
    }
1155919
public function secureHash($data) {
        $salt = $this->_getCryptKey();
        return hash(self::HASH_ALGORITHM, sprintf('%s:%s', $salt, $data));
    }
969367
public function FromBytes($stream, $offset) // ret <= 0 - error
	{
		$moved = 0;
		$vi = 0;
		$moved += $parsed = ProtoBuf_Bytes2Varint($stream, $offset + $moved, $vi);
		if ($parsed <= 0)
			return $parsed;

		//echo "vi $vi\n";
		$this->wt = ($vi & 0x07);
		$this->fn = ($vi >> 3);
		//echo "field wt " . $this->wt . " fn " . $this->fn . "\n";

		switch ($this->wt)
		{
			case ProtoBuf_Field::ProtoBuf_WireType_Varint: // TODO: in case we need this information, create get function
				$this->data = 0;
				$moved += $parsed = ProtoBuf_Bytes2Varint($stream, $offset + $moved, $this->data);
				break;
			case ProtoBuf_Field::ProtoBuf_WireType_64bit:
				$this->data = substr($stream, $offset + $moved, 8);
				$high4 = ProtoBuf_Bytes2Fixed(substr($this->data, 4), 4);
				if ($high4 == 0)
				{
					$this->fixed64_is_32bit = true;
					$this->data = ProtoBuf_Bytes2Fixed($this->data, 4);
				}
				else
				{
					$this->fixed64_is_32bit = false;
					$this->data = ProtoBuf_Bytes2Fixed($this->data, 8);
				}
				$moved += 8;
				break;
			case ProtoBuf_Field::ProtoBuf_WireType_LengthDelimited:
				$moved += $parsed = ProtoBuf_Bytes2Varint($stream, $offset + $moved, $vi);
				if ($parsed <= 0)
					return $parsed;
				$this->data = substr($stream, $offset + $moved, $vi);
				$moved += $vi;
				break;
			/*case ProtoBuf_Field::ProtoBuf_WireType_StartGroup: // TODO: in case we need this information, create get function
				break;
			case ProtoBuf_Field::ProtoBuf_WireType_EndGroup: // TODO: in case we need this information, create get function
				break;*/
			case ProtoBuf_Field::ProtoBuf_WireType_32bit:
				$this->data = substr($stream, $offset + $moved, 4);
				$this->data = ProtoBuf_Bytes2Fixed($this->data, 4);
				$moved += 4;
				break;
			default:
				die("[lib_protobuf.inc] Unrecognized protobuf wire type '" . $this->wt . "'\n");
				break;
		}

		return $moved;
	}
337919
public function readZigzag(Stream $stream)
    {
        $number = $this->readVarint($stream);
        $zigzag = ($number >> 1) ^ (-($number & 1));

        return $zigzag;
    }