File: xz_iface.c

package info (click to toggle)
clamav 0.98.7+dfsg-0+deb6u2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 60,204 kB
  • ctags: 49,129
  • sloc: cpp: 267,090; ansic: 152,211; sh: 35,196; python: 2,630; makefile: 2,220; perl: 1,690; pascal: 1,218; lisp: 184; csh: 117; xml: 38; asm: 32; exp: 4
file content (81 lines) | stat: -rw-r--r-- 2,566 bytes parent folder | download
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
/*
 *  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

#include "clamav.h"
#include "7z/XzCrc64.h"
#include "xz_iface.h"

void *__xz_wrap_alloc(void *unused, size_t size);
void __xz_wrap_free(void *unused, void *freeme);

void *__xz_wrap_alloc(void *unused, size_t size) {
    UNUSEDPARAM(unused);
    if(!size || size > CLI_MAX_ALLOCATION)
	return NULL;
    if(!size || size > CLI_MAX_ALLOCATION) {
	cli_dbgmsg("xz_iface: Attempt to allocate %lu bytes exceeds CLI_MAX_ALLOCATION.\n",
                   (unsigned long int) size);
	return NULL;
    }
    return cli_malloc(size);
}
void __xz_wrap_free(void *unused, void *freeme) {
    UNUSEDPARAM(unused);
    free(freeme);
}

static ISzAlloc g_Alloc = { __xz_wrap_alloc, __xz_wrap_free };
    
int cli_XzInit(struct CLI_XZ *XZ) {
    if (SZ_OK != XzUnpacker_Create(&XZ->state, &g_Alloc))
        return XZ_RESULT_DATA_ERROR;
    if (g_Crc64Table[1] == 0)
        Crc64GenerateTable();
    return XZ_RESULT_OK;
}
	
void cli_XzShutdown(struct CLI_XZ *XZ) {
    XzUnpacker_Free(&XZ->state);
}

int cli_XzDecode(struct CLI_XZ *XZ) {
    SRes res;
    SizeT outbytes, inbytes;

    inbytes = XZ->avail_in;
    outbytes = XZ->avail_out;
    res = XzUnpacker_Code(&XZ->state, XZ->next_out, &outbytes, 
                          XZ->next_in, &inbytes, CODER_FINISH_ANY, &XZ->status);
    XZ->avail_in -= inbytes;
    XZ->next_in += inbytes;
    XZ->avail_out -= outbytes;
    XZ->next_out += outbytes;
    if (XZ->status == CODER_STATUS_FINISHED_WITH_MARK || XzUnpacker_IsStreamWasFinished(&XZ->state))
        return XZ_STREAM_END;
    if (XZ->status == CODER_STATUS_NOT_FINISHED && XZ->avail_out == 0)
        return XZ_RESULT_OK;
    if (((inbytes == 0) && (outbytes == 0)) || res != SZ_OK)
	return XZ_RESULT_DATA_ERROR;
    return XZ_RESULT_OK;
}