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 294 295 296 297 298 299 300
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
#include "glue/bzip2.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#else /* No config.h? Hmm. Assume the zlib library is available for linking. */
#define BZ2GLUE_ASSUME_BZIP2 1
#endif /* !HAVE_CONFIG_H */
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef HAVE_BZIP2 /* In case we're _not_ doing runtime linking. */
#define BZGLUE_ASSUME_BZIP2 1
#include <bzlib.h>
#endif /* HAVE_BZIP2 */
#include <Inventor/C/basic.h>
#include <Inventor/C/errors/debugerror.h>
#include <Inventor/C/tidbits.h>
#include <Inventor/C/glue/dl.h>
#include "threads/threadsutilp.h"
#include "tidbitsp.h"
typedef const char * (*cc_bzglue_BZ2_bzlibVersion_t)(void);
typedef void * (*cc_bzglue_BZ2_bzReadOpen_t)(int * bzerror,
FILE * f,
int verbosity,
int bzsmall,
void * unused,
int nunused);
typedef void (*cc_bzglue_BZ2_bzReadClose_t)(int * bzerror,
void * bzfile);
typedef int (*cc_bzglue_BZ2_bzRead_t)(int * bzerror,
void * bzfile,
void * buf,
int len);
typedef void * (*cc_bzglue_BZ2_bzWriteOpen_t)(int * bzerror,
FILE * fp,
int blocksize100k,
int verbosity,
int workfactor);
typedef void (*cc_bzglue_BZ2_bzWriteClose_t)(int * bzerror,
void * bzfile,
int abandon,
unsigned int * nbytesin,
unsigned int * nbytesout);
typedef void (*cc_bzglue_BZ2_bzWrite_t)(int * bzerror,
void * bzfile,
void * buf,
int len);
typedef struct {
int available;
cc_bzglue_BZ2_bzlibVersion_t BZ2_bzlibVersion;
cc_bzglue_BZ2_bzReadOpen_t BZ2_bzReadOpen;
cc_bzglue_BZ2_bzReadClose_t BZ2_bzReadClose;
cc_bzglue_BZ2_bzRead_t BZ2_bzRead;
cc_bzglue_BZ2_bzWriteOpen_t BZ2_bzWriteOpen;
cc_bzglue_BZ2_bzWriteClose_t BZ2_bzWriteClose;
cc_bzglue_BZ2_bzWrite_t BZ2_bzWrite;
} cc_bzglue_t;
static cc_bzglue_t * bzlib_instance = NULL;
static cc_libhandle bzlib_libhandle = NULL;
static int bzlib_failed_to_load = 0;
/* Cleans up at exit. */
static void
bzglue_cleanup(void)
{
#ifdef LIBBZIP2_RUNTIME_LINKING
if (bzlib_libhandle) {
cc_dl_close(bzlib_libhandle);
bzlib_libhandle = NULL;
}
#endif /* LIBBZIP2_RUNTIME_LINKING */
assert(bzlib_instance);
free(bzlib_instance);
bzlib_instance = NULL;
bzlib_failed_to_load = 0;
}
static const cc_bzglue_t *
bzglue_init(void)
{
CC_SYNC_BEGIN(bzglue_init);
if (!bzlib_instance && !bzlib_failed_to_load) {
/* First invocation, do initializations. */
cc_bzglue_t * bi = (cc_bzglue_t *)malloc(sizeof(cc_bzglue_t));
(void)coin_atexit((coin_atexit_f *)bzglue_cleanup, CC_ATEXIT_DYNLIBS);
/* The common case is that libbz2 is either available from the
linking process or we're successfully going to link it in. */
bi->available = 1;
#ifdef LIBBZIP2_RUNTIME_LINKING
{
int idx;
/* FIXME: should we get the system shared library name from an
Autoconf check? 20000930 mortene. */
const char * possiblelibnames[] = {
NULL, /* is set below */
"bz2", "libbz2", "libbz2.so",
"libbz2.dylib",
NULL
};
possiblelibnames[0] = coin_getenv("COIN_BZIP2_LIBNAME");
idx = possiblelibnames[0] ? 0 : 1;
while (!bzlib_libhandle && possiblelibnames[idx]) {
bzlib_libhandle = cc_dl_open(possiblelibnames[idx]);
idx++;
}
if (!bzlib_libhandle) {
bi->available = 0;
bzlib_failed_to_load = 1;
}
}
/* Define BZGLUE_REGISTER_FUNC macro. Casting the type is
necessary for this file to be compatible with C++ compilers. */
#define BZGLUE_REGISTER_FUNC(_funcsig_, _funcname_) \
do { \
bi->_funcname_ = (_funcsig_)cc_dl_sym(bzlib_libhandle, SO__QUOTE(_funcname_)); \
if (bi->_funcname_ == NULL) bi->available = 0; \
} while (0)
#elif defined(BZGLUE_ASSUME_BZIP2) /* !LIBBZIP2_RUNTIME_LINKING */
/* Define BZGLUE_REGISTER_FUNC macro. */
#define BZGLUE_REGISTER_FUNC(_funcsig_, _funcname_) \
bi->_funcname_ = (_funcsig_)_funcname_
#else /* !BZGLUE_ASSUME_BZIP2 */
bi->available = 0;
/* Define BZGLUE_REGISTER_FUNC macro. */
#define BZGLUE_REGISTER_FUNC(_funcsig_, _funcname_) \
bi->_funcname_ = NULL
#endif /* !BZGLUE_ASSUME_BZIP2 */
if (bi->available) {
BZGLUE_REGISTER_FUNC(cc_bzglue_BZ2_bzlibVersion_t, BZ2_bzlibVersion);
}
if (!bi->available || !bi->BZ2_bzlibVersion) {
if (!bi->available) {
cc_debugerror_post("libbzip2 glue",
"Unable to load bzip2 DLL/shared object.");
}
else {
/* something is seriously wrong */
cc_debugerror_post("libbzip2 glue",
"Loaded bzip2 DLL ok, but couldn't resolve symbol "
"BZ2_bzlibVersion().");
}
bi->available = 0;
bzlib_failed_to_load = 1;
bzlib_instance = bi;
}
else {
int major, minor, patch;
if (!coin_parse_versionstring(bi->BZ2_bzlibVersion(), &major, &minor, &patch) ||
(major < 1)) {
cc_debugerror_post("bzip2 glue",
"Loaded bzip2 DLL ok, but version >= 1.0.0 is needed.");
bi->available = 0;
bzlib_failed_to_load = 1;
bzlib_instance = bi;
}
else {
BZGLUE_REGISTER_FUNC(cc_bzglue_BZ2_bzReadOpen_t, BZ2_bzReadOpen);
BZGLUE_REGISTER_FUNC(cc_bzglue_BZ2_bzReadClose_t, BZ2_bzReadClose);
BZGLUE_REGISTER_FUNC(cc_bzglue_BZ2_bzRead_t, BZ2_bzRead);
BZGLUE_REGISTER_FUNC(cc_bzglue_BZ2_bzWriteOpen_t, BZ2_bzWriteOpen);
BZGLUE_REGISTER_FUNC(cc_bzglue_BZ2_bzWriteClose_t, BZ2_bzWriteClose);
BZGLUE_REGISTER_FUNC(cc_bzglue_BZ2_bzWrite_t, BZ2_bzWrite);
/* Do this late, so we can detect recursive calls to this function. */
bzlib_instance = bi;
}
}
}
CC_SYNC_END(bzglue_init);
return bzlib_instance;
}
int
cc_bzglue_available(void)
{
bzglue_init();
return bzlib_instance && bzlib_instance->available;
}
const char *
cc_bzglue_BZ2_bzlibVersion(void)
{
bzglue_init();
return bzlib_instance->BZ2_bzlibVersion();
}
void *
cc_bzglue_BZ2_bzReadOpen(int * bzerror,
FILE * f,
int verbosity,
int bzsmall,
void * unused,
int nunused)
{
bzglue_init();
return bzlib_instance->BZ2_bzReadOpen(bzerror,
f,
verbosity,
bzsmall,
unused,
nunused);
}
void
cc_bzglue_BZ2_bzReadClose(int * bzerror,
void * bzfile)
{
bzglue_init();
bzlib_instance->BZ2_bzReadClose(bzerror, bzfile);
}
int
cc_bzglue_BZ2_bzRead(int * bzerror,
void * bzfile,
void * buf,
int len)
{
bzglue_init();
return bzlib_instance->BZ2_bzRead(bzerror, bzfile, buf, len);
}
void *
cc_bzglue_BZ2_bzWriteOpen(int * bzerror,
FILE * fp,
int blocksize100k,
int verbosity,
int workfactor)
{
bzglue_init();
return bzlib_instance->BZ2_bzWriteOpen(bzerror, fp, blocksize100k,
verbosity, workfactor);
}
void
cc_bzglue_BZ2_bzWriteClose(int * bzerror,
void * bzfile,
int abandon,
unsigned int * nbytesin,
unsigned int * nbytesout)
{
bzglue_init();
bzlib_instance->BZ2_bzWriteClose(bzerror, bzfile,
abandon, nbytesin, nbytesout);
}
void
cc_bzglue_BZ2_bzWrite(int * bzerror,
void * bzfile,
void * buf,
int len)
{
bzglue_init();
bzlib_instance->BZ2_bzWrite(bzerror, bzfile, buf, len);
}
|