-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrmfile.h
100 lines (82 loc) · 1.85 KB
/
crmfile.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include "decrunchmania.h"
class CrmFile
{
public:
uint32_t size;
uint8_t* data;
bool Load(const char* name)
{
if (data) free(data);
FILE *file;
int filesize;
int headroom;
uint8_t *indata;
uint8_t *outdata;
char header[14];
/* open input file */
file = fopen(name, "rb");
if (file == 0)
{
//printf("Could not open input file %s\n", name);
printf("Could not open '%s' (maybe not part of this release)\n", name);
return false;
}
/* 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)
{
headroom = GetSecDist(header);
size = GetSize(header);
}
if (filesize <= 14 || size == 0)
{
//printf("Not a valid CrM2 file\n");
// assume unpacked
fseek(file, 0, SEEK_SET);
data = static_cast<uint8_t*>(malloc(filesize));
fread(data, filesize, 1, file);
size = filesize;
fclose(file);
return true;
}
/* allocate memory to hold header and decrunched data */
indata = static_cast<uint8_t*>(malloc(size + 14));
data = static_cast<uint8_t*>(malloc(size));
outdata = static_cast<uint8_t*>(malloc(size + headroom));
if (indata == 0)
{
//printf("Out of memory\n");
fclose(file);
return false;
}
/* read file */
rewind(file);
fread(indata, 1, filesize, file);
fclose(file);
/* decrunch! */
// in place decrunch is broken for some files with minsecdist.
//OverlapCheck(indata);
Decrunch(indata, outdata);
std::copy(outdata, outdata+size, data);
free(indata);
free(outdata);
return true;
}
CrmFile()
{
size = 0;
data = nullptr;
}
~CrmFile()
{
if (data) free(data);
}
};