Nostafaru
Moderating the Crunch Bunch.
Retired Staff
Community Elite
Community Veteran
Determined Poster
Active Member
Console ID Poster
This is useful for IDA, code this as a plugin.
#include <idc.idc>
static escape(str) {
auto i, j, new_str;
new_str = "";
j = 0;
for (i = 0; i < strlen(str); i = i + 1) {
if (str == "\n") {
new_str[j] = "\\";
new_str[j+1] = "n";
j = j + 2;
} else if (str == "\"") {
new_str[j] = "\\";
new_str[j+1] = "\"";
j = j + 2;
} else {
new_str[j] = str;
j = j + 1;
}
}
return new_str;
}
static setComment(handle, cmt) {
auto i;
cmt = escape(cmt);
for (i = 0; i < strlen(cmt); i = i + 1024) {
if (i == 0) {
writestr(handle, form(" cmt = \"%s\";\n", substr(cmt, i, i + 1024)));
} else {
writestr(handle, form(" cmt = sprintf(\"%%s%%s\", cmt, \"%s\");\n", substr(cmt, i, i + 1024)));
}
}
}
static main() {
auto ea, filename, handle, cmt;
filename = AskFile (1, "*.idc", "IDC file to save function comments");
Message("Filename is : %s\n", filename);
if (filename == "") {
Message("Action canceled\n");
return;
}
handle = fopen(filename, "w");
if (handle == 0) {
Warning("Could not open file for writing\n");
return;
}
writestr(handle, "//\n");
writestr(handle, "// +-------------------------------------------------------+\n");
writestr(handle, "// | This file was generated by dump_function_cmt.idc |\n");
writestr(handle, "// +-------------------------------------------------------+\n");
writestr(handle, "//\n");
writestr(handle, "//\n");
writestr(handle, "\n");
writestr(handle, "#define UNLOADED_FILE 1\n");
writestr(handle, "#include <idc.idc>\n");
writestr(handle, "\n");
writestr(handle, "static main(void)\n");
writestr(handle, "{\n");
writestr(handle, " FunctionComments();\n");
writestr(handle, "}\n");
writestr(handle, "\n");
writestr(handle, "//-----------------------------\n");
writestr(handle, "// Function Comments\n");
writestr(handle, "\n");
writestr(handle, "static FunctionComments(void) {\n");
writestr(handle, " auto cmt;\n");
writestr(handle, "\n");
for (ea = 0; ea != BADADDR; ea = NextFunction(ea)) {
cmt = GetFunctionCmt(ea, 0);
if (cmt != "") {
setComment(handle, cmt);
writestr(handle, form(" SetFunctionCmt (0x%X, cmt, 0);\n\n", ea));
}
cmt = GetFunctionCmt(ea, 1);
if (cmt != "") {
setComment(handle, cmt);
writestr(handle, form(" SetFunctionCmt (0x%X, cmt, 1);\n\n", ea));
}
}
writestr(handle, "}\n");
writestr(handle, "\n");
writestr(handle, "// End of file.\n");
}
#include <idc.idc>
static escape(str) {
auto i, j, new_str;
new_str = "";
j = 0;
for (i = 0; i < strlen(str); i = i + 1) {
if (str == "\n") {
new_str[j] = "\\";
new_str[j+1] = "n";
j = j + 2;
} else if (str == "\"") {
new_str[j] = "\\";
new_str[j+1] = "\"";
j = j + 2;
} else {
new_str[j] = str;
j = j + 1;
}
}
return new_str;
}
static setComment(handle, cmt) {
auto i;
cmt = escape(cmt);
for (i = 0; i < strlen(cmt); i = i + 1024) {
if (i == 0) {
writestr(handle, form(" cmt = \"%s\";\n", substr(cmt, i, i + 1024)));
} else {
writestr(handle, form(" cmt = sprintf(\"%%s%%s\", cmt, \"%s\");\n", substr(cmt, i, i + 1024)));
}
}
}
static main() {
auto ea, filename, handle, cmt;
filename = AskFile (1, "*.idc", "IDC file to save function comments");
Message("Filename is : %s\n", filename);
if (filename == "") {
Message("Action canceled\n");
return;
}
handle = fopen(filename, "w");
if (handle == 0) {
Warning("Could not open file for writing\n");
return;
}
writestr(handle, "//\n");
writestr(handle, "// +-------------------------------------------------------+\n");
writestr(handle, "// | This file was generated by dump_function_cmt.idc |\n");
writestr(handle, "// +-------------------------------------------------------+\n");
writestr(handle, "//\n");
writestr(handle, "//\n");
writestr(handle, "\n");
writestr(handle, "#define UNLOADED_FILE 1\n");
writestr(handle, "#include <idc.idc>\n");
writestr(handle, "\n");
writestr(handle, "static main(void)\n");
writestr(handle, "{\n");
writestr(handle, " FunctionComments();\n");
writestr(handle, "}\n");
writestr(handle, "\n");
writestr(handle, "//-----------------------------\n");
writestr(handle, "// Function Comments\n");
writestr(handle, "\n");
writestr(handle, "static FunctionComments(void) {\n");
writestr(handle, " auto cmt;\n");
writestr(handle, "\n");
for (ea = 0; ea != BADADDR; ea = NextFunction(ea)) {
cmt = GetFunctionCmt(ea, 0);
if (cmt != "") {
setComment(handle, cmt);
writestr(handle, form(" SetFunctionCmt (0x%X, cmt, 0);\n\n", ea));
}
cmt = GetFunctionCmt(ea, 1);
if (cmt != "") {
setComment(handle, cmt);
writestr(handle, form(" SetFunctionCmt (0x%X, cmt, 1);\n\n", ea));
}
}
writestr(handle, "}\n");
writestr(handle, "\n");
writestr(handle, "// End of file.\n");
}