Avril 5 years ago
commit d2e130a4ad
Signed by: flanchan
GPG Key ID: 284488987C31F630

5
.gitignore vendored

@ -0,0 +1,5 @@
*~
sfexec
bin/
obj/
sha256_literal/

@ -0,0 +1,9 @@
[package]
name = "generator-native"
version = "0.1.0"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

@ -0,0 +1,162 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace generator
{
[Flags]
enum Mode
{
None = 0,
Silent = 1<<0,
Execute = 1<<1,
}
class Program
{
static string execstr=null;
static string output = "file.h";
static string[] files = null;
static Mode ParseArgs(Taker<string> args)
{
List<string> f = new List<string>();
Mode mode = Mode.None;
bool look=true;
while(args.TryTake(out var arg0))
{
switch(arg0)
{
case "-s" when look:
mode |= Mode.Silent;
break;
case "-e" when look:
mode |= Mode.Execute;
if(args.TryTake(out var exec))
execstr = exec;
else
throw new InvalidOperationException("-e must be given a string");
break;
case "-o" when look:
if(!args.TryTake(out output))
throw new InvalidOperationException("-o must be given an output");
break;
case "-" when look:
look =false;
break;
default:
f.Add(arg0);
break;
}
}
files = f.ToArray();
return mode;
}
static void Main(string[] args)
{
Mode mode;
try {
mode = ParseArgs(new Taker<string>(args));
} catch(Exception e)
{
Console.WriteLine("Error: "+e.Message);
return;
}
if(files.Length<1)
{
Console.WriteLine("Usage: generator [-e <exec str>] [-s] [-] <files...>");
return;
}
Console.WriteLine("Writing to "+output+"...");
using(var fs = new FileStream(output, FileMode.Create))
{
using(var sw = new StreamWriter(fs)) {
if(mode.HasFlag(Mode.Silent))
sw.WriteLine("#define SILENT");
sw.WriteLine($"constexpr const int DATA_COUNT = {files.Length};");
if(mode.HasFlag(Mode.Execute)) {
var exec_str = $"\"{CEscape(execstr)}\"";
sw.WriteLine($"constexpr const char* const DATA_EXEC_AFTER = {exec_str};");
sw.WriteLine($"static constexpr auto DATA_EXEC_AFTER_HASH = {exec_str}_sha256;");
}
else {
sw.WriteLine("constexpr const char* const DATA_EXEC_AFTER = nullptr;");
sw.WriteLine("static constexpr auto DATA_EXEC_AFTER_HASH = \"unbound\"_sha256;");
}
List<long> sizes= new List<long>();
sw.WriteLine("constexpr const unsigned char DATA[] = {");
foreach(var str in files)
{
using(var ifs = new FileStream(str, FileMode.Open, FileAccess.Read))
{
Console.Write(" + "+str);
try {
sizes.Add(WriteFile(sw, ifs));
sw.WriteLine();
Console.WriteLine(" OK");
} catch(Exception ex) {
Console.WriteLine(" FAILED: "+ex.Message);
}
}
}
sw.WriteLine("};");
Console.WriteLine("Adding lengths");
sw.WriteLine("constexpr const long DATA_LENGTHS[DATA_COUNT] = {");
foreach(var len in sizes)
sw.Write($"\t{len}ll,");
sw.WriteLine("\n};");
Console.WriteLine("Adding names");
sw.WriteLine("constexpr const char* const DATA_NAMES[DATA_COUNT] = {");
foreach(var n in files)
sw.Write("\t\""+CEscape(n.Split(Path.DirectorySeparatorChar)[^1])+"\",");
sw.WriteLine("};");
}
}
}
static long WriteFile(StreamWriter to, Stream from)
{
int rd;
long len=0;
while( (rd = from.ReadByte()) >= 0)
{
to.Write($"0x{((byte)rd):x2}, ");
len+=1;
}
return len;
}
static string CEscape(string inp)
{
return inp.Replace("\\", @"\\")
.Replace("\"", "\\\"");
}
}
class Taker<T>
{
readonly IEnumerator<T> iter;
public Taker(IEnumerable<T> input)
{
iter =input.GetEnumerator();
}
public bool TryTake(out T value)
{
if(!iter.MoveNext()){
value=default;
return false;
}
value = iter.Current;
return true;
}
}
}

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

@ -0,0 +1,3 @@
#!/bin/bash
git clone https://github.com/aguinet/sha256_literal

@ -0,0 +1 @@
../sha256_literal/sha256.h

@ -0,0 +1 @@
../sha256_literal/sha256_literal.h

@ -0,0 +1,150 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <filesystem>
#include <string>
#include <random>
#include <vector>
#include <regex>
#pragma GCC diagnostic ignored "-Wattributes"
#include <sha256_literal.h>
#include <sha256.h>
using namespace std;
namespace fs = std::filesystem;
string get_uuid() {
static random_device dev;
static mt19937 rng(dev());
uniform_int_distribution<int> dist(0, 15);
const char *v = "0123456789abcdef";
const bool dash[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
string res;
for (int i = 0; i < 16; i++) {
if (dash[i]) res += "-";
res += v[dist(rng)];
res += v[dist(rng)];
}
return res;
}
// DATA
// DATA_LENGTHS
// DATA_NAMES
// DATA_COUNT
// DATA_EXEC_AFTER
// DATA_EXEC_AFTER_HASH
#include "file.h"
bool verify_hash()
{
return (!DATA_EXEC_AFTER || sha256::compute((const uint8_t*)DATA_EXEC_AFTER, strlen(DATA_EXEC_AFTER)) == DATA_EXEC_AFTER_HASH);
}
string arg_vec_int(int i)
{
stringstream ss;
ss << "%arg\\[" << i << "\\]";
return ss.str();
}
string get_exec_str(vector<string> args, string args_full, string loc)
{
string str(DATA_EXEC_AFTER);
str = regex_replace(str, std::regex("%location"), loc);
str = regex_replace(str, std::regex("%argc"), std::to_string(args.size()));
str = regex_replace(str, std::regex("%args"), args_full);
for(int i=0;i<args.size();i++)
{
str = regex_replace(str, std::regex(arg_vec_int(i)), args[i]);
}
return str;
}
string arg_strings(int argc,char** argv, vector<string>& output)
{
stringstream ss;
for(int i=1;i<argc;i++)
{
auto argi = string(argv[i]);
ss << argi;
if(i!=argc-1)
ss << ' ';
output.push_back(argi);
}
return ss.str();
}
void write_to_file(string to, const unsigned char* data, long length)
{
ofstream file;
file.open(to, ios::out | ios::binary );
file.write((const char*)data, length);
file.close();
}
long get_index(int i)
{
if(i<=0) return 0;
else return get_index(i-2) + DATA_LENGTHS[i-1];
}
const unsigned char* get_data(int i)
{
const unsigned char* data = DATA;
if(i==0) return DATA;
for(int j=0;j<i;j++)
{
data+=DATA_LENGTHS[j];
}
return data;
}
int main(int argc,char** argv)
{
auto path = fs::temp_directory_path() / get_uuid();
vector<string> vecargs;
auto args = arg_strings(argc,argv, vecargs);
if(!verify_hash())
{
cerr << "Error: Bad message hash\n";
return 1;
}
#ifndef SILENT
cout << "Extracting " << DATA_COUNT << " files to " << path << "...\n";
#endif
fs::create_directory(path);
for(int i=0;i<DATA_COUNT;i++)
{
#ifndef SILENT
cout << " <- " << DATA_NAMES[i] << " (" << DATA_LENGTHS[i] << ")\n";
#endif
write_to_file(path / DATA_NAMES[i], get_data(i), DATA_LENGTHS[i]);
}
if(DATA_EXEC_AFTER) {
string execstr = get_exec_str(vecargs, args, path);
#ifndef SILENT
cout << "exec: " << execstr << '\n';
#endif
system(execstr.c_str());
}
fs::remove_all(path);
return 0;
}

@ -0,0 +1,17 @@
#!/bin/bash
SCRIPTLINK=$(readlink -f "$0")
SCRIPTPATH="$( cd "$(dirname "$SCRIPTLINK")" >/dev/null 2>&1 ; pwd -P )"
dotnet run --project "$SCRIPTPATH/generator" -o "$SCRIPTPATH/file.h" "$@" || exit 1
[[ -f "$SCRIPTPATH/file.h" ]] || exit 1
echo "Compiling binary..."
g++ -I$SCRIPTPATH/include/ -w --std=c++17 "$SCRIPTPATH/main.cpp" "$SCRIPTPATH/sha256_literal/sha256.cpp" -o sfexec
rm "$SCRIPTPATH/file.h"
echo "Complete."

@ -0,0 +1,11 @@
#!/bin/bash
sfexec-create "$@" || exit 1
echo "Compressing..."
gzip sfexec || exit 1
sfexec-create -s -e 'gzip -d %location/sfexec.gz && chmod +x %location/sfexec && %location/sfexec %args' sfexec.gz > /dev/null || exit 1
rm sfexec.gz
echo "Done."
Loading…
Cancel
Save