Esempio n. 1
0
def gen_python(code, verbosity_level, debug):
    if verbosity_level >= 1:
        print(f"Generating {_PYVM_OUT}")

    old_lines = read_file(_PYVM_TEMPLATE)
    lines = []
    del_lext_line = False
    for line in old_lines:
        # Perform template substitutions.
        if line.startswith("p="):
            prg_str = bin2str.convert(code,
                                      use_hex=False).replace("\\", "\\\\")
            line = f"p='{prg_str}'"

        # Perform simple minification (except for debug builds).
        if not debug:
            # Remove comments (except the shebang).
            if not line.startswith("#!/"):
                line = remove_line_comment(line)

            # Remove debug code and empty lines.
            if (line.lstrip().startswith("WriteDebug") or line.strip() == ""
                    or del_lext_line):
                line = ""
            if line.startswith("def WriteDebug"):
                line = ""
                del_lext_line = True
            else:
                del_lext_line = False
            if line:
                lines.append(line)
        else:
            lines.append(line)

    write_file(_PYVM_OUT, lines, make_executable=True)
Esempio n. 2
0
def gen_bat(code, verbosity_level, debug):
    if verbosity_level >= 1:
        print(f"Generating {_BATVM_OUT}")

    old_lines = read_file(_BATVM_TEMPLATE)
    lines = []
    num_del_lines = 0
    for line in old_lines:
        # Perform template substitutions.
        if line.startswith("set p="):
            prg_str = bin2str.convert(code, use_hex=True)
            line = f"set p={prg_str}"
        elif line.startswith("set /A ps="):
            line = f"set /A ps={len(code)}"

        # Remove debug code in non-debug builds.
        if not debug:
            if line.startswith(":WriteDebug"):
                num_del_lines = 3
            if num_del_lines == 0 and not line.lstrip().startswith(
                    "call :WriteDebug"):
                lines.append(line)
            if num_del_lines > 0:
                num_del_lines -= 1
        else:
            lines.append(line)

    if not debug:
        lines = minify_bat(lines)

    write_file(_BATVM_OUT, lines, make_executable=True, line_end="\r\n")
Esempio n. 3
0
def gen_sh(template, out, code, verbosity_level, debug):
    if verbosity_level >= 1:
        print(f"Generating {out}")

    old_lines = read_file(template)
    lines = []
    for line in old_lines:
        # Perform template substitutions.
        if line.startswith("p="):
            prg_str = bin2str.convert(code, use_hex=False)
            line = f"p='{prg_str}'"

        # Remove debug code in non-debug builds.
        if debug or (not line.lstrip().startswith("WriteDebug")):
            lines.append(line)

    if not debug:
        lines = minify_sh(lines)

    write_file(out, lines, make_executable=True)
Esempio n. 4
0
def gen_powershell(code, verbosity_level, debug):
    if verbosity_level >= 1:
        print(f"Generating {_PSVM_OUT}")

    old_lines = read_file(_PSVM_TEMPLATE)
    lines = []
    for line in old_lines:
        # Perform template substitutions.
        if line.startswith("$p="):
            prg_str = bin2str.convert(code, use_hex=False)
            line = f"$p='{prg_str}'\n"
        elif line.startswith("$DebugPreference"):
            line = '$DebugPreference="Continue"\n' if debug else ""

        # Remove debug code in non-debug builds.
        if debug or (not line.lstrip().startswith("Write-Debug")):
            lines.append(line)

    if not debug:
        lines = minify_sh(lines)

    write_file(_PSVM_OUT, lines, make_executable=True)
Esempio n. 5
0
def gen_c(code, verbosity_level, debug):
    if verbosity_level >= 1:
        print(f"Generating {_CVM_OUT}")

    old_lines = read_file(_CVM_TEMPLATE)
    lines = []
    for line in old_lines:
        # Perform template substitutions.
        if line.startswith("const char p[]="):
            prg_str = bin2str.convert(code,
                                      use_hex=False).replace("\\", "\\\\")
            line = f'const char p[]="{prg_str}";'

        # Perform simple minification (except for debug builds).
        if not debug:
            # Remove comments.
            line = remove_line_comment(line, "//")

            # Remove indent, trailing whitespace and newlines.
            line = line.strip()

            # Remove debug code.
            if "WriteDebug" in line:
                line = ""

            if line:
                # Preprocessor directives need to be on separate lines.
                if line.startswith("#"):
                    lines.append(line)
                else:
                    if len(lines) == 0 or lines[-1].startswith("#"):
                        lines.append(line)
                    else:
                        lines[-1] += line
        else:
            lines.append(line)

    write_file(_CVM_OUT, lines, make_executable=False)