2013-10-08 17:17:44 -04:00
|
|
|
/*
|
2025-02-14 10:24:30 -05:00
|
|
|
* Copyright (C) 2013-2025 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
2013-10-08 17:17:44 -04:00
|
|
|
* Copyright (C) 2013 Sourcefire, Inc.
|
|
|
|
*
|
|
|
|
* Authors: Steven Morgan (smorgan@sourcefire.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-01 19:38:01 -04:00
|
|
|
#include "clamav.h"
|
2013-10-09 17:18:27 -04:00
|
|
|
#include "7z/XzCrc64.h"
|
2013-10-08 17:17:44 -04:00
|
|
|
#include "xz_iface.h"
|
|
|
|
|
2014-07-10 18:11:49 -04:00
|
|
|
void *__xz_wrap_alloc(void *unused, size_t size);
|
|
|
|
void __xz_wrap_free(void *unused, void *freeme);
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
void *__xz_wrap_alloc(void *unused, size_t size)
|
|
|
|
{
|
2014-07-10 18:11:49 -04:00
|
|
|
UNUSEDPARAM(unused);
|
2024-01-08 22:50:17 -05:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!size || size > CLI_MAX_ALLOCATION) {
|
|
|
|
return NULL;
|
2013-10-08 17:17:44 -04:00
|
|
|
}
|
2024-01-08 22:50:17 -05:00
|
|
|
|
2022-05-09 14:28:34 -07:00
|
|
|
return cli_max_malloc(size);
|
2013-10-08 17:17:44 -04:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
void __xz_wrap_free(void *unused, void *freeme)
|
|
|
|
{
|
2014-07-10 18:11:49 -04:00
|
|
|
UNUSEDPARAM(unused);
|
2013-10-08 17:17:44 -04:00
|
|
|
free(freeme);
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static ISzAlloc g_Alloc = {__xz_wrap_alloc, __xz_wrap_free};
|
|
|
|
|
|
|
|
int cli_XzInit(struct CLI_XZ *XZ)
|
|
|
|
{
|
2013-10-08 17:17:44 -04:00
|
|
|
if (SZ_OK != XzUnpacker_Create(&XZ->state, &g_Alloc))
|
|
|
|
return XZ_RESULT_DATA_ERROR;
|
2013-10-09 17:18:27 -04:00
|
|
|
if (g_Crc64Table[1] == 0)
|
|
|
|
Crc64GenerateTable();
|
2013-10-08 17:17:44 -04:00
|
|
|
return XZ_RESULT_OK;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
void cli_XzShutdown(struct CLI_XZ *XZ)
|
|
|
|
{
|
2019-02-10 01:22:03 -05:00
|
|
|
if (!XZ)
|
|
|
|
return;
|
2013-10-08 17:17:44 -04:00
|
|
|
XzUnpacker_Free(&XZ->state);
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
int cli_XzDecode(struct CLI_XZ *XZ)
|
|
|
|
{
|
2013-10-08 17:17:44 -04:00
|
|
|
SRes res;
|
|
|
|
SizeT outbytes, inbytes;
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
inbytes = XZ->avail_in;
|
2013-10-08 17:17:44 -04:00
|
|
|
outbytes = XZ->avail_out;
|
Code cleanup: Refactor to clean up formatting issues
Refactored the clamscan code that determines 'what to scan' in order
to clean up some very messy logic and also to get around a difference in
how vscode and clang-format handle formatting #ifdef blocks in the
middle of an else/if.
In addition to refactoring, there is a slight behavior improvement. With
this change, doing `clamscan blah -` will now scan `blah` and then also
scan `stdin`. You can even do `clamscan - blah` to now scan `stdin` and
then scan `blah`. Before, The `-` had to be the only "filename" argument
in order to scan from stdin.
In addition, added a bunch of extra empty lines or changing multi-line
function calls to single-line function calls in order to get around a
bug in clang-format with these two options do not playing nice together:
- AlignConsecutiveAssignments: true
- AlignAfterOpenBracket: true
AlignAfterOpenBracket is not taking account the spaces inserted by
AlignConsecutiveAssignments, so you end up with stuff like this:
```c
bleeblah = 1;
blah = function(arg1,
arg2,
arg3);
// ^--- these args 4-left from where they should be.
```
VSCode, meanwhile, somehow fixes this whitespace issue so code that is
correctly formatted by VSCode doesn't have this bug, meaning that:
1. The clang-format check in GH Actions fails.
2. We'd all have to stop using format-on-save in VSCode and accept the
bug if we wanted those GH Actions tests to pass.
Adding an empty line before variable assignments from multi-line
function calls evades the buggy behavior.
This commit should resolve the clang-format github action test failures,
for now.
2022-03-10 20:55:13 -08:00
|
|
|
|
|
|
|
res = XzUnpacker_Code(&XZ->state, XZ->next_out, &outbytes,
|
|
|
|
XZ->next_in, &inbytes, CODER_FINISH_ANY, &XZ->status);
|
|
|
|
|
2013-10-08 17:17:44 -04:00
|
|
|
XZ->avail_in -= inbytes;
|
|
|
|
XZ->next_in += inbytes;
|
|
|
|
XZ->avail_out -= outbytes;
|
|
|
|
XZ->next_out += outbytes;
|
2013-10-11 12:04:29 -04:00
|
|
|
if (XZ->status == CODER_STATUS_FINISHED_WITH_MARK || XzUnpacker_IsStreamWasFinished(&XZ->state))
|
2013-10-08 17:17:44 -04:00
|
|
|
return XZ_STREAM_END;
|
|
|
|
if (XZ->status == CODER_STATUS_NOT_FINISHED && XZ->avail_out == 0)
|
|
|
|
return XZ_RESULT_OK;
|
2017-06-19 15:41:17 -04:00
|
|
|
if (((inbytes == 0) && (outbytes == 0)) || res != SZ_OK) {
|
2018-12-03 12:40:13 -05:00
|
|
|
return XZ_RESULT_DATA_ERROR;
|
2017-06-19 15:41:17 -04:00
|
|
|
}
|
2013-10-08 17:17:44 -04:00
|
|
|
return XZ_RESULT_OK;
|
|
|
|
}
|