-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREUtils.cpp
250 lines (206 loc) · 6.68 KB
/
REUtils.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*******************************************************************************
* FPSX/ROFS unpacking program
******************************************************************************/
#include <cstdio>
#include <zlib.h>
#include "REUtils.hpp"
using std::string;
using std::vector;
using std::wstring;
/******************************************************************************/
int ZEXPORT uncompress_(Bytef *dest, uLongf *destLen, const Bytef *source, uLong *sourceLen) {
z_stream stream;
int err;
const uInt max = (uInt)-1;
uLong len, left;
Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */
len = *sourceLen;
if (*destLen) {
left = *destLen;
*destLen = 0;
}
else {
left = 1;
dest = buf;
}
stream.next_in = (z_const Bytef *)source;
stream.avail_in = 0;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
err = inflateInit(&stream);
printf("err1=%d\n", err);
if (err != Z_OK) return err;
stream.next_out = dest;
stream.avail_out = 0;
do {
if (stream.avail_out == 0) {
stream.avail_out = left > (uLong)max ? max : (uInt)left;
left -= stream.avail_out;
}
if (stream.avail_in == 0) {
stream.avail_in = len > (uLong)max ? max : (uInt)len;
len -= stream.avail_in;
}
err = inflate(&stream, Z_NO_FLUSH);
} while (err == Z_OK);
*sourceLen -= len + stream.avail_in;
if (dest != buf)
*destLen = stream.total_out;
else if (stream.total_out && err == Z_BUF_ERROR)
left = 1;
inflateEnd(&stream);
printf("err2=%d\n", err);
return err == Z_STREAM_END ? Z_OK :
err == Z_NEED_DICT ? Z_DATA_ERROR :
err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
err;
}
ByteArray uncompress(const ByteArray &inData, size_t uncompressedLengthHint) {
if (0==uncompressedLengthHint)
uncompressedLengthHint=inData.size()*2;
uLongf outLength=uncompressedLengthHint;
ByteArray outData;
for (bool unpacked=false; !unpacked;) {
outData.resize(outLength);
size_t inLength=inData.size();
int retval=uncompress_(&outData[0], &outLength, &inData[0], &inLength);
if (retval==Z_OK) {
outData.resize(outLength);
unpacked=true;
}
else if (retval==Z_DATA_ERROR)
throw "Z_DATA_ERROR";
else if (retval==Z_MEM_ERROR)
throw "Z_MEM_ERROR";
else if (retval==Z_BUF_ERROR)
outLength+=outLength;
else
throw "Z_UNKNOWN_ERROR";
}
return outData;
}
/******************************************************************************/
BinaryReader::BinaryReader(upp::File &file) :
file(file), start(0), offset(0), size(file.seek(0, SEEK_END)) {}
BinaryReader::BinaryReader(const BinaryReader &other) :
file(other.file), start(other.start+other.offset), offset(0), size(other.size) {}
BinaryReader::BinaryReader(const BinaryReader &other, size_t length) :
file(other.file), start(other.start+other.offset), offset(0), size(length) {}
BinaryReader::BinaryReader(const BinaryReader &other, off_t start, size_t length) :
file(other.file), start(other.start+start), offset(0), size(length) {}
BinaryReader::BinaryReader(const BinaryReader &other, off_t start, ToEnd marker) :
file(other.file), start(other.start+start), offset(0), size(other.size-start) {}
BinaryReader::~BinaryReader() {}
off_t BinaryReader::debug() const {
return start+offset;
}
off_t BinaryReader::tell() const {
return offset;
}
size_t BinaryReader::available() const {
return size-offset;
}
bool BinaryReader::atEnd(size_t margin) const {
return offset+margin>=size;
}
void BinaryReader::skip(unsigned bytes) {
if (offset+bytes>size)
throw "cannot skip bytes";
offset+=bytes;
}
void BinaryReader::align(unsigned block) {
unsigned padding=(block-(offset%block))%block;
if (offset+padding>size)
throw "cannot align to block";
offset+=padding;
}
BinaryReader BinaryReader::window(size_t length) {
if (offset>size)
throw "cannot create a window, because the cursor is beyond the end";
if (offset+length>size)
throw std::string("window is too big: ")+std::to_string(length)+">"+std::to_string(size-offset);
BinaryReader result(*this, length);
skip(length);
return result;
}
uint8_t BinaryReader::readByte() {
uint8_t result;
read(&result, sizeof(result));
return result;
}
uint16_t BinaryReader::readShort() {
return __builtin_bswap16(read<uint16_t>());
}
uint16_t BinaryReader::readShortLE() {
return read<uint16_t>();
}
uint32_t BinaryReader::readInt() {
return __builtin_bswap32(read<uint32_t>());
}
uint32_t BinaryReader::readIntLE() {
return read<uint32_t>();
}
uint64_t BinaryReader::readLong() {
return __builtin_bswap64(read<uint64_t>());
}
uint64_t BinaryReader::readLongLE() {
return read<uint64_t>();
}
string BinaryReader::readShortString() {
return readString(readByte());
}
string BinaryReader::readString(size_t length) {
string result(length, '\0');
read(&result[0], length);
return result;
}
string BinaryReader::readShortUnicodeString() {
return readUnicodeString(readByte());
}
string BinaryReader::readUnicodeString(size_t length) {
string result(length, '\0');
for (unsigned i=0; i<length; i++) {
result[i]=readByte();
readByte();
}
return result;
}
wstring BinaryReader::readWideString(size_t length) {
wstring result(length, '\0');
for (unsigned i=0; i<length; i++)
result[i]=readShort();
return result;
}
void BinaryReader::extract(const string &destination, off_t offset, size_t length, bool truncate) {
int flags=O_WRONLY|O_CREAT;
if (truncate)
flags|=O_TRUNC;
upp::File out(destination.c_str(), flags);
out.seek(offset);
string data(length, '\0');
read(&data[0], length);
out.write(data.data(), data.length());
}
void BinaryReader::extract(const string &destination, bool truncate) {
off_t pos=tell();
extract(destination, pos, size-pos, truncate);
}
ByteArray BinaryReader::read(size_t maxLength) {
ByteArray result(maxLength);
size_t nRead=file.read(&result[0], maxLength, offset+start);
offset+=nRead;
result.resize(nRead);
return result;
}
ByteArray BinaryReader::readAll() {
return read(available());
}
void BinaryReader::read(void * buffer, size_t length) {
if (length) {
size_t retval=file.read(buffer, length, offset+start);
offset+=retval;
if (retval<length)
throw EOFException();
}
}