-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrmtool.c
85 lines (68 loc) · 1.61 KB
/
decrmtool.c
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
82
83
84
85
/*
decrmtool.c
Robert Leffmann 2-Nov-2009
robert.leffmann@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include "decrunchmania.h"
int main(int nargs, char **argp)
{
FILE *file;
int filesize, datasize;
char *indata;
char header[14];
/* check for correct usage */
if(nargs != 2 && nargs != 3)
{
printf("Usage: decrunch <input> [output]\n");
return 0;
}
/* open input file */
file = fopen(argp[1], "rb");
if(file == 0)
{
printf("Could not open input file\n");
return 0;
}
/* read header and get file size */
fread(header, 14, 1, file);
fseek(file, 0, SEEK_END);
filesize = ftell(file);
/* check if file is valid */
if(filesize > 14)
datasize = GetSize(header);
if(filesize <= 14 || datasize == 0)
{
printf("Not a valid CrM2 file\n");
fclose(file);
return 0;
}
/* allocate memory to hold header and decrunched data */
indata = malloc(datasize+14);
if(indata == 0)
{
printf("Out of memory\n");
fclose(file);
return 0;
}
/* read file */
rewind(file);
fread(indata, filesize, 1, file);
fclose(file);
/* decrunch! */
Decrunch(indata);
/* open output file */
file = fopen(argp[nargs == 2 ? 1 : 2], "wb");
if(file == 0)
{
printf("Could not create output file\n");
free(indata);
return 0;
}
/* save decrunched data and clean up */
fwrite(indata+14, datasize, 1, file);
fclose(file);
free(indata);
return 0;
}