~ubuntu-branches/ubuntu/breezy/clamav/breezy-backports

« back to all changes in this revision

Viewing changes to libclamav/mspack/system.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Gran
  • Date: 2005-09-19 09:05:59 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050919090559-hikpqduq8yx5qxo2
Tags: 0.87-1
* New upstream version
  - Fixes CAN-2005-2920 and CAN-2005-2919 (closes: #328660)
* New logcheck line for clamav-daemon (closes: #323132)
* relibtoolize and apply kfreebsd patch (closes: #327707)
* Make sure init.d script starts freshclam up again after upgrade when run
  from if-up.d (closes: #328912)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of libmspack.
 
2
 * (C) 2003-2004 Stuart Caie.
 
3
 *
 
4
 * libmspack is free software; you can redistribute it and/or modify it under
 
5
 * the terms of the GNU Lesser General Public License (LGPL) version 2.1
 
6
 *
 
7
 * For further details, see the file COPYING.LIB distributed with libmspack
 
8
 */
 
9
 
 
10
#if HAVE_CONFIG_H
 
11
#include "clamav-config.h"
 
12
#endif
 
13
 
 
14
#include <mspack.h>
 
15
#include "others.h"
 
16
 
 
17
int mspack_version(int entity) {
 
18
  switch (entity) {
 
19
  case MSPACK_VER_LIBRARY:
 
20
  case MSPACK_VER_SYSTEM:
 
21
  case MSPACK_VER_MSCABD:
 
22
  case MSPACK_VER_MSCHMD:
 
23
    return 1;
 
24
  case MSPACK_VER_MSCABC:
 
25
  case MSPACK_VER_MSCHMC:
 
26
  case MSPACK_VER_MSLITD:
 
27
  case MSPACK_VER_MSLITC:
 
28
  case MSPACK_VER_MSHLPD:
 
29
  case MSPACK_VER_MSHLPC:
 
30
  case MSPACK_VER_MSSZDDD:
 
31
  case MSPACK_VER_MSSZDDC:
 
32
  case MSPACK_VER_MSKWAJD:
 
33
  case MSPACK_VER_MSKWAJC:
 
34
    return 0;
 
35
  }
 
36
  return -1;
 
37
}
 
38
 
 
39
int mspack_sys_selftest_internal(int offt_size) {
 
40
  return (sizeof(off_t) == offt_size) ? MSPACK_ERR_OK : MSPACK_ERR_SEEK;
 
41
}
 
42
 
 
43
/* validates a system structure */
 
44
int mspack_valid_system(struct mspack_system *sys) {
 
45
  return (sys != NULL) && (sys->open != NULL) && (sys->close != NULL) &&
 
46
    (sys->read != NULL) && (sys->write != NULL) && (sys->seek != NULL) &&
 
47
    (sys->tell != NULL) && (sys->message != NULL) && (sys->alloc != NULL) &&
 
48
    (sys->free != NULL) && (sys->copy != NULL) && (sys->null_ptr == NULL);
 
49
}
 
50
 
 
51
/* returns the length of a file opened for reading */
 
52
int mspack_sys_filelen(struct mspack_system *system,
 
53
                       struct mspack_file *file, off_t *length)
 
54
{
 
55
  off_t current;
 
56
 
 
57
  if (!system || !file || !length) return MSPACK_ERR_OPEN;
 
58
 
 
59
  /* get current offset */
 
60
  current = system->tell(file);
 
61
 
 
62
  /* seek to end of file */
 
63
  if (system->seek(file, (off_t) 0, MSPACK_SYS_SEEK_END)) {
 
64
    return MSPACK_ERR_SEEK;
 
65
  }
 
66
 
 
67
  /* get offset of end of file */
 
68
  *length = system->tell(file);
 
69
 
 
70
  /* seek back to original offset */
 
71
  if (system->seek(file, current, MSPACK_SYS_SEEK_START)) {
 
72
    return MSPACK_ERR_SEEK;
 
73
  }
 
74
 
 
75
  return MSPACK_ERR_OK;
 
76
}
 
77
 
 
78
 
 
79
 
 
80
/* definition of mspack_default_system -- if the library is compiled with
 
81
 * MSPACK_NO_DEFAULT_SYSTEM, no default system will be provided. Otherwise,
 
82
 * an appropriate default system (e.g. the standard C library, or some native
 
83
 * API calls)
 
84
 */
 
85
 
 
86
#ifdef MSPACK_NO_DEFAULT_SYSTEM
 
87
struct mspack_system *mspack_default_system = NULL;
 
88
#else
 
89
 
 
90
/* implementation of mspack_default_system for standard C library */
 
91
 
 
92
#include <stdio.h>
 
93
#include <stdlib.h>
 
94
#include <string.h>
 
95
#include <stdarg.h>
 
96
 
 
97
struct mspack_file_p {
 
98
  FILE *fh;
 
99
  const char *name;
 
100
  int desc;
 
101
};
 
102
 
 
103
static struct mspack_file *msp_open(struct mspack_system *this,
 
104
                                    char *filename, int mode)
 
105
{
 
106
  struct mspack_file_p *fh;
 
107
  char *fmode;
 
108
 
 
109
  switch (mode) {
 
110
  case MSPACK_SYS_OPEN_READ:   fmode = "rb";  break;
 
111
  case MSPACK_SYS_OPEN_WRITE:  fmode = "wb";  break;
 
112
  case MSPACK_SYS_OPEN_UPDATE: fmode = "r+b"; break;
 
113
  case MSPACK_SYS_OPEN_APPEND: fmode = "ab";  break;
 
114
  default: return NULL;
 
115
  }
 
116
 
 
117
  if ((fh = malloc(sizeof(struct mspack_file_p)))) {
 
118
    fh->name = filename;
 
119
    fh->desc = 0;
 
120
    if ((fh->fh = fopen(filename, fmode))) return (struct mspack_file *) fh;
 
121
    free(fh);
 
122
  }
 
123
  return NULL;
 
124
}
 
125
 
 
126
static struct mspack_file *msp_dopen(struct mspack_system *this,
 
127
                                    int desc, int mode)
 
128
{
 
129
  struct mspack_file_p *fh;
 
130
  char *fmode;
 
131
 
 
132
  switch (mode) {
 
133
  case MSPACK_SYS_OPEN_READ:   fmode = "rb";  break;
 
134
  case MSPACK_SYS_OPEN_WRITE:  fmode = "wb";  break;
 
135
  case MSPACK_SYS_OPEN_UPDATE: fmode = "r+b"; break;
 
136
  case MSPACK_SYS_OPEN_APPEND: fmode = "ab";  break;
 
137
  default: return NULL;
 
138
  }
 
139
 
 
140
  if ((fh = malloc(sizeof(struct mspack_file_p)))) {
 
141
    fh->name = "descriptor";
 
142
    fh->desc = desc;
 
143
    if ((fh->fh = fdopen(desc, fmode))) return (struct mspack_file *) fh;
 
144
    free(fh);
 
145
  }
 
146
  return NULL;
 
147
}
 
148
 
 
149
static void msp_close(struct mspack_file *file) {
 
150
  struct mspack_file_p *this = (struct mspack_file_p *) file;
 
151
  if (this) {
 
152
    fclose(this->fh);
 
153
    free(this);
 
154
  }
 
155
}
 
156
 
 
157
static int msp_read(struct mspack_file *file, void *buffer, int bytes) {
 
158
  struct mspack_file_p *this = (struct mspack_file_p *) file;
 
159
  if (this) {
 
160
    size_t count = fread(buffer, 1, (size_t) bytes, this->fh);
 
161
    if (!ferror(this->fh)) return (int) count;
 
162
  }
 
163
  return -1;
 
164
}
 
165
 
 
166
static int msp_write(struct mspack_file *file, void *buffer, int bytes) {
 
167
  struct mspack_file_p *this = (struct mspack_file_p *) file;
 
168
  if (this) {
 
169
    size_t count = fwrite(buffer, 1, (size_t) bytes, this->fh);
 
170
    if (!ferror(this->fh)) return (int) count;
 
171
  }
 
172
  return -1;
 
173
}
 
174
 
 
175
static int msp_seek(struct mspack_file *file, off_t offset, int mode) {
 
176
  struct mspack_file_p *this = (struct mspack_file_p *) file;
 
177
  if (this) {
 
178
    switch (mode) {
 
179
    case MSPACK_SYS_SEEK_START: mode = SEEK_SET; break;
 
180
    case MSPACK_SYS_SEEK_CUR:   mode = SEEK_CUR; break;
 
181
    case MSPACK_SYS_SEEK_END:   mode = SEEK_END; break;
 
182
    default: return -1;
 
183
    }
 
184
#ifdef HAVE_FSEEKO
 
185
    return fseeko(this->fh, offset, mode);
 
186
#else
 
187
    return fseek(this->fh, offset, mode);
 
188
#endif
 
189
  }
 
190
  return -1;
 
191
}
 
192
 
 
193
static off_t msp_tell(struct mspack_file *file) {
 
194
  struct mspack_file_p *this = (struct mspack_file_p *) file;
 
195
#ifdef HAVE_FSEEKO
 
196
  return (this) ? (off_t) ftello(this->fh) : 0;
 
197
#else
 
198
  return (this) ? (off_t) ftell(this->fh) : 0;
 
199
#endif
 
200
}
 
201
 
 
202
static void msp_msg(struct mspack_file *file, char *format, ...) {
 
203
  va_list ap;
 
204
  char buff[512];
 
205
 
 
206
  va_start(ap, format);
 
207
  vsnprintf(buff, 512, format, ap);
 
208
  va_end(ap);
 
209
  cli_dbgmsg("libmspack: %s\n", buff);
 
210
}
 
211
 
 
212
static void *msp_alloc(struct mspack_system *this, size_t bytes) {
 
213
#ifdef DEBUG
 
214
  /* make uninitialised data obvious */
 
215
  char *buf = malloc(bytes + 8);
 
216
  if (buf) memset(buf, 0xDC, bytes);
 
217
  *((size_t *)buf) = bytes;
 
218
  return &buf[8];
 
219
#else
 
220
  return cli_calloc(bytes, 1);
 
221
#endif
 
222
}
 
223
 
 
224
static void msp_free(void *buffer) {
 
225
#ifdef DEBUG
 
226
  char *buf = buffer;
 
227
  size_t bytes;
 
228
  if (buf) {
 
229
    buf -= 8;
 
230
    bytes = *((size_t *)buf);
 
231
    /* make freed data obvious */
 
232
    memset(buf, 0xED, bytes);
 
233
    free(buf);
 
234
  }
 
235
#else
 
236
  free(buffer);
 
237
#endif
 
238
}
 
239
 
 
240
static void msp_copy(void *src, void *dest, size_t bytes) {
 
241
  memcpy(dest, src, bytes);
 
242
}
 
243
 
 
244
static struct mspack_system msp_system = {
 
245
  &msp_open, &msp_dopen, &msp_close, &msp_read,  &msp_write, &msp_seek,
 
246
  &msp_tell, &msp_msg, &msp_alloc, &msp_free, &msp_copy, NULL
 
247
};
 
248
 
 
249
struct mspack_system *mspack_default_system = &msp_system;
 
250
 
 
251
#endif