~vcs-imports/clamav/main-old

« back to all changes in this revision

Viewing changes to libclamav/msexpand.c

  • Committer: nervoso
  • Date: 2006-05-21 15:16:39 UTC
  • Revision ID: Arch-1:clamav@arch.ubuntu.com%clamav--MAIN--0--patch-1959
repository moved to cvs.clamav.net

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  msexpand: Microsoft "compress.exe/expand.exe" compatible decompressor
3
 
 *
4
 
 *  Copyright (c) 2000 Martin Hinner <mhi@penguin.cz>
5
 
 *  Algorithm & data structures by M. Winterhoff <100326.2776@compuserve.com>
6
 
 *
7
 
 *  Corrected and adapted to ClamAV by Tomasz Kojm <tkojm@clamav.net>
8
 
 *
9
 
 *  This program is free software; you can redistribute it and/or modify
10
 
 *  it under the terms of the GNU General Public License as published by
11
 
 *  the Free Software Foundation; either version 2, or (at your option)
12
 
 *  any later version.
13
 
 *
14
 
 *  This program is distributed in the hope that it will be useful,
15
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 *  GNU General Public License for more details.
18
 
 *
19
 
 *  You should have received a copy of the GNU General Public License
20
 
 *  along with this program; if not, write to the Free Software
21
 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
 
 *  MA 02110-1301, USA.
23
 
 */
24
 
 
25
 
#include <stdio.h>
26
 
#include <stdlib.h>
27
 
#include <unistd.h>
28
 
#include <string.h>
29
 
 
30
 
#if HAVE_CONFIG_H
31
 
#include "clamav-config.h"
32
 
#endif
33
 
#include "cltypes.h"
34
 
#include "others.h"
35
 
#include "msexpand.h"
36
 
 
37
 
int cli_msexpand(FILE *in, FILE *out)
38
 
{
39
 
        int bits, ch, i, j, len, mask;
40
 
        unsigned char *buffer;
41
 
        uint32_t magic1, magic2, magic3, filesize;
42
 
        uint16_t reserved;
43
 
 
44
 
 
45
 
    if(fread(&magic1, sizeof(magic1), 1, in) != 1) {
46
 
        return -1;
47
 
    }
48
 
 
49
 
    if(magic1 == le32_to_host(0x44445A53L))
50
 
    {
51
 
        if(fread(&magic2, sizeof(magic2), 1, in) != 1) {
52
 
            return -1;
53
 
        }
54
 
 
55
 
        if(fread(&reserved, sizeof(reserved), 1, in) != 1) {
56
 
            return -1;
57
 
        }
58
 
 
59
 
        if(fread(&filesize, sizeof(filesize), 1, in) != 1) {
60
 
            return -1;
61
 
        }
62
 
 
63
 
        if(magic2 != le32_to_host(0x3327F088L))
64
 
        {
65
 
            cli_warnmsg("msexpand: Not a MS-compressed file\n");
66
 
            return -1;
67
 
        }
68
 
 
69
 
    } else
70
 
    if(magic1 == le32_to_host(0x4A41574BL))
71
 
    {
72
 
        if(fread(&magic2, sizeof(magic2), 1, in) != 1) {
73
 
            return -1;
74
 
        }
75
 
 
76
 
        if(fread(&magic3, sizeof(magic3), 1, in) != 1) {
77
 
            return -1;
78
 
        }
79
 
 
80
 
        if(fread(&reserved, sizeof(reserved), 1, in) != 1) {
81
 
            return -1;
82
 
        }
83
 
 
84
 
        if(magic2 != le32_to_host(0xD127F088L) || magic3 != le32_to_host(0x00120003L))
85
 
        {
86
 
            cli_warnmsg("msexpand: Not a MS-compressed file\n");
87
 
            return -1;
88
 
        }
89
 
 
90
 
        cli_warnmsg("msexpand: unsupported version 6.22\n");
91
 
        return -1;
92
 
 
93
 
    } else {
94
 
        cli_warnmsg("msexpand: Not a MS-compressed file\n");
95
 
        return -1;
96
 
    }
97
 
 
98
 
    if((buffer = (unsigned char *) cli_calloc(4096, sizeof(char))) == NULL) {
99
 
        cli_errmsg("msexpand: Can't allocate memory\n");
100
 
        return -1;
101
 
    }
102
 
 
103
 
    i = 4096 - 16;
104
 
 
105
 
    while (1) {
106
 
        if((bits = fgetc(in)) == EOF)
107
 
            break;
108
 
 
109
 
        for(mask = 0x01; mask & 0xFF; mask <<= 1) {
110
 
            if(!(bits & mask)) {
111
 
                if((j = fgetc(in)) == EOF)
112
 
                    break;
113
 
                len = fgetc(in);
114
 
                j += (len & 0xF0) << 4;
115
 
                len = (len & 15) + 3;
116
 
                while(len--) {
117
 
                    buffer[i] = buffer[j];
118
 
                    if(fwrite(&buffer[i], sizeof(unsigned char), 1, out) != 1) {
119
 
                        free(buffer);
120
 
                        return -1;
121
 
                    }
122
 
                    j++;
123
 
                    j %= 4096;
124
 
                    i++;
125
 
                    i %= 4096;
126
 
                }
127
 
            } else {
128
 
                if((ch = fgetc(in)) == EOF)
129
 
                    break;
130
 
 
131
 
                buffer[i] = ch;
132
 
                if(fwrite(&buffer[i], sizeof(unsigned char), 1, out) != 1) {
133
 
                    free(buffer);
134
 
                    return -1;
135
 
                }
136
 
                i++;
137
 
                i %= 4096;
138
 
            }
139
 
        }
140
 
    }
141
 
 
142
 
    free(buffer);
143
 
    return 0;
144
 
}