-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathunrar_iface.cpp
492 lines (418 loc) · 16.4 KB
/
unrar_iface.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Interface to libclamunrar
*
* Copyright (C) 2013-2025 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2007-2013 Sourcefire, Inc.
*
* Authors: Trog, Torok Edvin, Tomasz Kojm, Micah Snyder
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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 "dll.hpp"
extern "C" {
#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <wchar.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "unrar_iface.h"
#include "clamav-types.h"
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
/* tell compiler about branches that are very rarely taken,
* such as debug paths, and error paths */
#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)
#define UNLIKELY(cond) __builtin_expect(!!(cond), 0)
#define LIKELY(cond) __builtin_expect(!!(cond), 1)
#else
#define UNLIKELY(cond) (cond)
#define LIKELY(cond) (cond)
#endif
#define unrar_dbgmsg (!UNLIKELY(unrar_debug)) ? (void)0 : unrar_dbgmsg_internal
#define CMTBUFSIZE (64 * 1024)
int CALLBACK CallbackProc(UINT msg, LPARAM UserData, LPARAM P1, LPARAM P2);
static void unrar_dbgmsg_internal(const char* str, ...)
{
va_list ap;
va_start(ap, str);
vfprintf(stderr, str, ap);
va_end(ap);
}
uint8_t unrar_debug = 0;
/**
* @brief Translate an ERAR_<code> to the appropriate UNRAR_<code>
*
* @param errorCode ERAR_<code>
* @return cl_unrar_error_t UNRAR_OK, UNRAR_ENCRYPTED, or UNRAR_ERR.
*/
static cl_unrar_error_t unrar_retcode(int retcode)
{
cl_unrar_error_t status = UNRAR_ERR;
switch (retcode) {
case ERAR_SUCCESS: {
unrar_dbgmsg("unrar_retcode: Success!\n");
status = UNRAR_OK;
break;
}
case ERAR_END_ARCHIVE: {
unrar_dbgmsg("unrar_retcode: No more files in archive.\n");
status = UNRAR_BREAK;
break;
}
case ERAR_NO_MEMORY: {
unrar_dbgmsg("unrar_retcode: Not enough memory!\n");
status = UNRAR_EMEM;
break;
}
case ERAR_MISSING_PASSWORD: {
unrar_dbgmsg("unrar_retcode: Encrypted file header found in archive.\n");
status = UNRAR_ENCRYPTED;
break;
}
case ERAR_BAD_PASSWORD: {
unrar_dbgmsg("unrar_retcode: Encrypted archive or encrypted file in archive.\n");
status = UNRAR_ENCRYPTED;
break;
}
case ERAR_BAD_DATA: {
unrar_dbgmsg("unrar_retcode: Bad data / File CRC error.\n");
break;
}
case ERAR_UNKNOWN_FORMAT: {
unrar_dbgmsg("unrar_retcode: Unknown archive format.\n");
break;
}
case ERAR_EOPEN: {
unrar_dbgmsg("unrar_retcode: Volume open error.\n");
status = UNRAR_EOPEN;
break;
}
case ERAR_ECREATE: {
unrar_dbgmsg("unrar_retcode: File create error.\n");
break;
}
case ERAR_ECLOSE: {
unrar_dbgmsg("unrar_retcode: File close error.\n");
break;
}
case ERAR_EREAD: {
unrar_dbgmsg("unrar_retcode: Read error.\n");
break;
}
case ERAR_EWRITE: {
unrar_dbgmsg("unrar_retcode: Write error.\n");
break;
}
case ERAR_EREFERENCE: {
unrar_dbgmsg("unrar_retcode: Error attempting to unpack the reference record without its source file.\n");
break;
}
default: {
unrar_dbgmsg("unrar_retcode: Unexpected error code: %d\n", retcode);
}
}
return status;
}
static size_t unrar_strnlen(const char* s, size_t n)
{
size_t i = 0;
for (; (i < n) && s[i] != '\0'; ++i)
;
return i;
}
static char* unrar_strndup(const char* s, size_t n)
{
char* alloc;
size_t len;
if (!s) {
return NULL;
}
len = unrar_strnlen(s, n);
alloc = (char*)malloc(len + 1);
if (!alloc) {
return NULL;
} else
memcpy(alloc, s, len);
alloc[len] = '\0';
return alloc;
}
cl_unrar_error_t unrar_open(const char* filename, void** hArchive, char** comment, uint32_t* comment_size, uint8_t debug_flag)
{
struct RAROpenArchiveDataEx* archiveData = NULL;
HANDLE archiveHandle = NULL;
cl_unrar_error_t status = UNRAR_ERR;
if (NULL == filename || NULL == hArchive || NULL == comment || NULL == comment_size) {
unrar_dbgmsg("unrar_open: Invalid arguments.\n");
goto done;
}
/* Enable debug messages in unrar_iface.cpp */
unrar_debug = debug_flag;
archiveData = (struct RAROpenArchiveDataEx*)calloc(sizeof(struct RAROpenArchiveDataEx), 1);
if (archiveData == NULL) {
unrar_dbgmsg("unrar_open: Not enough memory to allocate main archive header data structure.\n");
status = UNRAR_EMEM;
goto done;
}
archiveData->ArcName = (char*)filename;
archiveData->OpenMode = RAR_OM_EXTRACT;
archiveData->OpFlags |= ROADOF_KEEPBROKEN;
archiveData->CmtBuf = (char*)calloc(1, CMTBUFSIZE);
if (archiveData->CmtBuf == NULL) {
unrar_dbgmsg("unrar_open: Not enough memory to allocate main archive header comment buffer.\n");
status = UNRAR_EMEM;
goto done;
}
archiveData->CmtBufSize = CMTBUFSIZE;
if (NULL == (archiveHandle = RAROpenArchiveEx(archiveData))) {
/* Failed to open archive */
unrar_dbgmsg("unrar_open: Failed to open archive: %s\n", filename);
status = unrar_retcode(archiveData->OpenResult);
goto done;
}
switch (archiveData->CmtState) {
case ERAR_SUCCESS: {
unrar_dbgmsg("unrar_open: Comments are not present in this archive.\n");
break;
}
case ERAR_BAD_DATA: {
unrar_dbgmsg("unrar_open: Archive Comments may be broken.\n");
break;
}
case ERAR_SMALL_BUF: {
unrar_dbgmsg("unrar_open: Comment buffer was too small, comments are not read completely.\n");
break;
}
case 1: {
unrar_dbgmsg("unrar_open: Archive Comments read completely.\n");
break;
}
case ERAR_NO_MEMORY: {
unrar_dbgmsg("unrar_open: Not enough memory to extract comments!\n");
break;
}
default: {
unrar_dbgmsg("unrar_open: Unknown archive comment state %u!\n", archiveData->CmtState);
}
}
if (archiveData->CmtSize > 0) {
*comment_size = MIN(archiveData->CmtSize, archiveData->CmtBufSize);
*comment = unrar_strndup(archiveData->CmtBuf, *comment_size);
if (NULL == *comment) {
unrar_dbgmsg("unrar_open: Error duplicating comment buffer.\n");
*comment_size = 0;
}
}
unrar_dbgmsg("unrar_open: Volume attribute (archive volume): %s\n", (archiveData->Flags & ROADF_VOLUME) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Archive comment present: %s\n", (archiveData->Flags & ROADF_COMMENT) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Archive lock attribute: %s\n", (archiveData->Flags & ROADF_LOCK) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Solid attribute (solid archive): %s\n", (archiveData->Flags & ROADF_SOLID) ? "yes" : "no");
unrar_dbgmsg("unrar_open: New volume naming scheme ('volname.partN.rar'): %s\n", (archiveData->Flags & ROADF_NEWNUMBERING) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Authenticity information present (obsolete): %s\n", (archiveData->Flags & ROADF_SIGNED) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Recovery record present: %s\n", (archiveData->Flags & ROADF_RECOVERY) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Block headers are encrypted: %s\n", (archiveData->Flags & ROADF_ENCHEADERS) ? "yes" : "no");
unrar_dbgmsg("unrar_open: First volume (set only by RAR 3.0 and later): %s\n", (archiveData->Flags & ROADF_FIRSTVOLUME) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Opened archive: %s\n", filename);
*hArchive = (void*)archiveHandle;
status = UNRAR_OK;
done:
if (NULL != archiveData) {
if (NULL != archiveData->CmtBuf) {
free(archiveData->CmtBuf);
archiveData->CmtBuf = NULL;
}
free(archiveData);
}
return status;
}
/**
* @brief Get file metadata from the next file header.
*
* @param hArchive Handle to the archive we're extracting.
* @param[in,out] file_metadata Pointer to a pre-allocated metadata structure.
* @return cl_unrar_error_t UNRAR_OK if metadata retrieved, UNRAR_BREAK if no more files, UNRAR_ENCRYPTED if header was encrypted, else maybe UNRAR_EMEM or UNRAR_ERR.
*/
cl_unrar_error_t unrar_peek_file_header(void* hArchive, unrar_metadata_t* file_metadata)
{
cl_unrar_error_t status = UNRAR_ERR;
struct RARHeaderDataEx headerData;
int read_header_ret = 0;
wchar_t RedirName[2048];
memset(&headerData, 0, sizeof(struct RARHeaderDataEx));
if (NULL == hArchive || NULL == file_metadata) {
unrar_dbgmsg("unrar_peek_file_header: Invalid arguments.\n");
goto done;
}
memset(file_metadata, 0, sizeof(unrar_metadata_t));
/*
* File header comments are not functional in unrar 5.6.5 and the struct member only exists for backwards compatibility.
* The unrar user manual says to set headerData.CmtBuff = NULL, and headerData.CmtBufSize = 0.
*/
headerData.CmtBuf = NULL;
headerData.CmtBufSize = 0;
headerData.RedirNameSize = 2048;
headerData.RedirName = (wchar_t*)&RedirName;
memset(headerData.RedirName, 0, headerData.RedirNameSize * sizeof(wchar_t));
read_header_ret = RARReadHeaderEx(hArchive, &headerData);
if (ERAR_SUCCESS != read_header_ret) {
status = unrar_retcode(read_header_ret);
goto done;
}
file_metadata->unpack_size = headerData.UnpSize + ((int64_t)headerData.UnpSizeHigh << 32);
file_metadata->pack_size = headerData.PackSize + ((int64_t)headerData.PackSizeHigh << 32);
strncpy(file_metadata->filename, headerData.FileName, 1024);
file_metadata->crc = headerData.FileCRC;
file_metadata->encrypted = (headerData.Flags & RHDF_ENCRYPTED) ? 1 : 0;
file_metadata->is_dir = (headerData.Flags & RHDF_DIRECTORY) ? 1 : 0;
file_metadata->method = headerData.Method;
unrar_dbgmsg("unrar_peek_file_header: Name: %s\n", headerData.FileName);
unrar_dbgmsg("unrar_peek_file_header: Directory?: %u\n", file_metadata->is_dir);
unrar_dbgmsg("unrar_peek_file_header: Target Dir: %u\n", headerData.DirTarget);
unrar_dbgmsg("unrar_peek_file_header: RAR Version: %u\n", headerData.UnpVer);
unrar_dbgmsg("unrar_peek_file_header: Packed Size: %" PRIu64 "\n", file_metadata->pack_size);
unrar_dbgmsg("unrar_peek_file_header: Unpacked Size: %" PRIu64 "\n", file_metadata->unpack_size);
// PrintTime("mtime",HeaderData.MtimeLow,HeaderData.MtimeHigh);
// PrintTime("ctime",HeaderData.CtimeLow,HeaderData.CtimeHigh);
// PrintTime("atime",HeaderData.AtimeLow,HeaderData.AtimeHigh);
if (headerData.RedirType != 0) {
unrar_dbgmsg("unrar_peek_file_header: link type %d, target %ls\n", headerData.RedirType, headerData.RedirName);
}
status = UNRAR_OK;
done:
if (NULL != headerData.CmtBuf) {
free(headerData.CmtBuf);
headerData.CmtBuf = NULL;
}
return status;
}
cl_unrar_error_t unrar_extract_file(void* hArchive, const char* destPath, char* outputBuffer)
{
cl_unrar_error_t status = UNRAR_ERR;
int process_file_ret = 0;
if (NULL == hArchive || NULL == destPath) {
unrar_dbgmsg("unrar_extract_file: Invalid arguments.\n");
goto done;
}
if (NULL != outputBuffer) {
LPARAM UserData = (LPARAM)outputBuffer;
RARSetCallback(hArchive, CallbackProc, UserData);
}
process_file_ret = RARProcessFile(hArchive, RAR_EXTRACT, NULL, (char*)destPath);
if (ERAR_BAD_DATA == process_file_ret) {
unrar_dbgmsg("unrar_extract_file: Warning: Bad data/Invalid CRC. Attempting to scan anyways...\n");
} else if (ERAR_SUCCESS != process_file_ret) {
status = unrar_retcode(process_file_ret);
goto done;
}
#ifdef _WIN32
unrar_dbgmsg("unrar_extract_file: Extracted file to: %s\n", destPath);
#else
unrar_dbgmsg("unrar_extract_file: Extracted file to: %s\n", destPath);
#endif
status = UNRAR_OK;
done:
return status;
}
cl_unrar_error_t unrar_skip_file(void* hArchive)
{
cl_unrar_error_t status = UNRAR_ERR;
int process_file_ret = 0;
if (NULL == hArchive) {
unrar_dbgmsg("unrar_skip_file: Invalid arguments.\n");
goto done;
}
process_file_ret = RARProcessFile(hArchive, RAR_SKIP, NULL, NULL);
if (ERAR_SUCCESS != process_file_ret) {
status = unrar_retcode(process_file_ret);
goto done;
}
unrar_dbgmsg("unrar_skip_file: File skipped.\n");
status = UNRAR_OK;
done:
return status;
}
void unrar_close(void* hArchive)
{
RARCloseArchive(hArchive);
}
int CALLBACK CallbackProc(UINT msg, LPARAM UserData, LPARAM P1, LPARAM P2)
{
int status = 1; /* -1 to cancel, 1 to continue */
switch (msg) {
case UCM_CHANGEVOLUMEW: {
/* We don't support RAR's split into multiple volumes
* ClamAV is not aware of more than 1 file at a time */
status = -1;
unrar_dbgmsg("CallbackProc: Archive has multiple volumes, but we don't support multiple volumes.\n");
break;
}
case UCM_PROCESSDATA: {
char* UserBuffer = (char*)UserData;
if (UserBuffer == NULL) {
/* No buffer provided, continue with extraction to a temp file. */
status = 1;
unrar_dbgmsg("CallbackProc: Extracting to a new tempfile!\n");
} else {
/* Buffer provided, write to it and cancel extraction to the temp file. */
memcpy(UserBuffer, (char*)P1, P2);
status = -1;
unrar_dbgmsg("CallbackProc: Extracting %lu bytes of data to a provided buffer.\n", P2);
}
break;
}
case UCM_NEEDPASSWORDW: {
/* Let's try an empty password. Probably won't work. */
wchar_t* password_buffer = (wchar_t*)P1;
if (NULL == password_buffer || P2 == 0) {
status = -1;
unrar_dbgmsg("CallbackProc: P1 callback argument is invalid.\n");
break;
}
memset(password_buffer, 0, P2 * sizeof(wchar_t));
status = 1;
unrar_dbgmsg("CallbackProc: Password required, attempting empty password.\n");
break;
}
default: {
/* ... */
unrar_dbgmsg("CallbackProc: Unexpected callback type!\n");
}
}
return status;
}
} /* extern "C" */