From f0e550345e353813875b5b5d4d9ea7e3d5a77bae Mon Sep 17 00:00:00 2001 From: Gentoo Date: Fri, 4 Dec 2020 12:46:34 +1100 Subject: initial commit --- Makefile | 12 +++++++++++ main.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 Makefile create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6d5f37a --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +CC=gcc +CFLAGS=-O3 -march=native -pipe + +OBJ = main.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +gct-generate: $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) +clean: + rm gct-generate main.o diff --git a/main.c b/main.c new file mode 100644 index 0000000..8e19b51 --- /dev/null +++ b/main.c @@ -0,0 +1,69 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc < 2){fprintf(stderr,"Usage: %s \n", argv[0]); exit(1);} + + FILE *output = fopen("output.gct","wb"); + if (!output){fprintf(stderr,"Could not open output file!\n"); exit(1);} + + /* Header */ + char header[] = {0x00,0xd0,0xc0,0xde,0x00,0xd0,0xc0,0xde}; + fwrite(header, 1, 8, output); + + FILE *input = fopen(argv[1],"r"); + if (!input){fprintf(stderr,"Could not open:%s!\n",argv[1]); exit(1);} + + /* check that the given file isn't empty */ + fseek(input, 0L, SEEK_END); + if (!ftell(input)){fprintf(stderr,"File %s is empty.\n", argv[1]); exit(1);} + rewind(input); + + char code[18]; + size_t result; + + /* convert each ASCII encoded hex line into the byte values and write that to the gct file */ + while ((result = fread(code, 1, 18, input))) + { + if (result < 18){fprintf(stderr, "Invalid code length.\n"); exit(1);} + + /* remove space in middle of code line */ + for (int i = 8; i < 17; ++i) + { + code[i] = code[i+1]; + } + + /* convert ASCII encoded hex to byte values */ + for (int i = 0, j = 0; i < 8; ++i, j += 2) + { + int amount0; + int amount1; + + /* check if first number is decimal or hex */ + if (code[j] >= '0' && code[j] <= '9'){amount0 = '0';} + else if (code[j] >= 'A' && code[j] <= 'F') {amount0 = 55;} + else {fprintf(stderr,"Invalid code character: %c\n", code[j]); exit(1);} + + /* check if second number is decimal or hex */ + if (code[j+1] >= '0' && code[j+1] <= '9'){amount1 = '0';} + else if (code[j+1] >= 'A' && code[j+1] <= 'F') {amount1 = 55;} + else {fprintf(stderr,"Invalid code character: %c\n", code[j+1]); exit(1);} + + /* extract the two characters and place them into one byte */ + code[i] = ((code[j] - amount0) << 4) | (code[j+1] - amount1); + } + + /* write the code to the gct file */ + fwrite(code, 1, 8, output); + } + + + /* Footer */ + char footer[] = {0xf0,0,0,0,0,0,0,0}; + fwrite(footer, 1, 8, output); + + fclose(output); + fclose(input); + return 0; +} -- cgit v1.2.3