numerics.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. include_once("oracles/base.php");
  3. class numerics extends oracle {
  4. public $info = [
  5. "name" => "numeric base conversion"
  6. ];
  7. public function check_query($q) {
  8. if (str_contains($q, " ")) {
  9. return false;
  10. }
  11. $q = strtolower($q);
  12. $profiles = [
  13. ["0x", str_split("0123456789abcdef")],
  14. ["", str_split("1234567890")],
  15. ["b", str_split("10")]
  16. ];
  17. foreach ($profiles as $profile) {
  18. $good = true;
  19. $good &= str_starts_with($q, $profile[0]);
  20. $nq = substr($q, strlen($profile[0]));
  21. foreach (str_split($nq) as $c) {
  22. $good &= in_array($c, $profile[1]);
  23. }
  24. if ($good) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. public function generate_response($q) {
  31. $n = 0;
  32. if (str_starts_with($q, "0x")) {
  33. $nq = substr($q, strlen("0x"));
  34. $n = hexdec($nq);
  35. }
  36. elseif (str_starts_with($q, "b")) {
  37. $nq = substr($q, strlen("b"));
  38. $n = bindec($nq);
  39. }
  40. else {
  41. $n = (int)$q;
  42. }
  43. return [
  44. "decimal (base 10)" => (string)$n,
  45. "hexadecimal (base 16)" => "0x".(string)dechex($n),
  46. "binary (base 2)" => "b".(string)decbin($n),
  47. "" => "binary inputs should be prefixed with 'b', hex with '0x'."
  48. ];
  49. }
  50. }
  51. ?>