Skip to content

Instantly share code, notes, and snippets.

@iGlitch
Last active August 2, 2023 14:25
Show Gist options
  • Save iGlitch/d81f44ecd51532195a485749d22e2110 to your computer and use it in GitHub Desktop.
Save iGlitch/d81f44ecd51532195a485749d22e2110 to your computer and use it in GitHub Desktop.
Yet another Gecko code to .gct. Accepts codes formatted like: 00000000 11111111
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 2){fprintf(stderr,"Usage: %s <codes.txt>\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;
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment