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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
/**************************************************************************\
*
* 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 "io/SoInput_Reader.h"
#include <string.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#ifdef HAVE_UNISTD_H
#include <unistd.h> // dup()
#endif // HAVE_UNISTD_H
#ifdef HAVE_IO_H
#include <io.h> // Win32 dup()
#endif // HAVE_IO_H
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <Inventor/errors/SoDebugError.h>
#include "io/gzmemio.h"
#include "glue/zlib.h"
#include "glue/bzip2.h"
// We don't want to include bzlib.h, so we just define the constants
// we use here
#ifndef BZ_OK
#define BZ_OK 0
#endif // BZ_OK
#ifndef BZ_STREAM_END
#define BZ_STREAM_END 4
#endif // BZ_STREAM_END
//
// abstract class
//
SoInput_Reader::SoInput_Reader(void)
: dummyname("")
{
}
SoInput_Reader::~SoInput_Reader()
{
}
const SbString &
SoInput_Reader::getFilename(void)
{
return this->dummyname;
}
FILE *
SoInput_Reader::getFilePointer(void)
{
return NULL;
}
// creates the correct reader based on the file type in fp (will
// examine the file header). If fullname is empty, it's assumed that
// file FILE pointer is passed from the user, and that we cannot
// necessarily find the file handle.
SoInput_Reader *
SoInput_Reader::createReader(FILE * fp, const SbString & fullname)
{
SoInput_Reader * reader = NULL;
SbBool trycompression = FALSE;
#ifdef HAVE_FSTAT
// need to make sure stream is seekable to enable compression
// support, because we need to fseek() the stream
int fn = fileno(fp);
struct stat sb;
if ( fstat(fn, &sb) == 0 ) {
if ( sb.st_mode & S_IFREG ) { // regular file
trycompression = TRUE;
}
}
#endif // HAVE_FSTAT
if ( trycompression ) {
static const size_t HEADER_SIZE = 4;
unsigned char header[HEADER_SIZE];
long offset = ftell(fp);
SbBool valid_header = TRUE;
if (fread(header, 1, HEADER_SIZE, fp)<HEADER_SIZE) {
valid_header = FALSE;
}
(void) fseek(fp, offset, SEEK_SET);
fflush(fp); // needed since we fetch the file descriptor later
if (valid_header && header[0] == 'B' && header[1] == 'Z') {
if (!cc_bzglue_available()) {
SoDebugError::postWarning("SoInput_Reader::createReader",
"File seems to be in bzip2 format, but "
"libbz2 support is not available.");
}
else {
int bzerror = BZ_OK;
void * bzfp = cc_bzglue_BZ2_bzReadOpen(&bzerror, fp, 0, 0, NULL, 0);
if ((bzerror == BZ_OK) && (bzfp != NULL)) {
reader = new SoInput_BZ2FileReader(fullname.getString(), bzfp);
}
else {
SoDebugError::postWarning("SoInput_Reader::createReader",
"Unable to open bzip2 file.");
}
}
}
if ((reader == NULL) && valid_header &&
(header[0] == 0x1f) &&
(header[1] == 0x8b)) {
if (!cc_zlibglue_available()) {
SoDebugError::postWarning("SoInput_Reader::createReader",
"File seems to be in gzip format, but "
"zlib support is not available.");
}
else {
int fd = fileno(fp);
// need to use dup() if we didn't open the file since gzdclose
// will close it
if (fd >= 0 && fullname.getLength() && fullname != "<stdin>")
fd = dup(fd);
if (fd >= 0) {
void * gzfp = 0;
#ifdef HAVE_GZDOPEN
gzfp = cc_zlibglue_gzdopen(fd, "rb");
#else // gzdopen() after reading from the compressed file does not work on Mac OS X
if (fullname.getLength()) {
gzfp = cc_zlibglue_gzopen(fullname.getString(), "rb");
} else {
SoDebugError::postWarning("SoInput_Reader::createReader",
"Passing FILE* for gzipped files on "
"Mac OS X not allowed, unable to open.");
}
#endif
if (gzfp) {
reader = new SoInput_GZFileReader(fullname.getString(), gzfp);
}
}
else {
SoDebugError::postWarning("SoInput_Reader::createReader",
"Unable to create file descriptor from stream.");
}
}
}
}
if (reader == NULL) {
reader = new SoInput_FileReader(fullname.getString(), fp);
}
return reader;
}
//
// standard FILE * class
//
SoInput_FileReader::SoInput_FileReader(const char * const filenamearg, FILE * filepointer)
{
this->fp = filepointer;
this->filename = filenamearg;
}
SoInput_FileReader::~SoInput_FileReader()
{
// Close files which are not a memory buffer nor the stdin and
// which we do have a filename for (if we don't have a filename,
// the FILE ptr was just passed in through setFilePointer() and
// is the library programmer's responsibility).
if (this->fp &&
(this->filename != "<stdin>") &&
(this->filename.getLength())) {
fclose(this->fp);
}
}
SoInput_Reader::ReaderType
SoInput_FileReader::getType(void) const
{
return REGULAR_FILE;
}
size_t
SoInput_FileReader::readBuffer(char * buf, const size_t readlen)
{
return fread(buf, 1, readlen, this->fp);
}
const SbString &
SoInput_FileReader::getFilename(void)
{
return this->filename;
}
FILE *
SoInput_FileReader::getFilePointer(void)
{
return this->fp;
}
//
// standard membuffer class
//
SoInput_MemBufferReader::SoInput_MemBufferReader(void * bufPointer, size_t bufSize)
{
this->buf = (char*) bufPointer;
this->buflen = bufSize;
this->bufpos = 0;
}
SoInput_MemBufferReader::~SoInput_MemBufferReader()
{
}
SoInput_Reader::ReaderType
SoInput_MemBufferReader::getType(void) const
{
return MEMBUFFER;
}
size_t
SoInput_MemBufferReader::readBuffer(char * buffer, const size_t readlen)
{
size_t len = this->buflen - this->bufpos;
if (len > readlen) len = readlen;
memcpy(buffer, this->buf + this->bufpos, len);
this->bufpos += len;
return len;
}
//
// gzip readers
//
//
// gzipped membuffer class
//
SoInput_GZMemBufferReader::SoInput_GZMemBufferReader(void * bufPointer, size_t bufSize)
{
// FIXME: the bufSize cast (as size_t can be 64 bits wide) is there
// to humour the interface of gzmemio -- should really be fixed
// in the interface instead. 20050525 mortene.
this->gzmfile = cc_gzm_open((uint8_t *)bufPointer, (uint32_t)bufSize);
this->buf = bufPointer;
}
SoInput_GZMemBufferReader::~SoInput_GZMemBufferReader()
{
cc_gzm_close(this->gzmfile);
}
SoInput_Reader::ReaderType
SoInput_GZMemBufferReader::getType(void) const
{
return GZMEMBUFFER;
}
size_t
SoInput_GZMemBufferReader::readBuffer(char * buffer, const size_t readlen)
{
// FIXME: about the cast; see note about the call to cc_gzm_open()
// above. 20050525 mortene.
return cc_gzm_read(this->gzmfile, buffer, (uint32_t)readlen);
}
//
// gzFile class
//
SoInput_GZFileReader::SoInput_GZFileReader(const char * const filenamearg, void * fp)
{
this->gzfp = fp;
this->filename = filenamearg;
}
SoInput_GZFileReader::~SoInput_GZFileReader()
{
assert(this->gzfp);
cc_zlibglue_gzclose(this->gzfp);
}
SoInput_Reader::ReaderType
SoInput_GZFileReader::getType(void) const
{
return GZFILE;
}
size_t
SoInput_GZFileReader::readBuffer(char * buf, const size_t readlen)
{
// FIXME: about the cast; see note about the call to cc_gzm_open()
// above. 20050525 mortene.
int result = cc_zlibglue_gzread(this->gzfp, (void*) buf, (uint32_t)readlen);
// the signature of this this function was changed to return size_t
// without checking that gzread() actually returns a signed
// integer. We need to check for this and not just cast to size_t on
// return
if (result < 0) result = 0; // EOF
return (size_t) result;
}
const SbString &
SoInput_GZFileReader::getFilename(void)
{
return this->filename;
}
//
// bzFile class
//
SoInput_BZ2FileReader::SoInput_BZ2FileReader(const char * const filenamearg, void * fp)
{
this->bzfp = fp;
this->filename = filenamearg;
}
SoInput_BZ2FileReader::~SoInput_BZ2FileReader()
{
if (this->bzfp) {
int bzerror = BZ_OK;
cc_bzglue_BZ2_bzReadClose(&bzerror, this->bzfp);
}
}
SoInput_Reader::ReaderType
SoInput_BZ2FileReader::getType(void) const
{
return BZ2FILE;
}
size_t
SoInput_BZ2FileReader::readBuffer(char * buf, const size_t readlen)
{
if (this->bzfp == NULL) { return 0; }
int bzerror = BZ_OK;
// FIXME: about the cast; see note about the call to cc_gzm_open()
// above. 20050525 mortene.
int ret = cc_bzglue_BZ2_bzRead(&bzerror, this->bzfp,
buf, (uint32_t)readlen);
if ((bzerror != BZ_OK) && (bzerror != BZ_STREAM_END)) {
ret = 0;
cc_bzglue_BZ2_bzReadClose(&bzerror, this->bzfp);
this->bzfp = NULL;
}
// the signature of this this function was changed to return size_t
// without checking that bzRead() actually returns a signed
// integer. We need to check for this and not just cast to size_t on
// return.
if (ret < 0) ret = 0; // might not be necessary, but will catch other errors
return (size_t) ret;
}
const SbString &
SoInput_BZ2FileReader::getFilename(void)
{
return this->filename;
}
#undef BZ_OK
#undef BZ_STREAM_END
|