From 73b18fcfc94ab7f66b1a36d6cdee4e25e374e636 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 11 May 2012 12:02:10 +0200 Subject: [PATCH] fexc: move bin2fex's main() in --- bin2fex.c | 62 ------------------------------------------------ fexc.c | 66 +++++++++++++++++++++++++++++++++++++++++++++------- script_bin.c | 7 ++++++ script_bin.h | 4 +++- script_fex.c | 6 +++++ script_fex.h | 1 + 6 files changed, 75 insertions(+), 71 deletions(-) diff --git a/bin2fex.c b/bin2fex.c index 8777d38..20e1693 100644 --- a/bin2fex.c +++ b/bin2fex.c @@ -14,17 +14,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "common.h" -#include "bin2fex.h" - -#include #include #include #include #include -#include #include -#include #define pr_info(F, ...) do { \ fprintf(stderr, "bin2fex: " F, __VA_ARGS__); \ @@ -214,59 +208,3 @@ static int decompile(void *bin, size_t bin_size, FILE *out) } 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 [ []]\n", argv[0]); - - if (in > 2) close(in); -done: - if (out && out != stdout) fclose(out); - return ret; -} diff --git a/fexc.c b/fexc.c index d70c12d..0036625 100644 --- a/fexc.c +++ b/fexc.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include @@ -42,15 +44,50 @@ static inline int script_parse(enum script_format format, switch (format) { case FEX_SCRIPT_FORMAT: { FILE *in = stdin; - if (filename && (in = fopen(filename, "r")) == NULL) { - errf("%s: %s\n", filename, strerror(errno)); + if (!filename) + filename = ""; + else if ((in = fopen(filename, "r")) == NULL) { + pr_err("%s: %s\n", filename, strerror(errno)); break; } - ret = script_parse_fex(in, filename ? filename : "", - script); + ret = script_parse_fex(in, filename, script); fclose(in); }; break; case BIN_SCRIPT_FORMAT: { + int in = 0; /* stdin */ + struct stat sb; + void *bin = NULL; + + if (!filename) + filename = ""; + 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; } return ret; @@ -62,6 +99,17 @@ static inline int script_generate(enum script_format format, int ret = 0; switch (format) { case FEX_SCRIPT_FORMAT: { + FILE *out = stdout; + + if (!filename) + filename = ""; + 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; case BIN_SCRIPT_FORMAT: { int out = 1; /* stdout */ @@ -71,14 +119,14 @@ static inline int script_generate(enum script_format format, if (!filename) filename = ""; 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; } bin_size = script_bin_size(script, §ions, &entries); bin = calloc(1, bin_size); if (!bin) - perror("malloc"); + pr_err("%s: %s\n", "malloc", strerror(errno)); else if (script_generate_bin(bin, bin_size, script, sections, entries)) { while(bin_size) { ssize_t wc = write(out, bin, bin_size); @@ -87,14 +135,16 @@ static inline int script_generate(enum script_format format, bin += wc; bin_size -= wc; } else if (wc < 0 && errno != EINTR) { - pr_err("%s: write: %s\n", filename, - strerror(errno)); + pr_err("%s: %s: %s\n", filename, + "write", strerror(errno)); break; } } if (bin_size == 0) ret = 0; } + free(bin); + close(out); }; break; } return ret; diff --git a/script_bin.c b/script_bin.c index 6ee89b2..ede5bc7 100644 --- a/script_bin.c +++ b/script_bin.c @@ -208,3 +208,10 @@ int script_generate_bin(void *bin, size_t UNUSED(bin_size), } return 1; } + +int script_decompile_bin(void *UNUSED(bin), size_t UNUSED(bin_size), + const char *UNUSED(filename), + struct script *UNUSED(script)) +{ + return 0; +} diff --git a/script_bin.h b/script_bin.h index 1830cfa..d9051a0 100644 --- a/script_bin.h +++ b/script_bin.h @@ -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, size_t sections, size_t entries); - +int script_decompile_bin(void *bin, size_t bin_size, + const char *filename, + struct script *script); #endif diff --git a/script_fex.c b/script_fex.c index e4409be..bb1ab80 100644 --- a/script_fex.c +++ b/script_fex.c @@ -220,3 +220,9 @@ parse_error: ok = 0; return ok; } + +int script_generate_fex(FILE *UNUSED(out), const char *UNUSED(filename), + struct script *UNUSED(script)) +{ + return 0; +} diff --git a/script_fex.h b/script_fex.h index 6c24179..1b074f2 100644 --- a/script_fex.h +++ b/script_fex.h @@ -18,5 +18,6 @@ #define _SUBXI_TOOLS_SCRIPT_FEX_H 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