/**
* VTF3 - Vampir Trace Format handling 3.
*
* Copyright (c) 1999-2004 TU Dresden
* Center for High Performance Computing (ZHR)
*
* @name vtf3zlib.c
*
* @version $Revision: 1.2 $
* $Date: 2005/10/22 00:57:03 $
* $Author: sameer $
*
* @author Stephan Seidl (ZHR)
*/
/*****************************************************************************/
#include
#include
#include
/* This is for `size_t'. */
#include
#include "vtf3.h"
/*****************************************************************************/
/* Note for VAMPIR, VPTMERGE and other applications.
To handle `*.gz' streams as input or as output,
the VTF3 library needs ZLIB support. */
#if (0)
#ifndef VTF3_WITHOUT_ZLIB
#define VTF3_WITHOUT_ZLIB
#endif
#endif
/*****************************************************************************/
#if (!defined (VTF3_WITHOUT_ZLIB))
/*****************************************************************************/
#include "zlib.h"
int
VTF3_HaveZlib (void)
{
return (1);
}
const char *
VTF3_GetZlibVersion (void)
{
static char hexlower [16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
static unsigned int versionnum = (ZLIB_VERNUM);
static char versionstring [8];
*(versionstring + 0) = hexlower[versionnum >> 12 & (unsigned int) 0xf];
*(versionstring + 1) = '.';
*(versionstring + 2) = hexlower[versionnum >> 8 & (unsigned int) 0xf];
*(versionstring + 3) = '.';
*(versionstring + 4) = hexlower[versionnum >> 4 & (unsigned int) 0xf];
*(versionstring + 5) = '.';
*(versionstring + 6) = hexlower[versionnum >> 0 & (unsigned int) 0xf];
*(versionstring + 7) = '\0';
return (&versionstring[0]);
}
int
VTF3_GzRead (FILE **vtf_ppfile, void **vtf_pzfile, void *vtf_buffer,
size_t vtf_size, size_t *vtf_zfileposition)
{
int vtf_bytes;
long int vtf_filepos;
if (vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzRead(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (vtf_pzfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzRead(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (*vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzRead(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (*vtf_pzfile == 0)
if ((*vtf_pzfile = gzalloc (*vtf_ppfile, "rb")) == 0)
return (-2);
if (vtf_size != 0)
vtf_bytes = gzread (*vtf_pzfile, vtf_buffer, (unsigned int) vtf_size);
else
vtf_bytes = 0;
if (vtf_bytes > (int) vtf_size)
vtf_bytes = -1;
else if (vtf_bytes < 0)
vtf_bytes = -1;
if (vtf_bytes < 0)
goto vtf_CloseFile;
if ((vtf_filepos = (long int) ftell (*vtf_ppfile)) < 0) {
vtf_bytes = -1;
goto vtf_CloseFile;
}
*vtf_zfileposition = (size_t) vtf_filepos;
if (vtf_bytes == 0)
goto vtf_CloseFile;
if (vtf_bytes < (int) vtf_size)
goto vtf_CloseFile;
return (vtf_bytes);
/* LABEL */ vtf_CloseFile:
if (gzclose (*vtf_pzfile) != 0) {
if (vtf_bytes >= 0)
vtf_bytes = -1;
}
*vtf_pzfile = 0;
*vtf_ppfile = 0;
return (vtf_bytes);
}
int
VTF3_GzWrite (FILE **vtf_ppfile, void **vtf_pzfile, void *vtf_buffer,
size_t vtf_size)
{
int vtf_bytes;
if (vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzWrite(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (vtf_pzfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzWrite(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (*vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzWrite(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (*vtf_pzfile == 0)
/* Really, using fast mode `1' is a good idea here. */
if ((*vtf_pzfile = gzalloc (*vtf_ppfile, "wb1")) == 0)
return (-2);
if (vtf_size != 0)
vtf_bytes = gzwrite (*vtf_pzfile, vtf_buffer, (unsigned int) vtf_size);
else
vtf_bytes = 0;
if (vtf_bytes != (int) vtf_size)
vtf_bytes = -1;
if (vtf_bytes < 1)
goto vtf_CloseFile;
return (vtf_bytes);
/* LABEL */ vtf_CloseFile:
if (gzclose (*vtf_pzfile) != 0) {
if (vtf_bytes >= 0)
vtf_bytes = -1;
}
*vtf_pzfile = 0;
*vtf_ppfile = 0;
return (vtf_bytes);
}
/*****************************************************************************/
#else
/*****************************************************************************/
int
VTF3_HaveZlib (void)
{
return (0);
}
const char *
VTF3_GetZlibVersion (void)
{
static const char *versionstring = (const char *) "";
return (versionstring);
}
int
VTF3_GzRead (FILE **vtf_ppfile, void **vtf_pzfile, void *vtf_buffer,
size_t vtf_size, size_t *vtf_zfileposition)
{
size_t vtf_bytes;
int vtf_retval;
long int vtf_filepos;
if (vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzRead(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (vtf_pzfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzRead(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (*vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzRead(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (vtf_size != 0)
vtf_bytes = fread (vtf_buffer, 1, vtf_size, *vtf_ppfile);
else
vtf_bytes = 0;
if (vtf_bytes == vtf_size)
vtf_retval = (int) vtf_bytes;
else if (vtf_bytes > vtf_size)
vtf_retval = -1;
else if (ferror (*vtf_ppfile) != 0)
vtf_retval = -1;
else if (feof (*vtf_ppfile) == 0)
vtf_retval = -1;
else
vtf_retval = (int) vtf_bytes;
if (vtf_retval < 0)
goto vtf_CloseFile;
if ((vtf_filepos = (long int) ftell (*vtf_ppfile)) < 0) {
vtf_retval = -1;
goto vtf_CloseFile;
}
*vtf_zfileposition = (size_t) vtf_filepos;
if (vtf_retval == 0)
goto vtf_CloseFile;
if (vtf_retval < (int) vtf_size)
goto vtf_CloseFile;
return (vtf_retval);
/* LABEL */ vtf_CloseFile:
if (fclose (*vtf_ppfile) != 0) {
if (vtf_retval >= 0)
vtf_retval = -1;
}
*vtf_ppfile = 0;
return (vtf_retval);
}
int
VTF3_GzWrite (FILE **vtf_ppfile, void **vtf_pzfile, void *vtf_buffer,
size_t vtf_size)
{
int vtf_retval;
if (vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzWrite(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (vtf_pzfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzWrite(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (*vtf_ppfile == 0) {
(void) fprintf (stderr, "VTF3: VTF3_GzWrite(): Unrecoverable error\n");
(void) fflush (stderr);
(void) exit (127);
}
if (vtf_size == 0)
vtf_retval = 0;
else if (fwrite (vtf_buffer, 1, vtf_size, *vtf_ppfile) != vtf_size)
vtf_retval = -1;
else
vtf_retval = (int) vtf_size;
if (vtf_retval < 1)
goto vtf_CloseFile;
return (vtf_retval);
/* LABEL */ vtf_CloseFile:
if (fclose (*vtf_ppfile) != 0) {
if (vtf_retval >= 0)
vtf_retval = -1;
}
*vtf_ppfile = 0;
return (vtf_retval);
}
/*****************************************************************************/
#endif
/*****************************************************************************/
/* {{{ HISTORY */
/*
*
* $Log: vtf3zlib.c,v $
* Revision 1.2 2005/10/22 00:57:03 sameer
* committing source of vtf3 1.43.
*
* Revision 1.10 2004/03/01 23:29:13 seidl
* Changed the ZLIB open mechanism to simplify porting to far-off lands.
* The code does no longer take advantage of obscure OS features, hence,
* it should properly run now under Windows, too.
* Pay attention, the new interface `gzalloc()', being used now, is not
* part of the official ZLIB, it comes with vtf3-zlib-1.2.1.f only.
*
* Revision 1.9 2003/11/25 18:21:04 seidl
* Cosmetic change.
*
* Revision 1.8 2003/11/24 23:54:10 seidl
* A small modification for me for g++-3.3.
*
* Revision 1.7 2003/11/24 00:12:23 seidl
* Copyright notice updated.
*
* Revision 1.6 2002/03/02 22:05:53 seidl
* Surplus blanks removed.
*
* Revision 1.5 2002/02/28 16:47:04 seidl
* Copyright year changed.
*
* Revision 1.4 2002/01/22 14:20:41 seidl
* New interface VTF3_HaveZlib implemented.
*
* Revision 1.3 2001/09/19 08:33:23 seidl
* Globally changed the prefix from `VTF_' to `VTF3_'.
*
* Revision 1.2 2001/08/13 13:36:06 seidl
* Some cosmetic changes.
*
* Revision 1.1 2001/08/13 13:32:01 seidl
* Z-stream support for `*.gz' files.
* The interfaces using `zlib-1.1.3' functions.
*
*/
/* }}} */
/*****************************************************************************/