Skip to content

Instantly share code, notes, and snippets.

@asgrim
Created July 30, 2012 07:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asgrim/3205498 to your computer and use it in GitHub Desktop.
Save asgrim/3205498 to your computer and use it in GitHub Desktop.
Crazy Morse-encoded PHP executor. Dirty and quick, no apologies for this code.
<?php
require 'morselib.php';
$morse = new morselib();
eval($morse->decode(file_get_contents("site.morse")));
<?php
/**
* Standard morse code (as per translation table)
* Plus characters not in the standard set are enclosed in ------ and ......
* with the ascii character code in between
*
* @author james
*
*/
class morselib
{
private $_translation_table;
public function __construct()
{
$this->_translation_table = array(
".-" => "a",
"-..." => "b",
"-.-." => "c",
"-.." => "d",
"." => "e",
"..-." => "f",
"--." => "g",
"...." => "h",
".." => "i",
".-" => "j",
"-.-" => "k",
".-.." => "l",
"--" => "m",
"-." => "n",
"---" => "o",
".--." => "p",
"--.-" => "q",
".-." => "r",
"..." => "s",
"-" => "t",
"..-" => "u",
"...-" => "v",
".--" => "w",
"-..-" => "x",
"-.--" => "y",
"--.." => "z",
"-----" => "0",
".----" => "1",
"..---" => "2",
"...--" => "3",
"....-" => "4",
"....." => "5",
"-...." => "6",
"--..." => "7",
"---.." => "8",
"----." => "9",
".-.-.-" => ".",
"--..--" => ",",
"..--.." => "?",
);
}
public function encode($text)
{
// Reverse the translation table
$rx_translation = array_flip($this->_translation_table);
$output = "";
for ($i = 0; $i < strlen($text); $i++)
{
if (isset($rx_translation[$text[$i]]))
{
$output .= "{$rx_translation[$text[$i]]} ";
}
else
{
$code = (string)ord($text[$i]);
$chr = "";
for ($j = 0; $j < strlen($code); $j++)
{
$chr .= "{$rx_translation[$code[$j]]} ";
}
$chr = trim($chr);
$output .= "------ {$chr} ...... ";
}
}
return $output;
}
public function decode($text)
{
$bits = explode(" ", str_replace("\n", " ", $text));
$output = "";
$in_ascii_mode = false;
$ascii = "";
foreach($bits as $bit_id => $bit)
{
if ($bit == "------")
{
$in_ascii_mode = true;
$ascii = "";
}
else if ($bit == "......")
{
$in_ascii_mode = false;
$chr = chr((int)$ascii);
$output .= $chr;
}
else if ($in_ascii_mode)
{
$ascii .= $this->_translation_table[$bit];
}
else if (isset($this->_translation_table[$bit]))
{
$output .= $this->_translation_table[$bit];
}
else
{
die("Failed on character {$bit_id} [$bit].");
}
}
return $output;
}
}
. -.-. .... --- ------ ...-- ..--- ...... ------ ...-- ....- ...... ------ -....
----- ...... .... - -- .-.. ------ -.... ..--- ...... ------ ----. ..--- ......
-. ------ ----. ...... ------ -.... ----- ...... .... . ------ ----. --...
...... -.. ------ -.... ..--- ...... ------ ----. ..--- ...... -. ------ ----.
...... ------ ----. ...... ------ -.... ----- ...... - .. - .-.. . ------ -....
..--- ...... ------ ---.. ----- ...... ------ ----. --... ...... --. . ------
...-- ..--- ...... .. -. ------ ...-- ..--- ...... -- --- .-. ... . ------ ...--
..--- ...... -.-. --- -.. . ------ -.... ----- ...... ------ ....- --... ......
- .. - .-.. . ------ -.... ..--- ...... ------ ----. ..--- ...... -. ------
----. ...... ------ -.... ----- ...... ------ ....- --... ...... .... . ------
----. --... ...... -.. ------ -.... ..--- ...... ------ ----. ..--- ...... -.
------ ----. ...... ------ -.... ----- ...... -... --- -.. -.-- ------ -....
..--- ...... ------ ----. ..--- ...... -. ------ ----. ...... ------ ----.
...... ------ -.... ----- ...... .... .---- ------ -.... ..--- ...... ------
---.. ....- ...... .... .. ... ------ ...-- ..--- ...... .-- ------ ----. --...
...... ... ------ ...-- ..--- ...... .-- .-. .. - - . -. ------ ...-- ..---
...... .. -. ------ ...-- ..--- ...... -- --- .-. ... . ------ ...-- ..---
...... -.-. --- -.. . ------ ...-- ...-- ...... ------ -.... ----- ...... ------
....- --... ...... .... .---- ------ -.... ..--- ...... ------ ----. ..---
...... -. ------ ----. ...... ------ -.... ----- ...... ------ ....- --...
...... -... --- -.. -.-- ------ -.... ..--- ...... ------ ----. ..--- ...... -.
------ -.... ----- ...... ------ ....- --... ...... .... - -- .-.. ------ -....
..--- ...... ------ ----. ..--- ...... -. ------ ...-- ....- ...... ------ .....
----. ......
@diamondsea
Copy link

The code for "J" is wrong on morselib.php:27. It should be ".---"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment