1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
/* 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 <http://www.gnu.org/licenses/>.
*/
#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;
}
|