/* Copyright (C) 2020 Gentoo-libre Install This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #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; }