Last active
March 12, 2021 13:30
-
-
Save divinity76/d614a2d9a094ea32e4ec6b22be5b3702 to your computer and use it in GitHub Desktop.
files to c++ vector<tuple<string,string>> converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
$auto_ldd = true; | |
$add_template_cpp = true; | |
$files = [ | |
"plink.exe" => '/cygdrive/c/Users/hansh/Downloads/plink.exe', | |
]; | |
if (0) { | |
$files = [ | |
"sh" => '/bin/sh' | |
]; | |
} | |
if ($auto_ldd) { | |
$auto_ldd_blacklist = [ | |
"ntdll.dll", | |
"KERNEL32.DLL", | |
"KERNELBASE.dll", | |
"wow64.dll", | |
"wow64win.dll", | |
]; | |
$ldd_files = []; | |
foreach ($files as $file) { | |
foreach (ldd($file) as $ldd) { | |
if (in_array(basename($ldd), $auto_ldd_blacklist, true)) { | |
continue; | |
} | |
$ldd_files[] = $ldd; | |
} | |
} | |
foreach ($ldd_files as $ldd_file) { | |
$files[basename($ldd_file)] = $ldd_file; | |
} | |
} | |
if ($add_template_cpp) { | |
echo <<<'STR' | |
#include <iostream> | |
#include <vector> | |
#include <tuple> | |
STR; | |
} | |
echo (cpptar($files)); | |
echo "\n"; | |
if ($add_template_cpp) { | |
echo <<<'STR' | |
int main(int argc, char *argv[]){ | |
for(auto t: files){ | |
auto [filename, content] = t; | |
std::cout << filename << ":" << content << std::endl; | |
} | |
for(auto t: files){ | |
auto [filename, content] = t; | |
std::cout << "recap filename: "<< filename << std::endl; | |
} | |
} | |
STR; | |
} | |
function ldd(string $bin): array | |
{ | |
$cmd = 'ldd ' . escapeshellarg($bin) . " 2>&1"; | |
$out = shell_exec($cmd); | |
if (empty($out)) { | |
return []; | |
} | |
$lines = array_filter(array_map("trim", explode("\n", trim($out))), 'strlen'); | |
$intersting = []; | |
foreach ($lines as $line) { | |
$foo = explode(" => ", $line, 2); | |
if (count($foo) !== 2) { | |
continue; | |
} | |
$foo = $foo[1]; | |
$lastspace = strrpos($foo, " "); | |
if ($lastspace !== false) { | |
$foo = substr($foo, 0, $lastspace); | |
} | |
$foo = realpath($foo); | |
if (false === $foo) { | |
die("REALPATH FAILED"); | |
continue; | |
} | |
$intersting[] = $foo; | |
} | |
return $intersting; | |
} | |
function cpp_string(string $str): string | |
{ | |
return "std::string(" . cpp_quote($str) . "," . strlen($str) . ")"; | |
} | |
function cpp_quote(string $str): string | |
{ | |
$ret = ""; | |
$translations = [ | |
"\r" => "\\r", | |
"\n" => "\\n", | |
"\t" => "\\t", | |
"\\" => "\\\\", | |
"'" => "\\'", | |
"\"" => "\\\"", | |
"?" => "\\?" | |
]; | |
$whitelist = [ | |
'(', | |
')', | |
'[', | |
']', | |
'{', | |
'}', | |
',', | |
'.', | |
'=', | |
'=', | |
'-', | |
'_', | |
":", | |
";", | |
",", | |
" ", | |
"\$", | |
"+", | |
"-", | |
"/", | |
"*", | |
"!", | |
"|", | |
"@", | |
"<", | |
">" | |
// probably lots more should be on this list. | |
]; | |
for ($i = 0, $imax = strlen($str); $i < $imax; ++ $i) { | |
$chr = $str[$i]; | |
if (in_array($chr, $whitelist, true)) { | |
$ret .= $chr; | |
} elseif (ctype_alnum($chr)) { | |
$ret .= $chr; | |
} elseif (isset($translations[$chr])) { | |
$ret .= $translations[$chr]; | |
} else { | |
// var_dump($chr);die(); | |
$ret .= "\\x" . strtoupper(bin2hex($chr)) . '""'; | |
} | |
} | |
return '"' . $ret . '"'; | |
} | |
function cpptar(array $files): string | |
{ | |
$ret = ""; | |
foreach ($files as $filename => $filepath) { | |
$ret .= "{" . cpp_string($filename) . "," . cpp_string(file_get_contents($filepath)) . "},\n"; | |
} | |
if (! empty($ret)) { | |
$ret = substr($ret, 0, - strlen(",\n")) . "\n"; | |
} | |
$ret = "const std::vector<std::tuple<std::string,std::string>> files = {\n" . $ret . "\n};\n"; | |
return $ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment