-
Notifications
You must be signed in to change notification settings - Fork 0
/
rarres.cpp
410 lines (369 loc) · 11.4 KB
/
rarres.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) 2018-present, Jhuix (Hui Jin) <jhuix0117@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the
// following conditions are met.
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "rar.hpp"
#include "rarres.h"
#include "VERSION"
namespace RARRES {
CRarRes::CRarRes(bool ignorecase)
: unp_(nullptr)
, arc_(&cmd_)
, flags_(0)
, total_packsize_(0)
, total_unpsize_(0)
, ignorecase_(ignorecase) {
}
CRarRes::~CRarRes() {
Close();
}
void CRarRes::Close() {
if (unp_) {
delete unp_;
unp_ = nullptr;
}
arc_.Close();
for (auto it : fileheadersW_) {
if (it.second->Data) {
free(it.second->Data);
it.second->Data = nullptr;
}
delete it.second;
}
fileheadersW_.clear();
fileheadersA_.clear();
flags_ = 0;
total_packsize_ = 0;
total_unpsize_ = 0;
}
void CRarRes::Release() {
delete this;
}
bool CRarRes::Open(const char* filename, char path_sep) {
wchar_t FileName[NM];
#ifdef _WIN32
CharToWide(filename, FileName, ASIZE(FileName));
#else
UtfToWide(filename, FileName, ASIZE(FileName));
#endif
wchar_t sep = 0;
((char*)&sep)[0] = path_sep;
return Open(FileName, sep);
}
bool CRarRes::Open(const wchar_t* filename, wchar_t path_sep) {
Close();
cmd_.Init();
cmd_.AddArcName(filename);
cmd_.Overwrite = OVERWRITE_ALL;
cmd_.VersionControl = 1;
cmd_.OpenShared = true;
if (!arc_.Open(filename, FMF_OPENSHARED)) {
ErrHandler.OpenErrorMsg(filename);
return false;
}
if (!arc_.IsArchive(true)
|| arc_.GetHeaderType() != HEAD_MAIN) {
arc_.Close();
ErrHandler.OpenErrorMsg(filename);
return false;
}
if (arc_.Volume)
flags_ |= 0x01;
if (arc_.MainComment)
flags_ |= 0x02;
if (arc_.Locked)
flags_ |= 0x04;
if (arc_.Solid)
flags_ |= 0x08;
if (arc_.NewNumbering)
flags_ |= 0x10;
if (arc_.Signed)
flags_ |= 0x20;
if (arc_.Protected)
flags_ |= 0x40;
if (arc_.Encrypted)
flags_ |= 0x80;
if (arc_.FirstVolume)
flags_ |= 0x100;
return ListFiles(path_sep);
}
bool CRarRes::CheckUnpVer()
{
bool WrongVer;
if (arc_.Format == RARFMT50) // Both SFX and RAR can unpack RAR 5.0 archives.
WrongVer = arc_.FileHead.UnpVer > VER_UNPACK5;
else
{
// All formats since 1.3 for RAR.
WrongVer = arc_.FileHead.UnpVer<13 || arc_.FileHead.UnpVer>VER_UNPACK;
}
// We can unpack stored files regardless of compression version field.
if (arc_.FileHead.Method == 0)
WrongVer = false;
if (WrongVer)
{
ErrHandler.UnknownMethodMsg(arc_.FileName, arc_.FileHead.FileName);
uiMsg(UIERROR_NEWERRAR, arc_.FileName);
}
return !WrongVer;
}
bool CRarRes::ListFiles(wchar_t path_sep) {
if (!arc_.IsOpened())
return false;
uint FileCount = 0;
wchar_t VolNumText[50];
*VolNumText = 0;
while (arc_.ReadHeader() > 0)
{
HEADER_TYPE HeaderType = arc_.GetHeaderType();
switch (HeaderType) {
case HEAD_FILE:
ListFileHeader(arc_.FileHead, path_sep);
if (!arc_.FileHead.SplitBefore)
{
total_unpsize_ += arc_.FileHead.UnpSize;
FileCount++;
}
total_packsize_ += arc_.FileHead.PackSize;
break;
case HEAD_SERVICE:
ListFileHeader(arc_.SubHead, path_sep);
break;
}
arc_.SeekToNext();
}
return (bool)(FileCount > 0);
}
void CRarRes::ListFileHeader(FileHeader &hd, wchar_t path_sep) {
if (!hd.Dir) {
RARRES_FILEHEADER* rhd = new RARRES_FILEHEADER;
*((BlockHeader*)rhd) = hd;
rhd->Pos = arc_.CurBlockPos;
rhd->PackSize = hd.PackSize < 0 ? 0 : hd.PackSize;
rhd->UnpSize = hd.UnpSize < 0 ? 0 : hd.UnpSize;
rhd->FileName = hd.FileName;
#ifdef _WIN32
rhd->Mtime = hd.mtime.GetWin();
rhd->Ctime = hd.ctime.GetWin();
#else
rhd->Mtime = hd.mtime.GetUnixNS();
rhd->Ctime = hd.ctime.GetUnixNS();
#endif
rhd->FileAttr = hd.FileAttr;
rhd->Data = nullptr;
//Path sep default value is L'\\'
if ((path_sep && path_sep != L'\\')) {
wchar_t* ch = (wchar_t*)hd.FileName;
while (*ch) {
if (L'\\' == *ch) *ch = path_sep;
++ch;
}
}
if (ignorecase_) {
wchar_t* ch = (wchar_t*)hd.FileName;
while (*ch) {
if ((*ch) >= L'A' && (*ch) <= L'Z')
*ch = (*ch) + 32;
++ch;
}
}
fileheadersW_[hd.FileName] = rhd;
char NameA[NM];
#ifdef NO_USE_UTF8
WideToChar(hd.FileName, NameA, ASIZE(NameA));
#else
WideToUtf(hd.FileName, NameA, ASIZE(NameA));
#endif
fileheadersA_[NameA] = rhd;
}
}
void* CRarRes::LoadResource(const char* id, char** buf, size_t& bufsize) {
if (!arc_.IsOpened()) {
ErrHandler.SetErrorCode(RARX_OPEN);
return nullptr;
}
std::map<std::string, RARRES_FILEHEADER* >::iterator it = fileheadersA_.find(id);
if (it == fileheadersA_.end()) {
ErrHandler.SetErrorCode(RARX_NOFILES);
return nullptr;
}
RARRES_FILEHEADER* rhd = it->second;
if (NULL == rhd) {
ErrHandler.SetErrorCode(RARX_NOFILES);
return nullptr;
}
return Extract(rhd, buf, bufsize);
}
void* CRarRes::Extract(RARRES_FILEHEADER* rhd, char** buf, size_t& bufsize) {
if (!buf || ((*buf) && rhd->UnpSize > (int64)bufsize)) {
bufsize = (size_t)rhd->UnpSize;
ErrHandler.SetErrorCode(RARX_SUCCESS);
return nullptr;
}
dio_.UnpArcSize = arc_.FileLength();
dio_.UnpVolume = false;
arc_.Seek(rhd->Pos, SEEK_SET);
void* result = nullptr;
if (arc_.ReadHeader() > 0) {
if (!CheckUnpVer()) {
ErrHandler.SetErrorCode(RARX_FATAL);
return nullptr;
}
if (!unp_) {
unp_ = new Unpack(&dio_);
if (!unp_) {
ErrHandler.SetErrorCode(RARX_MEMORY);
return nullptr;
}
}
uint threads = GetNumberOfThreads();
unp_->SetThreads(threads);
dio_.UnpVolume = arc_.FileHead.SplitAfter;
dio_.NextVolumeMissing = false;
arc_.Seek(arc_.NextBlockPos - arc_.FileHead.PackSize, SEEK_SET);
dio_.CurUnpRead = 0;
dio_.CurUnpWrite = 0;
dio_.UnpHash.Init(arc_.FileHead.FileHash.Type, threads);
dio_.PackedDataHash.Init(arc_.FileHead.FileHash.Type, threads);
dio_.SetPackedSizeToRead(arc_.FileHead.PackSize);
dio_.SetFiles(&arc_, NULL);
bufsize = (size_t)arc_.FileHead.UnpSize;
if (!(*buf)) {
result = malloc(bufsize);
*buf = (char*)result;
}
dio_.SetUnpackToMemory((byte*)(*buf), (uint)bufsize);
dio_.SetTestMode(arc_.Solid);
dio_.SetSkipUnpCRC(arc_.Solid);
if (arc_.FileHead.Method == 0) {
int64 dest_unpsize = arc_.FileHead.UnpSize;
Array<byte> buffer(File::CopyBufferSize());
while (true)
{
int readsize = dio_.UnpRead(&buffer[0], buffer.Size());
if (readsize <= 0)
break;
int writesize = (int64)readsize < dest_unpsize ? readsize : (int)dest_unpsize;
if (writesize > 0)
{
dio_.UnpWrite(&buffer[0], writesize);
dest_unpsize -= writesize;
}
}
}
else {
unp_->Init(arc_.FileHead.WinSize, arc_.FileHead.Solid);
unp_->SetDestSize(arc_.FileHead.UnpSize);
if (arc_.Format != RARFMT50 && arc_.FileHead.UnpVer <= 15)
unp_->DoUnpack(15, arc_.Solid);
else
unp_->DoUnpack(arc_.FileHead.UnpVer, arc_.FileHead.Solid);
}
}
rhd->Data = result;
return rhd;
}
void* CRarRes::LoadResource(const wchar_t* id, char** buf, size_t& bufsize) {
if (!arc_.IsOpened()) {
ErrHandler.SetErrorCode(RARX_OPEN);
return nullptr;
}
std::map<std::wstring, RARRES_FILEHEADER* >::iterator it = fileheadersW_.find(id);
if (it == fileheadersW_.end()) {
ErrHandler.SetErrorCode(RARX_NOFILES);
return nullptr;
}
RARRES_FILEHEADER* rhd = it->second;
if (NULL == rhd) {
ErrHandler.SetErrorCode(RARX_NOFILES);
return nullptr;
}
return Extract(rhd, buf, bufsize);
}
#ifdef _WIN32
IStream* CRarRes::LoadResource(const char* id) {
size_t res_size = 0;
LoadResource(id, nullptr, res_size);
if (!res_size)
return nullptr;
IStream* stream = nullptr;
HGLOBAL buffer_handler = ::GlobalAlloc(GMEM_MOVEABLE, res_size);
if (buffer_handler) {
void* buffer = ::GlobalLock(buffer_handler);
if (buffer) {
void* res = LoadResource(id, (char**)&buffer, res_size);
if (res) {
::CreateStreamOnHGlobal(buffer_handler, TRUE, &stream);
FreeResource(res);
}
::GlobalUnlock(buffer_handler);
}
}
return stream;
}
IStream* CRarRes::LoadResource(const wchar_t* id) {
size_t res_size = 0;
LoadResource(id, nullptr, res_size);
if (!res_size)
return nullptr;
IStream* stream = nullptr;
HGLOBAL buffer_handler = ::GlobalAlloc(GMEM_MOVEABLE, res_size);
if (buffer_handler) {
void* buffer = ::GlobalLock(buffer_handler);
if (buffer) {
void* res = LoadResource(id, (char**)&buffer, res_size);
if (res) {
::CreateStreamOnHGlobal(buffer_handler, TRUE, &stream);
FreeResource(res);
}
::GlobalUnlock(buffer_handler);
}
}
return stream;
}
#endif
void CRarRes::FreeResource(void* res) {
if (res) {
RARRES_FILEHEADER* rhd = (RARRES_FILEHEADER*)res;
if (rhd->Data) {
free(rhd->Data);
rhd->Data = nullptr;
}
}
}
int CRarRes::GetErrorCode() {
return ErrHandler.GetErrorCode();
}
};
namespace JRES {
const char* PASCAL GetVersion() {
return PRODUCT_VERSION;
}
IRes* PASCAL CreateRarRes(bool ignorecase) {
return new RARRES::CRarRes(ignorecase);
}
};