summaryrefslogtreecommitdiff
path: root/oracles/numerics.php
blob: 3d428e39ab7a6fd93e6d3c12afacbceb663c9da0 (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
<?php
include_once("oracles/base.php");
class numerics extends oracle {
	public $info = [
		"name" => "numeric base conversion"
	];
	public function check_query($q) {
		if (str_contains($q, " ")) {
			return false;
		}

		$q = strtolower($q);

		$profiles = [
			["0x", str_split("0123456789abcdef")],
			["", str_split("1234567890")],
			["b", str_split("10")]
		];

		foreach ($profiles as $profile) {
			$good = true;
			$good &= str_starts_with($q, $profile[0]);
			$nq = substr($q, strlen($profile[0]));
			foreach (str_split($nq) as $c) {
				$good &= in_array($c, $profile[1]);
			}
			if ($good) {
				return true;
			}
		}
		return false;
	}
	public function generate_response($q) {
		$n = 0;
		if (str_starts_with($q, "0x")) {
			$nq = substr($q, strlen("0x"));
			$n = hexdec($nq);
		}
		elseif (str_starts_with($q, "b")) {
			$nq = substr($q, strlen("b"));
			$n = bindec($nq);
		}
		else {
			$n = (int)$q;
		}
		return [
			"decimal (base 10)" => (string)$n,
			"hexadecimal (base 16)" => "0x".(string)dechex($n),
			"binary (base 2)" => "b".(string)decbin($n),
			"" => "binary inputs should be prefixed with 'b', hex with '0x'."
		];
	}
}
?>