Files
clamav/libclammspack/mspack/system.c
Micah Snyder (micasnyd) 9e20cdf6ea Add CMake build tooling
This patch adds experimental-quality CMake build tooling.

The libmspack build required a modification to use "" instead of <> for
header #includes. This will hopefully be included in the libmspack
upstream project when adding CMake build tooling to libmspack.

Removed use of libltdl when using CMake.

Flex & Bison are now required to build.

If -DMAINTAINER_MODE, then GPERF is also required, though it currently
doesn't actually do anything.  TODO!

I found that the autotools build system was generating the lexer output
but not actually compiling it, instead using previously generated (and
manually renamed) lexer c source. As a consequence, changes to the .l
and .y files weren't making it into the build. To resolve this, I
removed generated flex/bison files and fixed the tooling to use the
freshly generated files. Flex and bison are now required build tools.
On Windows, this adds a dependency on the winflexbison package,
which can be obtained using Chocolatey or may be manually installed.

CMake tooling only has partial support for building with external LLVM
library, and no support for the internal LLVM (to be removed in the
future). I.e. The CMake build currently only supports the bytecode
interpreter.

Many files used include paths relative to the top source directory or
relative to the current project, rather than relative to each build
target. Modern CMake support requires including internal dependency
headers the same way you would external dependency headers (albeit
with "" instead of <>). This meant correcting all header includes to
be relative to the build targets and not relative to the workspace.

For example, ...

```c
include "../libclamav/clamav.h"
include "clamd/clamd_others.h"
```

... becomes:

```c
// libclamav
include "clamav.h"

// clamd
include "clamd_others.h"
```

Fixes header name conflicts by renaming a few of the files.

Converted the "shared" code into a static library, which depends on
libclamav. The ironically named "shared" static library provides
features common to the ClamAV apps which are not required in
libclamav itself and are not intended for use by downstream projects.
This change was required for correct modern CMake practices but was
also required to use the automake "subdir-objects" option.
This eliminates warnings when running autoreconf which, in the next
version of autoconf & automake are likely to break the build.

libclamav used to build in multiple stages where an earlier stage is
a static library containing utils required by the "shared" code.
Linking clamdscan and clamdtop with this libclamav utils static lib
allowed these two apps to function without libclamav. While this is
nice in theory, the practical gains are minimal and it complicates
the build system. As such, the autotools and CMake tooling was
simplified for improved maintainability and this feature was thrown
out. clamdtop and clamdscan now require libclamav to function.

Removed the nopthreads version of the autotools
libclamav_internal_utils static library and added pthread linking to
a couple apps that may have issues building on some platforms without
it, with the intention of removing needless complexity from the
source. Kept the regular version of libclamav_internal_utils.la
though it is no longer used anywhere but in libclamav.

Added an experimental doxygen build option which attempts to build
clamav.h and libfreshclam doxygen html docs.

The CMake build tooling also may build the example program(s), which
isn't a feature in the Autotools build system.

Changed C standard to C90+ due to inline linking issues with socket.h
when linking libfreshclam.so on Linux.

Generate common.rc for win32.

Fix tabs/spaces in shared Makefile.am, and remove vestigial ifndef
from misc.c.

Add CMake files to the automake dist, so users can try the new
CMake tooling w/out having to build from a git clone.

clamonacc changes:
- Renamed FANOTIFY macro to HAVE_SYS_FANOTIFY_H to better match other
  similar macros.
- Added a new clamav-clamonacc.service systemd unit file, based on
  the work of ChadDevOps & Aaron Brighton.
- Added missing clamonacc man page.

Updates to clamdscan man page, add missing options.

Remove vestigial CL_NOLIBCLAMAV definitions (all apps now use
libclamav).

Rename Windows mspack.dll to libmspack.dll so all ClamAV-built
libraries have the lib-prefix with Visual Studio as with CMake.
2020-08-13 00:25:34 -07:00

246 lines
6.3 KiB
C

/* This file is part of libmspack.
* (C) 2003-2004 Stuart Caie.
*
* libmspack is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License (LGPL) version 2.1
*
* For further details, see the file COPYING.LIB distributed with libmspack
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "system.h"
#if !LARGEFILE_SUPPORT
const char *largefile_msg = "library not compiled to support large files.";
#endif
int mspack_version(int entity) {
switch (entity) {
/* CHM decoder version 1 -> 2 changes:
* - added mschmd_sec_mscompressed::spaninfo
* - added mschmd_header::first_pmgl
* - added mschmd_header::last_pmgl
* - added mschmd_header::chunk_cache;
*/
case MSPACK_VER_MSCHMD:
/* CAB decoder version 1 -> 2 changes:
* - added MSCABD_PARAM_SALVAGE
*/
case MSPACK_VER_MSCABD:
/* OAB decoder version 1 -> 2 changes:
* - added msoab_decompressor::set_param and MSOABD_PARAM_DECOMPBUF
*/
case MSPACK_VER_MSOABD:
return 2;
case MSPACK_VER_LIBRARY:
case MSPACK_VER_SYSTEM:
case MSPACK_VER_MSSZDDD:
case MSPACK_VER_MSKWAJD:
return 1;
case MSPACK_VER_MSCABC:
case MSPACK_VER_MSCHMC:
case MSPACK_VER_MSLITD:
case MSPACK_VER_MSLITC:
case MSPACK_VER_MSHLPD:
case MSPACK_VER_MSHLPC:
case MSPACK_VER_MSSZDDC:
case MSPACK_VER_MSKWAJC:
case MSPACK_VER_MSOABC:
return 0;
}
return -1;
}
int mspack_sys_selftest_internal(int offt_size) {
return (sizeof(off_t) == offt_size) ? MSPACK_ERR_OK : MSPACK_ERR_SEEK;
}
/* validates a system structure */
int mspack_valid_system(struct mspack_system *sys) {
return (sys != NULL) && (sys->open != NULL) && (sys->close != NULL) &&
(sys->read != NULL) && (sys->write != NULL) && (sys->seek != NULL) &&
(sys->tell != NULL) && (sys->message != NULL) && (sys->alloc != NULL) &&
(sys->free != NULL) && (sys->copy != NULL) && (sys->null_ptr == NULL);
}
/* returns the length of a file opened for reading */
int mspack_sys_filelen(struct mspack_system *system,
struct mspack_file *file, off_t *length)
{
off_t current;
if (!system || !file || !length) return MSPACK_ERR_OPEN;
/* get current offset */
current = system->tell(file);
/* seek to end of file */
if (system->seek(file, (off_t) 0, MSPACK_SYS_SEEK_END)) {
return MSPACK_ERR_SEEK;
}
/* get offset of end of file */
*length = system->tell(file);
/* seek back to original offset */
if (system->seek(file, current, MSPACK_SYS_SEEK_START)) {
return MSPACK_ERR_SEEK;
}
return MSPACK_ERR_OK;
}
/* definition of mspack_default_system -- if the library is compiled with
* MSPACK_NO_DEFAULT_SYSTEM, no default system will be provided. Otherwise,
* an appropriate default system (e.g. the standard C library, or some native
* API calls)
*/
#ifdef MSPACK_NO_DEFAULT_SYSTEM
struct mspack_system *mspack_default_system = NULL;
#else
/* implementation of mspack_default_system for standard C library */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
struct mspack_file_p {
FILE *fh;
const char *name;
};
static struct mspack_file *msp_open(struct mspack_system *self,
const char *filename, int mode)
{
struct mspack_file_p *fh;
const char *fmode;
switch (mode) {
case MSPACK_SYS_OPEN_READ: fmode = "rb"; break;
case MSPACK_SYS_OPEN_WRITE: fmode = "wb"; break;
case MSPACK_SYS_OPEN_UPDATE: fmode = "r+b"; break;
case MSPACK_SYS_OPEN_APPEND: fmode = "ab"; break;
default: return NULL;
}
if ((fh = (struct mspack_file_p *) malloc(sizeof(struct mspack_file_p)))) {
fh->name = filename;
if ((fh->fh = fopen(filename, fmode))) return (struct mspack_file *) fh;
free(fh);
}
return NULL;
}
static void msp_close(struct mspack_file *file) {
struct mspack_file_p *self = (struct mspack_file_p *) file;
if (self) {
fclose(self->fh);
free(self);
}
}
static int msp_read(struct mspack_file *file, void *buffer, int bytes) {
struct mspack_file_p *self = (struct mspack_file_p *) file;
if (self && buffer && bytes >= 0) {
size_t count = fread(buffer, 1, (size_t) bytes, self->fh);
if (!ferror(self->fh)) return (int) count;
}
return -1;
}
static int msp_write(struct mspack_file *file, void *buffer, int bytes) {
struct mspack_file_p *self = (struct mspack_file_p *) file;
if (self && buffer && bytes >= 0) {
size_t count = fwrite(buffer, 1, (size_t) bytes, self->fh);
if (!ferror(self->fh)) return (int) count;
}
return -1;
}
static int msp_seek(struct mspack_file *file, off_t offset, int mode) {
struct mspack_file_p *self = (struct mspack_file_p *) file;
if (self) {
switch (mode) {
case MSPACK_SYS_SEEK_START: mode = SEEK_SET; break;
case MSPACK_SYS_SEEK_CUR: mode = SEEK_CUR; break;
case MSPACK_SYS_SEEK_END: mode = SEEK_END; break;
default: return -1;
}
#if HAVE_FSEEKO
return fseeko(self->fh, offset, mode);
#else
return fseek(self->fh, offset, mode);
#endif
}
return -1;
}
static off_t msp_tell(struct mspack_file *file) {
struct mspack_file_p *self = (struct mspack_file_p *) file;
#if HAVE_FSEEKO
return (self) ? (off_t) ftello(self->fh) : 0;
#else
return (self) ? (off_t) ftell(self->fh) : 0;
#endif
}
static void msp_msg(struct mspack_file *file, const char *format, ...) {
va_list ap;
if (file) fprintf(stderr, "%s: ", ((struct mspack_file_p *) file)->name);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fputc((int) '\n', stderr);
fflush(stderr);
}
static void *msp_alloc(struct mspack_system *self, size_t bytes) {
#if DEBUG
/* make uninitialised data obvious */
char *buf = malloc(bytes + 8);
if (buf) memset(buf, 0xDC, bytes);
*((size_t *)buf) = bytes;
return &buf[8];
#else
return malloc(bytes);
#endif
}
static void msp_free(void *buffer) {
#if DEBUG
char *buf = buffer;
size_t bytes;
if (buf) {
buf -= 8;
bytes = *((size_t *)buf);
/* make freed data obvious */
memset(buf, 0xED, bytes);
free(buf);
}
#else
free(buffer);
#endif
}
static void msp_copy(void *src, void *dest, size_t bytes) {
memcpy(dest, src, bytes);
}
static struct mspack_system msp_system = {
&msp_open, &msp_close, &msp_read, &msp_write, &msp_seek,
&msp_tell, &msp_msg, &msp_alloc, &msp_free, &msp_copy, NULL
};
struct mspack_system *mspack_default_system = &msp_system;
#endif