sync with dc5d2b9

This commit is contained in:
2025-03-05 22:55:46 +08:00
parent 5e1c4037a9
commit a2b8ab1205
6 changed files with 145 additions and 35 deletions

View File

@@ -7,6 +7,7 @@
#include "pyc_module.h"
#include "pyc_numeric.h"
#include "bytecode.h"
#include "ASTree.h"
#ifdef WIN32
# define PATHSEP '\\'
@@ -104,7 +105,7 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent,
unsigned int orig_flags = codeObj->flags();
if (mod->verCompare(3, 8) < 0) {
// Remap flags back to the value stored in the PyCode object
orig_flags = (orig_flags & 0xFFFF) | ((orig_flags & 0xFFF00000) >> 4);
orig_flags = (orig_flags & 0x1FFF) | ((orig_flags & 0xFFFE0000) >> 4);
}
iprintf(pyc_output, indent + 1, "Flags: 0x%08X", orig_flags);
print_coflags(codeObj->flags(), pyc_output);
@@ -251,23 +252,17 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent,
int main(int argc, char* argv[])
{
const char* infile = nullptr;
const char* outfile = nullptr;
bool marshalled = false;
const char* version = nullptr;
unsigned disasm_flags = 0;
std::ostream* pyc_output = &std::cout;
std::ofstream out_file;
std::ofstream dc_out_file;
std::ofstream das_out_file;
for (int arg = 1; arg < argc; ++arg) {
if (strcmp(argv[arg], "-o") == 0) {
if (arg + 1 < argc) {
const char* filename = argv[++arg];
out_file.open(filename, std::ios_base::out);
if (out_file.fail()) {
fprintf(stderr, "Error opening file '%s' for writing\n",
filename);
return 1;
}
pyc_output = &out_file;
outfile = argv[++arg];
} else {
fputs("Option '-o' requires a filename\n", stderr);
return 1;
@@ -288,7 +283,7 @@ int main(int argc, char* argv[])
} else if (strcmp(argv[arg], "--help") == 0 || strcmp(argv[arg], "-h") == 0) {
fprintf(stderr, "Usage: %s [options] input.pyc\n\n", argv[0]);
fputs("Options:\n", stderr);
fputs(" -o <filename> Write output to <filename> (default: stdout)\n", stderr);
fputs(" -o <filename> Write output to <filename>.das and <filename>.cdc.py\n", stderr);
fputs(" -c Specify loading a compiled code object. Requires the version to be set\n", stderr);
fputs(" -v <x.y> Specify a Python version for loading a compiled code object\n", stderr);
fputs(" --pycode-extra Show extra fields in PyCode object dumps\n", stderr);
@@ -308,6 +303,20 @@ int main(int argc, char* argv[])
return 1;
}
std::string prefix_name(outfile ? outfile : infile);
dc_out_file.open(prefix_name + ".cdc.py", std::ios_base::out);
if (dc_out_file.fail()) {
fprintf(stderr, "Error opening file '%s' for writing\n", (prefix_name + ".cdc.py").c_str());
return 1;
}
das_out_file.open(prefix_name + ".das", std::ios_base::out);
if (das_out_file.fail()) {
fprintf(stderr, "Error opening file '%s' for writing\n", (prefix_name + ".das").c_str());
return 1;
}
PycModule mod;
if (!marshalled) {
try {
@@ -333,16 +342,28 @@ int main(int argc, char* argv[])
}
const char* dispname = strrchr(infile, PATHSEP);
dispname = (dispname == NULL) ? infile : dispname + 1;
formatted_print(*pyc_output, "%s (Python %d.%d%s)\n", dispname,
formatted_print(das_out_file, "%s (Python %d.%d%s)\n", dispname,
mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " -U" : "");
try {
output_object(mod.code().try_cast<PycObject>(), &mod, 0, disasm_flags,
*pyc_output);
das_out_file);
} catch (std::exception& ex) {
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
return 1;
}
dc_out_file << "# Source Generated with Decompyle++\n";
formatted_print(dc_out_file, "# File: %s (Python %d.%d%s)\n\n", dispname,
mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : "");
try {
decompyle(mod.code(), &mod, dc_out_file);
} catch (std::exception& ex) {
fprintf(stderr, "Error decompyling %s: %s\n", infile, ex.what());
return 1;
}
return 0;
}