fexc: move bin2fex's main() in
This commit is contained in:
parent
650b1dfa79
commit
73b18fcfc9
62
bin2fex.c
62
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "common.h"
|
||||
#include "bin2fex.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#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 [<script.bin> [<script.fex>]]\n", argv[0]);
|
||||
|
||||
if (in > 2) close(in);
|
||||
done:
|
||||
if (out && out != stdout) fclose(out);
|
||||
return ret;
|
||||
}
|
||||
|
||||
66
fexc.c
66
fexc.c
@ -21,6 +21,8 @@
|
||||
#include <libgen.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
@ -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 = "<stdin>";
|
||||
else if ((in = fopen(filename, "r")) == NULL) {
|
||||
pr_err("%s: %s\n", filename, strerror(errno));
|
||||
break;
|
||||
}
|
||||
ret = script_parse_fex(in, filename ? filename : "<stdin>",
|
||||
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 = "<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;
|
||||
}
|
||||
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 = "<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;
|
||||
case BIN_SCRIPT_FORMAT: {
|
||||
int out = 1; /* stdout */
|
||||
@ -71,14 +119,14 @@ static inline int script_generate(enum script_format format,
|
||||
if (!filename)
|
||||
filename = "<stdout>";
|
||||
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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user