aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_external/3rdparty/icewind/streams/src/HashWrapper.php
blob: 7fb739e0d49be3f88a3ad4e80904c06f2417378f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
 * SPDX-FileCopyrightText: 2019 Roeland Jago Douma <roeland@famdouma.nl>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Icewind\Streams;

abstract class HashWrapper extends Wrapper {

	/**
	 * @var callable|null
	 */
	private $callback;

	/**
	 * @var resource|\HashContext
	 */
	private $hashContext;

	/**
	 * Wraps a stream to make it seekable
	 *
	 * @param resource $source
	 * @param string $hash
	 * @param callable $callback
	 * @return resource|false
	 *
	 * @throws \BadMethodCallException
	 */
	public static function wrap($source, $hash, $callback) {
		$context = [
			'hash'     => $hash,
			'callback' => $callback,
		];
		return self::wrapSource($source, $context);
	}

	public function dir_opendir($path, $options) {
		return false;
	}

	public function stream_open($path, $mode, $options, &$opened_path) {
		$context = $this->loadContext();
		$this->callback = $context['callback'];
		$this->hashContext = hash_init($context['hash']);
		return true;
	}

	protected function updateHash($data) {
		hash_update($this->hashContext, $data);
	}

	public function stream_close() {
		$hash = hash_final($this->hashContext);
		if ($this->hashContext !== false && is_callable($this->callback)) {
			call_user_func($this->callback, $hash);
		}
		return parent::stream_close();
	}
}