File: adc.c

package info (click to toggle)
clamav 0.98.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 54,844 kB
  • sloc: cpp: 267,090; ansic: 151,215; sh: 36,044; python: 2,630; makefile: 2,224; perl: 1,690; pascal: 1,218; lisp: 184; csh: 117; xml: 38; asm: 32; exp: 4
file content (293 lines) | stat: -rw-r--r-- 10,440 bytes parent folder | download | duplicates (2)
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
/*
 *  Copyright (C) 2013 Sourcefire, Inc.
 *
 *  Authors: David Raynor <draynor@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 <stdio.h>
#include <errno.h>
#if HAVE_STRING_H
#include <string.h>
#endif

#include "shared/misc.h"
#include "clamav.h"
#include "cltypes.h"
#include "others.h"
#include "adc.h"

/* #define DEBUG_ADC */

#ifdef DEBUG_ADC
#  define adc_dbgmsg(...) cli_dbgmsg( __VA_ARGS__ )
#else
#  define adc_dbgmsg(...) ;
#endif

/* Initialize values and collect buffer
 * NOTE: buffer size must be larger than largest lookback offset */
int adc_decompressInit(adc_stream *strm)
{
    if (strm == NULL) {
        return ADC_IO_ERROR;
    }
    if (strm->state != ADC_STATE_UNINIT) {
        return ADC_DATA_ERROR;
    }

    /* Have to buffer maximum backward lookup */
    strm->buffer = (uint8_t *)calloc(ADC_BUFF_SIZE, 1);
    if (strm->buffer == NULL) {
        return ADC_MEM_ERROR;
    }
    strm->buffered = 0;
    strm->state = ADC_STATE_GETTYPE;
    strm->length = 0;
    strm->offset = 0;
    strm->curr = strm->buffer;

    return ADC_OK;
}

/* Decompress routine
 * NOTE: Reaching end of input buffer does not mean end of output.
 * It may fill the output buffer but have more to output.
 * It will only return ADC_STREAM_END if output buffer is not full.
 * It will return ADC_DATA_ERROR if it ends in the middle of a phrase
 * (i.e. in the middle of a lookback code or data run)
 */
int adc_decompress(adc_stream *strm)
{
    uint8_t bData;
    uint8_t didNothing = 1;

    /* first, the error returns based on strm */
    if ((strm == NULL) || (strm->next_in == NULL) || (strm->next_out == NULL)) {
        return ADC_IO_ERROR;
    }
    if (strm->state == ADC_STATE_UNINIT) {
        return ADC_DATA_ERROR;
    }

    cli_dbgmsg("adc_decompress: avail_in %" _sizet " avail_out %" _sizet " state %u\n",
		    strm->avail_in, strm->avail_out, strm->state);
    while (strm->avail_out) {
        /* Exit if needs more in bytes and none available */
        int needsInput;
        switch (strm->state) {
           case ADC_STATE_SHORTLOOK:
           case ADC_STATE_LONGLOOK:
               needsInput = 0;
               break;
           default:
               needsInput = 1;
               break;
        }
        if (needsInput && (strm->avail_in == 0)) {
            break;
        }
        else {
            didNothing = 0;
        }

        /* Find or execute statecode */
        switch (strm->state) {
            case ADC_STATE_GETTYPE: {
                /* Grab action code */
                bData = *(strm->next_in);
                strm->next_in++;
                strm->avail_in--;
                if (bData & 0x80) {
                    strm->state = ADC_STATE_RAWDATA;
                    strm->offset = 0;
                    strm->length = (bData & 0x7F) + 1;
                }
                else if (bData & 0x40) {
                    strm->state = ADC_STATE_LONGOP2;
                    strm->offset = 0;
                    strm->length = (bData & 0x3F) + 4;
                }
                else {
                    strm->state = ADC_STATE_SHORTOP;
                    strm->offset = (bData & 0x3) * 0x100;
                    strm->length = ((bData & 0x3C) >> 2) + 3;
                }
                adc_dbgmsg("adc_decompress: GETTYPE bData %x state %u offset %u length %u\n",
                           bData, strm->state, strm->offset, strm->length);
                break;
           }
           case ADC_STATE_LONGOP2: {
                /* Grab first offset byte */
                bData = *(strm->next_in);
                strm->next_in++;
                strm->avail_in--;
                strm->offset = bData * 0x100;
                strm->state = ADC_STATE_LONGOP1;
                adc_dbgmsg("adc_decompress: LONGOP2 bData %x state %u offset %u length %u\n",
                           bData, strm->state, strm->offset, strm->length);
                break;
           }
           case ADC_STATE_LONGOP1: {
                /* Grab second offset byte */
                bData = *(strm->next_in);
                strm->next_in++;
                strm->avail_in--;
                strm->offset += bData + 1;
                strm->state = ADC_STATE_LONGLOOK;
                adc_dbgmsg("adc_decompress: LONGOP1 bData %x state %u offset %u length %u\n",
                           bData, strm->state, strm->offset, strm->length);
                break;
           }
           case ADC_STATE_SHORTOP: {
                /* Grab offset byte */
                bData = *(strm->next_in);
                strm->next_in++;
                strm->avail_in--;
                strm->offset += bData + 1;
                strm->state = ADC_STATE_SHORTLOOK;
                adc_dbgmsg("adc_decompress: SHORTOP bData %x state %u offset %u length %u\n",
                           bData, strm->state, strm->offset, strm->length);
                break;
           }

           case ADC_STATE_RAWDATA: {
                /* Grab data */
                adc_dbgmsg("adc_decompress: RAWDATA offset %u length %u\n", strm->offset, strm->length);
                while ((strm->avail_in > 0) && (strm->avail_out > 0) && (strm->length > 0)) {
                    bData = *(strm->next_in);
                    strm->next_in++;
                    strm->avail_in--;
                    /* store to output */
                    *(strm->next_out) = bData;
                    strm->next_out++;
                    strm->avail_out--;
                    /* store to buffer */
                    if (strm->curr >= (strm->buffer + ADC_BUFF_SIZE)) {
                        strm->curr = strm->buffer;
                    }
                    *(strm->curr) = bData;
                    strm->curr++;
                    if (strm->buffered < ADC_BUFF_SIZE) {
                        strm->buffered++;
                    }
                    strm->length--;
                }
                if (strm->length == 0) {
                    /* adc_dbgmsg("adc_decompress: RAWDATADONE buffered %u avail_in %u avail_out %u \n",
                        strm->buffered, strm->avail_in, strm->avail_out); */
                    strm->state = ADC_STATE_GETTYPE;
                }
                break;
           }

           case ADC_STATE_SHORTLOOK:
           case ADC_STATE_LONGLOOK: {
                /* Copy data */
                adc_dbgmsg("adc_decompress: LOOKBACK offset %u length %u avail_in %u avail_out %u\n",
                    strm->offset, strm->length, strm->avail_in, strm->avail_out);
                while ((strm->avail_out > 0) && (strm->length > 0)) {
                    /* state validation first */
                    if (strm->offset > 0x10000) {
                        cli_dbgmsg("adc_decompress: bad LOOKBACK offset %u\n", strm->offset);
                        return ADC_DATA_ERROR;
                    }
                    else if ((strm->state == ADC_STATE_SHORTLOOK) && (strm->offset > 0x400)) {
                        cli_dbgmsg("adc_decompress: bad LOOKBACK offset %u\n", strm->offset);
                        return ADC_DATA_ERROR;
                    }
                    if (strm->offset > strm->buffered) {
                        cli_dbgmsg("adc_decompress: too large LOOKBACK offset %u\n", strm->offset);
                        return ADC_DATA_ERROR;
                    }
                    /* retrieve byte */
                    if (strm->curr >= (strm->buffer + ADC_BUFF_SIZE)) {
                        strm->curr = strm->buffer;
                    }
                    if (strm->curr >= (strm->buffer + strm->offset)) {
                        bData = *(uint8_t *)(strm->curr - strm->offset);
                    }
                    else {
                        bData = *(uint8_t *)(strm->curr + ADC_BUFF_SIZE - strm->offset);
                    }
                    /* store to output */
                    *(strm->next_out) = bData;
                    strm->next_out++;
                    strm->avail_out--;
                    /* store to buffer */
                    *(strm->curr) = bData;
                    strm->curr++;
                    if (strm->buffered < ADC_BUFF_SIZE) {
                        strm->buffered++;
                    }
                    strm->length--;
                }
                if (strm->length == 0) {
                    strm->state = ADC_STATE_GETTYPE;
                    /* adc_dbgmsg("adc_decompress: LOOKBACKDONE buffered %u avail_in %u avail_out %u \n",
                        strm->buffered, strm->avail_in, strm->avail_out); */
                }
                break;
            }

            default: {
                /* bad state */
                cli_errmsg("adc_decompress: invalid state %u\n", strm->state);
                return ADC_DATA_ERROR;
            }
        } /* end switch */
    } /* end while */

    /* There really isn't a terminator, just end of data */
    if (didNothing && strm->avail_out) {
        if (strm->state == ADC_STATE_GETTYPE) {
            /* Nothing left to do */
            return ADC_STREAM_END;
        }
        else {
            /* Ended mid phrase */
            cli_dbgmsg("adc_decompress: stream ended mid-phrase, state %u\n", strm->state);
            return ADC_DATA_ERROR;
        }
    }
    return ADC_OK;
}

/* Cleanup routine, frees buffer */
int adc_decompressEnd(adc_stream *strm)
{
    if (strm == NULL) {
        return ADC_IO_ERROR;
    }
    if (strm->state == ADC_STATE_UNINIT) {
        return ADC_DATA_ERROR;
    }

    if (strm->buffer != NULL) {
        free(strm->buffer);
    }
    strm->buffered = 0;
    strm->state = ADC_STATE_UNINIT;
    strm->length = 0;
    strm->offset = 0;

    return ADC_OK;
}