diff options
author | Pixel <pixel@nobis-crew.org> | 2009-11-08 05:38:26 +0100 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2009-11-08 05:38:26 +0100 |
commit | 90d611a5f1af7d88393067a97330796feadbff5f (patch) | |
tree | 9745a18e2a9f1b91a6df2ae6345ef99d931dd3b6 | |
parent | 62b68fcf14b4a7929597f09dc2728bf5682517f0 (diff) |
Adding a few tools.
-rw-r--r-- | src/bin2c.c | 61 | ||||
-rw-r--r-- | src/lua2c.c | 61 |
2 files changed, 122 insertions, 0 deletions
diff --git a/src/bin2c.c b/src/bin2c.c new file mode 100644 index 0000000..8f6638c --- /dev/null +++ b/src/bin2c.c @@ -0,0 +1,61 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <stdio.h> + +unsigned char *buffer; + +int main(int argc, char *argv[]) +{ + int fd_size; + FILE *source, *dest; + int i; + + if (argc != 4) { + printf("bin2c\n" "Usage: bin2c infile outfile label\n\n"); + return 1; + } + + if ((source = fopen(argv[1], "rb")) == NULL) { + printf("Error opening %s for reading.\n", argv[1]); + return 1; + } + + fseek(source, 0, SEEK_END); + fd_size = ftell(source); + fseek(source, 0, SEEK_SET); + + buffer = malloc(fd_size); + if (buffer == NULL) { + printf("Failed to allocate memory.\n"); + return 1; + } + + if (fread(buffer, 1, fd_size, source) != fd_size) { + printf("Failed to read file.\n"); + return 1; + } + fclose(source); + + if ((dest = fopen(argv[2], "w+")) == NULL) { + printf("Failed to open/create %s.\n", argv[2]); + return 1; + } + + fprintf(dest, "unsigned int size_%s = %d;\n", argv[3], fd_size); + fprintf(dest, "unsigned char %s[] = {", argv[3]); + + for (i = 0; i < fd_size; i += 1) { + if ((i % 16) == 0) + fprintf(dest, "\n\t"); + fprintf(dest, "0x%02x, ", buffer[i]); + } + + fprintf(dest, "\n};\n"); + + fclose(dest); + + return 0; +} diff --git a/src/lua2c.c b/src/lua2c.c new file mode 100644 index 0000000..4affa81 --- /dev/null +++ b/src/lua2c.c @@ -0,0 +1,61 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <stdio.h> + +unsigned char *buffer; + +int main(int argc, char *argv[]) +{ + int fd_size; + FILE *source, *dest; + int i; + + if (argc != 3) { + printf("bin2c\n" "Usage: lua2c infile outfile\n\n"); + return 1; + } + + if ((source = fopen(argv[1], "rb")) == NULL) { + printf("Error opening %s for reading.\n", argv[1]); + return 1; + } + + fseek(source, 0, SEEK_END); + fd_size = ftell(source); + fseek(source, 0, SEEK_SET); + + buffer = malloc(fd_size); + if (buffer == NULL) { + printf("Failed to allocate memory.\n"); + return 1; + } + + if (fread(buffer, 1, fd_size, source) != fd_size) { + printf("Failed to read file.\n"); + return 1; + } + fclose(source); + + if ((dest = fopen(argv[2], "w+")) == NULL) { + printf("Failed to open/create %s.\n", argv[2]); + return 1; + } + + fprintf(dest, "{\n"); + fprintf(dest, "static const unsigned char B1[] = {"); + + for (i = 0; i < fd_size; i += 1) { + if ((i % 16) == 0) + fprintf(dest, "\n\t"); + fprintf(dest, "0x%02x, ", buffer[i]); + } + + fprintf(dest, "\n};\n\n if (luaL_loadbuffer(L, (const char *)B1, sizeof(B1), \"%s\") == 0) lua_call(L, 0, 0);\n}\n", argv[1]); + + fclose(dest); + + return 0; +} |