1
/* This file is part of libmspack.
2
* (C) 2003-2004 Stuart Caie.
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
7
* For further details, see the file COPYING.LIB distributed with libmspack
11
#include "clamav-config.h"
17
int mspack_version(int entity) {
19
case MSPACK_VER_LIBRARY:
20
case MSPACK_VER_SYSTEM:
21
case MSPACK_VER_MSCABD:
22
case MSPACK_VER_MSCHMD:
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:
39
int mspack_sys_selftest_internal(int offt_size) {
40
return (sizeof(off_t) == offt_size) ? MSPACK_ERR_OK : MSPACK_ERR_SEEK;
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);
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)
57
if (!system || !file || !length) return MSPACK_ERR_OPEN;
59
/* get current offset */
60
current = system->tell(file);
62
/* seek to end of file */
63
if (system->seek(file, (off_t) 0, MSPACK_SYS_SEEK_END)) {
64
return MSPACK_ERR_SEEK;
67
/* get offset of end of file */
68
*length = system->tell(file);
70
/* seek back to original offset */
71
if (system->seek(file, current, MSPACK_SYS_SEEK_START)) {
72
return MSPACK_ERR_SEEK;
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
86
#ifdef MSPACK_NO_DEFAULT_SYSTEM
87
struct mspack_system *mspack_default_system = NULL;
90
/* implementation of mspack_default_system for standard C library */
97
struct mspack_file_p {
103
static struct mspack_file *msp_open(struct mspack_system *this,
104
char *filename, int mode)
106
struct mspack_file_p *fh;
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;
117
if ((fh = malloc(sizeof(struct mspack_file_p)))) {
120
if ((fh->fh = fopen(filename, fmode))) return (struct mspack_file *) fh;
126
static struct mspack_file *msp_dopen(struct mspack_system *this,
129
struct mspack_file_p *fh;
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;
140
if ((fh = malloc(sizeof(struct mspack_file_p)))) {
141
fh->name = "descriptor";
143
if ((fh->fh = fdopen(desc, fmode))) return (struct mspack_file *) fh;
149
static void msp_close(struct mspack_file *file) {
150
struct mspack_file_p *this = (struct mspack_file_p *) file;
157
static int msp_read(struct mspack_file *file, void *buffer, int bytes) {
158
struct mspack_file_p *this = (struct mspack_file_p *) file;
160
size_t count = fread(buffer, 1, (size_t) bytes, this->fh);
161
if (!ferror(this->fh)) return (int) count;
166
static int msp_write(struct mspack_file *file, void *buffer, int bytes) {
167
struct mspack_file_p *this = (struct mspack_file_p *) file;
169
size_t count = fwrite(buffer, 1, (size_t) bytes, this->fh);
170
if (!ferror(this->fh)) return (int) count;
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;
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;
185
return fseeko(this->fh, offset, mode);
187
return fseek(this->fh, offset, mode);
193
static off_t msp_tell(struct mspack_file *file) {
194
struct mspack_file_p *this = (struct mspack_file_p *) file;
196
return (this) ? (off_t) ftello(this->fh) : 0;
198
return (this) ? (off_t) ftell(this->fh) : 0;
202
static void msp_msg(struct mspack_file *file, char *format, ...) {
206
va_start(ap, format);
207
vsnprintf(buff, 512, format, ap);
209
cli_dbgmsg("libmspack: %s\n", buff);
212
static void *msp_alloc(struct mspack_system *this, size_t bytes) {
214
/* make uninitialised data obvious */
215
char *buf = malloc(bytes + 8);
216
if (buf) memset(buf, 0xDC, bytes);
217
*((size_t *)buf) = bytes;
220
return cli_calloc(bytes, 1);
224
static void msp_free(void *buffer) {
230
bytes = *((size_t *)buf);
231
/* make freed data obvious */
232
memset(buf, 0xED, bytes);
240
static void msp_copy(void *src, void *dest, size_t bytes) {
241
memcpy(dest, src, bytes);
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
249
struct mspack_system *mspack_default_system = &msp_system;