~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/lzma_iface.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2007-2008 Sourcefire, Inc.
 
2
 *  Copyright (C) 2009 Sourcefire, Inc.
3
3
 *
4
 
 *  Authors: Alberto Wu
 
4
 *  Authors: aCaB
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License version 2 as
18
18
 *  MA 02110-1301, USA.
19
19
 */
20
20
 
21
 
/* a cleaner state interface to LZMA */
 
21
/* zlib-alike state interface to LZMA */
22
22
 
 
23
#if HAVE_CONFIG_H
 
24
#include "clamav-config.h"
 
25
#endif
23
26
 
24
27
#include "lzma_iface.h"
25
 
#include "LzmaStateDecode.h"
26
 
#include "cltypes.h"
27
 
 
28
 
/* we don't need zlib, and zlib defines Byte, that lzma also defines.
29
 
 * Enabling prefixes for zlib types avoids problems, and since
30
 
 * we don't call any zlib functions here avoids unresolved symbols too */
31
 
#define Z_PREFIX
32
 
#include "others.h"
33
 
 
34
 
struct CLI_LZMA_tag {
35
 
  CLzmaDecoderState state;
36
 
  unsigned char *next_in;
37
 
  SizeT avail_in;
38
 
  unsigned char *next_out;
39
 
  SizeT avail_out;
40
 
  int initted;
41
 
  uint64_t usize;
42
 
};
43
 
 
44
 
int cli_LzmaInit(CLI_LZMA **Lp, uint64_t size_override) {
45
 
  CLI_LZMA *L = *Lp;
46
 
 
47
 
  if(!L) {
48
 
          *Lp = L = cli_calloc(sizeof(*L), 1);
49
 
          if(!L) {
50
 
                  return CL_EMEM;
51
 
          }
52
 
  }
53
 
 
54
 
  L->initted = 0;
55
 
  if(size_override) L->usize=size_override;
56
 
 
57
 
  if (!L->next_in || L->avail_in < LZMA_PROPERTIES_SIZE + 8) return LZMA_RESULT_OK;
58
 
  if (LzmaDecodeProperties(&L->state.Properties, L->next_in, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
59
 
    return LZMA_RESULT_DATA_ERROR;
60
 
 
61
 
  L->next_in += LZMA_PROPERTIES_SIZE;
62
 
  L->avail_in -= LZMA_PROPERTIES_SIZE;
63
 
 
64
 
  if (!L->usize) {
65
 
    L->usize=(uint64_t)cli_readint32(L->next_in) + ((uint64_t)cli_readint32(L->next_in+4)<<32);
66
 
    L->next_in += 8;
67
 
    L->avail_in -= 8;
68
 
  }
 
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
}
69
52
    
70
 
  if (!(L->state.Probs = (CProb *)cli_malloc(LzmaGetNumProbs(&L->state.Properties) * sizeof(CProb))))
71
 
    return LZMA_RESULT_DATA_ERROR;
72
 
 
73
 
  if (!(L->state.Dictionary = (unsigned char *)cli_malloc(L->state.Properties.DictionarySize))) {
74
 
    free(L->state.Probs);
75
 
    return LZMA_RESULT_DATA_ERROR;
76
 
  }
77
 
 
78
 
  L->initted = 1;
79
 
 
80
 
  LzmaDecoderInit(&L->state);
81
 
  return LZMA_RESULT_OK;
82
 
}
83
 
 
84
 
void cli_LzmaShutdown(CLI_LZMA **Lp) {
85
 
  CLI_LZMA *L;
86
 
 
87
 
  if(!Lp) return;
88
 
  L = *Lp;
89
 
  if(L->initted) {
90
 
    if(L->state.Probs) free(L->state.Probs);
91
 
    if(L->state.Dictionary) free(L->state.Dictionary);
92
 
  }
93
 
  free(L);
94
 
  *Lp = NULL;
95
 
  return;
96
 
}
97
 
 
98
 
int cli_LzmaDecode(CLI_LZMA **Lp, struct stream_state* state) {
99
 
  int res;
100
 
  SizeT processed_in, processed_out;
101
 
  CLI_LZMA* L = *Lp;
102
 
 
103
 
  if(L) {
104
 
          L->avail_in = state->avail_in;
105
 
          L->next_in = state->next_in;
106
 
          L->avail_out = state->avail_out;
107
 
          L->next_out = state->next_out;
108
 
  }
109
 
 
110
 
  if (!L || !L->initted) {
111
 
          if(cli_LzmaInit(Lp, 0) != LZMA_RESULT_OK)
112
 
                  return LZMA_RESULT_DATA_ERROR;
113
 
          L = *Lp;
114
 
  }
115
 
 
116
 
 
117
 
  res = LzmaDecode(&L->state, L->next_in, L->avail_in, &processed_in, L->next_out, L->avail_out, &processed_out, (L->avail_in==0));
118
 
 
119
 
  L->next_in += processed_in;
120
 
  L->avail_in -= processed_in;
121
 
  L->next_out += processed_out;
122
 
  L->avail_out -= processed_out;
123
 
 
124
 
  state->avail_in = L->avail_in;
125
 
  state->next_in = L->next_in;
126
 
  state->avail_out = L->avail_out;
127
 
  state->next_out = L->next_out;
128
 
 
129
 
  return res;
130
 
}
131
 
 
132
 
int cli_LzmaInitUPX(CLI_LZMA **Lp, uint32_t dictsz) {
133
 
  CLI_LZMA *L = *Lp;
134
 
 
135
 
  if(!L) {
136
 
    *Lp = L = cli_calloc(sizeof(*L), 1);
137
 
    if(!L) {
138
 
      return LZMA_RESULT_DATA_ERROR;
139
 
    }
140
 
  }
141
 
 
142
 
  L->state.Properties.pb = 2; /* FIXME: these  */
143
 
  L->state.Properties.lp = 0; /* values may    */
144
 
  L->state.Properties.lc = 3; /* not be static */
145
 
 
146
 
  L->state.Properties.DictionarySize = dictsz;
147
 
 
148
 
  if (!(L->state.Probs = (CProb *)cli_malloc(LzmaGetNumProbs(&L->state.Properties) * sizeof(CProb))))
149
 
    return LZMA_RESULT_DATA_ERROR;
150
 
 
151
 
  if (!(L->state.Dictionary = (unsigned char *)cli_malloc(L->state.Properties.DictionarySize))) {
152
 
    free(L->state.Probs);
153
 
    return LZMA_RESULT_DATA_ERROR;
154
 
  }
155
 
 
156
 
  L->initted = 1;
157
 
 
158
 
  LzmaDecoderInit(&L->state);
159
 
  return LZMA_RESULT_OK;
 
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;
160
126
}