~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/lzma_iface.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2009 Sourcefire, Inc.
3
 
 *
4
 
 *  Authors: aCaB
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License version 2 as
8
 
 *  published by the Free Software Foundation.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 
 *  MA 02110-1301, USA.
19
 
 */
20
 
 
21
 
/* zlib-alike state interface to LZMA */
22
 
 
23
 
#if HAVE_CONFIG_H
24
 
#include "clamav-config.h"
25
 
#endif
26
 
 
27
 
#include "lzma_iface.h"
28
 
 
29
 
void *__lzma_wrap_alloc(void *unused, size_t size) { 
30
 
    unused = unused;
31
 
    return cli_malloc(size);
32
 
}
33
 
void __lzma_wrap_free(void *unused, void *freeme) {
34
 
    unused = unused;
35
 
    free(freeme);
36
 
}
37
 
static ISzAlloc g_Alloc = { __lzma_wrap_alloc, __lzma_wrap_free };
38
 
 
39
 
 
40
 
static unsigned char lzma_getbyte(struct CLI_LZMA *L, int *fail) {
41
 
    unsigned char c;
42
 
    if(!L->next_in || !L->avail_in) {
43
 
        *fail = 1;
44
 
        return 0;
45
 
    }
46
 
    *fail = 0;
47
 
    c = L->next_in[0];
48
 
    L->next_in++;
49
 
    L->avail_in--;
50
 
    return c;
51
 
}
52
 
    
53
 
 
54
 
int cli_LzmaInit(struct CLI_LZMA *L, uint64_t size_override) {
55
 
    int fail;
56
 
 
57
 
    if(!L->init) {
58
 
        L->p_cnt = LZMA_PROPS_SIZE;
59
 
        if(size_override)
60
 
            L->usize = size_override;
61
 
        else
62
 
            L->s_cnt = 8;
63
 
        L->init = 1;
64
 
    } else if(size_override)
65
 
        cli_warnmsg("cli_LzmaInit: ignoring late size override\n");
66
 
 
67
 
    if(L->freeme) return LZMA_RESULT_OK;
68
 
 
69
 
    while(L->p_cnt) {
70
 
        L->header[LZMA_PROPS_SIZE - L->p_cnt] = lzma_getbyte(L, &fail);
71
 
        if(fail) return LZMA_RESULT_OK;
72
 
        L->p_cnt--;
73
 
    }
74
 
 
75
 
    while(L->s_cnt) {
76
 
        uint64_t c = (uint64_t)lzma_getbyte(L, &fail);
77
 
        if(fail) return LZMA_RESULT_OK;
78
 
        L->usize = c << (8 * (8 - L->s_cnt));
79
 
        L->s_cnt--;
80
 
    }
81
 
 
82
 
    LzmaDec_Construct(&L->state);
83
 
    if(LzmaDec_Allocate(&L->state, L->header, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK)
84
 
        return LZMA_RESULT_DATA_ERROR;
85
 
    LzmaDec_Init(&L->state);
86
 
 
87
 
    L->freeme = 1;
88
 
    return LZMA_RESULT_OK;
89
 
}
90
 
        
91
 
 
92
 
void cli_LzmaShutdown(struct CLI_LZMA *L) {
93
 
    if(L->freeme)
94
 
        LzmaDec_Free(&L->state, &g_Alloc);
95
 
    return;
96
 
}
97
 
 
98
 
 
99
 
int cli_LzmaDecode(struct CLI_LZMA *L) {
100
 
    SRes res;
101
 
    SizeT outbytes, inbytes;
102
 
    ELzmaStatus status;
103
 
    ELzmaFinishMode finish;
104
 
 
105
 
    if(!L->freeme) return cli_LzmaInit(L, 0);
106
 
 
107
 
    inbytes = L->avail_in;
108
 
    if(~L->usize && L->avail_out > L->usize) {
109
 
        outbytes = L->usize;
110
 
        finish = LZMA_FINISH_END;
111
 
    } else {
112
 
        outbytes = L->avail_out;
113
 
        finish = LZMA_FINISH_ANY;
114
 
    }
115
 
    res = LzmaDec_DecodeToBuf(&L->state, L->next_out, &outbytes, L->next_in, &inbytes, finish, &status);
116
 
    L->avail_in -= inbytes;
117
 
    L->next_in += inbytes;
118
 
    L->avail_out -= outbytes;
119
 
    L->next_out += outbytes;
120
 
    if(~L->usize) L->usize -= outbytes;
121
 
    if(res != SZ_OK)
122
 
        return LZMA_RESULT_DATA_ERROR;
123
 
    if(!L->usize || status == LZMA_STATUS_FINISHED_WITH_MARK)
124
 
        return LZMA_STREAM_END;
125
 
    return LZMA_RESULT_OK;
126
 
}