161 lines
6.9 KiB
PHP
161 lines
6.9 KiB
PHP
<?php
|
||
|
||
function str_replace_char_to_2xChar($to,$str,$i)
|
||
{
|
||
return substr($str,0,$i).$to.substr($str,$i+1);
|
||
}
|
||
|
||
$code = $_POST["codeSource"];
|
||
|
||
$function2hexArray = array(
|
||
'BG-None' => 'f778',
|
||
'LabelOff' => 'f7d4',
|
||
'Plot/Line-Color ' => 'f999',
|
||
'Black ' => 'f99b',
|
||
'SketchThin ' => 'f9f3',
|
||
'F-Line ' => 'f7a7',
|
||
'PlotOn ' => 'f7a8',
|
||
'(' => '28',
|
||
')' => '29',
|
||
'AxesOff' => 'f7d2',
|
||
'<' => '3c',
|
||
'>' => '3e',
|
||
'Ù' => '0d',
|
||
'ViewWindow ' => 'eb',
|
||
',' => '2c',
|
||
'ã' => '0e',
|
||
'Do' => 'f70a',
|
||
'Cls' => 'd1',
|
||
'Text ' => 'f7a5',
|
||
'"' => '22',
|
||
'Getkey' => '7f8f',
|
||
'-' => '99',
|
||
'Á' => 'b9',
|
||
'+' => '89',
|
||
'Frac ' => 'b6',
|
||
'=' => '3d',
|
||
'×' => '13',
|
||
'LpWhile ' => 'f70b',
|
||
'È' => '11',
|
||
' And ' => '7fb0',
|
||
' Or ' => '7fb1',
|
||
'Isz ' => 'e9',
|
||
'If ' => 'f700',
|
||
'Then ' => 'f701',
|
||
'.' => '2e',
|
||
'IfEnd' => 'f703',
|
||
'AxesOn' => 'f7c2',
|
||
'ClrGraph' => 'f719',
|
||
' ' => '20',
|
||
"\n" => '0d',
|
||
"\r" => '0d');
|
||
//Liste des fonctions
|
||
$functionsArray = array_keys($function2hexArray);
|
||
//Get the max length of all function
|
||
$maxFuncLength = max(array_map('strlen',$functionsArray));
|
||
//Transform each char in hex
|
||
$output = '';
|
||
$pos = 0;
|
||
while(isset($code[$pos]))
|
||
{
|
||
$char = $code[$pos];
|
||
//Take some "futur" chars
|
||
$after = substr($code, $pos,$maxFuncLength);
|
||
//Test if there is function in theses chars
|
||
$functionPosArray = array_map('strpos', array_fill(0, count($functionsArray),$after),$functionsArray); //It return an array with a cell for each function if a cell value is 0 (position 0) then the function is here
|
||
$key = array_search(0,$functionPosArray,true); //Find cell where value == 0
|
||
if($key !== false) //If there is a function here
|
||
{
|
||
//put the hex value in the output
|
||
$output .= $function2hexArray[$functionsArray[$key]];
|
||
//jump after the function
|
||
$pos += strlen($functionsArray[$key]);
|
||
}
|
||
else//just add the hex char
|
||
{
|
||
$output .= bin2hex($char);
|
||
$pos++;
|
||
}
|
||
}
|
||
//init
|
||
$nbrPrgm = 0;
|
||
$textSize = 0;
|
||
$prgms = "";
|
||
//Creation du bloc prgm (ce bloc peut être répété pour ajouter plusieurs programmes)
|
||
//contenu
|
||
$title = $_POST["nomProgramme"]; //Entre 1 et 8 caractères en caractères normaux
|
||
$contenuPrgm = $output; //en hexadecimal
|
||
//$contenuPrgm = "4142434445464748494a4b4c4d4e4f"; //en hexadecimal
|
||
//gestion du titre
|
||
$titleHex = bin2hex($title);
|
||
$titleHex = str_pad($titleHex, 16, '0', STR_PAD_RIGHT);
|
||
//prgm content
|
||
$text ="00000000000000000000".$contenuPrgm;
|
||
//calcul taille texte
|
||
$tailleText = strlen($text)/2;
|
||
$tailleText= $tailleText + (4-$tailleText%4);
|
||
$textSize += $tailleText+44;
|
||
$tailleTextHex = str_pad(dechex($tailleText), 8, '0', STR_PAD_LEFT);
|
||
//arrondi la taille a 4
|
||
$text = str_pad($text, $tailleText*2, '0', STR_PAD_RIGHT);
|
||
//validation prgm
|
||
$prgms .= "50524f4752414d0000000000000000000000000173797374656d0000".$titleHex."01".$tailleTextHex."000000".$text;
|
||
$nbrPrgm++;
|
||
|
||
|
||
//G1M generating
|
||
//filesize calculation
|
||
// $filesize = 32 + $textSize + 44*$nbrPrgm;
|
||
$filesize = 32 + $textSize;
|
||
if($filesize > 0xFFFFFFFF)
|
||
{
|
||
error("File too big.", "Fichier trop gros.");
|
||
}
|
||
//OFFSET 0xE : last filesize byte + 65(0x41) and NOT (inverted)
|
||
$offset0xE = $filesize + 0x41;
|
||
$offset0xE = 0xFFFFFFFF - $offset0xE;
|
||
$offset0xE = dechex($offset0xE);
|
||
$offset0xE = str_pad($offset0xE, 2, '0', STR_PAD_LEFT);
|
||
$offset0xE = substr($offset0xE, -2); // take the last byte
|
||
//OFFSET 0x10 : filesize NOT (inverted)
|
||
$offset0x10 = 0xFFFFFFFF - $filesize;
|
||
$offset0x10 = dechex($offset0x10);// to hex
|
||
$offset0x10 = str_pad($offset0x10, 8, '0', STR_PAD_LEFT);
|
||
//OFFSET 0x14 : last filesize byte + 184(0xB8) and NOT (inverted)
|
||
$offset0x14 = $filesize + 0xB8;
|
||
$offset0x14 = 0xFFFFFFFF - $offset0x14;
|
||
$offset0x14 = dechex($offset0x14);
|
||
$offset0x14 = str_pad($offset0x14, 2, '0', STR_PAD_LEFT);
|
||
$offset0x14 = substr($offset0x14, -2);
|
||
//OFFSET 0x14 : last filesize byte + 184(0xB8) and NOT (inverted)
|
||
$offset0x1E = $nbrPrgm;
|
||
$offset0x1E = 0xFFFF - $offset0x1E;
|
||
$offset0x1E = dechex($offset0x1E);
|
||
$offset0x1E = str_pad($offset0x1E, 4, '0', STR_PAD_LEFT);
|
||
//making
|
||
if ($_POST["ext"] == '1')
|
||
{
|
||
$fileContent = pack('H*',
|
||
"aaacbdaf90889a8dceffefffefff".$offset0xE."fe".
|
||
$offset0x10.$offset0x14."000000000000000000".$offset0x1E.
|
||
$prgms);
|
||
}
|
||
else
|
||
{
|
||
$fileContent = pack('H*',
|
||
"aaacbdaf90889a8d8affefffefff".$offset0xE."fe".
|
||
$offset0x10.$offset0x14."000000000000000000".$offset0x1E.
|
||
$prgms);
|
||
}
|
||
//sending
|
||
header('Content-Type: application/octet-stream');
|
||
if ($_POST["ext"] == '1')
|
||
header('Content-Disposition: attachment;filename="'.$title.'.g1r"');
|
||
else
|
||
header('Content-Disposition: attachment;filename="'.$title.'.g3m"');
|
||
header('Cache-Control: max-age=0');
|
||
$fh = fopen('php://output', 'wb');
|
||
fwrite($fh, $fileContent);
|
||
fclose($fh);
|
||
|
||
?>
|