Commit 73b18fcf authored by Alejandro Mery's avatar Alejandro Mery
Browse files

fexc: move bin2fex's main() in

parent 650b1dfa
...@@ -14,17 +14,11 @@ ...@@ -14,17 +14,11 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "common.h"
#include "bin2fex.h"
#include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <fcntl.h>
#define pr_info(F, ...) do { \ #define pr_info(F, ...) do { \
fprintf(stderr, "bin2fex: " F, __VA_ARGS__); \ fprintf(stderr, "bin2fex: " F, __VA_ARGS__); \
...@@ -214,59 +208,3 @@ static int decompile(void *bin, size_t bin_size, FILE *out) ...@@ -214,59 +208,3 @@ static int decompile(void *bin, size_t bin_size, FILE *out)
} }
return 0; /* success */ return 0; /* success */
} }
/**
*/
int main(int argc, char *argv[])
{
struct stat sb;
int ret = -1;
int in = 0;
FILE *out = stdout;
const char *filename[] = {"stdin", "stdout"};
void *p;
/* open */
if (argc>1) {
filename[0] = argv[1];
if ((in = open(filename[0], O_RDONLY)) < 0) {
errf("%s: %s\n", filename[0], strerror(errno));
goto usage;
}
if (argc > 2) {
filename[1] = argv[2];
if ((out = fopen(filename[1], "w")) == NULL) {
errf("%s: %s\n", filename[1], strerror(errno));
goto usage;
}
}
}
/* mmap input */
if (fstat(in, &sb) == -1)
errf("fstat: %s: %s\n", filename[0], strerror(errno));
else if (!S_ISREG(sb.st_mode))
errf("%s: not a regular file (mode:%d).\n", filename[0], sb.st_mode);
else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, in, 0)) == MAP_FAILED)
errf("mmap: %s: %s\n", filename[0], strerror(errno));
else {
/* close and decompile mmap */
close(in);
ret = decompile(p, sb.st_size, out);
if (munmap(p, sb.st_size) == -1)
errf("munmap: %s: %s\n", filename[0], strerror(errno));
goto done;
}
usage:
errf("Usage: %s [<script.bin> [<script.fex>]]\n", argv[0]);
if (in > 2) close(in);
done:
if (out && out != stdout) fclose(out);
return ret;
}
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#include <libgen.h> #include <libgen.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -42,15 +44,50 @@ static inline int script_parse(enum script_format format, ...@@ -42,15 +44,50 @@ static inline int script_parse(enum script_format format,
switch (format) { switch (format) {
case FEX_SCRIPT_FORMAT: { case FEX_SCRIPT_FORMAT: {
FILE *in = stdin; FILE *in = stdin;
if (filename && (in = fopen(filename, "r")) == NULL) { if (!filename)
errf("%s: %s\n", filename, strerror(errno)); filename = "<stdin>";
else if ((in = fopen(filename, "r")) == NULL) {
pr_err("%s: %s\n", filename, strerror(errno));
break; break;
} }
ret = script_parse_fex(in, filename ? filename : "<stdin>", ret = script_parse_fex(in, filename, script);
script);
fclose(in); fclose(in);
}; break; }; break;
case BIN_SCRIPT_FORMAT: { case BIN_SCRIPT_FORMAT: {
int in = 0; /* stdin */
struct stat sb;
void *bin = NULL;
if (!filename)
filename = "<stdin>";
else if ((in = open(filename, O_RDONLY)) < 0) {
pr_err("%s: %s\n", filename, strerror(errno));
break;
} else if (fstat(in, &sb) == -1) {
pr_err("%s: %s: %s\n", filename,
"fstat", strerror(errno));
goto bin_close;
} else if (S_ISREG(sb.st_mode)) {
/* regular file, mmap it */
bin = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
if (bin == MAP_FAILED) {
pr_err("%s: %s: %s\n", filename,
"mmap", strerror(errno));
goto bin_close;
}
} else {
pr_err("%s: not a regular file (mode:%d).\n",
filename, sb.st_mode);
goto bin_close;
}
ret = script_decompile_bin(bin, sb.st_size, filename, script);
if (munmap(bin, sb.st_size) == -1) {
pr_err("%s: %s: %s\n", filename,
"munmap", strerror(errno));
}
bin_close:
close(in);
}; break; }; break;
} }
return ret; return ret;
...@@ -62,6 +99,17 @@ static inline int script_generate(enum script_format format, ...@@ -62,6 +99,17 @@ static inline int script_generate(enum script_format format,
int ret = 0; int ret = 0;
switch (format) { switch (format) {
case FEX_SCRIPT_FORMAT: { case FEX_SCRIPT_FORMAT: {
FILE *out = stdout;
if (!filename)
filename = "<stdout>";
else if ((out = fopen(filename, "w")) == NULL) {
pr_err("%s: %s\n", filename, strerror(errno));
break;
}
ret = script_generate_fex(out, filename, script);
fclose(out);
}; break; }; break;
case BIN_SCRIPT_FORMAT: { case BIN_SCRIPT_FORMAT: {
int out = 1; /* stdout */ int out = 1; /* stdout */
...@@ -71,14 +119,14 @@ static inline int script_generate(enum script_format format, ...@@ -71,14 +119,14 @@ static inline int script_generate(enum script_format format,
if (!filename) if (!filename)
filename = "<stdout>"; filename = "<stdout>";
else if ((out = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) { else if ((out = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
errf("%s: %s\n", filename, strerror(errno)); pr_err("%s: %s\n", filename, strerror(errno));
break; break;
} }
bin_size = script_bin_size(script, &sections, &entries); bin_size = script_bin_size(script, &sections, &entries);
bin = calloc(1, bin_size); bin = calloc(1, bin_size);
if (!bin) if (!bin)
perror("malloc"); pr_err("%s: %s\n", "malloc", strerror(errno));
else if (script_generate_bin(bin, bin_size, script, sections, entries)) { else if (script_generate_bin(bin, bin_size, script, sections, entries)) {
while(bin_size) { while(bin_size) {
ssize_t wc = write(out, bin, bin_size); ssize_t wc = write(out, bin, bin_size);
...@@ -87,14 +135,16 @@ static inline int script_generate(enum script_format format, ...@@ -87,14 +135,16 @@ static inline int script_generate(enum script_format format,
bin += wc; bin += wc;
bin_size -= wc; bin_size -= wc;
} else if (wc < 0 && errno != EINTR) { } else if (wc < 0 && errno != EINTR) {
pr_err("%s: write: %s\n", filename, pr_err("%s: %s: %s\n", filename,
strerror(errno)); "write", strerror(errno));
break; break;
} }
} }
if (bin_size == 0) if (bin_size == 0)
ret = 0; ret = 0;
} }
free(bin);
close(out);
}; break; }; break;
} }
return ret; return ret;
......
...@@ -208,3 +208,10 @@ int script_generate_bin(void *bin, size_t UNUSED(bin_size), ...@@ -208,3 +208,10 @@ int script_generate_bin(void *bin, size_t UNUSED(bin_size),
} }
return 1; return 1;
} }
int script_decompile_bin(void *UNUSED(bin), size_t UNUSED(bin_size),
const char *UNUSED(filename),
struct script *UNUSED(script))
{
return 0;
}
...@@ -22,5 +22,7 @@ size_t script_bin_size(struct script *script, ...@@ -22,5 +22,7 @@ size_t script_bin_size(struct script *script,
int script_generate_bin(void *bin, size_t bin_size, struct script *script, int script_generate_bin(void *bin, size_t bin_size, struct script *script,
size_t sections, size_t entries); size_t sections, size_t entries);
int script_decompile_bin(void *bin, size_t bin_size,
const char *filename,
struct script *script);
#endif #endif
...@@ -220,3 +220,9 @@ parse_error: ...@@ -220,3 +220,9 @@ parse_error:
ok = 0; ok = 0;
return ok; return ok;
} }
int script_generate_fex(FILE *UNUSED(out), const char *UNUSED(filename),
struct script *UNUSED(script))
{
return 0;
}
...@@ -18,5 +18,6 @@ ...@@ -18,5 +18,6 @@
#define _SUBXI_TOOLS_SCRIPT_FEX_H #define _SUBXI_TOOLS_SCRIPT_FEX_H
int script_parse_fex(FILE *in, const char *filename, struct script *script); int script_parse_fex(FILE *in, const char *filename, struct script *script);
int script_generate_fex(FILE *out, const char *filename, struct script *script);
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment